Merge pull request #40594 from rohitwaghchaure/fixed-serial-batch-bundle-scr

fix: use serial/batch fields for subcontracting receipt and in transit in stock entry
diff --git a/erpnext/accounts/doctype/pricing_rule/utils.py b/erpnext/accounts/doctype/pricing_rule/utils.py
index 17293ad..73c5d59 100644
--- a/erpnext/accounts/doctype/pricing_rule/utils.py
+++ b/erpnext/accounts/doctype/pricing_rule/utils.py
@@ -736,7 +736,6 @@
 
 def validate_coupon_code(coupon_name):
 	coupon = frappe.get_doc("Coupon Code", coupon_name)
-
 	if coupon.valid_from:
 		if coupon.valid_from > getdate(today()):
 			frappe.throw(_("Sorry, this coupon code's validity has not started"))
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json
index 1d8983e..cffb097 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json
@@ -90,6 +90,7 @@
   "section_break_49",
   "apply_discount_on",
   "base_discount_amount",
+  "coupon_code",
   "is_cash_or_non_trade_discount",
   "additional_discount_account",
   "column_break_51",
@@ -2174,13 +2175,20 @@
    "no_copy": 1
   },
   {
+   "fieldname": "coupon_code",
+   "fieldtype": "Link",
+   "label": "Coupon Code",
+   "options": "Coupon Code"
+  },
+  {
    "default": "1",
    "depends_on": "eval: doc.is_return && doc.return_against",
    "description": "Credit Note will update it's own outstanding amount, even if \"Return Against\" is specified.",
    "fieldname": "update_outstanding_for_self",
    "fieldtype": "Check",
    "label": "Update Outstanding for Self",
-   "no_copy": 1
+   "no_copy": 1,
+   "print_hide": 1
   }
  ],
  "icon": "fa fa-file-text",
@@ -2193,7 +2201,7 @@
    "link_fieldname": "consolidated_invoice"
   }
  ],
- "modified": "2024-03-20 16:02:52.237732",
+ "modified": "2024-03-22 17:50:34.395602",
  "modified_by": "Administrator",
  "module": "Accounts",
  "name": "Sales Invoice",
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 1228bbb..aa0048d 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -16,6 +16,10 @@
 	get_loyalty_program_details_with_points,
 	validate_loyalty_points,
 )
+from erpnext.accounts.doctype.pricing_rule.utils import (
+	update_coupon_code_count,
+	validate_coupon_code,
+)
 from erpnext.accounts.doctype.repost_accounting_ledger.repost_accounting_ledger import (
 	validate_docs_for_deferred_accounting,
 	validate_docs_for_voucher_types,
@@ -106,6 +110,7 @@
 		contact_person: DF.Link | None
 		conversion_rate: DF.Float
 		cost_center: DF.Link | None
+		coupon_code: DF.Link | None
 		currency: DF.Link
 		customer: DF.Link | None
 		customer_address: DF.Link | None
@@ -294,6 +299,10 @@
 			self.doctype, self.customer, self.company, self.inter_company_invoice_reference
 		)
 
+		# Validating coupon code
+		if self.coupon_code:
+			validate_coupon_code(self.coupon_code)
+
 		if cint(self.is_pos):
 			self.validate_pos()
 
@@ -473,6 +482,9 @@
 			self.update_project()
 		update_linked_doc(self.doctype, self.name, self.inter_company_invoice_reference)
 
+		if self.coupon_code:
+			update_coupon_code_count(self.coupon_code, "used")
+
 		# create the loyalty point ledger entry if the customer is enrolled in any loyalty program
 		if (
 			not self.is_return
@@ -563,6 +575,9 @@
 		self.db_set("status", "Cancelled")
 		self.db_set("repost_required", 0)
 
+		if self.coupon_code:
+			update_coupon_code_count(self.coupon_code, "cancelled")
+
 		if (
 			frappe.db.get_single_value("Selling Settings", "sales_update_frequency") == "Each Transaction"
 		):
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index 15dfc36..7c2c439 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -356,7 +356,7 @@
 erpnext.patches.v15_0.create_advance_payment_status
 erpnext.patches.v15_0.allow_on_submit_dimensions_for_repostable_doctypes
 erpnext.patches.v14_0.create_accounting_dimensions_in_reconciliation_tool
-erpnext.patches.v14_0.update_flag_for_return_invoices
+erpnext.patches.v14_0.update_flag_for_return_invoices #2024-03-22
 # below migration patch should always run last
 erpnext.patches.v14_0.migrate_gl_to_payment_ledger
 erpnext.stock.doctype.delivery_note.patches.drop_unused_return_against_index # 2023-12-20
diff --git a/erpnext/patches/v14_0/update_flag_for_return_invoices.py b/erpnext/patches/v14_0/update_flag_for_return_invoices.py
index feb43be..bea9957 100644
--- a/erpnext/patches/v14_0/update_flag_for_return_invoices.py
+++ b/erpnext/patches/v14_0/update_flag_for_return_invoices.py
@@ -12,6 +12,10 @@
 	creation_date = "2024-01-25"
 
 	si = qb.DocType("Sales Invoice")
+
+	# unset flag, as migration would have set it for all records, as the field was introduced with default '1'
+	qb.update(si).set(si.update_outstanding_for_self, False).run()
+
 	if cr_notes := (
 		qb.from_(si)
 		.select(si.name)
@@ -37,6 +41,10 @@
 			).run()
 
 	pi = qb.DocType("Purchase Invoice")
+
+	# unset flag, as migration would have set it for all records, as the field was introduced with default '1'
+	qb.update(pi).set(pi.update_outstanding_for_self, False).run()
+
 	if dr_notes := (
 		qb.from_(pi)
 		.select(pi.name)