<?php
session_start();
$usr=$_GET['usr']; 
include 'class.ezpdf.php';
include 'includes/config.php';
include 'defs.php';
$pdf = new Cezpdf('A4','landscape');
$pdf->selectFont('fonts/Times-Roman.afm');
$pdf->ezStartPageNumbers(800, 16, 9, 'right', '', 1);

$nwords = array(  "", "One", "Two", "Three", "Four", "Five", "Six",
                        "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",
                        "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eightteen",
                       "Nineteen", "Twenty", 30 => "Thirty", 40 => "Forty",
                     50 => "Fifty", 60 => "Sixty", 70 => "Seventy", 80 => "Eigthy",
                     90 => "Ninety" );
                     
                     
                     function number_to_words ($x)
{
     global $nwords;
     if(!is_numeric($x))
     {
         $w = '#';
     }else if(($x % 1) != 0)
     {
         $w = '#';
     }else{
         if($x < 0)
         {
             $w = 'minus ';
             $x = -$x;
         }else{
             $w = '';
         }
         if($x < 21)
         {
             $w .= $nwords[$x];
         }else if($x < 100)
         {
             $w .= $nwords[10 * floor($x/10)];
             $r = ($x % 10);
             if($r > 0)
             {
                 $w .= ' '. $nwords[$r];
             }
         } else if($x < 1000)
         {

                     $w .= $nwords[floor($x/100)] .' Hundred';
             $r = ($x % 100);
             if($r > 0)
             {
                 $w .= ' '. number_to_words($r);
             }
         } else if($x < 1000000)
         {
                 $w .= number_to_words(floor($x/1000)) .' Thousand';
             $r = ($x % 1000);
             if($r > 0)
             {
                 $w .= ' ';
                 if($r < 100)
                 {
                     $w .= ' ';
                 }
                 $w .= number_to_words($r);
             }
         } else {
             $w .= number_to_words(floor($x/1000000)) .' Million';
             $r = ($x % 1000000);
             if($r > 0)
             {
                 $w .= ' ';
                 if($r < 100)
                 {
                     $word .= ' ';
                 }
                 $w .= number_to_words($r);
             }
         }
     }
     return $w;
}

$frm=$_POST['strFrom'];
$too=$_POST['strTo'];
$comp=$_POST['comp'];

if ($frm=='')
{
$nResult = MySQLi_Query($link, "select MIN(Inv_Date) as dte from p_details");
while($rstRow = mysqli_fetch_array($nResult))
{
$frm=$rstRow['dte'];
}
}

$result42 = mysqli_query($link,"delete from invprint");

$result3 = mysqli_query($link,"select * from p_details where Inv_Date between '$frm' and '$too' order by Inv_No ");
while($rstRow = mysqli_fetch_array($result3))
{
InsertRec("invprint",array(
"Inv_No" =>$rstRow['Inv_No'],
"Warehouse" => $rstRow['Warehouse'],
"Salesman" => $rstRow['Company'],

));
}


if(empty($comp))
{
$result4 = mysqli_query($link,"select * from invprint order by Inv_No ");
}


elseif(!empty($comp))
{
$result4 = mysqli_query($link,"select * from invprint where Salesman='$comp' order by Inv_No ");
}

$totalRows = mysqli_num_rows($result4);

$p_cnt = 0; 
while($rstRow4 = mysqli_fetch_array($result4))
{
$p_cnt++;    
$inv_no = $rstRow4['Inv_No'];

$pdf->addJpegFromFile('images/logo1.jpg',40,508,65);
$date1=Date("j-n-Y");
$pdf->ezSetDy(-130);
$pdf->setColor(0.72, 0.72, 0.72);
$pdf->filledRectangle(220, 805, 160, 20, 'rgb');
$pdf->setColor(0/255, 0/255, 139/255); //darkblue
$pdf->addText(300,560,14,"<b>GULF LUBRICANTS (PVT) LTD PAKISTAN</b>");

$pdf->addText(340,545,13,"<b>(SALES TAX COMPANY INVOICE)</b>");
$pdf->setColor(0,0,0);

$pdf->addText(10,442,12,"_________________________________________________________________________________________________________________________________________");


$result3 = mysqli_query($link,"select * from p_details where Inv_No='$inv_no' ");
while($rstRow = mysqli_fetch_array($result3))
{
$invdate=$rstRow['Inv_Date'];
$invdet=$rstRow['Inv_Det'];

$dcn=$rstRow['DC_No'];
$ftax=$rstRow['F_Amt'];
$st=$rstRow['S_Amt'];

$tot=$rstRow['Total'];
$gtot=$rstRow['G_Total'];
$ntot=$rstRow['N_Total'];
$purn=$rstRow['Pur_No'];

$comp=$rstRow['Company'];
$vcode=$rstRow['V_Id'];
$invi=$rstRow['Inv_N'];
$addby=$rstRow['addby'];
$addon=$rstRow['addon'];
$ware=$rstRow['Warehouse'];
}

$addby1 = strtoupper($addby);
$invdate=strtotime($invdate);
$invdate = date("d-m-Y", $invdate);

$pdf->addText(575, 500, 11, "<b>Invoice #: </b>     $invdet");
$pdf->addText(560, 485, 11, "<b>Invoice Date:</b>      $invdate");
$pdf->addText(562, 470, 11, "<b>Comp-DC #: </b>     $dcn <b>(Pur-Order #: </b> $purn)");
$pdf->addText(575, 455, 11, "<b>Entry By: </b>      $addby1");

$rst16 = MySQLi_Query($link, "Select NTN1, Adress, C_Person, Contact_No from acc_heads where Name='$comp'");
while($rstRow = mysqli_fetch_array($rst16))
{
$lic=$rstRow['NTN1'];
$cper=$rstRow['C_Person'];
$cont=$rstRow['Contact_No'];
$adr=$rstRow['Adress'];
}

$pdf->addText(30, 500, 11, "<b>Company Name:</b>     $comp");
$pdf->addText(30, 485, 11, "<b>Contact Person:</b>      $cper ($cont)");
$pdf->addText(30, 470, 11, "<b>Address:</b>                  $adr");

$pdf->setColor(0.72, 0.72, 0.72);
$pdf->filledRectangle(30, 443, 210, 20, 'rgb');
$pdf->setColor(0,0,0);
$pdf->addText(35, 450, 11, "<b>Target Warehouse:</b>             $ware");


$rst6 = MySQLi_Query($link, "Select * from purchases where Inv_No='$inv_no' and Inv_Type='Dir Purchase' order by Id");
$count=0;
$one=1;

$ftot=0;
$stot=0;
$gtot=0;
$ntot=0;
$tot1=0;
$tdis=0;

$qtot=0;
$qtqty=0;
$qcart=0;
$data = array();
    $cols = array();

while($rstRow = mysqli_fetch_array($rst6))
{
    $count=$one+$count;

$qtot += $rstRow['Qty'];
$qtqty += $rstRow['Tqty'];
$qcart += $rstRow['Carton'];
$ftot += $rstRow['F_Amt'];
$stot += $rstRow['S_Amt'];
$gtot += $rstRow['G_Total'];
$ntot += $rstRow['N_Total'];
$tot1 += $rstRow['Total'];
$tdis += $rstRow['R_Dis'];

$skul=$rstRow['Unit']."L";

    $data[] = array(
        'num'=>$count,
        'cod'=>$rstRow['Name'],
        'grade'=>$rstRow['Grade'],
        'unit'=>$skul,
        'qty'=>$rstRow['Qty'],
        'tqty'=>$rstRow['Tqty'],
        'carts'=>$rstRow['Carton'],
        'rate'=>$rstRow['Rate'],
        'dis'=>number_format($rstRow['R_Dis'],2),
        'tot'=>number_format($rstRow['Total'],2),
        'fed'=>number_format($rstRow['F_Amt'],2),
        'gross'=>number_format($rstRow['G_Total'],2),
        'stax'=>number_format($rstRow['S_Amt'],2),
        'ntot'=>number_format($rstRow['N_Total'],2)
    );
}

/* ----------------------------
   ADD GRAND TOTAL ROW
----------------------------- */

$data[] = array(
    'num'   => '',
    'cod'   => '<b>Grand Total</b>',
    'grade' => '',
    'unit'  => '',
    'qty'   => number_format($qtot,2),
    'tqty'  => number_format($qtqty,2),
    'carts' => number_format($qcart,2),
    'rate'  => '',
    'dis'   => number_format($tdis,2),
    'tot'   => number_format($tot1,2),
    'fed'   => number_format($ftot,2),
    'gross' => number_format($gtot,2),
    'stax'  => number_format($stot,2),
    'ntot'  => number_format($ntot,2)
);

$cols = array(
    'num'=>'<b>S. No</b>',
    'cod'=>'<b>Description of Goods</b>',
    'grade'=>'<b>Grade</b>',
    'unit'=>'<b>SKU</b>',
    'qty'=>'<b>QTY-Liters</b>',
    'tqty'=>'<b>QTY-Packs</b>',
    'carts'=>'<b>QTY-Cartons</b>',
    'rate'=>'<b>Value/Unit</b>',
    'dis'=>'<b>Trade Discount</b>',
    'tot'=>'<b>Total Value</b>',
    'fed'=>'<b>FED @ 5%</b>',
    'gross'=>'<b>Value Included FED</b>',
    'stax'=>'<b>Sales Tax @ 18%</b>',
    'ntot'=>'<b>Value Including FED+S.Tax</b>'
);

$pdf->ezTable($data,$cols,'',
    array('xPos'=>20,'xOrientation'=>'right','fontSize'=>9,'width'=>680,'showHeadings'=>1,'shaded'=>0,'showLines'=>2,
        'cols'=>array(
            'num'=>array('justification'=>'center','width'=>32),
            'cod'=>array('justification'=>'left','width'=>160),
            'grade'=>array('justification'=>'center','width'=>55),
            'unit'=>array('justification'=>'center','width'=>45),
            'qty'=>array('justification'=>'center','width'=>48),
            'tqty'=>array('justification'=>'center','width'=>48),
            'carts'=>array('justification'=>'center','width'=>48),
            'rate'=>array('justification'=>'right','width'=>53),
            'dis'=>array('justification'=>'right','width'=>48),
            'tot'=>array('justification'=>'right','width'=>59),
            'fed'=>array('justification'=>'right','width'=>52),
            'gross'=>array('justification'=>'right','width'=>59),
            'stax'=>array('justification'=>'right','width'=>55),
            'ntot'=>array('justification'=>'right','width'=>59)
        )
    )
);


$words=number_to_words($ntot);
$pdf->ezSetDy(-15);
$fntot=number_format($ntot,2);
$pdf->ezText("<b>Net Amount:  </b>       $fntot <b>($words Rupees Only)</b>" , 10, array("justification" => 'left'));
$pdf->ezSetDy(-38);

$pdf->ezText("<b>Checked By:  </b> <b>                                                                                                                                                                                                                                                                                                         Prepared By:               </b>" , 9, array("justification" => 'left'));



if ($p_cnt < $totalRows) {
        $pdf->ezNewPage();
    }

  } 
ob_clean();
$pdf->ezStream();
?>
