public JsonResult SaveFile(CompanyInvoiceViewModel model)
        {
            Response response;

            try
            {
                var    currentUser = GetAuthenticatedUser();
                string oldFileName = "";
                using (var db = new KiaGalleryContext())
                {
                    if (model.id > 0)
                    {
                        var entity = db.CompanyInvoices.SingleOrDefault(x => x.Id == model.id);
                        entity.AttachmentFile = model.attachmentFile;
                        entity.ModifyUserId   = currentUser.Id;
                        entity.ModifyDate     = DateTime.Now;
                        if (!string.IsNullOrEmpty(entity.AttachmentFile) && entity.AttachmentFile != model.attachmentFile)
                        {
                            oldFileName = entity.FileName;
                        }
                    }
                    else
                    {
                        var entity = new CompanyInvoice()
                        {
                            AttachmentFile = model.attachmentFile,
                            CreateUserId   = currentUser.Id,
                            ModifyUserId   = currentUser.Id,
                            CreateDate     = DateTime.Now,
                            ModifyDate     = DateTime.Now,
                        };
                        db.CompanyInvoices.Add(entity);
                    }
                    db.SaveChanges();

                    if (!string.IsNullOrEmpty(oldFileName) && System.IO.File.Exists(Server.MapPath("~/Upload/companyInvoice/" + oldFileName)))
                    {
                        System.IO.File.Delete(Server.MapPath("~/Upload/companyInvoice/" + oldFileName));
                    }
                }
                response = new Response()
                {
                    status  = 200,
                    message = "عملیات با موفقیت ثبت شد"
                };
            }
            catch (Exception ex)
            {
                response = Core.GetExceptionResponse(ex);
            }
            return(Json(response, JsonRequestBehavior.AllowGet));
        }
        /// <summary>
        /// ذخیره
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public JsonResult Save(CompanyInvoiceViewModel model)
        {
            Response response;

            try
            {
                var    user        = GetAuthenticatedUser();
                string oldFileName = "";
                using (var db = new KiaGalleryContext())
                {
                    if (model.id != null && model.id > 0)
                    {
                        var entity = db.CompanyInvoices.Where(x => x.Id == model.id).SingleOrDefault();
                        entity.BranchId              = user.BranchId;
                        entity.BuyerAddress          = model.buyerAddress;
                        entity.Reduction             = model.reduction;
                        entity.BuyerEconomicalNumber = model.buyerEconomicalNumber;
                        entity.BuyerName             = model.buyerName;
                        entity.BuyerNationalId       = model.buyerNationalId;
                        entity.BuyerPhone            = model.buyerPhone;
                        entity.BuyerPostalCode       = model.buyerPostalCode;
                        entity.Date         = DateUtility.GetDateTime(model.date);
                        entity.ModifyUserId = user.Id;
                        entity.ModifyDate   = DateTime.Now;


                        foreach (var item in model.companyInvoiceDetailViewModel)
                        {
                            if (item.detailId != null && item.detailId > 0)
                            {
                                var detail = entity.CompanyInvoiceDetailList.Where(y => y.Id == item.detailId).SingleOrDefault();
                                detail.Carat = item.carat;
                                detail.DescriptionProduct = item.descriptionProduct;
                                detail.Gram = item.gram;
                                detail.IdentificationCode = item.identificationCode;
                                detail.StonePrice         = item.stonePrice;
                                detail.StoneWeight        = item.stoneWeight;
                                detail.Wage      = item.wage;
                                detail.Whistle   = item.whistle;
                                detail.GoldPrice = item.goldPrice;
                            }
                        }
                    }
                    else
                    {
                        var item = new CompanyInvoice()
                        {
                            BranchId              = user.BranchId,
                            BuyerAddress          = model.buyerAddress,
                            Reduction             = model.reduction,
                            BuyerEconomicalNumber = model.buyerEconomicalNumber,
                            BuyerName             = model.buyerName,
                            BuyerNationalId       = model.buyerNationalId,
                            BuyerPhone            = model.buyerPhone,
                            BuyerPostalCode       = model.buyerPostalCode,
                            Date = DateTime.Now,
                            //Number = model.number,
                            AttachmentFile = model.attachmentFile,
                            CreateUserId   = user.Id,
                            ModifyUserId   = user.Id,
                            CreateDate     = DateTime.Now,
                            ModifyDate     = DateTime.Now,
                        };
                        db.CompanyInvoices.Add(item);
                        foreach (var value in model.companyInvoiceDetailViewModel)
                        {
                            var detail = new CompanyInvoiceDetail()
                            {
                                CompanyInvoice     = item,
                                Carat              = value.carat,
                                DescriptionProduct = value.descriptionProduct,
                                Gram = value.gram,
                                IdentificationCode = value.identificationCode,
                                StonePrice         = value.stonePrice,
                                StoneWeight        = value.stoneWeight,
                                Wage      = value.wage,
                                Whistle   = value.whistle,
                                GoldPrice = value.goldPrice,
                            };
                            db.CompanyInvoiceDetails.Add(detail);
                        }
                    }
                    db.SaveChanges();

                    response = new Response()
                    {
                        status  = 200,
                        message = "عملیات با موفقیت انجام شد."
                    };
                }
            }
            catch (Exception ex)
            {
                response = Core.GetExceptionResponse(ex);
            }
            return(Json(response, JsonRequestBehavior.AllowGet));
        }