Automatic calculation of not set item prices from home currency and base price list...
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Fri, 9 May 2008 07:27:36 +0000 (07:27 +0000)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Fri, 9 May 2008 07:27:36 +0000 (07:27 +0000)
12 files changed:
CHANGELOG.txt
admin/company_preferences.php
admin/db/company_db.inc
includes/ui/ui_lists.inc
sales/includes/cart_class.inc
sales/includes/db/sales_order_db.inc
sales/includes/db/sales_types_db.inc
sales/includes/sales_db.inc
sales/includes/ui/sales_credit_ui.inc
sales/includes/ui/sales_order_ui.inc
sales/manage/sales_types.php
sql/alter.sql

index 6ae1f35ff17d53600b41e94130ab289bb1914207..33f3cc36dc3e049513fcdd65da2edb647c1be3cb 100644 (file)
@@ -19,6 +19,24 @@ Legend:
 ! -> Note
 $ -> Affected files
 
+09-May-2008 Janusz Dobrowolski
+# Automatic calculation of not set item prices from home currency and base sales type settings.
+$ /admin/company_preferences.php
+  /admin/db/company_db.inc
+  /includes/ui/ui_lists.inc
+  /sales/includes/cart_class.inc
+  /sales/includes/sales_db.inc
+  /sales/includes/db/sales_order_db.inc
+  /sales/includes/db/sales_types_db.inc
+  /sales/includes/ui/sales_credit_ui.inc
+  /sales/includes/ui/sales_order_ui.inc
+  /sales/manage/sales_types.php
+  /sql/alter.sql
+# Fixed dev bug blocking change of price on order entry.
+$ /sales/includes/ui/sales_order_ui.inc
+# Small display fix
+$ /sales/manage/sales_people.php
+
 30-Apr-2008 Joe Hunt
 # Fixed price update also in purchase order
 $ /purchasing/includes/ui/po_ui.inc
index 69dc25b65d8fbf0fb863cbbd16f895f4d19ac851..37e4222db2fbe347442fd67e9765b39e36eb7ae7 100644 (file)
@@ -31,7 +31,7 @@ if (isset($_POST['submit']) && $_POST['submit'] != "")
                        $_POST['use_dimension'], $_POST['custom1_name'], $_POST['custom2_name'], $_POST['custom3_name'],
                        $_POST['custom1_value'], $_POST['custom2_value'], $_POST['custom3_value'],
                        $_POST['curr_default'], $_POST['f_year'], check_value('no_item_list'), check_value('no_customer_list'),
-                       check_value('no_supplier_list'));
+                       check_value('no_supplier_list'), $_POST['base_sales']);
 
                display_notification_centered(_("Company setup has been updated."));
        }
@@ -57,6 +57,7 @@ $_POST['email']  = $myrow["email"];
 $_POST['coy_logo']  = $myrow["coy_logo"];
 $_POST['domicile']  = $myrow["domicile"];
 $_POST['use_dimension']  = $myrow["use_dimension"];
+$_POST['base_sales']  = $myrow["base_sales"];
 $_POST['no_item_list']  = $myrow["no_item_list"];
 $_POST['no_customer_list']  = $myrow["no_customer_list"];
 $_POST['no_supplier_list']  = $myrow["no_supplier_list"];
@@ -90,6 +91,8 @@ text_row_ex(_("Company Logo:"), 'coy_logo', 25, 55);
 text_row_ex(_("Domicile:"), 'domicile', 25, 55);
 
 number_list_row(_("Use Dimensions:"), 'use_dimension', null, 0, 2);
+sales_types_list_row(_("Base for auto price calculations:"), 'base_sales', $_POST['base_sales'], false,
+    _('No base price list') );
 
 check_row(_("No Item List"), 'no_item_list', $_POST['no_item_list']);
 check_row(_("No Customer List"), 'no_customer_list', $_POST['no_customer_list']);
