Files
crm.twinpol.com/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/ecommerceInvoicesListView.php

197 lines
7.5 KiB
PHP
Raw Normal View History

2025-05-12 15:44:39 +00:00
<?php
$db = $GLOBALS['db'];
global $app_list_strings;
if (!isset($_GET['source'])) {
$_GET['source'] = '';
}
if (!isset($_GET['date'])) {
$_GET['date'] = date('Y-m', strtotime('-1 month'));
}
if (!isset($_GET['type'])) {
$_GET['type'] = '';
}
if (isset($_GET['ajax'])) {
switch ($_GET['ajax']) {
case 'exportToRewizor':
exportToRewizor($_GET['source'], $_GET['date'], $_GET['type']);
break;
}
} else {
show();
}
function show()
{
$smarty = new Sugar_Smarty();
$inv = getInvoices($_GET['source'], $_GET['date'], $_GET['type']);
$total_netto = 0;
$total_brutto = 0;
$total_vat = 0;
foreach ($inv as $invoice) {
$total_netto += $invoice['total_netto'];
$total_brutto += $invoice['total_brutto'];
$total_vat += $invoice['total_vat'];
}
// format totals with 2 decimals
$total_netto = number_format($total_netto, 2, '.', '');
$total_brutto = number_format($total_brutto, 2, '.', '');
$total_vat = number_format($total_vat, 2, '.', '');
$smarty->assign("data", $inv);
$smarty->assign("totalNetto", $total_netto);
$smarty->assign("totalBrutto", $total_brutto);
$smarty->assign("totalVat", $total_vat);
$smarty->assign("sources", getSources());
$smarty->assign("source", $_GET['source']);
$smarty->assign("dates", getDates());
$smarty->assign("date", $_GET['date']);
$smarty->assign("type", $_GET['type']);
echo $smarty->display(getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/ecommerceInvoicesListView.tpl');
}
function getInvoices($source, $date, $type)
{
$db = $GLOBALS['db'];
$date .= '%';
$query = "SELECT i.*, wz.document_no AS wz_document_no, wz.id AS wz_id FROM ecommerce_invoices AS i
LEFT JOIN ecmstockdocouts AS wz on wz.id = i.ecmstockdocout_id
";
$query .= " WHERE i.register_date LIKE '$date'";
if ($source != '') {
if ($source == 'baselinker') {
$query .= " AND i.origin IN ('allegro', 'shop')";
} else {
$query .= " AND i.origin = '$source'";
}
}
if ($type != '') {
$query .= " AND i.type='$type'";
2025-05-12 15:44:39 +00:00
}
$query .= " ORDER BY i.register_date";
2025-07-10 08:04:15 +00:00
2025-05-12 15:44:39 +00:00
$result = $db->query($query);
$invoices = array();
while ($row = $db->fetchByAssoc($result)) {
// format date in row register_date from mysql format into d.m.Y
$row['register_date'] = date('d.m.Y', strtotime($row['register_date']));
$row['sell_date'] = date('d.m.Y', strtotime($row['sell_date']));
$row['products'] = getInvoicProducts($row['id']);
$invoices[] = $row;
}
return $invoices;
}
function getInvoicProducts($invoiceId) {
$db = $GLOBALS['db'];
$query = sprintf("SELECT p.id, p.code, p.name, ip.price_netto, ip.price_brutto, ip.quantity, ip.code as ecommerce_code, ip.vat_value FROM ecommerce_invoices_products as ip INNER JOIN ecmproducts AS p ON ip.ecmproduct_id = p.id WHERE ip.invoice_id='%s'", $invoiceId);
$result = $db->query($query);
$products = array();
while ($row = $db->fetchByAssoc($result)) {
$row['vat_value'] =str_replace(".00","",$row['vat_value']);
$products[] = $row;
}
return $products;
}
function getDates()
{
$dates = array();
$date = new DateTime();
$dates[] = $date->format('Y-m');
while ($date->format('Y-m') != '2024-01') {
$date->modify('-1 month');
$dates[] = $date->format('Y-m');
}
return $dates;
}
function getSources() {
$db = $GLOBALS['db'];
$sources = array();
$res = $db->query("SELECT DISTINCT origin FROM ecommerce_invoices WHERE origin NOT IN ('allegro', 'shop')");
2025-05-12 15:44:39 +00:00
while ($row = $db->fetchByAssoc($res)) {
$sources[]= $row['origin'];
}
$sources[] = 'baselinker';
2025-05-12 15:44:39 +00:00
return $sources;
}
function exportToRewizor($source, $date, $type)
{
$smarty = new Sugar_Smarty();
$invoices = getInvoices($source, $date, $type);
2025-08-05 13:52:35 +00:00
$accounts = array();
2025-05-12 15:44:39 +00:00
foreach ($invoices as &$i) {
$i['register_date'] = date("Ymd000000",strtotime($i['register_date']));
$i['sell_date'] = date("Ymd000000",strtotime($i['sell_date']));
// create 20 characters length hash from parent_name
$i['parent_hash'] = substr(md5($i['parent_name']), 0, 20);
$i['parent_short_name'] = substr($i['parent_name'], 0, 40);
if ($type == 'correcting') {
$db = $GLOBALS['db'];
$fv = $db->fetchByAssoc($db->query(sprintf("SELECT document_no, register_date FROM ecommerce_invoices WHERE id='%s'", $i['corrected_invoice_id'])));
$i['corrected_document_no'] = $fv['document_no'];
$i['corrected_register_date'] = date("Ymd000000",strtotime(date('d.m.Y', strtotime($fv['register_date']))));
}
2025-08-05 13:52:35 +00:00
if ($source == 'amazon b2b') {
$i['category'] = 'Dostawa Wewnątrzwspólnotowa';
$i['parent_country_code'] = substr($i['parent_nip'], 0, 2);
$i['parent_name'] = $i['parent_name'] . ' ' . $i['parent_nip'];
$i['parent_hash'] = $i['parent_nip']; //substr(md5($i['parent_name']), 0, 20);
$i['parent_short_name'] = $i['parent_name']; //substr($i['parent_name'], 0, 40);
} else {
$i['parent_country_code'] = 'PL';
$i['parent_short_name'] = $i['parent_name'];
if (isset($i['parent_nip']) && !empty($r['parent_nip'])) {
$i['parent_name'] = $i['parent_name'] . ' ' . $i['parent_nip'];
$i['parent_hash'] = $i['parent_nip'];
}
$i['category'] = "Sprzedaż";
}
$i['products'] = getInvoicProducts($i['id']);
2025-07-10 08:04:15 +00:00
$i['vats'] = array();
foreach ($i['products'] as $p) {
if (!isset($i['vats'][$p['vat_value']])) {
$i['vats'][$p['vat_value']] = array(
'netto' => 0,
'brutto' => 0,
'vat' => 0,
);
2025-08-05 13:52:35 +00:00
$i['vats'][$p['vat_value']]['vat_value'] = $p['vat_value'];
if ($source == 'amazon b2b') {
$i['vats'][$p['vat_value']]['vat_name'] = 'ue';
} else {
$i['vats'][$p['vat_value']]['vat_name'] = $p['vat_value'];
}
2025-07-10 08:04:15 +00:00
}
$i['vats'][$p['vat_value']]['netto'] += $p['price_netto'] * $p['quantity'];
$i['vats'][$p['vat_value']]['brutto'] += $p['price_brutto'] * $p['quantity'];
$i['vats'][$p['vat_value']]['vat'] += ($p['price_brutto'] - $p['price_netto']) * $p['quantity'];
2025-08-05 13:52:35 +00:00
}
if (!isset($accounts['parent_hash'])) {
$accounts[$i['parent_hash']] = array (
'parent_hash' => $i['parent_hash'],
'parent_name' => $i['parent_name'],
'parent_address_city' => $i['parent_address_city'],
'parent_address_postalcode' => $i['parent_address_postalcode'],
'parent_address_street' => $i['parent_address_street'],
'parent_country_code' => $i['parent_country_code'],
);
2025-07-10 08:04:15 +00:00
}
2025-05-12 15:44:39 +00:00
}
$smarty->assign("data", $invoices);
2025-08-05 13:52:35 +00:00
$smarty->assign("accounts", $accounts);
2025-05-12 15:44:39 +00:00
if ($type == 'normal') {
$result = $smarty->fetch(getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/rewizor.tpl');
2025-05-12 15:44:39 +00:00
} else {
$result = $smarty->fetch(getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/rewizor_fvkor.tpl');
2025-05-12 15:44:39 +00:00
}
2025-08-05 13:52:35 +00:00
//echo $result;
echo mb_convert_encoding($result, 'ISO-8859-2', 'UTF-8');
2025-05-12 15:44:39 +00:00
}
function brecho($var)
{
echo '<pre>';
print_r($var);
echo '</pre>';
}