[fix] warehouse test cases

This commit is contained in:
Rushabh Mehta 2017-01-16 17:23:20 +05:30
parent 6695830f3f
commit ffd80a6061

View File

@ -3,7 +3,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import frappe import frappe
from frappe.utils import cint, flt, cstr, now from frappe.utils import cint, flt, cstr
from frappe import msgprint, _ from frappe import msgprint, _
import frappe.defaults import frappe.defaults
from erpnext.accounts.utils import get_fiscal_year from erpnext.accounts.utils import get_fiscal_year
@ -14,7 +14,7 @@ class StockController(AccountsController):
def validate(self): def validate(self):
super(StockController, self).validate() super(StockController, self).validate()
self.validate_inspection() self.validate_inspection()
def make_gl_entries(self, gl_entries=None, repost_future_gle=True, from_repost=False): def make_gl_entries(self, gl_entries=None, repost_future_gle=True, from_repost=False):
if self.docstatus == 2: if self.docstatus == 2:
delete_gl_entries(voucher_type=self.doctype, voucher_no=self.name) delete_gl_entries(voucher_type=self.doctype, voucher_no=self.name)
@ -43,16 +43,16 @@ class StockController(AccountsController):
gl_list = [] gl_list = []
warehouse_with_no_account = [] warehouse_with_no_account = []
for item_row in voucher_details: for item_row in voucher_details:
sle_list = sle_map.get(item_row.name) sle_list = sle_map.get(item_row.name)
if sle_list: if sle_list:
for sle in sle_list: for sle in sle_list:
if warehouse_account.get(sle.warehouse): if warehouse_account.get(sle.warehouse):
# from warehouse account # from warehouse account
self.check_expense_account(item_row) self.check_expense_account(item_row)
if not sle.stock_value_difference: if not sle.stock_value_difference:
self.validate_negative_stock(sle) self.validate_negative_stock(sle)
@ -80,16 +80,16 @@ class StockController(AccountsController):
for wh in warehouse_with_no_account: for wh in warehouse_with_no_account:
if frappe.db.get_value("Warehouse", wh, "company"): if frappe.db.get_value("Warehouse", wh, "company"):
frappe.throw(_("Warehouse {0} is not linked to any account, please create/link the corresponding (Asset) account for the warehouse.").format(wh)) frappe.throw(_("Warehouse {0} is not linked to any account, please create/link the corresponding (Asset) account for the warehouse.").format(wh))
msgprint(_("No accounting entries for the following warehouses") + ": \n" + msgprint(_("No accounting entries for the following warehouses") + ": \n" +
"\n".join(warehouse_with_no_account)) "\n".join(warehouse_with_no_account))
return process_gl_map(gl_list) return process_gl_map(gl_list)
def validate_negative_stock(self, sle): def validate_negative_stock(self, sle):
if sle.qty_after_transaction < 0 and sle.actual_qty < 0: if sle.qty_after_transaction < 0 and sle.actual_qty < 0:
frappe.throw(_("For the Item {0}, valuation rate not found for warehouse {1}. To be able to do accounting entries (for booking expenses), we need valuation rate for item {2}. Please create an incoming stock transaction, on or before {3} {4}, and then try submiting {5}") frappe.throw(_("For the Item {0}, valuation rate not found for warehouse {1}. To be able to do accounting entries (for booking expenses), we need valuation rate for item {2}. Please create an incoming stock transaction, on or before {3} {4}, and then try submiting {5}")
.format(sle.item_code, sle.warehouse, .format(sle.item_code, sle.warehouse,
sle.item_code, sle.posting_date, sle.posting_time, self.name)) sle.item_code, sle.posting_date, sle.posting_time, self.name))
def get_voucher_details(self, default_expense_account, default_cost_center, sle_map): def get_voucher_details(self, default_expense_account, default_cost_center, sle_map):
@ -248,7 +248,7 @@ class StockController(AccountsController):
incoming_rate = incoming_rate[0][0] if incoming_rate else 0.0 incoming_rate = incoming_rate[0][0] if incoming_rate else 0.0
return incoming_rate return incoming_rate
def validate_warehouse(self): def validate_warehouse(self):
from erpnext.stock.utils import validate_warehouse_company from erpnext.stock.utils import validate_warehouse_company
@ -257,7 +257,7 @@ class StockController(AccountsController):
for w in warehouses: for w in warehouses:
validate_warehouse_company(w, self.company) validate_warehouse_company(w, self.company)
def update_billing_percentage(self, update_modified=True): def update_billing_percentage(self, update_modified=True):
self._update_percent_field({ self._update_percent_field({
"target_dt": self.doctype + " Item", "target_dt": self.doctype + " Item",
@ -271,21 +271,21 @@ class StockController(AccountsController):
def validate_inspection(self): def validate_inspection(self):
'''Checks if quality inspection is set for Items that require inspection. '''Checks if quality inspection is set for Items that require inspection.
On submit, throw an exception''' On submit, throw an exception'''
inspection_required_fieldname = None inspection_required_fieldname = None
if self.doctype in ["Purchase Receipt", "Purchase Invoice"]: if self.doctype in ["Purchase Receipt", "Purchase Invoice"]:
inspection_required_fieldname = "inspection_required_before_purchase" inspection_required_fieldname = "inspection_required_before_purchase"
elif self.doctype in ["Delivery Note", "Sales Invoice"]: elif self.doctype in ["Delivery Note", "Sales Invoice"]:
inspection_required_fieldname = "inspection_required_before_delivery" inspection_required_fieldname = "inspection_required_before_delivery"
if not inspection_required_fieldname or \ if not inspection_required_fieldname or \
(self.doctype in ["Sales Invoice", "Purchase Invoice"] and not self.update_stock): (self.doctype in ["Sales Invoice", "Purchase Invoice"] and not self.update_stock):
return return
for d in self.get('items'): for d in self.get('items'):
if (frappe.db.get_value("Item", d.item_code, inspection_required_fieldname) if (frappe.db.get_value("Item", d.item_code, inspection_required_fieldname)
and not d.quality_inspection): and not d.quality_inspection):
frappe.msgprint(_("Quality Inspection required for Item {0}").format(d.item_code)) frappe.msgprint(_("Quality Inspection required for Item {0}").format(d.item_code))
if self.docstatus==1: if self.docstatus==1:
raise frappe.ValidationError raise frappe.ValidationError
@ -301,7 +301,7 @@ def update_gl_entries_after(posting_date, posting_time, for_warehouses=None, for
future_stock_vouchers = get_future_stock_vouchers(posting_date, posting_time, for_warehouses, for_items) future_stock_vouchers = get_future_stock_vouchers(posting_date, posting_time, for_warehouses, for_items)
gle = get_voucherwise_gl_entries(future_stock_vouchers, posting_date) gle = get_voucherwise_gl_entries(future_stock_vouchers, posting_date)
for voucher_type, voucher_no in future_stock_vouchers: for voucher_type, voucher_no in future_stock_vouchers:
existing_gle = gle.get((voucher_type, voucher_no), []) existing_gle = gle.get((voucher_type, voucher_no), [])
voucher_obj = frappe.get_doc(voucher_type, voucher_no) voucher_obj = frappe.get_doc(voucher_type, voucher_no)
@ -364,14 +364,19 @@ def get_voucherwise_gl_entries(future_stock_vouchers, posting_date):
return gl_entries return gl_entries
def get_warehouse_account(): def get_warehouse_account():
if not frappe.flags.warehouse_account_map: if not frappe.flags.warehouse_account_map or frappe.flags.in_test:
warehouse_account = frappe._dict() warehouse_account = frappe._dict()
for d in frappe.db.sql("""select warehouse, name, account_currency from tabAccount for d in frappe.db.sql("""select
where account_type = 'Stock' and (warehouse is not null and warehouse != '' warehouse, name, account_currency
and is_group != 1) and is_group=0 """, as_dict=1): from
warehouse_account.setdefault(d.warehouse, d) tabAccount
where
account_type = 'Stock'
and (warehouse is not null and warehouse != '')
and is_group=0 """, as_dict=1):
warehouse_account.setdefault(d.warehouse, d)
frappe.flags.warehouse_account_map = warehouse_account frappe.flags.warehouse_account_map = warehouse_account
return frappe.flags.warehouse_account_map return frappe.flags.warehouse_account_map