Ejemplo n.º 1
0
        /// <summary>
        /// 1. Updates the wallet balance of the supplier.
        /// 2. Creates a transaction entity associated with the supplier.
        /// </summary>
        /// <param name="db"></param>
        /// <returns>Retruns the newly creaated transaction with the customer included in it.</returns>
        public async Task <CustomerTransaction> CreateNewTransactionAsync(HyperStoreServiceContext db)
        {
            var walletSnapshot = await this.UpdateCustomerWalletBalanceAsync(db);

            if (walletSnapshot == null)
            {
                throw new Exception(String.Format("Customer with id {0} has null wallet balance", this.CustomerId));
            }
            var transaction = this.AddNewTransaction(db, (decimal)walletSnapshot);

            return(transaction);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a transaction entity associated with supplier.
        /// </summary>
        /// <param name="walletSnapshot"></param>
        /// <param name="db"></param>
        /// <returns>Retruns the newly creaated transaction with the customer included in it.</returns>
        private SupplierTransaction AddNewTransaction(HyperStoreServiceContext db, decimal walletSnapshot)
        {
            var transaction = new SupplierTransaction
            {
                SupplierTransactionId = Guid.NewGuid(),
                TransactionNo         = Utility.GenerateSupplierTransactionNo(),
                TransactionDate       = DateTime.Now,
                TransactionAmount     = (decimal)this.TransactionAmount,
                SupplierOrderNo       = this.Description,
                IsCredit       = (bool)this.IsCredit,
                SupplierId     = (Guid)this.SupplierId,
                WalletSnapshot = walletSnapshot
            };

            db.SupplierTransactions.Add(transaction);
            return(transaction);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 1. Updates the wallet balance of the supplier.
        /// 2. Creates a transaction entity associated with the supplier.
        /// </summary>
        /// <param name="db"></param>
        /// <returns>Retruns the newly creaated transaction with the customer included in it.</returns>
        public async Task <SupplierTransaction> CreateNewTransactionAsync(HyperStoreServiceContext db)
        {
            var walletSnapshot = await this.UpdateSupplierWalletBalanceAsync(db);

            if (walletSnapshot == null)
            {
                throw new Exception(String.Format("Supplier with id {0} ad null wallet balance", this.SupplierId));
            }
            var transaction = this.AddNewTransaction(db, (decimal)walletSnapshot);
            List <SupplierOrder> settleUpOrders;

            if (transaction.IsCredit == false)
            {
                settleUpOrders = SettleUpOrders(transaction, db);
            }
            return(transaction);
        }
Ejemplo n.º 4
0
        private List <SupplierOrder> SettleUpOrders(SupplierTransaction transaction, HyperStoreServiceContext db)
        {
            List <SupplierOrder> settleUpSupplierOrder = new List <SupplierOrder>();

            if (transaction.IsCredit)
            {
                throw new Exception(String.Format("While settling up the orders transaction {0} cannot be of type credit", transaction.SupplierTransactionId));
            }
            var partiallyPaidOrders = db.SupplierOrders.Where(so => so.SupplierId == transaction.SupplierId &&
                                                              so.BillAmount - so.SettledPayedAmount > 0)
                                      .OrderBy(wo => wo.OrderDate);
            var debitTransactionAmount = transaction.TransactionAmount;

            foreach (var partiallyPaidOrder in partiallyPaidOrders)
            {
                if (debitTransactionAmount <= 0)
                {
                    break;
                }
                var remainingAmount = partiallyPaidOrder.BillAmount - partiallyPaidOrder.SettledPayedAmount;
                if (remainingAmount < 0)
                {
                    throw new Exception(string.Format("Supplier OrderNo {0}, Amount remaining to be paid: {1} cannot be less than zero", partiallyPaidOrder.SupplierOrderNo, remainingAmount));
                }
                decimal payingAmountForOrder = Math.Min(remainingAmount, debitTransactionAmount);
                debitTransactionAmount -= payingAmountForOrder;
                var IsOrderSettleUp = SettleUpOrder(partiallyPaidOrder, payingAmountForOrder, db);
                settleUpSupplierOrder.Add(partiallyPaidOrder);
                db.SupplierOrderTransactions.Add(new SupplierOrderTransaction
                {
                    SupplierOrderTransactionId = Guid.NewGuid(),
                    SupplierOrderId            = partiallyPaidOrder.SupplierOrderId,
                    TransactionId     = transaction.SupplierTransactionId,
                    PaidAmount        = payingAmountForOrder,
                    IsPaymentComplete = IsOrderSettleUp
                });
            }
            return(settleUpSupplierOrder);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Updates the wallet balance of the customer.
        /// Positive Balance always means that the user owes the customer that much amount.
        /// </summary>
        /// <param name="db"></param>
        /// <returns>The wallet snapshot which was before this function updates it.</returns>
        private async Task <decimal?> UpdateCustomerWalletBalanceAsync(HyperStoreServiceContext db)
        {
            Guid    customerId        = (Guid)this.CustomerId;
            decimal transactionAmount = (decimal)this.TransactionAmount;
            var     customer          = await db.Customers.FindAsync(customerId);

            if (customer == null)
            {
                throw new Exception(String.Format("Customer with id {0} not found while updating its wallet balance", this.CustomerId));
            }
            var walletSnapshot = customer.WalletBalance;

            if (IsCredit == true)
            {
                customer.WalletBalance -= transactionAmount;
            }
            else
            {
                customer.WalletBalance += transactionAmount;
            }
            db.Entry(customer).State = EntityState.Modified;
            return(walletSnapshot);
        }
Ejemplo n.º 6
0
 private bool SettleUpOrder(SupplierOrder supplierOrder, decimal settleUpAmount, HyperStoreServiceContext db)
 {
     supplierOrder.SettledPayedAmount += settleUpAmount;
     db.Entry(supplierOrder).State     = EntityState.Modified;
     if (supplierOrder.SettledPayedAmount == supplierOrder.BillAmount)
     {
         return(true);
     }
     return(false);
 }