<?php

function get_lot_allocation_parent_line($type, $trans_no, $line_id) {
    switch ($type) {
        case ST_JOB_MATERIAL_ISSUE : 
        case ST_JOB_MATERIAL_RETURN : 
            $line = lot_get_job_material_issue_details_line_by_type_trans_no_line_no($type, $trans_no, $line_id);

            break;
        
        case ST_DIRECT_PURCHASE_RECEIPT : 
        case ST_DIRECT_PURCHASE_RETURN : 
            $line = lot_get_quality_check_details_line_by_type_trans_no_line_no($type, $trans_no, $line_id);

            break;
        
        case ST_BUDGET_TO_BUDGET_TRANSFER : 
            $line = lot_get_budget_to_budget_transfer_detail_record_type_trans_no_line_no($type, $trans_no, $line_id);
            
            break;
    }
    
    return $line;
}

function view_lot_allocation_lines_by_type($type, $trans_no, $line_id) {
    $line = get_lot_allocation_parent_line($type, $trans_no, $line_id);

        switch ($type) {
            case ST_JOB_MATERIAL_ISSUE : 
            case ST_JOB_MATERIAL_RETURN : 
            case ST_DIRECT_PURCHASE_RECEIPT : 
            case ST_DIRECT_PURCHASE_RETURN : 
                view_lot_allocation_lines($type, $trans_no, $line_id, $line);

                break;

            case ST_BUDGET_TO_BUDGET_TRANSFER : 
                view_budget_to_budget_lot_allocation_lines($type, $trans_no, $line_id, $line);

                break;
        }
}

function view_lot_allocation_lines($type, $trans_no, $line_id, $line) {

    $stock_id = $line['stock_id']; $loc_code = $line['loc_code']; $issued_tran_unit_name = $line['tran_unit_name']; 
    $issued_conversion_factor = $line['conversion_factor'];
    
    display_heading('Lot Allocation Details of '.get_stock_item_formatted_name($stock_id).' item in '.$line['location_name']);
    
    start_table(TABLESTYLE, "width=75%");

        $th = array(_("Id"), _("Lot Name"), _("Location"), _("Lot Remain in Base Qty"), _("Tran Unit"), _("Lot Remain in Tran Qty"), _("Tran Qty"), 
                    _("Qty in Tran Unit ($issued_tran_unit_name)"));  
        table_header($th);

        $dec = get_qty_dec($stock_id);
        $k = 0; //row colour counter
        $result = get_lot_allocation_lines_details($type, $trans_no, $line_id, $stock_id, $loc_code);   
            while ($row = db_fetch($result)) {
                $jmi_ls_id = $row['jmi_ls_id'];
                $base_lot_qty = $row['qty'];
                
                alt_table_row_color($k);
                    label_cell($jmi_ls_id, 'align=center');
                    label_cell($row['lot_reference']);
                    label_cell($row['location_name']);
                    
                    $total_lot_remain_base_qty = $row["total_lot_remain_base_qty"];
                    label_cell(number_format($total_lot_remain_base_qty, $dec), 'align=right');
                    
                    label_cell($row['unit_name'], 'align=center');
                    
                    $tran_unit = $row['tran_unit'];
                    $convesion_factor = 1;
                    get_convertion_factor2($stock_id, $tran_unit, $convesion_factor);
                    $total_remain_tran_qty = $total_lot_remain_base_qty / $convesion_factor;
                    label_cell(number_format($total_remain_tran_qty, $dec), 'align=right');
                    label_cell(number_format(abs($row["tran_qty"]), $dec), 'align=right');
                    
                    label_cell(number_format(abs($base_lot_qty / $issued_conversion_factor), $dec), 'align=right');
                end_row();        
            }
            
            $base_qty_total = get_lot_allocations_total_qty($type, $trans_no, $line_id, $stock_id, $loc_code);
            $tran_qty_total = abs($base_qty_total) / $issued_conversion_factor;
            start_row();
                label_cell(text_bold("Total '$issued_tran_unit_name'"), 'align=right colspan=7');
                label_cell(text_bold(number_format($tran_qty_total, $dec)), 'align=right');
            end_row();
            
    end_table(3);
}

function view_budget_to_budget_lot_allocation_lines($type, $trans_no, $line_id, $line) {
    
    $from_jo_id = $line['from_jo_id']; $to_jo_id = $line['to_jo_id']; $stock_id = $line['stock_id']; $to_loc_code = $line['loc_code'];
    $display_unit = $line['tran_unit']; 
    $convesion_factor = 1;
    
    display_heading("From Job #$from_jo_id Lots Selection Details of ".get_stock_item_formatted_name($stock_id)." item at ".
                    get_location_name($to_loc_code)." (unit in '".get_item_base_unit_name($stock_id)."')");

        start_table(TABLESTYLE, "width=99%");

            $th = array(_("Id"), _("Lot Name"), _("Location"), _("DP.(Recp+Retn) Lot Qty"), _("JM.(Issu+Retn) Lot Qty"), _("B2B.Trn. Lot Qty"), 
                        _("Alloc. (JM.Issu+B2B.Trnf) Lot Qty"), _("Remain Base Qty"), _("Tran Unit"), _("Remain Tran Qty"), _("Tran Qty"));  
            table_header($th);

            $dec = 4; //get_qty_dec($stock_id);
            $k = 0; //row colour counter

            $result = lot_get_budget_to_budget_transfer_lots($from_jo_id, $to_jo_id, $type, $trans_no, $line_id, $stock_id, true);   
                while ($row = db_fetch($result)) {
                    $tran_id = $row['tran_id'];
                    if (! ($tran_id > 0)) {
                        continue;
                    }
                    
                    $lot_id = $row['lot_id'];

                    alt_table_row_color($k);
                        label_cell($tran_id, 'align=center');
                        label_cell($row['lot_reference']);
                        label_cell($row['location_name']);
                        
                        $dp_rcp_rtn_qty = (float) $row['dp_rcp_rtn_qty']; 
                        qty_cell($dp_rcp_rtn_qty, false, $dec);
                        
                        $jm_iss_rtn_qty_ = lot_get_jm_issue_return_lot_qty($stock_id, $from_jo_id, $to_jo_id, $lot_id);
                        $jm_iss_rtn_qty = abs($jm_iss_rtn_qty_);
                        qty_cell($jm_iss_rtn_qty, false, $dec);
                        
                        $total_b2b_qty_ = lot_get_total_from_job_b_to_b_transfer_lot_qty($stock_id, $from_jo_id, $lot_id);
                        $total_b2b_qty = abs($total_b2b_qty_);
                        qty_cell($total_b2b_qty, false, $dec);
                        
                        $total_jm_iss_b2b_qty_ = lot_get_total_from_job_jm_issue_b_to_b_transfer_lot_qty($stock_id, $from_jo_id, $lot_id);
                        $total_jm_iss_b2b_qty = abs($total_jm_iss_b2b_qty_);
                        qty_cell($total_jm_iss_b2b_qty, false, $dec);
                        
                        $remain_base_qty = $dp_rcp_rtn_qty - $jm_iss_rtn_qty - $total_b2b_qty - $total_jm_iss_b2b_qty;
                        qty_cell($remain_base_qty, false, $dec);

                        label_cell($row['unit_name'], 'align=center');

                        $convesion_factor = $row['conversion_factor'];
                        $remain_tran_qty = $remain_base_qty / $convesion_factor;
                        qty_cell($remain_tran_qty, false, $dec);
                        
                        qty_cell(abs($row["tran_qty"]), false, $dec);

                    end_row();        
                }

                $base_qty_total = get_lot_allocations_total_qty($type, $trans_no, $line_id, $stock_id, $to_loc_code);
                $tran_qty_total = abs($base_qty_total)/ $convesion_factor;
                start_row();
                    label_cell(text_bold("Total '". get_unit_descr($display_unit)."'"), 'align=right colspan=7');
                    qty_cell($tran_qty_total, true, 3);
                    label_cell('');
                end_row();

        end_table(2);
}
    
?>