<?php
/**********************************************************************
    Copyright (C) FrontAccounting, LLC.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************/

/**
	Document blueprint use following parameters set in doctext.inc:

	$Addr1,	$Addr2 - address info
	$Payment_Terms - payment terms line
	$Footer - footer texts
	$this->company - company info
	$this->title - report title
	$this->formData - some other info
***/

		$this->row = $this->pageHeight - $this->topMargin;

		$upper = $this->row - 4 * $this->lineHeight;
		$lower = $this->bottomMargin + 6 * $this->lineHeight;
		$iline1 = $upper - 10 * $this->lineHeight;
		$iline2 = $iline1 - 5 * $this->lineHeight;
		$iline3 = $iline2 - 1.5 * $this->lineHeight;
		$iline4 = $iline3 - 1.5 * $this->lineHeight;
		$iline5 = $iline2 - 3 * $this->lineHeight;
		$iline6 = $iline5 - 1.5 * $this->lineHeight;
		$iline7 = $lower;
                $iline8 = $lower + 10 * $this->lineHeight;
		$right = $this->pageWidth - $this->rightMargin;
		$width = ($right - $this->leftMargin) / 5;
		$icol = $this->pageWidth / 2;
		$ccol = $this->cols[0] + 4;
		$c2col = $ccol + 60;
		$ccol2 = $icol / 2;
		$mcol = $icol + 8;
		$mcol2 = $this->pageWidth - $ccol2;
		$cols = count($this->cols);

                //customer details box
                $box_height = 80;
                $box_width = 250;
                $b1_col = $this->leftMargin;
                $this->LineTo($b1_col , $iline1,$b1_col, $iline1 - $box_height);
                $this->LineTo($b1_col + $box_width, $iline1,$b1_col + $box_width, $iline1 - $box_height);
                $this->LineTo($b1_col , $iline1,$b1_col + $box_width, $iline1 );
                $this->LineTo($b1_col , $iline1 - ($this->lineHeight * 2),$b1_col + $box_width, $iline1 - ($this->lineHeight * 2));
                $this->LineTo($b1_col , $iline1 - $box_height,$b1_col + $box_width, $iline1 -$box_height);
                
//                //Invoice details box
//                $box_width = 200;
                $b2_col = $this->pageWidth - $this->rightMargin - $box_width;
//                $this->LineTo($b2_col, $iline1,$b2_col, $iline1 - $box_height);
//                $this->LineTo($b2_col + $box_width, $iline1,$b2_col + $box_width, $iline1 - $box_height);
                for($i = 4;$i < 8;$i++){
                  $this->LineTo($b2_col + 100 , $iline1 - ($this->lineHeight * 2) + $i,$b2_col + $box_width, $iline1 - ($this->lineHeight * 2) + $i );
                }
//                $this->LineTo($b2_col , $iline1 - $box_height,$b2_col + $box_width, $iline1 -$box_height);
                
                /********** crazy fix for font distotion on page > 1 **************/
                {
                    $this->fontSize += 4;
                    $this->Font('bold');
                    $this->Text($ccol, '', $icol);
                    $this->Font();
                    $this->fontSize -= 4;
		}
                
                
		$this->rectangle($this->leftMargin, $iline5, $right - $this->leftMargin, $iline5 - $iline6, "F", null, array(222, 231, 236));   
		$this->Line($iline5);
		$this->Line($iline6);
		$this->Line($iline7);
                

		
		$this->LineTo($this->leftMargin, $iline5 ,$this->leftMargin, $iline7);
		if ($this->l['a_meta_dir'] == 'rtl') // avoid line overwrite in rtl language
			$this->LineTo($this->cols[$cols - 2], $iline5 ,$this->cols[$cols - 2], $iline7);
		else	
			$this->LineTo($this->cols[$cols - 2] + 4, $iline5 ,$this->cols[$cols - 2] + 4, $iline7);
		$this->LineTo($right, $iline5 ,$right, $iline7);

                // Company Logo
		$logo = company_path() . "/images/" . $this->company['coy_logo'];
		if ($this->company['coy_logo'] != '' && file_exists($logo))
		{
			$this->AddImage($logo, $this->leftMargin  , $this->row - 165, 0, 175);
		}

                
		if ($this->pageNumber > 1)
			$this->Text($mcol + 180, _("Page") . ' ' . $this->pageNumber);
		$this->row = $iline1 - $this->lineHeight;
		//$this->fontSize -= 4;
		$this->Text($ccol, 'CUSTOMER DETAILS:', $icol);
		$this->Text($mcol + 135, 'STATEMENT');
		//$this->fontSize += 4;

// address1
                
                $this->NewLine();
		$temp = $this->row = $this->row - $this->lineHeight - 5;
		$this->Text($ccol, $Addr1['name'], $icol);
		$this->NewLine();
                $this->NewLine();
		$this->TextWrapLines($ccol, $icol  - $ccol, $Addr1['address']);

 //address2
		$this->row = $temp;
		$this->Text($mcol + 100 , 'FROM : '.$this->formData['startdate'].'   TO : '.$this->formData['enddate']);
		//$this->Text($mcol + 180, sql2date($this->formData['document_date']));

		$this->NewLine();
		$this->Text($mcol + 100, 'Print Date : ');
		$this->Text($mcol + 180, $this->formData['print_date']);

		$this->NewLine();
		$this->Text($mcol + 100, _('Total Owing :'));
                $dec = user_price_dec();
		$this->Text($mcol + 180, number_format2($this->formData['Balance'],$dec));		//$this->NewLine(2);
                
                
                $this->Font('italic');
                $this->Font();
		// Line headers
		$this->row = $iline5 - $this->lineHeight - 1;
		$this->Font('bold');
		$count = count($this->headers);
		$this->cols[$count] = $right - 3;
		for ($i = 0; $i < $count; $i++)
			$this->TextCol($i, $i + 1, $this->headers[$i], -2);
		$this->Font();

		// Footer
		$this->Font('italic');
		$this->row = $iline7 - $this->lineHeight - 6;

		foreach ($Footer as $line => $txt)
		{
			if (!is_numeric($line))	// title => link
			{
				$this->fontSize -= 2;
				$this->TextWrap($ccol, $this->row, $right - $ccol, $line, 'C');
				$this->row -= $this->lineHeight;
				$this->SetTextColor(0, 0, 255);
				$this->TextWrap($ccol, $this->row, $right - $ccol, $txt, 'C');
				$this->SetTextColor(0, 0, 0);
				$this->addLink($txt, $ccol, $this->row, $this->pageWidth - $this->rightMargin, $this->row + $this->lineHeight);
				$this->fontSize += 2;
			}
			else
				$this->TextWrap($ccol, $this->row, $right - $ccol, $txt, 'C');
			$this->row -= $this->lineHeight;

		}

		$this->Font();
		$temp = $iline6 - $this->lineHeight - 2;
