<?php
error_reporting(E_ALL);




define("servername" ,"localhost");
define("username" , "upstreamlogistic_pos_spotwash");
define("password" , ',y@9-d$twcIi');
define("dbname" , "upstreamlogistic_pos_spotwash");




// Create connection
$conn = new mysqli(servername, username, password, dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " );
} 
//include_once("security.php");

 $sqlUpdate = "";
 
//$sql = "SELECT `laundary_job`.`job_code` FROM `laundary_job`,`laundary_busket` WHERE `laundary_job`.`job_code` != `laundary_busket`.`job_code` AND `laundary_busket`.`laundary_status` != 1 GROUP BY `laundary_job`.`job_code` ORDER BY `laundary_job`.`job_id` DESC LIMIT 5 ";
$sql = "SELECT `job_code` FROM `laundary_job` WHERE `job_code` NOT IN (SELECT `job_code` FROM `laundary_busket` ORDER BY `laundary_busket`.`busket_sn` DESC ) LIMIT 5";

$result = $conn->query($sql);


$count = 0;
$invoiceList = "";
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
    	//$output[]=$row;
        //$array[] =$row;
        $jobCode = $row['job_code'];
        $uniq = uniqid()."_$count";
        
        $invoiceList .=  ", ".$jobCode;
        
        if($count == 0){
                $sqlUpdate .= " (NULL, '$uniq', '$jobCode', 'N?A', '910', 'N?A', 'No Item', 'wash_iron', 'request_type_code', '1', '0', '0', '0', '0', 'N/A', '0', '0', 'Order_Placed', 'N/A', 'N/A', 'N/A', '0', 'N/A', 'No Items Added', '0', '0', '0', CURRENT_TIME, CURRENT_DATE, '0', '0', '0', '0', 'N/A', CURRENT_TIMESTAMP)";
        }else{
               $sqlUpdate .= ", (NULL, '$uniq', '$jobCode', 'N?A', '910', 'N?A', 'No Item', 'wash_iron', 'request_type_code', '1', '0', '0', '0', '0', 'N/A', '0', '0', 'Order_Placed', 'N/A', 'N/A', 'N/A', '0', 'N/A', 'No Items Added', '0', '0', '0', CURRENT_TIME, CURRENT_DATE, '0', '0', '0', '0', 'N/A', CURRENT_TIMESTAMP)";


        }
   
   
   $count++;
    }
    
    
             $sqlBasket = "INSERT INTO `laundary_busket` (`busket_sn`, `basket_code`, `job_code`, `mp_code`, `laundary_id`, `laundary_code`, `laundary_name`, `service_code`, `request_type_code`, `laundary_ismanual`, `laundary_qty`, `laundary_pieces`, `laundary_price`, `basket_discount`, `laundary_upcharge_name`, `basket_upcharge`, `basket_no_charge`, `basket_tracker_progress`, `rack_name`, `laundary_colour`, `laundary_label`, `basket_has_spotdamage`, `basket_desc_spotdamage`, `laundary_note`, `laundary_isreceived`, `laundary_isdispatched`, `basket_ispicked`, `basket_pick_time`, `basket_pick_date`, `basket_ispaid`, `basket_istaxed`, `laundary_status`, `laundary_user`, `ref_invoice_nocharge`, `laundary_timestamp`) VALUES ".$sqlUpdate;
            $resultBasket = $conn->query($sqlBasket);
            
            if( $resultBasket){
                echo "Updated $count invoice(s) <br /> ".$invoiceList;
                
            }else{
                echo "Failed to update";
            }

} else {
    echo "0 results";
}


$conn->close();

?>