Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions medblocks/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
# override_doctype_class = {
# "ToDo": "custom_app.overrides.CustomToDo"
# }

override_doctype_class = {
"Item": "medblocks.medblocks.thirvusoft_customisations.utils.python.item.TSItem"
}
Expand All @@ -147,10 +148,11 @@

doc_events = {
"Sales Invoice": {
"validate":["medblocks.medblocks.thirvusoft_customisations.utils.python.sales_invoice.check_batch_expiry_date",
"medblocks.medblocks.thirvusoft_customisations.utils.python.sales_invoice.validate"
],
"after_insert":"medblocks.medblocks.thirvusoft_customisations.utils.python.sales_invoice.after_insert",
# "validate":[
# # "medblocks.medblocks.thirvusoft_customisations.utils.python.sales_invoice.check_batch_expiry_date",
# "medblocks.medblocks.thirvusoft_customisations.utils.python.sales_invoice.validate"
# ],
# "after_insert":"medblocks.medblocks.thirvusoft_customisations.utils.python.sales_invoice.after_insert",
},
"Sales Order":{
"on_submit":"medblocks.medblocks.thirvusoft_customisations.utils.python.sales_order.reservation_serial_no",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ frappe.ui.form.on("Sales Order Item",
label:"Serial No",
fieldname:"serial_no",
fieldtype:"Link",
options:"Serial No",
options:"Serial No",
in_list_view:1
},
{
Expand Down Expand Up @@ -189,7 +189,6 @@ frappe.ui.form.on("Sales Order Item",

})


d.show();

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,155 +1,156 @@
import frappe
from datetime import datetime
from erpnext.stock.serial_batch_bundle import SerialBatchCreation

def validate(doc, action):
if not doc.get('__islocal'):
create_serial_batch_bundle(doc,action)



def check_batch_expiry_date(doc,method):
if not doc.is_return and not doc.is_pos and doc.update_stock:
batch_list = []
expiry_month = []
expiry_within_days = []
expiry_date_difference = []

for item in doc.items:
if not frappe.get_value('Item',item.item_code,'has_serial_no') and not item.serial_and_batch_bundle:
sn_doc = SerialBatchCreation(
{
"item_code": item.item_code,
"warehouse": item.warehouse,
"voucher_type": doc.doctype,
"posting_date": doc.posting_date,
"posting_time": doc.posting_time,
"qty": item.qty,
"type_of_transaction": "Outward" if item.qty > 0 and (doc.docstatus or 0) < 2 else "Inward",
"company": doc.company,
"do_not_submit": "True",
}
)

doc = sn_doc.make_serial_and_batch_bundle()
item.serial_and_batch_bundle = doc.name



if item.serial_and_batch_bundle:
bundle_number = item.serial_and_batch_bundle
bundle_doc = frappe.get_doc('Serial and Batch Bundle',bundle_number)
for batch in bundle_doc.entries:
if batch.batch_no:
batch_number = batch.batch_no
batch_list.append({"batch_no":batch_number,"row_no":item.idx})


for check_batch in batch_list:
batch_document = frappe.get_doc("Batch", check_batch.get("batch_no"))
batch_date = (batch_document.expiry_date)
# import frappe
# from datetime import datetime
# from erpnext.stock.serial_batch_bundle import SerialBatchCreation

# def validate(doc, action):
# if not doc.get('__islocal'):
# create_serial_batch_bundle(doc,action)



# def check_batch_expiry_date(doc,method):
# if not doc.is_return and not doc.is_pos and doc.update_stock:
# batch_list = []
# expiry_month = []
# expiry_within_days = []
# expiry_date_difference = []

# for item in doc.items:
# if not frappe.get_value('Item',item.item_code,'has_serial_no') and not item.serial_and_batch_bundle:
# sn_doc = SerialBatchCreation(
# {
# "item_code": item.item_code,
# "warehouse": item.warehouse,
# "voucher_type": doc.doctype,
# "posting_date": doc.posting_date,
# "posting_time": doc.posting_time,
# "qty": item.qty,
# "type_of_transaction": "Outward" if item.qty > 0 and (doc.docstatus or 0) < 2 else "Inward",
# "company": doc.company,
# "do_not_submit": "True",
# }
# )

# doc = sn_doc.make_serial_and_batch_bundle()
# item.serial_and_batch_bundle = doc.name



# if item.serial_and_batch_bundle:
# bundle_number = item.serial_and_batch_bundle
# bundle_doc = frappe.get_doc('Serial and Batch Bundle',bundle_number)
# for batch in bundle_doc.entries:
# if batch.batch_no:
# batch_number = batch.batch_no
# batch_list.append({"batch_no":batch_number,"row_no":item.idx})


# for check_batch in batch_list:
# batch_document = frappe.get_doc("Batch", check_batch.get("batch_no"))
# batch_date = (batch_document.expiry_date)

item_name = batch_document.item_name
# item_name = batch_document.item_name

expiry_date_batch = frappe.utils.getdate(batch_date)
current_date = frappe.utils.getdate(doc.posting_date)
# expiry_date_batch = frappe.utils.getdate(batch_date)
# current_date = frappe.utils.getdate(doc.posting_date)

difference_date = frappe.utils.date_diff(expiry_date_batch,current_date)
# difference_date = frappe.utils.date_diff(expiry_date_batch,current_date)

current_date_month = current_date.month
expiry_date_batch_month = expiry_date_batch.month
# current_date_month = current_date.month
# expiry_date_batch_month = expiry_date_batch.month



target_days = frappe.db.get_single_value('Aarthy Settings',"expiry_days")
# target_days = frappe.db.get_single_value('Aarthy Settings',"expiry_days")

if current_date_month == expiry_date_batch_month and expiry_date_batch.year == current_date.year:
expiry_month.append((item_name, check_batch.get("batch_no"),check_batch.get("row_no")))

# if current_date_month == expiry_date_batch_month and expiry_date_batch.year == current_date.year:
# expiry_month.append((item_name, check_batch.get("batch_no"),check_batch.get("row_no")))



elif difference_date <= int(target_days):
formatted_date = batch_date.strftime("%d-%m-%Y")
expiry_within_days.append((item_name, check_batch.get("batch_no"),formatted_date, check_batch.get("row_no")))
# elif difference_date <= int(target_days):
# formatted_date = batch_date.strftime("%d-%m-%Y")
# expiry_within_days.append((item_name, check_batch.get("batch_no"),formatted_date, check_batch.get("row_no")))


if len(expiry_month) == 1:
mes=f"In Row {expiry_month[0][2]}: Item-<b>{expiry_month[0][0]}</b>-Batch:<b>{expiry_month[0][1]}</b> is expired and please select a valid batch.<br>"
frappe.throw(title = "Restricted Batch",msg = mes)
# if len(expiry_month) == 1:
# mes=f"In Row {expiry_month[0][2]}: Item-<b>{expiry_month[0][0]}</b>-Batch:<b>{expiry_month[0][1]}</b> is expired and please select a valid batch.<br>"
# frappe.throw(title = "Restricted Batch",msg = mes)


elif len(expiry_month) > 1:
mes = " "
for item,batch,row in expiry_month:
mes += f"In Row {row}: Item:<b>{item}</b>-Batch:<b>{batch}</b> is expired and Please select a valid batch.<br><br>"
frappe.throw(title = "Restricted Batch",msg = mes)
# elif len(expiry_month) > 1:
# mes = " "
# for item,batch,row in expiry_month:
# mes += f"In Row {row}: Item:<b>{item}</b>-Batch:<b>{batch}</b> is expired and Please select a valid batch.<br><br>"
# frappe.throw(title = "Restricted Batch",msg = mes)




if len(expiry_within_days) == 1:
mes = f"In Row {expiry_within_days[0][3]}: Item: <b>{expiry_within_days[0][0]}</b> with batch: <b>{expiry_within_days[0][1]}</b> will expire on <b>{expiry_within_days[0][2]}</b><br><br>"
frappe.msgprint(title = "Warning",msg = mes)
# if len(expiry_within_days) == 1:
# mes = f"In Row {expiry_within_days[0][3]}: Item: <b>{expiry_within_days[0][0]}</b> with batch: <b>{expiry_within_days[0][1]}</b> will expire on <b>{expiry_within_days[0][2]}</b><br><br>"
# frappe.msgprint(title = "Warning",msg = mes)


elif len(expiry_within_days) > 1:
mes = " "
for item, batch, date,row in expiry_within_days:
mes += f"In Row {row}: Item: <b>{item}</b> with batch: <b>{batch}</b> will expire on <b>{date}</b><br><br>"
# elif len(expiry_within_days) > 1:
# mes = " "
# for item, batch, date,row in expiry_within_days:
# mes += f"In Row {row}: Item: <b>{item}</b> with batch: <b>{batch}</b> will expire on <b>{date}</b><br><br>"

frappe.msgprint(title = "Warning",msg = mes)
# frappe.msgprint(title = "Warning",msg = mes)








def create_serial_batch_bundle(self,event):
# # code
# def create_serial_batch_bundle(self,event):

if not self.is_return and not self.is_pos:
# if not self.is_return and not self.is_pos:

for item in self.items:
# for item in self.items:

if frappe.get_value('Item',item.item_code,'has_serial_no') and not item.serial_and_batch_bundle:
# if frappe.get_value('Item',item.item_code,'has_serial_no') and not item.serial_and_batch_bundle:

serial_batch_doc = frappe.new_doc('Serial and Batch Bundle')
# serial_batch_doc = frappe.new_doc('Serial and Batch Bundle')

serial_batch_doc.item_code = item.item_code
serial_batch_doc.company = self.company
serial_batch_doc.warehouse = item.warehouse
serial_batch_doc.type_of_transaction = 'Outward'
serial_batch_doc.voucher_type = 'Sales Invoice'
serial_batch_doc.voucher_no = self.name
serial_batch_doc.voucher_detail_no = item.name
# serial_batch_doc.item_code = item.item_code
# serial_batch_doc.company = self.company
# serial_batch_doc.warehouse = item.warehouse
# serial_batch_doc.type_of_transaction = 'Outward'
# serial_batch_doc.voucher_type = 'Sales Invoice'
# serial_batch_doc.voucher_no = self.name
# serial_batch_doc.voucher_detail_no = item.name

serial_batch_doc.flags.ignore_permissions = True
# serial_batch_doc.flags.ignore_permissions = True

if frappe.db.exists('Stock Reservation Entry',{'voucher_detail_no':item.so_detail}):
# if frappe.db.exists('Stock Reservation Entry',{'voucher_detail_no':item.so_detail}):

reservation = frappe.get_doc('Stock Reservation Entry',{'voucher_detail_no':item.so_detail})
# reservation = frappe.get_doc('Stock Reservation Entry',{'voucher_detail_no':item.so_detail})

for it in reservation.sb_entries:
# for it in reservation.sb_entries:

serial_batch_doc.append('entries',
{
'serial_no':it.serial_no,
'qty':-(it.qty),
'warehouse':it.warehouse
}
)
# serial_batch_doc.append('entries',
# {
# 'serial_no':it.serial_no,
# 'qty':-(it.qty),
# 'warehouse':it.warehouse
# }
# )

serial_batch_doc.save()
# serial_batch_doc.save()

item.serial_and_batch_bundle = serial_batch_doc.name
# item.serial_and_batch_bundle = serial_batch_doc.name

if event == 'after_insert' and not self.is_pos:
frappe.db.set_value(item.doctype, item.name, 'serial_and_batch_bundle', serial_batch_doc.name)
# if event == 'after_insert' and not self.is_pos:
# frappe.db.set_value(item.doctype, item.name, 'serial_and_batch_bundle', serial_batch_doc.name)




def after_insert(doc, event):
# def after_insert(doc, event):

create_serial_batch_bundle(doc, event)
# create_serial_batch_bundle(doc, event)
Loading
Loading