<?php



function create_batch_item_issue_pr_cart($pr_no, $po_no){
    
    if (isset($_SESSION['BatchItemIssuePrCart']))
    {
            $_SESSION['BatchItemIssuePrCart']->clear_items();
            unset ($_SESSION['BatchItemIssuePrCart']);
    }

    $_SESSION['BatchItemIssuePrCart'] = new items_cart(ST_PURCH_REQ);
    populate_pr_cart_from_database('BatchItemIssuePrCart', $pr_no, $po_no);
    
}


function update_batch_item_issue_pr_cart_from_database($biih_row, $trans_pr_common_row)
{
    $total_tran_qty =0;
    
    foreach ($_SESSION['BatchItemIssuePrCart']->line_items as &$line_item)
    {
//        $prefix = $line_item->row_id."_".$line_item->pr_po_line_id."_".$line_item->po_line_id;
        
        $type_id = $trans_pr_common_row['type'];
        $trans_no = $trans_pr_common_row['trans_no'];
        $source_type_id = $trans_pr_common_row['source_type_id'];
        $source_line_id = $line_item->row_id; 
        $movement_type = $trans_pr_common_row['movement_type'];
        $linked_field_type = $trans_pr_common_row['linked_field_type'];
        $linked_field_id = $trans_pr_common_row['linked_field_id'];
        $group_company_code = $trans_pr_common_row['group_company_code'];
        $po_no = $trans_pr_common_row['po_no'];
        $employee_company_code = $trans_pr_common_row['employee_company_code'];
        $employee_code = $trans_pr_common_row['employee_code'];
                
        $row_count = get_batch_item_issue_pr_line_transaction_count($type_id, $trans_no, $source_type_id, $source_line_id, 
                                                                $movement_type, $linked_field_type, $linked_field_id, $group_company_code, $po_no,
                                                                $employee_company_code, $employee_code);
        
        if($row_count == 0){
            $line_item->item_selected = false;
            continue;            
        }
        elseif($row_count == 1){
            $line_item->item_selected = true;
            $pr_line_row = get_batch_item_issue_pr_line_transaction($type_id, $trans_no, $source_type_id, $source_line_id, 
                                                                $movement_type, $linked_field_type, $linked_field_id, $group_company_code, $po_no,
                                                                $employee_company_code, $employee_code);
            $tran_unit = $pr_line_row['tran_unit'];
            $tran_qty = $pr_line_row['tran_qty'];
            $conversion_factor = 1;
            get_convertion_factor2($line_item->stock_id, $tran_unit, $conversion_factor); 
            $base_unit_qty = $tran_qty * $conversion_factor;
        }
        elseif($row_count > 1){ //if line count is more than one then shift to base unit as different lines of BII can have different units
            $line_item->item_selected = true;
            $pr_line_row = get_batch_item_issue_pr_line_transaction_summary($type_id, $trans_no, $source_type_id, $source_line_id, 
                                                                $movement_type, $linked_field_type, $linked_field_id, $group_company_code, $po_no,
                                                                $employee_company_code, $employee_code);  
            $tran_unit = $pr_line_row['base_unit'];
            $tran_qty = $pr_line_row['base_unit_qty_sum'];
            $conversion_factor = 1; 
        }
 
        $lot_details = "";   
        $line_item->lot_details = $lot_details;    

        $base_unit_qty = $tran_qty * $conversion_factor;
        $line_item->quantity = $base_unit_qty;
        $line_item->tran_unit = $tran_unit;
        $line_item->tran_qty = $tran_qty;
        $line_item->tran_unit_conv_factor = $conversion_factor;
        // In the BII entry screen do not allow selection of different jobs in different lines for the same itemcode.
        // Otherwise there will more then one site_id for a single PR Issue line.
        $line_item->project_site_id = $pr_line_row["project_site_id"]; 
        
        $total_tran_qty += $tran_qty;
    }

    if(!($total_tran_qty > 0)){
        $message = ("No lines loaded with valid quantities.");
        return array("success"=>false, "trans_no"=>$trans_no, "message"=>$message);
    }
    else{
        return array("success"=>true, "message"=>"");
    }    
}


