Skip to content

Commit

Permalink
feat: stock controller
Browse files Browse the repository at this point in the history
  • Loading branch information
sibikumarkuppusamy committed Jul 25, 2024
1 parent c6fe47f commit 3a4d4ad
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 7 deletions.
5 changes: 5 additions & 0 deletions asset/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
__version__ = "0.0.1"

from erpnext.controllers.buying_controller import BuyingController
from erpnext.controllers.stock_controller import StockController

from asset.asset.controllers.buying_controller.buying_controller import AssetBuyingController
from asset.asset.controllers.stock_controller.stock_controller import AssetStockController


BuyingController.validate = AssetBuyingController.validate
BuyingController.on_submit = AssetBuyingController.on_submit
BuyingController.on_cancel = AssetBuyingController.on_cancel
StockController.make_gl_entries = AssetStockController.make_gl_entries
StockController.make_bundle_using_old_serial_batch_fields = AssetStockController.make_bundle_using_old_serial_batch_fields
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def validate(self):


def on_submit(self):
super(BuyingController, self).on_submit()
if self.get("is_return"):
return

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import frappe

def asset_make_bundle_using_old_serial_batch_fields(self, table_name=None, via_landed_cost_voucher=False):
if self.get("_action") == "update_after_submit":
return

# To handle test cases
if frappe.flags.in_test and frappe.flags.use_serial_and_batch_fields:
return

if not table_name:
table_name = "items"

if self.doctype == "Asset Capitalization":
table_name = "stock_items"

for row in self.get(table_name):
if row.serial_and_batch_bundle and (row.serial_no or row.batch_no):
self.validate_serial_nos_and_batches_with_bundle(row)

if not row.serial_no and not row.batch_no and not row.get("rejected_serial_no"):
continue

if not row.use_serial_batch_fields and (
row.serial_no or row.batch_no or row.get("rejected_serial_no")
):
row.use_serial_batch_fields = 1

if row.use_serial_batch_fields and (
not row.serial_and_batch_bundle and not row.get("rejected_serial_and_batch_bundle")
):
bundle_details = {
"item_code": row.get("rm_item_code") or row.item_code,
"posting_date": self.posting_date,
"posting_time": self.posting_time,
"voucher_type": self.doctype,
"voucher_no": self.name,
"voucher_detail_no": row.name,
"company": self.company,
"is_rejected": 1 if row.get("rejected_warehouse") else 0,
"use_serial_batch_fields": row.use_serial_batch_fields,
"via_landed_cost_voucher": via_landed_cost_voucher,
"do_not_submit": True if not via_landed_cost_voucher else False,
}

if row.get("qty") or row.get("consumed_qty") or row.get("stock_qty"):
self.update_bundle_details(bundle_details, table_name, row)
self.create_serial_batch_bundle(bundle_details, row)

if row.get("rejected_qty"):
self.update_bundle_details(bundle_details, table_name, row, is_rejected=True)
self.create_serial_batch_bundle(bundle_details, row)
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import frappe
from frappe.utils import cint

import erpnext
from erpnext.accounts.general_ledger import (
make_gl_entries,
make_reverse_gl_entries,
)
from erpnext.stock import get_warehouse_account_map


def asset_make_gl_entries(self, gl_entries=None, from_repost=False, via_landed_cost_voucher=False):
if self.docstatus == 2:
make_reverse_gl_entries(voucher_type=self.doctype, voucher_no=self.name)

provisional_accounting_for_non_stock_items = cint(
frappe.get_cached_value(
"Company", self.company, "enable_provisional_accounting_for_non_stock_items"
)
)

is_asset_pr = any(d.get("is_fixed_asset") for d in self.get("items"))

if (
cint(erpnext.is_perpetual_inventory_enabled(self.company))
or provisional_accounting_for_non_stock_items
or is_asset_pr
):
warehouse_account = get_warehouse_account_map(self.company)

if self.docstatus == 1:
if not gl_entries:
gl_entries = (
self.get_gl_entries(warehouse_account, via_landed_cost_voucher)
if self.doctype == "Purchase Receipt"
else self.get_gl_entries(warehouse_account)
)
make_gl_entries(gl_entries, from_repost=from_repost)
13 changes: 13 additions & 0 deletions asset/asset/controllers/stock_controller/stock_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from erpnext.controllers.stock_controller import StockController

from asset.asset.controllers.stock_controller.override.make_gl_entries import asset_make_gl_entries
from asset.asset.controllers.stock_controller.override.bundle_using_old_serial_batch import asset_make_bundle_using_old_serial_batch_fields

class AssetStockController(StockController):
def make_gl_entries(self, gl_entries=None, from_repost=False, via_landed_cost_voucher=False):
asset_make_gl_entries(self, gl_entries=None, from_repost=False, via_landed_cost_voucher=False)


def make_bundle_using_old_serial_batch_fields(self, table_name=None, via_landed_cost_voucher=False):
asset_make_bundle_using_old_serial_batch_fields(self, table_name=None, via_landed_cost_voucher=False)

22 changes: 16 additions & 6 deletions asset/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
"Purchase Invoice": "asset/customizations/purchase_invoice/purchase_invoice.js",
"Serial and Batch Bundle": "asset/customizations/serial_and_batch_bundle/serial_and_batch_bundle.js",
"Sales Invoice": "asset/customizations/sales_invoice/sales_invoice.js",
"Journal Entry": "asset/customizations/journal_entry/journal_entry.js",
"Product Bundle": "asset/customizations/product_bundle/product_bundle.js"
}
# doctype_list_js = {"doctype" : "public/js/doctype_list.js"}
# doctype_tree_js = {"doctype" : "public/js/doctype_tree.js"}
Expand Down Expand Up @@ -132,8 +134,16 @@
# Document Events
# ---------------
# Hook on document methods and events
period_closing_doctypes = [
"Asset",
"Asset Capitalization",
"Asset Repair"
]

doc_events = {
tuple(period_closing_doctypes): {
"validate": "erpnext.accounts.doctype.accounting_period.accounting_period.validate_accounting_period_on_doc_save",
},
"Company": {
"on_update": "asset.asset.customizations.company.company.on_update",
},
Expand All @@ -149,6 +159,10 @@
"on_cancel": "asset.asset.customizations.journal_entry.journal_entry.on_cancel",
},
"Sales Invoice": {"validate": "asset.asset.customizations.sales_invoice.sales_invoice.validate"},
"Product Bundle": {
"validate": "asset.asset.customizations.product_bundle.product_bundle.validate"
},
"GL Entry": {"validate":"asset.asset.customizations.gl_entry.gl_entry.validate"}
}

# Scheduled Tasks
Expand Down Expand Up @@ -190,6 +204,8 @@
"erpnext.buying.doctype.purchase_order.purchase_order.make_purchase_invoice": "asset.asset.customizations.purchase_order.purchase_order.make_purchase_invoice",
"erpnext.buying.doctype.purchase_order.purchase_order.make_purchase_invoice_from_portal": "asset.asset.customizations.purchase_order.purchase_order.make_purchase_invoice_from_portal",
"erpnext.accounts.doctype.purchase_invoice.purchase_invoice.make_purchase_receipt": "asset.asset.customizations.purchase_invoice.purchase_invoice.make_purchase_receipt",
"erpnext.selling.page.point_of_sale.point_of_sale.get_items": "asset.asset.customizations.point_of_sale.point_of_sale.get_items",
"erpnext.stock.doctype.material_request.material_request.make_purchase_order": "asset.asset.customizations.material_request.material_request.make_purchase_order",
}
#
# each overriding function accepts a `data` argument;
Expand Down Expand Up @@ -257,12 +273,6 @@
# "Logging DocType Name": 30 # days to retain logs
# }

period_closing_doctypes = [
"Asset",
"Asset Capitalization",
"Asset Repair",
]

accounting_dimension_doctypes = [
"Asset",
"Asset Value Adjustment",
Expand Down

0 comments on commit 3a4d4ad

Please sign in to comment.