Skip to content

Commit eb300b1

Browse files
committed
Merge pull request #1 from guewen/export-new-expected-date
Export new expected date
2 parents cd3db23 + 10cafea commit eb300b1

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed

specific-parts/specific-addons/specific_magento/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from . import sale
3232
from . import partner
3333
from . import stock
34+
from . import stock_move
3435
from . import supplier
3536
from . import wizard
3637

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding: utf-8 -*-
2+
##############################################################################
3+
#
4+
# Author: Guewen Baconnier
5+
# Copyright 2014 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+
22+
from openerp.osv import orm
23+
from openerp.tools.translate import _
24+
25+
26+
def open_direct(session, job, id_pos=3):
27+
""" Open a form view with the record.
28+
29+
:param id_pos: position of the record ID in the args
30+
"""
31+
model = job.args[0]
32+
# shift one to the left because session is not in job.args
33+
record_id = job.args[id_pos - 1]
34+
action = {
35+
'name': _('Related Record'),
36+
'type': 'ir.actions.act_window',
37+
'view_type': 'form',
38+
'view_mode': 'form',
39+
}
40+
record = session.browse(model, record_id)
41+
if not record.exists():
42+
raise orm.except_orm(_('Error'),
43+
_('The record has been deleted'))
44+
action.update({
45+
'res_model': model,
46+
'res_id': record_id,
47+
})
48+
return action

specific-parts/specific-addons/specific_magento/sale.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@
2020
##############################################################################
2121

2222
import logging
23+
from datetime import datetime
2324

25+
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
2426
from openerp.tools.translate import _
2527

2628
from openerp.addons.connector.unit.mapper import (
2729
ImportMapChild,
2830
mapping,
2931
)
3032
from openerp.addons.connector_ecommerce.sale import SpecialOrderLineBuilder
33+
from openerp.addons.magentoerpconnect.unit.backend_adapter import (
34+
MAGENTO_DATETIME_FORMAT,
35+
)
3136
from openerp.addons.magentoerpconnect.sale import (
3237
SaleOrderImport,
3338
SaleOrderImportMapper,
@@ -95,6 +100,23 @@ def search(self, filters=None, from_date=None, to_date=None,
95100
}
96101
return self._call('%s.search' % self._magento_model, [arguments])
97102

103+
def set_expected_date(self, id, item_id, expected_date):
104+
""" Update the delivery date on Magento
105+
106+
:param id: the ID of the sale on Magento
107+
:param item_id: the ID of the sale line on Magento
108+
:param expected_date: the date
109+
"""
110+
new_date = datetime.strptime(expected_date,
111+
DEFAULT_SERVER_DATETIME_FORMAT)
112+
new_date = new_date.strftime(MAGENTO_DATETIME_FORMAT)
113+
# TODO, use the correct method name and arguments in _call
114+
_logger.info('%s.set_expected_date(%s)',
115+
self._magento_model,
116+
[id, item_id, new_date])
117+
# return self._call('%s.set_expected_date',
118+
# [id, item_id, new_date])
119+
98120

99121
@magento_debonix
100122
class FidelityLineBuilder(SpecialOrderLineBuilder):
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# -*- coding: utf-8 -*-
2+
##############################################################################
3+
#
4+
# Author: Guewen Baconnier
5+
# Copyright 2014 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+
22+
from datetime import datetime
23+
from openerp.osv import orm
24+
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
25+
from openerp.tools.translate import _
26+
from openerp.addons.connector.queue.job import job, related_action
27+
from openerp.addons.connector.session import ConnectorSession
28+
from openerp.addons.connector.unit.synchronizer import ExportSynchronizer
29+
from openerp.addons.magentoerpconnect.connector import get_environment
30+
from openerp.addons.magentoerpconnect.unit.backend_adapter import (
31+
GenericAdapter,
32+
)
33+
from .backend import magento_debonix
34+
from .related_action import open_direct
35+
36+
37+
class stock_move(orm.Model):
38+
_inherit = 'stock.move'
39+
40+
def write(self, cr, uid, ids, vals, context=None):
41+
if vals.get('date_expected'):
42+
self._on_write_expected_date(cr, uid, ids, vals['date_expected'],
43+
context=context)
44+
_super = super(stock_move, self)
45+
return _super.write(cr, uid, ids, vals, context=context)
46+
47+
def _on_write_expected_date(self, cr, uid, ids, date_expected,
48+
context=None):
49+
if context is None:
50+
context = {}
51+
if isinstance(ids, (int, long)):
52+
ids = [ids]
53+
if context.get('connector_no_export'):
54+
return
55+
session = ConnectorSession(cr, uid, context=context)
56+
for move in self.browse(cr, uid, ids, context=context):
57+
expected = datetime.strptime(date_expected,
58+
DEFAULT_SERVER_DATETIME_FORMAT)
59+
previous = datetime.strptime(move.date_expected,
60+
DEFAULT_SERVER_DATETIME_FORMAT)
61+
if expected.date() == previous.date():
62+
# only export if the date changed, we don't want to spam
63+
# with changes of only a few hours
64+
continue
65+
picking = move.picking_id
66+
if not picking:
67+
continue
68+
if picking.type != 'out':
69+
continue
70+
sale_line = move.sale_line_id
71+
if not sale_line:
72+
continue
73+
for binding in sale_line.magento_bind_ids:
74+
export_move_expected_date.delay(session,
75+
self._name,
76+
binding.backend_id.id,
77+
move.id)
78+
79+
80+
@job
81+
@related_action(action=open_direct)
82+
def export_move_expected_date(session, model_name, backend_id, record_id):
83+
""" Export the new expected delivery date of a stock move """
84+
env = get_environment(session, model_name, backend_id)
85+
exporter = env.get_connector_unit(MoveExpectedDateExport)
86+
return exporter.run(record_id)
87+
88+
89+
@magento_debonix
90+
class MoveExpectedDateExport(ExportSynchronizer):
91+
_model_name = 'stock.move'
92+
93+
def run(self, record_id):
94+
""" Export the new expected date to Magento """
95+
move = self.session.browse(self.model._name, record_id)
96+
sale_line = move.sale_line_id
97+
if not sale_line:
98+
return
99+
sale = sale_line.order_id
100+
sale_binder = self.get_binder_for_model('magento.sale.order')
101+
magento_sale_id = sale_binder.to_backend(sale.id, wrap=True)
102+
if not magento_sale_id:
103+
# cannot find the sale for this move, exit
104+
return
105+
sale_line_binder = self.get_binder_for_model('magento.sale.order.line')
106+
magento_sale_line_id = sale_line_binder.to_backend(sale_line.id,
107+
wrap=True)
108+
if not magento_sale_line_id:
109+
return _('Not a Magento order line')
110+
111+
adapter = self.get_connector_unit_for_model(GenericAdapter,
112+
'magento.sale.order')
113+
adapter.set_expected_date(magento_sale_id,
114+
magento_sale_line_id,
115+
move.date_expected)

0 commit comments

Comments
 (0)