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

« back to all changes in this revision

Viewing changes to procurement_request/procurement_request.py

UF-663: [IMP] first version

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
from tools.translate import _
24
24
import decimal_precision as dp
25
25
 
26
 
from sale_override import SALE_ORDER_STATE_SELECTION
27
 
 
28
26
class procurement_request(osv.osv):
29
27
    _name = 'sale.order'
30
28
    _inherit = 'sale.order'
70
68
    #@@@end override
71
69
    
72
70
    _columns = {
73
 
        'requestor': fields.char(size=128, string='Requestor', states={'draft': [('readonly', False)]}, readonly=True),
74
 
        'procurement_request': fields.boolean(string='Internal Request', readonly=True),
75
 
        'requested_date': fields.date(string='Requested date', states={'draft': [('readonly', False)]}, readonly=True),
76
 
        'warehouse_id': fields.many2one('stock.warehouse', string='Warehouse', states={'draft': [('readonly', False)]}, readonly=True),
77
 
        'origin': fields.char(size=64, string='Origin', states={'draft': [('readonly', False)]}, readonly=True),
 
71
        'requestor': fields.char(size=128, string='Requestor'),
 
72
        'procurement_request': fields.boolean(string='Procurement Request', readonly=True),
 
73
        'requested_date': fields.date(string='Requested date'),
 
74
        'warehouse_id': fields.many2one('stock.warehouse', string='Warehouse'),
 
75
        'origin': fields.char(size=64, string='Origin'),
78
76
        'notes': fields.text(string='Notes'),
79
77
        'order_ids': fields.many2many('purchase.order', 'procurement_request_order_rel',
80
78
                                      'request_id', 'order_id', string='Orders', readonly=True),
81
79
        
82
80
        # Remove readonly parameter from sale.order class
83
 
        'order_line': fields.one2many('sale.order.line', 'order_id', 'Order Lines', readonly=True, states={'draft': [('readonly', False)]}),
 
81
        'order_line': fields.one2many('sale.order.line', 'order_id', 'Order Lines', readonly=True, states={'procurement': [('readonly', False)], 'draft': [('readonly', False)]}),
84
82
        'amount_untaxed': fields.function(_amount_all, method=True, digits_compute= dp.get_precision('Sale Price'), string='Untaxed Amount',
85
83
            store = {
86
 
                'sale.order': (lambda self, cr, uid, ids, c=None: ids, ['order_line'], 10),
 
84
                'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 10),
87
85
                'sale.order.line': (_get_order, ['price_unit', 'tax_id', 'discount', 'product_uom_qty'], 10),
88
86
            },
89
87
            multi='sums', help="The amount without tax."),
90
88
        'amount_tax': fields.function(_amount_all, method=True, digits_compute= dp.get_precision('Sale Price'), string='Taxes',
91
89
            store = {
92
 
                'sale.order': (lambda self, cr, uid, ids, c=None: ids, ['order_line'], 10),
 
90
                'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 10),
93
91
                'sale.order.line': (_get_order, ['price_unit', 'tax_id', 'discount', 'product_uom_qty'], 10),
94
92
            },
95
93
            multi='sums', help="The tax amount."),
96
94
        'amount_total': fields.function(_amount_all, method=True, digits_compute= dp.get_precision('Sale Price'), string='Total',
97
95
            store = {
98
 
                'sale.order': (lambda self, cr, uid, ids, c=None: ids, ['order_line'], 10),
 
96
                'sale.order': (lambda self, cr, uid, ids, c={}: ids, ['order_line'], 10),
99
97
                'sale.order.line': (_get_order, ['price_unit', 'tax_id', 'discount', 'product_uom_qty'], 10),
100
98
            },
101
99
            multi='sums', help="The total amount."),
102
 
        'state': fields.selection(SALE_ORDER_STATE_SELECTION, 'Order State', readonly=True, help="Gives the state of the quotation or sales order. \nThe exception state is automatically set when a cancel operation occurs in the invoice validation (Invoice Exception) or in the picking list process (Shipping Exception). \nThe 'Waiting Schedule' state is set when the invoice is confirmed but waiting for the scheduler to run on the date 'Ordered Date'.", select=True),
 
100
        'state': fields.selection([
 
101
            ('procurement', 'Internal Supply Requirement'),
 
102
            ('draft', 'Quotation'),
 
103
            ('waiting_date', 'Waiting Schedule'),
 
104
            ('manual', 'Manual In Progress'),
 
105
            ('progress', 'In Progress'),
 
106
            ('shipping_except', 'Shipping Exception'),
 
107
            ('invoice_except', 'Invoice Exception'),
 
108
            ('done', 'Done'),
 
109
            ('cancel', 'Cancelled')
 
110
            ], 'Order State', readonly=True, help="Gives the state of the quotation or sales order. \nThe exception state is automatically set when a cancel operation occurs in the invoice validation (Invoice Exception) or in the picking list process (Shipping Exception). \nThe 'Waiting Schedule' state is set when the invoice is confirmed but waiting for the scheduler to run on the date 'Ordered Date'.", select=True),
103
111
    }
