tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 1 | import unittest |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 2 | |
Ankush Menat | 700e864 | 2022-04-19 13:24:29 +0530 | [diff] [blame] | 3 | import frappe |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 4 | from frappe.test_runner import make_test_objects |
| 5 | |
Devin Slauenwhite | 98c39c4 | 2023-01-01 22:25:12 -0500 | [diff] [blame] | 6 | from erpnext.accounts.doctype.payment_entry.payment_entry import get_payment_entry |
| 7 | from erpnext.accounts.doctype.purchase_invoice.test_purchase_invoice import make_purchase_invoice |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 8 | from erpnext.accounts.party import get_party_shipping_address |
Ankush Menat | 700e864 | 2022-04-19 13:24:29 +0530 | [diff] [blame] | 9 | from erpnext.accounts.utils import ( |
| 10 | get_future_stock_vouchers, |
| 11 | get_voucherwise_gl_entries, |
| 12 | sort_stock_vouchers_by_posting_date, |
Devin Slauenwhite | 98c39c4 | 2023-01-01 22:25:12 -0500 | [diff] [blame] | 13 | update_reference_in_payment_entry, |
Ankush Menat | 700e864 | 2022-04-19 13:24:29 +0530 | [diff] [blame] | 14 | ) |
| 15 | from erpnext.stock.doctype.item.test_item import make_item |
rohitwaghchaure | 058d983 | 2021-09-07 12:14:40 +0530 | [diff] [blame] | 16 | from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import make_purchase_receipt |
Ankush Menat | 700e864 | 2022-04-19 13:24:29 +0530 | [diff] [blame] | 17 | from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 18 | |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 19 | |
| 20 | class TestUtils(unittest.TestCase): |
| 21 | @classmethod |
| 22 | def setUpClass(cls): |
| 23 | super(TestUtils, cls).setUpClass() |
rohitwaghchaure | 058d983 | 2021-09-07 12:14:40 +0530 | [diff] [blame] | 24 | make_test_objects("Address", ADDRESS_RECORDS) |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 25 | |
| 26 | def test_get_party_shipping_address(self): |
rohitwaghchaure | 058d983 | 2021-09-07 12:14:40 +0530 | [diff] [blame] | 27 | address = get_party_shipping_address("Customer", "_Test Customer 1") |
| 28 | self.assertEqual(address, "_Test Billing Address 2 Title-Billing") |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 29 | |
| 30 | def test_get_party_shipping_address2(self): |
rohitwaghchaure | 058d983 | 2021-09-07 12:14:40 +0530 | [diff] [blame] | 31 | 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 Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 56 | voucher_type_and_no in gl_entries, |
| 57 | msg="get_voucherwise_gl_entries not returning expected GLes", |
rohitwaghchaure | 058d983 | 2021-09-07 12:14:40 +0530 | [diff] [blame] | 58 | ) |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 59 | |
Ankush Menat | 700e864 | 2022-04-19 13:24:29 +0530 | [diff] [blame] | 60 | 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 Menat | 700e864 | 2022-04-19 13:24:29 +0530 | [diff] [blame] | 68 | se3 = make_stock_entry(posting_date="2022-03-01", **stock_entry) |
Ankush Menat | 2535d5e | 2022-06-14 18:20:33 +0530 | [diff] [blame] | 69 | se2 = make_stock_entry(posting_date="2022-02-01", **stock_entry) |
Ankush Menat | 700e864 | 2022-04-19 13:24:29 +0530 | [diff] [blame] | 70 | |
| 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 Slauenwhite | 98c39c4 | 2023-01-01 22:25:12 -0500 | [diff] [blame] | 79 | def test_update_reference_in_payment_entry(self): |
| 80 | item = make_item().name |
| 81 | |
| 82 | purchase_invoice = make_purchase_invoice( |
ruthra kumar | 72bc5b3 | 2023-06-27 16:11:03 +0530 | [diff] [blame] | 83 | item=item, supplier="_Test Supplier USD", currency="USD", conversion_rate=82.32, do_not_submit=1 |
Devin Slauenwhite | 98c39c4 | 2023-01-01 22:25:12 -0500 | [diff] [blame] | 84 | ) |
ruthra kumar | 72bc5b3 | 2023-06-27 16:11:03 +0530 | [diff] [blame] | 85 | purchase_invoice.credit_to = "_Test Payable USD - _TC" |
Devin Slauenwhite | 98c39c4 | 2023-01-01 22:25:12 -0500 | [diff] [blame] | 86 | purchase_invoice.submit() |
| 87 | |
| 88 | payment_entry = get_payment_entry(purchase_invoice.doctype, purchase_invoice.name) |
Devin Slauenwhite | 98c39c4 | 2023-01-01 22:25:12 -0500 | [diff] [blame] | 89 | payment_entry.paid_amount = 15725 |
| 90 | payment_entry.deductions = [] |
ruthra kumar | 72bc5b3 | 2023-06-27 16:11:03 +0530 | [diff] [blame] | 91 | payment_entry.save() |
Devin Slauenwhite | 98c39c4 | 2023-01-01 22:25:12 -0500 | [diff] [blame] | 92 | |
ruthra kumar | 72bc5b3 | 2023-06-27 16:11:03 +0530 | [diff] [blame] | 93 | # 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 Slauenwhite | 98c39c4 | 2023-01-01 22:25:12 -0500 | [diff] [blame] | 102 | payment_entry.references = [] |
ruthra kumar | 72bc5b3 | 2023-06-27 16:11:03 +0530 | [diff] [blame] | 103 | self.assertEqual(payment_entry.difference_amount, 0.0) |
Devin Slauenwhite | 98c39c4 | 2023-01-01 22:25:12 -0500 | [diff] [blame] | 104 | 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 | |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 129 | |
| 130 | ADDRESS_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": [ |
rohitwaghchaure | 058d983 | 2021-09-07 12:14:40 +0530 | [diff] [blame] | 139 | {"link_doctype": "Customer", "link_name": "_Test Customer 2", "doctype": "Dynamic Link"} |
| 140 | ], |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 141 | }, |
| 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": [ |
rohitwaghchaure | 058d983 | 2021-09-07 12:14:40 +0530 | [diff] [blame] | 150 | {"link_doctype": "Customer", "link_name": "_Test Customer 2", "doctype": "Dynamic Link"} |
| 151 | ], |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 152 | }, |
| 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": [ |
rohitwaghchaure | 058d983 | 2021-09-07 12:14:40 +0530 | [diff] [blame] | 162 | {"link_doctype": "Customer", "link_name": "_Test Customer 2", "doctype": "Dynamic Link"} |
| 163 | ], |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 164 | }, |
| 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": [ |
rohitwaghchaure | 058d983 | 2021-09-07 12:14:40 +0530 | [diff] [blame] | 174 | {"link_doctype": "Customer", "link_name": "_Test Customer 1", "doctype": "Dynamic Link"} |
| 175 | ], |
| 176 | }, |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 177 | ] |