Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't create inward SLE against SI unless is internal customer enabled #27086

Merged
merged 3 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 39 additions & 17 deletions erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,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 @@ -1816,23 +1816,12 @@ 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"
)

if not frappe.db.exists("Supplier", "_Test Internal Supplier"):
supplier = frappe.get_doc({
Expand Down Expand Up @@ -1958,6 +1947,39 @@ def test_inter_company_transaction_without_default_warehouse(self):
frappe.local.enable_perpetual_inventory['_Test Company 1'] = old_perpetual_inventory
frappe.db.set_value("Stock Settings", None, "allow_negative_stock", old_negative_stock)

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
account = create_account(account_name="Unrealized Profit",
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 @@ -422,7 +422,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
25 changes: 25 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,28 @@ def set_credit_limit(customer, company, credit_limit):
'credit_limit': credit_limit
})
customer.credit_limits[-1].db_insert()

def create_internal_customer(**args):
args = frappe._dict(args)

customer_name = args.get("customer_name") or "_Test Internal Customer"

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

customer.append("companies", {
"company": args.allowed_company or "Wind Power LLC"
})
customer.insert()

return customer
else:
return frappe.get_cached_doc("Customer", customer_name)
25 changes: 20 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,18 @@ 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 = create_internal_customer(
customer_name="_Test Internal Customer 2",
allowed_company="_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 +450,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 +499,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