fetchByAssoc($GLOBALS[db]->query("select * from ecmreceipts 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 from ecmsales where id='".$r_wz['so_id']."'"));
$r_so_acc=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select iln from accounts where id='".$r_wz['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=$r['order_no'];
$register_date=$r['register_date'];
$dp=$r['delivery_place'];
if($r['parent_id']==134)$codebybuyer="";
else $codebybuyer=$r['supplier_code'];
$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.'
'.str_replace(" ","",$r['order_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 ecmreceiptitems where deleted='0' and ecmreceipt_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 ean from ecmproduct_language_pl_view where ecmproduct_id='".$r['ecmproduct_id']."'"));
$ean=$rr['ean'];
$total_netto+=round($r['price']*$r['quantity'],2);
$total_vat+=round($r['price']*$r['quantity']*$r['ecmvat_value']/100,2);
$vats[$r['ecmvat_value']]=$r['ecmvat_value'];
$total_netto_arr[$r['ecmvat_value']]+=round($r['price']*$r['quantity'],2);
$total_vat_arr[$r['ecmvat_value']]+=round($r['price']*$r['quantity']*$r['ecmvat_value']/100,2);
$i++;
$xml.='
'.$i.'
'.$ean.'
'.$r['recipient_code'].'
'.$r['code'].'
'.$r['name'].'
CU
'.$r['quantity'].'
PCE
1.000
PCE
'.round($r['price'],2).'
'.$r['ecmvat_value'].'
S
'.round(($r['quantity']*$r['price']*$r['ecmvat_value']/100),2).'
'.round(($r['quantity']*$r['price']),2).'
'.$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).'
'.EcmReceipt::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 * from ecmreceipts 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 ecmreceipts where id='".$r['ecmreceipt_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,order_no from ecmsales where id='".$r_wz['so_id']."'"));
$r_so_acc=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select iln from accounts where id='".$r_wz['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 ecmreceipts where id='".$r['ecmreceipt_id']."'"));
$cor_name=$rr['document_no'];
$cor_date=$rr['register_date'];
if($r['parent_id']==134)$codebybuyer="";
else $codebybuyer=23862;
$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'].'
'.str_replace(" ","",$r_so['order_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 ecmreceiptitems where deleted='0' and ecmreceipt_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 ean from ecmproduct_language_pl_view where ecmproduct_id='".$r['ecmproduct_id']."'"));
$ean=$rr['ean'];
$rrr=$GLOBALS[db]->fetchByAssoc($GLOBALS[db]->query("select price,quantity,ecmvat_value from ecmreceiptitems where id='".$r['ecmreceiptitem_id']."'"));
$total_netto+=round($r['price']*$r['quantity'],2);
$total_vat+=round($r['price']*$r['quantity']*$r['ecmvat_value']/100,2);
$vats[$r['ecmvat_value']]=$r['ecmvat_value'];
$total_netto_arr[$r['ecmvat_value']]+=round($r['price']*$r['quantity'],2);
$total_vat_arr[$r['ecmvat_value']]+=round($r['price']*$r['quantity']*$r['ecmvat_value'],2);
$total_netto_c+=round($rrr['price']*$rrr['quantity'],2);
$total_vat_c+=round($rrr['price']*$rrr['quantity']*$rrr['ecmvat_value']/100,2);
$vats_c[$rrr['ecmvat_value']]=$rrr['ecmvat_value'];
$total_netto_arr_c[$rrr['ecmvat_value']]+=round($rrr['price']*$rrr['quantity'],2);
$total_vat_arr_c[$rrr['ecmvat_value']]+=round($rrr['price']*$rrr['quantity']*$rrr['ecmvat_value'],2);
if($rrr['price']==$r['price'] && $rrr['quantity']==$r['quantity'])continue;
$i++;
$xml.='
'.$i.'
'.$ean.'
'.$r['recipient_code'].'
'.$r['code'].'
'.$r['name'].'
CU
'.$r['quantity'].'
PCE
1.000
'.round($r['price'],2).'
'.$r['ecmvat_value'].'
S
'.round(($r['quantity']*$r['price']*$r['ecmvat_value']/100),2).'
'.round(($r['quantity']*$r['price']),2).'
'.$rrr['quantity'].'
'.$rrr['price'].'
'.$rrr['ecmvat_value'].'
S
'.round(($rrr['price']*$rrr['quantity']*$rrr['ecmvat_value']/100),2).'
'.round(($rrr['price']*$rrr['quantity']),2).'
'.($r['quantity']-$rrr['quantity']).'
'.($r['price']-$rrr['price']).'
'.(round(($r['price']*$r['quantity']*$r['ecmvat_value']/100),2)-round(($rrr['price']*$rrr['quantity']*$rrr['ecmvat_value']/100),2)).'
'.(round(($r['price']*$r['quantity']),2)-round(($rrr['price']*$rrr['quantity']),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","",EcmReceipt::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;
}
?>