[IMP]remove tabs and add spaces instead of them
[odoo/odoo.git] / addons / point_of_sale / wizard / pos_return.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
22 import netsvc
23 from osv import osv,fields
24 from tools.translate import _
25 import time
26
27 class pos_return(osv.osv_memory):
28     _name = 'pos.return'
29     _description = 'Point of sale return'
30     _columns = {
31         'pos_moves_ids' : fields.one2many('pos.return.memory', 'pos_moves_id', 'Moves'),
32      }
33
34     def default_get(self, cr, uid, fields, context=None):
35         """
36              To get default values for the object.
37
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
43
44              @return: A dictionary which of fields with values.
45
46         """
47         res = super(pos_return, self).default_get(cr, uid, fields, context=context)
48         order_obj = self.pool.get('pos.order')
49         if context is None:
50             context={}
51         active_ids = context.get('active_ids')
52         result=[]
53         for order in order_obj.browse(cr, uid, active_ids, context=context):
54             for line in order.lines:
55                 result.append({
56                             'product_id' : line.product_id.id,
57                             'quantity' : line.qty,
58                             'line_id':line.id
59                         })
60             res.update({'pos_moves_ids': result})
61         return res
62
63     def  create_returns(self, cr, uid, data, context=None):
64         """
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
69
70              @return: Return the add product form again for adding more product
71
72         """
73         if context is None:
74             context = {}
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)
84         return {
85             'name': _('Add Product'),
86             'view_type': 'form',
87             'view_mode': 'form',
88             'res_model': 'pos.add.product',
89             'view_id': False,
90             'target':'new',
91             'views': False,
92             'context': context,
93             'type': 'ir.actions.act_window',
94         }
95     def create_returns2(self, cr, uid, ids, context=None):
96
97         if context is None:
98             context = {}
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
109         if active_id:
110             data = self.browse(cr, uid, ids, context=context)[0]
111             date_cur = time.strftime('%Y-%m-%d %H:%M:%S')
112
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,))
118                 res = cr.fetchone()
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,
121                                                                                'move_lines': [],
122                                                                                'state':'draft',
123                                                                                'type': 'in',
124                                                                                'partner_id': order_id.partner_id.id,
125                                                                                'date': date_cur })
126                 new_order = order_obj.copy(cr, uid, order_id.id, {'name': 'Refund %s'%order_id.name,
127                                                               'lines':[],
128                                                               'statement_ids':[],
129                                                               'picking_id':[]})
130                 account_def = property_obj.get(cr, uid, 'property_account_payable', 'res.partner', context=context)
131                 amount = 0.0
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, {
138                                 'product_qty': qty ,
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,
146                                 'date': date_cur
147                             })
148                             if qty != 0.0:
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,
157                                                 'amount': -amount,
158                                                 })
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)
162             act = {
163                 'domain': "[('id', 'in', ["+str(new_order)+"])]",
164                 'name': 'Refunded Orders',
165                 'view_type': 'form',
166                 'view_mode': 'tree,form',
167                 'res_model': 'pos.order',
168                 'auto_refresh':0,
169                 'res_id':new_order,
170                 'view_id': False,
171                 'context':context,
172                 'type': 'ir.actions.act_window'
173             }
174         return act
175
176 pos_return()
177
178 class add_product(osv.osv_memory):
179     _inherit = 'pos.add.product'
180     def select_product(self, cr, uid, ids, context=None):
181         """
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
188         """
189         if context is None:
190             context = {}
191
192         active_id=context.get('active_id', False)
193         data =  self.read(cr, uid, ids)
194         data = data and data[0] or False
195         if active_id:
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)
205
206             for order_id in order_obj.browse(cr, uid, [active_id], context=context):
207                 prod=data['product_id']
208                 qty=data['quantity']
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,))
213                 res=cr.fetchone()
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,
218                                 'move_lines':[],
219                                 'state':'draft',
220                                 'type':'out',
221                                 'date':date_cur
222                             })
223                 stock_move_obj.create(cr, uid, {
224                                 'product_qty': qty,
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,
232                                 'date':date_cur
233                             })
234
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})
238
239         return {
240             'name': _('Add Product'),
241             'view_type': 'form',
242             'view_mode': 'form',
243             'res_model': 'pos.add.product',
244             'view_id': False,
245             'target':'new',
246             'context':context,
247             'views': False,
248             'type': 'ir.actions.act_window',
249         }
250
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,[])
264         data = {}
265         if return_id:
266             data = return_boj.read(cr,uid,return_id,[])[0]
267
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)
271
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,))
277             res=cr.fetchone()
278             location_id=res and res[0] or None
279
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',
285                             'type':'in',
286                             'date':date_cur
287                         })
288             for line in order_id.lines:
289                 key= 'return%s' % line.id
290                 if line.id:
291                     if data.has_key(key):
292                         qty = data[key]
293                         lines_obj.write(cr,uid,[line.id], {
294                                 'qty':line.qty-(data[key] or 0.0)
295                         })
296                     else:
297                         qty = line.qty
298                     stock_move_obj.create(cr, uid, {
299                         'product_qty': qty,
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,
307                         'date':date_cur,
308                     })
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'})
313
314         if obj.amount_total != obj.amount_paid:
315             return {
316                 'name': _('Make Payment'),
317                 'context ':context,
318                 'view_type': 'form',
319                 'view_mode': 'form',
320                 'res_model': 'pos.make.payment',
321                 'view_id': False,
322                 'target': 'new',
323                 'views': False,
324                 'type': 'ir.actions.act_window',
325             }
326         return True
327
328 add_product()
329 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: