<?
session_start();
 if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}
if($savecount==0)
{
//$tbl2="stocksummary1".$brcode.$user;
include ("opendb.php");
  
 

 $prbldt = date("Y-m-d", strtotime($prbldt));
 $prdt = date("Y-m-d", strtotime($prdt));
$i=0;
foreach($partcode as $a)
{
 $partcode[$i]=$a; 

 $i++;
}
$i=0;
foreach($partname as $a)
{
 $partname[$i]=$a; 

 $i++;
}
$i=0;
foreach($qty as $a)
{
 $qty[$i]=$a; 
 $i++;
}
$i=0;
foreach($unitprice as $a)
{
 $unitprice[$i]=$a; 
 $i++;
}
$i=0;
foreach($tot as $a)
{
 $tot[$i]=$a; 

 $i++;
}

$i=0;
foreach($dis as $a)
{
 $dis[$i]=$a; 
 $i++;
}


$i=0;
foreach($unit as $a)
{
 $unit[$i]=$a; 
 $i++;
}

$i=0;
foreach($conv as $a)
{
 $conv[$i]=$a; 
 $i++;
}
$i=0;
foreach($part_type as $a)
{
 $part_type[$i]=$a; 
 $i++;
}


$flg=0;

for($j=0;$j<$i;$j++)
{
 $k=$j+1;
 $qry=mysqli_query($db,"select * from spareprmast where partcode='$partcode[$j]' ");
 if(mysqli_num_rows($qry)<=0)
 {
  // echo "select * spareprmast where partcode='$partcode[$j]' and brcode='$brcode'";
  print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
  $flg++;

 }
if(($qty[$j]=='')||($qty[$j]=='0'))

    {
    print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
  $flg++;
    }
  
}
if($flg>0)
 {
  echo "<small>Check the  partname/Qty of Red marked row(s)</small>";

 }
else
 {


  $re=mysqli_query($db,"select max(prno) from workshop_purmast where brcode='$brcode' ");
//print "select max(invno) from invoice where brcode='$brcode' ";
     if(mysqli_num_rows($re)<1)
       {
           $purid=1; 
       }
       else
       {
        $row=mysqli_fetch_array($re);
        
         $purid=$row[0]+1;
        }

 

		$jvres=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
		$jvrow=mysqli_fetch_row($jvres);

		if($jvrow[0]>0)
 			$jvno=($jvrow[0]+1);
		else
                     $jvno=1;





 $flg1=0;
	for($m=0;$m<$i;$m++)
       {
    
   $qry1=mysqli_query($db,"select * from newpartmaster where partcode='$partcode[$m]' ");
     $row1=mysqli_fetch_array($qry1);
      $qry2=mysqli_query($db,"select * from subgroupmasterparts where subname='$row1[2]' ");
     $row2=mysqli_fetch_array($qry2);
    // echo " insert into workshop_purmast values('$partname[$m]','$qty[$m]','$row1[1]','$row2[1]','$purid','$prdt','$qty[$m]','$unitprice[$m]','$dis[$m]','$tot[$m]','$brcode')";
  mysqli_query($db,"insert into workshop_purmast values('$partcode[$m]','$partname[$m]','$row1[2]','$row2[1]','$purid','$prdt','$qty[$m]','$unitprice[$m]','$dis[$m]','$tot[$m]','$brcode','$prbil','$prbldt','$sup','$unit[$m]','$conv[$m]','$jvno','$part_type[$m]')");
 mysqli_query($db,"update sparestock set currstock=(currstock+'$qty[$m]') where partcode='$partcode[$m]'and brcode='$brcode'");
	$flg1++;

      }  
  

  $taxdrres=mysqli_query($db,"select sum(tot) from workshop_purmast  where prno='$purid' and  brcode='$brcode'");

  $taxdrrow=mysqli_fetch_array($taxdrres);
  
    $ledger='Spare Purchase';  
  $sql="insert into journal2 values('$prdt','$jvno','Dr','$ledger','$taxdrrow[0]','0','Spare purno: $purid,PBill:$prbil','$brcode','Spare Purchase',' ','','$prbil')";
     mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
  
  $sql="insert into journal2 values('$prdt','$jvno','Cr','$sup','0','$taxdrrow[0]','Spare purno: $purid,PBill:$prbil','$brcode','Spare Purchase',' ','','$prbil')";
     mysqli_query($db,$sql) or die('Led Post Err3'.mysqli_error($db));

  


if($pmode=='CASH')
{
$rs=mysqli_query($db,"select max(cpno) from cshpay where brcode='$brcode'");
$cr_row=mysqli_fetch_row($rs);
 if($cr_row[0]<=0)
   $crno=1;
 else   
   $crno=$cr_row[0]+1;

$jpostres=mysqli_query($db,"select sum(dramt) from journal2 where jvno='$jvno' and partic='$ledger' and brcode='$brcode'");
$jpostrow=mysqli_fetch_row($jpostres);
$jvno++;

 $sql="insert into journal2 values('$prdt','$jvno','Dr','$sup','$taxdrrow[0]','0','Cash Paid: $purid,PBill:$prbil,CP:$crno','$brcode','CP','lpurchase','$purid','$crno')";
mysqli_query($db,$sql) or die(mysqli_error($db));

  $sql="insert into journal2 values('$prdt','$jvno','Cr','Cash','0','$taxdrrow[0]','Purchase $ledger($purid,PBill:$prbil,CP:$crno)','$brcode','CP','lpurchase','$jcno','$crno')";
mysqli_query($db,$sql) or die(mysqli_error($db));

 $sql="insert into cshpay values('$prdt','$crno','Dr','$ledger','$taxdrrow[0]','0','Purchase $ledger($purid,pbillno:$prbil)','$brcode','$jvno','$purid','$purid')";
mysqli_query($db,$sql) or die(mysqli_error($db));

//$sql="update pjcardparts set csh_jvno='$jvno' where invno='$invno' and brcode='$brcode'";
// mysqli_query($db,$sql) or die(mysqli_error($db));

//print "<script> window.open('../accounts/payrec/prncshrec.php?no=$crno'); </script>";

} // end if pay type




 if($flg1==$i) 
 {
  echo "Data saved with Purchase id:".$purid;
  echo "<script> document.getElementById('savecount').value=1;document.location='workshop_purchase.php';</script>";
 }  
  }


}
elseif($savecount==1)
{
echo "Data already saved";
  echo "<script> document.location='workshop_purchase.php';</script>";
}

?>
