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 ##############################################################################
25 from osv import osv,fields
26 from tools.translate import _
28 class stock_return_picking(osv.osv_memory):
29 _name = 'stock.return.picking'
30 _description = 'Return Picking'
32 def default_get(self, cr, uid, fields, context):
34 To get default values for the object.
35 @param self: The object pointer.
36 @param cr: A database cursor
37 @param uid: ID of the user currently logged in
38 @param fields: List of fields for which we want default values
39 @param context: A standard dictionary
40 @return: A dictionary with default values for all field in ``fields``
42 res = super(stock_return_picking, self).default_get(cr, uid, fields, context=context)
43 record_id = context and context.get('active_id', False) or False
44 pick_obj = self.pool.get('stock.picking')
45 pick = pick_obj.browse(cr, uid, record_id)
47 if 'invoice_state' in fields:
48 if pick.invoice_state=='invoiced':
49 res['invoice_state'] = '2binvoiced'
51 res['invoice_state'] = 'none'
52 for line in pick.move_lines:
53 return_id = 'return%s'%(line.id)
54 if return_id in fields:
55 res[return_id] = line.product_qty
58 def view_init(self, cr, uid, fields_list, context=None):
60 Creates view dynamically and adding fields at runtime.
61 @param self: The object pointer.
62 @param cr: A database cursor
63 @param uid: ID of the user currently logged in
64 @param context: A standard dictionary
65 @return: New arch of view with new columns.
67 res = super(stock_return_picking, self).view_init(cr, uid, fields_list, context=context)
68 record_id = context and context.get('active_id', False)
70 pick_obj = self.pool.get('stock.picking')
71 pick = pick_obj.browse(cr, uid, record_id)
72 for m in [line for line in pick.move_lines]:
73 if 'return%s'%(m.id) not in self._columns:
74 self._columns['return%s'%(m.id)] = fields.float(string=m.name, required=True)
75 if 'invoice_state' not in self._columns:
76 self._columns['invoice_state'] = fields.selection([('2binvoiced', 'To be Invoiced'), ('none', 'None')], string='Invoice State', required=True)
77 for rec in m.move_history_ids2:
78 if rec.product_qty==m.product_qty:
79 raise osv.except_osv(_('Warning !'), _("There is no product to return!"))
82 def fields_view_get(self, cr, uid, view_id=None, view_type='form',
83 context=None, toolbar=False, submenu=False):
85 Changes the view dynamically
86 @param self: The object pointer.
87 @param cr: A database cursor
88 @param uid: ID of the user currently logged in
89 @param context: A standard dictionary
90 @return: New arch of view.
92 res = super(stock_return_picking, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar,submenu=False)
93 record_id = context and context.get('active_id', False)
94 active_model = context.get('active_model')
95 if active_model != 'stock.picking':
98 pick_obj = self.pool.get('stock.picking')
99 pick = pick_obj.browse(cr, uid, record_id)
100 if pick.state != 'done':
101 raise osv.except_osv(_('Warning !'), _("The Picking is not completed yet!\nYou cannot return picking which is not in 'Done' state!"))
104 for m_line in pick.move_lines:
105 return_history[m_line.id] = 0
106 for rec in m_line.move_history_ids2:
107 return_history[m_line.id] += rec.product_qty
108 res['fields'].clear()
109 arch_lst=['<?xml version="1.0"?>', '<form string="%s">' % _('Return lines'), '<label string="%s" colspan="4"/>' % _('Provide the quantities of the returned products.')]
110 for m in [line for line in pick.move_lines]:
111 quantity = m.product_qty
112 if quantity > return_history[m.id] and (quantity - return_history[m.id])>0:
113 arch_lst.append('<field name="return%s"/>\n<newline/>' % (m.id,))
114 res['fields']['return%s' % m.id]={'string':m.name, 'type':'float', 'required':True}
115 res.setdefault('returns', []).append(m.id)
116 # if not res.get('returns',False): Todo : It may be used
117 # raise osv.except_osv(_('Warning!'),_('There is no product to return!'))
119 arch_lst.append('<field name="invoice_state"/>\n<newline/>')
120 if pick.invoice_state=='invoiced':
121 new_invoice_state='2binvoiced'
123 new_invoice_state=pick.invoice_state
124 res['fields']['invoice_state']={'string':_('Invoice state'), 'type':'selection','required':True, 'selection':[('2binvoiced', _('To Be Invoiced')), ('none', _('None'))]}
125 arch_lst.append('<group col="2" colspan="4">')
126 arch_lst.append('<button icon="gtk-cancel" special="cancel" string="Cancel" />')
127 arch_lst.append('<button name="create_returns" string="Return" colspan="1" type="object" icon="gtk-apply" />')
128 arch_lst.append('</group>')
129 arch_lst.append('</form>')
130 res['arch'] = '\n'.join(arch_lst)
133 def create_returns(self, cr, uid, ids, context):
135 Creates return picking.
136 @param self: The object pointer.
137 @param cr: A database cursor
138 @param uid: ID of the user currently logged in
139 @param ids: List of ids selected
140 @param context: A standard dictionary
141 @return: A dictionary which of fields with values.
143 record_id = context and context.get('active_id', False) or False
144 move_obj = self.pool.get('stock.move')
145 pick_obj = self.pool.get('stock.picking')
146 uom_obj = self.pool.get('product.uom')
147 wf_service = netsvc.LocalService("workflow")
149 pick = pick_obj.browse(cr, uid, record_id)
150 data = self.read(cr, uid, ids[0])
152 date_cur = time.strftime('%Y-%m-%d %H:%M:%S')
154 move_ids = [m.id for m in [line for line in pick.move_lines]]
155 set_invoice_state_to_none = True
156 for move in move_obj.browse(cr, uid, move_ids):
160 elif pick.type=='in':
163 new_type = 'internal'
164 new_picking = pick_obj.copy(cr, uid, pick.id, {'name':'%s (return)' % pick.name,
165 'move_lines':[], 'state':'draft', 'type':new_type,
166 'date':date_cur, 'invoice_state':data['invoice_state'],})
167 new_location=move.location_dest_id.id
169 new_qty = data['return%s' % move.id]
170 returned_qty = move.product_qty
172 for rec in move.move_history_ids2:
173 returned_qty -= rec.product_qty
175 if returned_qty != new_qty:
176 set_invoice_state_to_none = False
178 new_move=move_obj.copy(cr, uid, move.id, {
179 'product_qty': new_qty,
180 'product_uos_qty': uom_obj._compute_qty(cr, uid, move.product_uom.id,
181 new_qty, move.product_uos.id),
182 'picking_id':new_picking, 'state':'draft',
183 'location_id':new_location, 'location_dest_id':move.location_id.id,
184 'date':date_cur, 'date_planned':date_cur,})
185 move_obj.write(cr, uid, [move.id], {'move_history_ids2':[(4,new_move)]})
187 if set_invoice_state_to_none:
188 pick_obj.write(cr, uid, [pick.id], {'invoice_state':'none'})
191 wf_service.trg_validate(uid, 'stock.picking', new_picking, 'button_confirm', cr)
192 pick_obj.force_assign(cr, uid, [new_picking], context)
194 'domain': "[('id', 'in', ["+str(new_picking)+"])]",
195 'name': 'Picking List',
197 'view_mode':'tree,form',
198 'res_model':'stock.picking',
200 'type':'ir.actions.act_window',
205 stock_return_picking()
207 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: