Try ERPNext Buy Support Partners Foundation

Getting current POS Profile to populate Sales Invoice

Hi:

I would like to automatically fill the “Sales Partner” and “Sales Person” fields for the Invoice when using POS based on a configuration.

I’m thinking about this solution:

  1. Adding the custom fields “Sales Partner” and “Sales Person” to the POS Profile, being those “Links” to their respective Document Types. This way seems to nicely cover my use case.

  2. When the user is placing a sale on the POS, the “Sale Invoice” form could take those POS Profile fields and fetch them to update its own “Sales Partner” and “Sales Person” fields.

The first part was not a problem. However, for the second part, as I understand it, there is no relation between the “Sales Invoice” document and the POS Profile. So I guess that’s why I can’t just invoke something like: posprofile.sales_partner inside the “Sales Invoice”.

So in short, I’m not sure how to get the current POS Profile for the current user and I appreciate any of your help on this.

Thank you!

For the record, the solution I found myself:

The following goes in a custom script for the DocType Sales Invoice

frappe.ui.form.on('Sales Invoice', 'validate', function(frm) {
    console.log('We will update Sales Person and Sales Team on validation from the custom fields at POS Profile')
    if(frm.doc.pos_profile) {
        // 	We'll work with the POS Profile object
    	frappe.model.with_doc('POS Profile', frm.doc.pos_profile, function() { 
    		var temporal_pos_profile_selected = frappe.model.get_doc("POS Profile", frm.doc.pos_profile)
    	    // 1) We'll update the Invoice Sales Partner only if the POS Profile has a Sales Partner
    	    // and there is no previous info on it
    	    if( (temporal_pos_profile_selected.xsales_partner) && !(frm.doc.sales_partner) ) {
    	       frm.doc.sales_partner = temporal_pos_profile_selected.xsales_partner
    	       console.log('Sales Partner changed')
    	    }
    	    // 2) We'll append the Sales Team on the Invoice only if the POS Profile has a Sales Person
    	    // and there is no previous info on it    	    
    	    if( (temporal_pos_profile_selected.xsales_person) && !(frm.doc.sales_team) ) {
    	        // Insert a row
    	        var childTable = cur_frm.add_child("sales_team");
    	        // Update the row
                childTable.sales_person = temporal_pos_profile_selected.xsales_person
                childTable.allocated_percentage = 100             
                cur_frm.refresh_fields("sales_team");
                console.log('Changed TEAM')
    	    }
    	});
    } else {
        console.log('The Invoice does not have a POS Profile')
    }
})

It seems the problem I originally had was that I was expecting the field pos_profile being valid when the field is_post was evaluated.

I found that’s not the case. I guess it takes a while to fetch the POS Profile when Is POS is triggered. The solution presented here uses the ‘validate’ “trigger”, so at that time the POS Profile is expected to be fetched.