<?php

function process_batch_item_issue_dii_line1(&$dii_no, $selected_id) {    
    global $systypes_array;
    
    $standard_cost = 0; $conversion_factor = 1;
    
    $header = get_daily_item_issue_header_record($dii_no);
    $trans_no = $header['trans_no'];
    $type = $header['type'];
    
    $tran_unit = $_POST['tran_unit'];
    
    if ($_POST['daily_item_type'] == NSIT_STOCK_ITEM) {
        $standard_cost = get_standard_cost($_POST['stock_id']);
        get_convertion_factor2($_POST['stock_id'], $tran_unit, $conversion_factor);

    } elseif ($_POST['daily_item_type'] == NSIT_NON_STOCK_ITEM) {
        $tran_unit_cost = input_num('item_cost');
        get_convertion_factor2($_POST['stock_id'], $tran_unit, $conversion_factor);
        $standard_cost = $tran_unit_cost/$conversion_factor;
    }
    
    $qty = input_num('tran_qty') * $conversion_factor;
    
    $af_no = write_daily_item_issue_line($trans_no, $selected_id, $type, $_POST['location'], $_POST['tran_date'], $_POST['stock_id'], 
                                         $_POST['daily_item_type'], $_POST['item_description'], $_POST['tran_unit'], input_num('tran_qty'), $qty, 
                                         $conversion_factor, $standard_cost, get_post('site_id'), $_POST['line_comment'], $_POST['linked_field_id'],
                                         $_POST['task_id']); 
    
        if ($af_no > 0) {
            return array("success" => true, 'trans_no' => $dii_no, "message" => $systypes_array[$header['type']]." line saved.");
        } else {
            if ($selected_id > 0) {
                $ln_ms = $systypes_array[$header['type']]." #$selected_id line could not be updated.";
            } else {
                $ln_ms = $systypes_array[$header['type']]." line could not be saved.";
            }

                return array("success" => false, 'trans_no' => $dii_no, "message" => $ln_ms);
        }
}



function process_batch_item_issue_dii_header($biih_row, $trans_dii_common_row)
{
    global $systypes_array;

    $type = ST_DAILY_ITEM_ISSUE;
    $reference = get_next_reference_for_auto_generation($type);
    
    $project_id = $biih_row['project_id'];
    
    $movement_type = $trans_dii_common_row['movement_type'];
    $linked_field_type = $trans_dii_common_row['linked_field_type'];
    
    if ($trans_dii_common_row['linked_field_type'] != IMLF_EMPLOYEE) {
        $group_company_code = '';
    }else{
        $group_company_code = $trans_dii_common_row['group_company_code'];
    }
    
    $location = $biih_row['loc_code'];
    $comments = $biih_row['comments'];
    $tran_date_ = sql2date($biih_row['tran_date']);

    $dii_no = write_daily_item_issue_header($type, 0, $reference, $project_id, $movement_type, 
                                             $linked_field_type, '', $group_company_code, $location, $comments, 
                                             $tran_date_); 
    
    if (!($dii_no > 0)) {
        return array("success" => false, 'trans_no' => '', "message" => "Could not be saved ".$systypes_array[$type].' header.'); 
    }      
        
    $af_rows = update_daily_item_issue_header_approval_status($dii_no, AS_NEW, AS_APPROVED);
    if ($af_rows != 1) {
        return array("success" => false, 'trans_no' => $dii_no, "message" => "Could not update ".$systypes_array[$type].' header approval '
                                                                              .'status.');
    } 
    
    return array("success" => true, 'trans_no' => $dii_no, "message" => "");
}


function process_batch_item_issue_dii_lines($trans_dii_common_row, $biih_row) {
    
    global $systypes_array;
    
    $dii_trans_no = $biih_row['trans_no'];    
    $project_id = $biih_row['project_id'];    
    
    $bii_trans_type = $trans_dii_common_row['type']; 
    $bii_trans_no = $trans_dii_common_row['trans_no']; 
    $source_type_id = $trans_dii_common_row['source_type_id']; 
    $movement_type = $trans_dii_common_row['movement_type']; 
    $linked_field_type = $trans_dii_common_row['linked_field_type'];  
    $employee_company_code = $trans_dii_common_row['employee_company_code'];  
    $employee_code = $trans_dii_common_row['employee_code'];  
    
    $result = get_batch_item_issue_selected_dii_transactions($bii_trans_type, $bii_trans_no, $source_type_id, $movement_type, $linked_field_type,
                                                                $employee_company_code, $employee_code);
    while ($diit = db_fetch($result)) {
//        $dii_no = $pdiih_msg["trans_no"];
        $dii_trans_type = $diit["source_type_id"];
        $location = $biih_row["loc_code"];
        $tran_date_ = sql2date($biih_row["tran_date"]);
        $stock_id = $diit["stock_id"];
        $daily_item_type = $diit["daily_item_type"];
        $item_description = $diit["item_description"];
        $tran_unit = $diit["tran_unit"];
        $tran_qty = $diit["tran_qty"];
        $qty = $diit["qty"]; 
        
        $convesion_factor= 1;
        get_convertion_factor2($stock_id, $tran_unit, $convesion_factor);
        
        $standard_cost = $diit["standard_cost"];
        $project_site_id = $diit["project_site_id"];
        $line_comment = $diit["line_comment"];
        $linked_field_id = $diit["linked_field_id"];
        $task_id = $diit["task_id"];
        
        $dii_line_id = insert_daily_item_issue_line($dii_trans_no, $dii_trans_type, $location, $tran_date_, $stock_id, $daily_item_type, $item_description, $tran_unit, 
                                            $tran_qty, $qty, $convesion_factor, $standard_cost, $project_site_id, $line_comment, $linked_field_id, 
                                            $task_id, 0);
        
        $tran_qty = -$tran_qty;
        $qty = -$qty;
        
        if ($diit['mb_flag'] == 'D') {
            add_non_stock_move($dii_trans_type, $stock_id, $dii_trans_no, $location, $tran_date_, $biih_row["reference"], $qty, $standard_cost, 
                               0, 1, 0, 0, $tran_unit, $tran_qty, 
                               $convesion_factor, $task_id, $project_site_id, $dii_line_id, $diit["item_description"], 
                               $project_id);
        } else {
            add_stock_adjustment_item($dii_trans_no, $stock_id, $location, $tran_date_, $biih_row["reference"], $qty, $standard_cost, '', 
                                          $tran_unit, $tran_qty, $convesion_factor, $dii_trans_type, $task_id, $project_site_id, 
                                          $dii_line_id, $project_id);
        }
        
                                    
        $source_line_id = $diit["source_line_id"];
        $group_company_code = $diit["group_company_code"];
        $n = update_batch_item_issue_mr_transaction_target_details($bii_trans_type, $bii_trans_no, $source_type_id, $source_line_id, 
                                                            $movement_type, $linked_field_type, $linked_field_id, $group_company_code,                                                                 
                                                            $employee_company_code, $employee_code, 
                                                            $dii_trans_type, $dii_trans_no, $dii_line_id);
        
        if ($n == 0) {
            $message = "Could not update target transaction details of the selected ".$systypes_array[$bii_trans_type]." line #".$diit["id"];
            return array("success" => false, 'trans_no' => $bii_trans_no, "message" => $message);
        } 
        
    }
    
    return array("success" => true, 'trans_no' => $bii_trans_no, "message" => '');
}


function process_batch_item_issue_dii($biih_row, $trans_dii_common_row)
{
    
    
    $pdiih_msg = process_batch_item_issue_dii_header($biih_row, $trans_dii_common_row);
    if ($pdiih_msg["success"] !== true) {          
        return $pdiih_msg;
    }    
    $dii_header_id = $pdiih_msg["trans_no"];
    
    $dii_header = get_daily_item_issue_header_record($dii_header_id);
//    $dii_trans_type = $dii_header['type'];

    $pdiil_msg = process_batch_item_issue_dii_lines($trans_dii_common_row, $dii_header);
    if ($pdiil_msg["success"] !== true) {          
        return $pdiil_msg;
    } 

    $bii_trans_type = $dii_header["type"];
    $dii_trans_no = $dii_header['trans_no']; 
    
    $msg_diicc = batch_item_issue_dii_charge_cost_to_tasks($bii_trans_type, $dii_trans_no);
    if ($msg_diicc['success'] !== true) {
        return array("success" => $msg_diicc['success'], "tran_no" => $dii_trans_no, "message" => $msg_diicc['message']);
    }
        
    return array("success" => true, "tran_no" => $dii_trans_no, "message" => '');
    
    
}

function batch_item_issue_dii_charge_cost_to_tasks($type, $trans_no)
{

    $dii_details_array = $b = array();
            
    $result = get_daily_item_issue_details($trans_no, $type);      
    while ($row = db_fetch($result)) {
        if ($row['daily_item_type'] != NSIT_COMMENT) {
            $qty = $row['qty'];
            $b['row_id'] = $row['trans_id'];
            $b['stock_id'] = $row['stock_id'];
            $b['task_id'] = $row['task_id'];
            $b['requested_qty'] = $row['qty'];
            $b['high_value_item'] = 0;

            $standard_cost = $row['standard_cost'];
            $b['reference_cost'] = $standard_cost; 
            $b['standard_cost'] = $standard_cost;
            $b['charging_cost'] = $standard_cost;
            $b['total_issued_qty'] = $qty;
            $b['qty'] = $qty;

            $dii_details_array[] = $b;
        }
    }

    $mi_tasks_summary = calculate_task_remaining_totals($dii_details_array);
    $created_by = $_SESSION['wa_current_user']->user;

    $mtrec_msg = charge_cost_to_tasks($type, $trans_no, $mi_tasks_summary, $created_by);
    if ($mtrec_msg['success'] !== true) {
        return array("success" => $mtrec_msg['success'], "tran_no" => $trans_no, "message" => $mtrec_msg['message']);
    }else{
        return array("success" => true, "tran_no" => $trans_no, "message" => '');
    }

}

function process_batch_item_issue_diis($biih_row)
{
    $result = get_batch_item_issue_selected_dii_transactions_common_values($biih_row['type'], $biih_row['trans_no']); 
    while ($trans_dii_common_row = db_fetch($result)) {
        $pbii_prs_msg = process_batch_item_issue_dii($biih_row, $trans_dii_common_row);
        if($pbii_prs_msg["success"] !== true){
            return $pbii_prs_msg;
        }        
    }
    
    return array("success" => true, "message" => "");
    
}

