<?php
$w = "35"; // first column width
$w3 = "35";
$w2 = "25"; // second column width


$db = $GLOBALS['db'];
$ret = $db->query("
SELECT distinct o.ecminvoiceout_id
FROM
ecminvoiceoutitems as ii
INNER JOIN ecminvoiceouts as i
ON ii.ecminvoiceout_id = i.id
INNER JOIN ecminvoiceoutitems AS o
ON o.id = ii.ecminvoiceoutitem_id
WHERE
ii.ecminvoiceout_id = '$focus->id'");


if ($ret->num_rows > 1) $multi_correct = true;



$content = '
<table style="width: 100%; font-size: 8pt;">
	<tr>		
		<td style="width: ' . $w . '%">
			<b>NABYWCA / BUYER</b>
		</td>
		<td style="width: ' . $w3 . '%">';
		if($focus->parent_shipping_address_name!=''){ 
			$content.= '<b>DOSTAWA / DELIVERY</b>';
		}
		
		$content.= '</td>
		<td style="width: ' . $w2 . '%">';
if($focus->parent_payer_address_name!=''){ 
			$content.= '<b>PŁATNIK / PAYER</b>';
		}$content.= '</td>
		<td style="text-align: right"></td>
	</tr>
	<tr>		
		<td style="width: ' . $w . '%">
			' . $focus->parent_name . '
		</td>
		<td style="width: ' . $w3 . '%">
			'.$focus->parent_shipping_address_name.'
		</td>
		<td style="width: ' . $w2 . '%">'.$focus->parent_payer_address_name.'</td>
		<td style="text-align: right"></td>
	</tr>
	<tr>		
		<td style="width: ' . $w . '%">
			' . $focus->parent_address_street . '
		</td>
		<td style="width: ' . $w3 . '%">
			' . $focus->parent_shipping_address_street . '
		</td>
		<td style="width: ' . $w2 . '%">
		' . $focus->parent_payer_address_street . '
        </td>
		<td style="text-align: right">
		</td>
	</tr>
	<tr>		
		<td style="width: ' . $w . '%">
			'. $focus->parent_address_postalcode . ' ' . $focus->parent_address_city;
		if (!is_null($focus->parent_address_country) & $focus->parent_address_country!='')
			$content.=', '.$focus->parent_address_country;
	$content.='
		</td>
		<td style="width: ' . $w3 . '%">
			'. $focus->parent_shipping_address_postalcode . ' ' . $focus->parent_shipping_address_city;
	if (!is_null($focus->parent_shipping_address_country) & $focus->parent_shipping_address_country!='')
		$content.=', '.$focus->parent_shipping_address_country;
	$content.='
		</td>
		<td style="width: ' . $w2 . '%">
				'. $focus->parent_payer_address_postalcode . ' ' . $focus->parent_payer_address_city;
	if (!is_null($focus->parent_payer_address_country) & $focus->parent_payer_address_country!='')
		$content.=', '.$focus->parent_payer_address_country;
	$content.='
		</td>
		<td style="text-align: right">
		
		</td>
	</tr>';
		$content.='
		<tr>
			<td valign="top" style="width: ' . $w . '%">';
		if (!is_null($focus->parent_nip) && $focus->parent_nip) {
			$content.='NIP/Vat ID: '.$focus->parent_nip;
			$nip = true;
		}
		if (!is_null($focus->parent_iln) && $focus->parent_iln) {
			if ($nip) $content.'<br>'; $nip = false;
			$content.=$labels['LBL_PDF_CONTENT_ILN'].' '.$focus->parent_iln;	
		}	
		$content.='		
			</td>';
		$content.='
			<td valign="top" style="width: ' . $w3 . '%">';
		if (!is_null($focus->parent_shipping_address_nip) && $focus->parent_shipping_address_nip) {
			$content.='NIP/Vat ID: '.$focus->parent_shipping_address_nip;
			$nip = true;
		}
		if (!is_null($focus->parent_shipping_iln) && $focus->parent_shipping_iln) {
			if ($nip) $content.'<br>'; $nip = false;
			$content.=$labels['LBL_PDF_CONTENT_ILN'].' '.$focus->parent_shipping_iln;
		}
		$content.='
			</td>';
			$content.='
			<td valign="top" style="width: ' . $w3 . '%">';
		if (!is_null($focus->parent_payer_address_nip) && $focus->parent_payer_address_nip) {
			$content.='NIP/Vat ID: '.$focus->parent_payer_address_nip;
			$nip = true;
		}
		if (!is_null($focus->parent_payer_iln) && $focus->parent_payer_iln) {
			if ($nip) $content.'<br>'; $nip = false;
			$content.=$labels['LBL_PDF_CONTENT_ILN'].' '.$focus->parent_payer_iln;
		}
		$content.='</td>
		<td colspan="0" valign="top" style="text-align: right">
	
		</td>
		</tr>';
$content.='
</table>
';
if ($focus->name && $focus->name != '') {
	$content .= '
		<table style="width: 100%; text-align: center">
			<tr><td>
				<b>' . $focus->name . '</b>
			</td></tr>
		</table>
';
}
if($focus->type=='correct'){
    $corl='Korekta do / Correct to:';
    $labels['LBL_PDF_CONTENT_PARENT_DOCUMENT_NO'] = "Numer zwrotu / Return number";
}

$labels['LBL_PLACE_PDF'] = 'Miejsce wystawienia / Place of register:';
$labels['LBL_PDF_CONTENT_SELL_DATE'] = 'Data sprzedaży / Sell date:';
if (!$multi_correct) {
$inv = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("SELECT register_date FROM ecminvoiceouts WHERE id='".$focus->ecminvoiceout_id."'"));
$content .= '<br><table width="100%" style="font-size: 7pt;text-align:left;vertical-align:top;">
		<tr><td width="25%" >Data wystawienia / Register date<br>'.$labels['LBL_PLACE_PDF'].'<br>'.$labels['LBL_PDF_CONTENT_SELL_DATE'].'<br>'.$corl.'</td>
				<td style="text-align: left;" width="40%"><b>' . $focus->register_date . '</b><br><b>Warszawa</b><br><b>' . $focus->sell_date . '</b><br><b>' . $focus->ecminvoiceout_name . '</b>, '.$inv['register_date'].'</td>
		<td rowspan="2" style="text-align: left;vertical-align:top; width: 25%;">';
} else {
     $content .= '<br><table width="100%" style="font-size: 7pt;text-align:left;vertical-align:top;">
 		<tr><td width="25%" >'.$labels['LBL_PDF_CONTENT_REGISTER_DATE'].'<br>'.$labels['LBL_PLACE_PDF'].'<br>'.$labels['LBL_PDF_CONTENT_SELL_DATE'].'<br></td>
 				<td style="text-align: left;" width="50%"><b>' . $focus->register_date . '</b><br><b>Warszawa</b><br><b>' . $focus->sell_date . '</b><br><b></b></td>
 		<td rowspan="2" style="text-align: left;vertical-align:top; width: 25%">';
}
if($focus->wz_name!=''){
	$content.=$labels['LBL_PDF_CONTENT_PARENT_DOCUMENT_WZ'].':<br>';
}
if($focus->order_no!=''){
	$content.=$labels['LBL_PDF_CONTENT_PARENT_DOCUMENT_NO'].':<br>';
}
if($focus->supplier_code!=''){
    $content.=$labels['LBL_SUPPLIER_CODE'].':<br>';
}
if($focus->payment_date!=''){
    $content.='Data płatności / Payment date:<br>';
}	
if($focus->ecmpaymentcondition_id!=''){
    $content.='Metoda płatności / Payment method:<br>';
}

