<?php
global $db;

$pdf->focus=$this;

    $pdf->Ln(2);

	$pdf->SetY($pdf->GetY()+2);
	$current = $pdf->GetY();
	$pdf->SetFont('arialpl', '', 9);
	/*
	$p_name = html_entity_decode($this->parent_name);
	$p_name = explode(' ', $p_name);
	$name = array();
	if (sizeof($p_name)>4) {
		for ($i=0; $i<sizeof($p_name); $i++) {
			$n.=$p_name[$i].' ';
			//$pdf->Cell(40,5, $n,0,1,'L');
			if (($i%5==0) && ($i!=0)) {
				$name[] = $n;
				$n='';
			}
		}
		$name[] = $n;
	}
    if($this->parent_name) {
		if (sizeof($p_name)>4)
			foreach ($name as $n)
				$pdf->Cell(40,5, $n,0,1,'L');
		else
			$pdf->Cell(40,5, html_entity_decode($this->parent_name),0,1,'L');
	}
	*/
	$pdf->MultiCell(100,5, html_entity_decode($this->parent_name),0,1,'L');
	//$pdf->SetFont('arialpl', '', 10);
	if($this->parent_address_street) $pdf->Cell(20,5,html_entity_decode($this->parent_address_street),0,1,'L');
    if (($this->parent_address_postalcode) || ($this->parent_address_city))
		$pdf->Cell(20,5,html_entity_decode($this->parent_address_postalcode).' '.html_entity_decode($this->parent_address_city),0,1,'L');
    if($this->to_vatid) $pdf->Cell(20,5,$mod_strings['LBL_PDF_VATID'].' '.html_entity_decode($this->to_vatid),0,1,'L');

	$title='';
	if($this->parent_contact_title) $title.=$this->parent_contact_title.' ';
	if($this->parent_contact_name) {
		$pdf->SetFont('arialpl', 'b', 10);
		$pdf->Cell(20,5,$title.$this->parent_contact_name,0,1,'L');
		$pdf->SetFont('arialpl', 'b', 10);
	}


	if ($this->parent_shipping_address_name || $this->parent_shipping_address_street) {
		$x = $pdf->getX();
		$y = $pdf->getY();
		$pdf->setY($current);
		$pdf->setX(100);
		$pdf->Cell(100,5,$mod_strings['LBL_PDF_DELIVERY_TO'],0,1,'L');
		$pdf->setX(100);
		$pdf->MultiCell(100,5, html_entity_decode($this->parent_shipping_address_name),0,1,'L');
		$pdf->setX(100);
		if($this->parent_shipping_address_street) $pdf->Cell(100,5,html_entity_decode($this->parent_shipping_address_street),0,1,'L');
		$pdf->setX(100);
		if (($this->parent_shipping_address_postalcode) || ($this->parent_shipping_address_city))
			$pdf->Cell(20,5,html_entity_decode($this->parent_address_postalcode).' '.html_entity_decode($this->parent_address_city),0,1,'L');
		$pdf->setY($y);
		$pdf->setX($x);
	}

	/*
    if($this->name) {
		$pdf->Cell(20,5,'',0,1,'L');
		$pdf->SetFont('arialpl', 'b', 10);
		$pdf->Cell(20,5,$mod_strings['LBL_PDF_NAME'],0,1,'L');
		$pdf->SetFont('arialpl', '', 10);
		$pdf->Cell(20,5,html_entity_decode($this->name),0,1,'L');
	}
	*/
	  $pdf->SetFont('arialpl', '', 10);
      $pdf->Ln(2);
     // $current = $pdf->GetY();
      //$pdf->MultiCell(120,4,$address,0,'L');
	  //$pdf->SetFont('arialpl','B','10');
      //$pdf->MultiCell(0,4,$address_cnt,0,'L');
	  if($this->parent_type="Contacts")$pdf->Ln(14);
      $current2 = $pdf->GetY();
	  $pdf->SetFont('arialpl','','10');

	  $subFontSizeold = $pdf->FontSizePt;
		$pdf->SetFontSize($subFontSize);

