Skip to content

Commit

Permalink
Merge pull request #27166 from frappe-pr-bot/backport/version-13-hotf…
Browse files Browse the repository at this point in the history
…ix/27086

fix: Don't create inward SLE against SI unless is internal customer enabled
  • Loading branch information
marination authored Aug 26, 2021
2 parents 6e136c4 + 614ee71 commit 6ed5279
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 46 deletions.
82 changes: 42 additions & 40 deletions erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def test_payment_entry_unlink_against_standalone_credit_note(self):
si1 = create_sales_invoice(rate=1000)
si2 = create_sales_invoice(rate=300)
si3 = create_sales_invoice(qty=-1, rate=300, is_return=1)


pe = get_payment_entry("Sales Invoice", si1.name, bank_account="_Test Bank - _TC")
pe.append('references', {
Expand Down Expand Up @@ -1795,23 +1795,13 @@ def test_fixed_deferred_revenue(self):
acc_settings.save()

def test_inter_company_transaction(self):
from erpnext.selling.doctype.customer.test_customer import create_internal_customer

if not frappe.db.exists("Customer", "_Test Internal Customer"):
customer = frappe.get_doc({
"customer_group": "_Test Customer Group",
"customer_name": "_Test Internal Customer",
"customer_type": "Individual",
"doctype": "Customer",
"territory": "_Test Territory",
"is_internal_customer": 1,
"represents_company": "_Test Company 1"
})

customer.append("companies", {
"company": "Wind Power LLC"
})

customer.insert()
create_internal_customer(
customer_name="_Test Internal Customer",
represents_company="_Test Company 1",
allowed_to_interact_with="Wind Power LLC"
)

if not frappe.db.exists("Supplier", "_Test Internal Supplier"):
supplier = frappe.get_doc({
Expand Down Expand Up @@ -1854,8 +1844,43 @@ def test_inter_company_transaction(self):
self.assertEqual(target_doc.company, "_Test Company 1")
self.assertEqual(target_doc.supplier, "_Test Internal Supplier")

def test_sle_if_target_warehouse_exists_accidentally(self):
"""
Check if inward entry exists if Target Warehouse accidentally exists
but Customer is not an internal customer.
"""
se = make_stock_entry(
item_code="138-CMS Shoe",
target="Finished Goods - _TC",
company = "_Test Company",
qty=1,
basic_rate=500
)

si = frappe.copy_doc(test_records[0])
si.update_stock = 1
si.set_warehouse = "Finished Goods - _TC"
si.set_target_warehouse = "Stores - _TC"
si.get("items")[0].warehouse = "Finished Goods - _TC"
si.get("items")[0].target_warehouse = "Stores - _TC"
si.insert()
si.submit()

sles = frappe.get_all("Stock Ledger Entry", filters={"voucher_no": si.name},
fields=["name", "actual_qty"])

# check if only one SLE for outward entry is created
self.assertEqual(len(sles), 1)
self.assertEqual(sles[0].actual_qty, -1)

# tear down
si.cancel()
se.cancel()

def test_internal_transfer_gl_entry(self):
## Create internal transfer account
from erpnext.selling.doctype.customer.test_customer import create_internal_customer

account = create_account(account_name="Unrealized Profit",
parent_account="Current Liabilities - TCP1", company="_Test Company with perpetual inventory")

Expand Down Expand Up @@ -2437,29 +2462,6 @@ def get_taxes_and_charges():
"row_id": 1
}]

def create_internal_customer(customer_name, represents_company, allowed_to_interact_with):
if not frappe.db.exists("Customer", customer_name):
customer = frappe.get_doc({
"customer_group": "_Test Customer Group",
"customer_name": customer_name,
"customer_type": "Individual",
"doctype": "Customer",
"territory": "_Test Territory",
"is_internal_customer": 1,
"represents_company": represents_company
})

customer.append("companies", {
"company": allowed_to_interact_with
})

customer.insert()
customer_name = customer.name
else:
customer_name = frappe.db.get_value("Customer", customer_name)

return customer_name

def create_internal_supplier(supplier_name, represents_company, allowed_to_interact_with):
if not frappe.db.exists("Supplier", supplier_name):
supplier = frappe.get_doc({
Expand Down
2 changes: 1 addition & 1 deletion erpnext/controllers/selling_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def update_stock_ledger(self):
or (cint(self.is_return) and self.docstatus==2)):
sl_entries.append(self.get_sle_for_source_warehouse(d))

if d.target_warehouse:
if d.target_warehouse and self.get("is_internal_customer"):
sl_entries.append(self.get_sle_for_target_warehouse(d))

if d.warehouse and ((not cint(self.is_return) and self.docstatus==2)
Expand Down
23 changes: 23 additions & 0 deletions erpnext/selling/doctype/customer/test_customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,26 @@ def set_credit_limit(customer, company, credit_limit):
'credit_limit': credit_limit
})
customer.credit_limits[-1].db_insert()

def create_internal_customer(customer_name, represents_company, allowed_to_interact_with):
if not frappe.db.exists("Customer", customer_name):
customer = frappe.get_doc({
"doctype": "Customer",
"customer_group": "_Test Customer Group",
"customer_name": customer_name,
"customer_type": "Individual",
"territory": "_Test Territory",
"is_internal_customer": 1,
"represents_company": represents_company
})

customer.append("companies", {
"company": allowed_to_interact_with
})

customer.insert()
customer_name = customer.name
else:
customer_name = frappe.db.get_value("Customer", customer_name)

return customer_name
26 changes: 21 additions & 5 deletions erpnext/stock/doctype/delivery_note/test_delivery_note.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,12 +430,19 @@ def test_return_for_serialized_items(self):
})

