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' );
}
2025-09-29 17:18:32 +00:00
function getInvoices ( $source , $date , $type ) {
2025-05-12 15:44:39 +00:00
$db = $GLOBALS [ 'db' ];
$date .= '%' ;
2025-09-29 17:18:32 +00:00
$query = " SELECT
i . id ,
i . document_no ,
i . type ,
i . register_date ,
i . sell_date ,
i . origin ,
i . order_no ,
i . parent_name ,
i . parent_nip ,
i . parent_address_city ,
i . parent_address_postalcode ,
i . parent_address_street ,
i . parent_address_country ,
i . parent_address_country_code ,
i . currency ,
i . total_netto ,
i . total_brutto ,
i . total_vat ,
i . url ,
i . corrected_invoice_id ,
i . ecmstockdocout_id ,
i . ecmstockdoccorrect_id ,
i . ecmsale_id ,
i . series_id ,
wz . document_no AS wz_document_no ,
wz . id AS wz_id ,
p . id as product_id ,
p . code as product_code ,
p . name as product_name ,
ip . quantity ,
ip . price_netto ,
ip . price_brutto ,
ip . price_vat ,
ip . vat_value ,
ip . code as ecommerce_code
FROM ecommerce_invoices AS i
LEFT JOIN ecmstockdocouts AS wz ON wz . id = i . ecmstockdocout_id
LEFT JOIN ecommerce_invoices_products AS ip ON i . id = ip . invoice_id
LEFT JOIN ecmproducts AS p ON ip . ecmproduct_id = p . id
WHERE i . register_date LIKE '$date' " ;
2025-06-05 20:02:44 +00:00
if ( $source != '' ) {
if ( $source == 'baselinker' ) {
$query .= " AND i.origin IN ('allegro', 'shop') " ;
2025-09-29 17:18:32 +00:00
} else if ( $source == 'apilo' ) {
$query .= " AND i.origin LIKE 'apilo%' " ;
2025-06-05 20:02:44 +00:00
} else {
2025-09-29 17:18:32 +00:00
$query .= " AND i.origin = ' " . $db -> quote ( $source ) . " ' " ;
2025-06-05 20:02:44 +00:00
}
}
2025-09-29 17:18:32 +00:00
2025-06-05 20:02:44 +00:00
if ( $type != '' ) {
2025-09-29 17:18:32 +00:00
$query .= " AND i.type = ' " . $db -> quote ( $type ) . " ' " ;
2025-05-12 15:44:39 +00:00
}
2025-09-29 17:18:32 +00:00
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 ();
2025-09-29 17:18:32 +00:00
2025-05-12 15:44:39 +00:00
while ( $row = $db -> fetchByAssoc ( $result )) {
2025-09-29 17:18:32 +00:00
if ( ! isset ( $invoices [ $row [ 'id' ]])) {
$invoices [ $row [ 'id' ]] = array (
'id' => $row [ 'id' ],
'document_no' => $row [ 'document_no' ],
'type' => $row [ 'type' ],
'register_date' => date ( 'd.m.Y' , strtotime ( $row [ 'register_date' ])),
'sell_date' => date ( 'd.m.Y' , strtotime ( $row [ 'sell_date' ])),
'origin' => $row [ 'origin' ],
'order_no' => $row [ 'order_no' ],
'parent_name' => $row [ 'parent_name' ],
'parent_nip' => $row [ 'parent_nip' ],
'parent_address_city' => $row [ 'parent_address_city' ],
'parent_address_postalcode' => $row [ 'parent_address_postalcode' ],
'parent_address_street' => $row [ 'parent_address_street' ],
'parent_address_country' => $row [ 'parent_address_country' ],
'parent_address_country_code' => $row [ 'parent_address_country_code' ],
'currency' => $row [ 'currency' ],
'total_netto' => $row [ 'total_netto' ],
'total_brutto' => $row [ 'total_brutto' ],
'total_vat' => $row [ 'total_vat' ],
'url' => $row [ 'url' ],
'corrected_invoice_id' => $row [ 'corrected_invoice_id' ],
'ecmstockdocout_id' => $row [ 'ecmstockdocout_id' ],
'ecmstockdoccorrect_id' => $row [ 'ecmstockdoccorrect_id' ],
'ecmsale_id' => $row [ 'ecmsale_id' ],
'series_id' => $row [ 'series_id' ],
'wz_document_no' => $row [ 'wz_document_no' ],
'wz_id' => $row [ 'wz_id' ],
'products' => array (),
'sum_by_products' => 0
);
}
if ( $row [ 'product_id' ]) {
$invoices [ $row [ 'id' ]][ 'products' ][] = array (
'id' => $row [ 'product_id' ],
'code' => $row [ 'product_code' ],
'name' => $row [ 'product_name' ],
'price_netto' => $row [ 'price_netto' ],
'price_brutto' => $row [ 'price_brutto' ],
'quantity' => $row [ 'quantity' ],
'ecommerce_code' => $row [ 'ecommerce_code' ],
'vat_value' => str_replace ( " .00 " , " " , $row [ 'vat_value' ])
);
$invoices [ $row [ 'id' ]][ 'sum_by_products' ] += ( $row [ 'price_netto' ] * $row [ 'quantity' ]);
}
2025-05-12 15:44:39 +00:00
}
2025-09-29 17:18:32 +00:00
return array_values ( $invoices );
2025-05-12 15:44:39 +00:00
}
2025-09-25 21:50:59 +02:00
function getInvoiceSumByProducts ( $invoiceId ) {
$db = $GLOBALS [ 'db' ];
$query = sprintf ( " SELECT SUM(ip.price_netto * ip.quantity) as sum FROM ecommerce_invoices_products as ip WHERE ip.invoice_id='%s' " , $invoiceId );
$result = $db -> query ( $query );
$row = $db -> fetchByAssoc ( $result );
return $row [ 'sum' ];
}
2025-05-12 15:44:39 +00:00
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 ();
2025-06-05 20:02:44 +00:00
$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' ];
}
2025-09-29 17:18:32 +00:00
$sources [] = 'apilo' ;
2025-06-05 20:02:44 +00:00
$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' ) {
2025-08-21 18:12:57 +00:00
$i [ 'category' ] = 'WDT' ;
2025-08-05 13:52:35 +00:00
$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' ) {
2025-06-05 20:02:44 +00:00
$result = $smarty -> fetch ( getcwd () . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/rewizor.tpl' );
2025-05-12 15:44:39 +00:00
} else {
2025-06-05 20:02:44 +00:00
$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;
2025-06-05 20:02:44 +00:00
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>' ;
}