// acknowledge selected service report
        public ActionResult Acknowledge(string ServiceRequestNumber)
        {
            var tblServiceReport = new tblServiceReport();

            var tblServiceRequest = db.tblServiceRequests.
                                    Where(x => x.ServiceRequestNumber == ServiceRequestNumber).FirstOrDefault();

            tblServiceReport.ContactNo          = tblServiceRequest.ContactNumber;
            tblServiceReport.ContactEmail       = tblServiceRequest.ContactEmail;
            tblServiceReport.FacilityId         = tblServiceRequest.FacilityId;
            tblServiceReport.BuildingId         = tblServiceRequest.BuildingId;
            tblServiceReport.FloorId            = tblServiceRequest.FloorId;
            tblServiceReport.AreaId             = tblServiceRequest.AreaId;
            tblServiceReport.RoomId             = tblServiceRequest.RoomId;
            tblServiceReport.RequestType        = tblServiceRequest.RequestType;
            tblServiceReport.AssetTypeId        = tblServiceRequest.AssetTypeId;
            tblServiceReport.CategoryIdParentId = tblServiceRequest.CategoryIdParentId;
            tblServiceReport.CategoryTypeId     = tblServiceRequest.CategoryId;
            tblServiceReport.ServiceReportNo    = ServiceRequestNumber;
            tblServiceReport.ReportDate         = DateTime.Now;
            tblServiceReport.Status             = "Acknowledged";

            var TblWorkOrder = db.tblWorkOrders.Where(x => x.ServiceRequestId == tblServiceRequest.Id).FirstOrDefault();

            tblServiceReport.WorkOrderId = TblWorkOrder.Id;
            tblServiceReport.WORefNo     = TblWorkOrder.WorkOrderRefNo;

            tblServiceRequest.Status          = "Acknowledged";
            TblWorkOrder.Status               = "Acknowledged";
            tblServiceRequest.AcknowledgeDate = DateTime.Now;

            tblServiceRequestStatu TBLStatus = new tblServiceRequestStatu();

            TBLStatus.dtTimeStamp       = DateTime.Now;
            TBLStatus.iServiceRequestId = tblServiceRequest.Id;
            TBLStatus.iStatusId         = db.TblServiceStates.Where(x => x.ServiceState == "Acknowledged").FirstOrDefault().id;
            db.tblServiceRequestStatus.Add(TBLStatus);

            db.tblServiceReports.Add(tblServiceReport);

            db.SaveChanges();

            return(RedirectToAction("Edit", new { WORefNo = TblWorkOrder.WorkOrderRefNo }));
        }
        public ActionResult Edit(ServiceRequestsModels Model)
        {
            if (ModelState.IsValid)
            {
                var TblServiceReport = db.tblServiceReports.Where(x => x.WorkOrderId == Model.WorkOrderId)?.FirstOrDefault();

                var tblServiceRequest = db.tblServiceRequests.
                                        Where(x => x.ServiceRequestNumber == Model.ServiceRequestNumber).FirstOrDefault();

                var TblWorkOrder = db.tblWorkOrders.Where(x => x.ServiceRequestId == tblServiceRequest.Id)?.FirstOrDefault();

                if (tblServiceReport == null)
                {
                    var tblServiceReport = new tblServiceReport();

                    tblServiceReport.ContactNo          = tblServiceRequest.ContactNumber;
                    tblServiceReport.ContactEmail       = tblServiceRequest.ContactEmail;
                    tblServiceReport.FacilityId         = tblServiceRequest.FacilityId;
                    tblServiceReport.BuildingId         = tblServiceRequest.BuildingId;
                    tblServiceReport.FloorId            = tblServiceRequest.FloorId;
                    tblServiceReport.AreaId             = tblServiceRequest.AreaId;
                    tblServiceReport.RoomId             = tblServiceRequest.RoomId;
                    tblServiceReport.RequestType        = tblServiceRequest.RequestType;
                    tblServiceReport.AssetTypeId        = tblServiceRequest.AssetTypeId;
                    tblServiceReport.CategoryIdParentId = tblServiceRequest.CategoryIdParentId;
                    tblServiceReport.ServiceReportNo    = Model.ServiceRequestNumber;
                    tblServiceReport.ReportDate         = DateTime.Now;
                    tblServiceReport.Status             = "Acknowledged";

                    tblServiceReport.WorkOrderId = TblWorkOrder.Id;
                    tblServiceReport.WORefNo     = TblWorkOrder.WorkOrderRefNo;

                    tblServiceRequest.Status          = "Acknowledged";
                    TblWorkOrder.Status               = "Acknowledged";
                    tblServiceRequest.AcknowledgeDate = DateTime.Now;

                    tblServiceRequestStatu TBLStatus = new tblServiceRequestStatu();
                    TBLStatus.dtTimeStamp             = DateTime.Now;
                    TBLStatus.iServiceRequestId       = tblServiceRequest.Id;
                    TBLStatus.iServiceRequestStatusId = db.TblServiceStates.Where(x => x.ServiceState == "Acknowledged").FirstOrDefault().id;
                    db.tblServiceRequestStatus.Add(TBLStatus);

                    db.tblServiceReports.Add(tblServiceReport);

                    db.SaveChanges();
                    TblServiceReport = tblServiceReport;
                }
                if (!string.IsNullOrEmpty(Model.DigitalSignatureImageData))
                {
                    Model.DigitalSignatureImageData = Model.DigitalSignatureImageData.Replace("data:image/png;base64,", String.Empty);
                    string filename = DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString()
                                      + DateTime.Now.Year.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() +
                                      DateTime.Now.Second.ToString();
                    var    UniqueNumber = Convert.ToString(Guid.NewGuid());
                    string path         = Path.Combine("C:\\Program Files (x86)\\Johnson Controls\\EFIRM\\ImagesSignature",
                                                       Path.GetFileName(filename + UniqueNumber) + ".png");
                    using (FileStream fs = new FileStream(path, FileMode.Create))
                    {
                        using (BinaryWriter bw = new BinaryWriter(fs))

                        {
                            byte[] data = Convert.FromBase64String(Model.DigitalSignatureImageData);
                            bw.Write(data);
                            bw.Close();
                        }
                    }
                    TblServiceReport.SignaturePath = filename + UniqueNumber + ".png";
                    TblServiceReport.Status        = "Completed";
                    TblWorkOrder.Status            = "Completed";
                    tblServiceRequest.Status       = "Completed";
                    tblServiceRequest.ClosingDate  = DateTime.Now;


                    tblServiceRequestStatu TBLStatus = new tblServiceRequestStatu();
                    TBLStatus.dtTimeStamp       = DateTime.Now;
                    TBLStatus.iServiceRequestId = tblServiceRequest.Id;
                    TBLStatus.iStatusId         = db.TblServiceStates.Where(x => x.ServiceState == "Completed").FirstOrDefault().id;
                    db.tblServiceRequestStatus.Add(TBLStatus);
                }
                TblServiceReport.Comments = Model.Comments;
                db.SaveChanges();

                //Adding files to table
                if (Model.Files != null && Model.Files.Any())
                {
                    foreach (var file in Model.Files)
                    {
                        if (file != null && file.ContentLength > 0)
                        {
                            string newfilename = Path.GetFileNameWithoutExtension(file.FileName) + "_" + Guid.NewGuid() + "SREP" + Path.GetExtension(file.FileName);
                            string path        = Path.Combine("C:\\Program Files (x86)\\Johnson Controls\\EFIRM\\ImagesHelpDesk\\ServiceReport",
                                                              newfilename);
                            file.SaveAs(path);

                            var TblServiceRequestImages = new tblServiceRequestImage
                            {
                                sImageName        = newfilename,
                                nServiceRequestId = tblServiceRequest.Id
                            };
                            db.tblServiceRequestImages.Add(TblServiceRequestImages);
                        }
                    }
                    db.SaveChanges();
                }
            }
            return(RedirectToAction("Edit", new { WORefNo = Model.WorkOrderRef }));
        }