blob: 59cbc11794f419fd91578c3f030c920152507da7 [file] [log] [blame]
tundebabzy40a02762017-10-25 07:54:34 +01001import unittest
Chillar Anand915b3432021-09-02 16:44:59 +05302
Ankush Menat700e8642022-04-19 13:24:29 +05303import frappe
tundebabzy40a02762017-10-25 07:54:34 +01004from frappe.test_runner import make_test_objects
5
Devin Slauenwhite98c39c42023-01-01 22:25:12 -05006from erpnext.accounts.doctype.payment_entry.payment_entry import get_payment_entry
7from erpnext.accounts.doctype.purchase_invoice.test_purchase_invoice import make_purchase_invoice
Chillar Anand915b3432021-09-02 16:44:59 +05308from erpnext.accounts.party import get_party_shipping_address
Ankush Menat700e8642022-04-19 13:24:29 +05309from erpnext.accounts.utils import (
10 get_future_stock_vouchers,
11 get_voucherwise_gl_entries,
12 sort_stock_vouchers_by_posting_date,
13)
14from erpnext.stock.doctype.item.test_item import make_item
rohitwaghchaure058d9832021-09-07 12:14:40 +053015from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import make_purchase_receipt
Ankush Menat700e8642022-04-19 13:24:29 +053016from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry
Chillar Anand915b3432021-09-02 16:44:59 +053017
tundebabzy40a02762017-10-25 07:54:34 +010018
19class TestUtils(unittest.TestCase):
20 @classmethod
21 def setUpClass(cls):
Akhil Narang3effaf22024-03-27 11:37:26 +053022 super().setUpClass()
rohitwaghchaure058d9832021-09-07 12:14:40 +053023 make_test_objects("Address", ADDRESS_RECORDS)
tundebabzy40a02762017-10-25 07:54:34 +010024
Gursheen Anand813b7a92024-01-11 12:22:08 +053025 @classmethod
26 def tearDownClass(cls):
Gursheen Anandbbdf98a2024-01-10 22:06:06 +053027 frappe.db.rollback()
28
tundebabzy40a02762017-10-25 07:54:34 +010029 def test_get_party_shipping_address(self):
rohitwaghchaure058d9832021-09-07 12:14:40 +053030 address = get_party_shipping_address("Customer", "_Test Customer 1")
31 self.assertEqual(address, "_Test Billing Address 2 Title-Billing")
tundebabzy40a02762017-10-25 07:54:34 +010032
33 def test_get_party_shipping_address2(self):
rohitwaghchaure058d9832021-09-07 12:14:40 +053034 address = get_party_shipping_address("Customer", "_Test Customer 2")
35 self.assertEqual(address, "_Test Shipping Address 2 Title-Shipping")
36
37 def test_get_voucher_wise_gl_entry(self):
rohitwaghchaure058d9832021-09-07 12:14:40 +053038 pr = make_purchase_receipt(
39 item_code="_Test Item",
40 posting_date="2021-02-01",
41 rate=100,
42 qty=1,
43 warehouse="Stores - TCP1",
44 company="_Test Company with perpetual inventory",
45 )
46
47 future_vouchers = get_future_stock_vouchers("2021-01-01", "00:00:00", for_items=["_Test Item"])
48
49 voucher_type_and_no = ("Purchase Receipt", pr.name)
50 self.assertTrue(
51 voucher_type_and_no in future_vouchers,
52 msg="get_future_stock_vouchers not returning correct value",
53 )
54
55 posting_date = "2021-01-01"
56 gl_entries = get_voucherwise_gl_entries(future_vouchers, posting_date)
57 self.assertTrue(
Ankush Menat494bd9e2022-03-28 18:52:46 +053058 voucher_type_and_no in gl_entries,
59 msg="get_voucherwise_gl_entries not returning expected GLes",
rohitwaghchaure058d9832021-09-07 12:14:40 +053060 )
tundebabzy40a02762017-10-25 07:54:34 +010061
Ankush Menat700e8642022-04-19 13:24:29 +053062 def test_stock_voucher_sorting(self):
63 vouchers = []
64
65 item = make_item().name
66
67 stock_entry = {"item": item, "to_warehouse": "_Test Warehouse - _TC", "qty": 1, "rate": 10}
68
69 se1 = make_stock_entry(posting_date="2022-01-01", **stock_entry)
Ankush Menat700e8642022-04-19 13:24:29 +053070 se3 = make_stock_entry(posting_date="2022-03-01", **stock_entry)
Ankush Menat2535d5e2022-06-14 18:20:33 +053071 se2 = make_stock_entry(posting_date="2022-02-01", **stock_entry)
Ankush Menat700e8642022-04-19 13:24:29 +053072
73 for doc in (se1, se2, se3):
74 vouchers.append((doc.doctype, doc.name))
75
76 vouchers.append(("Stock Entry", "Wat"))
77
78 sorted_vouchers = sort_stock_vouchers_by_posting_date(list(reversed(vouchers)))
79 self.assertEqual(sorted_vouchers, vouchers)
80
Devin Slauenwhite98c39c42023-01-01 22:25:12 -050081 def test_update_reference_in_payment_entry(self):
82 item = make_item().name
83
84 purchase_invoice = make_purchase_invoice(
ruthra kumar72bc5b32023-06-27 16:11:03 +053085 item=item, supplier="_Test Supplier USD", currency="USD", conversion_rate=82.32, do_not_submit=1
Devin Slauenwhite98c39c42023-01-01 22:25:12 -050086 )
ruthra kumar72bc5b32023-06-27 16:11:03 +053087 purchase_invoice.credit_to = "_Test Payable USD - _TC"
Devin Slauenwhite98c39c42023-01-01 22:25:12 -050088 purchase_invoice.submit()
89
90 payment_entry = get_payment_entry(purchase_invoice.doctype, purchase_invoice.name)
Devin Slauenwhite98c39c42023-01-01 22:25:12 -050091 payment_entry.paid_amount = 15725
92 payment_entry.deductions = []
ruthra kumar72bc5b32023-06-27 16:11:03 +053093 payment_entry.save()
Devin Slauenwhite98c39c42023-01-01 22:25:12 -050094
ruthra kumar72bc5b32023-06-27 16:11:03 +053095 # below is the difference between base_received_amount and base_paid_amount
96 self.assertEqual(payment_entry.difference_amount, -4855.0)
97
98 payment_entry.target_exchange_rate = 62.9
99 payment_entry.save()
100
101 # below is due to change in exchange rate
102 self.assertEqual(payment_entry.references[0].exchange_gain_loss, -4855.0)
103
Devin Slauenwhite98c39c42023-01-01 22:25:12 -0500104 payment_entry.references = []
ruthra kumar72bc5b32023-06-27 16:11:03 +0530105 self.assertEqual(payment_entry.difference_amount, 0.0)
Devin Slauenwhite98c39c42023-01-01 22:25:12 -0500106 payment_entry.submit()
107
108 payment_reconciliation = frappe.new_doc("Payment Reconciliation")
109 payment_reconciliation.company = payment_entry.company
110 payment_reconciliation.party_type = "Supplier"
111 payment_reconciliation.party = purchase_invoice.supplier
112 payment_reconciliation.receivable_payable_account = payment_entry.paid_to
113 payment_reconciliation.get_unreconciled_entries()
114 payment_reconciliation.allocate_entries(
115 {
116 "payments": [d.__dict__ for d in payment_reconciliation.payments],
117 "invoices": [d.__dict__ for d in payment_reconciliation.invoices],
118 }
119 )
120 for d in payment_reconciliation.invoices:
121 # Reset invoice outstanding_amount because allocate_entries will zero this value out.
122 d.outstanding_amount = d.amount
123 for d in payment_reconciliation.allocation:
124 d.difference_account = "Exchange Gain/Loss - _TC"
125 payment_reconciliation.reconcile()
126
127 payment_entry.load_from_db()
128 self.assertEqual(len(payment_entry.references), 1)
129 self.assertEqual(payment_entry.difference_amount, 0)
130
Gursheen Anandbbdf98a2024-01-10 22:06:06 +0530131 def test_naming_series_variable_parsing(self):
132 """
133 Tests parsing utility used by Naming Series Variable hook for FY
134 """
135 from frappe.custom.doctype.property_setter.property_setter import make_property_setter
136 from frappe.utils import nowdate
137
138 from erpnext.accounts.utils import get_fiscal_year
139 from erpnext.buying.doctype.supplier.test_supplier import create_supplier
140
141 # Configure Supplier Naming in Buying Settings
142 frappe.db.set_default("supp_master_name", "Auto Name")
143
144 # Configure Autoname in Supplier DocType
Akhil Narang3effaf22024-03-27 11:37:26 +0530145 make_property_setter("Supplier", None, "naming_rule", "Expression", "Data", for_doctype="Doctype")
146 make_property_setter("Supplier", None, "autoname", "SUP-.FY.-.#####", "Data", for_doctype="Doctype")
Gursheen Anandbbdf98a2024-01-10 22:06:06 +0530147
Gursheen Anandbbdf98a2024-01-10 22:06:06 +0530148 fiscal_year = get_fiscal_year(nowdate())[0]
149
150 # Create Supplier
151 supplier = create_supplier()
152
153 # Check Naming Series in generated Supplier ID
154 doc_name = supplier.name.split("-")
155 self.assertEqual(len(doc_name), 3)
156 self.assertSequenceEqual(doc_name[0:2], ("SUP", fiscal_year))
Gursheen Anand813b7a92024-01-11 12:22:08 +0530157 frappe.db.set_default("supp_master_name", "Supplier Name")
Gursheen Anandbbdf98a2024-01-10 22:06:06 +0530158
tundebabzy40a02762017-10-25 07:54:34 +0100159
160ADDRESS_RECORDS = [
161 {
162 "doctype": "Address",
163 "address_type": "Billing",
164 "address_line1": "Address line 1",
165 "address_title": "_Test Billing Address Title",
166 "city": "Lagos",
167 "country": "Nigeria",
Akhil Narang3effaf22024-03-27 11:37:26 +0530168 "links": [{"link_doctype": "Customer", "link_name": "_Test Customer 2", "doctype": "Dynamic Link"}],
tundebabzy40a02762017-10-25 07:54:34 +0100169 },
170 {
171 "doctype": "Address",
172 "address_type": "Shipping",
173 "address_line1": "Address line 2",
174 "address_title": "_Test Shipping Address 1 Title",
175 "city": "Lagos",
176 "country": "Nigeria",
Akhil Narang3effaf22024-03-27 11:37:26 +0530177 "links": [{"link_doctype": "Customer", "link_name": "_Test Customer 2", "doctype": "Dynamic Link"}],
tundebabzy40a02762017-10-25 07:54:34 +0100178 },
179 {
180 "doctype": "Address",
181 "address_type": "Shipping",
182 "address_line1": "Address line 3",
183 "address_title": "_Test Shipping Address 2 Title",
184 "city": "Lagos",
185 "country": "Nigeria",
186 "is_shipping_address": "1",
Akhil Narang3effaf22024-03-27 11:37:26 +0530187 "links": [{"link_doctype": "Customer", "link_name": "_Test Customer 2", "doctype": "Dynamic Link"}],
tundebabzy40a02762017-10-25 07:54:34 +0100188 },
189 {
190 "doctype": "Address",
191 "address_type": "Billing",
192 "address_line1": "Address line 4",
193 "address_title": "_Test Billing Address 2 Title",
194 "city": "Lagos",
195 "country": "Nigeria",
196 "is_shipping_address": "1",
Akhil Narang3effaf22024-03-27 11:37:26 +0530197 "links": [{"link_doctype": "Customer", "link_name": "_Test Customer 1", "doctype": "Dynamic Link"}],
rohitwaghchaure058d9832021-09-07 12:14:40 +0530198 },
tundebabzy40a02762017-10-25 07:54:34 +0100199]