Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 1 | from collections import defaultdict |
| 2 | from typing import List |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 3 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 4 | import frappe |
| 5 | from frappe import _, bold |
| 6 | from frappe.model.naming import make_autoname |
Rohit Waghchaure | 86da306 | 2023-03-20 14:15:34 +0530 | [diff] [blame] | 7 | from frappe.query_builder.functions import CombineDatetime, Sum |
Rohit Waghchaure | f968f0f | 2023-06-14 23:22:22 +0530 | [diff] [blame] | 8 | from frappe.utils import cint, flt, get_link_to_form, now, nowtime, today |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 9 | |
| 10 | from erpnext.stock.deprecated_serial_batch import ( |
| 11 | DeprecatedBatchNoValuation, |
| 12 | DeprecatedSerialNoValuation, |
| 13 | ) |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 14 | from erpnext.stock.valuation import round_off_if_near_zero |
| 15 | |
| 16 | |
| 17 | class SerialBatchBundle: |
| 18 | def __init__(self, **kwargs): |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 19 | for key, value in kwargs.items(): |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 20 | setattr(self, key, value) |
| 21 | |
| 22 | self.set_item_details() |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 23 | self.process_serial_and_batch_bundle() |
| 24 | if self.sle.is_cancelled: |
| 25 | self.delink_serial_and_batch_bundle() |
| 26 | |
| 27 | self.post_process() |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 28 | |
| 29 | def process_serial_and_batch_bundle(self): |
| 30 | if self.item_details.has_serial_no: |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 31 | self.process_serial_no() |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 32 | elif self.item_details.has_batch_no: |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 33 | self.process_batch_no() |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 34 | |
| 35 | def set_item_details(self): |
| 36 | fields = [ |
| 37 | "has_batch_no", |
| 38 | "has_serial_no", |
| 39 | "item_name", |
| 40 | "item_group", |
| 41 | "serial_no_series", |
| 42 | "create_new_batch", |
| 43 | "batch_number_series", |
| 44 | ] |
| 45 | |
| 46 | self.item_details = frappe.get_cached_value("Item", self.sle.item_code, fields, as_dict=1) |
| 47 | |
| 48 | def process_serial_no(self): |
| 49 | if ( |
| 50 | not self.sle.is_cancelled |
| 51 | and not self.sle.serial_and_batch_bundle |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 52 | and self.item_details.has_serial_no == 1 |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 53 | ): |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 54 | self.make_serial_batch_no_bundle() |
| 55 | elif not self.sle.is_cancelled: |
| 56 | self.validate_item_and_warehouse() |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 57 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 58 | def make_serial_batch_no_bundle(self): |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 59 | self.validate_item() |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 60 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 61 | sn_doc = SerialBatchCreation( |
| 62 | { |
| 63 | "item_code": self.item_code, |
| 64 | "warehouse": self.warehouse, |
| 65 | "posting_date": self.sle.posting_date, |
| 66 | "posting_time": self.sle.posting_time, |
| 67 | "voucher_type": self.sle.voucher_type, |
| 68 | "voucher_no": self.sle.voucher_no, |
| 69 | "voucher_detail_no": self.sle.voucher_detail_no, |
Rohit Waghchaure | c2d7461 | 2023-03-29 11:40:36 +0530 | [diff] [blame] | 70 | "qty": self.sle.actual_qty, |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 71 | "avg_rate": self.sle.incoming_rate, |
| 72 | "total_amount": flt(self.sle.actual_qty) * flt(self.sle.incoming_rate), |
| 73 | "type_of_transaction": "Inward" if self.sle.actual_qty > 0 else "Outward", |
| 74 | "company": self.company, |
| 75 | "is_rejected": self.is_rejected_entry(), |
| 76 | } |
| 77 | ).make_serial_and_batch_bundle() |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 78 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 79 | self.set_serial_and_batch_bundle(sn_doc) |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 80 | |
Rohit Waghchaure | 42b2294 | 2023-05-27 19:18:03 +0530 | [diff] [blame] | 81 | def validate_actual_qty(self, sn_doc): |
Rohit Waghchaure | f968f0f | 2023-06-14 23:22:22 +0530 | [diff] [blame] | 82 | link = get_link_to_form("Serial and Batch Bundle", sn_doc.name) |
| 83 | |
| 84 | condition = { |
| 85 | "Inward": self.sle.actual_qty > 0, |
| 86 | "Outward": self.sle.actual_qty < 0, |
| 87 | }.get(sn_doc.type_of_transaction) |
| 88 | |
| 89 | if not condition: |
| 90 | correct_type = "Inward" |
| 91 | if sn_doc.type_of_transaction == "Inward": |
| 92 | correct_type = "Outward" |
| 93 | |
| 94 | msg = f"The type of transaction of Serial and Batch Bundle {link} is {bold(sn_doc.type_of_transaction)} but as per the Actual Qty {self.sle.actual_qty} for the item {bold(self.sle.item_code)} in the {self.sle.voucher_type} {self.sle.voucher_no} the type of transaction should be {bold(correct_type)}" |
| 95 | frappe.throw(_(msg), title=_("Incorrect Type of Transaction")) |
| 96 | |
Rohit Waghchaure | 42b2294 | 2023-05-27 19:18:03 +0530 | [diff] [blame] | 97 | precision = sn_doc.precision("total_qty") |
| 98 | if flt(sn_doc.total_qty, precision) != flt(self.sle.actual_qty, precision): |
Rohit Waghchaure | f968f0f | 2023-06-14 23:22:22 +0530 | [diff] [blame] | 99 | msg = f"Total qty {flt(sn_doc.total_qty, precision)} of Serial and Batch Bundle {link} is not equal to Actual Qty {flt(self.sle.actual_qty, precision)} in the {self.sle.voucher_type} {self.sle.voucher_no}" |
Rohit Waghchaure | 42b2294 | 2023-05-27 19:18:03 +0530 | [diff] [blame] | 100 | frappe.throw(_(msg)) |
| 101 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 102 | def validate_item(self): |
| 103 | msg = "" |
| 104 | if self.sle.actual_qty > 0: |
| 105 | if not self.item_details.has_batch_no and not self.item_details.has_serial_no: |
| 106 | msg = f"Item {self.item_code} is not a batch or serial no item" |
| 107 | |
| 108 | if self.item_details.has_serial_no and not self.item_details.serial_no_series: |
| 109 | msg += f". If you want auto pick serial bundle, then kindly set Serial No Series in Item {self.item_code}" |
| 110 | |
| 111 | if ( |
| 112 | self.item_details.has_batch_no |
| 113 | and not self.item_details.batch_number_series |
| 114 | and not frappe.db.get_single_value("Stock Settings", "naming_series_prefix") |
| 115 | ): |
| 116 | msg += f". If you want auto pick batch bundle, then kindly set Batch Number Series in Item {self.item_code}" |
| 117 | |
| 118 | elif self.sle.actual_qty < 0: |
| 119 | if not frappe.db.get_single_value( |
| 120 | "Stock Settings", "auto_create_serial_and_batch_bundle_for_outward" |
| 121 | ): |
| 122 | msg += ". If you want auto pick serial/batch bundle, then kindly enable 'Auto Create Serial and Batch Bundle' in Stock Settings." |
| 123 | |
| 124 | if msg: |
| 125 | error_msg = ( |
| 126 | f"Serial and Batch Bundle not set for item {self.item_code} in warehouse {self.warehouse}." |
| 127 | + msg |
| 128 | ) |
| 129 | frappe.throw(_(error_msg)) |
| 130 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 131 | def set_serial_and_batch_bundle(self, sn_doc): |
| 132 | self.sle.db_set("serial_and_batch_bundle", sn_doc.name) |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 133 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 134 | if sn_doc.is_rejected: |
| 135 | frappe.db.set_value( |
| 136 | self.child_doctype, self.sle.voucher_detail_no, "rejected_serial_and_batch_bundle", sn_doc.name |
| 137 | ) |
| 138 | else: |
| 139 | frappe.db.set_value( |
| 140 | self.child_doctype, self.sle.voucher_detail_no, "serial_and_batch_bundle", sn_doc.name |
| 141 | ) |
| 142 | |
| 143 | @property |
| 144 | def child_doctype(self): |
| 145 | child_doctype = self.sle.voucher_type + " Item" |
| 146 | if self.sle.voucher_type == "Stock Entry": |
| 147 | child_doctype = "Stock Entry Detail" |
| 148 | |
Rohit Waghchaure | 26b39ac | 2023-04-06 01:36:18 +0530 | [diff] [blame] | 149 | if self.sle.voucher_type == "Asset Capitalization": |
| 150 | child_doctype = "Asset Capitalization Stock Item" |
| 151 | |
| 152 | if self.sle.voucher_type == "Asset Repair": |
| 153 | child_doctype = "Asset Repair Consumed Item" |
| 154 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 155 | return child_doctype |
| 156 | |
| 157 | def is_rejected_entry(self): |
| 158 | return is_rejected(self.sle.voucher_type, self.sle.voucher_detail_no, self.sle.warehouse) |
| 159 | |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 160 | def process_batch_no(self): |
| 161 | if ( |
| 162 | not self.sle.is_cancelled |
| 163 | and not self.sle.serial_and_batch_bundle |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 164 | and self.item_details.has_batch_no == 1 |
| 165 | and self.item_details.create_new_batch |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 166 | ): |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 167 | self.make_serial_batch_no_bundle() |
| 168 | elif not self.sle.is_cancelled: |
| 169 | self.validate_item_and_warehouse() |
| 170 | |
| 171 | def validate_item_and_warehouse(self): |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 172 | if self.sle.serial_and_batch_bundle and not frappe.db.exists( |
| 173 | "Serial and Batch Bundle", |
| 174 | { |
| 175 | "name": self.sle.serial_and_batch_bundle, |
| 176 | "item_code": self.item_code, |
| 177 | "warehouse": self.warehouse, |
| 178 | "voucher_no": self.sle.voucher_no, |
| 179 | }, |
| 180 | ): |
| 181 | msg = f""" |
| 182 | The Serial and Batch Bundle |
| 183 | {bold(self.sle.serial_and_batch_bundle)} |
| 184 | does not belong to Item {bold(self.item_code)} |
| 185 | or Warehouse {bold(self.warehouse)} |
| 186 | or {self.sle.voucher_type} no {bold(self.sle.voucher_no)} |
| 187 | """ |
| 188 | |
| 189 | frappe.throw(_(msg)) |
| 190 | |
| 191 | def delink_serial_and_batch_bundle(self): |
| 192 | update_values = { |
| 193 | "serial_and_batch_bundle": "", |
| 194 | } |
| 195 | |
| 196 | if is_rejected(self.sle.voucher_type, self.sle.voucher_detail_no, self.sle.warehouse): |
| 197 | update_values["rejected_serial_and_batch_bundle"] = "" |
| 198 | |
| 199 | frappe.db.set_value(self.child_doctype, self.sle.voucher_detail_no, update_values) |
| 200 | |
| 201 | frappe.db.set_value( |
| 202 | "Serial and Batch Bundle", |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 203 | {"voucher_no": self.sle.voucher_no, "voucher_type": self.sle.voucher_type}, |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 204 | {"is_cancelled": 1, "voucher_no": ""}, |
| 205 | ) |
| 206 | |
Rohit Waghchaure | f79f2a3 | 2023-04-04 11:50:38 +0530 | [diff] [blame] | 207 | if self.sle.serial_and_batch_bundle: |
| 208 | frappe.get_cached_doc( |
| 209 | "Serial and Batch Bundle", self.sle.serial_and_batch_bundle |
| 210 | ).validate_serial_and_batch_inventory() |
| 211 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 212 | def post_process(self): |
Rohit Waghchaure | 674bd3e | 2023-03-17 16:42:59 +0530 | [diff] [blame] | 213 | if not self.sle.serial_and_batch_bundle: |
| 214 | return |
| 215 | |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 216 | docstatus = frappe.get_cached_value( |
| 217 | "Serial and Batch Bundle", self.sle.serial_and_batch_bundle, "docstatus" |
| 218 | ) |
| 219 | |
| 220 | if docstatus != 1: |
| 221 | self.submit_serial_and_batch_bundle() |
| 222 | |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 223 | if self.item_details.has_serial_no == 1: |
| 224 | self.set_warehouse_and_status_in_serial_nos() |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 225 | |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 226 | if ( |
| 227 | self.sle.actual_qty > 0 |
| 228 | and self.item_details.has_serial_no == 1 |
| 229 | and self.item_details.has_batch_no == 1 |
| 230 | ): |
| 231 | self.set_batch_no_in_serial_nos() |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 232 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 233 | if self.item_details.has_batch_no == 1: |
| 234 | self.update_batch_qty() |
| 235 | |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 236 | def submit_serial_and_batch_bundle(self): |
| 237 | doc = frappe.get_doc("Serial and Batch Bundle", self.sle.serial_and_batch_bundle) |
Rohit Waghchaure | 42b2294 | 2023-05-27 19:18:03 +0530 | [diff] [blame] | 238 | self.validate_actual_qty(doc) |
| 239 | |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 240 | doc.flags.ignore_voucher_validation = True |
| 241 | doc.submit() |
| 242 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 243 | def set_warehouse_and_status_in_serial_nos(self): |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 244 | serial_nos = get_serial_nos(self.sle.serial_and_batch_bundle) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 245 | warehouse = self.warehouse if self.sle.actual_qty > 0 else None |
| 246 | |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 247 | if not serial_nos: |
| 248 | return |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 249 | |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 250 | sn_table = frappe.qb.DocType("Serial No") |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 251 | ( |
| 252 | frappe.qb.update(sn_table) |
| 253 | .set(sn_table.warehouse, warehouse) |
| 254 | .set(sn_table.status, "Active" if warehouse else "Inactive") |
| 255 | .where(sn_table.name.isin(serial_nos)) |
| 256 | ).run() |
| 257 | |
| 258 | def set_batch_no_in_serial_nos(self): |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 259 | entries = frappe.get_all( |
| 260 | "Serial and Batch Entry", |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 261 | fields=["serial_no", "batch_no"], |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 262 | filters={"parent": self.sle.serial_and_batch_bundle}, |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 263 | ) |
| 264 | |
| 265 | batch_serial_nos = {} |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 266 | for ledger in entries: |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 267 | batch_serial_nos.setdefault(ledger.batch_no, []).append(ledger.serial_no) |
| 268 | |
| 269 | for batch_no, serial_nos in batch_serial_nos.items(): |
| 270 | sn_table = frappe.qb.DocType("Serial No") |
| 271 | ( |
| 272 | frappe.qb.update(sn_table) |
| 273 | .set(sn_table.batch_no, batch_no) |
| 274 | .where(sn_table.name.isin(serial_nos)) |
| 275 | ).run() |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 276 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 277 | def update_batch_qty(self): |
| 278 | from erpnext.stock.doctype.batch.batch import get_available_batches |
| 279 | |
| 280 | batches = get_batch_nos(self.sle.serial_and_batch_bundle) |
| 281 | |
| 282 | batches_qty = get_available_batches( |
| 283 | frappe._dict( |
| 284 | {"item_code": self.item_code, "warehouse": self.warehouse, "batch_no": list(batches.keys())} |
| 285 | ) |
| 286 | ) |
| 287 | |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 288 | for batch_no in batches: |
| 289 | frappe.db.set_value("Batch", batch_no, "batch_qty", batches_qty.get(batch_no, 0)) |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 290 | |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 291 | |
Rohit Waghchaure | 74ab20f | 2023-04-03 12:26:12 +0530 | [diff] [blame] | 292 | def get_serial_nos(serial_and_batch_bundle, serial_nos=None): |
Rohit Waghchaure | e88c5d6 | 2023-04-05 20:03:44 +0530 | [diff] [blame] | 293 | if not serial_and_batch_bundle: |
| 294 | return [] |
| 295 | |
| 296 | filters = {"parent": serial_and_batch_bundle, "serial_no": ("is", "set")} |
Rohit Waghchaure | f8bf4aa | 2023-04-02 13:13:42 +0530 | [diff] [blame] | 297 | if isinstance(serial_and_batch_bundle, list): |
| 298 | filters = {"parent": ("in", serial_and_batch_bundle)} |
| 299 | |
Rohit Waghchaure | 74ab20f | 2023-04-03 12:26:12 +0530 | [diff] [blame] | 300 | if serial_nos: |
| 301 | filters["serial_no"] = ("in", serial_nos) |
| 302 | |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 303 | entries = frappe.get_all("Serial and Batch Entry", fields=["serial_no"], filters=filters) |
Rohit Waghchaure | e88c5d6 | 2023-04-05 20:03:44 +0530 | [diff] [blame] | 304 | if not entries: |
| 305 | return [] |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 306 | |
Rohit Waghchaure | e88c5d6 | 2023-04-05 20:03:44 +0530 | [diff] [blame] | 307 | return [d.serial_no for d in entries if d.serial_no] |
| 308 | |
| 309 | |
| 310 | def get_serial_nos_from_bundle(serial_and_batch_bundle, serial_nos=None): |
| 311 | return get_serial_nos(serial_and_batch_bundle, serial_nos=serial_nos) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 312 | |
| 313 | |
Rohit Waghchaure | bb95451 | 2023-06-02 00:11:43 +0530 | [diff] [blame] | 314 | def get_serial_or_batch_nos(bundle): |
Rohit Waghchaure | 9cf645e | 2023-06-26 16:00:53 +0530 | [diff] [blame] | 315 | # For print format |
| 316 | |
| 317 | bundle_data = frappe.get_cached_value( |
| 318 | "Serial and Batch Bundle", bundle, ["has_serial_no", "has_batch_no"], as_dict=True |
| 319 | ) |
| 320 | |
| 321 | fields = [] |
| 322 | if bundle_data.has_serial_no: |
| 323 | fields.append("serial_no") |
| 324 | |
| 325 | if bundle_data.has_batch_no: |
| 326 | fields.extend(["batch_no", "qty"]) |
| 327 | |
| 328 | data = frappe.get_all("Serial and Batch Entry", fields=fields, filters={"parent": bundle}) |
| 329 | |
| 330 | if bundle_data.has_serial_no and not bundle_data.has_batch_no: |
| 331 | return ", ".join([d.serial_no for d in data]) |
| 332 | |
| 333 | elif bundle_data.has_batch_no: |
| 334 | html = "<table class= 'table table-borderless' style='margin-top: 0px;margin-bottom: 0px;'>" |
| 335 | for d in data: |
| 336 | if d.serial_no: |
| 337 | html += f"<tr><td>{d.batch_no}</th><th>{d.serial_no}</th ><th>{abs(d.qty)}</th></tr>" |
| 338 | else: |
| 339 | html += f"<tr><td>{d.batch_no}</td><td>{abs(d.qty)}</td></tr>" |
| 340 | |
| 341 | html += "</table>" |
| 342 | |
| 343 | return html |
Rohit Waghchaure | bb95451 | 2023-06-02 00:11:43 +0530 | [diff] [blame] | 344 | |
| 345 | |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 346 | class SerialNoValuation(DeprecatedSerialNoValuation): |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 347 | def __init__(self, **kwargs): |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 348 | for key, value in kwargs.items(): |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 349 | setattr(self, key, value) |
| 350 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 351 | self.calculate_stock_value_change() |
| 352 | self.calculate_valuation_rate() |
| 353 | |
| 354 | def calculate_stock_value_change(self): |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 355 | if flt(self.sle.actual_qty) > 0: |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 356 | self.stock_value_change = frappe.get_cached_value( |
| 357 | "Serial and Batch Bundle", self.sle.serial_and_batch_bundle, "total_amount" |
| 358 | ) |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 359 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 360 | else: |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 361 | entries = self.get_serial_no_ledgers() |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 362 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 363 | self.serial_no_incoming_rate = defaultdict(float) |
| 364 | self.stock_value_change = 0.0 |
| 365 | |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 366 | for ledger in entries: |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 367 | self.stock_value_change += ledger.incoming_rate |
| 368 | self.serial_no_incoming_rate[ledger.serial_no] += ledger.incoming_rate |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 369 | |
| 370 | self.calculate_stock_value_from_deprecarated_ledgers() |
| 371 | |
| 372 | def get_serial_no_ledgers(self): |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 373 | serial_nos = self.get_serial_nos() |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 374 | bundle = frappe.qb.DocType("Serial and Batch Bundle") |
| 375 | bundle_child = frappe.qb.DocType("Serial and Batch Entry") |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 376 | |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 377 | query = ( |
| 378 | frappe.qb.from_(bundle) |
| 379 | .inner_join(bundle_child) |
| 380 | .on(bundle.name == bundle_child.parent) |
| 381 | .select( |
| 382 | bundle.name, |
| 383 | bundle_child.serial_no, |
| 384 | (bundle_child.incoming_rate * bundle_child.qty).as_("incoming_rate"), |
| 385 | ) |
| 386 | .where( |
| 387 | (bundle.is_cancelled == 0) |
| 388 | & (bundle.docstatus == 1) |
| 389 | & (bundle_child.serial_no.isin(serial_nos)) |
Rohit Waghchaure | f8bf4aa | 2023-04-02 13:13:42 +0530 | [diff] [blame] | 390 | & (bundle.type_of_transaction.isin(["Inward", "Outward"])) |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 391 | & (bundle.item_code == self.sle.item_code) |
| 392 | & (bundle_child.warehouse == self.sle.warehouse) |
| 393 | ) |
| 394 | .orderby(bundle.posting_date, bundle.posting_time, bundle.creation) |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 395 | ) |
| 396 | |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 397 | # Important to exclude the current voucher |
Rohit Waghchaure | f8bf4aa | 2023-04-02 13:13:42 +0530 | [diff] [blame] | 398 | if self.sle.voucher_no: |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 399 | query = query.where(bundle.voucher_no != self.sle.voucher_no) |
| 400 | |
| 401 | if self.sle.posting_date: |
| 402 | if self.sle.posting_time is None: |
| 403 | self.sle.posting_time = nowtime() |
| 404 | |
| 405 | timestamp_condition = CombineDatetime( |
| 406 | bundle.posting_date, bundle.posting_time |
| 407 | ) <= CombineDatetime(self.sle.posting_date, self.sle.posting_time) |
| 408 | |
| 409 | query = query.where(timestamp_condition) |
| 410 | |
| 411 | return query.run(as_dict=True) |
| 412 | |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 413 | def get_serial_nos(self): |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 414 | if self.sle.get("serial_nos"): |
| 415 | return self.sle.serial_nos |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 416 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 417 | return get_serial_nos(self.sle.serial_and_batch_bundle) |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 418 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 419 | def calculate_valuation_rate(self): |
| 420 | if not hasattr(self, "wh_data"): |
| 421 | return |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 422 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 423 | new_stock_qty = self.wh_data.qty_after_transaction + self.sle.actual_qty |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 424 | |
| 425 | if new_stock_qty > 0: |
| 426 | new_stock_value = ( |
| 427 | self.wh_data.qty_after_transaction * self.wh_data.valuation_rate |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 428 | ) + self.stock_value_change |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 429 | if new_stock_value >= 0: |
| 430 | # calculate new valuation rate only if stock value is positive |
| 431 | # else it remains the same as that of previous entry |
| 432 | self.wh_data.valuation_rate = new_stock_value / new_stock_qty |
| 433 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 434 | if ( |
| 435 | not self.wh_data.valuation_rate and self.sle.voucher_detail_no and not self.is_rejected_entry() |
| 436 | ): |
| 437 | allow_zero_rate = self.sle_self.check_if_allow_zero_valuation_rate( |
| 438 | self.sle.voucher_type, self.sle.voucher_detail_no |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 439 | ) |
| 440 | if not allow_zero_rate: |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 441 | self.wh_data.valuation_rate = self.sle_self.get_fallback_rate(self.sle) |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 442 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 443 | self.wh_data.qty_after_transaction += self.sle.actual_qty |
| 444 | self.wh_data.stock_value = flt(self.wh_data.qty_after_transaction) * flt( |
| 445 | self.wh_data.valuation_rate |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 446 | ) |
| 447 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 448 | def is_rejected_entry(self): |
| 449 | return is_rejected(self.sle.voucher_type, self.sle.voucher_detail_no, self.sle.warehouse) |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 450 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 451 | def get_incoming_rate(self): |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 452 | return abs(flt(self.stock_value_change) / flt(self.sle.actual_qty)) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 453 | |
Rohit Waghchaure | f8bf4aa | 2023-04-02 13:13:42 +0530 | [diff] [blame] | 454 | def get_incoming_rate_of_serial_no(self, serial_no): |
| 455 | return self.serial_no_incoming_rate.get(serial_no, 0.0) |
| 456 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 457 | |
| 458 | def is_rejected(voucher_type, voucher_detail_no, warehouse): |
| 459 | if voucher_type in ["Purchase Receipt", "Purchase Invoice"]: |
| 460 | return warehouse == frappe.get_cached_value( |
| 461 | voucher_type + " Item", voucher_detail_no, "rejected_warehouse" |
| 462 | ) |
| 463 | |
| 464 | return False |
| 465 | |
| 466 | |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 467 | class BatchNoValuation(DeprecatedBatchNoValuation): |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 468 | def __init__(self, **kwargs): |
| 469 | for key, value in kwargs.items(): |
| 470 | setattr(self, key, value) |
| 471 | |
| 472 | self.batch_nos = self.get_batch_nos() |
Rohit Waghchaure | f704eb7 | 2023-03-30 11:32:39 +0530 | [diff] [blame] | 473 | self.prepare_batches() |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 474 | self.calculate_avg_rate() |
| 475 | self.calculate_valuation_rate() |
| 476 | |
| 477 | def calculate_avg_rate(self): |
Rohit Waghchaure | c2d7461 | 2023-03-29 11:40:36 +0530 | [diff] [blame] | 478 | if flt(self.sle.actual_qty) > 0: |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 479 | self.stock_value_change = frappe.get_cached_value( |
| 480 | "Serial and Batch Bundle", self.sle.serial_and_batch_bundle, "total_amount" |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 481 | ) |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 482 | else: |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 483 | entries = self.get_batch_no_ledgers() |
Rohit Waghchaure | 40ab3bd | 2023-06-01 16:08:49 +0530 | [diff] [blame] | 484 | self.stock_value_change = 0.0 |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 485 | self.batch_avg_rate = defaultdict(float) |
Rohit Waghchaure | 86da306 | 2023-03-20 14:15:34 +0530 | [diff] [blame] | 486 | self.available_qty = defaultdict(float) |
Rohit Waghchaure | f704eb7 | 2023-03-30 11:32:39 +0530 | [diff] [blame] | 487 | self.stock_value_differece = defaultdict(float) |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 488 | |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 489 | for ledger in entries: |
Rohit Waghchaure | f704eb7 | 2023-03-30 11:32:39 +0530 | [diff] [blame] | 490 | self.stock_value_differece[ledger.batch_no] += flt(ledger.incoming_rate) |
Rohit Waghchaure | 86da306 | 2023-03-20 14:15:34 +0530 | [diff] [blame] | 491 | self.available_qty[ledger.batch_no] += flt(ledger.qty) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 492 | |
| 493 | self.calculate_avg_rate_from_deprecarated_ledgers() |
Rohit Waghchaure | 40ab3bd | 2023-06-01 16:08:49 +0530 | [diff] [blame] | 494 | self.calculate_avg_rate_for_non_batchwise_valuation() |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 495 | self.set_stock_value_difference() |
| 496 | |
| 497 | def get_batch_no_ledgers(self) -> List[dict]: |
Rohit Waghchaure | f704eb7 | 2023-03-30 11:32:39 +0530 | [diff] [blame] | 498 | if not self.batchwise_valuation_batches: |
| 499 | return [] |
| 500 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 501 | parent = frappe.qb.DocType("Serial and Batch Bundle") |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 502 | child = frappe.qb.DocType("Serial and Batch Entry") |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 503 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 504 | timestamp_condition = "" |
| 505 | if self.sle.posting_date and self.sle.posting_time: |
| 506 | timestamp_condition = CombineDatetime( |
| 507 | parent.posting_date, parent.posting_time |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 508 | ) <= CombineDatetime(self.sle.posting_date, self.sle.posting_time) |
Rohit Waghchaure | 86da306 | 2023-03-20 14:15:34 +0530 | [diff] [blame] | 509 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 510 | query = ( |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 511 | frappe.qb.from_(parent) |
| 512 | .inner_join(child) |
| 513 | .on(parent.name == child.parent) |
| 514 | .select( |
| 515 | child.batch_no, |
| 516 | Sum(child.stock_value_difference).as_("incoming_rate"), |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 517 | Sum(child.qty).as_("qty"), |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 518 | ) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 519 | .where( |
Rohit Waghchaure | f704eb7 | 2023-03-30 11:32:39 +0530 | [diff] [blame] | 520 | (child.batch_no.isin(self.batchwise_valuation_batches)) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 521 | & (parent.warehouse == self.sle.warehouse) |
| 522 | & (parent.item_code == self.sle.item_code) |
Rohit Waghchaure | 86da306 | 2023-03-20 14:15:34 +0530 | [diff] [blame] | 523 | & (parent.docstatus == 1) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 524 | & (parent.is_cancelled == 0) |
Rohit Waghchaure | f8bf4aa | 2023-04-02 13:13:42 +0530 | [diff] [blame] | 525 | & (parent.type_of_transaction.isin(["Inward", "Outward"])) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 526 | ) |
| 527 | .groupby(child.batch_no) |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 528 | ) |
| 529 | |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 530 | # Important to exclude the current voucher |
| 531 | if self.sle.voucher_no: |
| 532 | query = query.where(parent.voucher_no != self.sle.voucher_no) |
Rohit Waghchaure | c2d7461 | 2023-03-29 11:40:36 +0530 | [diff] [blame] | 533 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 534 | if timestamp_condition: |
Rohit Waghchaure | c2d7461 | 2023-03-29 11:40:36 +0530 | [diff] [blame] | 535 | query = query.where(timestamp_condition) |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 536 | |
| 537 | return query.run(as_dict=True) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 538 | |
Rohit Waghchaure | f704eb7 | 2023-03-30 11:32:39 +0530 | [diff] [blame] | 539 | def prepare_batches(self): |
| 540 | self.batches = self.batch_nos |
| 541 | if isinstance(self.batch_nos, dict): |
| 542 | self.batches = list(self.batch_nos.keys()) |
| 543 | |
| 544 | self.batchwise_valuation_batches = [] |
| 545 | self.non_batchwise_valuation_batches = [] |
| 546 | |
| 547 | batches = frappe.get_all( |
| 548 | "Batch", filters={"name": ("in", self.batches), "use_batchwise_valuation": 1}, fields=["name"] |
| 549 | ) |
| 550 | |
| 551 | for batch in batches: |
| 552 | self.batchwise_valuation_batches.append(batch.name) |
| 553 | |
| 554 | self.non_batchwise_valuation_batches = list( |
| 555 | set(self.batches) - set(self.batchwise_valuation_batches) |
| 556 | ) |
| 557 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 558 | def get_batch_nos(self) -> list: |
| 559 | if self.sle.get("batch_nos"): |
| 560 | return self.sle.batch_nos |
| 561 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 562 | return get_batch_nos(self.sle.serial_and_batch_bundle) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 563 | |
| 564 | def set_stock_value_difference(self): |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 565 | for batch_no, ledger in self.batch_nos.items(): |
Rohit Waghchaure | 40ab3bd | 2023-06-01 16:08:49 +0530 | [diff] [blame] | 566 | if batch_no in self.non_batchwise_valuation_batches: |
| 567 | continue |
| 568 | |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 569 | if not self.available_qty[batch_no]: |
| 570 | continue |
| 571 | |
Rohit Waghchaure | f704eb7 | 2023-03-30 11:32:39 +0530 | [diff] [blame] | 572 | self.batch_avg_rate[batch_no] = ( |
| 573 | self.stock_value_differece[batch_no] / self.available_qty[batch_no] |
| 574 | ) |
| 575 | |
| 576 | # New Stock Value Difference |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 577 | stock_value_change = self.batch_avg_rate[batch_no] * ledger.qty |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 578 | self.stock_value_change += stock_value_change |
Rohit Waghchaure | 40ab3bd | 2023-06-01 16:08:49 +0530 | [diff] [blame] | 579 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 580 | frappe.db.set_value( |
Rohit Waghchaure | 40ab3bd | 2023-06-01 16:08:49 +0530 | [diff] [blame] | 581 | "Serial and Batch Entry", |
| 582 | ledger.name, |
| 583 | { |
| 584 | "stock_value_difference": stock_value_change, |
| 585 | "incoming_rate": self.batch_avg_rate[batch_no], |
| 586 | }, |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 587 | ) |
| 588 | |
| 589 | def calculate_valuation_rate(self): |
| 590 | if not hasattr(self, "wh_data"): |
| 591 | return |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 592 | |
| 593 | self.wh_data.stock_value = round_off_if_near_zero( |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 594 | self.wh_data.stock_value + self.stock_value_change |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 595 | ) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 596 | |
Rohit Waghchaure | c2d7461 | 2023-03-29 11:40:36 +0530 | [diff] [blame] | 597 | self.wh_data.qty_after_transaction += self.sle.actual_qty |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 598 | if self.wh_data.qty_after_transaction: |
| 599 | self.wh_data.valuation_rate = self.wh_data.stock_value / self.wh_data.qty_after_transaction |
| 600 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 601 | def get_incoming_rate(self): |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 602 | if not self.sle.actual_qty: |
| 603 | self.sle.actual_qty = self.get_actual_qty() |
| 604 | |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 605 | return abs(flt(self.stock_value_change) / flt(self.sle.actual_qty)) |
Rohit Waghchaure | 16f26fb | 2023-03-20 22:56:06 +0530 | [diff] [blame] | 606 | |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 607 | def get_actual_qty(self): |
| 608 | total_qty = 0.0 |
| 609 | for batch_no in self.available_qty: |
| 610 | total_qty += self.available_qty[batch_no] |
| 611 | |
| 612 | return total_qty |
| 613 | |
Rohit Waghchaure | 16f26fb | 2023-03-20 22:56:06 +0530 | [diff] [blame] | 614 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 615 | def get_batch_nos(serial_and_batch_bundle): |
Rohit Waghchaure | e88c5d6 | 2023-04-05 20:03:44 +0530 | [diff] [blame] | 616 | if not serial_and_batch_bundle: |
| 617 | return frappe._dict({}) |
| 618 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 619 | entries = frappe.get_all( |
| 620 | "Serial and Batch Entry", |
| 621 | fields=["batch_no", "qty", "name"], |
Rohit Waghchaure | e88c5d6 | 2023-04-05 20:03:44 +0530 | [diff] [blame] | 622 | filters={"parent": serial_and_batch_bundle, "batch_no": ("is", "set")}, |
Rohit Waghchaure | c2d7461 | 2023-03-29 11:40:36 +0530 | [diff] [blame] | 623 | order_by="idx", |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 624 | ) |
| 625 | |
Rohit Waghchaure | e88c5d6 | 2023-04-05 20:03:44 +0530 | [diff] [blame] | 626 | if not entries: |
| 627 | return frappe._dict({}) |
| 628 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 629 | return {d.batch_no: d for d in entries} |
| 630 | |
| 631 | |
Rohit Waghchaure | 16f26fb | 2023-03-20 22:56:06 +0530 | [diff] [blame] | 632 | def get_empty_batches_based_work_order(work_order, item_code): |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 633 | batches = get_batches_from_work_order(work_order, item_code) |
Rohit Waghchaure | 16f26fb | 2023-03-20 22:56:06 +0530 | [diff] [blame] | 634 | if not batches: |
| 635 | return batches |
| 636 | |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 637 | entries = get_batches_from_stock_entries(work_order, item_code) |
Rohit Waghchaure | 16f26fb | 2023-03-20 22:56:06 +0530 | [diff] [blame] | 638 | if not entries: |
| 639 | return batches |
| 640 | |
| 641 | ids = [d.serial_and_batch_bundle for d in entries if d.serial_and_batch_bundle] |
| 642 | if ids: |
| 643 | set_batch_details_from_package(ids, batches) |
| 644 | |
| 645 | # Will be deprecated in v16 |
| 646 | for d in entries: |
| 647 | if not d.batch_no: |
| 648 | continue |
| 649 | |
| 650 | batches[d.batch_no] -= d.qty |
| 651 | |
| 652 | return batches |
| 653 | |
| 654 | |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 655 | def get_batches_from_work_order(work_order, item_code): |
Rohit Waghchaure | 16f26fb | 2023-03-20 22:56:06 +0530 | [diff] [blame] | 656 | return frappe._dict( |
| 657 | frappe.get_all( |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 658 | "Batch", |
| 659 | fields=["name", "qty_to_produce"], |
| 660 | filters={"reference_name": work_order, "item": item_code}, |
| 661 | as_list=1, |
Rohit Waghchaure | 16f26fb | 2023-03-20 22:56:06 +0530 | [diff] [blame] | 662 | ) |
| 663 | ) |
| 664 | |
| 665 | |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 666 | def get_batches_from_stock_entries(work_order, item_code): |
Rohit Waghchaure | 16f26fb | 2023-03-20 22:56:06 +0530 | [diff] [blame] | 667 | entries = frappe.get_all( |
| 668 | "Stock Entry", |
| 669 | filters={"work_order": work_order, "docstatus": 1, "purpose": "Manufacture"}, |
| 670 | fields=["name"], |
| 671 | ) |
| 672 | |
| 673 | return frappe.get_all( |
| 674 | "Stock Entry Detail", |
| 675 | fields=["batch_no", "qty", "serial_and_batch_bundle"], |
| 676 | filters={ |
| 677 | "parent": ("in", [d.name for d in entries]), |
| 678 | "is_finished_item": 1, |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 679 | "item_code": item_code, |
Rohit Waghchaure | 16f26fb | 2023-03-20 22:56:06 +0530 | [diff] [blame] | 680 | }, |
| 681 | ) |
| 682 | |
| 683 | |
| 684 | def set_batch_details_from_package(ids, batches): |
| 685 | entries = frappe.get_all( |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 686 | "Serial and Batch Entry", |
Rohit Waghchaure | 16f26fb | 2023-03-20 22:56:06 +0530 | [diff] [blame] | 687 | filters={"parent": ("in", ids), "is_outward": 0}, |
| 688 | fields=["batch_no", "qty"], |
| 689 | ) |
| 690 | |
| 691 | for d in entries: |
| 692 | batches[d.batch_no] -= d.qty |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 693 | |
| 694 | |
| 695 | class SerialBatchCreation: |
| 696 | def __init__(self, args): |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 697 | self.set(args) |
| 698 | self.set_item_details() |
Rohit Waghchaure | 9b72845 | 2023-03-28 14:03:59 +0530 | [diff] [blame] | 699 | self.set_other_details() |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 700 | |
| 701 | def set(self, args): |
| 702 | self.__dict__ = {} |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 703 | for key, value in args.items(): |
| 704 | setattr(self, key, value) |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 705 | self.__dict__[key] = value |
| 706 | |
| 707 | def get(self, key): |
| 708 | return self.__dict__.get(key) |
| 709 | |
| 710 | def set_item_details(self): |
| 711 | fields = [ |
| 712 | "has_batch_no", |
| 713 | "has_serial_no", |
| 714 | "item_name", |
| 715 | "item_group", |
| 716 | "serial_no_series", |
| 717 | "create_new_batch", |
| 718 | "batch_number_series", |
| 719 | "description", |
| 720 | ] |
| 721 | |
| 722 | item_details = frappe.get_cached_value("Item", self.item_code, fields, as_dict=1) |
| 723 | for key, value in item_details.items(): |
| 724 | setattr(self, key, value) |
| 725 | |
| 726 | self.__dict__.update(item_details) |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 727 | |
Rohit Waghchaure | 9b72845 | 2023-03-28 14:03:59 +0530 | [diff] [blame] | 728 | def set_other_details(self): |
| 729 | if not self.get("posting_date"): |
| 730 | setattr(self, "posting_date", today()) |
| 731 | self.__dict__["posting_date"] = self.posting_date |
| 732 | |
Rohit Waghchaure | c2d7461 | 2023-03-29 11:40:36 +0530 | [diff] [blame] | 733 | if not self.get("actual_qty"): |
| 734 | qty = self.get("qty") or self.get("total_qty") |
| 735 | |
| 736 | setattr(self, "actual_qty", qty) |
| 737 | self.__dict__["actual_qty"] = self.actual_qty |
| 738 | |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 739 | def duplicate_package(self): |
| 740 | if not self.serial_and_batch_bundle: |
| 741 | return |
| 742 | |
| 743 | id = self.serial_and_batch_bundle |
| 744 | package = frappe.get_doc("Serial and Batch Bundle", id) |
| 745 | new_package = frappe.copy_doc(package) |
Rohit Waghchaure | f8bf4aa | 2023-04-02 13:13:42 +0530 | [diff] [blame] | 746 | |
| 747 | if self.get("returned_serial_nos"): |
| 748 | self.remove_returned_serial_nos(new_package) |
| 749 | |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 750 | new_package.docstatus = 0 |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 751 | new_package.type_of_transaction = self.type_of_transaction |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 752 | new_package.returned_against = self.get("returned_against") |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 753 | new_package.save() |
| 754 | |
| 755 | self.serial_and_batch_bundle = new_package.name |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 756 | |
Rohit Waghchaure | f8bf4aa | 2023-04-02 13:13:42 +0530 | [diff] [blame] | 757 | def remove_returned_serial_nos(self, package): |
| 758 | remove_list = [] |
| 759 | for d in package.entries: |
| 760 | if d.serial_no in self.returned_serial_nos: |
| 761 | remove_list.append(d) |
| 762 | |
| 763 | for d in remove_list: |
| 764 | package.remove(d) |
| 765 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 766 | def make_serial_and_batch_bundle(self): |
| 767 | doc = frappe.new_doc("Serial and Batch Bundle") |
| 768 | valid_columns = doc.meta.get_valid_columns() |
| 769 | for key, value in self.__dict__.items(): |
| 770 | if key in valid_columns: |
| 771 | doc.set(key, value) |
| 772 | |
| 773 | if self.type_of_transaction == "Outward": |
| 774 | self.set_auto_serial_batch_entries_for_outward() |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 775 | elif self.type_of_transaction == "Inward": |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 776 | self.set_auto_serial_batch_entries_for_inward() |
Rohit Waghchaure | e88c5d6 | 2023-04-05 20:03:44 +0530 | [diff] [blame] | 777 | self.add_serial_nos_for_batch_item() |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 778 | |
| 779 | self.set_serial_batch_entries(doc) |
Rohit Waghchaure | f8bf4aa | 2023-04-02 13:13:42 +0530 | [diff] [blame] | 780 | if not doc.get("entries"): |
| 781 | return frappe._dict({}) |
| 782 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 783 | doc.save() |
Rohit Waghchaure | dcb0462 | 2023-06-05 16:56:29 +0530 | [diff] [blame] | 784 | self.validate_qty(doc) |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 785 | |
| 786 | if not hasattr(self, "do_not_submit") or not self.do_not_submit: |
Rohit Waghchaure | c2d7461 | 2023-03-29 11:40:36 +0530 | [diff] [blame] | 787 | doc.flags.ignore_voucher_validation = True |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 788 | doc.submit() |
| 789 | |
| 790 | return doc |
| 791 | |
Rohit Waghchaure | e88c5d6 | 2023-04-05 20:03:44 +0530 | [diff] [blame] | 792 | def add_serial_nos_for_batch_item(self): |
| 793 | if not (self.has_serial_no and self.has_batch_no): |
| 794 | return |
| 795 | |
| 796 | if not self.get("serial_nos") and self.get("batches"): |
| 797 | batches = list(self.get("batches").keys()) |
| 798 | if len(batches) == 1: |
| 799 | self.batch_no = batches[0] |
| 800 | self.serial_nos = self.get_auto_created_serial_nos() |
Rohit Waghchaure | e88c5d6 | 2023-04-05 20:03:44 +0530 | [diff] [blame] | 801 | |
Rohit Waghchaure | 74ab20f | 2023-04-03 12:26:12 +0530 | [diff] [blame] | 802 | def update_serial_and_batch_entries(self): |
| 803 | doc = frappe.get_doc("Serial and Batch Bundle", self.serial_and_batch_bundle) |
| 804 | doc.type_of_transaction = self.type_of_transaction |
| 805 | doc.set("entries", []) |
| 806 | self.set_auto_serial_batch_entries_for_outward() |
| 807 | self.set_serial_batch_entries(doc) |
| 808 | if not doc.get("entries"): |
| 809 | return frappe._dict({}) |
| 810 | |
| 811 | doc.save() |
| 812 | return doc |
| 813 | |
Rohit Waghchaure | dcb0462 | 2023-06-05 16:56:29 +0530 | [diff] [blame] | 814 | def validate_qty(self, doc): |
| 815 | if doc.type_of_transaction == "Outward": |
| 816 | precision = doc.precision("total_qty") |
| 817 | |
| 818 | total_qty = abs(flt(doc.total_qty, precision)) |
| 819 | required_qty = abs(flt(self.actual_qty, precision)) |
| 820 | |
| 821 | if required_qty - total_qty > 0: |
| 822 | msg = f"For the item {bold(doc.item_code)}, the Avaliable qty {bold(total_qty)} is less than the Required Qty {bold(required_qty)} in the warehouse {bold(doc.warehouse)}. Please add sufficient qty in the warehouse." |
| 823 | frappe.throw(msg, title=_("Insufficient Stock")) |
| 824 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 825 | def set_auto_serial_batch_entries_for_outward(self): |
| 826 | from erpnext.stock.doctype.batch.batch import get_available_batches |
| 827 | from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos_for_outward |
| 828 | |
| 829 | kwargs = frappe._dict( |
| 830 | { |
| 831 | "item_code": self.item_code, |
| 832 | "warehouse": self.warehouse, |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 833 | "qty": abs(self.actual_qty) if self.actual_qty else 0, |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 834 | "based_on": frappe.db.get_single_value("Stock Settings", "pick_serial_and_batch_based_on"), |
| 835 | } |
| 836 | ) |
| 837 | |
Rohit Waghchaure | 74ab20f | 2023-04-03 12:26:12 +0530 | [diff] [blame] | 838 | if self.get("ignore_serial_nos"): |
| 839 | kwargs["ignore_serial_nos"] = self.ignore_serial_nos |
| 840 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 841 | if self.has_serial_no and not self.get("serial_nos"): |
| 842 | self.serial_nos = get_serial_nos_for_outward(kwargs) |
Rohit Waghchaure | 9b72845 | 2023-03-28 14:03:59 +0530 | [diff] [blame] | 843 | elif not self.has_serial_no and self.has_batch_no and not self.get("batches"): |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 844 | self.batches = get_available_batches(kwargs) |
| 845 | |
| 846 | def set_auto_serial_batch_entries_for_inward(self): |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 847 | if (self.get("batches") and self.has_batch_no) or ( |
| 848 | self.get("serial_nos") and self.has_serial_no |
| 849 | ): |
| 850 | return |
| 851 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 852 | self.batch_no = None |
| 853 | if self.has_batch_no: |
| 854 | self.batch_no = self.create_batch() |
| 855 | |
| 856 | if self.has_serial_no: |
| 857 | self.serial_nos = self.get_auto_created_serial_nos() |
| 858 | else: |
Rohit Waghchaure | c2d7461 | 2023-03-29 11:40:36 +0530 | [diff] [blame] | 859 | self.batches = frappe._dict({self.batch_no: abs(self.actual_qty)}) |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 860 | |
| 861 | def set_serial_batch_entries(self, doc): |
| 862 | if self.get("serial_nos"): |
| 863 | serial_no_wise_batch = frappe._dict({}) |
| 864 | if self.has_batch_no: |
| 865 | serial_no_wise_batch = self.get_serial_nos_batch(self.serial_nos) |
| 866 | |
| 867 | qty = -1 if self.type_of_transaction == "Outward" else 1 |
| 868 | for serial_no in self.serial_nos: |
| 869 | doc.append( |
| 870 | "entries", |
| 871 | { |
| 872 | "serial_no": serial_no, |
| 873 | "qty": qty, |
| 874 | "batch_no": serial_no_wise_batch.get(serial_no) or self.get("batch_no"), |
| 875 | "incoming_rate": self.get("incoming_rate"), |
| 876 | }, |
| 877 | ) |
| 878 | |
Rohit Waghchaure | e88c5d6 | 2023-04-05 20:03:44 +0530 | [diff] [blame] | 879 | elif self.get("batches"): |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 880 | for batch_no, batch_qty in self.batches.items(): |
| 881 | doc.append( |
| 882 | "entries", |
| 883 | { |
| 884 | "batch_no": batch_no, |
| 885 | "qty": batch_qty * (-1 if self.type_of_transaction == "Outward" else 1), |
| 886 | "incoming_rate": self.get("incoming_rate"), |
| 887 | }, |
| 888 | ) |
| 889 | |
| 890 | def get_serial_nos_batch(self, serial_nos): |
| 891 | return frappe._dict( |
| 892 | frappe.get_all( |
| 893 | "Serial No", |
| 894 | fields=["name", "batch_no"], |
| 895 | filters={"name": ("in", serial_nos)}, |
| 896 | as_list=1, |
| 897 | ) |
| 898 | ) |
| 899 | |
| 900 | def create_batch(self): |
| 901 | from erpnext.stock.doctype.batch.batch import make_batch |
| 902 | |
| 903 | return make_batch( |
| 904 | frappe._dict( |
| 905 | { |
Rohit Waghchaure | c2d7461 | 2023-03-29 11:40:36 +0530 | [diff] [blame] | 906 | "item": self.get("item_code"), |
| 907 | "reference_doctype": self.get("voucher_type"), |
| 908 | "reference_name": self.get("voucher_no"), |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 909 | } |
| 910 | ) |
| 911 | ) |
| 912 | |
| 913 | def get_auto_created_serial_nos(self): |
| 914 | sr_nos = [] |
| 915 | serial_nos_details = [] |
| 916 | |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 917 | if not self.serial_no_series: |
| 918 | msg = f"Please set Serial No Series in the item {self.item_code} or create Serial and Batch Bundle manually." |
| 919 | frappe.throw(_(msg)) |
| 920 | |
Rohit Waghchaure | c2d7461 | 2023-03-29 11:40:36 +0530 | [diff] [blame] | 921 | for i in range(abs(cint(self.actual_qty))): |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 922 | serial_no = make_autoname(self.serial_no_series, "Serial No") |
| 923 | sr_nos.append(serial_no) |
| 924 | serial_nos_details.append( |
| 925 | ( |
| 926 | serial_no, |
| 927 | serial_no, |
| 928 | now(), |
| 929 | now(), |
| 930 | frappe.session.user, |
| 931 | frappe.session.user, |
| 932 | self.warehouse, |
| 933 | self.company, |
| 934 | self.item_code, |
| 935 | self.item_name, |
| 936 | self.description, |
| 937 | "Active", |
| 938 | self.batch_no, |
| 939 | ) |
| 940 | ) |
| 941 | |
| 942 | if serial_nos_details: |
| 943 | fields = [ |
| 944 | "name", |
| 945 | "serial_no", |
| 946 | "creation", |
| 947 | "modified", |
| 948 | "owner", |
| 949 | "modified_by", |
| 950 | "warehouse", |
| 951 | "company", |
| 952 | "item_code", |
| 953 | "item_name", |
| 954 | "description", |
| 955 | "status", |
| 956 | "batch_no", |
| 957 | ] |
| 958 | |
| 959 | frappe.db.bulk_insert("Serial No", fields=fields, values=set(serial_nos_details)) |
| 960 | |
| 961 | return sr_nos |
| 962 | |
| 963 | |
| 964 | def get_serial_or_batch_items(items): |
| 965 | serial_or_batch_items = frappe.get_all( |
| 966 | "Item", |
| 967 | filters={"name": ("in", [d.item_code for d in items])}, |
| 968 | or_filters={"has_serial_no": 1, "has_batch_no": 1}, |
| 969 | ) |
| 970 | |
| 971 | if not serial_or_batch_items: |
| 972 | return |
| 973 | else: |
| 974 | serial_or_batch_items = [d.name for d in serial_or_batch_items] |
| 975 | |
| 976 | return serial_or_batch_items |