|
|
/* © 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 id of caisse from config
get_balance_id: function(){ if (this.config.balance_id) { return this.config.balance_id; } },
// returns the id of balance from config
get_caisse_id: function(){ if (this.config.caisse_id) { return this.config.caisse_id; } },
// returns if the pos is balance from config
get_is_balance_free: function(){ if (this.config.is_balance_free) { return this.config.is_balance_free; } },
get_is_comptoir: function(){ if (this.config.is_comptoir) { return this.config.is_comptoir; } },
});
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_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; },
});
// Add container to order line
models.Orderline = models.Orderline.extend({
export_as_JSON: function(){ var pack_lot_ids = []; if (this.has_product_lot){ this.pack_lot_lines.each(_.bind( function(item) { return pack_lot_ids.push([0, 0, item.export_as_JSON()]); }, this)); } return { qty: this.get_quantity(), price_unit: this.get_unit_price(), price_subtotal: this.get_price_without_tax(), price_subtotal_incl: this.get_price_with_tax(), discount: this.get_discount(), product_id: this.get_product().id, tax_ids: [[6, false, _.map(this.get_applicable_taxes(), function(tax){ return tax.id; })]], id: this.id, pack_lot_ids: pack_lot_ids, //custom starts here
tare: this.get_tare(), container_id: this.get_container() ? this.get_container().id : null, container_barcode: this.get_container() ? this.get_container().barcode : null, container_weight: this.get_container() ? this.get_container().weight : null, caisse_id: this.pos.config.caisse_id, }; }, }),
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); }, });
});
|