Exemplo n.º 1
0
        public async Task <bool> consumerRedeem(int productID, int userID, int amount)
        {
            string productName = await prodIDToProdName(productID);

            MConsumerController consumerCtrl = new MConsumerController(userID);

            consumerCtrl = await consumerCtrl.init();

            await consumerCtrl.redeemVoucher(productName, Decimal.ToInt32(amount));

            return(true);
        }
        public async Task <IHttpActionResult> redeemProduct(int userID, int productID, int numberUnits, DateTime startdate)
        {
            //check that the minimum number of units is applied according to what is in db:
            if (!isValidNumUnits(productID, numberUnits))
            {
                return(BadRequest("The minimum number of units constraint has not been met"));
            }


            //calculate price of products for the specified number of days/units
            //"ProductValue" field in activeProduct Items
            decimal prodTotalPrice = calcProductPrice(productID, numberUnits);

            //calculate totalVoucherValues for this user
            decimal totalVoucherValues = calcTotalVoucherValues(userID);

            //get the user's voucherList in ascending order
            List <voucher> vouchersList = new List <voucher>();

            vouchersList = getUserVouchersList(userID);

            //make a decimal to deduct
            decimal amountToDeduct = prodTotalPrice;

            //check that the user has sufficient vouchers: that prodTotalPrice <= totalVoucherValues
            if (prodTotalPrice <= totalVoucherValues)//can proceed with redeem process
            {
                //Add activeProductItem to db table
                var               consumerID     = (from c in db.consumers where c.User_ID == userID select c).FirstOrDefault();
                string            prodUnitType   = getProductUnitType(productID);                              //get the string eg 'per day'
                DateTime          endDate        = getEndDateForProduct(prodUnitType, startdate, numberUnits); //calculate the end date
                activeproductitem activeProdItem = createActiveProductItem(consumerID.Consumer_ID, productID, "", true, prodTotalPrice, numberUnits, startdate, endDate);
                activeProdItem.transactionlocation = consumerID.Location_ID;
                db.activeproductitems.Add(activeProdItem);
                db.SaveChanges();


                //Add productProviderPayment-right now it is for 2 help 1: PP_ID 11
                productproviderpayment ppPaymentRec = new productproviderpayment();
                ppPaymentRec.ProductProvider_ID    = 11;
                ppPaymentRec.ActiveProductItems_ID = activeProdItem.ActiveProductItems_ID;
                ppPaymentRec.Description           = "Payment to Product Provider";
                ppPaymentRec.AmountToPay           = activeProdItem.productValue;
                ppPaymentRec.hasBeenPayed          = false; //initially false
                db.productproviderpayments.Add(ppPaymentRec);
                db.SaveChanges();


                //Update the 1 voucher table, 2 voucherTransaction table-not anymore and the 3 productRedemptionLog table
                for (int i = 0; (i < vouchersList.Count) && (amountToDeduct > 0); i++)
                {
                    decimal voucherVal = vouchersList.ElementAt(i).voucherValue;

                    if (amountToDeduct > vouchersList.ElementAt(i).voucherValue)
                    {
                        //will have to finish up this small voucher, it's value becomes 0
                        //this voucher is recorded individually in the productRedemptionLog

                        vouchersList.ElementAt(i).voucherValue    = 0;
                        db.Entry(vouchersList.ElementAt(i)).State = EntityState.Modified;
                        db.SaveChanges();

                        //addVoucherTransactionLog()
                        //addVoucherTransactionLog(vouchersList.ElementAt(i).Voucher_ID, vouchersList.ElementAt(i).Voucher_ID, userID, 0, 31, voucherVal, "Voucher Redemption for Product Purchase", System.DateTime.Now);

                        //addProductRedemptionLog()
                        addProductRedemptionLog(activeProdItem.ActiveProductItems_ID, vouchersList.ElementAt(i).Voucher_ID, voucherVal);

                        //update amount to deduct: becomes less by voucher val
                        amountToDeduct -= voucherVal;
                    }

                    if (amountToDeduct <= vouchersList.ElementAt(i).voucherValue)
                    {
                        //use a part of this voucher
                        vouchersList.ElementAt(i).voucherValue    = voucherVal - amountToDeduct;
                        db.Entry(vouchersList.ElementAt(i)).State = EntityState.Modified;
                        db.SaveChanges();

                        //add voucherTransactionLog()
                        //addVoucherTransactionLog(vouchersList.ElementAt(i).Voucher_ID, vouchersList.ElementAt(i).Voucher_ID, userID, 0, 31, amountToDeduct, "Voucher Redemption for Product Purchase", DateTime.Now);

                        //add productRedemptionLog
                        addProductRedemptionLog(activeProdItem.ActiveProductItems_ID, vouchersList.ElementAt(i).Voucher_ID, amountToDeduct);

                        //update amount to deduct
                        amountToDeduct = 0;
                    }
                }//for loop

                string productName = await prodIDToProdName(productID);

                MConsumerController consumerCtrl = new MConsumerController(userID);
                consumerCtrl = await consumerCtrl.init();

                await consumerCtrl.redeemVoucher(productName, Decimal.ToInt32(prodTotalPrice));

                return(Content(HttpStatusCode.OK, activeProdItem.ActiveProductItems_ID));
            }
            return(BadRequest("Insufficient voucher total"));
        }//RedeemProduct method