-
Notifications
You must be signed in to change notification settings - Fork 15
/
remove_duplicates.py
48 lines (32 loc) · 1.01 KB
/
remove_duplicates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import frappe
def execute():
orders = frappe.get_all("Sales Order", fields=['name', 'po_no', 'creation'])
existing_orders = _group(orders)
processed = 1
total_orders = len(orders)
for order in orders:
print("Processing {}/{}".format(processed, total_orders))
po_no = order.get('po_no')
name = order.get('name')
existing_order = existing_orders[po_no]
if name != existing_order.get('name'):
frappe.delete_doc("Sales Order", name)
processed = processed + 1
def _group(orders):
return reduce(_reduce_to_groups, orders, {})
def _reduce_to_groups(_, group):
po_no = group.get('po_no')
if po_no in _:
creation = _compare_creation(_[po_no], group)
if creation == 1:
_[po_no] = group
else:
_[po_no] = group
return _
def _compare_creation(a, b):
a_creation = a.get('creation')
b_creation = b.get('creation')
if a_creation > b_creation:
return 1
else:
return -1