104
112
    
105
113
    _defaults = {
106
 
        'name': lambda obj, cr, uid, context: not context.get('procurement_request', False) and obj.pool.get('ir.sequence').get(cr, uid, 'sale.order') or obj.pool.get('ir.sequence').get(cr, uid, 'procurement.request'),
 
114
        'name': lambda obj, cr, uid, context: not context.get('procurement_request', False) and obj.pool.get('ir.sequence').get(cr, uid, 'sale.order') or '',
107
115
        'procurement_request': lambda obj, cr, uid, context: context.get('procurement_request', False),
108
 
        'state': 'draft',
109
 
        'warehouse_id': lambda obj, cr, uid, context: len(obj.pool.get('stock.warehouse').search(cr, uid, [])) and obj.pool.get('stock.warehouse').search(cr, uid, [])[0],
 
116
        'state': lambda self, cr, uid, c: c.get('procurement_request', False) and 'procurement' or 'draft',
110
117
    }
111
118
 
112
 
    def create(self, cr, uid, vals, context=None):
 
119
    def create(self, cr, uid, vals, context={}):
113
120
        if not context:
114
121
            context = {}
115
122
 
116
 
        if context.get('procurement_request') or vals.get('procurement_request', False):
 
123
        if context.get('procurement_request'):
117
124
            # Get the ISR number
118
125
            if not vals.get('name', False):
119
126
                vals.update({'name': self.pool.get('ir.sequence').get(cr, uid, 'procurement.request')})
133
140
 
134
141
        return super(procurement_request, self).create(cr, uid, vals, context)
135
142
 
136
 
    def unlink(self, cr, uid, ids, context=None):
 
143
    def unlink(self, cr, uid, ids, context={}):
137
144
        '''
138
145
        Changes the state of the order to allow the deletion
139
146
        '''
143
150
        normal_ids = []
144
151
        
145
152
        for request in self.browse(cr, uid, ids, context=context):
146
 
            if request.procurement_request and request.state in ['draft', 'cancel']:
 
153
            if request.procurement_request and request.state in ['procurement', 'cancel']:
147
154
                del_ids.append(request.id)
148
155
            elif not request.procurement_request:
149
156
                normal_ids.append(request.id)
150
157
            else:
151
 
                raise osv.except_osv(_('Invalid action !'), _('Cannot delete Internal Request(s) which are already validated !'))
 
158
                raise osv.except_osv(_('Invalid action !'), _('Cannot delete Procurement Request(s) which are already confirmed !'))
152
159
                
153
160
        if del_ids:
154
161
            osv.osv.unlink(self, cr, uid, del_ids, context=context)
155
162
                
156
163
        return super(procurement_request, self).unlink(cr, uid, normal_ids, context=context)
157
164
    
158
 
    def search(self, cr, uid, args=None, offset=0, limit=None, order=None, context=None, count=False):
 
165
    def search(self, cr, uid, args=[], offset=0, limit=None, order=None, context={}, count=False):
