<?php

function display_stock_status_selecting_parameters() {
    if (! isset($_POST['stock_id'])) {
        $_POST['stock_id'] = get_global_stock_item();
    }
    
    start_table(TABLESTYLE);
        label_row('Item:', stock_costable_items_list('stock_id', $_POST['stock_id'], false, true));
        date_row('Date:', 'date', null, null, 0, 0, 0, null, true);
    end_table(1);
    
    set_global_stock_item($_POST['stock_id']);
    
    start_table();
        submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
    end_table(2);
}

function display_stock_status_details($stock_id, $date_) {
    global $Ajax, $systypes_array;
    
    $date = date2sql($date_);
    
    $mb_flag = get_mb_flag($stock_id);
    $kitset_or_service = false;


    $Ajax->activate('status_tbl');
    div_start('status_tbl');
    if (is_service($mb_flag)) {
        display_note(_("This is a service and cannot have a stock holding, only the total quantity on outstanding sales orders is shown."), 0, 1);
        $kitset_or_service = true;
    }

        start_table(TABLESTYLE, "width=99%"); 
            $th = array(_("Location"), _("Quantity On Hand"), _($systypes_array[ST_SALESORDER].'s'), _($systypes_array[ST_PURCH_REQ].'s'), 
                        _($systypes_array[ST_INVENTORY_ISSUE_REQUEST].'s'), _($systypes_array[ST_TRANSFERREQ].'s <br>(To Be Issued)'), 
                        _($systypes_array[ST_MATERIAL_REQ].'s'), _("Total Demand"), _($systypes_array[ST_PURCHORDER].'s'), 
                        _($systypes_array[ST_INVENTORY_RECEIPT_REQUEST].'s'), _($systypes_array[ST_TRANSFERREQ].'s <br>(To Be Received)'), 
                        _("Total On Order"), _("Available"), _("Re-Order Level"));
            
            labelheader_cell('');
            labelheader_cell('');
            labelheader_cell('Demand', 'colspan=6');
            labelheader_cell('On Order', 'colspan=4');
            labelheader_cell('');
            labelheader_cell('');
            
            table_header($th);
            
            $dec = get_qty_dec($stock_id);
            $k = $qty_on_hand_total = $so_total = $pr_total = $iir_total = $mtr_nti_total = $mr_total = $demand_total = $po_total = $irr_ntr_total = 
            $mtr_ntr_total = $on_hand_total = $available_total = $reorder_level_total = 0; 

            $loc_details = get_loc_details($stock_id);
                while ($row = db_fetch($loc_details)) {
                    $loc_code = $row["loc_code"];
                    
                    $qoh = get_qoh_on_date($stock_id, $loc_code, $date_);
                    
                    // Demand
                    $so_out_qty = get_sales_order_details_outstanding_qty($stock_id, $loc_code, $date_);
                    $drct_pr_out_qty = get_purchase_requisition_details_outstanding_qty($stock_id, $loc_code, $date_);
                    $sa_iss_out_qty = get_stock_adjustment_issue_outstanding_qty($stock_id, $loc_code, $date_, ST_INVENTORY_ISSUE_REQUEST);
                    $mtr_nti = get_material_transfer_requisition_details_not_issued_qty($stock_id, $loc_code, $date_);
                    $mr_out_qty = get_material_requisition_details_outstanding_qty($stock_id, $loc_code, $date_);
                    
                    // On Order
                    $po_out_qty = get_purchase_order_details_outstanding_qty($stock_id, $loc_code, $date_);
                    $irr_rcp_out_qty = get_stock_adjustment_issue_outstanding_qty($stock_id, $loc_code, $date_, ST_INVENTORY_RECEIPT_REQUEST);
                    $mr_nrcv_out_qty = get_material_transfer_requisition_details_not_received_qty($stock_id, $loc_code, $date_);

                    alt_table_row_color($k);
                    
                        label_cell($row["location_name"]);
                        qty_cell($qoh, true, $dec);

                        // Demand
                        
                        $so_out_qty_link = get_stock_status_view_link($stock_id, $date, $loc_code, ST_SALESORDER, $so_out_qty, $dec);
                        label_cell($so_out_qty_link, 'align=right');
                        
                        $drct_pr_out_qty_link = get_stock_status_view_link($stock_id, $date, $loc_code, ST_PURCH_REQ, $drct_pr_out_qty, $dec);
                        label_cell($drct_pr_out_qty_link, 'align=right');
                        
                        $sa_iss_out_qty_link = get_stock_status_view_link($stock_id, $date, $loc_code, ST_INVENTORY_ISSUE_REQUEST, $sa_iss_out_qty, $dec);
                        label_cell($sa_iss_out_qty_link, 'align=right');
                        
                        $mtr_nti_link = get_stock_status_view_link($stock_id, $date, $loc_code, ST_TRANSFERREQ, $mtr_nti, $dec);
                        label_cell($mtr_nti_link, 'align=right');
                        
                        $mr_out_qty_link = get_stock_status_view_link($stock_id, $date, $loc_code, ST_MATERIAL_REQ, $mr_out_qty, $dec);
                        label_cell($mr_out_qty_link, 'align=right');

                        $demand = $so_out_qty + $drct_pr_out_qty + $sa_iss_out_qty + $mtr_nti + $mr_out_qty;
                        qty_cell($demand, true, $dec);

                        // On Order
                        $po_out_qty_link = get_stock_status_view_link($stock_id, $date, $loc_code, ST_PURCHORDER, $po_out_qty, $dec);
                        label_cell($po_out_qty_link, 'align=right');
                        
                        $irr_rcp_out_qty_link = get_stock_status_view_link($stock_id, $date, $loc_code, ST_INVENTORY_RECEIPT_REQUEST, $irr_rcp_out_qty, $dec);
                        label_cell($irr_rcp_out_qty_link, 'align=right');
                        
                        $mr_nrcv_out_qty_link = get_stock_status_view_link($stock_id, $date, $loc_code, ST_REQTRANSFER, $mr_nrcv_out_qty, $dec);
                        label_cell($mr_nrcv_out_qty_link, 'align=right');

                        $on_order = $po_out_qty + $irr_rcp_out_qty + $mr_nrcv_out_qty;
                        qty_cell($on_order, true, $dec);

                        $available = $qoh - $demand + $on_order;
                        qty_cell($available, false, $dec);
                        
                        $reorder_level = get_item_reorder_level($stock_id, $loc_code);
                        qty_cell($reorder_level, false, $dec);

                    end_row();
                    
                    $qty_on_hand_total += $qoh;
                    $so_total += $so_out_qty;
                    $pr_total += $drct_pr_out_qty;
                    $iir_total += $sa_iss_out_qty;
                    $mtr_nti_total += $mtr_nti;
                    $mr_total += $mr_out_qty;
                    $demand_total += $demand;
                    
                    $po_total += $po_out_qty;
                    $irr_ntr_total += $irr_rcp_out_qty;
                    $mtr_ntr_total += $mr_nrcv_out_qty;
                    $on_hand_total += $on_order;
                    $available_total += $available;
                    $reorder_level_total += $reorder_level;
                }
                
                start_row();
                    label_cell(text_bold('Total'), 'align=right');
                    qty_cell($qty_on_hand_total, true, $dec);
                    qty_cell($so_total, true, $dec);
                    qty_cell($pr_total, true, $dec);
                    qty_cell($iir_total, true, $dec);
                    qty_cell($mtr_nti_total, true, $dec);
                    qty_cell($mr_total, true, $dec);
                    qty_cell($demand_total, true, $dec);
                    qty_cell($po_total, true, $dec);
                    qty_cell($irr_ntr_total, true, $dec);
                    qty_cell($mtr_ntr_total, true, $dec);
                    qty_cell($on_hand_total, true, $dec);
                    qty_cell($available_total, true, $dec);
                    qty_cell($reorder_level_total, true, $dec);
                end_row();

        end_table(3);
        
    div_end();
}

function get_stock_status_view_link($stock_id, $date, $loc_code, $type, $qty, $dec) {
    $parameters = "stock_id=$stock_id&date=$date&loc_code=$loc_code&type=$type";
    return get_data_view_str(DV_STOCL_STATUS_DETAILS, $parameters, number_format($qty, $dec));
}

?>