Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Auto Select Batch Feature in Stock Settings and Delivery Notes, Add coa_batch field in batch doctype #1162

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion erpnext/compliance/doctype/package_tag/package_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def validate(self):
self.validate_source_package_tag()
self.update_coa_batch_no()
self.calculate_package_tag_qty()
self.set_coa_batch_in_batch_no()

def validate_source_package_tag(self):
source_package_tag = frappe.db.get_value("Package Tag", self.source_package_tag, "source_package_tag")
Expand All @@ -26,6 +27,13 @@ def update_coa_batch_no(self):
def calculate_package_tag_qty(self):
self.package_tag_qty = frappe.db.get_value("Stock Ledger Entry", {"docstatus": 1, "package_tag": self.package_tag}, "sum(actual_qty)")

def set_coa_batch_in_batch_no(self):
"""
Assigns the coa_batch_no to the batch specified in the package tag.
"""
if self.batch_no and self.coa_batch_no:
frappe.db.set_value("Batch", self.batch_no, "coa_batch", self.coa_batch_no)

@frappe.whitelist()
def get_package_tag_qty(package_tag=None):
out = 0
Expand Down Expand Up @@ -70,7 +78,7 @@ def set_missing_values(source, target):
"item_group": source.item_group,
"batch_no": source.batch_no
})

doc = get_mapped_doc("Package Tag", source_name, {
"Package Tag": {
"doctype": "Waste Disposal",
Expand Down
1 change: 1 addition & 0 deletions erpnext/patches.txt
Original file line number Diff line number Diff line change
Expand Up @@ -718,4 +718,5 @@ erpnext.patches.v13_0.delete_bank_reconciliation_doctype
erpnext.patches.v13_0.make_mobile_application_access_roles #2021-04-08
execute:frappe.reload_doc('setup', 'doctype', 'company', force=True)
erpnext.patches.v13_0.set_is_used #01-04-2021
erpnext.patches.v13_0.set_coa_batch_in_batch
erpnext.patches.v13_0.delete_company_custom_fields
12 changes: 12 additions & 0 deletions erpnext/patches/v13_0/set_coa_batch_in_batch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import frappe

def execute():
"""
Set coa batch value in batch document if batch and coa batch is set in package tag
"""
frappe.reload_doc("stock", "doctype", "batch")
package_tags = frappe.get_all("Package Tag",
filters = {'batch_no': ["is", "set"], "coa_batch_no": ["is", "set"]},
fields = ['batch_no', "coa_batch_no"])
for tag in package_tags:
frappe.db.set_value("Batch", tag.batch_no, "coa_batch", tag.coa_batch)
10 changes: 8 additions & 2 deletions erpnext/stock/doctype/batch/batch.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
"parent_batch",
"manufacturing_date",
"display_on_website",
"column_break_3",
"batch_qty",
"stock_uom",
"column_break_3",
"coa_batch",
"expiry_date",
"harvest_date",
"packaged_date",
Expand Down Expand Up @@ -220,13 +221,18 @@
"label": "Customer",
"options": "Customer",
"read_only": 1
},
{
"fieldname": "coa_batch",
"fieldtype": "Data",
"label": "COA Batch"
}
],
"icon": "fa fa-archive",
"idx": 1,
"image_field": "image",
"max_attachments": 5,
"modified": "2021-03-18 00:20:52.920465",
"modified": "2021-03-23 18:06:00.946525",
"modified_by": "Administrator",
"module": "Stock",
"name": "Batch",
Expand Down
17 changes: 17 additions & 0 deletions erpnext/stock/doctype/delivery_note/delivery_note.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def validate(self):
self.validate_uom_is_integer("uom", "qty")
self.validate_with_previous_doc()
self.validate_batch_coa()
self.update_item_batch_nos()

if self._action != 'submit' and not self.is_return:
set_batch_nos(self, 'warehouse', True)
Expand Down Expand Up @@ -162,6 +163,22 @@ def validate_batch_coa(self):
if item.coa_batch:
item.certificate_of_analysis = frappe.db.get_value("Batch", item.coa_batch, "certificate_of_analysis")

def update_item_batch_nos(self):
"""
Add batch nos to items that have coa batch assigned to them.
"""
auto_assign_batch_during_delivery = frappe.db.get_value("Stock Settings", None, "auto_assign_batch_during_delivery")
for item in self.items:
#If item has a coa batch and auto assigning batches are enabled in stock settings, we assign a batch to the item in the delivery note
if auto_assign_batch_during_delivery and item.coa_batch and not item.batch_no:
#fetch batch with same coa_batch and map quantity
valid_batches = frappe.get_all("Batch",
filters = {"batch_qty":[">", item.qty], "coa_batch": item.coa_batch}
limit=1
)
if valid_batches:
item.batch_no = valid_batches[0]

def validate_proj_cust(self):
"""check for does customer belong to same project as entered.."""
if self.project and self.customer:
Expand Down
9 changes: 8 additions & 1 deletion erpnext/stock/doctype/stock_settings/stock_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"column_break_10",
"automatically_set_serial_nos_based_on_fifo",
"automatically_set_batch_nos_based_on_fifo",
"auto_assign_batch_during_delivery",
"set_qty_in_transactions_based_on_serial_no_input",
"auto_material_request",
"auto_indent",
Expand Down Expand Up @@ -218,12 +219,18 @@
"fieldname": "no_of_days_to_calculate_avg_manufactured_qty",
"fieldtype": "Int",
"label": "Number of Days (Past) to Calculate Average Manufactured Quantity "
},
{
"default": "0",
"fieldname": "auto_assign_batch_during_delivery",
"fieldtype": "Check",
"label": "Auto Assign Batch During Delivery"
}
],
"icon": "icon-cog",
"idx": 1,
"issingle": 1,
"modified": "2021-03-23 01:19:37.977835",
"modified": "2021-03-28 22:06:48.428711",
"modified_by": "Administrator",
"module": "Stock",
"name": "Stock Settings",
Expand Down