[ADD] website_sale_payment_terms: allow ecommerce customers to choose payment terms

H4799
This commit is contained in:
Cedric Collins
2021-01-22 14:16:21 -06:00
parent e34bdbf52e
commit cb0c6361cc
16 changed files with 499 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
from . import controllers
from . import models

View File

@@ -0,0 +1,26 @@
{
'name': 'Website Payment Terms',
'author': 'Hibou Corp. <hello@hibou.io>',
'category': 'Sales',
'version': '13.0.1.0.0',
'description':
"""
Website Payment Terms
=====================
Allow customers to choose payment terms if order total meets a configured threshold.
""",
'depends': [
'sale_payment_deposit',
'website_sale',
'website_sale_delivery',
],
'auto_install': False,
'data': [
'views/account_views.xml',
'views/res_config_views.xml',
'views/web_assets.xml',
'views/website_templates.xml',
'views/website_views.xml',
],
}

View File

@@ -0,0 +1 @@
from . import main

View File

@@ -0,0 +1,76 @@
from odoo.http import request, route
from odoo.addons.website_sale.controllers.main import WebsiteSale
class WebsiteSalePaymentTerms(WebsiteSale):
# In case payment_term_id is set by query-string in a link (from website_sale_delivery)
@route(['/shop/payment'], type='http', auth="public", website=True)
def payment(self, **post):
order = request.website.sale_get_order()
payment_term_id = post.get('payment_term_id')
if order.amount_total <= request.website.payment_deposit_threshold:
if payment_term_id:
payment_term_id = int(payment_term_id)
if order:
order._check_payment_term_quotation(payment_term_id)
if payment_term_id:
return request.redirect("/shop/payment")
else:
order.payment_term_id = False
return super(WebsiteSalePaymentTerms, self).payment(**post)
# Main JS driven payment term updater.
@route(['/shop/update_payment_term'], type='json', auth='public', methods=['POST'], website=True, csrf=False)
def update_order_payment_term(self, **post):
order = request.website.sale_get_order()
payment_term_id = int(post['payment_term_id'])
try:
if order:
order._check_payment_term_quotation(payment_term_id)
return self._update_website_payment_term_return(order, **post)
except:
return {'error': '[101] Unable to update payment terms.'}
# Return values after order payment_term_id is updated
def _update_website_payment_term_return(self, order, **post):
if order:
return {
'payment_term_name': order.payment_term_id.name,
'payment_term_id': order.payment_term_id.id,
'note': order.payment_term_id.note,
'require_payment': order.require_payment,
}
return {}
@route(['/shop/reject_term_agreement'], type='http', auth='public', website=True)
def reject_term_agreement(self, **kw):
order = request.website.sale_get_order()
if order:
partner = request.env.user.partner_id
order.write({'payment_term_id': request.website.sale_get_payment_term(partner),
'require_payment': True})
return request.redirect('/shop/cart')
# Confirm order without taking payment
@route(['/shop/confirm_without_payment'], type='http', auth='public', website=True)
def confirm_without_payment(self, **post):
order = request.website.sale_get_order()
if not order:
return request.redirect('/shop')
if order.require_payment:
return request.redirect('/shop/payment')
if not order.payment_term_id or (
order.payment_term_id.deposit_percentage or order.payment_term_id.deposit_flat):
return request.redirect('/shop/payment')
# made it this far, lets confirm
order.sudo().action_confirm()
request.session['sale_last_order_id'] = order.id
# cleans session/context
# This should always exist, but it is possible to
if request.website and request.website.sale_reset:
request.website.sale_reset()
return request.redirect('/shop/confirmation')

View File

@@ -0,0 +1,5 @@
from . import account
from . import payment
from . import res_config
from . import sale
from . import website

View File

@@ -0,0 +1,7 @@
from odoo import fields, models
class AccountPaymentTerm(models.Model):
_inherit = 'account.payment.term'
allow_in_website_sale = fields.Boolean('Allow in website checkout')

View File

