<?php 

require 'connection.php';

for($count = 0; $count<count($_POST['hidden_vno']); $count++)
{
 	$vno= $_POST['hidden_vno'][$count];
 	$refno= $_POST['hidden_refno'][$count];
 	$dated= $_POST['hidden_dated'][$count];
 	$supid= $_POST['hidden_accid'][$count];
 	$custid= $_POST['hidden_custid'][$count];
 	$qty= $_POST['hidden_qty'][$count];
 	$crate= $_POST['hidden_rate'][$count];
 	$itemid= $_POST['hidden_itemid'][$count];
 	$samt= 0 ;
 	$srate= 0 ;
 	$kat= 0 ;
 	$camt= $qty * $crate ;


// *****************************

// Insert into Broiler Transfer table
 $query="INSERT INTO `broilertransfer`(`vno`, `dated`, `supid`, `accid`, `itemid`, `vrate`, `crate`, `qty`, `kat`, `refno`, `status`) VALUES('$vno','$dated','$supid','$custid','$itemid','$srate','$crate','$qty','$kat','$refno','Posted')";
	$result=mysqli_query($connect,$query) or die(mysqli_error($connect));
// Insert into Stock Ledger table
	$costout= ($qty)*($crate);
 $stockqty="INSERT INTO `stockledger`(`dated`, `accid`, `vno`, `refno`, `invoiceno`, `itemid`, `stockout`, `rateout`, `costout`) VALUES('$dated','$custid','$vno','$refno','BRL-SL','$itemid','$qty','$crate','$costout')";
	$stockresult=mysqli_query($connect,$stockqty) or die(mysqli_error($connect));
// Insert  Ledger table FOR CUSTOMER ACCOUNT
	$supname="SELECT * from accreg where accid='$supid'";
	$suprun=mysqli_query($connect,$supname) or die(mysqli_error($connect));
	$suprow=mysqli_fetch_array($suprun);
	$acname= $suprow['accname'];

$des= 'From '. $acname . ' weight= '.$qty.' @'.$crate;

	
 $ledgerqry="INSERT INTO `ledger`(`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `debit`) VALUES('$dated','$custid','$des','$vno','$refno','BRL-SL','$costout')";
	$ledgerrun=mysqli_query($connect,$ledgerqry) or die(mysqli_error($connect));

	$saleledgerqry="INSERT INTO `ledger`(`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `credit`) VALUES('$dated','2','$des','$vno','$refno','BRL-SL','$costout')";
	$saleledgerrun=mysqli_query($connect,$saleledgerqry) or die(mysqli_error($connect));
	
	}


	if($result){
		// Fetch name of customer
$qry = "SELECT * FROM accreg  where accid = '$custid'";
   $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
   $row= mysqli_fetch_array($run);
   $mobno =$row['phone'];
   $custname = $row['accname'];
 
 $qty1=0;
 $qty2=0;
 $rate1=0;
 $rate2=0;
 $amt1=0;
 $amt2=0;
 $tamt=0;
$dated2 = $dated;
 $dt2= date_create($dated2);
  $dated3=date_format($dt2,"d-m-Y");
// $msgs =$custname.'\n'.'Gari#  Qty  Rate \n';
 $msgs = $dated3."\r\n"."Dear ".$custname."\r\n".'Gari#  Qty  Rate'."\r\n";


   $qry1 = "SELECT * FROM broilertransfer  where accid = '$custid' and dated='$dated'";
   $run1 = mysqli_query($connect, $qry1) or die(mysqli_error($connect));
   while ( $row1= mysqli_fetch_array($run1)) {
    $msgs =$msgs.$row1['refno']. ' - ' .$row1['qty']. ' @ ' .$row1['crate']."\r\n";
     $qty1 =  $row1['qty'];
     $qty2 = $qty2 + $qty1;
     $tamt = $tamt + ($row1['qty'] *$row1['crate']);
     $rate1 = $row1['crate'];
     if($rate2< $row1['crate']){
      $rate2 = $row1['crate'];
     }

     $amt2= $qty1 * $rate1;
     $amt1 =$amt1+ $amt2;
   }
   $msgs = $msgs.'Total Weight = '.$qty2."\r\n";
   $msgs = $msgs.'Amount Rs.'.$tamt;
   $qty=$qty2;
    $amt= $amt1;
   $rate=$rate2;

  
$username = "923206656062";///Your Username
$password = "pashi253";///Your Password
$mobile = $mobno;///Recepient Mobile Number
$sender = "Kashif";
// $message =$custname.'\n Total Weight=  '.$qty.' \n Rate= '.$rate.' \n Amount= '.$amt;
$message =$msgs."\r\n".'IRFAN P/S Khanpur.';
////sending sms

$post = "sender=".urlencode($sender)."&mobile=".urlencode($mobile)."&message=".urlencode($message)."";
$url = "https://sendpk.com/api/sms.php?username=923206656062&password=pashi253";
$ch = curl_init();
$timeout = 30; // set to zero for no timeout
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)');
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$result = curl_exec($ch); 
/*Print Responce*/
// echo $result; 
echo "Message Sent...";
	}