<?php

global $app_list_strings, $mod_strings;

$mod_strings = return_module_language('pl_pl', 'EcmInvoiceOuts', true);
$mod_strings2 = return_module_language('en_us', 'EcmInvoiceOuts', true);

/*
require 'modules/EcmInvoiceOuts/PDFLabelList.php';

$a = $a2 = array();

foreach($PDFLabelList as $k => $v) {
    $a['labels'][] = array(
        'index' => strval($k + 1), 
        'label' => $v,
        'translation' => $mod_strings[$v],
    );
    
    $a2['labels'][] = array(
        'index' => strval($k + 1), 
        'label' => $v,
        'translation' => $mod_strings2[$v],
    );
}

// old
$x = ' ';
// pl
$y = ' ';
// en
$z = ' ';

//echo '<pre>' . var_export($a, true) . '</pre>';
//echo '<pre>' . var_export($a2, true) . '</pre>';
//echo '<pre>' . var_export(unserialize(base64_decode($x)), true) . '</pre>';exit;
//echo '<pre>' . var_export(unserialize(base64_decode($y)), true) . '</pre>';
//echo '<pre>' . var_export(unserialize(base64_decode($z)), true) . '</pre>';
echo '<pre>' . base64_encode(serialize($a)) . '</pre>';
echo '<pre>' . base64_encode(serialize($a2)) . '</pre>';
exit;
*/

$db = $GLOBALS['db'];

$address = 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.$mod_strings['LBL_PDF_VATID']." ".$this->to_vatid;
//}

if ($this->to_nip) {
    $address .= PHP_EOL . $mod_strings['LBL_PDF_NIP'] . '/' . $mod_strings2['LBL_PDF_NIP'] . ": " . $this->to_nip;
} elseif (!$this->to_nip && $this->to_vatid) {
    $address .= PHP_EOL . $mod_strings['LBL_PDF_NIP'] . '/' . $mod_strings2['LBL_PDF_NIP'] . ": " . $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, 57);

$pdf->SetFont('arialpl', 'B', 8);
$pdf->Cell(35, 1, $mod_strings['LBL_PDF_SELLER'] . '/' . $mod_strings2['LBL_PDF_SELLER'], 0, 0, 'L', 1);

$pdf->Ln(3);

$pdf->SetX(25);
$pdf->SetFont('arialpl', '', 8);

$qr = "SELECT nip FROM accounts WHERE id = '" . $pdf->edt->account_id . "'";
$r = $db->fetchByAssoc($db->query($qr));

$wys = $pdf->edt->name . PHP_EOL;

if ($pdf->edt->footer_address) {
    $wys .= $pdf->edt->footer_address . PHP_EOL;
}

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

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

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$pdf->SetXY(81, 57);
$pdf->SetFont('arialpl', 'B', 8);

$pdf->Cell(35, 1, $mod_strings['LBL_PDF_BUYER'] . '/' . $mod_strings2['LBL_PDF_BUYER'], 0, 0, 'L', 1);

$pdf->Ln(3);

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

if ($this->type == "correct") {
    $qr = "SELECT wz_id FROM ecminvoiceouts WHERE id = '" . $this->ecminvoiceout_id . "'";
    $r = $db->fetchByAssoc($db->query($qr));

    $wz_id = $r['wz_id'];
} else {
    $wz_id = $this->wz_id;
}

$qr = "SELECT * FROM ecmstockdocouts WHERE id='" . $wz_id . "'";
$r = $db->fetchByAssoc($db->query($qr));

if ($r['parent_id'] == $this->parent_id) {
    $qr = "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 . "'";
    $r = $db->fetchByAssoc($db->query($qr));
}

