Payment_terms table normalization.
[fa-stable.git] / sales / sales_order_entry.php
index aba35ebd108e82f151afb3f69e265ac0702c1899..663bcee718b6bc2d2e720c924a6d6be47fa38890 100644 (file)
@@ -63,12 +63,16 @@ if (isset($_GET['NewDelivery']) && is_numeric($_GET['NewDelivery'])) {
 
        $_SESSION['page_title'] = _($help_context = "Direct Sales Delivery");
        create_cart(ST_CUSTDELIVERY, $_GET['NewDelivery']);
-
 } elseif (isset($_GET['NewInvoice']) && is_numeric($_GET['NewInvoice'])) {
 
-       $_SESSION['page_title'] = _($help_context = "Direct Sales Invoice");
        create_cart(ST_SALESINVOICE, $_GET['NewInvoice']);
 
+       if (isset($_GET['FixedAsset'])) {
+               $_SESSION['page_title'] = _($help_context = "Fixed Assets Sale");
+               $_SESSION['Items']->fixed_asset = true;
+       } else
+               $_SESSION['page_title'] = _($help_context = "Direct Sales Invoice");
+
 } elseif (isset($_GET['ModifyOrderNumber']) && is_numeric($_GET['ModifyOrderNumber'])) {
 
        $help_context = 'Modifying Sales Order';
@@ -96,6 +100,11 @@ if (isset($_GET['NewDelivery']) && is_numeric($_GET['NewDelivery'])) {
 
 page($_SESSION['page_title'], false, false, "", $js);
 
+if (isset($_GET['ModifyOrderNumber']) && is_prepaid_order_open($_GET['ModifyOrderNumber']))
+{
+       display_error(_("This order cannot be edited because there are invoices or payments related to it, and prepayment terms were used."));
+       end_page(); exit;
+}
 if (isset($_GET['ModifyOrderNumber']))
        check_is_editable(ST_SALESORDER, $_GET['ModifyOrderNumber']);
 elseif (isset($_GET['ModifyQuotationNumber']))
@@ -248,24 +257,25 @@ function copy_to_cart()
 {
        $cart = &$_SESSION['Items'];
 
+
        $cart->reference = $_POST['ref'];
 
        $cart->Comments =  $_POST['Comments'];
 
        $cart->document_date = $_POST['OrderDate'];
 
-       $newpayment = false;
 
+       $newpayment = false;
        if (isset($_POST['payment']) && ($cart->payment != $_POST['payment'])) {
                $cart->payment = $_POST['payment'];
                $cart->payment_terms = get_payment_terms($_POST['payment']);
                $newpayment = true;
        }
-       if ($cart->payment_terms['cash_sale']) {
-               if ($newpayment) {
+       if ($cart->payment_terms['type'] == PTT_CASH) {
+               if ($newpayment) 
+               {
                        $cart->due_date = $cart->document_date;
                        $cart->phone = $cart->cust_ref = $cart->delivery_address = '';
-                       $cart->ship_via = 0;
                        $cart->deliver_to = '';
                        $cart->prep_amount = 0;
                }
@@ -275,10 +285,11 @@ function copy_to_cart()
                $cart->deliver_to = $_POST['deliver_to'];
                $cart->delivery_address = $_POST['delivery_address'];
                $cart->phone = $_POST['phone'];
-               $cart->ship_via = $_POST['ship_via'];
                if (!$cart->trans_no || ($cart->trans_type == ST_SALESORDER && !$cart->is_started()))
                        $cart->prep_amount = input_num('prep_amount', 0);
        }
+
+       $cart->ship_via = $_POST['ship_via'];
        $cart->Location = $_POST['Location'];
        $cart->freight_cost = input_num('freight_cost');
        if (isset($_POST['email']))
@@ -379,8 +390,8 @@ function can_process() {
                display_error(_("This document cannot be processed because there is insufficient quantity for items marked."));
                return false;
        }
-       if ($_SESSION['Items']->payment_terms['cash_sale'] == 0) {
-               if (!$_SESSION['Items']->is_started() && ($_SESSION['Items']->payment_terms['days_before_due'] < 0) && ((input_num('prep_amount')<=0) ||
+       if ($_SESSION['Items']->payment_terms['type'] != PTT_CASH) {
+               if (!$_SESSION['Items']->is_started() && ($_SESSION['Items']->payment_terms['type'] == PTT_PREPAY) && ((input_num('prep_amount')<=0) ||
                        input_num('prep_amount')>$_SESSION['Items']->get_trans_total())) {
                        display_error(_("Pre-payment required have to be positive and less than total amount."));
                        set_focus('prep_amount');
@@ -398,10 +409,7 @@ function can_process() {
                        return false;
                }
 
-               if ($_POST['freight_cost'] == "")
-                       $_POST['freight_cost'] = price_format(0);
-
-               if (!check_num('freight_cost',0)) {
+               if (isset($_POST['freight_cost']) && !check_num('freight_cost',0)) {
                        display_error(_("The shipping cost entered is expected to be numeric."));
                        set_focus('freight_cost');
                        return false;
@@ -443,6 +451,10 @@ function can_process() {
                display_error("Invoice total amount cannot be less than zero.");
                return false;
        }
+
+       if ($_SESSION['Items']->payment_terms['type'] == PTT_CASH && 
+               ($_SESSION['Items']->trans_type == ST_CUSTDELIVERY || $_SESSION['Items']->trans_type == ST_SALESINVOICE)) 
+               $_SESSION['Items']->due_date = $_SESSION['Items']->document_date;
        return true;
 }
 
@@ -458,17 +470,16 @@ if (isset($_POST['ProcessOrder']) && can_process()) {
        $modified = ($_SESSION['Items']->trans_no != 0);
        $so_type = $_SESSION['Items']->so_type;
 
-       $ret = $_SESSION['Items']->write(1);
+       $ret = write_sales_trans($_SESSION['Items'], 1);
        if ($ret == -1)
        {
                display_error(_("The entered reference is already in use."));
                $ref = $Refs->get_next($_SESSION['Items']->trans_type, null, array('date' => Today()));
                if ($ref != $_SESSION['Items']->reference)
                {
+                       unset($_POST['ref']); // force refresh reference
                        display_error(_("The reference number field has been increased. Please save the document again."));
-                       $_POST['ref'] = $_SESSION['Items']->reference = $ref;
-                       $Ajax->activate('ref');
-               }       
+               }
                set_focus('ref');
        }
        else
@@ -526,7 +537,7 @@ function check_item_data()
                return false;
        }
 
-       $cost_home = get_standard_cost(get_post('stock_id')); // Added 2011-03-27 Joe Hunt
+       $cost_home = get_unit_cost(get_post('stock_id')); // Added 2011-03-27 Joe Hunt
        $cost = $cost_home / get_exchange_rate_from_home_currency($_SESSION['Items']->customer_currency, $_SESSION['Items']->document_date);
        if (input_num('price') < $cost)
        {
@@ -688,7 +699,10 @@ if (isset($_POST['CancelItemChanges'])) {
 }
 
 //--------------------------------------------------------------------------------
-check_db_has_stock_items(_("There are no inventory items defined in the system."));
+if ($_SESSION['Items']->fixed_asset)
+       check_db_has_disposable_fixed_assets(_("There are no fixed assets defined in the system."));
+else
+       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."));