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

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

    1: using System.Collections.Generic;
    2: using System.Linq;
    3: using Ia.Cl.Models;
    4: using Microsoft.EntityFrameworkCore;
    5:  
    6: namespace Ia.Statistics.Cl.Models
    7: {
    8:     ////////////////////////////////////////////////////////////////////////////
    9:  
   10:     /// <summary publish="true">
   11:     ///
   12:     /// </summary>
   13:     /// 
   14:     /// <remarks> 
   15:     /// Copyright © 2006-2025 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   16:     ///
   17:     /// 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
   18:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   19:     ///
   20:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   21:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   22:     /// 
   23:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   24:     /// 
   25:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   26:     /// </remarks> 
   27:     public class ProductPriceStockQuantitySold
   28:     {
   29:         private static List<Ia.Statistics.Cl.Models.ProductPriceStockQuantitySold> list;
   30:  
   31:         private static readonly object objectLock = new object();
   32:  
   33:         /// <summary/>
   34:         public Ia.Statistics.Cl.Models.Product Product { get; set; }
   35:  
   36:         /// <summary/>
   37:         public decimal Price { get; set; }
   38:  
   39:         /// <summary/>
   40:         public int Stock { get; set; }
   41:  
   42:         /// <summary/>
   43:         public int QuantitySold { get; set; }
   44:  
   45:         ////////////////////////////////////////////////////////////////////////////
   46:  
   47:         /// <summary>
   48:         ///
   49:         /// </summary>
   50:         public static List<Ia.Statistics.Cl.Models.ProductPriceStockQuantitySold> ListBySiteId(int siteId)
   51:         {
   52:             int lastStock, quantitySold, lastStockIteration;
   53:             decimal lastPrice;
   54:  
   55:             var productListBySiteId = Ia.Statistics.Cl.Models.Product.ListBySiteId(siteId);
   56:  
   57:             var productIdToPriceListDictionary = Ia.Statistics.Cl.Models.ProductPriceSpot.ProductIdToPriceListDictionaryBySiteId(siteId);
   58:  
   59:             var productIdToStockListDictionary = Ia.Statistics.Cl.Models.ProductStockSpot.ProductIdToStockListDictionaryBySiteId(siteId);
   60:  
   61:             var productPriceStockQuantitySoldList = new List<Ia.Statistics.Cl.Models.ProductPriceStockQuantitySold>();
   62:  
   63:             foreach (var product in productListBySiteId)
   64:             {
   65:                 quantitySold = lastStockIteration = 0;
   66:  
   67:                 if (productIdToStockListDictionary.ContainsKey(product.Id))
   68:                 {
   69:                     foreach (var i in productIdToStockListDictionary[product.Id])
   70:                     {
   71:                         if (i >= 0)
   72:                         {
   73:                             if (i < lastStockIteration) quantitySold += (lastStockIteration - i);
   74:                             else if (i >= lastStockIteration) { }
   75:                         }
   76:  
   77:                         lastStockIteration = i;
   78:                     }
   79:  
   80:                     lastStock = productIdToStockListDictionary[product.Id].LastOrDefault();
   81:                     lastPrice = productIdToPriceListDictionary[product.Id].LastOrDefault();
   82:  
   83:                     productPriceStockQuantitySoldList.Add(new ProductPriceStockQuantitySold
   84:                     {
   85:                         Product = product,
   86:                         Price = lastPrice,
   87:                         Stock = lastStock,
   88:                         QuantitySold = quantitySold,
   89:                     }
   90:                     );
   91:                 }
   92:                 else
   93:                 {
   94:                 }
   95:             }
   96:  
   97:             return productPriceStockQuantitySoldList;
   98:         }
   99:  
  100:         ////////////////////////////////////////////////////////////////////////////
  101:  
  102:         /// <summary>
  103:         ///
  104:         /// </summary>
  105:         public static List<Ia.Statistics.Cl.Models.ProductPriceStockQuantitySold> List
  106:         {
  107:             get
  108:             {
  109:                 if (list == null || list.Count == 0)
  110:                 {
  111:                     lock (objectLock)
  112:                     {
  113:                         using (var db = new Ia.Statistics.Cl.Models.StatisticsDbContext())
  114:                         {
  115:                             list = (from p in db.Products
  116:                                     join pps in db.ProductPriceSpots on p.Id equals pps.Product.Id
  117:                                     join pss in db.ProductStockSpots on p.Id equals pss.Product.Id
  118:                                     orderby p.Created
  119:                                     select new Ia.Statistics.Cl.Models.ProductPriceStockQuantitySold()
  120:                                     {
  121:                                         Product = p,
  122:                                         Price = pps.Price,
  123:                                         Stock = pss.Stock
  124:                                     }).AsNoTracking().ToList();
  125:                         }
  126:                     }
  127:                 }
  128:  
  129:                 return list;
  130:             }
  131:         }
  132:  
  133:         ////////////////////////////////////////////////////////////////////////////
  134:  
  135:         /// <summary>
  136:         ///
  137:         /// </summary>
  138:         public static List<Ia.Statistics.Cl.Models.ProductPriceStockQuantitySold> Search(string input, out Ia.Cl.Models.Result result)
  139:         {
  140:             var maxTake = 100;
  141:  
  142:             var list = new List<ProductPriceStockQuantitySold>();
  143:  
  144:             result = new Ia.Cl.Models.Result();
  145:  
  146:             if (!string.IsNullOrEmpty(input))
  147:             {
  148:                 using (var db = new Ia.Statistics.Cl.Models.StatisticsDbContext())
  149:                 {
  150:                     var productList = (from p in db.Products where p.Name.Contains(input) select p).Take(maxTake).ToList();
  151:  
  152:                     var productIdList = (from p in productList select p.Id).ToList();
  153:  
  154:                     var productPriceSpotList = (from pps in db.ProductPriceSpots
  155:                                                 where productIdList.Contains(pps.Product.Id)
  156:                                                 select pps).Include(u => u.Product).ToList();
  157:  
  158:                     var productStockSpotList = (from pss in db.ProductStockSpots
  159:                                                 where productIdList.Contains(pss.Product.Id)
  160:                                                 select pss).Include(u => u.Product).ToList();
  161:  
  162:                     // productList ProductPriceSpots and ProductStockSpots are magically filled!
  163:  
  164:                     list = (from p in productList
  165:                             orderby p.Name
  166:                             select new Ia.Statistics.Cl.Models.ProductPriceStockQuantitySold()
  167:                             {
  168:                                 Product = p,
  169:                                 Price = p.ProductPriceSpots.OrderByDescending(u => u.Created).FirstOrDefault().Price,
  170:                                 Stock = p.ProductStockSpots.OrderByDescending(u => u.Created).FirstOrDefault().Stock,
  171:                             }).ToList();
  172:                 }
  173:             }
  174:             else
  175:             {
  176:                 result.AddError("Input string is empty.");
  177:             }
  178:  
  179:             return list;
  180:         }
  181:  
  182:         ////////////////////////////////////////////////////////////////////////////
  183:         ////////////////////////////////////////////////////////////////////////////
  184:     }
  185: }