public JsonResult Create(Brand model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(Json(new { Result = "ERROR", Message = "Form is not valid! Please correct it and try again." }));
                }
                var brandCount = BrandManager.GetCountDuplicate(model.sBrandName.Trim());
                if (brandCount.Count >= 1)
                {
                    return(Json(new { Result = "ERROR", Message = "Item Exists." }));
                }

                model.kBrandId = Guid.NewGuid();
                BrandManager.Create(model);

                var clientName = User.Identity.Name;
                Task.Factory.StartNew(() =>
                {
                    var clients = Hub.GetClients <RealTimeJTableDemoHub>();
                    clients.RecordCreated(clientName, model);
                });
                return(Json(new { Result = "OK", Record = model }));
            }
            catch (Exception ex)
            {
                return(Json(new { Result = "ERROR", Message = ex.Message }));
            }
        }
        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"));
        }