fetchByAssoc($GLOBALS[db]->query("select *, TRIM(order_no) as o_no from ecminvoiceoutolds where id='".$id."'"));
$r_wz=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select document_no,so_id,parent_id from ecmstockdocouts where id='".$r['wz_id']."'"));
$r_so=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select register_date,parent_id, parent_shipping_address_name from ecmsales where id='".$r_wz['so_id']."'"));
$r_so_acc=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select iln from accounts where name like '".trim($r_so['parent_shipping_address_name'])."%'"));
if (!$r_so_acc || $r_so_acc['iln']=='') {
$r_so_acc=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select iln from accounts where id = '".trim($r_so['parent_id'])."'"));
}
$rr=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select iln from accounts where id='".$r['parent_id']."'"));
$buyer_iln=$rr['iln'];
$rr=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select account_id,account_number from ecmdocumenttemplates where id='".$r['template_id']."'"));
$seller_account_number=$rr['account_number'];
$rr=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select iln,name,id,sic_code,billing_address_street,billing_address_city,billing_address_postalcode,billing_address_country from accounts where id='".$rr['account_id']."'"));
$seller_iln=$rr['iln'];
$seller_id=$rr['id'];
$seller_name=$rr['name'];
$seller_nip=$rr['sic_code'];
$seller_street=$rr['billing_address_street'];
$seller_city=$rr['billing_address_city'];
$seller_postalcode=$rr['billing_address_postalcode'];
$seller_country=$rr['billing_address_country'];
if(strlen($r['currency_id'])<36)$currency=$r['currency_id'];
else{
$rr=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select iso4217 from currencies where id='".$r['currency_id']."'"));
$currency=$rr['iso4217'];
}
if(!$currency)$currency="PLN";
$rr=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select days from ecmpaymentconditions where id='".$r['ecmpaymentcondition_id']."'"));
$days=$rr['days'];
$order_no=trim($r['o_no']);
$register_date=$r['register_date'];
$dp=$r['delivery_place'];
if($r['parent_id']==134)$codebybuyer="";
else $codebybuyer=$r['supplier_code'];
echo $r['o_no'].' - '.$r['document_no'].'
';
$dpp=array(
"10"=>"5900000900100",
"11"=>"5900000900117",
"12"=>"5900000900124",
"13"=>"5900000900131",
"14"=>"5900000900148",
"15"=>"5900000900155",
"16"=>"5900000900162",
"17"=>"5900000900179",
"18"=>"5900000900186",
"19"=>"5900000900193",
"20"=>"5900000900209",
"21"=>"5900000900216",
"22"=>"5900000900223",
"23"=>"5900000900230",
"24"=>"5900000900247",
"25"=>"5900000900254",
"26"=>"5900000900261",
"27"=>"5900000900278",
"28"=>"5900000900285",
"29"=>"5900000900292",
"30"=>"5900000900308",
"31"=>"5900000900315",
"32"=>"5900000900322",
"33"=>"5900000900339",
"34"=>"5900000900346",
"35"=>"5900000900353",
"36"=>"5900000900360",
"37"=>"5900000900377",
"38"=>"5900000900384",
"39"=>"5900000900391",
"41"=>"5900000900414",
"42"=>"5900000900421",
"43"=>"5900000900438",
"44"=>"5900000900445",
"45"=>"5900000900452",
"46"=>"5900000900469",
"47"=>"5900000900476",
"48"=>"5900000900483",
"49"=>"5900000900490",
"50"=>"5900000900506",
"51"=>"5900000900513",
"52"=>"5900000900520",
"53"=>"5900000900537",
"54"=>"5900000900544",
"55"=>"5900000900551",
"56"=>"5900000900568",
"57"=>"5900000900575",
"58"=>"5900000900582",
"59"=>"5900000900599",
"60"=>"5900000900605",
"61"=>"5900000900612",
"63"=>"5900000900636",
"64"=>"5900000900643",
"65"=>"5900000900650",
);
$prod_iln=$r_so_acc['iln'];
$xml='
'.$r['document_no'].'
'.$r['register_date'].'
'.$r['sell_date'].'';
if($_REQUEST['duplicate']==1){
$xml.=''.date("Y-m-d").'';
$dfc="D";
}
else{
$dfc="O";
}
$xml.=''.$currency.'
'.$r['payment_date'].'
'.$days.'
'.$r['register_date'].'
'.$dfc.'
'.trim($r['o_no']).'
'.$r_so['register_date'].'
'.str_replace(" ","",$prod_iln).'
'.$r_wz['document_no'].'
'.$r['register_date'].'
'.str_replace(" ","",$buyer_iln).'
'.str_replace("-","",$r['to_nip']).'
'.$r['parent_name'].'
'.$r['parent_address_street'].'
'.$r['parent_address_city'].'
'.$r['parent_address_postalcode'].'
PL
'.str_replace(" ","",$buyer_iln).'
'.str_replace("-","",$r['to_nip']).'
'.$r['parent_name'].'
'.$r['parent_address_street'].'
'.$r['parent_address_city'].'
'.$r['parent_address_postalcode'].'
PL
'.str_replace(" ","",$buyer_iln).'
'.str_replace("-","",$r['to_nip']).'
'.$r['parent_name'].'
'.$r['parent_address_street'].'
'.$r['parent_address_city'].'
'.$r['parent_address_postalcode'].'
PL
'.str_replace(" ","",$seller_iln).'
'.str_replace("-","",$seller_nip).'
'.$seller_account_number.'
'.$seller_name.'
'.$seller_street.'
'.$seller_city.'
'.$seller_postalcode.'
PL
'.$codebybuyer.'
'.str_replace(" ","",$seller_iln).'
'.str_replace("-","",$seller_nip).'
'.$seller_account_number.'
'.$seller_name.'
'.$seller_street.'
'.$seller_city.'
'.$seller_postalcode.'
PL
'.$codebybuyer.'
'.str_replace(" ","",$seller_iln).'
'.str_replace("-","",$seller_nip).'
'.$seller_account_number.'
'.$seller_name.'
'.$seller_street.'
'.$seller_city.'
'.$seller_postalcode.'
PL
'.$codebybuyer.'
';
$w=$GLOBALS[db]->query("select * from ecminvoiceoutolditems where deleted='0' and ecminvoiceoutold_id='".$id."'");
$i=0;
$total_netto=0;
$total_vat=0;
$total_netto_arr=array();
$vats=array();
while($r=$GLOBALS[db]->fetchByAssoc($w)){
$rr=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select TRIM(ean) as ean from ecmproduct_language_pl_view where ecmproduct_id='".$r['ecmproduct_id']."'"));
$ean=trim($rr['ean']);
if (strlen($ean) > 13)
$ean = substr($ean, 0,13);
$total_netto+=$r['subtotal'];
$total_vat+=($r['total']-$r['subtotal']);
$vats[$r['ecmvat_value']]=$r['ecmvat_value'];
$total_netto_arr[$r['ecmvat_value']]+=$r['subtotal'];
$total_vat_arr[$r['ecmvat_value']]+=($r['total']-$r['subtotal']);
if (strlen($r['name'])>=70)
$r['name'] = substr($r['name'], 0, 66).'...';
$i++;
$xml.='
'.$i.'
'.trim($ean).'
'.$r['recipient_code'].'
'.$r['code'].'
'.$r['name'].'
CU
'.$r['quantity'].'
PCE
1.000
PCE
'.$r['subprice'].'
'.$r['ecmvat_value'].'
S
'.round($r['total']-$r['subtotal'],2).'
'.$r['subtotal'].'
'.$order_no.'
'.$r_so['register_date'].'
'.$prod_iln.'
'.$r_wz['document_no'].'
'.$register_date.'
';
}
$xml.='
'.$i.'
'.round($total_netto,2).'
'.round($total_netto,2).'
'.round($total_vat,2).'
'.round(($total_netto+$total_vat),2).'
'.EcmInvoiceOutOld::slowniePL(number_format(($total_netto+$total_vat),2,",",".")).'
';
foreach($vats as $vat){
$xml.='
'.$vat.'
S
'.round(($total_vat_arr[$vat]),2).'
'.round(($total_netto_arr[$vat]),2).'
'.round(($total_netto_arr[$vat]),2).'
'.round(($total_netto_arr[$vat]+$total_vat_arr[$vat]),2).'
';
}
$xml.='
';
return $xml;
}
function createCorrectInvoiceXml($id){
$r=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select *, TRIM(order_no) as o_no from ecminvoiceoutolds where id='".$id."'"));
$supplier_code=$r['supplier_code'];
$rr=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select iln from accounts where id='".$r['parent_id']."'"));
$buyer_iln=$rr['iln'];
$rr=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select account_id,account_number from ecmdocumenttemplates where id='".$r['template_id']."'"));
$seller_account_numer=$rr['account_number'];
$rr=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select iln,name,id,sic_code,billing_address_street,billing_address_city,billing_address_postalcode,billing_address_country from accounts where id='".$rr['account_id']."'"));
$seller_iln=$rr['iln'];
$seller_id=$rr['id'];
$seller_name=$rr['name'];
$seller_nip=$rr['sic_code'];
$seller_street=$rr['billing_address_street'];
$seller_city=$rr['billing_address_city'];
$seller_postalcode=$rr['billing_address_postalcode'];
$seller_country=$rr['billing_address_country'];
$r_inv=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select wz_id,register_date from ecminvoiceoutolds where id='".$r['ecminvoiceoutold_id']."'"));
$r_wz=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select document_no,so_id,parent_id from ecmstockdocouts where id='".$r_inv['wz_id']."'"));
$r_so=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select register_date,parent_id, parent_shipping_address_name from ecmsales where id='".$r_wz['so_id']."'"));
if ($r_so['parent_shipping_address_name'] && $r_so['parent_shipping_address_name']!='')
$r_so_acc=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select iln from accounts where name like '".trim($r_so['parent_shipping_address_name'])."%'"));
if (!$r_so_acc || $r_so_acc['iln']=='') {
$r_so_acc=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select iln from accounts where id = '".trim($r_so['parent_id'])."'"));
}
$prod_iln=$r_so_acc['iln'];
if(strlen($r['currency_id'])<36)$currency=$r['currency_id'];
else{
$rr=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select iso4217 from currencies where id='".$r['currency_id']."'"));
$currency=$rr['iso4217'];
}
$rr=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select days from ecmpaymentconditions where id='".$r['ecmpaymentcondition_id']."'"));
$days=$rr['days'];
$rr=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select document_no,register_date from ecminvoiceoutolds where id='".$r['ecminvoiceoutold_id']."'"));
$cor_name=$rr['document_no'];
$cor_date=$rr['register_date'];
if($r['parent_id']==134)$codebybuyer="";
else $codebybuyer=23862;
echo $r['o_no'].' - '.$r['document_no'].'
';
$xml='
'.$r['document_no'].'
'.$r['register_date'].'
'.$r['sell_date'].'';
if($r['parent_id']==134)$xml.='KOREKTA CENOWA LUB ILOSCIOWA';
if($_REQUEST['duplicate']==1){
$xml.=''.date("Y-m-d").'';
$dfc="R";
}
else{
$dfc="C";
}
$xml.=''.$currency.'
'.$r['payment_date'].'
'.$days.'
'.$r['register_date'].'
'.$dfc.'
'.$cor_name.'
'.$cor_date.'
'.str_replace(" ","",$prod_iln).'
'.$r_wz['document_no'].'
'.$r_inv['register_date'].'
'.trim($r['o_no']).'
'.$r_so['register_date'].'
'.str_replace(" ","",$buyer_iln).'
'.str_replace("-","",$r['to_nip']).'
'.$r['parent_name'].'
'.$r['parent_address_street'].'
'.$r['parent_address_city'].'
'.$r['parent_address_postalcode'].'
PL
'.str_replace(" ","",$seller_iln).'
'.str_replace("-","",$seller_nip).'
'.$seller_account_number.'
'.$seller_name.'
'.$seller_street.'
'.$seller_city.'
'.$seller_postalcode.'
PL
'.$codebybuyer.'
';
$w=$GLOBALS[db]->query("select * from ecminvoiceoutolditems where deleted='0' and ecminvoiceoutold_id='".$id."'");
$i=0;
$total_netto=0;
$total_vat=0;
$total_netto_arr=array();
$vats=array();
while($r=$GLOBALS[db]->fetchByAssoc($w)){
$rr=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select TRIM(ean) as ean from ecmproducts where id='".$r['ecmproduct_id']."'"));
$ean=trim($rr['ean']);
// bazujemy na old_ecminvoiceoutolditem_id, zero dynamicznych faktur
if (is_null ( $r ['old_ecminvoiceoutolditem_id'] ) || $r ['old_ecminvoiceoutolditem_id'] == '')
continue; // ten produkt nie był korygowany
$oi = $GLOBALS ['db']->fetchByAssoc ( $GLOBALS ['db']->query ( "SELECT quantity, subtotal, total, ecmvat_value, discount, dd_unit_id, subprice, price, ecmvat_value, ecmvat_name, ecmvat_id FROM ecminvoiceoutolditems WHERE id='" . $r ['old_ecminvoiceoutolditem_id'] . "'" ) );
$rrr = $oi;
$total_netto+=round($r['subtotal'],2);
$total_vat+=round($r['subtotal']*$r['ecmvat_value']/100,2);
$vats[$r['ecmvat_value']]=$r['ecmvat_value'];
$total_netto_arr[$r['ecmvat_value']]+=round($r['subtotal'],2);
$total_vat_arr[$r['ecmvat_value']]+=round($r['subtotal']*$r['ecmvat_value'],2);
$total_netto_c+=round($rrr['subtotal'],2);
$total_vat_c+=round($rrr['subtotal']*$rrr['ecmvat_value']/100,2);
$vats_c[$rrr['ecmvat_value']]=$rrr['ecmvat_value'];
$total_netto_arr_c[$rrr['ecmvat_value']]+=round($rrr['subtotal'],2);
$total_vat_arr_c[$rrr['ecmvat_value']]+=round($rrr['subtotal']*$rrr['ecmvat_value'],2);
if (strlen($r['name'])>=70)
$r['name'] = substr($r['name'], 0, 66).'...';
$i++;
$xml.='
'.$i.'
'.$ean.'
'.$r['recipient_code'].'
'.$r['code'].'
'.$r['name'].'
CU
'.$r['quantity'].'
PCE
1.000
'.round($r['subprice'],2).'
'.$r['ecmvat_value'].'
S
'.round(($r['subtotal']*$r['ecmvat_value']/100),2).'
'.round(($r['subtotal']),2).'
'.$rrr['quantity'].'
'.$rrr['subprice'].'
'.$rrr['ecmvat_value'].'
S
'.round(($rrr['subtotal']*$rrr['ecmvat_value']/100),2).'
'.round(($rrr['subtotal']),2).'
'.($r['quantity']-$rrr['quantity']).'
'.($r['subprice']-$rrr['subprice']).'
'.(round(($r['subtotal']*$r['ecmvat_value']/100),2)-round(($rrr['subtotal']*$rrr['ecmvat_value']/100),2)).'
'.(round(($r['subtotal']),2)-round(($rrr['subtotal']),2)).'
';
}
$xml.='
'.$i.'
'.round($total_netto,2).'
'.round($total_netto,2).'
'.round($total_vat,2).'
'.round(($total_netto+$total_vat),2).'
'.round($total_netto_c,2).'
'.round($total_netto_c,2).'
'.round($total_vat_c,2).'
'.round(($total_netto_c+$total_vat_c),2).'
'.round(($total_netto-$total_netto_c),2).'
'.round(($total_netto-$total_netto_c),2).'
'.round(($total_vat-$total_vat_c),2).'
'.round((($total_netto+$total_vat)-($total_netto_c+$total_vat_c)),2).'
'.str_replace("minus","",EcmInvoiceOutOld::slowniePL(number_format(($total_netto+$total_vat),2,",","."))).'';
$xml.='';
foreach($vats as $vat){
$xml.='
'.$vat.'
S
'.round(($total_netto_arr[$vat]*$vat/100),2).'';
//'.round(($total_netto_arr[$vat]),2).'
$xml.=''.round(($total_netto_arr[$vat]),2).'';
//'.(round(($total_netto_arr[$vat]*$vat/100),2)+round(($total_netto_arr[$vat]),2)).'
$xml.=''.$vat.'
S
'.round(($total_netto_arr_c[$vat]*$vat/100),2).'
'.round(($total_netto_arr_c[$vat]),2).'
'.round(round(($total_netto_arr[$vat]*$vat/100),2)-round(($total_netto_arr_c[$vat]*$vat/100),2),2).'
'.round(round($total_netto_arr[$vat],2)-round($total_netto_arr_c[$vat],2),2).'
';
}
$xml.='
';
return $xml;
}
?>