def processBangCongDon(render, tenbang, congdon, baocao):
    bangdata = getattr(baocao, "bang" + tenbang)
    tongso = {}
    render.put_area(tenbang + "header")
    #render.put_area(range)

    for chitiet in bangdata:
        chitietobj = to_dict(chitiet)
        #tinhtoan cong don
        #for col in obj.__table__.c:
        if chitiet.tencuakhau in congdon:
            for key, value in (congdon[chitiet.tencuakhau]).items():
                chitietobj[key] = chitietobj[key] if chitietobj[
                    key] is not None else 0
                chitietobj['cd_' +
                           key] = chitietobj[key] + (value if value is not None
                                                     else 0)

                tongso[key] = tongso[key] + chitietobj[
                    key] if key in tongso else chitietobj[key]
                tongso['cd_' + key] = tongso['cd_' + key] + chitietobj[
                    'cd_' +
                    key] if 'cd_' + key in tongso else chitietobj['cd_' + key]
        else:
            for col in chitiet.__table__.c:
                key = col.name
                if (key not in notdict) and (str(
                        col.type) in ['INTEGER', 'SMALLINT', 'FLOAT']):
                    chitietobj[key] = chitietobj[key] if chitietobj[
                        key] is not None else 0
                    chitietobj['cd_' + key] = chitietobj[key]
                    tongso[key] = tongso[key] + chitietobj[
                        key] if key in tongso else chitietobj[key]
                    tongso['cd_' + key] = tongso['cd_' + key] + chitietobj[
                        'cd_' +
                        key] if 'cd_' + key in tongso else chitietobj['cd_' +
                                                                      key]

        #end congdong
        for key, value in chitietobj.items():
            if chitietobj[key] == 0:
                chitietobj[key] = None

        render.put_area(tenbang + "chitiet", ujson.dumps(chitietobj))
        #render.put_area(range)

    for key, value in tongso.items():
        if tongso[key] == 0:
            tongso[key] = None

    render.put_area(tenbang + "tongso", ujson.dumps(tongso))
    #render.put_area(range)

    render.put_area(tenbang + "footer")

    if baocao.loaikybaocao > 2:
        render.put_area(
            "nhanxetkiennghi",
            ujson.dumps({"nhanxet": getattr(baocao, "nhanxet" + tenbang)}))
async def timkiem(request):
    data = request.json
    tokhaiyte = db.session.query(ToKhaiYTe).filter(ToKhaiYTe.cmtnd.ilike('%'+data['hochieu']+'%')).\
        filter(ToKhaiYTe.cuakhau_id == data['cuakhau_id']).\
        order_by(ToKhaiYTe._created_at).first()

    if tokhaiyte is not None:
        return json(to_dict(tokhaiyte))

    return json({"error_code": "NOT_FOUND"}, status=404)
def processCuaKhau(render, cuakhaulist):
    render.put_area("cuakhauheader")
    #render.put_area(range)
    #chitiet
    for chitiet in cuakhaulist:
        chitietobj = to_dict(chitiet)
        render.put_area("cuakhauchitiet", ujson.dumps(chitietobj))
        #render.put_area(range)

    render.put_area("cuakhaufooter")
def processBang(render, tenbang, baocao):
    bangdata = getattr(baocao, "bang" + tenbang)

    render.put_area(tenbang + "header")
    #render.put_area(range)

    for chitiet in bangdata:
        chitietobj = to_dict(chitiet)
        render.put_area(tenbang + "chitiet", ujson.dumps(chitietobj))
        #render.put_area(range)
    render.put_area(tenbang + "footer")
def user_to_dict(user):
    roles = [{"id":role.id,"name":role.name} for role in user.roles]
    donviobj = to_dict(user.donvi)
    cuakhauobj = to_dict(user.cuakhau)
    
    user_info = {
        "id": user.id,
        "email": user.email,
        "name": user.name,
        "roles": roles,
        "info": {
            "donvi":donviobj,
            "cuakhau": cuakhauobj
        }
    }
    # obj = to_dict(user)
    # if "password" in obj:
    #     del(obj["password"])
    # if "salt" in obj:
    #     del(obj["salt"])
    return user_info
