<? 
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.php'; </script>";
 return;
}
include("test.php");
include("../opendb.php");
include ("../test.php");
//include("calstock.php");
$invno=$_REQUEST['invno'];
$mnfr=$_REQUEST['mnfr'];
$re=mysqli_query($db,"select * from spareinvoicedc where invno='$invno' and brcode='$brcode' and invdate>='2017-07-01' ");
if(mysqli_num_rows($re)<=0)
 {
    print "Invoice No does not exist";
    return;
 }
$jcrow=mysqli_fetch_array($re);
$ker=mysqli_query($db,"select brstate from branch where brcode='$brcode'");
$kar=mysqli_fetch_array($ker);
if($foc=='foc')
$re=mysqli_query($db,"select * from spareinvoicefoc where invno='$invno' and invtype='$invtype' and brcode='$brcode'")or die('err:'.mysqli_error($db));
else
$re=mysqli_query($db,"select * from spareinvoicedc where invno='$invno'  and brcode='$brcode' and invdate>='2017-07-01'  ")or die(mysqli_error($db));
$row=mysqli_fetch_array($re)or die("Fetch Error:".mysqli_error($db));



$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);

$cusres=mysqli_query($db,"select * from customer where acname='$row[customer]'");
$cusrow=mysqli_fetch_array($cusres);
?>
<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>AMPL Delivery Challan</title>

<style>

body{
  -webkit-print-color-adjust:exact;
}
.printable{ 
      page-break-before: always;

      margin-top:30px;
    }
.tbl
{
border: 1px solid black;
border-bottom:0px solid white;
font-size:10px;
text-transform:uppercase;
}
.tbl td
{
border: 0px solid black;
}
#tbl
{
border: 1px solid black;

}
.tble 
{
border: 1px solid black;
border-bottom:0px solid white;
font-size:10px;
border-collapse:collapse;
text-transform:uppercase;

}
.tble  td{
height:15px;
margin-left:10px;
margin-right:10px;
padding-left:10px;
padding-right:10px;
}

.tble th
{
border:1px solid black;
}
.tbl1{
border: 1px solid black;
border-bottom:0px solid white;
font-size:10px;
border-collapse:collapse;
text-align:right;
text-transform:uppercase;
}
.tbl1 td{
border-bottom:1px solid black;
height:10px;
}

#add_td{
font-size:12px;
height:15px;
}
#add_td br{
margin:0px;
padding:0px;
}
.tot_td{
border-bottom:0px solid black;
}
.tot_td1{
border-top:0px solid black;
}
#tot_tbl td{
border:1px solid black;
border-collapse:collapse;
}
body
 {font-family:Arial;
   }

p{
 margin:0px;
 padding:0px;
 }


  @media print
        {
            #head
            {
                background-color:grey;
            }
        }

</style>
<body onload='window.print()'>
<?
   if($foc=='foc')
 $sql="select * from spareinvoicefoc where invno='$invno' and invtype='$invtype' and brcode='$brcode'";
else
 $sql="select * from spareinvoicedc where invno='$invno' and brcode='$brcode'";
