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 | 9b72845 | 2023-03-28 14:03:59 +0530 | [diff] [blame] | 8 | from frappe.utils import cint, flt, now, 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, |
| 70 | "total_qty": self.sle.actual_qty, |
| 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 | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 81 | def validate_item(self): |
| 82 | msg = "" |
| 83 | if self.sle.actual_qty > 0: |
| 84 | if not self.item_details.has_batch_no and not self.item_details.has_serial_no: |
| 85 | msg = f"Item {self.item_code} is not a batch or serial no item" |
| 86 | |
| 87 | if self.item_details.has_serial_no and not self.item_details.serial_no_series: |
| 88 | msg += f". If you want auto pick serial bundle, then kindly set Serial No Series in Item {self.item_code}" |
| 89 | |
| 90 | if ( |
| 91 | self.item_details.has_batch_no |
| 92 | and not self.item_details.batch_number_series |
| 93 | and not frappe.db.get_single_value("Stock Settings", "naming_series_prefix") |
| 94 | ): |
| 95 | msg += f". If you want auto pick batch bundle, then kindly set Batch Number Series in Item {self.item_code}" |
| 96 | |
| 97 | elif self.sle.actual_qty < 0: |
| 98 | if not frappe.db.get_single_value( |
| 99 | "Stock Settings", "auto_create_serial_and_batch_bundle_for_outward" |
| 100 | ): |
| 101 | msg += ". If you want auto pick serial/batch bundle, then kindly enable 'Auto Create Serial and Batch Bundle' in Stock Settings." |
| 102 | |
| 103 | if msg: |
| 104 | error_msg = ( |
| 105 | f"Serial and Batch Bundle not set for item {self.item_code} in warehouse {self.warehouse}." |
| 106 | + msg |
| 107 | ) |
| 108 | frappe.throw(_(error_msg)) |
| 109 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 110 | def set_serial_and_batch_bundle(self, sn_doc): |
| 111 | self.sle.db_set("serial_and_batch_bundle", sn_doc.name) |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 112 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 113 | if sn_doc.is_rejected: |
| 114 | frappe.db.set_value( |
| 115 | self.child_doctype, self.sle.voucher_detail_no, "rejected_serial_and_batch_bundle", sn_doc.name |
| 116 | ) |
| 117 | else: |
| 118 | frappe.db.set_value( |
| 119 | self.child_doctype, self.sle.voucher_detail_no, "serial_and_batch_bundle", sn_doc.name |
| 120 | ) |
| 121 | |
| 122 | @property |
| 123 | def child_doctype(self): |
| 124 | child_doctype = self.sle.voucher_type + " Item" |
| 125 | if self.sle.voucher_type == "Stock Entry": |
| 126 | child_doctype = "Stock Entry Detail" |
| 127 | |
| 128 | return child_doctype |
| 129 | |
| 130 | def is_rejected_entry(self): |
| 131 | return is_rejected(self.sle.voucher_type, self.sle.voucher_detail_no, self.sle.warehouse) |
| 132 | |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 133 | def process_batch_no(self): |
| 134 | if ( |
| 135 | not self.sle.is_cancelled |
| 136 | and not self.sle.serial_and_batch_bundle |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 137 | and self.item_details.has_batch_no == 1 |
| 138 | and self.item_details.create_new_batch |
| 139 | and self.item_details.batch_number_series |
| 140 | ): |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 141 | self.make_serial_batch_no_bundle() |
| 142 | elif not self.sle.is_cancelled: |
| 143 | self.validate_item_and_warehouse() |
| 144 | |
| 145 | def validate_item_and_warehouse(self): |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 146 | if self.sle.serial_and_batch_bundle and not frappe.db.exists( |
| 147 | "Serial and Batch Bundle", |
| 148 | { |
| 149 | "name": self.sle.serial_and_batch_bundle, |
| 150 | "item_code": self.item_code, |
| 151 | "warehouse": self.warehouse, |
| 152 | "voucher_no": self.sle.voucher_no, |
| 153 | }, |
| 154 | ): |
| 155 | msg = f""" |
| 156 | The Serial and Batch Bundle |
| 157 | {bold(self.sle.serial_and_batch_bundle)} |
| 158 | does not belong to Item {bold(self.item_code)} |
| 159 | or Warehouse {bold(self.warehouse)} |
| 160 | or {self.sle.voucher_type} no {bold(self.sle.voucher_no)} |
| 161 | """ |
| 162 | |
| 163 | frappe.throw(_(msg)) |
| 164 | |
| 165 | def delink_serial_and_batch_bundle(self): |
| 166 | update_values = { |
| 167 | "serial_and_batch_bundle": "", |
| 168 | } |
| 169 | |
| 170 | if is_rejected(self.sle.voucher_type, self.sle.voucher_detail_no, self.sle.warehouse): |
| 171 | update_values["rejected_serial_and_batch_bundle"] = "" |
| 172 | |
| 173 | frappe.db.set_value(self.child_doctype, self.sle.voucher_detail_no, update_values) |
| 174 | |
| 175 | frappe.db.set_value( |
| 176 | "Serial and Batch Bundle", |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 177 | {"voucher_no": self.sle.voucher_no, "voucher_type": self.sle.voucher_type}, |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 178 | {"is_cancelled": 1, "voucher_no": ""}, |
| 179 | ) |
| 180 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 181 | def post_process(self): |
Rohit Waghchaure | 674bd3e | 2023-03-17 16:42:59 +0530 | [diff] [blame] | 182 | if not self.sle.serial_and_batch_bundle: |
| 183 | return |
| 184 | |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 185 | if self.item_details.has_serial_no == 1: |
| 186 | self.set_warehouse_and_status_in_serial_nos() |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 187 | |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 188 | if ( |
| 189 | self.sle.actual_qty > 0 |
| 190 | and self.item_details.has_serial_no == 1 |
| 191 | and self.item_details.has_batch_no == 1 |
| 192 | ): |
| 193 | self.set_batch_no_in_serial_nos() |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 194 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 195 | if self.item_details.has_batch_no == 1: |
| 196 | self.update_batch_qty() |
| 197 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 198 | def set_warehouse_and_status_in_serial_nos(self): |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 199 | serial_nos = get_serial_nos(self.sle.serial_and_batch_bundle, check_outward=False) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 200 | warehouse = self.warehouse if self.sle.actual_qty > 0 else None |
| 201 | |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 202 | if not serial_nos: |
| 203 | return |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 204 | |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 205 | sn_table = frappe.qb.DocType("Serial No") |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 206 | ( |
| 207 | frappe.qb.update(sn_table) |
| 208 | .set(sn_table.warehouse, warehouse) |
| 209 | .set(sn_table.status, "Active" if warehouse else "Inactive") |
| 210 | .where(sn_table.name.isin(serial_nos)) |
| 211 | ).run() |
| 212 | |
| 213 | def set_batch_no_in_serial_nos(self): |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 214 | entries = frappe.get_all( |
| 215 | "Serial and Batch Entry", |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 216 | fields=["serial_no", "batch_no"], |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 217 | filters={"parent": self.sle.serial_and_batch_bundle}, |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 218 | ) |
| 219 | |
| 220 | batch_serial_nos = {} |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 221 | for ledger in entries: |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 222 | batch_serial_nos.setdefault(ledger.batch_no, []).append(ledger.serial_no) |
| 223 | |
| 224 | for batch_no, serial_nos in batch_serial_nos.items(): |
| 225 | sn_table = frappe.qb.DocType("Serial No") |
| 226 | ( |
| 227 | frappe.qb.update(sn_table) |
| 228 | .set(sn_table.batch_no, batch_no) |
| 229 | .where(sn_table.name.isin(serial_nos)) |
| 230 | ).run() |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 231 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 232 | def update_batch_qty(self): |
| 233 | from erpnext.stock.doctype.batch.batch import get_available_batches |
| 234 | |
| 235 | batches = get_batch_nos(self.sle.serial_and_batch_bundle) |
| 236 | |
| 237 | batches_qty = get_available_batches( |
| 238 | frappe._dict( |
| 239 | {"item_code": self.item_code, "warehouse": self.warehouse, "batch_no": list(batches.keys())} |
| 240 | ) |
| 241 | ) |
| 242 | |
| 243 | for batch_no, qty in batches_qty.items(): |
| 244 | frappe.db.set_value("Batch", batch_no, "batch_qty", qty) |
| 245 | |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 246 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 247 | def get_serial_nos(serial_and_batch_bundle, check_outward=True): |
| 248 | filters = {"parent": serial_and_batch_bundle} |
| 249 | if check_outward: |
| 250 | filters["is_outward"] = 1 |
| 251 | |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 252 | entries = frappe.get_all("Serial and Batch Entry", fields=["serial_no"], filters=filters) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 253 | |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 254 | return [d.serial_no for d in entries] |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 255 | |
| 256 | |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 257 | class SerialNoValuation(DeprecatedSerialNoValuation): |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 258 | def __init__(self, **kwargs): |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 259 | for key, value in kwargs.items(): |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 260 | setattr(self, key, value) |
| 261 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 262 | self.calculate_stock_value_change() |
| 263 | self.calculate_valuation_rate() |
| 264 | |
| 265 | def calculate_stock_value_change(self): |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 266 | if self.sle.actual_qty > 0: |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 267 | self.stock_value_change = frappe.get_cached_value( |
| 268 | "Serial and Batch Bundle", self.sle.serial_and_batch_bundle, "total_amount" |
| 269 | ) |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 270 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 271 | else: |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 272 | entries = self.get_serial_no_ledgers() |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 273 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 274 | self.serial_no_incoming_rate = defaultdict(float) |
| 275 | self.stock_value_change = 0.0 |
| 276 | |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 277 | for ledger in entries: |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 278 | self.stock_value_change += ledger.incoming_rate * -1 |
| 279 | self.serial_no_incoming_rate[ledger.serial_no] = ledger.incoming_rate |
| 280 | |
| 281 | self.calculate_stock_value_from_deprecarated_ledgers() |
| 282 | |
| 283 | def get_serial_no_ledgers(self): |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 284 | serial_nos = self.get_serial_nos() |
| 285 | |
| 286 | subquery = f""" |
| 287 | SELECT |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 288 | MAX( |
| 289 | TIMESTAMP( |
| 290 | parent.posting_date, parent.posting_time |
| 291 | ) |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 292 | ), child.name, child.serial_no, child.warehouse |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 293 | FROM |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 294 | `tabSerial and Batch Bundle` as parent, |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 295 | `tabSerial and Batch Entry` as child |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 296 | WHERE |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 297 | parent.name = child.parent |
| 298 | AND child.serial_no IN ({', '.join([frappe.db.escape(s) for s in serial_nos])}) |
| 299 | AND child.is_outward = 0 |
Rohit Waghchaure | 86da306 | 2023-03-20 14:15:34 +0530 | [diff] [blame] | 300 | AND parent.docstatus = 1 |
Rohit Waghchaure | ba6e144 | 2023-03-22 23:21:47 +0530 | [diff] [blame] | 301 | AND parent.type_of_transaction != 'Maintenance' |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 302 | AND parent.is_cancelled = 0 |
| 303 | AND child.warehouse = {frappe.db.escape(self.sle.warehouse)} |
| 304 | AND parent.item_code = {frappe.db.escape(self.sle.item_code)} |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 305 | AND ( |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 306 | parent.posting_date < '{self.sle.posting_date}' |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 307 | OR ( |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 308 | parent.posting_date = '{self.sle.posting_date}' |
| 309 | AND parent.posting_time <= '{self.sle.posting_time}' |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 310 | ) |
| 311 | ) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 312 | GROUP BY |
| 313 | child.serial_no |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 314 | """ |
| 315 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 316 | return frappe.db.sql( |
| 317 | f""" |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 318 | SELECT |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 319 | ledger.serial_no, ledger.incoming_rate, ledger.warehouse |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 320 | FROM |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 321 | `tabSerial and Batch Entry` AS ledger, |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 322 | ({subquery}) AS SubQuery |
| 323 | WHERE |
| 324 | ledger.name = SubQuery.name |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 325 | AND ledger.serial_no = SubQuery.serial_no |
| 326 | AND ledger.warehouse = SubQuery.warehouse |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 327 | GROUP BY |
| 328 | ledger.serial_no |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 329 | Order By |
| 330 | ledger.creation |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 331 | """, |
| 332 | as_dict=1, |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 333 | ) |
| 334 | |
| 335 | def get_serial_nos(self): |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 336 | if self.sle.get("serial_nos"): |
| 337 | return self.sle.serial_nos |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 338 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 339 | return get_serial_nos(self.sle.serial_and_batch_bundle) |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 340 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 341 | def calculate_valuation_rate(self): |
| 342 | if not hasattr(self, "wh_data"): |
| 343 | return |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 344 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 345 | 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] | 346 | |
| 347 | if new_stock_qty > 0: |
| 348 | new_stock_value = ( |
| 349 | self.wh_data.qty_after_transaction * self.wh_data.valuation_rate |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 350 | ) + self.stock_value_change |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 351 | if new_stock_value >= 0: |
| 352 | # calculate new valuation rate only if stock value is positive |
| 353 | # else it remains the same as that of previous entry |
| 354 | self.wh_data.valuation_rate = new_stock_value / new_stock_qty |
| 355 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 356 | if ( |
| 357 | not self.wh_data.valuation_rate and self.sle.voucher_detail_no and not self.is_rejected_entry() |
| 358 | ): |
| 359 | allow_zero_rate = self.sle_self.check_if_allow_zero_valuation_rate( |
| 360 | self.sle.voucher_type, self.sle.voucher_detail_no |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 361 | ) |
| 362 | if not allow_zero_rate: |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 363 | 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] | 364 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 365 | self.wh_data.qty_after_transaction += self.sle.actual_qty |
| 366 | self.wh_data.stock_value = flt(self.wh_data.qty_after_transaction) * flt( |
| 367 | self.wh_data.valuation_rate |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 368 | ) |
| 369 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 370 | def is_rejected_entry(self): |
| 371 | 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] | 372 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 373 | def get_incoming_rate(self): |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 374 | return abs(flt(self.stock_value_change) / flt(self.sle.actual_qty)) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 375 | |
| 376 | |
| 377 | def is_rejected(voucher_type, voucher_detail_no, warehouse): |
| 378 | if voucher_type in ["Purchase Receipt", "Purchase Invoice"]: |
| 379 | return warehouse == frappe.get_cached_value( |
| 380 | voucher_type + " Item", voucher_detail_no, "rejected_warehouse" |
| 381 | ) |
| 382 | |
| 383 | return False |
| 384 | |
| 385 | |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 386 | class BatchNoValuation(DeprecatedBatchNoValuation): |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 387 | def __init__(self, **kwargs): |
| 388 | for key, value in kwargs.items(): |
| 389 | setattr(self, key, value) |
| 390 | |
| 391 | self.batch_nos = self.get_batch_nos() |
| 392 | self.calculate_avg_rate() |
| 393 | self.calculate_valuation_rate() |
| 394 | |
| 395 | def calculate_avg_rate(self): |
| 396 | if self.sle.actual_qty > 0: |
| 397 | self.stock_value_change = frappe.get_cached_value( |
| 398 | "Serial and Batch Bundle", self.sle.serial_and_batch_bundle, "total_amount" |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 399 | ) |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 400 | else: |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 401 | entries = self.get_batch_no_ledgers() |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 402 | |
| 403 | self.batch_avg_rate = defaultdict(float) |
Rohit Waghchaure | 86da306 | 2023-03-20 14:15:34 +0530 | [diff] [blame] | 404 | self.available_qty = defaultdict(float) |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 405 | |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 406 | for ledger in entries: |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 407 | self.batch_avg_rate[ledger.batch_no] += flt(ledger.incoming_rate) / flt(ledger.qty) |
Rohit Waghchaure | 86da306 | 2023-03-20 14:15:34 +0530 | [diff] [blame] | 408 | self.available_qty[ledger.batch_no] += flt(ledger.qty) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 409 | |
| 410 | self.calculate_avg_rate_from_deprecarated_ledgers() |
| 411 | self.set_stock_value_difference() |
| 412 | |
| 413 | def get_batch_no_ledgers(self) -> List[dict]: |
| 414 | parent = frappe.qb.DocType("Serial and Batch Bundle") |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 415 | child = frappe.qb.DocType("Serial and Batch Entry") |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 416 | |
| 417 | batch_nos = list(self.batch_nos.keys()) |
| 418 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 419 | timestamp_condition = "" |
| 420 | if self.sle.posting_date and self.sle.posting_time: |
| 421 | timestamp_condition = CombineDatetime( |
| 422 | parent.posting_date, parent.posting_time |
| 423 | ) < CombineDatetime(self.sle.posting_date, self.sle.posting_time) |
Rohit Waghchaure | 86da306 | 2023-03-20 14:15:34 +0530 | [diff] [blame] | 424 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 425 | query = ( |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 426 | frappe.qb.from_(parent) |
| 427 | .inner_join(child) |
| 428 | .on(parent.name == child.parent) |
| 429 | .select( |
| 430 | child.batch_no, |
| 431 | Sum(child.stock_value_difference).as_("incoming_rate"), |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 432 | Sum(child.qty).as_("qty"), |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 433 | ) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 434 | .where( |
| 435 | (child.batch_no.isin(batch_nos)) |
| 436 | & (child.parent != self.sle.serial_and_batch_bundle) |
| 437 | & (parent.warehouse == self.sle.warehouse) |
| 438 | & (parent.item_code == self.sle.item_code) |
Rohit Waghchaure | 86da306 | 2023-03-20 14:15:34 +0530 | [diff] [blame] | 439 | & (parent.docstatus == 1) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 440 | & (parent.is_cancelled == 0) |
Rohit Waghchaure | ba6e144 | 2023-03-22 23:21:47 +0530 | [diff] [blame] | 441 | & (parent.type_of_transaction != "Maintenance") |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 442 | ) |
| 443 | .groupby(child.batch_no) |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 444 | ) |
| 445 | |
| 446 | if timestamp_condition: |
| 447 | query.where(timestamp_condition) |
| 448 | |
| 449 | return query.run(as_dict=True) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 450 | |
| 451 | def get_batch_nos(self) -> list: |
| 452 | if self.sle.get("batch_nos"): |
| 453 | return self.sle.batch_nos |
| 454 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 455 | return get_batch_nos(self.sle.serial_and_batch_bundle) |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 456 | |
| 457 | def set_stock_value_difference(self): |
| 458 | self.stock_value_change = 0 |
| 459 | for batch_no, ledger in self.batch_nos.items(): |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 460 | stock_value_change = self.batch_avg_rate[batch_no] * ledger.qty |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 461 | self.stock_value_change += stock_value_change |
| 462 | frappe.db.set_value( |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 463 | "Serial and Batch Entry", ledger.name, "stock_value_difference", stock_value_change |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 464 | ) |
| 465 | |
| 466 | def calculate_valuation_rate(self): |
| 467 | if not hasattr(self, "wh_data"): |
| 468 | return |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 469 | |
| 470 | self.wh_data.stock_value = round_off_if_near_zero( |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 471 | self.wh_data.stock_value + self.stock_value_change |
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 | |
Rohit Waghchaure | f1b5966 | 2023-03-06 12:08:28 +0530 | [diff] [blame] | 474 | if self.wh_data.qty_after_transaction: |
| 475 | self.wh_data.valuation_rate = self.wh_data.stock_value / self.wh_data.qty_after_transaction |
| 476 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 477 | self.wh_data.qty_after_transaction += self.sle.actual_qty |
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 | def get_incoming_rate(self): |
Rohit Waghchaure | 5ddd55a | 2023-03-16 12:58:48 +0530 | [diff] [blame] | 480 | return abs(flt(self.stock_value_change) / flt(self.sle.actual_qty)) |
Rohit Waghchaure | 16f26fb | 2023-03-20 22:56:06 +0530 | [diff] [blame] | 481 | |
| 482 | |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 483 | def get_batch_nos(serial_and_batch_bundle): |
| 484 | entries = frappe.get_all( |
| 485 | "Serial and Batch Entry", |
| 486 | fields=["batch_no", "qty", "name"], |
| 487 | filters={"parent": serial_and_batch_bundle, "is_outward": 1}, |
| 488 | ) |
| 489 | |
| 490 | return {d.batch_no: d for d in entries} |
| 491 | |
| 492 | |
Rohit Waghchaure | 16f26fb | 2023-03-20 22:56:06 +0530 | [diff] [blame] | 493 | def get_empty_batches_based_work_order(work_order, item_code): |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 494 | batches = get_batches_from_work_order(work_order, item_code) |
Rohit Waghchaure | 16f26fb | 2023-03-20 22:56:06 +0530 | [diff] [blame] | 495 | if not batches: |
| 496 | return batches |
| 497 | |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 498 | entries = get_batches_from_stock_entries(work_order, item_code) |
Rohit Waghchaure | 16f26fb | 2023-03-20 22:56:06 +0530 | [diff] [blame] | 499 | if not entries: |
| 500 | return batches |
| 501 | |
| 502 | ids = [d.serial_and_batch_bundle for d in entries if d.serial_and_batch_bundle] |
| 503 | if ids: |
| 504 | set_batch_details_from_package(ids, batches) |
| 505 | |
| 506 | # Will be deprecated in v16 |
| 507 | for d in entries: |
| 508 | if not d.batch_no: |
| 509 | continue |
| 510 | |
| 511 | batches[d.batch_no] -= d.qty |
| 512 | |
| 513 | return batches |
| 514 | |
| 515 | |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 516 | def get_batches_from_work_order(work_order, item_code): |
Rohit Waghchaure | 16f26fb | 2023-03-20 22:56:06 +0530 | [diff] [blame] | 517 | return frappe._dict( |
| 518 | frappe.get_all( |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 519 | "Batch", |
| 520 | fields=["name", "qty_to_produce"], |
| 521 | filters={"reference_name": work_order, "item": item_code}, |
| 522 | as_list=1, |
Rohit Waghchaure | 16f26fb | 2023-03-20 22:56:06 +0530 | [diff] [blame] | 523 | ) |
| 524 | ) |
| 525 | |
| 526 | |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 527 | def get_batches_from_stock_entries(work_order, item_code): |
Rohit Waghchaure | 16f26fb | 2023-03-20 22:56:06 +0530 | [diff] [blame] | 528 | entries = frappe.get_all( |
| 529 | "Stock Entry", |
| 530 | filters={"work_order": work_order, "docstatus": 1, "purpose": "Manufacture"}, |
| 531 | fields=["name"], |
| 532 | ) |
| 533 | |
| 534 | return frappe.get_all( |
| 535 | "Stock Entry Detail", |
| 536 | fields=["batch_no", "qty", "serial_and_batch_bundle"], |
| 537 | filters={ |
| 538 | "parent": ("in", [d.name for d in entries]), |
| 539 | "is_finished_item": 1, |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 540 | "item_code": item_code, |
Rohit Waghchaure | 16f26fb | 2023-03-20 22:56:06 +0530 | [diff] [blame] | 541 | }, |
| 542 | ) |
| 543 | |
| 544 | |
| 545 | def set_batch_details_from_package(ids, batches): |
| 546 | entries = frappe.get_all( |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 547 | "Serial and Batch Entry", |
Rohit Waghchaure | 16f26fb | 2023-03-20 22:56:06 +0530 | [diff] [blame] | 548 | filters={"parent": ("in", ids), "is_outward": 0}, |
| 549 | fields=["batch_no", "qty"], |
| 550 | ) |
| 551 | |
| 552 | for d in entries: |
| 553 | batches[d.batch_no] -= d.qty |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 554 | |
| 555 | |
| 556 | class SerialBatchCreation: |
| 557 | def __init__(self, args): |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 558 | self.set(args) |
| 559 | self.set_item_details() |
Rohit Waghchaure | 9b72845 | 2023-03-28 14:03:59 +0530 | [diff] [blame] | 560 | self.set_other_details() |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 561 | |
| 562 | def set(self, args): |
| 563 | self.__dict__ = {} |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 564 | for key, value in args.items(): |
| 565 | setattr(self, key, value) |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 566 | self.__dict__[key] = value |
| 567 | |
| 568 | def get(self, key): |
| 569 | return self.__dict__.get(key) |
| 570 | |
| 571 | def set_item_details(self): |
| 572 | fields = [ |
| 573 | "has_batch_no", |
| 574 | "has_serial_no", |
| 575 | "item_name", |
| 576 | "item_group", |
| 577 | "serial_no_series", |
| 578 | "create_new_batch", |
| 579 | "batch_number_series", |
| 580 | "description", |
| 581 | ] |
| 582 | |
| 583 | item_details = frappe.get_cached_value("Item", self.item_code, fields, as_dict=1) |
| 584 | for key, value in item_details.items(): |
| 585 | setattr(self, key, value) |
| 586 | |
| 587 | self.__dict__.update(item_details) |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 588 | |
Rohit Waghchaure | 9b72845 | 2023-03-28 14:03:59 +0530 | [diff] [blame] | 589 | def set_other_details(self): |
| 590 | if not self.get("posting_date"): |
| 591 | setattr(self, "posting_date", today()) |
| 592 | self.__dict__["posting_date"] = self.posting_date |
| 593 | |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 594 | def duplicate_package(self): |
| 595 | if not self.serial_and_batch_bundle: |
| 596 | return |
| 597 | |
| 598 | id = self.serial_and_batch_bundle |
| 599 | package = frappe.get_doc("Serial and Batch Bundle", id) |
| 600 | new_package = frappe.copy_doc(package) |
| 601 | new_package.type_of_transaction = self.type_of_transaction |
Rohit Waghchaure | 0eaf6de | 2023-03-23 15:13:45 +0530 | [diff] [blame] | 602 | new_package.returned_against = self.returned_against |
Rohit Waghchaure | 4670464 | 2023-03-23 11:41:20 +0530 | [diff] [blame] | 603 | new_package.save() |
| 604 | |
| 605 | self.serial_and_batch_bundle = new_package.name |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 606 | |
| 607 | def make_serial_and_batch_bundle(self): |
| 608 | doc = frappe.new_doc("Serial and Batch Bundle") |
| 609 | valid_columns = doc.meta.get_valid_columns() |
| 610 | for key, value in self.__dict__.items(): |
| 611 | if key in valid_columns: |
| 612 | doc.set(key, value) |
| 613 | |
| 614 | if self.type_of_transaction == "Outward": |
| 615 | self.set_auto_serial_batch_entries_for_outward() |
| 616 | elif self.type_of_transaction == "Inward": |
| 617 | self.set_auto_serial_batch_entries_for_inward() |
| 618 | |
| 619 | self.set_serial_batch_entries(doc) |
Rohit Waghchaure | 9b72845 | 2023-03-28 14:03:59 +0530 | [diff] [blame] | 620 | doc.set_incoming_rate() |
Rohit Waghchaure | 648efca | 2023-03-28 12:16:27 +0530 | [diff] [blame] | 621 | doc.save() |
| 622 | |
| 623 | if not hasattr(self, "do_not_submit") or not self.do_not_submit: |
| 624 | doc.submit() |
| 625 | |
| 626 | return doc |
| 627 | |
| 628 | def set_auto_serial_batch_entries_for_outward(self): |
| 629 | from erpnext.stock.doctype.batch.batch import get_available_batches |
| 630 | from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos_for_outward |
| 631 | |
| 632 | kwargs = frappe._dict( |
| 633 | { |
| 634 | "item_code": self.item_code, |
| 635 | "warehouse": self.warehouse, |
| 636 | "qty": abs(self.total_qty), |
| 637 | "based_on": frappe.db.get_single_value("Stock Settings", "pick_serial_and_batch_based_on"), |
| 638 | } |
| 639 | ) |
| 640 | |
| 641 | if self.has_serial_no and not self.get("serial_nos"): |
| 642 | self.serial_nos = get_serial_nos_for_outward(kwargs) |
Rohit Waghchaure | 9b72845 | 2023-03-28 14:03:59 +0530 | [diff] [blame] | 643 | 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] | 644 | self.batches = get_available_batches(kwargs) |
| 645 | |
| 646 | def set_auto_serial_batch_entries_for_inward(self): |
| 647 | self.batch_no = None |
| 648 | if self.has_batch_no: |
| 649 | self.batch_no = self.create_batch() |
| 650 | |
| 651 | if self.has_serial_no: |
| 652 | self.serial_nos = self.get_auto_created_serial_nos() |
| 653 | else: |
| 654 | self.batches = frappe._dict({self.batch_no: abs(self.total_qty)}) |
| 655 | |
| 656 | def set_serial_batch_entries(self, doc): |
| 657 | if self.get("serial_nos"): |
| 658 | serial_no_wise_batch = frappe._dict({}) |
| 659 | if self.has_batch_no: |
| 660 | serial_no_wise_batch = self.get_serial_nos_batch(self.serial_nos) |
| 661 | |
| 662 | qty = -1 if self.type_of_transaction == "Outward" else 1 |
| 663 | for serial_no in self.serial_nos: |
| 664 | doc.append( |
| 665 | "entries", |
| 666 | { |
| 667 | "serial_no": serial_no, |
| 668 | "qty": qty, |
| 669 | "batch_no": serial_no_wise_batch.get(serial_no) or self.get("batch_no"), |
| 670 | "incoming_rate": self.get("incoming_rate"), |
| 671 | }, |
| 672 | ) |
| 673 | |
| 674 | if self.get("batches"): |
| 675 | for batch_no, batch_qty in self.batches.items(): |
| 676 | doc.append( |
| 677 | "entries", |
| 678 | { |
| 679 | "batch_no": batch_no, |
| 680 | "qty": batch_qty * (-1 if self.type_of_transaction == "Outward" else 1), |
| 681 | "incoming_rate": self.get("incoming_rate"), |
| 682 | }, |
| 683 | ) |
| 684 | |
| 685 | def get_serial_nos_batch(self, serial_nos): |
| 686 | return frappe._dict( |
| 687 | frappe.get_all( |
| 688 | "Serial No", |
| 689 | fields=["name", "batch_no"], |
| 690 | filters={"name": ("in", serial_nos)}, |
| 691 | as_list=1, |
| 692 | ) |
| 693 | ) |
| 694 | |
| 695 | def create_batch(self): |
| 696 | from erpnext.stock.doctype.batch.batch import make_batch |
| 697 | |
| 698 | return make_batch( |
| 699 | frappe._dict( |
| 700 | { |
| 701 | "item": self.item_code, |
| 702 | "reference_doctype": self.voucher_type, |
| 703 | "reference_name": self.voucher_no, |
| 704 | } |
| 705 | ) |
| 706 | ) |
| 707 | |
| 708 | def get_auto_created_serial_nos(self): |
| 709 | sr_nos = [] |
| 710 | serial_nos_details = [] |
| 711 | |
| 712 | for i in range(abs(cint(self.total_qty))): |
| 713 | serial_no = make_autoname(self.serial_no_series, "Serial No") |
| 714 | sr_nos.append(serial_no) |
| 715 | serial_nos_details.append( |
| 716 | ( |
| 717 | serial_no, |
| 718 | serial_no, |
| 719 | now(), |
| 720 | now(), |
| 721 | frappe.session.user, |
| 722 | frappe.session.user, |
| 723 | self.warehouse, |
| 724 | self.company, |
| 725 | self.item_code, |
| 726 | self.item_name, |
| 727 | self.description, |
| 728 | "Active", |
| 729 | self.batch_no, |
| 730 | ) |
| 731 | ) |
| 732 | |
| 733 | if serial_nos_details: |
| 734 | fields = [ |
| 735 | "name", |
| 736 | "serial_no", |
| 737 | "creation", |
| 738 | "modified", |
| 739 | "owner", |
| 740 | "modified_by", |
| 741 | "warehouse", |
| 742 | "company", |
| 743 | "item_code", |
| 744 | "item_name", |
| 745 | "description", |
| 746 | "status", |
| 747 | "batch_no", |
| 748 | ] |
| 749 | |
| 750 | frappe.db.bulk_insert("Serial No", fields=fields, values=set(serial_nos_details)) |
| 751 | |
| 752 | return sr_nos |
| 753 | |
| 754 | |
| 755 | def get_serial_or_batch_items(items): |
| 756 | serial_or_batch_items = frappe.get_all( |
| 757 | "Item", |
| 758 | filters={"name": ("in", [d.item_code for d in items])}, |
| 759 | or_filters={"has_serial_no": 1, "has_batch_no": 1}, |
| 760 | ) |
| 761 | |
| 762 | if not serial_or_batch_items: |
| 763 | return |
| 764 | else: |
| 765 | serial_or_batch_items = [d.name for d in serial_or_batch_items] |
| 766 | |
| 767 | return serial_or_batch_items |