)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » HomeController (Ia.Statistics.Wa.Controllers)

Public general use code classes and xml files that we've compiled and used over the years:

    1: using Microsoft.AspNetCore.Mvc;
    2: using Ia.Statistics.Wa.Models;
    3: using System.Diagnostics;
    4: using Microsoft.AspNetCore.Mvc.Rendering;
    5: using System.Net;
    6:  
    7: namespace Ia.Statistics.Wa.Controllers
    8: {
    9:     ////////////////////////////////////////////////////////////////////////////
   10:  
   11:     /// <summary publish="true">
   12:     ///
   13:     /// </summary>
   14:     /// 
   15:     /// <remarks> 
   16:     /// Copyright � 2006-2025 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   17:     ///
   18:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   19:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   20:     ///
   21:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   22:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   23:     /// 
   24:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   25:     /// 
   26:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   27:     /// </remarks> 
   28:     public class HomeController : Controller
   29:     {
   30:         ////////////////////////////////////////////////////////////////////////////
   31:  
   32:         /// <summary>
   33:         ///
   34:         /// </summary>
   35:         [Route("/")]
   36:         public IActionResult Index()
   37:         {
   38:             var indexViewModel = new Ia.Statistics.Wa.Models.Home.IndexViewModel();
   39:  
   40:             //Ia.Statistics.Cl.Models.StatisticsDbContext.CreateTables();
   41:  
   42:             ViewModel_DataBind(ref indexViewModel);
   43:  
   44:             return View(indexViewModel);
   45:         }
   46:  
   47:         ////////////////////////////////////////////////////////////////////////////
   48:  
   49:         /// <summary>
   50:         ///
   51:         /// </summary>
   52:         [HttpPost]
   53:         public IActionResult Search(Ia.Statistics.Wa.Models.Home.IndexViewModel indexViewModel)
   54:         {
   55:             var input = indexViewModel.SearchTextInput;
   56:  
   57:             return Redirect("/search?searchText=" + WebUtility.UrlEncode(input));
   58:         }
   59:  
   60:         ////////////////////////////////////////////////////////////////////////////
   61:  
   62:         /// <summary>
   63:         ///
   64:         /// </summary>
   65:         [HttpPost]
   66:         [Route("/")]
   67:         public IActionResult Analyze(Ia.Statistics.Wa.Models.Home.IndexViewModel indexViewModel)
   68:         {
   69:             var siteId = int.Parse(indexViewModel.SelectedValue);
   70:  
   71:             var site = Ia.Statistics.Cl.Models.Site.SiteFromId(siteId);
   72:  
   73:             if (site != null)
   74:             {
   75:                 Ia.Statistics.Cl.Models.Default.Analyze(site.Id, out List<Ia.Statistics.Cl.Models.ProductPriceStockQuantitySold> productPriceStockQuantitySoldList, out DateTime? startDateTime, out DateTime? endDateTime);
   76:  
   77:                 //panel.Visible = true;
   78:  
   79:                 indexViewModel.SiteLabel = site.Name;
   80:                 indexViewModel.SiteLinkText = indexViewModel.SiteLink = site.BaseUrl;
   81:  
   82:                 indexViewModel.StatusLabel = ActiveInactive(Ia.Statistics.Cl.Models.Default.ProcessIsActiveWithinLast20MinutesBySiteId(site.Id));
   83:  
   84:                 var s = string.Empty;
   85:  
   86:                 s += "Start datetime: " + (startDateTime.HasValue ? startDateTime.Value.ToString("yyyy-MM-dd HH:mm") : string.Empty) + "\r\n";
   87:                 s += "End datetime: " + (endDateTime.HasValue ? endDateTime.Value.ToString("yyyy-MM-dd HH:mm") : string.Empty) + "\r\n";
   88:                 s += "Products: " + productPriceStockQuantitySoldList.Count + "\r\n";
   89:                 s += "Product stocks: " + productPriceStockQuantitySoldList.Where(u => u.Stock >= 0).Sum(u => u.Stock) + "\r\n";
   90:                 s += "Products out-of-stock: " + productPriceStockQuantitySoldList.Where(u => u.Stock == 0).Count() + "\r\n";
   91:                 s += "Products sold: " + productPriceStockQuantitySoldList.Where(u => u.QuantitySold > 0).Count() + "\r\n";
   92:                 s += "Products quantities sold: " + productPriceStockQuantitySoldList.Sum(u => u.QuantitySold) + "\r\n";
   93:  
   94:                 var differenceTimeSpan = endDateTime - startDateTime;
   95:                 var productPriceStockQuantitySoldListPriceSum = productPriceStockQuantitySoldList.Where(u => u.QuantitySold > 0).Sum(u => u.Price);
   96:  
   97:                 s += "Products quantities monetary " + site.Currency + " value: " + productPriceStockQuantitySoldListPriceSum + " " + site.Currency + "\r\n";
   98:  
   99:                 indexViewModel.Label = s;
  100:  
  101:                 var differenceTimeSpanTotalDays = (int)((differenceTimeSpan.HasValue ? differenceTimeSpan.Value.TotalDays : 0) > 1 ? (differenceTimeSpan.HasValue ? differenceTimeSpan.Value.TotalDays : 0) : 1);
  102:  
  103:                 indexViewModel.SalesRatelabel = "Sales rate: " + (int)(productPriceStockQuantitySoldListPriceSum / differenceTimeSpanTotalDays) + " " + site.Currency + "/day";
  104:  
  105:                 indexViewModel.Link = "/list?siteId=" + siteId;
  106:  
  107:                 indexViewModel.ProductPriceStockQuantitySoldList = (from p in productPriceStockQuantitySoldList
  108:                                                                     orderby p.QuantitySold descending
  109:                                                                     select p).Take(100).ToList();
  110:             }
  111:             else
  112:             {
  113:             }
  114:  
  115:             ViewModel_DataBind(ref indexViewModel);
  116:  
  117:             return View("Index", indexViewModel);
  118:         }
  119:  
  120:         ////////////////////////////////////////////////////////////////////////////
  121:  
  122:         /// <summary>
  123:         ///
  124:         /// </summary>
  125:         private static string ActiveInactive(bool isActive)
  126:         {
  127:             var state = isActive ? "<span style=\"color:Green\">Active</span>" : "<span style=\"color:Red\">Inactive</span>";
  128:  
  129:             return state;
  130:         }
  131:  
  132:         ////////////////////////////////////////////////////////////////////////////
  133:  
  134:         /// <summary>
  135:         ///
  136:         /// </summary>
  137:         private static void ViewModel_DataBind(ref Ia.Statistics.Wa.Models.Home.IndexViewModel indexViewModel)
  138:         {
  139:             var list = (from s in Ia.Statistics.Cl.Models.Site.SiteList
  140:                         select new { Value = s.Id, Text = s.Name + " (" + s.BaseUrl + ")" }).OrderBy(u => u.Text).ToList();
  141:  
  142:             indexViewModel.SiteSelectList = new SelectList(list, "Value", "Text", "30");
  143:         }
  144:  
  145:         ////////////////////////////////////////////////////////////////////////////
  146:  
  147:         /// <summary>
  148:         ///
  149:         /// </summary>
  150:         [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
  151:         public IActionResult Error()
  152:         {
  153:             return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
  154:         }
  155:  
  156:         ////////////////////////////////////////////////////////////////////////////
  157:         ////////////////////////////////////////////////////////////////////////////
  158:     }
  159:  
  160:     ////////////////////////////////////////////////////////////////////////////
  161:     ////////////////////////////////////////////////////////////////////////////
  162: }