You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

705 lines
24 KiB

/*
© 2020 Le Filament (<http://www.le-filament.com>)
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
*/
odoo.define('vracoop_pos_free_balance_V2.container_balance', function (require) {
"use strict";
var chrome = require('point_of_sale.chrome');
var gui = require('point_of_sale.gui');
var models = require('point_of_sale.models');
var screens = require('point_of_sale.screens');
var popups = require('point_of_sale.popups');
var container = require('pos_container.container');
var models_and_db = require('pos_container.models_and_db');
var core = require('web.core');
var rpc = require('web.rpc');
var utils = require('web.utils');
var QWeb = core.qweb;
var _t = core._t;
var round_pr = utils.round_precision;
var ConfirmPopupWidgetPesee = popups.extend({
template: 'ConfirmPopupWidgetPesee',
});
gui.define_popup({name:'confirm-pesee', widget: ConfirmPopupWidgetPesee});
// The initial screen that allows you to scan container
var PresentationScreenWidget = screens.ScreenWidget.extend({
template: 'PresentationScreenWidget',
// Ignore products, discounts, and client barcodes
// barcode_product_action: function(code){},
barcode_discount_action: function(code){},
barcode_client_action: function(code){},
init: function(parent, options) {
this._super(parent, options);
this.transactions = [];
this.editing = false;
},
// this method shows the screen and sets up all the widget related to this screen. Extend this method
// if you want to alter the behavior of the screen.
show: function(){
this._super();
var scale_screen = this.gui.screen_instances['balancescale'];
scale_screen.$el.removeClass('oe_hidden');
var screen = this.gui.screen_instances['products'];
screen.$el.removeClass('oe_hidden');
$("#pos-header-text-selec").removeClass('oe_hidden');
$("#validation-bloc").addClass('oe_hidden');
$("#pos-topheader-scale").addClass('oe_hidden');
$("#pos-header-text-confirm").addClass('oe_hidden');
},
// this methods hides the screen. It's not a good place to put your cleanup stuff as it is called on the
// POS initialization.
hide: function(){
this._super();
var screen = this.gui.screen_instances['products'];
screen.$el.addClass('oe_hidden');
$("#pos-header-text-selec").addClass('oe_hidden');
$("#pos-topheader-scale").addClass('oe_hidden');
},
});
gui.define_screen({
'name': 'presentation',
'widget': PresentationScreenWidget,
'condition': function(){
return this.pos.config.balance_id;
},
});
// Screen confirmation de la pesée
var ConfirmationScreen = screens.ScreenWidget.extend({
template: 'ConfirmationScreen',
next_screen: 'presentation',
previous_screen: 'products',
init: function(parent, options) {
this._super(parent, options);
},
show: function(){
this._super();
var self = this;
this.renderElement();
var scale_screen = this.gui.screen_instances['balancescale'];
scale_screen.$el.removeClass('oe_hidden');
this.$('.next,.back-presentation').click(function(){
self.gui.show_screen('presentation');
});
$("#pos-header-text-confirm").removeClass('oe_hidden');
$("#validation-bloc").addClass('oe_hidden');
$("#pos-topheader-scale").addClass('oe_hidden');
},
hide: function(){
this._super();
$("#pos-topheader-scale").addClass('oe_hidden');
},
get_product_name: function(){
var product = this.gui.get_current_screen_param('product');
return (product ? product.display_name : undefined) || 'Unnamed Product';
},
// get_product_price: function(){
// var product = this.gui.get_current_screen_param('product');
// return (product ? product.get_price(pricelist, 2) : 0) || 0;
// },
// get_product_uom: function(){
// var product = this.gui.get_current_screen_param('product');
// if(product){
// return this.pos.units_by_id[product.uom_id[0]].name;
// }else{
// return '';
// }
// },
});
gui.define_screen({
'name': 'confirmation',
'widget': ConfirmationScreen,
});
// Add the FloorScreen to the GUI, and set it as the default screen
chrome.Chrome.include({
build_widgets: function(){
this._super();
if (this.pos.config.balance_id) {
this.gui.set_startup_screen('presentation');
}
},
});
gui.Gui.include({
show_saved_screen: function(order,options) {
this._super();
options = options || {};
this.close_popup();
this.show_screen(this.startup_screen);
},
});
// We need to modify the OrderSelector to hide itself when we're on
// the floor plan
chrome.OrderSelectorWidget.include({
hide: function(){
this.$el.addClass('oe_invisible');
},
show: function(){
this.$el.removeClass('oe_invisible');
},
renderElement: function(){
var self = this;
this._super();
if (this.pos.config.balance_id) {
if (this.pos.get_order()) {
this.$el.removeClass('oe_invisible');
} else {
this.$el.addClass('oe_invisible');
}
}
},
});
screens.ProductScreenWidget.include({
previous_screen: 'presentation',
hide: function(){
this._super();
$("#pos-header-text-prod").addClass('oe_hidden');
$("#pos-topheader-scale").addClass('oe_hidden');
var screen = this.gui.screen_instances['products'];
// screen.$el.removeClass('oe_hidden');
},
show: function(){
this._super();
var self = this;
this.product_categories_widget.reset_category();
this.numpad.state.reset();
this.$('.back').click(function(){
self.gui.show_screen('presentation');
});
$("#pos-header-text-prod").removeClass('oe_hidden');
$("#pos-topheader-scale").addClass('oe_hidden');
var container = this.gui.get_current_screen_param('container');
if (container) {
this.pos.proxy.reset_tare();
}
},
renderElement: function(){
this._super();
var self = this;
this.$('.back').click(function () {
self.gui.show_screen('presentation');
});
},
click_product: function(product) {
if (this.pos.config.is_balance_free){
var order = this.pos.get_order();
var selected_orderline = order.get_selected_orderline();
if (product.to_weight && selected_orderline &&
selected_orderline.product === this.pos.get_container_product()){
var container = selected_orderline.get_container();
this.gui.show_screen(
'balancescale',
{product: product,
container: container,
old_orderline: selected_orderline});
} else {
this._super(product);
}
}
else{
this._super(product);
}
$("#validation-bloc").removeClass('oe_hidden');
},
close: function(){
this._super();
this.pos.proxy_queue.clear();
},
});
screens.ScreenWidget.include({
barcode_container_action: function(code){
var self = this;
if (self.pos.scan_container(code)) {
// Vérfification: est-ce qu'un container vient d'être utilisé dans l'heure
if (self.pos.scan_container_check(code)){
var transaction = self.pos.scan_container_check(code);
this.gui.show_popup('doublon-barcode',{
title: _t('Contenu déjà enregistré récemment:'),
transaction: transaction,
confirm: function(){
var transaction = self.pos.scan_container_check(code)
self.delete_selected_transaction(transaction, code);
self.gui.show_screen('products');
},
});
} else {
self.gui.show_screen('products');
}
} else {
self.gui.show_screen('balancecontainerscale', {barcode: code.base_code});
}
},
delete_selected_transaction: function(transaction, barcode){
var self = this;
if (!transaction.id){
self.deleted_transaction(transaction.container_ean13)
}
else {
rpc.query({
model: 'pos.transaction',
method: 'unlink',
args: [transaction.id],
}).then(function(){
self.deleted_transaction(transaction.container_ean13);
},function(err,ev){
ev.preventDefault();
var error_body = _t('Your Internet connection is probably down.');
if (err.data) {
var except = err.data;
error_body = except.arguments && except.arguments[0] || except.message || error_body;
}
self.gui.show_popup('error',{
'title': _t('Error: Could not Save Changes'),
'body': error_body,
});
}
);
}
},
deleted_transaction: function(barcode){
var self = this;
this.pos.db.remove_transactions([barcode]);
},
});
var BalanceScaleScreenWidget = screens.ScaleScreenWidget.extend({
template: 'BalanceScaleScreenWidget',
next_screen: 'confirmation',
previous_screen: 'products',
init: function(parent, options){
this._super(parent, options);
this.weight_container = 0;
this.weight_brut = 0;
this.weight = 0;
},
show: function(){
// $("#validation-bloc").removeClass('oe_hidden');
// this._super();
var self = this;
var queue = this.pos.proxy_queue;
this.set_weight(0);
this.renderElement();
var container = this.gui.get_current_screen_param('container');
queue.schedule(function () {
return self.pos.proxy.reset_weight().then(function () {
self.set_weight(0);
self.set_price(0);
});
}, {duration: 500});
// format price
var price = this.format_price(this.get_product_price());
if (container) {
// format tare
var tare = this.format_tare(container);
queue.schedule(function () {
return self.pos.proxy.scale_read_data_price_tare(price, tare).then(function (scale_answer) {
self.set_weight(scale_answer.weight);
self.set_price(scale_answer.price);
if ((scale_answer.error === '30' || scale_answer.error === '31') && scale_answer.weight !== 0) {
self.gui.show_screen(self.next_screen);
// add product *after* switching screen to scroll properly
self.order_product();
self.pos.proxy.reset_tare();
}
});
}, {duration: 500, repeat: true});
} else {
queue.schedule(function () {
return self.pos.proxy.scale_read_data_price(price).then(function (scale_answer) {
self.set_weight(scale_answer.weight);
self.set_price(scale_answer.price);
if ((scale_answer.error === '30' || scale_answer.error === '31') && scale_answer.weight !== 0) {
self.gui.show_screen(self.next_screen);
// add product *after* switching screen to scroll properly
self.order_product();
}
});
}, {duration: 500, repeat: true});
}
this._super();
var self = this;
this.$('.next,.add-transaction').click(function(){
self.create_transaction();
});
$("#pos-topheader-scale").removeClass('oe_hidden');
$("#pos-header-text-peser").removeClass('oe_hidden');
},
get_container: function(){
return this.gui.get_current_screen_param('container');
},
set_weight: function(weight){
var container = this.get_container();
this.weight = weight;
this.weight_container = container.weight;
this.weight_brut = container.weight + this.weight;
this.$('.weight').text(this.get_product_weight_string());
this.$('.computed-price').text(this.get_computed_price_string());
this.$('.weight-brut').text(this.get_product_weight_string_brut());
},
//////////////////////////////
// Ajout fonction Toledo?
set_price: function (price) {
if (!price) {
this.$('.computed-price').text(this.get_computed_price_string());
} else {
this.price = price;
//this.$('.price').text(this.format_currency(price));
this.$('.computed-price').text(this.format_currency(price));
}
},
get_price: function () {
return this.price;
},
format_tare: function (container) {
var tare = (Math.abs(container.weight) * 1000).toString();
tare = ("0000" + tare).slice(-4);
return tare;
},
format_price: function (product_price) {
var price = (product_price * 1000).toString();
price = ("000000" + price).slice(-6);
return price;
},
// FIN
//////////////////////////////
get_current_container_weight: function(){
var container = this.get_container();
if (container){
return (this.weight_container || 0).toFixed(3) + ' kg';
}
else{
''
}
},
get_current_container_name: function(){
var container = this.get_container();
if (container){
return container.name;
}
else{
''
}
},
get_product_weight_string_brut: function(){
var product = this.get_product();
var defaultstr = (this.weight + this.weight_container || 0).toFixed(3) + ' Kg';
if(!product || !this.pos){
return defaultstr;
}
var unit_id = product.uom_id;
if(!unit_id){
return defaultstr;
}
var unit = this.pos.units_by_id[unit_id[0]];
var weight = round_pr(this.weight + this.weight_container || 0, unit.rounding);
var weightstr = weight.toFixed(Math.ceil(Math.log(1.0/unit.rounding) / Math.log(10) ));
weightstr += ' ' + unit.name;
return weightstr;
},
hide: function(){
this._super();
$("#pos-topheader-scale").addClass('oe_hidden');
$("#pos-header-text-peser").addClass('oe_hidden');
},
create_transaction: function(){
var self = this;
var fields = {};
var container = this.get_container();
var product = this.get_product();
var qrcode = '';
var ean13 = '';
var ean13_verif = '';
fields['qrcode'] = qrcode.concat('https://qr.mayam.fr/', container.barcode);
fields['container_ean13'] = container.barcode;
fields['product_id'] = this.get_product().id;
// var product_id = (this.get_product().id).toString();
var product_id = ("00000" + product.default_code.toString()).slice(-5);
var weight_str = (this.weight * 1000).toString();
weight_str = ("00000" + weight_str).slice(-5);
ean13 = ean13.concat(26,product_id,weight_str,4);
var weight_brut_str = (this.weight_brut * 1000).toString();
weight_brut_str = ("00000" + weight_brut_str).slice(-5);
ean13_verif = ean13_verif.concat(26,'00999',weight_brut_str,4);
var ean13_digit = this.pos.barcode_reader.barcode_parser.sanitize_ean(ean13);
var ean13_verif_digit = this.pos.barcode_reader.barcode_parser.sanitize_ean(ean13_verif);
fields['ean13'] = ean13_digit;
fields['ean13_verif'] = ean13_verif_digit;
fields['balance_id'] = this.pos.get_balance_id();
var today = new Date();
var date = today.getFullYear() + '-' + (today.getMonth()+1) + '-' + today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var date_time = date + ' ' + time;
fields['write_date'] = date_time;
fields['weight_net'] = this.weight;
var pricelist = this._get_active_pricelist();
fields['price_product'] = (product ? product.get_price(pricelist, this.weight) : 0) || 0;
fields['price_net'] = fields['weight_net'] * fields['price_product'];
fields.name = product.display_name;
this.pos.push_transaction(fields).then(
this.pushed_transaction(fields["ean13"])
);
},
pushed_transaction: function(barcode){
var self = this;
var product = this.get_product();
console.log(product);
this.gui.show_screen('confirmation',{product: product});
// self.gui.show_screen(self.next_screen);
// self.gui.show_popup('confirm-pesee',{
// 'title': _t('Merci'),
// 'body': _t('La pesée est validée'),
// confirm: function(){
// self.gui.show_screen(self.next_screen);
// },
// });
},
});
gui.define_screen({
name:'balancescale',
widget: BalanceScaleScreenWidget,
});
/*--------------------------------------*\
| THE SCALE SCREEN FREE |
| BALANCE CONTAINER |
\*======================================*/
// The free balance container scale screen
// displays the weight of
// a new container on the electronic scale.
var BalanceContainerScaleScreenWidget = screens.ScaleScreenWidget.extend({
template: 'BalanceContainerScaleScreenWidget',
next_screen: 'presentation',
previous_screen: 'presentation',
init: function(parent, options){
this._super(parent, options);
},
show: function(){
var self = this;
var queue = this.pos.proxy_queue;
var priceStr = '001000'; // bizerba doesn't accept '000000' as unit price
queue.schedule(function () {
return self.pos.proxy.reset_weight().then(function () {
self.set_weight(0);
});
}, {duration: 500});
queue.schedule(function () {
return self.pos.proxy.scale_read_data_price(priceStr).then(function (scale_answer) {
self.set_weight(scale_answer.weight);
if ((scale_answer.error === '30' || scale_answer.error === '31') && scale_answer.weight !== 0) {
self.gui.show_screen(self.next_screen);
self.create_container();
}
});
}, {duration: 500, repeat: true});
this._super();
var self = this;
this.$('.next,.add-container').click(function(){
self.create_container();
});
if(this.pos.config.iface_vkeyboard && this.chrome.widget.keyboard){
this.chrome.widget.keyboard.connect($(this.el.querySelector('.container-name input')));
}
$("#pos-header-text-peser").removeClass('oe_hidden');
},
hide: function(){
this._super();
$("#pos-header-text-peser").addClass('oe_hidden');
},
get_product: function(){
return this.pos.get_container_product();
},
create_container: function(){
var self = this;
var fields = {};
fields['weight'] = this.weight;
fields.barcode = this.gui.get_current_screen_param('barcode') || false;
fields.name = fields.name || _t('Container');
this.pos.push_container(fields).then(
this.pushed_container(fields["barcode"],fields)
);
},
pushed_container: function(barcode,container){
var self = this;
var selected_order = this.pos.get_order();
selected_order.add_container(container);
self.gui.show_popup('confirm-pesee',{
'title': _t('Merci'),
'body': _t('La pesée est validée'),
confirm: function(){
self.gui.show_screen(self.next_screen);
},
});
},
close: function(){
this._super();
if (this.pos.config.iface_vkeyboard && this.chrome.widget.keyboard) {
this.chrome.widget.keyboard.hide();
}
},
});
gui.define_screen({
name:'balancecontainerscale',
widget: BalanceContainerScaleScreenWidget,
});
var CheckBarcodePopupDoublon = popups.extend({
template:'CheckBarcodePopupDoublon',
show: function(options){
var self = this;
options = options || {};
this.name = options.transaction.name ;
this.weight_net = options.transaction.weight_net ;
this.price_product = options.transaction.price_product.toFixed(2); ;
this.price_net = options.transaction.price_net.toFixed(2);
this._super(options);
this.renderElement();
},
});
gui.define_popup({name:'doublon-barcode', widget: CheckBarcodePopupDoublon});
return {
BalanceContainerScaleScreenWidget: BalanceContainerScaleScreenWidget,
PresentationScreenWidget: PresentationScreenWidget,
BalanceScaleScreenWidget: BalanceScaleScreenWidget,
CheckBarcodePopupDoublon: CheckBarcodePopupDoublon,
ConfirmPopupWidgetPesee: ConfirmPopupWidgetPesee,
ConfirmationScreen: ConfirmationScreen,
};
});