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 _
27 class stock_partial_move_memory_out(osv.osv_memory):
28 _name = "stock.move.memory.out"
29 _rec_name = 'product_id'
31 'product_id' : fields.many2one('product.product', string="Product", required=True),
32 'quantity' : fields.float("Quantity", required=True),
33 'product_uom': fields.many2one('product.uom', 'Unit of Measure', required=True),
34 'prodlot_id' : fields.many2one('stock.production.lot', 'Production Lot'),
35 'move_id' : fields.many2one('stock.move', "Move"),
36 'wizard_id' : fields.many2one('stock.partial.move', string="Wizard"),
37 'cost' : fields.float("Cost", help="Unit Cost for this product line"),
38 'currency' : fields.many2one('res.currency', string="Currency", help="Currency in which Unit cost is expressed"),
41 class stock_partial_move_memory_in(osv.osv_memory):
42 _inherit = "stock.move.memory.out"
43 _name = "stock.move.memory.in"
45 class stock_partial_move(osv.osv_memory):
46 _name = "stock.partial.move"
47 _description = "Partial Move"
49 'date': fields.datetime('Date', required=True),
50 'type': fields.char("Type", size=3),
51 'product_moves_out' : fields.one2many('stock.move.memory.out', 'wizard_id', 'Moves'),
52 'product_moves_in' : fields.one2many('stock.move.memory.in', 'wizard_id', 'Moves'),
56 def __is_in(self,cr, uid, move_ids):
58 @return: True if one of the moves has as picking type 'in'
63 move_obj = self.pool.get('stock.move')
64 move_ids = move_obj.search(cr, uid, [('id','in',move_ids)])
66 for move in move_obj.browse(cr, uid, move_ids):
67 if move.picking_id.type == 'in' and move.product_id.cost_method == 'average':
71 def __get_picking_type(self, cr, uid, move_ids):
72 if self.__is_in(cr, uid, move_ids):
73 return "product_moves_in"
75 return "product_moves_out"
77 def view_init(self, cr, uid, fields_list, context=None):
78 res = super(stock_partial_move, self).view_init(cr, uid, fields_list, context=context)
79 move_obj = self.pool.get('stock.move')
83 for move in move_obj.browse(cr, uid, context.get('active_ids', []), context=context):
84 if move.state in ('done', 'cancel'):
85 raise osv.except_osv(_('Invalid action !'), _('Cannot deliver products which are already delivered !'))
90 def __create_partial_move_memory(self, move):
92 'product_id' : move.product_id.id,
93 'quantity' : move.product_qty,
94 'product_uom' : move.product_uom.id,
95 'prodlot_id' : move.prodlot_id.id,
99 if move.picking_id.type == 'in':
101 'cost' : move.product_id.standard_price,
102 'currency' : move.product_id.company_id and move.product_id.company_id.currency_id and move.product_id.company_id.currency_id.id or False,
106 def __get_active_stock_moves(self, cr, uid, context=None):
107 move_obj = self.pool.get('stock.move')
112 for move in move_obj.browse(cr, uid, context.get('active_ids', []), context=context):
113 if move.state in ('done', 'cancel'):
115 res.append(self.__create_partial_move_memory(move))
120 'product_moves_in' : __get_active_stock_moves,
121 'product_moves_out' : __get_active_stock_moves,
122 'date' : lambda *a : time.strftime('%Y-%m-%d %H:%M:%S'),
126 def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
131 'title' : _('Deliver Products'),
132 'info' : _('Delivery Information'),
133 'button' : _('Deliver'),
136 if context.get('product_receive', False):
138 'title' : _('Receive Products'),
139 'info' : _('Receive Information'),
140 'button' : _('Receive'),
143 move_ids = context.get('active_ids', False)
144 message['picking_type'] = self.__get_picking_type(cr, uid, move_ids)
145 result = super(stock_partial_move, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
146 _moves_fields = result['fields']
147 _moves_fields.update({
148 'product_moves_in' : {'relation': 'stock.move.memory.in', 'type' : 'one2many', 'string' : 'Product Moves'},
149 'product_moves_out' : {'relation': 'stock.move.memory.out', 'type' : 'one2many', 'string' : 'Product Moves'}
152 _moves_arch_lst = """
153 <form string="%(title)s">
154 <separator colspan="4" string="%(info)s"/>
155 <field name="date" colspan="2"/>
156 <separator colspan="4" string="Move Detail"/>
157 <field name="%(picking_type)s" colspan="4" nolabel="1" mode="tree,form" width="550" height="200" ></field>
158 <separator string="" colspan="4" />
159 <label string="" colspan="2"/>
160 <group col="2" colspan="2">
161 <button icon='gtk-cancel' special="cancel" string="_Cancel" />
162 <button name="do_partial" string="%(button)s"
163 colspan="1" type="object" icon="gtk-apply" />
165 </form> """ % message
167 result['arch'] = _moves_arch_lst
168 result['fields'] = _moves_fields
171 def do_partial(self, cr, uid, ids, context=None):
172 """ Makes partial moves and pickings done.
173 @param self: The object pointer.
174 @param cr: A database cursor
175 @param uid: ID of the user currently logged in
176 @param fields: List of fields for which we want default values
177 @param context: A standard dictionary
178 @return: A dictionary which of fields with values.
183 move_obj = self.pool.get('stock.move')
185 move_ids = context.get('active_ids', False)
186 partial = self.browse(cr, uid, ids[0], context=context)
188 'delivery_date' : partial.date
192 picking_type = self.__get_picking_type(cr, uid, move_ids)
194 moves_list = picking_type == 'product_moves_in' and partial.product_moves_in or partial.product_moves_out
195 for product_move in moves_list:
196 p_moves[product_move.move_id.id] = product_move
199 for move in move_obj.browse(cr, uid, move_ids, context=context):
200 if move.state in ('done', 'cancel'):
202 if not p_moves.get(move.id):
204 partial_datas['move%s' % (move.id)] = {
205 'product_id' : p_moves[move.id].id,
206 'product_qty' : p_moves[move.id].quantity,
207 'product_uom' :p_moves[move.id].product_uom.id,
208 'prodlot_id' : p_moves[move.id].prodlot_id.id,
211 moves_ids_final.append(move.id)
212 if (move.picking_id.type == 'in') and (move.product_id.cost_method == 'average'):
213 partial_datas['move%s' % (move.id)].update({
214 'product_price' : p_moves[move.id].cost,
215 'product_currency': p_moves[move.id].currency.id,
219 move_obj.do_partial(cr, uid, moves_ids_final, partial_datas, context=context)
220 return {'type': 'ir.actions.act_window_close'}
223 stock_partial_move_memory_out()
224 stock_partial_move_memory_in()
225 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: