Fixed merge conflict
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
from __future__ import unicode_literals
|
||||
import frappe, erpnext
|
||||
from frappe import _, throw
|
||||
from frappe.utils import today, flt, cint, fmt_money, formatdate, getdate
|
||||
from frappe.utils import today, flt, cint, fmt_money, formatdate, getdate, add_days, add_months, get_last_day
|
||||
from erpnext.setup.utils import get_exchange_rate
|
||||
from erpnext.accounts.utils import get_fiscal_years, validate_fiscal_year, get_account_currency
|
||||
from erpnext.utilities.transaction_base import TransactionBase
|
||||
@@ -26,11 +26,19 @@ class AccountsController(TransactionBase):
|
||||
return self.__company_currency
|
||||
|
||||
def onload(self):
|
||||
self.get("__onload").make_payment_via_journal_entry = frappe.db.get_single_value('Accounts Settings', 'make_payment_via_journal_entry')
|
||||
self.get("__onload").make_payment_via_journal_entry \
|
||||
= frappe.db.get_single_value('Accounts Settings', 'make_payment_via_journal_entry')
|
||||
|
||||
if self.is_new():
|
||||
relevant_docs = ("Quotation", "Purchase Order", "Sales Order",
|
||||
"Purchase Invoice", "Sales Invoice")
|
||||
if self.doctype in relevant_docs:
|
||||
self.set_payment_schedule()
|
||||
|
||||
def validate(self):
|
||||
if self.get("_action") and self._action != "update_after_submit":
|
||||
self.set_missing_values(for_validate=True)
|
||||
|
||||
self.validate_date_with_fiscal_year()
|
||||
|
||||
if self.meta.get_field("currency"):
|
||||
@@ -42,9 +50,7 @@ class AccountsController(TransactionBase):
|
||||
validate_return(self)
|
||||
self.set_total_in_words()
|
||||
|
||||
if self.doctype in ("Sales Invoice", "Purchase Invoice") and not self.is_return:
|
||||
self.validate_due_date()
|
||||
self.validate_advance_entries()
|
||||
self.validate_all_documents_schedule()
|
||||
|
||||
if self.meta.get_field("taxes_and_charges"):
|
||||
self.validate_enabled_taxes_and_charges()
|
||||
@@ -55,6 +61,26 @@ class AccountsController(TransactionBase):
|
||||
if self.doctype == 'Purchase Invoice':
|
||||
self.validate_paid_amount()
|
||||
|
||||
def validate_invoice_documents_schedule(self):
|
||||
self.validate_payment_schedule_dates()
|
||||
self.set_due_date()
|
||||
self.validate_invoice_portion()
|
||||
self.set_payment_schedule()
|
||||
self.validate_payment_schedule_amount()
|
||||
self.validate_due_date()
|
||||
self.validate_advance_entries()
|
||||
|
||||
def validate_non_invoice_documents_schedule(self):
|
||||
self.validate_invoice_portion()
|
||||
self.set_payment_schedule()
|
||||
self.validate_payment_schedule_amount()
|
||||
|
||||
def validate_all_documents_schedule(self):
|
||||
if self.doctype in ("Sales Invoice", "Purchase Invoice") and not self.is_return:
|
||||
self.validate_invoice_documents_schedule()
|
||||
elif self.doctype in ("Quotation", "Purchase Order", "Sales Order"):
|
||||
self.validate_non_invoice_documents_schedule()
|
||||
|
||||
def before_print(self):
|
||||
if self.doctype in ['Purchase Order', 'Sales Order']:
|
||||
if self.get("group_same_items"):
|
||||
@@ -74,11 +100,11 @@ class AccountsController(TransactionBase):
|
||||
self.paid_amount = 0
|
||||
frappe.throw(_("Note: Payment Entry will not be created since 'Cash or Bank Account' was not specified"))
|
||||
else:
|
||||
frappe.db.set(self,'paid_amount',0)
|
||||
frappe.db.set(self, 'paid_amount', 0)
|
||||
|
||||
def set_missing_values(self, for_validate=False):
|
||||
if frappe.flags.in_test:
|
||||
for fieldname in ["posting_date","transaction_date"]:
|
||||
for fieldname in ["posting_date", "transaction_date"]:
|
||||
if self.meta.get_field(fieldname) and not self.get(fieldname):
|
||||
self.set(fieldname, today())
|
||||
break
|
||||
@@ -109,9 +135,9 @@ class AccountsController(TransactionBase):
|
||||
if not self.due_date:
|
||||
frappe.throw(_("Due Date is mandatory"))
|
||||
|
||||
validate_due_date(self.posting_date, self.due_date, "Customer", self.customer, self.company)
|
||||
validate_due_date(self.posting_date, self.due_date, "Customer", self.customer)
|
||||
elif self.doctype == "Purchase Invoice":
|
||||
validate_due_date(self.posting_date, self.due_date, "Supplier", self.supplier, self.company)
|
||||
validate_due_date(self.posting_date, self.due_date, "Supplier", self.supplier)
|
||||
|
||||
def set_price_list_currency(self, buying_or_selling):
|
||||
if self.meta.get_field("posting_date"):
|
||||
@@ -619,6 +645,67 @@ class AccountsController(TransactionBase):
|
||||
for item in duplicate_list:
|
||||
self.remove(item)
|
||||
|
||||
def set_payment_schedule(self):
|
||||
posting_date = self.get("posting_date") or self.get("transaction_date")
|
||||
date = self.get("due_date")
|
||||
due_date = date or posting_date
|
||||
grand_total = self.get("rounded_total") or self.grand_total
|
||||
|
||||
if not self.get("payment_schedule"):
|
||||
if self.get("payment_terms_template"):
|
||||
data = get_payment_terms(self.payment_terms_template, posting_date, grand_total)
|
||||
for item in data:
|
||||
self.append("payment_schedule", item)
|
||||
else:
|
||||
data = dict(due_date=due_date, invoice_portion=100, payment_amount=grand_total)
|
||||
self.append("payment_schedule", data)
|
||||
else:
|
||||
for d in self.get("payment_schedule"):
|
||||
d.payment_amount = grand_total * flt(d.invoice_portion) / 100
|
||||
|
||||
def set_due_date(self):
|
||||
due_dates = [d.due_date for d in self.get("payment_schedule") if d.due_date]
|
||||
if due_dates:
|
||||
self.due_date = max(due_dates)
|
||||
|
||||
def validate_payment_schedule_dates(self):
|
||||
dates = []
|
||||
li = []
|
||||
if self.due_date and getdate(self.due_date) < getdate(self.posting_date):
|
||||
frappe.throw(_("Due Date cannot be before posting date"))
|
||||
|
||||
for d in self.get("payment_schedule"):
|
||||
if getdate(d.due_date) < getdate(self.posting_date):
|
||||
frappe.throw(_("Row {0}: Due Date cannot be before posting date").format(d.idx))
|
||||
elif d.due_date in dates:
|
||||
li.append('{0} in row {1}'.format(d.due_date, d.idx))
|
||||
# frappe.throw(_("Row {0}: Duplicate due date found").format(d.idx))
|
||||
dates.append(d.due_date)
|
||||
|
||||
if li:
|
||||
duplicates = '<br>' + '<br>'.join(li)
|
||||
frappe.throw(_("Rows with duplicate due dates in other rows were found: {list}")
|
||||
.format(list=duplicates))
|
||||
|
||||
def validate_payment_schedule_amount(self):
|
||||
if self.get("payment_schedule"):
|
||||
total = 0
|
||||
for d in self.get("payment_schedule"):
|
||||
total += flt(d.payment_amount)
|
||||
|
||||
grand_total = self.get("rounded_total") or self.grand_total
|
||||
if total != grand_total:
|
||||
frappe.throw(_("Total Payment Amount in Payment Schedule must be equal to Grand / Rounded Total"))
|
||||
|
||||
def validate_invoice_portion(self):
|
||||
if self.get("payment_schedule"):
|
||||
total_portion = 0
|
||||
for term in self.payment_schedule:
|
||||
total_portion += flt(term.get('invoice_portion', 0))
|
||||
|
||||
if flt(total_portion, 2) != 100.00:
|
||||
frappe.throw(_('Combined invoice portion must equal 100%'), indicator='red')
|
||||
|
||||
def is_rounded_total_disabled(self):
|
||||
if self.meta.get_field("disable_rounded_total"):
|
||||
return self.disable_rounded_total
|
||||
@@ -796,4 +883,43 @@ def update_invoice_status():
|
||||
where due_date < CURDATE() and docstatus = 1 and outstanding_amount > 0""")
|
||||
|
||||
frappe.db.sql(""" update `tabPurchase Invoice` set status = 'Overdue'
|
||||
where due_date < CURDATE() and docstatus = 1 and outstanding_amount > 0""")
|
||||
where due_date < CURDATE() and docstatus = 1 and outstanding_amount > 0""")
|
||||
|
||||
@frappe.whitelist()
|
||||
def get_payment_terms(terms_template, posting_date=None, grand_total=None):
|
||||
if not terms_template:
|
||||
return
|
||||
|
||||
terms_doc = frappe.get_doc("Payment Terms Template", terms_template)
|
||||
|
||||
schedule = []
|
||||
for d in terms_doc.get("terms"):
|
||||
term_details = get_payment_term_details(d, posting_date, grand_total)
|
||||
schedule.append(term_details)
|
||||
|
||||
return schedule
|
||||
|
||||
@frappe.whitelist()
|
||||
def get_payment_term_details(term, posting_date=None, grand_total=None):
|
||||
term_details = frappe._dict()
|
||||
if isinstance(term, unicode):
|
||||
term = frappe.get_doc("Payment Term", term)
|
||||
else:
|
||||
term_details.payment_term = term.payment_term
|
||||
term_details.description = term.description
|
||||
term_details.invoice_portion = term.invoice_portion
|
||||
term_details.payment_amount = flt(term.invoice_portion) * flt(grand_total) / 100
|
||||
if posting_date:
|
||||
term_details.due_date = get_due_date(posting_date, term)
|
||||
return term_details
|
||||
|
||||
def get_due_date(posting_date, term):
|
||||
due_date = None
|
||||
if term.due_date_based_on == "Day(s) after invoice date":
|
||||
due_date = add_days(posting_date, term.credit_days)
|
||||
elif term.due_date_based_on == "Day(s) after the end of the invoice month":
|
||||
due_date = add_days(get_last_day(posting_date), term.credit_days)
|
||||
elif term.due_date_based_on == "Month(s) after the end of the invoice month":
|
||||
due_date = add_months(get_last_day(posting_date), term.credit_months)
|
||||
|
||||
return due_date
|
||||
|
||||
Reference in New Issue
Block a user