$pay=new EcmPaymentCondition();

$pay->retrieve($focus->ecmpaymentcondition_id);

		$content.='</td>
		<td rowspan="2" style="vertical-align:top;">';
		if($focus->wz_name!=''){
			$content.='<b>' . $focus->wz_name.'</b><br>';
		}
		if($focus->order_no!=''){
			$content.='<b>' . $focus->order_no.'</b><br>';
		}
	   if($focus->supplier_code!=''){
			$content.='<b>' . $focus->supplier_code.'</b><br>';
		}
		if($focus->payment_date!=''){
		   $content.='<b>' . date('d.m.Y',strtotime($focus->payment_date)).'</b><br>';
		}
		if($focus->ecmpaymentcondition_id!=''){
		    $content.='<b>' . $GLOBALS['app_list_strings']['ecmpaymentconditions_payment_method_dom'][$pay->payment_method].'</b><br>';
		}
		$content.='</td></tr></table><br>';
// start items table
$columns = array ();

$columns ['position'] = array (
		'field' => array (
				'position' 
		),
		'label' => 'Lp. / Pos.',
		'align' => 'center' 
);

$columns ['name'] = array (
		'field' => array (
				'name',
				'product_code' 
		),
		'label' => 'Nazwa / Name<br>Indeks / Index ',
		'align' => 'left' 
);
if($recipient_code==1){
    $columns ['recipient_code'] = array (
            'field' => array (
                    'recipient_code'
            ),
            'label' => 'Kod Tow. / Recipient Code',
            'align' => 'center'
    );
}
$columns ['qty'] = array (
		'field' => array (
				'quantity',
				'unit_name' 
		),
		'label' => 'Ilość / Qantity<br>J.m. / Unit',
		'align' => 'right' 
);

$columns ['price_netto'] = array (
		'field' => array (
				'price_netto' 
		),
		'label' => 'Cena /Price',
		'align' => 'right' 
);

$columns ['total_brutto'] = array (
        'field' => array (
                'total_brutto'
        ),
        'label' => 'Wartość / Value',
        'align' => 'right'
);
// set widths
// set widths
$totals = array ();
if($recipient_code==1){
    $columns ['position'] ['width'] = '5';
    $columns ['name'] ['width'] = '41';
    $columns ['recipient_code'] ['width'] = '12';
    $columns ['qty'] ['width'] = '12';
    $columns ['price_netto'] ['width'] = '12';
    $columns ['total_brutto'] ['width'] = '12';
} else {
    $columns ['position'] ['width'] = '5';
    $columns ['name'] ['width'] = '51';
    $columns ['qty'] ['width'] = '14';
    $columns ['price_netto'] ['width'] = '14';
    $columns ['total_brutto'] ['width'] = '14';
}

// rysujemy :)
$content .= '
	<table style="width: 100%; font-size: 7pt; border: 0.5 solid black; border-collapse: collapse;"><thead>
		<tr>
';

foreach ( $columns as $col ) {
	$content .= '
		<th style="border: 0.5 solid black; width: ' . $col ['width'] . '%;background-color: #E9E9E9;">' . $col ['label'] . '</th>		
	';
}
$content .= '
	</tr></thead><tbody>
