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