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

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

Service Request support class for Fixed Telecommunications Network (FTN) data model.

    1: using Microsoft.EntityFrameworkCore;
    2: using System;
    3: using System.Collections;
    4: using System.Collections.Generic;
    5: using System.Data;
    6: using System.IO;
    7: using System.Linq;
    8: using System.Reflection;
    9: using System.Text;
   10: using System.Text.RegularExpressions;
   11: using System.Xml.Linq;
   12: using Ia.Ftn.Cl.Models.Business; // needed for extension
   13:  
   14: namespace Ia.Ftn.Cl.Models.Data
   15: {
   16:     ////////////////////////////////////////////////////////////////////////////
   17:  
   18:     /// <summary publish="true">
   19:     /// Service Request support class for Fixed Telecommunications Network (FTN) data model.
   20:     /// </summary>
   21:     /// 
   22:     /// <remarks> 
   23:     /// Copyright © 2006-2022 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   24:     ///
   25:     /// 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
   26:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   27:     ///
   28:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   29:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   30:     /// 
   31:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   32:     /// 
   33:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   34:     /// </remarks> 
   35:     public class ServiceRequest
   36:     {
   37:         private static int serviceRequestIdStartEndRangeBufferedListIndex, serviceRequestIdLatestStartEndRangeBufferedListIndex, serviceRequestIdLatestStartEndRangeBufferedListCount = 0;
   38:         private static SortedList serviceCategorySortedList, serviceSortedList, customerCategorySortedList, statusSortedList;
   39:         private static XDocument xDocument;
   40:         private static List<int> serviceIdAllowedForProcessingList, serviceRequestDomainList;
   41:         private static Dictionary<int, string> systemCode;
   42:         private static List<Tuple<int, int>> serviceRequestIdStartEndRangeBufferedTupleList;
   43:         private static Dictionary<string, string> serviceToDbNameDictionary, serviceToCustomerAddressDictionary;
   44:         private static Dictionary<int, List<int>> changedServiceNumberToChangedToServiceNumberListDictionary;
   45:         private static Dictionary<int, List<int>> pbxServiceKeyToPbxServiceBranchListDictionary;
   46:         private static Dictionary<string, List<string>> changedAndChangedToServiceToRelatedChangedAndChangedToServiceListDictionary;
   47:         private static DateTime serviceToDbNameDictionaryAndServiceToCustomerAddressDictionaryTimestamp;
   48:         private static Dictionary<string, List<string>> dbNameToServiceListDictionary, accessNameToServiceListWithinAllowedToBeMigratedOltDictionary;
   49:         private static DateTime dbNameToServiceListDictionaryTimestamp, accessNameToServiceListWithinAllowedToBeMigratedOltDictionaryTimestamp;
   50:         private static Dictionary<string, string> serviceToAccessNameWithinAllowedToBeMigratedOltDictionary;
   51:  
   52:         private static readonly object objectLock = new object();
   53:  
   54:         ////////////////////////////////////////////////////////////////////////////
   55:  
   56:         /// <summary>
   57:         ///
   58:         /// </summary>
   59:         public ServiceRequest() { }
   60:  
   61:         ////////////////////////////////////////////////////////////////////////////
   62:  
   63:         /// <summary>
   64:         ///
   65:         /// </summary>
   66:         public static Dictionary<int, string> SystemCode
   67:         {
   68:             get
   69:             {
   70:                 if (systemCode == null || systemCode.Count == 0)
   71:                 {
   72:                     lock (objectLock)
   73:                     {
   74:                         systemCode = Ia.Ftn.Cl.Models.Data.ServiceRequest._SystemCode;
   75:                     }
   76:                 }
   77:  
   78:                 return systemCode;
   79:             }
   80:         }
   81:  
   82:         ////////////////////////////////////////////////////////////////////////////
   83:  
   84:         /// <summary>
   85:         ///
   86:         /// </summary>
   87:         private static Dictionary<int, string> _SystemCode
   88:         {
   89:             get
   90:             {
   91:                 // select code_id, code_name from system_codes
   92:  
   93:                 systemCode = new Dictionary<int, string>(100)
   94:                 {
   95:                     [6008] = "قطع مطالبات",
   96:                     [6009] = "قطع إدارى",
   97:                     [4007] = "إفراج",
   98:                     [4008] = "قيد التجديد",
   99:                     [4009] = "قيد إخطار الجهة",
  100:                     [4010] = "تسيل جزئي",
  101:                     [1001] = "جديد",
  102:                     [1002] = "مطلوب",
  103:                     [1003] = "متاح",
  104:                     [2001] = "قيد الموافقة الفنية",
  105:                     [2002] = "قيد الدفع",
  106:                     [2003] = "قيد التنفيذ",
  107:                     [2004] = "تعذر التنفيذ",
  108:                     [2005] = "تم التنفيذ",
  109:                     [3001] = "نقل من رصيد خدمة - دائن",
  110:                     [3002] = "نقل من رصيد خدمة - مدين",
  111:                     [4001] = "سارية",
  112:                     [4002] = "منتهية",
  113:                     [4003] = "مستردة",
  114:                     [4004] = "تسييل",
  115:                     [5001] = "سارية",
  116:                     [5002] = "منتهية",
  117:                     [5003] = "ملغاة",
  118:                     [4005] = "ملغاة",
  119:                     [5004] = "مبدئية",
  120:                     [6001] = "قيد التنفيذ",
  121:                     [6002] = "تم التنفيذ",
  122:                     [6003] = "تعذر التنفيذ",
  123:                     [4006] = "جديدة",
  124:                     [7001] = "يعمل",
  125:                     [7002] = "قطع مؤقت",
  126:                     [7003] = "قطع مبرمج",
  127:                     [1004] = "مقطوع",
  128:                     [5005] = "متأخرة السداد",
  129:                     [7004] = "قيد التنفيذ",
  130:                     [8001] = "قيد التنفيذ",
  131:                     [8002] = "تم التنفيذ",
  132:                     [8003] = "تعذر التنفيذ",
  133:                     [1005] = "يعمل",
  134:                     [7005] = "تعذر التنفيذ",
  135:                     [9001] = "دائن",
  136:                     [9002] = "مدين",
  137:                     [7006] = "رفع نهائي",
  138:                     [1203] = "مبدئية",
  139:                     [1201] = "سارية",
  140:                     [1202] = "ملغاة",
  141:                     [1101] = "انهاء",
  142:                     [1102] = "قطع مؤقت",
  143:                     [1301] = "قطع مبرمج",
  144:                     [1302] = "أمر عمل",
  145:                     [7007] = "رفع محاسبة",
  146:                     [1401] = "نص",
  147:                     [1402] = "رقم",
  148:                     [1403] = "تاريخ",
  149:                     [1404] = "تاريخ/وقت",
  150:                     [7008] = "قطع معاكسة",
  151:                     [1501] = "خدمة أساسية",
  152:                     [1502] = "تشغيل خدمة فرعية",
  153:                     [1503] = "إيقاف خدمة فرعية",
  154:                     [1504] = "عملية",
  155:                     [1505] = "منتج",
  156:                     [1510] = "خدمات متنوعة",
  157:                     [1103] = "إعادة",
  158:                     [1506] = "خدمة اساسية - مجموعة",
  159:                     [1507] = "خدمة تابعة",
  160:                     [1508] = "خدمة مدمجة",
  161:                     [2101] = "جديد",
  162:                     [2102] = "تم الحساب",
  163:                     [2103] = "نسخة جديدة",
  164:                     [2201] = "ملف المكالمات الدولية",
  165:                     [6004] = "إعادة الحالة",
  166:                     [1601] = "مبدئية",
  167:                     [1602] = "نهائية",
  168:                     [1603] = "ملغاة",
  169:                     [2006] = "ملغاة",
  170:                     [7009] = "ملغى",
  171:                     [7010] = "لا يعمل",
  172:                     [2104] = "مفقود",
  173:                     [5006] = "سارية - مؤقتة",
  174:                     [5007] = "مرحلة",
  175:                     [1701] = "عادي",
  176:                     [1702] = "نطاق",
  177:                     [1509] = "إيقاف خدمة أساسية"
  178:                 };
  179:  
  180:                 return systemCode;
  181:             }
  182:         }
  183:  
  184:         ////////////////////////////////////////////////////////////////////////////
  185:  
  186:         /// <summary>
  187:         ///
  188:         /// </summary>
  189:         public static SortedList ServiceCategorySortedList
  190:         {
  191:             get
  192:             {
  193:                 if (serviceCategorySortedList == null || serviceCategorySortedList.Count == 0)
  194:                 {
  195:                     lock (objectLock)
  196:                     {
  197:                         serviceCategorySortedList = Ia.Ftn.Cl.Models.Data.ServiceRequest._ServiceCategorySortedList;
  198:                     }
  199:                 }
  200:  
  201:                 return serviceCategorySortedList;
  202:             }
  203:         }
  204:  
  205:         ////////////////////////////////////////////////////////////////////////////
  206:  
  207:         /// <summary>
  208:         ///
  209:         /// </summary>
  210:         private static SortedList _ServiceCategorySortedList
  211:         {
  212:             get
  213:             {
  214:                 int id;
  215:  
  216:                 serviceCategorySortedList = new SortedList(10);
  217:  
  218:                 foreach (XElement x in XDocument.Element("serviceRequest").Elements("service").Elements("categoryList").Elements("category"))
  219:                 {
  220:                     id = int.Parse(x.Attribute("id").Value);
  221:  
  222:                     serviceCategorySortedList[id] = x.Attribute("arabicName").Value;
  223:                 }
  224:  
  225:                 return serviceCategorySortedList;
  226:             }
  227:         }
  228:  
  229:         ////////////////////////////////////////////////////////////////////////////
  230:  
  231:         /// <summary>
  232:         ///
  233:         /// </summary>
  234:         public static SortedList ServiceSortedList
  235:         {
  236:             get
  237:             {
  238:                 if (serviceSortedList == null || serviceSortedList.Count == 0)
  239:                 {
  240:                     lock (objectLock)
  241:                     {
  242:                         serviceSortedList = Ia.Ftn.Cl.Models.Data.ServiceRequest._ServiceSortedList;
  243:                     }
  244:                 }
  245:  
  246:                 return serviceSortedList;
  247:             }
  248:         }
  249:  
  250:         ////////////////////////////////////////////////////////////////////////////
  251:  
  252:         /// <summary>
  253:         ///
  254:         /// </summary>
  255:         private static SortedList _ServiceSortedList
  256:         {
  257:             get
  258:             {
  259:                 int id;
  260:  
  261:                 serviceSortedList = new SortedList(10);
  262:  
  263:                 foreach (XElement x in XDocument.Element("serviceRequest").Elements("service").Elements("serviceList").Elements("service"))
  264:                 {
  265:                     id = int.Parse(x.Attribute("id").Value);
  266:  
  267:                     serviceSortedList[id] = x.Attribute("arabicName").Value;
  268:                 }
  269:  
  270:                 return serviceSortedList;
  271:             }
  272:         }
  273:  
  274:         ////////////////////////////////////////////////////////////////////////////
  275:  
  276:         /// <summary>
  277:         ///
  278:         /// </summary>
  279:         public static SortedList CustomerCategorySortedList
  280:         {
  281:             get
  282:             {
  283:                 if (customerCategorySortedList == null || customerCategorySortedList.Count == 0)
  284:                 {
  285:                     lock (objectLock)
  286:                     {
  287:                         customerCategorySortedList = Ia.Ftn.Cl.Models.Data.ServiceRequest._CustomerCategorySortedList;
  288:                     }
  289:                 }
  290:  
  291:                 return customerCategorySortedList;
  292:             }
  293:         }
  294:  
  295:         ////////////////////////////////////////////////////////////////////////////
  296:  
  297:         /// <summary>
  298:         ///
  299:         /// </summary>
  300:         private static SortedList _CustomerCategorySortedList
  301:         {
  302:             get
  303:             {
  304:                 int id;
  305:  
  306:                 customerCategorySortedList = new SortedList(10);
  307:  
  308:                 foreach (XElement x in XDocument.Element("serviceRequest").Elements("customer").Elements("categoryList").Elements("category"))
  309:                 {
  310:                     id = int.Parse(x.Attribute("id").Value);
  311:  
  312:                     customerCategorySortedList[id] = x.Attribute("arabicName").Value;
  313:                 }
  314:  
  315:                 return customerCategorySortedList;
  316:             }
  317:         }
  318:  
  319:         ////////////////////////////////////////////////////////////////////////////
  320:  
  321:         /// <summary>
  322:         ///
  323:         /// </summary>
  324:         public static SortedList StatusSortedList
  325:         {
  326:             get
  327:             {
  328:                 if (statusSortedList == null || statusSortedList.Count == 0)
  329:                 {
  330:                     lock (objectLock)
  331:                     {
  332:                         statusSortedList = Ia.Ftn.Cl.Models.Data.ServiceRequest._StatusSortedList;
  333:                     }
  334:                 }
  335:  
  336:                 return statusSortedList;
  337:             }
  338:         }
  339:  
  340:         ////////////////////////////////////////////////////////////////////////////
  341:  
  342:         /// <summary>
  343:         ///
  344:         /// </summary>
  345:         private static SortedList _StatusSortedList
  346:         {
  347:             get
  348:             {
  349:                 int id;
  350:  
  351:                 statusSortedList = new SortedList(10);
  352:  
  353:                 foreach (XElement x in XDocument.Element("serviceRequest").Elements("statusList").Elements("status"))
  354:                 {
  355:                     id = int.Parse(x.Attribute("id").Value);
  356:  
  357:                     statusSortedList[id] = x.Attribute("arabicName").Value;
  358:                 }
  359:  
  360:                 return statusSortedList;
  361:             }
  362:         }
  363:  
  364:         ////////////////////////////////////////////////////////////////////////////
  365:  
  366:         /// <summary>
  367:         ///
  368:         /// </summary>
  369:         public static List<int> ServiceIdAllowedForProcessingList
  370:         {
  371:             get
  372:             {
  373:                 if (serviceIdAllowedForProcessingList == null || serviceIdAllowedForProcessingList.Count == 0)
  374:                 {
  375:                     lock (objectLock)
  376:                     {
  377:                         serviceIdAllowedForProcessingList = Ia.Ftn.Cl.Models.Data.ServiceRequest._ServiceIdAllowedForProcessingList;
  378:                     }
  379:                 }
  380:  
  381:                 return serviceIdAllowedForProcessingList;
  382:             }
  383:         }
  384:  
  385:         ////////////////////////////////////////////////////////////////////////////
  386:  
  387:         /// <summary>
  388:         ///
  389:         /// </summary>
  390:         private static List<int> _ServiceIdAllowedForProcessingList
  391:         {
  392:             get
  393:             {
  394:                 int id;
  395:  
  396:                 serviceIdAllowedForProcessingList = new List<int>(100);
  397:  
  398:                 foreach (XElement x in XDocument.Element("serviceRequest").Elements("service").Elements("serviceList").Elements("service"))
  399:                 {
  400:                     if (x.HasAttributes && x.Attribute("allowProcessing") != null)
  401:                     {
  402:                         if (x.Attribute("allowProcessing").Value == "true")
  403:                         {
  404:                             id = int.Parse(x.Attribute("id").Value);
  405:                             serviceIdAllowedForProcessingList.Add(id);
  406:                         }
  407:                     }
  408:                 }
  409:  
  410:                 return serviceIdAllowedForProcessingList;
  411:             }
  412:         }
  413:  
  414:         ////////////////////////////////////////////////////////////////////////////    
  415:         ////////////////////////////////////////////////////////////////////////////    
  416:  
  417:         /// <summary>
  418:         ///
  419:         /// </summary>
  420:         public static Tuple<int, int> ServiceRequestIdStartEndRangeManager(ref int index, out string result)
  421:         {
  422:             int count, edgeBufferRange, latestFromNDaysBeforeNow;
  423:             Tuple<int, int> tuple;
  424:             List<int> serviceRequestIdList;
  425:  
  426:             count = 40;
  427:             edgeBufferRange = 400;
  428:             latestFromNDaysBeforeNow = 7;
  429:  
  430:             if (serviceRequestIdStartEndRangeBufferedTupleList == null || serviceRequestIdStartEndRangeBufferedListIndex == 0)
  431:             {
  432:                 serviceRequestIdStartEndRangeBufferedListIndex = index;
  433:  
  434:                 using (var db = new Ia.Ftn.Cl.Db())
  435:                 {
  436:                     serviceRequestIdList = (from sr in db.ServiceRequests
  437:                                             orderby sr.Id ascending
  438:                                             select sr.Id).ToList();
  439:                 }
  440:  
  441:                 serviceRequestIdStartEndRangeBufferedTupleList = Ia.Ftn.Cl.Models.Business.ServiceRequest.OptimizedStartEndRangeBufferedList(serviceRequestIdList, count, edgeBufferRange);
  442:  
  443:                 var firstServiceRequestIdAfterDateTime = Ia.Ftn.Cl.Models.Data.ServiceRequest.FirstIdAfterDateTime(DateTime.UtcNow.AddHours(3).AddDays(-latestFromNDaysBeforeNow));
  444:  
  445:                 serviceRequestIdLatestStartEndRangeBufferedListCount = 0;
  446:                 for (int i = serviceRequestIdStartEndRangeBufferedTupleList.Count - 1; i > 0; i--)
  447:                 {
  448:                     if (serviceRequestIdStartEndRangeBufferedTupleList[i].Item1 < firstServiceRequestIdAfterDateTime) break;
  449:  
  450:                     serviceRequestIdLatestStartEndRangeBufferedListCount++;
  451:                 }
  452:             }
  453:  
  454:             if (serviceRequestIdStartEndRangeBufferedTupleList.Count > 0)
  455:             {
  456:                 if (Ia.Ftn.Cl.Models.Business.Administration.NowIsOfficialWorkingTime)
  457:                 {
  458:                     // flip between last and latest range to favor reading fresh work orders quickly
  459:                     if (Ia.Cl.Models.Default.RandomBool)
  460:                     {
  461:                         serviceRequestIdLatestStartEndRangeBufferedListIndex = serviceRequestIdStartEndRangeBufferedTupleList.Count - Ia.Cl.Models.Default.Random(serviceRequestIdLatestStartEndRangeBufferedListCount) - 1;
  462:                         serviceRequestIdLatestStartEndRangeBufferedListIndex = (serviceRequestIdLatestStartEndRangeBufferedListIndex >= 0) ? serviceRequestIdLatestStartEndRangeBufferedListIndex : 0;
  463:                     }
  464:                     else serviceRequestIdLatestStartEndRangeBufferedListIndex = serviceRequestIdStartEndRangeBufferedTupleList.Count - 1;
  465:  
  466:                     tuple = serviceRequestIdStartEndRangeBufferedTupleList[serviceRequestIdLatestStartEndRangeBufferedListIndex];
  467:  
  468:                     if (serviceRequestIdLatestStartEndRangeBufferedListIndex == serviceRequestIdStartEndRangeBufferedTupleList.Count - 1)
  469:                     {
  470:                         tuple = new Tuple<int, int>(tuple.Item1, tuple.Item2 + edgeBufferRange);
  471:                     }
  472:  
  473:                     result = "(latest:" + tuple.Item1 + "-" + tuple.Item2 + " " + serviceRequestIdLatestStartEndRangeBufferedListIndex + "/" + serviceRequestIdStartEndRangeBufferedTupleList.Count + ")";
  474:                 }
  475:                 else
  476:                 {
  477:                     tuple = serviceRequestIdStartEndRangeBufferedTupleList[serviceRequestIdStartEndRangeBufferedListIndex];
  478:  
  479:                     result = "(historic:" + tuple.Item1 + "-" + tuple.Item2 + " " + serviceRequestIdStartEndRangeBufferedListIndex + "/" + serviceRequestIdStartEndRangeBufferedTupleList.Count + ")";
  480:  
  481:                     serviceRequestIdStartEndRangeBufferedListIndex = Ia.Cl.Models.Default.IncrementListIndexOrRestart(serviceRequestIdStartEndRangeBufferedTupleList, serviceRequestIdStartEndRangeBufferedListIndex);
  482:                 }
  483:             }
  484:             else
  485:             {
  486:                 result = "(0-0 0/0)";
  487:  
  488:                 tuple = null;
  489:             }
  490:  
  491:             index = serviceRequestIdStartEndRangeBufferedListIndex;
  492:  
  493:             return tuple;
  494:         }
  495:  
  496:         ////////////////////////////////////////////////////////////////////////////    
  497:  
  498:         /// <summary>
  499:         ///
  500:         /// </summary>
  501:         public static string AlterSessionOfCustomerDepartmentOracleDatabase
  502:         {
  503:             get
  504:             {
  505:                 return @"alter session set nls_date_format = 'DD/MM/YYYY HH24:MI:SS'";
  506:             }
  507:         }
  508:  
  509:         ////////////////////////////////////////////////////////////////////////////
  510:  
  511:         /// <summary>
  512:         /// Return list of service requests
  513:         /// </summary>
  514:         public static List<Ia.Ftn.Cl.Models.ServiceRequest> List()
  515:         {
  516:             List<Ia.Ftn.Cl.Models.ServiceRequest> serviceRequestList;
  517:  
  518:             using (var db = new Ia.Ftn.Cl.Db())
  519:             {
  520:                 serviceRequestList = (from sr in db.ServiceRequests select sr).ToList();
  521:             }
  522:  
  523:             return serviceRequestList;
  524:         }
  525:  
  526:         ////////////////////////////////////////////////////////////////////////////
  527:  
  528:         /// <summary>
  529:         ///
  530:         /// </summary>
  531:         public static List<Ia.Ftn.Cl.Models.ServiceRequest> List(int number)
  532:         {
  533:             List<Ia.Ftn.Cl.Models.ServiceRequest> serviceRequestList;
  534:  
  535:             using (var db = new Ia.Ftn.Cl.Db())
  536:             {
  537:                 serviceRequestList = (from sr in db.ServiceRequests where sr.Number == number select sr).ToList();
  538:             }
  539:  
  540:             return serviceRequestList;
  541:         }
  542:  
  543:         ////////////////////////////////////////////////////////////////////////////
  544:  
  545:         /// <summary>
  546:         ///
  547:         /// </summary>
  548:         public static List<Ia.Ftn.Cl.Models.ServiceRequest> List(string service)
  549:         {
  550:             List<Ia.Ftn.Cl.Models.ServiceRequest> serviceRequestList;
  551:  
  552:             if (!string.IsNullOrEmpty(service))
  553:             {
  554:                 if (int.TryParse(service, out int number))
  555:                 {
  556:                     using (var db = new Ia.Ftn.Cl.Db())
  557:                     {
  558:                         serviceRequestList = (from sr in db.ServiceRequests
  559:                                               where sr.Number == number
  560:                                               select sr).ToList();
  561:                     }
  562:                 }
  563:                 else
  564:                 {
  565:                     throw new ArgumentException(@"List(): service is not a number, service: " + service);
  566:                 }
  567:             }
  568:             else serviceRequestList = new List<Ia.Ftn.Cl.Models.ServiceRequest>();
  569:  
  570:             return serviceRequestList;
  571:         }
  572:  
  573:         ////////////////////////////////////////////////////////////////////////////
  574:  
  575:         /// <summary>
  576:         ///
  577:         /// </summary>
  578:         public static List<Ia.Ftn.Cl.Models.ServiceRequest> ListById(int id)
  579:         {
  580:             List<Ia.Ftn.Cl.Models.ServiceRequest> serviceRequestList;
  581:  
  582:             if (id > 0)
  583:             {
  584:                 using (var db = new Ia.Ftn.Cl.Db())
  585:                 {
  586:                     serviceRequestList = (from sr in db.ServiceRequests where sr.Id == id select sr).ToList();
  587:                 }
  588:             }
  589:             else serviceRequestList = new List<Ia.Ftn.Cl.Models.ServiceRequest>();
  590:  
  591:             return serviceRequestList;
  592:         }
  593:  
  594:         ////////////////////////////////////////////////////////////////////////////
  595:  
  596:         /// <summary>
  597:         ///
  598:         /// </summary>
  599:         public static List<Ia.Ftn.Cl.Models.ServiceRequest> List(List<int> numberList)
  600:         {
  601:             List<string> serviceList;
  602:  
  603:             serviceList = (from n in numberList select n.ToString()).ToList();
  604:  
  605:             return List(serviceList);
  606:         }
  607:  
  608:         ////////////////////////////////////////////////////////////////////////////
  609:  
  610:         /// <summary>
  611:         ///
  612:         /// </summary>
  613:         public static List<Ia.Ftn.Cl.Models.ServiceRequest> List(List<string> serviceList)
  614:         {
  615:             List<Ia.Ftn.Cl.Models.ServiceRequest> serviceRequestList, viaServiceRequestTypeList;
  616:  
  617:             using (var db = new Ia.Ftn.Cl.Db())
  618:             {
  619:                 if (serviceList.Count > 0)
  620:                 {
  621:                     serviceRequestList = (from sr in db.ServiceRequests
  622:                                           where serviceList.Contains(sr.Number.ToString())
  623:                                           select sr).Include(a => a.ServiceRequestService).Include(a => a.ServiceRequestTypes).ToList();
  624:  
  625:                     viaServiceRequestTypeList = (from srt in db.ServiceRequestTypes
  626:                                                  join sr in db.ServiceRequests on srt.ServiceRequest.Id equals sr.Id
  627:                                                  where srt.TypeId == 11 && serviceList.Contains(srt.Value)
  628:                                                  select sr).Include(a => a.ServiceRequestService).ToList();
  629:  
  630:                     serviceRequestList = serviceRequestList.Union(viaServiceRequestTypeList).Distinct().ToList();
  631:                 }
  632:                 else serviceRequestList = new List<Ia.Ftn.Cl.Models.ServiceRequest>();
  633:             }
  634:  
  635:             return serviceRequestList.ToList();
  636:         }
  637:  
  638:         ////////////////////////////////////////////////////////////////////////////
  639:  
  640:         /// <summary>
  641:         ///
  642:         /// </summary>
  643:         public static List<Ia.Ftn.Cl.Models.ServiceRequest> List(List<Ia.Ftn.Cl.Models.Business.ServiceRequest.NumberSerial> numberSerialList)
  644:         {
  645:             List<long> idList;
  646:             List<Ia.Ftn.Cl.Models.ServiceRequest> list;
  647:  
  648:             if (numberSerialList.Count > 0)
  649:             {
  650:                 idList = numberSerialList.IdList();
  651:  
  652:                 using (var db = new Ia.Ftn.Cl.Db())
  653:                 {
  654:                     list = (from sr in db.ServiceRequests.Include(a => a.ServiceRequestService)
  655:                             where
  656:                             //numberSerialList.Contains(q.Number, q.Serial) does not work
  657:                             //((from r in numberSerialList where r.Number == q.Number && r.Serial == q.Serial select r) != null) does not work
  658:                             //numberList.Any<int>(i=> i == q.Number)  does not work
  659:                             idList.Contains((long)sr.Number * 100 + sr.Serial)
  660:                             select sr).ToList();
  661:                 }
  662:             }
  663:             else list = new List<Ia.Ftn.Cl.Models.ServiceRequest>();
  664:  
  665:             return list;
  666:         }
  667:  
  668:         ////////////////////////////////////////////////////////////////////////////
  669:  
  670:         /// <summary>
  671:         ///
  672:         /// </summary>
  673:         public static int FirstIdAfterDateTime(DateTime dateTime)
  674:         {
  675:             int serviceRequestId;
  676:  
  677:             using (var db = new Ia.Ftn.Cl.Db())
  678:             {
  679:                 serviceRequestId = (from sr in db.ServiceRequests
  680:                                     where sr.RequestDateTime >= dateTime
  681:                                     select sr.Id).FirstOrDefault();
  682:             }
  683:  
  684:             return serviceRequestId;
  685:         }
  686:  
  687:         /*
  688:         ////////////////////////////////////////////////////////////////////////////
  689: 
  690:         /// <summary>
  691:         ///
  692:         /// </summary>
  693:         [Obsolete]
  694:         public static List<int> OldNumberAndChangedAndChangedToList(List<int> numberList)
  695:         {
  696:             List<int> number1List, number2List, number3List;//, number4List, number5List;
  697:             List<string> serviceList; //, service1List, service2List, service3List, service4List, service5List;
  698:             List<Ia.Ftn.Cl.Model.ServiceRequest> serviceRequestList, serviceRequest1List, serviceRequest2List, serviceRequest3List, serviceRequest4List, serviceRequest5List;
  699: 
  700:             using (var db = new Ia.Ftn.Cl.Model.Ftn())
  701:             {
  702:                 if (numberList.Count > 0)
  703:                 {
  704:                     serviceList = (from n in numberList select n.ToString()).ToList();
  705: 
  706:                     serviceRequest1List = (from sr in db.ServiceRequests.Include(a => a.ServiceRequestTypes)
  707:                                            where numberList.Contains(sr.Number)
  708:                                            select sr).ToList();
  709: 
  710:                     if (serviceRequest1List.Count > 0)
  711:                     {
  712:                         number1List = (from sr in serviceRequest1List select sr.Number).Distinct().ToList();
  713: 
  714:                         number2List = new List<int>();
  715: 
  716:                         foreach (var srt in serviceRequest1List.SelectMany(u => u.ServiceRequestTypes))
  717:                         {
  718:                             if (srt.TypeId == 11 && int.TryParse(srt.Value, out int i)) number2List.Add(i);
  719:                         }
  720: 
  721:                         // below: this is the collect first changed-to number information
  722:                         if (number2List.Count > 0)
  723:                         {
  724:                             serviceRequest2List = (from sr in db.ServiceRequests.Include(a => a.ServiceRequestTypes)
  725:                                                    where number2List.Contains(sr.Number)
  726:                                                    select sr).ToList();
  727: 
  728:                             if (serviceRequest2List.Count > 0)
  729:                             {
  730:                                 number1List = (from sr in serviceRequest2List select sr.Number).Distinct().ToList();
  731: 
  732:                                 number3List = new List<int>();
  733: 
  734:                                 foreach (var srt in serviceRequest2List.SelectMany(u => u.ServiceRequestTypes))
  735:                                 {
  736:                                     if (srt.TypeId == 11 && int.TryParse(srt.Value, out int i)) number3List.Add(i);
  737:                                 }
  738: 
  739:                                 // below: this is the collect second changed-to number information
  740:                                 if (number3List.Count > 0)
  741:                                 {
  742:                                     serviceRequest3List = (from sr in db.ServiceRequests.Include(a => a.ServiceRequestTypes)
  743:                                                            where number3List.Contains(sr.Number)
  744:                                                            select sr).ToList();
  745:                                 }
  746:                                 else serviceRequest3List = new List<Ia.Ftn.Cl.Model.ServiceRequest>();
  747:                             }
  748:                             else
  749:                             {
  750:                                 serviceRequest2List = new List<Ia.Ftn.Cl.Model.ServiceRequest>();
  751:                                 serviceRequest3List = new List<Ia.Ftn.Cl.Model.ServiceRequest>();
  752:                             }
  753:                         }
  754:                         else
  755:                         {
  756:                             serviceRequest2List = new List<Ia.Ftn.Cl.Model.ServiceRequest>();
  757:                             serviceRequest3List = new List<Ia.Ftn.Cl.Model.ServiceRequest>();
  758:                         }
  759:                     }
  760:                     else
  761:                     {
  762:                         serviceRequest2List = new List<Ia.Ftn.Cl.Model.ServiceRequest>();
  763:                         serviceRequest3List = new List<Ia.Ftn.Cl.Model.ServiceRequest>();
  764:                     }
  765: 
  766: 
  767:                     //
  768:                     serviceRequest4List = (from srt in db.ServiceRequestTypes.Include(a => a.ServiceRequest)
  769:                                            where srt.TypeId == 11 && serviceList.Contains(srt.Value)
  770:                                            select srt.ServiceRequest).ToList();
  771: 
  772:                     if (serviceRequest4List.Count > 0)
  773:                     {
  774:                         number1List = (from sr in serviceRequest4List select sr.Number).Distinct().ToList();
  775: 
  776:                         serviceRequest5List = (from sr in db.ServiceRequests.Include(a => a.ServiceRequestTypes)
  777:                                                where number1List.Contains(sr.Number)
  778:                                                select sr).ToList();
  779:                     }
  780:                     else serviceRequest5List = new List<Ia.Ftn.Cl.Model.ServiceRequest>();
  781: 
  782:                     //
  783:                     serviceRequestList = serviceRequest1List.Union(serviceRequest2List).Union(serviceRequest3List).Union(serviceRequest4List).Union(serviceRequest5List).Distinct().ToList();
  784: 
  785:                     if (serviceRequestList.Count > 0)
  786:                     {
  787:                         numberList = (from n in serviceRequestList select n.Number).Distinct().ToList();
  788:                     }
  789:                     else numberList = new List<int>();
  790:                 }
  791:                 else numberList = new List<int>();
  792:             }
  793: 
  794:             return numberList.ToList();
  795:         }
  796:         */
  797:  
  798:         ////////////////////////////////////////////////////////////////////////////
  799:  
  800:         /// <summary>
  801:         ///
  802:         /// </summary>
  803:         public static List<int> NumberAndChangedNumberAndChangedToNumberList(List<int> numberList)
  804:         {
  805:             List<int> list;
  806:  
  807:             if (numberList.Count > 0)
  808:             {
  809:                 var changedServiceNumberToChangedToServiceNumberDictionary = Ia.Ftn.Cl.Models.Data.ServiceRequest.ChangedServiceNumberToChangedToServiceNumberListDictionary();
  810:  
  811:                 list = new List<int>();
  812:  
  813:                 foreach (var kvp in changedServiceNumberToChangedToServiceNumberDictionary)
  814:                 {
  815:                     if (numberList.Contains(kvp.Key) || list.Contains(kvp.Key))
  816:                     {
  817:                         list.Add(kvp.Key);
  818:  
  819:                         foreach (var i in kvp.Value) list.Add(i);
  820:                     }
  821:                     else
  822:                     {
  823:                         foreach (var i in kvp.Value)
  824:                         {
  825:                             if (numberList.Contains(i)) list.Add(i);
  826:                         }
  827:                     }
  828:                 }
  829:  
  830:                 list.AddRange(numberList);
  831:             }
  832:             else list = new List<int>();
  833:  
  834:             return list.Distinct().ToList();
  835:         }
  836:  
  837:         /*
  838:         ////////////////////////////////////////////////////////////////////////////
  839: 
  840:         /// <summary>
  841:         ///
  842:         /// </summary>
  843:         [Obsolete]
  844:         private static void IterativeNumberAndChangedAndChangedToList(List<int> numberList, ref List<int> list)
  845:         {
  846:             List<int> list0, changedList, changedToList, changedExceptList, changedToExceptList;
  847:             List<string> serviceList;
  848: 
  849:             using (var db = new Ia.Ftn.Cl.Model.Ftn())
  850:             {
  851:                 if (numberList.Count > 0)
  852:                 {
  853:                     changedList = new List<int>();
  854:                     changedToList = new List<int>();
  855: 
  856:                     serviceList = numberList.ConvertAll(delegate (int i) { return i.ToString(); });
  857: 
  858:                     // list
  859:                     var serviceRequestNumberAndSrtTypeIdAndSrtValueList = (from srt in db.ServiceRequestTypes
  860:                                                                            where srt.ServiceRequest != null && numberList.Contains(srt.ServiceRequest.Number)
  861:                                                                            select new { srt.ServiceRequest.Number, srt.TypeId, srt.Value }).AsNoTracking().ToList();
  862: 
  863:                     list0 = (from sr in serviceRequestNumberAndSrtTypeIdAndSrtValueList select sr.Number).Distinct().ToList();
  864: 
  865:                     list = list.Union(list0).Distinct().ToList();
  866: 
  867:                     // changedToList
  868:                     foreach (var srntv in serviceRequestNumberAndSrtTypeIdAndSrtValueList)
  869:                     {
  870:                         if (srntv.TypeId == 11 && int.TryParse(srntv.Value, out int i)) changedToList.Add(i);
  871:                     }
  872: 
  873:                     // changedList
  874:                     changedList = (from srt in db.ServiceRequestTypes
  875:                                    where srt.TypeId == 11 && serviceList.Contains(srt.Value)
  876:                                    select srt.ServiceRequest.Number).Distinct().ToList();
  877: 
  878:                     changedToExceptList = changedToList.Except(list).ToList();
  879:                     if (changedToExceptList.Count > 0) IterativeNumberAndChangedAndChangedToList(changedToExceptList, ref list);
  880: 
  881:                     list = list.Union(changedToExceptList).Distinct().ToList();
  882: 
  883:                     changedExceptList = changedList.Except(list).ToList();
  884:                     if (changedExceptList.Count > 0) IterativeNumberAndChangedAndChangedToList(changedExceptList, ref list);
  885: 
  886:                     list = list.Union(changedExceptList).Distinct().ToList();
  887:                 }
  888:                 else
  889:                 {
  890: 
  891:                 }
  892:             }
  893:         }
  894:         */
  895:  
  896:         ////////////////////////////////////////////////////////////////////////////
  897:  
  898:         /// <summary>
  899:         ///
  900:         /// </summary>
  901:         public static Dictionary<string, List<string>> ChangedServiceToChangedToServiceListDictionary()
  902:         {
  903:             Dictionary<string, List<string>> dictionary;
  904:  
  905:             dictionary = new Dictionary<string, List<string>>();
  906:  
  907:             var list = ChangedServiceNumberToChangedToServiceNumberListDictionary();
  908:  
  909:             foreach (var kvp in list)
  910:             {
  911:                 dictionary[kvp.Key.ToString()] = kvp.Value.ConvertAll<string>(x => x.ToString());
  912:             }
  913:  
  914:             return dictionary;
  915:         }
  916:  
  917:         ////////////////////////////////////////////////////////////////////////////
  918:  
  919:         /// <summary>
  920:         ///
  921:         /// </summary>
  922:         public static Dictionary<int, List<int>> ChangedServiceNumberToChangedToServiceNumberListDictionary()
  923:         {
  924:             if (changedServiceNumberToChangedToServiceNumberListDictionary == null || changedServiceNumberToChangedToServiceNumberListDictionary.Count == 0)
  925:             {
  926:                 lock (objectLock)
  927:                 {
  928:                     changedServiceNumberToChangedToServiceNumberListDictionary = Ia.Ftn.Cl.Models.Data.ServiceRequest._ChangedServiceNumberToChangedToServiceNumberListDictionary();
  929:                 }
  930:             }
  931:  
  932:             return changedServiceNumberToChangedToServiceNumberListDictionary;
  933:         }
  934:  
  935:         ////////////////////////////////////////////////////////////////////////////
  936:  
  937:         /// <summary>
  938:         ///
  939:         /// </summary>
  940:         private static Dictionary<int, List<int>> _ChangedServiceNumberToChangedToServiceNumberListDictionary()
  941:         {
  942:             int i;
  943:             Dictionary<int, List<int>> dictionary;
  944:  
  945:             using (var db = new Ia.Ftn.Cl.Db())
  946:             {
  947:                 /*
  948:                  * select sr.Number, srt.Value from ServiceRequests as sr
  949:                  * left outer join ServiceRequestTypes as srt on srt.ServiceRequest_Id = sr.Id
  950:                  * where srt.TypeId = 11 and srt.Value is not null
  951:                  */
  952:  
  953:                 dictionary = new Dictionary<int, List<int>>();
  954:  
  955:                 // <status id="2003" arabicName="قيد التنفيذ" />
  956:                 // <status id="2005" arabicName="تم التنفيذ" />
  957:  
  958:                 var list = (from sr in db.ServiceRequests
  959:                             join srt in db.ServiceRequestTypes on sr.Id equals srt.ServiceRequest.Id
  960:                             where srt.TypeId == 11 && srt.Value != null && (sr.Status == 2003 || sr.Status == 2005)
  961:                             select new { Changed = sr.Number, ChangedTo = srt.Value }).ToList();
  962:  
  963:                 foreach (var v in list)
  964:                 {
  965:                     if (!dictionary.ContainsKey(v.Changed)) dictionary[v.Changed] = new List<int>();
  966:  
  967:                     i = int.Parse(v.ChangedTo);
  968:  
  969:                     if (!dictionary[v.Changed].Contains(i)) dictionary[v.Changed].Add(i);
  970:                 }
  971:             }
  972:  
  973:             return dictionary;
  974:         }
  975:  
  976:         ////////////////////////////////////////////////////////////////////////////
  977:  
  978:         /// <summary>
  979:         ///
  980:         /// </summary>
  981:         public static Dictionary<int, List<int>> PbxServiceKeyToPbxServiceBranchListDictionary()
  982:         {
  983:             if (pbxServiceKeyToPbxServiceBranchListDictionary == null || pbxServiceKeyToPbxServiceBranchListDictionary.Count == 0)
  984:             {
  985:                 lock (objectLock)
  986:                 {
  987:                     pbxServiceKeyToPbxServiceBranchListDictionary = Ia.Ftn.Cl.Models.Data.ServiceRequest._PbxServiceKeyToPbxServiceBranchListDictionary();
  988:                 }
  989:             }
  990:  
  991:             return pbxServiceKeyToPbxServiceBranchListDictionary;
  992:         }
  993:  
  994:         ////////////////////////////////////////////////////////////////////////////
  995:  
  996:         /// <summary>
  997:         ///
  998:         /// </summary>
  999:         private static Dictionary<int, List<int>> _PbxServiceKeyToPbxServiceBranchListDictionary()
 1000:         {
 1001:             int key, branch;
 1002:             string s, key0;
 1003:             Match match;
 1004:             List<int> branchList;
 1005:             Dictionary<int, List<int>> dictionary;
 1006:  
 1007:             using (var db = new Ia.Ftn.Cl.Db())
 1008:             {
 1009:                 /*
 1010: select sr.Number, srt.Value from ServiceRequests as sr
 1011: left outer join ServiceRequestTypes as srt on srt.ServiceRequest_Id = sr.Id
 1012: where srt.TypeId = 12 and srt.Value is not null                 
 1013:                 */
 1014:  
 1015:                 var dictionary0 = new Dictionary<string, List<int>>();
 1016:                 dictionary = new Dictionary<int, List<int>>();
 1017:  
 1018:                 // <status id="2003" arabicName="قيد التنفيذ" />
 1019:                 // <status id="2005" arabicName="تم التنفيذ" />
 1020:  
 1021:                 var list = (from sr in db.ServiceRequests
 1022:                             join srt in db.ServiceRequestTypes on sr.Id equals srt.ServiceRequest.Id
 1023:                             where srt.TypeId == 12 && srt.Value != null && (sr.Status == 2003 || sr.Status == 2005)
 1024:                             select new { ServiceBranch = sr.Number, ServiceKey = srt.Value }).ToList();
 1025:  
 1026:                 foreach (var l in list)
 1027:                 {
 1028:                     branch = l.ServiceBranch;
 1029:                     key0 = l.ServiceKey;
 1030:  
 1031:                     // pad the single digit serial with a 0 because serials might have two digits, to preserve order
 1032:                     key0 = Regex.Replace(key0, @"(\d{8})\/(\d)", @"$1/0$2");
 1033:  
 1034:                     if (!dictionary0.ContainsKey(key0)) dictionary0[key0] = new List<int>();
 1035:  
 1036:                     if (!dictionary0[key0].Contains(branch)) dictionary0[key0].Add(branch);
 1037:                 }
 1038:  
 1039:                 foreach (var l in dictionary0.Keys.OrderBy(u => u))
 1040:                 {
 1041:                     match = Regex.Match(l, @"(\d+)");
 1042:  
 1043:                     if (match.Groups[1].Success)
 1044:                     {
 1045:                         s = match.Groups[1].Captures[0].Value;
 1046:                         key = int.Parse(s);
 1047:  
 1048:                         branchList = dictionary0[l];
 1049:  
 1050:                         // this will replace numbers with earlier serials
 1051:                         dictionary[key] = branchList;
 1052:                     }
 1053:                     else
 1054:                     {
 1055:                     }
 1056:                 }
 1057:  
 1058:             }
 1059:  
 1060:             return dictionary;
 1061:         }
 1062:  
 1063:         ////////////////////////////////////////////////////////////////////////////
 1064:  
 1065:         /// <summary>
 1066:         ///
 1067:         /// </summary>
 1068:         public static Dictionary<string, List<string>> ChangedAndChangedToServiceToRelatedChangedAndChangedToServiceListDictionary()
 1069:         {
 1070:             if (changedAndChangedToServiceToRelatedChangedAndChangedToServiceListDictionary == null || changedAndChangedToServiceToRelatedChangedAndChangedToServiceListDictionary.Count == 0)
 1071:             {
 1072:                 lock (objectLock)
 1073:                 {
 1074:                     changedAndChangedToServiceToRelatedChangedAndChangedToServiceListDictionary = Ia.Ftn.Cl.Models.Data.ServiceRequest._ChangedAndChangedToServiceToRelatedChangedAndChangedToServiceListDictionary();
 1075:                 }
 1076:             }
 1077:  
 1078:             return changedAndChangedToServiceToRelatedChangedAndChangedToServiceListDictionary;
 1079:         }
 1080:  
 1081:         ////////////////////////////////////////////////////////////////////////////
 1082:  
 1083:         /// <summary>
 1084:         ///
 1085:         /// </summary>
 1086:         private static Dictionary<string, List<string>> _ChangedAndChangedToServiceToRelatedChangedAndChangedToServiceListDictionary()
 1087:         {
 1088:             var dictionary0 = new Dictionary<string, List<string>>();
 1089:  
 1090:             var changedServiceToChangedToServiceListDictionary = Ia.Ftn.Cl.Models.Data.ServiceRequest.ChangedServiceToChangedToServiceListDictionary();
 1091:  
 1092:             foreach (var kvp in changedServiceToChangedToServiceListDictionary)
 1093:             {
 1094:                 var changed = kvp.Key;
 1095:                 var changedToList = kvp.Value;
 1096:  
 1097:                 if (!dictionary0.ContainsKey(changed)) dictionary0[changed] = new List<string>();
 1098:  
 1099:                 if (!dictionary0[changed].Contains(changed)) dictionary0[changed].Add(changed);
 1100:  
 1101:                 foreach (var changedTo in changedToList)
 1102:                 {
 1103:                     if (!dictionary0[changed].Contains(changedTo)) dictionary0[changed].Add(changedTo);
 1104:                 }
 1105:             }
 1106:  
 1107:             foreach (var kvp in changedServiceToChangedToServiceListDictionary)
 1108:             {
 1109:                 var changed = kvp.Key;
 1110:                 var changedToList = kvp.Value;
 1111:  
 1112:                 foreach (var changedTo in changedToList)
 1113:                 {
 1114:                     if (!dictionary0.ContainsKey(changedTo)) dictionary0[changedTo] = new List<string>();
 1115:  
 1116:                     if (!dictionary0[changedTo].Contains(changedTo)) dictionary0[changedTo].Add(changedTo);
 1117:  
 1118:                     if (!dictionary0[changedTo].Contains(changed)) dictionary0[changedTo].Add(changed);
 1119:                 }
 1120:             }
 1121:  
 1122:  
 1123:             var dictionary = new Dictionary<string, List<string>>();
 1124:  
 1125:             foreach (var kvp in dictionary0)
 1126:             {
 1127:                 var changed = kvp.Key;
 1128:                 var changedToList = kvp.Value;
 1129:  
 1130:                 if (!dictionary.ContainsKey(changed)) dictionary[changed] = new List<string>();
 1131:  
 1132:                 if (!dictionary[changed].Contains(changed)) dictionary[changed].Add(changed);
 1133:  
 1134:                 foreach (var changedTo in changedToList)
 1135:                 {
 1136:                     if (!dictionary[changed].Contains(changedTo)) dictionary[changed].Add(changedTo);
 1137:  
 1138:                     var s = dictionary0[changedTo];
 1139:  
 1140:                     foreach (var t in s)
 1141:                     {
 1142:                         if (!dictionary[changed].Contains(t)) dictionary[changed].Add(t);
 1143:                     }
 1144:  
 1145:                 }
 1146:             }
 1147:  
 1148:             return dictionary;
 1149:         }
 1150:  
 1151:         ////////////////////////////////////////////////////////////////////////////
 1152:  
 1153:         /// <summary>
 1154:         ///
 1155:         /// </summary>
 1156:         public static List<string> RelatedChangedAndChangedToServiceListByService(string service)
 1157:         {
 1158:             List<string> list;
 1159:  
 1160:             var dictionary = Ia.Ftn.Cl.Models.Data.ServiceRequest.ChangedAndChangedToServiceToRelatedChangedAndChangedToServiceListDictionary();
 1161:  
 1162:             if (dictionary.ContainsKey(service))
 1163:             {
 1164:                 list = dictionary[service];
 1165:             }
 1166:             else list = new List<string>();
 1167:  
 1168:             list = list.Distinct().ToList();
 1169:  
 1170:             return list;
 1171:         }
 1172:  
 1173:         ////////////////////////////////////////////////////////////////////////////
 1174:  
 1175:         /// <summary>
 1176:         ///
 1177:         /// </summary>
 1178:         public static List<int> RelatedChangedAndChangedToServiceNumberListByServiceNumber(int serviceNumber)
 1179:         {
 1180:             List<int> list;
 1181:             List<string> list0;
 1182:  
 1183:             var dictionary = Ia.Ftn.Cl.Models.Data.ServiceRequest.ChangedAndChangedToServiceToRelatedChangedAndChangedToServiceListDictionary();
 1184:  
 1185:             var service = serviceNumber.ToString();
 1186:  
 1187:             if (dictionary.ContainsKey(service))
 1188:             {
 1189:                 list0 = dictionary[service];
 1190:             }
 1191:             else list0 = new List<string>();
 1192:  
 1193:             list0 = list0.Distinct().ToList();
 1194:  
 1195:             list = list0.Select(int.Parse).ToList();
 1196:  
 1197:             return list;
 1198:         }
 1199:  
 1200:         ////////////////////////////////////////////////////////////////////////////
 1201:  
 1202:         /// <summary>
 1203:         ///
 1204:         /// </summary>
 1205:         public static List<string> ServiceListAndRelatedChangedAndChangedToServiceListByServiceList(List<string> serviceList)
 1206:         {
 1207:             List<string> list;
 1208:  
 1209:             if (serviceList.Count > 0)
 1210:             {
 1211:                 var dictionary = Ia.Ftn.Cl.Models.Data.ServiceRequest.ChangedAndChangedToServiceToRelatedChangedAndChangedToServiceListDictionary();
 1212:  
 1213:                 list = new List<string>();
 1214:  
 1215:                 foreach (var service in serviceList)
 1216:                 {
 1217:                     if (dictionary.ContainsKey(service))
 1218:                     {
 1219:                         list.AddRange(dictionary[service]);
 1220:                     }
 1221:                 }
 1222:  
 1223:                 list = list.Distinct().ToList();
 1224:  
 1225:                 list.AddRange(serviceList);
 1226:             }
 1227:             else list = new List<string>();
 1228:  
 1229:             return list.Distinct().ToList();
 1230:         }
 1231:  
 1232:         ////////////////////////////////////////////////////////////////////////////
 1233:  
 1234:         /// <summary>
 1235:         ///
 1236:         /// </summary>
 1237:         public static List<int> ServiceNumberListAndRelatedChangedAndChangedToServiceNumberListByServiceNumberList(List<int> serviceNumberList)
 1238:         {
 1239:             List<int> list;
 1240:             List<string> list0;
 1241:  
 1242:             if (serviceNumberList.Count > 0)
 1243:             {
 1244:                 var dictionary = Ia.Ftn.Cl.Models.Data.ServiceRequest.ChangedAndChangedToServiceToRelatedChangedAndChangedToServiceListDictionary();
 1245:  
 1246:                 var serviceList = serviceNumberList.ConvertAll<string>(delegate (int i) { return i.ToString(); });
 1247:  
 1248:                 list0 = new List<string>();
 1249:  
 1250:                 foreach (var service in serviceList)
 1251:                 {
 1252:                     if (dictionary.ContainsKey(service))
 1253:                     {
 1254:                         list0.AddRange(dictionary[service]);
 1255:                     }
 1256:                 }
 1257:  
 1258:                 list0 = list0.Distinct().ToList();
 1259:  
 1260:                 list = list0.Select(int.Parse).ToList();
 1261:  
 1262:                 list.AddRange(serviceNumberList);
 1263:             }
 1264:             else list = new List<int>();
 1265:  
 1266:             return list.Distinct().ToList();
 1267:         }
 1268:  
 1269:         ////////////////////////////////////////////////////////////////////////////
 1270:  
 1271:         /// <summary>
 1272:         ///
 1273:         /// </summary>
 1274:         public static List<string> ChangedAndChangedToServiceList()
 1275:         {
 1276:             var changedServiceToChangedToServiceDictionary = Ia.Ftn.Cl.Models.Data.ServiceRequest.ChangedServiceNumberToChangedToServiceNumberListDictionary();
 1277:  
 1278:             var list = new List<string>();
 1279:  
 1280:             foreach (var kvp in changedServiceToChangedToServiceDictionary)
 1281:             {
 1282:                 var changed = kvp.Key;
 1283:                 var changedToList = kvp.Value;
 1284:  
 1285:                 if (!list.Contains(changed.ToString())) list.Add(changed.ToString());
 1286:  
 1287:                 foreach (var changedTo in changedToList)
 1288:                 {
 1289:                     if (!list.Contains(changedTo.ToString())) list.Add(changedTo.ToString());
 1290:                 }
 1291:             }
 1292:  
 1293:             list.Sort();
 1294:  
 1295:             return list;
 1296:         }
 1297:  
 1298:         ////////////////////////////////////////////////////////////////////////////
 1299:  
 1300:         /// <summary>
 1301:         ///
 1302:         /// </summary>
 1303:         public static List<string> ChangedAndChangedToServiceIdList()
 1304:         {
 1305:             string serviceId;
 1306:             var changedAndChangedToServiceList = Ia.Ftn.Cl.Models.Data.ServiceRequest.ChangedAndChangedToServiceList();
 1307:  
 1308:             var serviceType = Ia.Ftn.Cl.Models.Business.Service.ServiceType.ImsService;
 1309:  
 1310:             var list = new List<string>();
 1311:  
 1312:             foreach (var service in changedAndChangedToServiceList)
 1313:             {
 1314:                 serviceId = Ia.Ftn.Cl.Models.Business.Service.ServiceToServiceId(service, serviceType);
 1315:  
 1316:                 list.Add(serviceId);
 1317:             }
 1318:  
 1319:             list.Sort();
 1320:  
 1321:             return list;
 1322:         }
 1323:  
 1324:         ////////////////////////////////////////////////////////////////////////////
 1325:         ////////////////////////////////////////////////////////////////////////////
 1326:  
 1327:         /// <summary>
 1328:         ///
 1329:         /// </summary>
 1330:         public static List<Ia.Ftn.Cl.Models.ServiceRequest> ListByDomain(List<int> domainList)
 1331:         {
 1332:             List<string> stringDomainList;
 1333:             List<Ia.Ftn.Cl.Models.ServiceRequest> list;
 1334:  
 1335:             stringDomainList = new List<string>();
 1336:  
 1337:             using (var db = new Ia.Ftn.Cl.Db())
 1338:             {
 1339:                 if (domainList.Count > 0)
 1340:                 {
 1341:                     foreach (int i in domainList) stringDomainList.Add(i.ToString());
 1342:  
 1343:                     list = (from sr in db.ServiceRequests.Include(a => a.ServiceRequestService)
 1344:                             where domainList.Any(u => sr.Number.ToString().StartsWith(u.ToString()))
 1345:                             select sr).ToList();
 1346:                 }
 1347:                 else list = new List<Ia.Ftn.Cl.Models.ServiceRequest>();
 1348:             }
 1349:  
 1350:             return list;
 1351:         }
 1352:  
 1353:         ////////////////////////////////////////////////////////////////////////////
 1354:  
 1355:         /// <summary>
 1356:         ///
 1357:         /// </summary>
 1358:         public static List<Ia.Ftn.Cl.Models.ServiceRequest> ListWithinDateTimeRange(DateTime startDateTime, DateTime endDateTime)
 1359:         {
 1360:             List<Ia.Ftn.Cl.Models.ServiceRequest> serviceRequestList;
 1361:  
 1362:             using (var db = new Ia.Ftn.Cl.Db())
 1363:             {
 1364:                 serviceRequestList = (from sr in db.ServiceRequests.Include(a => a.ServiceRequestService)
 1365:                                       where sr.RequestDateTime >= startDateTime && sr.RequestDateTime < endDateTime
 1366:                                       select sr).ToList();
 1367:             }
 1368:  
 1369:             return serviceRequestList;
 1370:         }
 1371:  
 1372:         ////////////////////////////////////////////////////////////////////////////
 1373:  
 1374:         /// <summary>
 1375:         ///
 1376:         /// </summary>
 1377:         public static List<Ia.Ftn.Cl.Models.ServiceRequest> ListWithinIdRange(int start, int end)
 1378:         {
 1379:             List<Ia.Ftn.Cl.Models.ServiceRequest> serviceRequestList;
 1380:  
 1381:             using (var db = new Ia.Ftn.Cl.Db())
 1382:             {
 1383:                 serviceRequestList = (from sr in db.ServiceRequests.Include(a => a.ServiceRequestService)
 1384:                                       where sr.Id >= start && sr.Id <= end
 1385:                                       select sr)/*.AsNoTracking()*/.ToList(); // do not use .AsNoTracking() here, it will cause exception: The instance of entity type 'ServiceRequestService' cannot be tracked because another instance with the same key value for {'Id'} is ...
 1386:             }
 1387:  
 1388:             return serviceRequestList;
 1389:         }
 1390:  
 1391:         ////////////////////////////////////////////////////////////////////////////
 1392:  
 1393:         /// <summary>
 1394:         ///
 1395:         /// </summary>
 1396:         public static List<Ia.Ftn.Cl.Models.ServiceRequest> ListWithinIdRangeWithoutInclude(int start, int end)
 1397:         {
 1398:             List<Ia.Ftn.Cl.Models.ServiceRequest> serviceRequestList;
 1399:  
 1400:             using (var db = new Ia.Ftn.Cl.Db())
 1401:             {
 1402:                 serviceRequestList = (from sr in db.ServiceRequests
 1403:                                       where sr.Id >= start && sr.Id <= end
 1404:                                       select sr).ToList();
 1405:             }
 1406:  
 1407:             return serviceRequestList;
 1408:         }
 1409:  
 1410:         ////////////////////////////////////////////////////////////////////////////
 1411:         ////////////////////////////////////////////////////////////////////////////
 1412:  
 1413:         /// <summary>
 1414:         ///
 1415:         /// </summary>
 1416:         public static void UpdateWithServiceList(List<string> serviceList, List<Ia.Ftn.Cl.Models.ServiceRequest> newServiceRequestList, out string result)
 1417:         {
 1418:             int readItemCount, existingItemCount, insertedItemCount, updatedItemCount, deletedItemCount, serviceRequestId;
 1419:             string serviceRequestServiceId;
 1420:             Ia.Ftn.Cl.Models.ServiceRequest serviceRequest;
 1421:  
 1422:             readItemCount = existingItemCount = insertedItemCount = updatedItemCount = deletedItemCount = 0;
 1423:             result = string.Empty;
 1424:  
 1425:             readItemCount = newServiceRequestList.Count;
 1426:  
 1427:             using (var db = new Ia.Ftn.Cl.Db())
 1428:             {
 1429:                 foreach (Ia.Ftn.Cl.Models.ServiceRequest newServiceRequest in newServiceRequestList)
 1430:                 {
 1431:                     serviceRequestId = newServiceRequest.Id;
 1432:  
 1433:                     serviceRequest = (from sr in db.ServiceRequests
 1434:                                       where sr.Id == serviceRequestId
 1435:                                       select sr).SingleOrDefault();
 1436:  
 1437:                     if (serviceRequest != null) existingItemCount++;
 1438:  
 1439:                     try
 1440:                     {
 1441:                         if (newServiceRequest.ServiceRequestService != null)
 1442:                         {
 1443:                             serviceRequestServiceId = newServiceRequest.ServiceRequestService.Id;
 1444:  
 1445:                             newServiceRequest.ServiceRequestService = (from srs in db.ServiceRequestServices
 1446:                                                                        where srs.Id == serviceRequestServiceId
 1447:                                                                        select srs).SingleOrDefault();
 1448:                         }
 1449:  
 1450:                         if (serviceRequest.Update(newServiceRequest))
 1451:                         {
 1452:                             db.ServiceRequests.Attach(serviceRequest);
 1453:                             db.Entry(serviceRequest).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
 1454:  
 1455:                             updatedItemCount++;
 1456:                         }
 1457:                     }
 1458:                     catch (Exception)// e)
 1459:                     {
 1460:  
 1461:                     }
 1462:                 }
 1463:  
 1464:                 db.SaveChanges();
 1465:  
 1466:                 result = "(" + readItemCount + "/" + existingItemCount + "/" + insertedItemCount + "," + updatedItemCount + "," + deletedItemCount + ")";
 1467:             }
 1468:         }
 1469:  
 1470:         ////////////////////////////////////////////////////////////////////////////
 1471:  
 1472:         /// <summary>
 1473:         ///
 1474:         /// </summary>
 1475:         public static bool UpdateServiceRequestService(Ia.Ftn.Cl.Models.ServiceRequest serviceRequest, Ia.Ftn.Cl.Models.ServiceRequestService serviceRequestService, out string result)
 1476:         {
 1477:             bool b;
 1478:  
 1479:             using (var db = new Ia.Ftn.Cl.Db())
 1480:             {
 1481:                 serviceRequest = (from sr in db.ServiceRequests where sr.Id == serviceRequest.Id select sr).SingleOrDefault();
 1482:  
 1483:                 if (serviceRequest.ServiceRequestService != serviceRequestService)
 1484:                 {
 1485:                     serviceRequest.ServiceRequestService = (from srs in db.ServiceRequestServices where srs.Id == serviceRequestService.Id select srs).SingleOrDefault();
 1486:  
 1487:                     db.ServiceRequests.Attach(serviceRequest);
 1488:                     db.Entry(serviceRequest).Property(u => u.ServiceRequestService).IsModified = true;
 1489:  
 1490:                     db.SaveChanges();
 1491:  
 1492:                     result = "Success: ServiceRequests ServiceRequestService updated. ";
 1493:                     b = true;
 1494:                 }
 1495:                 else
 1496:                 {
 1497:                     result = "Warning: ServiceRequests ServiceRequestService value was not updated because its the same. ";
 1498:  
 1499:                     b = false;
 1500:                 }
 1501:             }
 1502:  
 1503:             return b;
 1504:         }
 1505:  
 1506:         ////////////////////////////////////////////////////////////////////////////
 1507:  
 1508:         /// <summary>
 1509:         ///
 1510:         /// </summary>
 1511:         public static void UpdateForADateTimeRangeWithOutputDataTable(DataTable dataTable, DateTime dateTime, out string result)
 1512:         {
 1513:             // below: the SQL statement should be within the dataTable.TableName variable
 1514:             int number, readItemCount, existingItemCount, insertedItemCount, updatedItemCount, deletedItemCount;
 1515:             int serviceRequestId;
 1516:             string sql, r, customerAddress;
 1517:             ArrayList newServiceRequestIdArryList;
 1518:             DateTime startDateTime, endDateTime;
 1519:             Match match;
 1520:             Ia.Ftn.Cl.Models.Business.ServiceAddress serviceAddress;
 1521:             Ia.Ftn.Cl.Models.ServiceRequest serviceRequest, newServiceRequest;
 1522:             List<Ia.Ftn.Cl.Models.ServiceRequest> serviceRequestList;
 1523:             List<Ia.Ftn.Cl.Models.ServiceRequestType> serviceRequestTypeList;
 1524:  
 1525:             readItemCount = existingItemCount = insertedItemCount = updatedItemCount = deletedItemCount = 0;
 1526:             result = r = string.Empty;
 1527:  
 1528:             startDateTime = DateTime.MinValue;
 1529:  
 1530:             if (dataTable != null)
 1531:             {
 1532:                 sql = dataTable.TableName;
 1533:  
 1534:                 // select * from SRV_REQ_FIPER where REQ_DATE >= 'dd/MM/yyyy' and REQ_DATE < 'dd/MM/yyyy' order by REQ_DATE ASC, SRV_REQ_ID ASC;
 1535:                 // select * from SRV_REQ_FIPER where REQ_DATE >= '01-10-2006' and REQ_DATE < '02-10-2006' order by REQ_DATE asc, SRV_REQ_ID asc;
 1536:  
 1537:                 match = Regex.Match(sql, @".+'(\d{2})\/(\d{2})\/(\d{4})'.+'(\d{2})\/(\d{2})\/(\d{4})'.+", RegexOptions.Singleline);
 1538:                 //                             1        2        3        4          5        6
 1539:  
 1540:                 if (match.Success)
 1541:                 {
 1542:                     using (var db = new Ia.Ftn.Cl.Db())
 1543:                     {
 1544:                         readItemCount = dataTable.Rows.Count;
 1545:  
 1546:                         //if (dataTable.Rows.Count > 0)
 1547:                         //{
 1548:                         startDateTime = DateTime.Parse(match.Groups[3].Value + "-" + match.Groups[2].Value + "-" + match.Groups[1].Value);
 1549:                         endDateTime = DateTime.Parse(match.Groups[6].Value + "-" + match.Groups[5].Value + "-" + match.Groups[4].Value);
 1550:  
 1551:                         serviceRequestList = Ia.Ftn.Cl.Models.Data.ServiceRequest.ListWithinDateTimeRange(startDateTime, endDateTime);
 1552:                         existingItemCount = serviceRequestList.Count;
 1553:  
 1554:                         newServiceRequestIdArryList = new ArrayList(dataTable.Rows.Count + 1);
 1555:  
 1556:                         foreach (DataRow dataRow in dataTable.Rows)
 1557:                         {
 1558:                             number = int.Parse(dataRow["SRV_NO"].ToString());
 1559:  
 1560:                             if (Ia.Ftn.Cl.Models.Business.Service.ServiceHasEightDigitsAndIsWithinAllowedDomainList(number))
 1561:                             {
 1562:                                 serviceRequestId = int.Parse(dataRow["SRV_REQ_ID"].ToString());
 1563:  
 1564:                                 customerAddress = dataRow["ADDRESS"].ToString();
 1565:                                 serviceAddress = Ia.Ftn.Cl.Models.Business.ServiceRequest.ServiceAddress(number.ToString(), customerAddress, out string level);
 1566:  
 1567:                                 newServiceRequest = new Ia.Ftn.Cl.Models.ServiceRequest()
 1568:                                 {
 1569:                                     Id = serviceRequestId,
 1570:                                     Number = number,
 1571:  
 1572:                                     CustomerAddress = customerAddress,
 1573:                                     AreaId = serviceAddress.AreaId,
 1574:  
 1575:                                     CustomerCategoryId = int.Parse(dataRow["CUST_CAT_ID"].ToString()),
 1576:                                     CustomerId = int.Parse(dataRow["ACCOUNT_NO"].ToString()),
 1577:                                     CustomerName = Ia.Ftn.Cl.Models.Business.Default.CorrectCustomerName(dataRow["NAME"].ToString()),
 1578:                                     RequestDateTime = DateTime.Parse(dataRow["REQ_DATE"].ToString()),
 1579:                                     Serial = int.Parse(dataRow["SRV_SER_NO"].ToString()),
 1580:                                     ServiceCategoryId = int.Parse(dataRow["SRV_CAT_ID"].ToString()),
 1581:                                     ServiceId = int.Parse(dataRow["SRV_ID"].ToString()),
 1582:                                     Balance = double.Parse(dataRow["BALANCE"].ToString()),
 1583:                                     Status = int.Parse(dataRow["STATUS"].ToString())
 1584:                                 };
 1585:  
 1586:                                 serviceRequest = (from sr in serviceRequestList where sr.Id == newServiceRequest.Id select sr).SingleOrDefault();
 1587:  
 1588:                                 if (serviceRequest == null)
 1589:                                 {
 1590:                                     newServiceRequest.Created = newServiceRequest.Updated = DateTime.UtcNow.AddHours(3);
 1591:  
 1592:                                     db.ServiceRequests.Add(newServiceRequest);
 1593:  
 1594:                                     insertedItemCount++;
 1595:                                 }
 1596:                                 else
 1597:                                 {
 1598:                                     // below: copy values from newServiceRequest to serviceRequest
 1599:  
 1600:                                     if (serviceRequest.Update(newServiceRequest))
 1601:                                     {
 1602:                                         db.ServiceRequests.Attach(serviceRequest);
 1603:                                         db.Entry(serviceRequest).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
 1604:  
 1605:                                         updatedItemCount++;
 1606:                                     }
 1607:                                 }
 1608:  
 1609:                                 newServiceRequestIdArryList.Add(serviceRequestId);
 1610:                             }
 1611:                             else
 1612:                             {
 1613:                                 r += "Number: " + number + " is not within allowed domain list, ";
 1614:                             }
 1615:                         }
 1616:  
 1617:                         // below: this function will remove values that were not present in the reading
 1618:                         if (serviceRequestList.Count > 0)
 1619:                         {
 1620:                             foreach (Ia.Ftn.Cl.Models.ServiceRequest sr in serviceRequestList)
 1621:                             {
 1622:                                 if (!newServiceRequestIdArryList.Contains(sr.Id))
 1623:                                 {
 1624:                                     serviceRequest = (from sr2 in db.ServiceRequests where sr2.Id == sr.Id select sr2).SingleOrDefault();
 1625:  
 1626:                                     db.ServiceRequests.Remove(serviceRequest);
 1627:  
 1628:                                     // below: we will also remove SRT records referensing this SR
 1629:                                     serviceRequestTypeList = (from srt in db.ServiceRequestTypes where srt.ServiceRequest.Id == sr.Id select srt).ToList();
 1630:  
 1631:                                     foreach (Ia.Ftn.Cl.Models.ServiceRequestType srt in serviceRequestTypeList) db.ServiceRequestTypes.Remove(srt);
 1632:  
 1633:                                     deletedItemCount++;
 1634:                                 }
 1635:                             }
 1636:                         }
 1637:  
 1638:                         db.SaveChanges();
 1639:  
 1640:                         result = "(" + readItemCount + "/" + existingItemCount + "/" + insertedItemCount + "," + updatedItemCount + "," + deletedItemCount + ")" + r;
 1641:                         //}
 1642:                         //else
 1643:                         //{
 1644:                         //    result = "(" + readItemCount + "/?/?)";
 1645:                         //}
 1646:                     }
 1647:                 }
 1648:                 else
 1649:                 {
 1650:                     result = "(?/?/?: SQL in TableName is unmatched)";
 1651:                 }
 1652:             }
 1653:             else
 1654:             {
 1655:                 result = "(dataTable == null/?/?)";
 1656:             }
 1657:         }
 1658:  
 1659:         ////////////////////////////////////////////////////////////////////////////
 1660:  
 1661:         /// <summary>
 1662:         ///
 1663:         /// </summary>
 1664:         public static string UpdateForServiceRequestWithOutputDataTableAndIdRange(DataTable dataTable, out List<string> insertedOrUpdatedOrDeletedServiceList)
 1665:         {
 1666:             // below: the SQL statement should be within the dataTable.TableName variable
 1667:             int serviceRequestId, start, end, readItemCount, existingItemCount, insertedItemCount, updatedItemCount, deletedItemCount;
 1668:             string sql, r, customerAddress, result;
 1669:             ArrayList newServiceRequestIdArrayList;
 1670:             Match match;
 1671:             Ia.Ftn.Cl.Models.Business.ServiceAddress serviceAddress;
 1672:             Ia.Ftn.Cl.Models.ServiceRequest serviceRequest, newServiceRequest;
 1673:             List<int> numbersNotWithinAllowedDomainList;
 1674:             List<Ia.Ftn.Cl.Models.ServiceRequest> serviceRequestList;
 1675:             List<Ia.Ftn.Cl.Models.ServiceRequestType> serviceRequestTypeList;
 1676:  
 1677:             readItemCount = existingItemCount = insertedItemCount = updatedItemCount = deletedItemCount = 0;
 1678:             result = r = string.Empty;
 1679:             numbersNotWithinAllowedDomainList = new List<int>();
 1680:  
 1681:             insertedOrUpdatedOrDeletedServiceList = new List<string>();
 1682:  
 1683:             if (dataTable != null)
 1684:             {
 1685:                 sql = dataTable.TableName;
 1686:  
 1687:                 // select * from SRV_REQ_FIPER where SRV_REQ_ID >= 110000 and SRV_REQ_ID <= 321203 order by REQ_DATE asc, SRV_REQ_ID asc
 1688:                 match = Regex.Match(sql, @"SRV_REQ_ID >= (\d+) and SRV_REQ_ID <= (\d+) ", RegexOptions.Singleline);
 1689:                 //                                       1                       2
 1690:  
 1691:                 if (match.Success)
 1692:                 {
 1693:                     using (var db = new Ia.Ftn.Cl.Db())
 1694:                     {
 1695:                         readItemCount = dataTable.Rows.Count;
 1696:  
 1697:                         start = int.Parse(match.Groups[1].Value);
 1698:                         end = int.Parse(match.Groups[2].Value);
 1699:  
 1700:                         serviceRequestList = Ia.Ftn.Cl.Models.Data.ServiceRequest.ListWithinIdRange(start, end);
 1701:                         existingItemCount = serviceRequestList.Count;
 1702:  
 1703:                         newServiceRequestIdArrayList = new ArrayList(dataTable.Rows.Count + 1);
 1704:  
 1705:                         foreach (DataRow dataRow in dataTable.Rows)
 1706:                         {
 1707:                             if (int.TryParse(dataRow["SRV_NO"].ToString(), out int number))
 1708:                             {
 1709:                                 if (Ia.Ftn.Cl.Models.Business.Service.ServiceHasEightDigitsAndIsWithinAllowedDomainList(number))
 1710:                                 {
 1711:                                     serviceRequestId = int.Parse(dataRow["SRV_REQ_ID"].ToString());
 1712:  
 1713:                                     if (Ia.Ftn.Cl.Models.Business.ServiceRequest.ServiceRequestIdIsAllowedForProcessing(serviceRequestId))
 1714:                                     {
 1715:                                         customerAddress = dataRow["ADDRESS"].ToString();
 1716:  
 1717:                                         serviceAddress = Ia.Ftn.Cl.Models.Business.ServiceRequest.ServiceAddress(number.ToString(), customerAddress, out string level);
 1718:  
 1719:                                         newServiceRequest = new Ia.Ftn.Cl.Models.ServiceRequest()
 1720:                                         {
 1721:                                             Id = serviceRequestId,
 1722:                                             Number = number,
 1723:  
 1724:                                             CustomerAddress = customerAddress,
 1725:                                             AreaId = serviceAddress.AreaId,
 1726:  
 1727:                                             CustomerCategoryId = int.TryParse(dataRow["CUST_CAT_ID"].ToString(), out int i) ? i : 0,
 1728:                                             CustomerId = int.TryParse(dataRow["ACCOUNT_NO"].ToString(), out i) ? i : 0,
 1729:                                             CustomerName = Ia.Ftn.Cl.Models.Business.Default.CorrectCustomerName(dataRow["NAME"].ToString()),
 1730:                                             RequestDateTime = DateTime.Parse(dataRow["REQ_DATE"].ToString()),
 1731:                                             Serial = int.TryParse(dataRow["SRV_SER_NO"].ToString(), out i) ? i : 0,
 1732:                                             ServiceCategoryId = int.TryParse(dataRow["SRV_CAT_ID"].ToString(), out i) ? i : 0,
 1733:                                             ServiceId = int.TryParse(dataRow["SRV_ID"].ToString(), out i) ? i : 0,
 1734:                                             Balance = double.Parse(dataRow["BALANCE"].ToString()),
 1735:  
 1736:                                             Status = int.TryParse(dataRow["STATUS"].ToString(), out i) ? i : 0
 1737:                                         };
 1738:  
 1739:                                         serviceRequest = (from sr in serviceRequestList
 1740:                                                           where sr.Id == newServiceRequest.Id
 1741:                                                           select sr).SingleOrDefault();
 1742:  
 1743:                                         if (serviceRequest == null)
 1744:                                         {
 1745:                                             newServiceRequest.Created = newServiceRequest.Updated = DateTime.UtcNow.AddHours(3);
 1746:  
 1747:                                             db.ServiceRequests.Add(newServiceRequest);
 1748:  
 1749:                                             insertedOrUpdatedOrDeletedServiceList.Add(newServiceRequest.Number.ToString());
 1750:                                             insertedItemCount++;
 1751:                                         }
 1752:                                         else
 1753:                                         {
 1754:                                             // below: copy values from newServiceRequest to serviceRequest
 1755:  
 1756:                                             if (serviceRequest.UpdateSkipServiceRequestServiceAndCheckIfOnlyBalanceWasUpdated(newServiceRequest, out bool onlyBalanceWasUpdated))
 1757:                                             {
 1758:                                                 db.ServiceRequests.Attach(serviceRequest);
 1759:                                                 db.Entry(serviceRequest).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
 1760:  
 1761:                                                 if (!onlyBalanceWasUpdated) insertedOrUpdatedOrDeletedServiceList.Add(serviceRequest.Number.ToString());
 1762:  
 1763:                                                 updatedItemCount++;
 1764:                                             }
 1765:                                         }
 1766:  
 1767:                                         newServiceRequestIdArrayList.Add(serviceRequestId); // keep at the end
 1768:                                     }
 1769:                                     else
 1770:                                     {
 1771:  
 1772:                                     }
 1773:                                 }
 1774:                                 else
 1775:                                 {
 1776:                                     numbersNotWithinAllowedDomainList.Add(number);
 1777:                                 }
 1778:                             }
 1779:                             else
 1780:                             {
 1781:                                 //
 1782:                             }
 1783:                         }
 1784:  
 1785:                         /*
 1786:                         if (numbersNotWithinAllowedDomainList.Count > 0)
 1787:                         {
 1788:                             r = "Numbers not within allowed domain list: ";
 1789: 
 1790:                             foreach (int n in numbersNotWithinAllowedDomainList) r += n + ",";
 1791: 
 1792:                             r = r.Trim(',');
 1793:                         }
 1794:                         */
 1795:  
 1796:                         // below: this function will remove values that were not present in the reading
 1797:                         if (serviceRequestList.Count > 0)
 1798:                         {
 1799:                             foreach (Ia.Ftn.Cl.Models.ServiceRequest sr in serviceRequestList)
 1800:                             {
 1801:                                 if (!newServiceRequestIdArrayList.Contains(sr.Id))
 1802:                                 {
 1803:                                     serviceRequest = (from sr2 in db.ServiceRequests
 1804:                                                       where sr2.Id == sr.Id
 1805:                                                       select sr2).SingleOrDefault();
 1806:  
 1807:                                     db.ServiceRequests.Remove(serviceRequest);
 1808:  
 1809:                                     // below: we will also remove SRT records referensing this SR
 1810:                                     serviceRequestTypeList = (from srt in db.ServiceRequestTypes
 1811:                                                               where srt.ServiceRequest.Id == sr.Id
 1812:                                                               select srt).ToList();
 1813:  
 1814:                                     foreach (Ia.Ftn.Cl.Models.ServiceRequestType srt in serviceRequestTypeList) db.ServiceRequestTypes.Remove(srt);
 1815:  
 1816:                                     insertedOrUpdatedOrDeletedServiceList.Add(serviceRequest.Number.ToString());
 1817:                                     deletedItemCount++;
 1818:                                 }
 1819:                             }
 1820:                         }
 1821:  
 1822:                         db.SaveChanges();
 1823:  
 1824:                         //if (insertedItemCount != 0 || updatedItemCount != 0 || deletedItemCount != 0) isUpdated = true;
 1825:                         //else isUpdated = false;
 1826:  
 1827:                         result = "(" + readItemCount + "/" + existingItemCount + "/" + insertedItemCount + "," + updatedItemCount + "," + deletedItemCount + ")" + r;
 1828:                     }
 1829:                 }
 1830:                 else
 1831:                 {
 1832:                     result = "(?/?/?: SQL in TableName is unmatched)";
 1833:                 }
 1834:             }
 1835:             else
 1836:             {
 1837:                 result = "(dataTable == null/?/?)";
 1838:             }
 1839:  
 1840:             return result;
 1841:         }
 1842:  
 1843:         ////////////////////////////////////////////////////////////////////////////
 1844:  
 1845:         /// <summary>
 1846:         ///
 1847:         /// </summary>
 1848:         public static void UpdateForServiceRequestWithOutputDataTableAndService(DataTable dataTable, string service, out bool isUpdated, out Ia.Cl.Models.Result result)
 1849:         {
 1850:             int serviceRequestId, readItemCount, existingItemCount, insertedItemCount, updatedItemCount, deletedItemCount;
 1851:             string sql, sqlService, r;
 1852:  
 1853:             int id, number, areaId, customerCategoryId, customerId, serial, serviceCategoryId, serviceId, statusId;
 1854:             double balance;
 1855:             string customerAddress, customerName;
 1856:             DateTime requestDateTime;
 1857:  
 1858:             ArrayList newServiceRequestIdArrayList;
 1859:             Match match;
 1860:  
 1861:             Ia.Ftn.Cl.Models.Business.ServiceAddress serviceAddress;
 1862:             Ia.Ftn.Cl.Models.ServiceRequest serviceRequest, newServiceRequest;
 1863:             List<int> numbersNotWithinAllowedDomainList;
 1864:             List<Ia.Ftn.Cl.Models.ServiceRequest> serviceRequestList;
 1865:             List<Ia.Ftn.Cl.Models.ServiceRequestType> serviceRequestTypeList;
 1866:  
 1867:             isUpdated = false;
 1868:             readItemCount = existingItemCount = insertedItemCount = updatedItemCount = deletedItemCount = 0;
 1869:             numbersNotWithinAllowedDomainList = new List<int>();
 1870:  
 1871:             result = new Ia.Cl.Models.Result();
 1872:  
 1873:             if (dataTable != null)
 1874:             {
 1875:                 // below: the SQL statement should be within the dataTable.TableName variable
 1876:                 sql = dataTable.TableName;
 1877:  
 1878:                 // select * from SRV_REQ_FIPER where SRV_NO = 23632222 order by SRV_REQ_ID asc
 1879:                 match = Regex.Match(sql, @"SRV_NO = (\d+) order by SRV_REQ_ID asc", RegexOptions.Singleline);
 1880:  
 1881:                 if (match.Success)
 1882:                 {
 1883:                     using (var db = new Ia.Ftn.Cl.Db())
 1884:                     {
 1885:                         readItemCount = dataTable.Rows.Count;
 1886:  
 1887:                         sqlService = match.Groups[1].Value;
 1888:  
 1889:                         if (service == sqlService)
 1890:                         {
 1891:                             serviceRequestList = Ia.Ftn.Cl.Models.Data.ServiceRequest.List(service);
 1892:                             existingItemCount = serviceRequestList.Count;
 1893:  
 1894:                             newServiceRequestIdArrayList = new ArrayList(dataTable.Rows.Count + 1);
 1895:  
 1896:                             foreach (DataRow dataRow in dataTable.Rows)
 1897:                             {
 1898:                                 if (int.TryParse(dataRow["SRV_NO"].ToString(), out number))
 1899:                                 {
 1900:                                     if (Ia.Ftn.Cl.Models.Business.Service.ServiceHasEightDigitsAndIsWithinAllowedDomainList(number))
 1901:                                     {
 1902:                                         serviceRequestId = int.Parse(dataRow["SRV_REQ_ID"].ToString());
 1903:                                         customerAddress = dataRow["ADDRESS"].ToString();
 1904:  
 1905:                                         customerCategoryId = int.TryParse(dataRow["CUST_CAT_ID"].ToString(), out int i) ? i : 0;
 1906:                                         customerId = int.TryParse(dataRow["ACCOUNT_NO"].ToString(), out i) ? i : 0;
 1907:                                         customerName = Ia.Ftn.Cl.Models.Business.Default.CorrectCustomerName(dataRow["NAME"].ToString());
 1908:                                         requestDateTime = DateTime.Parse(dataRow["REQ_DATE"].ToString());
 1909:                                         serial = int.TryParse(dataRow["SRV_SER_NO"].ToString(), out i) ? i : 0;
 1910:                                         serviceCategoryId = int.TryParse(dataRow["SRV_CAT_ID"].ToString(), out i) ? i : 0;
 1911:                                         serviceId = int.TryParse(dataRow["SRV_ID"].ToString(), out i) ? i : 0;
 1912:                                         balance = double.Parse(dataRow["BALANCE"].ToString());
 1913:                                         statusId = int.TryParse(dataRow["STATUS"].ToString(), out i) ? i : 0;
 1914:  
 1915:                                         if (Ia.Ftn.Cl.Models.Data.ServiceRequest.StatusSortedList.ContainsKey(statusId))
 1916:                                         {
 1917:                                             if (Ia.Ftn.Cl.Models.Data.ServiceRequest.CustomerCategorySortedList.ContainsKey(customerCategoryId))
 1918:                                             {
 1919:                                                 if (Ia.Ftn.Cl.Models.Data.ServiceRequest.ServiceCategorySortedList.ContainsKey(serviceCategoryId))
 1920:                                                 {
 1921:                                                     if (Ia.Ftn.Cl.Models.Data.ServiceRequest.ServiceSortedList.ContainsKey(serviceId))
 1922:                                                     {
 1923:                                                         serviceAddress = Ia.Ftn.Cl.Models.Business.ServiceRequest.ServiceAddress(number.ToString(), customerAddress, out string level);
 1924:  
 1925:                                                         newServiceRequest = new Ia.Ftn.Cl.Models.ServiceRequest()
 1926:                                                         {
 1927:                                                             Id = serviceRequestId,
 1928:                                                             Number = number,
 1929:                                                             CustomerAddress = customerAddress,
 1930:                                                             AreaId = serviceAddress.AreaId,
 1931:                                                             CustomerCategoryId = customerCategoryId,
 1932:                                                             CustomerId = customerId,
 1933:                                                             CustomerName = customerName,
 1934:                                                             RequestDateTime = requestDateTime,
 1935:                                                             Serial = serial,
 1936:                                                             ServiceCategoryId = serviceCategoryId,
 1937:                                                             ServiceId = serviceId,
 1938:                                                             Balance = balance,
 1939:                                                             Status = statusId
 1940:                                                         };
 1941:  
 1942:                                                         serviceRequest = (from sr in serviceRequestList
 1943:                                                                           where sr.Id == newServiceRequest.Id
 1944:                                                                           select sr).SingleOrDefault();
 1945:  
 1946:                                                         if (serviceRequest == null)
 1947:                                                         {
 1948:                                                             newServiceRequest.Created = newServiceRequest.Updated = DateTime.UtcNow.AddHours(3);
 1949:  
 1950:                                                             db.ServiceRequests.Add(newServiceRequest);
 1951:  
 1952:                                                             insertedItemCount++;
 1953:                                                         }
 1954:                                                         else
 1955:                                                         {
 1956:                                                             // below: copy values from newServiceRequest to serviceRequest
 1957:  
 1958:                                                             if (serviceRequest.UpdateSkipServiceRequestService(newServiceRequest))
 1959:                                                             {
 1960:                                                                 db.ServiceRequests.Attach(serviceRequest);
 1961:                                                                 db.Entry(serviceRequest).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
 1962:  
 1963:                                                                 updatedItemCount++;
 1964:                                                             }
 1965:                                                         }
 1966:  
 1967:                                                         newServiceRequestIdArrayList.Add(serviceRequestId); // keep at the end
 1968:                                                     }
 1969:                                                     else result.AddError("Service " + serviceId + " is undefined.");
 1970:                                                 }
 1971:                                                 else result.AddError("ServiceCategory " + serviceCategoryId + " is undefined.");
 1972:                                             }
 1973:                                             else result.AddError("CustomerCategory " + customerCategoryId + " is undefined.");
 1974:                                         }
 1975:                                         else result.AddError("Status " + statusId + " is undefined.");
 1976:                                     }
 1977:                                     else numbersNotWithinAllowedDomainList.Add(number);
 1978:                                 }
 1979:                                 else
 1980:                                 {
 1981:                                     //
 1982:                                 }
 1983:                             }
 1984:  
 1985:                             /*
 1986:                             if (numbersNotWithinAllowedDomainList.Count > 0)
 1987:                             {
 1988:                                 r = "Numbers not within allowed domain list: ";
 1989: 
 1990:                                 foreach (int n in numbersNotWithinAllowedDomainList) r += n + ",";
 1991: 
 1992:                                 r = r.Trim(',');
 1993:                             }
 1994:                             */
 1995:  
 1996:                             // below: this function will remove values that were not present in the reading
 1997:                             if (serviceRequestList.Count > 0)
 1998:                             {
 1999:                                 foreach (Ia.Ftn.Cl.Models.ServiceRequest sr in serviceRequestList)
 2000:                                 {
 2001:                                     if (!newServiceRequestIdArrayList.Contains(sr.Id))
 2002:                                     {
 2003:                                         serviceRequest = (from sr2 in db.ServiceRequests
 2004:                                                           where sr2.Id == sr.Id
 2005:                                                           select sr2).SingleOrDefault();
 2006:  
 2007:                                         db.ServiceRequests.Remove(serviceRequest);
 2008:  
 2009:                                         // below: we will also remove SRT records referencing this SR
 2010:                                         serviceRequestTypeList = (from srt in db.ServiceRequestTypes
 2011:                                                                   where srt.ServiceRequest.Id == sr.Id
 2012:                                                                   select srt).ToList();
 2013:  
 2014:                                         foreach (Ia.Ftn.Cl.Models.ServiceRequestType srt in serviceRequestTypeList) db.ServiceRequestTypes.Remove(srt);
 2015:  
 2016:                                         deletedItemCount++;
 2017:                                     }
 2018:                                 }
 2019:                             }
 2020:  
 2021:                             db.SaveChanges();
 2022:  
 2023:                             if (insertedItemCount != 0 || updatedItemCount != 0 || deletedItemCount != 0) isUpdated = true;
 2024:                             else isUpdated = false;
 2025:  
 2026:                             result.AddSuccess("(" + readItemCount + "/" + existingItemCount + "/" + insertedItemCount + "," + updatedItemCount + "," + deletedItemCount + ")");
 2027:                         }
 2028:                         else
 2029:                         {
 2030:                             throw new ArgumentException(@"UpdateForServiceRequestWithOutputDataTableAndService(): service != sqlService, service: " + service + ", sqlService: " + sqlService);
 2031:                         }
 2032:                     }
 2033:                 }
 2034:                 else result.AddError("(?/?/?: SQL in TableName is unmatched)");
 2035:             }
 2036:             else result.AddError("(dataTable == null/?/?)");
 2037:         }
 2038:  
 2039:         ////////////////////////////////////////////////////////////////////////////
 2040:  
 2041:         /// <summary>
 2042:         /// 
 2043:         /// </summary>
 2044:         public static Dictionary<string, Ia.Ftn.Cl.Models.ServiceRequest> ServiceToServiceRequestDictionary(List<int> domainList)
 2045:         {
 2046:             string key;
 2047:             List<string> stringDomainList;
 2048:             List<Ia.Ftn.Cl.Models.ServiceRequest> list;
 2049:             Dictionary<string, Ia.Ftn.Cl.Models.ServiceRequest> dictionary;
 2050:  
 2051:             stringDomainList = new List<string>();
 2052:  
 2053:             using (var db = new Ia.Ftn.Cl.Db())
 2054:             {
 2055:                 if (domainList.Count > 0)
 2056:                 {
 2057:                     foreach (int i in domainList) stringDomainList.Add(i.ToString());
 2058:  
 2059:                     list = (from sr in db.ServiceRequests.Include(a => a.ServiceRequestService).Include(a => a.ServiceRequestTypes) where domainList.Any(u => sr.Number.ToString().StartsWith(u.ToString())) orderby sr.RequestDateTime ascending select sr).ToList();
 2060:  
 2061:                     dictionary = new Dictionary<string, Ia.Ftn.Cl.Models.ServiceRequest>(list.Count);
 2062:  
 2063:                     foreach (var sr in list)
 2064:                     {
 2065:                         key = sr.Number.ToString();
 2066:  
 2067:                         if (dictionary.ContainsKey(key))
 2068:                         {
 2069:                             dictionary[key] = sr;
 2070:                         }
 2071:                         else dictionary[key] = sr;
 2072:                     }
 2073:                 }
 2074:                 else dictionary = new Dictionary<string, Ia.Ftn.Cl.Models.ServiceRequest>();
 2075:             }
 2076:  
 2077:             return dictionary;
 2078:         }
 2079:  
 2080:         ////////////////////////////////////////////////////////////////////////////
 2081:  
 2082:         /// <summary>
 2083:         ///
 2084:         /// </summary>
 2085:         public static Hashtable NumberToCustomerAddressHashtable(List<int> domainList)
 2086:         {
 2087:             Hashtable hashtable;
 2088:             List<Ia.Ftn.Cl.Models.ServiceRequest> serviceRequestList;
 2089:  
 2090:             using (var db = new Ia.Ftn.Cl.Db())
 2091:             {
 2092:                 if (domainList != null && domainList.Count > 0)
 2093:                 {
 2094:                     serviceRequestList = (from sr in db.ServiceRequests
 2095:                                           where domainList.Contains(sr.Number / 10000) || domainList.Contains(sr.Number / 1000)
 2096:                                           select sr).AsNoTracking().ToList();
 2097:  
 2098:                     hashtable = new Hashtable(serviceRequestList.Count);
 2099:  
 2100:                     foreach (Ia.Ftn.Cl.Models.ServiceRequest sr in serviceRequestList.OrderBy(u => u.Id)) hashtable[sr.Number.ToString()] = sr.CustomerAddress;
 2101:                 }
 2102:                 else hashtable = new Hashtable();
 2103:             }
 2104:  
 2105:             return hashtable;
 2106:         }
 2107:  
 2108:         ////////////////////////////////////////////////////////////////////////////
 2109:  
 2110:         /// <summary>
 2111:         ///
 2112:         /// </summary>
 2113:         public static DateTime LatestRequestDateTime(int number)
 2114:         {
 2115:             DateTime dateTime;
 2116:             Ia.Ftn.Cl.Models.ServiceRequest serviceRequest;
 2117:             List<Ia.Ftn.Cl.Models.ServiceRequest> serviceRequestList;
 2118:  
 2119:             using (var db = new Ia.Ftn.Cl.Db())
 2120:             {
 2121:                 serviceRequestList = Ia.Ftn.Cl.Models.Data.ServiceRequest.List(number);
 2122:  
 2123:                 if (serviceRequestList.Count > 0)
 2124:                 {
 2125:                     serviceRequest = serviceRequestList.OrderByDescending(u => u.Id).FirstOrDefault();
 2126:  
 2127:                     dateTime = serviceRequest.RequestDateTime;
 2128:                 }
 2129:                 else
 2130:                 {
 2131:                     dateTime = DateTime.MinValue;
 2132:                 }
 2133:             }
 2134:  
 2135:             return dateTime;
 2136:         }
 2137:  
 2138:         ////////////////////////////////////////////////////////////////////////////
 2139:  
 2140:         /// <summary>
 2141:         ///
 2142:         /// </summary>
 2143:         public static List<Ia.Ftn.Cl.Models.ServiceRequest> LastN(int numberOfServiceRequests)
 2144:         {
 2145:             // below:
 2146:             List<Ia.Ftn.Cl.Models.ServiceRequest> serviceRequestList;
 2147:  
 2148:             serviceRequestList = null;
 2149:  
 2150:             try
 2151:             {
 2152:                 using (var db = new Ia.Ftn.Cl.Db())
 2153:                 {
 2154:                     serviceRequestList = (from sr in db.ServiceRequests orderby sr.RequestDateTime descending select sr).Take(numberOfServiceRequests).ToList();
 2155:                 }
 2156:             }
 2157:             catch (Exception)
 2158:             {
 2159:                 //resultLabel.Text = "Error during retrieval of data for \"" + ip + "\": " + ex.Message + ". ";
 2160:                 //resultLabel.CssClass = "error";
 2161:             }
 2162:  
 2163:             return serviceRequestList;
 2164:         }
 2165:  
 2166:         ////////////////////////////////////////////////////////////////////////////
 2167:  
 2168:         /// <summary>
 2169:         ///
 2170:         /// </summary>
 2171:         public static List<Ia.Ftn.Cl.Models.ServiceRequest> ForRequestDate(DateTime requestDate)
 2172:         {
 2173:             DateTime nextDate;
 2174:             List<Ia.Ftn.Cl.Models.ServiceRequest> list;
 2175:  
 2176:             // below: 00:00 time values
 2177:             requestDate = requestDate.Date;
 2178:  
 2179:             nextDate = requestDate.AddDays(1);
 2180:  
 2181:             using (var db = new Ia.Ftn.Cl.Db())
 2182:             {
 2183:                 list = (from sr in db.ServiceRequests
 2184:                         where sr.RequestDateTime >= requestDate && sr.RequestDateTime < nextDate
 2185:                         orderby sr.RequestDateTime descending
 2186:                         select sr).AsNoTracking().ToList();
 2187:             }
 2188:  
 2189:             return list;
 2190:         }
 2191:  
 2192:         ////////////////////////////////////////////////////////////////////////////
 2193:         ////////////////////////////////////////////////////////////////////////////
 2194:  
 2195:         /// <summary>
 2196:         ///
 2197:         /// </summary>
 2198:         public static Dictionary<string, string> ServiceToAccessNameWithinAllowedToBeMigratedOltDictionary
 2199:         {
 2200:             get
 2201:             {
 2202:                 if (serviceToAccessNameWithinAllowedToBeMigratedOltDictionary == null || serviceToAccessNameWithinAllowedToBeMigratedOltDictionary.Count == 0)
 2203:                 {
 2204:                     lock (objectLock)
 2205:                     {
 2206:                         serviceToAccessNameWithinAllowedToBeMigratedOltDictionary = Ia.Ftn.Cl.Models.Data.ServiceRequest._ServiceToAccessNameWithinAllowedToBeMigratedOltDictionary;
 2207:                     }
 2208:                 }
 2209:  
 2210:                 return serviceToAccessNameWithinAllowedToBeMigratedOltDictionary;
 2211:             }
 2212:         }
 2213:  
 2214:         ////////////////////////////////////////////////////////////////////////////
 2215:  
 2216:         /// <summary>
 2217:         ///
 2218:         /// </summary>
 2219:         private static Dictionary<string, string> _ServiceToAccessNameWithinAllowedToBeMigratedOltDictionary
 2220:         {
 2221:             get
 2222:             {
 2223:                 Ia.Ftn.Cl.Models.Business.ServiceAddress serviceAddress;
 2224:                 Dictionary<string, Ia.Ftn.Cl.Models.Business.ServiceAddress> serviceToServiceAddressWithinAllowedToBeMigratedOltDictionary;
 2225:  
 2226:                 var areaSymbolList = Ia.Ftn.Cl.Models.Business.Service.AllowedToBeMigratedOltSymbolList;
 2227:  
 2228:                 var kuwaitFtnAreaList = (from k in Ia.Ftn.Cl.Models.Data.Service.KuwaitFtnAreaList
 2229:                                          where areaSymbolList.Contains(k.Symbol)
 2230:                                          select k.Id).ToList();
 2231:  
 2232:                 var serviceToDbNameWithinAllowedToBeMigratedOltDictionary = Ia.Ftn.Cl.Models.Data.ServiceRequest.ServiceToDbNameWithinAllowedToBeMigratedOltDictionary;
 2233:                 var serviceToCustomerAddressWithinAllowedToBeMigratedOltDictionary = Ia.Ftn.Cl.Models.Data.ServiceRequest.ServiceToCustomerAddressWithinAllowedToBeMigratedOltDictionary;
 2234:  
 2235:                 serviceToServiceAddressWithinAllowedToBeMigratedOltDictionary = new Dictionary<string, Ia.Ftn.Cl.Models.Business.ServiceAddress>(serviceToCustomerAddressWithinAllowedToBeMigratedOltDictionary.Count);
 2236:  
 2237:                 foreach (KeyValuePair<string, string> kvp in serviceToCustomerAddressWithinAllowedToBeMigratedOltDictionary)
 2238:                 {
 2239:                     var service = kvp.Key;
 2240:                     var address = kvp.Value;
 2241:                     serviceAddress = Ia.Ftn.Cl.Models.Business.ServiceRequest.ServiceAddress(service, address, out string level);
 2242:  
 2243:                     serviceToServiceAddressWithinAllowedToBeMigratedOltDictionary[service] = serviceAddress;
 2244:                 }
 2245:  
 2246:                 serviceToAccessNameWithinAllowedToBeMigratedOltDictionary = new Dictionary<string, string>(serviceToServiceAddressWithinAllowedToBeMigratedOltDictionary.Count);
 2247:                 var ontAccessIdToOntAccessNameWithinAllowedToBeMigratedOltDictionary = Ia.Ftn.Cl.Models.Data.NetworkDesignDocument.OntAccessIdToOntAccessNameWithinAllowedToBeMigratedOltDictionary;
 2248:  
 2249:                 using (var db = new Ia.Ftn.Cl.Db())
 2250:                 {
 2251:                     var accessList = (from a in db.Accesses
 2252:                                       where kuwaitFtnAreaList.Contains(a.AreaId)
 2253:                                       select new Ia.Ftn.Cl.Models.Ui.Access { Id = a.Id, AreaId = a.AreaId, Block = a.Block, Street = a.Street, PremisesOld = a.PremisesOld, PremisesNew = a.PremisesNew }).ToList();
 2254:  
 2255:                     foreach (var access in accessList)
 2256:                     {
 2257:                         if (ontAccessIdToOntAccessNameWithinAllowedToBeMigratedOltDictionary.ContainsKey(access.Id))
 2258:                         {
 2259:                             access.Name = ontAccessIdToOntAccessNameWithinAllowedToBeMigratedOltDictionary[access.Id];
 2260:                         }
 2261:                         else access.Name = string.Empty;
 2262:                     }
 2263:  
 2264:                     foreach (var kvp in serviceToServiceAddressWithinAllowedToBeMigratedOltDictionary)
 2265:                     {
 2266:                         serviceAddress = new Ia.Ftn.Cl.Models.Business.ServiceAddress();
 2267:  
 2268:                         serviceAddress.AreaId = kvp.Value.AreaId;
 2269:                         serviceAddress.Block = kvp.Value.Block;
 2270:                         serviceAddress.Street = kvp.Value.Street;
 2271:                         serviceAddress.Boulevard = kvp.Value.Boulevard;
 2272:                         serviceAddress.PremisesOld = kvp.Value.PremisesOld;
 2273:                         serviceAddress.PremisesNew = kvp.Value.PremisesNew;
 2274:                         serviceAddress.Paci = kvp.Value.Paci;
 2275:  
 2276:                         var statisticalAccess = Ia.Ftn.Cl.Models.Data.Access.StatisticalAccess(serviceAddress, ref accessList, out string note2);
 2277:                         if (statisticalAccess != null) statisticalAccess.Note = note2;
 2278:  
 2279:                         serviceToAccessNameWithinAllowedToBeMigratedOltDictionary[kvp.Key] = statisticalAccess != null ? statisticalAccess.Name : string.Empty;
 2280:                     }
 2281:                 }
 2282:  
 2283:                 return serviceToAccessNameWithinAllowedToBeMigratedOltDictionary;
 2284:             }
 2285:         }
 2286:  
 2287:         ////////////////////////////////////////////////////////////////////////////
 2288:  
 2289:         /// <summary>
 2290:         ///
 2291:         /// </summary>
 2292:         public static Dictionary<string, List<string>> AccessNameToServiceListWithinAllowedToBeMigratedOltDictionary
 2293:         {
 2294:             get
 2295:             {
 2296:                 DateTime now;
 2297:                 Ia.Ftn.Cl.Models.Business.Default.ValidityOfData validityOfData;
 2298:  
 2299:                 now = DateTime.UtcNow.AddHours(3);
 2300:                 validityOfData = Ia.Ftn.Cl.Models.Business.Default.ValidityOfData.TwelveHours;
 2301:  
 2302:                 if (accessNameToServiceListWithinAllowedToBeMigratedOltDictionary == null || accessNameToServiceListWithinAllowedToBeMigratedOltDictionary.Count == 0 || !Ia.Ftn.Cl.Models.Business.Default.DataIsValid(validityOfData, now, accessNameToServiceListWithinAllowedToBeMigratedOltDictionaryTimestamp))
 2303:                 {
 2304:                     lock (objectLock)
 2305:                     {
 2306:                         accessNameToServiceListWithinAllowedToBeMigratedOltDictionary = Ia.Ftn.Cl.Models.Data.ServiceRequest._AccessNameToServiceListWithinAllowedToBeMigratedOltDictionary();
 2307:                     }
 2308:                 }
 2309:  
 2310:                 return accessNameToServiceListWithinAllowedToBeMigratedOltDictionary;
 2311:             }
 2312:         }
 2313:  
 2314:         ////////////////////////////////////////////////////////////////////////////
 2315:  
 2316:         /// <summary>
 2317:         ///
 2318:         /// </summary>
 2319:         private static Dictionary<string, List<string>> _AccessNameToServiceListWithinAllowedToBeMigratedOltDictionary()
 2320:         {
 2321:             string key, value;
 2322:             List<string> valueList;
 2323:  
 2324:             var now = DateTime.UtcNow.AddHours(3);
 2325:  
 2326:             var serviceToAccessNameDictionary = ServiceToAccessNameWithinAllowedToBeMigratedOltDictionary;
 2327:  
 2328:             accessNameToServiceListWithinAllowedToBeMigratedOltDictionary = new Dictionary<string, List<string>>(serviceToAccessNameDictionary.Count);
 2329:  
 2330:             foreach (var kvp in serviceToAccessNameDictionary)
 2331:             {
 2332:                 key = kvp.Value;
 2333:  
 2334:                 if (!string.IsNullOrEmpty(key))
 2335:                 {
 2336:                     value = kvp.Key;
 2337:  
 2338:                     valueList = new List<string>();
 2339:  
 2340:                     if (!accessNameToServiceListWithinAllowedToBeMigratedOltDictionary.ContainsKey(key))
 2341:                     {
 2342:                         valueList.Add(value);
 2343:  
 2344:                         accessNameToServiceListWithinAllowedToBeMigratedOltDictionary[key] = valueList;
 2345:                     }
 2346:                     else
 2347:                     {
 2348:                         valueList = accessNameToServiceListWithinAllowedToBeMigratedOltDictionary[key];
 2349:                         valueList.Add(value);
 2350:  
 2351:                         accessNameToServiceListWithinAllowedToBeMigratedOltDictionary[key] = valueList;
 2352:                     }
 2353:                 }
 2354:             }
 2355:  
 2356:             accessNameToServiceListWithinAllowedToBeMigratedOltDictionaryTimestamp = now;
 2357:  
 2358:             return accessNameToServiceListWithinAllowedToBeMigratedOltDictionary;
 2359:         }
 2360:  
 2361:         ////////////////////////////////////////////////////////////////////////////
 2362:  
 2363:         /// <summary>
 2364:         ///
 2365:         /// </summary>
 2366:         public static List<string> DbPeerServiceList(List<string> serviceList)
 2367:         {
 2368:             List<string> list;
 2369:  
 2370:             if (serviceList.Count > 0)
 2371:             {
 2372:                 var dbNameToServiceListDictionary = Ia.Ftn.Cl.Models.Data.ServiceRequest.DbNameToServiceListDictionary;
 2373:  
 2374:                 var listList = (from d in dbNameToServiceListDictionary
 2375:                                 where serviceList.Any(u => d.Value.Contains(u))
 2376:                                 select d.Value).ToList();
 2377:  
 2378:                 var list0 = listList.SelectMany(x => x).ToList();
 2379:  
 2380:                 list = list0.Distinct().ToList();
 2381:             }
 2382:             else list = new List<string>();
 2383:  
 2384:             return list;
 2385:         }
 2386:  
 2387:         ////////////////////////////////////////////////////////////////////////////
 2388:  
 2389:         /// <summary>
 2390:         ///
 2391:         /// </summary>
 2392:         public static Dictionary<string, List<string>> DbNameToServiceListDictionary
 2393:         {
 2394:             get
 2395:             {
 2396:                 Ia.Ftn.Cl.Models.Business.Default.ValidityOfData validityOfData;
 2397:  
 2398:                 var dateTime = DateTime.UtcNow.AddHours(3);
 2399:                 validityOfData = Ia.Ftn.Cl.Models.Business.Default.ValidityOfData.TwelveHours;
 2400:  
 2401:                 if (dbNameToServiceListDictionary == null || dbNameToServiceListDictionary.Count == 0 || !Ia.Ftn.Cl.Models.Business.Default.DataIsValid(validityOfData, dateTime, dbNameToServiceListDictionaryTimestamp))
 2402:                 {
 2403:                     lock (objectLock)
 2404:                     {
 2405:                         dbNameToServiceListDictionary = Ia.Ftn.Cl.Models.Data.ServiceRequest._DbNameToServiceListDictionary();
 2406:                     }
 2407:                 }
 2408:  
 2409:                 return dbNameToServiceListDictionary;
 2410:             }
 2411:         }
 2412:  
 2413:         ////////////////////////////////////////////////////////////////////////////
 2414:  
 2415:         /// <summary>
 2416:         ///
 2417:         /// </summary>
 2418:         private static Dictionary<string, List<string>> _DbNameToServiceListDictionary()
 2419:         {
 2420:             string key, value;
 2421:             DateTime now;
 2422:  
 2423:             List<string> valueList;
 2424:  
 2425:             now = DateTime.UtcNow.AddHours(3);
 2426:  
 2427:             var serviceToDbNameDictionary = ServiceToDbNameWithinAllowedToBeMigratedOltDictionary;
 2428:  
 2429:             dbNameToServiceListDictionary = new Dictionary<string, List<string>>(serviceToDbNameDictionary.Count);
 2430:  
 2431:             foreach (KeyValuePair<string, string> kvp in serviceToDbNameDictionary)
 2432:             {
 2433:                 key = kvp.Value;
 2434:  
 2435:                 if (!string.IsNullOrEmpty(key) && key != ":")
 2436:                 {
 2437:                     value = kvp.Key;
 2438:  
 2439:                     valueList = new List<string>();
 2440:  
 2441:                     if (!dbNameToServiceListDictionary.ContainsKey(key))
 2442:                     {
 2443:                         valueList.Add(value);
 2444:  
 2445:                         dbNameToServiceListDictionary[key] = valueList;
 2446:                     }
 2447:                     else
 2448:                     {
 2449:                         valueList = dbNameToServiceListDictionary[key];
 2450:                         valueList.Add(value);
 2451:  
 2452:                         dbNameToServiceListDictionary[key] = valueList;
 2453:                     }
 2454:                 }
 2455:             }
 2456:  
 2457:             dbNameToServiceListDictionaryTimestamp = now;
 2458:  
 2459:             return dbNameToServiceListDictionary;
 2460:         }
 2461:  
 2462:         ////////////////////////////////////////////////////////////////////////////
 2463:  
 2464:         /// <summary>
 2465:         ///
 2466:         /// </summary>
 2467:         public static Dictionary<string, string> ServiceToDbNameWithinAllowedToBeMigratedOltDictionary
 2468:         {
 2469:             get
 2470:             {
 2471:                 DateTime now;
 2472:                 Ia.Ftn.Cl.Models.Business.Default.ValidityOfData validityOfData;
 2473:  
 2474:                 now = DateTime.UtcNow.AddHours(3);
 2475:                 validityOfData = Ia.Ftn.Cl.Models.Business.Default.ValidityOfData.TwelveHours;
 2476:  
 2477:                 if (serviceToDbNameDictionary == null || serviceToDbNameDictionary.Count == 0 || !Ia.Ftn.Cl.Models.Business.Default.DataIsValid(validityOfData, now, serviceToDbNameDictionaryAndServiceToCustomerAddressDictionaryTimestamp))
 2478:                 {
 2479:                     lock (objectLock)
 2480:                     {
 2481:                         Ia.Ftn.Cl.Models.Data.ServiceRequest._ServiceToDbNameDictionaryAndServiceToCustomerAddressDictionary(out serviceToDbNameDictionary, out serviceToCustomerAddressDictionary);
 2482:                     }
 2483:                 }
 2484:  
 2485:                 return serviceToDbNameDictionary;
 2486:             }
 2487:         }
 2488:  
 2489:         ////////////////////////////////////////////////////////////////////////////
 2490:  
 2491:         /// <summary>
 2492:         ///
 2493:         /// </summary>
 2494:         public static Dictionary<string, string> ServiceToCustomerAddressWithinAllowedToBeMigratedOltDictionary
 2495:         {
 2496:             get
 2497:             {
 2498:                 DateTime now;
 2499:                 Ia.Ftn.Cl.Models.Business.Default.ValidityOfData validityOfData;
 2500:  
 2501:                 now = DateTime.UtcNow.AddHours(3);
 2502:                 validityOfData = Ia.Ftn.Cl.Models.Business.Default.ValidityOfData.Day;
 2503:  
 2504:                 if (serviceToCustomerAddressDictionary == null || serviceToCustomerAddressDictionary.Count == 0 || !Ia.Ftn.Cl.Models.Business.Default.DataIsValid(validityOfData, now, serviceToDbNameDictionaryAndServiceToCustomerAddressDictionaryTimestamp))
 2505:                 {
 2506:                     lock (objectLock)
 2507:                     {
 2508:                         Ia.Ftn.Cl.Models.Data.ServiceRequest._ServiceToDbNameDictionaryAndServiceToCustomerAddressDictionary(out serviceToDbNameDictionary, out serviceToCustomerAddressDictionary);
 2509:                     }
 2510:                 }
 2511:  
 2512:                 return serviceToCustomerAddressDictionary;
 2513:             }
 2514:         }
 2515:  
 2516:         ////////////////////////////////////////////////////////////////////////////
 2517:  
 2518:         /// <summary>
 2519:         ///
 2520:         /// </summary>
 2521:         private static void _ServiceToDbNameDictionaryAndServiceToCustomerAddressDictionary(out Dictionary<string, string> serviceToDbNameDictionary, out Dictionary<string, string> serviceToCustomerAddressDictionary)
 2522:         {
 2523:             Dictionary<string, int> serviceToLastSerialDictionary;
 2524:  
 2525:             var areaSymbolList = Ia.Ftn.Cl.Models.Business.Service.AllowedToBeMigratedOltSymbolList;
 2526:  
 2527:             var areaIdList = (from k in Ia.Ftn.Cl.Models.Data.Service.KuwaitFtnAreaList
 2528:                               where areaSymbolList.Contains(k.Symbol)
 2529:                               select k.Id).ToList();
 2530:  
 2531:             using (var db = new Ia.Ftn.Cl.Db())
 2532:             {
 2533:                 var statisticalVariableList = (from sr in db.ServiceRequests
 2534:                                                    //join srs in db.ServiceRequestServices on sr.Number.ToString() equals srs.Service // on sr.ServiceRequestService.Id equals srs.Id
 2535:                                                join srt in db.ServiceRequestTypes on sr.Id equals srt.ServiceRequest.Id into srts
 2536:                                                from srt in srts.DefaultIfEmpty()
 2537:                                                where areaIdList.Contains(sr.AreaId) && (srt == null || srt.TypeId == 1 || srt.TypeId == 2)
 2538:                                                select new Ia.Ftn.Cl.Models.Business.ServiceRequestStatisticalVariable
 2539:                                                {
 2540:                                                    //Provisioned = srs.Provisioned,
 2541:                                                    ServiceRequestId = sr.Id,
 2542:                                                    Service = sr.Number.ToString(),
 2543:                                                    Serial = sr.Serial,
 2544:                                                    AreaId = sr.AreaId,
 2545:                                                    CustomerAddress = sr.CustomerAddress,
 2546:                                                    /* CustomerName = sr.CustomerName, */
 2547:                                                    ServiceRequestTypeValue = srt == null ? string.Empty : srt.Value,
 2548:                                                }).ToList();
 2549:  
 2550:                 serviceToLastSerialDictionary = new Dictionary<string, int>(statisticalVariableList.Count);
 2551:                 serviceToDbNameDictionary = new Dictionary<string, string>(statisticalVariableList.Count);
 2552:                 serviceToCustomerAddressDictionary = new Dictionary<string, string>(statisticalVariableList.Count);
 2553:  
 2554:                 foreach (var sv in statisticalVariableList/*.Where(u => u.Provisioned)*/.OrderBy(u => u.ServiceRequestId))
 2555:                 {
 2556:                     serviceToLastSerialDictionary[sv.Service] = sv.Serial;
 2557:                 }
 2558:  
 2559:                 foreach (var sv in statisticalVariableList/*.Where(u => u.Provisioned)*/.OrderBy(u => u.ServiceRequestId))
 2560:                 {
 2561:                     if (serviceToLastSerialDictionary[sv.Service] == sv.Serial)
 2562:                     {
 2563:                         if (serviceToDbNameDictionary.ContainsKey(sv.Service))
 2564:                         {
 2565:                             // ServiceRequestTypes values do not contain ":"
 2566:                             if (!serviceToDbNameDictionary[sv.Service].Contains(":")) serviceToDbNameDictionary[sv.Service] += ":" + sv.ServiceRequestTypeValue;
 2567:                         }
 2568:                         else serviceToDbNameDictionary[sv.Service] = sv.ServiceRequestTypeValue;
 2569:  
 2570:                         if (!serviceToCustomerAddressDictionary.ContainsKey(sv.Service)) serviceToCustomerAddressDictionary[sv.Service] = sv.CustomerAddress;
 2571:                     }
 2572:                 }
 2573:             }
 2574:         }
 2575:  
 2576:         ////////////////////////////////////////////////////////////////////////////
 2577:         ////////////////////////////////////////////////////////////////////////////
 2578:  
 2579:         /// <summary>
 2580:         ///
 2581:         /// </summary>
 2582:         public static List<int> ServiceRequestDomainList
 2583:         {
 2584:             get
 2585:             {
 2586:                 if (serviceRequestDomainList == null || serviceRequestDomainList.Count == 0)
 2587:                 {
 2588:                     lock (objectLock)
 2589:                     {
 2590:                         serviceRequestDomainList = Ia.Ftn.Cl.Models.Data.ServiceRequest._ServiceRequestDomainList;
 2591:                     }
 2592:                 }
 2593:  
 2594:                 return serviceRequestDomainList;
 2595:             }
 2596:         }
 2597:  
 2598:         ////////////////////////////////////////////////////////////////////////////
 2599:  
 2600:         /// <summary>
 2601:         ///
 2602:         /// </summary>
 2603:         private static List<int> _ServiceRequestDomainList
 2604:         {
 2605:             get
 2606:             {
 2607:                 using (var db = new Ia.Ftn.Cl.Db())
 2608:                 {
 2609:                     var fourDigitList = (from sr in db.ServiceRequests select sr.Number.ToString().Substring(0, 4)).Distinct().ToList();
 2610:                     var fiveDigitList = (from sr in db.ServiceRequests select sr.Number.ToString().Substring(0, 5)).Distinct().ToList();
 2611:  
 2612:                     var list = new List<string>();
 2613:  
 2614:                     foreach (var fourDigit in fourDigitList)
 2615:                     {
 2616:                         if (
 2617:                             fiveDigitList.Any(u => u == fourDigit + "0")
 2618:                             && fiveDigitList.Any(u => u == fourDigit + "1")
 2619:                             && fiveDigitList.Any(u => u == fourDigit + "2")
 2620:                             && fiveDigitList.Any(u => u == fourDigit + "3")
 2621:                             && fiveDigitList.Any(u => u == fourDigit + "4")
 2622:                             && fiveDigitList.Any(u => u == fourDigit + "5")
 2623:                             && fiveDigitList.Any(u => u == fourDigit + "6")
 2624:                             && fiveDigitList.Any(u => u == fourDigit + "7")
 2625:                             && fiveDigitList.Any(u => u == fourDigit + "8")
 2626:                             && fiveDigitList.Any(u => u == fourDigit + "9")
 2627:                             ) list.Add(fourDigit);
 2628:                         else
 2629:                         {
 2630:                             foreach (var fiveDigit in fiveDigitList)
 2631:                             {
 2632:                                 if (fiveDigit.StartsWith(fourDigit)) list.Add(fiveDigit);
 2633:                             }
 2634:                         }
 2635:                     }
 2636:  
 2637:                     serviceRequestDomainList = list.Select(int.Parse).ToList();
 2638:  
 2639:                     serviceRequestDomainList.Sort();
 2640:                 }
 2641:  
 2642:                 return serviceRequestDomainList;
 2643:             }
 2644:         }
 2645:  
 2646:         ////////////////////////////////////////////////////////////////////////////
 2647:  
 2648:         /// <summary>
 2649:         ///
 2650:         /// </summary>
 2651:         public static Dictionary<DateTime, int> RequestDateTimeWeekToCountDictionary()
 2652:         {
 2653:             var dictionary = new Dictionary<DateTime, int>();
 2654:  
 2655:             using (var db = new Ia.Ftn.Cl.Db())
 2656:             {
 2657:                 // too complicated
 2658:                 /*
 2659:                 dictionary = (from sr in db.ServiceRequests
 2660:                               group sr by SqlFunctions.DateAdd("ww", SqlFunctions.DateDiff("ww", DateTime.MinValue, sr.RequestDateTime), DateTime.MinValue) into sr1
 2661:                               select new { RequestDateTime = sr1.Key }).ToList();//, ServiceCount = sr1.Count() }).AsNoTracking().ToDictionary(t => t.RequestDateTime, t => t.ServiceCount);
 2662:                 */
 2663:             }
 2664:  
 2665:             return dictionary;
 2666:         }
 2667:  
 2668:         ////////////////////////////////////////////////////////////////////////////
 2669:         ////////////////////////////////////////////////////////////////////////////
 2670:  
 2671:         /// <summary>
 2672:         ///
 2673:         /// </summary>
 2674:         public static string ToSimpleTextString(Ia.Ftn.Cl.Models.ServiceRequest serviceRequest)
 2675:         {
 2676:             StringBuilder sb;
 2677:  
 2678:             sb = new StringBuilder();
 2679:  
 2680:             // Id    Number    Serial    Status    RequestDateTime    Service    ServiceCategory    CustomerId    CustomerName    CustomerCategory    CustomerAddress    Balance
 2681:             sb.AppendLine("Id: " + serviceRequest.Id);
 2682:             sb.AppendLine("Number: " + serviceRequest.Number + "/" + serviceRequest.Serial);
 2683:             //sb.AppendLine("Serial: " + serviceRequest.Serial);
 2684:             sb.AppendLine("Status: " + Ia.Ftn.Cl.Models.Data.ServiceRequest.StatusSortedList[serviceRequest.Status].ToString());
 2685:             sb.AppendLine("RequestDateTime: " + serviceRequest.RequestDateTime.ToString("yyyy-MM-dd HH:mm"));
 2686:             sb.AppendLine("Service: " + Ia.Ftn.Cl.Models.Data.ServiceRequest.ServiceSortedList[serviceRequest.ServiceId].ToString());
 2687:             //sb.AppendLine("ServiceCategory: " + Ia.Ftn.Cl.Model.Data.ServiceRequest.ServiceCategorySortedList[serviceRequest.ServiceCategoryId].ToString());
 2688:             //sb.AppendLine("CustomerId: " + serviceRequest.CustomerId);
 2689:             sb.AppendLine("CustomerName: " + serviceRequest.CustomerName);
 2690:             //sb.AppendLine("CustomerCategory: " + Ia.Ftn.Cl.Model.Data.ServiceRequest.CustomerCategorySortedList[serviceRequest.CustomerCategoryId].ToString());
 2691:  
 2692:             sb.AppendLine("CustomerAddress: " + serviceRequest.CustomerAddress);
 2693:             //sb.AppendLine("Balance: " + serviceRequest.Balance);
 2694:  
 2695:             return sb.ToString();
 2696:         }
 2697:  
 2698:         ////////////////////////////////////////////////////////////////////////////
 2699:  
 2700:         /// <summary>
 2701:         /// 
 2702:         /// How to embed and access resources by using Visual C# http://support.microsoft.com/kb/319292/en-us
 2703:         /// 
 2704:         /// 1. Change the "Build Action" property of your XML file from "Content" to "Embedded Resource".
 2705:         /// 2. Add "using System.Reflection".
 2706:         /// 3. Manifest resource stream will start with the project namespace, the location of XML file.
 2707:         /// 
 2708:         /// </summary>
 2709:  
 2710:         private static XDocument XDocument
 2711:         {
 2712:             get
 2713:             {
 2714:                 if (xDocument == null)
 2715:                 {
 2716:                     lock (objectLock)
 2717:                     {
 2718:                         Assembly assembly;
 2719:                         StreamReader streamReader;
 2720:  
 2721:                         assembly = Assembly.GetExecutingAssembly();
 2722:                         streamReader = new StreamReader(assembly.GetManifestResourceStream("Ia.Ftn.Cl.Models.Data.service-request.xml"));
 2723:  
 2724:                         try
 2725:                         {
 2726:                             if (streamReader.Peek() != -1)
 2727:                             {
 2728:                                 xDocument = System.Xml.Linq.XDocument.Load(streamReader);
 2729:                             }
 2730:                         }
 2731:                         catch (Exception)
 2732:                         {
 2733:                         }
 2734:                         finally
 2735:                         {
 2736:                         }
 2737:                     }
 2738:                 }
 2739:  
 2740:                 return xDocument;
 2741:             }
 2742:         }
 2743:  
 2744:         ////////////////////////////////////////////////////////////////////////////    
 2745:         ////////////////////////////////////////////////////////////////////////////    
 2746:     }
 2747:  
 2748:     ////////////////////////////////////////////////////////////////////////////
 2749:     ////////////////////////////////////////////////////////////////////////////
 2750: }