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