index 9ce6cee070330d153f97ee743a23a0de2c341cf3..5108f3aa4049905ae02531af1d676e983b9f4965 100644 (file)
@@ -52,7 +52,7 @@ function update_company_gl_setup($debtors_act, $pyt_discount_act, $creditors_act
 function update_company_setup($coy_name, $coy_no, $gst_no, $tax_prd, $tax_last, $postal_address, $phone, $fax, $email,
                $coy_logo, $domicile, $Dimension, $custom1_name, $custom2_name, $custom3_name,
                $custom1_value, $custom2_value, $custom3_value, $curr_default, $f_year, $no_item_list, $no_customer_list,
-               $no_supplier_list)
+               $no_supplier_list, $base_sales)
 {
        if ($f_year == null)
                $f_year = 0;
@@ -77,7 +77,8 @@ function update_company_setup($coy_name, $coy_no, $gst_no, $tax_prd, $tax_last,
         custom2_value=".db_escape($custom2_value).",
         custom3_value=".db_escape($custom3_value).",
                curr_default=".db_escape($curr_default).",
-               f_year=$f_year
+               f_year=$f_year,
+               base_sales=$base_sales
                WHERE coy_code=1";
 
        db_query($sql, "The company setup could not be updated ");
@@ -163,5 +164,14 @@ function delete_fiscalyear($from_date)
        commit_transaction();
 }
 
+function get_base_sales_type()
+{
+       $sql = "SELECT base_sales FROM ".TB_PREF."company WHERE coy_code=1";
+       
+       $result = db_query($sql, "could not get base sales type");
+       $myrow = db_fetch($result);
+       return $myrow[0];
+}
+
 
 ?>
\ No newline at end of file
index 09bc9592bdd562c81d397c18e5572fff0dd18011..80ff7269dff293484263d9d5c5b651953b81281c 100644 (file)
@@ -1276,25 +1276,26 @@ function credit_status_list_row($label, $name, $selected_id)
 
 //-----------------------------------------------------------------------------------------------
 
-function sales_types_list($name, $selected_id, $submit_on_change=false)
+function sales_types_list($name, $selected_id, $submit_on_change=false, $special_option=null)
 {
        simple_codeandname_list("SELECT id, sales_type FROM ".TB_PREF."sales_types",
-               $name, $selected_id, false, null, false, $submit_on_change);
+               $name, $selected_id, 
+               isset($special_option), $special_option, true, $submit_on_change);
 }
 
-function sales_types_list_cells($label, $name, $selected_id, $submit_on_change=false)
+function sales_types_list_cells($label, $name, $selected_id, $submit_on_change=false, $special_option=null)
 {
        if ($label != null)
                echo "<td>$label</td>\n";
        echo "<td>";
-       sales_types_list($name, $selected_id, $submit_on_change);
+       sales_types_list($name, $selected_id, $submit_on_change, $special_option);
        echo "</td>\n";
 }
 
-function sales_types_list_row($label, $name, $selected_id, $submit_on_change=false)
+function sales_types_list_row($label, $name, $selected_id, $submit_on_change=false, $special_option=null)
 {
        echo "<tr>\n";
-       sales_types_list_cells($label, $name, $selected_id, $submit_on_change);
+       sales_types_list_cells($label, $name, $selected_id, $submit_on_change, $special_option);
        echo "</tr>\n";
 }
 
index 9a730136fb4ed09806b210a394f71cf2fe264f2d..88d6cf5f5eabd4b9ed1a4791da15c2efe5ef5b56 100644 (file)
@@ -54,7 +54,7 @@ class cart
        var $tax_group_id;
        var $tax_group_name;
        var $tax_group_array = null; // saves db queries
-
+       var $price_factor;       // ditto for price calculations
        //-------------------------------------------------------------------------
        //
        //  $trans_no==0 => open new/direct document
@@ -189,12 +189,12 @@ class cart
                $this->tax_group_array = get_tax_group_items_as_array($tax_group_id);
        }
 
-       function set_sales_type($sales_type, $sales_name, $tax_included=0)
+       function set_sales_type($sales_type, $sales_name, $tax_included=0, $factor)
        {
-           $old_tax_inc = $this->tax_included;
            $this->sales_type = $sales_type;
            $this->sales_type_name = $sales_name;
            $this->tax_included = $tax_included;
+           $this->price_factor = $factor;
        }
 
        function set_location($id, $name)
@@ -335,6 +335,12 @@ class cart
                $taxes = get_tax_for_items($items, $prices, $shipping_cost,
                  $this->tax_group_id, $this->tax_included,  $this->tax_group_array);
 
+    // Adjustment for swiss franken, we always have 5 rappen = 1/20 franken
+    if ($this->customer_currency == 'CHF') {
+                       $val = $taxes['1']['Value'];
+      $val1 = (floatval((intval(round(($val*20),0)))/20));
+                       $taxes['1']['Value'] = $val1;
+               } 
                return $taxes;
        }
 
index 0e51a907dcb748a7375737625400aecd2a8b09b0..34e4ad9d89b3c0ed8e1a74f95910a3db1d9bc179 100644 (file)
@@ -362,7 +362,8 @@ function read_sales_order($order_no, &$order)
        $order->set_branch($myrow["branch_code"], $myrow["tax_group_id"],
          $myrow["tax_group_name"], $myrow["contact_phone"], $myrow["contact_email"]);
 
-       $order->set_sales_type($myrow["sales_type_id"], $myrow["sales_type"], $myrow["tax_included"]);
+       $order->set_sales_type($myrow["sales_type_id"], $myrow["sales_type"], 
+           $myrow["tax_included"], 0); // no default price calculations on edit
 
        $order->set_location($myrow["from_stk_loc"], $myrow["location_name"]);
 
@@ -451,6 +452,7 @@ function get_customer_to_order($customer_id) {
                  .TB_PREF."debtors_master.sales_type AS salestype, "
                  .TB_PREF."sales_types.sales_type, "
                  .TB_PREF."sales_types.tax_included, "
+                 .TB_PREF."sales_types.factor, "
                  .TB_PREF."debtors_master.curr_code, "
                  .TB_PREF."debtors_master.discount
                FROM ".TB_PREF."debtors_master, "
index 51e1142b39a6912e2037f2af139cec16333e1a23..4bde37506bf08cc65b12fe54ff816b6fa8da8ec2 100644 (file)
@@ -1,16 +1,20 @@
 <?php
 
-function add_sales_type($name, $tax_included)
+function add_sales_type($name, $tax_included, $factor)
 {
-       $sql = "INSERT INTO ".TB_PREF."sales_types (sales_type,tax_included) VALUES (".db_escape($name).",'$tax_included')";
+       // maximum one base price list
+         clear_base_sales_type();
+
+       $sql = "INSERT INTO ".TB_PREF."sales_types (sales_type,tax_included,factor) VALUES (".db_escape($name).",'$tax_included',$factor)";
                
        db_query($sql, "could not add sales type");             
 }
 
-function update_sales_type($id, $name, $tax_included)
+function update_sales_type($id, $name, $tax_included, $factor)
 {
+
        $sql = "UPDATE ".TB_PREF."sales_types SET sales_type = ".db_escape($name).",
-       tax_included =$tax_included WHERE id = $id";
+       tax_included =$tax_included, factor=$factor WHERE id = $id";
        
        db_query($sql, "could not update sales type");                  
 }
index 55d6138516dfee8b84464be2176db5d81f8453f1..75fca059a1d98575c5077451b45ecbb0819e112c 100644 (file)
@@ -46,27 +46,67 @@ function add_gl_trans_customer($type, $type_no, $date_, $account, $dimension, $d
 
 //----------------------------------------------------------------------------------------
 
-function get_price ($stock_id, $currency, $sales_type_id)
+function get_price ($stock_id, $currency, $sales_type_id, $factor, $date=null)
 {
+       if ($date == null)
+           $date = Today();
+           
        $sql = "SELECT ".TB_PREF."prices.price
                FROM ".TB_PREF."prices
-               WHERE ".TB_PREF."prices.stock_id = '" . $stock_id . "'
-                 AND ".TB_PREF."prices.sales_type_id = " . $sales_type_id . "
-                 AND ".TB_PREF."prices.curr_abrev = '$currency'";
+               WHERE ".TB_PREF."prices.stock_id = '" . $stock_id . "' "
+               ." AND ".TB_PREF."prices.sales_type_id = " . $sales_type_id
+               ." AND ".TB_PREF."prices.curr_abrev = '$currency'";
 
-       $result = db_query($sql, "There was a problem retrieving the pricing information for the part $stock_id for customer");
+       $msg = "There was a problem retrieving the pricing information for the part $stock_id for customer";
+       $result = db_query($sql, $msg);
 
-       if (db_num_rows($result) != 0)
+       if (db_num_rows($result) != 0) 
        {
-               /*There is a price from one of the above so return that */
                $myrow = db_fetch_row($result);
                return $myrow[0];
        }
-       else
+       if ($factor == 0) return 0; // auto price calculations off
+
+       $base_id = get_base_sales_type();
+        $home_curr = get_company_currency();
+
+    // get all prices which we can use to guess the price.
+    // alternative is make up to 2 additional sql queries
+       $sql = "SELECT ".TB_PREF."prices.price,".TB_PREF."prices.curr_abrev,
+               ".TB_PREF."prices.sales_type_id
+               FROM ".TB_PREF."prices
+               WHERE ".TB_PREF."prices.stock_id = '" . $stock_id . "' "
+               ." AND (".TB_PREF."prices.sales_type_id = " . $sales_type_id
+               ." OR ".TB_PREF."prices.sales_type_id = " . $base_id.")"
+               ." AND (".TB_PREF."prices.curr_abrev = '$currency'"
+               ." OR ".TB_PREF."prices.curr_abrev = '$home_curr')";
+
+       $result = db_query($sql, $msg);
+
+       $prices = array();
+       while($myrow = db_fetch($result)) 
        {
-               return 0;
+           $prices[$myrow['sales_type_id']][$myrow['curr_abrev']] = $myrow['price'];
        }
-
+       
+       $rate = round(get_exchange_rate_from_home_currency($currency, $date),
+           user_exrate_dec());
+       $price = 0.00;
+       
+       if (isset($prices[$sales_type_id][$home_curr])) 
+       {
+           $price = $prices[$sales_type_id][$home_curr] / $rate;
+       }
+       if (isset($prices[$base_id][$currency])) 
+       {
+           $price =$prices[$base_id][$currency] * $factor;
+       }
+       if (isset($prices[$base_id][$home_curr])) 
+       {
+           $price =$prices[$base_id][$home_curr] * $factor / $rate;
+       }
+       
+       return round($price, user_price_dec());
 }
 
 //-----------------------------------------------------------------------------
@@ -179,7 +219,7 @@ function read_sales_trans($doc_type, $trans_no, &$cart)
                else
                        $cart->trans_no = array($trans_no[0]=>$myrow["version"]);
 
-               $cart->set_sales_type($myrow["tpe"], $myrow["sales_type"], $myrow["tax_included"]);
+               $cart->set_sales_type($myrow["tpe"], $myrow["sales_type"], $myrow["tax_included"],0);
 
                $cart->set_customer($myrow["debtor_no"], $myrow["DebtorName"],
                        $myrow["curr_code"], $myrow["discount"]);
index 5fab4ce7305e45bf8b582e741930fd2a221548a4..5acd6ac3d1bd8139ee0f53fe88ac12cda942bad5 100644 (file)
@@ -208,8 +208,9 @@ function credit_edit_item_controls(&$order, $rowcounter, $line_no=-1)
                $_POST['units'] = $item_info["units"];
 
                $_POST['qty'] = qty_format(0);
-               $_POST['price'] = get_price($_POST['stock_id'],
-                    $order->customer_currency, $order->sales_type);
+               $_POST['price'] = price_format(get_price($_POST['stock_id'], $order->customer_currency,
+                   $order->sales_type, $order->price_factor, $order->document_date));
+
                // default to the customer's discount %
                $_POST['Disc'] = percent_format($order->default_discount * 100);
        }
index a33834f1bf2507608c1e1c4923926aee51f65426..5b8b69e6c343dbcd92b0f6c643ee1776a2a7727e 100644 (file)
@@ -45,7 +45,8 @@ function get_customer_details_to_order(&$order, $customer_id, $branch_id)
        $order->set_customer($customer_id, $name, $myrow['curr_code'], $myrow['discount']);
 
        // the sales type determines the price list to be used by default
-       $order->set_sales_type($myrow['salestype'], $myrow['sales_type'], $myrow['tax_included']);
+       $order->set_sales_type($myrow['salestype'], $myrow['sales_type'], $myrow['tax_included'],
+           $myrow['factor']);
 
 //     if ($order->Branch != 0)
        {
@@ -215,6 +216,7 @@ function display_order_header(&$order, $editable, $date_text, $display_tax_group
                                $_POST['delivery_address'] = $order->delivery_address;
                                $_POST['phone'] = $order->phone;
                                $_POST['sales_type'] = $order->sales_type;
+                               $_POST['factor'] = $order->price_factor;
 
                                // change prices if necessary
                                // what about discount in template case?
@@ -251,18 +253,18 @@ 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['tax_included'], $myrow['factor']);
                $change_prices = 1;
        }
 
-//     if ($change_prices != 0) {
+       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->sales_type, $order->price_factor, $order->document_date);
                //              $line->discount_percent = $order->default_discount;
                }