@@ -0,0 +1,21 @@
from odoo import models, _
class PaymentTransaction(models.Model):
_inherit = 'payment.transaction'
def render_sale_button(self, order, submit_txt=None, render_values=None):
values = {
'partner_id': order.partner_id.id,
'type': self.type,
}
if render_values:
values.update(render_values)
# Not very elegant to do that here but no choice regarding the design.
self._log_payment_transaction_sent()
return self.acquirer_id.with_context(submit_class='btn btn-primary', submit_txt=submit_txt or _('Pay Now')).sudo().render(
self.reference,
order.amount_total_deposit or order.amount_total,
order.pricelist_id.currency_id.id,
values=values,
)

View File

@@ -0,0 +1,9 @@
from odoo import fields, models
class WebsiteConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'
payment_deposit_threshold = fields.Monetary(string="Payment Deposit Threshold",
related="website_id.payment_deposit_threshold",
readonly=False)

View File

@@ -0,0 +1,18 @@
from odoo import fields, models
class SaleOrder(models.Model):
_inherit = 'sale.order'
def _check_payment_term_quotation(self, payment_term_id):
self.ensure_one()
if payment_term_id and self.payment_term_id.id != payment_term_id:
# TODO how can we set a default if we can only set ones partner has assigned...
# Otherwise.. how do we prevent using any payment term by ID?
payment_term = self.env['account.payment.term'].sudo().browse(payment_term_id)
if not payment_term.exists():
raise Exception('Could not find payment terms.')
self.write({
'payment_term_id': payment_term_id,
'require_payment': bool(payment_term.deposit_percentage) or bool(payment_term.deposit_flat),
})

View File

@@ -0,0 +1,12 @@
from odoo import fields, models
class Website(models.Model):
_inherit = 'website'
payment_deposit_threshold = fields.Monetary(string='Payment Deposit Threshold',
help='Allow customers to make a deposit when their order '
'total is above this amount.')
def get_payment_terms(self):
return self.env['account.payment.term'].search([('allow_in_website_sale', '=', True)])

View File

