Skip to content

Commit

Permalink
test: add test case for internal PR GL Entries
Browse files Browse the repository at this point in the history
(cherry picked from commit c86c543)

# Conflicts:
#	erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py
  • Loading branch information
s-aga-r authored and mergify[bot] committed Apr 20, 2023
1 parent b73422e commit 046bf64
Showing 1 changed file with 195 additions and 0 deletions.
195 changes: 195 additions & 0 deletions erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,201 @@ def test_batch_expiry_for_purchase_receipt(self):

self.assertTrue(return_pi.docstatus == 1)

<<<<<<< HEAD
=======
def test_disable_last_purchase_rate(self):
from erpnext.stock.get_item_details import get_item_details

item = make_item(
"_Test Disable Last Purchase Rate",
{"is_purchase_item": 1, "is_stock_item": 1},
)

frappe.db.set_single_value("Buying Settings", "disable_last_purchase_rate", 1)

pr = make_purchase_receipt(
qty=1,
rate=100,
item_code=item.name,
)

args = pr.items[0].as_dict()
args.update(
{
"supplier": pr.supplier,
"doctype": pr.doctype,
"conversion_rate": pr.conversion_rate,
"currency": pr.currency,
"company": pr.company,
"posting_date": pr.posting_date,
"posting_time": pr.posting_time,
}
)

res = get_item_details(args)
self.assertEqual(res.get("last_purchase_rate"), 0)

frappe.db.set_single_value("Buying Settings", "disable_last_purchase_rate", 0)

pr = make_purchase_receipt(
qty=1,
rate=100,
item_code=item.name,
)

res = get_item_details(args)
self.assertEqual(res.get("last_purchase_rate"), 100)

def test_validate_received_qty_for_internal_pr(self):
prepare_data_for_internal_transfer()
customer = "_Test Internal Customer 2"
company = "_Test Company with perpetual inventory"
from_warehouse = create_warehouse("_Test Internal From Warehouse New", company=company)
target_warehouse = create_warehouse("_Test Internal GIT Warehouse New", company=company)
to_warehouse = create_warehouse("_Test Internal To Warehouse New", company=company)

# Step 1: Create Item
item = make_item(properties={"is_stock_item": 1, "valuation_rate": 100})

# Step 2: Create Stock Entry (Material Receipt)
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry

make_stock_entry(
purpose="Material Receipt",
item_code=item.name,
qty=15,
company=company,
to_warehouse=from_warehouse,
)

# Step 3: Create Delivery Note with Internal Customer
from erpnext.stock.doctype.delivery_note.test_delivery_note import create_delivery_note

dn = create_delivery_note(
item_code=item.name,
company=company,
customer=customer,
cost_center="Main - TCP1",
expense_account="Cost of Goods Sold - TCP1",
qty=10,
rate=100,
warehouse=from_warehouse,
target_warehouse=target_warehouse,
)

# Step 4: Create Internal Purchase Receipt
from erpnext.controllers.status_updater import OverAllowanceError
from erpnext.stock.doctype.delivery_note.delivery_note import make_inter_company_purchase_receipt

pr = make_inter_company_purchase_receipt(dn.name)
pr.items[0].qty = 15
pr.items[0].from_warehouse = target_warehouse
pr.items[0].warehouse = to_warehouse
pr.items[0].rejected_warehouse = from_warehouse
pr.save()

self.assertRaises(OverAllowanceError, pr.submit)

# Step 5: Test Over Receipt Allowance
frappe.db.set_single_value("Stock Settings", "over_delivery_receipt_allowance", 50)

make_stock_entry(
purpose="Material Transfer",
item_code=item.name,
qty=5,
company=company,
from_warehouse=from_warehouse,
to_warehouse=target_warehouse,
)

pr.submit()

frappe.db.set_single_value("Stock Settings", "over_delivery_receipt_allowance", 0)

def test_internal_pr_gl_entries(self):
from erpnext.stock import get_warehouse_account_map
from erpnext.stock.doctype.delivery_note.delivery_note import make_inter_company_purchase_receipt
from erpnext.stock.doctype.delivery_note.test_delivery_note import create_delivery_note
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry
from erpnext.stock.doctype.stock_reconciliation.test_stock_reconciliation import (
create_stock_reconciliation,
)

prepare_data_for_internal_transfer()
customer = "_Test Internal Customer 2"
company = "_Test Company with perpetual inventory"
from_warehouse = create_warehouse("_Test Internal From Warehouse New", company=company)
target_warehouse = create_warehouse("_Test Internal GIT Warehouse New", company=company)
to_warehouse = create_warehouse("_Test Internal To Warehouse New", company=company)

item = make_item(properties={"is_stock_item": 1, "valuation_rate": 100})
make_stock_entry(
purpose="Material Receipt",
item_code=item.name,
qty=10,
company=company,
to_warehouse=from_warehouse,
posting_date=add_days(today(), -3),
)

# Step - 1: Create Delivery Note with Internal Customer
dn = create_delivery_note(
item_code=item.name,
company=company,
customer=customer,
cost_center="Main - TCP1",
expense_account="Cost of Goods Sold - TCP1",
qty=10,
rate=100,
warehouse=from_warehouse,
target_warehouse=target_warehouse,
posting_date=add_days(today(), -2),
)

# Step - 2: Create Internal Purchase Receipt
pr = make_inter_company_purchase_receipt(dn.name)
pr.items[0].qty = 10
pr.items[0].from_warehouse = target_warehouse
pr.items[0].warehouse = to_warehouse
pr.items[0].rejected_warehouse = from_warehouse
pr.save()
pr.submit()

# Step - 3: Create back-date Stock Reconciliation [After DN and Before PR]
create_stock_reconciliation(
item_code=item,
warehouse=target_warehouse,
qty=10,
rate=50,
company=company,
posting_date=add_days(today(), -1),
)

warehouse_account = get_warehouse_account_map(company)
stock_account_value = frappe.db.get_value(
"GL Entry",
{
"account": warehouse_account[target_warehouse]["account"],
"voucher_type": "Purchase Receipt",
"voucher_no": pr.name,
"is_cancelled": 0,
},
fieldname=["credit"],
)
stock_diff = frappe.db.get_value(
"Stock Ledger Entry",
{
"voucher_type": "Purchase Receipt",
"voucher_no": pr.name,
"is_cancelled": 0,
},
fieldname=["sum(stock_value_difference)"],
)

# Value of Stock Account should be equal to the sum of Stock Value Difference
self.assertEqual(stock_account_value, stock_diff)

>>>>>>> c86c543fbf (test: add test case for internal PR GL Entries)

def prepare_data_for_internal_transfer():
from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_internal_supplier
Expand Down

0 comments on commit 046bf64

Please sign in to comment.