bugfixd in pos,invoice_layout,improved reports and sxw files(ref:vra)
[odoo/odoo.git] / addons / point_of_sale / wizard / wizard_pos_payment.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import pooler
24 import netsvc
25 import wizard
26 import time
27
28
29 def _get_journal(self, cr, uid, context):
30     pool = pooler.get_pool(cr.dbname)
31     obj = pool.get('account.journal')
32     ids = obj.search(cr, uid, [('type', '=', 'cash')])
33     res = obj.read(cr, uid, ids, ['id', 'name'], context)
34     res = [(r['id'], r['name']) for r in res]
35     return res
36
37
38 payment_form = """<?xml version="1.0"?>
39 <form string="Add payment :">
40     <field name="amount" />
41     <field name="journal"/>
42     <field name="payment_id" />
43     <field name="payment_date" />
44     <field name="payment_nb" />
45     <field name="payment_name" />
46     <field name="invoice_wanted" />
47 </form>
48 """
49
50 payment_fields = {
51     'amount': {'string': 'Amount', 'type': 'float', 'required': True},
52     'invoice_wanted': {'string': 'Invoice', 'type': 'boolean'},
53     'journal': {'string': 'Journal',
54             'type': 'selection',
55             'selection': _get_journal,
56             'required': True,
57         },
58     'payment_id': {'string': 'Payment Term', 'type': 'many2one', 'relation': 'account.payment.term', 'required': True},
59     'payment_date': {'string': 'Payment date', 'type': 'date', 'required': True},
60     'payment_name': {'string': 'Payment name', 'type': 'char', 'size': '32'},
61     'payment_nb': {'string': 'Piece number', 'type': 'char', 'size': '32'},
62     }
63
64
65 def _pre_init(self, cr, uid, data, context):
66
67     def _get_journal(pool, order):
68         j_obj = pool.get('account.journal')
69
70         journal_to_fetch = 'DEFAULT'
71         if order.amount_total < 0:
72             journal_to_fetch = 'GIFT'
73         else:
74             if order.amount_paid > 0:
75                 journal_to_fetch = 'REBATE'
76
77         pos_config_journal = pool.get('pos.config.journal')
78         ids = pos_config_journal.search(cr, uid, [('code', '=', journal_to_fetch)])
79         objs = pos_config_journal.browse(cr, uid, ids)
80         if objs:
81             journal = objs[0].journal_id.id
82         else:
83             existing = [payment.journal_id.id for payment in order.payments]
84             ids = j_obj.search(cr, uid, [('type', '=', 'cash')])
85             for i in ids:
86                 if i not in existing:
87                     journal = i
88                     break
89             if not journal:
90                 journal = ids[0]
91
92         return journal
93
94     pool = pooler.get_pool(cr.dbname)
95     order = pool.get('pos.order').browse(cr, uid, data['id'], context)
96
97     # get amount to pay:
98     amount = order.amount_total - order.amount_paid
99
100     # get journal:
101     journal = _get_journal(pool, order)
102
103     # check if an invoice is wanted:
104     #invoice_wanted_checked = not not order.partner_id # not not -> boolean
105     invoice_wanted_checked = False
106
107     # select the current date
108     current_date = time.strftime('%Y-%m-%d')
109
110     return {'journal': journal, 'amount': amount, 'invoice_wanted': invoice_wanted_checked, 'payment_date': current_date}
111
112
113 def _add_pay(self, cr, uid, data, context):
114     pool = pooler.get_pool(cr.dbname)
115     order_obj = pool.get('pos.order')
116     result = data['form']
117     invoice_wanted = data['form']['invoice_wanted']
118     # add 'invoice_wanted' in 'pos.order'
119     order_obj.write(cr, uid, [data['id']], {'invoice_wanted': invoice_wanted})
120
121     order_obj.add_payment(cr, uid, data['id'], result, context=context)
122     return {}
123
124 def _validate(self, cr, uid, data, context):
125     pool = pooler.get_pool(cr.dbname)
126     order_obj = pool.get('pos.order')
127     order = order_obj.browse(cr, uid, data['id'], context)
128 #    if not order.amount_total:
129 #       return 'receipt'
130     order_obj.test_order_lines(cr, uid, order, context=context)
131     return {}
132     
133 def _check(self, cr, uid, data, context):
134     """Check the order:
135     if the order is not paid: continue payment,
136     if the order is paid print invoice (if wanted) or ticket.
137     """
138     pool = pooler.get_pool(cr.dbname)
139     order_obj = pool.get('pos.order')
140     order = order_obj.browse(cr, uid, data['id'], context)
141     action = 'ask_pay'
142     if order.state == 'paid':
143         if order.partner_id:
144             if order.invoice_wanted:
145                 action = 'invoice'
146             else:
147                 action = 'paid'
148         else:
149             action = 'receipt'
150     return action
151
152
153 def _test_no_line(self, cr, uid, data, context):
154     pool = pooler.get_pool(cr.dbname)
155     order = pool.get('pos.order').browse(cr, uid, data['id'], context)
156
157     if not order.lines:
158         raise wizard.except_wizard(_('Error'), _('No order lines defined for this sale.'))
159
160     return {}
161
162
163 def create_invoice(self, cr, uid, data, context):
164     pool = pooler.get_pool(cr.dbname)
165     order_obj = pool.get('pos.order')
166     order = order_obj.browse(cr, uid, data['id'], context)
167     if not order.invoice_id:
168         inv_id = order_obj.action_invoice(cr,uid,[data['id']])
169         #raise wizard.except_wizard(_('Error !'), _('Please create an invoice for this sale.'))
170 #    wf_service = netsvc.LocalService("workflow")
171 #    for i in data['ids']:
172 #        wf_service.trg_validate(uid, 'pos.order', i, 'invoice', cr)
173     return {}
174
175
176 class pos_payment(wizard.interface):
177     states = {
178         'init': {
179             'actions': [_validate],
180             'result': {
181                 'type': 'choice',
182                 'next_state': _check,
183             }
184         },
185         'ask_pay': {
186             'actions': [_pre_init],
187             'result': {
188                 'type': 'form',
189                 'arch': payment_form,
190                 'fields': payment_fields,
191                 'state': (('end', 'Cancel'), ('add_pay', 'Ma_ke payment', 'gtk-ok', True)
192                          )
193             }
194         },
195         'add_pay': {
196             'actions': [_add_pay],
197             'result': {
198                 'type': 'state',
199                 'state': "init",
200             }
201         },
202         'invoice': {
203             'actions': [create_invoice],
204             'result': {
205                 'type': 'print',
206                 'report': 'pos.invoice',
207                 'state': 'end'
208             }
209         },
210         'receipt': {
211             'actions': [],
212             'result': {
213                 'type': 'print',
214                 'report': 'pos.receipt',
215                 'state': 'end'
216             }
217         },
218         'paid': {
219             'actions': [],
220             'result': {
221                 'type': 'state',
222                 'state': 'end'
223             }
224         },
225
226     }
227
228 pos_payment('pos.payment')
229
230 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
231