$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 ($r['to_nip']) {
//    $address .= PHP_EOL . $mod_strings['LBL_PDF_NIP'] . ": " . $r['to_nip'];
//} elseif (!$r['to_nip'] && $r['to_vatid']) {
//    $address .= PHP_EOL . $mod_strings['LBL_PDF_NIP'] . ": " . $r['to_vatid'];
//}
//
//$rr = $db->fetchByAssoc($db->query("SELECT iln FROM accounts WHERE id='" . $r['parent_id'] . "'"));
//
//if ($rr['iln']) {
//    $address .= "\nILN: " . $rr['iln'];
//}

if ($address) {
    $pdf->SetXY(143, 57);

    $pdf->SetFont('arialpl', 'B', 8);
    $pdf->Cell(35, 1, $mod_strings['LBL_PDF_DELIVERY'] . '/' . $mod_strings2['LBL_PDF_DELIVERY'], 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;

$pc = null;

if ($this->ecmpaymentcondition_id) {
    require_once("modules/EcmPaymentConditions/EcmPaymentCondition.php");
    $pc = new EcmPaymentCondition();
    $pc->retrieve($this->ecmpaymentcondition_id);
}

$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'] . '/' . $mod_strings2['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'] . '/' . $mod_strings2['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,
);

$table[2]['date_sell'] = array(
    'width' => 20,
    'value' => $mod_strings['LBL_PDF_DATE_SELL'] . '/' . $mod_strings2['LBL_PDF_DATE_SELL'],
    'border' => 0,
    'overflow' => 1,
    'align' => 'L',
    'font-size' => 8,
);

$table[2]['date_sell_value'] = array(
    'width' => 36,
    'value' => $this->sell_date,
    'border' => 0,
    'overflow' => 1,
    'align' => 'L',
    'font-style' => 'b',
    'font-size' => 8,
    'valign' => 'C',
);

//if ($this->ecmpaymentcondition_id) {
//    $table[4]['payment_termin'] = array(
//        'width' => 20,
//        'value' => $mod_strings['LBL_PDF_PAYMENT_TERMIN'] . '/' . $mod_strings2['LBL_PDF_NIP'],
//        'border' => 0,
//        'overflow' => 1,
//        'align' => 'L',
//        'font-size' => 8,
//    );
//
//    $r = $db->fetchByAssoc($db->query("SELECT paid FROM ecminvoiceouts WHERE id='" . $_REQUEST['record'] . "'"));
//
//    if ($r['paid'] == 1) {
//        $paid = "zaplacono";
//    } else {
//        $paid = $pc->days . " " . $mod_strings['LBL_PDF_DAYS'] . '/' . $mod_strings2['LBL_PDF_NIP'];
//    }
//
//    if ($this->document_no == "FV 1421/10") {
//        $table[4]['payment_termin_value'] = array(
//            'width' => 36,
//            'value' => "prepayment",
//            'border' => 0,
//            'overflow' => 1,
//            'align' => 'L',
//            'font-style' => 'b',
//            'font-size' => 8,
//        );
//    } else {
//        $table[4]['payment_termin_value'] = array(
//            'width' => 36,
//            'value' => $paid,
//            'border' => 0,
//            'overflow' => 1,
//            'align' => 'L',
//            'font-style' => 'b',
//            'font-size' => 8,
//        );
//    }
//}
//
//if ($this->ecmpaymentcondition_id) {
//    $table[5]['payment_method'] = array(
//        'width' => 20,
//        'value' => $mod_strings['LBL_PDF_PAYMENT_METHOD'] . '/' . $mod_strings2['LBL_PDF_NIP'],
//        'border' => 0,
//        'overflow' => 1,
//        'align' => 'L',
//        'font-size' => 8,
//    );
//
//    if ($this->document_no == "FV 1421/10") {
//        $table[5]['payment_method_value'] = array(
//            'width' => 25,
//            'value' => "money transfer",
//            'border' => 0,
//            'overflow' => 1,
//            'align' => 'L',
//            'font-style' => 'b',
//            'font-size' => 8,
//        );
//    } else {
//        $table[5]['payment_method_value'] = array(
//            'width' => 25,
//            'value' => $app_list_strings['ecmpaymentconditions_payment_method_dom'][$pc->payment_method],
//            'border' => 0,
//            'overflow' => 1,
//            'align' => 'L',
//            'font-style' => 'b',
//            'font-size' => 8,
//        );
//    }
//}

if ($this->supplier_code) {
    $table[6]['cr'] = array(
        'width' => 20,
        'value' => $mod_strings['LBL_PDF_SUPPLIER_CODE'] . '/' . $mod_strings2['LBL_PDF_SUPPLIER_CODE'],
        'border' => 0,
        'overflow' => 1,
        'align' => 'L',
        'font-size' => 8,
    );
    $table[6]['cr_value'] = array(
        'width' => 25,
        'value' => $this->supplier_code,
        'border' => 0,
        'overflow' => 1,
        'align' => 'L',
        'font-style' => 'b',
        'font-size' => 8,
    );
}

if ($this->type == "normal") {
    if (file_exists("modules/EcmStockDocOuts/EcmStockDocOut.php")) {
        require_once("modules/EcmStockDocOuts/EcmStockDocOut.php");
        $wz = new EcmStockDocOut();
        $wz->retrieve($this->wz_id);
    }

//    $table[7]['wz'] = array(
//        'width' => 20,
//        'value' => $mod_strings['LBL_PDF_TO_WZ'] . '/' . $mod_strings2['LBL_PDF_NIP'],
//        'border' => 0,
//        'overflow' => 1,
//        'align' => 'L',
//        'font-size' => 8,
//    );
//
//    $table[7]['wz_value'] = array(
//        'width' => 25,
//        'value' => $wz->document_no,
//        'border' => 0,
//        'overflow' => 1,
//        'align' => 'L',
//        'font-style' => 'b',
//        'font-size' => 8,
//    );
} else {
    $this->loadEcmInvoiceOut();
    $this->ecminvoiceout->format_all_fields();

    $table[7]['wz'] = array(
        'width' => 20,
        'value' => $mod_strings['LBL_PDF_DOCUMENT_NAME_CORRECT_TO'] . '/' . $mod_strings2['LBL_PDF_DOCUMENT_NAME_CORRECT_TO'],
        'border' => 0,
        'overflow' => 1,
        'align' => 'L',
        'font-size' => 8,
    );
    $table[7]['wz_value'] = array(
        'width' => 25,
        'value' => $this->ecminvoiceout->document_no,
        'border' => 0,
        'overflow' => 1,
        'align' => 'L',
        'font-style' => 'b',
        'font-size' => 8,
    );
}

$r = $db->fetchByAssoc($db->query("SELECT so_id FROM ecminvoiceouts WHERE id = '" . $this->id . "'"));
$this->so_id = $r['so_id'];

if (!$this->so_id) {
    $this->so_id = $_SESSION['invoice_so_id'];
}

$qr = "SELECT wz_id FROM ecminvoiceouts WHERE id = '" . $this->id . "'";
$r = $db->fetchByAssoc($db->query($qr));

$qr = "SELECT so_id FROM ecmstockdocouts WHERE id = '" . $r['wz_id'] . "'";
$r = $db->fetchByAssoc($db->query($qr));

$qr = "SELECT order_no FROM ecmsales WHERE id = '" . $r['so_id'] . "'";
$r = $db->fetchByAssoc($db->query($qr));

if ($r['order_no'] && $this->type != "correct") {
    $table[8]['order_no'] = array(
        'width' => 20,
        'value' => $mod_strings['LBL_PDF_ORDER_NO'] . '/' . $mod_strings2['LBL_PDF_ORDER_NO'],
        'border' => 0,
        'overflow' => 1,
        'align' => 'L',
        'font-size' => 8,
    );
    $table[8]['order_no_value'] = array(
        'width' => 25,
        'value' => $r['order_no'],
        'border' => 0,
        'overflow' => 1,
        'align' => 'L',
        'font-style' => 'b',
        'font-size' => 8,
    );
}

if ($this->type != "normal") {
    if (!$this->so_id) {
        $this->so_id = $_SESSION['invoice_so_id'];
    }

    $qr_so = "SELECT * FROM ecmsales WHERE id='" . $this->so_id . "'";
    $r_so = $db->fetchByAssoc($db->query($qr_so));

    $table[9]['cr'] = array(
        'width' => 20,
        'value' => $mod_strings['LBL_PDF_CORRECT_REASON'] . '/' . $mod_strings2['LBL_PDF_CORRECT_REASON'],
        'border' => 0,
        'overflow' => 1,
        'align' => 'L',
        'font-size' => 8,
    );

    $table[9]['cr_value'] = array(
        'width' => 25,
        'value' => $this->order_no,
        '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);

if ($this->type == "normal") {
    $pdf->MultiCell(80, 7,  $mod_strings['LBL_PDF_INVOICE'] . '/' . $mod_strings2['LBL_PDF_INVOICE'] . ' nr ' . $this->document_no, 0, 'C');
    $pdf->SetFont('arialpl', 'B', 7);
    $pdf->Ln(3);
    $pdf->SetX(60);
} else if ($this->type == "correct") {
    $pdf->MultiCell(80, 7, $mod_strings['LBL_PDF_DOCUMENT_NAME_CORRECT']  . '/' . $mod_strings2['LBL_PDF_DOCUMENT_NAME_CORRECT'] . " " . $this->document_no, 0, 'C');
    $pdf->SetFont('arialpl', 'B', 7);
    $pdf->Ln(3);
    $pdf->SetX(60);
    $pdf->Cell(80, 1, $mod_strings['LBL_PDF_ORIGINAL_COPY'] . '/' . $mod_strings2['LBL_PDF_ORIGINAL_COPY'], 0, 0, 'C', 1);
}

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

$currency = new Currency();

$currency->retrieve($this->currency_id);
$this->currency_symbol = $currency->iso4217;

if($this->currency_symbol == 'PLN') {
    $this->currency_symbol = 'zł';
}

$this->currency_value = $currency->conversion_rate;

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

$summary_offset = 108 + 27;

$widths = array(
    // List.
    'position' => 6,
    'description' => 47,
    'quantity' => 7,
    'unit' => 6,
    'unit_price' => 12,
    'unit_price_total' => 12,
//    'vat_id' => 6,
//    'vat_value' => 10,
    'draw_discount' => 6,
    'total' => 12,
    // Summary.
    'vat_rate' => 12,
    'netto_total' => 12,
//    'vat_total' => 14,
    'brutto_total' => 12,
);

$pdf->Ln(7);

if ($calc['draw_discount']) {
    $widths['description'] -= 4;
    $widths['position'] -= 1;
    $widths['unit'] -= 1;

    $widths['brutto_total'] += $widths['draw_discount'];

    $summary_offset -= 10;
}

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

/* BOF: Position list. */
$list = array();

$list[0]['position'] = array(
    'background' => array(233, 233, 233),
    'width' => $widths['position'],
    'value' => $mod_strings['LBL_PDF_LIST_POSITION'] . '/' . $mod_strings2['LBL_PDF_LIST_POSITION'],
    'border' => 1,
    'overflow' => 1,
    'align' => 'C',
    'font-style' => 'b',
    'font-size' => 8,
);

$list[0]['description'] = array(
    'background' => array(233, 233, 233),
    'width' => $widths['description'],
    'value' => $mod_strings['LBL_PDF_LIST_DESCRIPTION'] . '/' . $mod_strings2['LBL_PDF_LIST_DESCRIPTION'] . PHP_EOL . implode(' - ', array($mod_strings['LBL_PDF_LIST_INDEX'] . '/' . $mod_strings2['LBL_PDF_LIST_INDEX'], $mod_strings['LBL_PDF_LIST_WEIGHT'] . '/' . $mod_strings2['LBL_PDF_LIST_WEIGHT'], $mod_strings['LBL_PDF_LIST_COUNTRY_OF_ORIGIN'] . '/' . $mod_strings2['LBL_PDF_LIST_COUNTRY_OF_ORIGIN'])),
    'border' => 1,
    'overflow' => 1,
    'align' => 'C',
    'font-style' => 'b',
    'font-size' => 7,
);

//$list[0]['tax_code'] = array(
//    'background' => array(233, 233, 233),
//    'width' => $widths['tax_code'],
//    'value' => $mod_strings['LBL_PDF_LIST_TAX_CODE'] . '/' . $mod_strings2['LBL_PDF_LIST_TAX_CODE'],
//    'border' => 1,
//    'overflow' => 1,
//    'align' => 'C',
//    'font-style' => 'b',
//    'font-size' => 7,
//);
//
//$list[0]['recipient_code'] = array(
//    'background' => array(233, 233, 233),
//    'width' => $widths['recipient_code'],
//    'value' => $mod_strings['LBL_PDF_LIST_TAX_CODE'] . '/' . $mod_strings2['LBL_PDF_LIST_TAX_CODE'],
//    'border' => 1,
//    'overflow' => 1,
//    'align' => 'C',
//    'font-style' => 'b',
//    'font-size' => 7,
//);

$list[0]['quantity'] = array(
    'background' => array(233, 233, 233),
    'width' => $widths['quantity'],
    'value' => $mod_strings['LBL_PDF_LIST_QUANTITY'] . '/' . $mod_strings2['LBL_PDF_LIST_QUANTITY'],
    'border' => 1,
    'overflow' => 1,
    'align' => 'C',
    'font-style' => 'b',
    'font-size' => 7,
);

$list[0]['unit'] = array(
    'background' => array(233, 233, 233),
    'width' => $widths['unit'],
    'value' => $mod_strings['LBL_PDF_LIST_UNIT'] . '/' . $mod_strings2['LBL_PDF_LIST_UNIT'],
    'border' => 1,
    'overflow' => 1,
    'align' => 'C',
    'font-style' => 'b',
    'font-size' => 8,
);

$list[0]['unit_price'] = array(
    'background' => array(233, 233, 233),
    'width' => $widths['unit_price'],
    'value' => $mod_strings['LBL_PDF_LIST_UNIT_PRICE'] . '/' . $mod_strings2['LBL_PDF_LIST_UNIT_PRICE'],
    'border' => 1,
    'overflow' => 1,
    'align' => 'C',
    'font-style' => 'b',
    'font-size' => 7,
);

$list[0]['unit_price_total'] = array(
    'background' => array(233, 233, 233),
    'width' => $widths['unit_price_total'],
    'value' => $mod_strings['LBL_PDF_LIST_UNIT_PRICE_TOTAL'] . '/' . $mod_strings2['LBL_PDF_LIST_UNIT_PRICE_TOTAL'],
    'border' => 1,
    'overflow' => 1,
    'align' => 'C',
    'font-style' => 'b',
    'font-size' => 7,
);

if ($calc['draw_discount']) {
    $list[0]['discount'] = array(
        'background' => array(233, 233, 233),
        'width' => $widths['draw_discount'],
        'value' => $mod_strings['LBL_PDF_LIST_DISCOUNT'] . '/' . $mod_strings2['LBL_PDF_LIST_DISCOUNT'],
        'border' => 1,
        'overflow' => 1,
        'align' => 'C',
        'font-style' => 'b',
        'font-size' => 7,
    );
}

//if($calc['draw_vat']) {
//if ($this->currency_symbol != "USD") {
//
//$list[0]['vat_id'] = array(
//    'background' => array(233, 233, 233),
//    'width' => $widths['vat_id'],
//    'value' => $mod_strings['LBL_PDF_LIST_VAT_ID'] . '/' . $mod_strings2['LBL_PDF_LIST_VAT_ID'],
//    'border' => 1,
//    'overflow' => 1,
//    'align' => 'C',
//    'font-style' => 'b',
//    'font-size' => 7,
//);
//
//$list[0]['vat_value'] = array(
//    'background' => array(233, 233, 233),
//    'width' => $widths['vat_value'],
//    'value' => $mod_strings['LBL_PDF_LIST_VAT_VALUE'] . '/' . $mod_strings2['LBL_PDF_LIST_VAT_VALUE'],
//    'border' => 1,
//    'overflow' => 1,
//    'align' => 'C',
//    'font-style' => 'b',
//    'font-size' => 7,
//);
//
//}
//}
//
//if ($this->currency_symbol == "USD") {
//    $list[0]['nw'] = array(
//        'background' => array(233, 233, 233),
//        'width' => 9,
//        'value' => "Netto weight",
//        'border' => 1,
//        'overflow' => 1,
//        'align' => 'C',
//        'font-style' => 'b',
//        'font-size' => 7,
//    );
//
//    $list[0]['bw'] = array(
//        'background' => array(233, 233, 233),
//        'width' => 9,
//        'value' => "Brutto weight",
//        'border' => 1,
//        'overflow' => 1,
//        'align' => 'C',
//        'font-style' => 'b',
//        'font-size' => 7,
//    );
//}

$list[0]['total'] = array(
    'background' => array(233, 233, 233),
    'width' => $widths['total'],
    'value' => $mod_strings['LBL_PDF_LIST_TOTAL'] . '/' . $mod_strings2['LBL_PDF_LIST_TOTAL'],
    'border' => 1,
    'overflow' => 1,
    'align' => 'C',
    'font-style' => 'b',
    'font-size' => 7,
);

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

//$pdf->SetY($pdf->GetY() - $calc['draw_discount'] ? $widths['draw_discount'] : 0);

/* EOF: Positions list. */

//$pdf->Ln(0);
//$table = array();

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

$i = 1;
$netto_weight = $brutto_weight = 0;

if ($this->position_list != '') {
    foreach ($this->position_list AS $position) {
        if ($this->type == "correct") {
            $qrrrr = "SELECT quantity, price FROM ecminvoiceoutitems WHERE id = '" . $position['iid'] . "'";
            $qrrr = "SELECT quantity, price FROM ecminvoiceoutitems WHERE id = '" . $position['item_id'] . "'";

            $rrrr = $db->fetchByAssoc($db->query($qrrrr));
            $rrr = $db->fetchByAssoc($db->query($qrrr));

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

        $list[$i]['position'] = array(
            'width' => $widths['position'],
            'value' => $i,
            'border' => 1,
            'align' => 'R',
            'font-size' => 7,
        );

        $list[$i]['description'] = array(
            'width' => $widths['description'],
            'value' => htmlspecialchars_decode($position['code'] . ' ' . $position['name']) . PHP_EOL . implode(' - ', array($position['product_code'], $position['netto_weight'] . ' KG', $position['origin_country_code'])),
            'border' => 1,
            'align' => 'L',
            'font-size' => 7,
        );

        $netto_weight += $position['netto_weight'];
        //$brutto_weight += $position['brutto_weight'];
        $brutto_weight += ($position['quantity'] * $position['netto_weight']);
        
//        $list[$i]['recipient_code'] = array(
//            'width' => 10,
//            'value' => $p['recipient_code'], 'border' => 1,
//            'align' => 'C',
//            'font-size' => 7,
//        );

        $list[$i]['quantity'] = array(
            'width' => $widths['quantity'],
            'value' => $position['quantity'],
            'border' => 1,
            'align' => 'C',
            'font-size' => 7,
        );

        $list[$i]['unit'] = array(
            'width' => $widths['unit'],
            'value' => $position['unit_id'],
            'border' => 1,
            'align' => 'C',
            'font-size' => 7,
        );

        $list[$i]['unit_price'] = array(
            'width' => $widths['unit_price'],
            'value' => format_number($position['price']) . ' ' . $this->currency_symbol,
            'border' => 1,
            'align' => 'R',
            'font-size' => 7,
        );

        $price_total = $position['price'] * $position['quantity'];

        $list[$i]['unit_price_total'] = array(
            'width' => $widths['unit_price_total'],
            'value' => format_number($price_total) . ' ' . $this->currency_symbol,
            'border' => 1,
            'align' => 'R',
            'font-size' => 7,
        );

        if ($calc['draw_discount']) {
            $list[$i]['draw_discount'] = array(
                'width' => $widths['draw_discount'],
                'value' => format_number($position['draw_discount']),
                'border' => 1,
                'align' => 'R',
                'font-size' => 7,
            );
        }

//        $list[$i]['vat_id'] = array(
//            'width' => $widths['vat_id'],
//            'value' => format_number($position['vat_value']),
//            'border' => 1,
//            'align' => 'R',
//            'font-size' => 7,
//        );
//
//        $vat_value = round($price_total * ($position['vat_value'] / 100), 2);
//
//        $list[$i]['vat_value'] = array(
//            'width' => $widths['vat_value'],
//            'value' => format_number($vat_value) . ' ' . $this->currency_symbol,
//            'border' => 1,
//            'align' => 'R',
//            'font-size' => 7,
//        );
//
//        if ($this->currency_symbol == "USD") {
//            $list[$i]['nw'] = array(
//                'width' => 9,
//                'value' => format_number(floatval(str_replace(",", ".", $p['netto_weight']))),
//                'border' => 1,
//                'overflow' => 1,
//                'align' => 'C',
//                'font-size' => 7,
//            );
//            $list[$i]['bw'] = array(
//                'width' => 9,
//                'value' => format_number(floatval(str_replace(",", ".", $p['brutto_weight']))),
//                'border' => 1,
//                'overflow' => 1,
//                'align' => 'C',
//                'font-size' => 7,
//            );
//
//            $nw += floatval(str_replace(",", ".", $p['netto_weight']));
//            $bw += floatval(str_replace(",", ".", $p['brutto_weight']));
//        }

        $list[$i]['total'] = array(
            'width' => $widths['total'],
            'value' => format_number($price_total + $vat_value) . ' ' . $this->currency_symbol,
            'border' => 1,
            'align' => 'R',
            'font-size' => 7,
        );

        $totals[$position['vat_id']]['price'] += $price_total;
        $totals[$position['vat_id']]['total'] += $price_total + $vat_value;
        $totals[$position['vat_id']]['vat'] += $vat_value;

        $sum_total += $price_total + $vat_value;
        $sum_netto += $price_total;

        if ($this->type == "correct") {
            $i++;

            $qr = "SELECT * FROM ecminvoiceoutitems WHERE id = '" . $position['item_id'] . "'";
            $r = $db->fetchByAssoc($db->query($qr));

            if ($position['id'] == $position['item_id']) {
                $ssss = "t";
            }

            $list[$i]['position'] = array(
                'width' => $widths['position'],
                'value' => '',
                'border' => 1,
                'align' => 'R',
                'font-size' => 7,
            );

            $list[$i]['description'] = array(
                'width' => $widths['description'],
                'value' => "bylo" . $ssss,
                'border' => 1,
                'align' => 'L',
                'font-size' => 7,
            );

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

            $list[$i]['quantity'] = array(
                'width' => $widths['quantity'],
                'value' => (int) $r['quantity'],
                'border' => 1,
                'align' => 'C',
                'font-size' => 7,
            );

            $list[$i]['unit'] = array(
                'width' => $widths['unit'],
                'value' => $lv['ecmproducts_unit_dom'][$r['dd_unit_id']],
                'border' => 1,
                'align' => 'C',
                'font-size' => 7,
            );

            $list[$i]['unit_price'] = array(
                'width' => $widths['unit_price'],
                'value' => format_number($r['price']) . ' ' . $this->currency_symbol,
                'border' => 1,
                'align' => 'R',
                'font-size' => 7,
            );

            $price_total = ($r['price'] - ($r['price'] * $r['discount'] / 100)) * $r['quantity'];

            $list[$i]['unit_price_total'] = array(
                'width' => $widths['unit_price_total'],
                'value' => format_number($price_total) . ' ' . $this->currency_symbol,
                'border' => 1,
                'align' => 'R',
                'font-size' => 7,
            );

            $list[$i]['vat_id'] = array(
                'width' => $widths['vat_id'],
                'value' => format_number($r['ecmvat_value']),
                'border' => 1,
                'align' => 'R',
                'font-size' => 7,
            );

            $vat_value = round($price_total * ($r['ecmvat_value'] / 100), 2);

            $list[$i]['vat_value'] = array(
                'width' => $widths['vat_value'],
                'value' => format_number($vat_value) . ' ' . $this->currency_symbol,
                'border' => 1,
                'align' => 'R',
                'font-size' => 7,
            );

            $list[$i]['total'] = array(
                'width' => $widths['total'],
                'value' => format_number($price_total + $vat_value) . ' ' . $this->currency_symbol,
                'border' => 1,
                'align' => 'R',
                'font-size' => 7,
            );

            $totals[$position['vat_id']]['price'] -= $price_total;
            $totals[$position['vat_id']]['total'] -= ($price_total + $vat_value);
            $totals[$position['vat_id']]['vat'] -= $vat_value;

            $sum_total -= $price_total + $vat_value;
            $sum_netto -= $price_total;
        }

        $i++;
    }
}

$pdf->DrawTable($list, array(0));

/* EOF: Position list. */
$pdf->Ln(1);

$pdf->SetFont('arialpl', '', '6');
$pdf->Cell(30, 4, 'Waga całkowita/Total weight: ', 0, 0, 'L', 1);

$pdf->SetFont('arialpl', 'B', '6');
$pdf->Cell(10, 4, $brutto_weight . ' KG', 0, 0, 'L', 1);

$pdf->Ln(4);

$pr = ($pdf->fw - $pdf->lMargin - $pdf->rMargin) / 100;

//$pdf->SetX($pdf->fw-$pdf->rMargin-$pr*34);

$summary = array();

$i = 0;

if ($calc['vats']) {
//    $summary[] = array(
//        'subtotal1' => array(
//            'width' => 42,
//            'value' => $mod_strings['LBL_PDF_TOTAL'],
//            'background' => array(255, 255, 255),
//            'border' => 1,
//            'overflow' => 1,
//            'align' => 'L',
//            'font-size' => 7,
//        ),
//        'subtotal2' => array(
//            'width' => 14,
//            'value' => $calc['subtotal'] . ' ' . $this->currency_symbol,
//            'border' => 1,
//            'overflow' => 1,
//            'align' => 'R',
//            'font-size' => 7,
//        ),
//    );

    $summary[$i]['vat_rate'] = array(
        'width' => $widths['vat_rate'],
//        'value' => $mod_strings['LBL_PDF_LIST_VAT_RATE'],
        'border' => 0,
        'overflow' => 1,
        'align' => 'C',
        'font-style' => 'b',
        'font-size' => 7,
    );

    $summary[$i]['netto_total'] = array(
        'width' => $widths['netto_total'],
        'value' => $mod_strings['LBL_PDF_LIST_NETTO_TOTAL'] . '/' . $mod_strings2['LBL_PDF_LIST_NETTO_TOTAL'], //$mod_strings['LBL_PDF_LIST_TOTAL_PRICE'],
        'background' => array(255, 255, 255),
        'border' => 1,
        'overflow' => 1,
        'align' => 'C',
        'font-style' => 'b',
        'font-size' => 7,
    );

//    $summary[$i]['vat_total'] = array(
//        'width' => $widths['vat_total'],
//        'value' => $mod_strings['LBL_PDF_LIST_VAT_TOTAL'],
//        'background' => array(255, 255, 255),
//        'border' => 1,
//        'overflow' => 1,
//        'align' => 'C',
//        'font-style' => 'b',
//        'font-size' => 7,
//    );

    $summary[$i]['brutto_total'] = array(
        'width' => $widths['brutto_total'],
        'value' => $mod_strings['LBL_PDF_LIST_BRUTTO_TOTAL'] . '/' . $mod_strings2['LBL_PDF_LIST_BRUTTO_TOTAL'],
        'border' => 1,
        'overflow' => 1,
        'align' => 'C',
        'font-style' => 'b',
        'font-size' => 7,
    );

//    foreach ($calc['vats'] AS $key => $value) {
//        $i++;
//
//        $qr = "SELECT id, name, value FROM ecmvats WHERE id = '" . $key . "'";
//        $r = $db->fetchByAssoc($db->query($qr));
//
//        $vvn = $r['name'];
//
//        $summary[$i]['vat_rate'] = array(
//            'width' => $widths['vat_rate'],
////            'value' => format_number($r['value']),
//            'background' => array(255, 255, 255),
//            'border' => 0,
//            'overflow' => 1,
//            'align' => 'R',
//            'font-size' => 7,
//        );
//
//        $summary[$i]['netto_total'] = array(
//            'width' => $widths['netto_total'],
//            'value' => format_number($totals[$r['id']]['price']) . ' ' . $this->currency_symbol,
//            'background' => array(255, 255, 255),
//            'border' => 1,
//            'overflow' => 1,
//            'align' => 'R',
//            'font-size' => 7,
//        );
//
////        $summary[$i]['vat_total'] = array(
////            'width' => $widths['vat_total'],
////            'value' => format_number($totals[$r['id']]['vat']) . ' ' . $this->currency_symbol,
////            'background' => array(255, 255, 255),
////            'border' => 1,
////            'overflow' => 1,
////            'align' => 'R',
////            'font-size' => 7,
////        );
//
//        $summary[$i]['brutto_total'] = array(
//            'width' => $widths['brutto_total'],
//            'value' => format_number($totals[$r['id']]['total']) . ' ' . $this->currency_symbol,
//            'border' => 1,
//            'overflow' => 1,
//            'align' => 'R',
//            'font-size' => 7,
//        );
//    }
}

//if (isset($calc['discount'])) {
//    $disc = floatval(str_replace(",", ".", str_replace(".", "", $this->discount)));
//
////    $summary[]['total21'] = array(
////        'width' => 35,
////        'value' => 'RAZEM',
////        'background' => array(255, 255, 255),
////        'border' => 1,
////        'overflow' => 1,
////        'align' => 'C',
////        'font-size' => 7,
////        'font-style' => 'b',
////    );
////
////    $summary[]['total22'] = array(
////        'width' => 14,
////        'value' => $calc['total2'],
////        'border' => 1,
////        'overflow' => 1,
////        'align' => 'R',
////        'font-size' => 7,
////    );
//
//    $summary[] = array(
//        'discount1' => array(
//            'width' => 10,
//            'value' => $mod_strings['LBL_PDF_DISCOUNT'],
//            'background' => array(255, 255, 255),
//            'border' => 1,
//            'overflow' => 1,
//            'align' => 'L',
//            'font-size' => 7,
//            'font-style' => 'b',
//        ),
//        'discount2' => array(
//            'width' => 41,
//            'value' => format_number($disc) . ' ' . $this->currency_symbol,
//            'border' => 1,
//            'overflow' => 1,
//            'align' => 'R',
//            'font-size' => 7,
//        ),
//    );
//}
//
//if ($this->currency_symbol == "USD") {
//    $summary[] = array(
//        'total1' => array(
//            'width' => 10,
//            'value' => $mod_strings['LBL_PDF_END_TOTAL'] . '/' . $mod_strings2['LBL_PDF_END_TOTAL'],
//            'border' => 1,
//            'overflow' => 1,
//            'align' => 'C',
//            'font-size' => 7,
//            'font-style' => 'b',
//        ),
//        'total2' => array(
//            'width' => 12,
//            'value' => format_number($sum_netto - floatval(str_replace(",", ".", str_replace(".", "", str_replace("PLN", "", $calc['discount']['value']))))) . ' ' . $this->currency_symbol,
//            'border' => 1,
//            'overflow' => 1,
//            'align' => 'R',
//            'font-size' => 7,
//            'font-style' => 'b',
//        ),
//        'total3' => array(
//            'width' => 9,
//            'value' => format_number($nw),
//            'border' => 1,
//            'overflow' => 1,
//            'align' => 'R',
//            'font-size' => 7,
//            'font-style' => 'b',
//        ),
//        'total4' => array(
//            'width' => 9,
//            'value' => format_number($bw),
//            'border' => 1,
//            'overflow' => 1,
//            'align' => 'R',
//            'font-size' => 7,
//            'font-style' => 'b',
//        ),
//        'total5' => array(
//            'width' => 14,
//            'value' => format_number($sum_total - floatval(str_replace(",", ".", str_replace(".", "", str_replace("PLN", "", $calc['discount']['value']))))) . ' ' . $this->currency_symbol,
//            'border' => 1,
//            'overflow' => 1,
//            'align' => 'R',
//            'font-size' => 7,
//            'font-style' => 'b',
//        ),
//    );
//} else {

$brutto = $sum_total - $disc;
$netto = $sum_netto;
$vvat = $brutto - $netto;


$i++;

$summary[$i]['vat_rate'] = array(
    'background' => array(233, 233, 233),
    'width' => $widths['vat_rate'],
    'value' => $mod_strings['LBL_PDF_LIST_SUMMARY_TOTAL'] . '/' . $mod_strings2['LBL_PDF_LIST_SUMMARY_TOTAL'],
    'border' => 1,
    'overflow' => 1,
    'align' => 'C',
    'font-size' => 7,
    'font-style' => 'b',
);

$summary[$i]['netto_total'] = array(
    'background' => array(233, 233, 233),
    'width' => $widths['netto_total'],
    'value' => format_number($netto) . ' ' . $this->currency_symbol,
    'border' => 1,
    'overflow' => 1,
    'align' => 'R',
    'font-size' => 7,
    'font-style' => 'b',
);

//$summary[$i]['vat_total'] = array(
//    'background' => array(233, 233, 233),
//    'width' => $widths['vat_total'],
//    'value' => format_number($vvat) . ' ' . $this->currency_symbol,
//    'border' => 1,
//    'overflow' => 1,
//    'align' => 'R',
//    'font-size' => 7,
//    'font-style' => 'b',
//);

$summary[$i]['brutto_total'] = array(
    'background' => array(233, 233, 233),
    'width' => $widths['brutto_total'],
    'value' => format_number($brutto) . ' ' . $this->currency_symbol,
    'border' => 1,
    'align' => 'R',
    'font-size' => 7,
    'font-style' => 'b',
);

//}
//
// ~~~~~~~~~~~~~~
//
//$q = 'SELECT i.`document_no` FROM `ecmprepaymentinvoices` AS i WHERE i.`ecmsale_id` IN (SELECT DISTINCT(ii.`parent_doc_id`) FROM `ecminvoiceoutitems` AS ii JOIN `ecminvoiceouts` AS i ON ii.`ecminvoiceout_id` = i.`id` WHERE i.`id` = \'' . $this->id . '\' AND ii.`parent_doc_type` = \'EcmSales\');';
$q = 'SELECT i.`ecmvat_value`, SUM(i.`inv_value`) AS \'inv_value\', SUM((1 - (i.`ecmvat_value` / 100)) * i.`inv_value`) AS \'value\' FROM `ecmprepaymentinvoices` AS i WHERE i.`ecmsale_id` IN (SELECT DISTINCT(ii.`parent_doc_id`) FROM `ecminvoiceoutitems` AS ii JOIN `ecminvoiceouts` AS i ON ii.`ecminvoiceout_id` = i.`id` WHERE i.`id` = \'' . $this->id . '\' AND ii.`parent_doc_type` = \'EcmSales\') GROUP BY i.`ecmvat_value`;';
//$q = 'SELECT i.`document_no`, i.`ecmvat_value`, i.`inv_value`, ((1 - (i.`ecmvat_value` / 100)) * i.`inv_value`) AS `value` FROM `ecmprepaymentinvoices` AS i WHERE i.`ecmsale_id` IN (SELECT DISTINCT(ii.`parent_doc_id`) FROM `ecminvoiceoutitems` AS ii JOIN `ecminvoiceouts` AS i ON ii.`ecminvoiceout_id` = i.`id` WHERE i.`id` = \'' . $this->id . '\' AND ii.`parent_doc_type` = \'EcmSales\');';

$r = $db->query($q);

$document_no = array();
$ecmvat_value = array();
$inv_value = array();
$value = array();

$v = 0.0;

while ($rr = $db->fetchByAssoc($r)) {
    array_push($document_no, $rr['document_no']);
    array_push($ecmvat_value, $rr['ecmvat_value']);
    array_push($inv_value, $rr['inv_value']);
    array_push($value, $rr['value']);

    //echo '<pre>' . var_export($rr, true) . '</pre>';

    extract($rr, EXTR_PREFIX_ALL, 'r');

    $i++;

    $summary[$i]['vat_rate'] = array(
        'width' => $widths['vat_rate'],
//        'value' => format_number($r_ecmvat_value),
        'background' => array(255, 255, 255),
        'border' => 0,
        'overflow' => 1,
        'align' => 'R',
        'font-size' => 7,
    );

    $summary[$i]['netto_total'] = array(
        'width' => $widths['netto_total'],
        'value' => format_number(-$r_value) . ' ' . $this->currency_symbol,
        'background' => array(255, 255, 255),
        'border' => 1,
        'overflow' => 1,
        'align' => 'R',
        'font-size' => 7,
    );

//    $summary[$i]['vat_total'] = array(
//        'width' => $widths['vat_total'],
//        'value' => format_number(-($r_inv_value - $r_value)) . ' ' . $this->currency_symbol,
//        'background' => array(255, 255, 255),
//        'border' => 1,
//        'overflow' => 1,
//        'align' => 'R',
//        'font-size' => 7,
//    );

    $summary[$i]['brutto_total'] = array(
        'width' => $widths['brutto_total'],
        'value' => format_number(-$r_inv_value) . ' ' . $this->currency_symbol,
        'border' => 1,
        'overflow' => 1,
        'align' => 'R',
        'font-size' => 7,
    );
}

if ($value) {
    $i++;

    $summary[$i]['vat_rate'] = array(
        'background' => array(233, 233, 233),
        'width' => $widths['vat_rate'],
        'value' => $mod_strings['LBL_PDF_LIST_SUMMARY_TOTAL'] . '/' . $mod_strings2['LBL_PDF_LIST_SUMMARY_TOTAL'],
        'border' => 1,
        'overflow' => 1,
        'align' => 'C',
        'font-size' => 7,
        'font-style' => 'b',
    );

    $summary[$i]['netto_total'] = array(
        'background' => array(233, 233, 233),
        'width' => $widths['netto_total'],
        'value' => format_number($netto - array_sum($value)) . ' ' . $this->currency_symbol,
        'border' => 1,
        'overflow' => 1,
        'align' => 'R',
        'font-size' => 7,
        'font-style' => 'b',
    );

//    $summary[$i]['vat_total'] = array(
//        'background' => array(233, 233, 233),
//        'width' => $widths['vat_total'],
//        'value' => format_number($vvat - (array_sum($inv_value) - array_sum($value))) . ' ' . $this->currency_symbol,
//        'border' => 1, 'overflow' => 1,
//        'align' => 'R',
//        'font-size' => 7,
//        'font-style' => 'b',
//    );

    $summary[$i]['brutto_total'] = array(
        'background' => array(233, 233, 233),
        'width' => $widths['brutto_total'],
        'value' => format_number($brutto - array_sum($inv_value)) . ' ' . $this->currency_symbol,
        'border' => 1,
        'align' => 'R',
        'font-size' => 7,
        'font-style' => 'b',
    );
}

// ~~~~~~~~~~~~~~

if ($pdf->GetY() + 30 > $pdf->fh - $pdf->bMargin) {
    $pdf->AddPage();
}

//if ($this->currency_symbol == "USD") {
//    $pdf->SetX(103);
//} else {
$pdf->SetX($summary_offset);
//}
//
//echo '<pre>' . var_export($summary, true) . '</pre>';exit;

$pdf->DrawTable($summary);

$pdf->Ln(1);