<?php
    include_once 'opendb.php';
    $mess="OK";
    $upderrors="None";
    if (isset($_POST["data"]))
    {
        $content=$_POST["data"];
        $content = iconv('UTF-8', 'UTF-8//IGNORE', $content);
        $xml=simplexml_load_string($content) or die("Error: Cannot create object/parse error");
        $rownum=1;
        $RecID=0;
        foreach ($xml as $Record){ 
            if($rownum==1){
                $CompID= $Record->comp_id;
                 if( $CompID==0){
                    $conn->close();
                    die ( "Error: Co ID is Zero");
                }
                $sql = "SELECT * FROM sales_return WHERE web_ref_no =$Record->web_ref_no and comp_id=" .$CompID;
                $result = $conn->query($sql);
                if ($result->num_rows == 0 ) {
                       $sql = "INSERT INTO sales_return (web_ref_no,comp_id) VALUES ($Record->web_ref_no,$CompID)";
                       if ($conn->query($sql) === TRUE)
                       {
                          $RecID = $conn->insert_id;
                          $mess="OK";
                       } else
                       {
                          $mess= "Error: " . $sql . "<br>" . $conn->error;
                          $upderrors.="Error: " . $sql . "<br>" . $conn->error;
                       }       
                }
                else {
                     if($srow = $result->fetch_assoc()){
                        $RecID=$srow["pkey"]; 
                        $sql = "delete FROM sales_return_sub WHERE Entry_No =$RecID and compid=$CompID";
                        $conn->query($sql);
                     } else{
                          $mess= "Error: fetching row ";
                          $upderrors.="Error:  fetching row " . $sql ;
                     }
                }
                if ($mess=="OK")
                {
                    $sql = "UPDATE sales_return SET entry_date='$Record->entry_date',
                                             amount=$Record->amount,
                                             voucher_no='$Record->voucher_no',
                                             sub_code='$Record->sub_code',
                                             acc_no=$Record->Acc_No,
                                             pay_mode=$Record->pay_mode,
                                             tax_amt=$Record->tax_amt,
                                             disc_amt=$Record->discount,
                                             additions=$Record->additions,
                                             cust_ref='$Record->cust_ref',
                                             cust_name='$Record->cust_name',
                                             cust_area='$Record->add1',                                             
                                             sgst_amt=$Record->sgst_amt,
                                             cgst_amt=$Record->cgst_amt,                                                                                    
                                             counter_code='$Record->counter_code',                                                                                          
                                             sync_entry_no=0,                                            
											 Entry_no=$RecID
                                              WHERE pKey = $RecID and comp_id=".$CompID;

                    if ($conn->query($sql) === TRUE) {
                        $mess="OK";
                    } else {
                        $mess= "Error updating sales_return  record: " . $conn->error;
                         $upderrors.="Error updating sales_return record:<br>" . $conn->error . $sql;
                          $conn->close();
                         die($upderrors);
                    }
                }
                
            }
            $rownum+=1;
                $sql = "INSERT INTO sales_return_sub (Entry_No,compid,prod_code,batch,loc_code,quantity,rate,unit,taxrate,cost,sl_no,pack_qty,foc_qty,foc_unit,discount,sale_date,barcode,unitindex,cgst_rate,sgst_rate,cess_rate) ";  
                $sql.=" VALUES ($RecID,$CompID,'$Record->prod_code','$Record->batch','$Record->loc_code',$Record->quantity,$Record->rate,'$Record->unit',$Record->taxrate,$Record->cost,$Record->sl_no,$Record->pack_qty,$Record->foc_qty,'$Record->foc_unit',$Record->item_disc,'$Record->entry_date','$Record->barcode',$Record->unitindex,$Record->cgst_rate,$Record->sgst_rate,$Record->cess_rate)";
               if ($conn->query($sql) === TRUE)
               {                         
                  $mess="OK";
               } else
               {
                  $mess= "Error: " . $sql . "<br>" . $conn->error;
                  $upderrors.="Error: " . $sql . "<br>" . $conn->error;
               }   
    
        }
        if ($upderrors=="None"){
           echo  $RecID;
        }
        else {
             echo $upderrors;            
        }
    }
    else
    {
        echo "ERROR: No data!";
    }
    $conn->close();
?>