29299257750d914190c9945352b396e4e536fb9e
[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 netsvc
23 from osv import osv, fields
24 from tools.translate import _
25 import pos_box_entries
26 import time
27
28
29 class pos_make_payment(osv.osv_memory):
30     _name = 'pos.make.payment'
31     _description = 'Point of Sale Payment'
32
33     def default_get(self, cr, uid, fields, context=None):
34         """
35          To get default values for the object.
36          @param self: The object pointer.
37          @param cr: A database cursor
38          @param uid: ID of the user currently logged in
39          @param fields: List of fields for which we want default values
40          @param context: A standard dictionary
41          @return: A dictionary which of fields with values.
42         """
43         if context is None:
44             context = {}
45
46         res = super(pos_make_payment, self).default_get(cr, uid, fields, context=context)
47         record_id = context and context.get('active_id', False)
48         j_obj = self.pool.get('account.journal')
49         company_id = self.pool.get('res.users').browse(cr, uid, uid).company_id.id
50         journal = j_obj.search(cr, uid, [('type', '=', 'cash'), ('company_id', '=', company_id)])
51         if journal:
52             journal = journal[0]
53         else:
54             journal = None
55
56         order = self.pool.get('pos.order').browse(cr, uid, record_id, context)
57         #get amount to pay
58         amount = order.amount_total - order.amount_paid
59
60         if amount <= 0.0:
61             context.update({'flag': True})
62             self.pool.get('pos.order').action_paid(cr, uid, [record_id], context)
63         elif order.amount_paid > 0.0:
64             self.pool.get('pos.order').write(cr, uid, [record_id], {'state': 'advance'})
65
66         invoice_wanted_checked = False
67
68         current_date = time.strftime('%Y-%m-%d')
69
70         if 'journal' in fields:
71             res.update({'journal': journal})
72         if 'amount' in fields:
73             res.update({'amount': amount})
74         if 'invoice_wanted' in fields:
75             res.update({'invoice_wanted': invoice_wanted_checked})
76         if 'payment_date' in fields:
77             res.update({'payment_date': current_date})
78         if 'payment_name'  in fields:
79             res.update({'payment_name': 'Payment'})
80         return res
81
82     def view_init(self, cr, uid, fields_list, context=None):
83         res = super(pos_make_payment, self).view_init(cr, uid, fields_list, context=context)
84         record_id = context and context.get('active_id', False) or False
85         order = self.pool.get('pos.order').browse(cr, uid, record_id)
86         if not order.lines:
87             raise osv.except_osv('Error!', 'No Order Lines ')
88         True
89
90     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
91         """
92              Changes the view dynamically
93
94              @param self: The object pointer.
95              @param cr: A database cursor
96              @param uid: ID of the user currently logged in
97              @param context: A standard dictionary
98
99              @return: New arch of view.
100
101         """
102         record_id = context and context.get('record_id', False) or False
103         res = super(pos_make_payment, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=False)
104
105         if record_id:
106             order = self.pool.get('pos.order').browse(cr, uid, record_id)
107             amount = order.amount_total - order.amount_paid
108             if amount == 0.0:
109                 res['arch'] = """ <form string="Make Payment" colspan="4">
110                                 <group col="2" colspan="2">
111                                     <label string="Do you want to print the Receipt?" colspan="4"/>
112                                     <separator colspan="4"/>
113                                     <button icon="gtk-cancel" special="cancel" string="No" readonly="0"/>
114                                     <button name="print_report" string="Print Receipt" type="object" icon="gtk-print"/>
115                                 </group>
116                             </form>
117                         """
118         return res
119
120     def check(self, cr, uid, ids, context=None):
121
122         """Check the order:
123         if the order is not paid: continue payment,
124         if the order is paid print invoice (if wanted) or ticket.
125         """
126         record_id = context and context.get('active_id', False)
127         order_obj = self.pool.get('pos.order')
128         jrnl_obj = self.pool.get('account.journal')
129         order = order_obj.browse(cr, uid, record_id, context)
130         amount = order.amount_total - order.amount_paid
131         data = self.read(cr, uid, ids)[0]
132
133         # Todo need to check ...
134         if amount != 0.0:
135             invoice_wanted = data['invoice_wanted']
136             order_obj.write(cr, uid, [record_id], {'invoice_wanted': invoice_wanted})
137             order_obj.add_payment(cr, uid, record_id, data, context=context)
138
139         if amount <= 0.0:
140             context.update({'flag': True})
141             order_obj.action_paid(cr, uid, [record_id], context)
142         if order_obj.test_paid(cr, uid, [record_id]):
143             if order.partner_id and order.invoice_wanted:
144                 return self.create_invoice(cr, uid, ids, context)
145             else:
146                 order_obj.write(cr, uid, [record_id],{'state':'paid'})                            
147                 return self.print_report(cr, uid, ids, context)  
148         if order.amount_paid > 0.0:
149             self.pool.get('pos.order').write(cr, uid, [record_id],{'state':'advance'})
150             return self.print_report(cr, uid, ids, context)            
151         return {}
152
153     def create_invoice(self, cr, uid, ids, context):
154         wf_service = netsvc.LocalService("workflow")
155         record_ids = context and context.get('active_ids', False)
156         for i in record_ids:
157             wf_service.trg_validate(uid, 'pos.order', i, 'invoice', cr)
158         datas = {'ids': context.get('active_ids', [])}
159         return {
160             'type': 'ir.actions.report.xml',
161             'report_name': 'pos.invoice',
162             'datas': datas,
163         }
164
165     def print_report(self, cr, uid, ids, context=None):
166         """
167              @summary: To get the date and print the report
168              @param self: The object pointer.
169              @param cr: A database cursor
170              @param uid: ID of the user currently logged in
171              @param context: A standard dictionary
172              @return : retrun report
173         """
174         if context is None:
175             context = {}
176
177         datas = {'ids': context.get('active_ids', [])}
178         res = {}
179         datas['form'] = res
180         return {
181             'type': 'ir.actions.report.xml',
182             'report_name': 'pos.receipt',
183             'datas': datas,
184        }
185
186     def trigger_wkf(self, cr, uid, data, context):
187         record_id = context and context.get('active_id', False)
188         wf_service = netsvc.LocalService("workflow")
189         wf_service.trg_validate(uid, 'pos.order', record_id, 'payment', cr)
190         return {}
191
192     _columns = {
193         'journal': fields.selection(pos_box_entries.get_journal, "Journal", required=True),
194         'product_id': fields.many2one('product.product', "Acompte"),
195         'amount': fields.float('Amount', digits=(16, 2), required= True),
196         'payment_name': fields.char('Payment name', size=32, required=True),
197         'payment_date': fields.date('Payment date', required=True),
198         'is_acc': fields.boolean('Accompte'),
199         'invoice_wanted': fields.boolean('Invoice'),
200         'num_sale': fields.char('Num.File', size=32),
201     }
202
203 pos_make_payment()
204
205 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
206