<?php
require_once ("varables.php");

$myJSON = 0;
if(isset($_POST["nekot"]) && isset($_POST["json"])&& isset($_POST["json1"]))
{

	$postdata = $_POST["json"];
    $products = json_decode($postdata,true);
    $postdata1 = $_POST["json1"];
	$products1 = json_decode($postdata1,true);

       $products[0]["Creation_Date"] = str_replace("ق.ظ","a.m",  $products[0]["Creation_Date"]);
       $products[0]["Creation_Date"] = str_replace("ب.ظ","p.m",  $products[0]["Creation_Date"]);
     $products[0]["Creation_Date"] = date_create($products[0]["Creation_Date"]);
    $products[0]["Creation_Date"] = date_format($products[0]["Creation_Date"],"Y/m/d H:i:s");
	$firsdate =  $products[0]["Creation_Date"]; 
	$enddate =  $products[0]["Creation_Date"]; 
	for($i=0;$i<sizeof($products);$i++){
	    
	      $products[$i]["Creation_Date"] = str_replace("ق.ظ","a.m",  $products[$i]["Creation_Date"]);
          $products[$i]["Creation_Date"] = str_replace("ب.ظ","p.m",  $products[$i]["Creation_Date"]);
          $products[$i]["Creation_Date"] = date_create($products[$i]["Creation_Date"]);
          $products[$i]["Creation_Date"] = date_format($products[$i]["Creation_Date"],"Y/m/d H:i:s");
    
	    if($products[$i]["Creation_Date"]<$firsdate)
    	  $firsdate = $products[$i]["Creation_Date"];
    	  
	    if($products[$i]["Creation_Date"]>$enddate)
    	  $enddate = $products[$i]["Creation_Date"];

	 }
	 
	
    
	$toke = $_POST["nekot"];
if($toke == "QWEqweRTYrty"){
    $connect = @mysqli_connect($hostname,$username,$password,$database);
    if($connect)
    {
        @mysqli_query($connect,"SET CHARACTER SET utf8;");
        
        // echo sizeof($products);
        //  $qqqqq = "SELECT * FROM ".$tbl_factor." WHERE Creation_Date BETWEEN '".$firsdate."' AND '".$enddate."'";
        //  echo $qqqqq;
        // $RRRRR =  @mysqli_query($connect,$qqqqq);
        //   if(!$RRRRR)
        //     echo "\n". @mysqli_error($connect)."\n";
        //   else{
        //       $c = 0;
        //       while($row = @mysqli_fetch_assoc($RRRRR))
        //          $c++;
        //       echo "\ncount1 =". $c;
        //   }   
          
     
     
        $qdelete = "DELETE FROM ".$tbl_factor." WHERE Creation_Date BETWEEN '".$firsdate."' AND '".$enddate."'";
        $resultt =  @mysqli_query($connect,$qdelete);
          if(!$resultt)
          echo "\n". @mysqli_error($connect)."\n";
     
    //   $qqqqq = "SELECT * FROM ".$tbl_factor." WHERE Creation_Date BETWEEN '".$firsdate."' AND '".$enddate."'";
    //     $RRRRR =  @mysqli_query($connect,$qqqqq);
    //       if(!$RRRRR)
    //         echo "\n". @mysqli_error($connect)."\n";
    //       else{
    //           $c = 0;
    //           while($row = @mysqli_fetch_assoc($RRRRR))
    //             $c++;
    //           echo "\ncount2 =". $c;
    //       }   
        
     
        for($i=0;$i<sizeof($products);$i++){
                $select_query = "DELETE FROM ".$tbl_factor." WHERE Fac_Code ='".$products[$i]["Fac_Code"]."' && Fac_Type ='".$products[$i]["Fac_Type"]."' && Fac_Code_C ='".$products[$i]["Fac_Code_C"]."'";
                $res = @mysqli_query($connect,$select_query);
                if(!$res){
                        $myJSON = 401 . @mysqli_error($connect);
                                echo json_encode($myJSON);
                        return;
                }
                else {
                 $products[$i]["Creation_Date"] =  str_replace("ق.ظ","A.M", $products[$i]["Creation_Date"]); 
                        $insert_query= "INSERT INTO ".$tbl_factor." (`Fac_Code`, `Fac_Type`, `Fac_Code_C`, `C_Code`, `Fac_Date`, `Fac_Time`, `Sum_Price`, `Sum_Few`, `Fac_Comment`, `PageFooter`,
                                `FNaghd`, `FCheck`, `FNesieh`, `TasviehDate`, `Takhfif`, `UserCode`, `Sanad_Code`, `HlpFieldL`, `ShowPrice`, `DAmozesh`, `DShahrdary`, `DCEghtesad`, `DMaliat`, `HazFactK`,
                                `Num_Havaleh`, `RoundFact`, `NaghdCheck`, `DateUser`, `TimeUser`, `CorrectDateTasvieh`, `DarsadDirkardTasvieh`, `HazSarfasl`, `Card`, `Sum_Levy`, `Sum_Scot`, `Fac_State`,
                                `IsLevyAndScot`, `IsCMS`, `OldDAmoozesh`, `OldDShahrdary`, `Kind_Vat`, `Creation_Date`, `Naghd_Moddat`, `Res_ServicePrice`, `Res_HazPrice`, `Tala`, `Dollar`, `FCode_ChangeGold`,
                                `Selection`, `ResFac_Code_C`, `Choice`, `UserSell`, `SayerAvarez`, `Fac_Time_Res`, `Res_Alias_Name`, `UseSarsheKan`, `HazHamlFact`, `SumTakhfif_P`, `Takhfif_P_KolFact`, `FBon`,
                                `Bon_No`, `StateFactForKartabl`, `AcceptOrRejectSuperVisor`, `Vaset_delayvalue`, `Vasete`, `Porsant`)
                                VALUES ('".$products[$i]["Fac_Code"]."','".$products[$i]["Fac_Type"]."','".$products[$i]["Fac_Code_C"]."','".$products[$i]["C_Code"]."',"
                                . "     '".$products[$i]["Fac_Date"]."','".$products[$i]["Fac_Time"]."','".$products[$i]["Sum_Price"]."','".$products[$i]["Sum_Few"]."',"
                                . "     '".$products[$i]["Fac_Comment"]."','".$products[$i]["PageFooter"]."','".$products[$i]["FNaghd"]."','".$products[$i]["FCheck"]."',"
                                . "     '".$products[$i]["FNesieh"]."','".$products[$i]["TasviehDate"]."','".$products[$i]["Takhfif"]."','".$products[$i]["UserCode"]."',"
                                . "     '".$products[$i]["Sanad_Code"]."','".$products[$i]["HlpFieldL"]."','".$products[$i]["ShowPrice"]."','".$products[$i]["DAmozesh"]."',"
                                . "     '".$products[$i]["DShahrdary"]."','".$products[$i]["DCEghtesad"]."','".$products[$i]["DMaliat"]."','".$products[$i]["HazFactK"]."',"
                                . "     '".$products[$i]["Num_Havaleh"]."','".$products[$i]["RoundFact"]."','".$products[$i]["NaghdCheck"]."','".$products[$i]["DateUser"]."',"
                                . "     '".$products[$i]["TimeUser"]."','".$products[$i]["CorrectDateTasvieh"]."','".$products[$i]["DarsadDirkardTasvieh"]."','".$products[$i]["HazSarfasl"]."',"
                                . "     '".$products[$i]["Card"]."','".$products[$i]["Sum_Levy"]."','".$products[$i]["Sum_Scot"]."','".$products[$i]["Fac_State"]."',"
                                . "     '".$products[$i]["IsLevyAndScot"]."','".$products[$i]["IsCMS"]."','".$products[$i]["OldDAmoozesh"]."','".$products[$i]["OldDShahrdary"]."',"
                                . "     '".$products[$i]["Kind_Vat"]."','".$products[$i]["Creation_Date"]."','".$products[$i]["Naghd_Moddat"]."','".$products[$i]["Res_ServicePrice"]."',"
                                . "     '".$products[$i]["Res_HazPrice"]."','".$products[$i]["Tala"]."','".$products[$i]["Dollar"]."','".$products[$i]["FCode_ChangeGold"]."',"
                                . "     '".$products[$i]["Selection"]."','".$products[$i]["ResFac_Code_C"]."','".$products[$i]["Choice"]."','".$products[$i]["UserSell"]."',"
                                . "     '".$products[$i]["SayerAvarez"]."','".$products[$i]["Fac_Time_Res"]."','".$products[$i]["Res_Alias_Name"]."','".$products[$i]["UseSarsheKan"]."',"
                                . "     '".$products[$i]["HazHamlFact"]."','".$products[$i]["SumTakhfif_P"]."','".$products[$i]["Takhfif_P_KolFact"]."','".$products[$i]["FBon"]."',"
                                . "     '".$products[$i]["Bon_No"]."','".$products[$i]["StateFactForKartabl"]."','".$products[$i]["AcceptOrRejectSuperVisor"]."','".$products[$i]["Vaset_delayvalue"]."',"
                                . "     '".$products[$i]["VASETE"]."','".$products[$i]["PORSANT"]."')";
                        $insres =@mysqli_query($connect,$insert_query);
                        if(!$insres){
                        $myJSON = @mysqli_error($connect);
                                echo $myJSON;
                        return;
                        }
                        
            }
        }
        for($i=0;$i<sizeof($products1);$i++){
                $select_query = "SELECT Fac_Code,Fac_Type,A_Index FROM ".$tbl_factor_art." WHERE Fac_Code ='".$products1[$i]["Fac_Code"]."' && Fac_Type ='".$products1[$i]["Fac_Type"]."' && A_Index ='".$products1[$i]["A_Index"]."'";
                $res =@mysqli_query($connect,$select_query);
                if(!$res){
                        $myJSON = 401 . @mysqli_error($connect);
                                echo json_encode($myJSON);
                        return;
                }
                else {
                        
                        $faccod="0";
                        while($row = @mysqli_fetch_assoc($res)){
                                $faccod= $row["Fac_Code"];
                        } 
                        if( $faccod == "0"){
                                
                                $insert_query= "INSERT INTO ".$tbl_factor_art." (`Fac_Code`, `Fac_Type`, `A_Code`, `A_Index`, `Few_Article`,"
                                        . " `Few_Article2`, `Price_BS`, `FacArtic_Comment`, `Buy_Price`, `Money_Code`,"
                                        . " `Price_Dollari`, `DarsadTakhfif`, `HlpFieldS`, `CompNumber`, `VahedCode`, `VahedFew`, `ArtExist`, `DarsadPorsant`, `DarsadHaz`, `ArtClassSIndex`, `Few2_Or_Few`, `Price_BS2`, `FewKarton`, `FewBasteh`, `TakhfifSatriR`, `Tool_Arz`, `Tool`, `Arz`, `Tedad`, `Metr`, `ToolArzType`, `Darsad_Type`, `Levy`, `Scot`, `Takhfif_kharid`, `IsCMS`, `ACode_C`, `GDarsadSoodF`, `Ojrat_Price`, `M_Ojrat`, `J_Ojrat`, `HazineHamlPrice`, `PricePay`, `HazTypePay`, `HazArtSelect`, `Vaseteh_Code`, `Cust_Porsant`, `Sel_Price`, `Sakht_Ojrat`, `Abkari_Ojrat`, `SumTakhfifSatri_P`, `CheckTakhfifSood`, `SumPorsantSatri_P`, `SumPorsantKoli_P`, `EndBuy_Price`, `Ghotr`)"
                                        . " VALUES ('".$products1[$i]["Fac_Code"]."', '".$products1[$i]["Fac_Type"]."', '".$products1[$i]["A_Code"]."', '".$products1[$i]["A_Index"]."', '".$products1[$i]["Few_Article"]."',"
                                        . " '".$products1[$i]["Few_Article2"]."', '".$products1[$i]["Price_BS"]."', '".$products1[$i]["FacArtic_Comment"]."', '".$products1[$i]["Buy_Price"]."', '".$products1[$i]["Money_Code"]."', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '', '', '', '', '', '', '', '', NULL, '', NULL, '', '', '', NULL, '', '', '', NULL)";
                                        $insres =@mysqli_query($connect,$insert_query);
                                if(!$insres){
                                        $myJSON = @mysqli_error($connect);
                                        echo $myJSON;
                                        return;
                                }
                        }
                        else{
                                $insert_query= "UPDATE ".$tbl_factor_art." SET Fac_Code = '".$products1[$i]["Fac_Code"]."', Fac_Type= '".$products1[$i]["Fac_Type"]."',A_Code= '".$products1[$i]["A_Code"]."',A_Index= '".$products1[$i]["A_Index"]."',Few_Article= '".$products1[$i]["Few_Article"]."',Few_Article2 ='".$products1[$i]["Few_Article2"]."',Price_BS= '".$products1[$i]["Price_BS"]."',FacArtic_Comment= '".$products1[$i]["FacArtic_Comment"]."',Buy_Price= '".$products1[$i]["Buy_Price"]."'
                                ,Money_Code ='".$products1[$i]["Money_Code"]."',Price_Dollari ='".$products1[$i]["Price_Dollari"]."',DarsadTakhfif ='".$products1[$i]["DarsadTakhfif"]."',HlpFieldS ='".$products1[$i]["HlpFieldS"]."',CompNumber= '".$products1[$i]["CompNumber"]."',VahedCode= '".$products1[$i]["VahedCode"]."',VahedFew ='".$products1[$i]["VahedFew"]."',ArtExist= '".$products1[$i]["ArtExist"]."',DarsadPorsant= '".$products1[$i]["DarsadPorsant"]."',DarsadHaz= '".$products1[$i]["DarsadHaz"]."'
                                ,ArtClassSIndex ='".$products1[$i]["ArtClassSIndex"]."',Few2_Or_Few= '".$products1[$i]["Few2_Or_Few"]."',Price_BS2 ='".$products1[$i]["Price_BS2"]."',FewKarton= '".$products1[$i]["FewKarton"]."',FewBasteh ='".$products1[$i]["FewBasteh"]."',TakhfifSatriR ='".$products1[$i]["TakhfifSatriR"]."',Tool_Arz ='".$products1[$i]["Tool_Arz"]."',Tool ='".$products1[$i]["Tool"]."',Arz ='".$products1[$i]["Arz"]."',Tedad= '".$products1[$i]["Tedad"]."',Metr ='".$products1[$i]["Metr"]."'
                                ,ToolArzType= '".$products1[$i]["ToolArzType"]."',Darsad_Type= '".$products1[$i]["Darsad_Type"]."',Levy ='".$products1[$i]["Levy"]."',Scot= '".$products1[$i]["Scot"]."',Takhfif_kharid ='".$products1[$i]["Takhfif_kharid"]."',IsCMS= '".$products1[$i]["IsCMS"]."',ACode_C= '".$products1[$i]["ACode_C"]."',GDarsadSoodF ='".$products1[$i]["GDarsadSoodF"]."',Ojrat_Price ='".$products1[$i]["Ojrat_Price"]."',M_Ojrat ='".$products1[$i]["M_Ojrat"]."',J_Ojrat= '".$products1[$i]["J_Ojrat"]."'
                                ,HazineHamlPrice= '".$products1[$i]["HazineHamlPrice"]."',PricePay ='".$products1[$i]["PricePay"]."',HazTypePay ='".$products1[$i]["HazTypePay"]."',HazArtSelect= '".$products1[$i]["HazArtSelect"]."',Vaseteh_Code ='".$products1[$i]["Vaseteh_Code"]."',Cust_Porsant ='".$products1[$i]["Cust_Porsant"]."',Sel_Price ='".$products1[$i]["Sel_Price"]."',Sakht_Ojrat ='".$products1[$i]["Sakht_Ojrat"]."',Abkari_Ojrat ='".$products1[$i]["Abkari_Ojrat"]."',SumTakhfifSatri_P = '".$products1[$i]["SumTakhfifSatri_P"]."'
                                ,CheckTakhfifSood= '".$products1[$i]["CheckTakhfifSood"]."',SumPorsantSatri_P= '".$products1[$i]["SumPorsantSatri_P"]."',SumPorsantKoli_P ='".$products1[$i]["SumPorsantKoli_P"]."',EndBuy_Price ='".$products1[$i]["EndBuy_Price"]."',Ghotr= '".$products1[$i]["Ghotr"]."' WHERE Fac_Code ='".$products1[$i]["Fac_Code"]."' && Fac_Type ='".$products1[$i]["Fac_Type"]."' && A_Index ='".$products1[$i]["A_Index"]."'";
                                $insres =@mysqli_query($connect,$insert_query);
                        if(!$insres){
                                $myJSON = @mysqli_error($connect);
                                echo $myJSON;
                                return;
                        }
                                
                }
                }
        }
                        $myJSON = 200;
		}
	}
        
	 else
		 $myJSON =405;
}
    echo $myJSON;

?>