<?php

global $db;
$mod_strings['LBL_PDF_FOOTER_DOCUMENT_NAME'] = 'Zamówienie';

$pdf->focus = $this;

$pdf->Ln(2);

$pdf->SetY($pdf->GetY() + 2);
$current = $pdf->GetY();
$pdf->SetFont('arialpl', '', 9);



if ($this->name) {
    $pdf->Cell(20, 5, '', 0, 1, 'L');
    $pdf->SetFont('arialpl', 'b', 10);
    $pdf->Cell(20, 5, $mod_strings['LBL_PDF_NAME'], 0, 1, 'L');
    $pdf->SetFont('arialpl', '', 10);
    $pdf->Cell(20, 5, html_entity_decode($this->name), 0, 1, 'L');
}

$pdf->SetFont('arialpl', '', 10);
$pdf->Ln(5);


if ($this->parent_type = "Contacts") {
    $pdf->Ln(14);
}

$current2 = $pdf->GetY();
$pdf->SetFont('arialpl', '', '10');

$subFontSizeold = $pdf->FontSizePt;
$pdf->SetFontSize($subFontSize);


$pdf->SetXY($pdf->fw - $pdf->rMargin - 37, 8);

$table = array();

$table [0]['data'] = array(
    'width' => 30,
    'value' => $mod_strings['LBL_PDF_DATE_REGISTER'],
    'border' => 0,
    'overflow' => 1,
    'align' => 'L',
    'font-style' => 'b',
    'font-size' => 9,
);

$table [1]['data'] = array(
    'width' => 30,
    'value' => $this->register_date,
    'border' => 0,
    'overflow' => 1,
    'align' => 'L',
);

$table [2]['data'] = array(
		'width' => 30,
		'value' => $mod_strings['LBL_PDF_VALIDTILL_DATE'],
		'border' => 0,
		'overflow' => 1,
		'align' => 'L',
		'font-style' => 'b',
		'font-size' => 9,
);

$table [3]['data'] = array(
		'width' => 30,
		'value' => $this->validtill_date,
		'border' => 0,
		'overflow' => 1,
		'align' => 'L',
);

$table [4]['data'] = array(
    'width' => 30,
    'value' => $mod_strings['LBL_PDF_OWNER'],
    'border' => 0,
    'overflow' => 1,
    'align' => 'L',
    'font-style' => 'b',
    'font-size' => 9,
);

$table [5]['data'] = array(
    'width' => 30,
    'value' => $this->setUser()->full_name,
    'border' => 0,
    'overflow' => 1,
    'align' => 'L',
);

if ($this->parent_document_no && $this->parent_document_no!='') {
$table [6]['data'] = array(
		'width' => 30,
		'value' => $mod_strings['LBL_PDF_NUMBER'],
		'border' => 0,
		'overflow' => 1,
		'align' => 'L',
		'font-style' => 'b',
		'font-size' => 9,
);

$table [7]['data'] = array(
		'width' => 30,
		'value' => $this->parent_document_no,
		'border' => 0,
		'overflow' => 1,
		'align' => 'L',
);
}

$pdf->DrawTable($table, null, 0, 0, 3);

$pdf->SetY($current+10);
$pdf->SetX(70);

//$pdf->Ln(8);

$pdf->SetFont('arialpl', '', '18');

$pdf->MultiCell(0, 0, $mod_strings['LBL_PDF_DOCUMENT_NAME'] . " " . $this->document_no, 0, 'L');
//$pdf->MultiCell(0,0,'Szablon dokumentu jest aktualnie przebudowywany.',0,'L');

//$pdf->MultiCell(0,0,'Spróbuj za 30 min.',0,'L');

//$this->loadParser();
 $pdf->SetFont('arialpl', '', '10');

$title = '';



//new start

$address = htmlspecialchars_decode($this->parent_name);//str_replace(array('&lt;', '&gt;'), array('<', '>'), $this->parent_name);

