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