<?php

//payment conditions
//
//if (!empty($this->ecmpaymentcondition_text)) {
//    $pdf->Ln(4);
//    $pdf->SetFont('arialpl', '', '8');
//    $pdf->MultiCell(0, 4, $this->ecmpaymentcondition_text, 0, 'L');
//    $pdf->Ln();
//}
//
//if ($this->type == "correct") {
//    $pdf->SetFont('arialpl', 'b', '12');
//    $pdf->Ln(3);
//    $pdf->MultiCell(0, 4, $mod_strings['LBL_PDF_DOCUMENT_CORRECT_DIFFERENCE'] . " " . format_number(abs(unformat_number($this->total) + unformat_number($this->ecminvoiceout->total))) . ' ' . $this->currency_symbol);
//    $pdf->Ln(3);
//}

$pdf->writeHTML('</table>'); // fix background-color overflowing FROM table ... why ? //

$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\');';
$rr = $db->fetchByAssoc($db->query($q));

$pdf->Ln(4);

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

if ($rr) {
    $pdf->Cell(60, 4, $mod_strings['LBL_PDF_PREPAID_DOCUMENTS'] . '/' . $mod_strings2['LBL_PDF_PREPAID_DOCUMENTS'] . ':', 0, 0, 'L', 1);
    $pdf->Cell(30, 4, implode(', ', $rr), 0, 0, 'L', 1);

    $pdf->Ln(4);

    $pdf->Cell(50, 4, $mod_strings['LBL_PDF_PREPAID'] . '/' . $mod_strings2['LBL_PDF_PREPAID'] . ':', 0, 0, 'L', 1);
    $pdf->SetFont('arialpl', 'B', '8');
    $pdf->Cell(30, 4, format_number(array_sum($inv_value)) . ' ' . $this->currency_symbol, 0, 0, 'L', 1);

    $pdf->Ln(4);
}

$pdf->Ln(4);

$pdf->SetFont('arialpl', '', '8');
$pdf->Cell(50, 4, $mod_strings['LBL_PDF_PAID'] . '/' . $mod_strings2['LBL_PDF_PAID'] . ':', 0, 0, 'L', 1);
$pdf->SetFont('arialpl', 'B', '8');
$pdf->Cell(30, 4, format_number(0) . ' ' . $this->currency_symbol, 0, 0, 'L', 1);

$pdf->Ln(4);

if ($pdf->GetY() > 240) {
    $pdf->AddPage();
}

if ($this->type == "correct") {
    $qw = "SELECT price, ecmvat_value, quantity, ecminvoiceoutitem_id FROM ecminvoiceoutitems WHERE ecminvoiceout_id = '" . $this->id . "'";
    $w = $GLOBALS['db']->query($qw);

    while ($r = $GLOBALS['db']->fetchByAssoc($w)) {
        $qrr = "SELECT price, quantity, ecmvat_value FROM ecminvoiceoutitems WHERE id = '" . $r['ecminvoiceoutitem_id'] . "'";
        $rr = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query($qrr));

        $total += $r['price'] * $r['quantity'] + round($r['price'] * $r['quantity'] * $r['ecmvat_value'] / 100, 2);
        $total -= $rr['price'] * $rr['quantity'] + round($rr['price'] * $rr['quantity'] * $rr['ecmvat_value'] / 100, 2);
    }

    $total -= array_sum($inv_value);

    $total = format_number(abs($total));

    $pdf->SetFont('arialpl', '', '8');
    $pdf->Cell(50, 4, $mod_strings['LBL_PDF_TO_PAID'] . '/' . $mod_strings2['LBL_PDF_TO_PAID']. ':', 0, 0, 'L', 1);
    $pdf->SetFont('arialpl', 'B', '8');
    $pdf->Cell(30, 4, $total . ' ' . $this->currency_symbol, 0, 0, 'L', 1);
    
    if (false == is_null($pc)) {
        $pattern['pl_pl'] = 'w terminie <b>%s</b> dni do <b>%s</b> <b>%s</b>';
        $pattern['en_us'] = 'in <b>%s</b> days until <b>%s</b> <b>%s</b>';
  
        $pdf->SetX(100);
        
        $pdf->SetFont('arialpl', '', '8');
        $pdf->WriteHtml(sprintf($pattern['pl_pl'], $pc->days, $this->payment_date, $pc->name));

        $pdf->Ln(4);
        
        $pdf->SetX(100);
        
        $pdf->SetFont('arialpl', '', '8');
        $pdf->WriteHtml(sprintf($pattern['en_us'], $pc->days, $this->payment_date, $pc->name));

        //echo '<pre>' . var_export($cx, true) . '</pre>';
        //echo '<pre>' . var_export($cy, true) . '</pre>'; exit;
    }
} else {
    $total = 0;

    $qw = "SELECT price, ecmvat_value, quantity, ecminvoiceoutitem_id FROM ecminvoiceoutitems WHERE ecminvoiceout_id = '" . $this->id . "'";
    $w = $GLOBALS['db']->query($qw);

    while ($r = $GLOBALS['db']->fetchByAssoc($w)) {
        $total+=$r['price'] * $r['quantity'] + round($r['price'] * $r['quantity'] * $r['ecmvat_value'] / 100, 2);
    }

    $total-=floatval(str_replace(",", ".", str_replace(".", "", $this->discount)));

    $total -= array_sum($inv_value);

    $total = format_number(abs($total));

    $pdf->SetFont('arialpl', '', '8');
    $pdf->Cell(50, 4, $mod_strings['LBL_PDF_TO_PAID'] . '/' . $mod_strings2['LBL_PDF_TO_PAID'] . ':', 0, 0, 'L', 1);
    $pdf->SetFont('arialpl', 'B', '8');
    $pdf->Cell(30, 4, $total . ' ' . $this->currency_symbol, 0, 0, 'L', 1);

    if (false == is_null($pc)) {
        $pattern['pl_pl'] = 'w terminie <b>%s</b> dni do <b>%s</b> <b>%s</b>';
        $pattern['en_us'] = 'in <b>%s</b> days until <b>%s</b> <b>%s</b>';
  
        $pdf->SetX(100);
        
        $pdf->SetFont('arialpl', '', '8');
        $pdf->WriteHtml(sprintf($pattern['pl_pl'], $pc->days, $this->payment_date, $pc->name));

        $pdf->Ln(4);
        
        $pdf->SetX(100);
        
        $pdf->SetFont('arialpl', '', '8');
        $pdf->WriteHtml(sprintf($pattern['en_us'], $pc->days, $this->payment_date, $pc->name));

        //echo '<pre>' . var_export($cx, true) . '</pre>';
        //echo '<pre>' . var_export($cy, true) . '</pre>'; exit;
    }
}

