c81e8bbf2eb9cc816c98e69513a8385d821ca084
[odoo/odoo.git] / addons / stock / wizard / stock_partial_picking.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 from osv import fields, osv
22 from service import web_services
23 from tools.translate import _
24 import netsvc
25 import pooler
26 import time
27
28 class stock_partial_picking(osv.osv_memory):
29     _name = "stock.partial.picking"
30     _description = "Partial Picking"
31     _columns = {
32             'date': fields.datetime('Date', required=True),
33      }
34
35     def view_init(self, cr, uid, fields_list, context=None):
36         res = super(stock_partial_picking, self).view_init(cr, uid, fields_list, context=context)
37         pick_obj = self.pool.get('stock.picking')        
38         if not context:
39             context={}
40         moveids = []
41         for pick in pick_obj.browse(cr, uid, context.get('active_ids', [])):            
42             for m in pick.move_lines:
43                 if m.state in ('done', 'cancel'):
44                     continue
45                 if 'move%s_product_id'%(m.id) not in self._columns:
46                     self._columns['move%s_product_id'%(m.id)] = fields.many2one('product.product',string="Product")
47                 if 'move%s_product_qty'%(m.id) not in self._columns:
48                     self._columns['move%s_product_qty'%(m.id)] = fields.float("Quantity")
49                 if 'move%s_product_uom'%(m.id) not in self._columns:
50                     self._columns['move%s_product_uom'%(m.id)] = fields.many2one('product.uom',string="Product UOM")
51
52                 if (m.product_id.cost_method == 'average'):
53                     if 'move%s_product_price'%(m.id) not in self._columns:
54                         self._columns['move%s_product_price'%(m.id)] = fields.float("Price")
55                     if 'move%s_product_currency'%(m.id) not in self._columns:
56                         self._columns['move%s_product_currency'%(m.id)] = fields.many2one('res.currency',string="Currency")
57         return res   
58
59     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False,submenu=False):
60         result = super(stock_partial_picking, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar,submenu)        
61         pick_obj = self.pool.get('stock.picking')
62         picking_ids = context.get('active_ids', False) 
63         _moves_arch_lst = """<form string="Deliver Products">
64                         <separator colspan="4" string="Delivery Information"/>
65                         <field name="date" colspan="4" />
66                         <separator colspan="4" string="Move Detail"/>
67                         """
68         _moves_fields = result['fields']
69         if picking_ids and view_type in ['form']:
70             for pick in pick_obj.browse(cr, uid, picking_ids, context):
71                 for m in pick.move_lines:
72                     if m.state in ('done', 'cancel'):
73                         continue
74                     _moves_fields.update({
75                         'move%s_product_id'%(m.id)  : {
76                                     'string': _('Product'),
77                                     'type' : 'many2one', 
78                                     'relation': 'product.product', 
79                                     'required' : True, 
80                                     'readonly' : True,                                    
81                                     },
82                         'move%s_product_qty'%(m.id) : {
83                                     'string': _('Quantity'),
84                                     'type' : 'float',
85                                     'required': True,                                    
86                                     },
87                         'move%s_product_uom'%(m.id) : {
88                                     'string': _('Product UOM'),
89                                     'type' : 'many2one', 
90                                     'relation': 'product.uom', 
91                                     'required' : True, 
92                                     'readonly' : True,                                    
93                                     }
94                     })                
95                     
96                     _moves_arch_lst += """
97                         <group colspan="4" col="10">
98                         <field name="move%s_product_id" nolabel="1"/>
99                         <field name="move%s_product_qty" string="Qty" />
100                         <field name="move%s_product_uom" nolabel="1" />
101                     """%(m.id, m.id, m.id)
102                     if (m.product_id.cost_method == 'average'):                        
103                         _moves_fields.update({
104                             'move%s_product_price'%(m.id) : {
105                                     'string': _('Price'),
106                                     'type' : 'float',
107                                     },
108                             'move%s_product_currency'%(m.id): {
109                                     'string': _('Currency'),
110                                     'type' : 'float',      
111                                     'type' : 'many2one', 
112                                     'relation': 'res.currency',                                    
113                                     }
114                         })
115                         _moves_arch_lst += """
116                             <field name="move%s_product_price" />
117                             <field name="move%s_product_currency" nolabel="1"/>
118                         """%(m.id, m.id)
119                     _moves_arch_lst += """
120                         </group>
121                         """
122         _moves_arch_lst += """
123                 <separator string="" colspan="4" />
124                 <label string="" colspan="2"/>
125                 <group col="2" colspan="2">
126                         <button icon='gtk-cancel' special="cancel"
127                                 string="_Cancel" />
128                         <button name="do_partial" string="_Deliver"
129                                 colspan="1" type="object" icon="gtk-apply" />
130                 </group>                        
131         </form>"""
132         result['arch'] = _moves_arch_lst
133         result['fields'] = _moves_fields           
134         return result
135
136     def default_get(self, cr, uid, fields, context=None):
137         """ To get default values for the object.
138         @param self: The object pointer.
139         @param cr: A database cursor
140         @param uid: ID of the user currently logged in
141         @param fields: List of fields for which we want default values 
142         @param context: A standard dictionary 
143         @return: A dictionary which of fields with values. 
144         """ 
145
146         res = super(stock_partial_picking, self).default_get(cr, uid, fields, context=context)
147         pick_obj = self.pool.get('stock.picking')        
148         if not context:
149             context={}
150         moveids = []
151         if 'date' in fields:
152             res.update({'date': time.strftime('%Y-%m-%d %H:%M:%S')})
153         for pick in pick_obj.browse(cr, uid, context.get('active_ids', [])):
154             for m in pick.move_lines:
155                 if m.state in ('done', 'cancel'):
156                     continue
157                 if 'move%s_product_id'%(m.id) in fields:
158                     res['move%s_product_id'%(m.id)] = m.product_id.id
159                 if 'move%s_product_qty'%(m.id) in fields:
160                     res['move%s_product_qty'%(m.id)] = m.product_qty
161                 if 'move%s_product_uom'%(m.id) in fields:
162                     res['move%s_product_uom'%(m.id)] = m.product_uom.id
163
164                 if (m.product_id.cost_method == 'average'):
165                     currency = False
166                     price = 0
167                     if (pick.type == 'in'):
168                         if hasattr(m, 'purchase_line_id') and m.purchase_line_id:
169                             price = m.purchase_line_id.price_unit
170                         if hasattr(pick, 'purchase_id') and pick.purchase_id:
171                             currency = pick.purchase_id.pricelist_id.currency_id.id
172                     if (pick.type == 'out'):
173                         if hasattr(m, 'sale_line_id') and m.sale_line_id:
174                             price = m.sale_line_id.price_unit
175                         if hasattr(pick, 'sale_id') and pick.sale_id:
176                             currency = pick.sale_id.pricelist_id.currency_id.id
177                     if 'move%s_product_price'%(m.id) in fields:
178                         res['move%s_product_price'%(m.id)] = price
179                     if 'move%s_product_currency'%(m.id) in fields:
180                         res['move%s_product_currency'%(m.id)] = currency
181         return res   
182
183     def do_partial(self, cr, uid, ids, context): 
184         """ Makes partial moves and pickings done.
185         @param self: The object pointer.
186         @param cr: A database cursor
187         @param uid: ID of the user currently logged in
188         @param fields: List of fields for which we want default values 
189         @param context: A standard dictionary 
190         @return: A dictionary which of fields with values. 
191         """    
192         pick_obj = self.pool.get('stock.picking')    
193         picking_ids = context.get('active_ids', False)
194         partial = self.browse(cr, uid, ids[0], context)
195         partial_datas = {
196             'delivery_date' : partial.date         
197         }
198         for pick in pick_obj.browse(cr, uid, picking_ids):
199             for m in pick.move_lines:
200                 if m.state in ('done', 'cancel'):
201                     continue
202                 partial_datas['move%s'%(m.id)] = {
203                     'product_id' : getattr(partial, 'move%s_product_id'%(m.id)).id,
204                     'product_qty' : getattr(partial, 'move%s_product_qty'%(m.id)),
205                     'product_uom' : getattr(partial, 'move%s_product_uom'%(m.id)).id
206                 }
207
208                 if (m.product_id.cost_method == 'average'):   
209                     partial_datas['move%s'%(m.id)].update({             
210                         'product_price' : getattr(partial, 'move%s_product_price'%(m.id)),
211                         'product_currency': getattr(partial, 'move%s_product_currency'%(m.id)).id
212                     })          
213         res = pick_obj.do_partial(cr, uid, picking_ids, partial_datas, context=context)
214         return {}
215  
216 stock_partial_picking()    
217
218
219
220 #_moves_arch_end = '''<?xml version="1.0"?>
221 #<form string="Picking result">
222 #    <label string="The picking has been successfully made !" colspan="4"/>
223 #    <field name="back_order_notification" colspan="4" nolabel="1"/>
224 #</form>'''
225
226 #_moves_fields_end = {
227 #    'back_order_notification': {'string':'Back Order' ,'type':'text', 'readonly':True}
228 #                     }
229 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
230