<?php

function display_item_transaction_summary_selecting_parameters() {

    start_table(TABLESTYLE);
        date_row('Date:', 'from_date', null, null, 0, -6, 0, null, true);
        date_row('Date:', 'to_date', null, null, 0, 0, 0, null, true);
        locations_list_row("Location:", 'location', null, "", true);
    end_table(1);

    
    start_table();
        submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'process');
    end_table(2);
}


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));
}

function _its_itemcode($row){ 
    return $row['stock_id'];
}

function _its_item_description($row){ 
    return $row['description'];
}

function _its_unit_name($row){ 
    return $row['unit_name'];
}

function _its_total_qty($row){
    $total_qty = number_format($row['total_qty'], 2);
    return $total_qty;
}
function _its_opening_balance($row){
    $stock_id = $row['stock_id'];
    $loc_code = $_POST['location'];
    $ob = get_qoh_on_date($stock_id, $loc_code, add_days($_POST['from_date'], -1));
    return number_format($ob, 2);
}
function _its_closing_balance($row){
    $stock_id = $row['stock_id'];
    $loc_code = $_POST['location'];
    $cb = get_qoh_on_date($stock_id, $loc_code, $_POST['to_date']);
    return number_format($cb, 2);
}

function _its_ST_SUPPRECEIVE($row){
    return _its_cell_value(ST_SUPPRECEIVE, $row);
}
function _its_ST_PR_RETURN($row){
    return _its_cell_value(ST_PR_RETURN, $row);
}
function _its_ST_MATERIAL_RETURN($row){
    return _its_cell_value(ST_MATERIAL_RETURN, $row);
}
function _its_ST_DAILY_ITEM_RETURN($row){
    return _its_cell_value(ST_DAILY_ITEM_RETURN, $row);
}
function _its_ST_PETTY_CASH_ITEM_RECEIPT($row){
    return _its_cell_value(ST_PETTY_CASH_ITEM_RECEIPT, $row);
}
function _its_ST_INVENTORY_RECEIPT($row){
    return _its_cell_value(ST_INVENTORY_RECEIPT, $row);
}
function _its_ST_PR_ISSUE($row){
    return _its_cell_value(ST_PR_ISSUE, $row);
}
function _its_ST_LOCTRANSFER($row){
    return _its_cell_value(ST_LOCTRANSFER, $row);
}
function _its_ST_SUPPCREDIT($row){
    return _its_cell_value(ST_SUPPCREDIT, $row);
}
function _its_ST_MATERIAL_ISSUE($row){
    return _its_cell_value(ST_MATERIAL_ISSUE, $row);
}
function _its_ST_DAILY_ITEM_ISSUE($row){
    return _its_cell_value(ST_DAILY_ITEM_ISSUE, $row);
}
function _its_ST_INVENTORY_ISSUE($row){
    return _its_cell_value(ST_INVENTORY_ISSUE, $row);
}
function _its_ST_STOCK_COUNT($row){
    return _its_cell_value(ST_STOCK_COUNT, $row);
}
function _its_ST_STOCK_ASSET_TRANSFER($row){
    return _its_cell_value(ST_STOCK_ASSET_TRANSFER, $row);
}
function _its_cell_value($trans_type, $row){

    $stock_id = $row['stock_id'];
    $from_date = date2sql($_POST['from_date']);
    $to_date = date2sql($_POST['to_date']); 
    $loc_code = $_POST['location'];
    $dec = 2;
    
    $qty_total = get_type_item_transaction_summary($from_date, $to_date, $stock_id, $trans_type, $loc_code);
    if($qty_total != 0){
        $parameters = "stock_id=$stock_id&fdate=$from_date&tdate=$to_date&type=$trans_type&loc_code=$loc_code";
        $link = get_data_view_str(DV_ITEM_TRANSACTION_SUMMARY, $parameters, number_format($qty_total, $dec)); 
    }else{
        $link = '';
    }
    return $link;
}


                        
function display_item_transaction_summary($from_date_, $to_date_, $loc_code='') {
    
    global $systypes_array;
    
    $from_date = date2sql($from_date_);
    $to_date = date2sql($to_date_);
    
    $sql = get_sql_for_items_for_item_transaction_summary($from_date, $to_date, $loc_code);


	$cols = array(
		_("Item") => array('fun'=>'_its_itemcode'), 
                _("Description") => array('fun'=>'_its_item_description'),
                _("Base Unit") => array('fun'=>'_its_unit_name'),
                _("Opening Balance") => array('fun'=>'_its_opening_balance', 'align' => 'right'),
		_($systypes_array[ST_SUPPRECEIVE]) => array('fun'=>'_its_ST_SUPPRECEIVE', 'align' => 'right'),
		_($systypes_array[ST_PR_RETURN]) => array('fun'=>'_its_ST_PR_RETURN', 'align' => 'right'),
		_($systypes_array[ST_MATERIAL_RETURN]) => array('fun'=>'_its_ST_MATERIAL_RETURN', 'align' => 'right'),
		_($systypes_array[ST_DAILY_ITEM_RETURN]) => array('fun'=>'_its_ST_DAILY_ITEM_RETURN', 'align' => 'right'),
		_($systypes_array[ST_PETTY_CASH_ITEM_RECEIPT]) => array('fun'=>'_its_ST_PETTY_CASH_ITEM_RECEIPT', 'align' => 'right'),
		_($systypes_array[ST_INVENTORY_RECEIPT]) => array('fun'=>'_its_ST_INVENTORY_RECEIPT', 'align' => 'right'),
		_($systypes_array[ST_PR_ISSUE]) => array('fun'=>'_its_ST_PR_ISSUE', 'align' => 'right'),
		_($systypes_array[ST_LOCTRANSFER]) => array('fun'=>'_its_ST_LOCTRANSFER', 'align' => 'right'),
		_($systypes_array[ST_SUPPCREDIT]) => array('fun'=>'_its_ST_SUPPCREDIT', 'align' => 'right'),
		_($systypes_array[ST_MATERIAL_ISSUE]) => array('fun'=>'_its_ST_MATERIAL_ISSUE', 'align' => 'right'),
		_($systypes_array[ST_DAILY_ITEM_ISSUE]) => array('fun'=>'_its_ST_DAILY_ITEM_ISSUE', 'align' => 'right'),
		_($systypes_array[ST_INVENTORY_ISSUE]) => array('fun'=>'_its_ST_INVENTORY_ISSUE', 'align' => 'right'),
		_($systypes_array[ST_STOCK_COUNT]) => array('fun'=>'_its_ST_STOCK_COUNT', 'align' => 'right'),
		_($systypes_array[ST_STOCK_ASSET_TRANSFER]) => array('fun'=>'_its_ST_STOCK_ASSET_TRANSFER', 'align' => 'right'),
		_("Transactions Total") => array('fun'=>'_its_total_qty', 'align' => 'right'),
		_("Closing Balance") => array('fun'=>'_its_closing_balance', 'align' => 'right'));

//	array_append($cols, array(
//        array('insert'=>true, 'fun'=>'edit_link'),
//        array('insert'=>true, 'fun'=>'order_link', 'align' => 'center'), 
//		array('insert'=>true, 'fun'=>'dispatch_link', 'align' => 'center'), 
//        array('insert'=>true, 'fun'=>'return_link', 'align' => 'center'), 
//		));


    $table =& new_db_pager('orders_tbl', $sql, $cols, null, null, 500);

    $table->width = "80%";

    display_db_pager($table);
}

?>