from django.shortcuts import render from django.http import HttpResponse, StreamingHttpResponse from django.views.decorators.csrf import csrf_exempt import json from .models import FabriRoll, FabricBatch def grns(request): grns = FabricBatch.objects.all() data = [] for grn in grns: tempGRN = {} tempGRN["GRN"] = grn.GRN tempGRN["quantity"] = grn.quantity tempGRN["fabricDescription"] = grn.fabricDescription tempGRN["itemDescription"] = grn.itemDescription tempGRN["fabricForm"] = grn.fabricForm tempGRN["POWidth"] = grn.POWidth tempGRN["POUOM"] = grn.POUOM tempGRN["PointsUOM"] = grn.PointsUOM tempGRN["Tolarence"] = grn.Tolarence tempGRN["color"] = grn.color tempGRN["buyer"] = grn.buyer tempGRN["supplier"] = grn.supplier tempGRN["item"] = grn.item tempGRN["supplierName"] = grn.supplierName tempGRN["PCH"] = grn.PCH tempGRN["invoice"] = grn.invoice tempGRN["fabricOdour"] = grn.fabricOdour tempGRN["PO"] = grn.PO tempGRN["basicUOM"] = grn.basicUOM tempGRN["Warehouse"] = grn.Warehouse tempGRN["convFactor"] = grn.convFactor tempGRN["created_at"] = grn.created_at.strftime("%Y-%m-%d %H:%M:%S") tempGRN["updated_at"] = grn.updated_at.strftime("%Y-%m-%d %H:%M:%S") data.append(tempGRN) data = sorted(data, key=lambda x: int(x["GRN"])) return HttpResponse(json.dumps({"data": list(data)}), content_type="application/json") def grnRolls(request, grn_id): rolls = FabriRoll.objects.filter(GRN__GRN=grn_id) data = [] for roll in rolls: tempRoll = {} tempRoll["name"] = roll.name tempRoll["GRN"] = roll.GRN.GRN tempRoll["rLength"] = roll.rLength tempRoll["aLength"] = roll.aLength tempRoll["minWidth"] = roll.minWidth tempRoll["maxWidth"] = roll.maxWidth tempRoll["CS"] = roll.CS tempRoll["LWV"] = roll.LWV tempRoll["EPI"] = roll.EPI tempRoll["PPI"] = roll.PPI tempRoll["CutPcs"] = roll.CutPcs tempRoll["sWarp"] = roll.sWarp tempRoll["sWeft"] = roll.sWeft tempRoll["GSM"] = roll.GSM tempRoll["SL"] = roll.SL tempRoll["SG"] = roll.SG tempRoll["Bowing"] = roll.Bowing tempRoll["buyer"] = roll.GRN.buyer tempRoll["supplierName"] = roll.GRN.supplierName tempRoll["created_at"] = roll.created_at.strftime("%Y-%m-%d %H:%M:%S") tempRoll["updated_at"] = roll.updated_at.strftime("%Y-%m-%d %H:%M:%S") data.append(tempRoll) data = sorted(data, key=lambda x: int(x["name"])) return HttpResponse(json.dumps({"data": list(data)}), content_type="application/json") def rolls(request): rolls = FabriRoll.objects.all() data = [] for roll in rolls: tempRoll = {} tempRoll["name"] = roll.name tempRoll["GRN"] = roll.GRN.GRN tempRoll["rLength"] = roll.rLength tempRoll["aLength"] = roll.aLength tempRoll["minWidth"] = roll.minWidth tempRoll["maxWidth"] = roll.maxWidth tempRoll["CS"] = roll.CS tempRoll["LWV"] = roll.LWV tempRoll["EPI"] = roll.EPI tempRoll["PPI"] = roll.PPI tempRoll["CutPcs"] = roll.CutPcs tempRoll["sWarp"] = roll.sWarp tempRoll["sWeft"] = roll.sWeft tempRoll["GSM"] = roll.GSM tempRoll["SL"] = roll.SL tempRoll["SG"] = roll.SG tempRoll["Bowing"] = roll.Bowing tempRoll["buyer"] = roll.GRN.buyer tempRoll["supplierName"] = roll.GRN.supplierName tempRoll["created_at"] = roll.created_at.strftime("%Y-%m-%d %H:%M:%S") tempRoll["updated_at"] = roll.updated_at.strftime("%Y-%m-%d %H:%M:%S") data.append(tempRoll) data = sorted(data, key=lambda x: int(x["name"])) return HttpResponse(json.dumps({"data": list(data)}), content_type="application/json") def roll(request, roll_id): roll = FabriRoll.objects.get(name=roll_id) data = roll.__dict__ data.pop("_state") data["created_at"] = data["created_at"].strftime("%Y-%m-%d %H:%M:%S") data["updated_at"] = data["updated_at"].strftime("%Y-%m-%d %H:%M:%S") return HttpResponse(json.dumps({"data": data}), content_type="application/json") def rollEntry(request): if request.method == "POST": try: name = request.POST.get("name", "") supplierName = request.POST.get("supplierName", "") stock = request.POST.get("stock", "") description = request.POST.get("description", "") image = request.FILES.get("image", "") if FabriRoll.objects.filter(name=name).exists(): return HttpResponse(json.dumps({"msg": "Roll already exists", "status": "error"}), content_type="application/json") roll = FabriRoll.objects.create( name=name, supplierName=supplierName, stock=stock, description=description, image=image ) roll.save() return HttpResponse(json.dumps({"msg": "Roll created successfully", "status": "success"}), content_type="application/json") except Exception as e: return HttpResponse(json.dumps({"msg": str(e), "status": "error"}), content_type="application/json") else: return HttpResponse(json.dumps({"msg": "Method not allowed", "status": "error"}), content_type="application/json") def rollUpdate(request, roll_id): if request.method == "POST": try: roll = FabriRoll.objects.get(name=roll_id) rLength = request.POST.get("rLength", None) aLength = request.POST.get("aLength", None) minWidth = request.POST.get("minWidth", None) maxWidth = request.POST.get("maxWidth", None) CS = request.POST.get("CS", None) LWV = request.POST.get("LWV", None) EPI = request.POST.get("EPI", None) PPI = request.POST.get("PPI", None) CutPcs = request.POST.get("CutPcs", None) sWarp = request.POST.get("sWarp", None) sWeft = request.POST.get("sWeft", None) GSM = request.POST.get("GSM", None) SL = request.POST.get("SL", None) SG = request.POST.get("SG", None) Bowing = request.POST.get("Bowing", None) print(rLength, aLength, minWidth, maxWidth, CS, LWV, EPI, PPI, CutPcs, sWarp, sWeft, GSM, SL, SG, Bowing) roll.rLength = rLength roll.aLength = aLength roll.minWidth = minWidth roll.maxWidth = maxWidth roll.CS = CS roll.LWV = LWV roll.EPI = EPI roll.PPI = PPI roll.CutPcs = CutPcs roll.sWarp = sWarp roll.sWeft = sWeft roll.GSM = GSM roll.SL = SL roll.SG = SG roll.Bowing = Bowing roll.save() return HttpResponse(json.dumps({"msg": "Roll updated successfully", "status": "success"}), content_type="application/json") except Exception as e: return HttpResponse(json.dumps({"msg": str(e), "status": "error"}), content_type="application/json") else: return HttpResponse(json.dumps({"msg": "Method not allowed", "status": "error"}), content_type="application/json") def GRNUpdate(request, grn_id): if request.method == "POST": try: grn = FabricBatch.objects.get(GRN=grn_id) fabricForm = request.POST.get("fabricForm", "Roll") POWidth = request.POST.get("POWidth", "") PointsUOM = request.POST.get("PointsUOM", "YRD") Tolarence = request.POST.get("Tolarence", 20) grn.fabricForm = fabricForm grn.POWidth = POWidth grn.PointsUOM = PointsUOM grn.Tolarence = Tolarence grn.save() return HttpResponse(json.dumps({"msg": "GRN updated successfully", "status": "success"}), content_type="application/json") except Exception as e: return HttpResponse(json.dumps({"msg": str(e), "status": "error"}), content_type="application/json") else: return HttpResponse(json.dumps({"msg": "Method not allowed", "status": "error"}), content_type="application/json") # def dataAdder(request): # for i in data: # newRoll = FabriRoll() # newRoll.name=int(i['Ro.No']) # newRoll.GRN = FabricBatch.objects.filter(GRN=1524983023).first() # newRoll.rLength=i['R.Len.'] # newRoll.aLength = i['A.Len'] # newRoll.minWidth=i['Min.W'] # newRoll.maxWidth=i['Max.W.'] # newRoll.EPI=i['EPI'] # newRoll.PPI=i['PPI'] # newRoll.CutPcs=i['Cut Pcs'] # newRoll.sWarp=i['Warp'] # newRoll.sWeft=i['Weft'] # newRoll.GSM=i['Gsm'] # newRoll.SL=i['S.L'] # newRoll.Bowing = i['Bowing'] # if i["CS"] != "-": # newRoll.CS = i["CS"] # if i["LWV"] != "-": # newRoll.LWV = i["LWV"] # if i["S.G."] != "-": # newRoll.SG = i["S.G."] # newRoll.save() # print(int(i['Ro.No'])) # return "Done"