blob: 3aca60eae5bdde65dc6dfd0644564f15622de5b5 [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(
83 item=item, supplier="_Test Supplier USD", currency="USD", conversion_rate=82.32
84 )
85 purchase_invoice.submit()
86
87 payment_entry = get_payment_entry(purchase_invoice.doctype, purchase_invoice.name)
88 payment_entry.target_exchange_rate = 62.9
89 payment_entry.paid_amount = 15725
90 payment_entry.deductions = []
91 payment_entry.insert()
92
93 self.assertEqual(payment_entry.difference_amount, -4855.00)
94 payment_entry.references = []
95 payment_entry.submit()
96
97 payment_reconciliation = frappe.new_doc("Payment Reconciliation")
98 payment_reconciliation.company = payment_entry.company
99 payment_reconciliation.party_type = "Supplier"
100 payment_reconciliation.party = purchase_invoice.supplier
101 payment_reconciliation.receivable_payable_account = payment_entry.paid_to
102 payment_reconciliation.get_unreconciled_entries()
103 payment_reconciliation.allocate_entries(
104 {
105 "payments": [d.__dict__ for d in payment_reconciliation.payments],
106 "invoices": [d.__dict__ for d in payment_reconciliation.invoices],
107 }
108 )
109 for d in payment_reconciliation.invoices:
110 # Reset invoice outstanding_amount because allocate_entries will zero this value out.
111 d.outstanding_amount = d.amount
112 for d in payment_reconciliation.allocation:
113 d.difference_account = "Exchange Gain/Loss - _TC"
114 payment_reconciliation.reconcile()
115
116 payment_entry.load_from_db()
117 self.assertEqual(len(payment_entry.references), 1)
118 self.assertEqual(payment_entry.difference_amount, 0)
119
tundebabzy40a02762017-10-25 07:54:34 +0100120
121ADDRESS_RECORDS = [
122 {
123 "doctype": "Address",
124 "address_type": "Billing",
125 "address_line1": "Address line 1",
126 "address_title": "_Test Billing Address Title",
127 "city": "Lagos",
128 "country": "Nigeria",
129 "links": [
rohitwaghchaure058d9832021-09-07 12:14:40 +0530130 {"link_doctype": "Customer", "link_name": "_Test Customer 2", "doctype": "Dynamic Link"}
131 ],
tundebabzy40a02762017-10-25 07:54:34 +0100132 },
133 {
134 "doctype": "Address",
135 "address_type": "Shipping",
136 "address_line1": "Address line 2",
137 "address_title": "_Test Shipping Address 1 Title",
138 "city": "Lagos",
139 "country": "Nigeria",
140 "links": [
rohitwaghchaure058d9832021-09-07 12:14:40 +0530141 {"link_doctype": "Customer", "link_name": "_Test Customer 2", "doctype": "Dynamic Link"}
142 ],
tundebabzy40a02762017-10-25 07:54:34 +0100143 },
144 {
145 "doctype": "Address",
146 "address_type": "Shipping",
147 "address_line1": "Address line 3",
148 "address_title": "_Test Shipping Address 2 Title",
149 "city": "Lagos",
150 "country": "Nigeria",
151 "is_shipping_address": "1",
152 "links": [
rohitwaghchaure058d9832021-09-07 12:14:40 +0530153 {"link_doctype": "Customer", "link_name": "_Test Customer 2", "doctype": "Dynamic Link"}
154 ],
tundebabzy40a02762017-10-25 07:54:34 +0100155 },
156 {
157 "doctype": "Address",
158 "address_type": "Billing",
159 "address_line1": "Address line 4",
160 "address_title": "_Test Billing Address 2 Title",
161 "city": "Lagos",
162 "country": "Nigeria",
163 "is_shipping_address": "1",
164 "links": [
rohitwaghchaure058d9832021-09-07 12:14:40 +0530165 {"link_doctype": "Customer", "link_name": "_Test Customer 1", "doctype": "Dynamic Link"}
166 ],
167 },
tundebabzy40a02762017-10-25 07:54:34 +0100168]