~unifield-team/unifield-wm/us-826

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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) Copyright (C) 2011 MSF, TeMPO Consulting.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import fields, osv
from tools.translate import _
import decimal_precision as dp

import netsvc

from msf_outgoing import INTEGRITY_STATUS_SELECTION


class kit_selection(osv.osv_memory):
    '''
    kit selection
    '''
    _name = "kit.selection"
    
    def _vals_get(self, cr, uid, ids, fields, arg, context=None):
        '''
        multi fields function method
        '''
        # Some verifications
        if context is None:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
            
        # objects
        sol_obj = self.pool.get('sale.order.line')
        pol_obj = self.pool.get('purchase.order.line')
        
        result = {}
        for obj in self.browse(cr, uid, ids, context=context):
            result[obj.id] = {}
            # corresponding_so_line_id_kit_selection
            sol_ids = False
            if obj.order_line_id_kit_selection:
                sol_ids = pol_obj.get_sol_ids_from_pol_ids(cr, uid, [obj.order_line_id_kit_selection.id], context=context)
                assert len(sol_ids) <= 1, 'kit selection purchase line: the number of corresponding sale order line is greater than 1: %s'%len(sol_ids)
            # true if we get some sale order lines
            result[obj.id].update({'corresponding_so_line_id_kit_selection': sol_ids and sol_ids[0] or False})
            # corresponding_so_id_kit_selection
            so_id = False
            if sol_ids:
                datas = sol_obj.read(cr, uid, sol_ids, ['order_id'], context=context)
                for data in datas:
                    if data['order_id']:
                        so_id = data['order_id'][0]
            # write the value
            result[obj.id].update({'corresponding_so_id_kit_selection': so_id})
        return result
    
    _columns = {'product_id': fields.many2one('product.product', string='Kit Product', readonly=True),
                'kit_id': fields.many2one('composition.kit', string='Theoretical Kit'),
                'order_line_id_kit_selection': fields.many2one('purchase.order.line', string='Purchase Order Line', readonly=True, required=True),
                'impact_so_kit_selection': fields.boolean('Impact Field Order', help='Impact corresponding Field Order by creating a corresponding Field Order line.'),
                # one2many
                'product_ids_kit_selection': fields.one2many('kit.selection.line', 'wizard_id_kit_selection_line', string='Replacement Products'),
                # related fields
                'partner_id_kit_selection': fields.related('order_line_id_kit_selection', 'order_id', 'partner_id', string='Partner', type='many2one', relation='res.partner', readonly=True),
                'pricelist_id_kit_selection': fields.related('order_line_id_kit_selection', 'order_id', 'pricelist_id', string='Currency', type='many2one', relation='product.pricelist', readonly=True),
                'warehouse_id_kit_selection': fields.related('order_line_id_kit_selection', 'order_id', 'warehouse_id', string='Warehouse', type='many2one', relation='stock.warehouse', readonly=True),
                # function fields
                'corresponding_so_line_id_kit_selection': fields.function(_vals_get, method=True, type='many2one', relation='sale.order.line', string='Corresponding Fo Line', multi='get_vals_kit_selection', store=False, readonly=True),
                'corresponding_so_id_kit_selection': fields.function(_vals_get, method=True, type='many2one', relation='sale.order', string='Corresponding Fo', multi='get_vals_kit_selection', store=False, readonly=True),
                }
    
    _defaults = {'product_id': lambda s, cr, uid, c: c.get('product_id', False),
                 'order_line_id_kit_selection': lambda s, cr, uid, c: c.get('active_ids') and c.get('active_ids')[0] or False,
                 'impact_so_kit_selection': True,
                 }
    
    def import_items(self, cr, uid, ids, context=None):
        '''
        import lines into product_ids_kit_selection
        '''
        # objects
        line_obj = self.pool.get('kit.selection.line')
        # purchase order line id
        pol_ids = context['active_ids']
        
        for obj in self.browse(cr, uid, ids, context=context):
            if not obj.kit_id:
                raise osv.except_osv(_('Warning !'), _('A theoretical version should be selected.'))
            if obj.kit_id.state != 'completed':
                raise osv.except_osv(_('Warning !'), _('The theoretical version must be completed.'))
            for item in obj.kit_id.composition_item_ids:
                values = {'order_line_id_kit_selection_line': obj.order_line_id_kit_selection.id,
                          'wizard_id_kit_selection_line': obj.id,
                          'product_id_kit_selection_line': item.item_product_id.id,
                          'qty_kit_selection_line': item.item_qty,
                          'uom_id_kit_selection_line': item.item_uom_id.id,
                          }
                line_obj.create(cr, uid, values, context=dict(context, pol_ids=context['active_ids']))
        return self.pool.get('wizard').open_wizard(cr, uid, pol_ids, type='update', context=context)
    
    def validate_lines(self, cr, uid, ids, context=None):
        '''
        validate the lines
        
        - qty > 0.0 (must_be_greater_than_0)
        - unit price > 0.0
        
        return True or False
        '''
        # objects
        prod_obj = self.pool.get('product.product')
        lot_obj = self.pool.get('stock.production.lot')
        # errors
        errors = {'must_be_greater_than_0': False,
                  'price_must_be_greater_than_0': False,
                  }
        for obj in self.browse(cr, uid, ids, context=context):
            for item in obj.product_ids_kit_selection:
                # reset the integrity status
                item.write({'integrity_status': 'empty'}, context=context)
                # qty
                if item.qty_kit_selection_line <= 0.0:
                    # qty is needed
                    errors.update(must_be_greater_than_0=True)
                    item.write({'integrity_status': 'must_be_greater_than_0'}, context=context)
                # unit price
                if item.price_unit_kit_selection_line <= 0.0:
                    # unit price is needed
                    errors.update(price_must_be_greater_than_0=True)
                    item.write({'integrity_status': 'price_must_be_greater_than_0'}, context=context)
        # check the encountered errors
        return all([not x for x in errors.values()])

    def do_de_kitting(self, cr, uid, ids, context=None):
        '''
        create a purchase order line for each kit item and delete the selected kit purchase order line
        '''
        # quick integrity check
        assert context, 'No context defined, problem on method call'
        if isinstance(ids, (int, long)):
            ids = [ids]
        # objects
        wf_service = netsvc.LocalService("workflow")
        pol_obj = self.pool.get('purchase.order.line')
        so_obj = self.pool.get('sale.order')
        sol_obj = self.pool.get('sale.order.line')
        # id of corresponding purchase order line
        pol_ids = context['active_ids']
        pol_id = context['active_ids'][0]
        pol = pol_obj.browse(cr, uid, pol_id, context=context)
        # integrity constraint
        integrity_check = self.validate_lines(cr, uid, ids, context=context)
        if not integrity_check:
            # the windows must be updated to trigger tree colors
            return self.pool.get('wizard').open_wizard(cr, uid, pol_ids, type='update', context=context)
        # process
        ctx_keep_info = context.copy()
        ctx_keep_info['keepDateAndDistrib'] = True
        for obj in self.browse(cr, uid, ids, context=context):
            if not len(obj.product_ids_kit_selection):
                raise osv.except_osv(_('Warning !'), _('Replacement Items must be selected.'))
            # to keep a link to previous line (for copy) and as a flag to write in the first loop
            last_line_id = False
            # for each item from the product_ids_kit_selection
            for item_v in obj.product_ids_kit_selection:
                # price unit is mandatory
                if item_v.price_unit_kit_selection_line <= 0.0:
                    raise osv.except_osv(_('Warning !'), _('Unit Price must be specified for each line.'))
                # selected product_id
                product_id = item_v.product_id_kit_selection_line.id
                # selected qty
                qty = item_v.qty_kit_selection_line
                if qty <= 0.0:
                    raise osv.except_osv(_('Warning !'), _('Quantity must be greater than 0.0.'))
                # selected uom
                uom_id = item_v.uom_id_kit_selection_line.id
                # price unit
                price_unit = item_v.price_unit_kit_selection_line
                # call purchase order line on change function
                data = self.pool.get('kit.selection.line')._call_pol_on_change(cr, uid, ids, product_id, qty, uom_id, price_unit,
                                                                               type='product_id_change',
                                                                               context=dict(context, pol_ids=context['active_ids']))
                # common dictionary of data
                values = {'product_id': product_id,
                          'price_unit': item_v.price_unit_kit_selection_line,
                          'product_uom': uom_id,
                          'default_code': data['value']['default_code'],
                          'name': data['value']['name'],
                          'default_name': data['value']['default_name'],
                          }
                # if we are treating a line with link to so
                # if Internal Request, we do not update corresponding Internal Request
                if obj.corresponding_so_line_id_kit_selection and obj.impact_so_kit_selection and not obj.corresponding_so_id_kit_selection.procurement_request:
                    # if we have already update the existing pol, we create a new sol
                    # an go through the whole process
                    # if not, we simply update the pol, corresponding sol will be updated
                    # when the pol is confirmed
                    if last_line_id:
                        # we create a Fo line by copying related Fo line. we then execute procurement creation function, and process the procurement
                        # the merge into the actual Po is forced
                        # copy the original sale order line, reset po_cft to 'po' (we don't want a new tender if any)
                        values.update({'line_number': obj.corresponding_so_line_id_kit_selection.line_number, # the Fo is not draft anyway (sourced), following sequencing policy, split Fo line maintains original one
                                       'po_cft': 'po',
                                       'product_uom_qty': pol.product_qty*qty,
                                       'product_uom': uom_id,
                                       'product_uos_qty': pol.product_qty*qty,
                                       'product_uos': uom_id,
                                       'so_back_update_dest_po_id_sale_order_line': obj.order_line_id_kit_selection.order_id.id,
                                       'so_back_update_dest_pol_id_sale_order_line': obj.order_line_id_kit_selection.id,
                                       })
                        # copy existing sol
                        last_line_id = sol_obj.copy(cr, uid, last_line_id, values, context=ctx_keep_info)
                        # call the new procurement creation method
                        so_obj.action_ship_proc_create(cr, uid, [obj.corresponding_so_id_kit_selection.id], context=context)
                        # run the procurement, the make_po function detects the link to original po
                        # and force merge the line to this po (even if it is not draft anymore)
                        new_data_so = sol_obj.read(cr, uid, [last_line_id], ['procurement_id'], context=context)
                        new_proc_id = new_data_so[0]['procurement_id'][0]
                        wf_service.trg_validate(uid, 'procurement.order', new_proc_id, 'button_check', cr)
                        # if original po line is confirmed, we action_confirm new line
                        if obj.order_line_id_kit_selection.state == 'confirmed':
                            # the correct line number according to new line number policy is set in po_line_values_hook of order_line_number/order_line_number.py/procurement_order
                            new_po_ids = pol_obj.search(cr, uid, [('procurement_id', '=', new_proc_id)], context=context)
                            pol_obj.action_confirm(cr, uid, new_po_ids, context=context)
                    else:
                        # first item to be treated, we update the existing purchase order line
                        # sale order line will be updated when the Po is confirmed
                        last_line_id = obj.corresponding_so_line_id_kit_selection.id
                        # update values for pol structure
                        values.update({'product_qty': pol.product_qty*qty})
                        pol_obj.write(cr, uid, [obj.order_line_id_kit_selection.id], values, context=context)
                else:
                    # no link to so, or no impact desired
                    # create a new pol
                    # update values for pol structure
                    values.update({'product_qty': pol.product_qty*qty})
                    # following new sequencing policy, we check if resequencing occur (behavior 1).
                    # if not (behavior 2), the split line keeps the same line number as original line
                    if not pol_obj.allow_resequencing(cr, uid, [obj.order_line_id_kit_selection.id], context=context):
                        # set default value for line_number as the same as original line
                        values.update({'line_number': obj.order_line_id_kit_selection.line_number})
                    
                    if last_line_id:
                        # the existing purchase order line has already been updated, we create a new one
                        # copy the original purchase order line
                        last_line_id = pol_obj.copy(cr, uid, last_line_id, values, context=ctx_keep_info)
                        # if original po line is confirmed, we action_confirm new line
                        if obj.order_line_id_kit_selection.state == 'confirmed':
                            pol_obj.action_confirm(cr, uid, [last_line_id], context=context)
                    else:
                        # first item to be treated, we update the existing line
                        last_line_id = obj.order_line_id_kit_selection.id
                        pol_obj.write(cr, uid, [last_line_id], values, context=context)
                
        return {'type': 'ir.actions.act_window_close'}
    
