<?php
include 'conne.php';

$foo = file_get_contents("php://input");

$arr = json_decode($foo, true);
$values = $arr["products"];
$id = $arr["code"];
$storeno = $arr["number"];
$mma = $arr["selectedToppings"];


echo $test2;

$txt2 = $foo;

//Change storenumber here for each outlet
$storenumber = "021";

$txt = "".$id;
$inc = 0;

foreach($values as $item) {
//takes "itemname" column in json and then compare with text contain DIY COMBO 2 or 3
    $name = $item['name'];
    if (strpos($name, 'DIY Combo 2') !== false) {
        $preparedb4 = "insert into preparedstatement (store_id,billing_id,itemcode,qty,product_type,parent_code) values ('".$storenumber."','".$id."','8001','".$item['quantity']."',null,null)";
mysqli_query($conn,$preparedb4);
//takes the first item in the diy combo 2
     $prepared = "insert into preparedstatement (store_id,billing_id,itemcode,qty,product_type,parent_code) values ('".$storenumber."','".$id."','".$item['remoteCode']."','".$item['quantity']."',2,8001)";
mysqli_query($conn,$prepared);
        $mmass = $item['selectedToppings'];
        
       foreach($mmass as $mmas)
       {
           //for each items in the diy combo will be query here
           $prepared2 = "insert into preparedstatement (store_id,billing_id,itemcode,qty,product_type,parent_code) values ('".$storenumber."','".$id."','".$mmas['remoteCode']."','".$item['quantity']."','2','8001')";
$txt = $txt."\r\n".$prepared2;
mysqli_query($conn,$prepared2);
       }
}
// if item name is DIY 3
else if (strpos($name, 'DIY Combo 3') !== false) {
        $preparedb4 = "insert into preparedstatement (store_id,billing_id,itemcode,qty,product_type,parent_code) values ('".$storenumber."','".$id."','8002','".$item['quantity']."',null,null)";
mysqli_query($conn,$preparedb4);
// takes the first item in diy combo 3
     $prepared = "insert into preparedstatement (store_id,billing_id,itemcode,qty,product_type,parent_code) values ('".$storenumber."','".$id."','".$item['remoteCode']."','".$item['quantity']."',2,8002)";
mysqli_query($conn,$prepared);
        $mmass = $item['selectedToppings'];
        
       foreach($mmass as $mmas)
       {
      //each item in the diy combo to query here  
           $prepared2 = "insert into preparedstatement (store_id,billing_id,itemcode,qty,product_type,parent_code) values ('".$storenumber."','".$id."','".$mmas['remoteCode']."','".$item['quantity']."','2','8002')";
$txt = $txt."\r\n".$prepared2;
mysqli_query($conn,$prepared2);
       }
}
// any other item that is not diy combo
else{
    
 $prepared = "insert into preparedstatement (store_id,billing_id,itemcode,qty,product_type,parent_code) values ('".$storenumber."','".$id."','".$item['remoteCode']."','".$item['quantity']."',null,null)";


mysqli_query($conn,$prepared);
        $mmass = $item['selectedToppings'];
        
       foreach($mmass as $mmas)
       {
      // if there any add on / condiment in menu (right now is VLS)     
      if($mmas['remoteCode'] == 7784 || $mmas['remoteCode'] == 7785 || $mmas['remoteCode'] == 7786 || $mmas['remoteCode'] == 7787 || $mmas['remoteCode'] == 7789 || $mmas['remoteCode'] == 6875 || $mmas['remoteCode'] == 6874){
           $prepared2 = "insert into preparedstatement (store_id,billing_id,itemcode,qty,product_type,parent_code) values ('".$storenumber."','".$id."','".$mmas['remoteCode']."','".$item['quantity']."',null,null)";
$txt = $txt."\r\n".$prepared2;
mysqli_query($conn,$prepared2);
      }else{
      
           $prepared2 = "insert into preparedstatement (store_id,billing_id,itemcode,qty,product_type,parent_code) values ('".$storenumber."','".$id."','".$mmas['remoteCode']."','".$item['quantity']."','2','".$item['remoteCode']."')";
$txt = $txt."\r\n".$prepared2;
mysqli_query($conn,$prepared2);
}
       }


   
    }
}


 $inc++;

  
//query from online db to link the items, if there is a product link will query into the format for insert.php

$f1 = "";
$f2 = "";
$f3 = "";
$f4 = "";
$f5 = "";
$inc2 = 0;
$presql = "select itemcode,sum(qty) as ttl ,product_type , parent_code from preparedstatement where billing_id = '".$id."' group by itemcode,parent_code";
$tesql = mysqli_query($conn,$presql);


while ($ssql = mysqli_fetch_assoc($tesql)) {
   $rowsq = $ssql['itemcode'];
$rowsq2 = $ssql['ttl'];
$rowsq3 = $ssql['product_type'];
$rowsq4 = $ssql['parent_code'];

if ($inc2 == 0){
$f1 = $f1."".$ssql['itemcode'];
$f2 = $f2."".$ssql['ttl'];
$f3 = $f3."".$ssql['product_type'];
$f4 = $f4."".$ssql['parent_code'];
$f5 = $f5."1";

}
else{

    $f1 = $f1.",".$ssql['itemcode'];
$f2 = $f2.",".$ssql['ttl'];
$f3 = $f3.",".$ssql['product_type'];
$f4 = $f4.",".$ssql['parent_code'];
$f5 = $f5.",1";

}
$inc2++;
    }



$final = "http://localhost/AndroidAPI/insert.php?pi=".$f1."&pq=".$f2."&pax=0&pcd=".$f4."&type=".$f3."&ita=".$f5;

$final2 = "use sushiking;
insert into q_orders (STRING_REQUESTS) values ('".$final."');";



$ids = rand(1,1000);
$format = "kj".$id.".sql";
echo $format;
$ftp_server='ftp.sushi-king.com'; 
$ftp_user_name='fpftp@sushi-king.com'; 
$ftp_user_pass='Sushiking123';

// Create temporary file
$local_file=fopen('php://temp', 'w');
fwrite($local_file, $final2);
rewind($local_file);       

// FTP connection
$ftp_conn=ftp_connect($ftp_server); 

// FTP login
@$login_result=ftp_login($ftp_conn, $ftp_user_name, $ftp_user_pass); 
ftp_chdir($ftp_conn, $storenumber);

// FTP upload
if($login_result) $upload_result=ftp_fput($ftp_conn, $format, $local_file, FTP_ASCII);

// Error handling
if(!$login_result or !$upload_result)
{
    echo('<p>FTP error: The file could not be written to the FTP server.</p>');
}
if($id != ""){
    echo '{
    "202": "202 Accepted"
}';
}

// Close FTP connection
ftp_close($ftp_conn);

// Close file handle
fclose($local_file); 



?>