159
166
        '''
160
167
        Adds automatically a domain to search only True sale orders if no procurement_request in context
161
168
        '''
162
169
        test = True
163
 
        if args is None:
164
 
            args = []
165
 
        if context is None:
166
 
            context = {}
167
170
        for a in args:
168
171
            if a[0] == 'procurement_request':
169
172
                test = False
193
196
        })
194
197
        
195
198
        return super(osv.osv, self).copy(cr, uid, id, default, context=context)
196
 
 
197
 
    def wkf_action_cancel(self, cr, uid, ids, context=None):
198
 
        '''
199
 
        Cancel the procurement request and all lines
200
 
        '''
201
 
        line_ids = []
202
 
        for req in self.browse(cr, uid, ids, context=context):
203
 
            for line in req.order_line:
204
 
                if line.id not in line_ids:
205
 
                    line_ids.append(line.id)
206
 
 
207
 
        self.write(cr, uid, ids, {'state': 'cancel'}, context=context)
208
 
        self.pool.get('sale.order.line').write(cr, uid, line_ids, {'state': 'cancel'}, context=context)
209
 
 
210
 
        return True
211
 
 
212
 
    def validate_procurement(self, cr, uid, ids, context=None):
213
 
        '''
214
 
        Validate the request
215
 
        '''
216
 
        for req in self.browse(cr, uid, ids, context=context):
217
 
            if len(req.order_line) <= 0:
218
 
                raise osv.except_osv(_('Error'), _('You cannot validate an Internal request with no lines !'))
219
 
        self.write(cr, uid, ids, {'state': 'validated'}, context=context)
220
 
 
221
 
        return True
222
199
    
223
 
    def confirm_procurement(self, cr, uid, ids, context=None):
 
200
    def confirm_procurement(self, cr, uid, ids, context={}):
224
201
        '''
225
202
        Confirmed the request
226
203
        '''
227
 
        if context is None:
228
 
            context = {}
229
 
 
230
 
        self.write(cr, uid, ids, {'state': 'progress'}, context=context)
231
 
 
232
 
        for request in self.browse(cr, uid, ids, context=context):
233
 
            message = _("The internal request '%s' has been confirmed.") %(request.name,)
234
 
            proc_view = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'procurement_request', 'procurement_request_form_view')
235
 
            context.update({'view_id': proc_view and proc_view[1] or False})
236
 
            self.log(cr, uid, request.id, message, context=context)
 
204
        self.write(cr, uid, ids, {'state': 'progress'})
237
205
        
 
206
        return True
 
207
    
 
208
    def procurement_done(self, cr, uid, ids, context={}):
 
209
        '''
 
210
        Creates all procurement orders according to lines
 
211
        '''
238
212
        self.action_ship_create(cr, uid, ids, context=context)
239
 
        
240
 
        return True
241
 
    
242
 
    def procurement_done(self, cr, uid, ids, context=None):
243
 
        '''
244
 
        Creates all procurement orders according to lines
245
 
        '''
246
213
        self.write(cr, uid, ids, {'state': 'done'})
247
214
        
248
215
        return True
253
220
    _name = 'sale.order.line'
254
221
    _inherit= 'sale.order.line'
255
222
    
256
 
    def _amount_line(self, cr, uid, ids, field_name, arg, context=None):
 
223
    def _amount_line(self, cr, uid, ids, field_name, arg, context={}):
257
224
        '''
258
225
        Override the method to return 0.0 if the line is a procurement request line
259
226
        '''
269
236
        
270
237
        return res
271
238
    
272
 
    def create(self, cr, uid, vals, context=None):
 
239
    def create(self, cr, uid, vals, context={}):
273
240
        '''
274
241
        Adds the date_planned value
275
242
        '''
276
 
        if context is None:
277
 
            context = {}
278
 
 
279
 
        if not 'date_planned' in vals and context.get('procurement_request'):
 
243
        if not 'date_planned' in vals:
280
244
            if 'date_planned' in context:
281
245
                vals.update({'date_planned': context.get('date_planned')})
