[IMP] website_sale_payment_terms: keep default terms on order

1. Keep default payment terms on order at checkout
2. Do not set terms on order until user agrees to conditions
3. Make date context-aware for terms amount calculation
This commit is contained in:
Cedric Collins
2022-03-04 18:56:51 -06:00
parent cebc7757ab
commit 24721b0bd4
4 changed files with 62 additions and 70 deletions

View File

@@ -14,16 +14,12 @@ class WebsiteSalePaymentTerms(WebsiteSaleDelivery):
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.

View File

@@ -9,7 +9,7 @@ class SaleOrder(models.Model):
@api.depends('amount_total', 'payment_term_id')
def _compute_amount_due_today(self):
today_string = fields.Date.to_string(fields.Date.today())
today_string = fields.Date.to_string(fields.Date.context_today(self))
for order in self:
amount = order.amount_total
if order.website_id and order.amount_total > order.website_id.payment_deposit_threshold and order.payment_term_id:

View File

@@ -14,7 +14,6 @@ odoo.define('website_sale_payment_terms.payment_terms', function (require) {
events: {
"click input[name='payment_term_id']": '_onPaymentTermClick',
"click #btn_accept_payment_terms": '_acceptPaymentTerms',
"click #btn_deny_payment_terms": '_denyPaymentTerms',
},
/**
@@ -25,10 +24,10 @@ odoo.define('website_sale_payment_terms.payment_terms', function (require) {
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');
var $payButton = $('button[name="o_payment_submit_button"]');
$payButton.prop('disabled', true);
var disabledReasons = $payButton.data('disabled_reasons') || {};
if ($('input[name="payment_term_id"][checked]')) {
if ($('input[name="payment_term_id"]:checked').length > 0) {
disabledReasons.payment_terms_selection = false;
} else {
disabledReasons.payment_terms_selection = true;
@@ -48,75 +47,67 @@ odoo.define('website_sale_payment_terms.payment_terms', function (require) {
* @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)));
ev.preventDefault();
$('button[name="o_payment_submit_button"]').prop('disabled', true);
// Get Payment Term note/description for modal
var selected_term = $(ev.currentTarget);
var note = $.parseHTML(selected_term.attr('data-note'));
if (!$(note).text()) {
note = $.parseHTML('<p>' + selected_term.attr('data-name') + '</p>');
}
// Open agreement modal with message
$('#payment_term_agreement_modal .success-modal-note').html(note);
$('#btn_accept_payment_terms').data('payment_term_id', selected_term.val());
$('#payment_term_agreement_modal').modal();
},
/**
* 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
* Set terms on the order
*
* @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>');
_acceptPaymentTerms: function (ev) {
var payment_term_id = $(ev.currentTarget).data('payment_term_id');
if (payment_term_id) {
dp.add(this._rpc({
'route': '/shop/update_payment_term',
'params': {'payment_term_id': payment_term_id},
}).then(this._onPaymentTermUpdateAmount.bind(this)));
}
},
/**
* Update amount on the page
*
* @param {Object} result
*/
_onPaymentTermUpdateAmount: function(result) {
if (result.error) {
// Open error modal
console.error(result.error);
$('#payment_term_error_modal').modal();
} else {
// Change forms based on order payment requirement
$('input[name="payment_term_id"]').filter(
(i, e)=>e.value==result.payment_term_id
).prop('checked', true);
$('#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);
// Update the total cost according to the selected shipping method
core.bus.trigger('update_shipping_cost', 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 $payButton = $('button[name="o_payment_submit_button"]');
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';
}
},
});

View File

@@ -9,6 +9,8 @@
<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-name="partner_term.name"
t-att-data-note="partner_term.note"
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"
@@ -23,6 +25,8 @@
<!-- 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-name="term.name"
t-att-data-note="term.note"
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"
@@ -41,7 +45,7 @@
<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_agreement_modal"/>
<t t-call="website_sale_payment_terms.payment_term_error_modal"/>
<h3 class="mb24 mt24">Payment Terms</h3>
<div class="card border-0">
@@ -73,9 +77,9 @@
</xpath>
</template>
<!-- Modal to handle success message -->
<template id="payment_term_success_modal">
<div class="modal fade" id="payment_term_success_modal" role="dialog">
<!-- Modal to handle agreement message -->
<template id="payment_term_agreement_modal">
<div class="modal fade" id="payment_term_agreement_modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
@@ -96,7 +100,8 @@
</button>
<button type="button"
class="btn btn-default"
id="btn_deny_payment_terms">Deny
id="btn_deny_payment_terms"
data-dismiss="modal">Deny
</button>
</div>
</div>