Merging changes up to 2.0.4
[fa-stable.git] / sales / includes / ui / sales_order_ui.inc
index bf08381ce1949b836e7eff5dffa8d8f7ae1f2142..2b2b028465ea5d045677f52ec424a38fa8f3bd29 100644 (file)
@@ -47,7 +47,7 @@ function get_customer_details_to_order(&$order, $customer_id, $branch_id)
                if (db_num_rows($result) == 0)
                {
                return _("The selected customer and branch are not valid, or the customer does not have any branches.");
-               } 
+               }
 
                $myrow = db_fetch($result);
 
@@ -62,6 +62,8 @@ function get_customer_details_to_order(&$order, $customer_id, $branch_id)
                $order->set_location($myrow["default_location"], $myrow["location_name"]);
                $order->set_delivery($myrow["default_ship_via"], $myrow["br_name"],
                $address);
+               if ($order->trans_type == 10)
+                       $order->due_date = get_invoice_duedate($customer_id, $order->document_date);
        }
 
        return $ret_error;
@@ -108,7 +110,7 @@ function display_order_summary($title, &$order, $editable_items=false)
                        view_stock_status_cell($stock_item->stock_id);
 
                        label_cell($stock_item->item_description, "nowrap" );
-                       qty_cell($stock_item->qty_dispatched);
+                       qty_cell($stock_item->qty_dispatched, false, get_qty_dec($stock_item->stock_id));
 
                        if ($order->trans_no!=0)
                                amount_cell($stock_item->qty_done);
@@ -132,7 +134,7 @@ function display_order_summary($title, &$order, $editable_items=false)
                {
                        sales_order_item_controls($order, $k,  $line_no);
                }
-               
+
                $total += $line_total;
        }
 
