2012-08-07 16:18:44 +00:00
|
|
|
function openerp_pos_db(instance, module){
|
|
|
|
|
2012-08-10 13:58:32 +00:00
|
|
|
/* PosLS is a LocalStorage based implementation of the point of sale database,
|
|
|
|
it performs better for few products, but does not scale beyond 500 products.
|
|
|
|
*/
|
2012-08-07 16:18:44 +00:00
|
|
|
module.PosLS = instance.web.Class.extend({
|
2012-08-10 13:58:32 +00:00
|
|
|
name: 'openerp_pos_ls', //the prefix of the localstorage data
|
|
|
|
limit: 100, // the maximum number of results returned by a search
|
2012-08-07 16:18:44 +00:00
|
|
|
init: function(options){
|
|
|
|
options = options || {};
|
|
|
|
this.name = options.name || this.name;
|
|
|
|
this.limit = options.limit || this.limit;
|
|
|
|
this.products = this.name + '_products';
|
2012-08-09 17:21:13 +00:00
|
|
|
this.categories = this.name + '_categories';
|
|
|
|
|
2012-08-10 13:58:32 +00:00
|
|
|
//products cache put the data in memory to avoid roundtrips to the localstorage
|
|
|
|
this.products_cache = null;
|
|
|
|
this.categories_cache = null;
|
|
|
|
|
2012-08-09 17:21:13 +00:00
|
|
|
this.category_by_id = {};
|
|
|
|
this.root_category_id = 0;
|
|
|
|
this.category_products = {};
|
|
|
|
this.category_ancestors = {};
|
|
|
|
this.category_childs = {};
|
|
|
|
this.category_parent = {};
|
|
|
|
},
|
2012-08-10 13:58:32 +00:00
|
|
|
/* returns the category object from its id */
|
2012-08-09 17:21:13 +00:00
|
|
|
get_category_by_id: function(categ_id){
|
|
|
|
if(categ_id instanceof Array){
|
|
|
|
var list = [];
|
|
|
|
for(var i = 0, len = categ_id.length; i < len; i++){
|
|
|
|
var cat = this.category_by_id[categ_id[i]];
|
|
|
|
if(cat){
|
|
|
|
list.push(cat);
|
|
|
|
}else{
|
|
|
|
console.error("get_category_by_id: no category has id:",categ_id[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}else{
|
|
|
|
return this.category_by_id[categ_id];
|
|
|
|
}
|
|
|
|
},
|
2012-08-10 13:58:32 +00:00
|
|
|
/* returns a list of the category's child categories ids, or an empty list
|
|
|
|
* if a category has no childs */
|
2012-08-09 17:21:13 +00:00
|
|
|
get_category_childs_ids: function(categ_id){
|
|
|
|
return this.category_childs[categ_id] || [];
|
|
|
|
},
|
2012-08-10 13:58:32 +00:00
|
|
|
/* returns a list of all ancestors (parent, grand-parent, etc) categories ids
|
|
|
|
* starting from the root category to the direct parent */
|
2012-08-09 17:21:13 +00:00
|
|
|
get_category_ancestors_ids: function(categ_id){
|
|
|
|
return this.category_ancestors[categ_id] || [];
|
|
|
|
},
|
2012-08-10 13:58:32 +00:00
|
|
|
/* returns the parent category's id of a category, or the root_category_id if no parent.
|
|
|
|
* the root category is parent of itself. */
|
2012-08-09 17:21:13 +00:00
|
|
|
get_category_parent_id: function(categ_id){
|
|
|
|
return this.category_parent[categ_id] || this.root_category_id;
|
|
|
|
},
|
2012-08-10 13:58:32 +00:00
|
|
|
/* adds categories definitions to the database. categories is a list of categories objects as
|
|
|
|
* returned by the openerp server. Categories must be inserted before the products or the
|
|
|
|
* product/ categories association may (will) not work properly */
|
2012-08-09 17:21:13 +00:00
|
|
|
add_categories: function(categories){
|
|
|
|
var self = this;
|
|
|
|
if(!this.category_by_id[this.root_category_id]){
|
|
|
|
this.category_by_id[this.root_category_id] = {
|
|
|
|
id : this.root_category_id,
|
|
|
|
name : 'Root',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
for(var i=0, len = categories.length; i < len; i++){
|
|
|
|
this.category_by_id[categories[i].id] = categories[i];
|
|
|
|
}
|
|
|
|
for(var i=0, len = categories.length; i < len; i++){
|
|
|
|
var cat = categories[i];
|
|
|
|
var parent_id = cat.parent_id[0] || this.root_category_id;
|
|
|
|
this.category_parent[cat.id] = cat.parent_id[0];
|
|
|
|
if(!this.category_childs[parent_id]){
|
|
|
|
this.category_childs[parent_id] = [];
|
|
|
|
}
|
|
|
|
this.category_childs[parent_id].push(cat.id);
|
|
|
|
}
|
|
|
|
function make_ancestors(cat_id, ancestors){
|
|
|
|
self.category_ancestors[cat_id] = ancestors;
|
|
|
|
|
|
|
|
ancestors = ancestors.slice(0);
|
|
|
|
ancestors.push(cat_id);
|
|
|
|
|
|
|
|
var childs = self.category_childs[cat_id] || [];
|
|
|
|
for(var i=0, len = childs.length; i < len; i++){
|
|
|
|
make_ancestors(childs[i], ancestors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
make_ancestors(this.root_category_id, []);
|
2012-08-07 16:18:44 +00:00
|
|
|
},
|
2012-08-10 13:58:32 +00:00
|
|
|
/* this internal method returns from disc a dictionary associating ids to the products */
|
2012-08-13 16:00:26 +00:00
|
|
|
_load_products: function(){
|
2012-08-10 13:58:32 +00:00
|
|
|
if(this.products_cache){
|
|
|
|
return this.products_cache;
|
|
|
|
}
|
2012-08-07 16:18:44 +00:00
|
|
|
var products = localStorage[this.products];
|
|
|
|
if(products){
|
|
|
|
return JSON.parse(products) || {};
|
|
|
|
}else{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
},
|
2012-08-10 13:58:32 +00:00
|
|
|
/* this internal method saves a dictionary associating ids to product to the disc */
|
2012-08-13 16:00:26 +00:00
|
|
|
_save_products: function(products){
|
2012-08-07 16:18:44 +00:00
|
|
|
localStorage[this.products] = JSON.stringify(products);
|
2012-08-10 13:58:32 +00:00
|
|
|
this.products_cache = products;
|
2012-08-07 16:18:44 +00:00
|
|
|
},
|
2012-08-10 13:58:32 +00:00
|
|
|
/* this internal method returns from disc a dictionary associating ids to the categories */
|
2012-08-13 16:00:26 +00:00
|
|
|
_load_categories: function(){
|
2012-08-10 13:58:32 +00:00
|
|
|
if(this.categories_cache){
|
|
|
|
return this.categories_cache;
|
|
|
|
}
|
2012-08-09 17:21:13 +00:00
|
|
|
var categories = localStorage[this.categories];
|
|
|
|
if(categories){
|
|
|
|
return JSON.parse(categories) || {};
|
2012-08-07 16:18:44 +00:00
|
|
|
}else{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
},
|
2012-08-10 13:58:32 +00:00
|
|
|
/* this internal method saves to disc an array associating ids to the categories */
|
2012-08-13 16:00:26 +00:00
|
|
|
_save_categories: function(categories){
|
2012-08-09 17:21:13 +00:00
|
|
|
localStorage[this.categories] = JSON.stringify(categories);
|
2012-08-10 13:58:32 +00:00
|
|
|
this.categories_cache = categories;
|
2012-08-07 16:18:44 +00:00
|
|
|
},
|
2012-08-09 19:55:55 +00:00
|
|
|
add_products: function(products){
|
2012-08-13 16:00:26 +00:00
|
|
|
var stored_products = this._load_products();
|
|
|
|
var stored_categories = this._load_categories();
|
2012-08-07 16:18:44 +00:00
|
|
|
|
|
|
|
if(!products instanceof Array){
|
|
|
|
products = [products];
|
|
|
|
}
|
|
|
|
for(var i = 0, len = products.length; i < len; i++){
|
|
|
|
var product = products[i];
|
2012-08-09 17:21:13 +00:00
|
|
|
var categ_id = product.pos_categ_id[0];
|
|
|
|
if(!stored_categories[categ_id]){
|
|
|
|
stored_categories[categ_id] = [];
|
|
|
|
}
|
|
|
|
stored_categories[categ_id].push(product.id);
|
|
|
|
var ancestors = this.get_category_ancestors_ids(categ_id) || [];
|
|
|
|
|
|
|
|
for(var j = 0; j < ancestors.length; j++){
|
|
|
|
if(! stored_categories[ancestors[j]]){
|
|
|
|
stored_categories[ancestors[j]] = [];
|
|
|
|
}
|
|
|
|
stored_categories[ancestors[j]].push(product.id);
|
2012-08-07 16:18:44 +00:00
|
|
|
}
|
|
|
|
stored_products[product.id] = product;
|
|
|
|
}
|
2012-08-13 16:00:26 +00:00
|
|
|
this._save_products(stored_products);
|
|
|
|
this._save_categories(stored_categories);
|
2012-08-07 16:18:44 +00:00
|
|
|
},
|
2012-08-10 13:58:32 +00:00
|
|
|
/* removes all the data from the database. TODO : being able to selectively remove data */
|
2012-08-13 16:00:26 +00:00
|
|
|
clear: function(){
|
2012-08-07 16:18:44 +00:00
|
|
|
localStorage.removeItem(this.products);
|
2012-08-09 17:21:13 +00:00
|
|
|
localStorage.removeItem(this.categories);
|
2012-08-07 16:18:44 +00:00
|
|
|
},
|
2012-08-10 13:58:32 +00:00
|
|
|
/* this internal methods returns the count of properties in an object. */
|
2012-08-07 16:18:44 +00:00
|
|
|
_count_props : function(obj){
|
2012-08-10 13:58:32 +00:00
|
|
|
var count = 0;
|
|
|
|
for(var prop in obj){
|
|
|
|
if(obj.hasOwnProperty(prop)){
|
|
|
|
count++;
|
2012-08-07 16:18:44 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-10 13:58:32 +00:00
|
|
|
return count;
|
2012-08-07 16:18:44 +00:00
|
|
|
},
|
2012-08-13 16:00:26 +00:00
|
|
|
get_product_by_id: function(id){
|
|
|
|
return this._load_products()[id];
|
2012-08-07 16:18:44 +00:00
|
|
|
},
|
2012-08-13 16:00:26 +00:00
|
|
|
get_product_by_ean13: function(ean13){
|
|
|
|
var products = this._load_products();
|
2012-08-07 16:18:44 +00:00
|
|
|
for(var i in products){
|
|
|
|
if( products[i] && products[i].ean13 === ean13){
|
2012-08-13 16:00:26 +00:00
|
|
|
return products[i];
|
2012-08-07 16:18:44 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-13 16:00:26 +00:00
|
|
|
return undefined;
|
2012-08-07 16:18:44 +00:00
|
|
|
},
|
2012-08-13 16:00:26 +00:00
|
|
|
get_product_by_category: function(category_id){
|
|
|
|
var stored_categories = this._load_categories();
|
|
|
|
var stored_products = this._load_products();
|
2012-08-09 17:21:13 +00:00
|
|
|
var product_ids = stored_categories[category_id];
|
2012-08-07 16:18:44 +00:00
|
|
|
var list = [];
|
2012-08-10 13:58:32 +00:00
|
|
|
for(var i = 0, len = Math.min(product_ids.length,this.limit); i < len; i++){
|
2012-08-09 17:21:13 +00:00
|
|
|
list.push(stored_products[product_ids[i]]);
|
2012-08-07 16:18:44 +00:00
|
|
|
}
|
2012-08-13 16:00:26 +00:00
|
|
|
return list;
|
2012-08-07 16:18:44 +00:00
|
|
|
},
|
2012-08-10 13:58:32 +00:00
|
|
|
/* returns as a parameter of the result_callback function a list of products with :
|
|
|
|
* - a category that is or is a child of category_id,
|
|
|
|
* - a field in fields that contains a value that contains the query
|
|
|
|
* If a search is started before the previous has returned, the previous search may be cancelled
|
|
|
|
* (and the corresponding result_callback never called)
|
|
|
|
*/
|
2012-08-13 16:00:26 +00:00
|
|
|
search_product_in_category: function(category_id, fields, query){
|
2012-08-09 17:21:13 +00:00
|
|
|
var self = this;
|
2012-08-13 16:00:26 +00:00
|
|
|
var stored_categories = this._load_categories();
|
|
|
|
var stored_products = this._load_products();
|
2012-08-10 13:58:32 +00:00
|
|
|
var product_ids = stored_categories[category_id];
|
|
|
|
var list = [];
|
|
|
|
var count = 0;
|
|
|
|
|
|
|
|
query = query.toString().toLowerCase();
|
2012-08-07 16:18:44 +00:00
|
|
|
|
2012-08-10 13:58:32 +00:00
|
|
|
if(!(fields instanceof Array)){
|
|
|
|
fields = [fields];
|
|
|
|
}
|
|
|
|
for(var i = 0, len = product_ids.length; i < len && count < this.limit; i++){
|
|
|
|
var product = stored_products[product_ids[i]];
|
|
|
|
for(var j = 0, jlen = fields.length; j < jlen; j++){
|
|
|
|
var field = product[fields[j]];
|
|
|
|
if(field === null || field === undefined){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
field = field.toString().toLowerCase();
|
|
|
|
if(field.indexOf(query) != -1){
|
|
|
|
list.push(product);
|
|
|
|
count++;
|
|
|
|
break;
|
2012-08-07 16:18:44 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-10 13:58:32 +00:00
|
|
|
}
|
2012-08-13 16:00:26 +00:00
|
|
|
return list;
|
2012-08-07 16:18:44 +00:00
|
|
|
},
|
2012-08-10 13:58:32 +00:00
|
|
|
// TODO move the order storage from the crappy DAO in pos_models.js
|
2012-08-13 16:00:26 +00:00
|
|
|
add_order: function(order){
|
2012-08-09 17:21:13 +00:00
|
|
|
},
|
2012-08-13 16:00:26 +00:00
|
|
|
remove_order: function(order_id){
|
2012-08-07 16:18:44 +00:00
|
|
|
},
|
2012-08-13 16:00:26 +00:00
|
|
|
get_orders: function(){
|
2012-08-09 17:21:13 +00:00
|
|
|
},
|
2012-08-07 16:18:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
window.PosLS = module.PosLS;
|
|
|
|
}
|