public JsonResult Delete(Guid kClaimId)
        {
            try
            {
                //Thread.Sleep(50);
                Claim itemFound = ClaimManager.GetById(kClaimId);
                ClaimManager.Delete(kClaimId);

                //create remind for history
                RemindHistory remind = new RemindHistory
                {
                    sRemind  = string.Format("ลบ {0}", itemFound.sClaimNo),
                    kStaffId = (Guid)Membership.GetUser().ProviderUserKey
                };
                RemindHistoryManager.Create(remind);

                var clientName = User.Identity.Name;
                Task.Factory.StartNew(() =>
                {
                    var clients = Hub.GetClients <RealTimeJTableDemoHub>();
                    clients.RecordDeleted(clientName, itemFound.sClaimNo);
                });


                return(Json(new { Result = "OK" }));
            }
            catch (Exception ex)
            {
                return(Json(new { Result = "ERROR", Message = ex.Message }));
            }
        }
 public static void Create(RemindHistory model)
 {
     using (DataContext db = new DataContext())
     {
         db.RemindHistories.Add(model);
         db.SaveChanges();
     }
 }
        public ActionResult PostNote(string content)
        {
            RemindHistory model = new RemindHistory
            {
                sRemind  = content,
                kStaffId = (Guid)Membership.GetUser().ProviderUserKey
            };

            RemindHistoryManager.Create(model);

            //List<RemindHistory> items = new List<RemindHistory>();
            //items.Add(model);
            var    items   = RemindHistoryManager.GetLasts(model.dtDateAdd);
            string message = string.Empty;
            bool   IsNew   = false;

            if (items.Count > 0)
            {
                IsNew = true;
            }


            var staff      = StaffManager.GetById((Guid)Membership.GetUser().ProviderUserKey);
            var clientName = staff.sStaffName;

            Task.Factory.StartNew(() =>
            {
                var clients = Hub.GetClients <RealTimeJTableDemoHub>();
                clients.RecordUpdated(clientName, content);
            });

            return(Json(new
            {
                Html = this.RenderPartialView("Lists", items),
                Message = message,
                IsNew = IsNew
            }, JsonRequestBehavior.AllowGet));
        }
        public ActionResult Create(BookingClaimViewModel model, FormCollection collection)
        {
            try
            {
                //initial working status
                var workingStatusId = WorkingStatusManager.GetAll().Where(m => m.iDefault == (int)Working.Claiming).SingleOrDefault();

                if (model.Product != null)
                {
                    model.Product.Brand = BrandManager.GetById(model.Product.kBrandId);
                }
                model.Product.ProductType = ProductTypeManager.GetById(model.Product.kProductTypeId);

                #region SAVE CUSTOMER
                var insCount = InsuranceManager.GetCountDuplicate(model.Insurance.sInsuranceName.Trim());
                if (insCount.Count <= 0)
                {
                    Insurance insurance = new Insurance
                    {
                        kInsuranceId   = Guid.NewGuid(),
                        sInsuranceName = model.Insurance.sInsuranceName,
                        sAddress1      = model.Insurance.sAddress1,
                        sCity          = model.Insurance.sCity,
                        sZip           = model.Insurance.sZip,
                        sPhone         = model.Insurance.sPhone,
                        sMobile        = model.Insurance.sMobile,
                        sFax           = model.Insurance.sFax,
                        sEmailAddress  = model.Insurance.sEmailAddress
                    };
                    InsuranceManager.Create(insurance);
                    model.Claim.Insurance = insurance;
                }
                else
                {
                    model.Claim.Insurance = insCount[0];
                }
                #endregion

                #region SAVE Brand
                var brandCount = BrandManager.GetCountDuplicate(model.Product.Brand.sBrandName.Trim());
                if (brandCount.Count <= 0)
                {
                    Brand brand = new Brand
                    {
                        kBrandId   = Guid.NewGuid(),
                        sBrandName = model.Product.Brand.sBrandName
                    };
                    BrandManager.Create(brand);
                    model.Product.Brand = brand;
                }
                else
                {
                    model.Product.Brand = brandCount[0];
                }
                #endregion

                #region SAVE Product Type
                var productTypeCount = ProductTypeManager.GetCountDuplicate(model.Product.ProductType.sDescription.Trim());
                if (productTypeCount.Count <= 0)
                {
                    ProductType productType = new ProductType
                    {
                        kProductTypeId = Guid.NewGuid(),
                        sDescription   = model.Product.ProductType.sDescription
                    };
                    ProductTypeManager.Create(productType);
                    model.Product.ProductType = productType;
                }
                else
                {
                    model.Product.ProductType = productTypeCount[0];
                }
                #endregion

                #region SAVE PRODUCT
                var productCount = ProductManager.GetCountDuplicate(
                    model.Product.sProductName.Trim()
                    //, model.Product.Brand.sBrandName.Trim()
                    //, model.Product.ProductType.sDescription.Trim()
                    );
                if (productCount.Count <= 0)
                {
                    Product product = new Product
                    {
                        kProductId     = Guid.NewGuid(),
                        kProductTypeId = productTypeCount.SingleOrDefault().kProductTypeId,
                        kBrandId       = brandCount.SingleOrDefault().kBrandId,
                        sProductName   = model.Product.sProductName,
                        sProductModel  = model.Product.sProductModel
                    };
                    ProductManager.Create(product);
                    model.Product = product;
                }
                else
                {
                    model.Product = productCount[0];
                }
                #endregion

                #region SAVE CLAIM BOOKING
                var repair = RepairManager.GetByRepairNo(model.Claim.sRepairNo ?? "");

                //initial booking first character
                string char_number = CharBooking.C.ToString();

                Claim claim = new Claim();
                claim.dtDateUpdate = DateTime.Now;
                claim.kClaimId     = Guid.NewGuid();
                claim.kStaffId     = (Guid)Membership.GetUser().ProviderUserKey;
                claim.kOwnerId     = repair.kOwnerId ?? (Guid)Membership.GetUser().ProviderUserKey;
                claim.kInsuranceId = model.Claim.Insurance.kInsuranceId;
                claim.kProductId   = model.Product.kProductId;
                claim.sRepairNo    = model.Claim.sRepairNo;
                claim.sClaimNo     = String.Format("{0}{1}", char_number, ClaimNextItemNoManager.GetNextItemNo());
                claim.sSerial      = model.Claim.sSerial;
                if (model.Claim.dtInsuranceExpire != null)
                {
                    claim.dtInsuranceExpire = model.Claim.dtInsuranceExpire.Value.AddYears(-543);
                }


                model.Claim = claim;
                ClaimManager.Create(model.Claim);
                ClaimNextItemNoManager.IncreaseNextItemNo();

                #region also pdate repair side

                repair.dtDateUpdate = DateTime.Now;
                repair.kStaffId     = (Guid)Membership.GetUser().ProviderUserKey;
                RepairManager.Edit(repair);

                #endregion

                #endregion

                #region SAVE REPAIR WORKING STATUS

                ClaimStatus claimStatus = new ClaimStatus
                {
                    kClaimStatusId   = Guid.NewGuid(),
                    kWorkingStatusId = workingStatusId.kWorkingStatusId,
                    kStaffId         = (Guid)Membership.GetUser().ProviderUserKey,
                    kClaimId         = model.Claim.kClaimId
                };
                ClaimStatusManager.Create(claimStatus);

                #endregion

                #region UPDATE REPAIR WORKING STATUS
                if (!String.IsNullOrEmpty(model.Claim.sRepairNo))
                {
                    repair = RepairManager.GetByRepairNo(model.Claim.sRepairNo);

                    RepairStatus repairStatus = new RepairStatus
                    {
                        kRepairStatusId  = Guid.NewGuid(),
                        kWorkingStatusId = workingStatusId.kWorkingStatusId,
                        kStaffId         = (Guid)Membership.GetUser().ProviderUserKey,
                        kRepairId        = repair.kRepairId
                    };
                    RepairStatusManager.Create(repairStatus);
                }
                #endregion



                #region SAVE CAUSE OF CLAIM

                //initial cause
                string keyname;
                string keyvalue;
                int    countvalue = 0;
                for (int i = 0; i <= collection.Count - 1; i++)
                {
                    keyname  = collection.AllKeys[i];
                    keyvalue = collection[i];

                    if (keyname.Contains("cause"))
                    {
                        countvalue++;
                    }
                }

                if (countvalue > 1)
                {
                    model.ClaimCauses = new List <ClaimCause>();
                    for (int i = 1; i <= countvalue / 4; i++)
                    {
                        ClaimCause claimCause = new ClaimCause();
                        claimCause.kClaimCauseId = Guid.NewGuid();
                        claimCause.kClaimId      = model.Claim.kClaimId;
                        claimCause.kStaffId      = (Guid)Membership.GetUser().ProviderUserKey;
                        claimCause.sDescription  = collection[String.Format("cause_description_{0}", i)];
                        claimCause.sNote         = collection[String.Format("cause_note_{0}", i)];

                        if (!String.IsNullOrEmpty(claimCause.sDescription))
                        {
                            int qty = 0;
                            if (int.TryParse(collection[String.Format("cause_qty_{0}", i)], out qty))
                            {
                                claimCause.iQty = qty;
                            }

                            Decimal price = 0;
                            if (Decimal.TryParse(collection[String.Format("cause_price_{0}", i)], out price))
                            {
                                claimCause.dPrice = price;
                            }

                            model.ClaimCauses.Add(claimCause);
                            ClaimCourseManager.Create(claimCause);
                        }
                    }
                }

                if (model.ClaimCauses != null)
                {
                    foreach (var item in model.ClaimCauses)
                    {
                        var causeCount = CauseManager.GetCountDuplicate(item.sDescription.Trim());
                        if (causeCount <= 0)
                        {
                            Cause cause = new Cause
                            {
                                kCauseId     = Guid.NewGuid(),
                                sDescription = item.sDescription.Trim()
                            };
                            CauseManager.Create(cause);
                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                return(RedirectToAction("Error", "Home"));
            }

            Claim model2 = ClaimManager.GetById(model.Claim.kClaimId);

            //create remind for history
            RemindHistory remind = new RemindHistory
            {
                sRemind  = model2.vMessage,
                kStaffId = model2.kStaffId
            };
            RemindHistoryManager.Create(remind);

            var clientName = User.Identity.Name;
            Task.Factory.StartNew(() =>
            {
                var clients = Hub.GetClients <RealTimeJTableDemoHub>();
                clients.RecordCreated(clientName, model2);
            });

            return(RedirectToAction("Index"));
        }
        public JsonResult EditClaim(Claim claim_sending, ClaimStatus status_sending)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(Json(new { Result = "ERROR", Message = "Form is not valid! Please correct it and try again." }));
                }

                Repair repair    = null;
                Claim  itemFound = ClaimManager.GetById(claim_sending.kClaimId);
                if (itemFound == null)
                {
                    return(Json(new { Result = "ERROR", Message = "Item Not Found" }));
                }
                if (itemFound.IsComplete == true)
                {
                    return(Json(new { Result = "ERROR", Message = "ปิด job!" }));
                }

                var status = WorkingStatusManager.GetById(status_sending.kWorkingStatusId);
                if (status.iDefault == (int)Working.ConfirmRepair && claim_sending.IsRecieved != true)
                {
                    return(Json(new { Result = "ERROR", Message = "ตรวจรับสินค้า!" }));
                }


                bool isStatusChange = false;
                if (status_sending.kWorkingStatusId != itemFound.kWorkingStatusId)
                {
                    #region update claim status side
                    if (status.iDefault == (int)Working.ConfirmRepair)
                    {
                        status_sending.kStaffId = itemFound.kOwnerId.Value;
                    }
                    else
                    {
                        status_sending.kStaffId = (Guid)Membership.GetUser().ProviderUserKey;
                    }
                    status_sending.dtDateAdd      = DateTime.Now;
                    status_sending.kClaimStatusId = Guid.NewGuid();
                    ClaimStatusManager.Create(status_sending);

                    #endregion

                    #region also update repair status side
                    if (!String.IsNullOrEmpty(itemFound.sRepairNo))
                    {
                        repair = RepairManager.GetByRepairNo(itemFound.sRepairNo);
                        RepairStatus repairStatus = new RepairStatus
                        {
                            kStaffId         = itemFound.kOwnerId.Value,
                            kRepairId        = repair.kRepairId,
                            kWorkingStatusId = status_sending.kWorkingStatusId,
                            kRepairStatusId  = Guid.NewGuid()
                        };
                        RepairStatusManager.Create(repairStatus);
                    }
                    #endregion

                    isStatusChange = true;
                }


                #region update claim
                //assign claim
                itemFound.dtDateUpdate = DateTime.Now;
                itemFound.IsRecieved   = claim_sending.IsRecieved;
                itemFound.IsNoCredit   = claim_sending.IsNoCredit;
                if (isStatusChange)
                {
                    itemFound.kStaffId = itemFound.kOwnerId.Value;
                }
                ClaimManager.Edit(itemFound);
                #endregion

                #region update repair
                if (isStatusChange)
                {
                    if (repair != null)
                    {
                        repair.kStaffId = status_sending.kStaffId;
                    }
                    RepairManager.Edit(repair);
                }
                #endregion

                if (isStatusChange)
                {
                    //create remind for history
                    RemindHistory remind = new RemindHistory
                    {
                        sRemind  = itemFound.vMessage,
                        kStaffId = itemFound.kOwnerId.Value
                    };
                    RemindHistoryManager.Create(remind);

                    var clientName = User.Identity.Name;
                    Task.Factory.StartNew(() =>
                    {
                        var clients = Hub.GetClients <RealTimeJTableDemoHub>();
                        clients.RecordUpdated(clientName, itemFound);
                    });
                }

                return(Json(new { Result = "OK", Records = itemFound }));
            }
            catch (Exception ex)
            {
                return(Json(new { Result = "ERROR", Message = ex.Message }));
            }
        }