def _rule_global(self, record, method): """ Rule always executed, whichever is the selected rule """ # the order has been canceled since the job has been created order_id = record['increment_id'] if record['state'] == 'canceled': raise NothingToDoJob('Order %s canceled' % order_id) max_days = method.days_before_cancel if max_days: fmt = '%Y-%m-%d %H:%M:%S' order_date = datetime.strptime(record['created_at'], fmt) if order_date + timedelta(days=max_days) < datetime.now(): raise NothingToDoJob('Import of the order %s canceled ' 'because it has not been paid since %d ' 'days' % (order_id, max_days))
def get_lines_info(): lines_info = self._get_lines_info(binding) if not lines_info: raise NothingToDoJob( _('Canceled: the delivery order does not ' 'contain lines from the original sale order.')) return lines_info
def run(self, binding): """ Export the picking to Magento """ def get_lines_info(): lines_info = self._get_lines_info(binding) if not lines_info: raise NothingToDoJob( _('Canceled: the delivery order does not ' 'contain lines from the original sale order.')) return lines_info if binding.external_id: return _('Already exported') if self.collection.version == '1.7': picking_method = binding.picking_method if picking_method == 'complete': args = self._get_args(binding) elif picking_method == 'partial': lines_info = get_lines_info() args = self._get_args(binding, lines_info) else: raise ValueError("Wrong value for picking_method, authorized " "values are 'partial' or 'complete', " "found: %s" % picking_method) try: external_id = self.backend_adapter.create(*args) except xmlrpc.client.Fault as err: # When the shipping is already created on Magento, it returns: # <Fault 102: u"Impossible de faire # l\'exp\xe9dition de la commande."> if err.faultCode == 102: raise NothingToDoJob( 'Canceled: the delivery order already ' 'exists on Magento (fault 102).') raise else: # Magento 2.x arguments = { 'items': [{ 'order_item_id': key, 'qty': val, } for key, val in get_lines_info().items()] } external_id = self.backend_adapter._call( 'order/%s/ship' % binding.sale_id.magento_bind_ids[0].external_id, arguments, http_method='post') self.binder.bind(external_id, binding) # ensure that we store the external ID if not odoo.tools.config['test_enable']: # pylint: disable=invalid-commit self.env.cr.commit() # noqa
def _rule_global(self, record, mode): """ Rule always executed, whichever is the selected rule """ order_id = record['id'] max_days = mode.days_before_cancel if not max_days: return if self._get_paid_amount(record) != 0.0: return fmt = '%Y-%m-%d %H:%M:%S' order_date = datetime.strptime(record['date_add'], fmt) if order_date + timedelta(days=max_days) < datetime.now(): raise NothingToDoJob('Import of the order %s canceled ' 'because it has not been paid since %d ' 'days' % (order_id, max_days))
def run(self, binding): """ Export the picking to Magento """ if binding.external_id: return _('Already exported') picking_method = binding.picking_method if picking_method == 'complete': args = self._get_args(binding) elif picking_method == 'partial': lines_info = self._get_lines_info(binding) if not lines_info: raise NothingToDoJob(_('Canceled: the delivery order does not ' 'contain lines from the original ' 'sale order.')) args = self._get_args(binding, lines_info) else: raise ValueError("Wrong value for picking_method, authorized " "values are 'partial' or 'complete', " "found: %s" % picking_method) try: external_id = self.backend_adapter.create(*args) except xmlrpclib.Fault as err: # When the shipping is already created on Magento, it returns: # <Fault 102: u"Impossible de faire # l\'exp\xe9dition de la commande."> if err.faultCode == 102: raise NothingToDoJob('Canceled: the delivery order already ' 'exists on Magento (fault 102).') else: raise else: self.binder.bind(external_id, binding) # ensure that we store the external ID if not odoo.tools.config['test_enable']: self.env.cr.commit() # noqa
def _rule_state(self, record, mode): """Check if order is importable by its state. If `backend_record.importable_order_state_ids` is valued we check if current order is in the list. If not, the job fails gracefully. """ if self.backend_record.importable_order_state_ids: ps_state_id = record['current_state'] state = self.binder_for('prestashop.sale.order.state').to_internal( ps_state_id, unwrap=1) if not state: raise FailedJobError( _("The configuration is missing " "for sale order state with PS ID=%s.\n\n" "Resolution:\n" " - Use the automatic import in 'Connectors > PrestaShop " "Backends', button 'Synchronize base data'.") % (ps_state_id, )) if state not in self.backend_record.importable_order_state_ids: raise NothingToDoJob( _('Import of the order with PS ID=%s canceled ' 'because its state is not importable') % record['id'])
def check(self, record): # cannot import this as a dependency since the data source was posted, not imported partner_binder = self.binder_for('magento.res.partner') partner_binding = partner_binder.to_internal(record['CustomerID']) if not partner_binding: raise RetryableJobError( "Couldn't find respective Customer ID - %s " "for the Request ID - %s" % (record['CustomerID'], record['request_id']), seconds=1 * 60) rewards_binder = self.binder_for('magento.res.partner.rewards') rewards_binding = rewards_binder.to_internal(record['CustomerID']) if rewards_binding: existing_date = datetime.strptime( rewards_binding.magento_date_modified, '%Y-%m-%d %H:%M:%S') modified_dt = datetime.strptime(record['ModifiedDate'], '%Y-%m-%d %H:%M:%S') if modified_dt < existing_date: raise NothingToDoJob( "The most recent rewards point already imported with this Request ID - %s.\n" "This is old Request ID - %s. Odoo ID - %s" % (rewards_binding.request_id, record['request_id'], rewards_binding.odoo_id.id))
def _rule_never(self, record, mode): """ Never import the order """ raise NothingToDoJob('Orders with payment modes %s ' 'are never imported.' % record['payment']['method'])