Merge branch 'mig/15.0/website_sale_payment_terms' into '15.0'

mig/15.0/website_sale_payment_terms into 15.0

See merge request hibou-io/hibou-odoo/suite!1320
This commit is contained in:
Cedric Collins
2022-01-28 00:01:52 +00:00
18 changed files with 625 additions and 0 deletions

View File

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

View File

@@ -0,0 +1,31 @@
{
'name': 'Website Payment Terms',
'author': 'Hibou Corp. <hello@hibou.io>',
'category': 'Sales',
'version': '15.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': [
'security/ir.model.access.csv',
'views/account_views.xml',
'views/res_config_views.xml',
'views/website_templates.xml',
'views/website_views.xml',
],
'assets': {
'web.assets_frontend': [
'/website_sale_payment_terms/static/src/js/payment_terms.js',
],
},
}

View File

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

View File

@@ -0,0 +1,88 @@
from odoo.http import request, route
from odoo.addons.website_sale_delivery.controllers.main import WebsiteSaleDelivery
class WebsiteSalePaymentTerms(WebsiteSaleDelivery):
def _get_shop_payment_values(self, order, **kwargs):
values = super(WebsiteSalePaymentTerms, self)._get_shop_payment_values(order, **kwargs)
values['amount'] = order.amount_due_today
return values
# 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, sitemap=False)
def shop_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).shop_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:
Monetary = request.env['ir.qweb.field.monetary']
currency = order.currency_id
return {
'payment_term_name': order.payment_term_id.name,
'payment_term_id': order.payment_term_id.id,
'note': order.payment_term_id.note,
'amount_due_today': order.amount_due_today,
'amount_due_today_html': Monetary.value_to_html(order.amount_due_today, {'display_currency': currency}),
}
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:
order.payment_term_id = False
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.amount_due_today:
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')
def _update_website_sale_delivery_return(self, order, **post):
res = super(WebsiteSalePaymentTerms, self)._update_website_sale_delivery_return(order, **post)
Monetary = request.env['ir.qweb.field.monetary']
currency = order.currency_id
if order:
res['amount_due_today'] = Monetary.value_to_html(order.amount_due_today, {'display_currency': currency})
res['amount_due_today_raw'] = order.amount_due_today
return res

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,30 @@
from odoo import models, _
import logging
_logger = logging.getLogger(__name__)
class PaymentTransaction(models.Model):
_inherit = 'payment.transaction'
# Override to confirm payments totaling the amount_due_today
def _check_amount_and_confirm_order(self):
self.ensure_one()
for order in self.sale_order_ids.filtered(lambda so: so.state in ('draft', 'sent')):
amount = order.amount_due_today
if order.currency_id.compare_amounts(self.amount, amount) == 0:
order.with_context(send_email=True).action_confirm()
else:
_logger.warning(
'<%s> transaction AMOUNT MISMATCH for order %s (ID %s): expected %r, got %r',
self.acquirer_id.provider, order.name, order.id,
amount, self.amount,
)
order.message_post(
subject=_("Amount Mismatch (%s)") % self.acquirer_id.provider,
body=_(
"The order was not confirmed despite response from the acquirer (%s): order total is %r but acquirer replied with %r.") % (
self.acquirer_id.provider,
amount,
self.amount,
)
)

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,29 @@
from odoo import api, fields, models
class SaleOrder(models.Model):
_inherit = 'sale.order'
amount_due_today = fields.Float('Due Now', compute='_compute_amount_due_today',
help='Amount due at checkout on the website.')
@api.depends('amount_total', 'payment_term_id')
def _compute_amount_due_today(self):
today_string = fields.Date.to_string(fields.Date.today())
for order in self:
amount = order.amount_total
if order.payment_term_id:
term_amount = [amt for date_string, amt in order.payment_term_id.compute(order.amount_total) if date_string == today_string]
term_amount = term_amount and term_amount[0] or 0.0
amount = term_amount if term_amount > order.amount_total_deposit else order.amount_total_deposit
order.amount_due_today = amount
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.payment_term_id = payment_term

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,2 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_account_payment_term_public,account.payment.term.public,account.model_account_payment_term,,1,0,0,0
1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
2 access_account_payment_term_public account.payment.term.public account.model_account_payment_term 1 0 0 0

View File

@@ -0,0 +1,136 @@
odoo.define('website_sale_payment_terms.payment_terms', function (require) {
"use strict";
require('web.dom_ready');
var concurrency = require('web.concurrency');
var core = require('web.core');
var dp = new concurrency.DropPrevious();
var publicWidget = require('web.public.widget');
require('website_sale_delivery.checkout');
publicWidget.registry.websiteSalePaymentTerms = publicWidget.Widget.extend({
selector: '.oe_payment_terms',
events: {
"click input[name='payment_term_id']": '_onPaymentTermClick',
"click #btn_accept_payment_terms": '_acceptPaymentTerms',
"click #btn_deny_payment_terms": '_denyPaymentTerms',
},
/**
* @override
*/
start: function () {
console.log('Payment Terms V10.4');
return this._super.apply(this, arguments).then(function () {
var available_term = $('input[name="payment_term_id"]').length;
if (available_term > 0) {
var $payButton = $('#o_payment_form_pay');
$payButton.prop('disabled', true);
var disabledReasons = $payButton.data('disabled_reasons') || {};
if ($('input[name="payment_term_id"][checked]')) {
disabledReasons.payment_terms_selection = false;
} else {
disabledReasons.payment_terms_selection = true;
}
$payButton.data('disabled_reasons', disabledReasons);
$payButton.prop('disabled', _.contains($payButton.data('disabled_reasons'), true));
}
});
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* @private
* @param {object} ev
*/
_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(this._rpc({
'route': '/shop/update_payment_term',
'params': values,
}).then(this._onPaymentTermUpdateAnswer.bind(this)));
},
/**
* Update the total cost according to the selected shipping method
*
* @private
* @param {float} amount : The new total amount of to be paid
*/
_updatePaymentAmount: function(amount){
core.bus.trigger('update_shipping_cost', amount);
},
/**
* Show amount due if operation is a success
*
* @private
* @param {Object} result
*/
_onPaymentTermUpdateAnswer: function (result) {
if (!result.error) {
// Get Payment Term note/description for modal
var note = $.parseHTML(result.note);
if (!$(note).text()) {
note = $.parseHTML('<p>' + result.payment_term_name + '</p>');
}
// Change forms based on order payment requirement
$('#order_due_today .monetary_field').html(result.amount_due_today_html);
if(result.amount_due_today == 0.0) {
$('#payment_method').hide();
$('#non_payment_method').show();
} else {
this._updatePaymentAmount(result.amount_due_today);
$('#payment_method').show();
$('#non_payment_method').hide();
}
// Open success modal with message
$('#payment_term_success_modal .success-modal-note').html(note);
$('#payment_term_success_modal').modal();
} else {
// Open error modal
console.error(result);
$('#payment_term_error_modal').modal();
}
},
/*
* @private
*/
_acceptPaymentTerms: function () {
var $payButton = $('#o_payment_form_pay');
var disabledReasons = $payButton.data('disabled_reasons') || {};
disabledReasons.payment_terms_selection = false;
$payButton.data('disabled_reasons', disabledReasons);
$payButton.prop('disabled', _.contains($payButton.data('disabled_reasons'), true));
},
/*
* @private
*/
_denyPaymentTerms: function () {
window.location = '/shop/reject_term_agreement';
},
});
// update amount due after delivery options change
publicWidget.registry.websiteSaleDelivery.include({
_handleCarrierUpdateResult: function (result) {
this._super.apply(this, arguments);
if (result.amount_due_today_raw !== undefined) {
this._updateShippingCost(result.amount_due_today_raw);
$('#order_due_today .monetary_field').html(result.amount_due_today);
}
},
});
return publicWidget.registry.websiteSalePaymentTerms;
});

View File

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

View File

@@ -0,0 +1,55 @@
from odoo.tests import tagged, TransactionCase
@tagged('post_install', '-at_install')
class TestWebsiteSalePaymentTerms(TransactionCase):
def setUp(self):
super(TestWebsiteSalePaymentTerms, self).setUp()
self.env.company.currency_id = self.browse_ref('base.USD')
self.so = self.env['sale.order'].create({
'partner_id': self.ref('base.res_partner_12'),
'order_line': [(0, 0, {
'product_id': self.env['product.product'].create({'name': 'Product A', 'list_price': 100}).id,
'product_uom_qty': 1,
'name': 'Product A',
'tax_id': False,
})]
})
def test_00_immediate(self):
# Double-check that we're asking for money if no payment terms are set
self.assertEqual(self.so.amount_due_today, self.so.amount_total)
immediate_terms = self.browse_ref('account.account_payment_term_immediate')
self.so._check_payment_term_quotation(immediate_terms.id)
self.assertEqual(self.so.amount_due_today, self.so.amount_total)
def test_10_thirty_percent(self):
thirty_percent_terms = self.browse_ref('account.account_payment_term_advance_60days')
self.so._check_payment_term_quotation(thirty_percent_terms.id)
self.assertEqual(self.so.amount_due_today, 30.0)
def test_20_fifteen_days(self):
fifteen_days_terms = self.browse_ref('account.account_payment_term_15days')
self.so._check_payment_term_quotation(fifteen_days_terms.id)
self.assertEqual(self.so.amount_due_today, 0.0)
def test_30_deposit_requested(self):
"""
Ask for deposit if deposit amount is greater
"""
thirty_percent_terms = self.browse_ref('account.account_payment_term_advance_60days')
thirty_percent_terms.deposit_percentage = 40
thirty_percent_terms.deposit_flat = 5
self.so._check_payment_term_quotation(thirty_percent_terms.id)
self.assertEqual(self.so.amount_due_today, 45.0)
def test_40_low_deposit(self):
"""
Ask for terms amount if greater than requested deposit
"""
thirty_percent_terms = self.browse_ref('account.account_payment_term_advance_60days')
thirty_percent_terms.deposit_percentage = 20
thirty_percent_terms.deposit_flat = 5
self.so._check_payment_term_quotation(thirty_percent_terms.id)
self.assertEqual(self.so.amount_due_today, 30.0)

View File

@@ -0,0 +1,15 @@
<?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="sale_payment_deposit.view_payment_term_form_inherit"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='deposit_flat']" position="after">
<field name="allow_in_website_sale"/>
</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,159 @@
<?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"/>
<t t-set="selected_term" t-value="order.payment_term_id or partner_term"/>
<!-- 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"
t-att-checked="partner_term == selected_term"
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"
t-att-checked="term == selected_term"
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">
<div class="oe_payment_terms">
<t t-call="website_sale_payment_terms.payment_term_success_modal"/>
<t t-call="website_sale_payment_terms.payment_term_error_modal"/>
<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="attributes">
<attribute name="t-att-style">'display: block;' if website_sale_order.amount_due_today else 'display: none;'</attribute>
</xpath>
<xpath expr="//div[@id='payment_method']" position="after">
<!-- Bypass Validation for users with 0 deposit payment terms -->
<div class="mt-3" t-if="website_sale_order.amount_total" t-att-style="'display: none;' if website_sale_order.amount_due_today else 'display: block;'" id="non_payment_method">
<div class="float-left mt-2">
<a role="button" href="/shop/cart" class="btn btn-secondary">
<i class="fa fa-chevron-left"/>
Return to Cart
</a>
</div>
<div class="float-right mt-2" >
<a href="/shop/confirm_without_payment" class="float-right btn btn-primary">
<span>Confirm Order <span class="fa fa-chevron-right"/></span>
</a>
</div>
</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"
id="btn_accept_payment_terms"
data-dismiss="modal">Accept
</button>
<button type="button"
class="btn btn-default"
id="btn_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">
<td class="text-right text-info">
<strong>Due Now:</strong>
</td>
<td class="text-xl-right">
<strong t-field="website_sale_order.amount_due_today" class="monetary_field"
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>