[FIX] point_of_sale: Add a context on launch_payment() method.
[odoo/odoo.git] / addons / point_of_sale / wizard / pos_payment.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 time
23
24 from osv import osv, fields
25 from tools.translate import _
26 import pos_box_entries
27 import netsvc
28
29 class account_journal(osv.osv):
30     _inherit = 'account.journal'
31
32     def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
33         if not context:
34             context = {}
35         session_id = context.get('pos_session_id', False) or False
36
37         if session_id:
38             session = self.pool.get('pos.session').browse(cr, uid, session_id, context=context)
39
40             if session:
41                 journal_ids = [journal.id for journal in session.config_id.journal_ids]
42                 args += [('id', 'in', journal_ids)]
43
44         return super(account_journal, self).search(cr, uid, args, offset=offset, limit=limit, order=order, context=context, count=count)
45
46 account_journal()        
47
48 class pos_make_payment(osv.osv_memory):
49     _name = 'pos.make.payment'
50     _description = 'Point of Sale Payment'
51     def check(self, cr, uid, ids, context=None):
52         """Check the order:
53         if the order is not paid: continue payment,
54         if the order is paid print ticket.
55         """
56         context = context or {}
57         order_obj = self.pool.get('pos.order')
58         obj_partner = self.pool.get('res.partner')
59         active_id = context and context.get('active_id', False)
60
61         order = order_obj.browse(cr, uid, active_id, context=context)
62         amount = order.amount_total - order.amount_paid
63         data = self.read(cr, uid, ids, context=context)[0]
64         # this is probably a problem of osv_memory as it's not compatible with normal OSV's
65         data['journal'] = data['journal_id'][0]
66
67         if amount != 0.0:
68             order_obj.add_payment(cr, uid, active_id, data, context=context)
69
70         if order_obj.test_paid(cr, uid, [active_id]):
71             wf_service = netsvc.LocalService("workflow")
72             wf_service.trg_validate(uid, 'pos.order', active_id, 'paid', cr)
73             return {'type' : 'ir.actions.act_window_close' }
74          ##self.print_report(cr, uid, ids, context=context)
75
76         return self.launch_payment(cr, uid, ids, context=context)
77
78     def launch_payment(self, cr, uid, ids, context=None):
79         return {
80             'name': _('Paiement'),
81             'view_type': 'form',
82             'view_mode': 'form',
83             'res_model': 'pos.make.payment',
84             'view_id': False,
85             'target': 'new',
86             'views': False,
87             'type': 'ir.actions.act_window',
88             'context': context,
89         }
90
91     def print_report(self, cr, uid, ids, context=None):
92         active_id = context.get('active_id', [])
93         datas = {'ids' : [active_id]}
94         return {
95             'type': 'ir.actions.report.xml',
96             'report_name': 'pos.receipt',
97             'datas': datas,
98         }
99
100     def _default_journal(self, cr, uid, context=None):
101         if not context:
102             context = {}
103         session = False
104         order_obj = self.pool.get('pos.order')
105         active_id = context and context.get('active_id', False)
106         if active_id:
107             order = order_obj.browse(cr, uid, active_id, context=context)
108             session = order.session_id
109         if session:
110             for journal in session.config_id.journal_ids:
111                 return journal.id
112         return False
113
114     def _default_amount(self, cr, uid, context=None):
115         order_obj = self.pool.get('pos.order')
116         active_id = context and context.get('active_id', False)
117         if active_id:
118             order = order_obj.browse(cr, uid, active_id, context=context)
119             return order.amount_total - order.amount_paid
120         return False
121
122     _columns = {
123         'journal_id' : fields.many2one('account.journal', 'Payment Mode', required=True),
124         'amount': fields.float('Amount', digits=(16,2), required= True),
125         'payment_name': fields.char('Payment Reference', size=32),
126         'payment_date': fields.date('Payment Date', required=True),
127     }
128     _defaults = {
129         'journal_id' : _default_journal,
130         'payment_date': time.strftime('%Y-%m-%d %H:%M:%S'),
131         'amount': _default_amount,
132     }
133
134 pos_make_payment()
135
136
137 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: