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