Replaced doc, doc.fields frappe/frappe#478

This commit is contained in:
Anand Doshi
2014-03-28 13:55:00 +05:30
parent 67d6a4e3aa
commit f78d1aee28
142 changed files with 2443 additions and 2444 deletions

View File

@@ -24,10 +24,10 @@ class AccountsController(TransactionBase):
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]
if not self.get(fieldname) and self.meta.get_field(fieldname):
self.set(fieldname, today())
if not self.fiscal_year:
self.fiscal_year = get_fiscal_year(self.fields[fieldname])[0]
def validate_date_with_fiscal_year(self):
if self.meta.get_field("fiscal_year") :
@@ -37,16 +37,16 @@ class AccountsController(TransactionBase):
elif self.meta.get_field("transaction_date"):
date_field = "transaction_date"
if date_field and self.doc.fields[date_field]:
validate_fiscal_year(self.doc.fields[date_field], self.doc.fiscal_year,
if date_field and self.fields[date_field]:
validate_fiscal_year(self.fields[date_field], self.fiscal_year,
label=self.meta.get_label(date_field))
def validate_for_freezed_account(self):
for fieldname in ["customer", "supplier"]:
if self.meta.get_field(fieldname) and self.doc.fields.get(fieldname):
if self.meta.get_field(fieldname) and self.get(fieldname):
accounts = frappe.db.get_values("Account",
{"master_type": fieldname.title(), "master_name": self.doc.fields[fieldname],
"company": self.doc.company}, "name")
{"master_type": fieldname.title(), "master_name": self.fields[fieldname],
"company": self.company}, "name")
if accounts:
from erpnext.accounts.doctype.gl_entry.gl_entry import validate_frozen_account
for account in accounts:
@@ -54,30 +54,30 @@ class AccountsController(TransactionBase):
def set_price_list_currency(self, buying_or_selling):
if self.meta.get_field("currency"):
company_currency = get_company_currency(self.doc.company)
company_currency = get_company_currency(self.company)
# price list part
fieldname = "selling_price_list" if buying_or_selling.lower() == "selling" \
else "buying_price_list"
if self.meta.get_field(fieldname) and self.doc.fields.get(fieldname):
self.doc.price_list_currency = frappe.db.get_value("Price List",
self.doc.fields.get(fieldname), "currency")
if self.meta.get_field(fieldname) and self.get(fieldname):
self.price_list_currency = frappe.db.get_value("Price List",
self.get(fieldname), "currency")
if self.doc.price_list_currency == company_currency:
self.doc.plc_conversion_rate = 1.0
if self.price_list_currency == company_currency:
self.plc_conversion_rate = 1.0
elif not self.doc.plc_conversion_rate:
self.doc.plc_conversion_rate = self.get_exchange_rate(
self.doc.price_list_currency, company_currency)
elif not self.plc_conversion_rate:
self.plc_conversion_rate = self.get_exchange_rate(
self.price_list_currency, company_currency)
# currency
if not self.doc.currency:
self.doc.currency = self.doc.price_list_currency
self.doc.conversion_rate = self.doc.plc_conversion_rate
elif self.doc.currency == company_currency:
self.doc.conversion_rate = 1.0
elif not self.doc.conversion_rate:
self.doc.conversion_rate = self.get_exchange_rate(self.doc.currency,
if not self.currency:
self.currency = self.price_list_currency
self.conversion_rate = self.plc_conversion_rate
elif self.currency == company_currency:
self.conversion_rate = 1.0
elif not self.conversion_rate:
self.conversion_rate = self.get_exchange_rate(self.currency,
company_currency)
def get_exchange_rate(self, from_currency, to_currency):
@@ -88,14 +88,14 @@ class AccountsController(TransactionBase):
"""set missing item values"""
from erpnext.stock.get_item_details import get_item_details
for item in self.get(self.fname):
if item.fields.get("item_code"):
if item.get("item_code"):
args = item.fields.copy()
args.update(self.doc.fields)
args.update(self.fields)
ret = get_item_details(args)
for fieldname, value in ret.items():
if self.meta.get_field(fieldname, parentfield=self.fname) and \
item.fields.get(fieldname) is None and value is not None:
item.fields[fieldname] = value
item.get(fieldname) is None and value is not None:
item.set(fieldname, value)
def set_taxes(self, tax_parentfield, tax_master_field):
if not self.meta.get_field(tax_parentfield):
@@ -104,22 +104,21 @@ class AccountsController(TransactionBase):
tax_master_doctype = self.meta.get_field(tax_master_field).options
if not self.get(tax_parentfield):
if not self.doc.fields.get(tax_master_field):
if not self.get(tax_master_field):
# get the default tax master
self.doc.fields[tax_master_field] = \
frappe.db.get_value(tax_master_doctype, {"is_default": 1})
self.set(tax_master_field, frappe.db.get_value(tax_master_doctype, {"is_default": 1}))
self.append_taxes_from_master(tax_parentfield, tax_master_field, tax_master_doctype)
def append_taxes_from_master(self, tax_parentfield, tax_master_field, tax_master_doctype=None):
if self.doc.fields.get(tax_master_field):
if self.get(tax_master_field):
if not tax_master_doctype:
tax_master_doctype = self.meta.get_field(tax_master_field).options
tax_doctype = self.meta.get_field(tax_parentfield).options
from frappe.model import default_fields
tax_master = frappe.bean(tax_master_doctype, self.doc.fields.get(tax_master_field))
tax_master = frappe.bean(tax_master_doctype, self.get(tax_master_field))
for i, tax in enumerate(tax_master.get(tax_parentfield)):
for fieldname in default_fields:
@@ -140,16 +139,16 @@ class AccountsController(TransactionBase):
def _calculate_taxes_and_totals(self):
# validate conversion rate
company_currency = get_company_currency(self.doc.company)
if not self.doc.currency or self.doc.currency == company_currency:
self.doc.currency = company_currency
self.doc.conversion_rate = 1.0
company_currency = get_company_currency(self.company)
if not self.currency or self.currency == company_currency:
self.currency = company_currency
self.conversion_rate = 1.0
else:
from erpnext.setup.doctype.currency.currency import validate_conversion_rate
validate_conversion_rate(self.doc.currency, self.doc.conversion_rate,
self.meta.get_label("conversion_rate"), self.doc.company)
validate_conversion_rate(self.currency, self.conversion_rate,
self.meta.get_label("conversion_rate"), self.company)
self.doc.conversion_rate = flt(self.doc.conversion_rate)
self.conversion_rate = flt(self.conversion_rate)
self.item_doclist = self.get(self.fname)
self.tax_doclist = self.get(self.other_fname)
@@ -175,7 +174,7 @@ class AccountsController(TransactionBase):
tax_fields.append("tax_amount")
for fieldname in tax_fields:
tax.fields[fieldname] = 0.0
tax.set(fieldname, 0.0)
self.validate_on_previous_row(tax)
self.validate_inclusive_tax(tax)
@@ -297,7 +296,7 @@ class AccountsController(TransactionBase):
self.precision("tax_amount", tax))
def adjust_discount_amount_loss(self, tax):
discount_amount_loss = self.doc.grand_total - flt(self.doc.discount_amount) - tax.total
discount_amount_loss = self.grand_total - flt(self.discount_amount) - tax.total
tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
discount_amount_loss, self.precision("tax_amount", tax))
tax.total = flt(tax.total + discount_amount_loss, self.precision("total", tax))
@@ -309,8 +308,8 @@ class AccountsController(TransactionBase):
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.base_amount / self.doc.net_total) * actual)
current_tax_amount = (self.net_total
and ((item.base_amount / self.net_total) * actual)
or 0)
elif tax.charge_type == "On Net Total":
current_tax_amount = (tax_rate / 100.0) * item.base_amount
@@ -355,32 +354,32 @@ class AccountsController(TransactionBase):
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),
item.set(base_field, flt((flt(item.fields[print_field],)
self.precision(print_field, item)) * self.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:
if self.doctype == parenttype and self.docstatus < 2:
sum_of_allocated_amount = sum([flt(adv.allocated_amount, self.precision("allocated_amount", adv))
for adv in self.get(advance_parentfield)])
self.doc.total_advance = flt(sum_of_allocated_amount, self.precision("total_advance"))
self.total_advance = flt(sum_of_allocated_amount, self.precision("total_advance"))
self.calculate_outstanding_amount()
def get_gl_dict(self, args):
"""this method populates the common properties of a gl entry record"""
gl_dict = frappe._dict({
'company': self.doc.company,
'posting_date': self.doc.posting_date,
'voucher_type': self.doc.doctype,
'voucher_no': self.doc.name,
'aging_date': self.doc.fields.get("aging_date") or self.doc.posting_date,
'remarks': self.doc.remarks,
'fiscal_year': self.doc.fiscal_year,
'company': self.company,
'posting_date': self.posting_date,
'voucher_type': self.doctype,
'voucher_no': self.name,
'aging_date': self.get("aging_date") or self.posting_date,
'remarks': self.remarks,
'fiscal_year': self.fiscal_year,
'debit': 0,
'credit': 0,
'is_opening': self.doc.fields.get("is_opening") or "No",
'is_opening': self.get("is_opening") or "No",
})
gl_dict.update(args)
return gl_dict
@@ -389,7 +388,7 @@ class AccountsController(TransactionBase):
self.doclist.remove_items({"parentfield": parentfield, "allocated_amount": ["in", [0, None, ""]]})
frappe.db.sql("""delete from `tab%s` where parentfield=%s and parent = %s
and ifnull(allocated_amount, 0) = 0""" % (childtype, '%s', '%s'), (parentfield, self.doc.name))
and ifnull(allocated_amount, 0) = 0""" % (childtype, '%s', '%s'), (parentfield, self.name))
def get_advances(self, account_head, child_doctype, parentfield, dr_or_cr):
res = frappe.db.sql("""select t1.name as jv_no, t1.remark,
@@ -419,7 +418,7 @@ class AccountsController(TransactionBase):
global_tolerance = None
for item in self.get("entries"):
if item.fields.get(item_ref_dn):
if item.get(item_ref_dn):
ref_amt = flt(frappe.db.get_value(ref_dt + " Item",
item.fields[item_ref_dn], based_on), self.precision(based_on, item))
if not ref_amt:
@@ -429,7 +428,7 @@ class AccountsController(TransactionBase):
already_billed = frappe.db.sql("""select sum(%s) from `tab%s`
where %s=%s and docstatus=1 and parent != %s""" %
(based_on, self.tname, item_ref_dn, '%s', '%s'),
(item.fields[item_ref_dn], self.doc.name))[0][0]
(item.fields[item_ref_dn], self.name))[0][0]
total_billed_amt = flt(flt(already_billed) + flt(item.fields[based_on]),
self.precision(based_on, item))
@@ -455,7 +454,7 @@ class AccountsController(TransactionBase):
def get_company_default(self, fieldname):
from erpnext.accounts.utils import get_company_default
return get_company_default(self.doc.company, fieldname)
return get_company_default(self.company, fieldname)
def get_stock_items(self):
stock_items = []
@@ -471,7 +470,7 @@ class AccountsController(TransactionBase):
@property
def company_abbr(self):
if not hasattr(self, "_abbr"):
self._abbr = frappe.db.get_value("Company", self.doc.company, "abbr")
self._abbr = frappe.db.get_value("Company", self.company, "abbr")
return self._abbr

View File

@@ -17,9 +17,9 @@ class BuyingController(StockController):
def validate(self):
super(BuyingController, self).validate()
if self.doc.supplier and not self.doc.supplier_name:
self.doc.supplier_name = frappe.db.get_value("Supplier",
self.doc.supplier, "supplier_name")
if self.supplier and not self.supplier_name:
self.supplier_name = frappe.db.get_value("Supplier",
self.supplier, "supplier_name")
self.is_item_table_empty()
self.validate_stock_or_nonstock_items()
self.validate_warehouse()
@@ -31,19 +31,19 @@ class BuyingController(StockController):
self.set_price_list_currency("Buying")
# set contact and address details for supplier, if they are not mentioned
if self.doc.supplier:
self.doc.update_if_missing(get_party_details(self.doc.supplier, party_type="Supplier"))
if self.supplier:
self.update_if_missing(get_party_details(self.supplier, party_type="Supplier"))
self.set_missing_item_details()
if self.doc.fields.get("__islocal"):
if self.get("__islocal"):
self.set_taxes("other_charges", "taxes_and_charges")
def set_supplier_from_item_default(self):
if self.meta.get_field("supplier") and not self.doc.supplier:
if self.meta.get_field("supplier") and not self.supplier:
for d in self.doclist.get({"doctype": self.tname}):
supplier = frappe.db.get_value("Item", d.item_code, "default_supplier")
if supplier:
self.doc.supplier = supplier
self.supplier = supplier
break
def validate_warehouse(self):
@@ -53,7 +53,7 @@ class BuyingController(StockController):
self.doclist.get({"doctype": self.tname}) if d.warehouse]))
for w in warehouses:
validate_warehouse_company(w, self.doc.company)
validate_warehouse_company(w, self.company)
def validate_stock_or_nonstock_items(self):
if not self.get_stock_items():
@@ -65,12 +65,12 @@ class BuyingController(StockController):
def set_total_in_words(self):
from frappe.utils import money_in_words
company_currency = get_company_currency(self.doc.company)
company_currency = get_company_currency(self.company)
if self.meta.get_field("in_words"):
self.doc.in_words = money_in_words(self.doc.grand_total, company_currency)
self.in_words = money_in_words(self.grand_total, company_currency)
if self.meta.get_field("in_words_import"):
self.doc.in_words_import = money_in_words(self.doc.grand_total_import,
self.doc.currency)
self.in_words_import = money_in_words(self.grand_total_import,
self.currency)
def calculate_taxes_and_totals(self):
self.other_fname = "other_charges"
@@ -97,54 +97,54 @@ class BuyingController(StockController):
def calculate_net_total(self):
self.doc.net_total = self.doc.net_total_import = 0.0
self.net_total = self.net_total_import = 0.0
for item in self.item_doclist:
self.doc.net_total += item.base_amount
self.doc.net_total_import += item.amount
self.net_total += item.base_amount
self.net_total_import += item.amount
self.round_floats_in(self.doc, ["net_total", "net_total_import"])
def calculate_totals(self):
self.doc.grand_total = flt(self.tax_doclist[-1].total if self.tax_doclist
else self.doc.net_total, self.precision("grand_total"))
self.doc.grand_total_import = flt(self.doc.grand_total / self.doc.conversion_rate,
self.grand_total = flt(self.tax_doclist[-1].total if self.tax_doclist
else self.net_total, self.precision("grand_total"))
self.grand_total_import = flt(self.grand_total / self.conversion_rate,
self.precision("grand_total_import"))
self.doc.total_tax = flt(self.doc.grand_total - self.doc.net_total,
self.total_tax = flt(self.grand_total - self.net_total,
self.precision("total_tax"))
if self.meta.get_field("rounded_total"):
self.doc.rounded_total = _round(self.doc.grand_total)
self.rounded_total = _round(self.grand_total)
if self.meta.get_field("rounded_total_import"):
self.doc.rounded_total_import = _round(self.doc.grand_total_import)
self.rounded_total_import = _round(self.grand_total_import)
if self.meta.get_field("other_charges_added"):
self.doc.other_charges_added = flt(sum([flt(d.tax_amount) for d in self.tax_doclist
self.other_charges_added = flt(sum([flt(d.tax_amount) for d in self.tax_doclist
if d.add_deduct_tax=="Add" and d.category in ["Valuation and Total", "Total"]]),
self.precision("other_charges_added"))
if self.meta.get_field("other_charges_deducted"):
self.doc.other_charges_deducted = flt(sum([flt(d.tax_amount) for d in self.tax_doclist
self.other_charges_deducted = flt(sum([flt(d.tax_amount) for d in self.tax_doclist
if d.add_deduct_tax=="Deduct" and d.category in ["Valuation and Total", "Total"]]),
self.precision("other_charges_deducted"))
if self.meta.get_field("other_charges_added_import"):
self.doc.other_charges_added_import = flt(self.doc.other_charges_added /
self.doc.conversion_rate, self.precision("other_charges_added_import"))
self.other_charges_added_import = flt(self.other_charges_added /
self.conversion_rate, self.precision("other_charges_added_import"))
if self.meta.get_field("other_charges_deducted_import"):
self.doc.other_charges_deducted_import = flt(self.doc.other_charges_deducted /
self.doc.conversion_rate, self.precision("other_charges_deducted_import"))
self.other_charges_deducted_import = flt(self.other_charges_deducted /
self.conversion_rate, self.precision("other_charges_deducted_import"))
def calculate_outstanding_amount(self):
if self.doc.doctype == "Purchase Invoice" and self.doc.docstatus == 0:
self.doc.total_advance = flt(self.doc.total_advance,
if self.doctype == "Purchase Invoice" and self.docstatus == 0:
self.total_advance = flt(self.total_advance,
self.precision("total_advance"))
self.doc.total_amount_to_pay = flt(self.doc.grand_total - flt(self.doc.write_off_amount,
self.total_amount_to_pay = flt(self.grand_total - flt(self.write_off_amount,
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.outstanding_amount = flt(self.total_amount_to_pay - self.total_advance,
self.precision("outstanding_amount"))
def _cleanup(self):
@@ -208,18 +208,18 @@ class BuyingController(StockController):
item.valuation_rate = 0.0
def validate_for_subcontracting(self):
if not self.doc.is_subcontracted and self.sub_contracted_items:
if not self.is_subcontracted and self.sub_contracted_items:
frappe.msgprint(_("""Please enter whether %s is made for subcontracting or purchasing,
in 'Is Subcontracted' field""" % self.doc.doctype), raise_exception=1)
in 'Is Subcontracted' field""" % self.doctype), raise_exception=1)
if self.doc.doctype == "Purchase Receipt" and self.doc.is_subcontracted=="Yes" \
and not self.doc.supplier_warehouse:
if self.doctype == "Purchase Receipt" and self.is_subcontracted=="Yes" \
and not self.supplier_warehouse:
frappe.msgprint(_("Supplier Warehouse mandatory subcontracted purchase receipt"),
raise_exception=1)
def update_raw_materials_supplied(self, raw_material_table):
self.set(raw_material_table, [])
if self.doc.is_subcontracted=="Yes":
if self.is_subcontracted=="Yes":
for item in self.get(self.fname):
if item.item_code in self.sub_contracted_items:
self.add_bom_items(item, raw_material_table)
@@ -230,7 +230,7 @@ class BuyingController(StockController):
for item in bom_items:
required_qty = flt(item.qty_consumed_per_unit) * flt(d.qty) * flt(d.conversion_factor)
rm_doclist = {
"doctype": self.doc.doctype + " Item Supplied",
"doctype": self.doctype + " Item Supplied",
"reference_name": d.name,
"bom_detail_no": item.name,
"main_item_code": d.item_code,
@@ -241,7 +241,7 @@ class BuyingController(StockController):
"rate": item.rate,
"amount": required_qty * flt(item.rate)
}
if self.doc.doctype == "Purchase Receipt":
if self.doctype == "Purchase Receipt":
rm_doclist.update({
"consumed_qty": required_qty,
"description": item.description,
@@ -251,7 +251,7 @@ class BuyingController(StockController):
raw_materials_cost += required_qty * flt(item.rate)
if self.doc.doctype == "Purchase Receipt":
if self.doctype == "Purchase Receipt":
d.rm_supp_cost = raw_materials_cost
def get_items_from_default_bom(self, item_code):

View File

@@ -28,27 +28,27 @@ class SellingController(StockController):
# set contact and address details for customer, if they are not mentioned
self.set_missing_lead_customer_details()
self.set_price_list_and_item_details()
if self.doc.fields.get("__islocal"):
if self.get("__islocal"):
self.set_taxes("other_charges", "taxes_and_charges")
def set_missing_lead_customer_details(self):
if self.doc.customer:
if self.customer:
from erpnext.accounts.party import _get_party_details
self.doc.update_if_missing(_get_party_details(self.doc.customer,
self.update_if_missing(_get_party_details(self.customer,
ignore_permissions=self.bean.ignore_permissions))
elif self.doc.lead:
elif self.lead:
from erpnext.selling.doctype.lead.lead import get_lead_details
self.doc.update_if_missing(get_lead_details(self.doc.lead))
self.update_if_missing(get_lead_details(self.lead))
def set_price_list_and_item_details(self):
self.set_price_list_currency("Selling")
self.set_missing_item_details()
def apply_shipping_rule(self):
if self.doc.shipping_rule:
shipping_rule = frappe.bean("Shipping Rule", self.doc.shipping_rule)
value = self.doc.net_total
if self.shipping_rule:
shipping_rule = frappe.bean("Shipping Rule", self.shipping_rule)
value = self.net_total
# TODO
# shipping rule calculation based on item's net weight
@@ -62,25 +62,25 @@ class SellingController(StockController):
self.append("other_charges", {
"doctype": "Sales Taxes and Charges",
"charge_type": "Actual",
"account_head": shipping_rule.doc.account,
"cost_center": shipping_rule.doc.cost_center,
"description": shipping_rule.doc.label,
"account_head": shipping_rule.account,
"cost_center": shipping_rule.cost_center,
"description": shipping_rule.label,
"rate": shipping_amount
})
def set_total_in_words(self):
from frappe.utils import money_in_words
company_currency = get_company_currency(self.doc.company)
company_currency = get_company_currency(self.company)
disable_rounded_total = cint(frappe.db.get_value("Global Defaults", None,
"disable_rounded_total"))
if self.meta.get_field("in_words"):
self.doc.in_words = money_in_words(disable_rounded_total and
self.doc.grand_total or self.doc.rounded_total, company_currency)
self.in_words = money_in_words(disable_rounded_total and
self.grand_total or self.rounded_total, company_currency)
if self.meta.get_field("in_words_export"):
self.doc.in_words_export = money_in_words(disable_rounded_total and
self.doc.grand_total_export or self.doc.rounded_total_export, self.doc.currency)
self.in_words_export = money_in_words(disable_rounded_total and
self.grand_total_export or self.rounded_total_export, self.currency)
def calculate_taxes_and_totals(self):
self.other_fname = "other_charges"
@@ -112,7 +112,7 @@ class SellingController(StockController):
cumulated_tax_fraction += tax.tax_fraction_for_current_item
if cumulated_tax_fraction and not self.discount_amount_applied:
item.base_amount = flt((item.amount * self.doc.conversion_rate) /
item.base_amount = flt((item.amount * self.conversion_rate) /
(1 + cumulated_tax_fraction), self.precision("base_amount", item))
item.base_rate = flt(item.base_amount / item.qty, self.precision("base_rate", item))
@@ -166,38 +166,38 @@ class SellingController(StockController):
self._set_in_company_currency(item, "amount", "base_amount")
def calculate_net_total(self):
self.doc.net_total = self.doc.net_total_export = 0.0
self.net_total = self.net_total_export = 0.0
for item in self.item_doclist:
self.doc.net_total += item.base_amount
self.doc.net_total_export += item.amount
self.net_total += item.base_amount
self.net_total_export += item.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.grand_total = flt(self.tax_doclist and \
self.tax_doclist[-1].total or self.net_total, self.precision("grand_total"))
self.grand_total_export = flt(self.grand_total / self.conversion_rate,
self.precision("grand_total_export"))
self.doc.other_charges_total = flt(self.doc.grand_total - self.doc.net_total,
self.other_charges_total = flt(self.grand_total - self.net_total,
self.precision("other_charges_total"))
self.doc.other_charges_total_export = flt(self.doc.grand_total_export -
self.doc.net_total_export + flt(self.doc.discount_amount),
self.other_charges_total_export = flt(self.grand_total_export -
self.net_total_export + flt(self.discount_amount),
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)
self.rounded_total = _round(self.grand_total)
self.rounded_total_export = _round(self.grand_total_export)
def apply_discount_amount(self):
if self.doc.discount_amount:
if self.discount_amount:
grand_total_for_discount_amount = self.get_grand_total_for_discount_amount()
if grand_total_for_discount_amount:
# calculate item amount after Discount Amount
for item in self.item_doclist:
distributed_amount = flt(self.doc.discount_amount) * item.base_amount / grand_total_for_discount_amount
distributed_amount = flt(self.discount_amount) * item.base_amount / grand_total_for_discount_amount
item.base_amount = flt(item.base_amount - distributed_amount, self.precision("base_amount", item))
self.discount_amount_applied = True
@@ -214,7 +214,7 @@ class SellingController(StockController):
flt(tax.rate) / 100
actual_taxes_dict.setdefault(tax.idx, actual_tax_amount)
grand_total_for_discount_amount = flt(self.doc.grand_total - sum(actual_taxes_dict.values()),
grand_total_for_discount_amount = flt(self.grand_total - sum(actual_taxes_dict.values()),
self.precision("grand_total"))
return grand_total_for_discount_amount
@@ -222,21 +222,21 @@ class SellingController(StockController):
# 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 == 0:
if self.doctype == "Sales Invoice" and self.docstatus == 0:
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"))
total_amount_to_pay = self.grand_total - self.write_off_amount
self.outstanding_amount = flt(total_amount_to_pay - self.total_advance \
- self.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:
if self.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.total_commission = flt(self.net_total * self.commission_rate / 100.0,
self.precision("total_commission"))
def calculate_contribution(self):
@@ -246,7 +246,7 @@ class SellingController(StockController):
self.round_floats_in(sales_person)
sales_person.allocated_amount = flt(
self.doc.net_total * sales_person.allocated_percentage / 100.0,
self.net_total * sales_person.allocated_percentage / 100.0,
self.precision("allocated_amount", sales_person))
total += sales_person.allocated_percentage
@@ -258,15 +258,15 @@ class SellingController(StockController):
def validate_order_type(self):
valid_types = ["Sales", "Maintenance", "Shopping Cart"]
if not self.doc.order_type:
self.doc.order_type = "Sales"
elif self.doc.order_type not in valid_types:
if not self.order_type:
self.order_type = "Sales"
elif self.order_type not in valid_types:
msgprint(_(self.meta.get_label("order_type")) + " " +
_("must be one of") + ": " + comma_or(valid_types), raise_exception=True)
def check_credit(self, grand_total):
customer_account = frappe.db.get_value("Account", {"company": self.doc.company,
"master_name": self.doc.customer}, "name")
customer_account = frappe.db.get_value("Account", {"company": self.company,
"master_name": self.customer}, "name")
if customer_account:
total_outstanding = frappe.db.sql("""select
sum(ifnull(debit, 0)) - sum(ifnull(credit, 0))
@@ -291,7 +291,7 @@ class SellingController(StockController):
reserved_warehouse = ""
reserved_qty_for_main_item = 0
if self.doc.doctype == "Sales Order":
if self.doctype == "Sales Order":
if (frappe.db.get_value("Item", d.item_code, "is_stock_item") == 'Yes' or
self.has_sales_bom(d.item_code)) and not d.warehouse:
frappe.throw(_("Please enter Reserved Warehouse for item ") +
@@ -300,11 +300,11 @@ class SellingController(StockController):
if flt(d.qty) > flt(d.delivered_qty):
reserved_qty_for_main_item = flt(d.qty) - flt(d.delivered_qty)
if self.doc.doctype == "Delivery Note" and d.against_sales_order:
if self.doctype == "Delivery Note" and d.against_sales_order:
# if SO qty is 10 and there is tolerance of 20%, then it will allow DN of 12.
# But in this case reserved qty should only be reduced by 10 and not 12
already_delivered_qty = self.get_already_delivered_qty(self.doc.name,
already_delivered_qty = self.get_already_delivered_qty(self.name,
d.against_sales_order, d.prevdoc_detail_docname)
so_qty, reserved_warehouse = self.get_so_qty_and_warehouse(d.prevdoc_detail_docname)
@@ -362,10 +362,10 @@ class SellingController(StockController):
def check_stop_sales_order(self, ref_fieldname):
for d in self.get(self.fname):
if d.fields.get(ref_fieldname):
if d.get(ref_fieldname):
status = frappe.db.get_value("Sales Order", d.fields[ref_fieldname], "status")
if status == "Stopped":
frappe.throw(self.doc.doctype +
frappe.throw(self.doctype +
_(" can not be created/modified against stopped Sales Order ") +
d.fields[ref_fieldname])

View File

@@ -25,27 +25,27 @@ status_map = {
],
"Opportunity": [
["Draft", None],
["Submitted", "eval:self.doc.docstatus==1"],
["Lost", "eval:self.doc.status=='Lost'"],
["Submitted", "eval:self.docstatus==1"],
["Lost", "eval:self.status=='Lost'"],
["Quotation", "has_quotation"],
["Replied", "communication_sent"],
["Cancelled", "eval:self.doc.docstatus==2"],
["Cancelled", "eval:self.docstatus==2"],
["Open", "communication_received"],
],
"Quotation": [
["Draft", None],
["Submitted", "eval:self.doc.docstatus==1"],
["Lost", "eval:self.doc.status=='Lost'"],
["Submitted", "eval:self.docstatus==1"],
["Lost", "eval:self.status=='Lost'"],
["Ordered", "has_sales_order"],
["Replied", "communication_sent"],
["Cancelled", "eval:self.doc.docstatus==2"],
["Cancelled", "eval:self.docstatus==2"],
["Open", "communication_received"],
],
"Sales Order": [
["Draft", None],
["Submitted", "eval:self.doc.docstatus==1"],
["Stopped", "eval:self.doc.status=='Stopped'"],
["Cancelled", "eval:self.doc.docstatus==2"],
["Submitted", "eval:self.docstatus==1"],
["Stopped", "eval:self.status=='Stopped'"],
["Cancelled", "eval:self.docstatus==2"],
],
"Support Ticket": [
["Replied", "communication_sent"],
@@ -66,26 +66,26 @@ class StatusUpdater(DocListController):
self.validate_qty()
def set_status(self, update=False):
if self.doc.get("__islocal"):
if self.get("__islocal"):
return
if self.doc.doctype in status_map:
sl = status_map[self.doc.doctype][:]
if self.doctype in status_map:
sl = status_map[self.doctype][:]
sl.reverse()
for s in sl:
if not s[1]:
self.doc.status = s[0]
self.status = s[0]
break
elif s[1].startswith("eval:"):
if eval(s[1][5:]):
self.doc.status = s[0]
self.status = s[0]
break
elif getattr(self, s[1])():
self.doc.status = s[0]
self.status = s[0]
break
if update:
frappe.db.set_value(self.doc.doctype, self.doc.name, "status", self.doc.status)
frappe.db.set_value(self.doctype, self.name, "status", self.status)
def on_communication(self):
self.communication_set = True
@@ -114,7 +114,7 @@ class StatusUpdater(DocListController):
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"]):
if d.doctype == args['source_dt'] and d.get(args["join_field"]):
args['name'] = d.fields[args['join_field']]
# get all qty where qty > target_field
@@ -181,10 +181,10 @@ class StatusUpdater(DocListController):
"""
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.replace('"', '\"')
if self.docstatus == 1:
args['cond'] = ' or parent="%s"' % self.name.replace('"', '\"')
else:
args['cond'] = ' and parent!="%s"' % self.doc.name.replace('"', '\"')
args['cond'] = ' and parent!="%s"' % self.name.replace('"', '\"')
args['modified_cond'] = ''
if change_modified:
@@ -194,7 +194,7 @@ class StatusUpdater(DocListController):
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['detail_id'] = d.get(args['join_field'])
args['second_source_condition'] = ""
if args.get('second_source_dt') and args.get('second_source_field') \
@@ -212,7 +212,7 @@ class StatusUpdater(DocListController):
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
for name in set([d.get(args['percent_join_field']) for d in self.doclist
if d.doctype == args['source_dt']]):
if name:
args['name'] = name
@@ -241,9 +241,9 @@ class StatusUpdater(DocListController):
where docstatus=1 and net_total = 0""" % ref_dt)
for item in self.get("entries"):
if item.fields.get(ref_fieldname) \
and item.fields.get(ref_fieldname) in all_zero_amount_refdoc \
and item.fields.get(ref_fieldname) not in zero_amount_refdoc:
if item.get(ref_fieldname) \
and item.get(ref_fieldname) in all_zero_amount_refdoc \
and item.get(ref_fieldname) not in zero_amount_refdoc:
zero_amount_refdoc.append(item.fields[ref_fieldname])
if zero_amount_refdoc:
@@ -256,7 +256,7 @@ class StatusUpdater(DocListController):
billed_qty = flt(frappe.db.sql("""select sum(ifnull(qty, 0))
from `tab%s Item` where %s=%s and docstatus=1""" %
(self.doc.doctype, ref_fieldname, '%s'), (ref_dn))[0][0])
(self.doctype, ref_fieldname, '%s'), (ref_dn))[0][0])
per_billed = ((ref_doc_qty if billed_qty > ref_doc_qty else billed_qty)\
/ ref_doc_qty)*100

View File

@@ -12,19 +12,19 @@ from erpnext.accounts.general_ledger import make_gl_entries, delete_gl_entries
class StockController(AccountsController):
def make_gl_entries(self, repost_future_gle=True):
if self.doc.docstatus == 2:
delete_gl_entries(voucher_type=self.doc.doctype, voucher_no=self.doc.name)
if self.docstatus == 2:
delete_gl_entries(voucher_type=self.doctype, voucher_no=self.name)
if cint(frappe.defaults.get_global_default("auto_accounting_for_stock")):
warehouse_account = self.get_warehouse_account()
if self.doc.docstatus==1:
if self.docstatus==1:
gl_entries = self.get_gl_entries(warehouse_account)
make_gl_entries(gl_entries)
if repost_future_gle:
items, warehouse_account = self.get_items_and_warehouse_accounts(warehouse_account)
update_gl_entries_after(self.doc.posting_date, self.doc.posting_time,
update_gl_entries_after(self.posting_date, self.posting_time,
warehouse_account, items)
def get_gl_entries(self, warehouse_account=None, default_expense_account=None,
@@ -49,7 +49,7 @@ class StockController(AccountsController):
"account": warehouse_account[sle.warehouse],
"against": detail.expense_account,
"cost_center": detail.cost_center,
"remarks": self.doc.remarks or "Accounting Entry for Stock",
"remarks": self.remarks or "Accounting Entry for Stock",
"debit": flt(sle.stock_value_difference, 2)
}))
@@ -58,7 +58,7 @@ class StockController(AccountsController):
"account": detail.expense_account,
"against": warehouse_account[sle.warehouse],
"cost_center": detail.cost_center,
"remarks": self.doc.remarks or "Accounting Entry for Stock",
"remarks": self.remarks or "Accounting Entry for Stock",
"credit": flt(sle.stock_value_difference, 2)
}))
elif sle.warehouse not in warehouse_with_no_account:
@@ -91,10 +91,10 @@ class StockController(AccountsController):
if hasattr(self, "fname"):
item_doclist = self.doclist.get({"parentfield": self.fname})
elif self.doc.doctype == "Stock Reconciliation":
elif self.doctype == "Stock Reconciliation":
import json
item_doclist = []
data = json.loads(self.doc.reconciliation_json)
data = json.loads(self.reconciliation_json)
for row in data[data.index(self.head_row)+1:]:
d = frappe._dict(zip(["item_code", "warehouse", "qty", "valuation_rate"], row))
item_doclist.append(d)
@@ -115,7 +115,7 @@ class StockController(AccountsController):
stock_ledger = {}
for sle in frappe.db.sql("""select warehouse, stock_value_difference, voucher_detail_no
from `tabStock Ledger Entry` where voucher_type=%s and voucher_no=%s""",
(self.doc.doctype, self.doc.name), as_dict=True):
(self.doctype, self.name), as_dict=True):
stock_ledger.setdefault(sle.voucher_detail_no, []).append(sle)
return stock_ledger
@@ -167,7 +167,7 @@ class StockController(AccountsController):
from `tabStock Ledger Entry` sle
where timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s) %s
order by timestamp(sle.posting_date, sle.posting_time) asc, name asc""" %
('%s', '%s', condition), (self.doc.posting_date, self.doc.posting_time),
('%s', '%s', condition), (self.posting_date, self.posting_time),
as_dict=True):
future_stock_vouchers.append([d.voucher_type, d.voucher_no])
@@ -179,7 +179,7 @@ class StockController(AccountsController):
for d in frappe.db.sql("""select * from `tabGL Entry`
where posting_date >= %s and voucher_no in (%s)""" %
('%s', ', '.join(['%s']*len(future_stock_vouchers))),
tuple([self.doc.posting_date] + [d[1] for d in future_stock_vouchers]), as_dict=1):
tuple([self.posting_date] + [d[1] for d in future_stock_vouchers]), as_dict=1):
gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d)
return gl_entries
@@ -235,20 +235,20 @@ class StockController(AccountsController):
sl_dict = {
"item_code": d.item_code,
"warehouse": d.warehouse,
"posting_date": self.doc.posting_date,
"posting_time": self.doc.posting_time,
"voucher_type": self.doc.doctype,
"voucher_no": self.doc.name,
"posting_date": self.posting_date,
"posting_time": self.posting_time,
"voucher_type": self.doctype,
"voucher_no": self.name,
"voucher_detail_no": d.name,
"actual_qty": (self.doc.docstatus==1 and 1 or -1)*flt(d.stock_qty),
"actual_qty": (self.docstatus==1 and 1 or -1)*flt(d.stock_qty),
"stock_uom": d.stock_uom,
"incoming_rate": 0,
"company": self.doc.company,
"fiscal_year": self.doc.fiscal_year,
"company": self.company,
"fiscal_year": self.fiscal_year,
"batch_no": cstr(d.batch_no).strip(),
"serial_no": d.serial_no,
"project": d.project_name,
"is_cancelled": self.doc.docstatus==2 and "Yes" or "No"
"is_cancelled": self.docstatus==2 and "Yes" or "No"
}
sl_dict.update(args)
@@ -260,7 +260,7 @@ class StockController(AccountsController):
def make_cancel_gl_entries(self):
if frappe.db.sql("""select name from `tabGL Entry` where voucher_type=%s
and voucher_no=%s""", (self.doc.doctype, self.doc.name)):
and voucher_no=%s""", (self.doctype, self.name)):
self.make_gl_entries()
def update_gl_entries_after(posting_date, posting_time, warehouse_account=None, for_items=None):