Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ERPNext support for Postgres #28359

Closed
wants to merge 56 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
f254f64
DEBUG: switch frappe repo temporarily for CI
ankush Nov 12, 2021
fa658ad
ci: postgres test job
ankush Nov 12, 2021
59b7d38
ci: run patch test on mariadb only
ankush Nov 18, 2021
4eaaa5a
create hsn's synchronously
c-p-b Nov 18, 2021
62e2592
feat: support postgres syntax #24389
c-p-b Dec 20, 2021
6f519ac
fix: remove extra function
c-p-b Dec 20, 2021
98a7401
Merge branch 'develop' into develop
Dec 20, 2021
29a49f9
trigger GitHub actions
c-p-b Dec 21, 2021
f698d8f
Merge branch 'develop' into develop
Dec 21, 2021
53866c6
fix: case sensitive warehouse name
c-p-b Dec 21, 2021
5b06e94
feat: sales_order_analysis postgres support
c-p-b Dec 21, 2021
94b6d60
Merge branch 'develop' into develop
Dec 28, 2021
4370133
fix: cost center order by
c-p-b Dec 28, 2021
d0a6b9b
fix: set max write transactions for db
c-p-b Dec 28, 2021
ec00c1b
fix: max write transaction for pcv
c-p-b Dec 28, 2021
6c514a3
fix: max write transactions for india setup
c-p-b Dec 28, 2021
b29bdd1
fix: revert auto_commit in chart of accounts
c-p-b Dec 28, 2021
905ae81
fix: revert auto_commit in various tests
c-p-b Dec 28, 2021
edc034b
fix: set max write transactions when creating charts in tests
c-p-b Dec 28, 2021
c0ad70c
fix chart of accounts import in tests
c-p-b Dec 28, 2021
384cac1
fix: test_tax_detail
c-p-b Dec 28, 2021
437ef26
fix: more max_write updates
c-p-b Dec 29, 2021
e414713
fix: more lcv fixes
c-p-b Dec 29, 2021
3a1e7e2
fix: more max_write updates
c-p-b Dec 29, 2021
ebf52e6
fix: more max_write updates
c-p-b Dec 29, 2021
ccc72d8
debug: try not escaping underscore
c-p-b Dec 29, 2021
9451e58
fix: more max_write updates
c-p-b Dec 29, 2021
d690058
fix: cwip account max_writes
c-p-b Dec 29, 2021
91f398e
Merge branch 'develop' into develop
ankush Jan 2, 2022
1604b35
revert: patch changes
ankush Jan 2, 2022
d8e00d7
chore: formatting changes
ankush Jan 2, 2022
928f1f0
Merge branch 'develop' into cpdeethree/develop
ankush Jan 2, 2022
fc5ba2d
ci: delete duplicate workflows
ankush Jan 2, 2022
f0014cf
Merge branch 'develop' into develop
Jan 4, 2022
7f09bf0
fix: is_pos requires int comparison in postgres
c-p-b Jan 4, 2022
70523be
DEBUG: change branch to frappe PR
ankush Jan 5, 2022
a2077ec
Revert "DEBUG: change branch to frappe PR"
c-p-b Jan 5, 2022
bb0ec33
Merge branch 'develop' into develop
Jan 5, 2022
16d72c5
Merge branch 'develop' into develop
Jan 11, 2022
4f93470
trigger GitHub actions
c-p-b Jan 12, 2022
001c4da
Merge branch 'develop' into develop
Jan 12, 2022
a9f79f5
Merge branch 'develop' into cpdeethree/develop
ankush Feb 4, 2022
9457aed
revert: ifnull -> coalesce
ankush Feb 4, 2022
9137c9d
fix: order of args
ankush Feb 4, 2022
a91e62d
debug: switch branch
ankush Feb 4, 2022
8150cc3
fix: dont abort transaction during fixture setup
ankush Feb 4, 2022
332f600
revert: commits during setup
ankush Feb 4, 2022
1b4bf3e
debug: disable parallel tests
ankush Feb 4, 2022
c3aa23e
Merge branch 'develop' into develop
Feb 7, 2022
5594208
Revert "debug: switch branch"
Feb 7, 2022
9b840ec
Merge branch 'develop' into develop
Feb 21, 2022
6ba7a07
fix: switch to fork for test
c-p-b Feb 21, 2022
48dd5be
Revert "debug: disable parallel tests"
c-p-b Feb 22, 2022
07558ed
Merge branch 'develop' into develop
Feb 23, 2022
99c4815
debug: remove savepoint code from india setup
Feb 24, 2022
d1bf52e
Revert "fix: switch to fork for test"
ankush Feb 26, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions erpnext/accounts/doctype/account/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ def on_trash(self):
def get_parent_account(doctype, txt, searchfield, start, page_len, filters):
return frappe.db.sql("""select name from tabAccount
where is_group = 1 and docstatus != 2 and company = %s
and %s like %s order by name limit %s, %s""" %
and %s like %s order by name limit %s offset %s""" %
("%s", searchfield, "%s", "%s", "%s"),
(filters["company"], "%%%s%%" % txt, start, page_len), as_list=1)
(filters["company"], "%%%s%%" % txt, page_len, start), as_list=1)

def get_account_currency(account):
"""Helper function to get account currency"""
Expand Down Expand Up @@ -381,6 +381,5 @@ def sync_update_account_number_in_child(descendants, old_acc_name, account_name,
}
if old_acc_number:
filters["account_number"] = old_acc_number

for d in frappe.db.get_values('Account', filters=filters, fieldname=["company", "name"], as_dict=True):
for d in frappe.db.get_values('Account', filters=filters, fieldname=["company", "name"], as_dict=True, order_by = 'company asc'):
update_account_number(d["name"], account_name, account_number, from_descendant=True)
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@


def create_charts(company, chart_template=None, existing_company=None, custom_chart=None, from_coa_importer=None):

chart = custom_chart or get_chart(chart_template, existing_company)
if chart:
accounts = []

def _import_accounts(children, parent, root_type, root_account=False):
if frappe.flags.in_test or frappe.flags.in_install or frappe.flags.in_setup_wizard:
frappe.db.MAX_WRITES_PER_TRANSACTION = 200_000_000
for account_name, child in children.items():
if root_account:
root_type = child.get("root_type")
Expand Down Expand Up @@ -55,14 +58,17 @@ def _import_accounts(children, parent, root_type, root_account=False):
accounts.append(account_name_in_db)

_import_accounts(child, account.name, root_type)

if frappe.flags.in_test or frappe.flags.in_install or frappe.flags.in_setup_wizard:
frappe.db.MAX_WRITES_PER_TRANSACTION = 200_000
frappe.db.commit()
# Rebuild NestedSet HSM tree for Account Doctype
# after all accounts are already inserted.
frappe.local.flags.ignore_update_nsm = True
_import_accounts(chart, None, None, root_account=True)
rebuild_tree("Account", "parent_account")
frappe.local.flags.ignore_update_nsm = False


def add_suffix_if_duplicate(account_name, account_number, accounts):
if account_number:
account_name_in_db = unidecode(" - ".join([account_number,
Expand Down
52 changes: 45 additions & 7 deletions erpnext/accounts/doctype/bank_clearance/bank_clearance.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ def get_payment_entries(self):
if not self.account:
frappe.throw(_("Account is mandatory to get payment entries"))

condition = ""
mdb_condition = ""
pg_condition = ""
if not self.include_reconciled_entries:
condition = "and (clearance_date IS NULL or clearance_date='0000-00-00')"
mdb_condition = "and (clearance_date IS NULL or clearance_date='0000-00-00')"
pg_condition = "and (clearance_date IS NULL)"

journal_entries = frappe.db.sql("""
journal_entries = frappe.db.multisql({
'mariadb': """
select
"Journal Entry" as payment_document, t1.name as payment_entry,
t1.cheque_no as cheque_number, t1.cheque_date,
Expand All @@ -38,12 +41,46 @@ def get_payment_entries(self):
and ifnull(t1.is_opening, 'No') = 'No' {condition}
group by t2.account, t1.name
order by t1.posting_date ASC, t1.name DESC
""".format(condition=condition), {"account": self.account, "from": self.from_date, "to": self.to_date}, as_dict=1)
""".format(condition=mdb_condition),
'postgres': """
select
"Journal Entry" as payment_document, t1.name as payment_entry,
t1.cheque_no as cheque_number, t1.cheque_date,
sum(t2.debit_in_account_currency) as debit, sum(t2.credit_in_account_currency) as credit,
t1.posting_date, t2.against_account, t1.clearance_date, t2.account_currency
from
`tabJournal Entry` t1, `tabJournal Entry Account` t2
where
t2.parent = t1.name and t2.account = %(account)s and t1.docstatus=1
and t1.posting_date >= %(from)s and t1.posting_date <= %(to)s
and ifnull(t1.is_opening, 'No') = 'No' {condition}
group by t2.account, t1.name
order by t1.posting_date ASC, t1.name DESC
""".format(condition=pg_condition)
}, {"account": self.account, "from": self.from_date, "to": self.to_date}, as_dict=1)

if self.bank_account:
condition += 'and bank_account = %(bank_account)s'
mdb_condition += 'and bank_account = %(bank_account)s'
pg_condition += 'and bank_account = %(bank_account)s'

payment_entries = frappe.db.sql("""
payment_entries = frappe.db.multisql({
'mariadb': """
select
"Payment Entry" as payment_document, name as payment_entry,
reference_no as cheque_number, reference_date as cheque_date,
if(paid_from=%(account)s, paid_amount, 0) as credit,
if(paid_from=%(account)s, 0, received_amount) as debit,
posting_date, ifnull(party,if(paid_from=%(account)s,paid_to,paid_from)) as against_account, clearance_date,
if(paid_to=%(account)s, paid_to_account_currency, paid_from_account_currency) as account_currency
from `tabPayment Entry`
where
(paid_from=%(account)s or paid_to=%(account)s) and docstatus=1
and posting_date >= %(from)s and posting_date <= %(to)s
{condition}
order by
posting_date ASC, name DESC
""".format(condition=mdb_condition),
'postgres': """
select
"Payment Entry" as payment_document, name as payment_entry,
reference_no as cheque_number, reference_date as cheque_date,
Expand All @@ -58,7 +95,8 @@ def get_payment_entries(self):
{condition}
order by
posting_date ASC, name DESC
""".format(condition=condition), {"account": self.account, "from":self.from_date,
""".format(condition=pg_condition)
}, {"account": self.account, "from":self.from_date,
"to": self.to_date, "bank_account": self.bank_account}, as_dict=1)

pos_sales_invoices, pos_purchase_invoices = [], []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def check_matching(bank_account, company, transaction, document_types):

for query in subquery:
matching_vouchers.extend(
frappe.db.sql(query, filters,)
frappe.db.sql(query, filters)
)

return sorted(matching_vouchers, key = lambda x: x[0], reverse=True) if matching_vouchers else []
Expand Down Expand Up @@ -450,13 +450,16 @@ def get_pe_matching_query(amount_condition, account_from_to, transaction):
paid_amount {amount_condition} %(amount)s
AND docstatus = 1
AND payment_type IN (%(payment_type)s, 'Internal Transfer')
AND ifnull(clearance_date, '') = ""
AND ifnull(cast(clearance_date as char), '') = ''
AND {account_from_to} = %(bank_account)s
ORDER BY modified
"""


def get_je_matching_query(amount_condition, transaction):

# get matching journal entry query
query_param = "or je.clearance_date='0000-00-00'" if frappe.db.db_type == "mariadb" else ""

# We have mapping at the bank level
# So one bank could have both types of bank accounts like asset and liability
Expand Down Expand Up @@ -484,15 +487,18 @@ def get_je_matching_query(amount_condition, transaction):
ON
jea.parent = je.name
WHERE
(je.clearance_date is null or je.clearance_date='0000-00-00')
(je.clearance_date is null {query_param})
AND jea.account = %(bank_account)s
AND jea.{cr_or_dr}_in_account_currency {amount_condition} %(amount)s
AND je.docstatus = 1
ORDER BY jea.modified
"""


def get_si_matching_query(amount_condition):
# get matchin sales invoice query
query_param = "or sip.clearance_date='0000-00-00'" if frappe.db.db_type == "mariadb" else ""

return f"""
SELECT
( CASE WHEN si.customer = %(party)s THEN 1 ELSE 0 END
Expand All @@ -513,10 +519,11 @@ def get_si_matching_query(amount_condition):
`tabSales Invoice` as si
ON
sip.parent = si.name
WHERE (sip.clearance_date is null or sip.clearance_date='0000-00-00')
WHERE (sip.clearance_date is null {query_param})
AND sip.account = %(bank_account)s
AND sip.amount {amount_condition} %(amount)s
AND si.docstatus = 1
ORDER BY si.modified
"""

def get_pi_matching_query(amount_condition):
Expand All @@ -540,8 +547,9 @@ def get_pi_matching_query(amount_condition):
paid_amount {amount_condition} %(amount)s
AND docstatus = 1
AND is_paid = 1
AND ifnull(clearance_date, '') = ""
AND ifnull(cast(clearance_date as text), '') = ""
AND cash_bank_account = %(bank_account)s
ORDER BY modified
"""

def get_ec_matching_query(bank_account, company, amount_condition):
Expand Down Expand Up @@ -571,4 +579,5 @@ def get_ec_matching_query(bank_account, company, amount_condition):
AND is_paid = 1
AND ifnull(clearance_date, '') = ""
AND mode_of_payment in {mode_of_payments}
ORDER BY modified
"""
6 changes: 4 additions & 2 deletions erpnext/accounts/doctype/budget/budget.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,11 @@ def get_requested_amount(args, budget):
condition = get_other_condition(args, budget, 'Material Request')

data = frappe.db.sql(""" select ifnull((sum(child.stock_qty - child.ordered_qty) * rate), 0) as amount
from `tabMaterial Request Item` child, `tabMaterial Request` parent where parent.name = child.parent and
from `tabMaterial Request Item` child, `tabMaterial Request` parent
where parent.name = child.parent and
child.item_code = %s and parent.docstatus = 1 and child.stock_qty > child.ordered_qty and {0} and
parent.material_request_type = 'Purchase' and parent.status != 'Stopped'""".format(condition), item_code, as_list=1)
parent.material_request_type = 'Purchase' and parent.status != 'Stopped'
group by child.rate""".format(condition), item_code, as_list=1)

return data[0][0] if data else 0

Expand Down
6 changes: 3 additions & 3 deletions erpnext/accounts/doctype/journal_entry/journal_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def validate_against_jv(self):

against_entries = frappe.db.sql("""select * from `tabJournal Entry Account`
where account = %s and docstatus = 1 and parent = %s
and (reference_type is null or reference_type in ("", "Sales Order", "Purchase Order"))
and (reference_type is null or reference_type in ('', 'Sales Order', 'Purchase Order'))
""", (d.account, d.reference_name), as_dict=True)

if not against_entries:
Expand Down Expand Up @@ -992,15 +992,15 @@ def get_against_jv(doctype, txt, searchfield, start, page_len, filters):
FROM `tabJournal Entry` jv, `tabJournal Entry Account` jv_detail
WHERE jv_detail.parent = jv.name
AND jv_detail.account = %(account)s
AND IFNULL(jv_detail.party, '') = %(party)s
AND ifnull(jv_detail.party, '') = %(party)s
AND (
jv_detail.reference_type IS NULL
OR jv_detail.reference_type = ''
)
AND jv.docstatus = 1
AND jv.`{0}` LIKE %(txt)s
ORDER BY jv.name DESC
LIMIT %(offset)s, %(limit)s
LIMIT %(limit)s offset %(offset)s
""".format(searchfield), dict(
account=filters.get("account"),
party=cstr(filters.get("party")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ def test_jv_account_and_party_balance_with_cost_centre(self):
self.assertEqual(expected_account_balance, account_balance)

def make_journal_entry(account1, account2, amount, cost_center=None, posting_date=None, exchange_rate=1, save=True, submit=False, project=None):
frappe.db.MAX_WRITES_PER_TRANSACTION = 200_000_000
if not cost_center:
cost_center = "_Test Cost Center - _TC"

Expand Down Expand Up @@ -391,7 +392,8 @@ def make_journal_entry(account1, account2, amount, cost_center=None, posting_dat

if submit:
jv.submit()

frappe.db.commit()
frappe.db.MAX_WRITES_PER_TRANSACTION = 200_000
return jv

test_records = frappe.get_test_records('Journal Entry')
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ def get_returned_amount():
returned_amount = get_returned_amount()
eligible_amount = flt(self.grand_total) - cint(self.loyalty_amount) - returned_amount
points_earned = cint(eligible_amount/lp_details.collection_factor)

return points_earned or 0

def create_sales_invoice_record(qty=1):
Expand All @@ -180,7 +179,7 @@ def create_sales_invoice_record(qty=1):
"due_date": today(),
"posting_date": today(),
"currency": "INR",
"taxes_and_charges": "",
"taxes_and_charges": '',
"debit_to": "Debtors - _TC",
"taxes": [],
"items": [{
Expand Down
2 changes: 1 addition & 1 deletion erpnext/accounts/doctype/payment_entry/payment_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ def get_negative_outstanding_invoices(party_type, party, party_account,
voucher_type = "Sales Invoice" if party_type == "Customer" else "Purchase Invoice"
supplier_condition = ""
if voucher_type == "Purchase Invoice":
supplier_condition = "and (release_date is null or release_date <= CURDATE())"
supplier_condition = "and (release_date is null or release_date <= CURRENT_DATE)"
if party_account_currency == company_currency:
grand_total_field = "base_grand_total"
rounded_total_field = "base_rounded_total"
Expand Down
4 changes: 2 additions & 2 deletions erpnext/accounts/doctype/payment_order/payment_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def update_payment_status(self, cancel=False):
def get_mop_query(doctype, txt, searchfield, start, page_len, filters):
return frappe.db.sql(""" select mode_of_payment from `tabPayment Order Reference`
where parent = %(parent)s and mode_of_payment like %(txt)s
limit %(start)s, %(page_len)s""", {
limit %(page_len)s offset %(start)s""", {
'parent': filters.get("parent"),
'start': start,
'page_len': page_len,
Expand All @@ -50,7 +50,7 @@ def get_supplier_query(doctype, txt, searchfield, start, page_len, filters):
return frappe.db.sql(""" select supplier from `tabPayment Order Reference`
where parent = %(parent)s and supplier like %(txt)s and
(payment_reference is null or payment_reference='')
limit %(start)s, %(page_len)s""", {
limit %(page_len)s offset %(start)s""", {
'parent': filters.get("parent"),
'start': start,
'page_len': page_len,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,5 @@ def get_pl_balances(self):
where t1.is_cancelled = 0 and t1.account = t2.name and t2.report_type = 'Profit and Loss'
and t2.docstatus < 2 and t2.company = %s
and t1.posting_date between %s and %s
group by t1.account, {dimension_fields}
group by t1.account, t2.account_currency, {dimension_fields}
""".format(dimension_fields = ', '.join(dimension_fields)), (self.company, self.get("year_start_date"), self.posting_date), as_dict=1)
Loading