async def get_baocaotonghopnghingonhiembenhnhoma_cuakhau(request):
    donvi_id = request.args.get("donvi_id", None)
    cuakhau_id = request.args.get("cuakhau_id", None)
    ngaybaocao = request.args.get("ngaybaocao", None)

    if (ngaybaocao is None) or (donvi_id is None) or (cuakhau_id is None):
        return text("Tham số không hợp lệ", status=520)

    donvi_id = int(donvi_id)
    cuakhau_id = int(cuakhau_id)
    loaibaocao = 2


    item = BaoCaoTongHopNghiNgoNhiemBenhNhomA.query.filter(BaoCaoTongHopNghiNgoNhiemBenhNhomA.donvi_id == donvi_id).\
                filter(BaoCaoTongHopNghiNgoNhiemBenhNhomA.cuakhau_id == cuakhau_id).\
                filter(BaoCaoTongHopNghiNgoNhiemBenhNhomA.loaibaocao == loaibaocao).\
                filter(BaoCaoTongHopNghiNgoNhiemBenhNhomA.ngaybaocao == ngaybaocao).first()

    if item is not None:
        return json(to_dict(item))

    cuakhau = db.session.query(CuaKhau).filter(
        CuaKhau.id == cuakhau_id).first()
    if (cuakhau is None):
        return text("Tham số không hợp lệ", status=520)

    donvi = db.session.query(DonVi).filter(DonVi.id == donvi_id).first()
    if (donvi is None):
        return text("Tham số không hợp lệ", status=520)

    ckobj = {
        "id": cuakhau.id,
        "ngaybaocao": ngaybaocao,
        "loaibaocao": 2,
        "donvi_id": donvi_id,
        "madonvi": donvi.ma,
        "tendonvi": donvi.ten,
        "cuakhau_id": cuakhau.id,
        "tencuakhau": cuakhau.ten,
        "macuakhau": cuakhau.ma,
        "songuoinhapcanh": None,
        "sohanhkhachkhaibao": None,
        "sochuyenbay": None,
        "songuoinghingo": None,
        "songuoidangcachlytaptrung": None,
        "songuoidangcachlytaptrung_cotrieuchung": None,
        "diadiemcachlytaptrung": None,
        "songuoihetcachly": None,
        "songuoicachlytainha": None
    }

    return json(ckobj)
def processBangKinhPhi(render, baocao):
    bangdata = getattr(baocao, "bangkinhphi")

    render.put_area("kinhphiheader")
    #render.put_area(range)

    tongso = 0
    for chitiet in bangdata:
        chitietobj = to_dict(chitiet)
        tongso = tongso + (chitiet.kinhphi
                           if chitiet.kinhphi is not None else 0)
        render.put_area("kinhphichitiet", ujson.dumps(chitietobj))
        #render.put_area(range)

    render.put_area("kinhphitongso", ujson.dumps({"tongso": tongso}))
    #render.put_area(range)
    render.put_area("kinhphifooter")
def create_tokhaiyte(request):
    data = request.json
    id = generation_id()

    cuakhau_id = data.get("cuakhau_id")
    if cuakhau_id is None:
        return text("Not found", status=404)

    cuakhau = get_cuakhau_info(cuakhau_id)
    data["donvi_id"] = cuakhau.get("donvi_id")
    data["tendonvi"] = cuakhau.get("tendonvi")
    data["tencuakhau"] = cuakhau.get("tencuakhau")

    new_tokhaiyte = ToKhaiYTe()
    new_tokhaiyte.id = id

    for key in data:
        if (key != "id") and hasattr(new_tokhaiyte, key):
            setattr(new_tokhaiyte, key, data.get(key))

    db.session.add(new_tokhaiyte)
    db.session.commit()
    result = to_dict(new_tokhaiyte)
    return json(result)
