[IMP] point_of_sale: automatically update partners data when they are modified

This commit is contained in:
Frederic van der Essen 2014-08-22 12:07:19 +02:00
parent ea7b440dc8
commit f1d4d8d86c
3 changed files with 88 additions and 24 deletions

View File

@ -21,10 +21,11 @@ function openerp_pos_db(instance, module){
this.product_by_category_id = {};
this.product_by_reference = {};
this.partners_sorted = [];
this.partner_sorted = [];
this.partner_by_id = {};
this.partner_by_ean13 = {};
this.partner_search_string = "";
this.partner_write_date = null;
this.category_by_id = {};
this.root_category_id = 0;
@ -218,19 +219,55 @@ function openerp_pos_db(instance, module){
return str + '\n';
},
add_partners: function(partners){
var updated_count = 0;
for(var i = 0, len = partners.length; i < len; i++){
var partner = partners[i];
this.partner_by_id[partner.id] = partner;
if(partner.ean13){
this.partner_by_ean13[partner.ean13] = partner;
if (!this.partner_write_date) {
this.partner_write_date = partner.write_date;
} else if ( this.partner_by_id[partner.id] &&
new Date(this.partner_write_date).getTime() + 1000 >=
new Date(partner.write_date).getTime() ) {
// FIXME: The write_date is stored with milisec precision in the database
// but the dates we get back are only precise to the second. This means when
// you read partners modified strictly after time X, you get back partners that were
// modified X - 1 sec ago.
continue;
} else if ( this.partner_write_date < partner.write_date ) {
this.partner_write_date = partner.write_date;
}
partner.address = (partner.street || '') +', '+
(partner.zip || '') +' '+
(partner.city || '') +', '+
(partner.country_id[1] || '');
this.partner_search_string += this._partner_search_string(partner);
this.partners_sorted.push(partner);
if (!this.partner_by_id[partner.id]) {
this.partner_sorted.push(partner.id);
}
this.partner_by_id[partner.id] = partner;
updated_count += 1;
}
if (updated_count) {
// If there were updates, we need to completely
// rebuild the search string and the ean13 indexing
this.partner_search_string = "";
this.partner_by_ean13 = {};
for (var id in this.partner_by_id) {
var partner = this.partner_by_id[id];
if(partner.ean13){
this.partner_by_ean13[partner.ean13] = partner;
}
partner.address = (partner.street || '') +', '+
(partner.zip || '') +' '+
(partner.city || '') +', '+
(partner.country_id[1] || '');
this.partner_search_string += this._partner_search_string(partner);
}
}
return updated_count;
},
get_partner_write_date: function(){
return this.partner_write_date;
},
get_partner_by_id: function(id){
return this.partner_by_id[id];
@ -238,8 +275,13 @@ function openerp_pos_db(instance, module){
get_partner_by_ean13: function(ean13){
return this.partner_by_ean13[ean13];
},
get_partners_sorted: function(){
return this.partners_sorted;
get_partners_sorted: function(max_count){
max_count = max_count ? Math.min(this.partner_sorted.length, max_count) : this.partner_sorted.length;
var partners = [];
for (var i = 0; i < max_count; i++) {
partners.push(this.partner_by_id[this.partner_sorted[i]]);
}
return partners;
},
search_partner: function(query){
try {

View File

@ -159,7 +159,7 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal
loaded: function(self,users){ self.users = users; },
},{
model: 'res.partner',
fields: ['name','street','city','country_id','phone','zip','mobile','email','ean13'],
fields: ['name','street','city','country_id','phone','zip','mobile','email','ean13','write_date'],
domain: null,
loaded: function(self,partners){
self.partners = partners;
@ -390,6 +390,26 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal
return loaded;
},
// reload the list of partner, returns as a deferred that resolves if there were
// updated partners, and fails if not
load_new_partners: function(){
var self = this;
var def = new $.Deferred();
var fields = _.find(this.models,function(model){ return model.model === 'res.partner'; }).fields;
new instance.web.Model('res.partner')
.query(fields)
.filter([['write_date','>',this.db.get_partner_write_date()]])
.all({'timeout':3000, 'shadow': true})
.then(function(partners){
if (self.db.add_partners(partners)) { // check if the partners we got were real updates
def.resolve();
} else {
def.reject();
}
}, function(){ def.reject(); });
return def;
},
// 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,index,reason){

View File

@ -583,7 +583,6 @@ function openerp_pos_screens(instance, module){ //module is instance.point_of_sa
init: function(parent, options){
this._super(parent, options);
this.partner_cache = new module.DomCache();
},
show_leftpane: false,
@ -608,8 +607,13 @@ function openerp_pos_screens(instance, module){ //module is instance.point_of_sa
self.pos_widget.screen_selector.back();
});
var partners = this.pos.db.get_partners_sorted();
var partners = this.pos.db.get_partners_sorted(1000);
this.render_list(partners);
this.pos.load_new_partners().then(function(){
// will only get called if new partners were reloaded.
self.render_list(self.pos.db.get_partners_sorted(1000));
});
if( this.old_client ){
this.display_client_details('show',this.old_client,0);
@ -650,7 +654,7 @@ function openerp_pos_screens(instance, module){ //module is instance.point_of_sa
}
},
clear_search: function(){
var customers = this.pos.db.get_partners_sorted();
var customers = this.pos.db.get_partners_sorted(1000);
this.render_list(customers);
this.$('.searchbox input')[0].value = '';
this.$('.searchbox input').focus();
@ -660,19 +664,17 @@ function openerp_pos_screens(instance, module){ //module is instance.point_of_sa
contents.innerHTML = "";
for(var i = 0, len = Math.min(partners.length,1000); i < len; i++){
var partner = partners[i];
var clientline = this.partner_cache.get_node(partner.id);
if(!clientline){
var clientline_html = QWeb.render('ClientLine',{widget: this, partner:partners[i]});
var clientline = document.createElement('tbody');
clientline.innerHTML = clientline_html;
clientline = clientline.childNodes[1];
this.partner_cache.cache_node(partner.id,clientline);
}
var clientline_html = QWeb.render('ClientLine',{widget: this, partner:partners[i]});
var clientline = document.createElement('tbody');
clientline.innerHTML = clientline_html;
clientline = clientline.childNodes[1];
if( partners === this.new_client ){
clientline.classList.add('highlight');
}else{
clientline.classList.remove('highlight');
}
contents.appendChild(clientline);
}
},