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