<?php  
 include_once 'opendb.php';
$json =file_get_contents('php://input');


$array=json_decode($json);
//var_dump($array1);
//die($array1[1]->Entry_Date);
//die(json_last_error() );
$Eno=-1;
$Pay_Mode="0";
$Cheque_No="";
for ($x = 0; $x < count($array); $x++) 
{
         if($x ==0)
        {  
              
               $Entry_date=$array[$x]->Trans_Date;
               $notes=$array[$x]->Notes;
               $Amount=$array[$x]->Amount;
               $Disc_Amt=$array[$x]->Disc_Amt;
               $Voucher_No=$array[$x]->Voucher_no;
               $sub_code=$array[$x]->Sub_Code;
               $Party_ID=$array[$x]->Party_ID;
               $cust_name=$array[$x]->CUST_NAME;
               $counter_code=$array[$x]->Counter_Code;
               $client_app_id=$array[$x]->client_app_id;
               $sync_time=$array[$x]->sync_time;
               $TransID=$array[$x]->TransID;
               $Acc_id=$array[$x]->Acc_id;
               $Acc_name=$array[$x]->Acc_name;
               $Ref_No=$array[$x]->Ref_No;
               $Comp_ID=$array[$x]->comp_id;
               try {
                    $Pay_Mode=$array[$x]->Pay_Mode;
                    $Cheque_No=$array[$x]->Cheque_No;
               } catch (Exception $ex) {

               }
               
               
               $sql="select entry_no from receipts where trans_date ='$Entry_date' and vchr_no like '$Voucher_No' and client_app_id like '$client_app_id' and Amount=$Amount and comp_id=$Comp_ID;";
               $sresult = $conn->query($sql);
                if ($sresult->num_rows > 0 )
                { 
                    if($srow = $sresult->fetch_assoc())
                      {
                       $Eno= $srow['entry_no'];
                        $conn->close();
                        exit($Eno);
                    }
                   
                }
               
               
               $sql = "INSERT INTO receipts (client_entry_id,client_app_id,counter_code,vchr_no,trans_date,cust_code,cust_id,cust_name,notes,Amount,disc_amt,sync_time,acc_id,acc_name,ref_no,comp_id,pay_mode,cheque_no) " .
               " VALUES (".$TransID.",'".$client_app_id. "','".$counter_code."','".$Voucher_No."','".$Entry_date."','".$sub_code."',".$Party_ID.",'".$cust_name."','".$notes."',". $Amount.",".$Disc_Amt.",'".$sync_time."',".$Acc_id.",'".$Acc_name."','".$Ref_No."',".$Comp_ID.",".$Pay_Mode.",'".$Cheque_No."')";
               
               if ($conn->query($sql) === TRUE)
               {
                  $Eno = $conn->insert_id;
                 
               } else
               {
                  die ( "Error in insert Receipt:" . $sql . "<br>" . $conn->error);
               } 
        }
        $Doc_Entry_No=$array[$x]->Doc_Entry_No;
       // if($Doc_Entry_No!=0){
            $Bill_Date=$array[$x]->Bill_Date;
            $Doc_Entry_No=$array[$x]->Doc_Entry_No;
            $Doc_Vchr_No=$array[$x]->Doc_Vchr_No;
            $Amt_Paid=$array[$x]->Amt_Paid;        
            $sql = "INSERT INTO receipts_bills (entry_no,bill_date,doc_entry_no,doc_vchr_no,amt_paid,compid) " .
                                  " VALUES ($Eno,'$Bill_Date',$Doc_Entry_No,'$Doc_Vchr_No',$Amt_Paid ,$Comp_ID)";
       
            if ($conn->query($sql) != true)
           {

                   echo $x.  $sql . $conn->error;
                  $sql="delete from receipts where entry_no=". $Eno  ;
                  if ($conn->query($sql) === TRUE) 
                  {
                          $sql="delete from receipts_bills where entry_no=". $Eno  ;
                          $conn->query($sql);
                  }
                  die ( "Error in insert Receipt Det " . $sql . "\n" . $conn->error);
           }      
       //}
        
       
}
$conn->close();
echo($Eno);


?>