[IMP] point_of_sale: Converted Add Product wizard into osv_memory wizard
[odoo/odoo.git] / addons / mrp / wizard / make_procurement_product.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 import netsvc
23 import time
24
25 from osv import fields, osv
26
27 class make_procurement(osv.osv_memory):
28     _name = 'make.procurement'
29     _description = 'Make Procurements'
30     
31     def onchange_product_id(self, cr, uid, ids, prod_id):
32         """ 
33              @summary: On Change of Product ID getting the value of related UoM.
34         
35              @param self: The object pointer.
36              @param cr: A database cursor
37              @param uid: ID of the user currently logged in
38              @param ids: List of IDs selected 
39              @param prod_id: Changed ID of Product 
40              
41              @return: A dictionary which gives the UoM of the changed Product 
42         
43         """
44         product = self.pool.get('product.product').browse(cr, uid, prod_id)
45         return {'value': {'uom_id': product.uom_id.id}}
46     
47     _columns = {
48         'qty': fields.float('Quantity', digits=(16,2), required=True),
49         'product_id': fields.many2one('product.product', 'Product', required=True, readonly=1),
50         'uom_id': fields.many2one('product.uom', 'Unit of Measure', required=True),
51         'warehouse_id': fields.many2one('stock.warehouse', 'Warehouse', required=True),
52         'date_planned': fields.date('Planned Date', required=True),
53     }
54     
55     _defaults = {
56             'date_planned': lambda *args: time.strftime('%Y-%m-%d'),
57             'qty': lambda *args: 1.0,
58     }
59     
60     def make_procurement(self, cr, uid, ids, context=None):
61         """ 
62              @summary: Creates procurement order for selected product.
63         
64              @param self: The object pointer.
65              @param cr: A database cursor
66              @param uid: ID of the user currently logged in
67              @param ids: List of IDs selected 
68              @param context: A standard dictionary 
69              
70              @return: A dictionary which loads Procurement form view. 
71         
72         """
73         user = self.pool.get('res.users').browse(cr, uid, uid, context).login
74         for proc in self.browse(cr, uid, ids):
75             wh = self.pool.get('stock.warehouse').browse(cr, uid, proc.warehouse_id.id, context)
76             procure_id = self.pool.get('mrp.procurement').create(cr, uid, {
77                 'name':'INT: '+str(user),
78                 'date_planned': proc.date_planned,
79                 'product_id': proc.product_id.id,
80                 'product_qty': proc.qty,
81                 'product_uom': proc.uom_id.id,
82                 'location_id': wh.lot_stock_id.id,
83                 'procure_method':'make_to_order',
84             })
85             wf_service = netsvc.LocalService("workflow")
86             wf_service.trg_validate(uid, 'mrp.procurement', procure_id, 'button_confirm', cr)
87         
88         data_obj = self.pool.get('ir.model.data')
89         id2 = data_obj._get_id(cr, uid, 'mrp', 'mrp_procurement_tree_view')
90         id3 = data_obj._get_id(cr, uid, 'mrp', 'mrp_procurement_form_view')
91         if id2:
92             id2 = data_obj.browse(cr, uid, id2, context=context).res_id
93         if id3:
94             id3 = data_obj.browse(cr, uid, id3, context=context).res_id
95         
96         return {
97                 'view_type': 'form',
98                 'view_mode': 'tree,form',
99                 'res_model': 'mrp.procurement',
100                 'res_id' : procure_id,
101                 'views': [(id3,'form'),(id2,'tree')],
102                 'type': 'ir.actions.act_window',
103          }
104     
105     def default_get(self, cr, uid, fields, context=None):
106         """ 
107              @summary: To get default values for the object.
108             
109              @param self: The object pointer.
110              @param cr: A database cursor
111              @param uid: ID of the user currently logged in
112              @param fields: List of fields for which we want default values 
113              @param context: A standard dictionary 
114              
115              @return: A dictionary which of fields with values. 
116         
117         """
118         record_id = context and context.get('record_id', False) or False
119
120         res = super(make_procurement, self).default_get(cr, uid, fields, context=context)
121
122         if record_id:
123             product_id = self.pool.get('product.product').browse(cr, uid, record_id, context=context).id
124             res['product_id'] = product_id
125
126         return res
127
128 make_procurement()
129 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
130