[openerp] corrige un oubli d'imports.
[burette/pos_membership.git] / pos_membership.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 # POS Membership module for OpenERP, Manage membership payments from POS.
5 # Copyright (C) 2013 L'Heureux Cyclage (<http://www.heureux-cyclage.org>)
6 #
7 # This file is a part of POS Membership
8 #
9 # POS Membership is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # ReMembership is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #
22 ##############################################################################
23 from openerp import netsvc
24 from openerp.osv import fields, osv
25 from openerp.tools.translate import _
26
27 class pos_order(osv.osv):
28 _inherit = 'pos.order'
29
30 def _check_membership_product(self, cr, uid, ids, context=None):
31 #TODO : check condition and return boolean accordingly
32 if not context is None:
33 context = {}
34 order_line_obj = self.pool.get('pos.order.line')
35 data_order_line = order_line_obj.browse(cr, uid, ids, context=context)
36 for data in data_order_line:
37 print("DEV: [pos_membership] [_check_membership_product] [order_line]: data=%s product=%s" % (str(data.id), str(data.product_id))),
38 return True
39
40 _constraints = [
41 (_check_membership_product, 'Error: Invalid Message', ['field_name']),
42 ]
43
44 # XXX: copied from openerp/addons/point_of_sale/point_of_sale.py
45 def action_invoice(self, cr, uid, ids, context=None):
46 print ("DEV: [pos_membership] [pos_order] [action_invoice]");
47 wf_service = netsvc.LocalService("workflow")
48 inv_ref = self.pool.get('account.invoice')
49 inv_line_ref = self.pool.get('account.invoice.line')
50 product_obj = self.pool.get('product.product')
51 inv_ids = []
52
53 for order in self.pool.get('pos.order').browse(cr, uid, ids, context=context):
54 if order.invoice_id:
55 inv_ids.append(order.invoice_id.id)
56 continue
57
58 if not order.partner_id:
59 raise osv.except_osv(_('Error!'), _('Please provide a partner for the sale.'))
60
61 acc = order.partner_id.property_account_receivable.id
62 # XXX: copied from openerp/addons/point_of_sale/point_of_sale.py to fix account.invoice.line creation
63 inv = {
64 'name': order.name,
65 'origin': order.name,
66 'account_id': acc,
67 'journal_id': order.sale_journal.id or None,
68 'type': 'out_invoice',
69 'reference': order.name,
70 'partner_id': order.partner_id.id,
71 'comment': order.note or '',
72 'currency_id': order.pricelist_id.currency_id.id, # considering partner's sale pricelist's currency
73 'invoice_line': []
74 }
75 inv.update(inv_ref.onchange_partner_id(cr, uid, [], 'out_invoice', order.partner_id.id)['value'])
76 if not inv.get('account_id', None):
77 inv['account_id'] = acc
78 for line in order.lines:
79 inv_line = {
80 #'invoice_id': inv_id,
81 'product_id': line.product_id.id,
82 'quantity': line.qty,
83 }
84 inv_name = product_obj.name_get(cr, uid, [line.product_id.id], context=context)[0][1]
85 inv_line.update(inv_line_ref.product_id_change(cr, uid, [],
86 line.product_id.id,
87 line.product_id.uom_id.id,
88 line.qty, partner_id = order.partner_id.id,
89 fposition_id=order.partner_id.property_account_position.id)['value'])
90 if line.product_id.description_sale:
91 inv_line['note'] = line.product_id.description_sale
92 inv_line['price_unit'] = line.price_unit
93 inv_line['discount'] = line.discount
94 inv_line['name'] = inv_name
95 inv_line['invoice_line_tax_id'] = ('invoice_line_tax_id' in inv_line)\
96 and [(6, 0, inv_line['invoice_line_tax_id'])] or []
97 #inv_line_ref.create(cr, uid, inv_line, context=context)
98 inv['invoice_line'].append((0, 0, inv_line))
99 inv_id = inv_ref.create(cr, uid, inv, context=context)
100
101 self.write(cr, uid, [order.id], {'invoice_id': inv_id, 'state': 'invoiced'}, context=context)
102 inv_ids.append(inv_id)
103 inv_ref.button_reset_taxes(cr, uid, [inv_id], context=context)
104 wf_service.trg_validate(uid, 'pos.order', order.id, 'invoice', cr)
105
106 if not inv_ids: return {}
107
108 mod_obj = self.pool.get('ir.model.data')
109 res = mod_obj.get_object_reference(cr, uid, 'account', 'invoice_form')
110 res_id = res and res[1] or False
111 return {
112 'name': _('Customer Invoice'),
113 'view_type': 'form',
114 'view_mode': 'form',
115 'view_id': [res_id],
116 'res_model': 'account.invoice',
117 'context': "{'type':'out_invoice'}",
118 'type': 'ir.actions.act_window',
119 'nodestroy': True,
120 'target': 'current',
121 'res_id': inv_ids and inv_ids[0] or False,
122 }
123
124 pos_order()
125
126 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: