<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_SUPPLIERINVOICE_GROUPING';
if (!@$_GET['popup'])
    $path_to_root = "..";
else	
    $path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/includes/approval_functions.inc");
include_once($path_to_root . "/gl/includes/db/gl_db_bank_accounts.inc");
include_once($path_to_root . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/purchasing/includes/db/supplier_invoice_grouping_db.inc");

$js = '';

if ($SysPrefs->use_popup_windows){
    $js .= get_js_open_window(900, 500);
}

if (user_use_date_picker()){
    $js .= get_js_date_picker();
}

page(_($help_context = "Supplier Invoice Grouping"), false, false, "", $js);
simple_page_mode(true);

//------------------------------------------------------------------------------------------    

function dislay_suppliers_invoice_details()
{
    global $Ajax, $petty_cash_float_transaction_types;   
    
    $Ajax->activate('leaves');
    div_start('leaves');
      
    start_table(TABLESTYLE, "width=80%");
    
    $result = get_supplier_invoice_details($_POST['from_date'], $_POST['to_date'], $_POST['view_all'], 
                                            isset($_POST['search_group_reference'])? $_POST['search_group_reference']:'');

    $th = array(_("Type"), _("Trans No"), _("Reference"), _("Supplier"),  
                 _("Sup Reference"),
                _("Invoice Date"), _("Due Date"), _("Amount"), _("Group Reference"), "");              
           
    table_header($th);

    global $systypes_array;
    
    $j = 0;
    $total = 0;
    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);
        label_cells('', $systypes_array[$myrow["type"]]);
        
        $s = get_trans_view_str($myrow["type"], $myrow["trans_no"]);
        label_cells('', $s,'', "align=center");   
        label_cells('', $myrow["reference"],'', "align=right"); 
        label_cells('', $myrow["supp_name"]);        
                
        label_cells('', $myrow["supp_reference"],'');        
        label_cells('', sql2date($myrow["tran_date"]),'');           
        label_cells('', sql2date($myrow["due_date"]),'');   
        label_cells('', number_format($myrow["ov_amount"], 2),'', "align=right");  
        
//        if(!(isset($_POST[$myrow["trans_no"]."_".'group_reference']))){  
//            $_POST[$myrow["trans_no"]."_".'group_reference'] = $myrow['group_reference'];                     
//        }
        label_cells(null, $myrow['group_reference']);   
                                   
        $check_cell_name = $myrow["type"]."_".$myrow["trans_no"]."_".'selected_'.$myrow["trans_no"];
        check_cells('', $check_cell_name);
        
        if(isset($_POST[$check_cell_name]) && ($_POST[$check_cell_name] == true)){ 
            $total += $myrow["ov_amount"];
        }

        end_row();
        $j++; 
    }
    
    start_row();
    label_cells("Total", number_format($total, 2), "align=right colspan=7", "align=right");
    
    end_row();
    end_table();
    div_end();     
}



function create_supplier_invoice_grouping($from_date, $to_date, $new_only, $search_group_reference)
{
    global $Refs;    
   
    $result = get_supplier_invoice_details($from_date, $to_date, $new_only, $search_group_reference); 
    
//    $max_value = get_max_amount_of_groud_id();
//    $group_id = $max_value['max_group_no'];    
//    $group_id++;  
    
    $new_group_reference = $_POST['new_group_reference'];

    while($myrow = db_fetch($result))            
    {                    
        $check_cell_name = $myrow["type"]."_".$myrow["trans_no"]."_".'selected_'.$myrow["trans_no"];
        if((isset($_POST[$check_cell_name])) && ($_POST[$check_cell_name] == true))
        {           
//            $group_reference = $_POST[$myrow["trans_no"]."_".'group_reference'];
            update_supplier_invoice_group_reference($myrow["type"], $myrow["trans_no"], $new_group_reference);            
        }
       
    } 
    
}

if(isset($_POST['update_supplier_grouping']))
{       
    create_supplier_invoice_grouping($_POST['from_date'], $_POST['to_date'], $_POST['view_all'],
                                    isset($_POST['search_group_reference'])? $_POST['search_group_reference']:'');
}

//-------------------------------------------------------------------------------------------


if (!@$_GET['popup'])
start_form();


br(); 

start_table(TABLESTYLE);
    date_cells("From Date :", 'from_date', '', null, -30);
    date_cells("To Date :", 'to_date', '', null, +30);
    yesno_list_cells("New/All :", 'view_all', null, 'New Only', 'All', true);
    text_cells("Search Group Reference :", 'search_group_reference', '', 30, 30);
    submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
    
end_table(2);

start_table(TABLESTYLE, "width=30%");
    text_row('New Group Reference', 'new_group_reference', '', 30, 50);    
end_table();        
        

dislay_suppliers_invoice_details($_POST['from_date'], $_POST['to_date'], $_POST['view_all']);

br();

submit_center_first('Calculate_Invoice_Grouping_Total', _("Calculate"), '', 'default');
submit('update_supplier_grouping', 'Update', true, '', 'default');

br(2);

if (!@$_GET['popup'])    
{
    end_form();
    end_page(@$_GET['popup'], false, false);    
}



?>
