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

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

    1: using System;
    2: using System.ComponentModel.DataAnnotations.Schema;
    3: using System.ComponentModel.DataAnnotations;
    4: using System.Collections.Generic;
    5: using System.Linq;
    6: using Microsoft.EntityFrameworkCore;
    7:  
    8: namespace Ia.Statistics.Cl.Models
    9: {
   10:     ////////////////////////////////////////////////////////////////////////////
   11:  
   12:     /// <summary publish="true">
   13:     ///
   14:     /// </summary>
   15:     /// 
   16:     /// <remarks> 
   17:     /// Copyright © 2006-2025 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   18:     ///
   19:     /// 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
   20:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   21:     ///
   22:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   23:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   24:     /// 
   25:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   26:     /// 
   27:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   28:     /// </remarks> 
   29:     public class ProductStockSpot
   30:     {
   31:         private static List<Ia.Statistics.Cl.Models.ProductStockSpot> productStockSpotList;
   32:         private static Dictionary<int, Dictionary<string, List<int>>> siteIdToProductIdToStockListDictionaryDictionary;
   33:  
   34:         private static readonly object objectLock = new object();
   35:  
   36:         /// <summary/>
   37:         [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
   38:         public string Id { get; set; }
   39:  
   40:         /// <summary/>
   41:         public int Stock { get; set; }
   42:  
   43:         /// <summary/>
   44:         public DateTime Created { get; set; }
   45:  
   46:         /// <summary/>
   47:         [ForeignKey("Product_Id")]
   48:         public virtual Product Product { get; set; }
   49:  
   50:         ////////////////////////////////////////////////////////////////////////////
   51:  
   52:         /// <summary>
   53:         ///
   54:         /// </summary>
   55:         public static string ProductStockSpotId(Ia.Statistics.Cl.Models.Product product)
   56:         {
   57:             string id;
   58:  
   59:             if (product != null)
   60:             {
   61:                 id = product.Id + ":" + DateTime.UtcNow.AddHours(3).ToString("yyyyMMddHHmmss");
   62:             }
   63:             else id = string.Empty;
   64:  
   65:             return id;
   66:         }
   67:  
   68:         ////////////////////////////////////////////////////////////////////////////
   69:  
   70:         /// <summary>
   71:         ///
   72:         /// </summary>
   73:         public static List<Ia.Statistics.Cl.Models.ProductStockSpot> List
   74:         {
   75:             get
   76:             {
   77:                 if (productStockSpotList == null || productStockSpotList.Count == 0)
   78:                 {
   79:                     lock (objectLock)
   80:                     {
   81:                         using (var db = new Ia.Statistics.Cl.Models.StatisticsDbContext())
   82:                         {
   83:                             productStockSpotList = (from pss in db.ProductStockSpots
   84:                                                     select pss).Include(p => p.Product).AsNoTracking().ToList();
   85:                         }
   86:                     }
   87:                 }
   88:  
   89:                 return productStockSpotList;
   90:             }
   91:         }
   92:  
   93:         ////////////////////////////////////////////////////////////////////////////
   94:  
   95:         /// <summary>
   96:         ///
   97:         /// </summary>
   98:         public static List<Ia.Statistics.Cl.Models.ProductStockSpot> ListByProductId(string productId)
   99:         {
  100:             using (var db = new Ia.Statistics.Cl.Models.StatisticsDbContext())
  101:             {
  102:                 var list = (from pss in db.ProductStockSpots
  103:                             where pss.Product.Id == productId
  104:                             select pss).AsNoTracking().ToList();
  105:  
  106:                 return list;
  107:             }
  108:  
  109:         }
  110:  
  111:         ////////////////////////////////////////////////////////////////////////////
  112:  
  113:         /// <summary>
  114:         ///
  115:         /// </summary>
  116:         public static Dictionary<string, List<int>> ProductIdToStockListDictionaryBySiteId(int siteId)
  117:         {
  118:             if (siteIdToProductIdToStockListDictionaryDictionary == null || siteIdToProductIdToStockListDictionaryDictionary.Count == 0)
  119:             {
  120:                 siteIdToProductIdToStockListDictionaryDictionary = new Dictionary<int, Dictionary<string, List<int>>>();
  121:             }
  122:  
  123:             if (!siteIdToProductIdToStockListDictionaryDictionary.ContainsKey(siteId) || siteIdToProductIdToStockListDictionaryDictionary[siteId] == null)
  124:             {
  125:                 siteIdToProductIdToStockListDictionaryDictionary[siteId] = new Dictionary<string, List<int>>();
  126:  
  127:                 lock (objectLock)
  128:                 {
  129:                     var siteShortName = Ia.Statistics.Cl.Models.Site.SiteShortNameFromId(siteId);
  130:  
  131:                     using (var db = new Ia.Statistics.Cl.Models.StatisticsDbContext())
  132:                     {
  133:                         var productIdStockForSiteList = (from pss in db.ProductStockSpots
  134:                                                          where pss.Id.StartsWith(siteShortName)
  135:                                                          orderby pss.Created ascending
  136:                                                          select new
  137:                                                          {
  138:                                                              ProductId = pss.Product.Id,
  139:                                                              pss.Stock,
  140:                                                          }).ToList();
  141:  
  142:                         foreach (var productIdStock in productIdStockForSiteList)
  143:                         {
  144:                             if (!siteIdToProductIdToStockListDictionaryDictionary[siteId].ContainsKey(productIdStock.ProductId))
  145:                             {
  146:                                 siteIdToProductIdToStockListDictionaryDictionary[siteId][productIdStock.ProductId] = new List<int>();
  147:                             }
  148:  
  149:                             siteIdToProductIdToStockListDictionaryDictionary[siteId][productIdStock.ProductId].Add(productIdStock.Stock);
  150:                         }
  151:                     }
  152:                 }
  153:             }
  154:  
  155:             return siteIdToProductIdToStockListDictionaryDictionary[siteId];
  156:         }
  157:  
  158:         ////////////////////////////////////////////////////////////////////////////
  159:  
  160:         /// <summary>
  161:         ///
  162:         /// </summary>
  163:         public static void EarliestAndLatestRecordDateTimeBySiteId(int siteId, out DateTime? earliestRecordDateTime, out DateTime? latestRecordDateTime)
  164:         {
  165:             var siteShortName = Ia.Statistics.Cl.Models.Site.SiteShortNameFromId(siteId);
  166:  
  167:             using (var db = new Ia.Statistics.Cl.Models.StatisticsDbContext())
  168:             {
  169:                 var list = (from pss in db.ProductStockSpots
  170:                             where pss.Id.StartsWith(siteShortName)
  171:                             select pss);
  172:  
  173:                 earliestRecordDateTime = list.Count() > 0 ? list.Min(u => u.Created) : null;
  174:  
  175:  
  176:                 list = (from pss in db.ProductStockSpots
  177:                         where pss.Id.StartsWith(siteShortName)
  178:                         select pss);
  179:  
  180:                 latestRecordDateTime = list.Count() > 0 ? list.Max(u => u.Created) : null;
  181:             }
  182:         }
  183:  
  184:         ////////////////////////////////////////////////////////////////////////////
  185:  
  186:         /// <summary>
  187:         ///
  188:         /// </summary>
  189:         public static void Create(Ia.Statistics.Cl.Models.Product product, int stock)
  190:         {
  191:             var productStockSpot = new Ia.Statistics.Cl.Models.ProductStockSpot();
  192:  
  193:             using (var db = new Ia.Statistics.Cl.Models.StatisticsDbContext())
  194:             {
  195:                 productStockSpot.Id = Ia.Statistics.Cl.Models.ProductStockSpot.ProductStockSpotId(product);
  196:                 productStockSpot.Stock = stock;
  197:                 productStockSpot.Product = (from p in db.Products where p.Id == product.Id select p).SingleOrDefault();
  198:                 productStockSpot.Created = DateTime.UtcNow.AddHours(3);
  199:  
  200:                 db.ProductStockSpots.Add(productStockSpot);
  201:                 db.SaveChanges();
  202:             }
  203:         }
  204:  
  205:         ////////////////////////////////////////////////////////////////////////////
  206:  
  207:         /// <summary>
  208:         ///
  209:         /// </summary>
  210:         public static void Create(Ia.Statistics.Cl.Models.ProductStockSpot productStockSpot)
  211:         {
  212:             using (var db = new Ia.Statistics.Cl.Models.StatisticsDbContext())
  213:             {
  214:                 db.ProductStockSpots.Add(productStockSpot);
  215:                 db.SaveChanges();
  216:             }
  217:         }
  218:  
  219:         ////////////////////////////////////////////////////////////////////////////
  220:         ////////////////////////////////////////////////////////////////////////////
  221:     }
  222:  
  223:     ////////////////////////////////////////////////////////////////////////////
  224:     ////////////////////////////////////////////////////////////////////////////
  225: }