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.
 
 
 

300 lines
11 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.models_and_db_balance', function (require) {
"use strict";
var PosDB = require('point_of_sale.DB');
var models = require('point_of_sale.models');
var rpc = require('web.rpc');
var config = require('web.config');
var config = require('web.config');
var core = require('web.core');
var QWeb = core.qweb;
var utils = require('web.utils');
var field_utils = require('web.field_utils');
var round_di = utils.round_decimals;
var round_pr = utils.round_precision;
// include not available => extend
models.PosModel = models.PosModel.extend({
scan_container_check: function(parsed_code){
var transactions = this.db.get_transactions_sorted(1000);
var container = this.db.get_container_by_barcode(
parsed_code.base_code);
var selected_order = this.get_order();
selected_order.add_container(container);
var today = new Date();
var date = today.getFullYear() + '-' + (today.getMonth()+1) + '-' + today.getDate();
// var isostring = today.toISOString();
// var localestring = today.toLocaleString();
// var dateUTC = today.toUTCString();
var min = today.getMinutes();
if (min < 10) {
min = "0" + today.getMinutes();
}
var hour = today.getHours()-1;
if (hour < 10) {
hour = "0" + hour;
}
var time = (hour) + ":" + min + ":" + today.getSeconds();
var date_time = date + ' ' + time;
for(var i = 0, len = transactions.length; i < len; i++) {
var transaction = transactions[i];
if (
(transaction.container_ean13 == parsed_code.base_code) &&
transaction.write_date > date_time ) {
return transaction;
}
}
return false;
},
// saves the transaction locally and try to send it to the backend.
// it returns a deferred that succeeds after having tried to send
// the container and all the other pending containers.
push_transaction: function(transaction, opts) {
opts = opts || {};
var self = this;
if(transaction){
this.db.add_transactions([transaction]);
}
var pushed = new $.Deferred();
this.flush_mutex.exec(function(){
var flushed = self._save_transactions_to_server(self.db.get_transactions_sorted(), opts);
flushed.always(function(ids){
pushed.resolve();
});
return flushed;
});
return pushed;
},
// send an array of containers to the server
// available options:
// - timeout: timeout for the rpc call in ms
// returns a deferred that resolves with the list of
// server generated ids for the sent containers
_save_transactions_to_server: function (transactions, options) {
var self = this;
var transactions= transactions.filter(transaction => !( "id" in transaction))
if (!transactions || !transactions.length) {
var result = $.Deferred();
result.resolve([]);
return result;
}
options = options || {};
var timeout = typeof options.timeout === 'number' ? options.timeout : 7500 * transactions.length;
return rpc.query({
model: 'pos.transaction',
method: 'create_from_ui',
args: [transactions],
}, {
timeout: timeout,
})
.then(function (server_ids) {
_.each(transactions, function(transaction, key){
transaction["id"] = server_ids[key]
});
self.set('failed',false);
return server_ids;
}).fail(function (type, error){
if(error.code === 200 ){ // Business Logic Error, not a connection problem
//if warning do not need to display traceback!!
if (error.data.exception_type == 'warning') {
delete error.data.debug;
}
// Hide error if already shown before ...
if ((!self.get('failed') || options.show_error) && !options.to_invoice) {
self.gui.show_popup('error-traceback',{
'title': error.data.message,
'body': error.data.debug
});
}
self.set('failed',error);
}
console.error('Failed to send transactions:', transactions);
});
},
// returns the header text from config
get_name_header: function(){
if (this.config.explication_header) {
return this.config.explication_header;
}
},
// returns the header text from config
get_balance_id: function(){
if (this.config.balance_id) {
return this.config.balance_id;
}
},
get_is_comptoir: function(){
if (this.config.is_comptoir) {
return this.config.is_comptoir;
}
},
// load_placeholder_transactions: function(){
// var self = this;
// var fields = _.find(this.models,function(model){
// return model.model === 'pos.transaction';
// }).fields;
// var domain = [['barcode', '=', 'CONTAINER']];
// // no need to load it when active because it is already done in standard
// return rpc.query({
// model: 'product.product',
// method: 'search_read',
// args: [domain, fields],
// }).then(function(products){
// self.db.add_products(_.map(products, function (product) {
// return new models.Product({}, product);
// }));
// });
// },
});
PosDB.include({
init: function(parent, options) {
this._super(parent, options);
this.transaction_sorted = [];
this.transaction_by_barcode = {};
this.transaction_by_id = {};
this.transaction_write_date = null;
this.header_text = "";
},
// returns the header text from config
get_name_header: function(){
return this.db.get_name_header();
},
add_transactions: function(transactions) {
var updated_count = 0;
var new_write_date = '';
for(var i = 0, len = transactions.length; i < len; i++) {
var transaction = transactions[i];
if (this.transaction_write_date &&
new Date(this.transaction_write_date).getTime() + 1000 >=
new Date(transaction.write_date).getTime() ) {
continue;
} else if ( new_write_date < transaction.write_date ) {
new_write_date = transaction.write_date;
}
if (!this.transaction_by_barcode[transaction.container_ean13]) {
this.transaction_sorted.push(transaction.container_ean13);
}
this.transaction_by_barcode[transaction.container_ean13] = transaction;
updated_count += 1;
}
this.transaction_write_date = new_write_date || this.transaction_write_date;
if (updated_count) {
// If there were updates, we need to completely
// rebuild the search string and the id indexing
// this.container_search_string = "";
this.transaction_by_id = {};
for (var barcode in this.transaction_by_barcode) {
var transaction = this.transaction_by_barcode[barcode];
if(transaction.id){
this.transaction_by_id[transaction.id] = transaction;
}
// this.container_search_string += this._container_search_string(container);
}
}
return updated_count;
},
get_orders: function(){
return this.load('transactions',[]);
},
get_transaction_by_id: function(id){
return this.transaction_by_id[id];
},
get_transactions_sorted: function(max_count){
max_count = max_count ? Math.min(this.transaction_sorted.length, max_count) : this.transaction_sorted.length;
var transactions = [];
for (var i = 0; i < max_count; i++) {
transactions.push(this.transaction_by_barcode[this.transaction_sorted[i]]);
}
return transactions;
},
remove_transactions: function(barcodes){
for(var i = 0; i < barcodes.length; i++) {
var transaction = this.transaction_by_barcode[barcodes[i]];
if (transaction){
var index_s = this.transaction_sorted.indexOf(transaction.container_ean13);
this.transaction_sorted.splice(index_s, 1);
delete this.transaction_by_id[transaction.id];
delete this.transaction_by_barcode[transaction.container_ean13];
}
}
},
transaction_by_barcode: function(barcode){
return this.transaction_by_barcode[barcode];
},
get_product_name: function(transaction){
return transaction.name;
},
get_today_date: function(){
var today = new Date();
var date = today.getFullYear() + '-' + (today.getMonth()+1) + '-' + today.getDate();
return date;
},
});
models.load_models({
model: 'pos.transaction',
fields: ['product_id', 'name', 'balance_id', 'ean13', 'write_date', 'container_ean13', 'weight_net', 'price_product', 'price_net', 'weight_tare'],
domain: function(self){ return [['write_date','>',self.db.get_today_date()]]; },
loaded: function(self, transactions){
self.db.add_transactions(transactions);
// return self.load_placeholder_transactions();
},
});
});