forked from OCA/purchase-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpurchase_merge.py
229 lines (196 loc) · 8.13 KB
/
purchase_merge.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# Copyright 2022 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from openupgradelib import openupgrade_merge_records
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
class MergePurchaseAutomatic(models.TransientModel):
"""
The idea behind this wizard is to create a list of potential purchases
to merge. We use two objects, the first one is the wizard for
the end-user. And the second will contain the purchase list to merge.
"""
_name = "purchase.merge.automatic.wizard"
purchase_ids = fields.Many2many(
comodel_name="purchase.order",
)
dst_purchase_id = fields.Many2one(
comodel_name="purchase.order",
string="Destination",
)
@api.model
def default_get(self, fields_list):
res = super(MergePurchaseAutomatic, self).default_get(fields_list)
active_ids = self.env.context.get("active_ids")
purchase_orders = self.purchase_ids.browse(active_ids)
self._check_all_values(purchase_orders)
if (
self.env.context.get("active_model") == "purchase.order"
and len(active_ids) >= 1
):
res["purchase_ids"] = [(6, 0, active_ids)]
res["dst_purchase_id"] = self._get_ordered_purchase(active_ids)[-1].id
return res
# ----------------------------------------
# Check method
# ----------------------------------------
def _check_all_values(self, purchase_orders):
"""Contain all check method"""
self._check_state(purchase_orders)
self._check_content(purchase_orders)
def _check_state(self, purchase_orders):
non_draft_po = purchase_orders.filtered(lambda p: p.state != "draft")
if non_draft_po:
po_names = non_draft_po.mapped("name")
raise ValidationError(
_(
"You can't merge purchase orders that aren't in draft state like: {}"
).format(po_names)
)
def _check_content(self, purchase_orders):
error_messages = []
currencies = purchase_orders.currency_id
if len(currencies) > 1:
error_messages.append(
_(
"You can't merge purchase orders with different currencies: %s",
", ".join(currencies.mapped("name")),
)
)
picking_types = purchase_orders.picking_type_id
if len(picking_types) > 1:
error_messages.append(
_(
"You can't merge purchase orders with different picking types: %s",
", ".join(picking_types.mapped("name")),
)
)
incoterms = purchase_orders.incoterm_id
if len(incoterms) > 1:
error_messages.append(
_(
"You can't merge purchase orders with different incoterms: %s",
", ".join(incoterms.mapped("name")),
)
)
payment_terms = purchase_orders.payment_term_id
if len(payment_terms) > 1:
error_messages.append(
_(
"You can't merge purchase orders with different payment terms: %s",
", ".join(payment_terms.mapped("name")),
)
)
fiscal_positions = purchase_orders.fiscal_position_id
if len(fiscal_positions) > 1:
error_messages.append(
_(
"You can't merge purchase orders with different fiscal positions: %s",
", ".join(fiscal_positions.mapped("name")),
)
)
suppliers = purchase_orders.partner_id
if len(suppliers) > 1:
error_messages.append(
_(
"You can't merge purchase orders with different suppliers: %s",
", ".join(suppliers.mapped("name")),
)
)
if error_messages:
raise ValidationError("\n".join(error_messages))
# ----------------------------------------
# Update method
# ----------------------------------------
@api.model
def _update_values(self, src_purchase, dst_purchase):
"""Update values of dst_purchase with the ones from the src_purchase.
:param src_purchase : recordset of source purchase.order
:param dst_purchase : record of destination purchase.order
"""
# merge all order lines + set origin and partner_ref
dst_purchase.write(self._get_update_values(src_purchase, dst_purchase))
for po in src_purchase:
self._add_message("to", [dst_purchase.name], po)
po_names = src_purchase.mapped("name")
self._add_message("from", po_names, dst_purchase)
@api.model
def _get_update_values(self, src_purchase, dst_purchase):
"""Generate values of dst_purchase with the ones from the src_purchase.
:param src_purchase : recordset of source purchase.order
:param dst_purchase : record of destination purchase.order
"""
# initialize destination origin and partner_ref
origin = {dst_purchase.origin or ""}
origin.update({x.origin for x in src_purchase if x.origin})
partner_ref = {dst_purchase.partner_ref or ""}
partner_ref.update({x.partner_ref for x in src_purchase if x.partner_ref})
# Generate destination origin and partner_ref
src_order_line = src_purchase.mapped("order_line")
return {
"order_line": [(4, line, 0) for line in src_order_line.ids],
"origin": ", ".join(origin),
"partner_ref": ", ".join(partner_ref),
}
def _add_message(self, way, po_name, po):
"""Send a message post with to advise the po about the merge.
:param way : choice between 'from' or 'to'
:param po_name : list of purchase order name to add in the body
:param po_name : the po where the message will be posted
"""
subject = "Merge purchase order"
body = _(
"This purchase order lines have been merged {way} : {po_names}",
way=way,
po_names=" ,".join(po_name),
)
po.message_post(body=body, subject=subject, content_subtype="plaintext")
def _merge(self, purchases, dst_purchase=None):
"""private implementation of merge purchase
:param purchases : ids of purchase to merge
:param dst_purchase : record of destination purchase.order
"""
if len(purchases) < 2:
return
record_ids = purchases - dst_purchase
openupgrade_merge_records.merge_records(
env=self.env,
model_name=self._name,
record_ids=record_ids.ids,
target_record_id=dst_purchase.id,
)
self._check_all_values(purchases)
# remove dst_purchase from purchases to merge
if dst_purchase and dst_purchase in purchases:
src_purchase = purchases - dst_purchase
else:
dst_purchase = self.purchase_ids[-1]
src_purchase = self.purchase_ids[:-1]
# call sub methods to do the merge
self._update_values(src_purchase, dst_purchase)
# cancel source purchase, since they are merged
src_purchase.button_cancel()
# ----------------------------------------
# Helpers
# ----------------------------------------
@api.model
def _get_ordered_purchase(self, purchase_ids):
"""Helper returns a `purchase.order` recordset ordered by create_date
:param purchase_ids : list of purchase ids to sort
"""
return (
self.env["purchase.order"]
.browse(purchase_ids)
.sorted(
key=lambda p: (p.create_date or ""),
reverse=True,
)
)
# ----------------------------------------
# Actions
# ----------------------------------------
def action_merge(self):
"""Merge Quotation button. Merge the selected purchases."""
if not self.purchase_ids:
return False
self._merge(self.purchase_ids, self.dst_purchase_id)
return True