Skip to content

Commit

Permalink
Upgraded 24.14, added search by buyer name, default contacts for sale…
Browse files Browse the repository at this point in the history
… orders, avoiding importing client ok
  • Loading branch information
fabriciocosta committed Feb 27, 2024
1 parent a72742b commit 0db315e
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 27 deletions.
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def pre_init_check(cr):
pre_init_check(cr=None)


def pre_init_hook(cr, registry):
def pre_init_hook(cr, registry=None):

env = api.Environment(cr, SUPERUSER_ID, {})
#env['ir.model'].search([('model', '=', 'warning')]).unlink()
Expand Down
2 changes: 1 addition & 1 deletion __manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{
'name': 'MercadoLibre Publisher / Mercado Libre Odoo Connector',
'summary': 'MercadoLibre Publisher / Mercado Libre Odoo Connector',
'version': '17.0.24.8',
'version': '17.0.24.14',
'author': 'Moldeo Interactive',
'website': 'https://www.moldeointeractive.com',
"category": "Sales",
Expand Down
12 changes: 6 additions & 6 deletions models/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,12 @@ class mercadolibre_category(models.Model):

def get_meli( self, meli=None ):

_logger.info("get_meli")
_logger.info(self)
_logger.info(meli)
_logger.info(str(meli and meli.seller_id))
_logger.info(str(meli and meli.client_id))
_logger.info(str(meli and meli.meli_login_id))
#_logger.info("get_meli")
#_logger.info(self)
#_logger.info(meli)
#_logger.info(str(meli and meli.seller_id))
#_logger.info(str(meli and meli.client_id))
#_logger.info(str(meli and meli.meli_login_id))

if meli:
return meli
Expand Down
10 changes: 6 additions & 4 deletions models/company.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,9 @@ def cron_meli_questions(self):
mercadolibre_recursive_import = fields.Boolean( string='Recursive import', help='Import all the category tree from Category Code')

mercadolibre_cron_refresh = fields.Boolean(string='Keep alive',help='Cron Automatic Token Refresh for keeping ML connection alive.')
mercadolibre_cron_mail = fields.Many2one(
comodel_name="mail.template",
mercadolibre_cron_mail = fields.Many2one("mail.template",
string="Error E-mail Template",
help="Select the email template that will be sent when "
"cron refresh fails.")
help="Select the email template that will be sent when cron refresh fails.")
mercadolibre_cron_get_orders = fields.Boolean(string="Importar pedidos",help='Cron Get Orders / Pedidos de venta')
mercadolibre_cron_get_orders_shipment = fields.Boolean(string='Importar envíos',help='Cron Get Orders Shipment')
mercadolibre_cron_get_orders_shipment_client = fields.Boolean(string='Importar clientes',help='Cron Get Orders Shipment Client')
Expand Down Expand Up @@ -429,6 +427,10 @@ def cron_meli_questions(self):

mercadolibre_banner = fields.Many2one("mercadolibre.banner",string="Plantilla Descriptiva")

mercadolibre_contact_partner = fields.Many2one("res.partner",string="Contacto Predeterminado")
mercadolibre_shipping_partner = fields.Many2one("res.partner",string="Contacto de Envio Predeterminado")
mercadolibre_invoice_partner = fields.Many2one("res.partner",string="Contacto de Facturación Predeterminado")

#mercadolibre_use_buyer_name = fields.Boolean(string="Use buyer name",default=True)

#Toma y lista los ids de las publicaciones del sitio de MercadoLibre, filtrados por official_store_id
Expand Down
73 changes: 63 additions & 10 deletions models/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ def _meli_status_brief(self):
order.meli_status_detail = order.meli_status_detail

def search_meli_status_brief(self, operator, value):
_logger.info("search_meli_status_brief")
_logger.info(operator)
_logger.info(value)
#_logger.info("search_meli_status_brief")
#_logger.info(operator)
#_logger.info(value)
if operator == 'ilike':
#name = self.env.context.get('name', False)
#if name is not False:
id_list = []
_logger.info(self.env.context)
#_logger.info(self.env.context)
#name = self.env.context.get('name', False)
sale_orders = self.env['sale.order'].search([], limit=10000,order='id desc')
if (value):
Expand All @@ -93,7 +93,50 @@ def search_meli_status_brief(self, operator, value):
' with the operator: {}',format(operator)
)

