Ejemplo n.º 1
0
        public virtual async Task <ActionResult> ApplyDiscountCode(ApplyDiscountCodeViewModel model)
        {
            var tblInvoice = await _invoiceService.FindByIdAsync(model.InvoiceId);

            if (tblInvoice == null)
            {
                return(RedirectToAction("GetInvoiceTablePartial", "Invoice", new { invoiceId = model.InvoiceId }));
            }
            if (string.IsNullOrWhiteSpace(model.DiscountCode))
            {
                await _dbContext.InvoicesDiscountCode.Where(p => p.InvoiceId == model.InvoiceId).DeleteAsync();

                await _invoiceService.ApplyInvoiceDiscountsAsync(tblInvoice);

                return(RedirectToAction("GetInvoiceTablePartial", "Invoice", new { invoiceId = model.InvoiceId }));
            }

            model.DiscountCode = model.DiscountCode.Trim();
            var tblDiscountCode = await _dbContext.DiscountCode.FirstOrDefaultAsync(p => p.DiscountCode == model.DiscountCode);

            if (tblDiscountCode == null)
            {
                TempData["discountCodeError"] = _localizationService.GetResource("Plugin.DiscountCode.InvalidDiscountCode");
                return(RedirectToAction("GetInvoiceTablePartial", "Invoice", new { invoiceId = model.InvoiceId }));
            }
            if (tblDiscountCode.ExpiryDate != null && tblDiscountCode.ExpiryDate < DateTime.Now)
            {
                TempData["DiscountCodeIsExpired"] = _localizationService.GetResource("Plugin.DiscountCode.InvalidDiscountCode");
                return(RedirectToAction("GetInvoiceTablePartial", "Invoice", new { invoiceId = model.InvoiceId }));
            }
            if (tblDiscountCode.MaxNumberOfUsage != null && tblDiscountCode.MaxNumberOfUsage <= await _dbContext.InvoicesDiscountCode.CountAsync(p => p.DiscountCode == tblDiscountCode.DiscountCode && p.Invoice.Status == InvoiceStatus.Paid))
            {
                TempData["DiscountCodeIsExpired"] = _localizationService.GetResource("Plugin.DiscountCode.InvalidDiscountCode");
                return(RedirectToAction("GetInvoiceTablePartial", "Invoice", new { invoiceId = model.InvoiceId }));
            }

            await _dbContext.InvoicesDiscountCode.Where(p => p.InvoiceId == model.InvoiceId).DeleteAsync();

            _dbContext.InvoicesDiscountCode.Add(new TblInvoicesDiscountCode()
            {
                DiscountCode = tblDiscountCode.DiscountCode,
                InvoiceId    = model.InvoiceId
            });
            await _dbContext.SaveChangesAsync();

            await _invoiceService.ApplyInvoiceDiscountsAsync(tblInvoice);

            return(RedirectToAction("GetInvoiceTablePartial", "Invoice", new { invoiceId = model.InvoiceId }));
        }
Ejemplo n.º 2
0
        public virtual ActionResult Index(string widgetZone, object additionalData = null)
        {
            if (additionalData is InvoiceModel invoiceModel)
            {
                var tblDiscountCode = _dbContext.InvoicesDiscountCode.FirstOrDefault(p => p.InvoiceId == invoiceModel.Id);
                var model           = new ApplyDiscountCodeViewModel()
                {
                    InvoiceId     = invoiceModel.Id,
                    InvoiceStatus = invoiceModel.Status,
                    DiscountCode  = tblDiscountCode?.DiscountCode
                };
                return(View("~/Plugins/Plugin.DiscountCode/Views/ApplyDiscountCode.cshtml", model));
            }

            return(Content(""));
        }