<?php

	  $pdf->Ln(20);
	  //if($pdf->GetY()>240)$pdf->AddPage();

	  	$total=0;
      	$pdf->SetFont('arialpl','','8');
		include_once("modules/EcmSales/EcmSale.php");
		$sale=new EcmSale();
		$sale->retrieve($this->ecmsale_id);
		$w=$GLOBALS['db']->query("select inv_value,currency_value from ecmprepaymentinvoices where ecmsale_id='".$this->ecmsale_id."' and date_entered<'".$this->date_entered."'");
		while($r=$GLOBALS['db']->fetchByAssoc($w)){
			if(!$r['currency_value'])$r['currency_value']=1;
			$total+=$r['inv_value']*$r['currency_value'];
		}
		$total=format_number($sale->getTotal()-$total);
		
	
	  	$pdf->Cell(30,4,"Do zapłaty",0,0,'L',1);
      	$pdf->SetFont('arialpl','B','8');
	  	$pdf->Cell(30,4,number_format($this->inv_value*$this->currency_value,2,",","."),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');

		include_once("modules/EcmInvoiceOuts/EcmInvoiceOut.php");
		$sl=EcmInvoiceOut::slowniePL(number_format($this->inv_value*$this->currency_value,2,",",""));
	  $pdf->Cell(150,4,$sl." PLN",0,0,'L',1);
	  
	  $pdf->Ln(10);
	  if(strlen($this->currency_id)>10){
	  	$rc=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select iso4217 from currencies where id='".$this->currency_id."'"));
		$this->currency_id=$rc['iso4217'];
	  }
	  $pdf->Cell(85,1,"Ceny wyliczono wg kursu sprzedaży ".$this->currency_id." z dnia wystawienia zamówienia nr ".$sale->document_no." z dnia ".$sale->register_date." kurs ".$this->currency_id." = ".$this->currency_value,0,0,'L',1);
	  
	  //get products
	  $pdf->SetFont('arialpl','','7');
	  $pdf->Ln(10);
	  //$pdf->Cell(85,1,"Dane dotyczące zamówienia, lub umowy",0,0,'L',1);
	  $pdf->Cell(85,1,"Dane dotyczące zamówienia, lub umowy",0,0,'L',1);
	  $pdf->Ln(5);
	  $inv_prod = explode("|", $this->products_on_pdf);
	  $query = "SELECT * FROM `ecmsaleitems` WHERE  id IN ('".implode("','", $inv_prod)."') order by position asc";
      $prod = $GLOBALS['db']->query($query);
	  $table = array(); 
      $table [0][] = array('width' => 4, 'value' => "Lp.", 'border' => 1,'font-style' => 'b', 'overflow' => 1, 'align' => 'L');
      $table [0][] = array('width' => 32, 'value' => "Nazwa", 'border' => 1,'font-style' => 'b', 'overflow' => 1, 'align' => 'L');
      $table [0][] = array('width' => 7, 'value' => "Ilość", 'border' => 1, 'overflow' => 1,'font-style' => 'b', 'align' => 'L');
      $table [0][] = array('width' => 5, 'value' => "J.M.", 'border' => 1,'font-style' => 'b', 'overflow' => 1, 'align' => 'L');
      $table [0][] = array('width' => 12, 'value' => "Cena Netto", 'border' => 1,'font-style' => 'b', 'overflow' => 1, 'align' => 'L');
      $table [0][] = array('width' => 12, 'value' => "Wartość Netto", 'border' => 1,'font-style' => 'b', 'overflow' => 1, 'align' => 'L');
      $table [0][] = array('width' => 8, 'value' => "VAT", 'border' => 1,'font-style' => 'b', 'overflow' => 1, 'align' => 'L');
      $table [0][] = array('width' => 11, 'value' => "Wartość Vat", 'border' => 1,'font-style' => 'b', 'overflow' => 1, 'align' => 'L');
      $table [0][] = array('width' => 11, 'value' => "Wartośc Brutto", 'border' => 1,'font-style' => 'b', 'overflow' => 1, 'align' => 'L');
	  
	  $i = 1;
	  $total = 0;
	  while ($p = $GLOBALS['db']->fetchByAssoc($prod)) {
	        $table [$i][] = array('width' => 4, 'value' => $i, 'border' => 1,'font-style' => '', 'overflow' => 1, 'align' => 'L');
			$table [$i][] = array('width' => 32, 'value' => $p['name'], 'border' => 1,'font-style' => '', 'overflow' => 1, 'align' => 'L');
			$table [$i][] = array('width' => 7, 'value' => format_number($p['quantity'],2), 'border' => 1, 'overflow' => 1,'font-style' => '', 'align' => 'L');
			$table [$i][] = array('width' => 5, 'value' => $p['dd_unit_name'], 'border' => 1,'font-style' => '', 'overflow' => 1, 'align' => 'L');
			$table [$i][] = array('width' => 12, 'value' => format_number($p['price'],2), 'border' => 1,'font-style' => '', 'overflow' => 1, 'align' => 'L');
			$table [$i][] = array('width' => 12, 'value' => format_number($p['quantity']*$p['price'],2), 'border' => 1,'font-style' => '', 'overflow' => 1, 'align' => 'L');
			$table [$i][] = array('width' => 8, 'value' => $p['ecmvat_name'], 'border' => 1,'font-style' => '', 'overflow' => 1, 'align' => 'L');
			$table [$i][] = array('width' => 11, 'value' => format_number($p['price']*$p['ecmvat_value'],2), 'border' => 1,'font-style' => '', 'overflow' => 1, 'align' => 'L');
			$table [$i][] = array('width' => 11, 'value' => format_number($p['quantity']*($p['price']+($p['price']*$p['ecmvat_value'])),2), 'border' => 1,'font-style' => '', 'overflow' => 1, 'align' => 'L');
			$total+=$p['quantity']*($p['price']+($p['price']*$p['ecmvat_value']));
	  $i++;
	  }
	  $pdf->DrawTable($table, array(0));

	  $table = array();
	  	    $table [0][] = array('width' => 4, 'value' => '', 'border' => 1,'font-style' => '', 'overflow' => 1, 'align' => 'L');
			$table [0][] = array('width' => 32, 'value' => '', 'border' => 1,'font-style' => '', 'overflow' => 1, 'align' => 'L');
			$table [0][] = array('width' => 7, 'value' => '', 'border' => 1, 'overflow' => 1,'font-style' => '', 'align' => 'L');
			$table [0][] = array('width' => 5, 'value' => '', 'border' => 1,'font-style' => '', 'overflow' => 1, 'align' => 'L');
			$table [0][] = array('width' => 12, 'value' => '', 'border' => 1,'font-style' => '', 'overflow' => 1, 'align' => 'L');
			$table [0][] = array('width' => 12, 'value' => '', 'border' => 1,'font-style' => '', 'overflow' => 1, 'align' => 'L');
			$table [0][] = array('width' => 8, 'value' => '', 'border' => 1,'font-style' => '', 'overflow' => 1, 'align' => 'L');
			$table [0][] = array('width' => 11, 'value' => Razem, 'border' => 1,'font-style' => '', 'overflow' => 1, 'align' => 'L');
			$table [0][] = array('width' => 11, 'value' => format_number($total,2), 'border' => 1,'font-style' => '', 'overflow' => 1, 'align' => 'L');
	  $pdf->DrawTable($table, array(0));
	  
	  $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,"Podpis wystawiajacego",0,0,'C',1);
	  $pdf->SetX(155);
	  $pdf->Cell(35,1,"Podpis odbiorcy",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');



?>