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;
|
|
|
|
|
2013-04-09 14:35:36 +00:00
|
|
|
var round_di = instance.web.round_digits;
|
|
|
|
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.ready = $.Deferred(); // used to notify the GUI that the PosModel has loaded all resources
|
|
|
|
this.flush_mutex = new $.Mutex(); // used to make sure the orders are sent to the server once at time
|
2012-08-09 17:21:13 +00:00
|
|
|
|
2012-05-21 14:09:01 +00:00
|
|
|
this.barcode_reader = new module.BarcodeReader({'pos': this}); // used to read barcodes
|
2012-08-09 17:21:13 +00:00
|
|
|
this.proxy = new module.ProxyDevice(); // used to communicate to the hardware devices via a local proxy
|
|
|
|
this.db = new module.PosLS(); // a database used to store the products and categories
|
2012-08-14 15:21:12 +00:00
|
|
|
this.db.clear('products','categories');
|
2013-01-22 13:18:01 +00:00
|
|
|
this.debug = jQuery.deparam(jQuery.param.querystring()).debug !== undefined; //debug mode
|
2012-08-09 17:21:13 +00:00
|
|
|
|
2012-05-21 14:09:01 +00:00
|
|
|
// default attributes values. If null, it will be loaded below.
|
2012-04-24 16:25:46 +00:00
|
|
|
this.set({
|
2012-05-21 14:09:01 +00:00
|
|
|
'nbr_pending_operations': 0,
|
|
|
|
|
|
|
|
'currency': {symbol: '$', position: 'after'},
|
|
|
|
'shop': null,
|
|
|
|
'company': null,
|
2012-07-10 10:15:15 +00:00
|
|
|
'user': null, // the user that loaded the pos
|
|
|
|
'user_list': null, // list of all users
|
2012-11-21 11:26:44 +00:00
|
|
|
'partner_list': null, // list of all partners with an ean
|
2012-07-10 10:15:15 +00:00
|
|
|
'cashier': null, // the logged cashier, if different from user
|
2012-05-21 14:09:01 +00:00
|
|
|
|
|
|
|
'orders': new module.OrderCollection(),
|
2012-06-04 11:52:44 +00:00
|
|
|
//this is the product list as seen by the product list widgets, it will change based on the category filters
|
|
|
|
'products': new module.ProductCollection(),
|
2012-05-21 14:09:01 +00:00
|
|
|
'cashRegisters': null,
|
|
|
|
|
|
|
|
'bank_statements': null,
|
|
|
|
'taxes': null,
|
|
|
|
'pos_session': null,
|
|
|
|
'pos_config': null,
|
2012-08-21 10:04:52 +00:00
|
|
|
'units': null,
|
|
|
|
'units_by_id': null,
|
2012-05-21 14:09:01 +00:00
|
|
|
|
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
|
|
|
|
2012-07-10 10:15:15 +00:00
|
|
|
this.get('orders').bind('remove', function(){ self.on_removed_order(); });
|
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.
|
|
|
|
$.when(this.load_server_data())
|
2012-10-31 10:31:48 +00:00
|
|
|
.done(function(){
|
2012-09-03 14:25:27 +00:00
|
|
|
//self.log_loaded_data(); //Uncomment if you want to log the data to the console for easier debugging
|
|
|
|
self.ready.resolve();
|
2012-10-31 10:31:48 +00:00
|
|
|
}).fail(function(){
|
2012-09-03 14:25:27 +00:00
|
|
|
//we failed to load some backend data, or the backend was badly configured.
|
|
|
|
//the error messages will be displayed in PosWidget
|
|
|
|
self.ready.reject();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-09-03 14:43:54 +00:00
|
|
|
// helper function to load data from the server
|
|
|
|
fetch: function(model, fields, domain, ctx){
|
|
|
|
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){
|
2012-08-21 10:04:52 +00:00
|
|
|
self.set('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
|
|
|
],
|
2012-08-21 10:04:52 +00:00
|
|
|
[['id','=',users[0].company_id[0]]]);
|
2012-10-31 10:31:48 +00:00
|
|
|
}).then(function(companies){
|
2012-08-21 10:04:52 +00:00
|
|
|
self.set('company',companies[0]);
|
2012-07-09 15:53:51 +00:00
|
|
|
|
2012-09-03 14:43:54 +00:00
|
|
|
return self.fetch('res.partner',['contact_address'],[['id','=',companies[0].partner_id[0]]]);
|
2012-10-31 10:31:48 +00:00
|
|
|
}).then(function(company_partners){
|
2012-08-21 10:04:52 +00:00
|
|
|
self.get('company').contact_address = company_partners[0].contact_address;
|
|
|
|
|
2013-01-28 17:18:00 +00:00
|
|
|
return self.fetch('res.currency',['symbol','position','rounding','accuracy'],[['id','=',self.get('company').currency_id[0]]]);
|
2012-10-31 10:31:48 +00:00
|
|
|
}).then(function(currencies){
|
2013-01-28 17:18:00 +00:00
|
|
|
console.log('Currency:',currencies[0]);
|
2012-08-21 10:04:52 +00:00
|
|
|
self.set('currency',currencies[0]);
|
2012-04-24 16:25:46 +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){
|
2012-08-21 10:04:52 +00:00
|
|
|
self.set('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
|
|
|
}
|
2012-08-21 10:04:52 +00:00
|
|
|
self.set('units_by_id',units_by_id);
|
2012-05-21 14:09:01 +00:00
|
|
|
|
2012-09-03 14:43:54 +00:00
|
|
|
return self.fetch('product.packaging', null, null);
|
2012-10-31 10:31:48 +00:00
|
|
|
}).then(function(packagings){
|
2012-08-21 10:04:52 +00:00
|
|
|
self.set('product.packaging',packagings);
|
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){
|
2012-08-21 10:04:52 +00:00
|
|
|
self.set('user_list',users);
|
|
|
|
|
2012-11-21 11:26:44 +00:00
|
|
|
return self.fetch('res.partner', ['name','ean13'], [['ean13', '!=', false]]);
|
|
|
|
}).then(function(partners){
|
|
|
|
self.set('partner_list',partners);
|
|
|
|
|
2012-09-03 14:43:54 +00:00
|
|
|
return self.fetch('account.tax', ['amount', 'price_include', 'type']);
|
2012-10-31 10:31:48 +00:00
|
|
|
}).then(function(taxes){
|
2012-08-21 10:04:52 +00:00
|
|
|
self.set('taxes', taxes);
|
|
|
|
|
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]]
|
|
|
|
);
|
2012-10-31 10:31:48 +00:00
|
|
|
}).then(function(sessions){
|
2012-08-21 10:04:52 +00:00
|
|
|
self.set('pos_session', sessions[0]);
|
|
|
|
|
2012-09-03 14:43:54 +00:00
|
|
|
return self.fetch(
|
2012-08-21 10:04:52 +00:00
|
|
|
'pos.config',
|
|
|
|
['name','journal_ids','shop_id','journal_id',
|
|
|
|
'iface_self_checkout', 'iface_led', 'iface_cashdrawer',
|
|
|
|
'iface_payment_terminal', 'iface_electronic_scale', 'iface_barscan', 'iface_vkeyboard',
|
|
|
|
'iface_print_via_proxy','iface_cashdrawer','state','sequence_id','session_ids'],
|
|
|
|
[['id','=', self.get('pos_session').config_id[0]]]
|
|
|
|
);
|
2012-10-31 10:31:48 +00:00
|
|
|
}).then(function(configs){
|
2012-08-21 10:04:52 +00:00
|
|
|
var pos_config = configs[0];
|
|
|
|
self.set('pos_config', pos_config);
|
2012-08-21 10:48:14 +00:00
|
|
|
self.iface_electronic_scale = !!pos_config.iface_electronic_scale;
|
|
|
|
self.iface_print_via_proxy = !!pos_config.iface_print_via_proxy;
|
|
|
|
self.iface_vkeyboard = !!pos_config.iface_vkeyboard;
|
|
|
|
self.iface_self_checkout = !!pos_config.iface_self_checkout;
|
|
|
|
self.iface_cashdrawer = !!pos_config.iface_cashdrawer;
|
2012-08-21 10:04:52 +00:00
|
|
|
|
2012-09-03 14:43:54 +00:00
|
|
|
return self.fetch('sale.shop',[],[['id','=',pos_config.shop_id[0]]]);
|
2012-10-31 10:31:48 +00:00
|
|
|
}).then(function(shops){
|
2012-08-21 10:04:52 +00:00
|
|
|
self.set('shop',shops[0]);
|
|
|
|
|
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);
|
|
|
|
|
2012-09-03 14:43:54 +00:00
|
|
|
return self.fetch('pos.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);
|
|
|
|
|
2012-09-03 14:43:54 +00:00
|
|
|
return self.fetch(
|
2012-08-21 10:04:52 +00:00
|
|
|
'product.product',
|
2012-09-03 13:32:39 +00:00
|
|
|
['name', 'list_price','price','pos_categ_id', 'taxes_id', 'ean13',
|
|
|
|
'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]],
|
2012-08-21 10:04:52 +00:00
|
|
|
{pricelist: self.get('shop').pricelist_id[0]} // context for price
|
|
|
|
);
|
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'],
|
|
|
|
[['state','=','open'],['pos_session_id', '=', self.get('pos_session').id]]
|
|
|
|
);
|
2012-10-31 10:31:48 +00:00
|
|
|
}).then(function(bank_statements){
|
2012-10-12 07:01:45 +00:00
|
|
|
var journals = new Array();
|
|
|
|
_.each(bank_statements,function(statement) {
|
|
|
|
journals.push(statement.journal_id[0])
|
|
|
|
});
|
2012-08-21 10:04:52 +00:00
|
|
|
self.set('bank_statements', bank_statements);
|
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){
|
2012-08-21 10:04:52 +00:00
|
|
|
self.set('journals',journals);
|
|
|
|
|
|
|
|
// associate the bank statements with their journals.
|
|
|
|
var bank_statements = self.get('bank_statements');
|
|
|
|
for(var i = 0, ilen = bank_statements.length; i < ilen; i++){
|
|
|
|
for(var j = 0, jlen = journals.length; j < jlen; j++){
|
|
|
|
if(bank_statements[i].journal_id[0] === journals[j].id){
|
|
|
|
bank_statements[i].journal = journals[j];
|
|
|
|
bank_statements[i].self_checkout_payment_method = journals[j].self_checkout_payment_method;
|
|
|
|
}
|
|
|
|
}
|
2012-05-08 16:42:26 +00:00
|
|
|
}
|
2012-08-21 10:04:52 +00:00
|
|
|
self.set({'cashRegisters' : new module.CashRegisterCollection(self.get('bank_statements'))});
|
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
|
|
|
// logs the usefull posmodel data to the console for debug purposes
|
|
|
|
log_loaded_data: function(){
|
|
|
|
console.log('PosModel data has been loaded:');
|
2012-06-28 12:38:25 +00:00
|
|
|
console.log('PosModel: units:',this.get('units'));
|
2012-05-21 14:09:01 +00:00
|
|
|
console.log('PosModel: bank_statements:',this.get('bank_statements'));
|
2012-05-24 10:22:57 +00:00
|
|
|
console.log('PosModel: journals:',this.get('journals'));
|
2012-05-21 14:09:01 +00:00
|
|
|
console.log('PosModel: taxes:',this.get('taxes'));
|
|
|
|
console.log('PosModel: pos_session:',this.get('pos_session'));
|
|
|
|
console.log('PosModel: pos_config:',this.get('pos_config'));
|
|
|
|
console.log('PosModel: cashRegisters:',this.get('cashRegisters'));
|
|
|
|
console.log('PosModel: shop:',this.get('shop'));
|
|
|
|
console.log('PosModel: company:',this.get('company'));
|
|
|
|
console.log('PosModel: currency:',this.get('currency'));
|
2012-06-28 12:38:25 +00:00
|
|
|
console.log('PosModel: user_list:',this.get('user_list'));
|
2012-07-03 11:47:41 +00:00
|
|
|
console.log('PosModel: user:',this.get('user'));
|
2012-05-21 14:09:01 +00:00
|
|
|
console.log('PosModel.session:',this.session);
|
|
|
|
console.log('PosModel end of data log.');
|
2012-04-24 16:25:46 +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
|
|
|
|
on_removed_order: function(removed_order){
|
|
|
|
if( this.get('orders').isEmpty()){
|
2012-08-09 17:21:13 +00:00
|
|
|
this.add_new_order();
|
2012-05-21 14:09:01 +00:00
|
|
|
}
|
|
|
|
if( this.get('selectedOrder') === removed_order){
|
|
|
|
this.set({ selectedOrder: this.get('orders').last() });
|
|
|
|
}
|
|
|
|
},
|
2012-04-24 16:25:46 +00:00
|
|
|
|
2012-07-11 16:03:25 +00:00
|
|
|
// saves the order locally and try to send it to the backend. 'record' is a bizzarely defined JSON version of the Order
|
2012-04-24 16:25:46 +00:00
|
|
|
push_order: function(record) {
|
2012-08-14 15:21:12 +00:00
|
|
|
this.db.add_order(record);
|
|
|
|
this.flush();
|
2012-04-24 16:25:46 +00:00
|
|
|
},
|
2012-05-21 14:09:01 +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
|
|
|
|
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() {
|
2012-09-05 16:42:21 +00:00
|
|
|
//TODO make the mutex work
|
2012-05-21 14:09:01 +00:00
|
|
|
//this makes sure only one _int_flush is called at the same time
|
2012-09-05 16:42:21 +00:00
|
|
|
/*
|
2012-04-24 16:25:46 +00:00
|
|
|
return this.flush_mutex.exec(_.bind(function() {
|
2012-08-14 15:21:12 +00:00
|
|
|
return this._flush(0);
|
2012-04-24 16:25:46 +00:00
|
|
|
}, this));
|
2012-09-05 16:42:21 +00:00
|
|
|
*/
|
|
|
|
this._flush(0);
|
2012-04-24 16:25:46 +00:00
|
|
|
},
|
2012-08-14 15:21:12 +00:00
|
|
|
// attempts to send an order of index 'index' in the list of order to send. The index
|
|
|
|
// is used to skip orders that failed. do not call this method outside the mutex provided
|
|
|
|
// by flush()
|
|
|
|
_flush: function(index){
|
2012-04-24 16:25:46 +00:00
|
|
|
var self = this;
|
2012-08-14 15:21:12 +00:00
|
|
|
var orders = this.db.get_orders();
|
|
|
|
self.set('nbr_pending_operations',orders.length);
|
2012-05-04 15:27:20 +00:00
|
|
|
|
2012-08-14 15:21:12 +00:00
|
|
|
var order = orders[index];
|
|
|
|
if(!order){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//try to push an order to the server
|
2013-03-27 12:55:41 +00:00
|
|
|
// shadow : true is to prevent a spinner to appear in case of timeout
|
|
|
|
(new instance.web.Model('pos.order')).call('create_from_ui',[[order]],undefined,{ shadow:true })
|
2012-08-14 15:21:12 +00:00
|
|
|
.fail(function(unused, event){
|
2012-08-16 15:02:13 +00:00
|
|
|
//don't show error popup if it fails
|
2012-08-14 15:21:12 +00:00
|
|
|
event.preventDefault();
|
|
|
|
console.error('Failed to send order:',order);
|
|
|
|
self._flush(index+1);
|
|
|
|
})
|
|
|
|
.done(function(){
|
|
|
|
//remove from db if success
|
|
|
|
self.db.remove_order(order.id);
|
|
|
|
self._flush(index);
|
|
|
|
});
|
2012-04-24 16:25:46 +00:00
|
|
|
},
|
2012-05-29 16:39:44 +00:00
|
|
|
|
2012-08-09 17:21:13 +00:00
|
|
|
scan_product: function(parsed_ean){
|
|
|
|
var self = this;
|
2012-08-13 16:00:26 +00:00
|
|
|
var product = this.db.get_product_by_ean13(parsed_ean.base_ean);
|
|
|
|
var selectedOrder = this.get('selectedOrder');
|
|
|
|
|
|
|
|
if(!product){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(parsed_ean.type === 'price'){
|
|
|
|
selectedOrder.addProduct(new module.Product(product), {price:parsed_ean.value});
|
|
|
|
}else if(parsed_ean.type === 'weight'){
|
|
|
|
selectedOrder.addProduct(new module.Product(product), {quantity:parsed_ean.value, merge:false});
|
|
|
|
}else{
|
|
|
|
selectedOrder.addProduct(new module.Product(product));
|
|
|
|
}
|
|
|
|
return true;
|
2012-05-29 13:31:41 +00:00
|
|
|
},
|
2012-04-24 16:25:46 +00:00
|
|
|
});
|
|
|
|
|
2012-05-16 13:01:42 +00:00
|
|
|
module.CashRegister = Backbone.Model.extend({
|
|
|
|
});
|
|
|
|
|
|
|
|
module.CashRegisterCollection = Backbone.Collection.extend({
|
|
|
|
model: module.CashRegister,
|
|
|
|
});
|
|
|
|
|
2012-04-24 16:25:46 +00:00
|
|
|
module.Product = Backbone.Model.extend({
|
2012-09-10 15:48:28 +00:00
|
|
|
get_image_url: function(){
|
2012-11-14 17:42:58 +00:00
|
|
|
return instance.session.url('/web/binary/image', {model: 'product.product', field: 'image', id: this.get('id')});
|
2012-09-10 15:48:28 +00:00
|
|
|
},
|
2012-04-24 16:25:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.ProductCollection = Backbone.Collection.extend({
|
|
|
|
model: module.Product,
|
|
|
|
});
|
|
|
|
|
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;
|
2012-10-08 12:57:09 +00:00
|
|
|
this.price = options.product.get('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;
|
2012-07-03 11:47:41 +00:00
|
|
|
this.trigger('change');
|
|
|
|
},
|
|
|
|
// 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'){
|
|
|
|
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
|
|
|
}
|
2012-07-03 11:47:41 +00:00
|
|
|
this.trigger('change');
|
|
|
|
},
|
|
|
|
// 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(){
|
2012-07-03 11:47:41 +00:00
|
|
|
var unit_id = (this.product.get('uos_id') || this.product.get('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;
|
|
|
|
}
|
|
|
|
return this.pos.get('units_by_id')[unit_id];
|
|
|
|
},
|
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;
|
|
|
|
this.trigger('change');
|
|
|
|
},
|
|
|
|
// 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){
|
2012-07-03 11:47:41 +00:00
|
|
|
if( this.get_product().get('id') !== orderline.get_product().get('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(),
|
2012-07-03 14:03:15 +00:00
|
|
|
product_id: this.get_product().get('id'),
|
|
|
|
};
|
|
|
|
},
|
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(),
|
|
|
|
product_name: this.get_product().get('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(),
|
2012-09-03 13:32:39 +00:00
|
|
|
product_description: this.get_product().get('description'),
|
|
|
|
product_description_sale: this.get_product().get('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-01-22 13:18:01 +00:00
|
|
|
this.trigger('change');
|
|
|
|
},
|
|
|
|
get_unit_price: function(){
|
2013-01-28 17:18:00 +00:00
|
|
|
var rounding = this.pos.get('currency').rounding;
|
|
|
|
return round_pr(this.price,rounding);
|
2013-01-22 13:18:01 +00:00
|
|
|
},
|
|
|
|
get_display_price: function(){
|
2013-01-28 17:18:00 +00:00
|
|
|
var rounding = this.pos.get('currency').rounding;
|
|
|
|
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
|
|
|
},
|
2013-01-28 17:18:00 +00:00
|
|
|
get_all_prices: function(){
|
2012-04-24 16:25:46 +00:00
|
|
|
var self = this;
|
2013-01-28 17:18:00 +00:00
|
|
|
var currency_rounding = this.pos.get('currency').rounding;
|
|
|
|
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_list = this.pos.get('product_list');
|
|
|
|
var product = this.get_product();
|
2012-11-05 10:43:57 +00:00
|
|
|
var taxes_ids = product.get('taxes_id');;
|
2012-04-24 17:20:47 +00:00
|
|
|
var taxes = self.pos.get('taxes');
|
2012-04-24 16:25:46 +00:00
|
|
|
var taxtotal = 0;
|
|
|
|
_.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;
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
"priceWithTax": totalTax,
|
|
|
|
"priceWithoutTax": totalNoTax,
|
|
|
|
"tax": taxtotal,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.OrderlineCollection = Backbone.Collection.extend({
|
|
|
|
model: module.Orderline,
|
|
|
|
});
|
|
|
|
|
2012-07-09 15:53:51 +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;
|
2012-07-03 16:50:39 +00:00
|
|
|
this.cashregister = options.cashRegister;
|
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-01-24 14:06:31 +00:00
|
|
|
this.amount = parseFloat(value) || 0;
|
|
|
|
this.trigger('change');
|
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
|
|
|
},
|
2012-07-03 11:47:41 +00:00
|
|
|
// returns the associated cashRegister
|
|
|
|
get_cashregister: function(){
|
|
|
|
return this.cashregister;
|
|
|
|
},
|
|
|
|
//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()),
|
2012-07-03 16:50:39 +00:00
|
|
|
statement_id: this.cashregister.get('id'),
|
2012-07-03 14:03:15 +00:00
|
|
|
account_id: (this.cashregister.get('account_id'))[0],
|
|
|
|
journal_id: (this.cashregister.get('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(),
|
|
|
|
journal: this.cashregister.get('journal_id')[1],
|
|
|
|
};
|
|
|
|
},
|
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)
|
|
|
|
// plus the associated payment information (the PaymentLines)
|
|
|
|
// 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);
|
|
|
|
this.set({
|
|
|
|
creationDate: new Date(),
|
|
|
|
orderLines: new module.OrderlineCollection(),
|
|
|
|
paymentLines: new module.PaymentlineCollection(),
|
|
|
|
name: "Order " + this.generateUniqueId(),
|
2012-07-12 17:00:13 +00:00
|
|
|
client: null,
|
2012-04-24 16:25:46 +00:00
|
|
|
});
|
2012-07-03 16:50:39 +00:00
|
|
|
this.pos = attributes.pos;
|
2012-06-28 12:38:25 +00:00
|
|
|
this.selected_orderline = 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 || {};
|
2012-05-22 14:59:56 +00:00
|
|
|
var attr = product.toJSON();
|
|
|
|
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
|
|
|
},
|
2012-05-21 09:14:42 +00:00
|
|
|
addPaymentLine: function(cashRegister) {
|
2012-07-03 16:50:39 +00:00
|
|
|
var paymentLines = this.get('paymentLines');
|
|
|
|
var newPaymentline = new module.Paymentline({},{cashRegister:cashRegister});
|
|
|
|
if(cashRegister.get('journal').type !== 'cash'){
|
|
|
|
newPaymentline.set_amount( this.getDueLeft() );
|
|
|
|
}
|
|
|
|
paymentLines.add(newPaymentline);
|
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);
|
|
|
|
},
|
|
|
|
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');
|
2012-07-03 14:03:15 +00:00
|
|
|
var cashier = this.pos.get('cashier') || this.pos.get('user');
|
2012-07-09 15:53:51 +00:00
|
|
|
var company = this.pos.get('company');
|
|
|
|
var shop = this.pos.get('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(),
|
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,
|
|
|
|
date: {
|
|
|
|
year: date.getFullYear(),
|
|
|
|
month: date.getMonth(),
|
|
|
|
date: date.getDate(), // day of the month
|
|
|
|
day: date.getDay(), // day of the week
|
|
|
|
hour: date.getHours(),
|
|
|
|
minute: date.getMinutes()
|
|
|
|
},
|
2012-07-09 15:53:51 +00:00
|
|
|
company:{
|
|
|
|
email: company.email,
|
|
|
|
website: company.website,
|
|
|
|
company_registry: company.company_registry,
|
2012-08-20 13:59:57 +00:00
|
|
|
contact_address: company.contact_address,
|
2012-07-09 15:53:51 +00:00
|
|
|
vat: company.vat,
|
|
|
|
name: company.name,
|
|
|
|
phone: company.phone,
|
|
|
|
},
|
|
|
|
shop:{
|
|
|
|
name: shop.name,
|
|
|
|
},
|
2012-07-03 14:03:15 +00:00
|
|
|
currency: this.pos.get('currency'),
|
|
|
|
};
|
|
|
|
},
|
2012-04-24 16:25:46 +00:00
|
|
|
exportAsJSON: function() {
|
|
|
|
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,
|
|
|
|
pos_session_id: this.pos.get('pos_session').id,
|
2012-07-03 11:47:41 +00:00
|
|
|
partner_id: this.pos.get('client') ? this.pos.get('client').id : undefined,
|
|
|
|
user_id: this.pos.get('cashier') ? this.pos.get('cashier').id : this.pos.get('user').id,
|
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;
|
|
|
|
}
|
|
|
|
},
|
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'));
|
2013-01-22 13:18:01 +00:00
|
|
|
}
|
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-04-24 16:25:46 +00:00
|
|
|
});
|
|
|
|
}
|