<?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;
        foreach ($xml as $Record){ 
            
            if($rownum==1){
                $CompID=$Record->CompID;
                $Entry_No= $Record->Entry_No;
                $Entry_Date= $Record->Entry_Date_Str;
                $Voucher_No = $Record->Voucher_No;
                $Amount = $Record->Amount;
                $Tax_Amt = $Record->Tax_Amt;
                $Counter_Code= $Record->Counter_Code;
                $Counter_Name = $Record->Counter_Name;
                $WebEntry_No = $Record->WebEntry_No;
                $Edit_Time=$Record->Edit_Time;
                $Ref_No=$Record->Ref_No;
                $Notes=$Record->Notes;
                $sql = "SELECT * FROM stock_return WHERE entry_no =".$Entry_No." and comp_id=" .$CompID;
                $result = $conn->query($sql);
                if ($result->num_rows == 0 ) {
                       $sql = "INSERT INTO stock_return (entry_no,comp_id) VALUES (".$Entry_No.",".$CompID.")";
                       if ($conn->query($sql) === TRUE)
                       {
                          $WebEntry_No = $conn->insert_id;
                          $mess="OK";
                       } else
                       {
                          $mess= "Error: " . $sql . "<br>" . $conn->error;
                          $upderrors.="Error: " . $sql . "<br>" . $conn->error;
                       }       
                }
                else {
                    $sql = "delete FROM stock_return_sub WHERE entry_no =".$Entry_No." and compid=" .$CompID;
                    $conn->query($sql);
                     if($srow = $result->fetch_assoc())
                        {
                        $WebEntry_No=$srow["WebEntry_No"]; }
                }
                if ($mess=="OK")
                {
                    $sql = "UPDATE stock_return SET Entry_Date='".$Entry_Date."'," 
                                                ."Voucher_No='".$Voucher_No."',"                                               
                                                ."Amount=".$Amount.","
                                                ."sync_time=now(),"
                                                ."Ref_No='".$Ref_No."',"
                                                ."Notes='".$Notes."',"
                                                ."exported=0,"
                                                ."pc_edited=0,"
                                                ."WebEntry_No=".$WebEntry_No.","
                                                ."Tax_Amt=".$Tax_Amt.","
                                                ."edit_time=".$Edit_Time.","                                                
                                                ."Counter_Code='".$Counter_Code."',"
                                                ."Counter_Name='".$Counter_Name."' WHERE Entry_No = ".$Entry_No." and comp_id=".$CompID;


                    if ($conn->query($sql) === TRUE) {
                        $mess="OK";
                    } else {
                        $mess= "Error updating stock return  record: " . $conn->error;
                         $upderrors.="Error updating stock record:<br>" . $conn->error;
                    }
                }
                
            }
            $rownum+=1;
            $Prod_Code = $Record->Prod_Code;
            $Quantity = $Record->Quantity;
            $Unit = $Record->Unit;
            $Tax_Rate = $Record->Tax_Rate;
            $Rate = $Record->Rate;
            $Sl_No = $Record->Sl_No;
            $FOC_Qty=$Record->FOC_Qty ;
            $sql = "INSERT INTO stock_return_sub (entry_no,compid,prod_code,quantity,unit,tax_rate,Rate,sl_no,Foc_qty,pack_qty) VALUES ($Entry_No,$CompID,'$Prod_Code',$Quantity,'$Unit',$Tax_Rate,$Rate,$Sl_No,$FOC_Qty,1)";
                       if ($conn->query($sql) === TRUE)
                       {                         
                          $mess="OK";
                       } else
                       {
                          $mess= "Error: " . $sql . "<br>" . $conn->error;
                          $upderrors.="Error: " . $sql . "<br>" . $conn->error;
                       }   
            
        }
        if ($upderrors=="None"){
           echo  $WebEntry_No;
        }
        else {
             echo $upderrors;            
        }
            
            
    }
    else
    {
        echo "ERROR: No data!";
    }
    $conn->close();
?>