<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='inde.php'; </script>";
 return;
}
include_once('opendb.php');
  $fromdt=$_REQUEST['fromdt'];
  $todt=$_REQUEST['todt'];
  $mfrom_date = $fromdt;
  $mto_date = $todt;
  $fromdt = implode('-', array_reverse(explode('-', $fromdt)));
  $todt = implode('-', array_reverse(explode('-', $todt)));
  $mgrp = "";
  $mgrp1[]="";
  $mtotal1[]=0;
  $brqry="select brname from branch where brcode='$brcode'";
$brrs=mysqli_query($db,$brqry);
$brrw=mysqli_fetch_array($brrs);
$brnchnm=$brrw[0];
$file =  'br_transfer.prn';
$handle = fopen($file, 'w');
$dss=chr(27).chr(71).chr(N);
$dse=chr(27).chr(72);
$initialized = chr(27).chr(64);
$load = chr(27).chr(25).chr(66);
$bold = chr(27).chr(14);
$unbold = chr(20);
$pagelenth=chr(27).chr(67).chr(36);
$Data = $initialized;
$Data .= "\n\n\n";
$Data .= "          KERALA KHADI & VILLAGE INDUSTRIES BOARD\n";
$Data .= "          CENTER NAME : ".$brnchnm." \n";
$Data .= "          Branch Transfer Report From ".$mfrom_date." To ".$mto_date."\n\n";
$Data .= chr(15);
$qry=mysqli_query($db,"select gname,invdate,sum(total),mba,comision,mrgin,pfchrgs,othr_chrgs,sum(taxamt),invno,tax,customer,refno,refdt,discount from brtransferout where invdate between '$fromdt' and '$todt' and brcode='$brcode' group by invdate,invno,gname");
//print "<table class='report' border='1'>";
//print "<tr><th COLSPAN=18>Branch Transfer Report for the period from <$mfrom_date> to <$mto_date></th></tr>";
$Data .= "|----|--------|--------|----------|-------|--------|--------|-------|------|------|------|-------|------|-------|-----|-----|----------|\n";
$Data .= "|TRS#|Supplier|Invoice | Cotton   |  Poly | Reeled |  Spun  |  VIP  | Wooll|Others| MDA  |Commiss|Discnt|Margin | PF  | Othr|  Total   |\n";
$Data .= "|    |        |   Dt   |          |       | Silk   |  Silk  |       |  en  |      |      | ion   |      |       |Chrgs|Chrgs| Amount   |\n";
$Data .= "|----|--------|--------|----------|-------|--------|--------|-------|------|------|------|-------|------|-------|-----|-----|----------|\n";
//print "<tr><th>GRN No</th><th>Supplier</th><th>Supplier Inv No</th><th>Entry Date</th><th>Cotton</th><th>Poly</th><th>Reeled<br>Silk</th><th>Spun<br>Silk</th><th>VIP</th><th>Woollen</th><th>Others</th><th>MDA</th><th>Commission</th><th>Discount</th><th>Margin</th><th>PF Charges</th><th>Other<br>Charges</th><th>Total Amount</th></tr>";
  $colspan=6;
  $mstr[]='';
  $i=1;
  $mgname = '.';
  $mtot_cot = 0;
  $mtot_pol = 0;
  $mtot_rel = 0;
  $mtot_spu = 0;
  $mtot_vip = 0;
  $mtot_woo = 0;
  $mtot_oth = 0;
  $mtot_total = 0;
  $mtot_mba= 0;
  $mtot_comision = 0;
  $mtot_discount = 0;
  $mtot_pfchrgs = 0;
  $mtot_othr_chrgs = 0;
  $mtot_taxamt = 0;
  $mtot_nettot = 0;
  $mtot_dbtrs = 0;
  $mgr_total = 0;
  $mgr_cot = 0;
  $mgr_pol = 0;
  $mgr_rel = 0;
  $mgr_spu = 0;
  $mgr_vip = 0;
  $mgr_woo = 0;
  $mgr_oth = 0;
  $mgr_mba= 0;
  $mgr_comision = 0;
  $mgr_discount = 0;
  $mgr_pfchrgs = 0;
  $mgr_othr_chrgs = 0;
  $mgr_taxamt = 0;
  $mgr_nettot = 0;
  $mgr_dbtrs = 0;
  $ctr=0;
  $mgr_dbtrs=0; $mgr_dbtrs1=0;
  $minvno = '';
  while($row=mysqli_fetch_array($qry))
    {
    if($minvno <>  $row[9] && $ctr <> 0)
    {
        $mtot_nettot = $mtot_cot+$mtot_pol+$mtot_rel+$mtot_spu+$mtot_vip+$mtot_woo+$mtot_oth-$mtot_mba-$mtot_comision-$mtot_discount+$mtot_mrgin+$mtot_pfchrgs+$mtot_othr_chrgs;
        $Data .= "|".substr(str_repeat(" ",14).$minvno,-4);
        $Data .= "|".substr($msup.str_repeat(" ",14),0,8);
//        $Data .= "|".substr(str_repeat(" ",14).$mrefno,-6);
        $date1 = date_create($mrefdt);
        $Data .= "|".substr(date_format($date1, 'd-m-y').str_repeat(" ",8),0,8);
        $mnum = number_format($mtot_cot, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",10).$mnum,-10);
        $mnum = number_format($mtot_pol, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",7).$mnum,-7);
        $mnum = number_format($mtot_rel, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",8).$mnum,-8);
        $mnum = number_format($mtot_spu, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",8).$mnum,-8);
        $mnum = number_format($mtot_vip, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",7).$mnum,-7);
        $mnum = number_format($mtot_woo, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",6).$mnum,-6);
        $mnum = number_format($mtot_oth, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",6).$mnum,-6);
        $mnum = number_format($mtot_mba, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",6).$mnum,-6);
        $mnum = number_format($mtot_comision, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",7).$mnum,-7);
        $mnum = number_format($mtot_discount, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",6).$mnum,-6);
        $mnum = number_format($mtot_mrgin, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",7).$mnum,-7);
        $mnum = number_format($mtot_pfchrgs, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",5).$mnum,-5);
        $mnum = number_format($mtot_othr_chrgs, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",7).$mnum,-5);
        $mnum = number_format($mtot_nettot, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",10).$mnum,-10)."|\n";

//        print "<tr><td>$minvno</td><td>$msup</td><td>$mrefno</td><td>$mrefdt</td><td align='right'>".number_format($mtot_cot,2,'.','')."</td><td align='right'>".number_format($mtot_pol,2,'.','')."</td><td align='right'>".number_format($mtot_rel,2,'.','')."</td><td align='right'>".number_format($mtot_spu,2,'.','')."</td><td align='right'>".number_format($mtot_vip,2,'.','')."</td><td align='right'>".number_format($mtot_woo,2,'.','')."</td><td align='right'>".number_format($mtot_oth,2,'.','')."</td><td align='right'>".number_format($mtot_mba,2,'.','')."</td><td align='right'>".number_format($mtot_comision,2,'.','')."</td><td align='right'>".number_format($mtot_discount,2,'.','')."</td><td align='right'>".number_format($mtot_mrgin,2,'.','')."</td><td align='right'>".number_format($mtot_pfchrgs,2,'.','')."</td><td align='right'>".number_format($mtot_othr_chrgs,2,'.','')."</td><td align='right'>".number_format($mtot_nettot,2,'.','')."</td></tr>";
        $mgr_cot +=  $mtot_cot;
        $mgr_pol +=  $mtot_pol;
        $mgr_rel +=  $mtot_rel;
        $mgr_spu +=  $mtot_spu;
        $mgr_vip +=  $mtot_vip;
        $mgr_woo +=  $mtot_woo;
        $mgr_oth +=  $mtot_oth;
        $mgr_mba+=  $mtot_mba;
        $mgr_comision +=  $mtot_comision;
        $mgr_discount +=  $mtot_discount;
        $mgr_pfchrgs +=  $mtot_pfchrgs;
        $mgr_othr_chrgs +=  $mtot_othr_chrgs;
        $mgr_mrgin +=  $mtot_mrgin;
        $mgr_nettot +=  $mtot_nettot;
        $mgr_dbtrs +=  $mtot_dbtrs;

        $mtot_cot = 0;
        $mtot_pol = 0;
        $mtot_rel = 0;
        $mtot_spu = 0;
        $mtot_vip = 0;
        $mtot_woo = 0;
        $mtot_oth = 0;
        $mtot_mba= 0;
        $mtot_comision = 0;
        $mtot_discount = 0;
        $mtot_pfchrgs = 0;
        $mtot_othr_chrgs = 0;
        $mtot_mrgin = 0;
        $mtot_nettot = 0;
        $mtot_dbtrs = 0;

    }
    $minvno = $row[9];
    ++$ctr;
    $mrefno = $row[12];
    $mrefdt = $row[13];
  $msup = $row[11];

                $mrefdt=implode('-', array_reverse(explode('-', $mrefdt)));
    $date = $row[1];
    if($row[0]== 'COTTON')
      $mtot_cot += $row[2];
    else if ($row[0]== 'POLYSTER')
      $mtot_pol += $row[2];
    else if ($row[0]== 'REELED SILK')
      $mtot_rel += $row[2];
    else if ($row[0]== 'SPUNSILK')
      $mtot_spu += $row[2];
    else if ($row[0]== '0% TAXABLE VIP' || $row[0]== '5% TAXABLE VIP' || $row[0]== '14.5% TAXABLE VIP' || $row[0]== '1% TAXABLE VIP')
      $mtot_vip += $row[2];
    else if ($row[0]== 'WOOLEN')
      $mtot_woo += $row[2];
    else
      $mtot_oth += $row[2];
    $mtot_mba+= $row[3];
    $mtot_comision =$row[4];
    $mtot_discount =$row[14];
    $mtot_mrgin+=$row[2]*($row[5]/100);
    $mtot_pfchrgs =$row[6];
    $mtot_othr_chrgs =$row[7];
    $i++;
  }
  $mgr_cot +=  $mtot_cot;
  $mgr_pol +=  $mtot_pol;
  $mgr_rel +=  $mtot_rel;
  $mgr_spu +=  $mtot_spu;
  $mgr_vip +=  $mtot_vip;
  $mgr_woo +=  $mtot_woo;
  $mgr_oth +=  $mtot_oth;
  $mgr_mba+=  $mtot_mba;
  $mgr_comision +=$mtot_comision;
  $mgr_discount +=$mtot_discount;
  $mgr_mrgin +=$mtot_mrgin;
  $mgr_pfchrgs +=$mtot_pfchrgs;
  $mgr_othr_chrgs +=$mtot_othr_chrgs;
  $mtot_nettot = $mtot_cot+$mtot_pol+$mtot_rel+$mtot_spu+$mtot_vip+$mtot_woo+$mtot_oth-$mtot_mba-$mtot_comision-$mtot_discount+$mtot_mrgin+$mtot_pfchrgs+$mtot_othr_chrgs;
        $Data .= "|".substr(str_repeat(" ",14).$minvno,-4);
        $Data .= "|".substr($msup.str_repeat(" ",14),0,8);
     //   $Data .= "|".substr(str_repeat(" ",14).$mrefno,-6);
        $date1 = date_create($mrefdt);
        $Data .= "|".substr(date_format($date1, 'd-m-y').str_repeat(" ",8),0,8);
        $mnum = number_format($mtot_cot, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",10).$mnum,-10);
        $mnum = number_format($mtot_pol, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",7).$mnum,-7);
        $mnum = number_format($mtot_rel, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",8).$mnum,-8);
        $mnum = number_format($mtot_spu, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",8).$mnum,-8);
        $mnum = number_format($mtot_vip, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",7).$mnum,-7);
        $mnum = number_format($mtot_woo, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",6).$mnum,-6);
        $mnum = number_format($mtot_oth, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",6).$mnum,-6);
        $mnum = number_format($mtot_mba, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",6).$mnum,-6);
        $mnum = number_format($mtot_comision, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",7).$mnum,-7);
        $mnum = number_format($mtot_discount, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",6).$mnum,-6);
        $mnum = number_format($mtot_mrgin, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",7).$mnum,-7);
        $mnum = number_format($mtot_pfchrgs, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",5).$mnum,-5);
        $mnum = number_format($mtot_othr_chrgs, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",7).$mnum,-5);
        $mnum = number_format($mtot_nettot, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",10).$mnum,-10)."|\n";
$Data .= "|----|--------|--------|----------|-------|--------|--------|-------|------|------|------|-------|------|-------|-----|-----|----------|\n";
    $Data .= "|         TOTAL        ";
        $mnum = number_format($mgr_cot, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",10).$mnum,-10);
        $mnum = number_format($mgr_pol, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",7).$mnum,-7);
        $mnum = number_format($mgr_rel, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",8).$mnum,-8);
        $mnum = number_format($mgr_spu, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",8).$mnum,-8);
        $mnum = number_format($mgr_vip, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",7).$mnum,-7);
        $mnum = number_format($mgr_woo, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",6).$mnum,-6);
        $mnum = number_format($mgr_oth, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",6).$mnum,-6);
        $mnum = number_format($mgr_mba, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",6).$mnum,-6);
        $mnum = number_format($mgr_comision, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",7).$mnum,-7);
        $mnum = number_format($mgr_discount, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",6).$mnum,-6);
        $mnum = number_format($mgr_mrgin, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",7).$mnum,-7);
        $mnum = number_format($mgr_pfchrgs, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",5).$mnum,-5);
        $mnum = number_format($mgr_othr_chrgs, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",7).$mnum,-5);
		$mgr_nettot +=  $mtot_nettot;
        $mnum = number_format($mgr_nettot, 1, '.', '');
        $Data .= "|".substr(str_repeat(" ",10).$mnum,-10)."|\n";
//  print "<tr><td>$minvno</td><td>$msup</td><td>$mrefno</td><td>$mrefdt</td><td align='right'>".number_format($mtot_cot,2)."</td><td align='right'>".number_format($mtot_pol,2)."</td><td align='right'>".number_format($mtot_rel,2)."</td><td align='right'>".number_format($mtot_spu,2)."</td><td align='right'>".number_format($mtot_vip,2)."</td><td align='right'>".number_format($mtot_woo,2)."</td><td align='right'>".number_format($mtot_oth,2)."</td><td align='right'>".number_format($mtot_mba,2)."</td><td align='right'>".number_format($mtot_comision,2)."</td><td align='right'>".number_format($mtot_discount,2)."</td><td align='right'>".number_format($mtot_mrgin,2)."</td><td align='right'>".number_format($mtot_pfchrgs,2)."</td><td align='right'>".number_format($mtot_othr_chrgs,2)."</td><td align='right'>".number_format($mtot_nettot,2)."</td></tr>";
$Data .= "|----|--------|--------|----------|-------|--------|--------|-------|------|------|------|-------|------|-------|-----|-----|----------|\n";
  $mgr_nettot +=  $mtot_nettot;
 // print "<tr><td colspan=4><b>Grand Total</td><td align='right'>".number_format($mgr_cot,2)."</td><td align='right'>".number_format($mgr_pol,2)."</td><td align='right'>".number_format($mgr_rel,2)."</td><td align='right'>".number_format($mgr_spu,2)."</td><td align='right'>".number_format($mgr_vip,2)."</td><td align='right'>".number_format($mgr_woo,2)."</td><td align='right'>".number_format($mgr_oth,2)."</td><td align='right'>".number_format($mgr_mba,2)."</td><td align='right'>".number_format($mgr_comision,2)."</td><td align='right'>".number_format($mgr_discount,2)."</td><td align='right'>".number_format($mgr_mrgin,2)."</td><td align='right'>".number_format($mgr_pfchrgs,2)."</td><td align='right'>".number_format($mgr_othr_chrgs,2)."</td><td align='right'>".number_format($mgr_nettot,2)."</td></tr>";
//  print "</table>";
//
$Data .= chr(18);
$Data .= chr(12);
fwrite($handle, $Data);
fclose($handle);
$output = shell_exec("lp -o media=Custom.10x6in '$file'");
$fromdt = implode('-', array_reverse(explode('-', $fromdt)));
$todt = implode('-', array_reverse(explode('-', $todt)));
header('location:branch_trasf_repo.php?fromdt='.$fromdt.'&&todt='.$todt);
?>