Exemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("ID,Code,Name,Status,CompanyId")] Department department)
        {
            if (ModelState.IsValid)
            {
                _context.Add(department);
                await _context.SaveChangesAsync();


                Log log = new Log
                {
                    Descriptions = "Create Department - " + department.ID,
                    Action       = "Create",
                    Status       = "success",
                    UserId       = User.Identity.GetUserName()
                };
                _context.Add(log);

                _context.SaveChanges();



                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CompanyId"] = new SelectList(_context.Companies, "ID", "Name", department.CompanyId);
            return(View(department));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create(Company company)
        {
            if (ModelState.IsValid)
            {
                _context.Add(company);
                await _context.SaveChangesAsync();

                Log log = new Log
                {
                    Descriptions = "Create Company - " + company.ID,
                    Action       = "Create",
                    Status       = "success",
                    UserId       = User.Identity.GetUserName()
                };
                _context.Add(log);

                _context.SaveChanges();



                return(RedirectToAction(nameof(Index)));
            }
            return(View(company));
        }
Exemplo n.º 3
0
        public string GetNoSeries(string Code)
        {
            string str    = "";
            int    number = 0;

            try
            {
                //new setup for num series
                string lno = _context.NoSeries.Where(i => i.Code == Code).Select(n => n.LastNoUsed).FirstOrDefault();
                if (lno == null)
                {
                    var ns = new NoSeries
                    {
                        Code        = Code,
                        LastNoUsed  = "00000",
                        DateCreated = DateTime.Now.Date,
                        DateUpdated = DateTime.Now.Date
                    };
                    _context.Add(ns);
                    _context.SaveChanges();

                    number = 0;
                }
                else
                {
                    number = Convert.ToInt32(lno);
                }


                number += 1;

                str = number.ToString("D5");
            }
            catch (Exception e)
            {
                throw;
            }


            return(str);
        }
Exemplo n.º 4
0
        public JsonResult Approve(int[] id, string docstatus, string equipmenttype)
        {
            string status  = "";
            string message = "";
            string refno   = "";


            if (docstatus == "For Review")
            {
                docstatus = "For Approval";
            }
            else
            {
                docstatus = "Approved";
            }

            switch (equipmenttype)
            {
            case "fe":

                foreach (var item in id)
                {
                    var wdif = _context.FireExtinguisherHeaders.Find(item);
                    wdif.DocumentStatus = docstatus;

                    if (docstatus == "For Approval")
                    {
                        wdif.ReviewedDate = DateTime.Now.Date;
                        _context.FireExtinguisherDetails.Where(a => a.FireExtinguisherHeaderId == item).ToList()
                        .ForEach(a =>
                                 a.ReviewedBy = User.Identity.GetFullName()
                                 );
                    }
                    else
                    {
                        wdif.ApprovedDate = DateTime.Now.Date;
                        _context.FireExtinguisherDetails.Where(a => a.FireExtinguisherHeaderId == item).ToList()
                        .ForEach(a =>
                                 a.NotedBy = User.Identity.GetFullName()
                                 );
                    }

                    _context.Entry(wdif).State = EntityState.Modified;



                    Log log = new Log
                    {
                        Descriptions = "Modified FEHeader, Id = " + item,
                        Action       = "Modify",
                        Status       = "Success",
                        UserId       = User.Identity.GetUserName(),
                        CreatedDate  = DateTime.Now
                    };
                    _context.Add(log);

                    _context.SaveChanges();

                    string stat = new NotifyController(_context).SendNotification(docstatus, equipmenttype, item);    // send email
                }


                status = "success";

                var model = new
                {
                    status,
                    message,
                };



                return(Json(model));

            case "el":

                foreach (var item in id)
                {
                    var wdif = _context.EmergencyLightHeaders.Find(item);
                    wdif.DocumentStatus = docstatus;

                    if (docstatus == "For Approval")
                    {
                        wdif.ReviewedDate = DateTime.Now.Date;
                        _context.EmergencyLightDetails.Where(a => a.EmergencyLightHeaderId == item).ToList()
                        .ForEach(a =>
                                 a.ReviewedBy = User.Identity.GetFullName()
                                 );
                    }
                    else
                    {
                        wdif.ApprovedDate = DateTime.Now.Date;
                        _context.EmergencyLightDetails.Where(a => a.EmergencyLightHeaderId == item).ToList()
                        .ForEach(a =>
                                 a.NotedBy = User.Identity.GetFullName()
                                 );
                    }

                    _context.Entry(wdif).State = EntityState.Modified;



                    Log log = new Log
                    {
                        Descriptions = "Modified ELHeader, Id = " + item,
                        Action       = "Modify",
                        Status       = "Success",
                        UserId       = User.Identity.GetUserName(),
                        CreatedDate  = DateTime.Now
                    };
                    _context.Add(log);

                    _context.SaveChanges();

                    string stat = new NotifyController(_context).SendNotification(docstatus, equipmenttype, item);     // send email
                }


                status = "success";

                var modelel = new
                {
                    status,
                    message,
                };



                return(Json(modelel));


            case "it":

                foreach (var item in id)
                {
                    var wdif = _context.InergenTankHeaders.Find(item);
                    wdif.DocumentStatus = docstatus;

                    if (docstatus == "For Approval")
                    {
                        wdif.ReviewedDate = DateTime.Now.Date;
                        _context.InergenTankDetails.Where(a => a.InergenTankHeaderId == item).ToList()
                        .ForEach(a =>
                                 a.ReviewedBy = User.Identity.GetFullName()
                                 );
                    }
                    else
                    {
                        wdif.ApprovedDate = DateTime.Now.Date;
                        _context.InergenTankDetails.Where(a => a.InergenTankHeaderId == item).ToList()
                        .ForEach(a =>
                                 a.NotedBy = User.Identity.GetFullName()
                                 );
                    }

                    _context.Entry(wdif).State = EntityState.Modified;

                    Log log = new Log
                    {
                        Descriptions = "Modified ITHeader, Id = " + item,
                        Action       = "Modify",
                        Status       = "Success",
                        UserId       = User.Identity.GetUserName(),
                        CreatedDate  = DateTime.Now
                    };
                    _context.Add(log);

                    _context.SaveChanges();

                    string stat = new NotifyController(_context).SendNotification(docstatus, equipmenttype, item);     // send email
                }


                status = "success";

                var modelit = new
                {
                    status,
                    message,
                };



                return(Json(modelit));

            case "bc":

                foreach (var item in id)
                {
                    var wdif = _context.BicycleEntryHeaders.Find(item);
                    wdif.DocumentStatus = docstatus;


                    wdif.ApprovedDate = DateTime.Now.Date;
                    _context.BicycleEntryDetails.Where(a => a.BicycleEntryHeaderId == item).ToList()
                    .ForEach(a =>
                             a.NotedBy = User.Identity.GetFullName()
                             );

                    _context.Entry(wdif).State = EntityState.Modified;

                    Log log = new Log
                    {
                        Descriptions = "Modified BCHeader, Id = " + item,
                        Action       = "Modify",
                        Status       = "Success",
                        UserId       = User.Identity.GetUserName(),
                        CreatedDate  = DateTime.Now
                    };
                    _context.Add(log);

                    _context.SaveChanges();

                    string stat = new NotifyController(_context).SendNotification(docstatus, equipmenttype, item);     // send email
                }


                status = "success";

                var modelbc = new
                {
                    status,
                    message,
                };



                return(Json(modelbc));


            case "fh":

                foreach (var item in id)
                {
                    var wdif = _context.FireHydrantHeaders.Find(item);
                    wdif.DocumentStatus = docstatus;

                    if (docstatus == "For Approval")
                    {
                        wdif.ReviewedDate = DateTime.Now.Date;
                        _context.FireHydrantDetails.Where(a => a.FireHydrantHeaderId == item).ToList()
                        .ForEach(a =>
                                 a.ReviewedBy = User.Identity.GetFullName()
                                 );
                    }
                    else
                    {
                        wdif.ApprovedDate = DateTime.Now.Date;
                        _context.FireHydrantDetails.Where(a => a.FireHydrantHeaderId == item).ToList()
                        .ForEach(a =>
                                 a.NotedBy = User.Identity.GetFullName()
                                 );
                    }

                    _context.Entry(wdif).State = EntityState.Modified;



                    Log log = new Log
                    {
                        Descriptions = "Modified FHHeader, Id = " + item,
                        Action       = "Modify",
                        Status       = "Success",
                        UserId       = User.Identity.GetUserName(),
                        CreatedDate  = DateTime.Now
                    };
                    _context.Add(log);

                    _context.SaveChanges();

                    string stat = new NotifyController(_context).SendNotification(docstatus, equipmenttype, item);     // send email
                }


                status = "success";

                var modelfh = new
                {
                    status,
                    message,
                };



                return(Json(modelfh));



            default:
                return(null);
            }
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Create(ItemViewModel item)
        {
            if (item.DatePurchased == null)
            {
                item.Warranty = 0;
            }
            if (ModelState.IsValid)
            {
                //int companyAccess = Convert.ToInt32(User.Identity.GetCompanyAccess());//disabled 01032022
                int    companyAccess = item.CompanyId;
                string series        = "";
                string refno         = "";
                string st            = "";

                string series_code = "";

                if (companyAccess == 1)
                {
                    series_code = "ITEMSLPGC";
                    st          = "SLIT";
                }
                if (companyAccess == 2)
                {
                    series_code = "ITEMSCPC";
                    st          = "SCIT";
                }


                series = new NoSeriesController(_context).GetNoSeries(series_code);

                refno = st + series;



                item.Code = refno;
                Item _item = new Item {
                    Code = item.Code,

                    Name = item.Name,

                    SerialNo = item.SerialNo,

                    DatePurchased = item.DatePurchased,

                    Warranty = item.Warranty,

                    ItemStatus = item.ItemStatus,

                    Status = "Active",

                    EquipmentType = item.EquipmentType,

                    IsIn = 1,

                    Type = item.Type,

                    Capacity = item.Capacity
                };

                _context.Add(_item);
                await _context.SaveChangesAsync();


                string x = new NoSeriesController(_context).UpdateNoSeries(series, series_code);


                ItemLog itemLog = new ItemLog
                {
                    ItemId      = item.Id,
                    OldStatus   = "",
                    NewStatus   = "New",
                    CreatedDate = DateTime.Now,
                    CreatedBy   = User.Identity.GetFullName(),
                    Description = "Item added to item maintenance"
                };
                _context.Add(itemLog);


                Log log = new Log
                {
                    Descriptions = "Create Item - " + item.Id,
                    Action       = "Create",
                    Status       = "success",
                    UserId       = User.Identity.GetUserName()
                };
                _context.Add(log);
                _context.SaveChanges();



                return(RedirectToAction(nameof(Index)));
            }
            return(View(item));
        }
Exemplo n.º 6
0
        public IActionResult SaveData(FireExtinguisherViewModel[] item)
        {
            int    headerId = 0;
            string status   = "";
            string message  = "";

            string series = "";
            string refno  = "";

            //string series_code = "INERGENTANK";
            //series = new NoSeriesController(_context).GetNoSeries(series_code);
            //refno = "IT" + series;


            try
            {
                //var _header = _context.InergenTankHeaders.Where(a => a.Status == "Active")
                //      //.Where(a => a.AreaId == item[0].AreaId)
                //    .Where(a => a.LocationInergenTankId == item[0].LocationInergenTankId)
                //    .Where(a => a.CreatedAt == DateTime.Now.Date);
                var _header = _context.InergenTankHeaders
                              .Where(a => a.AreaId == item[0].AreaId)
                              .Where(a => a.Status == "Active")
                              .Where(a => a.DocumentStatus != "Approved");
                if (_header.Count() == 0)
                {
                    var comp = _context.LocationInergenTanks.Include(a => a.Areas.Companies).Where(a => a.Id == item[0].LocationInergenTankId)
                               .FirstOrDefault().Areas.Companies.Code;

                    if (comp == "SCPC")
                    {
                        comp = "SC";
                    }
                    else
                    {
                        comp = "SL";
                    }
                    string series_code = comp + "INERGENTANK";
                    series = new NoSeriesController(_context).GetNoSeries(series_code);
                    refno  = comp + "IT" + series;


                    InergenTankHeader header = new InergenTankHeader
                    {
                        ReferenceNo = refno,
                        AreaId      = item[0].AreaId,
                        CreatedAt   = DateTime.Now.Date,
                        CreatedBy   = User.Identity.GetFullName()
                    };
                    _context.Add(header);
                    _context.SaveChanges();
                    headerId = header.Id;

                    string x = new NoSeriesController(_context).UpdateNoSeries(series, series_code);



                    foreach (var detail in item)
                    {
                        var _detail = new InergenTankDetail
                        {
                            ItemId = detail.ItemId,

                            Cylinder = Convert.ToInt32(detail.Cylinder),
                            Gauge    = Convert.ToInt32(detail.Gauge),
                            Hose     = Convert.ToInt32(detail.Hose),
                            Pressure = Convert.ToInt32(detail.Pressure),

                            CreatedAt             = DateTime.Now.Date,
                            UpdatedAt             = DateTime.Now.Date,
                            InergenTankHeaderId   = headerId,
                            Remarks               = detail.Remarks,
                            InspectedBy           = detail.InspectedBy,
                            ReviewedBy            = detail.ReviewedBy,
                            NotedBy               = detail.NotedBy,
                            LocationInergenTankId = detail.LocationInergenTankId,
                        };

                        _context.Add(_detail);
                    }
                }
                else
                {
                    //headerId = item[0].ID;
                    headerId = _header.FirstOrDefault().Id;
                    foreach (var detail in item)
                    {
                        var d = _context.InergenTankDetails
                                .Where(a => a.InergenTankHeaderId == headerId)
                                .Where(a => a.LocationInergenTankId == detail.LocationInergenTankId)
                                .Where(a => a.ItemId == detail.ItemId)
                                .FirstOrDefault();

                        if (d == null)
                        {
                            var _detail = new InergenTankDetail
                            {
                                ItemId   = detail.ItemId,
                                Cylinder = Convert.ToInt32(detail.Cylinder),
                                Gauge    = Convert.ToInt32(detail.Gauge),
                                Hose     = Convert.ToInt32(detail.Hose),
                                Pressure = Convert.ToInt32(detail.Pressure),

                                CreatedAt             = DateTime.Now.Date,
                                UpdatedAt             = DateTime.Now,
                                InergenTankHeaderId   = headerId,
                                Remarks               = detail.Remarks,
                                InspectedBy           = detail.InspectedBy,
                                ReviewedBy            = detail.ReviewedBy,
                                NotedBy               = detail.NotedBy,
                                LocationInergenTankId = detail.LocationInergenTankId,
                            };

                            _context.Add(_detail);
                        }
                        else
                        {
                            d.Cylinder = Convert.ToInt32(detail.Cylinder);
                            d.Gauge    = Convert.ToInt32(detail.Gauge);
                            d.Hose     = Convert.ToInt32(detail.Hose);
                            d.Pressure = Convert.ToInt32(detail.Pressure);

                            d.UpdatedAt             = DateTime.Now;
                            d.UpdatedBy             = User.Identity.GetUserName();
                            d.InergenTankHeaderId   = headerId;
                            d.Remarks               = detail.Remarks;
                            d.InspectedBy           = detail.InspectedBy;
                            d.ReviewedBy            = detail.ReviewedBy;
                            d.NotedBy               = detail.NotedBy;
                            d.LocationInergenTankId = detail.LocationInergenTankId;
                            _context.Update(d);
                        }
                        _context.SaveChanges();
                    }
                }



                _context.SaveChanges();
                status = "success";
            }
            catch (Exception ex)
            {
                status  = "failed";
                message = ex.InnerException.Message;
            }



            var model = new
            {
                status
                ,
                message,
                ReferenceId = headerId
            };

            return(Json(model));
        }
Exemplo n.º 7
0
        public IActionResult CreateModifyBicycle(BicycleEntryDetail item, int BicycleId)
        {
            int    headerId = 0;
            string status   = "";
            string message  = "";



            string series = "";
            string refno  = "";


            try
            {
                var _header = _context.BicycleEntryHeaders
                              .Where(a => a.Status == "Active")
                              .Where(a => a.BicycleId == BicycleId)
                              .Where(a => a.DocumentStatus != "Approved").FirstOrDefault();

                if (_header == null)
                {
                    var comp = _context.Bicycles.Include(a => a.Departments.Companies)
                               .Where(a => a.ID == BicycleId).FirstOrDefault().Departments.Companies.Code;


                    if (comp == "SCPC")
                    {
                        comp = "SC";
                    }
                    else
                    {
                        comp = "SL";
                    }
                    string series_code = comp + "BICYCLE";
                    series = new NoSeriesController(_context).GetNoSeries(series_code);
                    refno  = comp + "BIC" + series;

                    BicycleEntryHeader header = new BicycleEntryHeader
                    {
                        ReferenceNo = refno,
                        BicycleId   = BicycleId,
                        CreatedAt   = DateTime.Now.Date,
                        CreatedBy   = User.Identity.GetFullName()
                    };
                    _context.Add(header);
                    _context.SaveChanges();

                    string x = new NoSeriesController(_context).UpdateNoSeries(series, series_code);


                    item.BicycleEntryHeaderId = header.Id;
                    item.UpdatedBy            = User.Identity.GetUserName();
                    item.UpdatedAt            = DateTime.Now.Date;
                    _context.Add(item);


                    headerId = header.Id;
                }
                else
                {
                    var d = _context.BicycleEntryDetails.Where(a => a.BicycleEntryHeaderId == _header.Id).FirstOrDefault();
                    d.BrakeRemarks      = item.BrakeRemarks;
                    d.BrakeSafe         = item.BrakeSafe;
                    d.BrakeUnSafe       = item.BrakeUnSafe;
                    d.ChainRemarks      = item.ChainRemarks;
                    d.ChainSafe         = item.ChainSafe;
                    d.ChainUnSafe       = item.ChainUnSafe;
                    d.CrankChainRemarks = item.CrankChainRemarks;
                    d.CrankChainSafe    = item.CrankChainSafe;
                    d.CrankChainUnSafe  = item.CrankChainUnSafe;
                    d.FrameRemarks      = item.FrameRemarks;
                    d.FrameSafe         = item.FrameSafe;
                    d.FrameUnSafe       = item.FrameUnSafe;
                    d.FrontForkRemarks  = item.FrontForkRemarks;
                    d.FrontForkSafe     = item.FrontForkSafe;
                    d.FrontForkUnSafe   = item.FrontForkUnSafe;
                    d.FrontRearRemarks  = item.FrontRearRemarks;
                    d.FrontRearSafe     = item.FrontRearSafe;
                    d.FrontRearUnSafe   = item.FrontRearUnSafe;

                    d.HandlebarRemarks = item.HandlebarRemarks;
                    d.HandlebarSafe    = item.HandlebarSafe;
                    d.HandlebarUnSafe  = item.HandlebarUnSafe;

                    d.SeatRemarks = item.SeatRemarks;
                    d.SeatSafe    = item.SeatSafe;
                    d.SeatUnSafe  = item.SeatUnSafe;

                    d.InspectedBy = item.InspectedBy;
                    d.NotedBy     = item.NotedBy;

                    d.UpdatedBy = User.Identity.GetUserName();
                    d.UpdatedAt = DateTime.Now.Date;

                    _context.Entry(d).State = EntityState.Modified;
                    _context.SaveChanges();

                    headerId = _header.Id;
                }



                _context.SaveChanges();
                status = "success";
            }
            catch (Exception ex)
            {
                headerId = 0;
                status   = "failed";
                message  = ex.InnerException.Message;
            }



            var model = new
            {
                status
                ,
                message,
                ReferenceId = headerId
            };

            return(Json(model));
        }
Exemplo n.º 8
0
        public IActionResult SaveData(EmergencyLightViewModel[] item)
        {
            int    headerId = 0;
            string status   = "";
            string message  = "";

            string series = "";
            string refno  = "";

            //string series_code = "EMERGENCYLIGHT";
            //series = new NoSeriesController(_context).GetNoSeries(series_code);
            //refno = "EL" + series;


            try
            {
                //var _header = _context.EmergencyLightHeaders
                //    .Where(a => a.Status == "Active")
                //    .Where(a => a.LocationEmergencyLightId == item[0].LocationEmergencyLightId)
                //    .Where(a => a.CreatedAt == DateTime.Now.Date);

                var _header = _context.EmergencyLightHeaders
                              .Where(a => a.AreaId == item[0].AreaId)
                              .Where(a => a.Status == "Active")
                              .Where(a => a.DocumentStatus != "Approved");

                if (_header.Count() == 0)
                {
                    var comp = _context.LocationEmergencyLights.Include(a => a.Areas.Companies).Where(a => a.Id == item[0].LocationEmergencyLightId).FirstOrDefault()
                               .Areas.Companies.Code;
                    if (comp == "SCPC")
                    {
                        comp = "SC";
                    }
                    else
                    {
                        comp = "SL";
                    }
                    string series_code = comp + "EMERGENCYLIGHT";
                    series = new NoSeriesController(_context).GetNoSeries(series_code);
                    refno  = comp + "EL" + series;


                    EmergencyLightHeader header = new EmergencyLightHeader
                    {
                        //header.AreaId = item[0].AreaId;
                        ReferenceNo = refno,
                        AreaId      = item[0].AreaId,
                        CreatedAt   = DateTime.Now.Date,
                        CreatedBy   = User.Identity.GetFullName()
                    };
                    _context.Add(header);
                    _context.SaveChanges();
                    headerId = header.Id;
                    string x = new NoSeriesController(_context).UpdateNoSeries(series, series_code);

                    foreach (var detail in item)
                    {
                        var _detail = new EmergencyLightDetail
                        {
                            ItemId  = detail.ItemId,
                            Battery = Convert.ToInt32(detail.Battery),
                            Bulb    = Convert.ToInt32(detail.Bulb),
                            Usable  = Convert.ToInt32(detail.Usable),

                            CreatedAt = DateTime.Now.Date,
                            UpdatedAt = DateTime.Now.Date,
                            EmergencyLightHeaderId = headerId,
                            Remarks = detail.Remarks,

                            InspectedBy = detail.InspectedBy,
                            ReviewedBy  = detail.ReviewedBy,
                            NotedBy     = detail.NotedBy,
                            LocationEmergencyLightId = detail.LocationEmergencyLightId
                        };

                        _context.Add(_detail);
                    }
                }
                else
                {
                    headerId = _header.FirstOrDefault().Id;

                    foreach (var detail in item)
                    {
                        var d = _context.EmergencyLightDetails
                                .Where(a => a.EmergencyLightHeaderId == headerId)
                                .Where(a => a.LocationEmergencyLightId == detail.LocationEmergencyLightId)
                                .Where(a => a.ItemId == detail.ItemId)
                                .FirstOrDefault();

                        if (d == null)
                        {
                            var _detail = new EmergencyLightDetail
                            {
                                ItemId  = Convert.ToInt32(detail.ItemId),
                                Battery = Convert.ToInt32(detail.Battery),
                                Bulb    = Convert.ToInt32(detail.Bulb),
                                Usable  = Convert.ToInt32(detail.Usable),

                                CreatedAt = DateTime.Now.Date,
                                UpdatedAt = DateTime.Now,
                                EmergencyLightHeaderId = headerId,
                                Remarks = detail.Remarks,

                                InspectedBy = detail.InspectedBy,
                                ReviewedBy  = detail.ReviewedBy,
                                NotedBy     = detail.NotedBy,
                                LocationEmergencyLightId = detail.LocationEmergencyLightId,
                            };

                            _context.Add(_detail);
                        }
                        else
                        {
                            d.Battery = Convert.ToInt32(detail.Battery);
                            d.Bulb    = Convert.ToInt32(detail.Bulb);
                            d.Usable  = Convert.ToInt32(detail.Usable);

                            d.UpdatedAt = DateTime.Now;
                            d.UpdatedBy = User.Identity.GetUserName();
                            d.EmergencyLightHeaderId = headerId;
                            d.Remarks     = detail.Remarks;
                            d.InspectedBy = detail.InspectedBy;
                            d.ReviewedBy  = detail.ReviewedBy;
                            d.NotedBy     = detail.NotedBy;
                            d.LocationEmergencyLightId = detail.LocationEmergencyLightId;
                            _context.Update(d);
                        }
                        _context.SaveChanges();
                    }
                }



                _context.SaveChanges();
                status = "success";
            }
            catch (Exception ex)
            {
                status  = "failed";
                message = ex.InnerException.Message;
            }



            var model = new
            {
                status
                ,
                message,
                ReferenceId = headerId
            };

            return(Json(model));
        }
Exemplo n.º 9
0
        public IActionResult SaveData(FireHydrantViewModel[] item)
        {
            string series = "";
            string refno  = "";
            //string series_code = "FIREHYDRANT";
            //series = new NoSeriesController(_context).GetNoSeries(series_code);
            //refno = "FH" + series;

            int    headerId = 0;
            string status   = "";
            string message  = "";

            try
            {
                //var _header = _context.FireHydrantHeaders.Where(a => a.Status == "Active")
                //// .Where(a => a.AreaId == item[0].AreaId)
                //.Where(a => a.LocationFireHydrantId == item[0].LocationFireHydrantId)
                //.Where(a => a.CreatedAt == DateTime.Now.Date);

                var _header = _context.FireHydrantHeaders
                              .Where(a => a.AreaId == item[0].AreaId)
                              .Where(a => a.Status == "Active")
                              .Where(a => a.DocumentStatus != "Approved");

                if (_header.Count() == 0)
                {
                    var comp = _context.LocationFireHydrants.Include(a => a.Areas.Companies).Where(a => a.Id == item[0].LocationFireHydrantId).FirstOrDefault()
                               .Areas.Companies.Code;
                    if (comp == "SCPC")
                    {
                        comp = "SC";
                    }
                    else
                    {
                        comp = "SL";
                    }
                    string series_code = comp + "FIREHYDRANT";
                    series = new NoSeriesController(_context).GetNoSeries(series_code);
                    refno  = comp + "FH" + series;


                    FireHydrantHeader header = new FireHydrantHeader
                    {
                        ReferenceNo = refno,
                        //LocationFireHydrantId = item[0].LocationFireHydrantId,
                        AreaId    = item[0].AreaId,
                        CreatedAt = DateTime.Now.Date,
                        CreatedBy = User.Identity.GetFullName()
                    };
                    _context.Add(header);
                    _context.SaveChanges();
                    string x = new NoSeriesController(_context).UpdateNoSeries(series, series_code);
                    headerId = header.Id;

                    foreach (var detail in item)
                    {
                        var _detail = new FireHydrantDetail
                        {
                            ItemId                = detail.ItemId,
                            GlassCabinet          = Convert.ToInt32(detail.GlassCabinet),
                            Hanger                = Convert.ToInt32(detail.Hanger),
                            Hose15                = Convert.ToInt32(detail.Hose15),
                            Nozzle15              = Convert.ToInt32(detail.Nozzle15),
                            Hose25                = Convert.ToInt32(detail.Hose25),
                            Nozzle25              = Convert.ToInt32(detail.Nozzle25),
                            SpecialTools          = Convert.ToInt32(detail.SpecialTools),
                            InspectedBy           = detail.InspectedBy,
                            ReviewedBy            = detail.ReviewedBy,
                            NotedBy               = detail.NotedBy,
                            CreatedAt             = DateTime.Now.Date,
                            UpdatedAt             = DateTime.Now.Date,
                            FireHydrantHeaderId   = headerId,
                            Remarks               = detail.Remarks,
                            LocationFireHydrantId = detail.LocationFireHydrantId,
                        };

                        _context.Add(_detail);
                    }
                }
                else
                {
                    headerId = _header.FirstOrDefault().Id;

                    foreach (var detail in item)
                    {
                        var d = _context.FireHydrantDetails
                                .Where(a => a.FireHydrantHeaderId == headerId)
                                .Where(a => a.LocationFireHydrantId == item[0].LocationFireHydrantId)
                                .Where(a => a.ItemId == detail.ItemId)
                                .FirstOrDefault();

                        if (d == null)
                        {
                            var _detail = new FireHydrantDetail
                            {
                                ItemId = detail.ItemId,


                                GlassCabinet = Convert.ToInt32(detail.GlassCabinet),
                                Hanger       = Convert.ToInt32(detail.Hanger),
                                Hose15       = Convert.ToInt32(detail.Hose15),
                                Nozzle15     = Convert.ToInt32(detail.Nozzle15),
                                Hose25       = Convert.ToInt32(detail.Hose25),
                                Nozzle25     = Convert.ToInt32(detail.Nozzle25),
                                SpecialTools = Convert.ToInt32(detail.SpecialTools),


                                InspectedBy = detail.InspectedBy,
                                ReviewedBy  = detail.ReviewedBy,
                                NotedBy     = detail.NotedBy,

                                LocationFireHydrantId = detail.LocationFireHydrantId,


                                CreatedAt           = DateTime.Now.Date,
                                UpdatedAt           = DateTime.Now,
                                FireHydrantHeaderId = headerId,
                                Remarks             = detail.Remarks
                            };

                            _context.Add(_detail);
                        }
                        else
                        {
                            d.GlassCabinet = Convert.ToInt32(detail.GlassCabinet);
                            d.Hanger       = Convert.ToInt32(detail.Hanger);
                            d.Hose15       = Convert.ToInt32(detail.Hose15);
                            d.Nozzle15     = Convert.ToInt32(detail.Nozzle15);
                            d.Hose25       = Convert.ToInt32(detail.Hose25);
                            d.Nozzle25     = Convert.ToInt32(detail.Nozzle25);
                            d.SpecialTools = Convert.ToInt32(detail.SpecialTools);


                            d.InspectedBy = detail.InspectedBy;
                            d.Remarks     = detail.Remarks;
                            d.NotedBy     = detail.NotedBy;

                            d.LocationFireHydrantId = detail.LocationFireHydrantId;



                            d.UpdatedAt           = DateTime.Now;
                            d.UpdatedBy           = User.Identity.GetUserName();
                            d.FireHydrantHeaderId = headerId;
                            d.ReviewedBy          = detail.ReviewedBy;

                            _context.Update(d);
                        }
                        _context.SaveChanges();
                    }
                }



                _context.SaveChanges();
                status = "success";
            }
            catch (Exception ex)
            {
                status  = "failed";
                message = ex.InnerException.Message;
            }



            var model = new
            {
                status
                ,
                message
                ,
                ReferenceId = headerId
            };

            return(Json(model));
        }