Skip to content

Commit

Permalink
Merge pull request #56 from tarunbhardwaj/import-error-6678
Browse files Browse the repository at this point in the history
Handle case when magento sale_order product is missing #6678
  • Loading branch information
prakashpp committed Jan 12, 2015
2 parents c91fc76 + 9cdba01 commit acd4f71
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 6 deletions.
3 changes: 2 additions & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
ExportInventoryStart, ExportInventory, StorePriceTier,
ExportTierPricesStart, ExportTierPrices, ExportTierPricesStatus,
ExportShipmentStatusStart, ExportShipmentStatus, ImportOrderStatesStart,
ImportOrderStates, ImportCarriersStart, ImportCarriers
ImportOrderStates, ImportCarriersStart, ImportCarriers, MagentoException
)
from party import Party, MagentoWebsiteParty, Address
from product import (
Expand Down Expand Up @@ -54,6 +54,7 @@ def register():
Party,
MagentoWebsiteParty,
Category,
MagentoException,
MagentoInstanceCategory,
Template,
MagentoWebsiteTemplate,
Expand Down
12 changes: 12 additions & 0 deletions magento.xml
Original file line number Diff line number Diff line change
Expand Up @@ -354,5 +354,17 @@
<field name="name">export_shipment_status_start_form</field>
</record>

<!--Magento Exception-->
<record model="ir.ui.view" id="magento_exception_form_view">
<field name="model">magento.exception</field>
<field name="type">form</field>
<field name="name">magento_exception_form</field>
</record>
<record model="ir.ui.view" id="magento_exception_tree_view">
<field name="model">magento.exception</field>
<field name="type">tree</field>
<field name="name">magento_exception_tree</field>
</record>

</data>
</tryton>
24 changes: 23 additions & 1 deletion magento_.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
'ImportWebsites', 'ExportInventoryStart', 'ExportInventory',
'StorePriceTier', 'ExportTierPricesStart', 'ExportTierPrices',
'ExportTierPricesStatus', 'ExportShipmentStatusStart',
'ExportShipmentStatus', 'ImportOrderStatesStart',
'ExportShipmentStatus', 'ImportOrderStatesStart', 'MagentoException',
'ImportOrderStates', 'ImportCarriersStart', 'ImportCarriers'
]
__metaclass__ = PoolMeta
Expand Down Expand Up @@ -1182,3 +1182,25 @@ def do_export_(self, action):

def transition_export_(self):
return 'end'


class MagentoException(ModelSQL, ModelView):
"""
Magento Exception model
"""
__name__ = 'magento.exception'

origin = fields.Reference(
"Origin", selection='models_get', select=True,
)
log = fields.Text('Exception Log')

@classmethod
def models_get(cls):
'''
Return valid models allowed for origin
'''
return [
('sale.sale', 'Sale'),
('sale.line', 'Sale Line'),
]
72 changes: 68 additions & 4 deletions sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from trytond.model import ModelView, ModelSQL, fields
from trytond.transaction import Transaction
from trytond.exceptions import UserError
from trytond.pool import PoolMeta, Pool
from trytond.pyson import Eval, Not, Bool, PYSONEncoder
from trytond.wizard import Wizard, StateView, Button, StateAction
Expand Down Expand Up @@ -172,6 +173,14 @@ class Sale:
magento_store_view = fields.Many2One(
'magento.store.store_view', 'Store View', readonly=True,
)
magento_exceptions = fields.Function(
fields.One2Many('magento.exception', None, 'Magento Exceptions'),
'get_magento_exceptions'
)
has_magento_exception = fields.Function(
fields.Boolean('Has Magento import exception'),
'get_has_magento_exception'
)

@classmethod
def __setup__(cls):
Expand All @@ -192,6 +201,7 @@ def __setup__(cls):
cls._error_messages.update({
'invalid_instance': 'Store view must have same instance as sale '
'order',
'magento_exception': 'Magento exception in sale %s.'
})

def check_store_view_instance(self):
Expand All @@ -203,6 +213,35 @@ def check_store_view_instance(self):
return False
return True

def get_magento_exceptions(self, name):
"""
Return magento exceptions related to sale
"""
MagentoException = Pool().get('magento.exception')

return map(int, MagentoException.search([
('origin', '=', '%s,%s' % (self.__name__, self.id)),
]))

