! -> 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
$_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."));
}
$_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"];
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']);
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;
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 ");
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
//-----------------------------------------------------------------------------------------------
-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";
}
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
$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)
$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;
}
$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"]);
.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, "
<?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");
}
//----------------------------------------------------------------------------------------
-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());
}
//-----------------------------------------------------------------------------
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"]);
$_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);
}
$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)
{
$_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?
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)
$_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);
}
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']);
}
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']);
}
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'");
//----------------------------------------------------------------------------------------------------
start_form();
if (!isset($_POST['tax_included']))
$_POST['tax_included'] = 0;
+ if (!isset($_POST['base']))
+ $_POST['base'] = 0;
start_table("$table_style2 width=30%");
$_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);
-- 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,