[FIX] set active=true in anlysis report
[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
28
29 class pos_make_payment(osv.osv_memory):
30     _name = 'pos.make.payment'
31     _description = 'Point of Sale Payment'
32     def check(self, cr, uid, ids, context=None):
33         """Check the order:
34         if the order is not paid: continue payment,
35         if the order is paid print ticket.
36         """
37         context = context or {}
38         order_obj = self.pool.get('pos.order')
39         obj_partner = self.pool.get('res.partner')
40         active_id = context and context.get('active_id', False)
41
42         order = order_obj.browse(cr, uid, active_id, context=context)
43         amount = order.amount_total - order.amount_paid
44         data = self.read(cr, uid, ids, context=context)[0]
45         # this is probably a problem of osv_memory as it's not compatible with normal OSV's
46         #data['journal'] = data['journal'][0]
47
48         if amount != 0.0:
49             order_obj.add_payment(cr, uid, active_id, data, context=context)
50
51         if order_obj.test_paid(cr, uid, [active_id]):
52             order_obj.action_paid(cr, uid, [active_id], context=context)
53             return self.print_report(cr, uid, ids, context=context)
54
55         return self.launch_payment(cr, uid, ids, context=context)
56
57     def launch_payment(self, cr, uid, ids, context=None):
58         return {
59             'name': _('Paiement'),
60             'view_type': 'form',
61             'view_mode': 'form',
62             'res_model': 'pos.make.payment',
63             'view_id': False,
64             'target': 'new',
65             'views': False,
66             'type': 'ir.actions.act_window',
67         }
68
69     def print_report(self, cr, uid, ids, context=None):
70         active_id = context.get('active_id', [])
71         datas = {'ids' : [active_id]}
72         return {
73             'type': 'ir.actions.report.xml',
74             'report_name': 'pos.receipt',
75             'datas': datas,
76         }
77
78     def _default_journal(self, cr, uid, context=None):
79         res = pos_box_entries.get_journal(self, cr, uid, context=context)
80         return len(res)>1 and res[1][0] or False
81
82     def _default_amount(self, cr, uid, context=None):
83         order_obj = self.pool.get('pos.order')
84         active_id = context and context.get('active_id', False)
85         if active_id:
86             order = order_obj.browse(cr, uid, active_id, context=context)
87             return order.amount_total - order.amount_paid
88         return False
89
90     _columns = {
91         'journal': fields.selection(pos_box_entries.get_journal, "Payment Mode", required=True),
92         'amount': fields.float('Amount', digits=(16,2), required= True),
93         'payment_name': fields.char('Payment Reference', size=32),
94         'payment_date': fields.date('Payment Date', required=True),
95     }
96     _defaults = {
97         'payment_date': time.strftime('%Y-%m-%d %H:%M:%S'),
98         'amount': _default_amount,
99         'journal': _default_journal
100     }
101
102 pos_make_payment()
103