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, |
| 13 | ) |
| 14 | from erpnext.stock.doctype.item.test_item import make_item |
rohitwaghchaure | 058d983 | 2021-09-07 12:14:40 +0530 | [diff] [blame] | 15 | 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] | 16 | 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] | 17 | |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 18 | |
| 19 | class TestUtils(unittest.TestCase): |
| 20 | @classmethod |
| 21 | def setUpClass(cls): |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 22 | super().setUpClass() |
rohitwaghchaure | 058d983 | 2021-09-07 12:14:40 +0530 | [diff] [blame] | 23 | make_test_objects("Address", ADDRESS_RECORDS) |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 24 | |
Gursheen Anand | 813b7a9 | 2024-01-11 12:22:08 +0530 | [diff] [blame] | 25 | @classmethod |
| 26 | def tearDownClass(cls): |
Gursheen Anand | bbdf98a | 2024-01-10 22:06:06 +0530 | [diff] [blame] | 27 | frappe.db.rollback() |
| 28 | |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 29 | def test_get_party_shipping_address(self): |
rohitwaghchaure | 058d983 | 2021-09-07 12:14:40 +0530 | [diff] [blame] | 30 | address = get_party_shipping_address("Customer", "_Test Customer 1") |
| 31 | self.assertEqual(address, "_Test Billing Address 2 Title-Billing") |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 32 | |
| 33 | def test_get_party_shipping_address2(self): |
rohitwaghchaure | 058d983 | 2021-09-07 12:14:40 +0530 | [diff] [blame] | 34 | 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): |
rohitwaghchaure | 058d983 | 2021-09-07 12:14:40 +0530 | [diff] [blame] | 38 | 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 Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 58 | voucher_type_and_no in gl_entries, |
| 59 | msg="get_voucherwise_gl_entries not returning expected GLes", |
rohitwaghchaure | 058d983 | 2021-09-07 12:14:40 +0530 | [diff] [blame] | 60 | ) |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 61 | |
Ankush Menat | 700e864 | 2022-04-19 13:24:29 +0530 | [diff] [blame] | 62 | 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 Menat | 700e864 | 2022-04-19 13:24:29 +0530 | [diff] [blame] | 70 | se3 = make_stock_entry(posting_date="2022-03-01", **stock_entry) |
Ankush Menat | 2535d5e | 2022-06-14 18:20:33 +0530 | [diff] [blame] | 71 | se2 = make_stock_entry(posting_date="2022-02-01", **stock_entry) |
Ankush Menat | 700e864 | 2022-04-19 13:24:29 +0530 | [diff] [blame] | 72 | |
| 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 Slauenwhite | 98c39c4 | 2023-01-01 22:25:12 -0500 | [diff] [blame] | 81 | def test_update_reference_in_payment_entry(self): |
| 82 | item = make_item().name |
| 83 | |
| 84 | purchase_invoice = make_purchase_invoice( |
ruthra kumar | 72bc5b3 | 2023-06-27 16:11:03 +0530 | [diff] [blame] | 85 | 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] | 86 | ) |
ruthra kumar | 72bc5b3 | 2023-06-27 16:11:03 +0530 | [diff] [blame] | 87 | purchase_invoice.credit_to = "_Test Payable USD - _TC" |
Devin Slauenwhite | 98c39c4 | 2023-01-01 22:25:12 -0500 | [diff] [blame] | 88 | purchase_invoice.submit() |
| 89 | |
| 90 | payment_entry = get_payment_entry(purchase_invoice.doctype, purchase_invoice.name) |
Devin Slauenwhite | 98c39c4 | 2023-01-01 22:25:12 -0500 | [diff] [blame] | 91 | payment_entry.paid_amount = 15725 |
| 92 | payment_entry.deductions = [] |
ruthra kumar | 72bc5b3 | 2023-06-27 16:11:03 +0530 | [diff] [blame] | 93 | payment_entry.save() |
Devin Slauenwhite | 98c39c4 | 2023-01-01 22:25:12 -0500 | [diff] [blame] | 94 | |
ruthra kumar | 72bc5b3 | 2023-06-27 16:11:03 +0530 | [diff] [blame] | 95 | # 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 Slauenwhite | 98c39c4 | 2023-01-01 22:25:12 -0500 | [diff] [blame] | 104 | payment_entry.references = [] |
ruthra kumar | 72bc5b3 | 2023-06-27 16:11:03 +0530 | [diff] [blame] | 105 | self.assertEqual(payment_entry.difference_amount, 0.0) |
Devin Slauenwhite | 98c39c4 | 2023-01-01 22:25:12 -0500 | [diff] [blame] | 106 | 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 Anand | bbdf98a | 2024-01-10 22:06:06 +0530 | [diff] [blame] | 131 | 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 Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 145 | 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 Anand | bbdf98a | 2024-01-10 22:06:06 +0530 | [diff] [blame] | 147 | |
Gursheen Anand | bbdf98a | 2024-01-10 22:06:06 +0530 | [diff] [blame] | 148 | 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 Anand | 813b7a9 | 2024-01-11 12:22:08 +0530 | [diff] [blame] | 157 | frappe.db.set_default("supp_master_name", "Supplier Name") |
Gursheen Anand | bbdf98a | 2024-01-10 22:06:06 +0530 | [diff] [blame] | 158 | |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 159 | |
| 160 | ADDRESS_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 Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 168 | "links": [{"link_doctype": "Customer", "link_name": "_Test Customer 2", "doctype": "Dynamic Link"}], |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 169 | }, |
| 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 Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 177 | "links": [{"link_doctype": "Customer", "link_name": "_Test Customer 2", "doctype": "Dynamic Link"}], |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 178 | }, |
| 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 Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 187 | "links": [{"link_doctype": "Customer", "link_name": "_Test Customer 2", "doctype": "Dynamic Link"}], |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 188 | }, |
| 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 Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 197 | "links": [{"link_doctype": "Customer", "link_name": "_Test Customer 1", "doctype": "Dynamic Link"}], |
rohitwaghchaure | 058d983 | 2021-09-07 12:14:40 +0530 | [diff] [blame] | 198 | }, |
tundebabzy | 40a0276 | 2017-10-25 07:54:34 +0100 | [diff] [blame] | 199 | ] |