<?php

global $app_list_strings;

$this->parent_name = htmlspecialchars_decode($this->parent_name);

$address = str_replace("&lt;", "<", str_replace("&gt;", ">", $this->parent_name));

if ($this->parent_contact_name) {
    $address .= "\n" . $this->parent_contact_name;

    if ($this->parent_contact_title) {
        $address .= "\n" . $this->parent_contact_title;
    }

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

if ($this->parent_address_street) {
    $address .= "\n" . $this->parent_address_street;
}

if ($this->parent_address_postalcode) {
    $address .= "\n" . $this->parent_address_postalcode;
}

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

//if($this->to_vatid)
//  $address .= "\n".$mod_strings['LBL_PDF_VATID']." ".$this->to_vatid;

if ($this->to_nip) {
    $address .= "\n" . $mod_strings['LBL_PDF_NIP'] . ": " . $this->to_nip;
} elseif (!$this->to_nip && $this->to_vatid) {
    $address .= "\n" . $mod_strings['LBL_PDF_NIP'] . ": " . $this->to_vatid;
}

$r = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select iln,to_nip from accounts where id='" . $this->parent_id . "'"));

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, "SPRZEDAWCA:", 0, 0, 'L', 1);
$pdf->Ln(3);
$pdf->SetX(25);
$pdf->SetFont('arialpl', '', 8);

$r = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select nip from accounts where id='" . $pdf->edt->account_id . "'"));
$wys = $pdf->edt->name . "\n";

if ($pdf->edt->footer_address) {
    $wys .= $pdf->edt->footer_address . "\n";
}

if ($r['nip']) {
    $wys.= "NIP: " . $r['nip'];
}

$pdf->MultiCell(60, 4, $wys, 0, 'L');

$pdf->SetXY(81, 53);
$pdf->SetFont('arialpl', 'B', 8);
$pdf->Cell(35, 1, "NABYWCA:", 0, 0, 'L', 1);
$pdf->Ln(3);
$pdf->SetX(81);
$pdf->SetFont('arialpl', '', 8);
$pdf->MultiCell(60, 4, $address, 0, 'L');



$r = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select * from ecmstockdocouts where id='" . $wz_id . "'"));

if ($r['parent_id'] == $this->parent_id) {
    $r = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select name as parent_name,shipping_address_street as parent_address_street,shipping_address_city as parent_address_city,shipping_address_postalcode as parent_address_postalcode,shipping_address_country as parent_address_country from accounts where id='" . $this->parent_id . "'"));
}

$address = "";
$address = str_replace("&lt;", "<", str_replace("&gt;", ">", $r['parent_name']));

if ($r['parent_address_street']) {
    $address .= "\n" . $r['parent_address_street'];
}

if ($r['parent_address_postalcode']) {
    $address .= "\n" . $r['parent_address_postalcode'];
}

if ($r['parent_address_city']) {
    $address .= " " . $r['parent_address_city'];
}

if ($r['to_nip']) {
    $address .= "\n" . $mod_strings['LBL_PDF_NIP'] . ": " . $r['to_nip'];
} elseif (!$r['to_nip'] && $r['to_vatid']) {
    $address .= "\n" . $mod_strings['LBL_PDF_NIP'] . ": " . $r['to_vatid'];
}

$rr = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select iln from accounts where id='" . $r['parent_id'] . "'"));

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

if ($address) {
    $pdf->SetXY(143, 53);
    $pdf->SetFont('arialpl', 'B', 8);
    $pdf->Cell(35, 1, "DOSTAWA:", 0, 0, 'L', 1);
    $pdf->Ln(3);
    $pdf->SetX(143);
    $pdf->SetFont('arialpl', '', 8);
    $pdf->MultiCell(60, 4, $address, 0, 'L');
}

$ynew = $pdf->GetY() + 12;



$pdf->SetXY(160, $old_y + 15 + $personplus);

$table = array();

// $table [0]['place_of_register'] = array('width' => 20, 'value' => $mod_strings['LBL_PDF_PLACE_OF_REGISTER'], 'border' => 0, 'overflow' => 1, 'align' => 'L', 'font-size' => 8);
// $table [0]['place_of_register_value'] = array('width' => 25, 'value' => $this->template->place_of_register, 'border' => 0, 'overflow' => 1, 'align' => 'L', 'font-style' => 'b', 'font-size' => 8);

$table [1]['date_reg'] = array(
    'width' => 20,
    'value' => $mod_strings['LBL_PDF_DATE_REGISTER'],
    'border' => 0,
    'overflow' => 1,
    'align' => 'L',
    'font-size' => 8,
);
$table [1]['date_reg_value'] = array(
    'width' => 36,
    'value' => $this->register_date,
    'border' => 0,
    'overflow' => 1,
    'align' => 'L',
    'font-style' => 'b',
    'font-size' => 8,
);

$pdf->SetXY(143, $pdf->tMargin + 20);
$pdf->DrawTable($table, null, 0.5, 0.5, 3);

$pdf->Ln(9);

$pdf->SetFont('arialpl', '', '18');
$pdf->SetXY(60, $pdf->tMargin + 25);


    $pdf->MultiCell(80, 7, "Specyfikacja WZ do FV " . $this->document_no, 0, 'C');
    $pdf->SetFont('arialpl', 'B', 7);
    $pdf->Ln(3);
    $pdf->SetX(60);


$this->loadParser();

$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);

$pdf->SetY($ynew);

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

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->getPositionList(true);
}

$calc = $this->calculate($this->position_list, true);
//$this->position_list = $this->formatPositions($this->position_list);

$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;
    }
}

$pdf->Ln(7);
$name_w = 80;
//if($calc['draw_discount']) $name_w -= 8;
//if($calc['draw_vat']) $name_w -= 8;
$table = array();

$table [0]['position'] = array(
    'background' => array(233, 233, 233),
    'width' => 6,
    'value' => $mod_strings['LBL_PDF_LIST_POSITION'],
    'border' => 1,
    'overflow' => 1,
    'align' => 'C',
    'font-style' => 'b',
    'font-size' => 8,
);
$table [0]['description'] = array(
    'background' => array(233, 233, 233),
    'width' => $name_w+2,
    'value' => 'Nazwa',
    'border' => 1,
    'overflow' => 1,
    'align' => 'C',
    'font-style' => 'b',
    'font-size' => 7,
);

//$table [0]['tax_code'] = array('width' => 7, 'value' => $mod_strings['LBL_PDF_LIST_TAX_CODE'],'border' => 1, 'overflow' => 1, 'align' => 'C', 'font-style' => 'b', 'font-size' => 7);
//$table [0]['recipient_code'] = array('width' => 10, 'value' => $mod_strings['LBL_PDF_LIST_TAX_CODE'],'border' => 1, 'overflow' => 1, 'align' => 'C', 'font-style' => 'b', 'font-size' => 7);

$table [0]['quantity'] = array(
    'background' => array(233, 233, 233),
    'width' => 9,
    'value' => $mod_strings['LBL_PDF_LIST_QUANTITY'],
    'border' => 1,
    'overflow' => 1,
    'align' => 'C',
    'font-style' => 'b',
    'font-size' => 7,
);
$table [0]['unit'] = array(
    'background' => array(233, 233, 233),
    'width' => 7,
    'value' => "J.m.",
    'border' => 1,
    'overflow' => 1,
    'align' => 'C',
    'font-style' => 'b',
    'font-size' => 8,
);

if ($this->type != "normal") {
    $pdf->SetY(76);
}

$pdf->DrawTable($table, array(0));
$pdf->Ln(0);

$i = 1;

$table = array();

$lv = return_app_list_strings_language($this->ecmlanguage);

if ($this->position_list != '') {
    foreach ($this->position_list as $p) {
        if ($this->type == "correct") {
            $rrrr = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select quantity,price from ecminvoiceoutitems where id='" . $p['iid'] . "'"));
            $rrr = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select quantity,price from ecminvoiceoutitems where id='" . $p['item_id'] . "'"));

            if ((float) $rrrr['quantity'] == (float) $rrr['quantity'] && (float) $rrrr['price'] == (float) $rrr['price']) {
                continue;
            }
        }

        $j++;

        $table [$i]['position'] = array(
            'width' => 6,
            'value' => $j,
            'border' => 1,
            'align' => 'R',
            'font-size' => 7,
        );
        $table [$i]['description'] = array(
            'width' => $name_w + 2,
            'value' => htmlspecialchars_decode($p['code'] . ' ' . $p['name']),
            'border' => 1,
            'align' => 'L',
            'font-size' => 7,
        );

        //$table [$i]['recipient_code'] = array('width' => 10, 'value' => $p['recipient_code'],'border' => 1, 'align' => 'C', 'font-size' => 7);

        $table [$i]['quantity'] = array(
            'width' => 9,
            'value' => $p['quantity'],
            'border' => 1,
            'align' => 'C',
            'font-size' => 7,
        );
        $table [$i]['unit'] = array(
            'width' => 7,
            'value' => $p['unit_id'],
            'border' => 1,
            'align' => 'C',
            'font-size' => 7,
        );

        $i++;
    }
}

$pdf->DrawTable($table, array(0));
//total, vat, subtotal

$pdf->Ln(4);
$pr = ($pdf->fw - $pdf->lMargin - $pdf->rMargin) / 100;
//$pdf->SetX($pdf->fw-$pdf->rMargin-$pr*34);
$table = array();
$pdf->Ln(1);