/*
		// reposition y
		$subOffset = ((($subFontSize - $subFontSizeold) / $pdf->k) * 0.3) + ($subOffset / $pdf->k);
		$subX		= $pdf->x;
		$subY		= $pdf->y;
		$pdf->SetXY($subX, $subY - $subOffset);

		//Output text
		$pdf->Write(5, 'test','');

		// restore y position
		$subX		= $pdf->x;
		$subY		= $pdf->y;
		$pdf->SetXY($subX,   $subY + $subOffset);

		// restore font size
	   $pdf->SetFontSize($subFontSizeold);
*/

//      $pdf->Ln(7);
      $pdf->SetXY($pdf->fw-$pdf->rMargin-37,$current);
      $table = array();
      $table [0]['data'] = array('width' => 30, 'value' => $mod_strings['LBL_PDF_DATE_REGISTER'], 'border' => 0, 'overflow' => 1, 'align' => 'L', 'font-style' => 'b', 'font-size' => 9);
      $table [1]['data'] = array('width' => 30, 'value' => date("d.m.Y"), 'border' => 0, 'overflow' => 1, 'align' => 'L');
      $table [2]['data'] = array('width' => 30, 'value' => "", 'border' => 0, 'overflow' => 1, 'align' => 'L');
	  $r_created_by = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select first_name, last_name from users where id='".$this->modified_user_id."'"));

      $table [4]['data'] = array('width' => 30, 'value' => $mod_strings['LBL_PDF_OWNER'], 'border' => 0, 'overflow' => 1, 'align' => 'L', 'font-style' => 'b', 'font-size' => 9);      $table [4]['data'] = array('width' => 30, 'value' => $mod_strings['LBL_PDF_OWNER'], 'border' => 0, 'overflow' => 1, 'align' => 'L', 'font-style' => 'b', 'font-size' => 9);
      $table [5]['data'] = array('width' => 30, 'value' => $r_created_by['first_name']." ".$r_created_by['last_name'] , 'border' => 0, 'overflow' => 1, 'align' => 'L');
      $table [6]['data'] = array('width' => 30, 'value' => $mod_strings['LBL_PDF_ASSIGNED_TO'], 'border' => 0, 'overflow' => 1, 'align' => 'L', 'font-style' => 'b', 'font-size' => 9);
      $table [7]['data'] = array('width' => 30, 'value' => $this->setUser()->full_name.'
tel.: '.$this->setUser()->phone_work.'
kom.: '.$this->setUser()->phone_mobile.'
e-mail: '.$this->setUser()->email1, 'border' => 0, 'overflow' => 1, 'align' => 'L');
      $pdf->DrawTable($table, null, 0, 0, 3);

      $pdf->SetY($current2-10);

      $pdf->Ln(2);

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

      $pdf->MultiCell(0,0,$mod_strings['LBL_PDF_CONFIRM']." ".$this->document_no,0,'L');

	  $pdf->SetFont('arialpl','','9');
	  $pdf->Ln(5);

	  $table = array();
	  $table[0][0] = array('width' => 30, 'value' => $mod_strings['LBL_PDF_PARENT_DOCUMENT_NO'], 'background' => array(255,255,255), 'border' => 0, 'overflow' => '1', 'align' => 'L');
	  $table[0][1] = array('width' => 80, 'value' => $this->parent_document_no, 'background' => array(255,255,255), 'border' => 0, 'overflow' => '1', 'align' => 'L');
	  $pdf->DrawTable($table);

	  //$pdf->MultiCell(0,0,'Szablon dokumentu jest aktualnie przebudowywany.',0,'L');
	  $pdf->SetY($current2+5);
	  //$pdf->MultiCell(0,0,'Spróbuj za 30 min.',0,'L');





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





      //header text
	  if($this->header_text){
      $pdf->Ln(8);

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

      $pdf->MultiCell(0,4,$this->header_text,0,'J');



	  }
      $pdf->Ln(8);


//**************************************************
	  global $mod_strings;
      if(!isset($this->position_list) || !is_array($this->position_list))
        $this->position_list = $this->getPositionList(true);
      $calc = $this->calculate_currency($this->position_list,$this->currency_id, $this->currency_value);
      $this->position_list = $this->formatPositions($this->position_list, $this->currency_id, $this->currency_value);
     /*
      $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;
*/


      $name_w = 59;
	  $image_w = 10;
	  $recipient_code_w = 10;

	  if($this->show_images_on_offers){
	  	$name_w -= $image_w;
	  }
	  if($this->show_recipient_code){
	  	$name_w -= $recipient_code_w;
	  }

      if($calc['draw_discount']){
	  	$name_w -= 8;
	  }
      if($calc['draw_vat']){
	  	$name_w -= 8;
	  }

	  //jest rabat?
	  $rabat = false;
	  foreach($this->position_list as $p) {
	  		if ($p['discount']!=0) $rabat=true;
	  }

	  	if ($this->total_netto!='') $name_w+=35;


      $table = array();
      $table [0]['position'] = array('width' => 5, 'value' => $mod_strings['LBL_PDF_LIST_POSITION'], 'background' => array(233,233,233), 'border' => 0, 'overflow' => 1, 'align' => 'C');
      $table [0]['description'] = array('width' => $name_w, 'value' => $mod_strings['LBL_PDF_LIST_DESCRIPTION'], 'background' => array(233,233,233), 'border' => 0, 'overflow' => 1, 'align' => 'C');
      $table [0]['quantity'] = array('width' => 8, 'value' => $mod_strings['LBL_PDF_LIST_QUANTITY'],'background' => array(233,233,233), 'border' => 0, 'overflow' => 1, 'align' => 'C');
      if ($this->total_netto=='') {
	  $table [0]['unit_price'] = array('width' => 14, 'value' => $mod_strings['LBL_PDF_LIST_PRICE'], 'background' => array(233,233,233), 'border' => 0, 'overflow' => 1, 'align' => 'C');
		if ($rabat==true) {
			$table [0]['discount'] = array('width' => 7, 'value' => $mod_strings['LBL_PDF_LIST_DISCOUNT'], 'background' => array(233,233,233), 'border' => 0, 'overflow' => 1, 'align' => 'C');
		}
		$table [0]['total'] = array('width' => 14, 'value' => $mod_strings['LBL_PDF_LIST_TOTAL'], 'background' => array(233,233,233), 'border' => 0, 'overflow' => 1, 'align' => 'R');
		}


      $i = 1;
      $lv=return_app_list_strings_language($this->ecmlanguage);
      if($this->position_list != '')

	 // $result = $db->query("SELECT total_netto from ecmquotes where id=$p['id']");


      foreach($this->position_list as $p) {

        $table [$i]['position'] = array('width' => 5, 'value' => $i, 'border' => 0, 'align' => 'C', );
		$name = str_replace('–','-',htmlspecialchars_decode($p['name']));
        $table [$i]['description'] = array('width' => $name_w, 'value' => $name, 'border' => 0, 'align' => 'L', );
        $table [$i]['quantity'] = array('width' => 8, 'value' => $p['quantity'], 'border' => 0, 'align' => 'C', );
		if ($this->total_netto=='') {
					$table [$i]['unit_price'] = array('width' => 14, 'value' => $p['price'], 'border' => 0, 'align' => 'C', );
				if ($rabat==true) {
					if ($p['discount']==0) $tmp='---'; else $tmp=$p['discount'];
					$table [$i]['discount'] = array('width' => 7, 'value' => $tmp, 'border' => 0, 'align' => 'R', );
				}
			$table [$i]['total'] = array('width' => 14, 'value' => $p['total'], 'border' => 0, 'align' => 'R', );
		}



	$tmp_sep = ereg_replace('[^0-9.,]', '', $p['total']);
	$len  = strlen($tmp_sep);
	$separator = substr($tmp_sep, $len-3, 1);

	if ($separator == '.') $tmp = (float) ereg_replace('[^0-9.]', '', $p['total']);
	if ($separator == ',') $tmp = (float) ereg_replace('[^0-9,]', '', $p['total']);

		if ($p['currency_id'] == 'PLN') {
			if (isset($total_price_pln)) $total_price_pln += $tmp;
				else $total_price_pln = $tmp;
				}


		if ($p['currency_id'] == '6336d9a0-ee5f-52e3-7d0c-4e6f1472b2bf') {
			if (isset($total_price_eur)) $total_price_eur+= $tmp;
				else $total_price_eur= $tmp;
				}

	 $i++;
      }
      $starttab=$pdf->GetY()+10;
      $pdf->DrawTable($table,array(0));
      $endtab=$pdf->GetY();
      $pdf->SetY($endtab);

	  //mz
      $table = array();
		//EURO
      if($calc['vats']) {
        $table [] = array(
          'subtotal1' => array('width' => 66, 'value' => $mod_strings['LBL_PDF_TOTAL'], 'background' => array(255,255,255), 'border' => 0, 'overflow' => 1, 'align' => 'L', 'font-size' => 9),
          'subtotal2' => array('width' => 34, 'value' => $calc['subtotal'].' '.$calc['currency_symbol'], 'border' => 0, 'overflow' => 1, 'align' => 'R', 'font-size' => 9), );

      foreach($calc['vats'] as $key => $value){
		  $r=mysql_fetch_array(mysql_query("select name from ecmvats where id='".$key."'"));
		  $vvn=$r['name'];
          $table [] = array(
            'vat1' => array('width' => 66, 'value' => $mod_strings['LBL_PDF_VAT'].' ('.$vvn.')', 'background' => array(255,255,255), 'border' => 0, 'overflow' => 1, 'align' => 'L', 'font-size' => 9),
            'vat2' => array('width' => 34, 'value' => $value.' '.$calc['currency_symbol'], 'border' => 0, 'overflow' => 1, 'align' => 'R', 'font-size' => 9), );
		}
      }

      $table [] = array(
        'total1' => array('width' => 66, 'value' => $mod_strings['LBL_PDF_END_TOTAL'], 'background' => array(233,233,233), 'border' => 0, 'overflow' => 1, 'align' => 'L', 'font-size' => 9, 'font-style' => 'b'),
        'total2' => array('width' => 34, 'value' => $calc['total'].' '.$calc['currency_symbol'], 'border' => 0, 'background' => array(233,233,233), 'overflow' => 1, 'align' => 'R' , 'font-size' => 9, 'font-style' => 'b'), );

      $pdf->DrawTable($table);

	  //PLN
	  /*
	  $starttab=$pdf->GetY()+1;
      $pdf->SetY($starttab);

      $table = array();
      if($calc['vats_pln']) {
        $table [] = array(
          'subtotal1' => array('width' => 66, 'value' => $mod_strings['LBL_PDF_TOTAL'], 'background' => array(255,255,255), 'border' => 0, 'overflow' => 1, 'align' => 'L', 'font-size' => 9),
          'subtotal2' => array('width' => 34, 'value' => $calc['subtotal_pln'].' PLN', 'border' => 0, 'overflow' => 1, 'align' => 'R', 'font-size' => 9), );

        foreach($calc['vats_pln'] as $key => $value){
		  $r=mysql_fetch_array(mysql_query("select name from ecmvats where id='".$key."'"));
		  $vvn=$r['name'];
          $table [] = array(
            'vat1' => array('width' => 66, 'value' => $mod_strings['LBL_PDF_VAT'].' ('.$vvn.')', 'background' => array(255,255,255), 'border' => 0, 'overflow' => 1, 'align' => 'L', 'font-size' => 9),
            'vat2' => array('width' => 34, 'value' => $value.' PLN', 'border' => 0, 'overflow' => 1, 'align' => 'R', 'font-size' => 9), );
		}
      }

      $table [] = array(
        'total1' => array('width' => 66, 'value' => $mod_strings['LBL_PDF_END_TOTAL'], 'background' => array(233,233,233), 'border' => 0, 'overflow' => 1, 'align' => 'L', 'font-size' => 9, 'font-style' => 'b'),
        'total2' => array('width' => 34, 'value' => $calc['subtotal_pln'].' PLN', 'border' => 0, 'background' => array(233,233,233), 'overflow' => 1, 'align' => 'R' , 'font-size' => 9, 'font-style' => 'b'), );

      $pdf->DrawTable($table);
	  */

?>