fix orders with pack id mercado libre

This commit is contained in:
wilsongomez 2022-08-16 14:19:05 -05:00
parent 0d34903250
commit abb03e1e0c
6 changed files with 51 additions and 35 deletions

View File

@ -2,23 +2,23 @@
# this repository contains the full copyright notices and license terms.
from trytond.model import ModelView, ModelSQL, fields
import simplejson as json
from trytond.pool import Pool
from trytond.pool import Pool, PoolMeta
STATES = {'readonly': True}
class ApiLog(ModelSQL, ModelView):
class ApiLog(metaclass=PoolMeta):
"API Log"
__name__ = "api.log.channel"
__name__ = "api.log"
channel = fields.Many2One('sale.web_channel', 'channel', states=STATES)
number = fields.Char('Number Doc', states=STATES)
order = fields.Char('Order', states=STATES)
record_date = fields.Date('Record Date', states=STATES)
msg_response = fields.Char('Msg Response', states=STATES)
request_json = fields.Text("Request Json", states=STATES)
headers_json = fields.Text("Header Json", states=STATES)
file_json = fields.Binary("File Json", states=STATES)
status = fields.Char('Status', states=STATES)
# number = fields.Char('Number Doc', states=STATES)
# order = fields.Char('Order', states=STATES)
# record_date = fields.Date('Record Date', states=STATES)
# msg_response = fields.Char('Msg Response', states=STATES)
# request_json = fields.Text("Request Json", states=STATES)
# headers_json = fields.Text("Header Json", states=STATES)
# file_json = fields.Binary("File Json", states=STATES)
# status = fields.Char('Status', states=STATES)
@classmethod
def __setup__(cls):

View File

@ -4,7 +4,7 @@ this repository contains the full copyright notices and license terms. -->
<tryton>
<data>
<record model="ir.ui.view" id="api_log_view_tree">
<!-- <record model="ir.ui.view" id="api_log_view_tree">
<field name="model">api.log.channel</field>
<field name="type">tree</field>
<field name="priority">20</field>
@ -62,7 +62,7 @@ this repository contains the full copyright notices and license terms. -->
<field name="perm_write" eval="True"/>
<field name="perm_create" eval="True"/>
<field name="perm_delete" eval="True"/>
</record>
</record> -->
</data>
</tryton>

View File

@ -262,17 +262,22 @@ class MercadoLibre(SaleWebChannel):
def validate_sale(self, sale_):
if sale_.get('pack_id'):
if not sale_.get('shipping'):
return
shipment_id = sale_['shipping']['id']
URI = 'https://api.mercadolibre.com/shipments/%s/items?access_token=%s' % (
shipment_id, self.access_token)
res = self.get_response(URI)
shipment_items = res.json()
if len(shipment_items) > 1:
# if not sale_.get('shipping'):
# return
# shipment_id = sale_['shipping']['id']
# URI = 'https://api.mercadolibre.com/shipments/%s/items?access_token=%s' % (
# shipment_id, self.access_token)
# res = self.get_response(URI)
# shipment_items = res.json()
# if len(shipment_items) > 1:
URI = 'https://api.mercadolibre.com/packs/%s?access_token=%s' % (
sale_.get('pack_id'), self.access_token)
response = self.get_response(URI)
response = response.json()
if response.get('orders'):
ids = ''
for item in shipment_items:
order_id = item['order_id']
for item in response['orders']:
order_id = str(item['id'])
if order_id != str(sale_['id']):
URI2 = 'https://api.mercadolibre.com/orders/%s?access_token=%s' % (
order_id, self.access_token)

View File

@ -53,12 +53,10 @@ class Shopify(SaleWebChannel):
description = line['variant_title']
list_price = round(float(line['price'])/1.19, 2)
sale_price_w_tax = round(Decimal(line['price']), 2)
print(list_price)
print(sale_price_w_tax)
create_template = {
'name': line['title'],
'list_price': 1,
'sale_price_w_tax': 1,
'list_price': list_price,
'sale_price_w_tax': sale_price_w_tax,
'type': 'goods',
'salable': True,
'purchasable': True,
@ -66,6 +64,7 @@ class Shopify(SaleWebChannel):
'sale_uom': 1,
'default_uom': 1,
'account_category': 7,
'code': code,
}
template, = Template.create([create_template])
create_product = []
@ -185,12 +184,17 @@ class Shopify(SaleWebChannel):
year, month, day = date_created[0].split('-')
sale_date = date(int(year), int(month), int(day))
with Transaction().set_user(ctx['user']):
comment = ''
try:
comment = 'GUIA DE ENVIO NO. ' + sale_['fulfillments'][0].get('tracking_number', '')\
if sale_['fulfillment_status'] == 'fulfilled' and sale_['fulfillments'] else ''
except:
pass
sale, = Sale.create([{
'payment_term': 1,
'party': party.id,
'sale_date': sale_date,
'comment': 'GUIA DE ENVIO NO. ' + sale_['fulfillments'][0].get('tracking_number', '')
if sale_['fulfillment_status'] == 'fulfilled' and sale_['fulfillments'] else '',
'comment': comment,
'state': 'draft',
'company': ctx['company'],
'currency': user_.company.currency.id,
@ -289,9 +293,10 @@ class Shopify(SaleWebChannel):
'type': 'line',
'unit': product.default_uom.id,
'quantity': quantity,
'base_price': round(Decimal(un_price), 3),
'unit_price': round(Decimal(un_price), 3),
'unit_price_full': round(Decimal(line['price']),2),
'discount': 0,
# 'discount': 0,
'product': product.id,
'taxes': [('add', product.customer_taxes_used)],
'description': description,
@ -350,10 +355,15 @@ class Shopify(SaleWebChannel):
# channel.upload_note(sale, 'Error, al generar factura orden duplicada')
return response
if data.get('fulfillment_status') == 'fulfilled':
track_number = ''
try:
track_number = str(data['fulfillments'][0]['tracking_number'])
except:
pass
Sale.write([sale], {
'description': sale.description + ' - ' + data['fulfillment_status'],
'comment': 'GUIA DE ENVIO NO. ' + data['fulfillments'][0]['tracking_number'],
'tracking_number': data['fulfillments'][0]['tracking_number']
'comment': 'GUIA DE ENVIO NO. ' + track_number,
'tracking_number': track_number
})
return sale

View File

@ -1,8 +1,9 @@
[tryton]
version=6.0.1
version=6.0.2
depends:
sale_pos
product_reference
log
xml:
web_channel.xml
sale.xml

View File

@ -201,8 +201,8 @@ class SaleWebChannel(Workflow, ModelSQL, ModelView):
def get_status_token(self, name):
if self.creation_time:
now = datetime.now()
res = now - self.creation_time
if res.seconds >= 21600:
res = (now - self.creation_time).total_seconds()
if res >= 21600:
return 'expired'
else:
return 'active'