Merge branch 'develop' into crm-carry-forward-communication-comments
This commit is contained in:
		
						commit
						6b9dfaea72
					
				| @ -184,6 +184,8 @@ class AccountsController(TransactionBase): | ||||
| 					frappe.throw(_("Row #{0}: Service Start Date cannot be greater than Service End Date").format(d.idx)) | ||||
| 				elif getdate(self.posting_date) > getdate(d.service_end_date): | ||||
| 					frappe.throw(_("Row #{0}: Service End Date cannot be before Invoice Posting Date").format(d.idx)) | ||||
| 				elif getdate(self.posting_date) > getdate(d.service_start_date): | ||||
| 					frappe.throw(_("Row #{0}: Service Start Date cannot be before Invoice Posting Date").format(d.idx)) | ||||
| 
 | ||||
| 	def validate_invoice_documents_schedule(self): | ||||
| 		self.validate_payment_schedule_dates() | ||||
|  | ||||
| @ -59,22 +59,16 @@ frappe.ui.form.on("Project", { | ||||
| 
 | ||||
| 			frm.trigger('show_dashboard'); | ||||
| 		} | ||||
| 		frm.events.set_buttons(frm); | ||||
| 		frm.trigger("set_custom_buttons"); | ||||
| 	}, | ||||
| 
 | ||||
| 	set_buttons: function(frm) { | ||||
| 	set_custom_buttons: function(frm) { | ||||
| 		if (!frm.is_new()) { | ||||
| 			frm.add_custom_button(__('Duplicate Project with Tasks'), () => { | ||||
| 				frm.events.create_duplicate(frm); | ||||
| 			}); | ||||
| 			}, __("Actions")); | ||||
| 
 | ||||
| 			frm.add_custom_button(__('Completed'), () => { | ||||
| 				frm.events.set_status(frm, 'Completed'); | ||||
| 			}, __('Set Status')); | ||||
| 
 | ||||
| 			frm.add_custom_button(__('Cancelled'), () => { | ||||
| 				frm.events.set_status(frm, 'Cancelled'); | ||||
| 			}, __('Set Status')); | ||||
| 			frm.trigger("set_project_status_button"); | ||||
| 
 | ||||
| 
 | ||||
| 			if (frappe.model.can_read("Task")) { | ||||
| @ -83,7 +77,7 @@ frappe.ui.form.on("Project", { | ||||
| 						"project": frm.doc.name | ||||
| 					}; | ||||
| 					frappe.set_route("List", "Task", "Gantt"); | ||||
| 				}); | ||||
| 				}, __("View")); | ||||
| 
 | ||||
| 				frm.add_custom_button(__("Kanban Board"), () => { | ||||
| 					frappe.call('erpnext.projects.doctype.project.project.create_kanban_board_if_not_exists', { | ||||
| @ -91,13 +85,35 @@ frappe.ui.form.on("Project", { | ||||
| 					}).then(() => { | ||||
| 						frappe.set_route('List', 'Task', 'Kanban', frm.doc.project_name); | ||||
| 					}); | ||||
| 				}); | ||||
| 				}, __("View")); | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 
 | ||||
| 	}, | ||||
| 
 | ||||
| 	set_project_status_button: function(frm) { | ||||
| 		frm.add_custom_button(__('Set Project Status'), () => { | ||||
| 			let d = new frappe.ui.Dialog({ | ||||
| 				"title": __("Set Project Status"), | ||||
| 				"fields": [ | ||||
| 					{ | ||||
| 						"fieldname": "status", | ||||
| 						"fieldtype": "Select", | ||||
| 						"label": "Status", | ||||
| 						"reqd": 1, | ||||
| 						"options": "Completed\nCancelled", | ||||
| 					}, | ||||
| 				], | ||||
| 				primary_action: function() { | ||||
| 					frm.events.set_status(frm, d.get_values().status); | ||||
| 					d.hide(); | ||||
| 				}, | ||||
| 				primary_action_label: __("Set Project Status") | ||||
| 			}).show(); | ||||
| 		}, __("Actions")); | ||||
| 	}, | ||||
| 
 | ||||