if ($this->parent_contact_name) {
    $address .= PHP_EOL . $this->parent_contact_name;

    if ($this->parent_contact_title) {
        $address .= PHP_EOL . $this->parent_contact_title;
    }

    $personplus = 5;
} else {
    $personplus = 0;
}

if ($this->parent_address_street) {
    $address .= PHP_EOL . $this->parent_address_street;
}

if ($this->parent_address_postalcode) {
    $address .= PHP_EOL . $this->parent_address_postalcode;
}

if ($this->parent_address_city) {
    $address .= " " . $this->parent_address_city;
}
if ($this->to_vatid) {
    $address .= PHP_EOL . $this->to_vatid;
}


$qr = 'SELECT iln, to_nip FROM accounts WHERE id = \'' . $this->parent_id . '\'';
$r = $db->fetchByAssoc($db->query($qr));

if ($r['iln']) {
    $address .= "\nILN: " . $r['iln'];
}

//to address
$old_x = $pdf->GetX();
$old_y = $pdf->GetY();

$pdf->SetXY(25, 53);
$pdf->SetFont('arialpl', 'B', 8);

$pdf->Cell(35, 1, strtoupper($mod_strings['LBL_PDF_BUYER']), 0, 0, 'L', 1);
$pdf->Ln(3);

$pdf->SetX(25);

$pdf->SetFont('arialpl', '', 8);
$pdf->MultiCell(60, 4, $address, 0, 'L');
//delivery

$address = str_replace(array('&lt;', '&gt;'), array('<', '>'), $this->parent_shipping_address_name);

if ($this->parent_shipping_address_street) {
    $address .= PHP_EOL . $this->parent_shipping_address_street;
}

if ($this->parent_shipping_address_postalcode) {
    $address .= PHP_EOL . $this->parent_shipping_address_postalcode;
}

if ($this->parent_shipping_address_city) {
    $address .= ' ' . $this->parent_shipping_address_city;
}


if ($address) {
    $pdf->SetXY(85, 53);

    $pdf->SetFont('arialpl', 'B', 8);
    $pdf->Cell(35, 1, strtoupper($mod_strings['LBL_PDF_DELIVERY2']), 0, 0, 'L', 1);
    $pdf->Ln(3);

    $pdf->SetX(85);
    $pdf->SetFont('arialpl', '', 8);
    $pdf->MultiCell(60, 4, $address, 0, 'L');
}
// delivery end

$this->header_text = $this->template->mfp->parseText($this->header_text);

$this->footer_text = $this->template->mfp->parseText($this->footer_text);

$this->ads_text = $this->template->mfp->parseText($this->ads_text);


//header text
if ($this->header_text) {
    $pdf->Ln(8);

    $pdf->SetFont('arialpl', '', '10');

    $pdf->MultiCell(0, 4, $this->header_text, 0, 'J');
}

$pdf->Ln(8);

//**************************************************
global $mod_strings;

$currency = new Currency();
$currency->retrieve($this->currency_id);

$this->currency_symbol = $currency->iso4217;

//if (!isset($this->position_list) || !is_array($this->position_list)) {
    //$this->position_list = $this->formatPositions($this->position_list, $this->currency_id , $this->currency_value );
    $this->position_list = $this->getPositionList(true);
//}

//var_dump($this->getPositionList(true));
//exit;

//$calc = $this->calculate_currency($this->position_list, $this->currency_id , $this->currency_value );
//$this->position_list = $this->formatPositions($this->position_list, $this->currency_id , $this->currency_value );
$calc = $this->calculate_pdf($this->position_list, true );
/*
$calc['total'] .= ' ' . $this->currency_symbol;
$calc['subtotal'] .= ' ' . $this->currency_symbol;
*/
/*
if ($calc['discount']) {
    $calc['total2'] .= ' ' . $this->currency_symbol;
    $calc['discount']['value'] .= ' ' . $this->currency_symbol;
}
*/
/*
if (isset($calc['vats']) && is_array($calc['vats']) && count($calc['vats']) > 0) {
    foreach ($calc['vats'] as $key => $value) {
        $calc['vats'][$key] .= ' ' . $this->currency_symbol;
    }
}
*/

/*
echo '<pre>' . var_export($calc, true); 
echo '<pre>' . var_export($this->position_list, true); 
exit;
*/


$widths = array(
    'position' => 5,
    'description' => 60,
    'quantity' => 8,
    'unit_price' => 15,
    'discount' => 8,
    'total' => 15,
    //
    'fix' => 73 - 8,
    'subtotal1' => 15 + 8, // 66
    'subtotal2' => 15, // 34
);

$image_w = 10;
$recipient_code_w = 10;

if ($this->show_images_on_offers) {
    $widths['description'] -= $image_w;
}

if ($this->show_recipient_code) {
    $widths['description'] -= $recipient_code_w;
}

/*
if ($calc['draw_discount']) {
    //$widths['description'] -= 8;
}
if ($calc['draw_vat']) {
    $widths['description'] -= 8;
}
*/

//jest rabat?
$rabat = false;

foreach ($this->position_list as $p) {
    if ($p['discount'] != 0)
        $rabat = true;
}

if ($rabat) {
    $widths['description'] -= $widths['discount'];

    $widths['fix'] -= $widths['discount'];
    $widths['subtotal2'] += $widths['discount'];
}

if ($this->total_netto != '') {
    $widths['description']+=35;
}

$widths['description'] = $widths['description'] - 20;
$widths['code'] = 20;



$table = array();

$table [0]['position'] = array(
    'width' => $widths['position'],
    'value' => $mod_strings['LBL_PDF_LIST_POSITION'],
    'background' => array(233, 233, 233),
    'border' => 1,
    'overflow' => 1,
    'align' => 'C',
);

$table [0]['code'] = array(
		'width' => $widths['code'],
		'value' => $mod_strings['LBL_PDF_LIST_CODE'],
		'background' => array(233, 233, 233),
		'border' => 1,
		'overflow' => 1,
		'align' => 'C',
);

$table [0]['description'] = array(
    'width' => $widths['description'],
    'value' => $mod_strings['LBL_PDF_LIST_DESCRIPTION'],
    'background' => array(233, 233, 233),
    'border' => 1,
    'overflow' => 1,
    'align' => 'C',
);

$table [0]['quantity'] = array(
    'width' => $widths['quantity'],
    'value' => $mod_strings['LBL_PDF_LIST_QUANTITY'],
    'background' => array(233, 233, 233),
    'border' => 1,
    'overflow' => 1,
    'align' => 'C',
);

if ($this->total_netto == '') {
    $table [0]['unit_price'] = array(
        'width' => $widths['unit_price'],
        'value' => $mod_strings['LBL_PDF_LIST_PRICE'],
        'background' => array(233, 233, 233),
        'border' => 1,
        'overflow' => 1,
        'align' => 'C',
    );

    if ($rabat == true) {
        $table [0]['discount'] = array(
            'width' => $widths['discount'],
            'value' => $mod_strings['LBL_PDF_LIST_DISCOUNT'],
            'background' => array(233, 233, 233),
            'border' => 1,
            'overflow' => 1,
            'align' => 'C',
        );
    }

    $table [0]['total'] = array(
        'width' => $widths['total'],
        'value' => $mod_strings['LBL_PDF_LIST_TOTAL'],
        'background' => array(233, 233, 233),
        'border' => 1,
        'overflow' => 1,
        'align' => 'R',
    );
}

$i = 1;
$lv = return_app_list_strings_language($this->ecmlanguage);
if ($this->position_list != '') {
//    $p = array_pop($this->position_list);
//    $this->position_list = array_fill(0, 100, $p);
//
//    $result = $db->query("SELECT total_netto from ecmquotes where id=$p['id']");

    foreach ($this->position_list as $p) {
        $table [$i]['position'] = array(
            'width' => $widths['position'],
            'value' => $i,
            'border' => 1,
            'align' => 'C',
        );

        $name = str_replace('–', '-', htmlspecialchars_decode($p['name']));

        $table [$i]['code'] = array(
        		'width' => $widths['code'],
        		'value' => $p['code'],
        		'border' => 1,
        		'align' => 'L',
        );
                
        $table [$i]['description'] = array(
            'width' => $widths['description'],
            'value' => $name,
            'border' => 1,
            'align' => 'L',
        );

        $table [$i]['quantity'] = array(
            'width' => $widths['quantity'],
            'value' =>format_number($p['quantity'],null,null,null,0),
            'border' => 1,
            'align' => 'C',
        );

        if ($this->total_netto == '') {
            $table [$i]['unit_price'] = array(
                'width' => $widths['unit_price'],
                //'value' => format_number($p['price']) . ' ' . $this->currency_symbol,
                'value' => format_number($p['price']),
                'border' => 1,
                'align' => 'R',
            );

            if ($rabat == true) {
                if ($p['discount'] == 0) {
                    $tmp = '---';
                } else {
                    $tmp = $p['discount'];
                }

                $table [$i]['discount'] = array(
                    'width' => $widths['discount'],
                    'value' => format_number($tmp) . '%',
                    'border' => 1,
                    'align' => 'R',
                );
            }

            $table [$i]['total'] = array(
                'width' => $widths['total'],
                //'value' => format_number($p['total']) . ' ' . $this->currency_symbol,
                'value' => format_number($p['total']),
                'border' => 1,
                'align' => 'R',
            );
        }

        $tmp_sep = ereg_replace('[^0-9.,]', '', $p['total']);
        $len = strlen($tmp_sep);
        $separator = substr($tmp_sep, $len - 3, 1);

        if ($separator == '.') {
            $tmp = (float) ereg_replace('[^0-9.]', '', $p['total']);
        }
        if ($separator == ',') {
            $tmp = (float) ereg_replace('[^0-9,]', '', $p['total']);
        }

        if ($p['currency_id'] == 'PLN') {
            if (isset($total_price_pln)) {
                $total_price_pln += $tmp;
            } else {
                $total_price_pln = $tmp;
            }
        }

        if ($p['currency_id'] == '6336d9a0-ee5f-52e3-7d0c-4e6f1472b2bf') {
            if (isset($total_price_eur)) {
                $total_price_eur+= $tmp;
            } else {
                $total_price_eur = $tmp;
            }
        }

        $i++;
		
    }
}



$starttab = $pdf->GetY() + 10;
$pdf->DrawTable($table, array(0));

