public async Task <DTOproductproviderpayment> Postproductproviderpayment(DTOproductproviderpayment newDTO)
        {
            productproviderpayment newProd = EntityMapper.updateEntity(null, newDTO);

            db.productproviderpayments.Add(newProd);
            await db.SaveChangesAsync();

            return(newDTO);
        }
        public async Task <IHttpActionResult> Putproductproviderpayment(int ID, DTOproductproviderpayment editedDTO)
        {
            productproviderpayment toUpdate = db.productproviderpayments.Find(ID);

            toUpdate = EntityMapper.updateEntity(toUpdate, editedDTO);
            db.Entry(toUpdate).State = EntityState.Modified;
            await db.SaveChangesAsync();

            return(StatusCode(HttpStatusCode.NoContent));
        }
Exemple #3
0
        public DTOProductProviderAgregatePaymentInformation(int userID)
        {
            //user tmpUser = (from l in db.users where l.User_ID == userID select l).SingleOrDefault();
            user                      tmpUser            = db.users.Find(userID);
            productprovider           tmpProductProvider = (from l in db.productproviders where l.User_ID == userID select l).SingleOrDefault();
            productproviderpayment    Latestpayment      = (from a in db.productproviderpayments where a.ProductProvider_ID == tmpProductProvider.ProductProvider_ID orderby a.DatePayed descending select a).First();
            ProductProviderController ctrl = new ProductProviderController();

            companyName     = tmpProductProvider.ppCompanyName;
            cellPhoneNumber = tmpUser.userContactNumber;
            email           = tmpUser.userEmail;

            //totalCashedOwed = (int) ctrl.getTotalOwedToPP(tmpProductProvider.ProductProvider_ID);

            lastPaymentMade = Latestpayment.DatePayed;
        }
Exemple #4
0
        public static productproviderpayment updateEntity(productproviderpayment entityObjct, DTOproductproviderpayment dto)
        {
            if (entityObjct == null)
            {
                entityObjct = new productproviderpayment();
            }

            entityObjct.Productproviderpayment_ID = dto.Productproviderpayment_ID;
            entityObjct.ProductProvider_ID        = dto.ProductProvider_ID;
            entityObjct.ActiveProductItems_ID     = dto.ActiveProductItems_ID;
            entityObjct.Description  = dto.Description;
            entityObjct.AmountToPay  = dto.AmountToPay;
            entityObjct.hasBeenPayed = dto.hasBeenPayed;

            return(entityObjct);
        }
Exemple #5
0
        public Nullable <decimal> getTotalOwedToPP(int productProviderID)
        {
            List <DTOproductproviderpayment> toReturn = new List <DTOproductproviderpayment>();
            List <productproviderpayment>    list     = (from l in db.productproviderpayments where l.ProductProvider_ID == productProviderID && l.hasBeenPayed == false select l).ToList();

            productproviderpayment prodProvider = new productproviderpayment();
            Nullable <decimal>     AmountToPay  = 0;

            foreach (productproviderpayment p in list)
            {
                AmountToPay += p.AmountToPay;
            }


            return(AmountToPay);

            //decimal totalAmount = 0;

            //List<productproviderpayment> paymentsNotMadeList = (from c in db.productproviderpayments where c.hasBeenPayed == false && c.ProductProvider_ID == productProviderID select c).ToList();


            //foreach (productproviderpayment p in paymentsNotMadeList)
            //{
            //    activeproductitem activeProdRelatedToPayment = (from a in db.activeproductitems where a.ActiveProductItems_ID == p.ActiveProductItems_ID select a).SingleOrDefault();
            //    if (activeProdRelatedToPayment.Accepted == true)//this product purchase has gone through so the IM should get paid
            //    {
            //        totalAmount += activeProdRelatedToPayment.productValue;
            //    }
            //    else // IM has not accepted/rejected the product.
            //         //Check if date has passed - if yes pay IM. This assumes the rule: consumer covered by Product Provider unless IM rejects
            //    {
            //        if (hasDateExpired(activeProdRelatedToPayment.activeProductItemEndDate) == true)
            //        {
            //            totalAmount += activeProdRelatedToPayment.productValue;
            //        }
            //    }

            //}

            //return totalAmount;
        }
        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