1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
23 from osv import osv, fields
24 from tools.translate import _
25 import pos_box_entries
29 class pos_make_payment(osv.osv_memory):
30 _name = 'pos.make.payment'
31 _description = 'Point of Sale Payment'
33 def default_get(self, cr, uid, fields, context=None):
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.
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)])
56 order_obj=self.pool.get('pos.order')
57 order = order_obj.browse(cr, uid, record_id, context)
59 amount = order.amount_total - order.amount_paid
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'})
67 invoice_wanted_checked = False
69 current_date = time.strftime('%Y-%m-%d')
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'})
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)
88 raise osv.except_osv('Error!', 'No Order Lines ')
91 def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
93 Changes the view dynamically
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
100 @return: New arch of view.
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)
107 order = self.pool.get('pos.order').browse(cr, uid, record_id)
108 amount = order.amount_total - order.amount_paid
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"/>
121 def check(self, cr, uid, ids, context=None):
124 if the order is not paid: continue payment,
125 if the order is paid print invoice (if wanted) or ticket.
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]
134 # Todo need to check ...
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)
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)
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)
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)
158 wf_service.trg_validate(uid, 'pos.order', i, 'invoice', cr)
159 datas = {'ids': context.get('active_ids', [])}
161 'type': 'ir.actions.report.xml',
162 'report_name': 'pos.invoice',
166 def print_report(self, cr, uid, ids, context=None):
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
178 datas = {'ids': context.get('active_ids', [])}
182 'type': 'ir.actions.report.xml',
183 'report_name': 'pos.receipt',
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),
200 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: