<?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;
        $cust_reg_no='';
        $sub_code='';
        $pkey=0;
        $CompID=0;
        $cust_name='';
        $counter_code="";
        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 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 (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_sub WHERE Entry_No =$RecID and compid=$CompID";
                        $conn->query($sql);
                     } else{
                          $mess= "Error: fetching row ";
                          $upderrors.="Error:  fetching row " . $sql ;
                     }
                }
                
                $cust_reg_no=$Record->cust_reg_no;
                $sub_code=$Record->sub_code;
                $cust_name=$Record->cust_name;
                $counter_code=$Record->counter_code;
                if ($mess=="OK")
                {
                    $sql = "UPDATE sales 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',
                                             CUST_PHONE='$Record->phone',
                                             sgst_amt=$Record->sgst_amt,
                                             cgst_amt=$Record->cgst_amt,
                                             cess_amt=$Record->cess_amt,
                                             cust_reg_no='$Record->cust_reg_no',
                                             counter_code='$Record->counter_code',                                                                                          
                                             sync_entry_no=0,
                                             payacc_no1=$Record->payaccno1,
                                             pay_amount1=$Record->payamount1,
											 Entry_no=$RecID
                                              WHERE pKey = $RecID and comp_id=".$CompID;

                    if ($conn->query($sql) === TRUE) {
                        $mess="OK";
                    } else {
                        $mess= "Error updating sales  record: " . $conn->error;
                         $upderrors.="Error updating sales record:<br>" . $conn->error . $sql;
                          $conn->close();
                         die($upderrors);
                    }
                }
                
            }
            $rownum+=1;
                $sql = "INSERT INTO sales_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,serialno,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->serialno',$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"){
            if (strlen($sub_code) > 1 &&  substr($sub_code, 0, 1) === 'W' ) {
                $pkey=substr($sub_code, 1);
                $EDIT_TIME=date("ymdHi");
                $sql = "UPDATE subsidiary SET SUB_NAME='$cust_name'," 
                                    ."ADDRESS=''," 
                                    ."EMAIL=''," 
                                    ."PLACE=''," 
                                    ."PHONE=''," 
                                    ."MOBILE=''," 
                                    ."FAX='',"
                                    ."CONT_NAME=''," 
                                    ."CONT_PHONE='',"
                                    ."AREA_CODE=''," 
                                    ."Area_Name=''," 
                                    ."SMAN_CODE='$counter_code'," 
                                    ."SUB_TYPE='C'," 
                                    ."Inactive=0,"                                   
                                    ."Reg_No1='$cust_reg_no'," 
                                    ."Sub_code ='$sub_code'," 
                                    ."price_level=1," 
                                    ."comp_id=$CompID," 
                                    ."App_Pass='12345'," 
                                    ."EDIT_TIME=$EDIT_TIME where pkey=$pkey and comp_id=0 ";
                                    
               $conn->query($sql);
            } 
           echo  $RecID;
        }
        else {
             echo $upderrors;            
        }
    }
    else
    {
        echo "ERROR: No data!";
    }
    $conn->close();
?>