function can_process_batch_item_issue_pr_issue($biih_row, $trans_pr_common_row)
{

    global $systypes_array;
    
    $total_qty = 0;
    $prefix = $systypes_array[ST_PURCH_REQ]." ".$_SESSION['BatchItemIssuePrCart']->order_id;
    
    if($_SESSION['BatchItemIssuePrCart']->pr_complete == 1)
    {
        $message = ($prefix." does not allow ".$systypes_array[ST_PR_ISSUE].".");
        return array("success"=>false, "message"=>$message);
    }

    if ($trans_pr_common_row['linked_field_type'] == IMLF_ASSET) {
        $asset_row = get_asset($trans_pr_common_row['linked_field_id']);
        if ($asset_row['type'] == AT_VEHICLE) {
            $message = ($prefix." : Selected asset is a Vehicle. Only the assets of types Fixed Asset and Tool are allowed under this Movement Type.");
            return array("success"=>false, "message"=>$message); 
        }
    }        
    
    $total_tran_qty =0;
        
    foreach ($_SESSION['BatchItemIssuePrCart']->line_items as $line_item) 
    {
        if($line_item->item_selected == false){
            continue;
        }

        $tran_unit = $line_item->tran_unit;
        $total_issued_qty = get_pr_item_total_issued_qty($line_item->row_id);
        $base_unit_issued_qty = $total_issued_qty;
        get_convertion_factor2($line_item->stock_id, $tran_unit, $tran_unit_convesion_factor);
        $tran_unit_issued_qty = $base_unit_issued_qty / $tran_unit_convesion_factor;

        $base_unit_issuing_qty = $line_item->quantity;
        $tran_unit_issuing_qty = $line_item->tran_qty;

        $tran_unit_requested_qty = $line_item->requested_qty / $tran_unit_convesion_factor;

        if($tran_unit_issued_qty + $tran_unit_issuing_qty > $tran_unit_requested_qty + 0.0000001)
        {
            $message = (_("Line Id ".$line_item->row_id  
                    ." - Item ".$line_item->stock_id." - ".$line_item->item_description
                    ."<br> Issuing Unit : ". get_unit_descr($line_item->tran_unit) 
                    .", Issuing quantity (".number_format($tran_unit_issuing_qty, 3)
                    .") + already issued qty (".number_format($tran_unit_issued_qty, 3)
                    .") can not be greater than the requested quantity (".number_format($tran_unit_requested_qty,3).")."));
            return array("success"=>false, "message"=>$message); 
        }
        
        $total_tran_qty += $tran_unit_issuing_qty;
    }
    
    if (!($total_tran_qty > 0)) {
        $message = ($prefix." : No lines with valid quantities selected.");
        return array("success"=>false, "message"=>$message);         
    }
    
    $vtqd_msg = validate_transaction_quantity_details($_SESSION['BatchItemIssuePrCart']);		
    if ($vtqd_msg['success'] !== true) 		
    {
        return array("success"=>false, "message"=>$vtqd_msg['message']); 
    }
    
    return array("success"=>true, "message"=>$vtqd_msg['message']); 
    
}


