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

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

    1: using Microsoft.AspNetCore.Mvc.Rendering;
    2: using Microsoft.Identity.Client;
    3: using System.Data;
    4: using System.Net;
    5: using System.Text.RegularExpressions;
    6:  
    7: namespace Ia.Ftn.Wa.Models.Maintenance
    8: {
    9:     ////////////////////////////////////////////////////////////////////////////
   10:  
   11:     /// <summary publish="true">
   12:     ///
   13:     /// </summary>
   14:     /// 
   15:     /// <remarks> 
   16:     /// Copyright © 2006-2025 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   17:     ///
   18:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   19:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   20:     ///
   21:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   22:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   23:     /// 
   24:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   25:     /// 
   26:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   27:     /// </remarks> 
   28:     public static class Report
   29:     {
   30:         ////////////////////////////////////////////////////////////////////////////
   31:  
   32:         /// <summary>
   33:         ///
   34:         /// </summary>
   35:         public static void ViewModel(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel)
   36:         {
   37:             if (!string.IsNullOrEmpty(reportViewModel.FrameworkSelectedValue))
   38:             {
   39:                 ReportListAndFrameworkSelectList_ViewModel(ref reportViewModel, reportViewModel.FrameworkSelectedValue);
   40:             }
   41:             else if (reportViewModel.CurrentStaffIdentityUser.Framework != null) ReportListAndFrameworkSelectList_ViewModel(ref reportViewModel, reportViewModel.CurrentStaffIdentityUser.Framework.Id);
   42:             else
   43:             {
   44:             }
   45:  
   46:             //Telegram_ViewModel();
   47:  
   48:             reportViewModel.TelegramChatPanelVisible = false;
   49:         }
   50:  
   51:         ////////////////////////////////////////////////////////////////////////////
   52:  
   53:         /// <summary>
   54:         ///
   55:         /// </summary>
   56:         public static void Post(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel, string commandName, string _reportId)
   57:         {
   58:             if (!string.IsNullOrEmpty(commandName) && !string.IsNullOrEmpty(_reportId))
   59:             {
   60:                 if (commandName == "delete")
   61:                 {
   62:                     Ia.Ftn.Wa.Models.Maintenance.Report.DeleteReport(ref reportViewModel, _reportId);
   63:                 }
   64:             }
   65:         }
   66:  
   67:         ////////////////////////////////////////////////////////////////////////////
   68:  
   69:         /// <summary>
   70:         ///
   71:         /// </summary>
   72:         public static void DeleteReport(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel, string _reportId)
   73:         {
   74:             reportViewModel.UpdateResult = new Ia.Cl.Models.Result();
   75:  
   76:             var reportId = int.Parse(_reportId);
   77:  
   78:             var report = Ia.Ftn.Cl.Models.Data.Report.Read(reportId); // refresh report
   79:  
   80:             if (report != null)
   81:             {
   82:                 if (Ia.Ftn.Cl.Models.Business.Authority.StaffCanCreateReadUpdateDeleteReport(Ia.Ftn.Cl.Models.Business.Authority.PersistentStorageFunction.Delete, report, reportViewModel.CurrentStaffIdentityUser))
   83:                 {
   84:                     var b = Ia.Ftn.Cl.Models.Data.Report.Delete(reportId);
   85:  
   86:                     if (b)
   87:                     {
   88:                         reportViewModel.UpdateResult.AddSuccess("Report successfully deleted (مسح التقرير بنجاح)");
   89:                     }
   90:                     else throw new Exception("") { };
   91:                 }
   92:                 else
   93:                 {
   94:                     reportViewModel.UpdateResult.AddError("You are not authorized to modify this report (لست مخولاً بتعديل هذا التقرير)");
   95:                 }
   96:             }
   97:             else
   98:             {
   99:                 reportViewModel.UpdateResult.AddError("Report " + reportId + " does not exist (التقرير غير موجود).");
  100:             }
  101:         }
  102:  
  103:         ////////////////////////////////////////////////////////////////////////////
  104:  
  105:         /// <summary>
  106:         ///
  107:         /// </summary>
  108:         private static void ReportListAndFrameworkSelectList_ViewModel(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel, string frameworkId)
  109:         {
  110:             bool has;
  111:  
  112:             has = Ia.Ftn.Cl.Models.Business.Report.FrameworkHasReadabilityReport(frameworkId);
  113:  
  114:             if (has)
  115:             {
  116:                 var list = Ia.Ftn.Cl.Models.Data.Report.ReportReadabilityByFrameworkIdDictionary();
  117:  
  118:                 var reportIdList = list.ContainsKey(frameworkId) ? list[frameworkId] : new List<int>();
  119:  
  120:                 reportViewModel.ReportList = Ia.Ftn.Cl.Models.Data.Report.OpenStatusOrClosedStatusWithinLast24HourByReportIdReportList(reportIdList);
  121:  
  122:                 reportViewModel.StatisticLabel = reportViewModel.ReportList.Count.ToString();
  123:             }
  124:             else reportViewModel.StatisticLabel = string.Empty;
  125:  
  126:             FrameworkSelectList_ViewModel(ref reportViewModel, frameworkId);
  127:         }
  128:  
  129:         ////////////////////////////////////////////////////////////////////////////
  130:  
  131:         /// <summary>
  132:         ///
  133:         /// </summary>
  134:         private static void FrameworkSelectList_ViewModel(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel, string frameworkId)
  135:         {
  136:             var list = Ia.Ftn.Cl.Models.Data.Report.ReportReadabilityByFrameworkList().OrderBy(f => f.FullyQualifiedArabicName);
  137:  
  138:             var list1 = new List<SelectListItem>();
  139:             //list1.Add(new SelectListItem(" ", "0"));
  140:  
  141:             foreach (var l in list) list1.Add(new SelectListItem(l.FullyQualifiedArabicName, l.Id));
  142:  
  143:             reportViewModel.FrameworkSelectList = new SelectList(list1, "Value", "Text");
  144:  
  145:             if (string.IsNullOrEmpty(frameworkId)) reportViewModel.FrameworkSelectedValue = "0";
  146:             else reportViewModel.FrameworkSelectedValue = frameworkId;
  147:         }
  148:  
  149:         ////////////////////////////////////////////////////////////////////////////
  150:  
  151:         /// <summary>
  152:         ///
  153:         /// </summary>
  154:         private static void Telegram_ViewModel(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel)
  155:         {
  156:             Ia.Cl.Models.QrCode qrCode;
  157:  
  158:             var currentStaffIdentityUser = reportViewModel.CurrentStaffIdentityUser;
  159:  
  160:             //if (Ia.Ftn.Cl.Models.Business.Authority.StaffIsSuperUser(staff))
  161:             //{
  162:             var chat = (from c in Ia.Ftn.Cl.Models.Telegram.Chat.ChatList
  163:                         where c.Framework != null && (c.Framework == currentStaffIdentityUser.Framework || currentStaffIdentityUser.Framework.Parent != null && c.Framework == currentStaffIdentityUser.Framework.Parent)
  164:                         select c).FirstOrDefault();
  165:  
  166:             if (chat != null)
  167:             {
  168:                 reportViewModel.TelegramGroupInviteQrCodeText = chat.Name;
  169:                 reportViewModel.TelegramGroupInviteQrCodeNavigateUrl = chat.TelegramGroupUrl;
  170:                 reportViewModel.TelegramGroupInviteQrCodeImageUrl = chat.LogoImageUrl;
  171:  
  172:                 qrCode = new Ia.Cl.Models.QrCode(chat.InviteLinkUrl, "SlateGray", "White", chat.LogoImageAbsolutePathUrl);
  173:  
  174:                 //telegramGroupInviteQrCodeImage.AlternateText = chat.Name;
  175:                 reportViewModel.TelegramGroupInviteQrCodeImageUrl = qrCode.ImageUrlDataFormat;
  176:  
  177:                 reportViewModel.TelegramChatPanelVisible = true;
  178:             }
  179:             else reportViewModel.TelegramChatPanelVisible = false;
  180:             //}
  181:             //else reportViewModel.TelegramChatPanelVisible = false;
  182:         }
  183:  
  184:         ////////////////////////////////////////////////////////////////////////////
  185:         ////////////////////////////////////////////////////////////////////////////
  186:  
  187:  
  188:  
  189:  
  190:  
  191:  
  192:  
  193:  
  194:  
  195:  
  196:  
  197:         ////////////////////////////////////////////////////////////////////////////
  198:         ////////////////////////////////////////////////////////////////////////////
  199:  
  200:         /// <summary>
  201:         ///
  202:         /// </summary>
  203:         public static void HistoryViewModel(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel, string _reportyId)
  204:         {
  205:             bool otherReportsExist;
  206:             List<Ia.Ftn.Cl.Models.Report> reportList;
  207:  
  208:             reportViewModel.InformationResult = new Ia.Cl.Models.Result();
  209:  
  210:             var reportId = int.Parse(_reportyId);
  211:  
  212:             var report = Ia.Ftn.Cl.Models.Data.Report.Read(reportId);
  213:  
  214:             if (report != null)
  215:             {
  216:                 // below: search for all available reports for this dn and display them as link
  217:                 reportList = Ia.Ftn.Cl.Models.Data.Report.ListByReportId(reportId);
  218:  
  219:                 otherReportsExist = false;
  220:  
  221:                 reportViewModel.ReportLabel = string.Empty;
  222:  
  223:                 foreach (var report0 in reportList)
  224:                 {
  225:                     // below: we will check if there are open reports
  226:  
  227:                     reportViewModel.ReportLabel += "<a href=\"" + "history?reportId=" + report0.Id + "\">" + report0.Created.ToString("yyyy-MM-dd") + "</a> ";
  228:                     otherReportsExist = true;
  229:                 }
  230:  
  231:                 if (!otherReportsExist) reportViewModel.ReportLabel = "None (لا يوجد)";
  232:             }
  233:             else
  234:             {
  235:                 if (reportViewModel.ReportId > -1)
  236:                 {
  237:                     reportId = reportViewModel.ReportId;
  238:  
  239:                     report = Ia.Ftn.Cl.Models.Data.Report.Read(reportId);
  240:                 }
  241:                 else reportViewModel.InformationResult.AddWarning("Report " + reportId + " does not exist (التقرير غير موجود)");
  242:             }
  243:  
  244:             // below: if the report status is closed we will hide the disable panel
  245:             if (report.StatusIsOpen)
  246:             {
  247:                 reportViewModel.ReportStatusIsOpen = true;
  248:                 reportViewModel.InsertButtonEnabled = true;
  249:                 reportViewModel.ReopenClosedReportButtonEnabled = false;
  250:  
  251:                 if (Ia.Ftn.Cl.Models.Business.Authority.StaffCanCloseReport(report, reportViewModel.CurrentStaffIdentityUser)) reportViewModel.CloseReportCheckBoxEnabled = true;
  252:                 else reportViewModel.CloseReportCheckBoxEnabled = false;
  253:             }
  254:             else
  255:             {
  256:                 reportViewModel.ReportStatusIsOpen = false;
  257:                 reportViewModel.InsertButtonEnabled = false;
  258:  
  259:                 if (Ia.Ftn.Cl.Models.Business.Authority.StaffCanReopenClosedReport(report, reportViewModel.CurrentStaffIdentityUser)) reportViewModel.ReopenClosedReportButtonEnabled = true;
  260:                 else reportViewModel.ReopenClosedReportButtonEnabled = false;
  261:  
  262:                 reportViewModel.InformationResult.AddWarning("Report is Closed (التقرير مغلق)");
  263:             }
  264:  
  265:             StaffFrameworkAssignmentSelectList_ViewModel(ref reportViewModel, report);
  266:  
  267:             IndicationSelectList_ViewModel(ref reportViewModel, report);
  268:             ActionSelectList_ViewModel(ref reportViewModel, report);
  269:             ResolutionSelectList_ViewModel(ref reportViewModel);
  270:             EstimateSelectList_ViewModel(ref reportViewModel);
  271:  
  272:             ReportSelectList_ViewModel(ref reportViewModel);
  273:             ReportHistorySelectList_ViewModel(ref reportViewModel, report);
  274:         }
  275:  
  276:         ////////////////////////////////////////////////////////////////////////////
  277:  
  278:         /// <summary>
  279:         ///
  280:         /// </summary>
  281:         public static void HistoryPost(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel, string commandName, string reportHistoryId)
  282:         {
  283:             if (!string.IsNullOrEmpty(commandName))
  284:             {
  285:                 if (commandName == "delete")
  286:                 {
  287:                     if (!string.IsNullOrEmpty(reportHistoryId))
  288:                     {
  289:                         Ia.Ftn.Wa.Models.Maintenance.Report.DeleteHistoryReport(ref reportViewModel, reportHistoryId);
  290:                     }
  291:                     else
  292:                     {
  293:                     }
  294:                 }
  295:                 else if (commandName == "insert")
  296:                 {
  297:                     if (reportViewModel.ReportId > -1)
  298:                     {
  299:                         InsertReportHistory(ref reportViewModel);
  300:                     }
  301:                     else
  302:                     {
  303:                     }
  304:                 }
  305:                 else if (commandName == "reopenClosedReport")
  306:                 {
  307:                     if (reportViewModel.ReportId > -1)
  308:                     {
  309:                         ReopenClosedReport(ref reportViewModel);
  310:                     }
  311:                     else
  312:                     {
  313:                     }
  314:                 }
  315:                 else
  316:                 {
  317:                 }
  318:             }
  319:         }
  320:  
  321:         ////////////////////////////////////////////////////////////////////////////
  322:  
  323:         /// <summary>
  324:         ///
  325:         /// </summary>
  326:         public static void DeleteHistoryReport(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel, string _reportHistoryId)
  327:         {
  328:             reportViewModel.UpdateResult = new Ia.Cl.Models.Result();
  329:  
  330:             var reportHistoryId = int.Parse(_reportHistoryId);
  331:  
  332:             var report = Ia.Ftn.Cl.Models.Data.Report.Read(reportViewModel.ReportId);
  333:             var reportHistory = Ia.Ftn.Cl.Models.Data.ReportHistory.Read(reportHistoryId);
  334:  
  335:             if (reportHistory != null)
  336:             {
  337:                 if (Ia.Ftn.Cl.Models.Business.Authority.StaffCanCreateReadUpdateDeleteReportHistory(Ia.Ftn.Cl.Models.Business.Authority.PersistentStorageFunction.Delete, report, reportHistory, reportViewModel.CurrentStaffIdentityUser))
  338:                 {
  339:                     var b = Ia.Ftn.Cl.Models.Data.ReportHistory.Delete(reportHistoryId);
  340:  
  341:                     if (b)
  342:                     {
  343:                         reportViewModel.UpdateResult.AddSuccess("Report history successfully deleted (مسح التقرير بنجاح)");
  344:                     }
  345:                     else throw new Exception("") { };
  346:                 }
  347:                 else
  348:                 {
  349:                     reportViewModel.InsertButtonEnabled = false;
  350:  
  351:                     reportViewModel.UpdateResult.AddError("You are not authorized to modify this report history (لست مخولاً بتعديل هذا التقرير)");
  352:                 }
  353:             }
  354:             else
  355:             {
  356:                 reportViewModel.UpdateResult.AddError("Report history " + reportHistoryId + " does not exist (التقرير غير موجود).");
  357:             }
  358:         }
  359:  
  360:         ////////////////////////////////////////////////////////////////////////////
  361:  
  362:         /// <summary>
  363:         ///
  364:         /// </summary>
  365:         private static void InsertReportHistory(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel)
  366:         {
  367:             bool reportInserted, reportClosed, complainantNotified;
  368:             int area;
  369:             string result;
  370:             DateTime now;
  371:  
  372:             reportViewModel.InsertResult = new Ia.Cl.Models.Result();
  373:  
  374:             var reportId = reportViewModel.ReportId;
  375:  
  376:             var report = Ia.Ftn.Cl.Models.Data.Report.Read(reportId); // refresh report
  377:             Ia.Ftn.Cl.Models.ReportHistory reportHistory = null;
  378:  
  379:             if (!string.IsNullOrEmpty(reportViewModel.ResolutionSelectedValue))
  380:             {
  381:                 if (Ia.Ftn.Cl.Models.Business.Authority.StaffCanCreateReadUpdateDeleteReportHistory(Ia.Ftn.Cl.Models.Business.Authority.PersistentStorageFunction.Create, report, null, reportViewModel.CurrentStaffIdentityUser))
  382:                 {
  383:                     now = DateTime.UtcNow.AddHours(3);
  384:  
  385:                     area = 11; // <area id="11" name="Service"
  386:  
  387:                     var indication = int.Parse(reportViewModel.IndicationSelectedValue);
  388:                     var resolution = int.Parse(reportViewModel.ResolutionSelectedValue);
  389:                     var action = int.Parse(reportViewModel.ActionSelectedValue);
  390:                     var estimate = int.Parse(reportViewModel.EstimateSelectedValue);
  391:  
  392:                     var detail = WebUtility.HtmlDecode(reportViewModel.DetailTextArea);
  393:                     var note = WebUtility.HtmlDecode(reportViewModel.NoteTextArea);
  394:  
  395:                     // below: first check if the last resolution is an assignement and weather the users is rejecting it
  396:                     if (resolution == 1005)
  397:                     {
  398:                         if (report.LastReportHistory != null && report.LastReportHistory.Resolution == 1003)
  399:                         {
  400:                             /*
  401:                              * <resolution id="1002" name="Assign" arabicName="تعيين" insertForm="false"/>
  402:                              * <resolution id="1003" name="Assigned" arabicName="تم تعيين" insertForm="false"/>
  403:                              * <resolution id="1004" name="Accepted" arabicName="قُبِل التعيين" insertForm="false"/>
  404:                              * <resolution id="1005" name="Not Accepted" arabicName="رفض التعيين" insertForm="true"/>
  405:                              */
  406:  
  407:                             // below: the user refuses an assignement, code will look for assingment before last, which should be resolution=1002 and reverse it back to the assigner
  408:  
  409:                             var assigneeStaffIdentityUser = report.SecondToLastReportHistory.StaffIdentityUser;
  410:  
  411:                             // below: first say that this person rejects an assignment
  412:                             // below: <resolution id="1005" name="Not Accepted"
  413:                             resolution = 1005;
  414:                             reportHistory = new Ia.Ftn.Cl.Models.ReportHistory
  415:                             {
  416:                                 Report = Ia.Ftn.Cl.Models.Data.Report.Read(reportId),
  417:                                 Area = area,
  418:                                 Indication = indication,
  419:                                 Resolution = resolution,
  420:                                 Action = action,
  421:                                 Estimate = estimate,
  422:                                 Detail = detail,
  423:                                 StaffIdentityUser = reportViewModel.CurrentStaffIdentityUser,
  424:                                 Note = note,
  425:                                 Created = now,
  426:                                 Updated = now
  427:                             };
  428:  
  429:                             reportInserted = Ia.Ftn.Cl.Models.Data.ReportHistory.Create(reportHistory, out result);
  430:  
  431:                             if (reportInserted)
  432:                             {
  433:                                 // below: this user assigns to other user
  434:                                 // below: <resolution id="1002" name="Assign"
  435:                                 resolution = 1002;
  436:                                 reportHistory = new Ia.Ftn.Cl.Models.ReportHistory
  437:                                 {
  438:                                     Report = Ia.Ftn.Cl.Models.Data.Report.Read(reportId),
  439:                                     Area = area,
  440:                                     Indication = indication,
  441:                                     Resolution = resolution,
  442:                                     Action = action,
  443:                                     Estimate = estimate,
  444:                                     Detail = detail,
  445:                                     StaffIdentityUser = reportViewModel.CurrentStaffIdentityUser,
  446:                                     Note = note,
  447:                                     Created = now,
  448:                                     Updated = now
  449:                                 };
  450:  
  451:                                 reportInserted = Ia.Ftn.Cl.Models.Data.ReportHistory.Create(reportHistory, out result);
  452:  
  453:                                 if (reportInserted)
  454:                                 {
  455:                                     // below: <resolution id="1003" name="Assigned"
  456:                                     resolution = 1003;
  457:                                     reportHistory = new Ia.Ftn.Cl.Models.ReportHistory
  458:                                     {
  459:                                         Report = Ia.Ftn.Cl.Models.Data.Report.Read(reportId),
  460:                                         Area = area,
  461:                                         Indication = indication,
  462:                                         Resolution = resolution,
  463:                                         Action = action,
  464:                                         Estimate = estimate,
  465:                                         Detail = detail,
  466:                                         StaffIdentityUser = assigneeStaffIdentityUser,
  467:                                         Note = note,
  468:                                         Created = now,
  469:                                         Updated = now
  470:                                     };
  471:  
  472:                                     reportInserted = Ia.Ftn.Cl.Models.Data.ReportHistory.Create(reportHistory, out result);
  473:  
  474:                                     if (reportInserted)
  475:                                     {
  476:                                         if (bool.TryParse(reportViewModel.ComplainantNotifiedSelectedValue, out bool complainantNotifiedSelected))
  477:                                         {
  478:                                             complainantNotified = complainantNotifiedSelected && Ia.Ftn.Cl.Models.Data.ReportHistory.ComplainantNotified(reportHistory);
  479:                                         }
  480:                                         else
  481:                                         {
  482:                                         }
  483:  
  484:                                         reportClosed = reportViewModel.CloseReportCheckBoxEnabled && Ia.Ftn.Cl.Models.Data.Report.CloseStatus(report);
  485:  
  486:                                         if (reportClosed)
  487:                                         {
  488:                                             reportViewModel.InsertResult.AddSuccess("Report history successfully inserted and report closed (أدخلت حالة تقرير بنجاح وأغلق التقرير)");
  489:                                         }
  490:                                         else
  491:                                         {
  492:                                             reportViewModel.InsertResult.AddSuccess("Report history successfully inserted (أدخلت حالة تقرير بنجاح)");
  493:                                         }
  494:                                     }
  495:                                     else
  496:                                     {
  497:                                         reportViewModel.InsertResult.AddWarning("Two report histories successfully created, but the assignment acknowledgment (third) report was not successful (أدخل التقرير الأول والثاني بنجاح، لكن لم يدخل الثالث).");
  498:                                     }
  499:                                 }
  500:                                 else
  501:                                 {
  502:                                     reportViewModel.InsertResult.AddWarning("One report history successfully created, but the assignment (second) report was not successful (أدخل التقرير الأول بنجاح، لكن لم يدخل الثاني).");
  503:                                 }
  504:                             }
  505:                             else
  506:                             {
  507:                                 reportViewModel.InsertResult.AddWarning("One report history successfully created, but you can't assign to yourself (أدخل التقرير الأول بنجاح، لكن لا تستطيع التعيين لنفسك).");
  508:                             }
  509:                         }
  510:                         else
  511:                         {
  512:                             reportViewModel.InsertResult.AddError("No one assigned this report history to you (لم يتم التعيين لك)");
  513:                         }
  514:                     }
  515:                     else
  516:                     {
  517:                         reportHistory = new Ia.Ftn.Cl.Models.ReportHistory
  518:                         {
  519:                             Report = Ia.Ftn.Cl.Models.Data.Report.Read(reportId),
  520:                             Area = area,
  521:                             Indication = indication,
  522:                             Resolution = resolution,
  523:                             Action = action,
  524:                             Estimate = estimate,
  525:                             Detail = detail,
  526:                             StaffIdentityUser = reportViewModel.CurrentStaffIdentityUser,
  527:                             Note = note,
  528:                             Created = now,
  529:                             Updated = now
  530:                         };
  531:  
  532:                         reportInserted = Ia.Ftn.Cl.Models.Data.ReportHistory.Create(reportHistory, out result);
  533:  
  534:                         if (reportInserted)
  535:                         {
  536:                             if (!string.IsNullOrEmpty(reportViewModel.StaffFrameworkAssignmentSelectedValue))
  537:                             {
  538:                                 // below: when an assignment is issued two more records will be inserted
  539:                                 /*
  540:                                  * <resolution id="1002" name="Assign" arabicName="تعيين" insertForm="false"/>
  541:                                  * <resolution id="1003" name="Assigned" arabicName="تم تعيين" insertForm="false"/>
  542:                                  * <resolution id="1004" name="Accepted" arabicName="قُبِل التعيين" insertForm="false"/>
  543:                                  * <resolution id="1005" name="Not Accepted" arabicName="رفض التعيين" insertForm="true"/>
  544:                                  */
  545:  
  546:                                 bool isFrameworkId;
  547:                                 Ia.Ftn.Cl.Models.StaffIdentityUser assigneeStaffIdentityUser;
  548:  
  549:                                 var staffFrameworkId = reportViewModel.StaffFrameworkAssignmentSelectedValue;
  550:  
  551:                                 if (Ia.Ftn.Cl.Models.Business.Administration.IsFrameworkId(staffFrameworkId))
  552:                                 {
  553:                                     isFrameworkId = true;
  554:  
  555:                                     assigneeStaffIdentityUser = reportViewModel.CurrentStaffIdentityUser;
  556:                                 }
  557:                                 else
  558:                                 {
  559:                                     isFrameworkId = false;
  560:  
  561:                                     assigneeStaffIdentityUser = (from s in Ia.Ftn.Cl.Models.Data.StaffIdentityUser.List()
  562:                                                                  where s.Id == staffFrameworkId
  563:                                                                  select s).SingleOrDefault();
  564:                                 }
  565:  
  566:                                 // below: check if the assigner and assignee are the same
  567:                                 //if (!isFrameworkId && reportViewModel.CurrentStaffIdentityUser.Id != assigneeStaffIdentityUser.Id)
  568:                                 //{
  569:                                 resolution = 1002; // <resolution id="1002" name="Assign" arabicName="تعيين" insertForm="false"/>
  570:  
  571:                                 // below: then indicate the assigner
  572:                                 reportHistory = new Ia.Ftn.Cl.Models.ReportHistory
  573:                                 {
  574:                                     Report = Ia.Ftn.Cl.Models.Data.Report.Read(reportId),
  575:                                     Area = area,
  576:                                     Indication = indication,
  577:                                     Resolution = resolution,
  578:                                     Action = action,
  579:                                     Estimate = estimate,
  580:                                     Detail = detail,
  581:                                     StaffIdentityUser = reportViewModel.CurrentStaffIdentityUser,
  582:                                     Note = note,
  583:                                     Created = now,
  584:                                     Updated = now
  585:                                 };
  586:  
  587:                                 reportInserted = Ia.Ftn.Cl.Models.Data.ReportHistory.Create(reportHistory, out result);
  588:  
  589:                                 if (reportInserted)
  590:                                 {
  591:                                     // below: check if assignee is staff or framework
  592:  
  593:                                     if (isFrameworkId) resolution = 1016; // <resolution id="1016" name="Domain Assignment" arabicName="تعيين نطاق"
  594:                                     else resolution = 1003; // <resolution id="1003" name="Assigned" arabicName="تم تعيين"/>
  595:  
  596:                                     // below: then indicate the person, or the framework, that was assigned to
  597:                                     reportHistory = new Ia.Ftn.Cl.Models.ReportHistory
  598:                                     {
  599:                                         Report = Ia.Ftn.Cl.Models.Data.Report.Read(reportId),
  600:                                         Area = area,
  601:                                         Indication = indication,
  602:                                         Resolution = resolution,
  603:                                         Action = action,
  604:                                         Estimate = estimate,
  605:                                         Detail = detail,
  606:                                         StaffIdentityUser = assigneeStaffIdentityUser,
  607:                                         FrameworkId = staffFrameworkId,
  608:                                         Note = note,
  609:                                         Created = now,
  610:                                         Updated = now
  611:                                     };
  612:  
  613:                                     reportInserted = Ia.Ftn.Cl.Models.Data.ReportHistory.Create(reportHistory, out result);
  614:  
  615:                                     if (reportInserted)
  616:                                     {
  617:                                         if (bool.TryParse(reportViewModel.ComplainantNotifiedSelectedValue, out bool complainantNotifiedSelected))
  618:                                         {
  619:                                             complainantNotified = complainantNotifiedSelected && Ia.Ftn.Cl.Models.Data.ReportHistory.ComplainantNotified(reportHistory);
  620:                                         }
  621:                                         else
  622:                                         {
  623:                                         }
  624:  
  625:                                         reportClosed = reportViewModel.CloseReportCheckBoxEnabled && Ia.Ftn.Cl.Models.Data.Report.CloseStatus(report);
  626:  
  627:                                         if (reportClosed)
  628:                                         {
  629:                                             reportViewModel.InsertResult.AddSuccess("Report history successfully inserted and report closed (أدخلت حالة تقرير بنجاح وأغلق التقرير)");
  630:                                         }
  631:                                         else
  632:                                         {
  633:                                             reportViewModel.InsertResult.AddSuccess("Report history successfully inserted (أدخلت حالة تقرير بنجاح)");
  634:                                         }
  635:                                     }
  636:                                     else
  637:                                     {
  638:                                         reportViewModel.InsertResult.AddWarning("Two report histories successfully created, but the assignment acknowledgment (third) report was not successful (أدخل التقرير الأول والثاني بنجاح، لكن لم يدخل الثالث)");
  639:                                     }
  640:                                 }
  641:                                 else
  642:                                 {
  643:                                     reportViewModel.InsertResult.AddWarning("One report history successfully created, but the assignment (second) report was not successful (أدخل التقرير الأول بنجاح، لكن لم يدخل الثاني)");
  644:                                 }
  645:                                 //}
  646:                                 //else
  647:                                 //{
  648:                                 //    reportViewModel.InsertResult.AddWarning("One report history successfully created, but you can't assign to yourself (أدخل التقرير الأول بنجاح، لكن لا تستطيع التعيين لنفسك)");
  649:                                 //}
  650:  
  651:                                 // below: reset the assignment to nothing
  652:                                 reportViewModel.StaffFrameworkAssignmentSelectedValue = string.Empty;
  653:                             }
  654:                             else
  655:                             {
  656:                                 if (bool.TryParse(reportViewModel.ComplainantNotifiedSelectedValue, out bool complainantNotifiedSelected))
  657:                                 {
  658:                                     complainantNotified = complainantNotifiedSelected && Ia.Ftn.Cl.Models.Data.ReportHistory.ComplainantNotified(reportHistory);
  659:                                 }
  660:                                 else
  661:                                 {
  662:                                 }
  663:  
  664:                                 if (bool.TryParse(reportViewModel.CloseReportSelectedValue, out bool closeReportSelected))
  665:                                 {
  666:                                     reportClosed = closeReportSelected && Ia.Ftn.Cl.Models.Data.Report.CloseStatus(report);
  667:  
  668:                                     if (reportClosed)
  669:                                     {
  670:                                         reportViewModel.InsertResult.AddSuccess("Report history successfully inserted and report closed (أدخلت حالة تقرير بنجاح وأغلق التقرير)");
  671:                                     }
  672:                                     else
  673:                                     {
  674:                                         reportViewModel.InsertResult.AddSuccess("Report history successfully inserted (أدخلت حالة تقرير بنجاح)");
  675:                                     }
  676:                                 }
  677:                                 else
  678:                                 {
  679:                                 }
  680:                             }
  681:                         }
  682:                         else
  683:                         {
  684:                             reportViewModel.InsertResult.AddError("Report history not created (لم يدخل التقرير)");
  685:                         }
  686:                     }
  687:                 }
  688:                 else
  689:                 {
  690:                     reportViewModel.InsertButtonEnabled = false;
  691:  
  692:                     reportViewModel.InsertResult.AddError("You are not authorized to modify this report (لست مخولاً بتعديل هذا التقرير)");
  693:                 }
  694:             }
  695:             else
  696:             {
  697:                 reportViewModel.InsertResult.AddError("Missing resolution (لا نتيجة)");
  698:             }
  699:  
  700:             report = Ia.Ftn.Cl.Models.Data.Report.Read(reportId); // refresh report
  701:         }
  702:  
  703:         ////////////////////////////////////////////////////////////////////////////
  704:  
  705:         /// <summary>
  706:         ///
  707:         /// </summary>
  708:         private static void ReopenClosedReport(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel)
  709:         {
  710:             bool reportInserted, reportReopened;
  711:             int area, indication, resolution, action, estimate;
  712:             string detail, note;
  713:             DateTime now;
  714:  
  715:             reportViewModel.Result = new Ia.Cl.Models.Result();
  716:  
  717:             var reportId = reportViewModel.ReportId;
  718:  
  719:             var report = Ia.Ftn.Cl.Models.Data.Report.Read(reportId);
  720:             Ia.Ftn.Cl.Models.ReportHistory reportHistory = null;
  721:  
  722:             if (Ia.Ftn.Cl.Models.Business.Authority.StaffCanCreateReadUpdateDeleteReportHistory(Ia.Ftn.Cl.Models.Business.Authority.PersistentStorageFunction.Create, report, null, reportViewModel.CurrentStaffIdentityUser)
  723:             && Ia.Ftn.Cl.Models.Business.Authority.StaffCanReopenClosedReport(report, reportViewModel.CurrentStaffIdentityUser))
  724:             {
  725:                 now = DateTime.UtcNow.AddHours(3);
  726:  
  727:                 area = 11; // <area id="11" name="Service"
  728:  
  729:                 indication = 0;
  730:                 resolution = 1022; // <resolution id = "1022" name = "Reopened" arabicName = "إعادة فتح" 
  731:                 action = 0;
  732:                 estimate = 0;
  733:                 detail = string.Empty;
  734:                 note = string.Empty;
  735:  
  736:                 reportHistory = new Ia.Ftn.Cl.Models.ReportHistory
  737:                 {
  738:                     Report = Ia.Ftn.Cl.Models.Data.Report.Read(reportId),
  739:                     Area = area,
  740:                     Indication = indication,
  741:                     Resolution = resolution,
  742:                     Action = action,
  743:                     Estimate = estimate,
  744:                     Detail = detail,
  745:                     StaffIdentityUser = reportViewModel.CurrentStaffIdentityUser,
  746:                     Note = note,
  747:                     Created = now,
  748:                     Updated = now
  749:                 };
  750:  
  751:                 reportInserted = Ia.Ftn.Cl.Models.Data.ReportHistory.Create(reportHistory, out string result);
  752:  
  753:                 if (reportInserted)
  754:                 {
  755:                     reportReopened = Ia.Ftn.Cl.Models.Data.Report.OpenStatus(report);//, staff);
  756:  
  757:                     if (reportReopened)
  758:                     {
  759:                         reportViewModel.Result.AddSuccess("Report history successfully reopened (تم فتح التقرير بنجاح)");
  760:                     }
  761:                     else
  762:                     {
  763:                         reportViewModel.Result.AddError("Report history not reopened (لم يفتح التقرير)");
  764:                     }
  765:                 }
  766:                 else
  767:                 {
  768:                     reportViewModel.Result.AddError("Report history not created (لم يدخل التقرير)");
  769:                 }
  770:             }
  771:             else
  772:             {
  773:                 reportViewModel.InsertButtonEnabled = false;
  774:  
  775:                 reportViewModel.Result.AddError("You are not authorized to modify this report (لست مخولاً بتعديل هذا التقرير)");
  776:             }
  777:  
  778:             report = Ia.Ftn.Cl.Models.Data.Report.Read(reportId);
  779:         }
  780:  
  781:         ////////////////////////////////////////////////////////////////////////////
  782:  
  783:         /// <summary>
  784:         ///
  785:         /// </summary>
  786:         private static void ReportSelectList_ViewModel(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel)
  787:         {
  788:             reportViewModel.ReportList = Ia.Ftn.Cl.Models.Data.Report.ReadSingleAsList(reportViewModel.ReportId);
  789:         }
  790:  
  791:         ////////////////////////////////////////////////////////////////////////////
  792:  
  793:         /// <summary>
  794:         ///
  795:         /// </summary>
  796:         private static void StaffFrameworkAssignmentSelectList_ViewModel(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel, Ia.Ftn.Cl.Models.Report report)
  797:         {
  798:             var list = Ia.Ftn.Cl.Models.Business.Authority.StaffFrameworkListOfAllowedReportAssignsByStaff(report, reportViewModel.CurrentStaffIdentityUser);
  799:  
  800:             var list1 = new List<SelectListItem>();
  801:             list1.Add(new SelectListItem("", ""));
  802:  
  803:             foreach (var l in list) list1.Add(new SelectListItem(l.Name, l.Id));
  804:  
  805:             reportViewModel.StaffFrameworkAssignmentSelectList = new SelectList(list1, "Value", "Text");
  806:             reportViewModel.StaffFrameworkAssignmentSelectedValue = ""; // always default to none
  807:         }
  808:  
  809:         ////////////////////////////////////////////////////////////////////////////
  810:  
  811:         /// <summary>
  812:         ///
  813:         /// </summary>
  814:         private static void IndicationSelectList_ViewModel(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel, Ia.Ftn.Cl.Models.Report report)
  815:         {
  816:             var currentStaffIdentityUser = reportViewModel.CurrentStaffIdentityUser;
  817:  
  818:             var list = (from i in Ia.Ftn.Cl.Models.Data.Report.IndicationList
  819:                         where (i.Area != null && i.Area.Frameworks != null && (i.Area.Frameworks.Intersect(currentStaffIdentityUser.Framework.AncestorsOrSelf.Select(u => u.ArabicName)).Any() || i.Area.Frameworks.Intersect(currentStaffIdentityUser.Framework.AncestorsOrSelf.Select(u => u.FullyQualifiedArabicName)).Any())
  820:                         && i.Frameworks != null && (i.Frameworks.Intersect(currentStaffIdentityUser.Framework.AncestorsOrSelf.Select(u => u.ArabicName)).Any() || i.Frameworks.Intersect(currentStaffIdentityUser.Framework.AncestorsOrSelf.Select(u => u.FullyQualifiedArabicName)).Any())
  821:                         && !i.Obsolete && i.CanInsert) || i.Area == null
  822:                         select new
  823:                         {
  824:                             i.ColoredEnglishAndArabicName,
  825:                             i.XmlId
  826:                         }).Distinct().ToList();
  827:  
  828:             reportViewModel.IndicationSelectList = new SelectList(list, "XmlId", "ColoredEnglishAndArabicName");
  829:             reportViewModel.IndicationSelectedValue ??= "1000"; // <indication id="1000" name="Unspecified" arabicName="غير محدد"/>
  830:         }
  831:  
  832:         ////////////////////////////////////////////////////////////////////////////
  833:  
  834:         /// <summary>
  835:         ///
  836:         /// </summary>
  837:         private static void ActionSelectList_ViewModel(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel, Ia.Ftn.Cl.Models.Report report)
  838:         {
  839:             var currentStaffIdentityUser = reportViewModel.CurrentStaffIdentityUser;
  840:  
  841:             var list = (from a in Ia.Ftn.Cl.Models.Data.Report.ActionList
  842:                         where (a.Area != null && a.Area.Frameworks != null && (a.Area.Frameworks.Intersect(currentStaffIdentityUser.Framework.AncestorsOrSelf.Select(u => u.ArabicName)).Any() || a.Area.Frameworks.Intersect(currentStaffIdentityUser.Framework.AncestorsOrSelf.Select(u => u.FullyQualifiedArabicName)).Any())
  843:                         && a.Frameworks != null && (a.Frameworks.Intersect(currentStaffIdentityUser.Framework.AncestorsOrSelf.Select(u => u.ArabicName)).Any() || a.Frameworks.Intersect(currentStaffIdentityUser.Framework.AncestorsOrSelf.Select(u => u.FullyQualifiedArabicName)).Any())
  844:                         && !a.Obsolete && a.CanInsert) || a.Area == null
  845:                         select new
  846:                         {
  847:                             a.ColoredEnglishAndArabicName,
  848:                             a.XmlId
  849:                         }).Distinct().ToList();
  850:  
  851:             reportViewModel.ActionSelectList = new SelectList(list, "XmlId", "ColoredEnglishAndArabicName");
  852:             reportViewModel.ActionSelectedValue ??= "1000"; // <resolution id="1000" name="Unspecified" arabicName="غير محدد"/>
  853:         }
  854:  
  855:         ////////////////////////////////////////////////////////////////////////////
  856:  
  857:         /// <summary>
  858:         ///
  859:         /// </summary>
  860:         private static void ResolutionSelectList_ViewModel(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel)
  861:         {
  862:             var list = (from r in Ia.Ftn.Cl.Models.Data.Report.ResolutionList
  863:                         where !r.Obsolete && r.CanInsert
  864:                         select new
  865:                         {
  866:                             r.ColoredEnglishAndArabicName,
  867:                             r.XmlId
  868:                         }).Distinct().ToList();
  869:  
  870:             reportViewModel.ResolutionSelectList = new SelectList(list, "XmlId", "ColoredEnglishAndArabicName");
  871:             reportViewModel.ResolutionSelectedValue ??= "1000"; // <indication id="1000" name="Unspecified" arabicName="غير محدد"/>
  872:         }
  873:  
  874:         ////////////////////////////////////////////////////////////////////////////
  875:  
  876:         /// <summary>
  877:         ///
  878:         /// </summary>
  879:         private static void EstimateSelectList_ViewModel(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel)
  880:         {
  881:             var list = Ia.Ftn.Cl.Models.Data.Report.EstimateEnglishAndArabicDictionary;
  882:  
  883:             reportViewModel.EstimateSelectList = new SelectList(list, "Key", "Value", "0");
  884:         }
  885:  
  886:         ////////////////////////////////////////////////////////////////////////////
  887:  
  888:         /// <summary>
  889:         ///
  890:         /// </summary>
  891:         private static void ReportHistorySelectList_ViewModel(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel, Ia.Ftn.Cl.Models.Report report)
  892:         {
  893:             var reportId = reportViewModel.ReportId;
  894:  
  895:             if (Ia.Ftn.Cl.Models.Business.Authority.StaffCanCreateReadUpdateDeleteReportHistory(Ia.Ftn.Cl.Models.Business.Authority.PersistentStorageFunction.Create, report, null, reportViewModel.CurrentStaffIdentityUser))
  896:             {
  897:                 reportViewModel.InsertButtonEnabled = true;
  898:             }
  899:             else
  900:             {
  901:                 // below: not owner
  902:                 reportViewModel.InsertButtonEnabled = false;
  903:  
  904:                 reportViewModel.InformationResult.AddError("You are not authorized to modify this report (لست مخولاً بتعديل هذا التقرير)");
  905:             }
  906:  
  907:             reportViewModel.ReportHistoryList = Ia.Ftn.Cl.Models.Data.ReportHistory.ListForReportId(reportId);
  908:         }
  909:  
  910:         ////////////////////////////////////////////////////////////////////////////
  911:         ////////////////////////////////////////////////////////////////////////////
  912:  
  913:  
  914:  
  915:  
  916:  
  917:  
  918:  
  919:  
  920:  
  921:  
  922:  
  923:  
  924:  
  925:  
  926:  
  927:  
  928:  
  929:  
  930:  
  931:  
  932:  
  933:  
  934:  
  935:         ////////////////////////////////////////////////////////////////////////////
  936:         ////////////////////////////////////////////////////////////////////////////
  937:  
  938:         /// <summary>
  939:         ///
  940:         /// </summary>
  941:         public static void InsertViewModel(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel)
  942:         {
  943:             var reportId = -1;
  944:  
  945:             reportViewModel.PreliminaryAnalysisPanelVisible = false; // treeview
  946:             reportViewModel.ServiceAPanelVisible = true;
  947:  
  948:             PrioritySelectList_ViewModel(ref reportViewModel);
  949:             SeverityRadioButton_ViewModel(ref reportViewModel);
  950:             TreeView_DataAssign(ref reportViewModel);
  951:         }
  952:  
  953:         ////////////////////////////////////////////////////////////////////////////
  954:  
  955:         /// <summary>
  956:         ///
  957:         /// </summary>
  958:         private static void SeverityRadioButton_ViewModel(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel)
  959:         {
  960:             var list = Ia.Ftn.Cl.Models.Data.Report.SeverityColoredDictionary;
  961:  
  962:             reportViewModel.SeveritySelectList = new SelectList(list, "Key", "Value");
  963:             reportViewModel.SeveritySelectedValue ??= "0";
  964:         }
  965:  
  966:         ////////////////////////////////////////////////////////////////////////////
  967:  
  968:         /// <summary>
  969:         ///
  970:         /// </summary>
  971:         private static void PrioritySelectList_ViewModel(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel)
  972:         {
  973:             var list = Ia.Ftn.Cl.Models.Data.Report.PriorityEnglishAndArabicDictionary;
  974:  
  975:             reportViewModel.PrioritySelectList = new SelectList(list, "Key", "Value");
  976:         }
  977:  
  978:         ////////////////////////////////////////////////////////////////////////////
  979:  
  980:         /// <summary>
  981:         ///
  982:         /// </summary>
  983:         private static void TreeView_DataAssign(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel)
  984:         {
  985:             //indicationXmlDataSource.Data = Ia.Ftn.Cl.Models.Data.Report.XDocument.ToString();
  986:             //indicationXmlDataSource.XPath = "report/customerCare/indication/*";
  987:         }
  988:  
  989:         ////////////////////////////////////////////////////////////////////////////
  990:  
  991:         /// <summary>
  992:         ///
  993:         /// </summary>
  994:         public static void InsertPost(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel, string commandName)
  995:         {
  996:             if (commandName == "findReport")
  997:             {
  998:                 Find(ref reportViewModel);
  999:             }
 1000:             else if (commandName == "insertReport")
 1001:             {
 1002:                 InsertReport(ref reportViewModel);
 1003:             }
 1004:  
 1005:             //insertPanel.Visible = treeView.Visible = serviceAPanel.Visible = preliminaryAnalysisPanel.Visible = true;
 1006:         }
 1007:  
 1008:         ////////////////////////////////////////////////////////////////////////////
 1009:  
 1010:         /// <summary>
 1011:         ///
 1012:         /// </summary>
 1013:         private static void Find(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel)
 1014:         {
 1015:             int reportServiceType;
 1016:             string input;
 1017:  
 1018:             input = WebUtility.HtmlDecode(reportViewModel.ServiceInput);
 1019:  
 1020:             reportViewModel.FindResult = new Ia.Cl.Models.Result();
 1021:  
 1022:             if (!string.IsNullOrEmpty(input))
 1023:             {
 1024:                 // below: space characters from start and end
 1025:  
 1026:                 input = input.Trim();
 1027:  
 1028:                 input = Regex.Replace(input, @"^\s+", "");
 1029:                 input = Regex.Replace(input, @"\s+$", "");
 1030:                 input = input.ToUpper();
 1031:  
 1032:                 // below: the regex checks the format of input:
 1033:  
 1034:                 if (Ia.Ftn.Cl.Models.Business.Service.ServiceHasEightDigitsAndIsWithinAllowedDomainList(input))
 1035:                 {
 1036:                     reportServiceType = (int)Ia.Ftn.Cl.Models.Business.Report.ServiceType.Dn;
 1037:  
 1038:                     FindProcess(ref reportViewModel, input, reportServiceType);
 1039:                 }
 1040:                 else if (Ia.Ftn.Cl.Models.Business.Access.AccessNameIsInAValidFormat(input))
 1041:                 {
 1042:                     reportServiceType = (int)Ia.Ftn.Cl.Models.Business.Report.ServiceType.AccessName;
 1043:  
 1044:                     FindProcess(ref reportViewModel, input, reportServiceType);
 1045:                 }
 1046:                 else
 1047:                 {
 1048:                     reportServiceType = (int)Ia.Ftn.Cl.Models.Business.Report.ServiceType.Unspecified;
 1049:  
 1050:                     reportViewModel.ServicePreviewText = string.Empty;
 1051:                     reportViewModel.InsertButtonEnabled = false;
 1052:  
 1053:                     reportViewModel.FindResult.AddError("Input format is unknown (قيمة غير مفهومة)");
 1054:                 }
 1055:  
 1056:                 reportViewModel.ReportServiceTypeLabel = reportServiceType.ToString();
 1057:             }
 1058:             else
 1059:             {
 1060:                 reportViewModel.FindResult.AddError("No input was entered (لم يتم إدخال أي شيء)");
 1061:  
 1062:                 reportViewModel.ServicePreviewText = string.Empty;
 1063:                 reportViewModel.InsertButtonEnabled = false;
 1064:             }
 1065:         }
 1066:  
 1067:         ////////////////////////////////////////////////////////////////////////////
 1068:  
 1069:         /// <summary>
 1070:         ///
 1071:         /// </summary>
 1072:         private static void FindProcess(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel, string input, int reportServiceType)
 1073:         {
 1074:             bool openReportExists;
 1075:             string serviceName, accessName;
 1076:             Ia.Ftn.Cl.Models.Access access;
 1077:             Ia.Ftn.Cl.Models.Business.NetworkDesignDocument.Vendor switchVendor, accessVendor;
 1078:             List<Ia.Ftn.Cl.Models.Report> reportList;
 1079:  
 1080:             reportViewModel.FindResult = new Ia.Cl.Models.Result();
 1081:  
 1082:             openReportExists = false;
 1083:             reportViewModel.ServicePreviewLabel = reportViewModel.ReportPreviewLabel = /*findResultLabel.Text =*/ string.Empty;
 1084:  
 1085:             serviceName = Ia.Ftn.Cl.Models.Business.Service.ServiceName(input, reportServiceType);
 1086:  
 1087:             if (reportServiceType == (int)Ia.Ftn.Cl.Models.Business.Report.ServiceType.Dn)
 1088:             {
 1089:                 if (Ia.Ftn.Cl.Models.Business.Service.ServiceHasEightDigitsAndIsWithinAllowedDomainList(input))
 1090:                 {
 1091:                     if (Ia.Ftn.Cl.Models.Business.Authority.NumberIsWithinFrameworkSiteDomainList(reportViewModel.CurrentStaffIdentityUser.Framework, input))
 1092:                     {
 1093:                         switchVendor = Ia.Ftn.Cl.Models.Business.Service.SwitchVendor(input);
 1094:  
 1095:                         reportViewModel.ServicePreviewText = serviceName;
 1096:                         reportViewModel.ServicePreviewLink = Ia.Ftn.Cl.Models.Business.Maintenance.Find.Url(serviceName);
 1097:                         reportViewModel.ServicePreviewPanelVisible = true;
 1098:  
 1099:                         using (var db = new Ia.Ftn.Cl.Db())
 1100:                         {
 1101:                             access = (from a in db.Accesses
 1102:                                       join srs in db.ServiceRequestServices on a equals srs.Access
 1103:                                       where srs.Service == input
 1104:                                       select a).FirstOrDefault();
 1105:  
 1106:                             if (access != null)
 1107:                             {
 1108:                                 reportViewModel.ServicePreviewLabel = "(" + access.Name + ") " + access.Address;
 1109:                             }
 1110:                             else
 1111:                             {
 1112:                                 reportViewModel.ServicePreviewLabel = "Number property data not available (لا توجد معلومات عنوان للرقم)";
 1113:  
 1114:                                 reportViewModel.InsertButtonEnabled = true;
 1115:                                 reportViewModel.ServiceLabel = input;
 1116:                             }
 1117:                         }
 1118:  
 1119:                         // below: search for all available reports for this service and display them as link
 1120:                         reportList = Ia.Ftn.Cl.Models.Data.Report.List(input);
 1121:  
 1122:                         if (reportList.Count >= 1)
 1123:                         {
 1124:                             reportViewModel.ReportPreviewLabel = string.Empty;
 1125:  
 1126:                             foreach (Ia.Ftn.Cl.Models.Report report in reportList)
 1127:                             {
 1128:                                 // below: check if there are open reports
 1129:  
 1130:                                 // <status id="1" name="Open"
 1131:                                 // <status id="2" name="Closed"
 1132:                                 if (report.Status == 1)
 1133:                                 {
 1134:                                     openReportExists = true;
 1135:  
 1136:                                     reportViewModel.ReportPreviewLabel += "<a href=\"" + "history?reportId=" + report.Id + "\">" + report.Created.ToString("yyyy-MM-dd") + "</a> (open) ";
 1137:                                 }
 1138:                                 else
 1139:                                 {
 1140:                                     reportViewModel.ReportPreviewLabel += "<a href=\"" + "history?reportId=" + report.Id + "\">" + report.Created.ToString("yyyy-MM-dd") + "</a> ";
 1141:                                 }
 1142:                             }
 1143:  
 1144:                             reportViewModel.InsertedReportList = reportList;
 1145:  
 1146:                             if (openReportExists)
 1147:                             {
 1148:                                 reportViewModel.FindResult.AddWarning("You can not insert a new report because there is still an open report.<br /> لا يمكن إضافة تقرير جديد لهذا الرقم لأن تقريراً سابقاً لا يزال مفتوحاً");
 1149:  
 1150:                                 reportViewModel.InsertButtonEnabled = false;
 1151:                             }
 1152:                             else
 1153:                             {
 1154:                                 reportViewModel.InsertButtonEnabled = true;
 1155:                                 reportViewModel.ServiceLabel = input;
 1156:                             }
 1157:                         }
 1158:                         else
 1159:                         {
 1160:                             reportViewModel.InsertButtonEnabled = true;
 1161:                             reportViewModel.ServiceLabel = input;
 1162:  
 1163:                             reportViewModel.ReportPreviewLabel = "None (لا توجد)";
 1164:                         }
 1165:  
 1166:                         Ia.Ftn.Cl.Models.Data.MessageQueue.SecretaryApplication.Enqueue(Ia.Ftn.Cl.Models.Business.MessageQueue.Application.FtnApplication, Ia.Ftn.Cl.Models.Business.MessageQueue.Process.ReadService, input);
 1167:                     }
 1168:                     else
 1169:                     {
 1170:                         reportViewModel.FindResult.AddError("Number " + serviceName + " is not in staff site domain (الرقم لا ينتمي لنطاقات المستخدم)");
 1171:  
 1172:                         reportViewModel.ServicePreviewPanelVisible = false;
 1173:                         reportViewModel.InsertButtonEnabled = false;
 1174:                     }
 1175:                 }
 1176:                 else
 1177:                 {
 1178:                     reportViewModel.FindResult.AddError("Number " + serviceName + " is not within network (الرقم لا ينتمي للشبكة)");
 1179:  
 1180:                     reportViewModel.ServicePreviewPanelVisible = false;
 1181:                     reportViewModel.InsertButtonEnabled = false;
 1182:                 }
 1183:             }
 1184:             else if (reportServiceType == (int)Ia.Ftn.Cl.Models.Business.Report.ServiceType.AccessName)
 1185:             {
 1186:                 if (Ia.Ftn.Cl.Models.Data.NetworkDesignDocument.AccessNameIsWithinAllowedOntList(input, out accessName))
 1187:                 {
 1188:                     if (Ia.Ftn.Cl.Models.Business.Authority.AccessNameIsWithinFrameworkSiteAreaSymbolList(reportViewModel.CurrentStaffIdentityUser.Framework, accessName))
 1189:                     {
 1190:                         accessVendor = Ia.Ftn.Cl.Models.Business.Service.AccessVendor(accessName);
 1191:  
 1192:                         reportViewModel.ServicePreviewText = accessName;
 1193:                         reportViewModel.ServicePreviewLink = Ia.Ftn.Cl.Models.Business.Maintenance.Find.Url(accessName);
 1194:  
 1195:                         access = Ia.Ftn.Cl.Models.Data.Access.ReadByAccessName(accessName);
 1196:  
 1197:                         if (access != null)
 1198:                         {
 1199:                             reportViewModel.ServicePreviewPanelVisible = true;
 1200:                             reportViewModel.ServicePreviewLabel = "(" + access.Name + ") " + access.Address;
 1201:  
 1202:                             // below: search for all available reports for this accessName and display them as link
 1203:                             reportList = Ia.Ftn.Cl.Models.Data.Report.List(access.Name);
 1204:  
 1205:                             if (reportList.Count >= 1)
 1206:                             {
 1207:                                 reportViewModel.ReportPreviewLabel = string.Empty;
 1208:  
 1209:                                 foreach (Ia.Ftn.Cl.Models.Report report in reportList)
 1210:                                 {
 1211:                                     // below: we will check if there are open reports
 1212:  
 1213:                                     // <status id="1" name="Open"
 1214:                                     // <status id="2" name="Closed"
 1215:                                     if (report.Status == 1)
 1216:                                     {
 1217:                                         openReportExists = true;
 1218:  
 1219:                                         reportViewModel.ReportPreviewLabel += "<a href=\"" + "history?reportId=" + report.Id + "\">" + report.Created.ToString("yyyy-MM-dd") + "</a> (open) ";
 1220:                                     }
 1221:                                     else
 1222:                                     {
 1223:                                         reportViewModel.ReportPreviewLabel += "<a href=\"" + "history?reportId=" + report.Id + "\">" + report.Created.ToString("yyyy-MM-dd") + "</a> ";
 1224:                                     }
 1225:                                 }
 1226:  
 1227:                                 reportViewModel.InsertedReportList = reportList;
 1228:  
 1229:                                 if (openReportExists)
 1230:                                 {
 1231:                                     reportViewModel.FindResult.AddWarning("You can not insert a new report because there is still an open report.<br /> لا يمكن إضافة تقرير جديد لهذا الرقم لأن تقريراً سابقاً لا يزال مفتوحاً");
 1232:  
 1233:                                     reportViewModel.InsertButtonEnabled = false;
 1234:                                 }
 1235:                                 else
 1236:                                 {
 1237:                                     reportViewModel.ServiceLabel = accessName;
 1238:                                     reportViewModel.InsertButtonEnabled = true;
 1239:                                 }
 1240:                             }
 1241:                             else
 1242:                             {
 1243:                                 reportViewModel.ReportPreviewLabel = "None (لا توجد)";
 1244:  
 1245:                                 reportViewModel.FindResult.AddWarning("None (لا توجد)");
 1246:                                 //findResultLabel.CssClass = "warn";
 1247:  
 1248:                                 reportViewModel.ServiceLabel = accessName;
 1249:                                 reportViewModel.InsertButtonEnabled = true;
 1250:                             }
 1251:                         }
 1252:                         else
 1253:                         {
 1254:                             reportViewModel.FindResult.AddWarning("Access " + accessName + " data not available (لا توجد معلومات جهاز)");
 1255:  
 1256:                             reportViewModel.ServiceLabel = accessName;
 1257:                             reportViewModel.InsertButtonEnabled = true;
 1258:                         }
 1259:  
 1260:                         Ia.Ftn.Cl.Models.Data.MessageQueue.SecretaryApplication.Enqueue(Ia.Ftn.Cl.Models.Business.MessageQueue.Application.FtnApplication, Ia.Ftn.Cl.Models.Business.MessageQueue.Process.ReadAccess, accessName);
 1261:                     }
 1262:                     else
 1263:                     {
 1264:                         reportViewModel.FindResult.AddError("Access " + accessName + " is not in staff site access list (رقم الجهاز لا ينتمي لمقسم المستخدم)");
 1265:  
 1266:                         reportViewModel.ServicePreviewPanelVisible = false;
 1267:                         reportViewModel.InsertButtonEnabled = false;
 1268:                     }
 1269:                 }
 1270:                 else
 1271:                 {
 1272:                     reportViewModel.ServicePreviewText = string.Empty;
 1273:  
 1274:                     reportViewModel.FindResult.AddError("Access " + accessName + " does not belong to the network (الجهاز لا ينتمي للشبكة)");
 1275:  
 1276:                     reportViewModel.ServicePreviewPanelVisible = false;
 1277:                     reportViewModel.InsertButtonEnabled = false;
 1278:                 }
 1279:             }
 1280:             else
 1281:             {
 1282:                 reportViewModel.InsertAuthorityResult.AddError("Unknown error (خطأ غير معروف)");
 1283:  
 1284:                 reportViewModel.ServicePreviewPanelVisible = false;
 1285:                 reportViewModel.InsertButtonEnabled = false;
 1286:             }
 1287:         }
 1288:  
 1289:         ////////////////////////////////////////////////////////////////////////////
 1290:  
 1291:         /// <summary>
 1292:         ///
 1293:         /// </summary>
 1294:         private static bool OpenReportExistsForService(int reportServiceType, string input)
 1295:         {
 1296:             bool openReportExists;
 1297:             long number;
 1298:             string serviceName, accessName;
 1299:             Ia.Ftn.Cl.Models.Access access;
 1300:             List<Ia.Ftn.Cl.Models.Report> reportList;
 1301:  
 1302:             serviceName = Ia.Ftn.Cl.Models.Business.Service.ServiceName(input, reportServiceType);
 1303:  
 1304:             if (reportServiceType == (int)Ia.Ftn.Cl.Models.Business.Report.ServiceType.Dn)
 1305:             {
 1306:                 number = int.Parse(input);
 1307:  
 1308:                 if (Ia.Ftn.Cl.Models.Business.Service.ServiceHasEightDigitsAndIsWithinAllowedDomainList(number))
 1309:                 {
 1310:                     reportList = Ia.Ftn.Cl.Models.Data.Report.List(number.ToString());
 1311:  
 1312:                     if (reportList.Count >= 1)
 1313:                     {
 1314:                         openReportExists = false;
 1315:  
 1316:                         foreach (Ia.Ftn.Cl.Models.Report report in reportList)
 1317:                         {
 1318:                             // below: check if there are open reports
 1319:  
 1320:                             // <status id="1" name="Open"
 1321:                             // <status id="2" name="Closed"
 1322:                             if (report.Status == 1)
 1323:                             {
 1324:                                 openReportExists = true;
 1325:                                 break;
 1326:                             }
 1327:                             else
 1328:                             {
 1329:                                 openReportExists = false;
 1330:                             }
 1331:                         }
 1332:                     }
 1333:                     else
 1334:                     {
 1335:                         openReportExists = false;
 1336:                     }
 1337:                 }
 1338:                 else
 1339:                 {
 1340:                     openReportExists = false;
 1341:                 }
 1342:             }
 1343:             else if (reportServiceType == (int)Ia.Ftn.Cl.Models.Business.Report.ServiceType.AccessName)
 1344:             {
 1345:                 if (Ia.Ftn.Cl.Models.Data.NetworkDesignDocument.AccessNameIsWithinAllowedOntList(input, out accessName))
 1346:                 {
 1347:                     access = Ia.Ftn.Cl.Models.Data.Access.ReadByAccessName(accessName);
 1348:  
 1349:                     if (access != null)
 1350:                     {
 1351:                         reportList = Ia.Ftn.Cl.Models.Data.Report.List(access.Name);
 1352:  
 1353:                         if (reportList.Count >= 1)
 1354:                         {
 1355:                             openReportExists = false;
 1356:  
 1357:                             foreach (Ia.Ftn.Cl.Models.Report report in reportList)
 1358:                             {
 1359:                                 // below: we will check if there are open reports
 1360:  
 1361:                                 // <status id="1" name="Open"
 1362:                                 // <status id="2" name="Closed"
 1363:                                 if (report.Status == 1)
 1364:                                 {
 1365:                                     openReportExists = true;
 1366:                                     break;
 1367:                                 }
 1368:                                 else
 1369:                                 {
 1370:                                     openReportExists = false;
 1371:                                 }
 1372:                             }
 1373:                         }
 1374:                         else
 1375:                         {
 1376:                             openReportExists = false;
 1377:                         }
 1378:                     }
 1379:                     else
 1380:                     {
 1381:                         openReportExists = false;
 1382:                     }
 1383:                 }
 1384:                 else
 1385:                 {
 1386:                     openReportExists = false;
 1387:                 }
 1388:             }
 1389:             else
 1390:             {
 1391:                 openReportExists = false;
 1392:             }
 1393:  
 1394:             return openReportExists;
 1395:         }
 1396:  
 1397:         ////////////////////////////////////////////////////////////////////////////
 1398:  
 1399:         /// <summary>
 1400:         ///
 1401:         /// </summary>
 1402:         private static void InsertReport(ref Ia.Ftn.Wa.Models.Maintenance.ReportViewModel reportViewModel)
 1403:         {
 1404:             int reportServiceType;
 1405:             string service, contact, result;
 1406:             DateTime now;
 1407:             Ia.Ftn.Cl.Models.Report report;
 1408:  
 1409:             reportViewModel.InsertResult = new Ia.Cl.Models.Result();
 1410:  
 1411:             reportViewModel.DetailTextArea ??= string.Empty;
 1412:             reportViewModel.ContactNameInput ??= string.Empty;
 1413:             reportViewModel.ContactPhoneInput ??= string.Empty;
 1414:             reportViewModel.NoteTextArea ??= string.Empty;
 1415:  
 1416:             if (reportViewModel.ServiceInput.Length > 0)
 1417:             {
 1418:                 if (reportViewModel.ServicePreviewLink.Length > 0)
 1419:                 {
 1420:                     if (reportViewModel.DetailTextArea.Length > 0)
 1421:                     {
 1422:                         if (reportViewModel.ContactNameInput.Length > 0)
 1423:                         {
 1424:                             if (reportViewModel.ContactPhoneInput.Length > 0)
 1425:                             {
 1426:                                 if (reportViewModel.ServiceLabel.Length > 0 && reportViewModel.ReportServiceTypeLabel.Length > 0)
 1427:                                 {
 1428:                                     reportServiceType = int.Parse(reportViewModel.ReportServiceTypeLabel);
 1429:                                     service = WebUtility.HtmlDecode(reportViewModel.ServiceLabel);
 1430:  
 1431:                                     if (!OpenReportExistsForService(reportServiceType, service))
 1432:                                     {
 1433:                                         now = DateTime.UtcNow.AddHours(3);
 1434:  
 1435:                                         contact = WebUtility.HtmlDecode(reportViewModel.ContactNameInput) + " " + WebUtility.HtmlDecode(reportViewModel.ContactPhoneInput);
 1436:  
 1437:                                         /*
 1438:                                         below:
 1439:                                         <type id="1" name="Dn"
 1440:                                         <type id="2" name="Ont Name"
 1441:                                         <type id="3" name="No Ont"
 1442:                                         <type id="0" name="Unspecified"
 1443:                                         */
 1444:  
 1445:                                         report = new Ia.Ftn.Cl.Models.Report
 1446:                                         {
 1447:                                             Service = service,
 1448:                                             ServiceType = reportServiceType,
 1449:                                             Area = 11, // <area id="11" name="Service"
 1450:                                             Category = 1,
 1451:                                             Severity = int.Parse(reportViewModel.SeveritySelectedValue),
 1452:                                             Priority = int.Parse(reportViewModel.PrioritySelectedValue),
 1453:                                             Status = 1, // <status id="1" name="Open" ...>
 1454:                                             Detail = WebUtility.HtmlDecode(reportViewModel.DetailTextArea),
 1455:                                             Contact = contact,
 1456:                                             StaffIdentityUser = reportViewModel.CurrentStaffIdentityUser,
 1457:                                             Note = WebUtility.HtmlDecode(reportViewModel.NoteTextArea),
 1458:                                             Created = now,
 1459:                                             Updated = now
 1460:                                         };
 1461:  
 1462:                                         var reportId = Ia.Ftn.Cl.Models.Data.Report.Create(report, out result);
 1463:  
 1464:                                         if (reportId >= 0)
 1465:                                         {
 1466:                                             reportViewModel.ReportId = reportId;
 1467:  
 1468:                                             var s = "Report successfully created (تم إدخال المعلومات)";
 1469:                                             reportViewModel.InsertResult.AddSuccess(s);
 1470:  
 1471:                                             reportViewModel.ServiceInput = reportViewModel.DetailTextArea = reportViewModel.ContactNameInput = reportViewModel.ContactPhoneInput = reportViewModel.NoteTextArea = string.Empty;
 1472:  
 1473:                                             reportViewModel.InsertButtonEnabled = false;
 1474:                                         }
 1475:                                         else
 1476:                                         {
 1477:                                             reportViewModel.ReportId = -1;
 1478:  
 1479:                                             reportViewModel.InsertResult.AddError(result);
 1480:                                         }
 1481:                                     }
 1482:                                     else
 1483:                                     {
 1484:                                         reportViewModel.InsertResult.AddError("You can not insert a new report because there is still an open report.<br /> لا يمكن إضافة تقرير جديد لهذا الرقم لأن تقريراً سابقاً لا يزال مفتوحاً");
 1485:  
 1486:                                         reportViewModel.InsertButtonEnabled = false;
 1487:                                     }
 1488:                                 }
 1489:                                 else
 1490:                                 {
 1491:                                     reportViewModel.InsertResult.AddError("Service and ServiceType hidden labels are missing");
 1492:                                 }
 1493:                             }
 1494:                             else
 1495:                             {
 1496:                                 reportViewModel.InsertResult.AddError("Contact phone is missing (رقم مراجعة المتصل مفقود)");
 1497:                             }
 1498:                         }
 1499:                         else
 1500:                         {
 1501:                             reportViewModel.InsertResult.AddError("Contact name is missing (اسم المتصل مفقود)");
 1502:                         }
 1503:                     }
 1504:                     else
 1505:                     {
 1506:                         reportViewModel.InsertResult.AddError("Detail is missing (وصف العطل مفقود)");
 1507:                     }
 1508:                 }
 1509:                 else
 1510:                 {
 1511:                     reportViewModel.InsertResult.AddError("Number format is incorrect or does not exist.<br /> (تركيب الرقم غير صحيح أو الرقم غير موجود ضمن الخدمة)");
 1512:                 }
 1513:             }
 1514:             else
 1515:             {
 1516:                 reportViewModel.InsertResult.AddError("No input was entered (لم يتم إدخال أي شيء)");
 1517:             }
 1518:  
 1519:             reportViewModel.InsertedReportList = Ia.Ftn.Cl.Models.Data.Report.ReadSingleAsList(reportViewModel.ReportId);
 1520:         }
 1521:  
 1522:         ////////////////////////////////////////////////////////////////////////////
 1523:         ////////////////////////////////////////////////////////////////////////////
 1524:     }
 1525:  
 1526:     ////////////////////////////////////////////////////////////////////////////
 1527:     ////////////////////////////////////////////////////////////////////////////
 1528: }