2012-05-14 14:03:40 +00:00
|
|
|
function openerp_pos_models(instance, module){ //module is instance.point_of_sale
|
2012-04-24 16:25:46 +00:00
|
|
|
var QWeb = instance.web.qweb;
|
2014-01-10 17:17:46 +00:00
|
|
|
var _t = instance.web._t;
|
2012-04-24 16:25:46 +00:00
|
|
|
|
2013-04-09 14:42:08 +00:00
|
|
|
var round_di = instance.web.round_decimals;
|
2013-04-09 14:35:36 +00:00
|
|
|
var round_pr = instance.web.round_precision
|
2012-04-24 16:25:46 +00:00
|
|
|
|
2012-05-21 14:09:01 +00:00
|
|
|
// The PosModel contains the Point Of Sale's representation of the backend.
|
|
|
|
// Since the PoS must work in standalone ( Without connection to the server )
|
|
|
|
// it must contains a representation of the server's PoS backend.
|
|
|
|
// (taxes, product list, configuration options, etc.) this representation
|
|
|
|
// is fetched and stored by the PosModel at the initialisation.
|
|
|
|
// this is done asynchronously, a ready deferred alows the GUI to wait interactively
|
|
|
|
// for the loading to be completed
|
|
|
|
// There is a single instance of the PosModel for each Front-End instance, it is usually called
|
2012-08-14 15:21:12 +00:00
|
|
|
// 'pos' and is available to all widgets extending PosWidget.
|
2012-05-21 14:09:01 +00:00
|
|
|
|
2012-04-24 16:25:46 +00:00
|
|
|
module.PosModel = Backbone.Model.extend({
|
|
|
|
initialize: function(session, attributes) {
|
|
|
|
Backbone.Model.prototype.initialize.call(this, attributes);
|
|
|
|
var self = this;
|
2012-08-09 17:21:13 +00:00
|
|
|
this.session = session;
|
2012-05-21 14:09:01 +00:00
|
|
|
this.flush_mutex = new $.Mutex(); // used to make sure the orders are sent to the server once at time
|
2014-01-10 17:17:46 +00:00
|
|
|
this.pos_widget = attributes.pos_widget;
|
2012-08-09 17:21:13 +00:00
|
|
|
|
2014-01-27 00:22:03 +00:00
|
|
|
this.proxy = new module.ProxyDevice(this); // used to communicate to the hardware devices via a local proxy
|
2014-01-03 13:09:30 +00:00
|
|
|
this.barcode_reader = new module.BarcodeReader({'pos': this, proxy:this.proxy}); // used to read barcodes
|
2013-12-16 14:35:59 +00:00
|
|
|
this.proxy_queue = new module.JobQueue(); // used to prevent parallels communications to the proxy
|
|
|
|
this.db = new module.PosDB(); // a local database used to search trough products and categories & store pending orders
|
2013-01-22 13:18:01 +00:00
|
|
|
this.debug = jQuery.deparam(jQuery.param.querystring()).debug !== undefined; //debug mode
|
2013-12-16 14:35:59 +00:00
|
|
|
|
|
|
|
// Business data; loaded from the server at launch
|
2013-12-24 15:11:12 +00:00
|
|
|
this.accounting_precision = 2; //TODO
|
|
|
|
this.company_logo = null;
|
|
|
|
this.company_logo_base64 = '';
|
2013-12-16 14:35:59 +00:00
|
|
|
this.currency = null;
|
|
|
|
this.shop = null;
|
|
|
|
this.company = null;
|
|
|
|
this.user = null;
|
|
|
|
this.users = [];
|
|
|
|
this.partners = [];
|
|
|
|
this.cashier = null;
|
|
|
|
this.cashregisters = [];
|
|
|
|
this.bankstatements = [];
|
|
|
|
this.taxes = [];
|
|
|
|
this.pos_session = null;
|
|
|
|
this.config = null;
|
|
|
|
this.units = [];
|
|
|
|
this.units_by_id = {};
|
|
|
|
this.pricelist = null;
|
2014-01-06 13:51:19 +00:00
|
|
|
window.posmodel = this;
|
2013-12-16 14:35:59 +00:00
|
|
|
|
|
|
|
// these dynamic attributes can be watched for change by other models or widgets
|
2012-04-24 16:25:46 +00:00
|
|
|
this.set({
|
2014-01-16 17:28:46 +00:00
|
|
|
'synch': { state:'connected', pending:0 },
|
2012-05-21 14:09:01 +00:00
|
|
|
'orders': new module.OrderCollection(),
|
2012-09-10 15:48:28 +00:00
|
|
|
'selectedOrder': null,
|
2012-04-24 16:25:46 +00:00
|
|
|
});
|
2012-05-21 14:09:01 +00:00
|
|
|
|
2014-01-16 17:28:46 +00:00
|
|
|
this.bind('change:synch',function(pos,synch){
|
|
|
|
clearTimeout(self.synch_timeout);
|
|
|
|
self.synch_timeout = setTimeout(function(){
|
|
|
|
if(synch.state !== 'disconnected' && synch.pending > 0){
|
|
|
|
self.set('synch',{state:'disconnected', pending:synch.pending});
|
|
|
|
}
|
|
|
|
},3000);
|
|
|
|
});
|
|
|
|
|
2013-09-05 10:43:57 +00:00
|
|
|
this.get('orders').bind('remove', function(order,_unused_,options){
|
|
|
|
self.on_removed_order(order,options.index,options.reason);
|
|
|
|
});
|
2012-04-24 16:25:46 +00:00
|
|
|
|
2012-07-11 16:03:25 +00:00
|
|
|
// We fetch the backend data on the server asynchronously. this is done only when the pos user interface is launched,
|
|
|
|
// Any change on this data made on the server is thus not reflected on the point of sale until it is relaunched.
|
2012-09-03 14:25:27 +00:00
|
|
|
// when all the data has loaded, we compute some stuff, and declare the Pos ready to be used.
|
2014-01-10 17:17:46 +00:00
|
|
|
this.ready = this.load_server_data()
|
|
|
|
.then(function(){
|
2014-01-17 17:58:30 +00:00
|
|
|
if(self.config.use_proxy){
|
|
|
|
return self.connect_to_proxy();
|
|
|
|
}
|
2014-01-10 17:17:46 +00:00
|
|
|
});
|
|
|
|
|
2012-09-03 14:25:27 +00:00
|
|
|
},
|
|
|
|
|
2013-09-02 15:59:07 +00:00
|
|
|
// releases ressources holds by the model at the end of life of the posmodel
|
|
|
|
destroy: function(){
|
|
|
|
// FIXME, should wait for flushing, return a deferred to indicate successfull destruction
|
|
|
|
// this.flush();
|
|
|
|
this.proxy.close();
|
|
|
|
this.barcode_reader.disconnect();
|
2014-01-17 17:58:30 +00:00
|
|
|
this.barcode_reader.disconnect_from_proxy();
|
2013-09-02 15:59:07 +00:00
|
|
|
},
|
2014-01-17 17:58:30 +00:00
|
|
|
connect_to_proxy: function(){
|
2014-01-10 17:17:46 +00:00
|
|
|
var self = this;
|
2014-01-29 13:57:10 +00:00
|
|
|
var done = new $.Deferred();
|
2014-01-17 17:58:30 +00:00
|
|
|
this.barcode_reader.disconnect_from_proxy();
|
2014-01-10 17:17:46 +00:00
|
|
|
this.pos_widget.loading_message(_t('Connecting to the PosBox'),0);
|
|
|
|
this.pos_widget.loading_skip(function(){
|
|
|
|
self.proxy.stop_searching();
|
|
|
|
});
|
2014-01-27 00:22:03 +00:00
|
|
|
this.proxy.autoconnect({
|
2014-01-10 17:17:46 +00:00
|
|
|
force_ip: self.config.proxy_ip || undefined,
|
|
|
|
progress: function(prog){
|
|
|
|
self.pos_widget.loading_progress(prog);
|
|
|
|
},
|
2014-01-27 00:22:03 +00:00
|
|
|
}).then(function(){
|
|
|
|
if(self.config.iface_scan_via_proxy){
|
|
|
|
self.barcode_reader.connect_to_proxy();
|
2014-01-10 17:17:46 +00:00
|
|
|
}
|
2014-01-29 13:57:10 +00:00
|
|
|
}).always(function(){
|
|
|
|
done.resolve();
|
2014-01-10 17:17:46 +00:00
|
|
|
});
|
2014-01-29 13:57:10 +00:00
|
|
|
return done;
|
2014-01-10 17:17:46 +00:00
|
|
|
},
|
|
|
|
|
2012-09-03 14:43:54 +00:00
|
|
|
// helper function to load data from the server
|
|
|
|
fetch: function(model, fields, domain, ctx){
|
2014-01-10 17:17:46 +00:00
|
|
|
this._load_progress = (this._load_progress || 0) + 0.05;
|
|
|
|
this.pos_widget.loading_message(_t('Loading')+' '+model,this._load_progress);
|
2012-09-03 14:43:54 +00:00
|
|
|
return new instance.web.Model(model).query(fields).filter(domain).context(ctx).all()
|
2012-09-03 14:59:58 +00:00
|
|
|
},
|
2012-09-03 14:25:27 +00:00
|
|
|
// loads all the needed data on the sever. returns a deferred indicating when all the data has loaded.
|
|
|
|
load_server_data: function(){
|
|
|
|
var self = this;
|
2012-05-21 14:09:01 +00:00
|
|
|
|
2012-09-03 14:43:54 +00:00
|
|
|
var loaded = self.fetch('res.users',['name','company_id'],[['id','=',this.session.uid]])
|
2012-10-31 10:31:48 +00:00
|
|
|
.then(function(users){
|
2013-12-16 14:35:59 +00:00
|
|
|
self.user = users[0];
|
2012-07-09 15:53:51 +00:00
|
|
|
|
2012-09-03 14:43:54 +00:00
|
|
|
return self.fetch('res.company',
|
2012-07-09 15:53:51 +00:00
|
|
|
[
|
|
|
|
'currency_id',
|
|
|
|
'email',
|
|
|
|
'website',
|
|
|
|
'company_registry',
|
|
|
|
'vat',
|
|
|
|
'name',
|
2012-08-20 13:59:57 +00:00
|
|
|
'phone',
|
|
|
|
'partner_id',
|
2012-07-09 15:53:51 +00:00
|
|
|
],
|
2014-01-27 16:10:05 +00:00
|
|
|
[['id','=',users[0].company_id[0]]],
|
|
|
|
{show_address_only: true});
|
2012-10-31 10:31:48 +00:00
|
|
|
}).then(function(companies){
|
2013-12-16 14:35:59 +00:00
|
|
|
self.company = companies[0];
|
2012-07-09 15:53:51 +00:00
|
|
|
|
2012-09-03 14:43:54 +00:00
|
|
|
return self.fetch('product.uom', null, null);
|
2012-10-31 10:31:48 +00:00
|
|
|
}).then(function(units){
|
2013-12-16 14:35:59 +00:00
|
|
|
self.units = units;
|
2012-06-28 12:38:25 +00:00
|
|
|
var units_by_id = {};
|
2012-08-21 10:04:52 +00:00
|
|
|
for(var i = 0, len = units.length; i < len; i++){
|
|
|
|
units_by_id[units[i].id] = units[i];
|
2012-06-28 12:38:25 +00:00
|
|
|
}
|
2013-12-16 14:35:59 +00:00
|
|
|
self.units_by_id = units_by_id;
|
2013-01-25 15:34:05 +00:00
|
|
|
|
2012-09-03 14:43:54 +00:00
|
|
|
return self.fetch('res.users', ['name','ean13'], [['ean13', '!=', false]]);
|
2012-10-31 10:31:48 +00:00
|
|
|
}).then(function(users){
|
2013-12-16 14:35:59 +00:00
|
|
|
self.users = users;
|
2012-08-21 10:04:52 +00:00
|
|
|
|
2012-11-21 11:26:44 +00:00
|
|
|
return self.fetch('res.partner', ['name','ean13'], [['ean13', '!=', false]]);
|
|
|
|
}).then(function(partners){
|
2013-12-16 14:35:59 +00:00
|
|
|
self.partners = partners;
|
2012-11-21 11:26:44 +00:00
|
|
|
|
2014-01-06 13:51:19 +00:00
|
|
|
return self.fetch('account.tax', ['name','amount', 'price_include', 'type']);
|
2012-10-31 10:31:48 +00:00
|
|
|
}).then(function(taxes){
|
2013-12-16 14:35:59 +00:00
|
|
|
self.taxes = taxes;
|
2012-08-21 10:04:52 +00:00
|
|
|
|
2012-09-03 14:43:54 +00:00
|
|
|
return self.fetch(
|
2012-08-21 10:04:52 +00:00
|
|
|
'pos.session',
|
|
|
|
['id', 'journal_ids','name','user_id','config_id','start_at','stop_at'],
|
|
|
|
[['state', '=', 'opened'], ['user_id', '=', self.session.uid]]
|
|
|
|
);
|
2013-12-16 14:35:59 +00:00
|
|
|
}).then(function(pos_sessions){
|
|
|
|
self.pos_session = pos_sessions[0];
|
2012-08-21 10:04:52 +00:00
|
|
|
|
2012-09-03 14:43:54 +00:00
|
|
|
return self.fetch(
|
2012-08-21 10:04:52 +00:00
|
|
|
'pos.config',
|
2013-02-06 12:59:13 +00:00
|
|
|
['name','journal_ids','warehouse_id','journal_id','pricelist_id',
|
2012-08-21 10:04:52 +00:00
|
|
|
'iface_self_checkout', 'iface_led', 'iface_cashdrawer',
|
2014-01-17 17:58:30 +00:00
|
|
|
'iface_payment_terminal', 'iface_electronic_scale', 'iface_barscan',
|
|
|
|
'iface_vkeyboard','iface_print_via_proxy','iface_scan_via_proxy',
|
|
|
|
'iface_cashdrawer','iface_invoicing','iface_big_scrollbars',
|
2014-01-10 17:17:46 +00:00
|
|
|
'receipt_header','receipt_footer','proxy_ip',
|
2013-11-06 16:17:23 +00:00
|
|
|
'state','sequence_id','session_ids'],
|
2013-12-16 14:35:59 +00:00
|
|
|
[['id','=', self.pos_session.config_id[0]]]
|
2012-08-21 10:04:52 +00:00
|
|
|
);
|
2012-10-31 10:31:48 +00:00
|
|
|
}).then(function(configs){
|
2013-12-16 14:35:59 +00:00
|
|
|
self.config = configs[0];
|
2014-01-17 17:58:30 +00:00
|
|
|
self.config.use_proxy = self.config.iface_payment_terminal ||
|
|
|
|
self.config.iface_electronic_scale ||
|
|
|
|
self.config.iface_print_via_proxy ||
|
|
|
|
self.config.iface_scan_via_proxy ||
|
|
|
|
self.config.iface_cashdrawer;
|
2013-12-16 14:35:59 +00:00
|
|
|
|
|
|
|
return self.fetch('stock.warehouse',[],[['id','=', self.config.warehouse_id[0]]]);
|
2012-10-31 10:31:48 +00:00
|
|
|
}).then(function(shops){
|
2013-12-16 14:35:59 +00:00
|
|
|
self.shop = shops[0];
|
2012-08-21 10:04:52 +00:00
|
|
|
|
2013-12-16 14:35:59 +00:00
|
|
|
return self.fetch('product.pricelist',['currency_id'],[['id','=',self.config.pricelist_id[0]]]);
|
2013-09-20 12:30:18 +00:00
|
|
|
}).then(function(pricelists){
|
2013-12-16 14:35:59 +00:00
|
|
|
self.pricelist = pricelists[0];
|
2013-09-20 12:30:18 +00:00
|
|
|
|
2013-12-16 14:35:59 +00:00
|
|
|
return self.fetch('res.currency',['symbol','position','rounding','accuracy'],[['id','=',self.pricelist.currency_id[0]]]);
|
2013-09-20 12:30:18 +00:00
|
|
|
}).then(function(currencies){
|
2013-12-16 14:35:59 +00:00
|
|
|
self.currency = currencies[0];
|
2013-09-20 12:30:18 +00:00
|
|
|
|
2013-12-24 15:11:12 +00:00
|
|
|
/*
|
|
|
|
return (new instance.web.Model('decimal.precision')).call('get_precision',[['Account']]);
|
|
|
|
}).then(function(precision){
|
|
|
|
self.accounting_precision = precision;
|
|
|
|
console.log("PRECISION",precision);
|
|
|
|
*/
|
2012-09-13 14:03:10 +00:00
|
|
|
return self.fetch('product.packaging',['ean','product_id']);
|
2012-10-31 10:31:48 +00:00
|
|
|
}).then(function(packagings){
|
2012-09-13 14:03:10 +00:00
|
|
|
self.db.add_packagings(packagings);
|
|
|
|
|
2014-01-27 16:10:05 +00:00
|
|
|
return self.fetch('product.public.category', ['id','name','parent_id','child_id','image'])
|
2012-10-31 10:31:48 +00:00
|
|
|
}).then(function(categories){
|
2012-08-21 10:04:52 +00:00
|
|
|
self.db.add_categories(categories);
|
2013-02-06 12:59:13 +00:00
|
|
|
|
2012-09-03 14:43:54 +00:00
|
|
|
return self.fetch(
|
2012-08-21 10:04:52 +00:00
|
|
|
'product.product',
|
2014-01-27 16:10:05 +00:00
|
|
|
['name', 'list_price','price','public_categ_id', 'taxes_id', 'ean13', 'default_code',
|
2013-02-06 12:59:13 +00:00
|
|
|
'to_weight', 'uom_id', 'uos_id', 'uos_coeff', 'mes_type', 'description_sale', 'description'],
|
2012-11-22 17:52:54 +00:00
|
|
|
[['sale_ok','=',true],['available_in_pos','=',true]],
|
2013-12-16 14:35:59 +00:00
|
|
|
{pricelist: self.pricelist.id} // context for price
|
2012-08-21 10:04:52 +00:00
|
|
|
);
|
2012-10-31 10:31:48 +00:00
|
|
|
}).then(function(products){
|
2012-08-21 10:04:52 +00:00
|
|
|
self.db.add_products(products);
|
|
|
|
|
2012-09-03 14:43:54 +00:00
|
|
|
return self.fetch(
|
2012-08-21 10:04:52 +00:00
|
|
|
'account.bank.statement',
|
|
|
|
['account_id','currency','journal_id','state','name','user_id','pos_session_id'],
|
2013-12-16 14:35:59 +00:00
|
|
|
[['state','=','open'],['pos_session_id', '=', self.pos_session.id]]
|
2012-08-21 10:04:52 +00:00
|
|
|
);
|
2013-12-16 14:35:59 +00:00
|
|
|
}).then(function(bankstatements){
|
|
|
|
var journals = [];
|
|
|
|
_.each(bankstatements,function(statement) {
|
2012-10-12 07:01:45 +00:00
|
|
|
journals.push(statement.journal_id[0])
|
|
|
|
});
|
2013-12-16 14:35:59 +00:00
|
|
|
self.bankstatements = bankstatements;
|
2012-10-12 07:01:45 +00:00
|
|
|
return self.fetch('account.journal', undefined, [['id','in', journals]]);
|
2012-10-31 10:31:48 +00:00
|
|
|
}).then(function(journals){
|
2013-12-16 14:35:59 +00:00
|
|
|
self.journals = journals;
|
2012-08-21 10:04:52 +00:00
|
|
|
|
|
|
|
// associate the bank statements with their journals.
|
2013-12-16 14:35:59 +00:00
|
|
|
var bankstatements = self.bankstatements
|
|
|
|
for(var i = 0, ilen = bankstatements.length; i < ilen; i++){
|
2012-08-21 10:04:52 +00:00
|
|
|
for(var j = 0, jlen = journals.length; j < jlen; j++){
|
2013-12-16 14:35:59 +00:00
|
|
|
if(bankstatements[i].journal_id[0] === journals[j].id){
|
|
|
|
bankstatements[i].journal = journals[j];
|
|
|
|
bankstatements[i].self_checkout_payment_method = journals[j].self_checkout_payment_method;
|
2012-08-21 10:04:52 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-08 16:42:26 +00:00
|
|
|
}
|
2013-12-16 14:35:59 +00:00
|
|
|
self.cashregisters = bankstatements;
|
2013-12-24 15:11:12 +00:00
|
|
|
|
|
|
|
// Load the company Logo
|
|
|
|
|
|
|
|
self.company_logo = new Image();
|
|
|
|
self.company_logo.crossOrigin = 'anonymous';
|
|
|
|
var logo_loaded = new $.Deferred();
|
|
|
|
self.company_logo.onload = function(){
|
2014-01-30 18:21:09 +00:00
|
|
|
var img = self.company_logo;
|
|
|
|
var ratio = 1;
|
2014-01-31 10:22:45 +00:00
|
|
|
var targetwidth = 300;
|
|
|
|
var maxheight = 150;
|
2014-01-30 18:21:09 +00:00
|
|
|
if( img.width !== targetwidth ){
|
|
|
|
ratio = targetwidth / img.width;
|
|
|
|
}
|
|
|
|
if( img.height * ratio > maxheight ){
|
|
|
|
ratio = maxheight / img.height;
|
|
|
|
}
|
|
|
|
var width = Math.floor(img.width * ratio);
|
|
|
|
var height = Math.floor(img.height * ratio);
|
2013-12-24 15:11:12 +00:00
|
|
|
var c = document.createElement('canvas');
|
2014-01-30 18:21:09 +00:00
|
|
|
c.width = width;
|
|
|
|
c.height = height
|
2013-12-24 15:11:12 +00:00
|
|
|
var ctx = c.getContext('2d');
|
2014-01-30 18:21:09 +00:00
|
|
|
ctx.drawImage(self.company_logo,0,0, width, height);
|
|
|
|
|
2013-12-24 15:11:12 +00:00
|
|
|
self.company_logo_base64 = c.toDataURL();
|
2014-01-30 18:21:09 +00:00
|
|
|
window.logo64 = self.company_logo_base64;
|
2013-12-24 15:11:12 +00:00
|
|
|
logo_loaded.resolve();
|
|
|
|
};
|
|
|
|
self.company_logo.onerror = function(){
|
|
|
|
logo_loaded.reject();
|
|
|
|
};
|
|
|
|
self.company_logo.src = window.location.origin + '/web/binary/company_logo';
|
|
|
|
|
|
|
|
return logo_loaded;
|
2012-04-24 16:25:46 +00:00
|
|
|
});
|
2012-09-03 14:25:27 +00:00
|
|
|
|
|
|
|
return loaded;
|
2012-05-21 14:09:01 +00:00
|
|
|
},
|
2012-04-24 17:20:47 +00:00
|
|
|
|
2012-05-21 14:09:01 +00:00
|
|
|
// this is called when an order is removed from the order collection. It ensures that there is always an existing
|
|
|
|
// order and a valid selected order
|
2013-09-05 10:43:57 +00:00
|
|
|
on_removed_order: function(removed_order,index,reason){
|
|
|
|
if(reason === 'abandon' && this.get('orders').size() > 0){
|
|
|
|
// when we intentionally remove an unfinished order, and there is another existing one
|
|
|
|
this.set({'selectedOrder' : this.get('orders').at(index) || this.get('orders').last()});
|
2013-07-02 20:40:48 +00:00
|
|
|
}else{
|
2013-09-05 10:43:57 +00:00
|
|
|
// when the order was automatically removed after completion,
|
|
|
|
// or when we intentionally delete the only concurrent order
|
|
|
|
this.add_new_order();
|
2012-05-21 14:09:01 +00:00
|
|
|
}
|
|
|
|
},
|
2012-04-24 16:25:46 +00:00
|
|
|
|
2012-08-09 17:21:13 +00:00
|
|
|
//creates a new empty order and sets it as the current order
|
|
|
|
add_new_order: function(){
|
|
|
|
var order = new module.Order({pos:this});
|
|
|
|
this.get('orders').add(order);
|
|
|
|
this.set('selectedOrder', order);
|
2012-04-24 17:20:47 +00:00
|
|
|
},
|
2012-08-09 17:21:13 +00:00
|
|
|
|
2013-09-05 10:43:57 +00:00
|
|
|
//removes the current order
|
|
|
|
delete_current_order: function(){
|
|
|
|
this.get('selectedOrder').destroy({'reason':'abandon'});
|
|
|
|
},
|
|
|
|
|
2013-04-11 12:07:19 +00:00
|
|
|
// saves the order locally and try to send it to the backend.
|
|
|
|
// it returns a deferred that succeeds after having tried to send the order and all the other pending orders.
|
|
|
|
push_order: function(order) {
|
2013-03-28 15:07:18 +00:00
|
|
|
var self = this;
|
2013-09-12 13:50:44 +00:00
|
|
|
this.proxy.log('push_order',order.export_as_JSON());
|
2013-04-11 12:07:19 +00:00
|
|
|
var order_id = this.db.add_order(order.export_as_JSON());
|
2013-03-28 15:07:18 +00:00
|
|
|
var pushed = new $.Deferred();
|
|
|
|
|
2014-01-16 17:28:46 +00:00
|
|
|
this.set('synch',{state:'connecting', pending:self.db.get_orders().length});
|
2013-03-28 15:07:18 +00:00
|
|
|
|
|
|
|
this.flush_mutex.exec(function(){
|
2013-04-11 12:07:19 +00:00
|
|
|
var flushed = self._flush_all_orders();
|
2013-03-28 15:07:18 +00:00
|
|
|
|
2013-04-11 12:07:19 +00:00
|
|
|
flushed.always(function(){
|
|
|
|
pushed.resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
return flushed;
|
|
|
|
});
|
|
|
|
return pushed;
|
|
|
|
},
|
|
|
|
|
|
|
|
// saves the order locally and try to send it to the backend and make an invoice
|
|
|
|
// returns a deferred that succeeds when the order has been posted and successfully generated
|
|
|
|
// an invoice. This method can fail in various ways:
|
|
|
|
// error-no-client: the order must have an associated partner_id. You can retry to make an invoice once
|
|
|
|
// this error is solved
|
|
|
|
// error-transfer: there was a connection error during the transfer. You can retry to make the invoice once
|
|
|
|
// the network connection is up
|
|
|
|
|
|
|
|
push_and_invoice_order: function(order){
|
|
|
|
var self = this;
|
|
|
|
var invoiced = new $.Deferred();
|
|
|
|
|
|
|
|
if(!order.get_client()){
|
|
|
|
invoiced.reject('error-no-client');
|
|
|
|
return invoiced;
|
|
|
|
}
|
2013-03-28 15:07:18 +00:00
|
|
|
|
2013-04-11 12:07:19 +00:00
|
|
|
var order_id = this.db.add_order(order.export_as_JSON());
|
|
|
|
|
2014-01-16 17:28:46 +00:00
|
|
|
this.set('synch',{state:'connecting', pending:self.db.get_orders().length});
|
2013-04-11 12:07:19 +00:00
|
|
|
|
|
|
|
this.flush_mutex.exec(function(){
|
|
|
|
var done = new $.Deferred(); // holds the mutex
|
|
|
|
|
|
|
|
// send the order to the server
|
|
|
|
// we have a 30 seconds timeout on this push.
|
|
|
|
// FIXME: if the server takes more than 30 seconds to accept the order,
|
|
|
|
// the client will believe it wasn't successfully sent, and very bad
|
|
|
|
// things will happen as a duplicate will be sent next time
|
|
|
|
// so we must make sure the server detects and ignores duplicated orders
|
|
|
|
|
|
|
|
var transfer = self._flush_order(order_id, {timeout:30000, to_invoice:true});
|
|
|
|
|
|
|
|
transfer.fail(function(){
|
|
|
|
invoiced.reject('error-transfer');
|
|
|
|
done.reject();
|
|
|
|
});
|
|
|
|
|
|
|
|
// on success, get the order id generated by the server
|
|
|
|
transfer.pipe(function(order_server_id){
|
|
|
|
// generate the pdf and download it
|
|
|
|
self.pos_widget.do_action('point_of_sale.pos_invoice_report',{additional_context:{
|
|
|
|
active_ids:order_server_id,
|
|
|
|
}});
|
|
|
|
invoiced.resolve();
|
|
|
|
done.resolve();
|
2013-03-28 15:07:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return done;
|
2013-04-11 12:07:19 +00:00
|
|
|
|
2013-03-28 15:07:18 +00:00
|
|
|
});
|
|
|
|
|
2013-04-11 12:07:19 +00:00
|
|
|
return invoiced;
|
2013-03-28 15:07:18 +00:00
|
|
|
},
|
|
|
|
|
2012-08-14 15:21:12 +00:00
|
|
|
// attemps to send all pending orders ( stored in the pos_db ) to the server,
|
|
|
|
// and remove the successfully sent ones from the db once
|
|
|
|
// it has been confirmed that they have been sent correctly.
|
2012-04-24 16:25:46 +00:00
|
|
|
flush: function() {
|
|
|
|
var self = this;
|
2013-03-28 15:07:18 +00:00
|
|
|
var flushed = new $.Deferred();
|
|
|
|
|
|
|
|
this.flush_mutex.exec(function(){
|
|
|
|
var done = new $.Deferred();
|
|
|
|
|
|
|
|
self._flush_all_orders()
|
|
|
|
.done( function(){ flushed.resolve();})
|
|
|
|
.fail( function(){ flushed.reject(); })
|
|
|
|
.always(function(){ done.resolve(); });
|
|
|
|
|
|
|
|
return done;
|
|
|
|
});
|
|
|
|
|
|
|
|
return flushed;
|
2012-04-24 16:25:46 +00:00
|
|
|
},
|
2013-03-28 15:07:18 +00:00
|
|
|
|
|
|
|
// attempts to send the locally stored order of id 'order_id'
|
2013-04-11 12:07:19 +00:00
|
|
|
// the sending is asynchronous and can take some time to decide if it is successful or not
|
2013-03-28 15:07:18 +00:00
|
|
|
// it is therefore important to only call this method from inside a mutex
|
|
|
|
// this method returns a deferred indicating wether the sending was successful or not
|
2013-04-11 12:07:19 +00:00
|
|
|
// there is a timeout parameter which is set to 2 seconds by default.
|
2014-02-14 14:49:30 +00:00
|
|
|
_flush_order: function( order_id, options) {
|
|
|
|
return this._flush_all_orders([this.db.get_order(order_id)], options);
|
2013-03-28 15:07:18 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// attempts to send all the locally stored orders. As with _flush_order, it should only be
|
|
|
|
// called from within a mutex.
|
|
|
|
// this method returns a deferred that always succeeds when all orders have been tried to be sent,
|
|
|
|
// even if none of them could actually be sent.
|
2014-02-14 14:49:30 +00:00
|
|
|
_flush_all_orders: function () {
|
2012-04-24 16:25:46 +00:00
|
|
|
var self = this;
|
2014-02-14 14:49:30 +00:00
|
|
|
self.set('synch', {
|
|
|
|
state: 'connecting',
|
|
|
|
pending: self.get('synch').pending
|
|
|
|
});
|
|
|
|
return self._save_to_server(self.db.get_orders()).done(function () {
|
|
|
|
var pending = self.db.get_orders().length;
|
|
|
|
self.set('synch', {
|
|
|
|
state: pending ? 'connecting' : 'connected',
|
|
|
|
pending: pending
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// send an array of orders to the server
|
|
|
|
// available options:
|
|
|
|
// - timeout: timeout for the rpc call in ms
|
|
|
|
_save_to_server: function (orders, options) {
|
|
|
|
if (!orders || !orders.length) {
|
|
|
|
var result = $.Deferred();
|
|
|
|
result.resolve();
|
|
|
|
return result;
|
2012-08-14 15:21:12 +00:00
|
|
|
}
|
2014-02-14 14:49:30 +00:00
|
|
|
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
var self = this;
|
2014-02-24 14:41:43 +00:00
|
|
|
var timeout = typeof options.timeout === 'number' ? options.timeout : 7500 * orders.length;
|
2013-03-28 15:07:18 +00:00
|
|
|
|
2014-02-14 14:49:30 +00:00
|
|
|
// we try to send the order. shadow prevents a spinner if it takes too long. (unless we are sending an invoice,
|
|
|
|
// then we want to notify the user that we are waiting on something )
|
|
|
|
var posOrderModel = new instance.web.Model('pos.order');
|
|
|
|
return posOrderModel.call('create_from_ui',
|
|
|
|
[_.map(orders, function (order) {
|
|
|
|
order.to_invoice = options.to_invoice || false;
|
|
|
|
return order;
|
|
|
|
})],
|
|
|
|
undefined,
|
|
|
|
{
|
|
|
|
shadow: !options.to_invoice,
|
|
|
|
timeout: timeout
|
|
|
|
}
|
|
|
|
).then(function () {
|
|
|
|
_.each(orders, function (order) {
|
|
|
|
self.db.remove_order(order.id);
|
|
|
|
});
|
|
|
|
}).fail(function (unused, event){
|
|
|
|
// prevent an error popup creation by the rpc failure
|
|
|
|
// we want the failure to be silent as we send the orders in the background
|
|
|
|
event.preventDefault();
|
|
|
|
console.error('Failed to send orders:', orders);
|
|
|
|
});
|
2012-04-24 16:25:46 +00:00
|
|
|
},
|
2012-05-29 16:39:44 +00:00
|
|
|
|
2013-09-23 14:51:39 +00:00
|
|
|
scan_product: function(parsed_code){
|
2012-08-09 17:21:13 +00:00
|
|
|
var self = this;
|
2012-08-13 16:00:26 +00:00
|
|
|
var selectedOrder = this.get('selectedOrder');
|
2013-09-23 14:51:39 +00:00
|
|
|
if(parsed_code.encoding === 'ean13'){
|
|
|
|
var product = this.db.get_product_by_ean13(parsed_code.base_code);
|
|
|
|
}else if(parsed_code.encoding === 'reference'){
|
|
|
|
var product = this.db.get_product_by_reference(parsed_code.code);
|
|
|
|
}
|
2012-08-13 16:00:26 +00:00
|
|
|
|
|
|
|
if(!product){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-23 14:51:39 +00:00
|
|
|
if(parsed_code.type === 'price'){
|
2013-12-02 16:21:44 +00:00
|
|
|
selectedOrder.addProduct(product, {price:parsed_code.value});
|
2013-09-23 14:51:39 +00:00
|
|
|
}else if(parsed_code.type === 'weight'){
|
2013-12-02 16:21:44 +00:00
|
|
|
selectedOrder.addProduct(product, {quantity:parsed_code.value, merge:false});
|
2012-08-13 16:00:26 +00:00
|
|
|
}else{
|
2013-12-02 16:21:44 +00:00
|
|
|
selectedOrder.addProduct(product);
|
2012-08-13 16:00:26 +00:00
|
|
|
}
|
|
|
|
return true;
|
2012-05-29 13:31:41 +00:00
|
|
|
},
|
2012-04-24 16:25:46 +00:00
|
|
|
});
|
|
|
|
|
2012-05-21 14:09:01 +00:00
|
|
|
// An orderline represent one element of the content of a client's shopping cart.
|
|
|
|
// An orderline contains a product, its quantity, its price, discount. etc.
|
|
|
|
// An Order contains zero or more Orderlines.
|
2012-04-24 16:25:46 +00:00
|
|
|
module.Orderline = Backbone.Model.extend({
|
2012-07-03 16:50:39 +00:00
|
|
|
initialize: function(attr,options){
|
2012-07-03 11:47:41 +00:00
|
|
|
this.pos = options.pos;
|
|
|
|
this.order = options.order;
|
|
|
|
this.product = options.product;
|
2013-12-02 16:21:44 +00:00
|
|
|
this.price = options.product.price;
|
2012-07-03 11:47:41 +00:00
|
|
|
this.quantity = 1;
|
2013-01-22 13:18:01 +00:00
|
|
|
this.quantityStr = '1';
|
2012-07-03 11:47:41 +00:00
|
|
|
this.discount = 0;
|
2013-01-22 13:18:01 +00:00
|
|
|
this.discountStr = '0';
|
2012-07-03 11:47:41 +00:00
|
|
|
this.type = 'unit';
|
|
|
|
this.selected = false;
|
|
|
|
},
|
|
|
|
// sets a discount [0,100]%
|
|
|
|
set_discount: function(discount){
|
2013-01-24 14:06:31 +00:00
|
|
|
var disc = Math.min(Math.max(parseFloat(discount) || 0, 0),100);
|
|
|
|
this.discount = disc;
|
|
|
|
this.discountStr = '' + disc;
|
2013-12-02 16:21:44 +00:00
|
|
|
this.trigger('change',this);
|
2012-07-03 11:47:41 +00:00
|
|
|
},
|
|
|
|
// returns the discount [0,100]%
|
|
|
|
get_discount: function(){
|
|
|
|
return this.discount;
|
|
|
|
},
|
2013-01-22 13:18:01 +00:00
|
|
|
get_discount_str: function(){
|
|
|
|
return this.discountStr;
|
|
|
|
},
|
2012-07-03 11:47:41 +00:00
|
|
|
get_product_type: function(){
|
|
|
|
return this.type;
|
|
|
|
},
|
|
|
|
// sets the quantity of the product. The quantity will be rounded according to the
|
|
|
|
// product's unity of measure properties. Quantities greater than zero will not get
|
|
|
|
// rounded to zero
|
|
|
|
set_quantity: function(quantity){
|
2013-01-24 14:06:31 +00:00
|
|
|
if(quantity === 'remove'){
|
2012-07-03 11:47:41 +00:00
|
|
|
this.order.removeOrderline(this);
|
2013-01-22 13:18:01 +00:00
|
|
|
return;
|
|
|
|
}else{
|
2013-01-24 14:06:31 +00:00
|
|
|
var quant = Math.max(parseFloat(quantity) || 0, 0);
|
2012-06-28 12:38:25 +00:00
|
|
|
var unit = this.get_unit();
|
2013-01-22 13:18:01 +00:00
|
|
|
if(unit){
|
2013-04-09 14:35:36 +00:00
|
|
|
this.quantity = Math.max(unit.rounding, round_pr(quant, unit.rounding));
|
2013-01-28 17:18:00 +00:00
|
|
|
this.quantityStr = this.quantity.toFixed(Math.max(0,Math.ceil(Math.log(1.0 / unit.rounding) / Math.log(10))));
|
2013-01-22 13:18:01 +00:00
|
|
|
}else{
|
|
|
|
this.quantity = quant;
|
|
|
|
this.quantityStr = '' + this.quantity;
|
2012-06-28 12:38:25 +00:00
|
|
|
}
|
2012-06-04 15:04:59 +00:00
|
|
|
}
|
2013-12-02 16:21:44 +00:00
|
|
|
this.trigger('change',this);
|
2012-07-03 11:47:41 +00:00
|
|
|
},
|
|
|
|
// return the quantity of product
|
|
|
|
get_quantity: function(){
|
|
|
|
return this.quantity;
|
2012-06-04 15:04:59 +00:00
|
|
|
},
|
2013-01-22 13:18:01 +00:00
|
|
|
get_quantity_str: function(){
|
|
|
|
return this.quantityStr;
|
|
|
|
},
|
|
|
|
get_quantity_str_with_unit: function(){
|
|
|
|
var unit = this.get_unit();
|
|
|
|
if(unit && unit.name !== 'Unit(s)'){
|
|
|
|
return this.quantityStr + ' ' + unit.name;
|
|
|
|
}else{
|
|
|
|
return this.quantityStr;
|
|
|
|
}
|
|
|
|
},
|
2012-07-03 11:47:41 +00:00
|
|
|
// return the unit of measure of the product
|
2012-06-28 12:38:25 +00:00
|
|
|
get_unit: function(){
|
2013-12-02 16:21:44 +00:00
|
|
|
var unit_id = (this.product.uos_id || this.product.uom_id);
|
2012-06-28 12:38:25 +00:00
|
|
|
if(!unit_id){
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
unit_id = unit_id[0];
|
|
|
|
if(!this.pos){
|
|
|
|
return undefined;
|
|
|
|
}
|
2013-12-16 14:35:59 +00:00
|
|
|
return this.pos.units_by_id[unit_id];
|
2012-06-28 12:38:25 +00:00
|
|
|
},
|
2012-07-03 11:47:41 +00:00
|
|
|
// return the product of this orderline
|
|
|
|
get_product: function(){
|
|
|
|
return this.product;
|
|
|
|
},
|
|
|
|
// selects or deselects this orderline
|
|
|
|
set_selected: function(selected){
|
|
|
|
this.selected = selected;
|
2013-12-02 16:21:44 +00:00
|
|
|
this.trigger('change',this);
|
2012-07-03 11:47:41 +00:00
|
|
|
},
|
|
|
|
// returns true if this orderline is selected
|
|
|
|
is_selected: function(){
|
|
|
|
return this.selected;
|
|
|
|
},
|
2012-05-22 14:59:56 +00:00
|
|
|
// when we add an new orderline we want to merge it with the last line to see reduce the number of items
|
|
|
|
// in the orderline. This returns true if it makes sense to merge the two
|
|
|
|
can_be_merged_with: function(orderline){
|
2013-12-02 16:21:44 +00:00
|
|
|
if( this.get_product().id !== orderline.get_product().id){ //only orderline of the same product can be merged
|
2012-05-22 14:59:56 +00:00
|
|
|
return false;
|
2012-07-03 11:47:41 +00:00
|
|
|
}else if(this.get_product_type() !== orderline.get_product_type()){
|
2012-05-22 14:59:56 +00:00
|
|
|
return false;
|
2012-07-03 11:47:41 +00:00
|
|
|
}else if(this.get_discount() > 0){ // we don't merge discounted orderlines
|
2012-05-22 14:59:56 +00:00
|
|
|
return false;
|
2012-07-30 15:41:43 +00:00
|
|
|
}else if(this.price !== orderline.price){
|
2012-05-22 14:59:56 +00:00
|
|
|
return false;
|
2012-07-30 15:41:43 +00:00
|
|
|
}else{
|
|
|
|
return true;
|
2012-05-22 14:59:56 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
merge: function(orderline){
|
2012-07-03 11:47:41 +00:00
|
|
|
this.set_quantity(this.get_quantity() + orderline.get_quantity());
|
2012-04-24 16:25:46 +00:00
|
|
|
},
|
2012-07-03 11:47:41 +00:00
|
|
|
export_as_JSON: function() {
|
|
|
|
return {
|
|
|
|
qty: this.get_quantity(),
|
2013-01-22 13:18:01 +00:00
|
|
|
price_unit: this.get_unit_price(),
|
2012-07-03 11:47:41 +00:00
|
|
|
discount: this.get_discount(),
|
2013-12-02 16:21:44 +00:00
|
|
|
product_id: this.get_product().id,
|
2012-07-03 14:03:15 +00:00
|
|
|
};
|
|
|
|
},
|
2012-07-10 10:15:15 +00:00
|
|
|
//used to create a json of the ticket, to be sent to the printer
|
2012-07-03 14:03:15 +00:00
|
|
|
export_for_printing: function(){
|
|
|
|
return {
|
|
|
|
quantity: this.get_quantity(),
|
|
|
|
unit_name: this.get_unit().name,
|
2013-01-22 13:18:01 +00:00
|
|
|
price: this.get_unit_price(),
|
2012-07-03 14:03:15 +00:00
|
|
|
discount: this.get_discount(),
|
2013-12-02 16:21:44 +00:00
|
|
|
product_name: this.get_product().name,
|
2013-01-22 13:18:01 +00:00
|
|
|
price_display : this.get_display_price(),
|
2012-07-03 14:03:15 +00:00
|
|
|
price_with_tax : this.get_price_with_tax(),
|
|
|
|
price_without_tax: this.get_price_without_tax(),
|
|
|
|
tax: this.get_tax(),
|
2013-12-02 16:21:44 +00:00
|
|
|
product_description: this.get_product().description,
|
|
|
|
product_description_sale: this.get_product().description_sale,
|
2012-07-03 11:47:41 +00:00
|
|
|
};
|
2012-05-11 16:02:23 +00:00
|
|
|
},
|
2013-01-22 13:18:01 +00:00
|
|
|
// changes the base price of the product for this orderline
|
|
|
|
set_unit_price: function(price){
|
2013-04-09 14:35:36 +00:00
|
|
|
this.price = round_di(parseFloat(price) || 0, 2);
|
2013-12-02 16:21:44 +00:00
|
|
|
this.trigger('change',this);
|
2013-01-22 13:18:01 +00:00
|
|
|
},
|
|
|
|
get_unit_price: function(){
|
2013-12-16 14:35:59 +00:00
|
|
|
var rounding = this.pos.currency.rounding;
|
2013-01-28 17:18:00 +00:00
|
|
|
return round_pr(this.price,rounding);
|
2013-01-22 13:18:01 +00:00
|
|
|
},
|
|
|
|
get_display_price: function(){
|
2013-12-16 14:35:59 +00:00
|
|
|
var rounding = this.pos.currency.rounding;
|
2013-01-28 17:18:00 +00:00
|
|
|
return round_pr(round_pr(this.get_unit_price() * this.get_quantity(),rounding) * (1- this.get_discount()/100.0),rounding);
|
2013-01-22 13:18:01 +00:00
|
|
|
},
|
2012-07-03 11:47:41 +00:00
|
|
|
get_price_without_tax: function(){
|
|
|
|
return this.get_all_prices().priceWithoutTax;
|
2012-04-24 16:25:46 +00:00
|
|
|
},
|
2012-07-03 11:47:41 +00:00
|
|
|
get_price_with_tax: function(){
|
|
|
|
return this.get_all_prices().priceWithTax;
|
2012-04-24 16:25:46 +00:00
|
|
|
},
|
2012-07-03 11:47:41 +00:00
|
|
|
get_tax: function(){
|
|
|
|
return this.get_all_prices().tax;
|
2012-04-24 16:25:46 +00:00
|
|
|
},
|
2014-01-06 13:51:19 +00:00
|
|
|
get_tax_details: function(){
|
|
|
|
return this.get_all_prices().taxDetails;
|
|
|
|
},
|
2013-01-28 17:18:00 +00:00
|
|
|
get_all_prices: function(){
|
2012-04-24 16:25:46 +00:00
|
|
|
var self = this;
|
2013-12-16 14:35:59 +00:00
|
|
|
var currency_rounding = this.pos.currency.rounding;
|
2013-01-28 17:18:00 +00:00
|
|
|
var base = round_pr(this.get_quantity() * this.get_unit_price() * (1.0 - (this.get_discount() / 100.0)), currency_rounding);
|
2012-04-24 16:25:46 +00:00
|
|
|
var totalTax = base;
|
|
|
|
var totalNoTax = base;
|
|
|
|
|
2012-07-03 11:47:41 +00:00
|
|
|
var product = this.get_product();
|
2013-12-02 16:21:44 +00:00
|
|
|
var taxes_ids = product.taxes_id;
|
|
|
|
var taxes = self.pos.taxes;
|
2012-04-24 16:25:46 +00:00
|
|
|
var taxtotal = 0;
|
2014-01-06 13:51:19 +00:00
|
|
|
var taxdetail = {};
|
2012-04-24 16:25:46 +00:00
|
|
|
_.each(taxes_ids, function(el) {
|
|
|
|
var tax = _.detect(taxes, function(t) {return t.id === el;});
|
|
|
|
if (tax.price_include) {
|
|
|
|
var tmp;
|
|
|
|
if (tax.type === "percent") {
|
2013-01-28 17:18:00 +00:00
|
|
|
tmp = base - round_pr(base / (1 + tax.amount),currency_rounding);
|
2012-04-24 16:25:46 +00:00
|
|
|
} else if (tax.type === "fixed") {
|
2013-01-28 17:18:00 +00:00
|
|
|
tmp = round_pr(tax.amount * self.get_quantity(),currency_rounding);
|
2012-04-24 16:25:46 +00:00
|
|
|
} else {
|
|
|
|
throw "This type of tax is not supported by the point of sale: " + tax.type;
|
|
|
|
}
|
2013-01-28 17:18:00 +00:00
|
|
|
tmp = round_pr(tmp,currency_rounding);
|
2012-04-24 16:25:46 +00:00
|
|
|
taxtotal += tmp;
|
|
|
|
totalNoTax -= tmp;
|
2014-01-06 13:51:19 +00:00
|
|
|
taxdetail[tax.id] = tmp;
|
2012-04-24 16:25:46 +00:00
|
|
|
} else {
|
|
|
|
var tmp;
|
|
|
|
if (tax.type === "percent") {
|
|
|
|
tmp = tax.amount * base;
|
|
|
|
} else if (tax.type === "fixed") {
|
2012-07-03 11:47:41 +00:00
|
|
|
tmp = tax.amount * self.get_quantity();
|
2012-04-24 16:25:46 +00:00
|
|
|
} else {
|
|
|
|
throw "This type of tax is not supported by the point of sale: " + tax.type;
|
|
|
|
}
|
2013-01-28 17:18:00 +00:00
|
|
|
tmp = round_pr(tmp,currency_rounding);
|
2012-04-24 16:25:46 +00:00
|
|
|
taxtotal += tmp;
|
|
|
|
totalTax += tmp;
|
2014-01-06 13:51:19 +00:00
|
|
|
taxdetail[tax.id] = tmp;
|
2012-04-24 16:25:46 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
"priceWithTax": totalTax,
|
|
|
|
"priceWithoutTax": totalNoTax,
|
|
|
|
"tax": taxtotal,
|
2014-01-06 13:51:19 +00:00
|
|
|
"taxDetails": taxdetail,
|
2012-04-24 16:25:46 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.OrderlineCollection = Backbone.Collection.extend({
|
|
|
|
model: module.Orderline,
|
|
|
|
});
|
|
|
|
|
2013-12-04 17:21:22 +00:00
|
|
|
// Every Paymentline contains a cashregister and an amount of money.
|
2012-04-24 16:25:46 +00:00
|
|
|
module.Paymentline = Backbone.Model.extend({
|
2012-07-03 16:50:39 +00:00
|
|
|
initialize: function(attributes, options) {
|
2012-07-03 11:47:41 +00:00
|
|
|
this.amount = 0;
|
2013-12-16 14:35:59 +00:00
|
|
|
this.cashregister = options.cashregister;
|
|
|
|
this.name = this.cashregister.journal_id[1];
|
2013-12-04 17:21:22 +00:00
|
|
|
this.selected = false;
|
2012-04-24 16:25:46 +00:00
|
|
|
},
|
2012-07-03 11:47:41 +00:00
|
|
|
//sets the amount of money on this payment line
|
|
|
|
set_amount: function(value){
|
2013-12-04 17:21:22 +00:00
|
|
|
this.amount = round_di(parseFloat(value) || 0, 2);
|
|
|
|
this.trigger('change:amount',this);
|
2012-04-24 16:25:46 +00:00
|
|
|
},
|
2012-07-03 11:47:41 +00:00
|
|
|
// returns the amount of money on this paymentline
|
|
|
|
get_amount: function(){
|
|
|
|
return this.amount;
|
2012-04-24 16:25:46 +00:00
|
|
|
},
|
2013-12-04 17:21:22 +00:00
|
|
|
set_selected: function(selected){
|
|
|
|
if(this.selected !== selected){
|
|
|
|
this.selected = selected;
|
|
|
|
this.trigger('change:selected',this);
|
|
|
|
}
|
2012-07-03 11:47:41 +00:00
|
|
|
},
|
2013-12-16 14:35:59 +00:00
|
|
|
// returns the associated cashregister
|
2012-07-03 11:47:41 +00:00
|
|
|
//exports as JSON for server communication
|
|
|
|
export_as_JSON: function(){
|
2012-04-24 16:25:46 +00:00
|
|
|
return {
|
|
|
|
name: instance.web.datetime_to_str(new Date()),
|
2013-12-16 14:35:59 +00:00
|
|
|
statement_id: this.cashregister.id,
|
|
|
|
account_id: this.cashregister.account_id[0],
|
|
|
|
journal_id: this.cashregister.journal_id[0],
|
2012-07-03 11:47:41 +00:00
|
|
|
amount: this.get_amount()
|
2012-04-24 16:25:46 +00:00
|
|
|
};
|
|
|
|
},
|
2012-07-10 10:15:15 +00:00
|
|
|
//exports as JSON for receipt printing
|
2012-07-03 14:03:15 +00:00
|
|
|
export_for_printing: function(){
|
|
|
|
return {
|
|
|
|
amount: this.get_amount(),
|
2013-12-16 14:35:59 +00:00
|
|
|
journal: this.cashregister.journal_id[1],
|
2012-07-03 14:03:15 +00:00
|
|
|
};
|
|
|
|
},
|
2012-04-24 16:25:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.PaymentlineCollection = Backbone.Collection.extend({
|
|
|
|
model: module.Paymentline,
|
|
|
|
});
|
|
|
|
|
2012-07-03 11:47:41 +00:00
|
|
|
|
2012-05-21 14:09:01 +00:00
|
|
|
// An order more or less represents the content of a client's shopping cart (the OrderLines)
|
2013-12-04 17:21:22 +00:00
|
|
|
// plus the associated payment information (the Paymentlines)
|
2012-05-21 14:09:01 +00:00
|
|
|
// there is always an active ('selected') order in the Pos, a new one is created
|
|
|
|
// automaticaly once an order is completed and sent to the server.
|
2012-04-24 16:25:46 +00:00
|
|
|
module.Order = Backbone.Model.extend({
|
|
|
|
initialize: function(attributes){
|
|
|
|
Backbone.Model.prototype.initialize.apply(this, arguments);
|
2013-04-11 12:07:19 +00:00
|
|
|
this.uid = this.generateUniqueId();
|
2012-04-24 16:25:46 +00:00
|
|
|
this.set({
|
|
|
|
creationDate: new Date(),
|
|
|
|
orderLines: new module.OrderlineCollection(),
|
|
|
|
paymentLines: new module.PaymentlineCollection(),
|
2013-04-11 12:07:19 +00:00
|
|
|
name: "Order " + this.uid,
|
2012-07-12 17:00:13 +00:00
|
|
|
client: null,
|
2012-04-24 16:25:46 +00:00
|
|
|
});
|
2013-12-04 17:21:22 +00:00
|
|
|
this.pos = attributes.pos;
|
|
|
|
this.selected_orderline = undefined;
|
|
|
|
this.selected_paymentline = undefined;
|
2012-07-09 15:53:51 +00:00
|
|
|
this.screen_data = {}; // see ScreenSelector
|
2012-09-05 16:42:21 +00:00
|
|
|
this.receipt_type = 'receipt'; // 'receipt' || 'invoice'
|
2012-04-24 16:25:46 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
generateUniqueId: function() {
|
|
|
|
return new Date().getTime();
|
|
|
|
},
|
2012-07-12 17:00:13 +00:00
|
|
|
addProduct: function(product, options){
|
|
|
|
options = options || {};
|
2013-12-02 16:21:44 +00:00
|
|
|
var attr = JSON.parse(JSON.stringify(product));
|
2012-05-22 14:59:56 +00:00
|
|
|
attr.pos = this.pos;
|
2012-06-28 12:38:25 +00:00
|
|
|
attr.order = this;
|
2012-07-03 16:50:39 +00:00
|
|
|
var line = new module.Orderline({}, {pos: this.pos, order: this, product: product});
|
2012-07-09 15:53:51 +00:00
|
|
|
|
2012-07-12 17:00:13 +00:00
|
|
|
if(options.quantity !== undefined){
|
|
|
|
line.set_quantity(options.quantity);
|
|
|
|
}
|
|
|
|
if(options.price !== undefined){
|
2013-01-22 13:18:01 +00:00
|
|
|
line.set_unit_price(options.price);
|
2012-07-09 15:53:51 +00:00
|
|
|
}
|
2012-05-22 14:59:56 +00:00
|
|
|
|
2012-06-28 12:38:25 +00:00
|
|
|
var last_orderline = this.getLastOrderline();
|
2012-07-12 17:00:13 +00:00
|
|
|
if( last_orderline && last_orderline.can_be_merged_with(line) && options.merge !== false){
|
2012-06-28 12:38:25 +00:00
|
|
|
last_orderline.merge(line);
|
2012-05-22 14:59:56 +00:00
|
|
|
}else{
|
|
|
|
this.get('orderLines').add(line);
|
|
|
|
}
|
2012-06-28 12:38:25 +00:00
|
|
|
this.selectLine(this.getLastOrderline());
|
2012-05-22 14:59:56 +00:00
|
|
|
},
|
2012-06-28 12:38:25 +00:00
|
|
|
removeOrderline: function( line ){
|
|
|
|
this.get('orderLines').remove(line);
|
|
|
|
this.selectLine(this.getLastOrderline());
|
|
|
|
},
|
|
|
|
getLastOrderline: function(){
|
|
|
|
return this.get('orderLines').at(this.get('orderLines').length -1);
|
2012-04-24 16:25:46 +00:00
|
|
|
},
|
2013-12-16 14:35:59 +00:00
|
|
|
addPaymentline: function(cashregister) {
|
2012-07-03 16:50:39 +00:00
|
|
|
var paymentLines = this.get('paymentLines');
|
2013-12-16 14:35:59 +00:00
|
|
|
var newPaymentline = new module.Paymentline({},{cashregister:cashregister});
|
|
|
|
if(cashregister.journal.type !== 'cash'){
|
2013-12-04 17:21:22 +00:00
|
|
|
newPaymentline.set_amount( Math.max(this.getDueLeft(),0) );
|
2012-07-03 16:50:39 +00:00
|
|
|
}
|
|
|
|
paymentLines.add(newPaymentline);
|
2013-12-04 17:21:22 +00:00
|
|
|
this.selectPaymentline(newPaymentline);
|
|
|
|
|
|
|
|
},
|
|
|
|
removePaymentline: function(line){
|
|
|
|
if(this.selected_paymentline === line){
|
|
|
|
this.selectPaymentline(undefined);
|
|
|
|
}
|
|
|
|
this.get('paymentLines').remove(line);
|
2012-04-24 16:25:46 +00:00
|
|
|
},
|
|
|
|
getName: function() {
|
|
|
|
return this.get('name');
|
|
|
|
},
|
2013-01-22 13:18:01 +00:00
|
|
|
getSubtotal : function(){
|
|
|
|
return (this.get('orderLines')).reduce((function(sum, orderLine){
|
|
|
|
return sum + orderLine.get_display_price();
|
|
|
|
}), 0);
|
|
|
|
},
|
|
|
|
getTotalTaxIncluded: function() {
|
2012-04-24 16:25:46 +00:00
|
|
|
return (this.get('orderLines')).reduce((function(sum, orderLine) {
|
2012-07-03 11:47:41 +00:00
|
|
|
return sum + orderLine.get_price_with_tax();
|
2012-04-24 16:25:46 +00:00
|
|
|
}), 0);
|
|
|
|
},
|
2012-10-05 11:05:04 +00:00
|
|
|
getDiscountTotal: function() {
|
|
|
|
return (this.get('orderLines')).reduce((function(sum, orderLine) {
|
2013-01-22 13:18:01 +00:00
|
|
|
return sum + (orderLine.get_unit_price() * (orderLine.get_discount()/100) * orderLine.get_quantity());
|
2012-10-05 11:05:04 +00:00
|
|
|
}), 0);
|
|
|
|
},
|
2012-04-24 16:25:46 +00:00
|
|
|
getTotalTaxExcluded: function() {
|
|
|
|
return (this.get('orderLines')).reduce((function(sum, orderLine) {
|
2012-07-03 11:47:41 +00:00
|
|
|
return sum + orderLine.get_price_without_tax();
|
2012-04-24 16:25:46 +00:00
|
|
|
}), 0);
|
|
|
|
},
|
|
|
|
getTax: function() {
|
|
|
|
return (this.get('orderLines')).reduce((function(sum, orderLine) {
|
2012-07-03 11:47:41 +00:00
|
|
|
return sum + orderLine.get_tax();
|
2012-04-24 16:25:46 +00:00
|
|
|
}), 0);
|
|
|
|
},
|
2014-01-06 13:51:19 +00:00
|
|
|
getTaxDetails: function(){
|
|
|
|
var details = {};
|
|
|
|
var fulldetails = [];
|
|
|
|
var taxes_by_id = {};
|
|
|
|
|
|
|
|
for(var i = 0; i < this.pos.taxes.length; i++){
|
|
|
|
taxes_by_id[this.pos.taxes[i].id] = this.pos.taxes[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.get('orderLines').each(function(line){
|
|
|
|
var ldetails = line.get_tax_details();
|
|
|
|
for(var id in ldetails){
|
|
|
|
if(ldetails.hasOwnProperty(id)){
|
|
|
|
details[id] = (details[id] || 0) + ldetails[id];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
for(var id in details){
|
|
|
|
if(details.hasOwnProperty(id)){
|
|
|
|
fulldetails.push({amount: details[id], tax: taxes_by_id[id]});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fulldetails;
|
|
|
|
},
|
2012-04-24 16:25:46 +00:00
|
|
|
getPaidTotal: function() {
|
|
|
|
return (this.get('paymentLines')).reduce((function(sum, paymentLine) {
|
2012-07-03 11:47:41 +00:00
|
|
|
return sum + paymentLine.get_amount();
|
2012-04-24 16:25:46 +00:00
|
|
|
}), 0);
|
|
|
|
},
|
|
|
|
getChange: function() {
|
2013-01-23 18:32:05 +00:00
|
|
|
return this.getPaidTotal() - this.getTotalTaxIncluded();
|
2012-04-24 16:25:46 +00:00
|
|
|
},
|
|
|
|
getDueLeft: function() {
|
2013-01-23 18:32:05 +00:00
|
|
|
return this.getTotalTaxIncluded() - this.getPaidTotal();
|
2012-04-24 16:25:46 +00:00
|
|
|
},
|
2012-09-05 16:42:21 +00:00
|
|
|
// sets the type of receipt 'receipt'(default) or 'invoice'
|
|
|
|
set_receipt_type: function(type){
|
|
|
|
this.receipt_type = type;
|
|
|
|
},
|
|
|
|
get_receipt_type: function(){
|
|
|
|
return this.receipt_type;
|
|
|
|
},
|
2012-07-12 17:00:13 +00:00
|
|
|
// the client related to the current order.
|
|
|
|
set_client: function(client){
|
|
|
|
this.set('client',client);
|
|
|
|
},
|
|
|
|
get_client: function(){
|
|
|
|
return this.get('client');
|
|
|
|
},
|
2012-11-21 11:26:44 +00:00
|
|
|
get_client_name: function(){
|
|
|
|
var client = this.get('client');
|
|
|
|
return client ? client.name : "";
|
|
|
|
},
|
2012-07-09 15:53:51 +00:00
|
|
|
// the order also stores the screen status, as the PoS supports
|
|
|
|
// different active screens per order. This method is used to
|
2012-07-10 10:15:15 +00:00
|
|
|
// store the screen status.
|
2012-07-09 15:53:51 +00:00
|
|
|
set_screen_data: function(key,value){
|
|
|
|
if(arguments.length === 2){
|
|
|
|
this.screen_data[key] = value;
|
|
|
|
}else if(arguments.length === 1){
|
|
|
|
for(key in arguments[0]){
|
|
|
|
this.screen_data[key] = arguments[0][key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
//see set_screen_data
|
|
|
|
get_screen_data: function(key){
|
|
|
|
return this.screen_data[key];
|
|
|
|
},
|
2012-07-10 10:15:15 +00:00
|
|
|
// exports a JSON for receipt printing
|
2012-07-03 14:03:15 +00:00
|
|
|
export_for_printing: function(){
|
|
|
|
var orderlines = [];
|
|
|
|
this.get('orderLines').each(function(orderline){
|
|
|
|
orderlines.push(orderline.export_for_printing());
|
|
|
|
});
|
|
|
|
|
|
|
|
var paymentlines = [];
|
|
|
|
this.get('paymentLines').each(function(paymentline){
|
|
|
|
paymentlines.push(paymentline.export_for_printing());
|
|
|
|
});
|
2012-07-12 17:00:13 +00:00
|
|
|
var client = this.get('client');
|
2013-12-16 14:35:59 +00:00
|
|
|
var cashier = this.pos.cashier || this.pos.user;
|
|
|
|
var company = this.pos.company;
|
|
|
|
var shop = this.pos.shop;
|
2012-07-03 14:03:15 +00:00
|
|
|
var date = new Date();
|
|
|
|
|
|
|
|
return {
|
|
|
|
orderlines: orderlines,
|
|
|
|
paymentlines: paymentlines,
|
2013-01-24 16:22:25 +00:00
|
|
|
subtotal: this.getSubtotal(),
|
2013-01-23 18:32:05 +00:00
|
|
|
total_with_tax: this.getTotalTaxIncluded(),
|
2012-07-03 14:03:15 +00:00
|
|
|
total_without_tax: this.getTotalTaxExcluded(),
|
|
|
|
total_tax: this.getTax(),
|
|
|
|
total_paid: this.getPaidTotal(),
|
2013-01-24 16:22:25 +00:00
|
|
|
total_discount: this.getDiscountTotal(),
|
2014-01-06 13:51:19 +00:00
|
|
|
tax_details: this.getTaxDetails(),
|
2012-07-03 14:03:15 +00:00
|
|
|
change: this.getChange(),
|
|
|
|
name : this.getName(),
|
|
|
|
client: client ? client.name : null ,
|
2012-08-20 13:59:57 +00:00
|
|
|
invoice_id: null, //TODO
|
2012-07-03 14:03:15 +00:00
|
|
|
cashier: cashier ? cashier.name : null,
|
2014-01-02 14:15:19 +00:00
|
|
|
header: this.pos.config.receipt_header || '',
|
|
|
|
footer: this.pos.config.receipt_footer || '',
|
2013-12-26 16:56:20 +00:00
|
|
|
precision: {
|
|
|
|
price: 2,
|
|
|
|
money: 2,
|
|
|
|
quantity: 3,
|
|
|
|
},
|
2012-07-03 14:03:15 +00:00
|
|
|
date: {
|
|
|
|
year: date.getFullYear(),
|
|
|
|
month: date.getMonth(),
|
|
|
|
date: date.getDate(), // day of the month
|
|
|
|
day: date.getDay(), // day of the week
|
|
|
|
hour: date.getHours(),
|
2013-12-20 16:30:57 +00:00
|
|
|
minute: date.getMinutes() ,
|
|
|
|
isostring: date.toISOString(),
|
2012-07-03 14:03:15 +00:00
|
|
|
},
|
2012-07-09 15:53:51 +00:00
|
|
|
company:{
|
|
|
|
email: company.email,
|
|
|
|
website: company.website,
|
|
|
|
company_registry: company.company_registry,
|
2014-01-27 16:10:05 +00:00
|
|
|
contact_address: company.partner_id[1],
|
2012-07-09 15:53:51 +00:00
|
|
|
vat: company.vat,
|
|
|
|
name: company.name,
|
|
|
|
phone: company.phone,
|
2013-12-24 15:11:12 +00:00
|
|
|
logo: this.pos.company_logo_base64,
|
2012-07-09 15:53:51 +00:00
|
|
|
},
|
|
|
|
shop:{
|
|
|
|
name: shop.name,
|
|
|
|
},
|
2013-12-16 14:35:59 +00:00
|
|
|
currency: this.pos.currency,
|
2012-07-03 14:03:15 +00:00
|
|
|
};
|
|
|
|
},
|
2013-04-11 12:07:19 +00:00
|
|
|
export_as_JSON: function() {
|
2012-04-24 16:25:46 +00:00
|
|
|
var orderLines, paymentLines;
|
|
|
|
orderLines = [];
|
|
|
|
(this.get('orderLines')).each(_.bind( function(item) {
|
2012-07-03 11:47:41 +00:00
|
|
|
return orderLines.push([0, 0, item.export_as_JSON()]);
|
2012-04-24 16:25:46 +00:00
|
|
|
}, this));
|
|
|
|
paymentLines = [];
|
|
|
|
(this.get('paymentLines')).each(_.bind( function(item) {
|
2012-07-03 11:47:41 +00:00
|
|
|
return paymentLines.push([0, 0, item.export_as_JSON()]);
|
2012-04-24 16:25:46 +00:00
|
|
|
}, this));
|
|
|
|
return {
|
|
|
|
name: this.getName(),
|
|
|
|
amount_paid: this.getPaidTotal(),
|
2013-01-23 18:32:05 +00:00
|
|
|
amount_total: this.getTotalTaxIncluded(),
|
2012-04-24 16:25:46 +00:00
|
|
|
amount_tax: this.getTax(),
|
|
|
|
amount_return: this.getChange(),
|
|
|
|
lines: orderLines,
|
2012-06-04 11:52:44 +00:00
|
|
|
statement_ids: paymentLines,
|
2013-12-16 14:35:59 +00:00
|
|
|
pos_session_id: this.pos.pos_session.id,
|
2013-03-29 11:00:48 +00:00
|
|
|
partner_id: this.get_client() ? this.get_client().id : false,
|
2013-12-16 14:35:59 +00:00
|
|
|
user_id: this.pos.cashier ? this.pos.cashier.id : this.pos.user.id,
|
2013-04-11 12:07:19 +00:00
|
|
|
uid: this.uid,
|
2012-04-24 16:25:46 +00:00
|
|
|
};
|
|
|
|
},
|
2012-06-28 12:38:25 +00:00
|
|
|
getSelectedLine: function(){
|
|
|
|
return this.selected_orderline;
|
|
|
|
},
|
|
|
|
selectLine: function(line){
|
|
|
|
if(line){
|
|
|
|
if(line !== this.selected_orderline){
|
|
|
|
if(this.selected_orderline){
|
2012-07-03 11:47:41 +00:00
|
|
|
this.selected_orderline.set_selected(false);
|
2012-06-28 12:38:25 +00:00
|
|
|
}
|
|
|
|
this.selected_orderline = line;
|
2012-07-03 11:47:41 +00:00
|
|
|
this.selected_orderline.set_selected(true);
|
2012-06-28 12:38:25 +00:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
this.selected_orderline = undefined;
|
|
|
|
}
|
|
|
|
},
|
2013-12-02 16:21:44 +00:00
|
|
|
deselectLine: function(){
|
|
|
|
if(this.selected_orderline){
|
|
|
|
this.selected_orderline.set_selected(false);
|
|
|
|
this.selected_orderline = undefined;
|
|
|
|
}
|
|
|
|
},
|
2013-12-04 17:21:22 +00:00
|
|
|
selectPaymentline: function(line){
|
|
|
|
if(line !== this.selected_paymentline){
|
|
|
|
if(this.selected_paymentline){
|
|
|
|
this.selected_paymentline.set_selected(false);
|
|
|
|
}
|
|
|
|
this.selected_paymentline = line;
|
|
|
|
if(this.selected_paymentline){
|
|
|
|
this.selected_paymentline.set_selected(true);
|
|
|
|
}
|
|
|
|
this.trigger('change:selected_paymentline',this.selected_paymentline);
|
|
|
|
}
|
|
|
|
},
|
2012-04-24 16:25:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.OrderCollection = Backbone.Collection.extend({
|
|
|
|
model: module.Order,
|
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
The numpad handles both the choice of the property currently being modified
|
|
|
|
(quantity, price or discount) and the edition of the corresponding numeric value.
|
|
|
|
*/
|
|
|
|
module.NumpadState = Backbone.Model.extend({
|
|
|
|
defaults: {
|
|
|
|
buffer: "0",
|
|
|
|
mode: "quantity"
|
|
|
|
},
|
|
|
|
appendNewChar: function(newChar) {
|
|
|
|
var oldBuffer;
|
|
|
|
oldBuffer = this.get('buffer');
|
|
|
|
if (oldBuffer === '0') {
|
|
|
|
this.set({
|
|
|
|
buffer: newChar
|
|
|
|
});
|
|
|
|
} else if (oldBuffer === '-0') {
|
|
|
|
this.set({
|
|
|
|
buffer: "-" + newChar
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.set({
|
|
|
|
buffer: (this.get('buffer')) + newChar
|
|
|
|
});
|
|
|
|
}
|
2013-01-24 14:06:31 +00:00
|
|
|
this.trigger('set_value',this.get('buffer'));
|
2012-04-24 16:25:46 +00:00
|
|
|
},
|
|
|
|
deleteLastChar: function() {
|
2013-01-24 14:06:31 +00:00
|
|
|
if(this.get('buffer') === ""){
|
2013-01-22 13:18:01 +00:00
|
|
|
if(this.get('mode') === 'quantity'){
|
2013-01-24 14:06:31 +00:00
|
|
|
this.trigger('set_value','remove');
|
2013-01-22 13:18:01 +00:00
|
|
|
}else{
|
2013-01-24 14:06:31 +00:00
|
|
|
this.trigger('set_value',this.get('buffer'));
|
2012-06-28 12:38:25 +00:00
|
|
|
}
|
|
|
|
}else{
|
2013-01-24 14:06:31 +00:00
|
|
|
var newBuffer = this.get('buffer').slice(0,-1) || "";
|
|
|
|
this.set({ buffer: newBuffer });
|
|
|
|
this.trigger('set_value',this.get('buffer'));
|
2012-04-24 16:25:46 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
switchSign: function() {
|
|
|
|
var oldBuffer;
|
|
|
|
oldBuffer = this.get('buffer');
|
|
|
|
this.set({
|
|
|
|
buffer: oldBuffer[0] === '-' ? oldBuffer.substr(1) : "-" + oldBuffer
|
|
|
|
});
|
2013-01-24 14:06:31 +00:00
|
|
|
this.trigger('set_value',this.get('buffer'));
|
2012-04-24 16:25:46 +00:00
|
|
|
},
|
|
|
|
changeMode: function(newMode) {
|
|
|
|
this.set({
|
|
|
|
buffer: "0",
|
|
|
|
mode: newMode
|
|
|
|
});
|
|
|
|
},
|
|
|
|
reset: function() {
|
|
|
|
this.set({
|
|
|
|
buffer: "0",
|
|
|
|
mode: "quantity"
|
|
|
|
});
|
|
|
|
},
|
2013-01-22 13:18:01 +00:00
|
|
|
resetValue: function(){
|
|
|
|
this.set({buffer:'0'});
|
2012-06-28 12:38:25 +00:00
|
|
|
},
|
2012-04-24 16:25:46 +00:00
|
|
|
});
|
|
|
|
}
|