add context price_list

This commit is contained in:
Elvis 2023-09-28 14:39:30 -05:00
parent d7105999f5
commit cfe082585f
3 changed files with 69 additions and 36 deletions

View File

@ -6,6 +6,8 @@ from rich import print
from trytond.pool import Pool
from . import endpoints_rappi
from decimal import Decimal
from trytond.transaction import Transaction
from trytond.modules.product import round_price
URL_AUTH_DEV = 'https://rests-integrations-dev.auth0.com/oauth/token'
URL_AUTH_PRODUCTION = 'https://rests-integrations.auth0.com/oauth/token'
@ -30,6 +32,7 @@ class Rappi:
"""Rappi"""
def __init__(self, web_shop):
self.id = web_shop.id
self.price_list = web_shop.price_list
self.company = web_shop.company
self.seller_id = web_shop.seller_id
self.secret_key = web_shop.secret_key
@ -102,7 +105,7 @@ class Rappi:
'items': items_data,
}
print(data)
return self._send_request(endpoint=endpoint, method='POST', data=data)
# return self._send_request(endpoint=endpoint, method='POST', data=data)
def get_products(self, products):
item_dicts = {}
@ -111,7 +114,7 @@ class Rappi:
item_dicts[product.code] = {
"name": product.name,
"description": product.description if product.description else 'not available',
"price": float(product.list_price),
"price": float(self.compute_unit_price(product)),
"sku": product.code,
"sortingPosition": 0,
"type": "PRODUCT",
@ -133,7 +136,7 @@ class Rappi:
items[product.code] = {
"name": product.name,
"description": product.description if product.description else 'not available',
"price": float(product.list_price),
"price": float(self.compute_unit_price(product)),
"sku": product.code,
"sortingPosition": 0,
"type": "PRODUCT",
@ -179,46 +182,68 @@ class Rappi:
orders = self.get_orders()
Sale = Pool().get('sale.sale')
Party = Pool().get('party.party')
pos_client, = Party.search_read([
pos_client, = Party.search([
('id_number', '=', '222222222')
], fields_names=['id'])
_sales = []
])
# _sales = []
for order in orders:
to_create = {
'web_shop': self.id,
'invoice_type': 'P',
'company': self.company,
'party': pos_client['id'],
'sale_device': None,
'payment_method': 'cash',
'payment_term': None,
'kind': 'take_away',
'web_id': order['order_detail']['order_id'],
'lines': [('create', self.get_sale_lines(order['order_detail']['items']))],
# 'turn': 1,
}
_sales.append(to_create)
sales = Sale.create(_sales)
return sales
to_create = Sale(
web_shop=self.id,
invoice_type='P',
company=self.company,
party=pos_client.id,
sale_device=None,
payment_method='cash',
payment_term=None,
kind='take_away',
web_id=order['order_detail']['order_id'],
)
to_create.save()
self.create_sale_lines(order['order_detail']['items'], to_create.id)
def get_sale_lines(cls, items):
def create_sale_lines(self, items, sale, sub_items=False):
print('create_sale_lines')
lines_to_create = []
lines_to_create_append = lines_to_create.append
Product = Pool().get('product.product')
print('voy por aqaui')
SaleLine = Pool().get('sale.line')
for item in items:
print(item, 'items')
product, = Product.search([('code', '=', item['sku'])])
print(item['unit_price_without_discount'], 'hola')
_line = {
'product': product.id,
'description': product.description,
'quantity': item['quantity'],
'base_price': Decimal(str(round(item['unit_price_without_discount']))),
'discount_rate': Decimal(str(round(item['percentage_discount']))),
'unit_price': Decimal(str(round(item['unit_price_without_discount']))),
'unit': product.default_uom.id,
# 'taxes': [('create', [tax.id for tax in product.taxes])]
}
_line = SaleLine(
sale=sale,
product=product.id,
description=product.description,
)
_line.on_change_product()
_line.quantity = item['quantity']
_line.base_price = self.get_price(item['unit_price_without_discount'], sub_items)
_line.discount_rate = self.get_price(item['percentage_discount'], sub_items)
_line.unit_price = self.get_price(item['unit_price_without_discount'], sub_items)
_line.unit = product.default_uom.id
_line.save()
lines_to_create_append(_line)
self.create_sale_lines(item['subitems'], sale, sub_items=True)
return lines_to_create
def get_price(self, price, subitem):
print('si entra')
if subitem:
return 0
else:
return Decimal(str(round(price)))
def compute_unit_price(self, product):
pool = Pool()
Product = pool.get('product.product')
context = {}
if self.price_list:
print('si tiene lista de precios')
context['price_list'] = self.price_list.id
with Transaction().set_context(context):
unit_price = Product.get_sale_price([product],
1 or 0)[product.id]
print(unit_price, 'este es el unit price')
if unit_price:
unit_price = round_price(unit_price)
return unit_price

View File

@ -2,6 +2,12 @@
<!-- This file is part sale_shop module for Tryton.
The COPYRIGHT file at the top level of this repository contains the full copyright notices and license terms. -->
<data>
<xpath
expr="/form/field[@name='guest_party']"
position="after">
<label name="price_list"/>
<field name="price_list"/>
</xpath>
<xpath
expr="/form/notebook/page[@id='products']"
position="after">

2
web.py
View File

@ -76,6 +76,7 @@ class Shop(metaclass=PoolMeta):
seller_id = fields.Char('Seller ID', states={
'invisible': ~Eval('type').in_(['mercadolibre', 'rappi']),
})
price_list = fields.Many2One('product.price_list', 'Price list', ondelete="RESTRICT")
@classmethod
def __setup__(cls):
@ -136,3 +137,4 @@ class Shop(metaclass=PoolMeta):
if record.type == 'rappi':
rappi_rec = rappi.Rappi(record)
rappi_rec.create_sales()