def _search_meli_buyer_name( self, operator, value ):
#_logger.info("_search_meli_buyer_name")
#_logger.info(operator)
#_logger.info(value)
if operator == 'ilike':
#name = self.env.context.get('name', False)
#if name is not False:
id_list = []
#_logger.info(self.env.context)
#name = self.env.context.get('name', False)
meli_orders = []
buyer_ids = []
if value:
buyers = self.env['mercadolibre.buyers'].search([('name','=ilike','%'+str(value)+'%')], limit=10000,order='name asc')
if buyers:
for buyer in buyers:
buyer_ids.append(buyer.id)
if buyer_ids:
meli_orders = self.env['mercadolibre.orders'].search([('buyer','in',buyers_ids)], limit=10000 )
#sale_orders = self.env['sale.order'].search([], limit=10000,order='id desc')
#if (value):
#for so in sale_orders:
# if (value in so.meli_buyer_name):
# id_list.append(so.id)
if (meli_orders):
for mo in meli_orders:
if mo.sale_order and mo.sale_order.id:
id_list.append(mo.sale_order.id)
return [('id', 'in', id_list)]
else:
_logger.error(
'The field name is not searchable'
' with the operator: {}',format(operator)
)

def _get_meli_order( self ):
for so in self:
so.meli_order = so.meli_orders and so.meli_orders[0]
so.meli_buyer = so.meli_order and so.meli_order.buyer
so.meli_buyer_name = so.meli_buyer and so.meli_buyer.name

meli_order = fields.Many2one( 'mercadolibre.orders',string="Meli Orden", compute="_get_meli_order" )
meli_buyer = fields.Many2one( "mercadolibre.buyers",string="Meli Comprador", compute="_get_meli_order")
meli_buyer_name = fields.Char( string="Meli Comprador Nombre", compute="_get_meli_order", search=_search_meli_buyer_name, store=False, index=True )

meli_status = fields.Selection( [
#Initial state of an order, and it has no payment yet.
Expand Down Expand Up @@ -1572,7 +1615,8 @@ def orders_update_order_json( self, data, context=None, config=None, meli=None )
pass;
else:
try:
partner_invoice_id = respartner_obj.create(( partner_update ))
if config.mercadolibre_cron_get_orders_shipment_client:
partner_invoice_id = respartner_obj.create(( partner_update ))
if partner_invoice_id:
#partner_update = self.update_partner_billing_info( partner_id=partner_invoice_id, meli_buyer_fields=partner_update )
#partner_invoice_id.write(partner_update)
Expand All @@ -1588,7 +1632,8 @@ def orders_update_order_json( self, data, context=None, config=None, meli=None )
#_logger.info( "creating new partner:" + str(meli_buyer_fields) )
try:
meli_buyer_fields.update(billing_partner_update)
partner_id = respartner_obj.create(( meli_buyer_fields ))
if config.mercadolibre_cron_get_orders_shipment_client:
partner_id = respartner_obj.create(( meli_buyer_fields ))
partner_invoice_id = partner_id
except Exception as e:
_logger.info("orders_update_order > Error creando Partner:"+str(e))
Expand Down Expand Up @@ -1629,7 +1674,8 @@ def orders_update_order_json( self, data, context=None, config=None, meli=None )
#partner_id.write( meli_buyer_fields )

if (partner_id):
partner_shipping_id = self.env["mercadolibre.shipment"].partner_delivery_id( partner_id=partner_id, Receiver=Receiver)
if config.mercadolibre_cron_get_orders_shipment_client:
partner_shipping_id = self.env["mercadolibre.shipment"].partner_delivery_id( partner_id=partner_id, Receiver=Receiver)

if (partner_id):
if ("fe_habilitada" in self.env['res.partner']._fields):
Expand All @@ -1644,12 +1690,19 @@ def orders_update_order_json( self, data, context=None, config=None, meli=None )
_logger.error("Buyer not fetched!")

if (not partner_id):
_logger.error("No partner founded or created for ML Order" )
return {'error': 'No partner founded or created for ML Order' }
if config.mercadolibre_cron_get_orders_shipment_client:
_logger.error("No partner founded or created for ML Order" )
return {'error': 'No partner founded or created for ML Order' }

#process base order fields
#asignar datos de invoicing predeterminado....(mexico)
partner_id = ("mercadolibre_contact_partner" in config._fields and config.mercadolibre_contact_partner) or partner_id
partner_invoice_id = ("mercadolibre_invoice_partner" in config._fields and config.mercadolibre_invoice_partner) or partner_invoice_id
partner_shipping_id = ("mercadolibre_shipping_partner" in config._fields and config.mercadolibre_shipping_partner) or partner_shipping_id

meli_order_fields = self.prepare_sale_order_vals( order_json=order_json, meli=meli, config=config, sale_order=sorder )
meli_order_fields.update({
'partner_id': partner_id.id,
'partner_id': (partner_id and partner_id.id),
'partner_invoice_id': (partner_invoice_id and partner_invoice_id.id),
'pricelist_id': plistid.id,
})
Expand Down
8 changes: 6 additions & 2 deletions models/shipment.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,15 @@ def _update_sale_order_shipping_info( self, order, meli=None, config=None ):
except:
_logger.info("Could not unlink.")

def partner_delivery_id( self, partner_id=None, Receiver=None ):
def partner_delivery_id( self, partner_id=None, Receiver=None, config=None ):

if (not Receiver or not partner_id):
_logger.info("partner_delivery_id > no Partner or no Receiver")
return None

if (config and not config.mercadolibre_cron_get_orders_shipment_client):
return None

orders_obj = self.env['mercadolibre.orders']

partner_shipping_id = None
Expand Down Expand Up @@ -969,7 +972,8 @@ def fetch( self, order, meli=None, config=None ):
#_logger.info("ship_json[receiver_address]:"+str(ship_json["receiver_address"]) )
partner_shipping_id = None
if "receiver_address" in ship_json:
partner_shipping_id = self.partner_delivery_id( partner_id=partner_id, Receiver=ship_json["receiver_address"])
if config.mercadolibre_cron_get_orders_shipment_client:
partner_shipping_id = self.partner_delivery_id( partner_id=partner_id, Receiver=ship_json["receiver_address"])

if partner_shipping_id:
sorder = sorder_pack
Expand Down
5 changes: 5 additions & 0 deletions views/company_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
<field name="mercadolibre_banner" invisible="0"/>
<field name="mercadolibre_seller_user" invisible="0"/>
<field name="mercadolibre_seller_team" invisible="0"/>

<field name="mercadolibre_contact_partner" invisible="0"/>
<field name="mercadolibre_shipping_partner" invisible="0"/>
<field name="mercadolibre_invoice_partner" invisible="0"/>

<field name="mercadolibre_attributes" invisible="0"/>
<field name="mercadolibre_exclude_attributes" widget="many2many" options="{'no_create': True}">
<tree>
Expand Down
9 changes: 6 additions & 3 deletions views/orders_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,17 @@
<field name="arch" type="xml">
<data>
<notebook>
<page string="MercadoLibre" attrs="{'invisible':[('meli_order_id', '=', False)]}">
<page string="MercadoLibre" invisible="meli_order_id==False">
<button name='meli_oerp_update' type="object"
string="Actualizar"
attrs="{'invisible':[('meli_order_id','=',False)]}"
invisible="meli_order_id==False"
class="oe_stat_button"
icon="fa-refresh"/>
<field name="meli_orders" invisible="0"/>
<group>
<field name="meli_order"/>
<field name="meli_buyer"/>
<field name="meli_buyer_name"/>
<field name="meli_update_forbidden" string="No actualizar desde ML"/>
<field name="meli_order_id" invisible="0"/>
<field name="meli_status" invisible="0"/>
Expand Down Expand Up @@ -117,7 +120,7 @@
<data>
<notebook>
<page string="MercadoLibre"
invisible="meli_buyer_id == False">
invisible="meli_buyer_id == False">
<group>
<field name="meli_buyer_id" invisible="0"/>
<field name="meli_buyer" invisible="0"/>
Expand Down

0 comments on commit 0db315e

Please sign in to comment.