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