<?php


function display_petty_cash_transaction_filtering_controls()
{    
    global $systypes_array;
    
    start_table(TABLESTYLE_NOBORDER);
        start_row();
            ref_cells(text_bold("Transaction No:"), 'trans_no', '',null, '', true);
        end_row();
    end_table();

    $values_array = array();
    $values_array[ST_FLOAT_RECEIPT] = $systypes_array[ST_FLOAT_RECEIPT];
    $values_array[ST_FLOAT_PAYMENT] = $systypes_array[ST_FLOAT_PAYMENT];

    start_table(TABLESTYLE_NOBORDER);
        start_row();
            date_cells(_("From:"), 'from_date');
            date_cells(_("To:"), 'to_date');
            custom_values_list_cells("Type:", 'type_id', $values_array, null, '');
            petty_cash_floats_list_cells(_("Float:"), 'float_id', null, _('All floats'));
            submit_cells('SearchOrders', _("Search"),'',_('Search'), 'default');
        end_row();
    end_table();


}



function display_petty_cash_transaction_inquiry()
{
    $from_date = "";
    if(isset($_POST['from_date'])){
        $from_date = $_POST['from_date'];
    }
    $to_date = "";
    if(isset($_POST['to_date'])){
        $to_date = $_POST['to_date'];
    }

    $type_id = 0;
    if(isset($_POST['type_id'])){
        $type_id = $_POST['type_id'];
    }

    $trans_no= 0;
    if(isset($_POST['trans_no'])){
        $trans_no = $_POST['trans_no'];
    }

    $float_id= 0;
    if(isset($_POST['float_id'])){
        $float_id = $_POST['float_id'];
    }
    
        start_table(TABLESTYLE, "width=90%");
    
    $th = array(
                _("Type"),
                _("#"),
                _("Reference"), 
                _("Date"), 
                _("Float"), 
                _("To Float"), 
                _("Comment"),
                _("Status"),
                _("Is Return"),  
                _("Amount"), 
                _("Balance"));

    table_header($th);
    
    $sales_total = display_petty_cash_transactions_sales($from_date,$to_date, $type_id, $trans_no, $float_id);
    
    $float_trans_total = display_petty_cash_transactions_petty_cash($from_date,$to_date, $type_id, $trans_no, $float_id);
    
    $total = $sales_total + $float_trans_total;
    start_row();
    label_cell(text_bold("Float Balance"), 'align=right colspan=9');
    amount_cell($total, true);
    label_cell("");
    end_row();
    
    
    end_table(2);
}

function display_petty_cash_transactions_sales($from_date,$to_date, $type_id, $trans_no, $float_id, $to_float_id=0)
{
    global $systypes_array, $approval_statuses;
    
    $k = $total = 0;
    
//    $result = get_float_transactions($from_date, $to_date, $type_id, $trans_no, $float_id);  
//    $result = get_petty_cash_float_transactions($from_date,$to_date, $type_id, $trans_no, $float_id, $to_float_id);
    
    $sql = get_sql_for_petty_cash_float_transactions_pos_customer_payments($from_date, $to_date, $type_id, $trans_no, $float_id, $to_float_id);
    $result = db_query($sql);
    while ($row = db_fetch($result)) {
        alt_table_row_color($k);
        label_cell($systypes_array[$row['trans_type']]);
        
        if($row['trans_no'] > 0){
            $link = get_customer_trans_view_str($row['trans_type'], $row['trans_no']);
        }else{
            $link = "";    
        }
        label_cell($link);
        label_cell($row['reference']);
        label_cell(sql2date($row['tran_date']));
        label_cell($row['float_name']);
        label_cell($row['to_float_name']);
        label_cell($row['comment']);
        label_cell($approval_statuses[$row['approval_status']]);
        label_cell(get_yes_no_name($row['is_return']));
        
        $amount = $row['amount'];
        
        if($row['trans_type'] == ST_FLOAT_TRANSFER){
            $amount *= -1;
        }
        amount_cell($amount);
        
        $total += $amount;
        amount_cell($total);
        end_row();       
        
    }
    start_row();
    label_cell(text_bold("Sales Collections Total"), 'align=right colspan=9');
    amount_cell($total, true);
    label_cell("");
    end_row();
    
    return $total;
        
}


function display_petty_cash_transactions_petty_cash($from_date,$to_date, $type_id, $trans_no, $float_id, $to_float_id=0)
{
    global $systypes_array, $approval_statuses;
    
    

    
    $k = $total = 0;
    
//    $result = get_float_transactions($from_date, $to_date, $type_id, $trans_no, $float_id);  
//    $result = get_petty_cash_float_transactions($from_date,$to_date, $type_id, $trans_no, $float_id, $to_float_id);
    
    $sql = get_sql_for_petty_cash_float_transactions_adjustment($from_date, $to_date, $type_id, $trans_no, $float_id, $to_float_id);
    $result = db_query($sql);
    
    while ($row = db_fetch($result)) {
        alt_table_row_color($k);
        label_cell($systypes_array[$row['trans_type']]);
        
        if($row['trans_no'] > 0){
            $link = get_petty_cash_trans_view_str($row['trans_type'], $row['trans_no']);
        }else{
            $link = "";    
        }
        label_cell($link);
        label_cell($row['reference']);
        label_cell(sql2date($row['tran_date']));
        label_cell($row['float_name']);
        label_cell($row['to_float_name']);
        label_cell($row['comment']);
        label_cell($approval_statuses[$row['approval_status']]);
        label_cell(get_yes_no_name($row['is_return']));
        
        $amount = $row['amount'];
        
        if($row['trans_type'] == ST_FLOAT_TRANSFER){
            $amount *= -1;
        }
        amount_cell($amount);
        
        $total += $amount;
        amount_cell($total);
        end_row();
        
        
    }
    start_row();
    label_cell(text_bold("Float Transactions Total"), 'align=right colspan=9');
    amount_cell($total, true);
    label_cell("");
    end_row();
        
    return $total;
    
}

