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'
37
37
def _get_nb_lines(self, cr, uid, ids, field_name, args, context={}):
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,),
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'),
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")
94
98
reason_type_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'reason_types_moves', 'reason_type_consumption_report')[1]
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'}
108
picking_id = self.pool.get('stock.picking').create(cr, uid, {'name': 'OUT-%s' % rac.name,
113
'invoice_state': 'none',
114
'date': time.strftime('%Y-%m-%d %H:%M:%S'),
115
'reason_type_id': reason_type_id}, context=context)
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,
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})
117
self.write(cr, uid, [rac.id], {'created_ok': True}, context=context)
134
# Confirm the picking
135
wf_service.trg_validate(uid, 'stock.picking', picking_id, 'button_confirm', cr)
138
move_obj.action_done(cr, uid, move_ids, context=context)
140
self.write(cr, uid, [rac.id], {'created_ok': True, 'picking_id': picking_id}, context=context)
119
142
return {'type': 'ir.actions.act_window',
120
143
'res_model': 'real.average.consumption',