def test_delivery_of_bundled_items_to_target_warehouse(self):
from erpnext.selling.doctype.customer.test_customer import create_internal_customer

company = frappe.db.get_value('Warehouse', 'Stores - TCP1', 'company')
customer_name = create_internal_customer(
customer_name="_Test Internal Customer 2",
represents_company="_Test Company with perpetual inventory",
allowed_to_interact_with="_Test Company with perpetual inventory"
)

set_valuation_method("_Test Item", "FIFO")
set_valuation_method("_Test Item Home Desktop 100", "FIFO")

target_warehouse=get_warehouse(company=company, abbr="TCP1",
target_warehouse = get_warehouse(company=company, abbr="TCP1",
warehouse_name="_Test Customer Warehouse").name

for warehouse in ("Stores - TCP1", target_warehouse):
Expand All @@ -444,10 +451,16 @@ def test_delivery_of_bundled_items_to_target_warehouse(self):
create_stock_reconciliation(item_code="_Test Item Home Desktop 100", company = company,
expense_account = "Stock Adjustment - TCP1", warehouse=warehouse, qty=500, rate=100)

dn = create_delivery_note(item_code="_Test Product Bundle Item",
company='_Test Company with perpetual inventory', cost_center = 'Main - TCP1',
expense_account = "Cost of Goods Sold - TCP1", do_not_submit=True, qty=5, rate=500,
warehouse="Stores - TCP1", target_warehouse=target_warehouse)
dn = create_delivery_note(
item_code="_Test Product Bundle Item",
company="_Test Company with perpetual inventory",
customer=customer_name,
cost_center = 'Main - TCP1',
expense_account = "Cost of Goods Sold - TCP1",
do_not_submit=True,
qty=5, rate=500,
warehouse="Stores - TCP1",
target_warehouse=target_warehouse)

dn.submit()

Expand Down Expand Up @@ -487,6 +500,9 @@ def test_delivery_of_bundled_items_to_target_warehouse(self):
for i, gle in enumerate(gl_entries):
self.assertEqual([gle.debit, gle.credit], expected_values.get(gle.account))

# tear down
frappe.db.rollback()

def test_closed_delivery_note(self):
from erpnext.stock.doctype.delivery_note.delivery_note import update_delivery_note_status

Expand Down

0 comments on commit 6ed5279

Please sign in to comment.