1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
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.
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.
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/>.
21 ##############################################################################
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]
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" />
51 'amount': {'string': 'Amount', 'type': 'float', 'required': True},
52 'invoice_wanted': {'string': 'Invoice', 'type': 'boolean'},
53 'journal': {'string': 'Journal',
55 'selection': _get_journal,
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'},
65 def _pre_init(self, cr, uid, data, context):
67 def _get_journal(pool, order):
68 j_obj = pool.get('account.journal')
70 journal_to_fetch = 'DEFAULT'
71 if order.amount_total < 0:
72 journal_to_fetch = 'GIFT'
74 if order.amount_paid > 0:
75 journal_to_fetch = 'REBATE'
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)
81 journal = objs[0].journal_id.id
83 existing = [payment.journal_id.id for payment in order.payments]
84 ids = j_obj.search(cr, uid, [('type', '=', 'cash')])
94 pool = pooler.get_pool(cr.dbname)
95 order = pool.get('pos.order').browse(cr, uid, data['id'], context)
98 amount = order.amount_total - order.amount_paid
101 journal = _get_journal(pool, order)
103 # check if an invoice is wanted:
104 #invoice_wanted_checked = not not order.partner_id # not not -> boolean
105 invoice_wanted_checked = False
107 # select the current date
108 current_date = time.strftime('%Y-%m-%d')
110 return {'journal': journal, 'amount': amount, 'invoice_wanted': invoice_wanted_checked, 'payment_date': current_date}
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})
121 order_obj.add_payment(cr, uid, data['id'], result, context=context)
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:
130 order_obj.test_order_lines(cr, uid, order, context=context)
133 def _check(self, cr, uid, data, context):
135 if the order is not paid: continue payment,
136 if the order is paid print invoice (if wanted) or ticket.
138 pool = pooler.get_pool(cr.dbname)
139 order_obj = pool.get('pos.order')
140 order = order_obj.browse(cr, uid, data['id'], context)
142 if order.state == 'paid':
144 if order.invoice_wanted:
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)
158 raise wizard.except_wizard(_('Error'), _('No order lines defined for this sale.'))
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)
176 class pos_payment(wizard.interface):
179 'actions': [_validate],
182 'next_state': _check,
186 'actions': [_pre_init],
189 'arch': payment_form,
190 'fields': payment_fields,
191 'state': (('end', 'Cancel'), ('add_pay', 'Ma_ke payment', 'gtk-ok', True)
196 'actions': [_add_pay],
203 'actions': [create_invoice],
206 'report': 'pos.invoice',
214 'report': 'pos.receipt',
228 pos_payment('pos.payment')
230 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: