<?php
/**********************************************************************
***********************************************************************/
//-----------------------------------------------------------------------------
//
//	Entry/Modify Sales Quotations
//	Entry/Modify Sales Order
//	Entry Direct Delivery
//	Entry Direct Invoice
//

$path_to_root = "..";
$page_security = 'SA_SALESQUOTE';

include_once($path_to_root . "/sales/includes/display_sales_quotation_class.inc"); 
include_once($path_to_root . "/sales/includes/save_sales_quotation_class.inc"); 
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/sales/includes/sales_ui.inc");
include_once($path_to_root . "/sales/includes/ui/sales_quotation_entry_ui.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 . "/reporting/includes/reporting.inc");
include_once($path_to_root . "/inventory/includes/db/alternate_units_db.inc");
include_once($path_to_root . "/includes/approval_functions.inc");

set_page_security( @$_SESSION['Items']->trans_type,
	array(ST_SALESQUOTE=>'SA_SALESQUOTE'),
	array(	'NewQuotation' => 'SA_SALESQUOTE',
			'ModifyOrderNumber' => 'SA_SALESQUOTE',
			'AddedID' => 'SA_SALESQUOTE',
			'UpdatedID' => 'SA_SALESQUOTE',
			)
);

$js = '';

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

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

$so_no = -1;
$so = null;

if (isset($_GET['ModifyOrderNumber']) && is_numeric($_GET['ModifyOrderNumber'])) {
    $so_no = $_GET['ModifyOrderNumber'];
    set_session_variable("ST_SALESQUOTE_no", $so_no);
}

if (isset($_GET['NewQuotation']) && strlen($_GET['NewQuotation']) > 2) {
    $so_no = -1;
    set_session_variable("ST_SALESQUOTE_no", $so_no);
}

if(get_session_variable("ST_SALESQUOTE_no") > 0){
    $so_no = get_session_variable("ST_SALESQUOTE_no");
}


//page($_SESSION['page_title'], false, false, "", $js);


$help_context = $so_no > 0 ? "Edit Sales Quotation #$so_no" : "New Sales Quotation";

page(_($help_context), false, false, "", $js);
//-----------------------------------------------------------------------------
simple_page_mode(true);


if (isset($_GET['ApprovedSO'])) {
	$order_no = $_GET['ApprovedSO'];
	display_notification_centered(sprintf( _("Order # %d has been approved."),$order_no));
        
        submenu_option(_("Approve another order"),"/sales/inquiry/sales_quotations_view.php?ApproveOrdersMode=Yes");

        submenu_option(_("Enter a &New Order"),	"/sales/sales_quotation_entry.php?NewQuotation=0");
        
        submenu_option(_("Select A Different &Order"),"/sales/inquiry/sales_quotations_view.php?OutstandingOnly=1");

	submenu_view(_("&View This Order"), ST_SALESQUOTE, $order_no);

	submenu_print(_("&Print This Order"), ST_SALESQUOTE, $order_no, 'prtopt'); 
	set_focus('prtopt');

	submenu_option(_("Work &Order Entry"),	"/manufacturing/work_order_entry.php?");

	display_footer_exit();

}elseif (isset($_GET['RejectedSO'])) {
	$order_no = $_GET['RejectedSO'];

	display_notification_centered(sprintf( _("Order # %d has been rejected."),$order_no));

	submenu_view(_("&View This Order"), ST_SALESQUOTE, $order_no);

	submenu_option(_("Select A Different &Order"),
		"/sales/inquiry/sales_quotations_view.php?OutstandingOnly=1");

	display_footer_exit();
        
} 
else{
//	check_edit_conflicts();
    
}

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

function line_start_focus() {
    global $Ajax;

    $Ajax->activate('items_table');
    set_focus('_stock_id_edit');
}


if (isset($_POST['update'])) {
	copy_to_cart();
	$Ajax->activate('items_table');
}

if (isset($_POST['ProcessOrder']) || isset($_POST['ReleaseForapproval'])){
    
    release_sales_quotation_for_approval($so_no);
		
}


if (isset($_POST['RejectOrder'])){
    
   reject_sales_quotation($so_no);
		
}




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

if (isset($_POST['SaveSOHeader'])){
	handle_update_sales_quotation_header($so_no);
}

$delete_line_id = find_submit('Delete');
if ($delete_line_id!=-1){
	handle_delete_sales_quotation_item($so_no, $delete_line_id);
}

if (isset($_POST['UpdateItem'])){
	handle_update_sales_quotation_item($so_no);
}

if (isset($_POST['AddItem'])){
        handle_new_sales_quotation_item($so_no);
        $so_no = get_session_variable("ST_SALESQUOTE_no");
}

if (isset($_POST['CancelItemChanges'])) {
	line_start_focus();
}


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

start_form();

if (isset($so_no) && $so_no > 0) {
        unset($so);
	$help_context = 'Modifying Sales Order';
	$_SESSION['page_title'] = sprintf( _("Modifying Sales Order # %d"), $so_no);
	$so = create_display_sales_quotation_cart(ST_SALESQUOTE, $so_no);

} 
elseif (isset($_GET['NewQuotation'])) {
	$_SESSION['page_title'] = _($help_context = "New Sales Order Entry");
	$so = create_display_sales_quotation_cart(ST_SALESQUOTE, 0);
        $_SESSION['ST_SALESQUOTE'] = $so;
} 
else{
    $_SESSION['page_title'] = _($help_context = "New Sales Order Entry");
        if(isset($_SESSION['ST_SALESQUOTE'])){
            $so = &$_SESSION['ST_SALESQUOTE'];
        }
        else{
            $so = create_display_sales_quotation_cart(ST_SALESQUOTE, 0);            
        }
        
}

//if (list_updated('branch_id')) {
//	// when branch is selected via external editor also customer can change
//	$br = get_branch(get_post('branch_id'));
//	$_POST['customer_id'] = $br['debtor_no'];
//	$Ajax->activate('customer_id');
//}

//--------------------------------------------------------------------------------
//check_db_has_stock_items(_("There are no inventory items defined in the system."));

//check_db_has_customer_branches(_("There are no customers, or there are no customers with branches. Please define customers and customer branches."));


    $idate = _("Order Date:");
    $orderitems = _("Sales Quotation Items");
    $deliverydetails = _("Enter Delivery Details and Confirm Order");
    $cancelorder = _("Cancel Order");
    $porder = _("Save");
    $corder = _("Commit Order Changes");
    $rorder = _("Release For Approval");


$Ajax->activate('customer_id');
hidden('cart_id');

$so_no > 0 ? (display_heading("Sales Quotation #".$so_no)) : '';
br(1);

$customer_error = display_sales_quotation_header($so, true, $idate);


if($so_no > 0){
    $msg = check_sales_quotation_integrity($so_no);
    if($msg['success'] !== true){
        display_error($msg['message']);
    }
}

//customer_list_row(_("Customer:"), 'customer_id22', null, 'Select a Customer', true);
    $Ajax->activate('soe_buttons');
    div_start('soe_buttons');   
	if ($so_no > 0) {            
            submit_center_first('SaveSOHeader', $porder, '', 'default');
            br(2);
            submit_center_first('ReleaseForapproval', $rorder, '', 'default');
            submit_center_last('RejectOrder', "Reject Order",_('Reject'), 'default');
	} 
    div_end();

        br(1);
//        else {
//            submit_center_first('ProcessOrder', $corder,_('Validate changes and update document'), 'default');
//            submit('ReleaseForapproval', $rorder, true, '', 'default');
//            submit_center_last('RejectOrder', "Reject",_('Reject'), 'default');
//	}

$Ajax->activate('items_table');

if ($customer_error == "") {

        div_start('items_table');    
	display_sales_quotation_item_details($orderitems, $so, true);
        div_end();
        
	display_sales_quotation_delivery_details($so);
	


} else {
	display_error($customer_error);
}



br(2);

unset($so);
unset($cart);

end_form();
end_page();
?>