blob: bf01362c97ff4aa33e64fa5f9de22c17d9b15dba [file] [log] [blame]
ruthra kumar3b580552023-07-28 21:02:21 +05301import frappe
ruthra kumaraf52f212023-08-21 14:54:03 +05302from frappe import qb
ruthra kumar3b580552023-07-28 21:02:21 +05303
4from erpnext.stock.doctype.item.test_item import create_item
5
6
7class AccountsTestMixin:
ruthra kumare64b0042023-08-08 20:27:12 +05308 def create_customer(self, customer_name="_Test Customer", currency=None):
ruthra kumar3b580552023-07-28 21:02:21 +05309 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 kumare64b0042023-08-08 20:27:12 +053021 def create_supplier(self, supplier_name="_Test Supplier", currency=None):
ruthra kumar3b580552023-07-28 21:02:21 +053022 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 kumare64b0042023-08-08 20:27:12 +053035 def create_item(self, item_name="_Test Item", is_stock=0, warehouse=None, company=None):
ruthra kumar3b580552023-07-28 21:02:21 +053036 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 kumar3b580552023-07-28 21:02:21 +053063 self.cash = "Cash - " + abbr
64 self.creditors = "Creditors - " + abbr
ruthra kumare64b0042023-08-08 20:27:12 +053065 self.retained_earnings = "Retained Earnings - " + abbr
ruthra kumar3b580552023-07-28 21:02:21 +053066
ruthra kumare64b0042023-08-08 20:27:12 +053067 # Deferred revenue, expense and bank accounts
68 other_accounts = [
69 frappe._dict(
ruthra kumar3b580552023-07-28 21:02:21 +053070 {
ruthra kumare64b0042023-08-08 20:27:12 +053071 "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 kumar3b580552023-07-28 21:02:21 +053086 "account_name": "HDFC",
87 "parent_account": "Bank Accounts - " + abbr,
ruthra kumar3b580552023-07-28 21:02:21 +053088 }
ruthra kumare64b0042023-08-08 20:27:12 +053089 ),
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 kumaraf52f212023-08-21 14:54:03 +0530106
ruthra kumarbb7bed42023-08-24 20:46:33 +0530107 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
ruthra kumaraf52f212023-08-21 14:54:03 +0530129 def clear_old_entries(self):
130 doctype_list = [
131 "GL Entry",
132 "Payment Ledger Entry",
133 "Sales Invoice",
134 "Purchase Invoice",
135 "Payment Entry",
136 "Journal Entry",
ruthra kumarbb7bed42023-08-24 20:46:33 +0530137 "Sales Order",
138 "Exchange Rate Revaluation",
ruthra kumaraf52f212023-08-21 14:54:03 +0530139 ]
140 for doctype in doctype_list:
141 qb.from_(qb.DocType(doctype)).delete().where(qb.DocType(doctype).company == self.company).run()