async def exporttruonghopcachlytaptrung(request):
    donvi_id = request.args.get("donvi_id")

    if donvi_id is None:
        return text("Không tìm thấy báo cáo...", status=404)
    filename = "truonghopcachlytaptrung-" + donvi_id + "-" + str(
        time.time()) + '.xlsx'
    donvi = db.session.query(DonVi).filter(DonVi.id == int(donvi_id)).first()

    cuakhau_id = request.args.get("cuakhau_id")
    ngaybaocao = request.args.get("ngaybaocao", "")

    items = TruongHopCachLyTapTrung.query.filter(
        TruongHopCachLyTapTrung.donvi_id == int(donvi_id))
    if cuakhau_id is not None:
        items = items.filter(
            TruongHopCachLyTapTrung.cuakhau_id == int(cuakhau_id))
    if ngaybaocao is not None:
        items = items.filter(TruongHopCachLyTapTrung.ngaybaocao == ngaybaocao)
    items = items.all()

    wb = Workbook()
    ws = wb.active

    ws.merge_cells('A2:D2')
    ws.merge_cells('A3:D3')
    ws.merge_cells('D4:K4')

    ws['A2'] = "Cục Y tế Dự phòng"
    ws['A3'] = donvi.ten
    ws['A3'].font = bold_font
    ws['D4'] = "DANH SÁCH THEO DÕI KHÁCH NHẬP CẢNH ĐƯỢC CÁCH LY"
    ws['D4'].font = bold_font
    ws['B5'] = "Ngày: "

    if ngaybaocao is not None:
        ngaybaocao_date = datetime.strptime(ngaybaocao, "%Y-%m-%dT%H:%M:%S")
        print(ngaybaocao_date)
        ws['C5'] = ngaybaocao_date.strftime("%d/%m/%Y")

    ws['A7'] = "STT"
    ws['B7'] = "Họ và tên"
    ws['C7'] = "CMTND/Hộ chiếu"
    ws['D7'] = "Tuổi"
    ws['E7'] = "Giới tính"
    ws['F7'] = "Nghề nghiệp"
    ws['G7'] = "Xã"
    ws['H7'] = "Huyện"
    ws['I7'] = "Tỉnh"
    ws['J7'] = "Quốc tịch"
    ws['K7'] = "Số điện thoại"
    ws['L7'] = "Tiền sử dịch tễ"
    ws['M7'] = "Triệu chứng"
    ws['N7'] = "Địa điểm cách ly"
    ws['O7'] = "Bệnh lý nền kèm theo"
    ws['P7'] = "Ghi chú"
    ws['Q7'] = "Nhập cảnh tại của khẩu"

    for j in range(17):
        ws[list_char[j] + "7"].alignment = center_alignment
        ws[list_char[j] + "7"].border = black_border
        ws[list_char[j] + "7"].font = bold_font

    start_row = 8

    for item in items:
        obj = to_dict(item)
        ws['A' + str(start_row)] = str(start_row - 7)
        ws['B' + str(start_row)] = obj["hoten"]
        ws['C' + str(start_row)] = obj["cmtnd"]
        ws['D' + str(start_row)] = obj["namsinh"]
        ws['E' + str(start_row)] = obj["gioitinh"]
        ws['F' + str(start_row)] = obj["nghenghiep"]
        ws['G' + str(start_row)] = obj["noio_xaphuong"]
        ws['H' + str(start_row)] = obj["noio_quanhuyen"]
        ws['I' + str(start_row)] = obj["noio_tinhthanh"]
        ws['J' + str(start_row)] = obj["quoctich"]
        ws['K' + str(start_row)] = obj["sodienthoai"]
        ws['L' + str(start_row)] = obj["tiensu_dichte"]
        ws['M' + str(start_row)] = obj["tiensu_trieuchunglamsang"]
        ws['N' + str(start_row)] = obj["noitiepnhan_xutri"]
        ws['O' + str(start_row)] = obj["benhly_kemtheo"]
        ws['P' + str(start_row)] = obj["ghichu"]
        ws['Q' + str(start_row)] = obj["tencuakhau"]
        start_row = start_row + 1

    for i in range(8, start_row + 1):
        for j in range(17):
            ws[list_char[j] + str(i)].border = black_border
            ws[list_char[j] + str(i)].alignment = center_alignment

    wb.save(filename=filename)
    return await file(
        filename,
        mime_type=
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        filename=filename)

    return json({})