[FIX]crm: crm lead/opp merge, when opp has stage with state cancel, then it is less...
[odoo/odoo.git] / addons / procurement / 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 from openerp import netsvc
23
24 from openerp.osv import fields, osv
25
26 class make_procurement(osv.osv_memory):
27     _name = 'make.procurement'
28     _description = 'Make Procurements'
29     
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 
38         """
39         product = self.pool.get('product.product').browse(cr, uid, prod_id)
40         return {'value': {'uom_id': product.uom_id.id}}
41     
42     _columns = {
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),
48     }
49
50     _defaults = {
51         'date_planned': fields.date.context_today,
52         'qty': lambda *args: 1.0,
53     }
54
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.
63         """
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')
69
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',
80             })
81
82             wf_service.trg_validate(uid, 'procurement.order', procure_id, 'button_confirm', cr)
83
84
85         id2 = data_obj._get_id(cr, uid, 'procurement', 'procurement_tree_view')
86         id3 = data_obj._get_id(cr, uid, 'procurement', 'procurement_form_view')
87
88         if id2:
89             id2 = data_obj.browse(cr, uid, id2, context=context).res_id
90         if id3:
91             id3 = data_obj.browse(cr, uid, id3, context=context).res_id
92
93         return {
94             'view_type': 'form',
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',
100          }
101
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.
110         """
111         if context is None:
112             context = {}
113         record_id = context.get('active_id')
114
115         res = super(make_procurement, self).default_get(cr, uid, fields, context=context)
116
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)
120             if product_ids:
121                 product_id = product_ids[0]
122
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
126
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
130
131         return res
132
133 make_procurement()
134 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
135