I need to apply a discount depending on the payment form used. Since seems a bit complicated to do, I've opted for adding a global discount on payment screen, replicating a function already present (pos_discount module).
JS code:
odoo.define('pos_discount.pos_discount2', function (require) {
"use strict";
var core = require('web.core');
var screens = require('point_of_sale.screens');
var _t = core._t;
var DiscountButton = screens.PaymentScreenWidget.extend({
template: 'DiscountButton2',
button_click: function(){
var self = this;
console.log (this);
this.gui.show_popup('number',{
'title': _t('Discount Percentage'),
'value': this.pos.config.discount_pc,
'confirm': function(val) {
val = Math.round(Math.max(0,Math.min(100,val)));
self.apply_discount(val);
},
});
},
apply_discount: function(pc) {
var order = this.pos.get_order();
var lines = order.get_orderlines();
var product = this.pos.db.get_product_by_id(this.pos.config.discount_product_id[0]);
// Remove existing discounts
var i = 0;
while ( i < lines.length ) {
if (lines[i].get_product() === product) {
order.remove_orderline(lines[i]);
} else {
i++;
}
}
// Add discount
var discount = - pc / 100.0 * order.get_total_with_tax();
if( discount < 0 ){
order.add_product(product, { price: discount });
}
},
});
screens.define_action_button({
'name': 'discount2',
'widget': DiscountButton2,
'condition': function(){
return this.pos.config.iface_discount && this.pos.config.discount_product_id;
},
});
});
XML:
<?xml version="1.0" encoding="UTF-8"?>
<templates id="template" inherit_id="point_of_sale.template">
<t t-extend="PaymentScreenWidget">
<t t-jquery="div[class='payment-buttons']" t-operation="after">
<t t-name="DiscountButton2">
<div class='control-button js_discount'>
<i class='fa fa-tag' /> Discount
</div>
</t>
</t>
</t>
</templates>
Template for inheriting:
<odoo>
<data>
<template id="assets" name="pos_discount2_data assets" inherit_id="point_of_sale.assets">
<xpath expr="." position="inside">
<script type="text/javascript" src="/pos_divina_custom/static/src/js/cpf.js"></script>
</xpath>
</template>
</data>
</odoo>
The button show right where I want, but I can't get a link to the event on mouse.click. Any clue?