<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_PROJECTS';
if (!@$_GET['popup'])
	$path_to_root = "..";
else	
	$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/sales/includes/sales_db.inc");
include_once($path_to_root . "/sales/includes/db/sales_types_db.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
//include_once($path_to_root . "/inventory/includes/inventory_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_invoicing_items_db.inc");
$js = '';

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

if (!@$_GET['popup'])
        page(_($help_context = "Inventory Item Sales prices"),false,false,"",$js);

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

check_db_has_projects(_("There are no projects defined in the system."));

simple_page_mode(true);
//---------------------------------------------------------------------------------------------------
$input_error = 0;

if (isset($_GET['project_id']))
{
	$_POST['project_id'] = $_GET['project_id'];
}
//---------------------------------------------------------------------------------------------------
if (!@$_GET['popup'])
	start_form();

	br(2);
//set_global_stock_item($_POST['project_id']);
function clear_task_data()
{
	unset($_POST['code']);
	unset($_POST['description']);
	unset($_POST['rate']);
	unset($_POST['unit']);
	unset($_POST['max_quantity']);
        unset($_POST['long_description']);	
        unset($_POST['tax_group_id']);	
}
//----------------------------------------------------------------------------------------------------
function can_process()
{
    if (get_post('description') == '')
    {
        $input_error = 1;
        display_error( _("Enter description."));
        set_focus('code');
        return false;  
    }
    
    if(get_post('type') == 0){ 
        if(get_post('code') == '')
        {
            $input_error = 1;
            display_error( _("Enter a code."));
            set_focus('code');
            return false;  
        }

        if(get_post('total_only') == 0){ 
            if(!(get_post('billing_type') > 0 ))
            {
                $input_error = 1;
                display_error( _("Select a billing type."));
                set_focus('code');
                return false;  
            }
        
            if(check_num('rate',0) == FALSE){
                $input_error = 1;
                display_error(_("Rate must be numeric and positive."));
                set_focus('rate');
                return false;
            } 

            if(!(get_post('tax_group_id') > 0 ))
            {
                $input_error = 1;
                display_error( _('Please select a tax group.'));
                set_focus('tax_group_id');
                return;            
            }
            if(check_num('max_quantity',0) == FALSE)
            {
                $input_error = 1;
                display_error(_("Quantity must be numeric and positive."));
                set_focus('max_quantity');
                return false;
            } 
        }
    }
    return true; 
}



if ($Mode=='ADD_ITEM' && can_process()) 
{
    if ($input_error =='0') 
    {
        if(get_post('parent_id') < 0){
            $parent_id = 0;
        }
        else{
            $parent_id = get_post('parent_id');
        }
        
        add_project_invoicing_item($project_id,  get_post('total_only'), get_post('deduction'), get_post('code'),
                                   get_post('description'), get_post('billing_type'), input_num('rate'), get_post('unit'),
                                   input_num('max_quantity'), get_post('long_description'), get_post('tax_group_id'),
                                   get_post('type'), get_post('display_order'), $parent_id, get_post('print_long_description'));
        clear_task_data();
        $msg = _("The new project billing item has been added.");            
        display_notification($msg);
        $Mode = 'RESET';
    }
	
}
if ($Mode=='UPDATE_ITEM' && can_process()) 
{
if ($input_error =='0') 
    {
        if(get_post('parent_id') < 0){
            $parent_id = 0;
        }
        else{
            $parent_id = get_post('parent_id');
        }
        update_project_invoicing_item($selected_id, get_post('total_only'), get_post('deduction'), get_post('code'),
                                      get_post('description'), get_post('billing_type'), input_num('rate'), get_post('unit'),
                                      input_num('max_quantity'), get_post('long_description'), get_post('tax_group_id'),
                                      get_post('type'), get_post('display_order'), $parent_id, get_post('print_long_description'));
        clear_task_data();
        $msg = _("This project billing item has been updated.");
        
        display_notification($msg);
        $Mode = 'RESET';
    }
}
//------------------------------------------------------------------------------------------------------

if ($Mode == 'Delete')
{
	//the link to delete a selected record was clicked
	delete_billing_item($selected_id);
	display_notification(_("The selected billing item has been deleted."));
	$Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$selected_id = -1;
}
//--------------------------------------------------------------------------------------------------------

$billings_list = get_project_invoicing_items($_POST['project_id']);

div_start('price_table');

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

$th = array(_("Display Order"), _("Group"), "Total Only", "Deduction",_("Code"), _("Description"), "Billing Type",
            _("Unit"), _("Rate"), _("Max. Qty"), _("Tax Group"),_("Parent"), "", "");

table_header($th);

$k = 0; //row colour counter
$name_yes = 0;
$calculated = false;
$parent_id = 0;

global $project_billing_types;

while ($myrow = db_fetch($billings_list))
{
    alt_table_row_color($k);
    
    label_cell($myrow["display_order"]); 
    if($myrow["type"] == 0){
        label_cell("No");   
        label_cell(get_yes_no_name($myrow["total_only"]));
        label_cell(get_yes_no_name($myrow["deduction"]));
        label_cell($myrow["code"]);
        label_cell($myrow["description"]);
        if($myrow["total_only"] == 0){
            label_cell($project_billing_types[$myrow["billing_type"]]);
            label_cell($myrow["unit"]);
            amount_cell($myrow["rate"]);   
            amount_cell($myrow["max_quantity"]);  
        }
        else{
            label_cell("");
            label_cell("");
            label_cell("");
            label_cell("");
        }
        label_cell($myrow["name"]);        
    }
    else{
        label_cell("Yes");
        label_cell("");
        label_cell("");
        label_cell("");
        label_cell($myrow["description"]);
        label_cell("");
        label_cell("");
        label_cell("");
        label_cell("");
        label_cell("");
    }
    
    if($myrow["parent_id"] == 0){
       label_cell("");   
    }else{        
       label_cell($myrow["parent_description"]);
    }
    
   

       
    edit_button_cell("Edit".$myrow['id'], _("Edit"));
    delete_button_cell("Delete".$myrow['id'], _("Delete"));
    end_row();
}

end_table();
div_end();
//------------------------------------------------------------------------------------------------

echo "<br>";

if ($Mode == 'Edit')
{
	$myrow = get_project_invoicing_item_details($selected_id);
        
        $_POST['total_only'] = $myrow["total_only"];
        $_POST['deduction'] = $myrow["deduction"];
        $_POST['code'] = $myrow["code"];
        $_POST['description'] = $myrow["description"];
        $_POST['billing_type'] = $myrow["billing_type"];
        $_POST['unit'] = $myrow["unit"];
        $_POST['rate'] = $myrow["rate"];
        $_POST['max_quantity'] = $myrow["max_quantity"];      
        $_POST['tax_group_id'] = $myrow["tax_group_id"]; 
        $_POST['type'] = $myrow["type"];
        $_POST['display_order'] = $myrow["display_order"];
        $_POST['parent_id'] = $myrow["parent_id"];
        $_POST['long_description'] = $myrow["long_description"];
        $_POST['print_long_description'] = $myrow["print_long_description"];
}
else{
        $_POST['rate'] = 0.00;
        $_POST['max_quantity'] = 0.00;
        $_POST['long_description'] = '';
        $_POST['print_long_description'] = false;
}

hidden('selected_id', $selected_id);
if (@$_GET['popup'])
{
	hidden('_tabs_sel', get_post('_tabs_sel'));
	hidden('popup', @$_GET['popup']);
}
div_start('price_details');

    start_table(TABLESTYLE2);
  
    text_row(_("Display Order:"), 'display_order', null, 20, 20);
    yesno_list_row("Group Header", 'type', null, "Yes", "No", true);
    if(get_post('type') == 0){
        yesno_list_row("Total Only", 'total_only', null, "Yes", "No", true);
        yesno_list_row("Deduction", 'deduction', null, "Yes", "No");
        text_row(_("Code:"), 'code', null, 50, 200);
        textarea_row(_("Description:"), 'description', null, 31, 3);
        
        if(get_post('total_only') == 1){
            hidden('billing_type', "0");
            hidden('unit', "");
            hidden('rate', "");
            hidden('max_quantity', "");
        }
        else{
            project_billing_types_list_row("Billing Type", "billing_type", null);
            stock_units_list_row(_("Units:"), 'unit', null);
            amount_row(_("Rate:"), 'rate');
            amount_row(_("Maximum Quantity:"), 'max_quantity');
        }
        textarea_row(_("Long Description:"), 'long_description', null, 31, 8);
        yesno_list_row("Print Long Description", 'print_long_description', null, "Yes", "No", false);
        if(get_post('total_only') == 0){
            tax_groups_list_row("Tax Group:", 'tax_group_id', null, ('Select a tax group'));
        }
        else{
            hidden('tax_group_id', "0");
        }
        project_invoicing_item_gropus_list_row("Parent:", 'parent_id', $_POST['project_id'], null, 'Select the Parent', false);
    }
    else{
        hidden('total_only', "0");
        hidden('deduction', "0");
        hidden('code', "");
        textarea_row(_("Description:"), 'description', null, 31, 3);
        hidden('billing_type', "0");
        hidden('unit', "");
        hidden('rate', "");
        hidden('max_quantity', "");
        hidden('long_description', "0");
        hidden('print_long_description', "0");
        hidden('tax_group_id', "0");
        hidden('code', "");

        project_invoicing_item_gropus_list_row("Parent:", 'parent_id', $_POST['project_id'], null, 'Select the Parent', false);
    }
   
    end_table(1);

    submit_add_or_update_center($selected_id == -1, '', 'both');

div_end();

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


?>
