Merge lp:~camptocamp/openerp-connector-magento/7.0-copy-quotation-move-binding into lp:~openerp-connector-core-editors/openerp-connector-magento/7.0

Proposed by Guewen Baconnier @ Camptocamp
Status: Rejected
Rejected by: Guewen Baconnier @ Camptocamp
Proposed branch: lp:~camptocamp/openerp-connector-magento/7.0-copy-quotation-move-binding
Merge into: lp:~openerp-connector-core-editors/openerp-connector-magento/7.0
Diff against target: 171 lines (+126/-1)
3 files modified
magentoerpconnect/sale.py (+63/-1)
magentoerpconnect/tests/__init__.py (+2/-0)
magentoerpconnect/tests/test_sale_order.py (+61/-0)
To merge this branch: bzr merge lp:~camptocamp/openerp-connector-magento/7.0-copy-quotation-move-binding
Reviewer Review Type Date Requested Status
OpenERP Connector Core Editors Pending
Review via email: mp+226137@code.launchpad.net

Description of the change

When a sales order is canceled, when one uses the 'New Copy from Quotation' button,
the binding are moved from the old (canceled) order to the new one, so the
synchronizations now happen on the new order.

Warning: needs https://github.com/odoo/odoo/pull/1025 to be merged!

To post a comment you must log in.
1007. By Guewen Baconnier @ Camptocamp

remove unused imports

Revision history for this message
Guewen Baconnier @ Camptocamp (gbaconnier-c2c) wrote :

Unmerged revisions

1007. By Guewen Baconnier @ Camptocamp

remove unused imports

1006. By Guewen Baconnier @ Camptocamp

When a sales order is canceled, when one uses the 'New Copy from Quotation' button,
the binding are moved from the old (canceled) order to the new one, so the
synchronizations now happen on the new order.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'magentoerpconnect/sale.py'
2--- magentoerpconnect/sale.py 2014-05-26 11:25:41 +0000
3+++ magentoerpconnect/sale.py 2014-07-09 14:04:57 +0000
4@@ -128,6 +128,28 @@
5 default=default,
6 context=context)
7
8+ def copy_quotation(self, cr, uid, ids, context=None):
9+ if isinstance(ids, (tuple, list)):
10+ assert len(ids) == 1, "1 ID expected, got %s" % ids
11+ if context is None:
12+ context = {}
13+ else:
14+ context = context.copy()
15+ context['__copy_from_quotation'] = True
16+ result = super(sale_order, self).copy_quotation(cr, uid, ids,
17+ context=context)
18+ # link binding of the canceled order to the new order, so the
19+ # operations done on the new order will be sync'ed with Magento
20+ new_id = result['res_id']
21+ binding_obj = self.pool['magento.sale.order']
22+ binding_ids = binding_obj.search(cr, uid,
23+ [('openerp_id', '=', ids[0])],
24+ context=context)
25+ binding_obj.write(cr, uid, binding_ids,
26+ {'openerp_id': new_id},
27+ context=context)
28+ return result
29+
30
31 class magento_sale_order_line(orm.Model):
32 _name = 'magento.sale.order.line'
33@@ -199,13 +221,53 @@
34 string="Magento Bindings"),
35 }
36
37+ def create(self, cr, uid, vals, context=None):
38+ if context is None:
39+ context= {}
40+
41+ old_line_id = None
42+ if context.get('__copy_from_quotation'):
43+ # when we are copying a sale.order from a canceled one,
44+ # the id of the copied line is inserted in the vals
45+ # in `copy_data`.
46+ old_line_id = vals.pop('__copy_from_line_id', None)
47+ new_id = super(sale_order_line, self).create(cr, uid, vals,
48+ context=context)
49+ if old_line_id:
50+ # link binding of the canceled order lines to the new order
51+ # lines, happens when we are using the 'New Copy of
52+ # Quotation' button on a canceled sales order
53+ binding_obj = self.pool['magento.sale.order.line']
54+ binding_ids = binding_obj.search(
55+ cr, uid,
56+ [('openerp_id', '=', old_line_id)],
57+ context=context)
58+ if binding_ids:
59+ binding_obj.write(cr, uid, binding_ids,
60+ {'openerp_id': new_id},
61+ context=context)
62+ return new_id
63+
64 def copy_data(self, cr, uid, id, default=None, context=None):
65 if default is None:
66 default = {}
67+ if context is None:
68+ context= {}
69+
70 default['magento_bind_ids'] = False
71- return super(sale_order_line, self).copy_data(cr, uid, id,
72+ data = super(sale_order_line, self).copy_data(cr, uid, id,
73 default=default,
74 context=context)
75+ if context.get('__copy_from_quotation'):
76+ # copy_data is called by `copy` of the sale.order which
77+ # builds a dict for the full new sale order, so we lose the
78+ # association between the old and the new line.
79+ # Keep a trace of the old id in the vals that will be passed
80+ # to `create`, from there, we'll be able to update the
81+ # Magento bindings, modifying the relation from the old to
82+ # the new line.
83+ data['__copy_from_line_id'] = id
84+ return data
85
86
87 @magento
88
89=== modified file 'magentoerpconnect/tests/__init__.py'
90--- magentoerpconnect/tests/__init__.py 2014-05-26 09:37:00 +0000
91+++ magentoerpconnect/tests/__init__.py 2014-07-09 14:04:57 +0000
92@@ -24,6 +24,7 @@
93 import test_export_invoice
94 import test_import_product_image
95 import test_related_action
96+import test_sale_order
97
98
99 fast_suite = [
100@@ -35,4 +36,5 @@
101 test_export_invoice,
102 test_import_product_image,
103 test_related_action,
104+ test_sale_order,
105 ]
106
107=== added file 'magentoerpconnect/tests/test_sale_order.py'
108--- magentoerpconnect/tests/test_sale_order.py 1970-01-01 00:00:00 +0000
109+++ magentoerpconnect/tests/test_sale_order.py 2014-07-09 14:04:57 +0000
110@@ -0,0 +1,61 @@
111+# -*- coding: utf-8 -*-
112+##############################################################################
113+#
114+# Author: Guewen Baconnier
115+# Copyright 2014 Camptocamp SA
116+#
117+# This program is free software: you can redistribute it and/or modify
118+# it under the terms of the GNU Affero General Public License as
119+# published by the Free Software Foundation, either version 3 of the
120+# License, or (at your option) any later version.
121+#
122+# This program is distributed in the hope that it will be useful,
123+# but WITHOUT ANY WARRANTY; without even the implied warranty of
124+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
125+# GNU Affero General Public License for more details.
126+#
127+# You should have received a copy of the GNU Affero General Public License
128+# along with this program. If not, see <http://www.gnu.org/licenses/>.
129+#
130+##############################################################################
131+
132+from openerp.addons.magentoerpconnect.unit.import_synchronizer import (
133+ import_record)
134+import openerp.tests.common as common
135+from .common import (mock_api,
136+ mock_urlopen_image)
137+from .test_data import magento_base_responses
138+from .test_synchronization import SetUpMagentoSynchronized
139+
140+DB = common.DB
141+ADMIN_USER_ID = common.ADMIN_USER_ID
142+
143+
144+class TestSaleOrder(SetUpMagentoSynchronized):
145+
146+ def test_sale_order_copy_quotation(self):
147+ """ Copy a sales order with copy_quotation move bindings """
148+ backend_id = self.backend_id
149+ with mock_api(magento_base_responses):
150+ with mock_urlopen_image():
151+ import_record(self.session,
152+ 'magento.sale.order',
153+ backend_id, 900000691)
154+ MagentoOrder = self.registry('magento.sale.order')
155+ MagentoLine = self.registry('magento.sale.order.line')
156+ SaleOrder = self.registry('sale.order')
157+ mag_order_ids = MagentoOrder.search(self.cr,
158+ self.uid,
159+ [('backend_id', '=', backend_id),
160+ ('magento_id', '=', '900000691')])
161+ self.assertEqual(len(mag_order_ids), 1)
162+ mag_order = MagentoOrder.browse(self.cr, self.uid, mag_order_ids[0])
163+ order = mag_order.openerp_id
164+ action = SaleOrder.copy_quotation(self.cr, self.uid, [order.id])
165+ new_id = action['res_id']
166+ order.refresh()
167+ self.assertFalse(order.magento_bind_ids)
168+ mag_order.refresh()
169+ self.assertEqual(mag_order.openerp_id.id, new_id)
170+ for mag_line in mag_order.magento_order_line_ids:
171+ self.assertEqual(mag_line.order_id.id, new_id)