kit_selection()


class kit_selection_line(osv.osv_memory):
    '''
    substitute items
    '''
    _name = 'kit.selection.line'
    
    def create(self, cr, uid, vals, context=None):
        '''
        default price unit from pol on_change function
        '''
        # objects
        pol_obj = self.pool.get('purchase.order.line')
        # id of corresponding purchase order line
        pol_id = context.get('active_ids', False) and context['active_ids'][0]
        if pol_id and ('price_unit_kit_selection_line' not in vals or vals.get('price_unit_kit_selection_line') == 0.0):
            pol = pol_obj.browse(cr, uid, pol_id, context=context)
            # selected product_id
            product_id = vals.get('product_id_kit_selection_line', False)
            # selected qty
            qty = vals.get('qty_kit_selection_line', 0.0)
            # selected uom
            uom_id = vals.get('uom_id_kit_selection_line', False)
            # price unit
            price_unit = vals.get('price_unit_kit_selection_line', 0.0)
            # gather default values
            data = self._call_pol_on_change(cr, uid, context['active_ids'],
                                            product_id, qty, uom_id, price_unit, type='product_id_on_change',
                                            context=dict(context, pol_ids=context['active_ids']))
            # update price_unit value
            vals.update({'price_unit_kit_selection_line': data['value']['price_unit']})
        return super(kit_selection_line, self).create(cr, uid, vals, context=context)
    
    def _call_pol_on_change(self, cr, uid, ids, product_id, qty, uom_id, price_unit, type, context=None):
        '''
        core function from purchase order line
        
        def product_id_change(self, cr, uid, ids, pricelist, product, qty, uom,
            partner_id, date_order=False, fiscal_position=False, date_planned=False,
            name=False, price_unit=False, notes=False):
        '''
        # quick integrity check
        assert context, 'No context defined, problem on method call'
        if isinstance(ids, (int, long)):
            ids = [ids]
            
        # objects
        pol_obj = self.pool.get('purchase.order.line')
        prod_obj = self.pool.get('product.product')
        # id of corresponding purchase order line
        pol_id = context['pol_ids'][0]
        pol = pol_obj.browse(cr, uid, pol_id, context=context)
        # pricelist from purchase order
        pricelist_id = pol.order_id.pricelist_id.id
        # partner_id from purchase order
        partner_id = pol.order_id.partner_id.id
        # date_order from purchase order
        date_order = pol.order_id.date_order
        # fiscal_position from purchase order
        fiscal_position_id = pol.order_id.fiscal_position.id
        # date_planned from purchase order line
        date_planned = pol.date_planned
        # name
        name = False
        if product_id:
            name = prod_obj.read(cr, uid, product_id, ['name'], context=context)['name']
        # notes
        notes = pol.notes
        # state
        state = pol.order_id.state
        # gather default values
        if type in ['product_id_change', 'product_uom_change']:
            data = getattr(pol_obj, type)(cr, uid, ids, pricelist=pricelist_id, product=product_id, qty=qty, uom=uom_id,
                                          partner_id=partner_id, date_order=date_order, fiscal_position=fiscal_position_id, date_planned=date_planned,
                                          name=name, price_unit=price_unit, notes=notes)
        elif type == 'product_id_on_change':
            data = getattr(pol_obj, type)(cr, uid, ids, pricelist=pricelist_id, product=product_id,
                                          qty=qty, uom=uom_id, partner_id=partner_id, date_order=date_order,
                                          fiscal_position=fiscal_position_id, date_planned=date_planned, name=name,
                                          price_unit=price_unit, notes=notes, state=state, old_price_unit=False)
        return data
    
    def on_product_id_change(self, cr, uid, ids, product_id, qty, uom_id, price_unit, context=None):
        '''
        core function from purchase order line
        
        def product_id_change(self, cr, uid, ids, pricelist, product, qty, uom,
            partner_id, date_order=False, fiscal_position=False, date_planned=False,
            name=False, price_unit=False, notes=False):
        '''
        # quick integrity check
        assert context, 'No context defined, problem on method call'
        if isinstance(ids, (int, long)):
            ids = [ids]
            
        # result
        result = {'value': {'qty_kit_selection_line': 0.0,
                            'uom_id_kit_selection_line': False,
                            'price_unit_kit_selection_line': 0.0}}
        # gather default values
