<?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");
        foreach ($xml as $Record){
            $pname= $Record->prod_name;
            $pname_local= $Record->prod_name_local;
            $pcode = $Record->prod_code;
            $barcode = $Record->barcode;
            $sstock = $Record->quantity;
            $unit = $Record->UNIT1;
            $unit2 = $Record->UNIT2;
            $unit3 = $Record->UNIT3;
            $unit2_qty = $Record->UNIT2_QTY;
            $unit3_qty = $Record->UNIT3_QTY;
            $ptax = $Record->tax_rate;
            $CGST_Rate = $Record->CGST_RATE;
            $SGST_Rate = $Record->SGST_RATE;
            $sale_unit_index=$Record->Def_Sale_unit;
            $server_edit_time=$Record->SERVER_EDIT_TIME_STR;
            
            $batch = $Record->Batch;
            $scost = $Record->Cost;
            $sprate = $Record->PurchRate;
            $PRICEU1 = $Record->PriceU1;
            $PRICEU2 = $Record->PriceU2;
            $PRICEU3 = $Record->PriceU3;            
            $CompID=$Record->CompID;
            
           // if(!isset($disp_order)){$disp_order=0;}
            
            
            
            $sql = "SELECT * FROM products WHERE prod_code like '".$pcode."' and comp_id=" .$CompID;
            $result = $conn->query($sql);
            if ($result->num_rows == 0 ) {
                   $sql = "INSERT INTO products (prod_code,comp_id) VALUES ('".$pcode."',".$CompID.")";
                   if ($conn->query($sql) === TRUE)
                   {
                      $mess="OK";
                   } else
                   {
                      $mess= "Error: " . $sql . "<br>" . $conn->error;
                      $upderrors.="Error: " . $sql . "<br>" . $conn->error;
                   } 
                   
                   $sql = "INSERT INTO batchs (prod_code,batch,comp_id) VALUES ('".$pcode."','".$batch."',".$CompID.")";
                   if ($conn->query($sql) === TRUE)
                   {
                      $mess="OK";
                   } else
                   {
                      $mess= "Error: " . $sql . "<br>" . $conn->error;
                      $upderrors.="Error: " . $sql . "<br>" . $conn->error;
                   } 
            }
            
            if ($mess=="OK")
            {
                $sql = "UPDATE products SET prod_name='".$pname."'," 
                                            ."prod_name_local='".$pname_local."',"
                                            ."barcode='".$barcode."',"
                                            ."quantity=".$sstock.","
                                            ."UNIT1='".$unit."',"
                                            ."UNIT2='".$unit2."',"
                                            ."UNIT3='".$unit3."',"
                                            ."UNIT2_QTY=".$unit2_qty.","
                                            ."UNIT3_QTY=".$unit3_qty.","
                                            ."tax_rate=".$ptax.","
                                            ."CGST_Rate=".$CGST_Rate.","
                                            ."SGST_Rate=".$SGST_Rate.","
                                            ."disp_order=".$disp_order.","    
                                            ."SERVER_EDIT_TIME='".$server_edit_time."',"
                                            ."DEF_SALE_UNIT=".$sale_unit_index." WHERE prod_code like '".$pcode."' and comp_id=".$CompID;

               
                if ($conn->query($sql) === TRUE) {
                    $mess="OK";
                } else {
                    $mess= "Error updating product record: " . $conn->error;
                     $upderrors.="Error updating product record:<br>" . $conn->error;
                }
         
                $sql = "UPDATE batchs SET cost=".$scost."," 
                                            ."purch_rate=".$sprate.","
                                            ."priceU1=".$PRICEU1.","
                                            ."priceU2=".$PRICEU2.","
                                            ."priceU3=".$PRICEU3." WHERE prod_code like '".$pcode."' and batch like '".$batch."' and comp_id=".$CompID;

                if ($conn->query($sql) === TRUE) {
                    $mess="OK";
                } else {
                    $mess= "Error updating batch record: " . $conn->error;
                    $upderrors.= "Error updating batch record: " . $conn->error;
                }
                    
            }
        }
        if ($upderrors=="None"){
           echo $mess;
        }
        else {
             echo $upderrors;            
        }
            
            
    }
    else
    {
        echo "ERROR: No data!";
    }
    $conn->close();
?>