@@ -159,8 +161,6 @@ function display_order_header(&$order, $editable, $date_text, $display_tax_group
 
        $customer_error = "";
        $change_prices = 0;
-       $newcust = ($order->customer_id != get_post('customer_id', -1));
-       $newbranch = ($order->Branch != get_post('branch_id', -1)) || $newcust;
 
        if (isset($order) && !$editable)
        {
@@ -174,16 +174,18 @@ function display_order_header(&$order, $editable, $date_text, $display_tax_group
        {
                customer_list_row(_("Customer:"), 'customer_id', null, false, true);
 
-               if ($newcust)
+               if ($order->customer_id != get_post('customer_id', -1))
                {
                        // customer has changed
-                       // clear the branch selection
-                       $_POST['branch_id'] = '';
                        $Ajax->activate('branch_id');
                }
-               customer_branches_list_row(_("Branch:"), 
+               customer_branches_list_row(_("Branch:"),
                  $_POST['customer_id'], 'branch_id', null, false, true, true);
 
+       if( ($order->customer_id != get_post('customer_id', -1)) ||
+                       ($order->Branch != get_post('branch_id', -1)) ||
+                       list_updated('customer_id')) {
+
                if (!isset($_POST['branch_id']) || $_POST['branch_id'] == "")
                {
                        // ignore errors on customer search box call
@@ -195,7 +197,7 @@ function display_order_header(&$order, $editable, $date_text, $display_tax_group
                    $order->Branch = 0;
                } else
                {
-                       if($newcust || $newbranch) {
+
                                $old_order = (PHP_VERSION<5) ? $order : clone( $order );
                                $customer_error = get_customer_details_to_order($order, $_POST['customer_id'], $_POST['branch_id']);
 
@@ -203,6 +205,11 @@ 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 ($order->trans_type == 10)
+                               {
+                                       $_POST['delivery_date'] = $order->due_date;
+                                       $Ajax->activate('delivery_date');
+                               }
                                $Ajax->activate('Location');
                                $Ajax->activate('deliver_to');
                                $Ajax->activate('phone');
@@ -220,8 +227,8 @@ function display_order_header(&$order, $editable, $date_text, $display_tax_group
                                }
                                unset($old_order);
                        }
-               } // changed branch
                set_global_customer($_POST['customer_id']);
+               } // changed branch
        }
 
        if ($_SESSION['Items']->trans_type!=30) {
@@ -238,7 +245,7 @@ function display_order_header(&$order, $editable, $date_text, $display_tax_group
                echo "<table height='5'>";
                label_row(_("Customer Currency:"), $order->customer_currency);
                exchange_rate_display($order->customer_currency, get_company_currency(),
-                       ($editable ? $_POST['OrderDate'] : $order->document_date), $editable);
+                       ($editable ? $_POST['OrderDate'] : $order->document_date));
                echo "</table>";
            div_end();
                echo "</td><td>"; // outer table
@@ -254,20 +261,11 @@ function display_order_header(&$order, $editable, $date_text, $display_tax_group
        if ($order->sales_type != $_POST['sales_type']) {
                $myrow = get_sales_type($_POST['sales_type']);
                $order->set_sales_type($myrow['id'], $myrow['sales_type'],
-               $myrow['tax_included'], $myrow['factor']);
+                       $myrow['tax_included'], $myrow['factor']);
                $Ajax->activate('sales_type');
                $change_prices = 1;
        }
 
-       if ($change_prices != 0) {
-               foreach ($order->line_items as $line_no=>$item) {
-                       $line = &$order->line_items[$line_no];
-                       $line->price = get_price($line->stock_id, $order->customer_currency,
-                               $order->sales_type, $order->price_factor, $order->document_date);
-               //              $line->discount_percent = $order->default_discount;
-               }
-           $Ajax->activate('items_table');
-       }
        label_row(_("Customer Discount:"), ($order->default_discount * 100) . "%");
        echo "</table>";
 
@@ -281,7 +279,16 @@ function display_order_header(&$order, $editable, $date_text, $display_tax_group
                        $_POST['OrderDate'] = $order->document_date;
 
                date_row($date_text, 'OrderDate',
-                 _('Date of order receive'));
+                 _('Date of order receive'), null, 0, 0, 0, null, true);
+               if (isset($_POST['_OrderDate_changed'])) {
+                       $change_prices = 1;
+                       $Ajax->activate('currency');
+                       if ($order->trans_type == 10) {
+                               $_POST['delivery_date'] = get_invoice_duedate(get_post('customer_id'), get_post('OrderDate'));
+                       } else 
+                               $_POST['delivery_date'] = add_days(get_post('OrderDate'), sys_prefs::default_delivery_required_by());
+                       $Ajax->activate('delivery_date');
+               }
        }
        else
        {
@@ -300,6 +307,16 @@ function display_order_header(&$order, $editable, $date_text, $display_tax_group
 
        end_table(1); // outer table
 
+       if ($change_prices != 0) {
+               foreach ($order->line_items as $line_no=>$item) {
+                       $line = &$order->line_items[$line_no];
+                       $line->price = get_price($line->stock_id, $order->customer_currency,
+                               $order->sales_type, $order->price_factor, get_post('OrderDate'));
+               //              $line->discount_percent = $order->default_discount;
+               }
+           $Ajax->activate('items_table');
+       }
+
        return $customer_error;
 }
 
@@ -315,7 +332,8 @@ function sales_order_item_controls(&$order, &$rowcounter, $line_no=-1)
        if ($line_no!=-1 && $line_no == $id)
        {
                $_POST['stock_id'] = $order->line_items[$id]->stock_id;
-               $_POST['qty'] = qty_format($order->line_items[$id]->qty_dispatched);
+               $dec = get_qty_dec($_POST['stock_id']);
+               $_POST['qty'] = number_format2($order->line_items[$id]->qty_dispatched, $dec);
                $_POST['price'] = price_format($order->line_items[$id]->price);
                $_POST['Disc'] = percent_format($order->line_items[$id]->discount_percent*100);
                $units = $order->line_items[$id]->units;
@@ -327,38 +345,39 @@ function sales_order_item_controls(&$order, &$rowcounter, $line_no=-1)
        else
        {
                stock_items_list_cells(null,'stock_id', null, false, true);
-               if(isset($_POST['_stock_id_update'])) {
+               if (list_updated('stock_id')) {
                            $Ajax->activate('price');
                            $Ajax->activate('units');
+                           $Ajax->activate('qty');
                            $Ajax->activate('line_total');
                }
 
                $item_info = get_item_edit_info($_POST['stock_id']);
                $units = $item_info["units"];
-
-               $_POST['qty'] = qty_format(1);
+               $dec = get_qty_dec($_POST['stock_id']);
+               $_POST['qty'] = number_format2(1, $dec);
 
                $_POST['price'] = price_format(get_price ($_POST['stock_id'],
-                       $order->customer_currency, $order->sales_type, 
-                       $order->price_factor, $order->document_date));
+                       $order->customer_currency, $order->sales_type,
+                       $order->price_factor, get_post('OrderDate')));
                // default to the customer's discount %
                $_POST['Disc'] = percent_format($order->default_discount * 100);
 
        }
 
-       qty_cells(null, 'qty', qty_format($_POST['qty']));
+       qty_cells(null, 'qty', $_POST['qty'], null, null, $dec);
 
        if ($order->trans_no!=0) {
-               amount_cell($line_no==-1 ? 0 :$order->line_items[$line_no]->qty_done);
+               qty_cell($line_no==-1 ? 0 :$order->line_items[$line_no]->qty_done, false, $dec);
        }
 
        label_cell($units, '', 'units');
 
        $str = amount_cells(null, 'price');
-       
+
        small_amount_cells(null, 'Disc', percent_format($_POST['Disc']), null, null, user_percent_dec());
 
-       $line_total = $_POST['qty'] * $_POST['price'] * (1 - $_POST['Disc'] / 100);
+       $line_total = input_num('qty') * input_num('price') * (1 - input_num('Disc') / 100);
 
        amount_cell($line_total, false, '','line_total');
 
@@ -370,11 +389,10 @@ function sales_order_item_controls(&$order, &$rowcounter, $line_no=-1)
                                _('Cancel changes'));
                hidden('LineNo', $line_no);
                set_focus('qty');
-               $Ajax->addFocus(true, 'qty');
        }
        else
        {
-               submit_cells('AddItem', _("Add Item"), "colspan=2", 
+               submit_cells('AddItem', _("Add Item"), "colspan=2",
                    _('Add new item to document'), true);
        }
 
@@ -410,7 +428,7 @@ function display_delivery_details(&$order)
 
        locations_list_row(_("Deliver from Location:"), 'Location', $order->Location);
 
-       date_row($delname, 'delivery_date', 
+       date_row($delname, 'delivery_date',
            _('Enter requested day of delivery'), $order->due_date, 0, 0, 0);
 
        text_row(_("Deliver To:"), 'deliver_to', $order->deliver_to, 40, 40,