/
purchase_request_line_make_purchase_order.py
312 lines (282 loc) · 12.9 KB
/
purchase_request_line_make_purchase_order.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# -*- coding: utf-8 -*-
# Copyright 2016 Eficent Business and IT Consulting Services S.L.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl-3.0).
import odoo.addons.decimal_precision as dp
from odoo import _, api, exceptions, fields, models
class PurchaseRequestLineMakePurchaseOrder(models.TransientModel):
_name = "purchase.request.line.make.purchase.order"
_description = "Purchase Request Line Make Purchase Order"
supplier_id = fields.Many2one('res.partner', string='Supplier',
required=False,
domain=[('supplier', '=', True)])
item_ids = fields.One2many(
'purchase.request.line.make.purchase.order.item',
'wiz_id', string='Items')
purchase_order_id = fields.Many2one('purchase.order',
string='Purchase Order',
required=False,
domain=[('state', '=', 'draft')])
@api.model
def _prepare_item(self, line):
return {
'line_id': line.id,
'request_id': line.request_id.id,
'product_id': line.product_id.id,
'name': line.name or line.product_id.name,
'product_qty': line.product_qty,
'product_uom_id': line.product_uom_id.id,
}
@api.model
def _check_valid_request_line(self, request_line_ids):
location = False
picking_type = False
company_id = False
for line in self.env['purchase.request.line'].browse(request_line_ids):
if line.request_id.state != 'approved':
raise exceptions.Warning(
_('Purchase Request %s is not approved') %
line.request_id.name)
if line.purchase_state == 'done':
raise exceptions.Warning(
_('The purchase has already been completed.'))
line_company_id = line.company_id \
and line.company_id.id or False
if company_id is not False \
and line_company_id != company_id:
raise exceptions.Warning(
_('You have to select lines '
'from the same company.'))
else:
company_id = line_company_id
line_picking_type = line.request_id.picking_type_id or False
if not line_picking_type:
raise exceptions.Warning(
_('You have to enter a Picking Type.'))
if picking_type is not False \
and line_picking_type != picking_type:
raise exceptions.Warning(
_('You have to select lines '
'from the same Picking Type.'))
else:
picking_type = line_picking_type
line_location = line.procurement_id and \
line.procurement_id.location_id or False
if location is not False and line_location != location and \
line_location:
raise exceptions.Warning(
_('You have to select lines '
'from the same procurement location.'))
else:
location = line_location
@api.model
def default_get(self, fields):
res = super(PurchaseRequestLineMakePurchaseOrder, self).default_get(
fields)
request_line_obj = self.env['purchase.request.line']
request_line_ids = self.env.context.get('active_ids', False)
active_model = self.env.context.get('active_model', False)
if not request_line_ids:
return res
assert active_model == 'purchase.request.line', \
'Bad context propagation'
items = []
self._check_valid_request_line(request_line_ids)
request_lines = request_line_obj.browse(request_line_ids)
for line in request_lines:
items.append([0, 0, self._prepare_item(line)])
res['item_ids'] = items
supplier_ids = request_lines.mapped('supplier_id').ids
if len(supplier_ids) == 1:
res['supplier_id'] = supplier_ids[0]
return res
@api.model
def _prepare_purchase_order(self, picking_type, location, company):
if not self.supplier_id:
raise exceptions.Warning(
_('Enter a supplier.'))
supplier = self.supplier_id
data = {
'origin': '',
'partner_id': self.supplier_id.id,
'fiscal_position_id': supplier.property_account_position_id and
supplier.property_account_position_id.id or False,
'picking_type_id': picking_type.id,
'company_id': company.id,
}
return data
@api.model
def _get_purchase_line_onchange_fields(self):
return ['product_uom', 'price_unit', 'name',
'taxes_id']
@api.model
def _execute_purchase_line_onchange(self, vals):
cls = self.env['purchase.order.line']
onchanges_dict = {
'onchange_product_id': self._get_purchase_line_onchange_fields(),
}
for onchange_method, changed_fields in onchanges_dict.items():
if any(f not in vals for f in changed_fields):
obj = cls.new(vals)
getattr(obj, onchange_method)()
for field in changed_fields:
vals[field] = obj._fields[field].convert_to_write(
obj[field], obj)
@api.model
def _prepare_purchase_order_line(self, po, item):
product = item.product_id
# Keep the standard product UOM for purchase order so we should
# convert the product quantity to this UOM
qty = item.product_uom_id._compute_quantity(
item.product_qty, product.uom_po_id)
# Suggest the supplier min qty as it's done in Odoo core
min_qty = item.line_id._get_supplier_min_qty(product, po.partner_id)
qty = max(qty, min_qty)
vals = {
'name': product.name,
'order_id': po.id,
'product_id': product.id,
'product_uom': product.uom_po_id.id,
'price_unit': 0.0,
'product_qty': qty,
'account_analytic_id': item.line_id.analytic_account_id.id,
'purchase_request_lines': [(4, item.line_id.id)],
'date_planned': item.line_id.date_required
}
if item.line_id.procurement_id:
vals['procurement_ids'] = [(4, item.line_id.procurement_id.id)]
self._execute_purchase_line_onchange(vals)
return vals
@api.model
def _get_purchase_line_name(self, order, line):
product_lang = line.product_id.with_context({
'lang': self.supplier_id.lang,
'partner_id': self.supplier_id.id,
})
name = product_lang.display_name
if product_lang.description_purchase:
name += '\n' + product_lang.description_purchase
return name
@api.model
def _get_order_line_search_domain(self, order, item):
vals = self._prepare_purchase_order_line(order, item)
name = self._get_purchase_line_name(order, item)
order_line_data = [('order_id', '=', order.id),
('name', '=', name),
('product_id', '=', item.product_id.id or False),
('date_planned', '=', item.line_id.date_required),
('product_uom', '=', vals['product_uom']),
('account_analytic_id', '=',
item.line_id.analytic_account_id.id or False),
]
if not item.product_id:
order_line_data.append(('name', '=', item.name))
if not item.line_id.procurement_id and \
item.line_id.procurement_id.location_id:
order_line_data.append(
('location_id', '=',
item.line_id.procurement_id.location_id.id))
return order_line_data
@api.multi
def make_purchase_order(self):
res = []
purchase_obj = self.env['purchase.order']
po_line_obj = self.env['purchase.order.line']
pr_line_obj = self.env['purchase.request.line']
purchase = False
for item in self.item_ids:
line = item.line_id
if item.product_qty <= 0.0:
raise exceptions.Warning(
_('Enter a positive quantity.'))
location = line.request_id.picking_type_id.default_location_dest_id
if self.purchase_order_id:
purchase = self.purchase_order_id
if not purchase:
po_data = self._prepare_purchase_order(
line.request_id.picking_type_id, location,
line.company_id)
purchase = purchase_obj.create(po_data)
# Look for any other PO line in the selected PO with same
# product and UoM to sum quantities instead of creating a new
# po line
domain = self._get_order_line_search_domain(purchase, item)
available_po_lines = po_line_obj.search(domain)
new_pr_line = True
if available_po_lines and not item.keep_description:
new_pr_line = False
po_line = available_po_lines[0]
po_line.purchase_request_lines = [(4, line.id)]
else:
po_line_data = self._prepare_purchase_order_line(purchase,
item)
if item.keep_description:
po_line_data['name'] = item.name
po_line = po_line_obj.create(po_line_data)
new_qty = pr_line_obj._calc_new_qty(
line, po_line=po_line,
new_pr_line=new_pr_line)
po_line.product_qty = new_qty
po_line._onchange_quantity()
# The onchange quantity is altering the scheduled date of the PO
# lines. We do not want that:
po_line.date_planned = item.line_id.date_required
res.append(purchase.id)
return {
'domain': [('id', 'in', res)],
'name': _('RFQ'),
'view_type': 'form',
'view_mode': 'tree,form',
'res_model': 'purchase.order',
'view_id': False,
'context': False,
'type': 'ir.actions.act_window'
}
class PurchaseRequestLineMakePurchaseOrderItem(models.TransientModel):
_name = "purchase.request.line.make.purchase.order.item"
_description = "Purchase Request Line Make Purchase Order Item"
wiz_id = fields.Many2one(
'purchase.request.line.make.purchase.order',
string='Wizard', required=True, ondelete='cascade',
readonly=True)
line_id = fields.Many2one('purchase.request.line',
string='Purchase Request Line',
required=True,
readonly=True)
request_id = fields.Many2one('purchase.request',
related='line_id.request_id',
string='Purchase Request',
readonly=True)
product_id = fields.Many2one('product.product', string='Product')
name = fields.Char(string='Description', required=True)
product_qty = fields.Float(string='Quantity to purchase',
digits=dp.get_precision('Product UoS'),
readonly=True)
product_uom_id = fields.Many2one('product.uom', string='UoM',
readonly=True)
keep_description = fields.Boolean(string='Copy descriptions to new PO',
help='Set true if you want to keep the '
'descriptions provided in the '
'wizard in the new PO.',
default=False)
@api.onchange('product_id')
def onchange_product_id(self):
if self.product_id:
name = self.product_id.name
code = self.product_id.code
sup_info_id = self.env['product.supplierinfo'].search([
'|', ('product_id', '=', self.product_id.id),
('product_tmpl_id', '=', self.product_id.product_tmpl_id.id),
('name', '=', self.wiz_id.supplier_id.id)])
if sup_info_id:
p_code = sup_info_id[0].product_code
p_name = sup_info_id[0].product_name
name = '[%s] %s' % (p_code if p_code else code,
p_name if p_name else name)
else:
if code:
name = '[%s] %s' % (code, name)
if self.product_id.description_purchase:
name += '\n' + self.product_id.description_purchase
self.product_uom_id = self.product_id.uom_id.id
self.product_qty = 1.0
self.name = name