282
246
            else:
286
250
        return super(procurement_request_line, self).create(cr, uid, vals, context=context)
287
251
    
288
252
    _columns = {
289
 
        'procurement_request': fields.boolean(string='Internal Request', readonly=True),
 
253
        'procurement_request': fields.boolean(string='Procurement Request', readonly=True),
290
254
        'latest': fields.char(size=64, string='Latest documents', readonly=True),
291
255
        'price_subtotal': fields.function(_amount_line, method=True, string='Subtotal', digits_compute= dp.get_precision('Sale Price')),
292
 
        'my_company_id': fields.many2one('res.company','Company',select=1),
293
 
        'supplier': fields.many2one('res.partner', 'Supplier', domain="[('id', '!=', my_company_id)]"),
294
256
    }
295
257
    
296
 
    def _get_planned_date(self, cr, uid, c=None):
297
 
        if c is None:
298
 
            c = {}
299
 
        if 'procurement_request' in c:
300
 
            return c.get('date_planned', False)
301
 
 
302
 
        return super(procurement_request_line, self)._get_planned_date(cr, uid, c)
303
 
 
304
258
    _defaults = {
305
259
        'procurement_request': lambda self, cr, uid, c: c.get('procurement_request', False),
306
 
        'date_planned': _get_planned_date,
307
 
        'my_company_id': lambda obj, cr, uid, context: obj.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id,
 
260
        'date_planned': lambda self, cr, uid, c: c.get('date_planned', False),
308
261
    }
309
262
    
310
 
    def requested_product_id_change(self, cr, uid, ids, product_id, type, context=None):
 
263
    def requested_product_id_change(self, cr, uid, ids, product_id, type, context={}):
311
264
        '''
312
265
        Fills automatically the product_uom_id field on the line when the 
313
266
        product was changed.
314
267
        '''
315
 
        if context is None:
316
 
            context = {}
317
268
        product_obj = self.pool.get('product.product')
318
269
 
319
270
        v = {}
329
280
    
330
281
procurement_request_line()
331
282
 
332
 
class purchase_order(osv.osv):
333
 
    _name = 'purchase.order'
334
 
    _inherit = 'purchase.order'
335
 
    
336
 
    def _hook_action_picking_create_modify_out_source_loc_check(self, cr, uid, ids, context=None, *args, **kwargs):
337
 
        '''
338
 
        Please copy this to your module's method also.
339
 
        This hook belongs to the action_picking_create method from purchase>purchase.py>purchase_order class
340
 
        
341
 
        - allow to choose whether or not the source location of the corresponding outgoing stock move should
342
 
        match the destination location of incoming stock move
343
 
        '''
344
 
        order_line = kwargs['order_line']
345
 
        move_id = kwargs['move_id']
346
 
        proc_obj = self.pool.get('procurement.order')
347
 
        move_obj = self.pool.get('stock.move')
348
 
        sale_line_obj = self.pool.get('sale.order.line')
349
 
        if order_line.move_dest_id:
350
 
            proc_ids = proc_obj.search(cr, uid, [('move_id', '=', order_line.move_dest_id.id)], context=context)
351
 
            so_line_ids = sale_line_obj.search(cr, uid, [('procurement_id', 'in', proc_ids)], context=context)
352
 
            if all(not line.order_id or line.order_id.procurement_request for line in sale_line_obj.browse(cr, uid, so_line_ids, context=context)):
353
 
                for proc in proc_obj.browse(cr, uid, proc_ids, context=context):
354
 
                    move_obj.write(cr, uid, [proc.move_id.id], {'state': 'draft'}, context=context)
355
 
                    move_obj.unlink(cr, uid, [proc.move_id.id], context=context)
356
 
                    proc_obj.write(cr, uid, [proc.id], {'move_id': move_id}, context=context)
357
 
                    
358
 
        return super(purchase_order, self)._hook_action_picking_create_modify_out_source_loc_check(cr, uid, ids, context, *args, **kwargs)
359
 
    
360
 
purchase_order()
361
 
 
362
283
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: