Skip to content

Commit

Permalink
fix: Warranty Expiry Date not set in the serial number (#42513)
Browse files Browse the repository at this point in the history
* fix: Warranty Expiry Date not set in the serial number

* chore: fix linters issue
  • Loading branch information
rohitwaghchaure authored Jul 29, 2024
1 parent 25dac1f commit 8eff168
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 30 deletions.
36 changes: 35 additions & 1 deletion erpnext/stock/doctype/delivery_note/test_delivery_note.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import frappe
from frappe.tests.utils import FrappeTestCase
from frappe.utils import add_days, cstr, flt, nowdate, nowtime, today
from frappe.utils import add_days, cstr, flt, getdate, nowdate, nowtime, today

from erpnext.accounts.doctype.account.test_account import get_inventory_account
from erpnext.accounts.utils import get_balance_on
Expand Down Expand Up @@ -2032,6 +2032,40 @@ def test_same_posting_date_and_posting_time(self):

self.assertRaises(frappe.ValidationError, dn5.submit)

def test_warranty_expiry_date_for_serial_item(self):
item_code = make_item(
"Test Warranty Expiry Date Item",
properties={
"has_serial_no": 1,
"serial_no_series": "TWE.#####",
"is_stock_item": 1,
"warranty_period": 100,
},
).name

se = make_stock_entry(
item_code=item_code,
target="_Test Warehouse - _TC",
qty=2,
basic_rate=50,
posting_date=nowdate(),
)

serial_nos = get_serial_nos_from_bundle(se.items[0].serial_and_batch_bundle)
create_delivery_note(
item_code=item_code,
qty=2,
rate=300,
use_serial_batch_fields=0,
serial_no=serial_nos,
)

for row in serial_nos:
sn = frappe.get_doc("Serial No", row)
self.assertEqual(getdate(sn.warranty_expiry_date), getdate(add_days(nowdate(), 100)))
self.assertEqual(sn.status, "Delivered")
self.assertEqual(sn.warranty_period, 100)


def create_delivery_note(**args):
dn = frappe.new_doc("Delivery Note")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,32 +393,6 @@ def set_serial_and_batch_values(self, parent, row, qty_field=None):

self.calculate_qty_and_amount(save=True)
self.validate_quantity(row, qty_field=qty_field)
self.set_warranty_expiry_date()

def set_warranty_expiry_date(self):
if self.type_of_transaction != "Outward":
return

if not (self.docstatus == 1 and self.voucher_type == "Delivery Note" and self.has_serial_no):
return

warranty_period = frappe.get_cached_value("Item", self.item_code, "warranty_period")

if not warranty_period:
return

warranty_expiry_date = add_days(self.posting_date, cint(warranty_period))

serial_nos = self.get_serial_nos()
if not serial_nos:
return

sn_table = frappe.qb.DocType("Serial No")
(
frappe.qb.update(sn_table)
.set(sn_table.warranty_expiry_date, warranty_expiry_date)
.where(sn_table.name.isin(serial_nos))
).run()

def validate_voucher_no(self):
if not (self.voucher_type and self.voucher_no):
Expand Down
19 changes: 16 additions & 3 deletions erpnext/stock/serial_batch_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from frappe import _, bold
from frappe.model.naming import make_autoname
from frappe.query_builder.functions import CombineDatetime, Sum, Timestamp
from frappe.utils import cint, cstr, flt, get_link_to_form, now, nowtime, today
from frappe.utils import add_days, cint, cstr, flt, get_link_to_form, now, nowtime, today
from pypika import Order

from erpnext.stock.deprecated_serial_batch import (
Expand Down Expand Up @@ -338,7 +338,8 @@ def set_warehouse_and_status_in_serial_nos(self):
status = "Delivered"

sn_table = frappe.qb.DocType("Serial No")
(

query = (
frappe.qb.update(sn_table)
.set(sn_table.warehouse, warehouse)
.set(
Expand All @@ -351,7 +352,19 @@ def set_warehouse_and_status_in_serial_nos(self):
)
.set(sn_table.company, self.sle.company)
.where(sn_table.name.isin(serial_nos))
).run()
)

if status == "Delivered":
warranty_period = frappe.get_cached_value("Item", self.sle.item_code, "warranty_period")
if warranty_period:
warranty_expiry_date = add_days(self.sle.posting_date, cint(warranty_period))
query = query.set(sn_table.warranty_expiry_date, warranty_expiry_date)
query = query.set(sn_table.warranty_period, warranty_period)
else:
query = query.set(sn_table.warranty_expiry_date, None)
query = query.set(sn_table.warranty_period, 0)

query.run()

def set_batch_no_in_serial_nos(self):
entries = frappe.get_all(
Expand Down

0 comments on commit 8eff168

Please sign in to comment.