<?php

//echo __FILE__; exit;

global $app_list_strings;

$eml = return_module_language('pl_pl', 'EcmInvoiceOuts');

//echo '<pre>' . var_export($mod_strings, true) . '</pre>';
//echo '<pre>' . var_export($eml, true) . '</pre>'; exit;

$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/SUPPLIER:", 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/PAYER:", 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") {
    $r = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select wz_id from ecminvoiceouts where id='" . $this->ecminvoiceout_id . "'"));
    $wz_id = $r['wz_id'];
} else {
    $wz_id = $this->wz_id;
}

$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;", ">", $this->parent_shipping_address_name));

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

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

if ($this->parent_shipping_address_city) {
    $address .= " " . $this->parent_shipping_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'];
}
*/

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

if ($address) {
    $pdf->SetXY(143, 53);
    $pdf->SetFont('arialpl', 'B', 8);
    $pdf->Cell(35, 1, "DOSTAWA/SHIPPING:", 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;

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'], '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'] . '/ Date of invoice',
    '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_SELL_DATE'] . '/ Date of sale' , //"Data dokonania, lub zakonczenia dostawy towarów, lub wykonania usługi.",
    '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'],
        'border' => 0,
        'overflow' => 1,
        'align' => 'L',
        'font-size' => 8,
    );

    $r = $GLOBALS['db']->fetchByAssoc($GLOBALS['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'];
    }

    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'],
        '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'],
        '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'], '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'],
        '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 = $GLOBALS['db']->fetchByAssoc($GLOBALS['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'];
}

$r = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select wz_id from ecminvoiceouts where id='" . $this->id . "'"));
$r = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select so_id from ecmstockdocouts where id='" . $r['wz_id'] . "'"));
$r = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select order_no from ecmsales where id='" . $r['so_id'] . "'"));

