<?php  
 include_once 'opendb.php';
$json =file_get_contents('php://input');
//$json='[{"Entry_Date":"2018-01-29","Notes":"","Amount":325,"Voucher_no":"-0001","CUST_NAME":" ","Pay_Mode":"0","Sub_Code":"","Counter_Code":"","client_app_id":"0866-7600-3933-0909-0","sync_time":"2018-01-29 13:56:35","BillID":"1","prod_code":"1104","SL_No":1,"Quantity":1,"Rate":250,"Unit":"CTN","COST":7.24,"pack_qty":1,"sale_Date":"2018-01-29","discount":0,"Foc_Qty":0,"Foc_Unit":"CTN","Loc_code":"","Batch":""},{"Entry_Date":"2018-01-29","Notes":"","Amount":325,"Voucher_no":"-0001","CUST_NAME":" ","Pay_Mode":"0","Sub_Code":"","Counter_Code":"","client_app_id":"0866-7600-3933-0909-0","sync_time":"2018-01-29 13:56:35","BillID":"1","prod_code":"1106-E","SL_No":2,"Quantity":3,"Rate":25,"Unit":"CTN","COST":2.02,"pack_qty":1,"sale_Date":"2018-01-29","discount":0,"Foc_Qty":0,"Foc_Unit":"CTN","Loc_code":"","Batch":""}]';


$array=json_decode($json);
//var_dump($array1);
//die($array1[1]->Entry_Date);
//die(json_last_error() );
$Eno=-1;
for ($x = 0; $x < count($array); $x++) 
{
    

           
        if($x ==0)
        {   
               
               $Entry_date=$array[$x]->Entry_Date;
               
               $notes=$array[$x]->Notes;
               $Amount=$array[$x]->Amount;
               $Voucher_No=$array[$x]->Voucher_no;
               $sub_code=$array[$x]->Sub_Code;
               $cust_name=$array[$x]->CUST_NAME; 
               $cust_name1=$array[$x]->CUST_NAME1;
               $counter_code=$array[$x]->Counter_Code;
               $client_app_id=$array[$x]->client_app_id;
               $Pay_Mode=$array[$x]->Pay_Mode;
               $tax_amt=$array[$x]->tax_amt;
               $cgst_amt=$array[$x]->cgst_amt;
               $sgst_amt=$array[$x]->sgst_amt;
                $Comp_ID=$array[$x]->comp_id;
               $sql = "INSERT INTO sales_return (Entry_no,Entry_date,notes,Amount,Voucher_No,sub_code,cust_name,counter_code,client_app_id,pay_mode,cust_name1,tax_amt,sgst_amt,cgst_amt,comp_id) " .
               " VALUES (".$Eno.",'".$Entry_date."','".$notes."',". $Amount.",'".$Voucher_No."','".$sub_code."','".$cust_name."','".$counter_code."','".$client_app_id. "',".$Pay_Mode.",'".$cust_name1."',".$tax_amt.",".$sgst_amt.",".$cgst_amt.",".$Comp_ID.")";
               
               if ($conn->query($sql) === TRUE)
               {
                  $Eno = $conn->insert_id;
                  $sql="update sales_return set entry_no=". $Eno . " where pkey=" . $Eno ;
                  if ($conn->query($sql) === TRUE)
                  {
                  }
                  else 
                  {
                     die ( "Error in Entry No update " . $sql . "\n" . $conn->error);
                  }
               } else
               {
                  die ( "Error in insert sales return :" . $sql . "\n" . $conn->error);
               } 
       }
       
       
       $Comp_ID=$array[$x]->comp_id;
       $prod_code=$array[$x]->prod_code;
       $SL_No=$array[$x]->SL_No;
       $Quantity=$array[$x]->Quantity;
       $Rate=$array[$x]->Rate;
       $Unit=$array[$x]->Unit;
       $COST=$array[$x]->COST;
       $pack_qty=$array[$x]->pack_qty;
       
       $discount=$array[$x]->discount;
       $Foc_Qty=$array[$x]->Foc_Qty;
       $Foc_Unit=$array[$x]->Foc_Unit;
       $Loc_code=$array[$x]->Loc_code;
       $Batch=$array[$x]->Batch;
       $TAX_RATE=$array[$x]->TAX_RATE;
       $SGST_RATE=$array[$x]->SGST_RATE;
       $CGST_RATE=$array[$x]->CGST_RATE;
       
       $sql = "INSERT INTO sales_return_sub (Entry_no,sale_date,prod_code,SL_No,Quantity,Rate,Unit,COST,pack_qty,discount,Foc_Qty,Foc_Unit,Loc_code,Batch,taxrate,cgst_rate,sgst_rate,compid) " .
                                  " VALUES (".$Eno.",'".$Entry_date."','".$prod_code."',". $SL_No.",".$Quantity.",".$Rate.",'".$Unit."',".$COST.",".$pack_qty. ",".$discount.",".$Foc_Qty.",'".$Foc_Unit."','".$Loc_code."','".$Batch."',".$TAX_RATE.",".$CGST_RATE.",".$SGST_RATE.",".$Comp_ID.")";
       
       
       if ($conn->query($sql) != true)
       {
       
               echo $x.  $sql . $conn->error;
              $sql="delete from sales_return where entry_no=". $Eno  ;
              if ($conn->query($sql) === TRUE) 
              {
                      $sql="delete from sale_return_sub where entry_no=". $Eno  ;
                      $conn->query($sql);
              }
              die ( "Error in insert Sales ret Det " . $sql . "\n" . $conn->error);
              
       }
       
}

echo($Eno);


?>