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

« back to all changes in this revision

Viewing changes to consumption_calculation/consumption_calculation.py

  • Committer: Quentin THEURET
  • Date: 2011-11-04 13:41:36 UTC
  • mto: (367.10.3 uf-514)
  • mto: This revision was merged to the branch mainline in revision 410.
  • Revision ID: qt@tempo-consulting.fr-20111104134136-ash50b8tvbvrz0un
UF-513 [IMP] Added the confirmation of the stock move at the confirmation of the consumption report
UF-624 [IMP] Included the generated stock moves in an outgoing delivery and confirm id
UF-624 [IMP] Added a name and a sequence to the consumption report to have this name in the outgoing delivery name and origin

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 
28
28
import time
29
29
import base64
 
30
import netsvc
30
31
 
31
32
 
32
33
class real_average_consumption(osv.osv):
33
34
    _name = 'real.average.consumption'
34
35
    _description = 'Real Average Consumption'
35
 
    _rec_name = 'period_from'
36
36
    
37
37
    def _get_nb_lines(self, cr, uid, ids, field_name, args, context={}):
38
38
        '''
46
46
        return res
47
47
    
48
48
    _columns = {
 
49
        'name': fields.char(size=64, string='Reference'),
49
50
        'creation_date': fields.date(string='Creation date'),
50
51
        'cons_location_id': fields.many2one('stock.location', string='Consumer location', domain=[('usage', '=', 'internal')], required=True),
51
52
        'activity_id': fields.many2one('stock.location', string='Activity', domain=[('usage', '=', 'customer')]),
54
55
        'sublist_id': fields.many2one('product.list', string='List/Sublist'),
55
56
        'nomen_id': fields.many2one('product.nomenclature', string='Products\' nomenclature level'),
56
57
        'line_ids': fields.one2many('real.average.consumption.line', 'rac_id', string='Lines'),
 
58
        'picking_id': fields.many2one('stock.picking', string='Picking', readonly=True),
57
59
        'valid_ok': fields.boolean(string='Create and process out moves'),
58
60
        'created_ok': fields.boolean(string='Out moves created'),
59
61
        'nb_lines': fields.function(_get_nb_lines, method=True, type='integer', string='# lines', readonly=True,),
60
62
    }
61
63
    
62
64
    _defaults = {
 
65
        'name': lambda obj, cr, uid, context: obj.pool.get('ir.sequence').get(cr, uid, 'consumption.report'),
63
66
        'creation_date': lambda *a: time.strftime('%Y-%m-%d'),
64
67
        'activity_id': lambda obj, cr, uid, context: obj.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'stock_location_internal_cust')[1],
65
68
        'period_to': lambda *a: time.strftime('%Y-%m-%d'),
90
93
        
91
94
        move_obj = self.pool.get('stock.move')
92
95
        line_obj = self.pool.get('real.average.consumption.line')
 
96
        wf_service = netsvc.LocalService("workflow")
93
97
        
94
98
        reason_type_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'reason_types_moves', 'reason_type_consumption_report')[1]
 
99
 
 
100
        move_ids = []
95
101
        
96
102
        for rac in self.browse(cr, uid, ids, context=context):
97
103
            if not rac.valid_ok:
98
104
                raise osv.except_osv(_('Error'), _('Please check the last checkbox before processing the lines'))
99
105
            if rac.created_ok:
100
106
                return {'type': 'ir.actions.close_window'}
101
 
            
 
107
 
 
108
            picking_id = self.pool.get('stock.picking').create(cr, uid, {'name': 'OUT-%s' % rac.name,
 
109
                                                                         'origin': rac.name,
 
110
                                                                         'type': 'out',
 
111
                                                                         'state': 'auto',
 
112
                                                                         'move_type': 'one',
 
113
                                                                         'invoice_state': 'none',
 
114
                                                                         'date': time.strftime('%Y-%m-%d %H:%M:%S'),
 
115
                                                                         'reason_type_id': reason_type_id}, context=context)
 
116
 
102
117
            for line in rac.line_ids:
103
 
                move_id = move_obj.create(cr, uid, {'name': 'RAC/%s' % (line.product_id.name),
 
118
                move_id = move_obj.create(cr, uid, {'name': '%s/%s' % (rac.name, line.product_id.name),
 
119
                                                    'picking_id': picking_id,
104
120
                                                    'product_uom': line.uom_id.id,
105
121
                                                    'product_id': line.product_id.id,
106
122
                                                    'date_expected': rac.period_to,
107
123
                                                    'date': rac.creation_date,
108
124
                                                    'product_qty': line.consumed_qty,
 
125
                                                    'prodlot_id': line.prodlot_id.id,
 
126
                                                    'expiry_date': line.expiry_date,
109
127
                                                    'location_id': rac.cons_location_id.id,
110
128
                                                    'location_dest_id': rac.activity_id.id,
111
129
                                                    'state': 'done',
112
130
                                                    'reason_type_id': reason_type_id})
 
131
                move_ids.append(move_id)
113
132
                line_obj.write(cr, uid, [line.id], {'move_id': move_id})
114
 
                
115
 
                
116
 
                
117
 
            self.write(cr, uid, [rac.id], {'created_ok': True}, context=context)
 
133
 
 
134
            # Confirm the picking
 
135
            wf_service.trg_validate(uid, 'stock.picking', picking_id, 'button_confirm', cr)
 
136
 
 
137
            # Confirm all moves
 
138
            move_obj.action_done(cr, uid, move_ids, context=context)
 
139
            
 
140
            self.write(cr, uid, [rac.id], {'created_ok': True, 'picking_id': picking_id}, context=context)
118
141
        
119
142
        return {'type': 'ir.actions.act_window',
120
143
                'res_model': 'real.average.consumption',