I made a child_table and then linked it to the parent doctype set values from the backend (python). After clicking the save button, the child_table data is automatically deleted.
def yarnTableCount(self, table, count, repeat):
tableValue = []
colorlist = []
totalVal = 0
extraVal = 0
for i in table:
if i.get("count") == count:
color = i.get("color")
if color not in colorlist:
colorlist.append(color)
repeat = int(repeat)
for color in colorlist:
totalVal = 0 # Redb_set totalVal for each color
extraVal = 0 # Redb_set extraVal for each color
for i in table:
if i.get("count") == count and i.get("color") == color:
total = int(i.get("total"))
totalVal += total
extra = i.get("extra")
if extra is None:
extra = 0
extra = int(extra)
extraVal += extra
subTotal = totalVal * repeat
nettTotal = (totalVal * repeat) + extraVal
gpm = "{:.2f}".format((nettTotal * 453.59) / (float(self.GetCount(count)) * 768))
gpm = float(gpm)
kgm = "{:.4f}".format(gpm/1000)
kgm = float(kgm)
total_warp_meter = float(self.total_weft_meters)
kg_warp_m = "{:.4f}".format((total_warp_meter * kgm))
tableValue.append({
"color": color,
"total": totalVal, # Use totalVal here, not total
"repeat": repeat,
"subtotal": subTotal,
"extra": extraVal, # Use extraVal here, not extra
"netttotal": nettTotal,
"gpm": gpm,
"kgm": kgm,
"kgwarp_m": kg_warp_m
})
return tableValue
def getColorList(self, table):
colorList = []
for x in table:
color = x.get("color")
if color not in colorList:
colorList.append(color)
return colorList
def dyeTableColor(self, table, color, repeat):
tableValue = []
countlist = []
totalVal = 0
extraVal = 0
for i in table:
if i.get("color") == color:
count = i.get("count")
if count not in countlist:
countlist.append(count)
repeat = int(repeat)
for count in countlist:
totalVal = 0
extraVal = 0
for i in table:
if i.get("color") == color and i.get("count") == count:
total = int(i.get("total"))
totalVal += total
extra = i.get("extra")
if extra is None:
extra = 0
extra = int(extra)
extraVal += extra
subTotal = totalVal * repeat
nettTotal = (totalVal * repeat) + extraVal
tableValue.append({
"count": count,
"total": totalVal,
"repeat": repeat,
"subtotal": subTotal,
"extra": extraVal,
"netttotal": nettTotal,
"gpm": "{:.2f}".format((nettTotal * 453.59) / (float(self.GetCount(count)) * 768))
})
return tableValue
def getWeftTable(self, design):
doc = frappe.get_doc("Design Master", design)
return doc.get("weft_pattern_table")
def weftYarnTable(self, table, count, pick, wefttotal):
# total_gpm = self.findWeftGPM(count)
tableValue = []
colorlist = []
totalVal = 0
for i in table:
if i.get("count") == count:
color = i.get("color")
if color not in colorlist:
colorlist.append(color)
total_of_total = 0
for color in colorlist:
totalVal = 0 # Redb_set totalVal for each color
for i in table:
if i.get("count") == count and i.get("color") == color:
total = int(i.get("total"))
totalVal += total
total_of_total += total
no_of_pick = (totalVal/wefttotal) * pick
gpm = (no_of_pick * 453.59) / (float(self.GetCount(count)) * 768)
tableValue.append({
"color": color,
"total": totalVal, # Use totalVal here, not total
"no_of_pick": "{:.2f}".format(no_of_pick),
"gpm": "{:.2f}".format(gpm),
})
return tableValue,total_of_total
def weftDyeTable(self,color):
tableValue = []
totaloftotal = 0
yarnweft = self.getYarnWeftValue()
for key,value in yarnweft.items():
count = key
for i in value:
if i.get("color") == color:
total = int(i.get("total"))
tableValue.append({
"count": count,
"total": total,
"gpm": i.get("gpm")
})
totaloftotal+=total
return tableValue,totaloftotal
def findWeftGPM(self,count):
#(((no of ends / onloom reed) + selvedge ends) * (onloom pick * 453.59)) / (count * 768))
total_no_ends = self.total_no_of_ends
onloom_reed = self.getOnloomreed(self.design)
selvedge_ends = self.selvedge_ends
onloom_pick = self.getOnloompick(self.design)
count = self.GetCount(count)
total_gpm = (((int(total_no_ends) / int(onloom_reed)) + selvedge_ends) * (int(onloom_pick) * 453.59)) / (float(count) * 768)
return total_gpm
def findNoofPick(self):
total_no_ends = self.total_no_of_ends
onloom_reed = self.getOnloomreed(self.design)
selvedge_ends = 3
onloom_pick = self.getOnloompick(self.design)
total_on_of_pick = ((int(total_no_ends) / int(onloom_reed)) + selvedge_ends) * (int(onloom_pick) )
total_on_of_pick = round(total_on_of_pick)
return total_on_of_pick
@frappe.whitelist()
def getYarnWeftValue(self):
result_dict = {}
for i in range(1,4):
tablename = f"yarnwefttable{i}"
titlename = f"weftyarntitle{i}"
table = getattr(self, tablename, None)
title = getattr(self, titlename, None)
if table is not None and title is not None:
result_dict[title] = table
return result_dict
@frappe.whitelist()
def createDesign(self, doctype, docname):
doc = frappe.get_doc(doctype, docname)
warpExcess = self.warp_excess_meters
totalWarp = self.total_warp_meters
weftMeter = self.weft_meter
weftExcess = self.weft_excess_meter
totalWeft = self.total_weft_meters
# doc = frappe.get_doc(self.doctype, self.name)
doc.db_set("warp_excess_meters", warpExcess)
doc.db_set("total_warp_meters", totalWarp)
doc.db_set("weft_meter", weftMeter)
doc.db_set("weft_excess_meter", weftExcess)
doc.db_set("total_weft_meters", totalWeft)
repeat = self.no_of_repeat
# extra = self.extra_ends_to_be_filled
#this is for warp only
table = self.designpattern
counts = self.getCountList(table)
colors = self.getColorList(table)
#this is for weft only
wefttable = self.getWeftTable(self.design)
weftcounts = self.getCountList(wefttable)
weftcolors = self.getColorList(wefttable)
pick = self.findNoofPick()
weft_total = self.getWeftTotal(self.design)
try:
#clear the table
# doc.db_db_set("designpattern", [])
doc.db_db_set("designpattern",table)
for i, count in enumerate(counts, 1): # Start from 1, not 0
title_name = f"title{i}"
table_name = f"yarntable{i}"
doc.db_set(title_name, count)
doc.db_set(table_name, self.yarnTableCount(table, count, repeat))
for i, count in enumerate(weftcounts, 1):
title_name = f"weftyarntitle{i}"
table_name = f"yarnwefttable{i}"
total_name = f"yarnwefttotal{i}"
doc.db_set("no_of_picks", self.findNoofPick())
doc.db_set("weft_total", weft_total)
doc.db_set(title_name, count)
table_of_counts,total_of_total = self.weftYarnTable(wefttable, count,pick, weft_total)
doc.db_set(table_name, table_of_counts)
doc.db_set(total_name,total_of_total)
for i, color in enumerate(colors, 1):
color_title = f"color{i}"
table_name = f"dyetable{i}"
doc.db_set(color_title, color)
doc.db_set(table_name, self.dyeTableColor(table, color, repeat))
for i, color in enumerate(weftcolors, 1):
title_name = f"dyewefttitle{i}"
table_name = f"dyewefttable{i}"
total_name = f"dyewefttotal{i}"
doc.db_set(title_name, color)
table_of_counts,total_of_total = self.weftDyeTable(color)
doc.db_set(table_name, table_of_counts)
doc.db_set(total_name,total_of_total)
doc.save()
frappe.db.commit()
return True
except Exception as e:
return str(e)
@frappe.whitelist()
def beforeSaveaction(self):
doc = frappe.get_doc(self.doctype, self.name)
doc.save()
frappe.db.commit()
this is my backend code, i trigger the createDesign function from frontend