Exemple #1
0
        public async Task <ActionResult <object> > Refill(long id, [FromBody] CustomerCredit customer, CancellationToken cancellationToken)
        {
            var card = _dbContext.Set <CcCard>().FirstOrDefault(p => p.Id == id);

            if (card == null /*|| card.Activated != "t"*/)
            {
                return(NotFound(new BaseResponseModel <object> {
                    success = false, message = "Card Not found", code = 404
                }));
            }
            if (customer.description == null || customer.description.Trim() == "")
            {
                customer.description = "Refill Using API";
            }
            var VAT            = card.Vat;
            var credited       = (Convert.ToDouble(customer.credit) * (100 - VAT) / 100);
            var currentBalance = card.Credit + Convert.ToDecimal(credited);

            card.Credit = currentBalance;
            //Update Card with Credit
            var updateTask = _dbContext.Set <CcCard>().Update(card);

            if (updateTask.State == EntityState.Modified)
            {
                var logRefill = new CcLogrefill()
                {
                    CardId       = card.Id,
                    Date         = DateTime.Now,
                    Description  = customer.description,
                    Credit       = Convert.ToDecimal(credited),
                    RefillType   = 1,
                    AddedInvoice = 1,
                    AgentId      = null
                };
                var logRefillRes = await _dbContext.Set <CcLogrefill>().AddAsync(logRefill);

                await _dbContext.SaveChangesAsync(cancellationToken);

                var res = new Dictionary <string, object>();
                res["card_id"]            = card.Id;
                res["credit_without_vat"] = customer.credit;
                res["credited"]           = credited;
                res["current_balance"]    = currentBalance;
                res["logrefill_id"]       = logRefillRes.Entity.Id;
                res["vat"] = card.Vat;

                return(Ok(new BaseResponseModel <IDictionary <string, object> > {
                    message = "Refill Successful", data = res, code = (int)HttpStatusCode.OK
                }));
            }
            return(BadRequest(new BaseResponseModel <object> {
                success = false, message = "Cannot Update Card", code = 400
            }));
        }
        public async Task <ActionResult> UseVoucher([FromBody] VoucherModel voucherModel, CancellationToken cancellationToken)
        {
            var voucher = _dbContext.Set <CcVoucher>().FirstOrDefault(p => p.Voucher == voucherModel.voucherCode && p.Activated == "t");

            if (voucher == null)
            {
                return(NotFound(new BaseResponseModel <object> {
                    message = "Voucher Not found!", code = 404
                }));
            }
            if (voucher.Expirationdate < DateTime.UtcNow.AddHours(7))
            {
                return(NotFound(new BaseResponseModel <object> {
                    message = "Voucher is Expired!", code = 404
                }));
            }
            var card = _dbContext.Set <CcCard>().FirstOrDefault(p => p.Id == voucherModel.cardId);

            if (card == null /*|| card.Activated != "t"*/)
            {
                return(NotFound(new BaseResponseModel <object> {
                    message = "Card Not found", code = 404
                }));
            }

            //Update Card Credit
            var currentBalance = card.Credit + Convert.ToDecimal(voucher.Credit);

            card.Credit = currentBalance;
            var updateTask = _dbContext.Set <CcCard>().Update(card);

            if (updateTask.State != EntityState.Modified)
            {
                return(BadRequest(new BaseResponseModel <object> {
                    message = "Use Voucher Fail", code = 400
                }));
            }

            //Update Voucher State
            voucher.Usedate        = DateTime.Now;
            voucher.Usedcardnumber = card.Username;
            voucher.Used           = 1;
            voucher.Activated      = "f";
            var updateVoucherTask = _dbContext.Set <CcVoucher>().Update(voucher);

            if (updateVoucherTask.State != EntityState.Modified)
            {
                return(BadRequest(new BaseResponseModel <object> {
                    message = "Use Voucher Fail", code = 400
                }));
            }

            //Create Log Refill
            var logRefill = new CcLogrefill()
            {
                CardId       = card.Id,
                Date         = DateTime.Now,
                Description  = card.Username + " use voucher",
                Credit       = Convert.ToDecimal(voucher.Credit),
                RefillType   = 1,
                AddedInvoice = 1,
                AgentId      = null
            };
            var addLogTask = await _dbContext.Set <CcLogrefill>().AddAsync(logRefill);

            if (addLogTask.State != EntityState.Added)
            {
                return(BadRequest(new BaseResponseModel <object> {
                    message = "Use Voucher Fail", code = 400
                }));
            }

            //Commit
            await _dbContext.SaveChangesAsync(cancellationToken);

            return(Ok(new BaseResponseModel <object> {
                message = "Refill Successful", code = (int)HttpStatusCode.OK
            }));
        }