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 ##############################################################################
23 from osv import osv,fields
24 from tools.translate import _
27 class pos_return(osv.osv_memory):
29 _description = 'Point of sale return'
31 'pos_moves_ids' : fields.one2many('pos.return.memory', 'pos_moves_id', 'Moves'),
34 def default_get(self, cr, uid, fields, context=None):
36 To get default values for the object.
38 @param self: The object pointer.
39 @param cr: A database cursor
40 @param uid: ID of the user currently logged in
41 @param fields: List of fields for which we want default values
42 @param context: A standard dictionary
44 @return: A dictionary which of fields with values.
47 res = super(pos_return, self).default_get(cr, uid, fields, context=context)
48 order_obj = self.pool.get('pos.order')
51 active_ids = context.get('active_ids')
53 for order in order_obj.browse(cr, uid, active_ids, context=context):
54 for line in order.lines:
56 'product_id' : line.product_id.id,
57 'quantity' : line.qty,
60 res.update({'pos_moves_ids': result})
63 def create_returns(self, cr, uid, data, context=None):
65 @param self: The object pointer.
66 @param cr: A database cursor
67 @param uid: ID of the user currently logged in
68 @param context: A standard dictionary
70 @return: Return the add product form again for adding more product
75 current_rec = self.browse(cr, uid, data, context=context)[0]
76 order_obj =self.pool.get('pos.order')
77 line_obj = self.pool.get('pos.order.line')
78 pos_current = order_obj.browse(cr, uid, context.get('active_id'), context=context)
79 for pos_line in pos_current.lines:
80 for record in current_rec.pos_moves_ids:
81 if pos_line.id == record.line_id:
82 less_qty = record.quantity
83 line_obj.write(cr, uid, pos_line.id, {'qty':pos_line.qty - less_qty}, context=context)
85 'name': _('Add Product'),
88 'res_model': 'pos.add.product',
93 'type': 'ir.actions.act_window',
95 def create_returns2(self, cr, uid, ids, context=None):
99 active_id = context.get('active_id', False)
100 order_obj =self.pool.get('pos.order')
101 line_obj = self.pool.get('pos.order.line')
102 picking_obj = self.pool.get('stock.picking')
103 stock_move_obj = self.pool.get('stock.move')
104 property_obj= self.pool.get("ir.property")
105 uom_obj =self. pool.get('product.uom')
106 statementl_obj = self.pool.get('account.bank.statement.line')
107 wf_service = netsvc.LocalService("workflow")
108 #Todo :Need to clean the code
110 data = self.browse(cr, uid, ids, context=context)[0]
111 date_cur = time.strftime('%Y-%m-%d %H:%M:%S')
113 for order_id in order_obj.browse(cr, uid, [active_id], context=context):
114 source_stock_id = property_obj.get(cr, uid, 'property_stock_customer', 'res.partner', context=context).id
115 cr.execute("SELECT s.id FROM stock_location s, stock_warehouse w "
116 "WHERE w.lot_stock_id=s.id AND w.id=%s ",
117 (order_id.shop_id.warehouse_id.id,))
119 location_id = res and res[0] or None
120 new_picking = picking_obj.copy(cr, uid, order_id.picking_id.id, {'name':'%s (return)' % order_id.name,
124 'partner_id': order_id.partner_id.id,
126 new_order = order_obj.copy(cr, uid, order_id.id, {'name': 'Refund %s'%order_id.name,
130 account_def = property_obj.get(cr, uid, 'property_account_payable', 'res.partner', context=context)
132 for line in order_id.lines:
133 for record in data.pos_moves_ids:
134 if line.id == record.line_id:
135 qty = record.quantity
136 amount += qty * line.price_unit
137 stock_move_obj.create(cr, uid, {
139 'product_uos_qty': uom_obj._compute_qty(cr, uid, qty ,line.product_id.uom_id.id),
140 'picking_id': new_picking,
141 'product_uom': line.product_id.uom_id.id,
142 'location_id': source_stock_id,
143 'product_id': line.product_id.id,
144 'location_dest_id': location_id,
145 'name': '%s (return)' %order_id.name,
149 line_obj.copy(cr, uid, line.id, {'qty': -qty, 'order_id': new_order})
150 statementl_obj.create(cr, uid, {
151 'name': 'Refund %s'%order_id.name,
152 'statement_id': order_id.statement_ids[0].statement_id.id,
153 'pos_statement_id': new_order,
154 'date': fields.date.context_today(self, cr, uid, context=context),
155 'account_id': order_id.partner_id and order_id.partner_id.property_account_payable \
156 and order_id.partner_id.property_account_payable.id or account_def.id,
159 order_obj.write(cr,uid, [active_id,new_order], {'state': 'done'})
160 wf_service.trg_validate(uid, 'stock.picking', new_picking, 'button_confirm', cr)
161 picking_obj.force_assign(cr, uid, [new_picking], context)
163 'domain': "[('id', 'in', ["+str(new_order)+"])]",
164 'name': 'Refunded Orders',
166 'view_mode': 'tree,form',
167 'res_model': 'pos.order',
172 'type': 'ir.actions.act_window'
178 class add_product(osv.osv_memory):
179 _inherit = 'pos.add.product'
180 def select_product(self, cr, uid, ids, context=None):
182 To get the product and quantity and add in order .
183 @param self: The object pointer.
184 @param cr: A database cursor
185 @param uid: ID of the user currently logged in
186 @param context: A standard dictionary
187 @return : Retrun the add product form again for adding more product
192 active_id=context.get('active_id', False)
193 data = self.read(cr, uid, ids)
194 data = data and data[0] or False
196 order_obj = self.pool.get('pos.order')
197 picking_obj = self.pool.get('stock.picking')
198 stock_move_obj = self.pool.get('stock.move')
199 property_obj= self.pool.get("ir.property")
200 date_cur=time.strftime('%Y-%m-%d')
201 uom_obj = self.pool.get('product.uom')
202 prod_obj=self.pool.get('product.product')
203 wf_service = netsvc.LocalService("workflow")
204 order_obj.add_product(cr, uid, active_id, data['product_id'], data['quantity'], context=context)
206 for order_id in order_obj.browse(cr, uid, [active_id], context=context):
207 prod=data['product_id']
209 stock_dest_id = property_obj.get(cr, uid, 'property_stock_customer', 'res.partner', context=context).id
210 cr.execute("SELECT s.id FROM stock_location s, stock_warehouse w "
211 "WHERE w.lot_stock_id=s.id AND w.id=%s ",
212 (order_id.shop_id.warehouse_id.id,))
214 location_id=res and res[0] or None
215 prod_id=prod_obj.browse(cr, uid, prod, context=context)
216 new_picking=picking_obj.create(cr, uid, {
217 'name':'%s (Added)' %order_id.name,
223 stock_move_obj.create(cr, uid, {
225 'product_uos_qty': uom_obj._compute_qty(cr, uid, prod_id.uom_id.id, qty, prod_id.uom_id.id),
226 'picking_id':new_picking,
227 'product_uom':prod_id.uom_id.id,
228 'location_id':location_id,
229 'product_id':prod_id.id,
230 'location_dest_id':stock_dest_id,
231 'name':'%s (return)' %order_id.name,
235 wf_service.trg_validate(uid, 'stock.picking', new_picking, 'button_confirm', cr)
236 picking_obj.force_assign(cr, uid, [new_picking], context)
237 order_obj.write(cr,uid,active_id,{'picking_id':new_picking})
240 'name': _('Add Product'),
243 'res_model': 'pos.add.product',
248 'type': 'ir.actions.act_window',
251 def close_action(self, cr, uid, ids, context=None):
252 if context is None: context = {}
253 active_ids=context.get('active_ids', False)
254 order_obj = self.pool.get('pos.order')
255 lines_obj = self.pool.get('pos.order.line')
256 picking_obj = self.pool.get('stock.picking')
257 stock_move_obj = self.pool.get('stock.move')
258 property_obj= self.pool.get("ir.property")
259 invoice_obj=self.pool.get('account.invoice')
260 date_cur=time.strftime('%Y-%m-%d %H:%M:%S')
261 uom_obj = self.pool.get('product.uom')
262 return_boj=self.pool.get('pos.return')
263 return_id = return_boj.search(cr,uid,[])
266 data = return_boj.read(cr,uid,return_id,[])[0]
268 wf_service = netsvc.LocalService("workflow")
269 self_data = self.browse(cr, uid, ids, context=context)[0]
270 order_obj.add_product(cr, uid, active_ids[0], self_data.product_id.id, self_data.quantity, context=context)
272 for order_id in order_obj.browse(cr, uid, active_ids, context=context):
273 stock_dest_id = property_obj.get(cr, uid, 'property_stock_customer', 'res.partner', context=context).id
274 cr.execute("SELECT s.id FROM stock_location s, stock_warehouse w "
275 " WHERE w.lot_stock_id=s.id AND w.id=%s ",
276 (order_id.shop_id.warehouse_id.id,))
278 location_id=res and res[0] or None
280 if order_id.invoice_id:
281 invoice_obj.refund(cr, uid, [order_id.invoice_id.id], time.strftime('%Y-%m-%d'), False, order_id.name)
282 new_picking=picking_obj.create(cr, uid, {
283 'name':'%s (return)' %order_id.name,
284 'move_lines':[], 'state':'draft',
288 for line in order_id.lines:
289 key= 'return%s' % line.id
291 if data.has_key(key):
293 lines_obj.write(cr,uid,[line.id], {
294 'qty':line.qty-(data[key] or 0.0)
298 stock_move_obj.create(cr, uid, {
300 'product_uos_qty': uom_obj._compute_qty(cr, uid, qty, line.product_id.uom_id.id),
301 'picking_id':new_picking,
302 'product_uom':line.product_id.uom_id.id,
303 'location_id':location_id,
304 'product_id':line.product_id.id,
305 'location_dest_id':stock_dest_id,
306 'name':'%s (return)' % order_id.name,
309 wf_service.trg_validate(uid, 'stock.picking',new_picking,'button_confirm', cr)
310 picking_obj.force_assign(cr, uid, [new_picking], context)
311 obj=order_obj.browse(cr,uid, active_ids[0])
312 context.update({'return':'return'})
314 if obj.amount_total != obj.amount_paid:
316 'name': _('Make Payment'),
320 'res_model': 'pos.make.payment',
324 'type': 'ir.actions.act_window',
329 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: