This question has been flagged
2 Replies
6246 Views

Hi everybody

At the moment I'm customizing the POS and I would love to know how to get the sequence_number.
I've figured out its already in the models.js file here:

{
            model:  'pos.session',
            fields: ['id', 'journal_ids','name','user_id','config_id','start_at','stop_at','sequence_number','login_number'],
            domain: function(self){ return [['state','=','opened'],['user_id','=',self.session.uid]]; },
            loaded: function(self,pos_sessions){
                self.pos_session = pos_sessions[0]; 

                var orders = self.db.get_orders();
                for (var i = 0; i < orders.length; i++) {
                    self.pos_session.sequence_number = Math.max(self.pos_session.sequence_number, orders[i].data.sequence_number+1);
                }
            },
        },

My question is now how can I access and use this id in another JS file? I would like to get the variable available in screens.js to keep track of session there.
Could anybody give me any tips or help me?

Thank you
Yenthe

Avatar
Discard
Author Best Answer

I've found the solution thanks to Shawn his ideas and information.
The idea is to make a global variable in Backbone.js so I did this in the file models.js

models.js:

    module.PosModel = Backbone.Model.extend({
    //Define a global variable right above the initialize. We'll fill this up later.
    sequentie_nummer: null,
        initialize: function(session, attributes) {
   //Lots of irrelevant code until the model pos.session that I need.
,{

            model:  'pos.session',
            fields: ['id', 'journal_ids','name','user_id','config_id','start_at','stop_at','sequence_number','login_number'],
            domain: function(self){ return [['state','=','opened'],['user_id','=',self.session.uid]]; },
            loaded: function(self,pos_sessions){
                self.pos_session = pos_sessions[0]; 

                var orders = self.db.get_orders();
                for (var i = 0; i < orders.length; i++) {
                    self.pos_session.sequence_number = Math.max(self.pos_session.sequence_number, orders[i].data.sequence_number+1);
                }
        alert('session: ' + self.pos_session.sequence_number);
        //Finally fill up the global variable
        sequentie_nummer = self.pos_session.sequence_number;
            },

Now we have the variable sequentie_nummer available in the whole Backbone (this means in any JS file for the POS). I now only needed to fetch the variable in my other JS file.

screens.js:

alert('Sequence number: ' + sequentie_nummer);

Reloading the module and then testing it did the job.

Avatar
Discard

@Yenthe , i want to do something similar to what you have achieved , i want to load the contracts module(account.analytic.account) so i can get the contract reference and display it on the receipt. The tricky part is the only way to link the product in the point of sale back to its to the contract is : product.product > account.analytic.invoice.line > account.analytic.account , so is there any way i can achieve this ?

Best Answer

Try logging the value of this object:

this.pos.attributes.pos_session.sequence_number;

Please let me know in where exactly in screens.js you intend to use this line, or if you are creating a new function for the same.

Couple of tips that I have found useful:

1) Almost all the details that you would require are present in the 'self' or 'this' object in javascript during point of sale execution. A good way to access this would be to use the command:

console.log(self);

,wherever you would like to use the contents.  Open the console in your browser (normally the shortcut key is Ctrl+Alt+J) , while in the POS session, and you will see the contents of self object. Hope this makes sense. Let me know if you have any queries - I will explain it in more detail.

2) Alternatively, you can use the local storage area of the browser to store any variables that you would like to use globally.

Simply use the following command to store and retrieve a localstorage object:

localStorage.setItem('object_name','value');

localStorage.getItem('object_name');

 

Hope this helps! I will try logging the self object and give you the exact code to retrieve the required value.

Avatar
Discard
Author

@Shawn thank you for the tips & pointers. I'd like to 'grab' the sequence_number in screens.js in the order_product: function() in the module.ScaleScreenWidget from screens.js so I know which sequence I'm in. Could you help me any further in this?

By 'grab', I assume you want to obtain the value somehow. The order_product function() in my module looks like this: order_product: function(){ var weight = this.pos.proxy.weighting_read_kg(); this.pos.get('selectedOrder').addProduct(this.get_product(),{ quantity:weight }); } Try adding the following line: console.log(this.pos.attributes.pos_session.sequence_number); So the modified function body should look like this: order_product: function(){ var weight = this.pos.proxy.weighting_read_kg(); this.pos.get('selectedOrder').addProduct(this.get_product(),{ quantity:weight }); console.log(this.pos.attributes.pos_session.sequence_number); } Let me know if this helps. By the way, I don't have a sequence_number variable in my models.js file. So this is a custom variable that you have added right?

Oops! Didn't know comments won't save with formatting. Let me know if you want me to email you the code instead.

Author

@Shawn no worries I still understood what you ment. Sadly the console.log does not work as pos_session is undefined. It gives me an Odoo client error.. (TypeError: this.pos.attributes.pos_session is undefined) so I guess you can't access it in screens.js? As for the sequence_number not showing up at your side, its in the file models.js under the model definitions. Any more ideas/tips about how I can obtain the value?

Author

@Shawn a little update. If you go to models.js and find this line: model: 'pos.session', you will see a function right under it loaded: function(self,pos_session). If you place an alert/log in it alert('session: ' + self.pos_session.sequence_number); it will print out the number I need. So how can I access this in my screens.js?

@Yenthe Thanks for that info. I had tried that, but I guess I have a different version of POS. Its not a problem anyway, as I have the other variables like 'start_at', 'stop_at' etc. Now, as for the console log, do you see the following line in your order_product function: var self=this; If you do, you might have to use 'self' instead of 'this' while logging. First, try using just console.log(this); or console.log(self); Let me know what output you get.

Author

@Shawn see my answer that I posted here, which is 95% of the solution. I now only face one more problem.. When I click to open a new session the variable stays the same number. Any clue where/how I can now re-populate my variable so it contains the correct number? Any time I switch from session (tab) in the POS I want to have the current sequence_number.

@Yenthe Yes using a global variable would achieve the result as well. Can you clarify what you are referring to as a 'new session'? Is it the 'New Session' button used to launch a POS interface? Or the '+' sign at the top, inside the pos interface which is used to start a new transaction?

Author

@Shawn I'm talking about the '+' sign at the top. Every time it is pressed I'd like to know which 'session' I'm in and when I switch from tab / click on the '-' sign I should also track this.

@Yenthe Oh ok... I will have to explore that myself as well. I will get back to you if I find something. Please do let me know in case you find a solution as well. Thanks.

Author

@Shawn will do! I'm trying things out at the moment but will post back when I find this. Thank you for helping and the information! I hope to hear back from you soon :)