#        data = self._call_pol_on_change(cr, uid, ids, product_id, qty, uom_id, price_unit, 'product_id_change', context=context)
        data = self._call_pol_on_change(cr, uid, ids, product_id, qty, uom_id, price_unit, 'product_id_on_change', context=context)
        # update result price_unit and default uom
        result['value'].update({'price_unit_kit_selection_line': 'price_unit' in data['value'] and data['value']['price_unit'] or 0.0,
                                'qty_kit_selection_line': 'product_qty' in data['value'] and data['value']['product_qty'] or 0.0,
                                'uom_id_kit_selection_line': 'product_uom' in data['value'] and data['value']['product_uom'] or False})
        # return result
        return result
    
    def on_uom_id_change(self, cr, uid, ids, product_id, qty, uom_id, price_unit, context=None):
        '''
        core function from purchase order line
        
        def product_uom_change(self, cr, uid, ids, pricelist, product, qty, uom,
            partner_id, date_order=False, fiscal_position=False, date_planned=False,
            name=False, price_unit=False, notes=False):
        '''
        # quick integrity check
        assert context, 'No context defined, problem on method call'
        if isinstance(ids, (int, long)):
            ids = [ids]
            
        # result
        result = {'value': {'qty_kit_selection_line': 0.0,
                            'price_unit_kit_selection_line': 0.0}}
        # gather default values
        data = self._call_pol_on_change(cr, uid, ids, product_id, qty, uom_id, price_unit, 'product_uom_change', context=context)
        # update result price_unit - qty
        result['value'].update({'price_unit_kit_selection_line': 'price_unit' in data['value'] and data['value']['price_unit'] or 0.0,
                                'qty_kit_selection_line': 'product_qty' in data['value'] and data['value']['product_qty'] or 0.0})
        # return result
        return result
    
    _columns = {'integrity_status': fields.selection(string=' ', selection=INTEGRITY_STATUS_SELECTION, readonly=True),
                'order_line_id_kit_selection_line': fields.many2one('purchase.order.line', string="Purchase Order Line", readonly=True, required=True),
                'wizard_id_kit_selection_line': fields.many2one('kit.selection', string='Kit Selection wizard'),
                # data
                'product_id_kit_selection_line': fields.many2one('product.product', string='Product', required=True),
                'qty_kit_selection_line': fields.float(string='Qty', digits_compute=dp.get_precision('Product UoM'), required=True),
                'uom_id_kit_selection_line': fields.many2one('product.uom', string='UoM', required=True),
                'price_unit_kit_selection_line': fields.float('Unit Price', required=True, digits_compute=dp.get_precision('Purchase Price')),
                }
    
    _defaults = {'integrity_status': 'empty',
                 }
    
kit_selection_line()