Current File : /home/users/barii/public_html/finansenl.com.pl/wodki/admin/classes/fv_ajax.php |
<?php
include('../config.php');
require('the_api.php');
header('Content-type: application/json');
if (isset($_POST['type']) && $_POST['type'] == 'addInvoice') {
$sql = "SELECT filename, numer FROM faktury WHERE checkout_id = '".$_POST['checkout_id']."' ORDER BY id DESC LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo json_encode(array('filename' => $row['filename'], 'numer_faktury' => $row['numer']));
} else {
$access_token = file_get_contents('../../api/demo/accessToken'.$_POST['konto']);
$filename = generateInvoice($access_token, $_POST['checkout_id'], $_POST);
$sql_insert = "INSERT INTO faktury (checkout_id, numer, filename, uwagi, kwota_faktury, data_utworzenia) VALUES ('".$_POST['checkout_id']."', '".$filename['numer_faktury']."', '".$filename['filename']."', '".$_POST['uwagi']."', '".$filename['kwota']."', NOW())";
$invoice_id = sendInvoiceToAllegro($access_token, $_POST['checkout_id'], $filename['numer_faktury'], $filename['filename']);
$check_upload = uploadInvoiceToAllegro($access_token, $_POST['checkout_id'], $invoice_id['id'], '/home/users/barii/public_html/finansenl.com.pl/wodki/admin/downloads/faktury/'.$filename['filename'] );
if ($conn->query($sql_insert) === TRUE) {
echo json_encode($filename);
}
}
}
if (isset($_POST['type']) && $_POST['type'] == 'updateInvoice') {
//$mysql->doQuery("DELETE FROM faktury WHERE checkout_id = '".$_POST['checkout_id']."'");
$nazwa_faktury = $_POST['numer_fv'];
$sql = "SELECT filename, numer FROM faktury WHERE checkout_id = '".$_POST['checkout_id']."' ORDER BY id DESC LIMIT 1";
$result = $conn->query($sql);
$access_token = file_get_contents('../../api/demo/accessToken'.$_POST['konto']);
$filename = generateInvoice($access_token, $_POST['checkout_id'], $_POST, $nazwa_faktury);
$mysql->doQuery("UPDATE faktury SET uwagi = '".$_POST['uwagi']."', filename = '".$filename['filename']."' WHERE checkout_id = '".$_POST['checkout_id']."'");
//$sql_insert = "INSERT INTO faktury (checkout_id, numer, filename, uwagi, kwota_faktury, data_utworzenia) VALUES ('".$_POST['checkout_id']."', '".$filename['numer_faktury']."', '".$filename['filename']."', '".$_POST['uwagi']."', '".$filename['kwota']."', NOW())";
$invoice_id = sendInvoiceToAllegro($access_token, $_POST['checkout_id'], $filename['numer_faktury'], $filename['filename']);
$check_upload = uploadInvoiceToAllegro($access_token, $_POST['checkout_id'], $invoice_id['id'], $filename['filename'] );
echo json_encode($filename);
}
if (isset($_POST['type']) && $_POST['type'] == 'getInvoice') {
$row = $mysql->sqlRow("*", "faktury", "WHERE checkout_id = '".$_POST['id']."'");
if ($row) {
echo json_encode(array('url' => 'https://www.etykietyweselne.pl/admin/downloads/faktury/'.$row['filename'], 'filename' => $row['filename']));
} else {
echo json_encode(array('error' => 'No invoice found'));
}
}
if (isset($_POST['id']) && isset($_POST['konto_id'])) {
$access_token = file_get_contents('../../api/demo/accessToken'.$_POST['konto_id']);
$zamowienie = getAllegroCheckoutForm($access_token, $_POST['id']);
$faktura_dane = $zamowienie['invoice'];
$faktura_ulica = $faktura_dane['address']['street'];
$faktura_city = $faktura_dane['address']['city'];
$faktura_zipcode = $faktura_dane['address']['zipCode'];
$faktura_nazwa = $faktura_dane['address']['company']['name'];
$faktura_nip = $faktura_dane['address']['company']['taxId'];
if (strlen($faktura_dane['address']['company']['name']) < 3) {
$faktura_imie = ucfirst($faktura_dane['address']['naturalPerson']['firstName']);
$faktura_nazwisko = ucfirst($faktura_dane['address']['naturalPerson']['lastName']);
$faktura_nazwa = $faktura_imie.' '.$faktura_nazwisko;
}
$koszt_dostawy = $zamowienie['delivery']['cost']['amount'];
$koszt_dostawy_wynik = obliczNettoIVAT($koszt_dostawy);
$metoda_wysylki = $zamowienie['delivery']['method']['name'];
$products = $zamowienie['lineItems'];
$produkty = '<ul class="list-group">';
foreach ($products as $index => $product) {
$i++;
$id_item = $product['id'];
$ilosc = $product['quantity'];
$nazwa = htmlspecialchars($product['offer']['name']);
$cena = $product['price']['amount'];
$suma = number_format($cena * $ilosc, 2);
$produkty.= '<div class="input-group">
<span class="input-group-addon">'.$ilosc.'x </span>
<input type="hidden" name="lineItems['.$index.'][id]" value="'.$id_item.'">
<input type="text" class="form-control" name="lineItems['.$index.'][offer][name]" value="'.$nazwa.'">
<input type="checkbox" name="lineItems['.$product['id'].'][selected]" value="1" checked>
<span class="input-group-addon">'.$suma.'zł</span>
</div>';
}
if (number_format($koszt_dostawy,0) > 0) {
$produkty.= '<li class="list-group-item">';
$produkty.= 'Wysyłka ';
$produkty.= $metoda_wysylki;
$produkty.= ' - ';
$produkty.= number_format($koszt_dostawy,2);
$produkty.= 'zł';
$produkty.= '</li>';
}
$produkty.= '</ul>';
if (isset($_POST['id_update'])) {
$row = $mysql->sqlRow("numer", "faktury", "WHERE checkout_id = '".$_POST['id']."'");
$numer_faktury = $row['numer'];
echo json_encode(array('ulica' => $faktura_ulica, 'city' => $faktura_city, 'zipcode' => $faktura_zipcode, 'nazwa' => $faktura_nazwa, 'nip' => $faktura_nip, 'produkty' => $produkty, 'id' => $_POST['id'], 'konto_id' => $_POST['konto_id'], 'numer_faktury' => $numer_faktury));
} else {
echo json_encode(array('ulica' => $faktura_ulica, 'city' => $faktura_city, 'zipcode' => $faktura_zipcode, 'nazwa' => $faktura_nazwa, 'nip' => $faktura_nip, 'produkty' => $produkty, 'id' => $_POST['id'], 'konto_id' => $_POST['konto_id']));
}
}