<?php 
global $SysPrefs;

    if($this->pageNumber == 1 && $this->formData['doctype'] != ST_CUSTPAYMENT) {
        $this->headers = array(_("Item"), _("Qty"), _("UnitPrice"), _("Total"));			
            
        if (!in_array($this->formData['doctype'], array(ST_STATEMENT, ST_WORKORDER))){
            $row = get_payment_terms($this->formData['payment_terms']);
            $Payment_Terms = _("Payment Terms") . ': ' . $row["terms"];
            if ($this->formData['doctype'] == ST_SALESINVOICE && $this->formData['prepaid'])
                $this->formData['prepaid'] = ($row['days_before_due'] >= 0) ? 'final' : 'partial';
        }

        $this->title = $this->formData['prepaid']=='partial' ? _("PREPAYMENT INVOICE")	: ($this->formData['prepaid']=='final' ? _("FINAL INVOICE")  : _("INVOICE"));
        $this->formData['document_name'] =_("Invoice No.");
        $this->formData['domicile'] = $this->company['domicile'];
        $Footer[0] = _("Thank you. Please come again");	

        // default values
        if (!isset($this->formData['document_date']))
            $this->formData['document_date'] = $this->formData['tran_date'];

        if (!isset($this->formData['document_number']))
            $this->formData['document_number'] =  isset($this->formData['reference'])	? $this->formData['reference'] : @$this->formData['trans_no'];

        if ($this->company['legal_text'] != "") {
            foreach(explode("\n", $this->company['legal_text']) as $line)
                $Footer[] = $line;
        }
        //$this->Footer = $Footer;
    }
	if($this->formData['doctype'] == ST_CUSTPAYMENT)
     	$this->headers = array(_('Type'),_('Ref #'), _("Amount"));

	$this->row = $this->pageHeight - $this->topMargin;
	$start=$this->row ;
	$this->leftMargin = 1;
	
	$upper = $this->row - 2 * $this->lineHeight;
	$lower = $this->bottomMargin + 8 * $this->lineHeight;
	$iline1 = $upper - 7.5 * $this->lineHeight;		
	$iline7 = $lower;
	$iline8 = $upper - 3 * $this->lineHeight;
	$right = $this->pageWidth - $this->rightMargin;
	$width = ($right - $this->leftMargin) / 5;
	$icol = $this->pageWidth / 2;
	$ccol = $this->cols[0];
	$c2col = $ccol + 42;
	$c3col = $ccol + 66;
	$mcol = $icol + 8;
	$mcol3 = $icol + 4;
	$cols = count_array($this->cols);

	$this->fontSize = 7;
	$this->lineHeight = 9;

	$this->SetDrawColor(128, 128, 128);
	if( isset($this->formData['token']) && $this->formData['token'] == 'yes')
		 $this->lineHeight = 11;
	$col = $this->leftMargin;
	if($this->pageNumber ==1){
		$logo = company_path() . "/images/" . $this->company['coy_logo'];

		$this->NewLine();
		if ($this->company['coy_logo'] != '' && file_exists($logo)){
		    $this->row -= 20;
			$this->AddImage($logo, $c2col-25, $this->row, 0, 25);
					
			$this->NewLine();
			$this->Font('bold');
			$this->fontSize += 2;
			$this->TextWrapLines($ccol, $this->pageWidth-10,$this->company['coy_name'],'left');
			$this->fontSize -= 2;
			$this->Line($this->row+5);
			$this->Font();
			$this->NewLine(0.8);
		}else{					
			$this->Font('bold');
			$this->fontSize += 2;
			$this->TextWrapLines($ccol, $this->pageWidth-10,$this->company['coy_name'],'left');
			$this->fontSize -= 2;
			$this->Font();
            $this->NewLine(0.8);
		}	
			
		if($this->company['phone']!='' ){
			$this->Text($ccol,_("Phone")." : ".$this->company['phone'], $mcol3);	
		}
		//$this->NewLine(1.3);
				
		$this->TextWrapLines($ccol,$right, _("Address")." : ".$this->company['postal_address'],'left');				
		$this->Line($this->row+2);
		$this->NewLine(0.8);

		$this->NewLine(0.1);
		if($this->formData['doctype'] == ST_CUSTPAYMENT)
			$this->Text($ccol, _("User")." : ".$_SESSION["wa_current_user"]->username, $mcol3);
		if($this->formData['doctype'] != ST_CUSTPAYMENT)
			$this->Text($ccol,  _("Sales Person")." : ".get_salesman_name($this->formData['salesman']), $mcol3);

		if($this->formData['doctype'] == ST_SALESINVOICE  || $this->formData['doctype'] == ST_CUSTDELIVERY){	
			$this->NewLine(1.4);
			$this->Font('bold');
			$this->Text($ccol, _("INVOICE")." : ".$this->formData['document_number'],$c3col+50);
			$this->Font();	

			if(isset($this->formData["delivery_no"]) && $this->formData["delivery_no"] > 0 ) {
				if($this->formData['doctype'] == ST_CUSTDELIVERY)
					$this->NewLine(1.4);
				else
					$this->NewLine();
				$this->Font('bold');
				$this->fontSize += 3;
				$this->Text($ccol, _("Delivery No")." : ".$this->formData['delivery_no'],$c3col+50);
				$this->fontSize -= 3;
				$this->Font();						
			}
		}	
		if(isset($this->formData['customer_ref'])){
			$this->NewLine();
			$this->Text($ccol,_("Customer Ref")." : ".$this->formData['customer_ref'], $mcol3);
		}
		$this->NewLine(1.2);
		//$date_time = date("d/m/Y  h:i a", strtotime(FAGetSingleValue('audit_trail','stamp',array('trans_no'=>$this->formData['trans_no'],'type'=>$this->formData['type']))));
		$this->Text($ccol, _("Date :")." ".sql2date($this->formData['tran_date'])); 
		$this->NewLine(0.9);
		if($this->formData['doctype'] == ST_SALESINVOICE && false){	
			$bank_name = get_invoice_payment_informations($this->formData['trans_no']);
			if($bank_name != ''){	
				$this->NewLine(0.3);	
				$this->Text($ccol, _("Payment") ." : ".$bank_name );
				$this->NewLine(0.8);	
			}
		}
		//$this->Line($this->row);
		// $this->NewLine();
		if(isset($this->formData['delivery_no']) && $this->formData['delivery_no'] > 0 )
			$this->Text($ccol, _("Client Name")." : ".$this->formData['customer_ref'], $mcol3);
		else
			$this->Text($ccol, _("Customer")." : ".$this->formData['DebtorName'], $mcol3);

		if($this->formData['doctype'] == ST_CUSTPAYMENT){
			$row = get_payment_terms($this->formData['payment_terms']);
            // $Payment_Terms = _("Payment Terms") . ': ' . $row["terms"];
            $this->NewLine();
			$this->Text($ccol,  _("Payment Terms")." : ".$row["terms"], $mcol3);
			$this->NewLine();
			$this->Text($ccol,  _("Reference")." : ".$this->formData['reference'],$mcol3);
		}

		if(isset($this->formData['contact_phone']) && trim($this->formData['contact_phone']) != ''){
			$this->NewLine(1.2);
			$this->Text($ccol, _("Phone")." : ".$this->formData['contact_phone'], $mcol3);
		}
		$this->NewLine(1.2);
	    $this->Line($this->row);
			
		$this->NewLine();
		// Line headers			
			
		//$this->row = $iline1 - $this->lineHeight +25;
		$this->Font('bold');
		$count = (is_array($this->headers) ? count($this->headers) : 0 );
		if($count > 0 ){
			$this->cols[$count] = $right - 3;
			for ($i = 0; $i < $count; $i++)
				$this->TextCol($i, $i + 1, $this->headers[$i], -2);
		}
		$this->Font();
		// if($this->formData['doctype'] != ST_CUSTPAYMENT)
		$this->Line($this->row-4);
		
		$content_start = 17;
		$temp = $this->row-$content_start; 		
	}
    $this->NewLine(1.5);