POS Selecting Batch

I been trying to hack the current POS so as to be able to select the appropriate batch than just give error

Here is what I did. I replaced the frappe.throw in mandatiry_batch_no() with a frappe.prompt displaying a selection box with the possible batch nos. The prompt box appears, but the item gets added even before I select the batch. When I was expecting the flow to pause wait for my input and proceed. What is it that I am doing wrong?

mandatory_batch_no: function () {
            var me = this;
            if (this.items[0].has_batch_no && !this.item_batch_no[this.items[0].item_code]) {
                frappe.prompt([
             {'fieldname': 'batch', 'fieldtype': 'Select', 'label': 'Batch No', 'reqd': 1, 'options':this.batch_no_data[this.items[0].item_code]}
                ],
            function(values){
             
             me.item_batch_no[me.items[0].item_code] = values.batch;
                },
            'Select Batch No'
                )
            }}

Some love here too guys :smiley:

May this will help :slight_smile:

frappe.ui.form.on("Sales Invoice", "popup",
            function() {
var dialog = new frappe.ui.Dialog({
  title: __("Set as Lost"),
  fields: [
    {"fieldtype": "Text", "label": __("Reason for losing"), "fieldname": "reason",
      "reqd": 1 },
    {"fieldtype": "Button", "label": __("Update"), "fieldname": "update"},
  ]
});

dialog.fields_dict.update.$input.click(function() {
  args = dialog.get_values();
  if(!args) return;
  return cur_frm.call({
    doc: cur_frm.doc,
    method: "declare_enquiry_lost",
    args: args.reason,
    callback: function(r) {
      if(r.exc) {
        msgprint(__("There were errors."));
      } else {
        dialog.hide();
        cur_frm.refresh();
      }
    },
    btn: this
  })
});
dialog.show();
});

Ty, but i am not sure what I should try here.

Could you tell me what is this or when it should be triggered for my testing

@Mohammed_Redha ty for the help! but I managed to get desired result by rerouting the flow of the code. Now i can select batch in pos instead of just showing mandatory error.

Hi @vivek,

Could you please tell us what exactly what you changed so we can reproduce it ?

To which doctype did you link your script ? The sales invoice ?

I’m surprised only a few people are annoyed by this issue…

Anyway, I hope with your help, I will try to make a correction and send a pull request.

Thanks a lot in advance!

1 Like

Ok got it, you changed directly pos.js…

It works indeed, but there is an issue when the stock level is at 0.

I selected a batch N° with 0 quantity and got the following issue:

Row #1: The batch cdr2011 has only 0.0 qty. Please select another batch which has 1.0 qty available or split the row into multiple rows, to deliver/issue from multiple batches

The problem is that the issue is displayed only after the submission of the invoice and blocks the synchronisation with all subsequent invoices.

1 Like

Would be great if it merges to core…

Hi @mdwala,

I am currently testing it and will submit a PR tomorrow.

1 Like

@chdecultot I made modifications on several files to achieve this.

Namely

  1. pos.js - create a dialog window to pick the batch. - check add_to_cart() & select_batch() - modifications to handle same item_code different batch_no as selection of item was based on item_code ; auto add the closest expiring batch. In food and medicine industry sometimes there is batch price- a crude implementation of this via custom field in batch doctype called batch price is also added. If there is batch price it will get pulled else price list rate is used. Stock qty shows sum of available batch qty in selected store.
  2. select_batch.html - defines how the window looks - might want to change visuals
  3. pos.py - get_batch_data() - modified to send expiry_date & batch_qty specific qty, batch price
  4. pos.html - added batch field removed discount , added change batch button
  5. pos_bill_item_new.html - added batch field - shows in red if there is a batch price available
  6. pos_selected_item.html - added batch field
  7. build.json - added select_batch.html
  8. added minified file to hooks.py

Check my repo

I been just horribly busy to clean the code and try for a PR.

2 Likes

@vivek, Wow your solution looks very nice!

I have only implemented and pushed a PR for the initial solution you proposed. It works fine but is very basic.
If I have some time I will try to enhance it with another PR based on your repo solution if you don’t have time yourself.

Thanks a lot, that’s a nice job!

Hi did you push this to the Core ? As i don’t see the option on POS to change the Batch ?

Also to advise that when no Batch is selected on POS by the user… a default is selected instead but the Available batch qtd in warehouse is not set resulting on errors later like this

Traceback (most recent call last):
File “/home/frappe/frappe-bench/apps/erpnext/erpnext/accounts/doctype/sales_invoice/pos.py”, line 497, in save_invoice
si_doc.insert()
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 192, in insert
self.run_before_save_methods()
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 772, in run_before_save_methods
self.run_method(“validate”)
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 666, in run_method
out = Document.hook(fn)(self, *args, **kwargs)
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 887, in composer
return composed(self, method, *args, **kwargs)
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 870, in runner
add_to_return_value(self, fn(self, *args, **kwargs))
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 660, in
fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)
File “/home/frappe/frappe-bench/apps/erpnext/erpnext/accounts/doctype/sales_invoice/sales_invoice.py”, line 90, in validate
set_batch_nos(self, ‘warehouse’, True)
File “/home/frappe/frappe-bench/apps/erpnext/erpnext/stock/doctype/batch/batch.py”, line 110, in set_batch_nos
d.batch_no = get_batch_no(d.item_code, warehouse, d.qty, throw)
File “/home/frappe/frappe-bench/apps/erpnext/erpnext/stock/doctype/batch/batch.py”, line 131, in get_batch_no
if throw: raise UnableToSelectBatchError
UnableToSelectBatchError

The same happens on SI … if the user does not select the Batch the Available Qtd is not updated…

Hi @Helio_Jesus,

Yes I pushed it to the Core, but since the POS has been fully refactored and I don’t know if this has been updated or not.

I need to make some tests.

Thanks but what about this Error that i have reported on Batch !!!

Seems that some Items POS is not selecting the Batch and adding the available Qtd on Batch …

Traceback (most recent call last):
File “/home/frappe/frappe-bench/apps/erpnext/erpnext/accounts/doctype/sales_invoice/pos.py”, line 482, in submit_invoice
si_doc.insert()
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 218, in insert
self.run_before_save_methods()
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 808, in run_before_save_methods
self.run_method(“validate”)
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 702, in run_method
out = Document.hook(fn)(self, *args, **kwargs)
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 965, in composer
return composed(self, method, *args, **kwargs)
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 948, in runner
add_to_return_value(self, fn(self, *args, **kwargs))
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 696, in
fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)
File “/home/frappe/frappe-bench/apps/erpnext/erpnext/accounts/doctype/sales_invoice/sales_invoice.py”, line 91, in validate
set_batch_nos(self, ‘warehouse’, True)
File “/home/frappe/frappe-bench/apps/erpnext/erpnext/stock/doctype/batch/batch.py”, line 111, in set_batch_nos
d.batch_no = get_batch_no(d.item_code, warehouse, qty, throw)
File “/home/frappe/frappe-bench/apps/erpnext/erpnext/stock/doctype/batch/batch.py”, line 132, in get_batch_no
if throw: raise UnableToSelectBatchError
UnableToSelectBatchError