1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# Author: Guewen Baconnier
5
# Copyright 2012 Camptocamp SA
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 _
27
class sale_order(osv.osv):
29
_inherit = 'sale.order'
31
_columns = {'magento_ref': fields.char('Magento Invoice ID', size=32),
32
'allow_magento_manual_invoice': fields.related(
33
'base_payment_type_id', 'allow_magento_manual_invoice',
35
string="Allow Manual Creation of Magento Invoice")}
37
def _magento_create_invoice(self, cr, uid, order, context=None):
43
'external_referential_type':
44
shop.referential_id.type_id.name, })
46
referential = shop.referential_id
47
connection = referential.external_connection()
49
external_invoice = self._create_external_invoice(
50
cr, uid, order, connection, referential.id, context=ctx)
51
except xmlrpclib.Fault, magento_error:
52
# TODO: in case of error on Magento because the invoice has
53
# already been created, get the invoice number
54
# and store it in magento_ref
56
_('Error'), _("Error on Magento on the invoice creation "
58
"%s" % (order.name, magento_error)))
61
{'magento_ref': external_invoice},
65
self._check_need_to_update_single(
66
cr, uid, order, connection, context=ctx)
69
def button_magento_create_invoice(self, cr, uid, ids, context=None):
70
order = self.browse(cr, uid, ids[0], context=context)
71
if order.state != 'draft':
73
_('Error'), _('This order is not a quotation.'))
75
if not order.is_magento:
77
_('Error'), _('This is not a Magento sale order.'))
79
if not order.base_payment_type_id:
81
_('Error'), _('This order has no external '
82
'payment type settings.'))
84
if not order.base_payment_type_id.allow_magento_manual_invoice:
87
"Manual creation of the invoice on Magento "
88
"is forbidden for external payment : %s" %
89
order.ext_payment_method))
91
# sale_exceptions module methods
92
# in order to check if the order is valid
93
# before create the invoice on magento
94
# it maybe has something to correct
95
exception_ids = self.detect_exceptions(
96
cr, uid, [order.id], context=context)
98
return self._popup_exceptions(
99
cr, uid, order.id, context=context)
101
self._magento_create_invoice(cr, uid, order, context=context)
104
def _prepare_invoice(self, cr, uid, order, lines, context=None):
105
"""Prepare the dict of values to create the new invoice for a
106
sale order. This method may be overridden to implement custom
107
invoice generation (making sure to call super() to establish
108
a clean extension chain).
110
Inherited in order to:
111
Update the Magento invoice id because the invoice has been
112
created from the sale order and it is stored on the sale order
115
:param browse_record order: sale.order record to invoice
116
:param list(int) lines: list of invoice line IDs that must be
117
attached to the invoice
118
:return: dict of value to create() the invoice
120
vals = super(sale_order, self)._prepare_invoice(
121
cr, uid, order, lines, context=context)
122
if order.magento_ref:
123
vals['magento_ref'] = order.magento_ref