def get_has_magento_exception(self, name):
"""
Return True is any sale line has magento exception
"""
SaleLine = Pool().get('sale.line')

return bool(SaleLine.search([
('sale', '=', self),
('has_magento_exception', '=', True),
], limit=1))

@classmethod
def confirm(cls, sales):
"Validate sale before confirming"
for sale in sales:
if sale.has_magento_exception:
cls.raise_user_error('magento_exception', sale.reference)
super(Sale, cls).confirm(sales)

@classmethod
def find_or_create_using_magento_data(cls, order_data):
"""
Expand Down Expand Up @@ -246,6 +285,7 @@ def create_using_magento_data(cls, order_data):
Party = Pool().get('party.party')
Address = Pool().get('party.address')
StoreView = Pool().get('magento.store.store_view')
MagentoException = Pool().get('magento.exception')
Currency = Pool().get('currency.currency')
Uom = Pool().get('product.uom')

Expand Down Expand Up @@ -322,7 +362,17 @@ def create_using_magento_data(cls, order_data):
sale, = cls.create([sale_data])

# Process sale now
sale.process_sale_using_magento_state(order_data['state'])
try:
sale.process_sale_using_magento_state(order_data['state'])
except UserError, e:
# Expecting UserError will only come when sale order has
# magento exception.
# Just ignore the error and leave this order in draft state
# and let the user fix this manually.
MagentoException.create([{
'origin': '%s,%s' % (sale.__name__, sale.id),
'log': e.message
}])

return sale

Expand All @@ -345,16 +395,25 @@ def get_item_line_data_using_magento_data(cls, order_data):
for item in order_data['items']:
if not item['parent_item_id']:
# If its a top level product, create it
try:
product = ProductTemplate.find_or_create_using_magento_id(
item['product_id'],
).products[0]
except xmlrpclib.Fault, exception:
if exception.faultCode == 101:
# Case when product doesnot exist on magento
product = None
else:
raise
values = {
'magento_id': int(item['item_id']),
'description': item['name'],
'unit_price': Decimal(item['price']),
'unit': unit.id,
'quantity': Decimal(item['qty_ordered']),
'note': item['product_options'],
'product': ProductTemplate.find_or_create_using_magento_id(
item['product_id'],
).products[0].id
'product': product,
'has_magento_exception': (product is None),
}
line_data.append(('create', [values]))

Expand Down Expand Up @@ -652,6 +711,11 @@ class SaleLine:

#: This field stores the magento ID corresponding to this sale line
magento_id = fields.Integer('Magento ID', readonly=True)
has_magento_exception = fields.Boolean('Has Magento import exception')

@staticmethod
def default_has_magento_exception():
return False


class StockShipmentOut:
Expand Down
8 changes: 8 additions & 0 deletions sale.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
<field name="name">sale_form</field>
</record>

<!--Sale Line -->
<record model="ir.ui.view" id="sale_line_view_form">
<field name="model">sale.line</field>
<field name="type">form</field>
<field name="inherit" ref="sale.sale_line_view_form"/>
<field name="name">sale_line_form</field>
</record>

<!-- Shipment -->
<record model="ir.ui.view" id="shipment_view_form">
<field name="model">stock.shipment.out</field>
Expand Down
8 changes: 8 additions & 0 deletions view/magento_exception_form.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<form string="Magento Exception" col="4">
<label name="origin"/>
<field name="origin"/>
<newline />
<label name="log"/>
<field name="log"/>
</form>
4 changes: 4 additions & 0 deletions view/magento_exception_tree.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0"?>
<tree string="Magento Exception">
<field name="origin"/>
</tree>
5 changes: 5 additions & 0 deletions view/sale_form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
<field name="magento_instance"/>
<label name="magento_store_view"/>
<field name="magento_store_view"/>
<label name="has_magento_exception"/>
<field name="has_magento_exception"/>
</page>
<page string="Magento Exceptions" id="magento_exceptions_tab">
<field name="magento_exceptions"/>
</page>
</xpath>
</data>
7 changes: 7 additions & 0 deletions view/sale_line_form.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<data>
<xpath expr="/form/notebook/page[@id='general']/field[@name='delivery_date']" position="after">
<label name="has_magento_exception"/>
<field name="has_magento_exception"/>
</xpath>
</data>

0 comments on commit acd4f71

Please sign in to comment.