public bool Add(VoucherAddView entity) { try { int count = 0; int max = entity.LimitNumber; while (count < max) { Voucher voucher = new Voucher(); voucher.AmountReduced = entity.AmountReduced; voucher.IsApplyOrder = entity.IsApplyOrder; voucher.IsUsed = false; voucher.PercentReduced = entity.PercentReduced; voucher.Product = entity.Product; voucher.Promotion = entity.Promotion; voucher.Type = entity.Type; voucher.Code = $"{entity.PromotionCode}-{RandomUtils.RandomString(9, 9, true, true, false)}"; if (!_voucher.CheckExistsCode(voucher.Code)) { if (_voucher.Add(voucher) != null) { _unitOfWork.Commit(); count += 1; } } } return(true); } catch (Exception) { return(false); } }
public IActionResult Create([FromBody] Voucher voucher) { if (voucher == null || voucher.UserId == null) { return(BadRequest()); } _repository.Add(voucher); // return CreatedAtRoute("GetVoucher", new { id = voucher.VoucherId }, voucher); return(new NoContentResult()); }
public ActionResult Create(CreateVoucherViewModel model) { if (!ModelState.IsValid) { return(View(model)); } using (ITransaction transaction = _session.BeginTransaction()) { var voucher = Voucher.CreateVoucher(model.CustomerId, new Money(model.Amount), _codeGenerator); _voucherRepository.Add(voucher); transaction.Commit(); } return(RedirectToAction("Index")); }
public async Task <IActionResult> Create(VoucherViewModel model) { // získání přihlášeného uživatele var user = await userManager.FindByNameAsync(model.ApplicationUserName); // zobrazení chyby v případě, že se uživatele nepodařilo získat if (user == null) { ModelState.AddModelError("ApplicationUserName", "Chyba při zjískávání Id uživatele"); } // kontrola datumů začátku a konce platnosti if (model.ValidFrom > model.ValidUntil) { ModelState.AddModelError("ValidFrom", "Datum začátku platnosti musí být dříve než datum konce platnosti"); ModelState.AddModelError("ValidUntil", "Datum konce platnosti musí být později než datum začátku platnosti"); } // kontrola platnosti předaného modelu if (ModelState.IsValid) { Voucher newVoucher = new Voucher { ValidFrom = model.ValidFrom, ValidUntil = model.ValidUntil, Title = model.Title, Description = model.Description, Value = model.Value, //VoucherTypeId = model.VoucherTypeId.Value, // vygenerování pseoudonáhodného kódu voucheru Code = (DateTime.Now.Ticks - new DateTime(2016, 1, 1).Ticks).ToString("x"), CreationDate = DateTime.Now, IsValid = true, ApplicationUserId = user.Id }; // uložení voucheru do DB voucherRepository.Add(newVoucher); return(RedirectToAction("details", new { id = newVoucher.Id })); } //model.VoucherTypes = GetVoucherTypesToList(true); return(View(model)); }
public async Task <string> Generate(TimeSpan duration, string productId, string comments) { const string UNAMBIGUOUS_LETTERS = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; var code = Enumerable.Range(0, 12).Select(x => UNAMBIGUOUS_LETTERS.PickRandom()).ToString(""); if (await VoucherRepository.GetByCode(code) is not null) { return(await Generate(duration, productId, comments)); } await VoucherRepository.Add(new Voucher { Id = Guid.NewGuid().ToString(), Code = code, Duration = duration, ProductId = productId, Comments = comments }); return(code); }
public IActionResult AddVoucher([FromBody] Voucher model) { Vou_repo.Add(model); if (model.IsFinal == true) { PostedVoucher PV = new PostedVoucher() { CompanyId = model.CompanyId, VoucherId = model.VoucherId, VoucherCode = model.VoucherCode, Date = model.Date, Description = model.Description, TotalCreditAmount = model.TotalCreditAmount, TotalDebitAmount = model.TotalDebitAmount, FinancialYearId = model.FinancialYearId, VoucherTypeId = model.VoucherTypeId, CreatedAt = DateTime.Now }; IEnumerable <TransactionAccount> UALs = UAL_Repo.GetAll(); IList <TransactionAccount> UpdateUALs = new List <TransactionAccount>(); IList <VoucherDetail> UpdateVoucherDetails = new List <VoucherDetail>(); foreach (VoucherDetail VD in model.VoucherDetails) { TransactionAccount UL = UALs.FirstOrDefault(a => a.AccountId == VD.AccountId); VD.AccountBalanceAmountBeforePosting = UL.CurrentBalance; UL.TotalDebit += VD.DebitAmount; UL.TotalCredit += VD.CreditAmount; UL.CurrentBalance = UL.CurrentBalance - VD.DebitAmount + VD.CreditAmount; VD.AccountBalanceAmountAfterPosting = UL.CurrentBalance; UpdateUALs.Add(UL); UpdateVoucherDetails.Add(VD); } UAL_Repo.UpdateRange(UpdateUALs); PostedVou_repo.Add(PV); model.PostedVoucherId = PV.PostedVoucherId; model.VoucherDetails = UpdateVoucherDetails; } else if (model.IsFinal == false || model.IsFinal == null) { UnpostedVoucher UV = new UnpostedVoucher() { CompanyId = model.CompanyId, VoucherId = model.VoucherId, VoucherCode = model.VoucherCode, Date = model.Date, Description = model.Description, TotalCreditAmount = model.TotalCreditAmount, TotalDebitAmount = model.TotalDebitAmount, FinancialYearId = model.FinancialYearId, VoucherTypeId = model.VoucherTypeId }; UnpostedVou_repo.Add(UV); model.UnpostedVoucherId = UV.UnpostedVoucherId; } Vou_repo.Update(model); return(new OkObjectResult(new { VoucherID = model.VoucherId })); }