~magentoerpconnect-core-editors/magentoerpconnect/trunk_version

« back to all changes in this revision

Viewing changes to magentoerpconnect_payment/sale.py

  • Committer: Guewen Baconnier @ Camptocamp
  • Date: 2012-03-29 06:11:58 UTC
  • mfrom: (583 magentoerpconnect)
  • mto: This revision was merged to the branch mainline in revision 586.
  • Revision ID: guewen.baconnier@camptocamp.com-20120329061158-oc0lo4r90z2p1mud
[MRG] from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    Author: Guewen Baconnier
 
5
#    Copyright 2012 Camptocamp SA
 
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
import xmlrpclib
 
22
 
 
23
from osv import osv, fields
 
24
from tools.translate import _
 
25
 
 
26
 
 
27
class sale_order(osv.osv):
 
28
 
 
29
    _inherit = 'sale.order'
 
30
 
 
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',
 
34
                type='boolean',
 
35
                string="Allow Manual Creation of Magento Invoice")}
 
36
 
 
37
    def _magento_create_invoice(self, cr, uid, order, context=None):
 
38
        shop = order.shop_id
 
39
 
 
40
        ctx = context.copy()
 
41
        ctx.update({
 
42
            'shop_id': shop.id,
 
43
            'external_referential_type':
 
44
                shop.referential_id.type_id.name, })
 
45
 
 
46
        referential = shop.referential_id
 
47
        connection = referential.external_connection()
 
48
        try:
 
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
 
55
            raise osv.except_osv(
 
56
                _('Error'), _("Error on Magento on the invoice creation "
 
57
                              "for order %s :\n" \
 
58
                              "%s" % (order.name, magento_error)))
 
59
        self.write(
 
60
            cr, uid, order.id,
 
61
            {'magento_ref': external_invoice},
 
62
            context=context)
 
63
        cr.commit()
 
64
 
 
65
        self._check_need_to_update_single(
 
66
            cr, uid, order, connection, context=ctx)
 
67
        return True
 
68
 
 
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':
 
72
            raise osv.except_osv(
 
73
                _('Error'), _('This order is not a quotation.'))
 
74
 
 
75
        if not order.is_magento:
 
76
            raise osv.except_osv(
 
77
                _('Error'), _('This is not a Magento sale order.'))
 
78
 
 
79
        if not order.base_payment_type_id:
 
80
            raise osv.except_osv(
 
81
                _('Error'), _('This order has no external '
 
82
                              'payment type settings.'))
 
83
 
 
84
        if not order.base_payment_type_id.allow_magento_manual_invoice:
 
85
            raise osv.except_osv(
 
86
                _('Error'), _(
 
87
                    "Manual creation of the invoice on Magento "
 
88
                    "is forbidden for external payment : %s" %
 
89
                    order.ext_payment_method))
 
90
 
 
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)
 
97
        if exception_ids:
 
98
            return self._popup_exceptions(
 
99
                cr, uid, order.id,  context=context)
 
100
 
 
101
        self._magento_create_invoice(cr, uid, order, context=context)
 
102
        return True
 
103
 
 
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).
 
109
 
 
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
 
113
           first.
 
114
 
 
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
 
119
        """
 
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
 
124
        return vals
 
125
 
 
126
sale_order()