blob: 3cb5e42e7ae85e6d194bd5a4d3bfaa416096677c [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,
Devin Slauenwhite98c39c42023-01-01 22:25:12 -050013 update_reference_in_payment_entry,
Ankush Menat700e8642022-04-19 13:24:29 +053014)
15from erpnext.stock.doctype.item.test_item import make_item
rohitwaghchaure058d9832021-09-07 12:14:40 +053016from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import make_purchase_receipt
Ankush Menat700e8642022-04-19 13:24:29 +053017from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry
Chillar Anand915b3432021-09-02 16:44:59 +053018
tundebabzy40a02762017-10-25 07:54:34 +010019
20class TestUtils(unittest.TestCase):
21 @classmethod
22 def setUpClass(cls):
23 super(TestUtils, cls).setUpClass()
rohitwaghchaure058d9832021-09-07 12:14:40 +053024 make_test_objects("Address", ADDRESS_RECORDS)
tundebabzy40a02762017-10-25 07:54:34 +010025
26 def test_get_party_shipping_address(self):
rohitwaghchaure058d9832021-09-07 12:14:40 +053027 address = get_party_shipping_address("Customer", "_Test Customer 1")
28 self.assertEqual(address, "_Test Billing Address 2 Title-Billing")
tundebabzy40a02762017-10-25 07:54:34 +010029
30 def test_get_party_shipping_address2(self):
rohitwaghchaure058d9832021-09-07 12:14:40 +053031 address = get_party_shipping_address("Customer", "_Test Customer 2")
32 self.assertEqual(address, "_Test Shipping Address 2 Title-Shipping")
33
34 def test_get_voucher_wise_gl_entry(self):
35
36 pr = make_purchase_receipt(
37 item_code="_Test Item",
38 posting_date="2021-02-01",
39 rate=100,
40 qty=1,
41 warehouse="Stores - TCP1",
42 company="_Test Company with perpetual inventory",
43 )
44
45 future_vouchers = get_future_stock_vouchers("2021-01-01", "00:00:00", for_items=["_Test Item"])
46
47 voucher_type_and_no = ("Purchase Receipt", pr.name)
48 self.assertTrue(
49 voucher_type_and_no in future_vouchers,
50 msg="get_future_stock_vouchers not returning correct value",
51 )
52
53 posting_date = "2021-01-01"
54 gl_entries = get_voucherwise_gl_entries(future_vouchers, posting_date)
55 self.assertTrue(
Ankush Menat494bd9e2022-03-28 18:52:46 +053056 voucher_type_and_no in gl_entries,
57 msg="get_voucherwise_gl_entries not returning expected GLes",
rohitwaghchaure058d9832021-09-07 12:14:40 +053058 )
tundebabzy40a02762017-10-25 07:54:34 +010059
Ankush Menat700e8642022-04-19 13:24:29 +053060 def test_stock_voucher_sorting(self):
61 vouchers = []
62
63 item = make_item().name
64
65 stock_entry = {"item": item, "to_warehouse": "_Test Warehouse - _TC", "qty": 1, "rate": 10}
66
67 se1 = make_stock_entry(posting_date="2022-01-01", **stock_entry)
Ankush Menat700e8642022-04-19 13:24:29 +053068 se3 = make_stock_entry(posting_date="2022-03-01", **stock_entry)
Ankush Menat2535d5e2022-06-14 18:20:33 +053069 se2 = make_stock_entry(posting_date="2022-02-01", **stock_entry)
Ankush Menat700e8642022-04-19 13:24:29 +053070
71 for doc in (se1, se2, se3):
72 vouchers.append((doc.doctype, doc.name))
73
74 vouchers.append(("Stock Entry", "Wat"))
75
76 sorted_vouchers = sort_stock_vouchers_by_posting_date(list(reversed(vouchers)))
77 self.assertEqual(sorted_vouchers, vouchers)
78
Devin Slauenwhite98c39c42023-01-01 22:25:12 -050079 def test_update_reference_in_payment_entry(self):
80 item = make_item().name
81
82 purchase_invoice = make_purchase_invoice(
ruthra kumar72bc5b32023-06-27 16:11:03 +053083 item=item, supplier="_Test Supplier USD", currency="USD", conversion_rate=82.32, do_not_submit=1
Devin Slauenwhite98c39c42023-01-01 22:25:12 -050084 )
ruthra kumar72bc5b32023-06-27 16:11:03 +053085 purchase_invoice.credit_to = "_Test Payable USD - _TC"
Devin Slauenwhite98c39c42023-01-01 22:25:12 -050086 purchase_invoice.submit()
87
88 payment_entry = get_payment_entry(purchase_invoice.doctype, purchase_invoice.name)
Devin Slauenwhite98c39c42023-01-01 22:25:12 -050089 payment_entry.paid_amount = 15725
90 payment_entry.deductions = []
ruthra kumar72bc5b32023-06-27 16:11:03 +053091 payment_entry.save()
Devin Slauenwhite98c39c42023-01-01 22:25:12 -050092
ruthra kumar72bc5b32023-06-27 16:11:03 +053093 # below is the difference between base_received_amount and base_paid_amount
94 self.assertEqual(payment_entry.difference_amount, -4855.0)
95
96 payment_entry.target_exchange_rate = 62.9
97 payment_entry.save()
98
99 # below is due to change in exchange rate
100 self.assertEqual(payment_entry.references[0].exchange_gain_loss, -4855.0)
101
Devin Slauenwhite98c39c42023-01-01 22:25:12 -0500102 payment_entry.references = []
ruthra kumar72bc5b32023-06-27 16:11:03 +0530103 self.assertEqual(payment_entry.difference_amount, 0.0)
Devin Slauenwhite98c39c42023-01-01 22:25:12 -0500104 payment_entry.submit()
105
106 payment_reconciliation = frappe.new_doc("Payment Reconciliation")
107 payment_reconciliation.company = payment_entry.company
108 payment_reconciliation.party_type = "Supplier"
109 payment_reconciliation.party = purchase_invoice.supplier
110 payment_reconciliation.receivable_payable_account = payment_entry.paid_to
111 payment_reconciliation.get_unreconciled_entries()
112 payment_reconciliation.allocate_entries(
113 {
114 "payments": [d.__dict__ for d in payment_reconciliation.payments],
115 "invoices": [d.__dict__ for d in payment_reconciliation.invoices],
116 }
117 )
118 for d in payment_reconciliation.invoices:
119 # Reset invoice outstanding_amount because allocate_entries will zero this value out.
120 d.outstanding_amount = d.amount
121 for d in payment_reconciliation.allocation:
122 d.difference_account = "Exchange Gain/Loss - _TC"
123 payment_reconciliation.reconcile()
124
125 payment_entry.load_from_db()
126 self.assertEqual(len(payment_entry.references), 1)
127 self.assertEqual(payment_entry.difference_amount, 0)
128
tundebabzy40a02762017-10-25 07:54:34 +0100129
130ADDRESS_RECORDS = [
131 {
132 "doctype": "Address",
133 "address_type": "Billing",
134 "address_line1": "Address line 1",
135 "address_title": "_Test Billing Address Title",
136 "city": "Lagos",
137 "country": "Nigeria",
138 "links": [
rohitwaghchaure058d9832021-09-07 12:14:40 +0530139 {"link_doctype": "Customer", "link_name": "_Test Customer 2", "doctype": "Dynamic Link"}
140 ],
tundebabzy40a02762017-10-25 07:54:34 +0100141 },
142 {
143 "doctype": "Address",
144 "address_type": "Shipping",
145 "address_line1": "Address line 2",
146 "address_title": "_Test Shipping Address 1 Title",
147 "city": "Lagos",
148 "country": "Nigeria",
149 "links": [
rohitwaghchaure058d9832021-09-07 12:14:40 +0530150 {"link_doctype": "Customer", "link_name": "_Test Customer 2", "doctype": "Dynamic Link"}
151 ],
tundebabzy40a02762017-10-25 07:54:34 +0100152 },
153 {
154 "doctype": "Address",
155 "address_type": "Shipping",
156 "address_line1": "Address line 3",
157 "address_title": "_Test Shipping Address 2 Title",
158 "city": "Lagos",
159 "country": "Nigeria",
160 "is_shipping_address": "1",
161 "links": [
rohitwaghchaure058d9832021-09-07 12:14:40 +0530162 {"link_doctype": "Customer", "link_name": "_Test Customer 2", "doctype": "Dynamic Link"}
163 ],
tundebabzy40a02762017-10-25 07:54:34 +0100164 },
165 {
166 "doctype": "Address",
167 "address_type": "Billing",
168 "address_line1": "Address line 4",
169 "address_title": "_Test Billing Address 2 Title",
170 "city": "Lagos",
171 "country": "Nigeria",
172 "is_shipping_address": "1",
173 "links": [
rohitwaghchaure058d9832021-09-07 12:14:40 +0530174 {"link_doctype": "Customer", "link_name": "_Test Customer 1", "doctype": "Dynamic Link"}
175 ],
176 },
tundebabzy40a02762017-10-25 07:54:34 +0100177]