removed complications from sales purchase return

This commit is contained in:
Anand Doshi 2013-03-19 13:33:51 +05:30
parent 5afe037a5b
commit f9e51bbab3
2 changed files with 53 additions and 115 deletions

View File

@ -279,7 +279,7 @@ class DocType(AccountsController):
def validate_return_reference_doc(self): def validate_return_reference_doc(self):
"""validate item with reference doc""" """validate item with reference doc"""
ref = get_return_reference_details(self.doc.fields) ref = get_return_doclist_and_details(self.doc.fields)
if ref.doclist: if ref.doclist:
# validate docstatus # validate docstatus
@ -703,7 +703,7 @@ def query_purchase_return_doc(doctype, txt, searchfield, start, page_len, filter
def query_return_item(doctype, txt, searchfield, start, page_len, filters): def query_return_item(doctype, txt, searchfield, start, page_len, filters):
txt = txt.replace("%", "") txt = txt.replace("%", "")
ref = get_return_reference_details(filters) ref = get_return_doclist_and_details(filters)
stock_items = get_stock_items_for_return(ref.doclist, ref.parentfields) stock_items = get_stock_items_for_return(ref.doclist, ref.parentfields)
@ -737,7 +737,7 @@ def get_stock_items_for_return(ref_doclist, parentfields):
return stock_items return stock_items
def get_return_reference_details(args): def get_return_doclist_and_details(args):
ref = webnotes._dict() ref = webnotes._dict()
# get ref_doclist # get ref_doclist
@ -768,7 +768,7 @@ def make_return_jv(stock_entry):
if not se.doc.purpose in ["Sales Return", "Purchase Return"]: if not se.doc.purpose in ["Sales Return", "Purchase Return"]:
return return
ref = get_return_reference_details(se.doc.fields) ref = get_return_doclist_and_details(se.doc.fields)
if ref.doclist[0].doctype == "Delivery Note": if ref.doclist[0].doctype == "Delivery Note":
result = make_return_jv_from_delivery_note(se, ref) result = make_return_jv_from_delivery_note(se, ref)
@ -794,8 +794,6 @@ def make_return_jv(stock_entry):
"doctype": "Journal Voucher Detail", "doctype": "Journal Voucher Detail",
"parentfield": "entries", "parentfield": "entries",
"account": r.get("account"), "account": r.get("account"),
"debit": r.get("debit"),
"credit": r.get("credit"),
"against_invoice": r.get("against_invoice"), "against_invoice": r.get("against_invoice"),
"against_voucher": r.get("against_voucher"), "against_voucher": r.get("against_voucher"),
"balance": get_balance_on(r.get("account"), se.doc.posting_date) "balance": get_balance_on(r.get("account"), se.doc.posting_date)
@ -807,42 +805,32 @@ def make_return_jv_from_sales_invoice(se, ref):
# customer account entry # customer account entry
parent = { parent = {
"account": ref.doclist[0].debit_to, "account": ref.doclist[0].debit_to,
"credit": 0.0,
"against_invoice": ref.doclist[0].name, "against_invoice": ref.doclist[0].name,
} }
# income account entries # income account entries
children = {} children = []
for se_item in se.doclist.get({"parentfield": "mtn_details"}): for se_item in se.doclist.get({"parentfield": "mtn_details"}):
# find item in ref.doclist # find item in ref.doclist
ref_item = ref.doclist.getone({"item_code": se_item.item_code}) ref_item = ref.doclist.getone({"item_code": se_item.item_code})
account, debit = get_sales_account_and_amount_from_item(ref.doclist, ref_item, account = get_sales_account_from_item(ref.doclist, ref_item)
se_item.transfer_qty)
if account not in children: if account not in children:
children[account] = 0 children.append(account)
children[account] += debit
parent["credit"] += debit
# find tax account and value and add corresponding rows return [parent] + [{"account": account} for account in children]
return [parent] + [{"account": account, "debit": debit} for account, debit in children.items()] def get_sales_account_from_item(doclist, ref_item):
account = None
def get_sales_account_and_amount_from_item(doclist, ref_item, transfer_qty):
account = debit = None
if not ref_item.income_account: if not ref_item.income_account:
if ref_item.parent_item: if ref_item.parent_item:
parent_item = doclist.getone({"item_code": ref_item.parent_item}) parent_item = doclist.getone({"item_code": ref_item.parent_item})
packing_ratio = parent_item.qty / ref_item.qty
debit = parent_item.basic_rate * transfer_qty * packing_ratio
account = parent_item.income_account account = parent_item.income_account
else: else:
debit = ref_item.basic_rate * transfer_qty
account = ref_item.income_account account = ref_item.income_account
return account, debit return account
def make_return_jv_from_delivery_note(se, ref): def make_return_jv_from_delivery_note(se, ref):
invoices_against_delivery = get_invoice_list("Sales Invoice Item", "delivery_note", invoices_against_delivery = get_invoice_list("Sales Invoice Item", "delivery_note",
@ -855,10 +843,13 @@ def make_return_jv_from_delivery_note(se, ref):
invoices_against_delivery = get_invoice_list("Sales Invoice Item", "sales_order", invoices_against_delivery = get_invoice_list("Sales Invoice Item", "sales_order",
sales_orders_against_delivery) sales_orders_against_delivery)
against_invoice = {} if not invoices_against_delivery:
return []
parent = {}
children = []
for se_item in se.doclist.get({"parentfield": "mtn_details"}): for se_item in se.doclist.get({"parentfield": "mtn_details"}):
pending = se_item.transfer_qty
for sales_invoice in invoices_against_delivery: for sales_invoice in invoices_against_delivery:
si = webnotes.bean("Sales Invoice", sales_invoice) si = webnotes.bean("Sales Invoice", sales_invoice)
si.run_method("make_packing_list") si.run_method("make_packing_list")
@ -869,41 +860,21 @@ def make_return_jv_from_delivery_note(se, ref):
ref_item = ref_item[0] ref_item = ref_item[0]
if ref_item.qty < pending: account = get_sales_account_from_item(si.doclist, ref_item)
transfer_qty = ref_item.qty
pending -= ref_item.qty
else:
transfer_qty = pending
pending = 0
account, debit = get_sales_account_and_amount_from_item(si.doclist, ref_item, if account not in children:
transfer_qty) children.append(account)
if si.doclist[0].name not in against_invoice: if not parent:
against_invoice[sales_invoice] = { parent = {"account": si.doc.debit_to}
"parent": {"account": si.doclist[0].debit_to, "credit": 0},
"children": {}
}
against_invoice[sales_invoice]["parent"]["credit"] += debit
if account not in against_invoice[sales_invoice]["children"]:
against_invoice[sales_invoice]["children"][account] = 0
against_invoice[sales_invoice]["children"][account] += debit
# find tax account and value and add corresponding rows
if pending <= 0:
break break
result = [] if len(invoices_against_delivery) == 1:
for sales_invoice, opts in against_invoice.items(): parent["against_invoice"] = invoices_against_delivery[0]
parent = opts["parent"]
parent.update({"against_invoice": sales_invoice}) result = [parent] + [{"account": account} for account in children]
children = [{"account": account, "debit": debit}
for account, debit in opts["children"].items()]
result += [parent] + children
return result return result
def get_invoice_list(doctype, link_field, value): def get_invoice_list(doctype, link_field, value):
@ -925,10 +896,13 @@ def make_return_jv_from_purchase_receipt(se, ref):
invoice_against_receipt = get_invoice_list("Purchase Invoice Item", "purchase_order", invoice_against_receipt = get_invoice_list("Purchase Invoice Item", "purchase_order",
purchase_orders_against_receipt) purchase_orders_against_receipt)
against_voucher = {} if not invoice_against_receipt:
return []
parent = {}
children = []
for se_item in se.doclist.get({"parentfield": "mtn_details"}): for se_item in se.doclist.get({"parentfield": "mtn_details"}):
pending = se_item.transfer_qty
for purchase_invoice in invoice_against_receipt: for purchase_invoice in invoice_against_receipt:
pi = webnotes.bean("Purchase Invoice", purchase_invoice) pi = webnotes.bean("Purchase Invoice", purchase_invoice)
ref_item = pi.doclist.get({"item_code": se_item.item_code}) ref_item = pi.doclist.get({"item_code": se_item.item_code})
@ -938,40 +912,20 @@ def make_return_jv_from_purchase_receipt(se, ref):
ref_item = ref_item[0] ref_item = ref_item[0]
if ref_item.qty < pending:
transfer_qty = ref_item.qty
pending -= ref_item.qty
else:
transfer_qty = pending
pending = 0
credit = ref_item.rate * transfer_qty
account = ref_item.expense_head account = ref_item.expense_head
if pi.doclist[0].name not in against_voucher: if account not in children:
against_voucher[purchase_invoice] = { children.append(account)
"parent": {"account": pi.doclist[0].credit_to, "debit": 0},
"children": {}
}
against_voucher[purchase_invoice]["parent"]["debit"] += credit if not parent:
parent = {"account": pi.doc.credit_to}
if account not in against_voucher[purchase_invoice]["children"]:
against_voucher[purchase_invoice]["children"][account] = 0
against_voucher[purchase_invoice]["children"][account] += credit
# find tax account and value and add corresponding rows
if pending <= 0:
break break
result = [] if len(invoice_against_receipt) == 1:
for purchase_invoice, opts in against_voucher.items(): parent["against_voucher"] = invoice_against_receipt[0]
parent = opts["parent"]
parent.update({"against_voucher": purchase_invoice}) result = [parent] + [{"account": account} for account in children]
children = [{"account": account, "credit": credit}
for account, credit in opts["children"].items()]
result += [parent] + children
return result return result

View File

@ -309,7 +309,7 @@ class TestStockEntry(unittest.TestCase):
def test_delivery_note_return_of_packing_item(self): def test_delivery_note_return_of_packing_item(self):
self._test_delivery_note_return("_Test Sales BOM Item", 25, 20) self._test_delivery_note_return("_Test Sales BOM Item", 25, 20)
def _test_sales_return_jv(self, se, returned_value): def _test_sales_return_jv(self, se):
from stock.doctype.stock_entry.stock_entry import make_return_jv from stock.doctype.stock_entry.stock_entry import make_return_jv
jv_list = make_return_jv(se.doc.name) jv_list = make_return_jv(se.doc.name)
@ -320,35 +320,27 @@ class TestStockEntry(unittest.TestCase):
self.assertEqual(jv_list[2].get("account"), "Sales - _TC") self.assertEqual(jv_list[2].get("account"), "Sales - _TC")
self.assertTrue(jv_list[1].get("against_invoice")) self.assertTrue(jv_list[1].get("against_invoice"))
# debit == credit
debit = sum([flt(d.get("debit")) for d in jv_list])
credit = sum([flt(d.get("credit")) for d in jv_list])
self.assertEqual(debit, credit)
# validate value of debit
self.assertEqual(debit, returned_value)
def test_make_return_jv_for_sales_invoice_non_packing_item(self): def test_make_return_jv_for_sales_invoice_non_packing_item(self):
se = self._test_sales_invoice_return("_Test Item", 5, 2) se = self._test_sales_invoice_return("_Test Item", 5, 2)
self._test_sales_return_jv(se, 1000) self._test_sales_return_jv(se)
def test_make_return_jv_for_sales_invoice_packing_item(self): def test_make_return_jv_for_sales_invoice_packing_item(self):
se = self._test_sales_invoice_return("_Test Sales BOM Item", 25, 20) se = self._test_sales_invoice_return("_Test Sales BOM Item", 25, 20)
self._test_sales_return_jv(se, 2000) self._test_sales_return_jv(se)
def test_make_return_jv_for_delivery_note_non_packing_item(self): def test_make_return_jv_for_delivery_note_non_packing_item(self):
se = self._test_delivery_note_return("_Test Item", 5, 2) se = self._test_delivery_note_return("_Test Item", 5, 2)
self._test_sales_return_jv(se, 200) self._test_sales_return_jv(se)
se = self._test_delivery_note_return_against_sales_order("_Test Item", 5, 2) se = self._test_delivery_note_return_against_sales_order("_Test Item", 5, 2)
self._test_sales_return_jv(se, 200) self._test_sales_return_jv(se)
def test_make_return_jv_for_delivery_note_packing_item(self): def test_make_return_jv_for_delivery_note_packing_item(self):
se = self._test_delivery_note_return("_Test Sales BOM Item", 25, 20) se = self._test_delivery_note_return("_Test Sales BOM Item", 25, 20)
self._test_sales_return_jv(se, 400) self._test_sales_return_jv(se)
se = self._test_delivery_note_return_against_sales_order("_Test Sales BOM Item", 25, 20) se = self._test_delivery_note_return_against_sales_order("_Test Sales BOM Item", 25, 20)
self._test_sales_return_jv(se, 400) self._test_sales_return_jv(se)
def _test_delivery_note_return_against_sales_order(self, item_code, delivered_qty, returned_qty): def _test_delivery_note_return_against_sales_order(self, item_code, delivered_qty, returned_qty):
self._insert_material_receipt() self._insert_material_receipt()
@ -477,7 +469,7 @@ class TestStockEntry(unittest.TestCase):
self.assertRaises(StockOverReturnError, se.insert) self.assertRaises(StockOverReturnError, se.insert)
def _test_purchase_return_jv(self, se, returned_value): def _test_purchase_return_jv(self, se):
from stock.doctype.stock_entry.stock_entry import make_return_jv from stock.doctype.stock_entry.stock_entry import make_return_jv
jv_list = make_return_jv(se.doc.name) jv_list = make_return_jv(se.doc.name)
@ -488,20 +480,12 @@ class TestStockEntry(unittest.TestCase):
self.assertEqual(jv_list[2].get("account"), "_Test Account Cost for Goods Sold - _TC") self.assertEqual(jv_list[2].get("account"), "_Test Account Cost for Goods Sold - _TC")
self.assertTrue(jv_list[1].get("against_voucher")) self.assertTrue(jv_list[1].get("against_voucher"))
# debit == credit
debit = sum([flt(d.get("debit")) for d in jv_list])
credit = sum([flt(d.get("credit")) for d in jv_list])
self.assertEqual(debit, credit)
# validate value of credit
self.assertEqual(credit, returned_value)
def test_make_return_jv_for_purchase_receipt(self): def test_make_return_jv_for_purchase_receipt(self):
se, pr_name = self.test_purchase_receipt_return() se, pr_name = self.test_purchase_receipt_return()
self._test_purchase_return_jv(se, 250) self._test_purchase_return_jv(se)
se, pr_name = self._test_purchase_return_return_against_purchase_order() se, pr_name = self._test_purchase_return_return_against_purchase_order()
self._test_purchase_return_jv(se, 250) self._test_purchase_return_jv(se)
def _test_purchase_return_return_against_purchase_order(self): def _test_purchase_return_return_against_purchase_order(self):
self._clear_stock() self._clear_stock()