Merge branch 'responsive' of https://github.com/webnotes/erpnext into responsive

This commit is contained in:
Saurabh
2013-06-24 14:10:36 +05:30
4583 changed files with 11393 additions and 94404 deletions

View File

@@ -16,14 +16,280 @@
from __future__ import unicode_literals
import webnotes
from webnotes.utils import flt
from utilities.transaction_base import TransactionBase
from webnotes import _, msgprint
from webnotes.utils import flt, cint, today
from setup.utils import get_company_currency, get_price_list_currency
from accounts.utils import get_fiscal_year
from utilities.transaction_base import TransactionBase, validate_conversion_rate
import json
class AccountsController(TransactionBase):
def validate(self):
if self.meta.get_field("grand_total"):
self.set_missing_values(for_validate=True)
if self.meta.get_field("currency"):
self.company_currency = get_company_currency(self.doc.company)
validate_conversion_rate(self.doc.currency, self.doc.conversion_rate,
self.meta.get_label("conversion_rate"), self.doc.company)
self.calculate_taxes_and_totals()
self.validate_value("grand_total", ">=", 0)
self.set_total_in_words()
def set_missing_values(self, for_validate=False):
for fieldname in ["posting_date", "transaction_date"]:
if not self.doc.fields.get(fieldname) and self.meta.get_field(fieldname):
self.doc.fields[fieldname] = today()
if not self.doc.fiscal_year:
self.doc.fiscal_year = get_fiscal_year(self.doc.fields[fieldname])[0]
def set_price_list_currency(self, buying_or_selling):
# TODO - change this, since price list now has only one currency allowed
if self.meta.get_field("price_list_name") and self.doc.price_list_name and \
not self.doc.price_list_currency:
self.doc.fields.update(get_price_list_currency({
"price_list_name": self.doc.price_list_name,
"buying_or_selling": buying_or_selling
}))
if self.doc.price_list_currency:
if not self.doc.plc_conversion_rate:
exchange = self.doc.price_list_currency + "-" + get_company_currency(self.doc.company)
self.doc.plc_conversion_rate = flt(webnotes.conn.get_value("Currency Exchange",
exchange, "exchange_rate"))
if not self.doc.currency:
self.doc.currency = self.doc.price_list_currency
self.doc.conversion_rate = self.doc.plc_conversion_rate
def set_missing_item_details(self, get_item_details):
"""set missing item values"""
for item in self.doclist.get({"parentfield": self.fname}):
if item.fields.get("item_code"):
args = item.fields.copy().update(self.doc.fields)
ret = get_item_details(args)
for fieldname, value in ret.items():
if self.meta.get_field(fieldname, parentfield=self.fname) and \
not item.fields.get(fieldname):
item.fields[fieldname] = value
def set_taxes(self, tax_doctype, tax_parentfield, tax_master_field):
if not self.meta.get_field(tax_parentfield):
return
if not self.doclist.get({"parentfield": tax_parentfield}):
if not self.doc.fields.get(tax_master_field):
# get the default tax master
self.doc.fields[tax_master_field] = \
webnotes.conn.get_value(tax_doctype + " Master", {"is_default": 1})
if self.doc.fields.get(tax_master_field):
from webnotes.model import default_fields
tax_master = webnotes.bean(tax_doctype + " Master", self.doc.fields.get(tax_master_field))
for i, tax in enumerate(tax_master.doclist.get({"parentfield": tax_parentfield})):
for fieldname in default_fields:
tax.fields[fieldname] = None
tax.fields.update({
"doctype": tax_doctype,
"parentfield": tax_parentfield,
"idx": i+1
})
self.doclist.append(tax)
def calculate_taxes_and_totals(self):
self.doc.conversion_rate = flt(self.doc.conversion_rate)
self.item_doclist = self.doclist.get({"parentfield": self.fname})
self.tax_doclist = self.doclist.get({"parentfield": self.other_fname})
self.calculate_item_values()
self.initialize_taxes()
if hasattr(self, "determine_exclusive_rate"):
self.determine_exclusive_rate()
self.calculate_net_total()
self.calculate_taxes()
self.calculate_totals()
self._cleanup()
# TODO
# print format: show net_total_export instead of net_total
def initialize_taxes(self):
for tax in self.tax_doclist:
tax.item_wise_tax_detail = {}
for fieldname in ["tax_amount", "total",
"tax_amount_for_current_item", "grand_total_for_current_item",
"tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]:
tax.fields[fieldname] = 0.0
self.validate_on_previous_row(tax)
self.validate_inclusive_tax(tax)
self.round_floats_in(tax)
def validate_on_previous_row(self, tax):
"""
validate if a valid row id is mentioned in case of
On Previous Row Amount and On Previous Row Total
"""
if tax.charge_type in ["On Previous Row Amount", "On Previous Row Total"] and \
(not tax.row_id or cint(tax.row_id) >= tax.idx):
msgprint((_("Row") + " # %(idx)s [%(taxes_doctype)s]: " + \
_("Please specify a valid") + " %(row_id_label)s") % {
"idx": tax.idx,
"taxes_doctype": tax.doctype,
"row_id_label": self.meta.get_label("row_id",
parentfield=self.other_fname)
}, raise_exception=True)
def validate_inclusive_tax(self, tax):
def _on_previous_row_error(row_range):
msgprint((_("Row") + " # %(idx)s [%(doctype)s]: " +
_("to be included in Item's rate, it is required that: ") +
" [" + _("Row") + " # %(row_range)s] " + _("also be included in Item's rate")) % {
"idx": tax.idx,
"doctype": tax.doctype,
"inclusive_label": self.meta.get_label("included_in_print_rate",
parentfield=self.other_fname),
"charge_type_label": self.meta.get_label("charge_type",
parentfield=self.other_fname),
"charge_type": tax.charge_type,
"row_range": row_range
}, raise_exception=True)
if cint(tax.included_in_print_rate):
if tax.charge_type == "Actual":
# inclusive tax cannot be of type Actual
msgprint((_("Row")
+ " # %(idx)s [%(doctype)s]: %(charge_type_label)s = \"%(charge_type)s\" "
+ "cannot be included in Item's rate") % {
"idx": tax.idx,
"doctype": tax.doctype,
"charge_type_label": self.meta.get_label("charge_type",
parentfield=self.other_fname),
"charge_type": tax.charge_type,
}, raise_exception=True)
elif tax.charge_type == "On Previous Row Amount" and \
not cint(self.tax_doclist[tax.row_id - 1].included_in_print_rate):
# referred row should also be inclusive
_on_previous_row_error(tax.row_id)
elif tax.charge_type == "On Previous Row Total" and \
not all([cint(t.included_in_print_rate) for t in self.tax_doclist[:tax.row_id - 1]]):
# all rows about the reffered tax should be inclusive
_on_previous_row_error("1 - %d" % (tax.row_id,))
def calculate_taxes(self):
for item in self.item_doclist:
item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
for i, tax in enumerate(self.tax_doclist):
# tax_amount represents the amount of tax for the current step
current_tax_amount = self.get_current_tax_amount(item, tax, item_tax_map)
if hasattr(self, "set_item_tax_amount"):
self.set_item_tax_amount(item, tax, current_tax_amount)
# case when net total is 0 but there is an actual type charge
# in this case add the actual amount to tax.tax_amount
# and tax.grand_total_for_current_item for the first such iteration
if tax.charge_type=="Actual" and \
not (current_tax_amount or self.doc.net_total or tax.tax_amount):
zero_net_total_adjustment = flt(tax.rate, self.precision("tax_amount", tax))
current_tax_amount += zero_net_total_adjustment
# store tax_amount for current item as it will be used for
# charge type = 'On Previous Row Amount'
tax.tax_amount_for_current_item = current_tax_amount
# accumulate tax amount into tax.tax_amount
tax.tax_amount += current_tax_amount
if tax.category:
# if just for valuation, do not add the tax amount in total
# hence, setting it as 0 for further steps
current_tax_amount = 0.0 if (tax.category == "Valuation") else current_tax_amount
current_tax_amount *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0
# Calculate tax.total viz. grand total till that step
# note: grand_total_for_current_item contains the contribution of
# item's amount, previously applied tax and the current tax on that item
if i==0:
tax.grand_total_for_current_item = flt(item.amount +
current_tax_amount, self.precision("total", tax))
else:
tax.grand_total_for_current_item = \
flt(self.tax_doclist[i-1].grand_total_for_current_item +
current_tax_amount, self.precision("total", tax))
# in tax.total, accumulate grand total of each item
tax.total += tax.grand_total_for_current_item
def get_current_tax_amount(self, item, tax, item_tax_map):
tax_rate = self._get_tax_rate(tax, item_tax_map)
current_tax_amount = 0.0
if tax.charge_type == "Actual":
# distribute the tax amount proportionally to each item row
actual = flt(tax.rate, self.precision("tax_amount", tax))
current_tax_amount = (self.doc.net_total
and ((item.amount / self.doc.net_total) * actual)
or 0)
elif tax.charge_type == "On Net Total":
current_tax_amount = (tax_rate / 100.0) * item.amount
elif tax.charge_type == "On Previous Row Amount":
current_tax_amount = (tax_rate / 100.0) * \
self.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item
elif tax.charge_type == "On Previous Row Total":
current_tax_amount = (tax_rate / 100.0) * \
self.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item
current_tax_amount = flt(current_tax_amount, self.precision("tax_amount", tax))
# store tax breakup for each item
tax.item_wise_tax_detail[item.item_code or item.item_name] = [tax_rate, current_tax_amount]
return current_tax_amount
def _load_item_tax_rate(self, item_tax_rate):
return json.loads(item_tax_rate) if item_tax_rate else {}
def _get_tax_rate(self, tax, item_tax_map):
if item_tax_map.has_key(tax.account_head):
return flt(item_tax_map.get(tax.account_head), self.precision("rate", tax))
else:
return tax.rate
def _cleanup(self):
for tax in self.tax_doclist:
for fieldname in ("grand_total_for_current_item",
"tax_amount_for_current_item",
"tax_fraction_for_current_item",
"grand_total_fraction_for_current_item"):
if fieldname in tax.fields:
del tax.fields[fieldname]
tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail)
def _set_in_company_currency(self, item, print_field, base_field):
"""set values in base currency"""
item.fields[base_field] = flt((flt(item.fields[print_field],
self.precision(print_field, item)) * self.doc.conversion_rate),
self.precision(base_field, item))
def calculate_total_advance(self, parenttype, advance_parentfield):
if self.doc.doctype == parenttype and self.doc.docstatus < 2:
sum_of_allocated_amount = sum([flt(adv.allocated_amount, self.precision("allocated_amount", adv))
for adv in self.doclist.get({"parentfield": advance_parentfield})])
self.doc.total_advance = flt(sum_of_allocated_amount, self.precision("total_advance"))
self.calculate_outstanding_amount()
def get_gl_dict(self, args, cancel=None):
"""this method populates the common properties of a gl entry record"""
@@ -97,4 +363,4 @@ class AccountsController(TransactionBase):
if not hasattr(self, "_abbr"):
self._abbr = webnotes.conn.get_value("Company", self.doc.company, "abbr")
return self._abbr
return self._abbr

View File

@@ -17,35 +17,46 @@
from __future__ import unicode_literals
import webnotes
from webnotes import _, msgprint
from webnotes.utils import flt, cint
import json
from webnotes.utils import flt
from buying.utils import get_item_details
from setup.utils import get_company_currency
from webnotes.model.utils import round_floats_in_doc
from controllers.stock_controller import StockController
class WrongWarehouseCompany(Exception): pass
class BuyingController(StockController):
def onload_post_render(self):
# contact, address, item details
self.set_missing_values()
self.set_taxes("Purchase Taxes and Charges", "purchase_tax_details", "purchase_other_charges")
def validate(self):
super(BuyingController, self).validate()
self.validate_stock_or_nonstock_items()
self.validate_warehouse_belongs_to_company()
if self.meta.get_field("currency"):
self.company_currency = get_company_currency(self.doc.company)
self.validate_conversion_rate("currency", "conversion_rate")
if self.doc.price_list_name and self.doc.price_list_currency:
self.validate_conversion_rate("price_list_currency", "plc_conversion_rate")
# IMPORTANT: enable this only when client side code is similar to this one
# self.calculate_taxes_and_totals()
def set_missing_values(self, for_validate=False):
super(BuyingController, self).set_missing_values(for_validate)
self.set_price_list_currency("Buying")
# set contact and address details for supplier, if they are not mentioned
if self.doc.supplier and not (self.doc.contact_person and self.doc.supplier_address):
for fieldname, val in self.get_default_address_and_contact("supplier").items():
if not self.doc.fields.get(fieldname) and self.meta.get_field(fieldname):
self.doc.fields[fieldname] = val
self.set_missing_item_details(get_item_details)
def set_supplier_defaults(self):
self.doc.fields.update(self.get_default_supplier_address(self.doc.fields))
# set total in words
self.set_total_in_words()
def get_purchase_tax_details(self):
self.doclist = self.doc.clear_table(self.doclist, "purchase_tax_details")
self.set_taxes("Purchase Taxes and Charges", "purchase_tax_details", "purchase_other_charges")
def validate_warehouse_belongs_to_company(self):
for warehouse, company in webnotes.conn.get_values("Warehouse",
self.doclist.get_distinct_values("warehouse"), "company").items():
@@ -62,46 +73,6 @@ class BuyingController(StockController):
webnotes.msgprint(_("""Tax Category can not be 'Valuation' or 'Valuation and Total'
as all items are non-stock items"""), raise_exception=1)
def update_item_details(self):
for item in self.doclist.get({"parentfield": self.fname}):
ret = get_item_details({
"doctype": self.doc.doctype,
"docname": self.doc.name,
"item_code": item.item_code,
"warehouse": item.warehouse,
"supplier": self.doc.supplier,
"transaction_date": self.doc.posting_date,
"conversion_rate": self.doc.conversion_rate,
"price_list_name": self.doc.price_list_name,
"price_list_currency": self.doc.price_list_currency,
"plc_conversion_rate": self.doc.plc_conversion_rate
})
for r in ret:
if not item.fields.get(r):
item.fields[r] = ret[r]
def validate_conversion_rate(self, currency_field, conversion_rate_field):
"""common validation for currency and price list currency"""
currency = self.doc.fields.get(currency_field)
conversion_rate = flt(self.doc.fields.get(conversion_rate_field))
conversion_rate_label = self.meta.get_label(conversion_rate_field)
if conversion_rate == 0:
msgprint(conversion_rate_label + _(' cannot be 0'), raise_exception=True)
# parenthesis for 'OR' are necessary as we want it to evaluate as
# mandatory valid condition and (1st optional valid condition
# or 2nd optional valid condition)
valid_conversion_rate = (conversion_rate and
((currency == self.company_currency and conversion_rate == 1.00)
or (currency != self.company_currency and conversion_rate != 1.00)))
if not valid_conversion_rate:
msgprint(_('Please enter valid ') + conversion_rate_label + (': ')
+ ("1 %s = [?] %s" % (currency, self.company_currency)),
raise_exception=True)
def set_total_in_words(self):
from webnotes.utils import money_in_words
company_currency = get_company_currency(self.doc.company)
@@ -112,154 +83,54 @@ class BuyingController(StockController):
self.doc.currency)
def calculate_taxes_and_totals(self):
self.doc.conversion_rate = flt(self.doc.conversion_rate)
self.item_doclist = self.doclist.get({"parentfield": self.fname})
self.tax_doclist = self.doclist.get({"parentfield": "purchase_tax_details"})
self.calculate_item_values()
self.initialize_taxes()
self.calculate_net_total()
self.calculate_taxes()
self.calculate_totals()
self.calculate_outstanding_amount()
self._cleanup()
self.other_fname = "purchase_tax_details"
super(BuyingController, self).calculate_taxes_and_totals()
self.calculate_total_advance("Purchase Invoice", "advance_allocation_details")
def calculate_item_values(self):
def _set_base(item, print_field, base_field):
"""set values in base currency"""
item.fields[base_field] = flt((flt(item.fields[print_field],
self.precision.item[print_field]) * self.doc.conversion_rate),
self.precision.item[base_field])
for item in self.item_doclist:
round_floats_in_doc(item, self.precision.item)
# hack! - cleaned up in _cleanup()
if self.doc.doctype != "Purchase Invoice":
df = self.meta.get_field("purchase_rate", parentfield=self.fname)
df.fieldname = "rate"
for item in self.item_doclist:
# hack! - cleaned up in _cleanup()
if self.doc.doctype != "Purchase Invoice":
item.rate = item.purchase_rate
self.precision.item.rate = self.precision.item.purchase_rate
item.discount = item.discount_rate
self.precision.item.discount = self.precision.item.discount_rate
self.round_floats_in(item)
if item.discount == 100:
if not item.import_ref_rate:
item.import_ref_rate = item.import_rate
item.import_rate = 0
else:
if item.import_ref_rate:
item.import_rate = flt(item.import_ref_rate *
(1.0 - (item.discount_rate / 100.0)),
self.precision.item.import_rate)
else:
# assume that print rate and discount are specified
item.import_ref_rate = flt(item.import_rate /
(1.0 - (item.discount_rate / 100.0)),
self.precision.item.import_ref_rate)
if item.discount_rate == 100.0:
item.import_rate = 0.0
elif item.import_ref_rate:
item.import_rate = flt(item.import_ref_rate * (1.0 - (item.discount_rate / 100.0)),
self.precision("import_rate", item))
item.import_amount = flt(item.import_rate * item.qty,
self.precision.item.import_amount)
self.precision("import_amount", item))
item.item_tax_amount = 0.0;
_set_base(item, "import_ref_rate", "purchase_ref_rate")
_set_base(item, "import_rate", "rate")
_set_base(item, "import_amount", "amount")
def initialize_taxes(self):
for tax in self.tax_doclist:
# initialize totals to 0
tax.tax_amount = tax.total = 0.0
self._set_in_company_currency(item, "import_ref_rate", "purchase_ref_rate")
self._set_in_company_currency(item, "import_rate", "rate")
self._set_in_company_currency(item, "import_amount", "amount")
# temporary fields
tax.tax_amount_for_current_item = tax.grand_total_for_current_item = 0.0
tax.item_wise_tax_detail = {}
self.validate_on_previous_row(tax)
round_floats_in_doc(tax, self.precision.tax)
def calculate_net_total(self):
self.doc.net_total = 0
self.doc.net_total_import = 0
self.doc.net_total = self.doc.net_total_import = 0.0
for item in self.item_doclist:
self.doc.net_total += item.amount
self.doc.net_total_import += item.import_amount
self.doc.net_total = flt(self.doc.net_total, self.precision.main.net_total)
self.doc.net_total_import = flt(self.doc.net_total_import,
self.precision.main.net_total_import)
def calculate_taxes(self):
for item in self.item_doclist:
item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
item.item_tax_amount = 0
for i, tax in enumerate(self.tax_doclist):
# tax_amount represents the amount of tax for the current step
current_tax_amount = self.get_current_tax_amount(item, tax, item_tax_map)
self.set_item_tax_amount(item, tax, current_tax_amount)
# case when net total is 0 but there is an actual type charge
# in this case add the actual amount to tax.tax_amount
# and tax.grand_total_for_current_item for the first such iteration
if not (current_tax_amount or self.doc.net_total or tax.tax_amount) and \
tax.charge_type=="Actual":
zero_net_total_adjustment = flt(tax.rate, self.precision.tax.tax_amount)
current_tax_amount += zero_net_total_adjustment
# store tax_amount for current item as it will be used for
# charge type = 'On Previous Row Amount'
tax.tax_amount_for_current_item = current_tax_amount
# accumulate tax amount into tax.tax_amount
tax.tax_amount += tax.tax_amount_for_current_item
if tax.category == "Valuation":
# if just for valuation, do not add the tax amount in total
# hence, setting it as 0 for further steps
current_tax_amount = 0
else:
current_tax_amount *= tax.add_deduct_tax == "Deduct" and -1.0 or 1.0
# Calculate tax.total viz. grand total till that step
# note: grand_total_for_current_item contains the contribution of
# item's amount, previously applied tax and the current tax on that item
if i==0:
tax.grand_total_for_current_item = flt(item.amount +
current_tax_amount, self.precision.tax.total)
else:
tax.grand_total_for_current_item = \
flt(self.tax_doclist[i-1].grand_total_for_current_item +
current_tax_amount, self.precision.tax.total)
# in tax.total, accumulate grand total of each item
tax.total += tax.grand_total_for_current_item
# store tax_breakup for each item
# DOUBT: should valuation type amount also be stored?
tax.item_wise_tax_detail[item.item_code] = current_tax_amount
self.round_floats_in(self.doc, ["net_total", "net_total_import"])
def calculate_totals(self):
if self.tax_doclist:
self.doc.grand_total = flt(self.tax_doclist[-1].total,
self.precision.main.grand_total)
self.doc.grand_total_import = flt(
self.doc.grand_total / self.doc.conversion_rate,
self.precision.main.grand_total_import)
else:
self.doc.grand_total = flt(self.doc.net_total,
self.precision.main.grand_total)
self.doc.grand_total_import = flt(
self.doc.grand_total / self.doc.conversion_rate,
self.precision.main.grand_total_import)
self.doc.grand_total = flt(self.tax_doclist and \
self.tax_doclist[-1].total or self.doc.net_total, self.precision("grand_total"))
self.doc.grand_total_import = flt(self.doc.grand_total / self.doc.conversion_rate,
self.precision("grand_total_import"))
self.doc.total_tax = \
flt(self.doc.grand_total - self.doc.net_total,
self.precision.main.total_tax)
self.doc.total_tax = flt(self.doc.grand_total - self.doc.net_total,
self.precision("total_tax"))
if self.meta.get_field("rounded_total"):
self.doc.rounded_total = round(self.doc.grand_total)
@@ -268,75 +139,31 @@ class BuyingController(StockController):
self.doc.rounded_total_import = round(self.doc.grand_total_import)
def calculate_outstanding_amount(self):
if self.doc.doctype == "Purchase Invoice" and self.doc.docstatus == 0:
if self.doc.doctype == "Purchase Invoice" and self.doc.docstatus < 2:
self.doc.total_advance = flt(self.doc.total_advance,
self.precision.main.total_advance)
self.precision("total_advance"))
self.doc.total_amount_to_pay = flt(self.doc.grand_total - flt(self.doc.write_off_amount,
self.precision.main.write_off_amount), self.precision.main.total_amount_to_pay)
self.precision("write_off_amount")), self.precision("total_amount_to_pay"))
self.doc.outstanding_amount = flt(self.doc.total_amount_to_pay - self.doc.total_advance,
self.precision.main.outstanding_amount)
self.precision("outstanding_amount"))
def _cleanup(self):
for tax in self.tax_doclist:
del tax.fields["grand_total_for_current_item"]
del tax.fields["tax_amount_for_current_item"]
tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail)
# except in purchase invoice, rate field is purchase_rate
super(BuyingController, self)._cleanup()
# except in purchase invoice, rate field is purchase_rate
# reset fieldname of rate
if self.doc.doctype != "Purchase Invoice":
df = self.meta.get_field("rate", parentfield=self.fname)
df.fieldname = "purchase_rate"
for item in self.item_doclist:
item.purchase_rate = item.rate
del item.fields["rate"]
if not self.meta.get_field("item_tax_amount", parentfield=self.fname):
for item in self.item_doclist:
del item.fields["item_tax_amount"]
item.discount_rate = item.discount
del item.fields["discount"]
def validate_on_previous_row(self, tax):
"""
validate if a valid row id is mentioned in case of
On Previous Row Amount and On Previous Row Total
"""
if tax.charge_type in ["On Previous Row Amount", "On Previous Row Total"] and \
(not tax.row_id or cint(tax.row_id) >= tax.idx):
msgprint((_("Row") + " # %(idx)s [%(taxes_doctype)s]: " + \
_("Please specify a valid") + " %(row_id_label)s") % {
"idx": tax.idx,
"taxes_doctype": tax.parenttype,
"row_id_label": self.meta.get_label("row_id",
parentfield="purchase_tax_details")
}, raise_exception=True)
def _load_item_tax_rate(self, item_tax_rate):
if not item_tax_rate:
return {}
return json.loads(item_tax_rate)
def get_current_tax_amount(self, item, tax, item_tax_map):
tax_rate = self._get_tax_rate(tax, item_tax_map)
if tax.charge_type == "Actual":
# distribute the tax amount proportionally to each item row
actual = flt(tax.rate, self.precision.tax.tax_amount)
current_tax_amount = (self.doc.net_total
and ((item.amount / self.doc.net_total) * actual)
or 0)
elif tax.charge_type == "On Net Total":
current_tax_amount = (tax_rate / 100.0) * item.amount
elif tax.charge_type == "On Previous Row Amount":
current_tax_amount = (tax_rate / 100.0) * \
self.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item
elif tax.charge_type == "On Previous Row Total":
current_tax_amount = (tax_rate / 100.0) * \
self.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item
return flt(current_tax_amount, self.precision.tax.tax_amount)
def _get_tax_rate(self, tax, item_tax_map):
if item_tax_map.has_key(tax.account_head):
return flt(item_tax_map.get(tax.account_head), self.precision.tax.rate)
else:
return tax.rate
def set_item_tax_amount(self, item, tax, current_tax_amount):
"""
item_tax_amount is the total tax amount applied on that item
@@ -345,28 +172,28 @@ class BuyingController(StockController):
TODO: rename item_tax_amount to valuation_tax_amount
"""
if tax.category in ["Valuation", "Valuation and Total"] and \
item.item_code in self.stock_items:
item.item_tax_amount += flt(current_tax_amount,
self.precision.item.item_tax_amount)
self.meta.get_field("item_tax_amount", parentfield=self.fname):
item.item_tax_amount += flt(current_tax_amount, self.precision("item_tax_amount", item))
# update valuation rate
def update_valuation_rate(self, parentfield):
for d in self.doclist.get({"parentfield": parentfield}):
d.conversion_factor = d.conversion_factor or flt(webnotes.conn.get_value(
"UOM Conversion Detail", {"parent": d.item_code, "uom": d.uom},
for item in self.doclist.get({"parentfield": parentfield}):
item.conversion_factor = item.conversion_factor or flt(webnotes.conn.get_value(
"UOM Conversion Detail", {"parent": item.item_code, "uom": item.uom},
"conversion_factor")) or 1
if d.item_code and d.qty:
if item.item_code and item.qty:
self.round_floats_in(item)
purchase_rate = item.rate if self.doc.doctype == "Purchase Invoice" else item.purchase_rate
# if no item code, which is sometimes the case in purchase invoice,
# then it is not possible to track valuation against it
d.valuation_rate = flt(((flt(d.purchase_rate, self.precision.item.purchase_rate) or
flt(d.rate, self.precision.item.rate)) +
(flt(d.item_tax_amount, self.precision.item.item_tax_amount) +
flt(d.rm_supp_cost, self.precision.item.rm_supp_cost)) /
flt(d.qty, self.precision.item.qty)) /
flt(d.conversion_factor, self.precision.item.conversion_factor),
self.precision.item.valuation_rate)
item.valuation_rate = flt((purchase_rate +
(item.item_tax_amount + item.rm_supp_cost) / item.qty) / item.conversion_factor,
self.precision("valuation_rate", item))
else:
d.valuation_rate = 0.0
item.valuation_rate = 0.0
def validate_for_subcontracting(self):
if not self.doc.is_subcontracted and self.sub_contracted_items:
@@ -428,18 +255,6 @@ class BuyingController(StockController):
return bom_items
@property
def precision(self):
if not hasattr(self, "_precision"):
self._precision = webnotes._dict()
self._precision.main = self.meta.get_precision_map()
self._precision.item = self.meta.get_precision_map(parentfield = self.fname)
if self.meta.get_field("purchase_tax_details"):
self._precision.tax = self.meta.get_precision_map(parentfield = \
"purchase_tax_details")
return self._precision
@property
def sub_contracted_items(self):
if not hasattr(self, "_sub_contracted_items"):

View File

@@ -0,0 +1,27 @@
cur_frm.cscript.onload = function(doc, cdt, cdn) {
cur_frm.add_fetch('customer', 'customer_name', 'customer_name');
cur_frm.add_fetch('supplier', 'supplier_name', 'supplier_name');
cur_frm.fields_dict.customer.get_query = erpnext.utils.customer_query;
cur_frm.fields_dict.supplier.get_query = erpnext.utils.supplier_query;
if(doc.__islocal) {
var last_route = wn.route_history.slice(-2, -1)[0];
if(last_route && last_route[0]==="Form") {
if(["Customer", "Quotation", "Sales Order", "Sales Invoice", "Delivery Note",
"Installation Note", "Opportunity", "Customer Issue", "Maintenance Visit",
"Maintenance Schedule"]
.indexOf(last_route[1])!==-1) {
var refdoc = wn.model.get_doc(last_route[1], last_route[2]);
cur_frm.set_value("customer", refdoc.customer || refdoc.name);
cur_frm.set_value("customer_name", refdoc.customer_name);
}
if(["Supplier", "Supplier Quotation", "Purchase Order", "Purchase Invoice", "Purchase Receipt"]
.indexOf(last_route[1])!==-1) {
var customer = wn.model.get_doc(last_route[1], last_route[2]);
cur_frm.set_value("supplier", refdoc.supplier || refdoc.name);
cur_frm.set_value("supplier_name", refdoc.supplier_name);
}
}
}
}

View File

@@ -1 +0,0 @@
[]

View File

@@ -1,8 +0,0 @@
[
" cannot be 0",
"for Company",
"Missing",
"Please enter valid ",
"Please specify a valid",
"Row"
]

View File

@@ -1,12 +0,0 @@
{
" cannot be 0": "\u0644\u0627 \u064a\u0645\u0643\u0646 \u0623\u0646 \u062a\u0643\u0648\u0646 0",
"If": "\u0625\u0630\u0627",
"Please enter valid ": "\u0645\u0646 \u0641\u0636\u0644\u0643 \u0627\u062f\u062e\u0644 \u0635\u0627\u0644\u062d\u0629",
"Please specify a valid": "\u064a\u0631\u062c\u0649 \u062a\u062d\u062f\u064a\u062f \u0635\u0627\u0644\u062d\u0629",
"Row": "\u0635\u0641",
"cannot be included in item's rate": "\u0644\u0627 \u064a\u0645\u0643\u0646 \u0645\u062a\u0636\u0645\u0646\u0629 \u0641\u064a \u0633\u0639\u0631 \u0627\u0644\u0639\u0646\u0635\u0631",
"checked": "\u0641\u062d\u0635",
"is checked for": "\u064a\u062a\u0645 \u0641\u062d\u0635 \u0644\u0644",
"should also have": "\u064a\u0646\u0628\u063a\u064a \u0623\u0646 \u064a\u0643\u0648\u0646 \u0623\u064a\u0636\u0627",
"then": "\u062b\u0645"
}

View File

@@ -1,4 +0,0 @@
{
"Please specify a valid": "Bitte geben Sie eine g\u00fcltige",
"Row": "Reihe"
}

View File

@@ -1,12 +0,0 @@
{
" cannot be 0": "no puede ser 0",
"If": "Si",
"Please enter valid ": "Por favor, introduzca v\u00e1lido",
"Please specify a valid": "Por favor, especifique una direcci\u00f3n v\u00e1lida",
"Row": "Fila",
"cannot be included in item's rate": "no puede ser incluido en la tarifa de elemento",
"checked": "comprobado",
"is checked for": "Se comprueba",
"should also have": "tambi\u00e9n debe tener",
"then": "entonces"
}

View File

@@ -1,12 +0,0 @@
{
" cannot be 0": "ne peut pas \u00eatre \u00e9gal \u00e0 0",
"If": "Si",
"Please enter valid ": "S&#39;il vous pla\u00eet entrez valide",
"Please specify a valid": "S&#39;il vous pla\u00eet sp\u00e9cifier une validit\u00e9",
"Row": "Rang\u00e9e",
"cannot be included in item's rate": "ne peuvent pas \u00eatre inclus dans le prix du produit",
"checked": "v\u00e9rifi\u00e9",
"is checked for": "est v\u00e9rifi\u00e9e pour",
"should also have": "devrait \u00e9galement avoir",
"then": "puis"
}

View File

@@ -1,12 +0,0 @@
{
" cannot be 0": "0 \u0928\u0939\u0940\u0902 \u0939\u094b \u0938\u0915\u0924\u0947 \u0939\u0948\u0902",
"If": "\u0905\u0917\u0930",
"Please enter valid ": "\u0935\u0948\u0927 \u0926\u0930\u094d\u091c \u0915\u0930\u0947\u0902",
"Please specify a valid": "\u090f\u0915 \u0935\u0948\u0927 \u0928\u093f\u0930\u094d\u0926\u093f\u0937\u094d\u091f \u0915\u0930\u0947\u0902",
"Row": "\u092a\u0902\u0915\u094d\u0924\u093f",
"cannot be included in item's rate": "\u0906\u0907\u091f\u092e \u0915\u0940 \u0926\u0930 \u092e\u0947\u0902 \u0936\u093e\u092e\u093f\u0932 \u0928\u0939\u0940\u0902 \u0915\u093f\u092f\u093e \u091c\u093e \u0938\u0915\u0924\u093e",
"checked": "\u0915\u0940 \u091c\u093e\u0901\u091a \u0915\u0940",
"is checked for": "\u0915\u0947 \u0932\u093f\u090f \u091c\u093e\u0901\u091a",
"should also have": "\u092d\u0940 \u0939\u094b\u0928\u093e \u091a\u093e\u0939\u093f\u090f",
"then": "\u0924\u094b"
}

View File

@@ -1,6 +0,0 @@
{
" cannot be 0": "ne mo\u017ee biti 0",
"Please enter valid ": "Unesite va\u017ee\u0107i",
"Please specify a valid": "Navedite va\u017ee\u0107i",
"Row": "Red"
}

View File

@@ -1,12 +0,0 @@
{
" cannot be 0": "mag niet 0",
"If": "Indien",
"Please enter valid ": "Voer een geldige",
"Please specify a valid": "Geef een geldige",
"Row": "Rij",
"cannot be included in item's rate": "kan niet worden opgenomen in het tarief item",
"checked": "geruit",
"is checked for": "wordt gecontroleerd op",
"should also have": "moet ook",
"then": "dan"
}

View File

@@ -1,6 +0,0 @@
{
" cannot be 0": "n\u00e3o pode ser 0",
"Please enter valid ": "Por favor, insira v\u00e1lido",
"Please specify a valid": "Por favor, especifique um v\u00e1lido",
"Row": "Linha"
}

View File

@@ -1,12 +0,0 @@
{
" cannot be 0": "n\u00e3o pode ser 0",
"If": "Se",
"Please enter valid ": "Por favor insira v\u00e1lido",
"Please specify a valid": "Por favor, especifique um v\u00e1lido",
"Row": "Linha",
"cannot be included in item's rate": "n\u00e3o pode ser inclu\u00eddo na taxa do item",
"checked": "verificado",
"is checked for": "est\u00e1 marcada para",
"should also have": "tamb\u00e9m deve ter",
"then": "depois"
}

View File

@@ -1,12 +0,0 @@
{
" cannot be 0": "\u043d\u0435 \u043c\u043e\u0436\u0435 \u0434\u0430 \u0431\u0443\u0434\u0435 0",
"If": "\u0410\u043a\u043e",
"Please enter valid ": "\u041c\u043e\u043b\u0438\u043c\u043e \u0412\u0430\u0441 \u0434\u0430 \u0443\u043d\u0435\u0441\u0435\u0442\u0435 \u0432\u0430\u0436\u0435\u045b\u0438",
"Please specify a valid": "\u041d\u0430\u0432\u0435\u0434\u0438\u0442\u0435 \u0432\u0430\u0436\u0435\u045b\u0438",
"Row": "\u0420\u0435\u0434",
"cannot be included in item's rate": "\u043d\u0435 \u043c\u043e\u0436\u0435 \u0431\u0438\u0442\u0438 \u0443\u043a\u0459\u0443\u0447\u0435\u043d\u0430 \u0443 \u0441\u0442\u043e\u043f\u0438 \u0441\u0442\u0430\u0432\u043a\u0435",
"checked": "\u043f\u0440\u043e\u0432\u0435\u0440\u0435\u043d",
"is checked for": "\u041f\u0440\u043e\u0432\u0435\u0440\u0430\u0432\u0430 \u0441\u0435",
"should also have": "\u0442\u0430\u043a\u043e\u0452\u0435 \u0442\u0440\u0435\u0431\u0430 \u0434\u0430 \u0438\u043c\u0430\u0458\u0443",
"then": "\u043e\u043d\u0434\u0430"
}

View File

@@ -1,12 +0,0 @@
{
" cannot be 0": "0 \u0b87\u0bb0\u0bc1\u0b95\u0bcd\u0b95 \u0bae\u0bc1\u0b9f\u0bbf\u0baf\u0bbe\u0ba4\u0bc1",
"If": "\u0b8e\u0ba9\u0bcd\u0bb1\u0bbe\u0bb2\u0bcd",
"Please enter valid ": "\u0b9a\u0bb0\u0bbf\u0baf\u0bbe\u0ba9 \u0b89\u0bb3\u0bcd\u0bb3\u0bbf\u0b9f\u0bb5\u0bc1\u0bae\u0bcd",
"Please specify a valid": "\u0b9a\u0bb0\u0bbf\u0baf\u0bbe\u0ba9 \u0b95\u0bc1\u0bb1\u0bbf\u0baa\u0bcd\u0baa\u0bbf\u0b9f\u0bb5\u0bc1\u0bae\u0bcd",
"Row": "\u0bb5\u0bb0\u0bbf\u0b9a\u0bc8",
"cannot be included in item's rate": "\u0b89\u0bb0\u0bc1\u0baa\u0bcd\u0baa\u0b9f\u0bbf\u0baf\u0bc8 \u0bb5\u0bc0\u0ba4\u0bae\u0bcd \u0b9a\u0bc7\u0bb0\u0bcd\u0b95\u0bcd\u0b95\u0baa\u0bcd\u0baa\u0b9f\u0bcd\u0b9f\u0bc1\u0bb3\u0bcd\u0bb3\u0ba4\u0bc1 \u0bae\u0bc1\u0b9f\u0bbf\u0baf\u0bbe\u0ba4\u0bc1",
"checked": "\u0b9a\u0ba4\u0bc1\u0bb0 \u0b85\u0bae\u0bc8\u0baa\u0bcd\u0baa\u0bc1 \u0b95\u0bc6\u0bbe\u0ba3\u0bcd\u0b9f\u0bc1\u0bb3\u0bcd\u0bb3",
"is checked for": "\u0b9a\u0bc7\u0bbe\u0ba4\u0bbf\u0b95\u0bcd\u0b95\u0bbf\u0bb1\u0bbe\u0bb0\u0bcd",
"should also have": "\u0bae\u0bc7\u0bb2\u0bc1\u0bae\u0bcd \u0bb5\u0bc7\u0ba3\u0bcd\u0b9f\u0bc1\u0bae\u0bcd",
"then": "\u0b85\u0baa\u0bcd\u0baa\u0bc6\u0bbe\u0bb4\u0bc1\u0ba4\u0bc1"
}

View File

@@ -1,6 +0,0 @@
{
" cannot be 0": "\u0e44\u0e21\u0e48\u0e2a\u0e32\u0e21\u0e32\u0e23\u0e16\u0e40\u0e1b\u0e47\u0e19 0",
"Please enter valid ": "\u0e01\u0e23\u0e38\u0e13\u0e32\u0e01\u0e23\u0e2d\u0e01\u0e17\u0e35\u0e48\u0e16\u0e39\u0e01\u0e15\u0e49\u0e2d\u0e07",
"Please specify a valid": "\u0e42\u0e1b\u0e23\u0e14\u0e23\u0e30\u0e1a\u0e38\u0e17\u0e35\u0e48\u0e16\u0e39\u0e01\u0e15\u0e49\u0e2d\u0e07",
"Row": "\u0e41\u0e16\u0e27"
}

View File

@@ -16,16 +16,60 @@
from __future__ import unicode_literals
import webnotes
from webnotes.utils import cint
from webnotes.utils import cint, flt, comma_or
from setup.utils import get_company_currency
from selling.utils import get_item_details
from webnotes import msgprint, _
from controllers.stock_controller import StockController
class SellingController(StockController):
def validate(self):
super(SellingController, self).validate()
self.set_total_in_words()
def onload_post_render(self):
# contact, address, item details and pos details (if applicable)
self.set_missing_values()
self.set_taxes("Sales Taxes and Charges", "other_charges", "charge")
def set_missing_values(self, for_validate=False):
super(SellingController, self).set_missing_values(for_validate)
self.set_price_list_currency("Selling")
# set contact and address details for customer, if they are not mentioned
self.set_missing_lead_customer_details()
self.set_missing_item_details(get_item_details)
def set_missing_lead_customer_details(self):
if self.doc.customer:
if not (self.doc.contact_person and self.doc.customer_address):
for fieldname, val in self.get_default_address_and_contact("customer").items():
if not self.doc.fields.get(fieldname) and self.meta.get_field(fieldname):
self.doc.fields[fieldname] = val
customer_fetch = webnotes.conn.get_value("Customer", self.doc.customer,
['customer_name', 'customer_group', 'territory'], as_dict=True)
for fieldname in ['customer_name', 'customer_group', 'territory']:
if not self.doc.fields.get(fieldname):
self.doc.fields[fieldname] = customer_fetch[fieldname]
elif self.doc.lead:
lead_fetch = webnotes.conn.get_value("Lead", self.doc.lead,
['company_name', 'lead_name', 'territory'], as_dict=True)
if not self.doc.customer_name:
self.doc.customer_name = lead_fetch.company_name or lead_fetch.lead_name
if not self.doc.territory:
self.doc.territory = lead_fetch.territory
def get_other_charges(self):
self.doclist = self.doc.clear_table(self.doclist, "other_charges")
self.set_taxes("Sales Taxes and Charges", "other_charges", "charge")
def set_customer_defaults(self):
self.get_default_customer_address()
if self.meta.get_field("shipping_address"):
self.doc.fields.update(self.get_shipping_address(self.doc.customer))
def set_total_in_words(self):
from webnotes.utils import money_in_words
@@ -71,4 +115,154 @@ class SellingController(StockController):
if item.buying_amount and not item.cost_center:
msgprint(_("""Cost Center is mandatory for item: """) + item.item_code,
raise_exception=1)
raise_exception=1)
def calculate_taxes_and_totals(self):
self.other_fname = "other_charges"
super(SellingController, self).calculate_taxes_and_totals()
self.calculate_total_advance("Sales Invoice", "advance_adjustment_details")
self.calculate_commission()
self.calculate_contribution()
def determine_exclusive_rate(self):
if not any((cint(tax.included_in_print_rate) for tax in self.tax_doclist)):
# no inclusive tax
return
for item in self.item_doclist:
item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
cumulated_tax_fraction = 0
for i, tax in enumerate(self.tax_doclist):
tax.tax_fraction_for_current_item = self.get_current_tax_fraction(tax, item_tax_map)
if i==0:
tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item
else:
tax.grand_total_fraction_for_current_item = \
self.tax_doclist[i-1].grand_total_fraction_for_current_item \
+ tax.tax_fraction_for_current_item
cumulated_tax_fraction += tax.tax_fraction_for_current_item
if cumulated_tax_fraction:
item.basic_rate = flt((item.export_rate * self.doc.conversion_rate) /
(1 + cumulated_tax_fraction), self.precision("basic_rate", item))
item.amount = flt(item.basic_rate * item.qty, self.precision("amount", item))
if item.adj_rate == 100:
item.base_ref_rate = item.basic_rate
item.basic_rate = 0.0
else:
item.base_ref_rate = flt(item.basic_rate / (1 - (item.adj_rate / 100.0)),
self.precision("base_ref_rate", item))
def get_current_tax_fraction(self, tax, item_tax_map):
"""
Get tax fraction for calculating tax exclusive amount
from tax inclusive amount
"""
current_tax_fraction = 0
if cint(tax.included_in_print_rate):
tax_rate = self._get_tax_rate(tax, item_tax_map)
if tax.charge_type == "On Net Total":
current_tax_fraction = tax_rate / 100.0
elif tax.charge_type == "On Previous Row Amount":
current_tax_fraction = (tax_rate / 100.0) * \
self.tax_doclist[cint(tax.row_id) - 1].tax_fraction_for_current_item
elif tax.charge_type == "On Previous Row Total":
current_tax_fraction = (tax_rate / 100.0) * \
self.tax_doclist[cint(tax.row_id) - 1].grand_total_fraction_for_current_item
return current_tax_fraction
def calculate_item_values(self):
for item in self.item_doclist:
self.round_floats_in(item)
if item.adj_rate == 100:
item.export_rate = 0
elif item.ref_rate:
item.export_rate = flt(item.ref_rate * (1.0 - (item.adj_rate / 100.0)),
self.precision("export_rate", item))
item.export_amount = flt(item.export_rate * item.qty,
self.precision("export_amount", item))
self._set_in_company_currency(item, "ref_rate", "base_ref_rate")
self._set_in_company_currency(item, "export_rate", "basic_rate")
self._set_in_company_currency(item, "export_amount", "amount")
def calculate_net_total(self):
self.doc.net_total = self.doc.net_total_export = 0.0
for item in self.item_doclist:
self.doc.net_total += item.amount
self.doc.net_total_export += item.export_amount
self.round_floats_in(self.doc, ["net_total", "net_total_export"])
def calculate_totals(self):
self.doc.grand_total = flt(self.tax_doclist and \
self.tax_doclist[-1].total or self.doc.net_total, self.precision("grand_total"))
self.doc.grand_total_export = flt(self.doc.grand_total / self.doc.conversion_rate,
self.precision("grand_total_export"))
self.doc.other_charges_total = flt(self.doc.grand_total - self.doc.net_total,
self.precision("other_charges_total"))
self.doc.other_charges_total_export = flt(self.doc.grand_total_export - self.doc.net_total_export,
self.precision("other_charges_total_export"))
self.doc.rounded_total = round(self.doc.grand_total)
self.doc.rounded_total_export = round(self.doc.grand_total_export)
def calculate_outstanding_amount(self):
# NOTE:
# write_off_amount is only for POS Invoice
# total_advance is only for non POS Invoice
if self.doc.doctype == "Sales Invoice" and self.doc.docstatus < 2:
self.round_floats_in(self.doc, ["grand_total", "total_advance", "write_off_amount",
"paid_amount"])
total_amount_to_pay = self.doc.grand_total - self.doc.write_off_amount
self.doc.outstanding_amount = flt(total_amount_to_pay - self.doc.total_advance - self.doc.paid_amount,
self.precision("outstanding_amount"))
def calculate_commission(self):
if self.meta.get_field("commission_rate"):
self.round_floats_in(self.doc, ["net_total", "commission_rate"])
if self.doc.commission_rate > 100.0:
msgprint(_(self.meta.get_label("commission_rate")) + " " +
_("cannot be greater than 100"), raise_exception=True)
self.doc.total_commission = flt(self.doc.net_total * self.doc.commission_rate / 100.0,
self.precision("total_commission"))
def calculate_contribution(self):
total = 0.0
sales_team = self.doclist.get({"parentfield": "sales_team"})
for sales_person in sales_team:
self.round_floats_in(sales_person)
sales_person.allocated_amount = flt(
self.doc.net_total * sales_person.allocated_percentage / 100.0,
self.precision("allocated_amount", sales_person))
total += sales_person.allocated_percentage
if sales_team and total != 100.0:
msgprint(_("Total") + " " +
_(self.meta.get_label("allocated_percentage", parentfield="sales_team")) +
" " + _("should be 100%"), raise_exception=True)
def validate_order_type(self):
valid_types = ["Sales", "Maintenance", "Shopping Cart"]
if self.doc.order_type not in valid_types:
msgprint(_(self.meta.get_label("order_type")) + " " +
_("must be one of") + ": " + comma_or(valid_types),
raise_exception=True)

View File

@@ -0,0 +1,177 @@
# ERPNext - web based ERP (http://erpnext.com)
# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals
import webnotes
from webnotes.utils import flt, cstr
from webnotes import msgprint
from webnotes.model.controller import DocListController
class StatusUpdater(DocListController):
"""
Updates the status of the calling records
Delivery Note: Update Delivered Qty, Update Percent and Validate over delivery
Sales Invoice: Update Billed Amt, Update Percent and Validate over billing
Installation Note: Update Installed Qty, Update Percent Qty and Validate over installation
"""
def update_prevdoc_status(self):
self.update_qty()
self.validate_qty()
def validate_qty(self):
"""
Validates qty at row level
"""
self.tolerance = {}
self.global_tolerance = None
for args in self.status_updater:
# get unique transactions to update
for d in self.doclist:
if d.doctype == args['source_dt'] and d.fields.get(args["join_field"]):
args['name'] = d.fields[args['join_field']]
# get all qty where qty > target_field
item = webnotes.conn.sql("""select item_code, `%(target_ref_field)s`,
`%(target_field)s`, parenttype, parent from `tab%(target_dt)s`
where `%(target_ref_field)s` < `%(target_field)s`
and name="%(name)s" and docstatus=1""" % args, as_dict=1)
if item:
item = item[0]
item['idx'] = d.idx
item['target_ref_field'] = args['target_ref_field'].replace('_', ' ')
if not item[args['target_ref_field']]:
msgprint("""As %(target_ref_field)s for item: %(item_code)s in \
%(parenttype)s: %(parent)s is zero, system will not check \
over-delivery or over-billed""" % item)
elif args.get('no_tolerance'):
item['reduce_by'] = item[args['target_field']] - \
item[args['target_ref_field']]
if item['reduce_by'] > .01:
msgprint("""
Row #%(idx)s: Max %(target_ref_field)s allowed for <b>Item \
%(item_code)s</b> against <b>%(parenttype)s %(parent)s</b> \
is <b>""" % item + cstr(item[args['target_ref_field']]) +
"""</b>.<br>You must reduce the %(target_ref_field)s by \
%(reduce_by)s""" % item, raise_exception=1)
else:
self.check_overflow_with_tolerance(item, args)
def check_overflow_with_tolerance(self, item, args):
"""
Checks if there is overflow condering a relaxation tolerance
"""
# check if overflow is within tolerance
tolerance = self.get_tolerance_for(item['item_code'])
overflow_percent = ((item[args['target_field']] - item[args['target_ref_field']]) /
item[args['target_ref_field']]) * 100
if overflow_percent - tolerance > 0.01:
item['max_allowed'] = flt(item[args['target_ref_field']] * (100+tolerance)/100)
item['reduce_by'] = item[args['target_field']] - item['max_allowed']
msgprint("""
Row #%(idx)s: Max %(target_ref_field)s allowed for <b>Item %(item_code)s</b> \
against <b>%(parenttype)s %(parent)s</b> is <b>%(max_allowed)s</b>.
If you want to increase your overflow tolerance, please increase tolerance %% in \
Global Defaults or Item master.
Or, you must reduce the %(target_ref_field)s by %(reduce_by)s
Also, please check if the order item has already been billed in the Sales Order""" %
item, raise_exception=1)
def get_tolerance_for(self, item_code):
"""
Returns the tolerance for the item, if not set, returns global tolerance
"""
if self.tolerance.get(item_code): return self.tolerance[item_code]
tolerance = flt(webnotes.conn.get_value('Item',item_code,'tolerance') or 0)
if not tolerance:
if self.global_tolerance == None:
self.global_tolerance = flt(webnotes.conn.get_value('Global Defaults', None,
'tolerance'))
tolerance = self.global_tolerance
self.tolerance[item_code] = tolerance
return tolerance
def update_qty(self, change_modified=True):
"""
Updates qty at row level
"""
for args in self.status_updater:
# condition to include current record (if submit or no if cancel)
if self.doc.docstatus == 1:
args['cond'] = ' or parent="%s"' % self.doc.name
else:
args['cond'] = ' and parent!="%s"' % self.doc.name
args['modified_cond'] = ''
if change_modified:
args['modified_cond'] = ', modified = now()'
# update quantities in child table
for d in self.doclist:
if d.doctype == args['source_dt']:
# updates qty in the child table
args['detail_id'] = d.fields.get(args['join_field'])
args['second_source_condition'] = ""
if args.get('second_source_dt') and args.get('second_source_field') \
and args.get('second_join_field'):
args['second_source_condition'] = """ + (select sum(%(second_source_field)s)
from `tab%(second_source_dt)s`
where `%(second_join_field)s`="%(detail_id)s"
and (docstatus=1))""" % args
if args['detail_id']:
webnotes.conn.sql("""update `tab%(target_dt)s`
set %(target_field)s = (select sum(%(source_field)s)
from `tab%(source_dt)s` where `%(join_field)s`="%(detail_id)s"
and (docstatus=1 %(cond)s)) %(second_source_condition)s
where name='%(detail_id)s'""" % args)
# get unique transactions to update
for name in set([d.fields.get(args['percent_join_field']) for d in self.doclist
if d.doctype == args['source_dt']]):
if name:
args['name'] = name
# update percent complete in the parent table
webnotes.conn.sql("""update `tab%(target_parent_dt)s`
set %(target_parent_field)s = (select sum(if(%(target_ref_field)s >
ifnull(%(target_field)s, 0), %(target_field)s,
%(target_ref_field)s))/sum(%(target_ref_field)s)*100
from `tab%(target_dt)s` where parent="%(name)s") %(modified_cond)s
where name='%(name)s'""" % args)
# update field
if args.get('status_field'):
webnotes.conn.sql("""update `tab%(target_parent_dt)s`
set %(status_field)s = if(ifnull(%(target_parent_field)s,0)<0.001,
'Not %(keyword)s', if(%(target_parent_field)s>=99.99,
'Fully %(keyword)s', 'Partly %(keyword)s'))
where name='%(name)s'""" % args)