<?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_order_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_to_order_ui.inc");
include_once($path_to_root . "/sales/includes/ui/sales_order_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['NewQuoteToSalesOrder']) && is_numeric($_GET['NewQuoteToSalesOrder'])) {
    $so_no = $_GET['NewQuoteToSalesOrder'];
    set_session_variable("ST_SALESQUOTE_TO_SALESORDER_no", $so_no);
}

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


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

page(_($help_context = "Convert Sales Quotation #".$so_no), false, false, "", $js);
//-----------------------------------------------------------------------------
simple_page_mode(true);


if (isset($_GET['NewSO'])) {
	$order_no = $_GET['NewSO'];
        
	display_notification_centered(sprintf( _("Order # %d has been created."),$order_no));
        
        submenu_view(_("&View this order"), ST_SALESORDER, $order_no);
        
        submenu_option(_("Convert another sales quotation"),"/sales/inquiry/sales_orders_view.php?type=".ST_SALESQUOTE);

        submenu_option(_("Enter a &New sales quotation"),	"/sales/sales_order_entry.php?NewOrder=0");        

	set_focus('prtopt');


	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['Process_Quote_To_Order'])){
	process_sales_quotation_to_order($so_no);
}


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

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');
$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);


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

    $Ajax->activate('soe_buttons');
    div_start('soe_buttons');   
	if ($so_no > 0) {            
            submit_center_first('Process_Quote_To_Order', 'Create Sales Order',  '', 'default');             
	} 
    div_end();

br(2);

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

end_form();
end_page();
?>