$re1=mysqli_query($db,$sql)or die('err:'.mysqli_error($db));

 


    $q_count1=mysqli_query($db,"select count(*) from spareinvoicedc where invno='$invno' and brcode='$brcode' ");
    $cnt1=mysqli_fetch_array($q_count1);

   

    $count=$cnt1[0];
    
    if(strlen($count)<=0)
    {
        $count=0;
    }

    $limit=23;
 
    $pg_no=floor($count/$limit);
 
 
    $i=1;
	$jtot=0;
	$pdisctot=0;
	$ltot=0;
	$subtot=0;
	$ldis=0;
 $ttdis=0;
 $handt=0;
	$res=mysqli_query($db,"select * from spareinvoicedc where invno='$invno' and brcode='$brcode' order by cgstp ");

	 while($res1=mysqli_fetch_array($res))
	 { 
	    
        $slno[$i]=$i;
        $code[$i]=$res1['partcode'];
        $item[$i]=$res1['partname'];
        $qty[$i] =$res1['qty'];
        $rate[$i]=$res1['price'];
        $disc[$i]=$res1['disc'];
		$partstotal+=$res1['qty']*$res1['price'];
		$pdisctot+=$res1['disc'];
		$tqty+=$res1['qty'];
		$ref=$res1['ref'];
		$paytype=$res1['paytype'];
		$handt+=$res1['hand'];
		$hsn1[$i]=$res1['hsncode'];

		if($row[vatorcst]=='INTRASTATE')
		{
		    $cgstprc[$i]=$res1['cgstp'];
		    $sgstprc[$i]=$res1['sgstp'];
		    $cgstamt[$i]=$res1['cgstamt'];
		    $sgstamt[$i]=$res1['sgstamt'];
		    $sgst_tot+=$res1['sgstamt'];
		    $cgst_tot+=$res1['cgstamt'];
		    $igst_tot=0;
		}
		if($row[vatorcst]=='INTERSTATE')
		{
		    $igstprc[$i]=$res1['igstp'];
		    $igstamt[$i]=$res1['igstamt'];
		     $sgst_tot=0;
		    $cgst_tot=0;
		    $igst_tot+=$res1['igstamt'];
		}
		$ptot[$i]=$res1['tot'];
		$jtot+=$res1['tot'];
		//$jtot+=$res1['hand'];
	
		$i++;
             
	}

 $ptype=$res1['paytype'];
	

$gtot=$jtot+$ltot;


print $invdate1;



$ntot1=$gtot-$pdisctot;
$ntot=$ntot1-$row['price']-$ldis;


 $tot_fill=0;

 for($p=0;$p<=$pg_no;$p++)
 {
   if($p==$pg_no)
      {
       $tot_fill=1;
      }
               /* to fill total amt in last table if it is last print page */
   $lower_lmt=($p*$limit)+1;
    $upper_lmt=($p+1)*$limit;
    

if($brcode=='MYS543')
{


?>

	<table border='0' width='100%' 
     
          <? if($p>0){
           ?>
            class='printable'
           <?
           }
           ?>
             >
	    
	    <tr>
	        <td align=right><img src='../images/psn logo.jpg' ></td>
	<td align='center' width='100%' id='add_td'>
<?
	$qry1=mysqli_query($db,"select * from branch where brcode='$brcode'");
	$row1=mysqli_fetch_array($qry1);
		print "<font size='5'><b>PSN AUTOMOTIVE MARKETING (P) LTD</b></font></p>";
	print "<font size='2'><b>".$row1[braddr]."</b></font><p >".$row1[brcity].",<b>".$row1[region]."<br>Phone : ".$row1[brphone];
		print "<p><b>GSTIN:$row1[cstno]</b></p>";
	//if(($row1[7]>0)&&($row1[7]!=''))
	
 	//if(($row1[8]>0)&&($row1[8]!=''))
  		 //print " Fax: ".$row1[8];
	print "<p>Email: sales@psnce.com";
	//if($row1[11]!='')
	//	print ",".$row1[11]."<br>Web: ".$row1[12];
	//else
		//print "</p><p>Web: ".$row1[12];
		$fromdt1=implode('-', array_reverse(explode('-', $row[entrydate])));
		
	    $brcol=substr($row[mnfr_code],0,3).'_ret_spr';
        $tr = "select $brcol from branch where brcode = '$brcode'";
        $trd=mysqli_query($db,$tr);
        $trdrow=mysqli_fetch_row($trd);	
		
		
		
		
		
?>
	</td>

	<td align='right'  width='40%'>  </td></tr>
	    
	    
	</table>
	<?
}
else
{
	?>
	
	<table border='0' width='100%' 
     
          <? if($p>0){
           ?>
            class='printable'
           <?
           }
           ?>
             >
	    
	    <tr>
	        
	<td align='center' width='100%' id='add_td'>
<?
	$qry1=mysqli_query($db,"select * from branch where brcode='$brcode'");
	$row1=mysqli_fetch_array($qry1);
		print "<font size='5'><b>PSN AUTOMOTIVE MARKETING (P) LTD</b></font></p>";
	print "<font size='2'><b>".$row1[braddr]."</b></font><p >".$row1[brcity].",<b>".$row1[region]."<br>Phone : ".$row1[brphone];
		print "<p><b>GSTIN:$row1[cstno]</b></p>";
	//if(($row1[7]>0)&&($row1[7]!=''))
	
 	//if(($row1[8]>0)&&($row1[8]!=''))
  		 //print " Fax: ".$row1[8];
//	print "<p>Email: sales@psnce.com";
	//if($row1[11]!='')
	//	print ",".$row1[11]."<br>Web: ".$row1[12];
	//else
		//print "</p><p>Web: ".$row1[12];
		$fromdt1=implode('-', array_reverse(explode('-', $row[entrydate])));
		
	    $brcol=substr($row[mnfr_code],0,3).'_ret_spr';
	    
        $tr = "select $brcol from branch where brcode = '$brcode'";
        $trd=mysqli_query($db,$tr);
        $trdrow=mysqli_fetch_row($trd);	
		
		
		
		
		
?>
	</td>

	<td align='right'  width='40%'>  </td></tr>
	    
	    
	</table>
	
	<?
}
	?>
       <br>
	<table border='1' width='100%' style='border-collapse:collapse;'><tr><td  align='left'  >
	    Challan No:
	    <?
if($brcode == 'HUBCHI')
{
print 'CHI';
}
else if($brcode == 'HUBEKA')
{
print 'ILK';
}
else if($brcode == 'HUBHOS')
{
print 'HOS';
}
else if($brcode == 'PAT649')
{
print 'TLA';
}
else if($brcode == 'KAN248')
{
print 'KAN';
}
else if($brcode == 'KAN332')
{
print 'KHD';
}
else
{
print substr($brcode,0,4);
}
?>

	    
	    
	    <? echo 'DC'.$invno?></td>
	    
	<th  align='center'  width='50%' >Delivery Challan<br>(Rule 55 of the GST Rules  2017)  </th>
	<td  align='right'  >Date:<? echo $fromdt1 ?></td>
	</tr></table>
 
<table width=100% class='tble' border='0'>
    <tr><td>
       <table width=100%  border='0'>
           <tr>
            <td colspan=3>Details of Consignee </td>   
           </tr>
           <tr>
            <td>Name</td><td>:</td> <td><? echo $row['customer']; ?></td>    
           </tr>
           <tr>
            <td>Address</td><td>:</td> <td><? echo $row['addr']; ?></td>    
           </tr>
           <?
           if ($cusrow['state1']=='Kerala')
           {
              $ccode='32'; 
           }
           if ($cusrow['state1']=='Karnataka')
           {
              $ccode='29'; 
           }
           
            if ($cusrow['state1']=='Tamil Nadu')
           {
              $ccode='33'; 
           }
            if ($cusrow['state1']=='Maharastra')
           {
              $ccode='27'; 
           }
           ?>
           <tr>
            <td>State</td><td>:</td> <td><? echo $cusrow['state1']; ?>   [StateCode :<?echo $ccode; ?>] </td>    
           </tr>
           <tr>
            <td>GSTIN</td><td>:</td> <td><? echo $cusrow['gst']; ?> </td>    
           </tr>
           <tr>
            <td>Transportation Purpose</td><td>:</td> <td><? echo $ref; ?> </td>    
           </tr>

           </table> 
    </td>
    
    <td>
       <table width=100%  border='0'>
           <tr>
            <td colspan=3>Details of Consignee (Shipped to)</td>   
           </tr>
           <tr>
            <td>Name</td><td>:</td> <td><? echo $row['customer']; ?></td>    
           </tr>
           <tr>
            <td>Address</td><td>:</td> <td><? echo $row['addr']; ?></td>    
           </tr>
           
           
           <tr>
            <td>State</td><td>:</td> <td><? echo $row['state']; ?></td>    
           </tr>
           <tr>
            <td>GSTIN</td><td>:</td> <td><? echo $cusrow['gst']; ?></td>    
           </tr>
         
           </table> 
        
    </td>
        
        
        
        
	</tr>
	</table>
	
	
	
	<table width=100% class='tble'  border='1' FRAME=BOX BORDER=1 CELLPADDING=0 CELLSPACING=0 RULES=COLS>
	 <? if($row[vatorcst]=='INTERSTATE')
		{
		    ?>
		 	   <tr><th>SlNo<th>PartCode<th >Part Name<th>HSN Code <th> Qty<th> Rate<th>Disc <th>Taxable Value<th>CGST %<th>CGSt Value<th>SGST %<th>SGST Value<th>IGST %<th>IGSt Value<th>Approx.Value</tr>
  
		    <? 
		}
		    if($row[vatorcst]=='INTRASTATE')
		{
		    ?>
		  
		  	<tr><th>SlNo<th>PartCode<th >Part Name  <th>HSN Code<th>Qty <th> Rate<th>Disc <th>Taxable Value<th>CGST %<th>CGSt Value<th>SGST %<th>SGST Value<th>IGST %<th>IGST Value<th>Approx.Value</tr>

<?
        }
    if($row[vatorcst]=='INTRASTATE')
	{ 
	    
        for($k=$lower_lmt;$k<=$upper_lmt;$k++)
	    {
	        
	      $rat=($qty[$k]*$rate[$k]);
         $dis=$rat*$disc[$k]/100;
         $ttdis+=$dis;
         $btamt=$rat-$dis;
         //$hsn=gethsncode($code[$k]);
          if($slno[$k]=='')
         {
              $btamt='';
         $dis='';  
          $hsn='';
         }
        print "<tr><td width='4%' >".$slno[$k]."</td><td >".$code[$k]."</td><td>".$item[$k]."</td><td>$hsn1[$k]</td><td align='right' >".$qty[$k] ."</td><td align='right' width='75'>".$rate[$k]."</td><td align='right' width='75'>".$dis."</td><td>$btamt</td><td align='right' width='75'>".$cgstprc[$k]."</td><td align='right' width='75'>".$cgstamt[$k]."</td><td align='right' width='75'>".$sgstprc[$k]."</td><td align='right' width='75'>".$sgstamt[$k]."</td><td></td><td></td><td align='right' width='75'>".$ptot[$k]."</td></tr>";
        
//        print "<tr><td width='4%' >".$slno[$k]."</td><td >".$code[$k]."</td><td>".$item[$k]."</td><td>$hsn1[$k]</td><td align='right' >".$qty[$k] ."</td><td align='right' width='75'>".$ptot[$k]."</td></tr>";
	    }
	}
	if($row[vatorcst]=='INTERSTATE')
	{ 
	    
        for($k=$lower_lmt;$k<=$upper_lmt;$k++)
	    {
	          
         $rat=($qty[$k]*$rate[$k]);
         $dis=$rat*$disc[$k]/100;
         $ttdis+=$dis;
         $btamt=$rat-$dis;
        // $hsn=gethsncode($code[$k]);
         if($slno[$k]=='')
         {
             $btamt='';
         $dis='';  
         $hsn='';
         }
       print "<tr><td width='4%' >".$slno[$k]."</td><td >".$code[$k]."</td><td>".$item[$k]."</td><td>$hsn1[$k]</td><td align='right' >".$qty[$k] ."</td><td align='right' width='75'>".$rate[$k]."</td><td align='right' width='75'>".$dis."</td><td>$btamt</td><td></td><td></td><td></td><td></td><td align='right' width='75'>".$igstprc[$k]."</td><td align='right' width='75'>".$igstamt[$k]."</td><td align='right' width='75'>".$ptot[$k]."</td></tr>";
	     
  //      print "<tr><td width='4%' >".$slno[$k]."</td><td >".$code[$k]."</td><td>".$item[$k]."</td><td>$hsn1[$k]</td><td align='right' >".$qty[$k] ."</td><td align='right' width='75'>".$rate[$k]."</td><td align='right' width='75'>".$ptot[$k]."</td></tr>";
	    }
	}


$tr1 = "select hypoth from hypo where brcode = '$brcode' and invno='$invno'";
        $trd1=mysqli_query($db,$tr1);
        $trdrow1=mysqli_fetch_row($trd1);	
?>

	</table>

	<table  width=100% class='tbl1' id='tbl' border='1' style='height:50px;'>
	<tr><td align='left'  width='30%' ><b>:</b><?  ?></td><td align='left' width='30%'><b>Verified by:</b>--</td><td rowspan='4'>

           <table width='100%'  style='border:0px solid black;border-collapse:collapse;margin:0px' border=0 id='tot_tbl'>
    	     	<tr><td  class='tot_td1' align='center'><b>Total Quantity : <?echo $tqty ?></b></td><td  class='tot_td1'>Amounts</td></tr>
 	   	
	   
          	
	   <!--
	   	<tr><td align='right'>Parts Total</td><td ><? if($p==$pg_no){ echo putcoma($partstotal); } ?></td></tr>
	   	<tr><td align='right'>Discount Total</td><td ><? if($p==$pg_no){ echo putcoma($ttdis); } ?></td></tr>
	   		   	<tr><td align='right'>Taxable Total</td><td ><? if($p==$pg_no){ echo putcoma($partstotal-$ttdis); } ?></td></tr>

        <tr><td  align='right' height='10'>CGST Total</td><td ><? if($p==$pg_no){ echo  $cgst_tot; }?></td></tr>

    <tr><td align='right'>Discount Total</td><td ><? if($p==$pg_no){ echo putcoma($pdisctot); } ?></td></tr>
     
	   	
	   	<tr><td align='right'>SGST Total</td><td ><? if($p==$pg_no){ echo $sgst_tot; } ?> </td></tr>
             <tr><td align='right'>IGST Total</td><td ><? if($p==$pg_no){ echo $igst_tot; } ?> </td></tr> 
             -->
              <tr><td align='right'>Transport</td><td ><? if($p==$pg_no){ echo $handt; } ?> </td></tr> 
             
             <?
             //$jtot=number_format($jtot,2,'','');
            
             $rnd=round($jtot);
             $rnd2=round(($rnd-$jtot),2);
?>
             <tr><td align='right'>Roundoff</td><td ><? if($p==$pg_no){ echo $rnd2; } ?> </td></tr> 
             
              <tr><td align='right' class='tot_td' style='font-size:12px'><b>Approx.Value</td><td class='tot_td' style='font-size:12px'><b><? if($p==$pg_no){ echo putcoma($rnd); } ?></b></td></tr>
          </table>


	</td></tr>

	
	

	<tr><td colspan=2>
	<table border=0 style='border:0px solid white; ' width=100%><tr>
	<td style='border:0px solid white;text-align:left;text-transform:none;'>
	</td><td style='border:0px solid white; text-align:right;'> </td></tr>
	<tr><td style='border:0px solid white;text-align:left;text-transform:none;'></br>

			Sign and Seal</td> <?echo word($rnd) ?> ONLY  <td style='border:0px solid white; text-align:right;'>
	</td</tr></table>


	</td>

	</tr></table>
      
       <table border='1' width='100%' style='border-collapse:collapse;margin-top:10px;font-size:10px;'>
	<td align='right'  width='100%' style='border:0px solid white;height:12px;'><? echo 'Page:'.($p+1).'/'.($pg_no+1);?></td></tr></table>

<?
 }//close for loop

?>
