1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
22 from openerp import netsvc
24 from openerp.osv import fields, osv
26 class make_procurement(osv.osv_memory):
27 _name = 'make.procurement'
28 _description = 'Make Procurements'
30 def onchange_product_id(self, cr, uid, ids, prod_id):
31 """ On Change of Product ID getting the value of related UoM.
32 @param self: The object pointer.
33 @param cr: A database cursor
34 @param uid: ID of the user currently logged in
35 @param ids: List of IDs selected
36 @param prod_id: Changed ID of Product
37 @return: A dictionary which gives the UoM of the changed Product
39 product = self.pool.get('product.product').browse(cr, uid, prod_id)
40 return {'value': {'uom_id': product.uom_id.id}}
43 'qty': fields.float('Quantity', digits=(16,2), required=True),
44 'product_id': fields.many2one('product.product', 'Product', required=True, readonly=1),
45 'uom_id': fields.many2one('product.uom', 'Unit of Measure', required=True),
46 'warehouse_id': fields.many2one('stock.warehouse', 'Warehouse', required=True),
47 'date_planned': fields.date('Planned Date', required=True),
51 'date_planned': fields.date.context_today,
52 'qty': lambda *args: 1.0,
55 def make_procurement(self, cr, uid, ids, context=None):
56 """ Creates procurement order for selected product.
57 @param self: The object pointer.
58 @param cr: A database cursor
59 @param uid: ID of the user currently logged in
60 @param ids: List of IDs selected
61 @param context: A standard dictionary
62 @return: A dictionary which loads Procurement form view.
64 user = self.pool.get('res.users').browse(cr, uid, uid, context=context).login
65 wh_obj = self.pool.get('stock.warehouse')
66 procurement_obj = self.pool.get('procurement.order')
67 wf_service = netsvc.LocalService("workflow")
68 data_obj = self.pool.get('ir.model.data')
70 for proc in self.browse(cr, uid, ids, context=context):
71 wh = wh_obj.browse(cr, uid, proc.warehouse_id.id, context=context)
72 procure_id = procurement_obj.create(cr, uid, {
73 'name':'INT: '+str(user),
74 'date_planned': proc.date_planned,
75 'product_id': proc.product_id.id,
76 'product_qty': proc.qty,
77 'product_uom': proc.uom_id.id,
78 'location_id': wh.lot_stock_id.id,
79 'procure_method':'make_to_order',
82 wf_service.trg_validate(uid, 'procurement.order', procure_id, 'button_confirm', cr)
85 id2 = data_obj._get_id(cr, uid, 'procurement', 'procurement_tree_view')
86 id3 = data_obj._get_id(cr, uid, 'procurement', 'procurement_form_view')
89 id2 = data_obj.browse(cr, uid, id2, context=context).res_id
91 id3 = data_obj.browse(cr, uid, id3, context=context).res_id
95 'view_mode': 'tree,form',
96 'res_model': 'procurement.order',
97 'res_id' : procure_id,
98 'views': [(id3,'form'),(id2,'tree')],
99 'type': 'ir.actions.act_window',
102 def default_get(self, cr, uid, fields, context=None):
103 """ To get default values for the object.
104 @param self: The object pointer.
105 @param cr: A database cursor
106 @param uid: ID of the user currently logged in
107 @param fields: List of fields for which we want default values
108 @param context: A standard dictionary
109 @return: A dictionary which of fields with values.
113 record_id = context.get('active_id')
115 res = super(make_procurement, self).default_get(cr, uid, fields, context=context)
117 if record_id and 'product_id' in fields:
118 proxy = self.pool.get('product.product')
119 product_ids = proxy.search(cr, uid, [('id', '=', record_id)], context=context, limit=1)
121 product_id = product_ids[0]
123 product = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
124 res['product_id'] = product.id
125 res['uom_id'] = product.uom_id.id
127 if 'warehouse_id' in fields:
128 warehouse_id = self.pool.get('stock.warehouse').search(cr, uid, [], context=context)
129 res['warehouse_id'] = warehouse_id[0] if warehouse_id else False
134 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: