158 lines
5.8 KiB
Python
158 lines
5.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Copyright 2024-2025 Nexa Systems Inc.
|
|
# License OPL-1 (Odoo Proprietary License v1.0)
|
|
# Part of the Fusion Claim Assistant product family.
|
|
|
|
import binascii
|
|
|
|
from odoo import fields, http, _
|
|
from odoo.exceptions import AccessError, MissingError
|
|
from odoo.http import request
|
|
from odoo.addons.sale.controllers.portal import CustomerPortal
|
|
|
|
|
|
class FusionCustomerPortal(CustomerPortal):
|
|
|
|
def _is_adp_order(self, order_sudo):
|
|
return (
|
|
hasattr(order_sudo, '_is_adp_sale')
|
|
and order_sudo._is_adp_sale()
|
|
)
|
|
|
|
def _is_adp_reg(self, order_sudo):
|
|
return self._is_adp_order(order_sudo) and order_sudo.x_fc_client_type == 'REG'
|
|
|
|
def _get_adp_payable(self, order_sudo):
|
|
if self._is_adp_reg(order_sudo):
|
|
return order_sudo.x_fc_client_portion_total or 0
|
|
return order_sudo.amount_total
|
|
|
|
# ------------------------------------------------------------------
|
|
# View Details: render ADP landscape report for ADP orders
|
|
# ------------------------------------------------------------------
|
|
@http.route()
|
|
def portal_order_page(
|
|
self, order_id, report_type=None, access_token=None,
|
|
message=False, download=False, payment_amount=None,
|
|
amount_selection=None, **kw
|
|
):
|
|
if report_type in ('html', 'pdf', 'text'):
|
|
try:
|
|
order_sudo = self._document_check_access(
|
|
'sale.order', order_id, access_token=access_token,
|
|
)
|
|
except (AccessError, MissingError):
|
|
return request.redirect('/my')
|
|
|
|
if self._is_adp_order(order_sudo):
|
|
return self._show_report(
|
|
model=order_sudo,
|
|
report_type=report_type,
|
|
report_ref='fusion_claims.action_report_saleorder_landscape',
|
|
download=download,
|
|
)
|
|
|
|
return super().portal_order_page(
|
|
order_id,
|
|
report_type=report_type,
|
|
access_token=access_token,
|
|
message=message,
|
|
download=download,
|
|
payment_amount=payment_amount,
|
|
amount_selection=amount_selection,
|
|
**kw,
|
|
)
|
|
|
|
# ------------------------------------------------------------------
|
|
# Payment amount overrides
|
|
# ------------------------------------------------------------------
|
|
def _determine_is_down_payment(self, order_sudo, amount_selection, payment_amount):
|
|
if self._is_adp_reg(order_sudo):
|
|
payable = self._get_adp_payable(order_sudo)
|
|
if amount_selection == 'down_payment':
|
|
return True
|
|
elif amount_selection == 'full_amount':
|
|
return False
|
|
return (
|
|
order_sudo.prepayment_percent < 1.0 if payment_amount is None
|
|
else payment_amount < payable
|
|
)
|
|
return super()._determine_is_down_payment(order_sudo, amount_selection, payment_amount)
|
|
|
|
def _get_payment_values(self, order_sudo, is_down_payment=False, payment_amount=None, **kwargs):
|
|
values = super()._get_payment_values(
|
|
order_sudo,
|
|
is_down_payment=is_down_payment,
|
|
payment_amount=payment_amount,
|
|
**kwargs,
|
|
)
|
|
if not self._is_adp_reg(order_sudo):
|
|
return values
|
|
|
|
client_portion = self._get_adp_payable(order_sudo)
|
|
if client_portion <= 0:
|
|
return values
|
|
|
|
current_amount = values.get('amount', 0)
|
|
if current_amount > client_portion:
|
|
values['amount'] = order_sudo.currency_id.round(client_portion)
|
|
return values
|
|
|
|
# ------------------------------------------------------------------
|
|
# Signature: attach ADP report instead of default Odoo quotation
|
|
# ------------------------------------------------------------------
|
|
@http.route()
|
|
def portal_quote_accept(self, order_id, access_token=None, name=None, signature=None):
|
|
access_token = access_token or request.httprequest.args.get('access_token')
|
|
try:
|
|
order_sudo = self._document_check_access('sale.order', order_id, access_token=access_token)
|
|
except (AccessError, MissingError):
|
|
return {'error': _('Invalid order.')}
|
|
|
|
if not order_sudo._has_to_be_signed():
|
|
return {'error': _('The order is not in a state requiring customer signature.')}
|
|
if not signature:
|
|
return {'error': _('Signature is missing.')}
|
|
|
|
try:
|
|
order_sudo.write({
|
|
'signed_by': name,
|
|
'signed_on': fields.Datetime.now(),
|
|
'signature': signature,
|
|
})
|
|
request.env.cr.flush()
|
|
except (TypeError, binascii.Error):
|
|
return {'error': _('Invalid signature data.')}
|
|
|
|
if not order_sudo._has_to_be_paid():
|
|
order_sudo._validate_order()
|
|
|
|
if self._is_adp_order(order_sudo):
|
|
report_ref = 'fusion_claims.action_report_saleorder_landscape'
|
|
else:
|
|
report_ref = 'sale.action_report_saleorder'
|
|
|
|
pdf = request.env['ir.actions.report'].sudo()._render_qweb_pdf(
|
|
report_ref, [order_sudo.id]
|
|
)[0]
|
|
|
|
order_sudo.message_post(
|
|
attachments=[('%s.pdf' % order_sudo.name, pdf)],
|
|
author_id=(
|
|
order_sudo.partner_id.id
|
|
if request.env.user._is_public()
|
|
else request.env.user.partner_id.id
|
|
),
|
|
body=_('Order signed by %s', name),
|
|
message_type='comment',
|
|
subtype_xmlid='mail.mt_comment',
|
|
)
|
|
|
|
query_string = '&message=sign_ok'
|
|
if order_sudo._has_to_be_paid():
|
|
query_string += '&allow_payment=yes'
|
|
return {
|
|
'force_refresh': True,
|
|
'redirect_url': order_sudo.get_portal_url(query_string=query_string),
|
|
}
|