|
2 | 2 | # License: GNU General Public License v3. See license.txt
|
3 | 3 |
|
4 | 4 | import frappe
|
5 |
| -from frappe import _ |
| 5 | +from frappe import _, qb |
6 | 6 | from frappe.model.document import Document
|
| 7 | +from frappe.query_builder import Criterion |
| 8 | +from frappe.query_builder.custom import ConstantColumn |
7 | 9 | from frappe.utils import (
|
8 | 10 | add_days,
|
9 | 11 | comma_and,
|
@@ -721,59 +723,69 @@ def get_matching_queries(
|
721 | 723 | filter_by_reference_date=None,
|
722 | 724 | from_reference_date=None,
|
723 | 725 | to_reference_date=None,
|
| 726 | + common_filters=None, |
724 | 727 | ):
|
725 | 728 | """Returns matching queries for Bank Reconciliation"""
|
726 | 729 | queries = []
|
727 | 730 | if transaction.withdrawal > 0:
|
728 | 731 | if "expense_claim" in document_types:
|
729 | 732 | ec_amount_matching = get_ec_matching_query(
|
730 |
| - bank_account, company, exact_match, from_date, to_date |
| 733 | + bank_account, company, exact_match, from_date, to_date, common_filters |
731 | 734 | )
|
732 | 735 | queries.extend([ec_amount_matching])
|
733 | 736 |
|
734 | 737 | return queries
|
735 | 738 |
|
736 | 739 |
|
737 |
| -def get_ec_matching_query(bank_account, company, exact_match, from_date=None, to_date=None): |
| 740 | +def get_ec_matching_query( |
| 741 | + bank_account, company, exact_match, from_date=None, to_date=None, common_filters=None |
| 742 | +): |
738 | 743 | # get matching Expense Claim query
|
| 744 | + filters = [] |
| 745 | + ec = qb.DocType("Expense Claim") |
| 746 | + |
739 | 747 | mode_of_payments = [
|
740 | 748 | x["parent"]
|
741 | 749 | for x in frappe.db.get_all(
|
742 | 750 | "Mode of Payment Account", filters={"default_account": bank_account}, fields=["parent"]
|
743 | 751 | )
|
744 | 752 | ]
|
745 |
| - |
746 |
| - mode_of_payments = "('" + "', '".join(mode_of_payments) + "' )" |
747 | 753 | company_currency = get_company_currency(company)
|
748 | 754 |
|
749 |
| - filter_by_date = "" |
| 755 | + filters.append(ec.docstatus == 1) |
| 756 | + filters.append(ec.is_paid == 1) |
| 757 | + filters.append(ec.clearance_date.isnull()) |
| 758 | + filters.append(ec.mode_of_payment.isin(mode_of_payments)) |
| 759 | + if exact_match: |
| 760 | + filters.append(ec.total_sanctioned_amount == common_filters.amount) |
| 761 | + else: |
| 762 | + filters.append(ec.total_sanctioned_amount.gt(common_filters.amount)) |
| 763 | + |
750 | 764 | if from_date and to_date:
|
751 |
| - filter_by_date = f"AND posting_date BETWEEN '{from_date}' AND '{to_date}'" |
752 |
| - order_by = "posting_date" |
753 |
| - |
754 |
| - return f""" |
755 |
| - SELECT |
756 |
| - ( CASE WHEN employee = %(party)s THEN 1 ELSE 0 END |
757 |
| - + 1 ) AS rank, |
758 |
| - 'Expense Claim' as doctype, |
759 |
| - name, |
760 |
| - total_sanctioned_amount as paid_amount, |
761 |
| - '' as reference_no, |
762 |
| - '' as reference_date, |
763 |
| - employee as party, |
764 |
| - 'Employee' as party_type, |
765 |
| - posting_date, |
766 |
| - {company_currency!r} as currency |
767 |
| - FROM |
768 |
| - `tabExpense Claim` |
769 |
| - WHERE |
770 |
| - total_sanctioned_amount {'= %(amount)s' if exact_match else '> 0.0'} |
771 |
| - AND docstatus = 1 |
772 |
| - AND is_paid = 1 |
773 |
| - AND ifnull(clearance_date, '') = "" |
774 |
| - AND mode_of_payment in {mode_of_payments} |
775 |
| - {filter_by_date} |
776 |
| - """ |
| 765 | + filters.append(ec.posting_date[from_date:to_date]) |
| 766 | + |
| 767 | + ref_rank = frappe.qb.terms.Case().when(ec.employee == common_filters.party, 1).else_(0) |
| 768 | + |
| 769 | + ec_query = ( |
| 770 | + qb.from_(ec) |
| 771 | + .select( |
| 772 | + (ref_rank + 1).as_("rank"), |
| 773 | + ec.name, |
| 774 | + ec.total_sanctioned_amount.as_("paid_amount"), |
| 775 | + ConstantColumn("").as_("reference_no"), |
| 776 | + ConstantColumn("").as_("reference_date"), |
| 777 | + ec.employee.as_("party"), |
| 778 | + ConstantColumn("Employee").as_("party_type"), |
| 779 | + ec.posting_date, |
| 780 | + ConstantColumn(company_currency).as_("currency"), |
| 781 | + ) |
| 782 | + .where(Criterion.all(filters)) |
| 783 | + ) |
| 784 | + |
| 785 | + if from_date and to_date: |
| 786 | + ec_query = ec_query.orderby(ec.posting_date) |
| 787 | + |
| 788 | + return ec_query |
777 | 789 |
|
778 | 790 |
|
779 | 791 | def notify_bulk_action_status(doctype: str, failure: list, success: list) -> None:
|
|
0 commit comments