| 	create_duplicate: function(frm) { | ||||
| 		return new Promise(resolve => { | ||||
| 			frappe.prompt('Project Name', (data) => { | ||||
| @ -117,7 +133,9 @@ frappe.ui.form.on("Project", { | ||||
| 	set_status: function(frm, status) { | ||||
| 		frappe.confirm(__('Set Project and all Tasks to status {0}?', [status.bold()]), () => { | ||||
| 			frappe.xcall('erpnext.projects.doctype.project.project.set_project_status', | ||||
| 				{project: frm.doc.name, status: status}).then(() => { /* page will auto reload */ }); | ||||
| 				{project: frm.doc.name, status: status}).then(() => { | ||||
| 				frm.reload_doc(); | ||||
| 			}); | ||||
| 		}); | ||||
| 	}, | ||||
| 
 | ||||
|  | ||||
| @ -3,6 +3,7 @@ | ||||
| 
 | ||||
| 
 | ||||
| from operator import itemgetter | ||||
| from typing import Dict, List, Tuple, Union | ||||
| 
 | ||||
| import frappe | ||||
| from frappe import _ | ||||
| @ -10,19 +11,29 @@ from frappe.utils import cint, date_diff, flt | ||||
| 
 | ||||
| from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos | ||||
| 
 | ||||
| Filters = frappe._dict | ||||
| 
 | ||||
| def execute(filters=None): | ||||
| 	columns = get_columns(filters) | ||||
| 	item_details = get_fifo_queue(filters) | ||||
| def execute(filters: Filters = None) -> Tuple: | ||||
| 	to_date = filters["to_date"] | ||||
| 	_func = itemgetter(1) | ||||
| 	columns = get_columns(filters) | ||||
| 
 | ||||
| 	item_details = FIFOSlots(filters).generate() | ||||
| 	data = format_report_data(filters, item_details, to_date) | ||||
| 
 | ||||
| 	chart_data = get_chart_data(data, filters) | ||||
| 
 | ||||
| 	return columns, data, None, chart_data | ||||
| 
 | ||||
| def format_report_data(filters: Filters, item_details: Dict, to_date: str) -> List[Dict]: | ||||
| 	"Returns ordered, formatted data with ranges." | ||||
| 	_func = itemgetter(1) | ||||
| 	data = [] | ||||
| 
 | ||||
| 	for item, item_dict in item_details.items(): | ||||
| 		earliest_age, latest_age = 0, 0 | ||||
| 		details = item_dict["details"] | ||||
| 
 | ||||
| 		fifo_queue = sorted(filter(_func, item_dict["fifo_queue"]), key=_func) | ||||
| 		details = item_dict["details"] | ||||
| 
 | ||||
| 		if not fifo_queue: continue | ||||
| 
 | ||||
| @ -31,23 +42,22 @@ def execute(filters=None): | ||||
| 		latest_age = date_diff(to_date, fifo_queue[-1][1]) | ||||
| 		range1, range2, range3, above_range3 = get_range_age(filters, fifo_queue, to_date, item_dict) | ||||
| 
 | ||||
| 		row = [details.name, details.item_name, | ||||
| 			details.description, details.item_group, details.brand] | ||||
| 		row = [details.name, details.item_name, details.description, | ||||
| 			details.item_group, details.brand] | ||||
| 
 | ||||
| 		if filters.get("show_warehouse_wise_stock"): | ||||
| 			row.append(details.warehouse) | ||||
| 
 | ||||
| 		row.extend([item_dict.get("total_qty"), average_age, | ||||
| 			range1, range2, range3, above_range3, | ||||
| 			earliest_age, latest_age, details.stock_uom]) | ||||
| 			earliest_age, latest_age, | ||||
| 			details.stock_uom]) | ||||
| 
 | ||||
| 		data.append(row) | ||||
| 
 | ||||
| 	chart_data = get_chart_data(data, filters) | ||||
| 	return data | ||||
| 
 | ||||
| 	return columns, data, None, chart_data | ||||
| 
 | ||||
| def get_average_age(fifo_queue, to_date): | ||||
| def get_average_age(fifo_queue: List, to_date: str) -> float: | ||||
| 	batch_age = age_qty = total_qty = 0.0 | ||||
| 	for batch in fifo_queue: | ||||
| 		batch_age = date_diff(to_date, batch[1]) | ||||
| @ -61,7 +71,7 @@ def get_average_age(fifo_queue, to_date): | ||||
| 
 | ||||
| 	return flt(age_qty / total_qty, 2) if total_qty else 0.0 | ||||
| 
 | ||||
| def get_range_age(filters, fifo_queue, to_date, item_dict): | ||||
| def get_range_age(filters: Filters, fifo_queue: List, to_date: str, item_dict: Dict) -> Tuple: | ||||
| 	range1 = range2 = range3 = above_range3 = 0.0 | ||||
| 
 | ||||
| 	for item in fifo_queue: | ||||
| @ -79,7 +89,7 @@ def get_range_age(filters, fifo_queue, to_date, item_dict): | ||||
| 
 | ||||
| 	return range1, range2, range3, above_range3 | ||||
| 
 | ||||
| def get_columns(filters): | ||||
| def get_columns(filters: Filters) -> List[Dict]: | ||||
| 	range_columns = [] | ||||
| 	setup_ageing_columns(filters, range_columns) | ||||
| 	columns = [ | ||||
| @ -164,106 +174,7 @@ def get_columns(filters): | ||||
| 
 | ||||
| 	return columns | ||||
| 
 | ||||
| def get_fifo_queue(filters, sle=None): | ||||
| 	item_details = {} | ||||
| 	transferred_item_details = {} | ||||
| 	serial_no_batch_purchase_details = {} | ||||
| 
 | ||||
| 	if sle == None: | ||||
| 		sle = get_stock_ledger_entries(filters) | ||||
| 
 | ||||
| 	for d in sle: | ||||
| 		key = (d.name, d.warehouse) if filters.get('show_warehouse_wise_stock') else d.name | ||||
| 		item_details.setdefault(key, {"details": d, "fifo_queue": []}) | ||||
| 		fifo_queue = item_details[key]["fifo_queue"] | ||||
| 
 | ||||
| 		transferred_item_key = (d.voucher_no, d.name, d.warehouse) | ||||
| 		transferred_item_details.setdefault(transferred_item_key, []) | ||||
| 
 | ||||
| 		if d.voucher_type == "Stock Reconciliation": | ||||
| 			d.actual_qty = flt(d.qty_after_transaction) - flt(item_details[key].get("qty_after_transaction", 0)) | ||||
| 
 | ||||
| 		serial_no_list = get_serial_nos(d.serial_no) if d.serial_no else [] | ||||
| 
 | ||||
| 		if d.actual_qty > 0: | ||||
| 			if transferred_item_details.get(transferred_item_key): | ||||
| 				batch = transferred_item_details[transferred_item_key][0] | ||||
| 				fifo_queue.append(batch) | ||||
| 				transferred_item_details[transferred_item_key].pop(0) | ||||
| 			else: | ||||
| 				if serial_no_list: | ||||
| 					for serial_no in serial_no_list: | ||||
| 						if serial_no_batch_purchase_details.get(serial_no): | ||||
| 							fifo_queue.append([serial_no, serial_no_batch_purchase_details.get(serial_no)]) | ||||
| 						else: | ||||
| 							serial_no_batch_purchase_details.setdefault(serial_no, d.posting_date) | ||||
| 							fifo_queue.append([serial_no, d.posting_date]) | ||||
| 				else: | ||||
| 					fifo_queue.append([d.actual_qty, d.posting_date]) | ||||
| 		else: | ||||
| 			if serial_no_list: | ||||
| 				fifo_queue[:] = [serial_no for serial_no in fifo_queue if serial_no[0] not in serial_no_list] | ||||
| 			else: | ||||
| 				qty_to_pop = abs(d.actual_qty) | ||||
| 				while qty_to_pop: | ||||
| 					batch = fifo_queue[0] if fifo_queue else [0, None] | ||||
| 					if 0 < flt(batch[0]) <= qty_to_pop: | ||||
| 						# if batch qty > 0 | ||||
| 						# not enough or exactly same qty in current batch, clear batch | ||||
| 						qty_to_pop -= flt(batch[0]) | ||||
| 						transferred_item_details[transferred_item_key].append(fifo_queue.pop(0)) | ||||
| 					else: | ||||
| 						# all from current batch | ||||
| 						batch[0] = flt(batch[0]) - qty_to_pop | ||||
| 						transferred_item_details[transferred_item_key].append([qty_to_pop, batch[1]]) | ||||
| 						qty_to_pop = 0 | ||||
| 
 | ||||
| 		item_details[key]["qty_after_transaction"] = d.qty_after_transaction | ||||
| 
 | ||||
| 		if "total_qty" not in item_details[key]: | ||||
| 			item_details[key]["total_qty"] = d.actual_qty | ||||
| 		else: | ||||
| 			item_details[key]["total_qty"] += d.actual_qty | ||||
| 
 | ||||
| 		item_details[key]["has_serial_no"] = d.has_serial_no | ||||
| 
 | ||||
| 	return item_details | ||||
| 
 | ||||
| def get_stock_ledger_entries(filters): | ||||
| 	return frappe.db.sql("""select | ||||
| 			item.name, item.item_name, item_group, brand, description, item.stock_uom, item.has_serial_no, | ||||
| 			actual_qty, posting_date, voucher_type, voucher_no, serial_no, batch_no, qty_after_transaction, warehouse | ||||
| 		from `tabStock Ledger Entry` sle, | ||||
| 			(select name, item_name, description, stock_uom, brand, item_group, has_serial_no | ||||
| 				from `tabItem` {item_conditions}) item | ||||
| 		where item_code = item.name and | ||||
| 			company = %(company)s and | ||||
| 			posting_date <= %(to_date)s and | ||||
| 			is_cancelled != 1 | ||||
| 			{sle_conditions} | ||||
| 			order by posting_date, posting_time, sle.creation, actual_qty""" #nosec | ||||
| 		.format(item_conditions=get_item_conditions(filters), | ||||
| 			sle_conditions=get_sle_conditions(filters)), filters, as_dict=True) | ||||
| 
 | ||||
| def get_item_conditions(filters): | ||||
| 	conditions = [] | ||||
| 	if filters.get("item_code"): | ||||
| 		conditions.append("item_code=%(item_code)s") | ||||
| 	if filters.get("brand"): | ||||
| 		conditions.append("brand=%(brand)s") | ||||
| 
 | ||||
| 	return "where {}".format(" and ".join(conditions)) if conditions else "" | ||||
| 
 | ||||
| def get_sle_conditions(filters): | ||||
| 	conditions = [] | ||||
| 	if filters.get("warehouse"): | ||||
| 		lft, rgt = frappe.db.get_value('Warehouse', filters.get("warehouse"), ['lft', 'rgt']) | ||||
| 		conditions.append("""warehouse in (select wh.name from `tabWarehouse` wh | ||||
| 			where wh.lft >= {0} and rgt <= {1})""".format(lft, rgt)) | ||||
| 
 | ||||
| 	return "and {}".format(" and ".join(conditions)) if conditions else "" | ||||
| 
 | ||||
| def get_chart_data(data, filters): | ||||
| def get_chart_data(data: List, filters: Filters) -> Dict: | ||||
| 	if not data: | ||||
| 		return [] | ||||
| 
 | ||||
| @ -294,17 +205,201 @@ def get_chart_data(data, filters): | ||||
| 		"type" : "bar" | ||||
| 	} | ||||
| 
 | ||||
| def setup_ageing_columns(filters, range_columns): | ||||
| 	for i, label in enumerate(["0-{range1}".format(range1=filters["range1"]), | ||||
| 		"{range1}-{range2}".format(range1=cint(filters["range1"])+ 1, range2=filters["range2"]), | ||||
| 		"{range2}-{range3}".format(range2=cint(filters["range2"])+ 1, range3=filters["range3"]), | ||||
| 		"{range3}-{above}".format(range3=cint(filters["range3"])+ 1, above=_("Above"))]): | ||||
| 			add_column(range_columns, label="Age ("+ label +")", fieldname='range' + str(i+1)) | ||||
| def setup_ageing_columns(filters: Filters, range_columns: List): | ||||
| 	ranges = [ | ||||
| 		f"0 - {filters['range1']}", | ||||
| 		f"{cint(filters['range1']) + 1} - {cint(filters['range2'])}", | ||||
| 		f"{cint(filters['range2']) + 1} - {cint(filters['range3'])}", | ||||
| 		f"{cint(filters['range3']) + 1} - {_('Above')}" | ||||
| 	] | ||||
| 	for i, label in enumerate(ranges): | ||||
| 		fieldname = 'range' + str(i+1) | ||||
| 		add_column(range_columns, label=f"Age ({label})",fieldname=fieldname) | ||||
| 
 | ||||
| def add_column(range_columns, label, fieldname, fieldtype='Float', width=140): | ||||
| def add_column(range_columns: List, label:str, fieldname: str, fieldtype: str = 'Float', width: int = 140): | ||||
| 	range_columns.append(dict( | ||||
| 		label=label, | ||||
| 		fieldname=fieldname, | ||||
| 		fieldtype=fieldtype, | ||||
| 		width=width | ||||
| 	)) | ||||
| 
 | ||||
| 
 | ||||
| class FIFOSlots: | ||||
| 	"Returns FIFO computed slots of inwarded stock as per date." | ||||
| 
 | ||||
| 	def __init__(self, filters: Dict = None , sle: List = None): | ||||
| 		self.item_details = {} | ||||
| 		self.transferred_item_details = {} | ||||
| 		self.serial_no_batch_purchase_details = {} | ||||
| 		self.filters = filters | ||||
| 		self.sle = sle | ||||
| 
 | ||||
| 	def generate(self) -> Dict: | ||||
| 		""" | ||||
| 			Returns dict of the foll.g structure: | ||||
| 			Key = Item A / (Item A, Warehouse A) | ||||
| 			Key: { | ||||
| 				'details' -> Dict: ** item details **, | ||||
| 				'fifo_queue' -> List: ** list of lists containing entries/slots for existing stock, | ||||
| 					consumed/updated and maintained via FIFO. ** | ||||
| 			} | ||||
| 		""" | ||||
| 		if self.sle is None: | ||||
| 			self.sle = self.__get_stock_ledger_entries() | ||||
| 
 | ||||
| 		for d in self.sle: | ||||
| 			key, fifo_queue, transferred_item_key = self.__init_key_stores(d) | ||||
| 
 | ||||
| 			if d.voucher_type == "Stock Reconciliation": | ||||
| 				prev_balance_qty = self.item_details[key].get("qty_after_transaction", 0) | ||||
| 				d.actual_qty = flt(d.qty_after_transaction) - flt(prev_balance_qty) | ||||
| 
 | ||||
| 			serial_nos = get_serial_nos(d.serial_no) if d.serial_no else [] | ||||
| 
 | ||||
| 			if d.actual_qty > 0: | ||||
| 				self.__compute_incoming_stock(d, fifo_queue, transferred_item_key, serial_nos) | ||||
| 			else: | ||||
| 				self.__compute_outgoing_stock(d, fifo_queue, transferred_item_key, serial_nos) | ||||
| 
 | ||||
| 			self.__update_balances(d, key) | ||||
| 
 | ||||
| 		return self.item_details | ||||
| 
 | ||||
| 	def __init_key_stores(self, row: Dict) -> Tuple: | ||||
| 		"Initialise keys and FIFO Queue." | ||||
| 
 | ||||
| 		key = (row.name, row.warehouse) if self.filters.get('show_warehouse_wise_stock') else row.name | ||||
| 		self.item_details.setdefault(key, {"details": row, "fifo_queue": []}) | ||||
| 		fifo_queue = self.item_details[key]["fifo_queue"] | ||||
| 
 | ||||
| 		transferred_item_key = (row.voucher_no, row.name, row.warehouse) | ||||
| 		self.transferred_item_details.setdefault(transferred_item_key, []) | ||||
| 
 | ||||
| 		return key, fifo_queue, transferred_item_key | ||||
| 
 | ||||
| 	def __compute_incoming_stock(self, row: Dict, fifo_queue: List, transfer_key: Tuple, serial_nos: List): | ||||
| 		"Update FIFO Queue on inward stock." | ||||
| 
 | ||||
| 		if self.transferred_item_details.get(transfer_key): | ||||
| 			# inward/outward from same voucher, item & warehouse | ||||
| 			slot = self.transferred_item_details[transfer_key].pop(0) | ||||
| 			fifo_queue.append(slot) | ||||
| 		else: | ||||
| 			if not serial_nos: | ||||
| 				if fifo_queue and flt(fifo_queue[0][0]) < 0: | ||||
| 					# neutralize negative stock by adding positive stock | ||||
| 					fifo_queue[0][0] += flt(row.actual_qty) | ||||
| 					fifo_queue[0][1] = row.posting_date | ||||
| 				else: | ||||
| 					fifo_queue.append([flt(row.actual_qty), row.posting_date]) | ||||
| 				return | ||||
| 
 | ||||
| 			for serial_no in serial_nos: | ||||
| 				if self.serial_no_batch_purchase_details.get(serial_no): | ||||
| 					fifo_queue.append([serial_no, self.serial_no_batch_purchase_details.get(serial_no)]) | ||||
| 				else: | ||||
| 					self.serial_no_batch_purchase_details.setdefault(serial_no, row.posting_date) | ||||
| 					fifo_queue.append([serial_no, row.posting_date]) | ||||
| 
 | ||||
| 	def __compute_outgoing_stock(self, row: Dict, fifo_queue: List, transfer_key: Tuple, serial_nos: List): | ||||
| 		"Update FIFO Queue on outward stock." | ||||
| 		if serial_nos: | ||||
| 			fifo_queue[:] = [serial_no for serial_no in fifo_queue if serial_no[0] not in serial_nos] | ||||
| 			return | ||||
| 
 | ||||
| 		qty_to_pop = abs(row.actual_qty) | ||||
| 		while qty_to_pop: | ||||
| 			slot = fifo_queue[0] if fifo_queue else [0, None] | ||||
| 			if 0 < flt(slot[0]) <= qty_to_pop: | ||||
| 				# qty to pop >= slot qty | ||||
| 				# if +ve and not enough or exactly same balance in current slot, consume whole slot | ||||
| 				qty_to_pop -= flt(slot[0]) | ||||
| 				self.transferred_item_details[transfer_key].append(fifo_queue.pop(0)) | ||||
| 			elif not fifo_queue: | ||||
| 				# negative stock, no balance but qty yet to consume | ||||
| 				fifo_queue.append([-(qty_to_pop), row.posting_date]) | ||||
| 				self.transferred_item_details[transfer_key].append([row.actual_qty, row.posting_date]) | ||||
| 				qty_to_pop = 0 | ||||
| 			else: | ||||
| 				# qty to pop < slot qty, ample balance | ||||
| 				# consume actual_qty from first slot | ||||
| 				slot[0] = flt(slot[0]) - qty_to_pop | ||||
| 				self.transferred_item_details[transfer_key].append([qty_to_pop, slot[1]]) | ||||
| 				qty_to_pop = 0 | ||||
| 
 | ||||
| 	def __update_balances(self, row: Dict, key: Union[Tuple, str]): | ||||
| 		self.item_details[key]["qty_after_transaction"] = row.qty_after_transaction | ||||
| 
 | ||||
| 		if "total_qty" not in self.item_details[key]: | ||||
| 			self.item_details[key]["total_qty"] = row.actual_qty | ||||
| 		else: | ||||
| 			self.item_details[key]["total_qty"] += row.actual_qty | ||||
| 
 | ||||
| 		self.item_details[key]["has_serial_no"] = row.has_serial_no | ||||
| 
 | ||||
| 	def __get_stock_ledger_entries(self) -> List[Dict]: | ||||
| 		sle = frappe.qb.DocType("Stock Ledger Entry") | ||||
| 		item = self.__get_item_query() # used as derived table in sle query | ||||
| 
 | ||||
| 		sle_query = ( | ||||
| 			frappe.qb.from_(sle).from_(item) | ||||
| 			.select( | ||||
| 				item.name, item.item_name, item.item_group, | ||||
| 				item.brand, item.description, | ||||
| 				item.stock_uom, item.has_serial_no, | ||||
| 				sle.actual_qty, sle.posting_date, | ||||
| 				sle.voucher_type, sle.voucher_no, | ||||
| 				sle.serial_no, sle.batch_no, | ||||
| 				sle.qty_after_transaction, sle.warehouse | ||||
| 			).where( | ||||
| 				(sle.item_code == item.name) | ||||
| 				& (sle.company == self.filters.get("company")) | ||||
| 				& (sle.posting_date <= self.filters.get("to_date")) | ||||
| 				& (sle.is_cancelled != 1) | ||||
| 			) | ||||
| 		) | ||||
| 
 | ||||
| 		if self.filters.get("warehouse"): | ||||
| 			sle_query = self.__get_warehouse_conditions(sle, sle_query) | ||||
| 
 | ||||
| 		sle_query = sle_query.orderby( | ||||
| 			sle.posting_date, sle.posting_time, sle.creation, sle.actual_qty | ||||
| 		) | ||||
| 
 | ||||
| 		return sle_query.run(as_dict=True) | ||||
| 
 | ||||
| 	def __get_item_query(self) -> str: | ||||
| 		item_table = frappe.qb.DocType("Item") | ||||
| 
 | ||||
| 		item = frappe.qb.from_("Item").select( | ||||
| 			"name", "item_name", "description", "stock_uom", | ||||
| 			"brand", "item_group", "has_serial_no" | ||||
| 		) | ||||
| 
 | ||||
| 		if self.filters.get("item_code"): | ||||
| 			item = item.where(item_table.item_code == self.filters.get("item_code")) | ||||
| 
 | ||||
| 		if self.filters.get("brand"): | ||||
| 			item = item.where(item_table.brand == self.filters.get("brand")) | ||||
| 
 | ||||
| 		return item | ||||
| 
 | ||||
| 	def __get_warehouse_conditions(self, sle, sle_query) -> str: | ||||
| 		warehouse = frappe.qb.DocType("Warehouse") | ||||
| 		lft, rgt = frappe.db.get_value( | ||||
| 			"Warehouse", | ||||
| 			self.filters.get("warehouse"), | ||||
| 			['lft', 'rgt'] | ||||
| 		) | ||||
| 
 | ||||
| 		warehouse_results = ( | ||||
| 			frappe.qb.from_(warehouse) | ||||
| 			.select("name").where( | ||||
| 				(warehouse.lft >= lft) | ||||
| 				& (warehouse.rgt <= rgt) | ||||
| 			).run() | ||||
| 		) | ||||
| 		warehouse_results = [x[0] for x in warehouse_results] | ||||
| 
 | ||||
| 		return sle_query.where(sle.warehouse.isin(warehouse_results)) | ||||
|  | ||||
							
								
								
									
										73
									
								
								erpnext/stock/report/stock_ageing/stock_ageing_fifo_logic.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								erpnext/stock/report/stock_ageing/stock_ageing_fifo_logic.md
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,73 @@ | ||||
| ### Concept of FIFO Slots | ||||
| 
 | ||||
| Since we need to know age-wise remaining stock, we maintain all the inward entries as slots. So each time stock comes in, a slot is added for the same. | ||||
| 
 | ||||
| Eg. For Item A: | ||||
| ---------------------- | ||||
| Date | Qty | Queue | ||||
| ---------------------- | ||||
| 1st  | +50 | [[50, 1-12-2021]] | ||||
| 2nd  | +20 | [[50, 1-12-2021], [20, 2-12-2021]] | ||||
| ---------------------- | ||||
| 
 | ||||
| Now the queue can tell us the total stock and also how old the stock is. | ||||
| Here, the balance qty is 70. | ||||
| 50 qty is (today-the 1st) days old | ||||
| 20 qty is (today-the 2nd) days old | ||||
| 
 | ||||
| ### Calculation of FIFO Slots | ||||
| 
 | ||||
| #### Case 1: Outward from sufficient balance qty | ||||
| ---------------------- | ||||
| Date | Qty | Queue | ||||
| ---------------------- | ||||
| 1st  | +50 | [[50, 1-12-2021]] | ||||
| 2nd  | -20 | [[30, 1-12-2021]] | ||||
| 2nd  | +20 | [[30, 1-12-2021], [20, 2-12-2021]] | ||||
| 
 | ||||
| Here after the first entry, while issuing 20 qty: | ||||
| - **since 20 is lesser than the balance**, **qty_to_pop (20)** is simply consumed from first slot (FIFO consumption) | ||||
| - Any inward entry after as usual will get its own slot added to the queue | ||||
| 
 | ||||
| #### Case 2: Outward from sufficient cumulative (slots) balance qty | ||||
| ---------------------- | ||||
| Date | Qty | Queue | ||||
| ---------------------- | ||||
| 1st  | +50 | [[50, 1-12-2021]] | ||||
| 2nd  | +20 | [[50, 1-12-2021], [20, 2-12-2021]] | ||||
| 2nd  | -60 | [[10, 2-12-2021]] | ||||
| 
 | ||||
| - Consumption happens slot wise. First slot 1 is consumed | ||||
| - Since **qty_to_pop (60) is greater than slot 1 qty (50)**, the entire slot is consumed and popped | ||||
| - Now the queue is [[20, 2-12-2021]], and **qty_to_pop=10** (remaining qty to pop) | ||||
| - It then goes ahead to the next slot and consumes 10 from it | ||||
| - Now the queue is [[10, 2-12-2021]] | ||||
| 
 | ||||
| #### Case 3: Outward from insufficient balance qty | ||||
| > This case is possible only if **Allow Negative Stock** was enabled at some point/is enabled. | ||||
| 
 | ||||
| ---------------------- | ||||
| Date | Qty | Queue | ||||
| ---------------------- | ||||
| 1st  | +50 | [[50, 1-12-2021]] | ||||
| 2nd  | -60 | [[-10, 1-12-2021]] | ||||
| 
 | ||||
| - Since **qty_to_pop (60)** is more than the balance in slot 1, the entire slot is consumed and popped | ||||
| - Now the queue is **empty**, and **qty_to_pop=10** (remaining qty to pop) | ||||
| - Since we still have more to consume, we append the balance since 60 is issued from 50 i.e. -10. | ||||
| - We register this negative value, since the stock issue has caused the balance to become negative | ||||
| 
 | ||||
| Now when stock is inwarded: | ||||
| - Instead of adding a slot we check if there are any negative balances. | ||||
| - If yes, we keep adding positive stock to it until we make the balance positive. | ||||
| - Once the balance is positive, the next inward entry will add a new slot in the queue | ||||
| 
 | ||||
| Eg: | ||||
| ---------------------- | ||||
| Date | Qty | Queue | ||||
| ---------------------- | ||||
| 1st  | +50 | [[50, 1-12-2021]] | ||||
| 2nd  | -60 | [[-10, 1-12-2021]] | ||||
| 3rd  | +5  | [[-5, 3-12-2021]] | ||||
| 4th  | +10 | [[5, 4-12-2021]] | ||||
| 4th  | +20 | [[5, 4-12-2021], [20, 4-12-2021]] | ||||
							
								
								
									
										126
									
								
								erpnext/stock/report/stock_ageing/test_stock_ageing.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										126
									
								
								erpnext/stock/report/stock_ageing/test_stock_ageing.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,126 @@ | ||||
| # Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors | ||||
| # See license.txt | ||||
| 
 | ||||
| import frappe | ||||
| 
 | ||||
| from erpnext.stock.report.stock_ageing.stock_ageing import FIFOSlots | ||||
| from erpnext.tests.utils import ERPNextTestCase | ||||
| 
 | ||||
| 
 | ||||
| class TestStockAgeing(ERPNextTestCase): | ||||
| 	def setUp(self) -> None: | ||||
| 		self.filters = frappe._dict( | ||||
| 			company="_Test Company", | ||||
| 			to_date="2021-12-10" | ||||
| 		) | ||||
| 
 | ||||
| 	def test_normal_inward_outward_queue(self): | ||||
| 		"Reference: Case 1 in stock_ageing_fifo_logic.md" | ||||
| 		sle = [ | ||||
| 			frappe._dict( | ||||
| 				name="Flask Item", | ||||
| 				actual_qty=30, qty_after_transaction=30, | ||||
| 				posting_date="2021-12-01", voucher_type="Stock Entry", | ||||
| 				voucher_no="001", | ||||
| 				has_serial_no=False, serial_no=None | ||||
| 			), | ||||
| 			frappe._dict( | ||||
| 				name="Flask Item", | ||||
| 				actual_qty=20, qty_after_transaction=50, | ||||
| 				posting_date="2021-12-02", voucher_type="Stock Entry", | ||||
| 				voucher_no="002", | ||||
| 				has_serial_no=False, serial_no=None | ||||
| 			), | ||||
| 			frappe._dict( | ||||
| 				name="Flask Item", | ||||
| 				actual_qty=(-10), qty_after_transaction=40, | ||||
| 				posting_date="2021-12-03", voucher_type="Stock Entry", | ||||
| 				voucher_no="003", | ||||
| 				has_serial_no=False, serial_no=None | ||||
| 			) | ||||
| 		] | ||||
| 
 | ||||
| 		slots = FIFOSlots(self.filters, sle).generate() | ||||
| 
 | ||||
| 		self.assertTrue(slots["Flask Item"]["fifo_queue"]) | ||||
| 		result = slots["Flask Item"] | ||||
| 		queue = result["fifo_queue"] | ||||
| 
 | ||||
| 		self.assertEqual(result["qty_after_transaction"], result["total_qty"]) | ||||
| 		self.assertEqual(queue[0][0], 20.0) | ||||
| 
 | ||||
| 	def test_insufficient_balance(self): | ||||
| 		"Reference: Case 3 in stock_ageing_fifo_logic.md" | ||||
| 		sle = [ | ||||
| 			frappe._dict( | ||||
| 				name="Flask Item", | ||||
| 				actual_qty=(-30), qty_after_transaction=(-30), | ||||
| 				posting_date="2021-12-01", voucher_type="Stock Entry", | ||||
| 				voucher_no="001", | ||||
| 				has_serial_no=False, serial_no=None | ||||
| 			), | ||||
| 			frappe._dict( | ||||
| 				name="Flask Item", | ||||
| 				actual_qty=20, qty_after_transaction=(-10), | ||||
| 				posting_date="2021-12-02", voucher_type="Stock Entry", | ||||
| 				voucher_no="002", | ||||
| 				has_serial_no=False, serial_no=None | ||||
| 			), | ||||
| 			frappe._dict( | ||||
| 				name="Flask Item", | ||||
| 				actual_qty=20, qty_after_transaction=10, | ||||
| 				posting_date="2021-12-03", voucher_type="Stock Entry", | ||||
| 				voucher_no="003", | ||||
| 				has_serial_no=False, serial_no=None | ||||
| 			), | ||||
| 			frappe._dict( | ||||
| 				name="Flask Item", | ||||
| 				actual_qty=10, qty_after_transaction=20, | ||||
| 				posting_date="2021-12-03", voucher_type="Stock Entry", | ||||
| 				voucher_no="004", | ||||
| 				has_serial_no=False, serial_no=None | ||||
| 			) | ||||
| 		] | ||||
| 
 | ||||
| 		slots = FIFOSlots(self.filters, sle).generate() | ||||
| 
 | ||||
| 		result = slots["Flask Item"] | ||||
| 		queue = result["fifo_queue"] | ||||
| 
 | ||||
| 		self.assertEqual(result["qty_after_transaction"], result["total_qty"]) | ||||
| 		self.assertEqual(queue[0][0], 10.0) | ||||
| 		self.assertEqual(queue[1][0], 10.0) | ||||
| 
 | ||||
| 	def test_stock_reconciliation(self): | ||||
| 		sle = [ | ||||
| 			frappe._dict( | ||||
| 				name="Flask Item", | ||||
| 				actual_qty=30, qty_after_transaction=30, | ||||
| 				posting_date="2021-12-01", voucher_type="Stock Entry", | ||||
| 				voucher_no="001", | ||||
| 				has_serial_no=False, serial_no=None | ||||
| 			), | ||||
| 			frappe._dict( | ||||
| 				name="Flask Item", | ||||
| 				actual_qty=0, qty_after_transaction=50, | ||||
| 				posting_date="2021-12-02", voucher_type="Stock Reconciliation", | ||||
| 				voucher_no="002", | ||||
| 				has_serial_no=False, serial_no=None | ||||
| 			), | ||||
| 			frappe._dict( | ||||
| 				name="Flask Item", | ||||
| 				actual_qty=(-10), qty_after_transaction=40, | ||||
| 				posting_date="2021-12-03", voucher_type="Stock Entry", | ||||
| 				voucher_no="003", | ||||
| 				has_serial_no=False, serial_no=None | ||||
| 			) | ||||
| 		] | ||||
| 
 | ||||
| 		slots = FIFOSlots(self.filters, sle).generate() | ||||
| 
 | ||||
| 		result = slots["Flask Item"] | ||||
| 		queue = result["fifo_queue"] | ||||
| 
 | ||||
| 		self.assertEqual(result["qty_after_transaction"], result["total_qty"]) | ||||
| 		self.assertEqual(queue[0][0], 20.0) | ||||
| 		self.assertEqual(queue[1][0], 20.0) | ||||
| @ -9,7 +9,7 @@ from frappe import _ | ||||
| from frappe.utils import cint, date_diff, flt, getdate | ||||
| 
 | ||||
| import erpnext | ||||
| from erpnext.stock.report.stock_ageing.stock_ageing import get_average_age, get_fifo_queue | ||||
| from erpnext.stock.report.stock_ageing.stock_ageing import FIFOSlots, get_average_age | ||||
| from erpnext.stock.report.stock_ledger.stock_ledger import get_item_group_condition | ||||
| from erpnext.stock.utils import add_additional_uom_columns, is_reposting_item_valuation_in_progress | ||||
| 
 | ||||
| @ -33,7 +33,7 @@ def execute(filters=None): | ||||
| 
 | ||||
| 	if filters.get('show_stock_ageing_data'): | ||||
| 		filters['show_warehouse_wise_stock'] = True | ||||
| 		item_wise_fifo_queue = get_fifo_queue(filters, sle) | ||||
| 		item_wise_fifo_queue = FIFOSlots(filters, sle).generate() | ||||
| 
 | ||||
| 	# if no stock ledger entry found return | ||||
| 	if not sle: | ||||
|  | ||||
| @ -9,7 +9,7 @@ import frappe | ||||
| from frappe import _ | ||||
| from frappe.utils import flt | ||||
| 
 | ||||
| from erpnext.stock.report.stock_ageing.stock_ageing import get_average_age, get_fifo_queue | ||||
| from erpnext.stock.report.stock_ageing.stock_ageing import FIFOSlots, get_average_age | ||||
| from erpnext.stock.report.stock_balance.stock_balance import ( | ||||
| 	get_item_details, | ||||
| 	get_item_warehouse_map, | ||||
| @ -33,7 +33,7 @@ def execute(filters=None): | ||||
| 	item_map = get_item_details(items, sle, filters) | ||||
| 	iwb_map = get_item_warehouse_map(filters, sle) | ||||
| 	warehouse_list = get_warehouse_list(filters) | ||||
| 	item_ageing = get_fifo_queue(filters) | ||||
| 	item_ageing = FIFOSlots(filters).generate() | ||||
| 	data = [] | ||||
| 	item_balance = {} | ||||
| 	item_value = {} | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user