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 c8f14220c1..78a20e8cb4 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 @@ -83,19 +83,17 @@ class POSInvoiceMergeLog(Document): pos_invoice_docs = [ frappe.get_cached_doc("POS Invoice", d.pos_invoice) for d in self.pos_invoices ] + batched_invoices = self.get_batched_invoices(pos_invoice_docs) - returns = [d for d in pos_invoice_docs if d.get("is_return") == 1] - sales = [d for d in pos_invoice_docs if d.get("is_return") == 0] + for invoice in batched_invoices: + sales_invoice, credit_note = "", "" + if not invoice[0].get("is_return"): + sales_invoice = self.process_merging_into_sales_invoice(invoice) + else: + credit_note = self.process_merging_into_credit_note(invoice) - sales_invoice, credit_note = "", "" - if returns: - credit_note = self.process_merging_into_credit_note(returns) - - if sales: - sales_invoice = self.process_merging_into_sales_invoice(sales) - - self.save() # save consolidated_sales_invoice & consolidated_credit_note ref in merge log - self.update_pos_invoices(pos_invoice_docs, sales_invoice, credit_note) + self.save() # save consolidated_sales_invoice & consolidated_credit_note ref in merge log + self.update_pos_invoices(pos_invoice_docs, sales_invoice, credit_note) def on_cancel(self): pos_invoice_docs = [ @@ -273,6 +271,21 @@ class POSInvoiceMergeLog(Document): si.flags.ignore_validate = True si.cancel() + def get_batched_invoices(self, pos_invoice_docs): + grouped_batch = [] + current_batch = [] + for item in pos_invoice_docs: + if not current_batch: + current_batch.append(item) + elif current_batch[-1].get("is_return") != item.get("is_return"): + grouped_batch.append(current_batch) + current_batch = [item] + else: + current_batch.append(item) + + grouped_batch.append(current_batch) + return grouped_batch + def update_item_wise_tax_detail(consolidate_tax_row, tax_row): consolidated_tax_detail = json.loads(consolidate_tax_row.item_wise_tax_detail)