Exemplo n.º 1
0
        public async Task <ActionResult> MyBillingOverview()
        {
            if (UserContact != null && UserContact.IsInRole("GN_ADMIN"))
            {
                var remainingBudgets = await(new AccountService(db)).GetRemainingBudgetForAllAccounts();
            }



            auditResult = audit.LogEvent(UserContact, Guid.Empty, this.ENTITY, this.Request.UserHostAddress, "LOAD_MY_BILLING_OVERVIEW_UI");

            MyBillingCanAccess();

            MyBillingOverviewModel model = new MyBillingOverviewModel();

            InvoiceService invoiceService = new InvoiceService(this.db);
            AccountService accountService = new AccountService(this.db);

            model.MyAccount = GetMyAccount();

            model.MyCurrentInvoice = await invoiceService.GetInvoiceForCurrentMonth(UserContact);

            model.MyLastInvoice = invoiceService.GetInvoiceForLastMonth(UserContact);

            model.MyBalancePastDue  = accountService.GetBalancePastDue(UserContact, model.MyAccount, model.MyCurrentInvoice, model.MyLastInvoice);
            model.MaxApprovedSpend  = accountService.GetTotalApprovedToSpend(UserContact, model.MyAccount);
            model.CurrentMonthSpend = -1 * model.MyCurrentInvoice.Total;
            model.MyRemainingBudget = (double)ViewBag.MyRemainingBudget;

            if (model.MyCurrentInvoice != null)
            {
                model.MyCurrentInvoice.TotalAppliedToPOs = invoiceService.GetInvoiceTotalPOApplied(model.MyCurrentInvoice.Id);
                model.MyAccount.TotalAppliedToPOs        = model.MyCurrentInvoice.TotalAppliedToPOs;
            }

            if (model.MyLastInvoice != null)
            {
                model.MyLastInvoice.TotalAppliedToPOs = invoiceService.GetInvoiceTotalPOApplied(model.MyLastInvoice.Id);
            }

            //calc storage used
            model.MyStorageUsed = accountService.CalcStorageUsed(model.MyAccount.Id, DateTime.Now);

            //get storage carry over product info from DB
            GNProduct storageCarryOverProduct = this.db.GNProducts.Where(p => p.Name == "STORAGE_S3_CARRYOVER").FirstOrDefault();

            model.StorageUnits              = storageCarryOverProduct.ValueUnits;
            model.StoragePricePerUnit       = storageCarryOverProduct.Price;
            model.MyStorageTotalCurrentCost = model.MyStorageUsed * model.StoragePricePerUnit;

            return(View(model));
        }
        private async Task <int> ProcessStorageCarryOverFees()
        {
            int result = 1;

            if (DateTime.Now.Day == DAY_TO_PROCESS_STORAGE_CARRYOVER_FEES)
            {
                LogUtil.Info(logger, "ProcessStorageCarryOverFees()...");
                System.Console.WriteLine("\nProcessStorageCarryOverFees()...");

                var                  db                   = new GNEntityModelContainer();
                InvoiceService       invoiceService       = new InvoiceService(db);
                InvoiceDetailService invoiceDetailService = new InvoiceDetailService(db);
                TransactionService   transactionService   = new TransactionService(db);
                AccountService       orgAccountService    = new AccountService(db);

                foreach (var orgAccount in await orgAccountService.FindAll())
                {
                    try
                    {
                        LogUtil.Info(logger, "Account = " + orgAccount.Organization.Name);
                        System.Console.WriteLine("\nAccount = " + orgAccount.Organization.Name);

                        //get user contact
                        GNContact userContact = orgAccount.AccountOwner;
                        if (userContact == null)
                        {
                            userContact = db.GNContacts.Where(c => c.GNOrganizationId == orgAccount.Organization.Id).FirstOrDefault();
                        }

                        if (userContact != null)
                        {
                            //get last month invoice
                            GNInvoice lastMonthInvoice = invoiceService.GetInvoiceForLastMonth(userContact);

                            if (lastMonthInvoice != null)
                            {
                                string txnKey = "STORAGE_S3_CARRYOVER";

                                //get invoice to update
                                GNInvoice invoiceToUpdate = null;
                                if (lastMonthInvoice.Status != GNInvoice.InvoiceStatus.PAID.ToString() &&
                                    lastMonthInvoice.Status != GNInvoice.InvoiceStatus.VOID.ToString())
                                {
                                    invoiceToUpdate = lastMonthInvoice;
                                }
                                else
                                {
                                    invoiceToUpdate = await invoiceService.GetInvoiceForCurrentMonth(orgAccount.AccountOwner);
                                }

                                if (invoiceToUpdate != null)
                                {
                                    //get storage carryover detail
                                    GNInvoiceDetail storageCarryOverInvoiceDetail =
                                        db.GNInvoiceDetails
                                        .Where(invd => (invd.GNInvoiceId == invoiceToUpdate.Id && invd.Description == txnKey))
                                        .FirstOrDefault();

                                    //add invoice detail, if missing
                                    if (storageCarryOverInvoiceDetail == null)
                                    {
                                        storageCarryOverInvoiceDetail =
                                            db.GNInvoiceDetails.Add(new GNInvoiceDetail
                                        {
                                            Id             = Guid.NewGuid(),
                                            Description    = txnKey,
                                            GNInvoiceId    = invoiceToUpdate.Id,
                                            Quantity       = 0.0,
                                            SubTotal       = 0.0,
                                            DiscountAmount = orgAccount.DefaultDiscountAmount,
                                            DiscountType   = orgAccount.DefaultDiscountType,
                                            Total          = 0.0,
                                            UnitCost       = 0.0,
                                            UnitPrice      = 0.0,
                                            CreateDateTime = DateTime.Now,
                                            CreatedBy      = orgAccount.AccountOwner.Id
                                        });

                                        await db.SaveChangesAsync();

                                        storageCarryOverInvoiceDetail = db.GNInvoiceDetails.Find(storageCarryOverInvoiceDetail.Id);
                                    }

                                    if (storageCarryOverInvoiceDetail != null)
                                    {
                                        //get txn count
                                        int txnCount =
                                            db.GNTransactions.Count(t =>
                                                                    (t.GNInvoiceDetailId == storageCarryOverInvoiceDetail.Id &&
                                                                     t.TransactionType.Name == txnKey));

                                        if (txnCount == 0)
                                        {
                                            //get storage balance for month
                                            //all uploads minus all downloads up until end of last month
                                            var storageUsed = orgAccountService.CalcStorageUsed(orgAccount.Id, lastMonthInvoice.InvoiceEndDate.AddDays(1));

                                            //get storage carryover product
                                            GNProduct storageCarryOverProduct = db.GNProducts.Where(p => p.Name == txnKey).FirstOrDefault();

                                            //calc storage carryover cost
                                            var totalCarryOverCost = storageUsed * storageCarryOverProduct.Price;

                                            //add storage cost transaction
                                            string txnTypeKey  = txnKey;
                                            string description = storageUsed + "GB";
                                            double valueUsed   = storageUsed;
                                            string valueUnits  = "GB";

                                            int updateResult = 0;

                                            GNTransaction txn =
                                                await transactionService.CreateTransaction(
                                                    userContact, txnTypeKey, description, valueUsed, valueUnits,
                                                    targetInvoice : invoiceToUpdate);

                                            if (txn != null)
                                            {
                                                updateResult = 1;
                                            }
                                            //update invoice totals
                                            //int updateResult = await invoiceDetailService.UpdateInvoiceDetailTotals(
                                            //    invDetailToUpdate.Id, invDetailToUpdate.GNInvoiceId, invDetailToUpdate.Invoice.GNAccountId);

                                            LogUtil.Info(logger, "Update result = " + updateResult);
                                            System.Console.WriteLine("\nUpdate result = " + updateResult);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        result = 0;
                        LogUtil.Warn(logger, ex.Message, ex);
                        System.Console.WriteLine(ex.Message);
                    }
                }
            }

            return(result);
        }