ruthra kumar | 3b58055 | 2023-07-28 21:02:21 +0530 | [diff] [blame] | 1 | import frappe |
ruthra kumar | af52f21 | 2023-08-21 14:54:03 +0530 | [diff] [blame] | 2 | from frappe import qb |
ruthra kumar | 3b58055 | 2023-07-28 21:02:21 +0530 | [diff] [blame] | 3 | |
| 4 | from erpnext.stock.doctype.item.test_item import create_item |
| 5 | |
| 6 | |
| 7 | class AccountsTestMixin: |
ruthra kumar | e64b004 | 2023-08-08 20:27:12 +0530 | [diff] [blame] | 8 | def create_customer(self, customer_name="_Test Customer", currency=None): |
ruthra kumar | 3b58055 | 2023-07-28 21:02:21 +0530 | [diff] [blame] | 9 | if not frappe.db.exists("Customer", customer_name): |
| 10 | customer = frappe.new_doc("Customer") |
| 11 | customer.customer_name = customer_name |
| 12 | customer.type = "Individual" |
| 13 | |
| 14 | if currency: |
| 15 | customer.default_currency = currency |
| 16 | customer.save() |
| 17 | self.customer = customer.name |
| 18 | else: |
| 19 | self.customer = customer_name |
| 20 | |
ruthra kumar | e64b004 | 2023-08-08 20:27:12 +0530 | [diff] [blame] | 21 | def create_supplier(self, supplier_name="_Test Supplier", currency=None): |
ruthra kumar | 3b58055 | 2023-07-28 21:02:21 +0530 | [diff] [blame] | 22 | if not frappe.db.exists("Supplier", supplier_name): |
| 23 | supplier = frappe.new_doc("Supplier") |
| 24 | supplier.supplier_name = supplier_name |
| 25 | supplier.supplier_type = "Individual" |
| 26 | supplier.supplier_group = "Local" |
| 27 | |
| 28 | if currency: |
| 29 | supplier.default_currency = currency |
| 30 | supplier.save() |
| 31 | self.supplier = supplier.name |
| 32 | else: |
| 33 | self.supplier = supplier_name |
| 34 | |
ruthra kumar | e64b004 | 2023-08-08 20:27:12 +0530 | [diff] [blame] | 35 | def create_item(self, item_name="_Test Item", is_stock=0, warehouse=None, company=None): |
ruthra kumar | 3b58055 | 2023-07-28 21:02:21 +0530 | [diff] [blame] | 36 | item = create_item(item_name, is_stock_item=is_stock, warehouse=warehouse, company=company) |
| 37 | self.item = item.name |
| 38 | |
| 39 | def create_company(self, company_name="_Test Company", abbr="_TC"): |
| 40 | self.company_abbr = abbr |
| 41 | if frappe.db.exists("Company", company_name): |
| 42 | company = frappe.get_doc("Company", company_name) |
| 43 | else: |
| 44 | company = frappe.get_doc( |
| 45 | { |
| 46 | "doctype": "Company", |
| 47 | "company_name": company_name, |
| 48 | "country": "India", |
| 49 | "default_currency": "INR", |
| 50 | "create_chart_of_accounts_based_on": "Standard Template", |
| 51 | "chart_of_accounts": "Standard", |
| 52 | } |
| 53 | ) |
| 54 | company = company.save() |
| 55 | |
| 56 | self.company = company.name |
| 57 | self.cost_center = company.cost_center |
| 58 | self.warehouse = "Stores - " + abbr |
| 59 | self.finished_warehouse = "Finished Goods - " + abbr |
| 60 | self.income_account = "Sales - " + abbr |
| 61 | self.expense_account = "Cost of Goods Sold - " + abbr |
| 62 | self.debit_to = "Debtors - " + abbr |
ruthra kumar | 3b58055 | 2023-07-28 21:02:21 +0530 | [diff] [blame] | 63 | self.cash = "Cash - " + abbr |
| 64 | self.creditors = "Creditors - " + abbr |
ruthra kumar | e64b004 | 2023-08-08 20:27:12 +0530 | [diff] [blame] | 65 | self.retained_earnings = "Retained Earnings - " + abbr |
ruthra kumar | 3b58055 | 2023-07-28 21:02:21 +0530 | [diff] [blame] | 66 | |
ruthra kumar | e64b004 | 2023-08-08 20:27:12 +0530 | [diff] [blame] | 67 | # Deferred revenue, expense and bank accounts |
| 68 | other_accounts = [ |
| 69 | frappe._dict( |
ruthra kumar | 3b58055 | 2023-07-28 21:02:21 +0530 | [diff] [blame] | 70 | { |
ruthra kumar | e64b004 | 2023-08-08 20:27:12 +0530 | [diff] [blame] | 71 | "attribute_name": "deferred_revenue", |
| 72 | "account_name": "Deferred Revenue", |
| 73 | "parent_account": "Current Liabilities - " + abbr, |
| 74 | } |
| 75 | ), |
| 76 | frappe._dict( |
| 77 | { |
| 78 | "attribute_name": "deferred_expense", |
| 79 | "account_name": "Deferred Expense", |
| 80 | "parent_account": "Current Assets - " + abbr, |
| 81 | } |
| 82 | ), |
| 83 | frappe._dict( |
| 84 | { |
| 85 | "attribute_name": "bank", |
ruthra kumar | 3b58055 | 2023-07-28 21:02:21 +0530 | [diff] [blame] | 86 | "account_name": "HDFC", |
| 87 | "parent_account": "Bank Accounts - " + abbr, |
ruthra kumar | 3b58055 | 2023-07-28 21:02:21 +0530 | [diff] [blame] | 88 | } |
ruthra kumar | e64b004 | 2023-08-08 20:27:12 +0530 | [diff] [blame] | 89 | ), |
| 90 | ] |
| 91 | for acc in other_accounts: |
| 92 | acc_name = acc.account_name + " - " + abbr |
| 93 | if frappe.db.exists("Account", acc_name): |
| 94 | setattr(self, acc.attribute_name, acc_name) |
| 95 | else: |
| 96 | new_acc = frappe.get_doc( |
| 97 | { |
| 98 | "doctype": "Account", |
| 99 | "account_name": acc.account_name, |
| 100 | "parent_account": acc.parent_account, |
| 101 | "company": self.company, |
| 102 | } |
| 103 | ) |
| 104 | new_acc.save() |
| 105 | setattr(self, acc.attribute_name, new_acc.name) |
ruthra kumar | af52f21 | 2023-08-21 14:54:03 +0530 | [diff] [blame] | 106 | |
ruthra kumar | bb7bed4 | 2023-08-24 20:46:33 +0530 | [diff] [blame] | 107 | def create_usd_receivable_account(self): |
| 108 | account_name = "Debtors USD" |
| 109 | if not frappe.db.get_value( |
| 110 | "Account", filters={"account_name": account_name, "company": self.company} |
| 111 | ): |
| 112 | acc = frappe.new_doc("Account") |
| 113 | acc.account_name = account_name |
| 114 | acc.parent_account = "Accounts Receivable - " + self.company_abbr |
| 115 | acc.company = self.company |
| 116 | acc.account_currency = "USD" |
| 117 | acc.account_type = "Receivable" |
| 118 | acc.insert() |
| 119 | else: |
| 120 | name = frappe.db.get_value( |
| 121 | "Account", |
| 122 | filters={"account_name": account_name, "company": self.company}, |
| 123 | fieldname="name", |
| 124 | pluck=True, |
| 125 | ) |
| 126 | acc = frappe.get_doc("Account", name) |
| 127 | self.debtors_usd = acc.name |
| 128 | |
RitvikSardana | e599f75 | 2023-09-02 13:08:08 +0530 | [diff] [blame] | 129 | def create_usd_payable_account(self): |
| 130 | account_name = "Creditors USD" |
| 131 | if not frappe.db.get_value( |
| 132 | "Account", filters={"account_name": account_name, "company": self.company} |
| 133 | ): |
| 134 | acc = frappe.new_doc("Account") |
| 135 | acc.account_name = account_name |
| 136 | acc.parent_account = "Accounts Payable - " + self.company_abbr |
| 137 | acc.company = self.company |
| 138 | acc.account_currency = "USD" |
| 139 | acc.account_type = "Payable" |
| 140 | acc.insert() |
| 141 | else: |
| 142 | name = frappe.db.get_value( |
| 143 | "Account", |
| 144 | filters={"account_name": account_name, "company": self.company}, |
| 145 | fieldname="name", |
| 146 | pluck=True, |
| 147 | ) |
| 148 | acc = frappe.get_doc("Account", name) |
| 149 | self.creditors_usd = acc.name |
| 150 | |
ruthra kumar | af52f21 | 2023-08-21 14:54:03 +0530 | [diff] [blame] | 151 | def clear_old_entries(self): |
| 152 | doctype_list = [ |
| 153 | "GL Entry", |
| 154 | "Payment Ledger Entry", |
| 155 | "Sales Invoice", |
| 156 | "Purchase Invoice", |
| 157 | "Payment Entry", |
| 158 | "Journal Entry", |
ruthra kumar | bb7bed4 | 2023-08-24 20:46:33 +0530 | [diff] [blame] | 159 | "Sales Order", |
| 160 | "Exchange Rate Revaluation", |
ruthra kumar | 2be0253 | 2023-09-15 14:44:07 +0530 | [diff] [blame] | 161 | "Bank Account", |
| 162 | "Bank Transaction", |
ruthra kumar | af52f21 | 2023-08-21 14:54:03 +0530 | [diff] [blame] | 163 | ] |
| 164 | for doctype in doctype_list: |
| 165 | qb.from_(qb.DocType(doctype)).delete().where(qb.DocType(doctype).company == self.company).run() |