//EURO
/*
if ($calc['vats_eur']) {
    $endtab = $pdf->GetY();
    $pdf->SetY($endtab);

    $table = array();

    $table[] = array(
        'fix' => array(
            'width' => $widths['fix'],
            'value' => '',
            'border' => 0,
            'overflow' => 1,
            'align' => 'C',
            'font-style' => 'b',
            'font-size' => 7,
        ),
        'subtotal1' => array(
            'width' => $widths['subtotal1'],
            'value' => $mod_strings['LBL_PDF_TOTAL'],
            'background' => array(255, 255, 255),
            'border' => 1,
            'overflow' => 1,
            'align' => 'L',
            'font-size' => 9,
        ),
        'subtotal2' => array(
            'width' => $widths['subtotal2'],
            'value' => $calc['subtotal_eur'] . ' EUR',
            'border' => 1,
            'overflow' => 1,
            'align' => 'R',
            'font-size' => 9,
        ),
    );

    foreach ($calc['vats_eur'] as $key => $value) {
        $r = mysql_fetch_array(mysql_query("select name from ecmvats where id='" . $key . "'"));
        $vvn = $r['name'];

        $table[] = array(
            'fix' => array(
                'width' => $widths['fix'],
                'value' => '',
                'border' => 0,
                'overflow' => 1,
                'align' => 'C',
                'font-style' => 'b',
                'font-size' => 7,
            ),
            'subtotal1' => array(
                'width' => $widths['subtotal1'],
                'value' => $mod_strings['LBL_PDF_VAT'] . ' (' . $vvn . ')',
                'background' => array(255, 255, 255),
                'border' => 1,
                'overflow' => 1,
                'align' => 'L',
                'font-size' => 9,
            ),
            'subtotal2' => array(
                'width' => $widths['subtotal2'],
                'value' => $value . ' EUR',
                'border' => 1,
                'overflow' => 1,
                'align' => 'R',
                'font-size' => 9,
            ),
        );
    }

    $table[] = array(
        'fix' => array(
            'width' => $widths['fix'],
            'value' => '',
            'border' => 0,
            'overflow' => 1,
            'align' => 'C',
            'font-style' => 'b',
            'font-size' => 7,
        ),
        'subtotal1' => array(
            'width' => $widths['subtotal1'],
            'value' => $mod_strings['LBL_PDF_END_TOTAL'],
            'background' => array(233, 233, 233),
            'border' => 1,
            'overflow' => 1,
            'align' => 'L',
            'font-size' => 9,
            'font-style' => 'b',
        ),
        'subtotal2' => array(
            'width' => $widths['subtotal2'],
            'value' => $calc['subtotal_eur'] . ' EUR',
            'border' => 1,
            'background' => array(233, 233, 233),
            'overflow' => 1,
            'align' => 'R',
            'font-size' => 9,
            'font-style' => 'b',
        ),
    );

    $pdf->Ln(4);
    $pdf->DrawTable($table);
}

if ($calc['vats_pln']) {
    //PLN
    $starttab = $pdf->GetY() + 4;
    $pdf->SetY($starttab);

    $table = array();

    $table[] = array(
        'fix' => array(
            'width' => $widths['fix'],
            'value' => '',
            'border' => 0,
            'overflow' => 1,
            'align' => 'C',
            'font-style' => 'b',
            'font-size' => 7,
        ),
        'subtotal1' => array(
            'width' => $widths['subtotal1'],
            'value' => $mod_strings['LBL_PDF_TOTAL'],
            'background' => array(255, 255, 255),
            'border' => 1,
            'overflow' => 1,
            'align' => 'L',
            'font-size' => 9,
        ),
        'subtotal2' => array(
            'width' => $widths['subtotal2'],
            'value' => $calc['subtotal_pln'] . ' PLN',
            'border' => 1,
            'overflow' => 1,
            'align' => 'R',
            'font-size' => 9,
        ),
    );

    foreach ($calc['vats_pln'] as $key => $value) {
        $r = mysql_fetch_array(mysql_query("select name from ecmvats where id='" . $key . "'"));
        $vvn = $r['name'];
        $table[] = array(
            'fix' => array(
                'width' => $widths['fix'],
                'value' => '',
                'border' => 0,
                'overflow' => 1,
                'align' => 'C',
                'font-style' => 'b',
                'font-size' => 7,
            ),
            'subtotal1' => array(
                'width' => $widths['subtotal1'],
                'value' => $mod_strings['LBL_PDF_VAT'] . ' (' . $vvn . ')',
                'background' => array(255, 255, 255),
                'border' => 1,
                'overflow' => 1,
                'align' => 'L',
                'font-size' => 9,
            ),
            'subtotal2' => array(
                'width' => $widths['subtotal2'],
                'value' => $value . ' PLN',
                'border' => 1,
                'overflow' => 1,
                'align' => 'R',
                'font-size' => 9,
            ),
        );
    }

    $table[] = array(
        'fix' => array(
            'width' => $widths['fix'],
            'value' => '',
            'border' => 0,
            'overflow' => 1,
            'align' => 'C',
            'font-style' => 'b',
            'font-size' => 7,
        ),
        'subtotal1' => array(
            'width' => $widths['subtotal1'],
            'value' => $mod_strings['LBL_PDF_END_TOTAL'],
            'background' => array(233, 233, 233),
            'border' => 1,
            'overflow' => 1,
            'align' => 'L',
            'font-size' => 9,
            'font-style' => 'b',
        ),
        'subtotal2' => array(
            'width' => $widths['subtotal2'],
            'value' => $calc['subtotal_pln'] . ' PLN',
            'border' => 1,
            'background' => array(233, 233, 233),
            'overflow' => 1,
            'align' => 'R',
            'font-size' => 9,
            'font-style' => 'b',
        ),
    );

    $pdf->DrawTable($table);
}
*/

if ($calc['vats']) {
    $starttab = $pdf->GetY() + 4;
    $pdf->SetY($starttab);

    $table = array();

    $table[] = array(
        'fix' => array(
            'width' => $widths['fix'],
            'value' => '',
            'border' => 0,
            'overflow' => 1,
            'align' => 'C',
            'font-style' => 'b',
            'font-size' => 7,
        ),
        'subtotal1' => array(
            'width' => $widths['subtotal1'],
            'value' => $mod_strings['LBL_PDF_TOTAL'],
            'background' => array(255, 255, 255),
            'border' => 1,
            'overflow' => 1,
            'align' => 'L',
            'font-size' => 9,
        ),
        'subtotal2' => array(
            'width' => $widths['subtotal2'],
            'value' => $calc['subtotal'] . ' ' . $this->currency_symbol,
            'border' => 1,
            'overflow' => 1,
            'align' => 'R',
            'font-size' => 9,
        ),
    );
   
if ($this->invoice_type!='U') {
    foreach ($calc['vats'] as $key => $value) {
        $r = mysql_fetch_array(mysql_query("select name from ecmvats where id='" . $key . "'"));
        $vvn = $r['name'];
        $table[] = array(
            'fix' => array(
                'width' => $widths['fix'],
                'value' => '',
                'border' => 0,
                'overflow' => 1,
                'align' => 'C',
                'font-style' => 'b',
                'font-size' => 7,
            ),
            'subtotal1' => array(
                'width' => $widths['subtotal1'],
                'value' => $mod_strings['LBL_PDF_VAT'] . ' (' . $vvn . ')',
                'background' => array(255, 255, 255),
                'border' => 1,
                'overflow' => 1,
                'align' => 'L',
                'font-size' => 9,
            ),
            'subtotal2' => array(
                'width' => $widths['subtotal2'],
                'value' => $value . ' ' . $this->currency_symbol,
                'border' => 1,
                'overflow' => 1,
                'align' => 'R',
                'font-size' => 9,
            ),
        );
    }
	
    $table[] = array(
        'fix' => array(
            'width' => $widths['fix'],
            'value' => '',
            'border' => 0,
            'overflow' => 1,
            'align' => 'C',
            'font-style' => 'b',
            'font-size' => 7,
        ),
        'subtotal1' => array(
            'width' => $widths['subtotal1'],
            'value' => $mod_strings['LBL_PDF_END_TOTAL'],
            'background' => array(233, 233, 233),
            'border' => 1,
            'overflow' => 1,
            'align' => 'L',
            'font-size' => 9,
            'font-style' => 'b',
        ),
        'subtotal2' => array(
            'width' => $widths['subtotal2'],
            'value' => $calc['total'] . ' ' . $this->currency_symbol ,
            'border' => 1,
            'background' => array(233, 233, 233),
            'overflow' => 1,
            'align' => 'R',
            'font-size' => 9,
            'font-style' => 'b',
        ),
    );
}
    $pdf->DrawTable($table);
    
}
