<?php

function cs_get_batch_item_issue_with_daily_item_issues() {
    
    $sql_sm = "SELECT ref_line_id  FROM ".TB_PREF."stock_moves WHERE type = ". db_escape(ST_DAILY_ITEM_ISSUE);
    $sql_nsm = "SELECT ref_line_id  FROM ".TB_PREF."non_stock_moves WHERE type = ". db_escape(ST_DAILY_ITEM_ISSUE);
    
    $sql_exisiting = $sql_sm." UNION ".$sql_nsm;
            
    $sql = "SELECT dii.project_id, diid.* 
            FROM ".TB_PREF."daily_item_issue_details diid 
                JOIN ".TB_PREF."batch_item_issue_transactions biit  
                    ON diid.trans_id = biit.target_line_id  
                        AND diid.trans_no = biit.target_type_no   
                        AND diid.type = biit.target_type_id    
                JOIN ".TB_PREF."daily_item_issues dii   
                    ON diid.trans_no = dii.trans_no   
                        AND diid.type = dii.type   
                        AND diid.type = biit.target_type_id    
            WHERE dii.type = ".db_escape(ST_DAILY_ITEM_ISSUE)." 
                AND diid.trans_id NOT IN (".$sql_exisiting.") 
            ORDER BY diid.trans_id 
            LIMIT 50";
                
    return db_query($sql, "cs_get_batch_item_issue_with_daily_item_issues");
}

function cs_get_stock_move_record_for_batch_item_issue_to_stock_non_stock_moves($trans_no, $type, $line_id, $st_itm=null) {
    
    if ($st_itm == NSIT_STOCK_ITEM) {
        $sql = "SELECT trans_id, trans_no 
                FROM ".TB_PREF."stock_moves  
                WHERE trans_no = ".db_escape($trans_no)." 
                    AND type = ".db_escape($type)." 
                    AND ref_line_id = ".db_escape($line_id);
    } else {
        $sql = "SELECT trans_id, trans_no 
                FROM ".TB_PREF."non_stock_moves   
                WHERE trans_no = ".db_escape($trans_no)." 
                    AND type = ".db_escape($type)." 
                    AND ref_line_id = ".db_escape($line_id);
    }

	$result = db_query($sql, "cs_get_stock_move_record_for_batch_item_issue_to_stock_non_stock_moves");
	return db_fetch($result);
}

function cs_update_dii_line_sm_line_id($dii_line_id, $sm_line_id) {
    
        $sql = "UPDATE ".TB_PREF."daily_item_issue_details 
                SET source_line_id = ".db_escape($sm_line_id)." 
                WHERE trans_id = ".db_escape($dii_line_id)." 
                    AND source_line_id = 0 ";    

	db_query($sql, "cs_update_dii_line_sm_line_id");
        
        $n = db_num_affected_rows();
        if($n == 1){
            return array("success"=>true,"message"=>"");
        }else{
            return array("success"=>false,"message"=>"Could not update DII detail line ".$dii_line_id);
        }
	
}
        
function cs_custom_script_create_batch_item_issue_to_stock_moves() {
    
    $message = "==================Script Started==========================";
    write_custom_script_log("cs_custom_script_create_batch_item_issue_to_stock_moves", $message);
    
    $result = cs_get_batch_item_issue_with_daily_item_issues();

        if (! (db_num_rows($result) > 0)) {
            write_custom_script_log("cs_custom_script_create_batch_item_issue_to_stock_moves", 'No items found');
            return;
        }
        
        while ($row = db_fetch($result)) {
            $trans_no = $row["trans_no"];
            $stock_id = $row["stock_id"];
            $loc_code = $row["loc_code"];
            $tran_date_ = sql2date($row['tran_date']);
            $reference = $row["reference"];
            $qty = -$row["qty"];
            $standard_cost = $row["standard_cost"];
            $tran_unit = $row["tran_unit"];
            $tran_qty = -$row["tran_qty"];
            $conversion_factor = $row["conversion_factor"];
            $type = $row["type"];
            $task_id = $row["task_id"];
            $project_site_id = $row["project_site_id"];
            $trans_id = $row["trans_id"];
            $project_id = $row["project_id"];
            
            $person_id = $row["person_id"];
            $visible = $row["visible"];
            $price = $row["price"];
            $discount_percent = $row["discount_percent"];
            $item_description = $row["item_description"];
            
            $msg = '';
            $msg = "Trans no:$trans_no, Stock id:$stock_id, Trans type:$type, Line id:$trans_id, Item type:"; 
            
            if ($row['daily_item_type'] == NSIT_STOCK_ITEM) {

                $msg .= "Stock item";
                $sm_row = cs_get_stock_move_record_for_batch_item_issue_to_stock_non_stock_moves($trans_no, $type, $trans_id, NSIT_STOCK_ITEM);
                    if (is_array($sm_row)) {
                        
                        $msg .= " - Skipped.";
                        write_custom_script_log("cs_custom_script_create_batch_item_issue_to_stock_moves", $msg);
                        
                        continue;
                    }
                    
                $sm_line_id = add_stock_adjustment_item($trans_no, $stock_id, $loc_code, $tran_date_, $reference, $qty, $standard_cost, '', $tran_unit, $tran_qty, 
                                          $conversion_factor, $type, $task_id, $project_site_id, $trans_id, $project_id);
            } else {
                
                $msg .= "Nonstock item";
                $sm_row = cs_get_stock_move_record_for_batch_item_issue_to_stock_non_stock_moves($trans_no, $type, $trans_id);
                    if (is_array($sm_row)) {
                        
                        $msg .= " - Skipped.";
                        write_custom_script_log("cs_custom_script_create_batch_item_issue_to_stock_moves", $msg);
                        
                        continue;
                    }
                    
                $sm_line_id = add_non_stock_move($type, $stock_id, $trans_no, $loc_code, $tran_date_, $reference, $qty, $standard_cost, $person_id, $visible, $price, 
                                   $discount_percent, $tran_unit, $tran_qty, $conversion_factor, $task_id, $project_site_id, $trans_id, $item_description, 
                                   $project_id);
            }
            
            $msg_diid = cs_update_dii_line_sm_line_id($trans_id, $sm_line_id);
            if($msg_diid["success"] !== true){
                $diid_message = $msg_diid["message"];
                write_custom_script_log("cs_custom_script_create_batch_item_issue_to_stock_moves", $diid_message);
                display_error($diid_message);
                return;
            }
            
            $msg .= "DIID Line Id $trans_id Completed.";
            write_custom_script_log("cs_custom_script_create_batch_item_issue_to_stock_moves", $msg);

        }
        
    $message_2 = "------------------Script Completed----------------------------";
    write_custom_script_log("cs_custom_script_create_batch_item_issue_to_stock_moves", $message_2);
    
}