mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-18 12:39:18 +00:00
Compare commits
4 Commits
v14.74.4
...
mergify/bp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9a3c8a3c11 | ||
|
|
13f5634570 | ||
|
|
7ff97794f4 | ||
|
|
9ac973fff6 |
@@ -3,7 +3,7 @@ import inspect
|
||||
|
||||
import frappe
|
||||
|
||||
__version__ = "14.74.4"
|
||||
__version__ = "14.67.1"
|
||||
|
||||
|
||||
def get_default_company(user=None):
|
||||
|
||||
@@ -129,31 +129,19 @@ def calculate_interest_and_amount(outstanding_amount, rate_of_interest, dunning_
|
||||
|
||||
|
||||
@frappe.whitelist()
|
||||
def get_dunning_letter_text(dunning_type: str, doc: str | dict, language: str | None = None) -> dict:
|
||||
DOCTYPE = "Dunning Letter Text"
|
||||
FIELDS = ["body_text", "closing_text", "language"]
|
||||
|
||||
def get_dunning_letter_text(dunning_type, doc, language=None):
|
||||
if isinstance(doc, str):
|
||||
doc = json.loads(doc)
|
||||
|
||||
if not language:
|
||||
language = doc.get("language")
|
||||
|
||||
if language:
|
||||
letter_text = frappe.db.get_value(
|
||||
DOCTYPE, {"parent": dunning_type, "language": language}, FIELDS, as_dict=1
|
||||
)
|
||||
|
||||
if not letter_text:
|
||||
letter_text = frappe.db.get_value(
|
||||
DOCTYPE, {"parent": dunning_type, "is_default_language": 1}, FIELDS, as_dict=1
|
||||
)
|
||||
|
||||
if not letter_text:
|
||||
return {}
|
||||
|
||||
return {
|
||||
"body_text": frappe.render_template(letter_text.body_text, doc),
|
||||
"closing_text": frappe.render_template(letter_text.closing_text, doc),
|
||||
"language": letter_text.language,
|
||||
}
|
||||
filters = {"parent": dunning_type, "language": language}
|
||||
else:
|
||||
filters = {"parent": dunning_type, "is_default_language": 1}
|
||||
letter_text = frappe.db.get_value(
|
||||
"Dunning Letter Text", filters, ["body_text", "closing_text", "language"], as_dict=1
|
||||
)
|
||||
if letter_text:
|
||||
return {
|
||||
"body_text": frappe.render_template(letter_text.body_text, doc),
|
||||
"closing_text": frappe.render_template(letter_text.closing_text, doc),
|
||||
"language": letter_text.language,
|
||||
}
|
||||
|
||||
@@ -154,17 +154,6 @@ frappe.ui.form.on('Payment Entry', {
|
||||
};
|
||||
});
|
||||
|
||||
frm.set_query("payment_request", "references", function (doc, cdt, cdn) {
|
||||
const row = frappe.get_doc(cdt, cdn);
|
||||
return {
|
||||
query: "erpnext.accounts.doctype.payment_request.payment_request.get_open_payment_requests_query",
|
||||
filters: {
|
||||
reference_doctype: row.reference_doctype,
|
||||
reference_name: row.reference_name,
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
frm.set_query("sales_taxes_and_charges_template", function () {
|
||||
return {
|
||||
filters: {
|
||||
@@ -182,15 +171,7 @@ frappe.ui.form.on('Payment Entry', {
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
frm.add_fetch(
|
||||
"payment_request",
|
||||
"outstanding_amount",
|
||||
"payment_request_outstanding",
|
||||
"Payment Entry Reference"
|
||||
);
|
||||
},
|
||||
|
||||
refresh: function (frm) {
|
||||
erpnext.hide_company(frm);
|
||||
frm.events.hide_unhide_fields(frm);
|
||||
@@ -203,7 +184,6 @@ frappe.ui.form.on('Payment Entry', {
|
||||
|
||||
}
|
||||
erpnext.accounts.unreconcile_payment.add_unreconcile_btn(frm);
|
||||
frappe.flags.allocate_payment_amount = true;
|
||||
},
|
||||
|
||||
validate_company: (frm) => {
|
||||
@@ -653,9 +633,10 @@ frappe.ui.form.on('Payment Entry', {
|
||||
frm.set_value("base_received_amount",
|
||||
flt(frm.doc.received_amount) * flt(frm.doc.target_exchange_rate));
|
||||
|
||||
if (frm.doc.payment_type == "Pay")
|
||||
frm.events.allocate_party_amount_against_ref_docs(frm, frm.doc.received_amount, true);
|
||||
else frm.events.set_unallocated_amount(frm);
|
||||
if(frm.doc.payment_type == "Pay")
|
||||
frm.events.allocate_party_amount_against_ref_docs(frm, frm.doc.received_amount, 1);
|
||||
else
|
||||
frm.events.set_unallocated_amount(frm);
|
||||
|
||||
frm.set_paid_amount_based_on_received_amount = false;
|
||||
frm.events.hide_unhide_fields(frm);
|
||||
@@ -673,9 +654,10 @@ frappe.ui.form.on('Payment Entry', {
|
||||
frm.set_value("base_received_amount", frm.doc.base_paid_amount);
|
||||
}
|
||||
|
||||
if (frm.doc.payment_type == "Receive")
|
||||
frm.events.allocate_party_amount_against_ref_docs(frm, frm.doc.paid_amount, true);
|
||||
else frm.events.set_unallocated_amount(frm);
|
||||
if(frm.doc.payment_type == "Receive")
|
||||
frm.events.allocate_party_amount_against_ref_docs(frm, frm.doc.paid_amount, 1);
|
||||
else
|
||||
frm.events.set_unallocated_amount(frm);
|
||||
},
|
||||
|
||||
get_outstanding_invoices_or_orders: function(frm, get_outstanding_invoices, get_orders_to_be_billed) {
|
||||
@@ -819,7 +801,6 @@ frappe.ui.form.on('Payment Entry', {
|
||||
c.outstanding_amount = d.outstanding_amount;
|
||||
c.bill_no = d.bill_no;
|
||||
c.payment_term = d.payment_term;
|
||||
c.payment_term_outstanding = d.payment_term_outstanding;
|
||||
c.allocated_amount = d.allocated_amount;
|
||||
|
||||
if(!in_list(frm.events.get_order_doctypes(frm), d.voucher_type)) {
|
||||
@@ -861,12 +842,10 @@ frappe.ui.form.on('Payment Entry', {
|
||||
}
|
||||
}
|
||||
|
||||
frm.events.allocate_party_amount_against_ref_docs(
|
||||
frm,
|
||||
frm.doc.payment_type == "Receive" ? frm.doc.paid_amount : frm.doc.received_amount,
|
||||
false
|
||||
);
|
||||
},
|
||||
frm.events.allocate_party_amount_against_ref_docs(frm,
|
||||
(frm.doc.payment_type=="Receive" ? frm.doc.paid_amount : frm.doc.received_amount));
|
||||
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
@@ -878,13 +857,76 @@ frappe.ui.form.on('Payment Entry', {
|
||||
return ["Sales Invoice", "Purchase Invoice"];
|
||||
},
|
||||
|
||||
allocate_party_amount_against_ref_docs: async function (frm, paid_amount, paid_amount_change) {
|
||||
await frm.call("allocate_amount_to_references", {
|
||||
paid_amount: paid_amount,
|
||||
paid_amount_change: paid_amount_change,
|
||||
allocate_payment_amount: frappe.flags.allocate_payment_amount ?? false,
|
||||
});
|
||||
allocate_party_amount_against_ref_docs: function(frm, paid_amount, paid_amount_change) {
|
||||
var total_positive_outstanding_including_order = 0;
|
||||
var total_negative_outstanding = 0;
|
||||
var total_deductions = frappe.utils.sum($.map(frm.doc.deductions || [],
|
||||
function(d) { return flt(d.amount) }));
|
||||
|
||||
paid_amount -= total_deductions;
|
||||
|
||||
$.each(frm.doc.references || [], function(i, row) {
|
||||
if(flt(row.outstanding_amount) > 0)
|
||||
total_positive_outstanding_including_order += flt(row.outstanding_amount);
|
||||
else
|
||||
total_negative_outstanding += Math.abs(flt(row.outstanding_amount));
|
||||
})
|
||||
var allocated_negative_outstanding = 0;
|
||||
if (
|
||||
(frm.doc.payment_type=="Receive" && frm.doc.party_type=="Customer") ||
|
||||
(frm.doc.payment_type=="Pay" && frm.doc.party_type=="Supplier") ||
|
||||
(frm.doc.payment_type=="Pay" && frm.doc.party_type=="Employee")
|
||||
) {
|
||||
if(total_positive_outstanding_including_order > paid_amount) {
|
||||
var remaining_outstanding = total_positive_outstanding_including_order - paid_amount;
|
||||
allocated_negative_outstanding = total_negative_outstanding < remaining_outstanding ?
|
||||
total_negative_outstanding : remaining_outstanding;
|
||||
}
|
||||
|
||||
var allocated_positive_outstanding = paid_amount + allocated_negative_outstanding;
|
||||
} else if (["Customer", "Supplier"].includes(frm.doc.party_type)) {
|
||||
total_negative_outstanding = flt(total_negative_outstanding, precision("outstanding_amount"))
|
||||
if(paid_amount > total_negative_outstanding) {
|
||||
if(total_negative_outstanding == 0) {
|
||||
frappe.msgprint(
|
||||
__("Cannot {0} {1} {2} without any negative outstanding invoice", [frm.doc.payment_type,
|
||||
(frm.doc.party_type=="Customer" ? "to" : "from"), frm.doc.party_type])
|
||||
);
|
||||
return false
|
||||
} else {
|
||||
frappe.msgprint(
|
||||
__("Paid Amount cannot be greater than total negative outstanding amount {0}", [total_negative_outstanding])
|
||||
);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
allocated_positive_outstanding = total_negative_outstanding - paid_amount;
|
||||
allocated_negative_outstanding = paid_amount +
|
||||
(total_positive_outstanding_including_order < allocated_positive_outstanding ?
|
||||
total_positive_outstanding_including_order : allocated_positive_outstanding)
|
||||
}
|
||||
}
|
||||
|
||||
$.each(frm.doc.references || [], function(i, row) {
|
||||
if (frappe.flags.allocate_payment_amount == 0) {
|
||||
//If allocate payment amount checkbox is unchecked, set zero to allocate amount
|
||||
row.allocated_amount = 0;
|
||||
|
||||
} else if (frappe.flags.allocate_payment_amount != 0 && (!row.allocated_amount || paid_amount_change)) {
|
||||
if (row.outstanding_amount > 0 && allocated_positive_outstanding >= 0) {
|
||||
row.allocated_amount = (row.outstanding_amount >= allocated_positive_outstanding) ?
|
||||
allocated_positive_outstanding : row.outstanding_amount;
|
||||
allocated_positive_outstanding -= flt(row.allocated_amount);
|
||||
|
||||
} else if (row.outstanding_amount < 0 && allocated_negative_outstanding) {
|
||||
row.allocated_amount = (Math.abs(row.outstanding_amount) >= allocated_negative_outstanding) ?
|
||||
-1*allocated_negative_outstanding : row.outstanding_amount;
|
||||
allocated_negative_outstanding -= Math.abs(flt(row.allocated_amount));
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
frm.refresh_fields()
|
||||
frm.events.set_total_allocated_amount(frm);
|
||||
},
|
||||
|
||||
@@ -1367,62 +1409,6 @@ frappe.ui.form.on('Payment Entry', {
|
||||
|
||||
return current_tax_amount;
|
||||
},
|
||||
|
||||
cost_center: function (frm) {
|
||||
if (frm.doc.posting_date && (frm.doc.paid_from || frm.doc.paid_to)) {
|
||||
return frappe.call({
|
||||
method: "erpnext.accounts.doctype.payment_entry.payment_entry.get_party_and_account_balance",
|
||||
args: {
|
||||
company: frm.doc.company,
|
||||
date: frm.doc.posting_date,
|
||||
paid_from: frm.doc.paid_from,
|
||||
paid_to: frm.doc.paid_to,
|
||||
ptype: frm.doc.party_type,
|
||||
pty: frm.doc.party,
|
||||
cost_center: frm.doc.cost_center,
|
||||
},
|
||||
callback: function (r, rt) {
|
||||
if (r.message) {
|
||||
frappe.run_serially([
|
||||
() => {
|
||||
frm.set_value(
|
||||
"paid_from_account_balance",
|
||||
r.message.paid_from_account_balance
|
||||
);
|
||||
frm.set_value("paid_to_account_balance", r.message.paid_to_account_balance);
|
||||
frm.set_value("party_balance", r.message.party_balance);
|
||||
},
|
||||
]);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
after_save: function (frm) {
|
||||
const { matched_payment_requests } = frappe.last_response;
|
||||
if (!matched_payment_requests) return;
|
||||
|
||||
const COLUMN_LABEL = [
|
||||
[__("Reference DocType"), __("Reference Name"), __("Allocated Amount"), __("Payment Request")],
|
||||
];
|
||||
|
||||
frappe.msgprint({
|
||||
title: __("Unset Matched Payment Request"),
|
||||
message: COLUMN_LABEL.concat(matched_payment_requests),
|
||||
as_table: true,
|
||||
wide: true,
|
||||
primary_action: {
|
||||
label: __("Allocate Payment Request"),
|
||||
action() {
|
||||
frappe.hide_msgprint();
|
||||
frm.call("set_matched_payment_requests", { matched_payment_requests }, () => {
|
||||
frm.dirty();
|
||||
});
|
||||
},
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -1509,5 +1495,35 @@ frappe.ui.form.on('Payment Entry Deduction', {
|
||||
|
||||
deductions_remove: function(frm) {
|
||||
frm.events.set_unallocated_amount(frm);
|
||||
}
|
||||
})
|
||||
frappe.ui.form.on('Payment Entry', {
|
||||
cost_center: function(frm){
|
||||
if (frm.doc.posting_date && (frm.doc.paid_from||frm.doc.paid_to)) {
|
||||
return frappe.call({
|
||||
method: "erpnext.accounts.doctype.payment_entry.payment_entry.get_party_and_account_balance",
|
||||
args: {
|
||||
company: frm.doc.company,
|
||||
date: frm.doc.posting_date,
|
||||
paid_from: frm.doc.paid_from,
|
||||
paid_to: frm.doc.paid_to,
|
||||
ptype: frm.doc.party_type,
|
||||
pty: frm.doc.party,
|
||||
cost_center: frm.doc.cost_center
|
||||
},
|
||||
callback: function(r, rt) {
|
||||
if(r.message) {
|
||||
frappe.run_serially([
|
||||
() => {
|
||||
frm.set_value("paid_from_account_balance", r.message.paid_from_account_balance);
|
||||
frm.set_value("paid_to_account_balance", r.message.paid_to_account_balance);
|
||||
frm.set_value("party_balance", r.message.party_balance);
|
||||
}
|
||||
]);
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
@@ -7,11 +7,9 @@ from functools import reduce
|
||||
|
||||
import frappe
|
||||
from frappe import ValidationError, _, qb, scrub, throw
|
||||
from frappe.query_builder import Tuple
|
||||
from frappe.query_builder.functions import Count
|
||||
from frappe.utils import cint, comma_or, flt, getdate, nowdate
|
||||
from frappe.utils.data import comma_and, fmt_money, get_link_to_form
|
||||
from pypika.functions import Coalesce, Sum
|
||||
from frappe.utils.data import comma_and, fmt_money
|
||||
from pypika.functions import Sum
|
||||
|
||||
import erpnext
|
||||
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import get_dimensions
|
||||
@@ -93,17 +91,13 @@ class PaymentEntry(AccountsController):
|
||||
self.set_tax_withholding()
|
||||
self.set_status()
|
||||
|
||||
def before_save(self):
|
||||
self.set_matched_unset_payment_requests_to_response()
|
||||
|
||||
def on_submit(self):
|
||||
if self.difference_amount:
|
||||
frappe.throw(_("Difference Amount must be zero"))
|
||||
self.make_gl_entries()
|
||||
self.update_outstanding_amounts()
|
||||
self.update_advance_paid()
|
||||
self.update_payment_schedule()
|
||||
self.update_payment_requests()
|
||||
self.update_advance_paid() # advance_paid_status depends on the payment request amount
|
||||
self.set_status()
|
||||
|
||||
def on_cancel(self):
|
||||
@@ -121,34 +115,30 @@ class PaymentEntry(AccountsController):
|
||||
super().on_cancel()
|
||||
self.make_gl_entries(cancel=1)
|
||||
self.update_outstanding_amounts()
|
||||
self.update_advance_paid()
|
||||
self.delink_advance_entry_references()
|
||||
self.update_payment_schedule(cancel=1)
|
||||
self.update_payment_requests(cancel=True)
|
||||
self.update_advance_paid() # advance_paid_status depends on the payment request amount
|
||||
self.set_payment_req_status()
|
||||
self.set_status()
|
||||
|
||||
def update_payment_requests(self, cancel=False):
|
||||
from erpnext.accounts.doctype.payment_request.payment_request import (
|
||||
update_payment_requests_as_per_pe_references,
|
||||
)
|
||||
def set_payment_req_status(self):
|
||||
from erpnext.accounts.doctype.payment_request.payment_request import update_payment_req_status
|
||||
|
||||
update_payment_requests_as_per_pe_references(self.references, cancel=cancel)
|
||||
update_payment_req_status(self, None)
|
||||
|
||||
def update_outstanding_amounts(self):
|
||||
self.set_missing_ref_details(force=True)
|
||||
|
||||
def validate_duplicate_entry(self):
|
||||
reference_names = set()
|
||||
reference_names = []
|
||||
for d in self.get("references"):
|
||||
key = (d.reference_doctype, d.reference_name, d.payment_term, d.payment_request)
|
||||
if key in reference_names:
|
||||
if (d.reference_doctype, d.reference_name, d.payment_term) in reference_names:
|
||||
frappe.throw(
|
||||
_("Row #{0}: Duplicate entry in References {1} {2}").format(
|
||||
d.idx, d.reference_doctype, d.reference_name
|
||||
)
|
||||
)
|
||||
|
||||
reference_names.add(key)
|
||||
reference_names.append((d.reference_doctype, d.reference_name, d.payment_term))
|
||||
|
||||
def set_bank_account_data(self):
|
||||
if self.bank_account:
|
||||
@@ -174,8 +164,6 @@ class PaymentEntry(AccountsController):
|
||||
if self.payment_type == "Internal Transfer":
|
||||
return
|
||||
|
||||
self.validate_allocated_amount_as_per_payment_request()
|
||||
|
||||
if self.party_type in ("Customer", "Supplier"):
|
||||
self.validate_allocated_amount_with_latest_data()
|
||||
else:
|
||||
@@ -188,27 +176,6 @@ class PaymentEntry(AccountsController):
|
||||
if flt(d.allocated_amount) < 0 and flt(d.allocated_amount) < flt(d.outstanding_amount):
|
||||
frappe.throw(fail_message.format(d.idx))
|
||||
|
||||
def validate_allocated_amount_as_per_payment_request(self):
|
||||
"""
|
||||
Allocated amount should not be greater than the outstanding amount of the Payment Request.
|
||||
"""
|
||||
if not self.references:
|
||||
return
|
||||
|
||||
pr_outstanding_amounts = get_payment_request_outstanding_set_in_references(self.references)
|
||||
|
||||
if not pr_outstanding_amounts:
|
||||
return
|
||||
|
||||
for ref in self.references:
|
||||
if ref.payment_request and ref.allocated_amount > pr_outstanding_amounts[ref.payment_request]:
|
||||
frappe.throw(
|
||||
msg=_(
|
||||
"Row #{0}: Allocated Amount cannot be greater than Outstanding Amount of Payment Request {1}"
|
||||
).format(ref.idx, get_link_to_form("Payment Request", ref.payment_request)),
|
||||
title=_("Invalid Allocated Amount"),
|
||||
)
|
||||
|
||||
def term_based_allocation_enabled_for_reference(
|
||||
self, reference_doctype: str, reference_name: str
|
||||
) -> bool:
|
||||
@@ -1455,380 +1422,6 @@ class PaymentEntry(AccountsController):
|
||||
|
||||
return current_tax_fraction
|
||||
|
||||
def set_matched_unset_payment_requests_to_response(self):
|
||||
"""
|
||||
Find matched Payment Requests for those references which have no Payment Request set.\n
|
||||
And set to `frappe.response` to show in the frontend for allocation.
|
||||
"""
|
||||
if not self.references:
|
||||
return
|
||||
|
||||
matched_payment_requests = get_matched_payment_request_of_references(
|
||||
[row for row in self.references if not row.payment_request]
|
||||
)
|
||||
|
||||
if not matched_payment_requests:
|
||||
return
|
||||
|
||||
frappe.response["matched_payment_requests"] = matched_payment_requests
|
||||
|
||||
@frappe.whitelist()
|
||||
def allocate_amount_to_references(self, paid_amount, paid_amount_change, allocate_payment_amount):
|
||||
"""
|
||||
Allocate `Allocated Amount` and `Payment Request` against `Reference` based on `Paid Amount` and `Outstanding Amount`.\n
|
||||
:param paid_amount: Paid Amount / Received Amount.
|
||||
:param paid_amount_change: Flag to check if `Paid Amount` is changed or not.
|
||||
:param allocate_payment_amount: Flag to allocate amount or not. (Payment Request is also dependent on this flag)
|
||||
"""
|
||||
if not self.references:
|
||||
return
|
||||
|
||||
if not allocate_payment_amount:
|
||||
for ref in self.references:
|
||||
ref.allocated_amount = 0
|
||||
return
|
||||
|
||||
# calculating outstanding amounts
|
||||
precision = self.precision("paid_amount")
|
||||
total_positive_outstanding_including_order = 0
|
||||
total_negative_outstanding = 0
|
||||
paid_amount -= sum(flt(d.amount, precision) for d in self.deductions)
|
||||
|
||||
for ref in self.references:
|
||||
reference_outstanding_amount = ref.outstanding_amount
|
||||
abs_outstanding_amount = abs(reference_outstanding_amount)
|
||||
|
||||
if reference_outstanding_amount > 0:
|
||||
total_positive_outstanding_including_order += abs_outstanding_amount
|
||||
else:
|
||||
total_negative_outstanding += abs_outstanding_amount
|
||||
|
||||
# calculating allocated outstanding amounts
|
||||
allocated_negative_outstanding = 0
|
||||
allocated_positive_outstanding = 0
|
||||
|
||||
# checking party type and payment type
|
||||
if (self.payment_type == "Receive" and self.party_type == "Customer") or (
|
||||
self.payment_type == "Pay" and self.party_type in ("Supplier", "Employee")
|
||||
):
|
||||
if total_positive_outstanding_including_order > paid_amount:
|
||||
remaining_outstanding = flt(
|
||||
total_positive_outstanding_including_order - paid_amount, precision
|
||||
)
|
||||
allocated_negative_outstanding = min(remaining_outstanding, total_negative_outstanding)
|
||||
|
||||
allocated_positive_outstanding = paid_amount + allocated_negative_outstanding
|
||||
|
||||
elif self.party_type in ("Supplier", "Employee"):
|
||||
if paid_amount > total_negative_outstanding:
|
||||
if total_negative_outstanding == 0:
|
||||
frappe.msgprint(
|
||||
_("Cannot {0} from {2} without any negative outstanding invoice").format(
|
||||
self.payment_type,
|
||||
self.party_type,
|
||||
)
|
||||
)
|
||||
else:
|
||||
frappe.msgprint(
|
||||
_("Paid Amount cannot be greater than total negative outstanding amount {0}").format(
|
||||
total_negative_outstanding
|
||||
)
|
||||
)
|
||||
|
||||
return
|
||||
|
||||
else:
|
||||
allocated_positive_outstanding = flt(total_negative_outstanding - paid_amount, precision)
|
||||
allocated_negative_outstanding = paid_amount + min(
|
||||
total_positive_outstanding_including_order, allocated_positive_outstanding
|
||||
)
|
||||
|
||||
# inner function to set `allocated_amount` to those row which have no PR
|
||||
def _allocation_to_unset_pr_row(
|
||||
row, outstanding_amount, allocated_positive_outstanding, allocated_negative_outstanding
|
||||
):
|
||||
if outstanding_amount > 0 and allocated_positive_outstanding >= 0:
|
||||
row.allocated_amount = min(allocated_positive_outstanding, outstanding_amount)
|
||||
allocated_positive_outstanding = flt(
|
||||
allocated_positive_outstanding - row.allocated_amount, precision
|
||||
)
|
||||
elif outstanding_amount < 0 and allocated_negative_outstanding:
|
||||
row.allocated_amount = min(allocated_negative_outstanding, abs(outstanding_amount)) * -1
|
||||
allocated_negative_outstanding = flt(
|
||||
allocated_negative_outstanding - abs(row.allocated_amount), precision
|
||||
)
|
||||
return allocated_positive_outstanding, allocated_negative_outstanding
|
||||
|
||||
# allocate amount based on `paid_amount` is changed or not
|
||||
if not paid_amount_change:
|
||||
for ref in self.references:
|
||||
allocated_positive_outstanding, allocated_negative_outstanding = _allocation_to_unset_pr_row(
|
||||
ref,
|
||||
ref.outstanding_amount,
|
||||
allocated_positive_outstanding,
|
||||
allocated_negative_outstanding,
|
||||
)
|
||||
|
||||
allocate_open_payment_requests_to_references(self.references, self.precision("paid_amount"))
|
||||
|
||||
else:
|
||||
payment_request_outstanding_amounts = (
|
||||
get_payment_request_outstanding_set_in_references(self.references) or {}
|
||||
)
|
||||
references_outstanding_amounts = get_references_outstanding_amount(self.references) or {}
|
||||
remaining_references_allocated_amounts = references_outstanding_amounts.copy()
|
||||
|
||||
# Re allocate amount to those references which have PR set (Higher priority)
|
||||
for ref in self.references:
|
||||
if not ref.payment_request:
|
||||
continue
|
||||
|
||||
# fetch outstanding_amount of `Reference` (Payment Term) and `Payment Request` to allocate new amount
|
||||
key = (ref.reference_doctype, ref.reference_name, ref.get("payment_term"))
|
||||
reference_outstanding_amount = references_outstanding_amounts[key]
|
||||
pr_outstanding_amount = payment_request_outstanding_amounts[ref.payment_request]
|
||||
|
||||
if reference_outstanding_amount > 0 and allocated_positive_outstanding >= 0:
|
||||
# allocate amount according to outstanding amounts
|
||||
outstanding_amounts = (
|
||||
allocated_positive_outstanding,
|
||||
reference_outstanding_amount,
|
||||
pr_outstanding_amount,
|
||||
)
|
||||
|
||||
ref.allocated_amount = min(outstanding_amounts)
|
||||
|
||||
# update amounts to track allocation
|
||||
allocated_amount = ref.allocated_amount
|
||||
allocated_positive_outstanding = flt(
|
||||
allocated_positive_outstanding - allocated_amount, precision
|
||||
)
|
||||
remaining_references_allocated_amounts[key] = flt(
|
||||
remaining_references_allocated_amounts[key] - allocated_amount, precision
|
||||
)
|
||||
payment_request_outstanding_amounts[ref.payment_request] = flt(
|
||||
payment_request_outstanding_amounts[ref.payment_request] - allocated_amount, precision
|
||||
)
|
||||
|
||||
elif reference_outstanding_amount < 0 and allocated_negative_outstanding:
|
||||
# allocate amount according to outstanding amounts
|
||||
outstanding_amounts = (
|
||||
allocated_negative_outstanding,
|
||||
abs(reference_outstanding_amount),
|
||||
pr_outstanding_amount,
|
||||
)
|
||||
|
||||
ref.allocated_amount = min(outstanding_amounts) * -1
|
||||
|
||||
# update amounts to track allocation
|
||||
allocated_amount = abs(ref.allocated_amount)
|
||||
allocated_negative_outstanding = flt(
|
||||
allocated_negative_outstanding - allocated_amount, precision
|
||||
)
|
||||
remaining_references_allocated_amounts[key] += allocated_amount # negative amount
|
||||
payment_request_outstanding_amounts[ref.payment_request] = flt(
|
||||
payment_request_outstanding_amounts[ref.payment_request] - allocated_amount, precision
|
||||
)
|
||||
# Re allocate amount to those references which have no PR (Lower priority)
|
||||
for ref in self.references:
|
||||
if ref.payment_request:
|
||||
continue
|
||||
|
||||
key = (ref.reference_doctype, ref.reference_name, ref.get("payment_term"))
|
||||
reference_outstanding_amount = remaining_references_allocated_amounts[key]
|
||||
|
||||
allocated_positive_outstanding, allocated_negative_outstanding = _allocation_to_unset_pr_row(
|
||||
ref,
|
||||
reference_outstanding_amount,
|
||||
allocated_positive_outstanding,
|
||||
allocated_negative_outstanding,
|
||||
)
|
||||
|
||||
@frappe.whitelist()
|
||||
def set_matched_payment_requests(self, matched_payment_requests):
|
||||
"""
|
||||
Set `Payment Request` against `Reference` based on `matched_payment_requests`.\n
|
||||
:param matched_payment_requests: List of tuple of matched Payment Requests.
|
||||
|
||||
---
|
||||
Example: [(reference_doctype, reference_name, allocated_amount, payment_request), ...]
|
||||
"""
|
||||
if not self.references or not matched_payment_requests:
|
||||
return
|
||||
|
||||
if isinstance(matched_payment_requests, str):
|
||||
matched_payment_requests = json.loads(matched_payment_requests)
|
||||
|
||||
# modify matched_payment_requests
|
||||
# like (reference_doctype, reference_name, allocated_amount): payment_request
|
||||
payment_requests = {}
|
||||
|
||||
for row in matched_payment_requests:
|
||||
key = tuple(row[:3])
|
||||
payment_requests[key] = row[3]
|
||||
|
||||
for ref in self.references:
|
||||
if ref.payment_request:
|
||||
continue
|
||||
|
||||
key = (ref.reference_doctype, ref.reference_name, ref.allocated_amount)
|
||||
|
||||
if key in payment_requests:
|
||||
ref.payment_request = payment_requests[key]
|
||||
del payment_requests[key] # to avoid duplicate allocation
|
||||
|
||||
|
||||
def get_matched_payment_request_of_references(references=None):
|
||||
"""
|
||||
Get those `Payment Requests` which are matched with `References`.\n
|
||||
- Amount must be same.
|
||||
- Only single `Payment Request` available for this amount.
|
||||
|
||||
Example: [(reference_doctype, reference_name, allocated_amount, payment_request), ...]
|
||||
"""
|
||||
if not references:
|
||||
return
|
||||
|
||||
# to fetch matched rows
|
||||
refs = {
|
||||
(row.reference_doctype, row.reference_name, row.allocated_amount)
|
||||
for row in references
|
||||
if row.reference_doctype and row.reference_name and row.allocated_amount
|
||||
}
|
||||
|
||||
if not refs:
|
||||
return
|
||||
|
||||
PR = frappe.qb.DocType("Payment Request")
|
||||
|
||||
# query to group by reference_doctype, reference_name, outstanding_amount
|
||||
subquery = (
|
||||
frappe.qb.from_(PR)
|
||||
.select(
|
||||
PR.reference_doctype,
|
||||
PR.reference_name,
|
||||
PR.outstanding_amount.as_("allocated_amount"),
|
||||
PR.name.as_("payment_request"),
|
||||
Count("*").as_("count"),
|
||||
)
|
||||
.where(Tuple(PR.reference_doctype, PR.reference_name, PR.outstanding_amount).isin(refs))
|
||||
.where(PR.status != "Paid")
|
||||
.where(PR.docstatus == 1)
|
||||
.groupby(PR.reference_doctype, PR.reference_name, PR.outstanding_amount)
|
||||
)
|
||||
|
||||
# query to fetch matched rows which are single
|
||||
matched_prs = (
|
||||
frappe.qb.from_(subquery)
|
||||
.select(
|
||||
subquery.reference_doctype,
|
||||
subquery.reference_name,
|
||||
subquery.allocated_amount,
|
||||
subquery.payment_request,
|
||||
)
|
||||
.where(subquery.count == 1)
|
||||
.run()
|
||||
)
|
||||
|
||||
return matched_prs if matched_prs else None
|
||||
|
||||
|
||||
def get_references_outstanding_amount(references=None):
|
||||
"""
|
||||
Fetch accurate outstanding amount of `References`.\n
|
||||
- If `Payment Term` is set, then fetch outstanding amount from `Payment Schedule`.
|
||||
- If `Payment Term` is not set, then fetch outstanding amount from `References` it self.
|
||||
|
||||
Example: {(reference_doctype, reference_name, payment_term): outstanding_amount, ...}
|
||||
"""
|
||||
if not references:
|
||||
return
|
||||
|
||||
refs_with_payment_term = get_outstanding_of_references_with_payment_term(references) or {}
|
||||
refs_without_payment_term = get_outstanding_of_references_with_no_payment_term(references) or {}
|
||||
|
||||
return {**refs_with_payment_term, **refs_without_payment_term}
|
||||
|
||||
|
||||
def get_outstanding_of_references_with_payment_term(references=None):
|
||||
"""
|
||||
Fetch outstanding amount of `References` which have `Payment Term` set.\n
|
||||
Example: {(reference_doctype, reference_name, payment_term): outstanding_amount, ...}
|
||||
"""
|
||||
if not references:
|
||||
return
|
||||
|
||||
refs = {
|
||||
(row.reference_doctype, row.reference_name, row.payment_term)
|
||||
for row in references
|
||||
if row.reference_doctype and row.reference_name and row.payment_term
|
||||
}
|
||||
|
||||
if not refs:
|
||||
return
|
||||
|
||||
PS = frappe.qb.DocType("Payment Schedule")
|
||||
|
||||
response = (
|
||||
frappe.qb.from_(PS)
|
||||
.select(PS.parenttype, PS.parent, PS.payment_term, PS.outstanding)
|
||||
.where(Tuple(PS.parenttype, PS.parent, PS.payment_term).isin(refs))
|
||||
).run(as_dict=True)
|
||||
|
||||
if not response:
|
||||
return
|
||||
|
||||
return {(row.parenttype, row.parent, row.payment_term): row.outstanding for row in response}
|
||||
|
||||
|
||||
def get_outstanding_of_references_with_no_payment_term(references):
|
||||
"""
|
||||
Fetch outstanding amount of `References` which have no `Payment Term` set.\n
|
||||
- Fetch outstanding amount from `References` it self.
|
||||
|
||||
Note: `None` is used for allocation of `Payment Request`
|
||||
Example: {(reference_doctype, reference_name, None): outstanding_amount, ...}
|
||||
"""
|
||||
if not references:
|
||||
return
|
||||
|
||||
outstanding_amounts = {}
|
||||
|
||||
for ref in references:
|
||||
if ref.payment_term:
|
||||
continue
|
||||
|
||||
key = (ref.reference_doctype, ref.reference_name, None)
|
||||
|
||||
if key not in outstanding_amounts:
|
||||
outstanding_amounts[key] = ref.outstanding_amount
|
||||
|
||||
return outstanding_amounts
|
||||
|
||||
|
||||
def get_payment_request_outstanding_set_in_references(references=None):
|
||||
"""
|
||||
Fetch outstanding amount of `Payment Request` which are set in `References`.\n
|
||||
Example: {payment_request: outstanding_amount, ...}
|
||||
"""
|
||||
if not references:
|
||||
return
|
||||
|
||||
referenced_payment_requests = {row.payment_request for row in references if row.payment_request}
|
||||
|
||||
if not referenced_payment_requests:
|
||||
return
|
||||
|
||||
PR = frappe.qb.DocType("Payment Request")
|
||||
|
||||
response = (
|
||||
frappe.qb.from_(PR)
|
||||
.select(PR.name, PR.outstanding_amount)
|
||||
.where(PR.name.isin(referenced_payment_requests))
|
||||
).run()
|
||||
|
||||
return dict(response) if response else None
|
||||
|
||||
|
||||
def validate_inclusive_tax(tax, doc):
|
||||
def _on_previous_row_error(row_range):
|
||||
@@ -2417,8 +2010,6 @@ def get_payment_entry(
|
||||
party_type=None,
|
||||
payment_type=None,
|
||||
reference_date=None,
|
||||
ignore_permissions=False,
|
||||
created_from_payment_request=False,
|
||||
):
|
||||
doc = frappe.get_doc(dt, dn)
|
||||
over_billing_allowance = frappe.db.get_single_value("Accounts Settings", "over_billing_allowance")
|
||||
@@ -2569,179 +2160,9 @@ def get_payment_entry(
|
||||
|
||||
pe.set_difference_amount()
|
||||
|
||||
# If PE is created from PR directly, then no need to find open PRs for the references
|
||||
if not created_from_payment_request:
|
||||
allocate_open_payment_requests_to_references(pe.references, pe.precision("paid_amount"))
|
||||
|
||||
return pe
|
||||
|
||||
|
||||
def get_open_payment_requests_for_references(references=None):
|
||||
"""
|
||||
Fetch all unpaid Payment Requests for the references. \n
|
||||
- Each reference can have multiple Payment Requests. \n
|
||||
|
||||
Example: {("Sales Invoice", "SINV-00001"): {"PREQ-00001": 1000, "PREQ-00002": 2000}}
|
||||
"""
|
||||
if not references:
|
||||
return
|
||||
|
||||
refs = {
|
||||
(row.reference_doctype, row.reference_name)
|
||||
for row in references
|
||||
if row.reference_doctype and row.reference_name and row.allocated_amount
|
||||
}
|
||||
|
||||
if not refs:
|
||||
return
|
||||
|
||||
PR = frappe.qb.DocType("Payment Request")
|
||||
|
||||
response = (
|
||||
frappe.qb.from_(PR)
|
||||
.select(PR.name, PR.reference_doctype, PR.reference_name, PR.outstanding_amount)
|
||||
.where(Tuple(PR.reference_doctype, PR.reference_name).isin(list(refs)))
|
||||
.where(PR.status != "Paid")
|
||||
.where(PR.docstatus == 1)
|
||||
.orderby(Coalesce(PR.transaction_date, PR.creation), order=frappe.qb.asc)
|
||||
).run(as_dict=True)
|
||||
|
||||
if not response:
|
||||
return
|
||||
|
||||
reference_payment_requests = {}
|
||||
|
||||
for row in response:
|
||||
key = (row.reference_doctype, row.reference_name)
|
||||
|
||||
if key not in reference_payment_requests:
|
||||
reference_payment_requests[key] = {row.name: row.outstanding_amount}
|
||||
else:
|
||||
reference_payment_requests[key][row.name] = row.outstanding_amount
|
||||
|
||||
return reference_payment_requests
|
||||
|
||||
|
||||
def allocate_open_payment_requests_to_references(references=None, precision=None):
|
||||
"""
|
||||
Allocate unpaid Payment Requests to the references. \n
|
||||
---
|
||||
- Allocation based on below factors
|
||||
- Reference Allocated Amount
|
||||
- Reference Outstanding Amount (With Payment Terms or without Payment Terms)
|
||||
- Reference Payment Request's outstanding amount
|
||||
---
|
||||
- Allocation based on below scenarios
|
||||
- Reference's Allocated Amount == Payment Request's Outstanding Amount
|
||||
- Allocate the Payment Request to the reference
|
||||
- This PR will not be allocated further
|
||||
- Reference's Allocated Amount < Payment Request's Outstanding Amount
|
||||
- Allocate the Payment Request to the reference
|
||||
- Reduce the PR's outstanding amount by the allocated amount
|
||||
- This PR can be allocated further
|
||||
- Reference's Allocated Amount > Payment Request's Outstanding Amount
|
||||
- Allocate the Payment Request to the reference
|
||||
- Reduce Allocated Amount of the reference by the PR's outstanding amount
|
||||
- Create a new row for the remaining amount until the Allocated Amount is 0
|
||||
- Allocate PR if available
|
||||
---
|
||||
- Note:
|
||||
- Priority is given to the first Payment Request of respective references.
|
||||
- Single Reference can have multiple rows.
|
||||
- With Payment Terms or without Payment Terms
|
||||
- With Payment Request or without Payment Request
|
||||
"""
|
||||
if not references:
|
||||
return
|
||||
|
||||
# get all unpaid payment requests for the references
|
||||
references_open_payment_requests = get_open_payment_requests_for_references(references)
|
||||
|
||||
if not references_open_payment_requests:
|
||||
return
|
||||
|
||||
if not precision:
|
||||
precision = references[0].precision("allocated_amount")
|
||||
|
||||
# to manage new rows
|
||||
row_number = 1
|
||||
MOVE_TO_NEXT_ROW = 1
|
||||
TO_SKIP_NEW_ROW = 2
|
||||
|
||||
while row_number <= len(references):
|
||||
row = references[row_number - 1]
|
||||
reference_key = (row.reference_doctype, row.reference_name)
|
||||
|
||||
# update the idx to maintain the order
|
||||
row.idx = row_number
|
||||
|
||||
# unpaid payment requests for the reference
|
||||
reference_payment_requests = references_open_payment_requests.get(reference_key)
|
||||
|
||||
if not reference_payment_requests:
|
||||
row_number += MOVE_TO_NEXT_ROW # to move to next reference row
|
||||
continue
|
||||
|
||||
# get the first payment request and its outstanding amount
|
||||
payment_request, pr_outstanding_amount = next(iter(reference_payment_requests.items()))
|
||||
allocated_amount = row.allocated_amount
|
||||
|
||||
# allocate the payment request to the reference and PR's outstanding amount
|
||||
row.payment_request = payment_request
|
||||
|
||||
if pr_outstanding_amount == allocated_amount:
|
||||
del reference_payment_requests[payment_request]
|
||||
row_number += MOVE_TO_NEXT_ROW
|
||||
|
||||
elif pr_outstanding_amount > allocated_amount:
|
||||
# reduce the outstanding amount of the payment request
|
||||
reference_payment_requests[payment_request] -= allocated_amount
|
||||
row_number += MOVE_TO_NEXT_ROW
|
||||
|
||||
else:
|
||||
# split the reference row to allocate the remaining amount
|
||||
del reference_payment_requests[payment_request]
|
||||
row.allocated_amount = pr_outstanding_amount
|
||||
allocated_amount = flt(allocated_amount - pr_outstanding_amount, precision)
|
||||
|
||||
# set the remaining amount to the next row
|
||||
while allocated_amount:
|
||||
# create a new row for the remaining amount
|
||||
new_row = frappe.copy_doc(row)
|
||||
references.insert(row_number, new_row)
|
||||
|
||||
# get the first payment request and its outstanding amount
|
||||
payment_request, pr_outstanding_amount = next(
|
||||
iter(reference_payment_requests.items()), (None, None)
|
||||
)
|
||||
|
||||
# update new row
|
||||
new_row.idx = row_number + 1
|
||||
new_row.payment_request = payment_request
|
||||
new_row.allocated_amount = min(
|
||||
pr_outstanding_amount if pr_outstanding_amount else allocated_amount, allocated_amount
|
||||
)
|
||||
|
||||
if not payment_request or not pr_outstanding_amount:
|
||||
row_number += TO_SKIP_NEW_ROW
|
||||
break
|
||||
|
||||
elif pr_outstanding_amount == allocated_amount:
|
||||
del reference_payment_requests[payment_request]
|
||||
row_number += TO_SKIP_NEW_ROW
|
||||
break
|
||||
|
||||
elif pr_outstanding_amount > allocated_amount:
|
||||
reference_payment_requests[payment_request] -= allocated_amount
|
||||
row_number += TO_SKIP_NEW_ROW
|
||||
break
|
||||
|
||||
else:
|
||||
allocated_amount = flt(allocated_amount - pr_outstanding_amount, precision)
|
||||
del reference_payment_requests[payment_request]
|
||||
row_number += MOVE_TO_NEXT_ROW
|
||||
|
||||
|
||||
def update_accounting_dimensions(pe, doc):
|
||||
"""
|
||||
Updates accounting dimensions in Payment Entry based on the accounting dimensions in the reference document
|
||||
|
||||
@@ -10,15 +10,12 @@
|
||||
"due_date",
|
||||
"bill_no",
|
||||
"payment_term",
|
||||
"payment_term_outstanding",
|
||||
"column_break_4",
|
||||
"total_amount",
|
||||
"outstanding_amount",
|
||||
"allocated_amount",
|
||||
"exchange_rate",
|
||||
"exchange_gain_loss",
|
||||
"payment_request",
|
||||
"payment_request_outstanding"
|
||||
"exchange_gain_loss"
|
||||
],
|
||||
"fields": [
|
||||
{
|
||||
@@ -104,33 +101,12 @@
|
||||
"label": "Exchange Gain/Loss",
|
||||
"options": "Company:company:default_currency",
|
||||
"read_only": 1
|
||||
},
|
||||
{
|
||||
"fieldname": "payment_request",
|
||||
"fieldtype": "Link",
|
||||
"label": "Payment Request",
|
||||
"options": "Payment Request"
|
||||
},
|
||||
{
|
||||
"depends_on": "eval: doc.payment_term",
|
||||
"fieldname": "payment_term_outstanding",
|
||||
"fieldtype": "Float",
|
||||
"label": "Payment Term Outstanding",
|
||||
"read_only": 1
|
||||
},
|
||||
{
|
||||
"depends_on": "eval: doc.payment_request && doc.payment_request_outstanding",
|
||||
"fieldname": "payment_request_outstanding",
|
||||
"fieldtype": "Float",
|
||||
"is_virtual": 1,
|
||||
"label": "Payment Request Outstanding",
|
||||
"read_only": 1
|
||||
}
|
||||
],
|
||||
"index_web_pages_for_search": 1,
|
||||
"istable": 1,
|
||||
"links": [],
|
||||
"modified": "2024-09-16 18:11:50.019343",
|
||||
"modified": "2022-12-12 12:31:44.919895",
|
||||
"modified_by": "Administrator",
|
||||
"module": "Accounts",
|
||||
"name": "Payment Entry Reference",
|
||||
|
||||
@@ -1,14 +1,9 @@
|
||||
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and contributors
|
||||
# For license information, please see license.txt
|
||||
|
||||
import frappe
|
||||
|
||||
from frappe.model.document import Document
|
||||
|
||||
|
||||
class PaymentEntryReference(Document):
|
||||
@property
|
||||
def payment_request_outstanding(self):
|
||||
if not self.payment_request:
|
||||
return
|
||||
|
||||
return frappe.db.get_value("Payment Request", self.payment_request, "outstanding_amount")
|
||||
pass
|
||||
|
||||
@@ -48,8 +48,8 @@ frappe.ui.form.on("Payment Request", "refresh", function (frm) {
|
||||
}
|
||||
|
||||
if (
|
||||
frm.doc.payment_request_type == "Outward" &&
|
||||
["Initiated", "Partially Paid"].includes(frm.doc.status)
|
||||
(!frm.doc.payment_gateway_account || frm.doc.payment_request_type == "Outward") &&
|
||||
frm.doc.status == "Initiated"
|
||||
) {
|
||||
frm.add_custom_button(__("Create Payment Entry"), function () {
|
||||
frappe.call({
|
||||
|
||||
@@ -18,11 +18,9 @@
|
||||
"reference_name",
|
||||
"transaction_details",
|
||||
"grand_total",
|
||||
"currency",
|
||||
"is_a_subscription",
|
||||
"column_break_18",
|
||||
"outstanding_amount",
|
||||
"party_account_currency",
|
||||
"currency",
|
||||
"subscription_section",
|
||||
"subscription_plans",
|
||||
"bank_account_details",
|
||||
@@ -70,7 +68,6 @@
|
||||
{
|
||||
"fieldname": "transaction_date",
|
||||
"fieldtype": "Date",
|
||||
"in_preview": 1,
|
||||
"label": "Transaction Date"
|
||||
},
|
||||
{
|
||||
@@ -135,8 +132,7 @@
|
||||
"no_copy": 1,
|
||||
"options": "reference_doctype",
|
||||
"print_hide": 1,
|
||||
"read_only": 1,
|
||||
"search_index": 1
|
||||
"read_only": 1
|
||||
},
|
||||
{
|
||||
"fieldname": "transaction_details",
|
||||
@@ -144,14 +140,11 @@
|
||||
"label": "Transaction Details"
|
||||
},
|
||||
{
|
||||
"description": "Amount in transaction currency",
|
||||
"description": "Amount in customer's currency",
|
||||
"fieldname": "grand_total",
|
||||
"fieldtype": "Currency",
|
||||
"in_preview": 1,
|
||||
"label": "Amount",
|
||||
"non_negative": 1,
|
||||
"options": "currency",
|
||||
"reqd": 1
|
||||
"options": "currency"
|
||||
},
|
||||
{
|
||||
"default": "0",
|
||||
@@ -395,31 +388,13 @@
|
||||
"fieldtype": "Link",
|
||||
"label": "Project",
|
||||
"options": "Project"
|
||||
},
|
||||
{
|
||||
"depends_on": "eval: doc.docstatus === 1",
|
||||
"description": "Amount in party's bank account currency",
|
||||
"fieldname": "outstanding_amount",
|
||||
"fieldtype": "Currency",
|
||||
"in_preview": 1,
|
||||
"label": "Outstanding Amount",
|
||||
"non_negative": 1,
|
||||
"options": "party_account_currency",
|
||||
"read_only": 1
|
||||
},
|
||||
{
|
||||
"fieldname": "party_account_currency",
|
||||
"fieldtype": "Link",
|
||||
"label": "Party Account Currency",
|
||||
"options": "Currency",
|
||||
"read_only": 1
|
||||
}
|
||||
],
|
||||
"in_create": 1,
|
||||
"index_web_pages_for_search": 1,
|
||||
"is_submittable": 1,
|
||||
"links": [],
|
||||
"modified": "2024-09-16 17:50:54.440090",
|
||||
"modified": "2022-12-21 16:56:40.115737",
|
||||
"modified_by": "Administrator",
|
||||
"module": "Accounts",
|
||||
"name": "Payment Request",
|
||||
@@ -454,8 +429,7 @@
|
||||
"write": 1
|
||||
}
|
||||
],
|
||||
"show_preview_popup": 1,
|
||||
"sort_field": "modified",
|
||||
"sort_order": "DESC",
|
||||
"states": []
|
||||
}
|
||||
}
|
||||
@@ -7,11 +7,9 @@ import json
|
||||
import frappe
|
||||
from frappe import _
|
||||
from frappe.model.document import Document
|
||||
from frappe.query_builder.functions import Sum
|
||||
from frappe.utils import flt, get_url, nowdate
|
||||
from frappe.utils.background_jobs import enqueue
|
||||
|
||||
from erpnext import get_company_currency
|
||||
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
|
||||
get_accounting_dimensions,
|
||||
)
|
||||
@@ -47,12 +45,6 @@ class PaymentRequest(Document):
|
||||
frappe.throw(_("To create a Payment Request reference document is required"))
|
||||
|
||||
def validate_payment_request_amount(self):
|
||||
if self.grand_total == 0:
|
||||
frappe.throw(
|
||||
_("{0} cannot be zero").format(self.get_label_from_fieldname("grand_total")),
|
||||
title=_("Invalid Amount"),
|
||||
)
|
||||
|
||||
existing_payment_request_amount = flt(
|
||||
get_existing_payment_request_amount(self.reference_doctype, self.reference_name)
|
||||
)
|
||||
@@ -100,29 +92,6 @@ class PaymentRequest(Document):
|
||||
).format(self.grand_total, amount)
|
||||
)
|
||||
|
||||
def before_submit(self):
|
||||
company = frappe.get_value(self.reference_doctype, self.reference_name, "company")
|
||||
if (
|
||||
self.currency != self.party_account_currency
|
||||
and self.party_account_currency == get_company_currency(company)
|
||||
):
|
||||
# set outstanding amount in party account currency
|
||||
invoice = frappe.get_value(
|
||||
self.reference_doctype,
|
||||
self.reference_name,
|
||||
["rounded_total", "grand_total", "base_rounded_total", "base_grand_total"],
|
||||
as_dict=1,
|
||||
)
|
||||
grand_total = invoice.get("rounded_total") or invoice.get("grand_total")
|
||||
base_grand_total = invoice.get("base_rounded_total") or invoice.get("base_grand_total")
|
||||
self.outstanding_amount = flt(
|
||||
self.grand_total / grand_total * base_grand_total,
|
||||
self.precision("outstanding_amount"),
|
||||
)
|
||||
|
||||
else:
|
||||
self.outstanding_amount = self.grand_total
|
||||
|
||||
def on_submit(self):
|
||||
if self.payment_request_type == "Outward":
|
||||
self.db_set("status", "Initiated")
|
||||
@@ -251,7 +220,7 @@ class PaymentRequest(Document):
|
||||
|
||||
def set_as_paid(self):
|
||||
if self.payment_channel == "Phone":
|
||||
self.db_set({"status": "Paid", "outstanding_amount": 0})
|
||||
self.db_set("status", "Paid")
|
||||
|
||||
else:
|
||||
payment_entry = self.create_payment_entry()
|
||||
@@ -272,32 +241,26 @@ class PaymentRequest(Document):
|
||||
else:
|
||||
party_account = get_party_account("Customer", ref_doc.get("customer"), ref_doc.company)
|
||||
|
||||
party_account_currency = (
|
||||
self.get("party_account_currency")
|
||||
or ref_doc.get("party_account_currency")
|
||||
or get_account_currency(party_account)
|
||||
)
|
||||
|
||||
party_amount = bank_amount = self.outstanding_amount
|
||||
party_account_currency = ref_doc.get("party_account_currency") or get_account_currency(party_account)
|
||||
|
||||
bank_amount = self.grand_total
|
||||
if party_account_currency == ref_doc.company_currency and party_account_currency != self.currency:
|
||||
exchange_rate = ref_doc.get("conversion_rate")
|
||||
bank_amount = flt(self.outstanding_amount / exchange_rate, self.precision("grand_total"))
|
||||
party_amount = ref_doc.get("base_rounded_total") or ref_doc.get("base_grand_total")
|
||||
else:
|
||||
party_amount = self.grand_total
|
||||
|
||||
# outstanding amount is already in Part's account currency
|
||||
payment_entry = get_payment_entry(
|
||||
self.reference_doctype,
|
||||
self.reference_name,
|
||||
party_amount=party_amount,
|
||||
bank_account=self.payment_account,
|
||||
bank_amount=bank_amount,
|
||||
created_from_payment_request=True,
|
||||
)
|
||||
|
||||
payment_entry.update(
|
||||
{
|
||||
"mode_of_payment": self.mode_of_payment,
|
||||
"reference_no": self.name, # to prevent validation error
|
||||
"reference_no": self.name,
|
||||
"reference_date": nowdate(),
|
||||
"remarks": "Payment Entry against {} {} via Payment Request {}".format(
|
||||
self.reference_doctype, self.reference_name, self.name
|
||||
@@ -305,9 +268,6 @@ class PaymentRequest(Document):
|
||||
}
|
||||
)
|
||||
|
||||
# Allocate payment_request for each reference in payment_entry (Payment Term can splits the row)
|
||||
self._allocate_payment_request_to_pe_references(references=payment_entry.references)
|
||||
|
||||
# Update dimensions
|
||||
payment_entry.update(
|
||||
{
|
||||
@@ -316,6 +276,14 @@ class PaymentRequest(Document):
|
||||
}
|
||||
)
|
||||
|
||||
if party_account_currency == ref_doc.company_currency and party_account_currency != self.currency:
|
||||
amount = payment_entry.base_paid_amount
|
||||
else:
|
||||
amount = self.grand_total
|
||||
|
||||
payment_entry.received_amount = amount
|
||||
payment_entry.get("references")[0].allocated_amount = amount
|
||||
|
||||
# Update 'Paid Amount' on Forex transactions
|
||||
if self.currency != ref_doc.company_currency:
|
||||
if (
|
||||
@@ -429,62 +397,6 @@ class PaymentRequest(Document):
|
||||
if payment_provider == "stripe":
|
||||
return create_stripe_subscription(gateway_controller, data)
|
||||
|
||||
def _allocate_payment_request_to_pe_references(self, references):
|
||||
"""
|
||||
Allocate the Payment Request to the Payment Entry references based on\n
|
||||
- Allocated Amount.
|
||||
- Outstanding Amount of Payment Request.\n
|
||||
Payment Request is doc itself and references are the rows of Payment Entry.
|
||||
"""
|
||||
if len(references) == 1:
|
||||
references[0].payment_request = self.name
|
||||
return
|
||||
|
||||
precision = references[0].precision("allocated_amount")
|
||||
outstanding_amount = self.outstanding_amount
|
||||
|
||||
# to manage rows
|
||||
row_number = 1
|
||||
MOVE_TO_NEXT_ROW = 1
|
||||
TO_SKIP_NEW_ROW = 2
|
||||
NEW_ROW_ADDED = False
|
||||
|
||||
while row_number <= len(references):
|
||||
row = references[row_number - 1]
|
||||
|
||||
# update the idx to maintain the order
|
||||
row.idx = row_number
|
||||
|
||||
if outstanding_amount == 0:
|
||||
if not NEW_ROW_ADDED:
|
||||
break
|
||||
|
||||
row_number += MOVE_TO_NEXT_ROW
|
||||
continue
|
||||
|
||||
# allocate the payment request to the row
|
||||
row.payment_request = self.name
|
||||
|
||||
if row.allocated_amount <= outstanding_amount:
|
||||
outstanding_amount = flt(outstanding_amount - row.allocated_amount, precision)
|
||||
row_number += MOVE_TO_NEXT_ROW
|
||||
else:
|
||||
remaining_allocated_amount = flt(row.allocated_amount - outstanding_amount, precision)
|
||||
row.allocated_amount = outstanding_amount
|
||||
outstanding_amount = 0
|
||||
|
||||
# create a new row without PR for remaining unallocated amount
|
||||
new_row = frappe.copy_doc(row)
|
||||
references.insert(row_number, new_row)
|
||||
|
||||
# update new row
|
||||
new_row.idx = row_number + 1
|
||||
new_row.payment_request = None
|
||||
new_row.allocated_amount = remaining_allocated_amount
|
||||
|
||||
NEW_ROW_ADDED = True
|
||||
row_number += TO_SKIP_NEW_ROW
|
||||
|
||||
|
||||
@frappe.whitelist(allow_guest=True)
|
||||
def make_payment_request(**args):
|
||||
@@ -515,15 +427,11 @@ def make_payment_request(**args):
|
||||
{"reference_doctype": args.dt, "reference_name": args.dn, "docstatus": 0},
|
||||
)
|
||||
|
||||
# fetches existing payment request `grand_total` amount
|
||||
existing_payment_request_amount = get_existing_payment_request_amount(ref_doc.doctype, ref_doc.name)
|
||||
existing_payment_request_amount = get_existing_payment_request_amount(args.dt, args.dn)
|
||||
|
||||
if existing_payment_request_amount:
|
||||
grand_total -= existing_payment_request_amount
|
||||
|
||||
if not grand_total:
|
||||
frappe.throw(_("Payment Request is already created"))
|
||||
|
||||
if draft_payment_request:
|
||||
frappe.db.set_value(
|
||||
"Payment Request", draft_payment_request, "grand_total", grand_total, update_modified=False
|
||||
@@ -537,13 +445,6 @@ def make_payment_request(**args):
|
||||
"Outward" if args.get("dt") in ["Purchase Order", "Purchase Invoice"] else "Inward"
|
||||
)
|
||||
|
||||
party_type = args.get("party_type") or "Customer"
|
||||
party_account_currency = ref_doc.party_account_currency
|
||||
|
||||
if not party_account_currency:
|
||||
party_account = get_party_account(party_type, ref_doc.get(party_type.lower()), ref_doc.company)
|
||||
party_account_currency = get_account_currency(party_account)
|
||||
|
||||
pr.update(
|
||||
{
|
||||
"payment_gateway_account": gateway_account.get("name"),
|
||||
@@ -552,7 +453,6 @@ def make_payment_request(**args):
|
||||
"payment_channel": gateway_account.get("payment_channel"),
|
||||
"payment_request_type": args.get("payment_request_type"),
|
||||
"currency": ref_doc.currency,
|
||||
"party_account_currency": party_account_currency,
|
||||
"grand_total": grand_total,
|
||||
"mode_of_payment": args.mode_of_payment,
|
||||
"email_to": args.recipient_id or ref_doc.owner,
|
||||
@@ -560,7 +460,7 @@ def make_payment_request(**args):
|
||||
"message": gateway_account.get("message") or get_dummy_message(ref_doc),
|
||||
"reference_doctype": args.dt,
|
||||
"reference_name": args.dn,
|
||||
"party_type": party_type,
|
||||
"party_type": args.get("party_type") or "Customer",
|
||||
"party": args.get("party") or ref_doc.get("customer"),
|
||||
"bank_account": bank_account,
|
||||
}
|
||||
@@ -603,11 +503,9 @@ def get_amount(ref_doc, payment_account=None):
|
||||
elif dt in ["Sales Invoice", "Purchase Invoice"]:
|
||||
if not ref_doc.get("is_pos"):
|
||||
if ref_doc.party_account_currency == ref_doc.currency:
|
||||
grand_total = flt(ref_doc.rounded_total or ref_doc.grand_total)
|
||||
grand_total = flt(ref_doc.grand_total)
|
||||
else:
|
||||
grand_total = flt(
|
||||
flt(ref_doc.base_rounded_total or ref_doc.base_grand_total) / ref_doc.conversion_rate
|
||||
)
|
||||
grand_total = flt(ref_doc.base_grand_total) / ref_doc.conversion_rate
|
||||
elif dt == "Sales Invoice":
|
||||
for pay in ref_doc.payments:
|
||||
if pay.type == "Phone" and pay.account == payment_account:
|
||||
@@ -629,20 +527,24 @@ def get_amount(ref_doc, payment_account=None):
|
||||
|
||||
def get_existing_payment_request_amount(ref_dt, ref_dn):
|
||||
"""
|
||||
Return the total amount of Payment Requests against a reference document.
|
||||
Get the existing payment request which are unpaid or partially paid for payment channel other than Phone
|
||||
and get the summation of existing paid payment request for Phone payment channel.
|
||||
"""
|
||||
PR = frappe.qb.DocType("Payment Request")
|
||||
|
||||
response = (
|
||||
frappe.qb.from_(PR)
|
||||
.select(Sum(PR.grand_total))
|
||||
.where(PR.reference_doctype == ref_dt)
|
||||
.where(PR.reference_name == ref_dn)
|
||||
.where(PR.docstatus == 1)
|
||||
.run()
|
||||
existing_payment_request_amount = frappe.db.sql(
|
||||
"""
|
||||
select sum(grand_total)
|
||||
from `tabPayment Request`
|
||||
where
|
||||
reference_doctype = %s
|
||||
and reference_name = %s
|
||||
and docstatus = 1
|
||||
and (status != 'Paid'
|
||||
or (payment_channel = 'Phone'
|
||||
and status = 'Paid'))
|
||||
""",
|
||||
(ref_dt, ref_dn),
|
||||
)
|
||||
|
||||
return response[0][0] if response[0] else 0
|
||||
return flt(existing_payment_request_amount[0][0]) if existing_payment_request_amount else 0
|
||||
|
||||
|
||||
def get_gateway_details(args): # nosemgrep
|
||||
@@ -690,67 +592,42 @@ def make_payment_entry(docname):
|
||||
return doc.create_payment_entry(submit=False).as_dict()
|
||||
|
||||
|
||||
def update_payment_requests_as_per_pe_references(references=None, cancel=False):
|
||||
"""
|
||||
Update Payment Request's `Status` and `Outstanding Amount` based on Payment Entry Reference's `Allocated Amount`.
|
||||
"""
|
||||
if not references:
|
||||
return
|
||||
def update_payment_req_status(doc, method):
|
||||
from erpnext.accounts.doctype.payment_entry.payment_entry import get_reference_details
|
||||
|
||||
precision = references[0].precision("allocated_amount")
|
||||
|
||||
referenced_payment_requests = frappe.get_all(
|
||||
"Payment Request",
|
||||
filters={"name": ["in", {row.payment_request for row in references if row.payment_request}]},
|
||||
fields=[
|
||||
"name",
|
||||
"grand_total",
|
||||
"outstanding_amount",
|
||||
"payment_request_type",
|
||||
],
|
||||
)
|
||||
|
||||
referenced_payment_requests = {pr.name: pr for pr in referenced_payment_requests}
|
||||
|
||||
for ref in references:
|
||||
if not ref.payment_request:
|
||||
continue
|
||||
|
||||
payment_request = referenced_payment_requests[ref.payment_request]
|
||||
pr_outstanding = payment_request["outstanding_amount"]
|
||||
|
||||
# update outstanding amount
|
||||
new_outstanding_amount = flt(
|
||||
pr_outstanding + ref.allocated_amount if cancel else pr_outstanding - ref.allocated_amount,
|
||||
precision,
|
||||
)
|
||||
|
||||
# to handle same payment request for the multiple allocations
|
||||
payment_request["outstanding_amount"] = new_outstanding_amount
|
||||
|
||||
if not cancel and new_outstanding_amount < 0:
|
||||
frappe.throw(
|
||||
msg=_(
|
||||
"The allocated amount is greater than the outstanding amount of Payment Request {0}"
|
||||
).format(ref.payment_request),
|
||||
title=_("Invalid Allocated Amount"),
|
||||
)
|
||||
|
||||
# update status
|
||||
if new_outstanding_amount == payment_request["grand_total"]:
|
||||
status = "Initiated" if payment_request["payment_request_type"] == "Outward" else "Requested"
|
||||
elif new_outstanding_amount == 0:
|
||||
status = "Paid"
|
||||
elif new_outstanding_amount > 0:
|
||||
status = "Partially Paid"
|
||||
|
||||
# update database
|
||||
frappe.db.set_value(
|
||||
for ref in doc.references:
|
||||
payment_request_name = frappe.db.get_value(
|
||||
"Payment Request",
|
||||
ref.payment_request,
|
||||
{"outstanding_amount": new_outstanding_amount, "status": status},
|
||||
{
|
||||
"reference_doctype": ref.reference_doctype,
|
||||
"reference_name": ref.reference_name,
|
||||
"docstatus": 1,
|
||||
},
|
||||
)
|
||||
|
||||
if payment_request_name:
|
||||
ref_details = get_reference_details(
|
||||
ref.reference_doctype,
|
||||
ref.reference_name,
|
||||
doc.party_account_currency,
|
||||
doc.party_type,
|
||||
doc.party,
|
||||
)
|
||||
pay_req_doc = frappe.get_doc("Payment Request", payment_request_name)
|
||||
status = pay_req_doc.status
|
||||
|
||||
if status != "Paid" and not ref_details.outstanding_amount:
|
||||
status = "Paid"
|
||||
elif status != "Partially Paid" and ref_details.outstanding_amount != ref_details.total_amount:
|
||||
status = "Partially Paid"
|
||||
elif ref_details.outstanding_amount == ref_details.total_amount:
|
||||
if pay_req_doc.payment_request_type == "Outward":
|
||||
status = "Initiated"
|
||||
elif pay_req_doc.payment_request_type == "Inward":
|
||||
status = "Requested"
|
||||
|
||||
pay_req_doc.db_set("status", status)
|
||||
|
||||
|
||||
def get_dummy_message(doc):
|
||||
return frappe.render_template(
|
||||
@@ -833,35 +710,3 @@ def validate_payment(doc, method=None):
|
||||
doc.reference_docname
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@frappe.whitelist()
|
||||
def get_open_payment_requests_query(doctype, txt, searchfield, start, page_len, filters):
|
||||
# permission checks in `get_list()`
|
||||
reference_doctype = filters.get("reference_doctype")
|
||||
reference_name = filters.get("reference_doctype")
|
||||
|
||||
if not reference_doctype or not reference_name:
|
||||
return []
|
||||
|
||||
open_payment_requests = frappe.get_list(
|
||||
"Payment Request",
|
||||
filters={
|
||||
"reference_doctype": filters["reference_doctype"],
|
||||
"reference_name": filters["reference_name"],
|
||||
"status": ["!=", "Paid"],
|
||||
"outstanding_amount": ["!=", 0], # for compatibility with old data
|
||||
"docstatus": 1,
|
||||
},
|
||||
fields=["name", "grand_total", "outstanding_amount"],
|
||||
order_by="transaction_date ASC,creation ASC",
|
||||
)
|
||||
|
||||
return [
|
||||
(
|
||||
pr.name,
|
||||
_("<strong>Grand Total:</strong> {0}").format(pr.grand_total),
|
||||
_("<strong>Outstanding Amount:</strong> {0}").format(pr.outstanding_amount),
|
||||
)
|
||||
for pr in open_payment_requests
|
||||
]
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
||||
# See license.txt
|
||||
|
||||
import re
|
||||
import unittest
|
||||
|
||||
import frappe
|
||||
from frappe.tests.utils import FrappeTestCase, change_settings
|
||||
from frappe.tests.utils import FrappeTestCase
|
||||
|
||||
from erpnext.accounts.doctype.payment_entry.test_payment_entry import create_payment_terms_template
|
||||
from erpnext.accounts.doctype.payment_request.payment_request import make_payment_request
|
||||
from erpnext.accounts.doctype.purchase_invoice.test_purchase_invoice import make_purchase_invoice
|
||||
from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sales_invoice
|
||||
@@ -17,7 +15,6 @@ from erpnext.setup.utils import get_exchange_rate
|
||||
|
||||
test_dependencies = ["Currency Exchange", "Journal Entry", "Contact", "Address"]
|
||||
|
||||
|
||||
payment_gateway = {"doctype": "Payment Gateway", "gateway": "_Test Gateway"}
|
||||
|
||||
payment_method = [
|
||||
@@ -281,246 +278,3 @@ class TestPaymentRequest(FrappeTestCase):
|
||||
self.assertEqual(pe.paid_amount, 800)
|
||||
self.assertEqual(pe.base_received_amount, 800)
|
||||
self.assertEqual(pe.received_amount, 10)
|
||||
|
||||
def test_multiple_payment_if_partially_paid_for_same_currency(self):
|
||||
so = make_sales_order(currency="INR", qty=1, rate=1000)
|
||||
|
||||
pr = make_payment_request(
|
||||
dt="Sales Order",
|
||||
dn=so.name,
|
||||
mute_email=1,
|
||||
submit_doc=1,
|
||||
return_doc=1,
|
||||
)
|
||||
|
||||
self.assertEqual(pr.grand_total, 1000)
|
||||
self.assertEqual(pr.outstanding_amount, pr.grand_total)
|
||||
self.assertEqual(pr.party_account_currency, pr.currency) # INR
|
||||
|
||||
so.load_from_db()
|
||||
|
||||
# to make partial payment
|
||||
pe = pr.create_payment_entry(submit=False)
|
||||
pe.paid_amount = 200
|
||||
pe.references[0].allocated_amount = 200
|
||||
pe.submit()
|
||||
|
||||
self.assertEqual(pe.references[0].payment_request, pr.name)
|
||||
|
||||
so.load_from_db()
|
||||
|
||||
pr.load_from_db()
|
||||
self.assertEqual(pr.status, "Partially Paid")
|
||||
self.assertEqual(pr.outstanding_amount, 800)
|
||||
self.assertEqual(pr.grand_total, 1000)
|
||||
|
||||
# complete payment
|
||||
pe = pr.create_payment_entry()
|
||||
|
||||
self.assertEqual(pe.paid_amount, 800) # paid amount set from pr's outstanding amount
|
||||
self.assertEqual(pe.references[0].allocated_amount, 800)
|
||||
self.assertEqual(pe.references[0].outstanding_amount, 800) # for Orders it is not zero
|
||||
self.assertEqual(pe.references[0].payment_request, pr.name)
|
||||
|
||||
so.load_from_db()
|
||||
|
||||
pr.load_from_db()
|
||||
self.assertEqual(pr.status, "Paid")
|
||||
self.assertEqual(pr.outstanding_amount, 0)
|
||||
self.assertEqual(pr.grand_total, 1000)
|
||||
|
||||
# creating a more payment Request must not allowed
|
||||
self.assertRaisesRegex(
|
||||
frappe.exceptions.ValidationError,
|
||||
re.compile(r"Payment Request is already created"),
|
||||
make_payment_request,
|
||||
dt="Sales Order",
|
||||
dn=so.name,
|
||||
mute_email=1,
|
||||
submit_doc=1,
|
||||
return_doc=1,
|
||||
)
|
||||
|
||||
@change_settings("Accounts Settings", {"allow_multi_currency_invoices_against_single_party_account": 1})
|
||||
def test_multiple_payment_if_partially_paid_for_multi_currency(self):
|
||||
pi = make_purchase_invoice(currency="USD", conversion_rate=50, qty=1, rate=100, do_not_save=1)
|
||||
pi.credit_to = "Creditors - _TC"
|
||||
pi.submit()
|
||||
|
||||
pr = make_payment_request(
|
||||
dt="Purchase Invoice",
|
||||
dn=pi.name,
|
||||
mute_email=1,
|
||||
submit_doc=1,
|
||||
return_doc=1,
|
||||
)
|
||||
|
||||
# 100 USD -> 5000 INR
|
||||
self.assertEqual(pr.grand_total, 100)
|
||||
self.assertEqual(pr.outstanding_amount, 5000)
|
||||
self.assertEqual(pr.currency, "USD")
|
||||
self.assertEqual(pr.party_account_currency, "INR")
|
||||
self.assertEqual(pr.status, "Initiated")
|
||||
|
||||
# to make partial payment
|
||||
pe = pr.create_payment_entry(submit=False)
|
||||
pe.paid_amount = 2000
|
||||
pe.references[0].allocated_amount = 2000
|
||||
pe.submit()
|
||||
|
||||
self.assertEqual(pe.references[0].payment_request, pr.name)
|
||||
|
||||
pr.load_from_db()
|
||||
self.assertEqual(pr.status, "Partially Paid")
|
||||
self.assertEqual(pr.outstanding_amount, 3000)
|
||||
self.assertEqual(pr.grand_total, 100)
|
||||
|
||||
# complete payment
|
||||
pe = pr.create_payment_entry()
|
||||
self.assertEqual(pe.paid_amount, 3000) # paid amount set from pr's outstanding amount
|
||||
self.assertEqual(pe.references[0].allocated_amount, 3000)
|
||||
self.assertEqual(pe.references[0].outstanding_amount, 0) # for Invoices it will zero
|
||||
self.assertEqual(pe.references[0].payment_request, pr.name)
|
||||
|
||||
pr.load_from_db()
|
||||
self.assertEqual(pr.status, "Paid")
|
||||
self.assertEqual(pr.outstanding_amount, 0)
|
||||
self.assertEqual(pr.grand_total, 100)
|
||||
|
||||
# creating a more payment Request must not allowed
|
||||
self.assertRaisesRegex(
|
||||
frappe.exceptions.ValidationError,
|
||||
re.compile(r"Payment Request is already created"),
|
||||
make_payment_request,
|
||||
dt="Purchase Invoice",
|
||||
dn=pi.name,
|
||||
mute_email=1,
|
||||
submit_doc=1,
|
||||
return_doc=1,
|
||||
)
|
||||
|
||||
def test_single_payment_with_payment_term_for_same_currency(self):
|
||||
create_payment_terms_template()
|
||||
|
||||
po = create_purchase_order(do_not_save=1, currency="INR", qty=1, rate=20000)
|
||||
po.payment_terms_template = "Test Receivable Template" # 84.746 and 15.254
|
||||
po.save()
|
||||
po.submit()
|
||||
|
||||
pr = make_payment_request(
|
||||
dt="Purchase Order",
|
||||
dn=po.name,
|
||||
mute_email=1,
|
||||
submit_doc=1,
|
||||
return_doc=1,
|
||||
)
|
||||
|
||||
self.assertEqual(pr.grand_total, 20000)
|
||||
self.assertEqual(pr.outstanding_amount, pr.grand_total)
|
||||
self.assertEqual(pr.party_account_currency, pr.currency) # INR
|
||||
self.assertEqual(pr.status, "Initiated")
|
||||
|
||||
po.load_from_db()
|
||||
|
||||
pe = pr.create_payment_entry()
|
||||
|
||||
self.assertEqual(len(pe.references), 2)
|
||||
self.assertEqual(pe.paid_amount, 20000)
|
||||
|
||||
# check 1st payment term
|
||||
self.assertEqual(pe.references[0].allocated_amount, 16949.2)
|
||||
self.assertEqual(pe.references[0].payment_request, pr.name)
|
||||
|
||||
# check 2nd payment term
|
||||
self.assertEqual(pe.references[1].allocated_amount, 3050.8)
|
||||
self.assertEqual(pe.references[1].payment_request, pr.name)
|
||||
|
||||
po.load_from_db()
|
||||
|
||||
pr.load_from_db()
|
||||
self.assertEqual(pr.status, "Paid")
|
||||
self.assertEqual(pr.outstanding_amount, 0)
|
||||
self.assertEqual(pr.grand_total, 20000)
|
||||
|
||||
@change_settings("Accounts Settings", {"allow_multi_currency_invoices_against_single_party_account": 1})
|
||||
def test_single_payment_with_payment_term_for_multi_currency(self):
|
||||
create_payment_terms_template()
|
||||
|
||||
si = create_sales_invoice(
|
||||
do_not_save=1, currency="USD", debit_to="Debtors - _TC", qty=1, rate=200, conversion_rate=50
|
||||
)
|
||||
si.payment_terms_template = "Test Receivable Template" # 84.746 and 15.254
|
||||
si.save()
|
||||
si.submit()
|
||||
|
||||
pr = make_payment_request(
|
||||
dt="Sales Invoice",
|
||||
dn=si.name,
|
||||
mute_email=1,
|
||||
submit_doc=1,
|
||||
return_doc=1,
|
||||
)
|
||||
|
||||
# 200 USD -> 10000 INR
|
||||
self.assertEqual(pr.grand_total, 200)
|
||||
self.assertEqual(pr.outstanding_amount, 10000)
|
||||
self.assertEqual(pr.currency, "USD")
|
||||
self.assertEqual(pr.party_account_currency, "INR")
|
||||
|
||||
pe = pr.create_payment_entry()
|
||||
self.assertEqual(len(pe.references), 2)
|
||||
self.assertEqual(pe.paid_amount, 10000)
|
||||
|
||||
# check 1st payment term
|
||||
# convert it via dollar and conversion_rate
|
||||
self.assertEqual(pe.references[0].allocated_amount, 8474.5) # multi currency conversion
|
||||
self.assertEqual(pe.references[0].payment_request, pr.name)
|
||||
|
||||
# check 2nd payment term
|
||||
self.assertEqual(pe.references[1].allocated_amount, 1525.5) # multi currency conversion
|
||||
self.assertEqual(pe.references[1].payment_request, pr.name)
|
||||
|
||||
pr.load_from_db()
|
||||
self.assertEqual(pr.status, "Paid")
|
||||
self.assertEqual(pr.outstanding_amount, 0)
|
||||
self.assertEqual(pr.grand_total, 200)
|
||||
|
||||
def test_payment_cancel_process(self):
|
||||
so = make_sales_order(currency="INR", qty=1, rate=1000)
|
||||
|
||||
pr = make_payment_request(
|
||||
dt="Sales Order",
|
||||
dn=so.name,
|
||||
mute_email=1,
|
||||
submit_doc=1,
|
||||
return_doc=1,
|
||||
)
|
||||
|
||||
self.assertEqual(pr.grand_total, 1000)
|
||||
self.assertEqual(pr.outstanding_amount, pr.grand_total)
|
||||
|
||||
so.load_from_db()
|
||||
|
||||
pe = pr.create_payment_entry(submit=False)
|
||||
pe.paid_amount = 800
|
||||
pe.references[0].allocated_amount = 800
|
||||
pe.submit()
|
||||
|
||||
self.assertEqual(pe.references[0].payment_request, pr.name)
|
||||
|
||||
so.load_from_db()
|
||||
|
||||
pr.load_from_db()
|
||||
self.assertEqual(pr.status, "Partially Paid")
|
||||
self.assertEqual(pr.outstanding_amount, 200)
|
||||
self.assertEqual(pr.grand_total, 1000)
|
||||
|
||||
# cancelling PE
|
||||
pe.cancel()
|
||||
|
||||
pr.load_from_db()
|
||||
self.assertEqual(pr.status, "Requested")
|
||||
self.assertEqual(pr.outstanding_amount, 1000)
|
||||
self.assertEqual(pr.grand_total, 1000)
|
||||
|
||||
so.load_from_db()
|
||||
|
||||
@@ -362,7 +362,8 @@ def process_closing_entries(gl_entries, closing_entries, voucher_name, company,
|
||||
)
|
||||
|
||||
try:
|
||||
make_closing_entries(gl_entries + closing_entries, voucher_name, company, closing_date)
|
||||
if gl_entries + closing_entries:
|
||||
make_closing_entries(gl_entries + closing_entries, voucher_name, company, closing_date)
|
||||
except Exception as e:
|
||||
frappe.db.rollback()
|
||||
frappe.log_error(e)
|
||||
|
||||
@@ -295,7 +295,7 @@ def get_tax_amount(party_type, parties, inv, tax_details, posting_date, pan_no=N
|
||||
tax_amount = 0
|
||||
else:
|
||||
# if no TCS has been charged in FY,
|
||||
# then chargeable value is "prev invoices + advances - advance_adjusted" value which cross the threshold
|
||||
# then chargeable value is "prev invoices + advances" value which cross the threshold
|
||||
tax_amount = get_tcs_amount(parties, inv, tax_details, vouchers, advance_vouchers)
|
||||
|
||||
if cint(tax_details.round_off_tax_amount):
|
||||
@@ -382,9 +382,6 @@ def get_advance_vouchers(parties, company=None, from_date=None, to_date=None, pa
|
||||
Use Payment Ledger to fetch unallocated Advance Payments
|
||||
"""
|
||||
|
||||
if party_type == "Supplier":
|
||||
return []
|
||||
|
||||
ple = qb.DocType("Payment Ledger Entry")
|
||||
|
||||
conditions = []
|
||||
@@ -602,12 +599,9 @@ def get_tcs_amount(parties, inv, tax_details, vouchers, adv_vouchers):
|
||||
)
|
||||
|
||||
cumulative_threshold = tax_details.get("cumulative_threshold", 0)
|
||||
advance_adjusted = get_advance_adjusted_in_invoice(inv)
|
||||
|
||||
current_invoice_total = get_invoice_total_without_tcs(inv, tax_details)
|
||||
total_invoiced_amt = (
|
||||
current_invoice_total + invoiced_amt + advance_amt - credit_note_amt - advance_adjusted
|
||||
)
|
||||
total_invoiced_amt = current_invoice_total + invoiced_amt + advance_amt - credit_note_amt
|
||||
|
||||
if cumulative_threshold and total_invoiced_amt >= cumulative_threshold:
|
||||
chargeable_amt = total_invoiced_amt - cumulative_threshold
|
||||
@@ -616,14 +610,6 @@ def get_tcs_amount(parties, inv, tax_details, vouchers, adv_vouchers):
|
||||
return tcs_amount
|
||||
|
||||
|
||||
def get_advance_adjusted_in_invoice(inv):
|
||||
advances_adjusted = 0
|
||||
for row in inv.get("advances", []):
|
||||
advances_adjusted += row.allocated_amount
|
||||
|
||||
return advances_adjusted
|
||||
|
||||
|
||||
def get_invoice_total_without_tcs(inv, tax_details):
|
||||
tcs_tax_row = [d for d in inv.taxes if d.account_head == tax_details.account_head]
|
||||
tcs_tax_row_amount = tcs_tax_row[0].base_tax_amount if tcs_tax_row else 0
|
||||
|
||||
@@ -214,50 +214,6 @@ class TestTaxWithholdingCategory(FrappeTestCase):
|
||||
d.reload()
|
||||
d.cancel()
|
||||
|
||||
@change_settings(
|
||||
"Accounts Settings",
|
||||
{"unlink_payment_on_cancellation_of_invoice": 1},
|
||||
)
|
||||
def test_tcs_on_allocated_advance_payments(self):
|
||||
frappe.db.set_value(
|
||||
"Customer", "Test TCS Customer", "tax_withholding_category", "Cumulative Threshold TCS"
|
||||
)
|
||||
|
||||
vouchers = []
|
||||
|
||||
# create advance payment
|
||||
pe = create_payment_entry(
|
||||
payment_type="Receive", party_type="Customer", party="Test TCS Customer", paid_amount=30000
|
||||
)
|
||||
pe.paid_from = "Debtors - _TC"
|
||||
pe.paid_to = "Cash - _TC"
|
||||
pe.submit()
|
||||
vouchers.append(pe)
|
||||
|
||||
si = create_sales_invoice(customer="Test TCS Customer", rate=50000)
|
||||
advances = si.get_advance_entries()
|
||||
si.append(
|
||||
"advances",
|
||||
{
|
||||
"reference_type": advances[0].reference_type,
|
||||
"reference_name": advances[0].reference_name,
|
||||
"advance_amount": advances[0].amount,
|
||||
"allocated_amount": 30000,
|
||||
},
|
||||
)
|
||||
si.submit()
|
||||
vouchers.append(si)
|
||||
|
||||
# assert tax collection on total invoice ,advance payment adjusted should be excluded.
|
||||
tcs_charged = sum([d.base_tax_amount for d in si.taxes if d.account_head == "TCS - _TC"])
|
||||
# tcs = (inv amt)50000+(adv amt)30000-(adv adj) 30000 - threshold(30000) * rate 10%
|
||||
self.assertEqual(tcs_charged, 2000)
|
||||
|
||||
# cancel invoice and payments to avoid clashing
|
||||
for d in reversed(vouchers):
|
||||
d.reload()
|
||||
d.cancel()
|
||||
|
||||
def test_tds_calculation_on_net_total(self):
|
||||
frappe.db.set_value(
|
||||
"Supplier", "Test TDS Supplier4", "tax_withholding_category", "Cumulative Threshold TDS"
|
||||
|
||||
@@ -311,7 +311,6 @@ def get_account_columns(invoice_list, include_payments):
|
||||
"""select distinct expense_account
|
||||
from `tabPurchase Invoice Item` where docstatus = 1
|
||||
and (expense_account is not null and expense_account != '')
|
||||
and parenttype='Purchase Invoice'
|
||||
and parent in (%s) order by expense_account"""
|
||||
% ", ".join(["%s"] * len(invoice_list)),
|
||||
tuple([inv.name for inv in invoice_list]),
|
||||
@@ -444,7 +443,7 @@ def get_invoice_expense_map(invoice_list):
|
||||
"""
|
||||
select parent, expense_account, sum(base_net_amount) as amount
|
||||
from `tabPurchase Invoice Item`
|
||||
where parent in (%s) and parenttype='Purchase Invoice'
|
||||
where parent in (%s)
|
||||
group by parent, expense_account
|
||||
"""
|
||||
% ", ".join(["%s"] * len(invoice_list)),
|
||||
@@ -515,7 +514,7 @@ def get_invoice_po_pr_map(invoice_list):
|
||||
"""
|
||||
select parent, purchase_order, purchase_receipt, po_detail, project
|
||||
from `tabPurchase Invoice Item`
|
||||
where parent in (%s) and parenttype='Purchase Invoice'
|
||||
where parent in (%s)
|
||||
"""
|
||||
% ", ".join(["%s"] * len(invoice_list)),
|
||||
tuple(inv.name for inv in invoice_list),
|
||||
|
||||
@@ -326,7 +326,6 @@ def apply_common_conditions(filters, query, doctype, child_doctype=None, payment
|
||||
|
||||
if join_required:
|
||||
query = query.inner_join(child_doc).on(parent_doc.name == child_doc.parent)
|
||||
query = query.where(child_doc.parenttype == doctype)
|
||||
query = query.distinct()
|
||||
|
||||
if parent_doc.get_table_name() != "tabJournal Entry":
|
||||
|
||||
@@ -1844,7 +1844,7 @@ class AccountsController(TransactionBase):
|
||||
).format(formatted_advance_paid, self.name, formatted_order_total)
|
||||
)
|
||||
|
||||
self.db_set("advance_paid", advance_paid)
|
||||
frappe.db.set_value(self.doctype, self.name, "advance_paid", advance_paid)
|
||||
|
||||
@property
|
||||
def company_abbr(self):
|
||||
@@ -3124,6 +3124,7 @@ def update_child_qty_rate(parent_doctype, trans_items, parent_doctype_name, chil
|
||||
items_added_or_removed = False # updated to true if any new item is added or removed
|
||||
any_conversion_factor_changed = False
|
||||
|
||||
sales_doctypes = ["Sales Order", "Sales Invoice", "Delivery Note", "Quotation"]
|
||||
parent = frappe.get_doc(parent_doctype, parent_doctype_name)
|
||||
|
||||
check_doc_permissions(parent, "write")
|
||||
@@ -3225,21 +3226,25 @@ def update_child_qty_rate(parent_doctype, trans_items, parent_doctype_name, chil
|
||||
# if rate is greater than price_list_rate, set margin
|
||||
# or set discount
|
||||
child_item.discount_percentage = 0
|
||||
child_item.margin_type = "Amount"
|
||||
child_item.margin_rate_or_amount = flt(
|
||||
child_item.rate - child_item.price_list_rate,
|
||||
child_item.precision("margin_rate_or_amount"),
|
||||
)
|
||||
child_item.rate_with_margin = child_item.rate
|
||||
|
||||
if parent_doctype in sales_doctypes:
|
||||
child_item.margin_type = "Amount"
|
||||
child_item.margin_rate_or_amount = flt(
|
||||
child_item.rate - child_item.price_list_rate,
|
||||
child_item.precision("margin_rate_or_amount"),
|
||||
)
|
||||
child_item.rate_with_margin = child_item.rate
|
||||
else:
|
||||
child_item.discount_percentage = flt(
|
||||
(1 - flt(child_item.rate) / flt(child_item.price_list_rate)) * 100.0,
|
||||
child_item.precision("discount_percentage"),
|
||||
)
|
||||
child_item.discount_amount = flt(child_item.price_list_rate) - flt(child_item.rate)
|
||||
child_item.margin_type = ""
|
||||
child_item.margin_rate_or_amount = 0
|
||||
child_item.rate_with_margin = 0
|
||||
|
||||
if parent_doctype in sales_doctypes:
|
||||
child_item.margin_type = ""
|
||||
child_item.margin_rate_or_amount = 0
|
||||
child_item.rate_with_margin = 0
|
||||
|
||||
child_item.flags.ignore_validate_update_after_submit = True
|
||||
if new_child_flag:
|
||||
|
||||
@@ -221,7 +221,7 @@ erpnext.ProductSearch = class {
|
||||
let thumbnail = res.thumbnail || "/assets/erpnext/images/ui-states/cart-empty-state.png";
|
||||
html += `
|
||||
<div class="dropdown-item" style="display: flex;">
|
||||
<img class="item-thumb col-2" src=${thumbnail} />
|
||||
<img class="item-thumb col-2" src=${encodeURI(thumbnail)} />
|
||||
<div class="col-9" style="white-space: normal;">
|
||||
<a href="/${res.route}">${res.web_item_name}</a><br>
|
||||
<span class="brand-line">${res.brand ? "by " + res.brand : ""}</span>
|
||||
|
||||
@@ -7,7 +7,9 @@ import frappe
|
||||
from frappe import _
|
||||
from frappe.utils.redis_wrapper import RedisWrapper
|
||||
from redis import ResponseError
|
||||
from redisearch import AutoCompleter, Client, IndexDefinition, Suggestion, TagField, TextField
|
||||
from redis.commands.search.field import TagField, TextField
|
||||
from redis.commands.search.indexDefinition import IndexDefinition
|
||||
from redis.commands.search.suggestion import Suggestion
|
||||
|
||||
WEBSITE_ITEM_INDEX = "website_items_index"
|
||||
WEBSITE_ITEM_KEY_PREFIX = "website_item:"
|
||||
@@ -35,12 +37,9 @@ def is_redisearch_enabled():
|
||||
def is_search_module_loaded():
|
||||
try:
|
||||
cache = frappe.cache()
|
||||
out = cache.execute_command("MODULE LIST")
|
||||
|
||||
parsed_output = " ".join(
|
||||
" ".join([frappe.as_unicode(s) for s in o if not isinstance(s, int)]) for o in out
|
||||
)
|
||||
return "search" in parsed_output
|
||||
for module in cache.module_list():
|
||||
if module.get(b"name") == b"search":
|
||||
return True
|
||||
except Exception:
|
||||
return False # handling older redis versions
|
||||
|
||||
@@ -58,18 +57,18 @@ def if_redisearch_enabled(function):
|
||||
|
||||
|
||||
def make_key(key):
|
||||
return f"{frappe.conf.db_name}|{key}".encode()
|
||||
return frappe.cache().make_key(key)
|
||||
|
||||
|
||||
@if_redisearch_enabled
|
||||
def create_website_items_index():
|
||||
"Creates Index Definition."
|
||||
|
||||
# CREATE index
|
||||
client = Client(make_key(WEBSITE_ITEM_INDEX), conn=frappe.cache())
|
||||
redis = frappe.cache()
|
||||
index = redis.ft(WEBSITE_ITEM_INDEX)
|
||||
|
||||
try:
|
||||
client.drop_index() # drop if already exists
|
||||
index.dropindex() # drop if already exists
|
||||
except ResponseError:
|
||||
# will most likely raise a ResponseError if index does not exist
|
||||
# ignore and create index
|
||||
@@ -86,9 +85,9 @@ def create_website_items_index():
|
||||
if "web_item_name" in idx_fields:
|
||||
idx_fields.remove("web_item_name")
|
||||
|
||||
idx_fields = list(map(to_search_field, idx_fields))
|
||||
idx_fields = [to_search_field(f) for f in idx_fields]
|
||||
|
||||
client.create_index(
|
||||
index.create_index(
|
||||
[TextField("web_item_name", sortable=True), *idx_fields],
|
||||
definition=idx_def,
|
||||
)
|
||||
@@ -119,8 +118,8 @@ def insert_item_to_index(website_item_doc):
|
||||
|
||||
@if_redisearch_enabled
|
||||
def insert_to_name_ac(web_name, doc_name):
|
||||
ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=frappe.cache())
|
||||
ac.add_suggestions(Suggestion(web_name, payload=doc_name))
|
||||
ac = frappe.cache().ft()
|
||||
ac.sugadd(WEBSITE_ITEM_NAME_AUTOCOMPLETE, Suggestion(web_name, payload=doc_name))
|
||||
|
||||
|
||||
def create_web_item_map(website_item_doc):
|
||||
@@ -157,9 +156,8 @@ def delete_item_from_index(website_item_doc):
|
||||
@if_redisearch_enabled
|
||||
def delete_from_ac_dict(website_item_doc):
|
||||
"""Removes this items's name from autocomplete dictionary"""
|
||||
cache = frappe.cache()
|
||||
name_ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=cache)
|
||||
name_ac.delete(website_item_doc.web_item_name)
|
||||
ac = frappe.cache().ft()
|
||||
ac.sugdel(website_item_doc.web_item_name)
|
||||
|
||||
|
||||
@if_redisearch_enabled
|
||||
@@ -170,8 +168,6 @@ def define_autocomplete_dictionary():
|
||||
"""
|
||||
|
||||
cache = frappe.cache()
|
||||
item_ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=cache)
|
||||
item_group_ac = AutoCompleter(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE), conn=cache)
|
||||
|
||||
# Delete both autocomplete dicts
|
||||
try:
|
||||
@@ -180,36 +176,40 @@ def define_autocomplete_dictionary():
|
||||
except Exception:
|
||||
raise_redisearch_error()
|
||||
|
||||
create_items_autocomplete_dict(autocompleter=item_ac)
|
||||
create_item_groups_autocomplete_dict(autocompleter=item_group_ac)
|
||||
create_items_autocomplete_dict()
|
||||
create_item_groups_autocomplete_dict()
|
||||
|
||||
|
||||
@if_redisearch_enabled
|
||||
def create_items_autocomplete_dict(autocompleter):
|
||||
def create_items_autocomplete_dict():
|
||||
"Add items as suggestions in Autocompleter."
|
||||
ac = frappe.cache().ft()
|
||||
items = frappe.get_all("Website Item", fields=["web_item_name", "item_group"], filters={"published": 1})
|
||||
|
||||
for item in items:
|
||||
autocompleter.add_suggestions(Suggestion(item.web_item_name))
|
||||
ac.sugadd(WEBSITE_ITEM_NAME_AUTOCOMPLETE, Suggestion(item.web_item_name))
|
||||
|
||||
|
||||
@if_redisearch_enabled
|
||||
def create_item_groups_autocomplete_dict(autocompleter):
|
||||
def create_item_groups_autocomplete_dict():
|
||||
"Add item groups with weightage as suggestions in Autocompleter."
|
||||
|
||||
published_item_groups = frappe.get_all(
|
||||
"Item Group", fields=["name", "route", "weightage"], filters={"show_in_website": 1}
|
||||
)
|
||||
if not published_item_groups:
|
||||
return
|
||||
|
||||
ac = frappe.cache().ft()
|
||||
|
||||
for item_group in published_item_groups:
|
||||
payload = json.dumps({"name": item_group.name, "route": item_group.route})
|
||||
autocompleter.add_suggestions(
|
||||
ac.sugadd(
|
||||
WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE,
|
||||
Suggestion(
|
||||
string=item_group.name,
|
||||
score=frappe.utils.flt(item_group.weightage) or 1.0,
|
||||
payload=payload, # additional info that can be retrieved later
|
||||
)
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -354,6 +354,7 @@ doc_events = {
|
||||
"Payment Entry": {
|
||||
"on_submit": [
|
||||
"erpnext.regional.create_transaction_log",
|
||||
"erpnext.accounts.doctype.payment_request.payment_request.update_payment_req_status",
|
||||
"erpnext.accounts.doctype.dunning.dunning.resolve_dunning",
|
||||
],
|
||||
"on_trash": "erpnext.regional.check_deletion_permission",
|
||||
|
||||
@@ -366,4 +366,3 @@ execute:frappe.db.set_single_value('E Commerce Settings', 'show_actual_qty', 1)
|
||||
erpnext.patches.v14_0.delete_orphaned_asset_movement_item_records
|
||||
erpnext.patches.v14_0.remove_cancelled_asset_capitalization_from_asset
|
||||
erpnext.patches.v14_0.enable_set_priority_for_pricing_rules #1
|
||||
erpnext.patches.v14_0.update_currency_exchange_settings_for_frankfurter
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
import frappe
|
||||
|
||||
|
||||
def execute():
|
||||
settings = frappe.get_doc("Currency Exchange Settings")
|
||||
if settings.service_provider != "frankfurter.app":
|
||||
return
|
||||
|
||||
settings.set_parameters_and_result()
|
||||
settings.flags.ignore_validate = True
|
||||
settings.save()
|
||||
@@ -68,9 +68,9 @@ def patched_requests_get(*args, **kwargs):
|
||||
if kwargs["params"].get("date") and kwargs["params"].get("from") and kwargs["params"].get("to"):
|
||||
if test_exchange_values.get(kwargs["params"]["date"]):
|
||||
return PatchResponse({"result": test_exchange_values[kwargs["params"]["date"]]}, 200)
|
||||
elif args[0].startswith("https://api.frankfurter.app") and kwargs.get("params"):
|
||||
elif args[0].startswith("https://frankfurter.app") and kwargs.get("params"):
|
||||
if kwargs["params"].get("base") and kwargs["params"].get("symbols"):
|
||||
date = args[0].replace("https://api.frankfurter.app/", "")
|
||||
date = args[0].replace("https://frankfurter.app/", "")
|
||||
if test_exchange_values.get(date):
|
||||
return PatchResponse(
|
||||
{"rates": {kwargs["params"].get("symbols"): test_exchange_values.get(date)}}, 200
|
||||
|
||||
@@ -80,7 +80,7 @@ def setup_currency_exchange():
|
||||
ces.set("result_key", [])
|
||||
ces.set("req_params", [])
|
||||
|
||||
ces.api_endpoint = "https://api.frankfurter.app/{transaction_date}"
|
||||
ces.api_endpoint = "https://frankfurter.app/{transaction_date}"
|
||||
ces.append("result_key", {"key": "rates"})
|
||||
ces.append("result_key", {"key": "{to_currency}"})
|
||||
ces.append("req_params", {"key": "base", "value": "{from_currency}"})
|
||||
|
||||
@@ -5,14 +5,13 @@ import json
|
||||
|
||||
import frappe
|
||||
from frappe.utils import cint, cstr
|
||||
from redisearch import AutoCompleter, Client, Query
|
||||
from redis.commands.search.query import Query
|
||||
|
||||
from erpnext.e_commerce.redisearch_utils import (
|
||||
WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE,
|
||||
WEBSITE_ITEM_INDEX,
|
||||
WEBSITE_ITEM_NAME_AUTOCOMPLETE,
|
||||
is_redisearch_enabled,
|
||||
make_key,
|
||||
)
|
||||
from erpnext.e_commerce.shopping_cart.product_info import set_product_info_for_website
|
||||
from erpnext.setup.doctype.item_group.item_group import get_item_for_list_in_html
|
||||
@@ -85,17 +84,17 @@ def product_search(query, limit=10, fuzzy_search=True):
|
||||
if not query:
|
||||
return search_results
|
||||
|
||||
red = frappe.cache()
|
||||
redis = frappe.cache()
|
||||
query = clean_up_query(query)
|
||||
|
||||
# TODO: Check perf/correctness with Suggestions & Query vs only Query
|
||||
# TODO: Use Levenshtein Distance in Query (max=3)
|
||||
ac = AutoCompleter(make_key(WEBSITE_ITEM_NAME_AUTOCOMPLETE), conn=red)
|
||||
client = Client(make_key(WEBSITE_ITEM_INDEX), conn=red)
|
||||
suggestions = ac.get_suggestions(
|
||||
redisearch = redis.ft(WEBSITE_ITEM_INDEX)
|
||||
suggestions = redisearch.sugget(
|
||||
WEBSITE_ITEM_NAME_AUTOCOMPLETE,
|
||||
query,
|
||||
num=limit,
|
||||
fuzzy=fuzzy_search and len(query) > 3, # Fuzzy on length < 3 can be real slow
|
||||
fuzzy=fuzzy_search and len(query) > 3,
|
||||
)
|
||||
|
||||
# Build a query
|
||||
@@ -105,8 +104,8 @@ def product_search(query, limit=10, fuzzy_search=True):
|
||||
query_string += f"|('{clean_up_query(s.string)}')"
|
||||
|
||||
q = Query(query_string)
|
||||
results = redisearch.search(q)
|
||||
|
||||
results = client.search(q)
|
||||
search_results["results"] = list(map(convert_to_dict, results.docs))
|
||||
search_results["results"] = sorted(
|
||||
search_results["results"], key=lambda k: frappe.utils.cint(k["ranking"]), reverse=True
|
||||
@@ -140,8 +139,8 @@ def get_category_suggestions(query):
|
||||
if not query:
|
||||
return search_results
|
||||
|
||||
ac = AutoCompleter(make_key(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE), conn=frappe.cache())
|
||||
suggestions = ac.get_suggestions(query, num=10, with_payloads=True)
|
||||
ac = frappe.cache().ft()
|
||||
suggestions = ac.sugget(WEBSITE_ITEM_CATEGORY_AUTOCOMPLETE, query, num=10, with_payloads=True)
|
||||
|
||||
results = [json.loads(s.payload) for s in suggestions]
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@ dependencies = [
|
||||
"pycountry~=20.7.3",
|
||||
"python-stdnum~=1.16",
|
||||
"Unidecode~=1.2.0",
|
||||
"redisearch~=2.1.0",
|
||||
"rapidfuzz~=2.15.0",
|
||||
"holidays~=0.28",
|
||||
|
||||
|
||||
Reference in New Issue
Block a user