@@ -0,0 +1,107 @@
odoo.define('website_sale_payment_terms.payment_terms', function (require) {
"use strict";
require('web.dom_ready');
var ajax = require('web.ajax');
var concurrency = require('web.concurrency');
var dp = new concurrency.DropPrevious();
var publicWidget = require('web.public.widget');
require('website_sale_delivery.checkout');
console.log('Payment Terms V10.1');
var available_term = $('input[name="payment_term_id"]').length;
if (available_term > 0) {
console.log('Payment term detected');
// Detect pay button and disable on page load - This isn't ideal but there is something I cant find that is preventing disabling this
setTimeout(function(){ $('#o_payment_form_pay').prop('disabled', true); }, 500);
} else {
console.log('no payment term detected');
}
// Calculate amount Due Now
function calculate_deposit(t, d, f) {
var amount = t * d / 100 + f;
if (amount > 0) {
amount = amount.toFixed(2);
amount = amount.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return amount;
} else {
amount = 0.00;
return amount;
}
}
// Payment input clicks update sale.order payment_term_id
var _onPaymentTermClick = function (ev) {
$('#o_payment_form_pay').prop('disabled', true);
var payment_term_id = $(ev.currentTarget).val();
var values = {'payment_term_id': payment_term_id};
dp.add(ajax.jsonRpc('/shop/update_payment_term', 'call', values).then(_onPaymentTermUpdateAnswer));
};
var $payment_terms = $("#payment_terms input[name='payment_term_id']");
$payment_terms.click(_onPaymentTermClick);
// All input clicks update due amount
function updateAmountDue() {
var $amount_total = $('#order_total span.oe_currency_value').html().replace(',', '');
$amount_total = parseFloat($amount_total);
var $checked = $('input[name="payment_term_id"]:checked');
var $deposit_percentage = $checked.attr('data-deposit-percentage');
var $deposit_flat = parseFloat($checked.attr('data-deposit-flat'));
var $due_amount = calculate_deposit($amount_total, $deposit_percentage, $deposit_flat);
$('#order_due_today span.oe_currency_value').html($due_amount);
}
// update amount due after delivery options change
publicWidget.registry.websiteSaleDelivery.include({
_handleCarrierUpdateResult: function (result) {
this._super.apply(this, arguments);
updateAmountDue();
},
});
// Show amount due if operation is a success
var _onPaymentTermUpdateAnswer = function (result) {
if (!result.error) {
// Get Payment Term note/description for modal
var note = result.note;
if (!result.note) {
note = result.payment_term_name;
}
// Change forms based on order payment requirement
updateAmountDue();
if(!result.require_payment) {
$('#payment_method').hide();
$('#non_payment_method').show();
$('#order_due_today').hide();
} else {
$('#payment_method').show();
$('#non_payment_method').hide();
$('#order_due_today').show();
}
// Open success modal with message
$('#payment_term_success_modal .success-modal-note').text(note);
$('#payment_term_success_modal').modal();
} else {
// Open error modal
console.error(result);
$('#payment_term_error_modal').modal();
}
};
});
function deny_payment_terms() {
$('#o_payment_form_pay').prop('disabled', true);
window.location = '/shop/reject_term_agreement';
}
function accept_payment_terms() {
$('#o_payment_form_pay').prop('disabled', false);
}

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="view_payment_term_form" model="ir.ui.view">
<field name="name">view.payment.term.form.inherit.website</field>
<field name="model">account.payment.term</field>
<field name="inherit_id" ref="account.view_payment_term_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='note']" position="before">
<group>
<field name="allow_in_website_sale"/>
</group>
</xpath>
</field>
</record>
</odoo>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="res_config_settings_view_form" model="ir.ui.view">
<field name="name">res.config.settings.view.form.inherit.payment.terms</field>
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="website_sale.res_config_settings_view_form"/>
<field name="arch" type="xml">
<xpath expr="//div[@id='website_tax_inclusion_setting']" position="after">
<div class="col-12 col-lg-6 o_setting_box" id="website_payment_deposit_threshold">
<div class="o_setting_right_pane">
<label string="Deposit Threshold" for="payment_deposit_threshold"/>
<div class="text-muted">
Allow customers to make percentage or flat deposits above this amount on website.
</div>
<div class="content-group">
<div class="mt16">
<field name="payment_deposit_threshold" class="o_light_label"/>
</div>
</div>
</div>
</div>
</xpath>
</field>
</record>
</odoo>

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<template id="web_assets_frontend" inherit_id="website.assets_frontend">
<xpath expr="//script[last()]" position="after">
<script type="text/javascript" src="/website_sale_payment_terms/static/src/js/payment_terms.js"/>
</xpath>
</template>
</odoo>

View File

@@ -0,0 +1,145 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<!-- Payment terms list items for /shop/payment -->
<template id="payment_term_items">
<t t-set="partner_term" t-value="order.partner_id.property_payment_term_id"/>
<!-- Show current partners payment terms -->
<t t-if="partner_term and partner_term not in website_terms">
<li class="list-group-item">
<input t-att-value="partner_term.id"
t-att-data-deposit-percentage="partner_term.deposit_percentage or '0'"
t-att-data-deposit-flat="partner_term.deposit_flat or '0'"
name="payment_term_id"
t-att-id="'payment_term_%i' % partner_term.id"
type="radio"/>
<label t-att-for="'payment_term_%i' % partner_term.id"
t-field="partner_term.name"
class="label-optional"/>
</li>
</t>
<!-- Show default option set by account.payment.term boolean -->
<li t-foreach="website_terms" t-as="term" class="list-group-item">
<input t-att-value="term.id"
t-att-data-deposit-percentage="term.deposit_percentage or '0'"
t-att-data-deposit-flat="term.deposit_flat or '0'"
t-att-id="'payment_term_%i' % term.id"
type="radio"
name="payment_term_id"/>
<label t-att-for="'payment_term_%i' % term.id"
t-field="term.name"
class="label-optional"/>
</li>
</template>
<!-- Add placeholder for list of payment terms to /shop/payment -->
<template id="payment_terms" inherit_id="website_sale.payment" name="Payment Terms Info">
<xpath expr="//div[@id='payment_method']" position="before">
<t t-set="website_terms" t-value="website.get_payment_terms()" />
<t t-if="website_terms and website_sale_order.amount_total &gt; website.payment_deposit_threshold">
<t t-call="website_sale_payment_terms.payment_term_success_modal"/>
<t t-call="website_sale_payment_terms.payment_term_error_modal"/>
<div id="payment_terms">
<h3 class="mb24 mt24">Payment Terms</h3>
<div class="card border-0">
<ul class="list-group">
<t t-call="website_sale_payment_terms.payment_term_items"/>
</ul>
</div>
</div>
</t>
</xpath>
<xpath expr="//div[@id='payment_method']" position="after">
<!-- Bypass Validation for users with 0 deposit payment terms -->
<div class="mt-3" style="display:none;" id="non_payment_method">
<a href="/shop/confirm_without_payment" class="float-right btn btn-primary">
<span>Confirm Order <span class="fa fa-chevron-right"/></span>
</a>
</div>
</xpath>
</template>
<!-- Modal to handle success message -->
<template id="payment_term_success_modal">
<div class="modal fade" id="payment_term_success_modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title text-info">User Agreement</h4>
<button type="button" class="close" data-dismiss="modal">
<i class="fa fa-times"/>
</button>
</div>
<div class="modal-body">
<p>By clicking Confirm you are agreeing to the payment terms indicated below:</p>
<p class="success-modal-note"></p>
</div>
<div class="modal-footer">
<button type="button"
class="btn btn-default"
onclick="accept_payment_terms()"
data-dismiss="modal">Accept
</button>
<button type="button"
class="btn btn-default"
onclick="deny_payment_terms()">Deny
</button>
</div>
</div>
</div>
</div>
</template>
<!-- Modal to handle error message -->
<template id="payment_term_error_modal">
<div class="modal fade" id="payment_term_error_modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title text-danger">Whoops!</h4>
<button type="button" class="close" data-dismiss="modal">
<i class="fa fa-times"/>
</button>
</div>
<div class="modal-body">
<p>Something Went Wrong. Please contact us to resolve this issue.</p>
<sup class="text-danger">Error Code: DEF-PTM</sup>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</template>
<!-- Add empty div to calculate amount due today in payment_terms.js -->
<template id="amount_due_today" inherit_id="website_sale.total">
<xpath expr="//tr[@id='order_total']" position="after">
<tr id="order_due_today" t-att-style="'' if website_sale_order.amount_total_deposit else 'display: none;'">
<td class="text-right text-info">
<strong>Due Now:</strong>
</td>
<td class="text-xl-right">
<strong t-field="website_sale_order.amount_total_deposit"
t-options='{"widget": "monetary", "display_currency": website_sale_order.pricelist_id.currency_id}'/>
</td>
</tr>
</xpath>
</template>
<template id="confirmation" inherit_id="website_sale.confirmation">
<xpath expr="//strong[@t-field='order.amount_total']" position="replace">
<strong t-esc="payment_tx_id.amount" t-options="{'widget': 'monetary', 'display_currency': order.pricelist_id.currency_id}" />
</xpath>
</template>
<template id="payment_confirmation_status" inherit_id="website_sale.payment_confirmation_status">
<xpath expr="//div[hasclass('oe_website_sale_tx_status')]/div[1]" position="attributes">
<attribute name="t-if">payment_tx_id</attribute>
</xpath>
</template>
</odoo>

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="view_website_form" model="ir.ui.view">
<field name="name">view.website.form.inherit.payment.terms</field>
<field name="model">website</field>
<field name="inherit_id" ref="website.view_website_form"/>
<field name="arch" type="xml">
<xpath expr="//group[@name='other']" position="inside">
<field name="currency_id" invisible="1"/>
<field name="payment_deposit_threshold"/>
</xpath>
</field>
</record>
</odoo>