mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-22 14:39:19 +00:00
refactor: deduplicate during repost background job
(cherry picked from commit ed94f0f3f2)
This commit is contained in:
@@ -2,23 +2,10 @@
|
|||||||
# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and contributors
|
# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and contributors
|
||||||
# For license information, please see license.txt
|
# For license information, please see license.txt
|
||||||
|
|
||||||
from __future__ import unicode_literals
|
|
||||||
|
|
||||||
import datetime
|
|
||||||
|
|
||||||
import frappe
|
import frappe
|
||||||
from frappe import _
|
from frappe import _
|
||||||
from frappe.model.document import Document
|
from frappe.model.document import Document
|
||||||
from frappe.utils import (
|
from frappe.utils import cint, get_link_to_form, get_weekday, now, nowtime, today
|
||||||
cint,
|
|
||||||
get_datetime,
|
|
||||||
get_link_to_form,
|
|
||||||
get_time,
|
|
||||||
get_weekday,
|
|
||||||
now,
|
|
||||||
nowtime,
|
|
||||||
today,
|
|
||||||
)
|
|
||||||
from frappe.utils.user import get_users_with_role
|
from frappe.utils.user import get_users_with_role
|
||||||
from rq.timeouts import JobTimeoutException
|
from rq.timeouts import JobTimeoutException
|
||||||
|
|
||||||
@@ -60,7 +47,6 @@ class RepostItemValuation(Document):
|
|||||||
self.db_set('status', self.status)
|
self.db_set('status', self.status)
|
||||||
|
|
||||||
def on_submit(self):
|
def on_submit(self):
|
||||||
self.deduplicate_similar_repost()
|
|
||||||
if not frappe.flags.in_test or self.flags.dont_run_in_test:
|
if not frappe.flags.in_test or self.flags.dont_run_in_test:
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -78,30 +64,27 @@ class RepostItemValuation(Document):
|
|||||||
if self.based_on != "Item and Warehouse":
|
if self.based_on != "Item and Warehouse":
|
||||||
return
|
return
|
||||||
|
|
||||||
queued = frappe.db.get_value(
|
filters = {
|
||||||
"Repost Item Valuation",
|
"item_code": self.item_code,
|
||||||
filters={
|
"warehouse": self.warehouse,
|
||||||
"docstatus": 1,
|
"name": self.name,
|
||||||
"status": "Queued",
|
"posting_date": self.posting_date,
|
||||||
"item_code": self.item_code,
|
"posting_time": self.posting_time,
|
||||||
"warehouse": self.warehouse,
|
}
|
||||||
"based_on": self.based_on,
|
|
||||||
"name": ("!=", self.name)
|
|
||||||
},
|
|
||||||
fieldname=["name", "posting_date", "posting_time"],
|
|
||||||
as_dict=True
|
|
||||||
)
|
|
||||||
if not queued:
|
|
||||||
return
|
|
||||||
|
|
||||||
posting_timestamp = datetime.datetime.combine(get_datetime(self.posting_date), get_time(self.posting_time))
|
|
||||||
queued_timestamp = datetime.datetime.combine(get_datetime(queued.posting_date), get_time(queued.posting_time))
|
|
||||||
|
|
||||||
if posting_timestamp > queued_timestamp:
|
|
||||||
self.set_status("Skipped")
|
|
||||||
else:
|
|
||||||
frappe.db.set_value("Repost Item Valuation", queued.name, "status", "Skipped")
|
|
||||||
|
|
||||||
|
frappe.db.sql("""
|
||||||
|
update `tabRepost Item Valuation`
|
||||||
|
set status = 'Skipped'
|
||||||
|
WHERE item_code = %(item_code)s
|
||||||
|
and warehouse = %(warehouse)s
|
||||||
|
and name != %(name)s
|
||||||
|
and TIMESTAMP(posting_date, posting_time) > TIMESTAMP(%(posting_date)s, %(posting_time)s)
|
||||||
|
and docstatus = 1
|
||||||
|
and status = 'Queued'
|
||||||
|
and based_on = 'Item and Warehouse'
|
||||||
|
""",
|
||||||
|
filters
|
||||||
|
)
|
||||||
|
|
||||||
def on_doctype_update():
|
def on_doctype_update():
|
||||||
frappe.db.add_index("Repost Item Valuation", ["warehouse", "item_code"], "item_warehouse")
|
frappe.db.add_index("Repost Item Valuation", ["warehouse", "item_code"], "item_warehouse")
|
||||||
@@ -184,6 +167,7 @@ def repost_entries():
|
|||||||
for row in riv_entries:
|
for row in riv_entries:
|
||||||
doc = frappe.get_doc('Repost Item Valuation', row.name)
|
doc = frappe.get_doc('Repost Item Valuation', row.name)
|
||||||
if doc.status in ('Queued', 'In Progress'):
|
if doc.status in ('Queued', 'In Progress'):
|
||||||
|
doc.deduplicate_similar_repost()
|
||||||
repost(doc)
|
repost(doc)
|
||||||
|
|
||||||
riv_entries = get_repost_item_valuation_entries()
|
riv_entries = get_repost_item_valuation_entries()
|
||||||
|
|||||||
@@ -116,12 +116,14 @@ class TestRepostItemValuation(unittest.TestCase):
|
|||||||
riv2 = frappe.get_doc(riv_args.update({"posting_date": "2021-01-03"}))
|
riv2 = frappe.get_doc(riv_args.update({"posting_date": "2021-01-03"}))
|
||||||
riv2.flags.dont_run_in_test = True
|
riv2.flags.dont_run_in_test = True
|
||||||
riv2.submit()
|
riv2.submit()
|
||||||
|
riv1.deduplicate_similar_repost()
|
||||||
_assert_status(riv2, "Skipped")
|
_assert_status(riv2, "Skipped")
|
||||||
|
|
||||||
# older than exisitng duplicate - riv1
|
# older than exisitng duplicate - riv1
|
||||||
riv3 = frappe.get_doc(riv_args.update({"posting_date": "2021-01-01"}))
|
riv3 = frappe.get_doc(riv_args.update({"posting_date": "2021-01-01"}))
|
||||||
riv3.flags.dont_run_in_test = True
|
riv3.flags.dont_run_in_test = True
|
||||||
riv3.submit()
|
riv3.submit()
|
||||||
|
riv3.deduplicate_similar_repost()
|
||||||
_assert_status(riv3, "Queued")
|
_assert_status(riv3, "Queued")
|
||||||
_assert_status(riv1, "Skipped")
|
_assert_status(riv1, "Skipped")
|
||||||
|
|
||||||
@@ -129,6 +131,7 @@ class TestRepostItemValuation(unittest.TestCase):
|
|||||||
riv4 = frappe.get_doc(riv_args.update({"warehouse": "Stores - _TC"}))
|
riv4 = frappe.get_doc(riv_args.update({"warehouse": "Stores - _TC"}))
|
||||||
riv4.flags.dont_run_in_test = True
|
riv4.flags.dont_run_in_test = True
|
||||||
riv4.submit()
|
riv4.submit()
|
||||||
|
riv4.deduplicate_similar_repost()
|
||||||
_assert_status(riv4, "Queued")
|
_assert_status(riv4, "Queued")
|
||||||
_assert_status(riv3, "Queued")
|
_assert_status(riv3, "Queued")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user