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