if ($r['order_no'] && $this->type != "correct") {
    $table [8]['order_no'] = array(
        'width' => 20,
        'value' => $mod_strings['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'];
    }

    $r_so = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select * from ecmsales where id='" . $this->so_id . "'"));

    $table [9]['cr'] = array(
        'width' => 20,
        'value' => $mod_strings['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, "Faktura/Invoice " . $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'] . " " . $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'], 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');

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

$this->currency_symbol = "USD";

//$pdf->Ln(2);
//if($calc['draw_discount']) $name_w -= 8;
//if($calc['draw_vat']) $name_w -= 8;

/* EOF: Items table */
$name_w = 20;

$body = array();

$body[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,
);

$body[0]['description'] = array(
    'background' => array(233, 233, 233),
    'width' => $name_w + 2 + 18,
    //'value' => 'Nazwa' . PHP_EOL . 'Kod - Waga - Kraj pochodzenia',
    //'value' => 'Name' . PHP_EOL . 'Code - Weight - Country of origin',
    'value' => 'Nazwa/Name' . PHP_EOL . 'Kod/Code - Waga/Weight - Kraj pochodzenia/Country of origin',
    'border' => 1,
    'overflow' => 1,
    'align' => 'C',
    'font-style' => 'b',
    'font-size' => 7,
);

//$body[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);
//$body[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);

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

$body[0]['unit'] = array(
    'background' => array(233, 233, 233),
    'width' => 7,
    'value' => 'J.m./Size',
    'border' => 1,
    'overflow' => 1,
    'align' => 'C',
    'font-style' => 'b',
    'font-size' => 8,
);

$body[0]['unit_price'] = array(
    'background' => array(233, 233, 233),
    'width' => 12,
    'value' => 'Cena netto/Price netto',
    'border' => 1,
    'overflow' => 1,
    'align' => 'C',
    'font-style' => 'b',
    'font-size' => 7,
);

$body[0]['unit_price_total'] = array(
    'background' => array(233, 233, 233),
    'width' => 14,
    'value' => 'Wartość netto/Total netto',
    'border' => 1,
    'overflow' => 1,
    'align' => 'C',
    'font-style' => 'b',
    'font-size' => 7,
);

/*
if($calc['draw_discount']) {
	$body[0]['discount'] = array('width' => 5, 'value' => $mod_strings['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") {
		$body[0]['vat_id'] = array(
			'background' => array(233, 233, 233),
			'width' => 5,
			'value' => 'VAT (%)',
			'border' => 1,
			'overflow' => 1,
			'align' => 'C',
			'font-style' => 'b',
			'font-size' => 7,
		);
		$body[0]['vat_value'] = array(
			'background' => array(233, 233, 233),
			'width' => 13,
			'value' => $mod_strings['LBL_PDF_LIST_VAT_VALUE'],
			'border' => 1,
			'overflow' => 1,
			'align' => 'C',
			'font-style' => 'b',
			'font-size' => 7,
		);
	}
//}
*/

//if ($this->currency_symbol == "USD") {
	/*
    $body[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,
    );
    $body[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,
    );
	*/
//} else
{
	// FIX width (9 + 9).
	//$body[0]['description']['width'] += 18;
	//$body[0]['unit_price_total']['width'] += 4;
	//$body[0]['unit_price']['width'] += 4;
}

$body[0]['total'] = array(
    'background' => array(233, 233, 233),
    'width' => 14,
    'value' => 'Wartość brutto/Total brutto',
    'border' => 1,
    'overflow' => 1,
    'align' => 'C',
    'font-style' => 'b',
    'font-size' => 7,
);

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

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

$lv = return_app_list_strings_language('en_us');


$i = 1;

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

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

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

        $j++;

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

		$dp = array();

		if(strlen($pc = $position['product_code']) > 0)
			$dp[] = $pc;

		if(strlen($nw = $position['netto_weight']. 'KG') > 0)
			$dp[] = $nw;

		if(strlen($occ = $position['origin_country_code']) > 0)
			$dp[] = $occ;

        $description = htmlspecialchars_decode($position['code'] . ' ' . $position['name']) . PHP_EOL; // htmlspecialchars_decode($p['code'] . ' ' . $p['name'])
		//$description = htmlspecialchars_decode($p['code'] . ' ' . $p['name']) . PHP_EOL . PHP_EOL;
		$description .= implode(' - ', $dp);

        $body[$i]['description'] = array(
            'width' => $name_w + 2,
            'value' => $description,
			'border' => 1,
            'align' => 'L',
            'font-size' => 7,
        );

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

        $body[$i]['quantity'] = array(
            'width' => 9,
            'value' => $position['quantity'], //. PHP_EOL . PHP_EOL . $p['netto_weight'],
            'border' => 1,
            'align' => 'C',
            'font-size' => 7,
        );

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

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

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

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

		/*
        if ($this->currency_symbol != "USD") {
            $body[$i]['vat_id'] = array(
                'width' => 5,
                'value' => format_number($p['vat_value']),
                'border' => 1,
                'align' => 'R',
                'font-size' => 7,
            );

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

            $body[$i]['vat_value'] = array(
                'width' => 13,
                'value' => format_number($vat_value) . ' ' . $this->currency_symbol,
                'border' => 1,
                'align' => 'R',
                'font-size' => 7,
            );
        }
		*/

        //if ($this->currency_symbol == "USD") {
			/*
            $body[$i]['nw'] = array(
                'width' => 9,
                'value' => format_number(floatval(str_replace(",", ".", $p['netto_weight']))),
                'border' => 1,
                'overflow' => 1,
                'align' => 'C',
                'font-size' => 7,
            );
            $body[$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']));
			*/
        //} else
		{
			// FIX width (9 + 9).
			$body[$i]['description']['width'] += 18;
			//$body[$i]['unit_price_total']['width'] += 4;
			//$body[$i]['unit_price']['width'] += 4;
		}

        $body[$i]['total'] = array(
            'width' => 14,
            '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++;
            $r = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select * from ecminvoiceoutitems where id='" . $position['item_id'] . "'"));

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

            $body[$i]['position'] = array(
                'width' => 4,
                'value' => '',
                'border' => 1,
                'align' => 'R',
                'font-size' => 7,
            );
            $body[$i]['description'] = array(
                'width' => $name_w + 2 + 18,
                'value' => "bylo" . $ssss,
                'border' => 1,
                'align' => 'L',
                'font-size' => 7,
            );
            $body[$i]['recipient_code'] = array(
                'width' => 10,
                'value' => "",
                'border' => 1,
                'align' => 'C',
                'font-size' => 7,
            );
            $body[$i]['quantity'] = array(
                'width' => 7,
                'value' => (int) $r['quantity'],
                'border' => 1,
                'align' => 'C',
                'font-size' => 7,
            );
            $body[$i]['unit'] = array(
                'width' => 5,
                'value' => $lv['ecmproducts_unit_dom'][$r['dd_unit_id']],
                'border' => 1,
                'align' => 'C',
                'font-size' => 7,
            );
            $body[$i]['unit_price'] = array(
                'width' => 10,
                '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'];

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

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

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

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

            $body[$i]['total'] = array(
                'width' => 14,
                '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++;
    }
}

//exit;

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

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


$ftable = array();

	$ftable[0]['vat_rate'] = array(
		'width' => 10,
		//'value' => 'VAT (%)',
		'border' => 1,
		'overflow' => 1,
		'align' => 'C',
		'font-style' => 'b',
		'font-size' => 7,
		'border' => 0,
	);

	$ftable[0]['netto_total'] = array(
		'width' => 12,
		'value' => $mod_strings['LBL_PDF_LIST_TOTAL_PRICE'] . '/Total netto',
		'background' => array(255, 255, 255),
		'border' => 1,
		'overflow' => 1,
		'align' => 'C',
		'font-style' => 'b',
		'font-size' => 7,
	);

	/*
	$ftable[0]['vat_total'] = array(
		'width' => 13,
		'value' => "Suma VAT",
		'background' => array(255, 255, 255),
		'border' => 1,
		'overflow' => 1,
		'align' => 'C',
		'font-style' => 'b',
		'font-size' => 7,
	);
	*/

	if($this->currency_symbol == 'USD') {
		/*
        $ftable[0]['total3'] = array(
            'width' => 9,
            'value' => 'NW',
            'border' => 1,
            'overflow' => 1,
            'align' => 'R',
            'font-size' => 7,
            'font-style' => 'b',
        );

        $ftable[0]['total4'] = array(
            'width' => 9,
            'value' => 'BW',
            'border' => 1,
            'overflow' => 1,
            'align' => 'R',
            'font-size' => 7,
            'font-style' => 'b',
        );
		*/
	} else {
		// FIX width.

	}

	$ftable[0]['vat2'] = array(
		'width' => 14,
		'value' => 'Wartość brutto/Total brutto',
		'border' => 1,
		'overflow' => 1,
		'align' => 'C',
		'font-style' => 'b',
		'font-size' => 7,
	);

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

// Not here. @ u
unset($calc['vats']);

if ($calc['vats']) {
    /* $ftable[] = 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), );
     */


	$i = 1;

        // Not here. @ u
    foreach ($calc['vats'] as $key => $value) {
	    $r = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select id,name,value from ecmvats where id='" . $key . "'"));

        $vvn = $r['name'];

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

		$ftable[$i]['netto_total'] = array(
			'width' => 12,
			'value' => format_number($totals[$r['id']]['price']) . ' ' . $this->currency_symbol,
			'background' => array(255, 255, 255),
			'border' => 1,
			'overflow' => 1,
			'align' => 'R',
			'font-size' => 7,
		);

		/*
		$ftable[$i]['vat_total'] = array(
			'width' => 13,
			'value' => format_number($totals[$r['id']]['vat']) . ' ' . $this->currency_symbol,
			'background' => array(255, 255, 255),
			'border' => 1,
			'overflow' => 1,
			'align' => 'R',
			'font-size' => 7,
		);
		*/

		if($this->currency_symbol == 'USD') {
			/*
			$ftable[$i]['total3'] = array(
				'width' => 9,
				'value' => format_number(0),
				'border' => 1,
				'overflow' => 1,
				'align' => 'C',
				'font-size' => 7,
			);

			$ftable[$i]['total4'] = array(
				'width' => 9,
				'value' => format_number(0),
				'border' => 1,
				'overflow' => 1,
				'align' => 'C',
				'font-size' => 7,
			);
			*/
		} else {
			// FIX width.

		}

		$ftable[$i]['vat2'] = array(
			'width' => 14,
			'value' => format_number($totals[$r['id']]['total']) . ' ' . $this->currency_symbol,
			'border' => 1,
			'overflow' => 1,
			'align' => 'R',
			'font-size' => 7,
        );

		$i++;
    }
	/*
	*/
}

if (isset($calc['discount'])) {
    $disc = floatval(str_replace(",", ".", str_replace(".", "", $this->discount)));

    $ftable[] = array(
        'total21' => array(
            'width' => 35,
            'value' => 'Razem/Total',//$mod_strings['LBL_PDF_TOTAL'],
            'background' => array(255, 255, 255),
            'border' => 1,
            'overflow' => 1,
            'align' => 'L',
            'font-size' => 7,
            'font-style' => 'b',
        ),

        'total22' => array(
            'width' => 14,
            'value' => $calc['total2'],
            'border' => 1,
            'overflow' => 1,
            'align' => 'R',
            'font-size' => 7,
        ),
    );

    $ftable[] = array(
        'discount1' => array(
            'width' => 35,
            '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' => 14,
            'value' => format_number($disc) . ' ' . $this->currency_symbol,
            'border' => 1,
            'overflow' => 1,
            'align' => 'R',
            'font-size' => 7,
        ),
    );
}

//if ($this->currency_symbol == "USD") {
	/*
    $ftable[] = array(
        'total1' => array(
            'background' => array(233, 233, 233),
            'width' => 10,
            'value' => $mod_strings['LBL_PDF_END_TOTAL'],
            'border' => 1,
            'overflow' => 1,
            'align' => 'L',
            'font-size' => 7,
            'font-style' => 'b',
        ),
        'total2' => array(
            'background' => array(233, 233, 233),
            '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(
            'background' => array(233, 233, 233),
            'width' => 9,
            'value' => format_number($nw),
            'border' => 1,
            'overflow' => 1,
            'align' => 'R',
            'font-size' => 7,
            'font-style' => 'b',
        ),
        'total4' => array(
            'background' => array(233, 233, 233),
            'width' => 9,
            'value' => format_number($bw),
            'border' => 1,
            'overflow' => 1,
            'align' => 'R',
            'font-size' => 7,
            'font-style' => 'b',
        ),
        'total5' => array(
            'background' => array(233, 233, 233),
            '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;

    $ftable[] = array(
        'total1' => array(
            'background' => array(233, 233, 233),
            'width' => 10,
            'value' => 'Razem/Total',//$mod_strings['LBL_PDF_END_TOTAL'],
            'border' => 1,
            'overflow' => 1,
            'align' => 'L',
            'font-size' => 7,
            'font-style' => 'b',
        ),
        'total2' => array(
            'background' => array(233, 233, 233),
            'width' => 12,
            'value' => format_number($netto) . ' ' . $this->currency_symbol,
            'border' => 1,
            'overflow' => 1,
            'align' => 'R',
            'font-size' => 7,
            'font-style' => 'b',
        ),
		/*
        'total3' => array(
            'background' => array(233, 233, 233),
            'width' => 13,
            'value' => format_number($vvat) . ' ' . $this->currency_symbol,
            'border' => 1, 'overflow' => 1,
            'align' => 'R',
            'font-size' => 7,
            'font-style' => 'b',
        ),
		*/
        'total4' => array(
            'background' => array(233, 233, 233),
            'width' => 14,
            'value' => format_number($brutto) . ' ' . $this->currency_symbol,
            'border' => 1,
            //'overflow' => 1,
            'align' => 'R',
            'font-size' => 7,
            'font-style' => 'b',
        ),
    );
}


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

$db = $GLOBALS['db'];

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

exit;

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']);
	
    extract($rr, EXTR_PREFIX_ALL, 'r');

	$table [] = array(
		/*
        'vat_rate' => array(
			'width' => 10,
			'value' => format_number($r_ecmvat_value),
			'background' => array(255, 255, 255),
			'border' => 1,
			'overflow' => 1,
			'align' => 'R',
			'font-size' => 7,
		),
        */
		'netto_total' => array(
			'width' => 12,
			'value' => format_number(-$r_value) . ' ' . $this->currency_symbol,
			'background' => array(255, 255, 255),
			'border' => 1,
			'overflow' => 1,
			'align' => 'R',
			'font-size' => 7,
		),
        /*
		'vat_total' => array(
			'width' => 13,
			'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,
		),
        */
		'vat2' => array(
			'width' => 14,
			'value' => format_number(-$r_inv_value) . ' ' . $this->currency_symbol,
			'border' => 1,
			'overflow' => 1,
			'align' => 'R',
			'font-size' => 7,
		),
	);
}

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

if($value) {
    $table [] = array(
        'total1' => array(
            'background' => array(233, 233, 233),
            'width' => 10,
            'value' => 'RAZEM',
            'border' => 1,
            'overflow' => 1,
            'align' => 'C',
            'font-size' => 7,
            'font-style' => 'b',
        ),
        'total2' => array(
            'background' => array(233, 233, 233),
            'width' => 14,//
            'value' => format_number($netto - array_sum($value)) . ' ' . $this->currency_symbol,
            'border' => 1,
            'overflow' => 1,
            'align' => 'R',
            'font-size' => 7,
            'font-style' => 'b',
        ),
        /*
        'total3' => array(
            'background' => array(233, 233, 233),
            'width' => 13,//
            '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',
        ),
        */
        'total4' => array(
            'background' => array(233, 233, 233),
            'width' => 14,
            'value' => format_number($brutto - array_sum($inv_value)) . ' ' . $this->currency_symbol,
            'border' => 1,
            //'overflow' => 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
{
	// FIX witdh +13
    $pdf->SetX(135);
}

$pdf->DrawTable($ftable);
$pdf->Ln(1);