1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import fields, osv
23 from tools.translate import _
26 class stock_partial_picking(osv.osv_memory):
27 _name = "stock.partial.picking"
28 _description = "Partial Picking"
30 'date': fields.datetime('Date', required=True),
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')
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'):
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")
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")
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">
68 <separator colspan="4" string="Move Detail"/>
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'):
76 _moves_fields.update({
77 'move%s_product_id'%(m.id) : {
78 'string': _('Product'),
80 'relation': 'product.product',
84 'move%s_product_qty'%(m.id) : {
85 'string': _('Quantity'),
89 'move%s_product_uom'%(m.id) : {
90 'string': _('Product UOM'),
92 'relation': 'product.uom',
96 'move%s_prodlot_id'%(m.id): {
97 'string': _('Production Lot'),
99 'relation': 'stock.production.lot',
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'),
117 'move%s_product_currency'%(m.id): {
118 'string': _('Currency'),
121 'relation': 'res.currency',
124 _moves_arch_lst += """
125 <field name="move%s_product_price" />
126 <field name="move%s_product_currency" nolabel="1"/>
128 _moves_arch_lst += """
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"
137 <button name="do_partial" string="_Validate"
138 colspan="1" type="object" icon="gtk-apply" />
141 result['arch'] = _moves_arch_lst
142 result['fields'] = _moves_fields
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.
155 res = super(stock_partial_picking, self).default_get(cr, uid, fields, context=context)
156 pick_obj = self.pool.get('stock.picking')
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'):
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
175 if (m.product_id.cost_method == 'average'):
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
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.
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)
207 'delivery_date' : partial.date
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'):
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
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
225 res = pick_obj.do_partial(cr, uid, picking_ids, partial_datas, context=context)
228 stock_partial_picking()
231 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: