Small cleanup to branch name length in input form and printouts.
[fa-stable.git] / sales / includes / ui / sales_order_ui.inc
index 9c6925f46246d6c7f9897651dc1a2e30a808e59d..d60431bf74bf680a292f4b47ed9920666326a924 100644 (file)
@@ -72,6 +72,8 @@ function add_to_order(&$order, $new_item, $new_item_qty, $price, $discount, $des
 
 function get_customer_details_to_order(&$order, $customer_id, $branch_id)
 {
+       global $SysPrefs;
+       
        $ret_error = "";
 
        $myrow = get_customer_to_order($customer_id);
@@ -90,6 +92,8 @@ function get_customer_details_to_order(&$order, $customer_id, $branch_id)
        $order->set_sales_type($myrow['salestype'], $myrow['sales_type'], $myrow['tax_included'],
            $myrow['factor']);
 
+       $order->credit = $myrow['cur_credit'];
+
        if ($order->trans_type != ST_SALESORDER && $order->trans_type != ST_SALESQUOTE)
        {
                $order->dimension_id = $myrow['dimension_id'];
@@ -104,8 +108,10 @@ function get_customer_details_to_order(&$order, $customer_id, $branch_id)
 
        $myrow = db_fetch($result);
 
+       // FIX - implement editable contact selector in sales order 
+       $contact = get_branch_contacts($branch_id, 'order', $customer_id);
        $order->set_branch($branch_id, $myrow["tax_group_id"],
-       $myrow["tax_group_name"], $myrow["phone"], $myrow["email"]);
+       $myrow["tax_group_name"], @$contact["phone"], @$contact["email"]);
 
        $address = trim($myrow["br_post_address"]) != '' ? $myrow["br_post_address"]
                : (trim($myrow["br_address"]) != '' ? $myrow["br_address"]:$deliver);
@@ -114,17 +120,17 @@ function get_customer_details_to_order(&$order, $customer_id, $branch_id)
                $address);
        if ($order->trans_type == ST_SALESINVOICE) {
                $order->due_date = get_invoice_duedate($order->payment, $order->document_date);
-//             if ($order->pos != -1)
-//                     $order->cash = date_diff2($order->due_date, Today(), 'd')<2;
        }
-       if($order->cash ) {
-               if($order->pos != -1) {
-                       $pos = get_sales_point($order->pos);
-                       $order->set_location($pos["pos_location"], $pos["location_name"]);
-               }
+       elseif ($order->trans_type == ST_SALESORDER)
+               $order->due_date = add_days($order->document_date, $SysPrefs->default_delivery_required_by());
+       elseif ($order->trans_type == ST_SALESQUOTE)
+               $order->due_date = add_days($order->document_date, $SysPrefs->default_quote_valid_days());
+
+       if($order->payment_terms['cash_sale']) {
+               $order->set_location($order->pos["pos_location"], $order->pos["location_name"]);
        } else
                $order->set_location($myrow["default_location"], $myrow["location_name"]);
-               
+
        return $ret_error;
 }
 
@@ -132,15 +138,14 @@ function get_customer_details_to_order(&$order, $customer_id, $branch_id)
 
 function display_order_summary($title, &$order, $editable_items=false)
 {
-       global $table_style, $path_to_root, $SysPrefs;
 
        display_heading($title);
 
     div_start('items_table');
-       start_table("$table_style colspan=7 width=90%");
+       start_table(TABLESTYLE, "colspan=7 width=90%");
        $th = array(_("Item Code"), _("Item Description"), _("Quantity"),
                _("Delivered"),
-               _("Unit"), _("Price"), _("Discount %"), _("Total"), "");
+               _("Unit"), $order->tax_included ? _("Price after Tax") : _("Price before Tax"), _("Discount %"), _("Total"), "");
 
        if ($order->trans_no == 0) {
        unset( $th[3] );
@@ -155,8 +160,9 @@ function display_order_summary($title, &$order, $editable_items=false)
        $k = 0;  //row colour counter
 
        $id = find_submit('Edit');
-       $has_marked = false;
-       foreach ($order->line_items as $line_no=>$stock_item)
+
+       $low_stock = $order->check_qoh($_POST['OrderDate'], $_POST['Location']);
+       foreach ($order->get_items() as $line_no=>$stock_item)
        {
 
                $line_total = round($stock_item->qty_dispatched * $stock_item->price * (1 - $stock_item->discount_percent),
@@ -165,22 +171,10 @@ function display_order_summary($title, &$order, $editable_items=false)
                $qoh_msg = '';
                if (!$editable_items || $id != $line_no)
                {
-                       if (!$SysPrefs->allow_negative_stock() && is_inventory_item($stock_item->stock_id)) {
-                               $qoh = get_qoh_on_date($stock_item->stock_id, 
-                                       $_POST['Location'], $_POST['OrderDate']);
-                               if ($stock_item->qty_dispatched > $qoh) 
-                               {
-                                       // oops, we don't have enough of one of the component items
-                                       start_row("class='stockmankobg'");
-                                       $qoh_msg .= $stock_item->stock_id . " - " . $stock_item->item_description . ": " .
-                                       _("Quantity On Hand") . " = " 
-                                       . number_format2($qoh, get_qty_dec($stock_item->stock_id)) . '<br>';
-                                       $has_marked = true;
-                                } else 
-                                       alt_table_row_color($k);
-                       } else {
+                       if (in_array($stock_item->stock_id, $low_stock))
+                               start_row("class='stockmankobg'");      // notice low stock status
+                       else 
                                alt_table_row_color($k);
-                       }
 
                        view_stock_status_cell($stock_item->stock_id);
 
@@ -235,26 +229,23 @@ function display_order_summary($title, &$order, $editable_items=false)
 
        start_row();
        label_cells(_("Amount Total"), $display_total, "colspan=$colspan align=right","align=right");
-       submit_cells('update', _("Update"), "colspan=2", _("Refresh"), true);
+       submit_cells('update', _("Update"), "colspan=2 align='center'", _("Refresh"), true);
        end_row();
 
        end_table();
-       if ($has_marked) {
+       if ($low_stock)
                display_note(_("Marked items have insufficient quantities in stock as on day of delivery."), 0, 1, "class='stockmankofg'");
-               if ($order->trans_type!=30 && !$SysPrefs->allow_negative_stock())
-                       display_error(_("The delivery cannot be processed because there is an insufficient quantity for item:")
-                               . '<br>'. $qoh_msg);
-       }
+
     div_end();
 }
 
 // ------------------------------------------------------------------------------
 
-function display_order_header(&$order, $editable, $date_text, $display_tax_group=false)
+function display_order_header(&$order, $editable, $date_text)
 {
-       global $table_style2, $Ajax, $SysPrefs;
+       global $Ajax, $SysPrefs;
 
-       start_outer_table("width=80% $table_style2");
+       start_outer_table(TABLESTYLE2, "width=80%");
 
        table_section(1);
        
@@ -311,7 +302,20 @@ function display_order_header(&$order, $editable, $date_text, $display_tax_group
                                $_POST['deliver_to'] = $order->deliver_to;
                                $_POST['delivery_address'] = $order->delivery_address;
                                $_POST['phone'] = $order->phone;
-                               if (get_post('payment') !== $order->payment) {
+                               $_POST['delivery_date'] = $order->due_date;
+
+                               if (!in_array($order->trans_type, array(ST_SALESQUOTE, ST_SALESORDER))
+                                       && ($order->pos['cash_sale'] != $order->pos['credit_sale'])
+                                       && (($order->payment_terms['cash_sale'] && !$order->pos['cash_sale']) ||
+                                               (!$order->payment_terms['cash_sale'] && !$order->pos['credit_sale']))) {
+                                                       // force payment terms refresh if terms are editable
+                                                       // and pos have no permitions for terms selected in customer record.
+                                                       // Terms are set to first terms in allowed category below.
+                                                       display_warning(
+                                                               sprintf(_("Customer's payment terms '%s' cannot be selected on this POS"),
+                                                                       $order->payment_terms['terms']));
+                                                       $order->payment = '';
+                               } elseif (get_post('payment') !== $order->payment) {
                                        $_POST['payment'] = $order->payment;
                                        $Ajax->activate('delivery');
                                        $Ajax->activate('payment');
@@ -354,44 +358,50 @@ function display_order_header(&$order, $editable, $date_text, $display_tax_group
                        $row = get_customer_to_order($_POST['customer_id']);
                        if ($row['dissallow_invoices'] == 1)
                                $customer_error = _("The selected customer account is currently on hold. Please contact the credit control personnel to discuss.");
-               
                }
        }
 
        ref_row(_("Reference").':', 'ref', _('Reference number unique for this document type'), null, '');
 
-       if (!is_company_currency($order->customer_currency))
-       {
-           table_section(2);
+       table_section(2);
 
+       if (!is_company_currency($order->customer_currency) && in_array($order->trans_type, array(ST_SALESINVOICE, ST_CUSTDELIVERY)))
+       {
                label_row(_("Customer Currency:"), $order->customer_currency);
-               exchange_rate_display($order->customer_currency, get_company_currency(),
+               exchange_rate_display(get_company_currency(), $order->customer_currency,
                        ($editable ? $_POST['OrderDate'] : $order->document_date));
        }
-       table_section(3);
 
-       if (($order->trans_type != ST_CUSTDELIVERY) && ($order->pos != -1)) { // editable payment type
+       customer_credit_row($_POST['customer_id'], $order->credit);
+
+       label_row(_("Customer Discount:"), ($order->default_discount * 100) . "%");
+
+       table_section(3);
+       if (($order->pos['cash_sale'] || $order->pos['credit_sale']) 
+               && !$order->is_started()) {
+                // editable payment type 
                if (get_post('payment') !== $order->payment) {
                        $order->payment = get_post('payment');
                        $order->payment_terms = get_payment_terms($order->payment);
-                       $order->cash = $order->payment_terms['cash_sale'];
-                       if ($order->cash) {
-                               $paym = get_sales_point(user_pos());
-                               $order->cash_account = $paym['pos_account'];
-                               $order->account_name = $paym['bank_account_name'];
-                               $_POST['Location'] = $order->Location = $paym['pos_location'];
-                               $order->location_name = $paym['location_name'];
+                       $order->due_date = get_invoice_duedate($order->payment, $order->document_date);
+                       if ($order->payment_terms['cash_sale']) {
+                               $_POST['Location'] = $order->Location = $order->pos['pos_location'];
+                               $order->location_name = $order->pos['location_name'];
                        }
                        $Ajax->activate('items_table');
                        $Ajax->activate('delivery');
-                       set_focus($order->pos == -1 ? 'delivery_date' : 'account');
                }
-               sale_payment_list_cells(_('Payment:'), 'payment', null);
+               $paymcat = !$order->pos['cash_sale'] ? PM_CREDIT : 
+                       (!$order->pos['credit_sale'] ? PM_CASH : PM_ANY);
+               // all terms are available for SO
+               $is_order = in_array($order->trans_type, array(ST_SALESQUOTE, ST_SALESORDER));
+               sale_payment_list_cells(_('Payment:'), 'payment', $is_order ? PM_ANY : $paymcat, null, true, $is_order);
        } else {
-               label_cells(_('Payment:'), $order->payment_terms['terms']);
+               label_cells(_('Payment:'), $order->payment_terms['terms'], "class='label'");
        }
+
        if($editable) {
-               $str = sales_types_list_row(_("Price List"), 'sales_type', null, true);
+               $str = sales_types_list_row(_("Price List:"), 'sales_type', null, true);
        } else {
                label_row(_("Price List:"), $order->sales_type_name);
        }
@@ -403,8 +413,6 @@ function display_order_header(&$order, $editable, $date_text, $display_tax_group
                $change_prices = 1;
        }
 
-       label_row(_("Customer Discount:"), ($order->default_discount * 100) . "%");
-       
        table_section(4);
 
        if ($editable)
@@ -448,12 +456,6 @@ function display_order_header(&$order, $editable, $date_text, $display_tax_group
                hidden('OrderDate', $order->document_date);
        }
 
-       if ($display_tax_group)
-       {
-           label_row(_("Tax Group:"), $order->tax_group_name);
-           hidden('tax_group_id', $order->tax_group_id);
-       }
-
        end_outer_table(1); // outer table
 
        if ($change_prices != 0) {
@@ -503,7 +505,7 @@ function sales_order_item_controls(&$order, &$rowcounter, $line_no=-1)
        }
        else    // prepare new line
        {
-               sales_items_list_cells(null,'stock_id', null, false, true);
+               sales_items_list_cells(null,'stock_id', null, false, true, true);
                if (list_updated('stock_id')) {
                            $Ajax->activate('price');
                            $Ajax->activate('units');
@@ -550,7 +552,7 @@ function sales_order_item_controls(&$order, &$rowcounter, $line_no=-1)
        }
        else
        {
-               submit_cells('AddItem', _("Add Item"), "colspan=2",
+               submit_cells('AddItem', _("Add Item"), "colspan=2 align='center'",
                    _('Add new item to document'), true);
        }
 
@@ -561,20 +563,23 @@ function sales_order_item_controls(&$order, &$rowcounter, $line_no=-1)
 
 function display_delivery_details(&$order)
 {
-       global $table_style2, $Ajax;
+       global $Ajax;
 
        div_start('delivery');  
 
-       if ($order->cash) {     // Direct payment sale
+
+       if ($order->payment_terms['cash_sale']) {       // Direct payment sale
                $Ajax->activate('items_table');
                display_heading(_('Cash payment'));
-               start_table("$table_style2 width=60%");
-               label_row(_("Deliver from Location:"), $order->location_name);
+               start_table(TABLESTYLE2, "width=60%");
 
-               hidden('Location', $order->Location);
-               label_row(_("Cash account:"), $order->account_name);
+               locations_list_row(_("Deliver from Location:"), 'Location', null, false, true);
+               if (list_updated('Location'))
+                       $Ajax->activate('items_table');
+               label_row(_("Cash account:"), $order->pos['bank_account_name']);
                textarea_row(_("Comments:"), "Comments", $order->Comments, 31, 5);
                end_table();
+               hidden('delivery_date', $order->due_date);
        } else {
                if ($order->trans_type==ST_SALESINVOICE)
                {
@@ -597,11 +602,18 @@ function display_delivery_details(&$order)
                        $delname = _("Required Delivery Date").':';
                }
                display_heading($title);
-               start_outer_table("$table_style2 width=90%");
+               start_outer_table(TABLESTYLE2, "width=90%");
                table_section(1);
 
-               locations_list_row(_("Deliver from Location:"), 'Location', null, 
-                       false, true);
+               locations_list_row(_("Deliver from Location:"), 'Location', null, false, true);
+               if ($order->payment_terms['days_before_due'] < 0)
+               {
+                       $Ajax->addUpdate('items_table', 'prep_amount', price_format($order->get_trans_total())); // bind to items_table update
+                       if (!$order->is_started())
+                               amount_row(_("Pre-Payment Required:"), 'prep_amount');
+                       else
+                               label_row(_("Pre-Payment Required:"), price_format($order->prep_amount), "class='label'");
+               }
                if (list_updated('Location'))
                        $Ajax->activate('items_table');
 
@@ -609,7 +621,7 @@ function display_delivery_details(&$order)
                date_row($delname, 'delivery_date',
                        $order->trans_type==ST_SALESORDER ?  _('Enter requested day of delivery') 
                                : $order->trans_type==ST_SALESQUOTE ? _('Enter Valid until Date') : '');
-               text_row(_("Deliver To:"), 'deliver_to', $order->deliver_to, 40, 40,
+               text_row(_("Deliver To:"), 'deliver_to', $order->deliver_to, 50, 60,
                        _('Additional identifier for delivery e.g. name of receiving person'));
 
                textarea_row(_("Address:"), 'delivery_address', $order->delivery_address, 35, 5,
@@ -630,4 +642,3 @@ function display_delivery_details(&$order)
        div_end();
 }
 
-?>
\ No newline at end of file