diff --git a/erpnext/accounts/doctype/pos_invoice_merge_log/pos_invoice_merge_log.py b/erpnext/accounts/doctype/pos_invoice_merge_log/pos_invoice_merge_log.py index 5bb43b3fa72..b9bf145c728 100644 --- a/erpnext/accounts/doctype/pos_invoice_merge_log/pos_invoice_merge_log.py +++ b/erpnext/accounts/doctype/pos_invoice_merge_log/pos_invoice_merge_log.py @@ -132,6 +132,7 @@ class POSInvoiceMergeLog(Document): self.update_pos_invoices(pos_invoice_docs) self.serial_and_batch_bundle_reference_for_pos_invoice() self.cancel_linked_invoices() + self.delink_serial_and_batch_bundle() def process_merging_into_sales_invoice(self, data): sales_invoice = self.get_new_sales_invoice() @@ -319,6 +320,38 @@ class POSInvoiceMergeLog(Document): for table_name in ["items", "packed_items"]: pos_invoice.set_serial_and_batch_bundle(table_name) + def delink_serial_and_batch_bundle(self): + bundles = self.get_serial_and_batch_bundles() + if not bundles: + return + + sle_table = frappe.qb.DocType("Stock Ledger Entry") + query = ( + frappe.qb.update(sle_table) + .set(sle_table.serial_and_batch_bundle, None) + .where(sle_table.serial_and_batch_bundle.isin(bundles) & sle_table.is_cancelled == 1) + ) + + query.run() + + def get_serial_and_batch_bundles(self): + pos_invoices = [] + for d in self.pos_invoices: + pos_invoices.append(d.pos_invoice) + + if pos_invoices: + return frappe.get_all( + "POS Invoice Item", + filters={ + "docstatus": 1, + "parent": ["in", pos_invoices], + "serial_and_batch_bundle": ["is", "set"], + }, + pluck="serial_and_batch_bundle", + ) + + return [] + def cancel_linked_invoices(self): for si_name in [self.consolidated_invoice, self.consolidated_credit_note]: if not si_name: @@ -503,6 +536,9 @@ def cancel_merge_logs(merge_logs, closing_entry=None): try: for log in merge_logs: merge_log = frappe.get_doc("POS Invoice Merge Log", log) + if merge_log.docstatus == 2: + continue + merge_log.flags.ignore_permissions = True merge_log.cancel() diff --git a/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py b/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py index 30538049e6c..f96a6760bec 100644 --- a/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py +++ b/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py @@ -519,6 +519,8 @@ class SerialandBatchBundle(Document): if not self.voucher_no or self.voucher_no != row.parent: values_to_set["voucher_no"] = row.parent + self.db_set("is_cancelled", 0) + if self.voucher_type != parent.doctype: values_to_set["voucher_type"] = parent.doctype