-//     }
+       }
 
        echo "<table height='5'>";
        if($editable)
@@ -341,7 +343,8 @@ function sales_order_item_controls(&$order, &$rowcounter, $line_no=-1)
 
                $_POST['qty'] = qty_format(1);
                $_POST['price'] = price_format(get_price ($_POST['stock_id'],
-                       $order->customer_currency, $order->sales_type));
+                       $order->customer_currency, $order->sales_type, 
+                       $order->price_factor, $order->document_date));
                // default to the customer's discount %
                $_POST['Disc'] = percent_format($order->default_discount * 100);
        }
index bad18a693cd4e623082b2f422f5f57462785930f..195a1a5bc24c7b7c422c61f041b8d3e094417436 100644 (file)
@@ -36,7 +36,8 @@ function can_process()
 
 if (isset($_POST['ADD_ITEM']) && can_process()) 
 {
-       add_sales_type($_POST['sales_type'], isset($_POST['tax_included']));
+       add_sales_type($_POST['sales_type'], isset($_POST['tax_included']) ? 1:0, 
+           $_POST['factor']);
        meta_forward($_SERVER['PHP_SELF']);     
 }
 
@@ -45,7 +46,8 @@ if (isset($_POST['ADD_ITEM']) && can_process())
 if (isset($_POST['UPDATE_ITEM']) && can_process()) 
 {
 
-       update_sales_type($selected_id, $_POST['sales_type'], isset($_POST['tax_included']) ? 1:0);
+       update_sales_type($selected_id, $_POST['sales_type'], isset($_POST['tax_included']) ? 1:0,
+            $_POST['factor']);
        meta_forward($_SERVER['PHP_SELF']);     
 } 
 
@@ -91,21 +93,29 @@ $result = get_all_sales_types();
 
 start_table("$table_style width=30%");
 
-$th = array (_("Type Name"), 'Tax Incl', '','');
+$th = array (_('Type Name'), _('Factor'), _('Tax Incl'), '','');
 table_header($th);
 $k = 0;
+$base_sales = get_base_sales_type();
 
 while ($myrow = db_fetch($result)) 
 {
-       alt_table_row_color($k);
-       label_cell($myrow["sales_type"]);       
+       if ($myrow["id"] == $base_sales)
+           start_row("class='overduebg'");
+       else
+           alt_table_row_color($k);
+       label_cell($myrow["sales_type"]);
+       $f = number_format2($myrow["factor"],4);
+       if($myrow["id"] == $base_sales) $f = "<I>"._('Base')."</I>";
+       label_cell($f); 
        label_cell($myrow["tax_included"] ? _('Yes'):_('No'), 'align=center');  
-    edit_link_cell("selected_id=".$myrow["id"]);
-    delete_link_cell("selected_id=".$myrow["id"]."&delete=1");
+       edit_link_cell("selected_id=".$myrow["id"]);
+       delete_link_cell("selected_id=".$myrow["id"]."&delete=1");
        end_row();
 }
 
 end_table();
+display_note(_("Marked sales type is the company base pricelist for prices calculations."), 0, 0, "class='overduefg'");
 
 //----------------------------------------------------------------------------------------------------
 
@@ -114,6 +124,8 @@ hyperlink_no_params($_SERVER['PHP_SELF'], _("New Sales type"));
 start_form();
  if (!isset($_POST['tax_included']))
        $_POST['tax_included'] = 0;
+ if (!isset($_POST['base']))
+       $_POST['base'] = 0;
 
 start_table("$table_style2 width=30%");
 
@@ -124,12 +136,14 @@ if ($selected_id != -1)
        
        $_POST['sales_type']  = $myrow["sales_type"];
        $_POST['tax_included']  = $myrow["tax_included"];
+       $_POST['factor']  = number_format2($myrow["factor"],4);
        
        hidden('selected_id', $selected_id);
 } 
 
-text_row_ex(_("Sales Type Name:"), 'sales_type', 20);
-check_cells("Tax  included", 'tax_included', $_POST['tax_included']);
+text_row_ex(_("Sales Type Name").':', 'sales_type', 20);
+amount_row(_("Calculation factor").':', 'factor');
+check_row(_("Tax included").':', 'tax_included', $_POST['tax_included']);
 
 end_table(1);
 
index 9d87fc5737f3cdd1edf0379395bf01760a8c5308..c6a3dfcddb578deb64f195a9ee93add9e807f173 100644 (file)
@@ -16,6 +16,9 @@
 -- ALTER TABLE
 -- 
 
+ALTER TABLE `0_sales_types` ADD `factor` DOUBLE DEFAULT '1' NOT NULL AFTER `tax_included` ;
+ALTER TABLE `0_sales_types` ADD `base` TINYINT(1) DEFAULT '0' NOT NULL AFTER `factor` ;
+
 DROP TABLE IF EXISTS `0_item_units`; 
 CREATE TABLE IF NOT EXISTS `0_item_units` (
   `abbr` varchar(20) NOT NULL,