$pdf->Ln(4);

//$pdf->SetFont('arialpl', '', '8');
//
//if (false == is_null($pc)) {
//    $pdf->Cell(50, 4, $mod_strings['LBL_PDF_PAYMENT_METHOD'] . ':', 0, 0, 'L', 1);
//    $pdf->Cell(50, 4, $pc->name ? : '-', 0, 0, 'L', 1);
//
//    $pdf->Ln(4);
//}
//
//$pdf->Cell(50, 4, $mod_strings['LBL_PDF_PAYMENT_TERMIN'] . ':', 0, 0, 'L', 1);
//$pdf->Cell(50, 4, $this->payment_date ? : '-', 0, 0, 'L', 1);
//
//$pdf->Ln(4);
//
//$pdf->SetFont('arialpl', '', '8');
//$pdf->Cell(30, 4, "Słownie:", 0, 0, 'L', 1);
//$pdf->SetFont('arialpl', 'B', '8');
//
//if ($this->type == "correct") {
//    $tot = format_number(abs((float) $total - array_sum($inv_value)));
//    $tot = str_replace(".", "", $tot);
//    //$tot=str_replace(",",".",$tot);
//    $sl = $this->slowniePL($tot);
//} else {
//    $tot = str_replace(".", "", str_replace("PLN", "", $total - array_sum($inv_value)));
//    $sl = $this->slowniePL($tot);
//}
//
//if ($this->document_no == "FV 1421/10") {
//    $sl = "one thousand one hundred six EUR 41/100";
//}
//
//$pdf->Cell(150, 4, $sl, 0, 0, 'L', 1);

if ($this->currency_value > 1) {
    $pdf->Ln(8);

    $pdf->Cell(50, 4, $mod_strings['LBL_CURRENCY_VALUE'] . '/' . $mod_strings2['LBL_CURRENCY_VALUE'] . ':', 0, 0, 'L', 1);
    $pdf->Cell(50, 4, '1 ' . $this->currency_symbol . ' = ' . format_number($this->currency_value ? : 1, null, 4) . ' zł', 0, 0, 'L', 1);
}

$pdf->Ln(16);

$pdf->SetFont('arialpl', 'B', '8');
$pdf->Cell(85, 1, 'Dla wszystkich pozycji faktury zastosowano stawkę VAT 0%', 0, 0, 'L', 1);

$pdf->Ln(4);
$pdf->Cell(85, 1, 'VAT is 0% for all invoice items', 0, 0, 'L', 1);

$pdf->Ln(4);
$pdf->Cell(85, 1, 'Zgodnie z art. 41 ust. 3 ustawy o podatku VAT', 0, 0, 'L', 1);

$pdf->Ln(20);

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

$pdf->Cell(85, 1, "...............................................................", 0, 0, 'L', 1);
$pdf->Cell(85, 1, "...............................................................", 0, 0, 'R', 1);

$pdf->Ln(4);

$pdf->Cell(43, 1, $mod_strings['LBL_PDF_OWNER_SIGNATURE'] . '/' . $mod_strings2['LBL_PDF_OWNER_SIGNATURE'], 0, 0, 'C', 1);

$pdf->SetX(155);

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

$pdf->Ln(4);

$pdf->Cell(43, 1, $this->setUser()->full_name, 0, 0, 'C', 1);

//footer text
$pdf->Ln(18);

$pdf->SetFont('arialpl', '', '8');
$pdf->MultiCell(0, 4, $this->footer_text, 0, 'J');

//header text
$pdf->Ln(6);

$pdf->SetFont('arialpl', 'B', '8');
$pdf->MultiCell(0, 4, $this->ads_text, 0, 'J');