2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2013-08-05 09:29:54 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
2012-02-23 07:05:32 +00:00
|
|
|
|
2012-07-19 08:10:31 +00:00
|
|
|
from __future__ import unicode_literals
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
2015-07-24 07:56:36 +00:00
|
|
|
import frappe.share
|
2014-02-21 09:14:35 +00:00
|
|
|
from frappe import _
|
2020-04-30 05:08:58 +00:00
|
|
|
from frappe.utils import cstr, now_datetime, cint, flt, get_time, get_datetime, get_link_to_form
|
2013-12-12 13:42:19 +00:00
|
|
|
from erpnext.controllers.status_updater import StatusUpdater
|
2020-04-30 05:08:58 +00:00
|
|
|
from erpnext.accounts.utils import get_fiscal_year
|
2013-01-15 13:09:21 +00:00
|
|
|
|
2018-02-15 06:09:45 +00:00
|
|
|
from six import string_types
|
|
|
|
|
2015-07-24 07:56:36 +00:00
|
|
|
class UOMMustBeIntegerError(frappe.ValidationError): pass
|
2014-01-02 06:17:23 +00:00
|
|
|
|
2014-01-30 13:17:12 +00:00
|
|
|
class TransactionBase(StatusUpdater):
|
2013-03-13 07:28:54 +00:00
|
|
|
def validate_posting_time(self):
|
2017-05-16 02:29:58 +00:00
|
|
|
# set Edit Posting Date and Time to 1 while data import
|
2017-06-19 06:42:22 +00:00
|
|
|
if frappe.flags.in_import and self.posting_date:
|
2017-05-16 02:29:58 +00:00
|
|
|
self.set_posting_time = 1
|
|
|
|
|
2017-03-14 15:36:15 +00:00
|
|
|
if not getattr(self, 'set_posting_time', None):
|
2017-03-14 11:29:11 +00:00
|
|
|
now = now_datetime()
|
|
|
|
self.posting_date = now.strftime('%Y-%m-%d')
|
2017-09-29 09:41:50 +00:00
|
|
|
self.posting_time = now.strftime('%H:%M:%S.%f')
|
2018-01-18 05:50:24 +00:00
|
|
|
elif self.posting_time:
|
2018-01-15 08:48:53 +00:00
|
|
|
try:
|
|
|
|
get_time(self.posting_time)
|
|
|
|
except ValueError:
|
|
|
|
frappe.throw(_('Invalid Posting Time'))
|
2014-04-15 09:06:12 +00:00
|
|
|
|
2020-04-30 05:08:58 +00:00
|
|
|
self.validate_with_last_transaction_posting_time()
|
|
|
|
|
2013-06-10 10:08:01 +00:00
|
|
|
def add_calendar_event(self, opts, force=False):
|
2015-06-29 13:08:27 +00:00
|
|
|
if cstr(self.contact_by) != cstr(self._prev.contact_by) or \
|
2018-03-12 13:19:54 +00:00
|
|
|
cstr(self.contact_date) != cstr(self._prev.contact_date) or force or \
|
|
|
|
(hasattr(self, "ends_on") and cstr(self.ends_on) != cstr(self._prev.ends_on)):
|
2014-04-15 09:06:12 +00:00
|
|
|
|
2013-06-10 09:45:40 +00:00
|
|
|
self.delete_events()
|
|
|
|
self._add_calendar_event(opts)
|
2014-04-15 09:06:12 +00:00
|
|
|
|
2013-06-10 09:45:40 +00:00
|
|
|
def delete_events(self):
|
2018-10-11 11:24:26 +00:00
|
|
|
participations = frappe.get_all("Event Participants", filters={"reference_doctype": self.doctype, "reference_docname": self.name,
|
|
|
|
"parenttype": "Event"}, fields=["name", "parent"])
|
|
|
|
|
|
|
|
if participations:
|
|
|
|
for participation in participations:
|
|
|
|
total_participants = frappe.get_all("Event Participants", filters={"parenttype": "Event", "parent": participation.parent})
|
|
|
|
|
|
|
|
if len(total_participants) <= 1:
|
|
|
|
frappe.db.sql("delete from `tabEvent` where name='%s'" % participation.parent)
|
|
|
|
|
|
|
|
frappe.db.sql("delete from `tabEvent Participants` where name='%s'" % participation.name)
|
|
|
|
|
2015-08-26 12:26:40 +00:00
|
|
|
|
2013-06-10 09:45:40 +00:00
|
|
|
def _add_calendar_event(self, opts):
|
2014-02-14 10:17:51 +00:00
|
|
|
opts = frappe._dict(opts)
|
2014-04-15 09:06:12 +00:00
|
|
|
|
2014-03-28 08:25:00 +00:00
|
|
|
if self.contact_date:
|
2015-02-13 04:46:41 +00:00
|
|
|
event = frappe.get_doc({
|
2013-06-10 09:45:40 +00:00
|
|
|
"doctype": "Event",
|
2014-03-28 08:25:00 +00:00
|
|
|
"owner": opts.owner or self.owner,
|
2013-06-10 09:45:40 +00:00
|
|
|
"subject": opts.subject,
|
|
|
|
"description": opts.description,
|
2015-08-26 12:26:40 +00:00
|
|
|
"starts_on": self.contact_date,
|
2018-03-12 13:19:54 +00:00
|
|
|
"ends_on": opts.ends_on,
|
2018-10-11 11:24:26 +00:00
|
|
|
"event_type": "Private"
|
2014-03-27 12:21:41 +00:00
|
|
|
})
|
2015-02-23 16:44:12 +00:00
|
|
|
|
2018-10-11 11:24:26 +00:00
|
|
|
event.append('event_participants', {
|
|
|
|
"reference_doctype": self.doctype,
|
|
|
|
"reference_docname": self.name
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2015-02-20 10:52:24 +00:00
|
|
|
event.insert(ignore_permissions=True)
|
2014-04-15 09:06:12 +00:00
|
|
|
|
2014-03-28 08:25:00 +00:00
|
|
|
if frappe.db.exists("User", self.contact_by):
|
2015-08-26 12:26:40 +00:00
|
|
|
frappe.share.add("Event", event.name, self.contact_by,
|
2015-05-21 11:21:15 +00:00
|
|
|
flags={"ignore_share_permission": True})
|
2014-04-15 09:06:12 +00:00
|
|
|
|
2013-07-25 12:15:59 +00:00
|
|
|
def validate_uom_is_integer(self, uom_field, qty_fields):
|
2014-04-03 06:16:52 +00:00
|
|
|
validate_uom_is_integer(self, uom_field, qty_fields)
|
2014-04-15 09:06:12 +00:00
|
|
|
|
2014-12-26 07:45:21 +00:00
|
|
|
def validate_with_previous_doc(self, ref):
|
2018-03-01 06:01:33 +00:00
|
|
|
self.exclude_fields = ["conversion_factor", "uom"] if self.get('is_return') else []
|
|
|
|
|
2013-07-08 13:30:29 +00:00
|
|
|
for key, val in ref.items():
|
2013-07-10 10:03:29 +00:00
|
|
|
is_child = val.get("is_child_table")
|
2013-07-08 13:30:29 +00:00
|
|
|
ref_doc = {}
|
2013-07-15 11:03:24 +00:00
|
|
|
item_ref_dn = []
|
2014-12-26 07:45:21 +00:00
|
|
|
for d in self.get_all_children(self.doctype + " Item"):
|
2014-03-28 08:25:00 +00:00
|
|
|
ref_dn = d.get(val["ref_dn_field"])
|
2013-07-10 10:03:29 +00:00
|
|
|
if ref_dn:
|
|
|
|
if is_child:
|
|
|
|
self.compare_values({key: [ref_dn]}, val["compare_fields"], d)
|
2013-07-15 11:03:24 +00:00
|
|
|
if ref_dn not in item_ref_dn:
|
|
|
|
item_ref_dn.append(ref_dn)
|
2013-07-29 09:59:51 +00:00
|
|
|
elif not val.get("allow_duplicate_prev_row_id"):
|
2014-04-15 09:06:12 +00:00
|
|
|
frappe.throw(_("Duplicate row {0} with same {1}").format(d.idx, key))
|
2013-07-15 11:03:24 +00:00
|
|
|
elif ref_dn:
|
2013-07-10 10:03:29 +00:00
|
|
|
ref_doc.setdefault(key, [])
|
|
|
|
if ref_dn not in ref_doc[key]:
|
|
|
|
ref_doc[key].append(ref_dn)
|
|
|
|
if ref_doc:
|
2013-07-08 13:30:29 +00:00
|
|
|
self.compare_values(ref_doc, val["compare_fields"])
|
2014-04-15 09:06:12 +00:00
|
|
|
|
2013-07-08 13:30:29 +00:00
|
|
|
def compare_values(self, ref_doc, fields, doc=None):
|
2015-03-19 11:45:45 +00:00
|
|
|
for reference_doctype, ref_dn_list in ref_doc.items():
|
|
|
|
for reference_name in ref_dn_list:
|
|
|
|
prevdoc_values = frappe.db.get_value(reference_doctype, reference_name,
|
2013-07-10 10:03:29 +00:00
|
|
|
[d[0] for d in fields], as_dict=1)
|
|
|
|
|
2015-12-01 10:23:07 +00:00
|
|
|
if not prevdoc_values:
|
|
|
|
frappe.throw(_("Invalid reference {0} {1}").format(reference_doctype, reference_name))
|
|
|
|
|
2013-07-10 10:03:29 +00:00
|
|
|
for field, condition in fields:
|
2018-03-01 06:01:33 +00:00
|
|
|
if prevdoc_values[field] is not None and field not in self.exclude_fields:
|
2013-07-10 15:25:15 +00:00
|
|
|
self.validate_value(field, condition, prevdoc_values[field], doc)
|
2015-08-26 12:26:40 +00:00
|
|
|
|
|
|
|
|
2015-07-08 07:40:52 +00:00
|
|
|
def validate_rate_with_reference_doc(self, ref_details):
|
|
|
|
for ref_dt, ref_dn_field, ref_link_field in ref_details:
|
|
|
|
for d in self.get("items"):
|
|
|
|
if d.get(ref_link_field):
|
|
|
|
ref_rate = frappe.db.get_value(ref_dt + " Item", d.get(ref_link_field), "rate")
|
2015-08-26 12:26:40 +00:00
|
|
|
|
2015-07-08 07:40:52 +00:00
|
|
|
if abs(flt(d.rate - ref_rate, d.precision("rate"))) >= .01:
|
2020-03-24 06:22:34 +00:00
|
|
|
frappe.msgprint(_("Row #{0}: Rate must be same as {1}: {2} ({3} / {4}) ")
|
2015-07-08 07:40:52 +00:00
|
|
|
.format(d.idx, ref_dt, d.get(ref_dn_field), d.rate, ref_rate))
|
2020-03-24 06:22:34 +00:00
|
|
|
frappe.throw(_("To allow different rates, disable the {0} checkbox in {1}.")
|
2020-04-21 07:22:29 +00:00
|
|
|
.format(frappe.bold(_("Maintain Same Rate Throughout Sales Cycle")),
|
2020-03-26 06:26:03 +00:00
|
|
|
get_link_to_form("Selling Settings", "Selling Settings", frappe.bold("Selling Settings"))))
|
2014-04-15 09:06:12 +00:00
|
|
|
|
2016-06-16 12:31:58 +00:00
|
|
|
def get_link_filters(self, for_doctype):
|
2016-06-27 09:18:26 +00:00
|
|
|
if hasattr(self, "prev_link_mapper") and self.prev_link_mapper.get(for_doctype):
|
2016-06-16 12:31:58 +00:00
|
|
|
fieldname = self.prev_link_mapper[for_doctype]["fieldname"]
|
2017-03-14 11:29:11 +00:00
|
|
|
|
2016-06-16 12:31:58 +00:00
|
|
|
values = filter(None, tuple([item.as_dict()[fieldname] for item in self.items]))
|
|
|
|
|
|
|
|
if values:
|
|
|
|
ret = {
|
|
|
|
for_doctype : {
|
|
|
|
"filters": [[for_doctype, "name", "in", values]]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
ret = None
|
|
|
|
else:
|
|
|
|
ret = None
|
2017-03-14 11:29:11 +00:00
|
|
|
|
2016-06-16 12:31:58 +00:00
|
|
|
return ret
|
2015-02-23 16:44:12 +00:00
|
|
|
|
2020-04-30 05:08:58 +00:00
|
|
|
def validate_with_last_transaction_posting_time(self):
|
|
|
|
|
|
|
|
if self.doctype not in ["Sales Invoice", "Purchase Invoice", "Stock Entry", "Stock Reconciliation",
|
|
|
|
"Delivery Note", "Purchase Receipt", "Fees"]:
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.doctype in ["Sales Invoice", "Purchase Invoice"]:
|
|
|
|
if not (self.get("update_stock") or self.get("is_pos")):
|
|
|
|
return
|
|
|
|
|
2020-06-22 13:35:39 +00:00
|
|
|
for item in self.get('items'):
|
|
|
|
last_transaction_time = frappe.db.sql("""
|
|
|
|
select MAX(timestamp(posting_date, posting_time)) as posting_time
|
|
|
|
from `tabStock Ledger Entry`
|
|
|
|
where docstatus = 1 and item_code = %s """, (item.item_code))[0][0]
|
|
|
|
|
|
|
|
cur_doc_posting_datetime = "%s %s" % (self.posting_date, self.get("posting_time") or "00:00:00")
|
|
|
|
|
|
|
|
if last_transaction_time and get_datetime(cur_doc_posting_datetime) < get_datetime(last_transaction_time):
|
|
|
|
msg = _("Last Stock Transaction for item {0} was on {1}.").format(frappe.bold(item.item_code), frappe.bold(last_transaction_time))
|
|
|
|
msg += "<br><br>" + _("Stock Transactions for Item {0} cannot be posted before this time.").format(frappe.bold(item.item_code))
|
|
|
|
msg += "<br><br>" + _("Please remove this item and try to submit again or update the posting time.")
|
|
|
|
frappe.throw(msg, title=_("Backdated Stock Entry"))
|
2020-04-30 05:08:58 +00:00
|
|
|
|
2013-06-17 07:21:36 +00:00
|
|
|
def delete_events(ref_type, ref_name):
|
2019-01-01 08:24:54 +00:00
|
|
|
events = frappe.db.sql_list(""" SELECT
|
|
|
|
distinct `tabEvent`.name
|
|
|
|
from
|
|
|
|
`tabEvent`, `tabEvent Participants`
|
|
|
|
where
|
|
|
|
`tabEvent`.name = `tabEvent Participants`.parent
|
|
|
|
and `tabEvent Participants`.reference_doctype = %s
|
|
|
|
and `tabEvent Participants`.reference_docname = %s
|
|
|
|
""", (ref_type, ref_name)) or []
|
|
|
|
|
|
|
|
if events:
|
|
|
|
frappe.delete_doc("Event", events, for_reload=True)
|
2013-07-25 12:15:59 +00:00
|
|
|
|
2015-02-09 11:16:46 +00:00
|
|
|
def validate_uom_is_integer(doc, uom_field, qty_fields, child_dt=None):
|
2018-02-15 06:09:45 +00:00
|
|
|
if isinstance(qty_fields, string_types):
|
2013-07-25 12:15:59 +00:00
|
|
|
qty_fields = [qty_fields]
|
2014-04-15 09:06:12 +00:00
|
|
|
|
2014-04-02 12:39:34 +00:00
|
|
|
distinct_uoms = list(set([d.get(uom_field) for d in doc.get_all_children()]))
|
2020-01-07 05:53:53 +00:00
|
|
|
integer_uoms = list(filter(lambda uom: frappe.db.get_value("UOM", uom,
|
|
|
|
"must_be_whole_number", cache=True) or None, distinct_uoms))
|
2014-04-15 09:06:12 +00:00
|
|
|
|
2013-07-25 12:15:59 +00:00
|
|
|
if not integer_uoms:
|
|
|
|
return
|
|
|
|
|
2015-02-09 11:16:46 +00:00
|
|
|
for d in doc.get_all_children(parenttype=child_dt):
|
2014-03-28 08:25:00 +00:00
|
|
|
if d.get(uom_field) in integer_uoms:
|
2013-07-25 12:15:59 +00:00
|
|
|
for f in qty_fields:
|
2017-04-20 09:51:01 +00:00
|
|
|
qty = d.get(f)
|
|
|
|
if qty:
|
2017-06-19 04:28:48 +00:00
|
|
|
if abs(cint(qty) - flt(qty)) > 0.0000001:
|
2020-04-21 07:22:29 +00:00
|
|
|
frappe.throw(_("Row {1}: Quantity ({0}) cannot be a fraction. To allow this, disable '{2}' in UOM {3}.") \
|
|
|
|
.format(qty, d.idx, frappe.bold(_("Must be Whole Number")), frappe.bold(d.get(uom_field))),
|
|
|
|
UOMMustBeIntegerError)
|