';
$counter=1;

    foreach ( $positions as $pos ) {
        $content .= '<tr>';
        
        foreach ( $columns as $col ) {
    
            $content .= '<td style="border: 0.5 solid black; text-align: ' . $col ['align'] . ';">';
            foreach ( $col ['field'] as $f ) {
    
                if ((! $pos [$f] || $pos [$f] == '') && $f!='quantity')
                   $pos [$f] = '-';
                $content .= $pos [$f] . '<br>';
    
            }
            $content .= '</td>';
        }
        $content .= '</tr><tr>';
        // old correct item
        $pos['name'] = 'było';
        if ($multi_correct) {
            // get invoice
            $c = $db->fetchByAssoc ( $db->query ( "
            		SELECT i.document_no, i.order_no, register_date FROM ecminvoiceouts AS i
            		INNER JOIN ecminvoiceoutitems as ii
            		ON i.id=ii.ecminvoiceout_id
            		WHERE ii.id = '" . $pos ['ecminvoiceoutitem_id'] . "'
            		" ) );
            if ($c['order_no'] && $c['order_no']!='')
                $pos['name'] .= '<br>' . $corl . ' ' . $c ['document_no'] .' - '.$c['register_date'].'<br>' .
                $mod_strings ['LBL_PDF_ORDER_NO'].' '.$c['order_no'];
            else
                $pos['name'] .= '<br>' . $corl .' '.$c ['document_no'] .' - '.$c['register_date'];
            unset ( $c );
        }
        $pos['total_netto'] = $pos['old_total_netto'];
        $pos['total_brutto'] = $pos['old_total_brutto'];
        $pos['total_vat'] = $pos['old_total_vat'];
        $pos['price_netto'] = $pos['old_price_netto'];
        $pos['quantity'] = $pos['old_quantity'];
        $pos['position']='';
        $pos['product_code']=' ';
        
        foreach ( $columns as $col ) {
        
            $content .= '<td style="border: 0.5 solid black; text-align: ' . $col ['align'] . ';">';
            foreach ( $col ['field'] as $f ) {
        
                if (! $pos [$f] || $pos [$f] == '')
                    $pos [$f] = '-';
                $content .= $pos [$f] . '<br>';
        
            }
            $content .= '</td>';
        }
        $content .= '</tr>';
    } 

$content .= '
	</tbody></table>
';
// summary table
// get currency symbol
$c = new Currency ();
$c->retrieve ( $focus->currency_id );
$symbol = $c->iso4217;
unset ( $c );
$content .= '
	<br>
	<table style="font-size: 9pt; border: 0.5 solid black; border-collapse: collapse; width: 300px; margin-left: 60%">
';

$content .= '
		<tr>
        <td style="font-size: 7pt; border: 0.5 solid black; width: 25%;text-align: center;">
				<b>Wartość / Value</b>
			</td>   
                <td style="font-size: 6.5pt; border: 0.5 solid black; width: 27%;text-align: right;background-color: #E9E9E9;">
				<b>'.format_number($focus->total_brutto).'</b>
			</td>
        ';

		$content.='</tr>
		';
		
	
		$left_to_paid=$focus->total_brutto-$focus->paid_val;
		
	if ($focus->total_brutto > 0)
		$labels['LBL_PDF_TO_PAY'] = 'Do zapłaty / To paid: ';
	else 
		$labels['LBL_PDF_TO_PAY'] = 'Do zwrotu / To paid back: ';
	
	$total_brutto = abs($focus->total_brutto);
$content .= '
	</table>
	<table style="font-size: 7pt;">
		<tr><td>'.$labels['LBL_PDF_TO_PAY'].'</td><td style="text-align: right;"><b>'.format_number($total_brutto).' '.$symbol.'</b></td><td></td></tr>';

		
		if($focus->ecmpaymentcondition_id!=''){
			
			$q= $db->query ( "select name from ecmpaymentconditions where id='$focus->ecmpaymentcondition_id'" );
			$r=$db->fetchByAssoc($q);
			//$paid_text='w Terminie <b>'.$r['name'].'</b> do <b>'.date('d.m.Y',strtotime($focus->payment_date)).'</b> '.$GLOBALS['app_list_strings']['payment_method_dom'][$focus->payment_method];
		} else {
			if($focus->payment_date!=''){
					
				$dni=(strtotime($focus->payment_date)-strtotime($focus->sell_date))/(3600*24);
				//$paid_text='w Terminie <b>'.$dni.' dni</b> do <b>'.date('d.m.Y',strtotime($focus->payment_date)).'</b> '.$GLOBALS['app_list_strings']['payment_method_dom'][$focus->payment_method];
			}
		}
	
		
if (!$focus->pdf_text || $focus->pdf_text=="")
	$focus->pdf_text = "Przyczyna korekty: Zwrot towaru.<br>Correct reason: Products return.";

$content .= '
	</table><br><p style="font-size: 7pt;">Wystawił / Issued by <b>'.$user->full_name.'</b></p>
	<br><br>
	' . $focus->pdf_text . '			
';