function process_batch_item_issue_pr_issue($biih_row, $trans_pr_common_row)
{    
    global $systypes_array;
    
    $target_type_id = ST_PR_ISSUE;
    
    $reference = get_next_reference_for_auto_generation($target_type_id);
    
    $pri_date_ = sql2date($biih_row['tran_date']);
    
    $pr_no = $_SESSION['BatchItemIssuePrCart']->order_id;
    
    $api_msg = add_pr_issue($_SESSION['BatchItemIssuePrCart']->line_items, $reference, $_SESSION['BatchItemIssuePrCart']->requesting_location,  
                            $biih_row['loc_code'], $_SESSION['BatchItemIssuePrCart']->project_id, $biih_row['comments'], $pri_date_,
                            $_SESSION['BatchItemIssuePrCart']->order_id, false, $_SESSION['BatchItemIssuePrCart']->row_state, $_SESSION['BatchItemIssuePrCart']->po_no,
                            $trans_pr_common_row['movement_type'], $trans_pr_common_row['linked_field_type'], $trans_pr_common_row['linked_field_id'], $trans_pr_common_row['group_company_code'],
                            $trans_pr_common_row['employee_code'], $trans_pr_common_row['employee_company_code']);
    
    $target_type_no = $api_msg['tran_no'];
    
    foreach ($_SESSION['BatchItemIssuePrCart']->line_items as $line_item) {
        $target_line_id = $line_item->target_line_id; 
        if(!($target_line_id > 0)){
            continue;
        }
        $type_id = $trans_pr_common_row['type'];
        $trans_no = $trans_pr_common_row['trans_no'];
        $source_type_id = $trans_pr_common_row['source_type_id'];
        $source_line_id = $line_item->row_id; 
        $movement_type = $trans_pr_common_row['movement_type'];
        $linked_field_type = $trans_pr_common_row['linked_field_type'];
        $linked_field_id = $trans_pr_common_row['linked_field_id'];
        $group_company_code = $trans_pr_common_row['group_company_code'];
        $po_no = $trans_pr_common_row['po_no'];
        $employee_company_code = $trans_pr_common_row['employee_company_code'];
        $employee_code = $trans_pr_common_row['employee_code']; 
                
        $n = update_batch_item_issue_pr_transaction_target_details($type_id, $trans_no, $source_type_id, $source_line_id, 
                                                                $movement_type, $linked_field_type, $linked_field_id, $group_company_code, $po_no,
                                                                $employee_company_code, $employee_code, 
                                                                $target_type_id, $target_type_no, $target_line_id);
        if(!($n > 0)){
            $message = "Could not update target transaction details of ".$systypes_array[$source_type_id]." #".$pr_no." line #".$source_line_id;
            return array("success"=>false, "tran_no"=>$target_type_no, "message"=>$message);
        }       
    }
                
    return $api_msg;
    
}

function process_batch_item_issue_pr($biih_row, $trans_pr_common_row)
{
    $pr_no = $trans_pr_common_row['source_type_no'];
    $po_no = $trans_pr_common_row['po_no'];
    
    create_batch_item_issue_pr_cart($pr_no, $po_no);
    
    $uprc_msg = update_batch_item_issue_pr_cart_from_database($biih_row, $trans_pr_common_row);
    if($uprc_msg["success"] !== true){
        return $uprc_msg;
    }
    
    $cpbii_msg = can_process_batch_item_issue_pr_issue($biih_row, $trans_pr_common_row);
    if($cpbii_msg['success'] !== true){
        return $cpbii_msg;
    }
    
    $pbii_pr_msg = process_batch_item_issue_pr_issue($biih_row, $trans_pr_common_row);
    return $pbii_pr_msg;
    
}

function close_batch_item_issue_source_pr($source_pr_no)
{
    if(check_all_pr_lines_issued($source_pr_no)=== true){
        return close_pr($source_pr_no, AS_COMPLETED);
    }
    
}

function process_batch_item_issue_prs($biih_row)
{
    $result = get_batch_item_issue_selected_distinct_pr_transactions($biih_row['type'], $biih_row['trans_no'], ST_PURCH_REQ); 
    while ($trans_pr_common_row = db_fetch($result)) {
        $pbii_prs_msg = process_batch_item_issue_pr($biih_row, $trans_pr_common_row);
        if($pbii_prs_msg["success"] !== true){
            return $pbii_prs_msg;
        }        
    }
    
    return array("success"=>true, "trans_no"=>$biih_row['trans_no'], "message"=>"");
    
}

