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