157 lines
7.7 KiB
Python
157 lines
7.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
#################################################################################
|
|
# Author : Webkul Software Pvt. Ltd. (<https://webkul.com/>:wink:
|
|
# Copyright(c): 2015-Present Webkul Software Pvt. Ltd.
|
|
# All Rights Reserved.
|
|
#
|
|
#
|
|
#
|
|
# This program is copyright property of the author mentioned above.
|
|
# You can`t redistribute it and/or modify it.
|
|
#
|
|
#
|
|
# You should have received a copy of the License along with this program.
|
|
# If not, see <https://store.webkul.com/license.html/>;
|
|
#################################################################################
|
|
import datetime
|
|
from odoo.addons.website_sale.controllers.main import WebsiteSale
|
|
from odoo.addons.website_sale.controllers.main import Website
|
|
from odoo import http
|
|
from odoo.http import request
|
|
from odoo import fields
|
|
import logging
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
class Website(Website):
|
|
|
|
@http.route()
|
|
def index(self, **kw):
|
|
enable_ppc = request.env['res.config.settings'].sudo().website_constant().get('enable_ppc')
|
|
aff_key = request.httprequest.args.get('aff_key')
|
|
if aff_key and enable_ppc:
|
|
partner_id = request.env['res.partner'].sudo().search([('res_affiliate_key', '=', aff_key), ('is_affiliate', '=', True)])
|
|
vals = {
|
|
'affiliate_method': 'ppc',
|
|
'affiliate_key': aff_key,
|
|
'affiliate_partner_id': partner_id.id,
|
|
'url': request.httprequest.full_path,
|
|
'ip_address': request.httprequest.environ['REMOTE_ADDR'],
|
|
'type_id': 0,
|
|
'convert_date': fields.datetime.now(),
|
|
'affiliate_program_id': partner_id.affiliate_program_id.id,
|
|
'affiliate_type': 'general'
|
|
}
|
|
if (len(partner_id) == 1):
|
|
check_unique_ppc = request.env['res.config.settings'].sudo().website_constant().get('unique_ppc_traffic')
|
|
if check_unique_ppc:
|
|
domain = [('affiliate_type', '=', 'general'), ('affiliate_method', '=', 'ppc'), ('affiliate_key', '=', aff_key), ('ip_address', '=', request.httprequest.environ['REMOTE_ADDR'])]
|
|
visit = request.env['affiliate.visit'].sudo().search(domain)
|
|
if not visit:
|
|
request.env['affiliate.visit'].sudo().create(vals)
|
|
else:
|
|
request.env['affiliate.visit'].sudo().create(vals)
|
|
|
|
return super().index(**kw)
|
|
|
|
class WebsiteSale(WebsiteSale):
|
|
|
|
def create_aff_visit_entry(self, vals):
|
|
ppc_exist = self.check_ppc_exist(vals)
|
|
if ppc_exist:
|
|
visit = ppc_exist
|
|
else:
|
|
visit = request.env['affiliate.visit'].sudo().create(vals)
|
|
return visit
|
|
|
|
def check_ppc_exist(self, vals):
|
|
domain = [('type_id', '=', vals['type_id']), ('affiliate_method', '=', vals['affiliate_method']), ('affiliate_key', '=', vals['affiliate_key']), ('ip_address', '=', vals['ip_address'])]
|
|
visit = request.env['affiliate.visit'].sudo().search(domain)
|
|
check_unique_ppc = request.env['res.config.settings'].sudo().website_constant().get('unique_ppc_traffic')
|
|
|
|
if check_unique_ppc:
|
|
if visit:
|
|
return visit
|
|
else:
|
|
return False
|
|
else:
|
|
return False
|
|
|
|
@http.route()
|
|
def shop(self, page=0, category=None, search='', ppg=False, **post):
|
|
enable_ppc = request.env['res.config.settings'].sudo().website_constant().get('enable_ppc')
|
|
result = super(WebsiteSale, self).shop(page=page, category=category, search=search, ppg=ppg, **post)
|
|
aff_key = request.httprequest.args.get('aff_key')
|
|
if category and aff_key:
|
|
partner_id = request.env['res.partner'].sudo().search([('res_affiliate_key', '=', aff_key), ('is_affiliate', '=', True)])
|
|
vals = self.create_affiliate_visit(aff_key, partner_id, category)
|
|
vals.update({'affiliate_type': 'category'})
|
|
if (len(partner_id) == 1):
|
|
self.create_aff_visit_entry(vals) if enable_ppc else False
|
|
else:
|
|
_logger.info("=====affiliate_visit not created by category===========")
|
|
return result
|
|
|
|
@http.route()
|
|
def product(self, product, category='', search='', **kwargs):
|
|
_logger.info("=====product page=========")
|
|
_logger.info("=====product page aff_key==%r=========", request.httprequest.args)
|
|
enable_ppc = request.env['res.config.settings'].sudo().website_constant().get('enable_ppc')
|
|
result = super(WebsiteSale, self).product(product=product, category=category, search=search, **kwargs)
|
|
if request.httprequest.args.get('aff_key'):
|
|
aff_key = request.httprequest.args.get('aff_key')
|
|
partner_id = request.env['res.partner'].sudo().search([('res_affiliate_key', '=', aff_key), ('is_affiliate', '=', True)])
|
|
vals = self.create_affiliate_visit(aff_key, partner_id, product)
|
|
vals.update({'affiliate_type': 'product'})
|
|
if (len(partner_id) == 1):
|
|
affiliate_visit = self.create_aff_visit_entry(vals) if enable_ppc else False
|
|
_logger.info("============affiliate_visit created by product==%r=======", affiliate_visit)
|
|
else:
|
|
_logger.info("=====affiliate_visit not created by product===========%s %s" % (aff_key, partner_id))
|
|
return result
|
|
|
|
@http.route()
|
|
def payment_confirmation(self, **post):
|
|
result = super(WebsiteSale, self).payment_confirmation(**post)
|
|
sale_order_id = result.qcontext.get('order')
|
|
return self.update_affiliate_visit_cookies(sale_order_id, result)
|
|
|
|
def create_affiliate_visit(self, aff_key, partner_id, type_id):
|
|
""" method to delete the cookie after update function on id"""
|
|
vals = {
|
|
'affiliate_method': 'ppc',
|
|
'affiliate_key': aff_key,
|
|
'affiliate_partner_id': partner_id.id,
|
|
'url': request.httprequest.full_path,
|
|
'ip_address': request.httprequest.environ['REMOTE_ADDR'],
|
|
'type_id': type_id.id,
|
|
'convert_date': fields.datetime.now(),
|
|
'affiliate_program_id': partner_id.affiliate_program_id.id,
|
|
}
|
|
return vals
|
|
|
|
def update_affiliate_visit_cookies(self, sale_order_id, result):
|
|
"""update affiliate.visit from cokkies data i.e created in product and shop method"""
|
|
cookies = dict(request.httprequest.cookies)
|
|
visit = request.env['affiliate.visit']
|
|
affiliate_key = cookies.get('affkey') # contains cookies product_id
|
|
if affiliate_key:
|
|
partner_id = request.env['res.partner'].sudo().search([('res_affiliate_key', '=', affiliate_key), ('is_affiliate', '=', True)])
|
|
for s in sale_order_id.order_line:
|
|
if partner_id:
|
|
product_tmpl_id = s.product_id.product_tmpl_id.id
|
|
visit.sudo().create({
|
|
'affiliate_method': 'pps',
|
|
'affiliate_key': affiliate_key,
|
|
'affiliate_partner_id': partner_id.id,
|
|
'url': "",
|
|
'ip_address': request.httprequest.environ['REMOTE_ADDR'],
|
|
'type_id': product_tmpl_id,
|
|
'affiliate_type': 'product',
|
|
'type_name': s.product_id.id,
|
|
'sales_order_line_id': s.id,
|
|
'convert_date': fields.datetime.now(),
|
|
'affiliate_program_id': partner_id.affiliate_program_id.id,
|
|
'product_quantity': s.product_uom_qty,
|
|
'is_converted': True
|
|
})
|
|
return result |