|
| 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