<?php
    include_once 'opendb.php';
    $mess="OK";
    $upderrors="None";
    $content="";
    if (isset($_POST["data"]))
    {
        $content=$_POST["data"];
        $content = iconv('UTF-8', 'UTF-8//IGNORE', $content);
        
    } else {
        $xmlData = file_get_contents('php://input');
        if (!empty($xmlData)) {
            //$xml = simplexml_load_string($xmlData);
            $content = iconv('UTF-8', 'UTF-8//IGNORE', $xmlData);
        }
    }
    if (empty($content)){
        die("ERROR: No data!"); 
    }
    include_once 'opendb.php';
    $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 ="";  // $xml->Products[0]->UNIT2;
            $unit3 =""; // $xml->Products[0]->UNIT3;
            $unit2_qty =1 ; //$xml->Products[0]->UNIT2_QTY;
            $unit3_qty =1; //$xml->Products[0]->UNIT3_QTY;
            $edit_time=$Record->edit_time;
            $price = $Record->price;
            $price2 = $Record->price2;
            $inactive = $Record->inactive;
            $brand = $Record->brand;
            $category = $Record->category;
            $outof_stock=$Record->outof_stock;
            $CompID=$Record->CompID;    
            $disp_order=$Record->disp_order;
            $packing=$Record->packing;
            if(!isset($disp_order)){$disp_order=0;}
            if ($price2=='')
            {      
                $price2=0;
            }
             $price3 = $Record->price3;
             if ($price3=='')
            {      
                $price3=0;
            }
             $cost=0 ;
             try {
                 $cost=$Record->cost;
             } catch (Exception $ex) {  

             }
             if ($cost=='') $cost="0";
            $offer_price=isset($Record->offer_price) ? $Record->offer_price: 0 ;
            $offer_date=isset($Record->offer_dateStr) ?  $Record->offer_dateStr: 20200101;
            $scheme_text=isset($Record->scheme_text) ?  $Record->scheme_text : " ";
            $scheme_date=isset($Record->scheme_dateStr) ? $Record->scheme_dateStr : 20200101 ;
            $mrp=isset($Record->mrp) ? $Record->mrp: 0 ;
            $tax_rate=isset($Record->tax_rate) ? $Record->tax_rate : 0 ;
            $sql = "SELECT * FROM products WHERE prod_code like '".$pcode."' and comp_id=" .$CompID;
            $pname_local=isset($Record->prod_name_local) ? $Record->prod_name_local: '' ;
            $hsncode=isset($Record->hsn_code) ?  $Record->hsn_code : " ";
            $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;
                   } 
                  
                  
            }
            
            if ($mess=="OK")
            {
                $sql = "UPDATE products SET prod_name='".$pname."',"
                                            ."barcode='".$barcode."',"
                                            ."quantity=".$sstock.","
                                            ."inactive=".$inactive.","
                                            ."outof_stock=".$outof_stock.","
					                        ."price=".$price.","
                                            ."price2=".$price2.","
                                            ."price3=".$price3.","
                                            ."tax_rate=".$tax_rate.","
                                            ."mrp=".$mrp.","
                                            ."cost=".$cost.","
                                            ."UNIT1='".$unit."',"
                                            ."packing='".$packing."',"
                                            ."edit_time=".$edit_time.","
                                            ."disp_order=".$disp_order.","  
                                            ."add_time='".$Record->ADD_TIME_STR."',"
                                            ."category='".$category."',"
                                             ."offer_price=".$offer_price.","
                                            ."offer_date=".$offer_date.","
                                            ."scheme_text='".$scheme_text."',"
                                            ."scheme_date=".$scheme_date.","
                                            ."prod_name_local='".$pname_local."',"
                                            ."hsn_code='".$hsncode."',"
                                            ."brand='".$brand."' 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;
                }
                    
            }
        }
        if ($upderrors=="None"){
           echo $mess;
        }
        else {
             echo $upderrors;            
        }
   
    $conn->close();
?>