Advertisement
Guest User

Untitled

a guest
Sep 11th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. /* Controllers - Quotation Controller */
  4.  
  5. angular.module('nsgsis').controller('QuotationController', ['$scope', 'NgTableParams', 'QuotationService','BookingService', '$translate', 'GeneralService','$mdDialog', '$location', '$timeout',
  6.     function($scope, NgTableParams, QuotationService, BookingService, $translate, GeneralService, $mdDialog, $location) {
  7.    
  8.     $scope.loader = false;
  9.    
  10.     $scope.searchStringClientName = "";
  11.     $scope.General = GeneralService;
  12.    
  13.    
  14.     $scope.quotation = {};
  15.     $scope.quotationId = null;
  16.     $scope.quotationList = {};
  17.     $scope.clientsList = {};
  18.     $scope.co_ordinatorsList = {};
  19.     $scope.clientContactsList = {};
  20.     $scope.clientDetails = {};
  21.     $scope.co_ordinatorsSelected = {};
  22.     $scope.TypeList = [];
  23.     $scope.courseDetails = {};
  24.     $scope.clientSelectedContactsList = {};
  25.     $scope.tariffList = {};
  26.     $scope.tariffSelected = " ";
  27.     $scope.courseItemsList = [];
  28.     $scope.courseDeletedItemsList = [];
  29.     $scope.costCentreItems= {};
  30.     $scope.selectedCourseName = null;
  31.     $scope.totalItems = 0;
  32.     $scope.allItemsSaved = true;
  33.     $scope.searchFilter = {};
  34.     $scope.collapse = false;
  35.     $scope.editMode = false;
  36.     $scope.emptyDate = true;
  37.     $scope.foundDelegatesList = [];
  38.     $scope.quotationDelegatesList = [];
  39.     $scope.addingDelegate = false;
  40.     $scope.delegateDetails = {};
  41.     $scope.copyCase = false;
  42.     $scope.delegatesResultEnabled = false;
  43.     $scope.racesList = [];
  44.     $scope.qualificationsList = [];
  45.     $scope.calendarList = [];
  46.     $scope.calendarSelected = {};
  47.     $scope.unsavedDelegates = 0;
  48.     $scope.delegatesNumber = 0;
  49.     $scope.searchFilter.hidedeleted = true;
  50.     $scope.venueDetails = {};
  51.     $scope.contractList = {};
  52.     $scope.contractSelected = {};
  53.     $scope.contractSelectedId = {};
  54.      
  55.     $scope.userName = "user";
  56.     $scope.pageSize = null;
  57.     $scope.pageNumber = null;
  58.    
  59.     $scope.username = "user";
  60.     $scope.password = "password";
  61.    
  62.    
  63.     // Restituisce la lista delle ultime 10 quotation dell'utente corrente
  64.     $scope.getLastQuotations = function () {
  65.             QuotationService.getUserToken($scope.username, $scope.password)
  66.             .then(
  67.                     function(response) {
  68.                         $scope.loader = true;
  69.                         $scope.size = "10";
  70.                         $scope.searchFilter.hidedeleted = "true";
  71.                         QuotationService.getUserQuotations($scope.userName, $scope.size, $scope.searchFilter)
  72.                             .then(
  73.                                 function(response) {
  74.                                     $scope.quotationList = response;
  75.                                     for (var i in response) {
  76.                                         $scope.quotationList[i].quotationDate = new Date (response[i].quotationDate);
  77.                                         $scope.quotationList[i].printStatus = getIconByStatus(String(response[i].status), String(response[i].subStatus));
  78.                                         if($scope.quotationList[i].status == "20" && $scope.quotationList[i].subStatus == "0") {
  79.                                             $scope.quotationList[i].classDeleted = "deleted";
  80.                                         }
  81.                                     }
  82.                                     $scope.defaultConfigTableParams =
  83.                                         new NgTableParams({
  84.                                             page: 1,
  85.                                             count: 10
  86.                                         }, {
  87.                                             counts: [],
  88.                                             total: 1,
  89.                                             dataset: $scope.quotationList
  90.                                         });
  91.                                     $scope.loader = false;
  92.                                 },
  93.                                 function(errResponse){
  94.                                     $scope.quotationList = [];
  95.                                     console.error("Error loading last quotations");
  96.                                     $scope.loader = false;
  97.                                 }            
  98.                             );
  99.                     },
  100.                     function(errResponse){
  101.                         console.error("Error token");
  102.                     }
  103.             );
  104.     }
  105.    
  106.    
  107.      
  108.     // Restituisce l'icona FA relativa allo stato della quotation
  109.     function getIconByStatus(status, substatus) {
  110.         var printStatus = {};
  111.         $scope.ss = status + substatus;
  112.         switch ($scope.ss) {
  113.             case "30"://draft
  114.                 printStatus.iconName = "hourglass-start";
  115.                 printStatus.title = "NSG_SIS_QUOTATION.GENERATE";
  116.                 break;
  117.             case "40"://submitted
  118.                 printStatus.iconName ="certificate";
  119.                 printStatus.title = "NSG_SIS_QUOTATION.AUTHORIZE";
  120.                 break;
  121.             case "50"://authorized
  122.                 printStatus.iconName = "registered";
  123.                 printStatus.title = "NSG_SIS_QUOTATION.SUBMIT";
  124.                 break;
  125.             case "60"://tentative booking (add calendar & delegates)
  126.                 printStatus.iconName = "calendar-alt";
  127.                 printStatus.title = "NSG_SIS_QUOTATION.TENTATIVE_BOOKING";
  128.                 break;
  129.             case "61": case "62": case "63": case "64": case "65":
  130.                 printStatus.iconName = "money-bill-alt";
  131.                 break;
  132.             case "70":
  133.                 printStatus.iconName = "";
  134.                 break;
  135.             case "71"://commit providers  (contract & venue)
  136.                 printStatus.iconName = "clipboard-check";
  137.                 printStatus.title = "NSG_SIS_QUOTATION.COMMIT_PROVIDERS";
  138.                 break;
  139.             case "72": case "73": case "74": case "75": case "76":
  140.                 printStatus.iconName = "clipboard-check";
  141.                 printStatus.title = "NSG_SIS_QUOTATION.SUBMIT";
  142.                 break;
  143.             case "80": case "81": case "82":
  144.                 printStatus.iconName = "money-bill";
  145.             case "90"://event completed
  146.                 printStatus.iconName = "calendar-check";
  147.                 break;
  148.             case "91"://cancelled training
  149.                 printStatus.iconName = "ban";
  150.                 break;
  151.             case "200"://cancelled quotation
  152.                 printStatus.iconName = "trash-alt";
  153.                 printStatus.title = "NSG_SIS_QUOTATION.DELETED";
  154.                 break;
  155.             default://not found
  156.                 printStatus.iconName = null;
  157.                 break;
  158.        }
  159.         return printStatus;
  160.     }
  161.    
  162.     $scope.editQuotation = function(item) {
  163.         $scope.loader = true;
  164.         $scope.editMode = true;
  165.         $scope.info = {};
  166.         $scope.info.clientInformation = false;
  167.         $scope.info.contactDetails = false;
  168.         $scope.info.pricesQuoted = false;
  169.         $scope.getQualifications();
  170.         $scope.getRaces();
  171.         getQuotation(item.id);
  172.     }
  173.    
  174.     // Operazioni dopo la selezione del cliente
  175.     $scope.selectedClientNameItemChanged = function (item) {
  176.         if (item !== undefined && item.clientName !== undefined){
  177.             $scope.selectedClientName = item.clientName;
  178.             $scope.clientId = item.clientId;
  179.             getClientDetails($scope.clientId);
  180.         }
  181.         // Clear fields
  182.         if (item === undefined) {
  183.             $scope.clientDetails = $scope.co_ordinatorsList = $scope.clientContactsList = {};
  184.             $scope.postalAddress = $scope.streetAddress = "";
  185.             $scope.clientSelectedContactsList = {};
  186.         }
  187.     }
  188.    
  189.    
  190.     $scope.selectedCourseNameItemChanged = function (item) {
  191.         switch(item) {
  192.         case undefined:
  193.             // putting previous items in delete list
  194.             for(var i in $scope.courseItemsList) {
  195.                 $scope.courseDeletedItemsList.push($scope.courseItemsList[i]);
  196.             }
  197.            
  198.             $scope.tariffSelected = "";
  199.             $scope.courseDetails = {};
  200.             $scope.courseItemsList = [];
  201.             $scope.selectedCourseName = null;
  202.             $scope.allItemsSaved = true;
  203.             $scope.emptyDate = true;
  204.             break;
  205.         default:
  206.             if (item.courseNameFull !== undefined) {
  207.                 $scope.selectedCourseName = item.courseNameFull;
  208.                 $scope.courseDetailsId = item.courseDetailsId;
  209.                 getCourseDetails($scope.courseDetailsId, null, false);
  210.             }
  211.             break;
  212.         }
  213.     }
  214.    
  215.     // Restituisce il dettaglio del Client
  216.     function getClientDetails(clientId,contactId) {
  217.         $scope.loader = true;
  218.         QuotationService.getClientDetails(clientId)
  219.         .then(
  220.             function(response) {
  221.                 $scope.clientDetails = response;
  222.                 loadClientType();
  223.                 $scope.co_ordinatorsList = response.clientContacts;
  224.                 $scope.clientContactsList = response.contacts;
  225.                 if ($scope.editMode) {
  226.                     if (contactId != undefined || contactId != null){  
  227.                         $scope.co_ordinatorsSelected = String(contactId);
  228.                         $scope.insertCoordinator();
  229.                     }
  230.                 }
  231.                 $scope.count = 0;
  232.                 $scope.postalAddress = "";
  233.                 $scope.streetAddress = "";
  234.                 $scope.clientDetails.typeName = "";
  235.                 if($scope.editMode)
  236.                     $scope.selectedClientName = $scope.clientDetails.clientName;
  237.                 for (var i in $scope.clientTypeList) {
  238.                     if ($scope.clientTypeList[i].typeId == $scope.clientDetails.type)
  239.                         $scope.clientDetails.typeName = $scope.clientTypeList[i].type;
  240.                 }
  241.                 for (var i in response.addresses) {
  242.                     $scope.count++;
  243.                     if($scope.count <= 5 && response.addresses[i] != null)
  244.                         $scope.postalAddress += response.addresses[i]+"\n";
  245.                     if($scope.count > 5 && response.addresses[i] != null)
  246.                         $scope.streetAddress += response.addresses[i]+"\n";
  247.                 }
  248.                 $scope.loader = false;
  249.             },
  250.             function(errResponse){
  251.                 $scope.loader = false;
  252.                 console.error("Error loading client details");
  253.             }            
  254.         );  
  255.     }
  256.    
  257.     function loadClientType() {
  258.         GeneralService.loadClientType().then(
  259.             function(response) {
  260.                 $scope.clientTypeList = response;  
  261.             }, function(errResponse) {
  262.                 console.error("Error loading client types");
  263.             });
  264.     }
  265.    
  266.    
  267.     function getCalendarCourse(courseId) {
  268.         $scope.today = GeneralService.getTodayDate();
  269.         BookingService.getCalendarCourse(courseId, $scope.today)
  270.         .then(
  271.             function(response) {
  272.                 $scope.calendarList = response;
  273.             },
  274.             function(errResponse){
  275.                 console.error("Error loading calendar list");
  276.             }            
  277.         )
  278.     }
  279.    
  280.     function getContractsCourse(courseId) {
  281.         BookingService.getContractsCourse(courseId)
  282.         .then(
  283.             function(response) {
  284.                 $scope.contractList = response;
  285.             },
  286.             function(errResponse){
  287.                 console.error("Error loading contract list");
  288.             }            
  289.         )
  290.     }
  291.    
  292.     // Valorizza i campi del co-ordinator selezionato
  293.     $scope.insertCoordinator = function() {
  294.         $scope.loader = true;
  295.         for (var i in $scope.co_ordinatorsList) {
  296.             if ($scope.co_ordinatorsList[i].id == $scope.co_ordinatorsSelected) {
  297.                 $scope.clientSelectedContactsList = $scope.co_ordinatorsList[i];
  298.                 if ($scope.co_ordinatorsList[i].designation == "1")
  299.                     $scope.clientSelectedContactsList.designationValue = "Departmental Coordinator";
  300.                 $scope.clientSelectedContactsList.postalAddress = "";
  301.                 $scope.clientSelectedContactsList.streetAddress = "";
  302.                 $scope.count = 0;
  303.                 for (var j in $scope.clientSelectedContactsList.addresses) {
  304.                     $scope.count++;
  305.                     if($scope.count <= 5 && $scope.clientSelectedContactsList.addresses[j] != null)
  306.                         $scope.clientSelectedContactsList.postalAddress += $scope.clientSelectedContactsList.addresses[j]+"\n";
  307.                     if($scope.count > 5 && $scope.clientSelectedContactsList.addresses[j] != null)
  308.                         $scope.clientSelectedContactsList.streetAddress += $scope.clientSelectedContactsList.addresses[j    ]+"\n";
  309.                 }
  310.             }
  311.         }
  312.         $scope.loader = false;
  313.     }
  314.    
  315.     // Valorizza i campi del calendar selezionato
  316.     $scope.insertCalendar = function() {
  317.         for (var i in $scope.calendarList) {
  318.             if ($scope.calendarList[i].calendarId == $scope.calendarIdSelected) {
  319.                 $scope.calendarSelected = $scope.calendarList[i];
  320.                 $scope.calendarSelected.fromDate = new Date ($scope.calendarSelected.fromDate);
  321.                 $scope.calendarSelected.toDate = new Date ($scope.calendarSelected.toDate);
  322.             }
  323.         }
  324.     }
  325.    
  326.     // Valorizza i campi del contract selezionato
  327.     $scope.insertContract = function() {
  328.         for (var i in $scope.contractList) {
  329.             if ($scope.contractList[i].contractID == $scope.contractSelectedId) {
  330.                 $scope.contractSelected = $scope.contractList[i];
  331.             }
  332.         }
  333.     }
  334.    
  335.     //Restituisce il dettaglio del Course
  336.     function getCourseDetails(courseId, quotationId, retrieveCourseList) {
  337.         $scope.loader = true;
  338.         QuotationService.getCourseDetails(courseId)
  339.         .then(
  340.             function(response) {
  341.                 $scope.courseDetails = response;
  342.                 $scope.courseDetails.coursePrice = $scope.courseDetails.coursePrice.toFixed(2);
  343.                 $scope.courseDetails.pricePerpersonPerDay = $scope.courseDetails.pricePerpersonPerDay.toFixed(2);
  344.                 if($scope.courseDetails.dateApproved!=null){
  345.                     $scope.emptyDate = false;
  346.                     $scope.courseDetails.dateApproved = new Date($scope.courseDetails.dateApproved);
  347.                 }else{
  348.                     $scope.emptyDate = true;
  349.                 }
  350.                
  351.                 if($scope.courseDetails.hasTariffGroup){
  352.                     $scope.loader = true;
  353.                     QuotationService.getTariffGroups()
  354.                     .then(
  355.                         function(response) {
  356.                             $scope.tariffList= response;
  357.                             $scope.loader = false;
  358.                         },
  359.                         function(errResponse){
  360.                             console.error("Error loading tariff details");
  361.                             $scope.loader = false;
  362.                         }            
  363.                     )
  364.                 }
  365.                
  366.                 if($scope.editMode) {
  367.                     $scope.selectedCourseName = $scope.courseDetails.courseNameFull;
  368.                     if ($scope.courseDetails.tariffSelected != undefined && $scope.courseDetails.tariffSelected != null)
  369.                         $scope.tariffSelected = $scope.courseDetails.tariffSelected;
  370.                     else $scope.tariffSelected = " ";
  371.                     if ($scope.quotation.calendarId != undefined && $scope.quotation.calendarId != null) {
  372.                         BookingService.getCalendarDetails($scope.quotation.calendarId)
  373.                             .then(
  374.                                  function(response) {
  375.                                      $scope.calendarDetails = response;
  376.                                      for (var i in $scope.calendarList) {
  377.                                          var duplicate = false;
  378.                                          if ($scope.calendarList[i].calendarId == $scope.calendarDetails.calendarId)
  379.                                              duplicate = true;
  380.                                      }
  381.                                      if(!duplicate)
  382.                                          $scope.calendarList.push($scope.calendarDetails);
  383.                                      
  384.                                      $scope.calendarIdSelected = String($scope.calendarDetails.calendarId);
  385.                                      $scope.insertCalendar();
  386.                                  },
  387.                                  function(errResponse){
  388.                                      console.error(errResponse);
  389.                                  }            
  390.                             )
  391.                     }
  392.                 }
  393.                
  394.                 $scope.CourseItemConfigTableParams =
  395.                     new NgTableParams({
  396.                         page: 1,
  397.                         count: 10
  398.                     }, {
  399.                         counts: [],
  400.                         total: 1,
  401.                         dataset: $scope.courseItemsList
  402.                     });
  403.                 loadCostCentre();
  404.                 if($scope.editMode && retrieveCourseList){
  405.                     $scope.loader = true;
  406.                     var index_list = 0;
  407.                     QuotationService.getQuotationCourseItemList(quotationId)
  408.                     .then(
  409.                         function(response) {
  410.                             $scope.courseItemsList= response;
  411.                             for(var i in $scope.courseItemsList){
  412.                                 //Save max number of learners
  413.                                 if($scope.courseItemsList[i].costCentreId==3){
  414.                                     $scope.courseDetails.noOfTrainee = $scope.courseItemsList[i].noOfTrainee;
  415.                                     $scope.courseItemsList[i].item = "course";
  416.                                 }
  417.                                 for(var j in $scope.costCentreItems){
  418.                                     if($scope.courseItemsList[i].costCentreId==$scope.costCentreItems[j].costId){
  419.                                         $scope.courseItemsList[i].item = $scope.costCentreItems[j].costCentre;
  420.                                     }
  421.                                 }
  422.                                
  423.                                 $scope.courseItemsList[i].saved = true;
  424.                                 $scope.courseItemsList[i].item_old = $scope.courseItemsList[i].item;
  425.                                 $scope.courseItemsList[i].coursePrice_old = $scope.courseItemsList[i].coursePrice;
  426.                                 $scope.courseItemsList[i].total_old = $scope.courseItemsList[i].total;
  427.                                 $scope.courseItemsList[i].noOfTrainee_old = $scope.courseItemsList[i].noOfTrainee;
  428.                                 $scope.courseItemsList[i].index = index_list;
  429.                                 $scope.courseItemsList[i].couresDetailsId = $scope.courseDetails.id;
  430.                                 index_list++;
  431.                                 $scope.courseItemsList[i].saved = true;
  432.                             }
  433.                             if($scope.courseItemsList.length > 0) {
  434.                                 $scope.allItemsSaved = true;
  435.                             }
  436.                             else {
  437.                                 $scope.allItemsSaved = false;
  438.                                 $scope.courseItemsList=[{"item":"course", "noOfTrainee":"", "coursePrice":$scope.courseDetails.coursePrice, "total":"0", "saved":false, "index":0,
  439.                                     "item_old":"course", "noOfTrainee_old":"", "total_old":0, "coursePrice_old":0}];
  440.                             }
  441.                             refreshTotal();
  442.                             $scope.loader = false;
  443.                         },
  444.                         function(errResponse){
  445.                             console.error("Error loading course items list");
  446.                             $scope.loader = false;
  447.                         }      
  448.                     )
  449.                 }
  450.                 else {
  451.                     var i = parseFloat($scope.courseDetails.coursePrice);
  452.                     $scope.allItemsSaved = false;
  453.                     $scope.courseItemsList=[{"item":"course", "noOfTrainee":"", "coursePrice":i, "total":"0", "saved":false, "index":0,
  454.                         "item_old":"course", "noOfTrainee_old":"", "total_old":0, "coursePrice_old":0}];
  455.                     $scope.loader = false;
  456.                 }
  457.             },
  458.             function(errResponse){
  459.                 $scope.loader = false;
  460.                 console.error("Error loading course details");
  461.             }            
  462.         );  
  463.     }
  464.    
  465.     // Salva la quotation come bozza (stato 3-0)
  466.     $scope.saveQuotation = function() {
  467.         console.log($scope.courseDeletedItemsList);
  468.         $scope.loader = true;
  469.         $scope.quotation.clientId = $scope.clientDetails.id;
  470.         $scope.quotation.clientContactId = $scope.clientSelectedContactsList.id;
  471.         $scope.quotation.dataId = $scope.quotationId;
  472.         $scope.quotation.courseDetailsId = $scope.courseDetails.id;
  473.         $scope.quotation.courseQuotedList = $scope.courseItemsList;
  474.         $scope.quotation.tariffId = parseInt($scope.tariffSelected);
  475.         if($scope.courseDeletedItemsList.length > 0){
  476.             for(var i in $scope.courseDeletedItemsList){
  477.                 if($scope.courseDeletedItemsList[i].id != null) {
  478.                     QuotationService.deleteCourseItem($scope.courseDeletedItemsList[i].id).then(
  479.                         function(response) {
  480.                             $scope.loader = false;
  481.                         }, function(errResponse) {
  482.                             $scope.loader = false;
  483.                             console.error("Error saving quotation");
  484.                         }
  485.                     );
  486.                 }
  487.             }
  488.         }
  489.        
  490.         for(var i in $scope.courseItemsList){
  491.             if($scope.courseItemsList[i].costCentreId == 3) $scope.courseItemsList[i].coursePrice = null;
  492.         }
  493.        
  494.         QuotationService.saveQuotation($scope.quotation).then(
  495.             function(response) {
  496.                 $scope.getUserQuotations();
  497.                 $scope.loader = false;
  498.                 dialogPop($translate.instant('NSG_SIS_QUOTATION.SAVE_SUCCESS'));
  499.                 $location.path( "/nsgsisfe/cco/manage-quotations" );
  500.                 $scope.editMode = false;
  501.             }, function(errResponse) {
  502.                 $scope.loader = false;
  503.                 console.error("Error saving quotation");
  504.                 dialogPop($translate.instant('NSG_SIS_QUOTATION.SAVE_ERROR'));
  505.             }
  506.         );
  507.     }
  508.    
  509.     // Send della quotation (stato 3-0 to 4-0)
  510.     $scope.submitQuotation = function() {
  511.         $scope.loader = true;
  512.         $scope.quotation.clientId = $scope.clientDetails.id;
  513.         $scope.quotation.clientContactId = $scope.clientSelectedContactsList.id;
  514.         $scope.quotation.dataId = $scope.quotationId;
  515.         $scope.quotation.courseDetailsId = $scope.courseDetails.id;
  516.         $scope.quotation.courseQuotedList = $scope.courseItemsList;
  517.        
  518.         for(var i in $scope.courseItemsList){
  519.             if($scope.courseItemsList[i].costCentreId == 3) $scope.courseItemsList[i].coursePrice = null;
  520.         }
  521.        
  522.         QuotationService.submitQuotation($scope.quotation).then(
  523.             function(response) {
  524.                 $scope.loader = false;
  525.                 dialogPop($translate.instant('NSG_SIS_QUOTATION.SUBMIT_SUCCESS'));
  526.                 $location.path( "/nsgsisfe/cco/manage-quotations" );
  527.                 $scope.editMode = false;
  528.                 $scope.getUserQuotations();
  529.             }, function(errResponse) {
  530.                 $scope.loader = false;
  531.                 console.error("Error saving quotation");
  532.                 dialogPop($translate.instant('NSG_SIS_QUOTATION.SUBMIT_ERROR'));
  533.             }
  534.         );
  535.     }
  536.      
  537.     //Back della quotation (stato 5-0 to 3-0)
  538.     $scope.backToGenerateQuotation = function () {
  539.         $scope.loader = true;
  540.         QuotationService.backToGenerateQuotation($scope.quotation.id, $scope.quotation.description, $scope.quotation.comments)
  541.             .then(
  542.                 function(response) {
  543.                     dialogPop($translate.instant('NSG_SIS_QUOTATION.BACK_30_SUCCESS'));
  544.                     $scope.loader = false;
  545.                     $scope.getLastQuotations();
  546.                     $scope.editMode = false;
  547.                 },
  548.                 function(errResponse){
  549.                     dialogPop($translate.instant('NSG_SIS_QUOTATION.BACK_30_ERROR'));
  550.                     $scope.loader = false;
  551.                 }            
  552.             );  
  553.     }
  554.    
  555.     // Delete della quotation (stato 3-0 to 20-0)
  556.    
  557.     $scope.selectedQuotationDelete = "";
  558.    
  559.     $scope.deleteSelected = function (quotationId) {
  560.         $scope.selectedQuotationDelete = quotationId;
  561.     }
  562.    
  563.     $scope.deleteQuotation = function(list) {
  564.         $scope.loader = true;      
  565.         QuotationService.deleteQuotation($scope.selectedQuotationDelete).then(
  566.             function(response) {
  567.                 $scope.loader = false;
  568.                 dialogPop($translate.instant('NSG_SIS_QUOTATION.DELETE_SUCCESS'));
  569.                 if(list == 'last')
  570.                     $scope.getLastQuotations();
  571.                 if(list == 'manage')
  572.                     $scope.getUserQuotations();
  573.             }, function(errResponse) {
  574.                 $scope.loader = false;
  575.                 console.error("Error deleting quotation");
  576.                 dialogPop($translate.instant('NSG_SIS_QUOTATION.DELETE_ERROR'));
  577.             }
  578.         );
  579.     }
  580.    
  581.     function dialogPop(title, text) {
  582.         $mdDialog.show(
  583.             $mdDialog.alert()
  584.                 .parent(angular.element(document.querySelector('#popupContainer')))
  585.                 .clickOutsideToClose(true)
  586.                 .title(title)
  587.                 .textContent(text)
  588.                 .ok('OK')
  589.         );
  590.     }
  591.    
  592.     $scope.refreshTariff = function() {
  593.         $scope.loader = true;
  594.         QuotationService.getTariffedCourse($scope.courseDetails.id, $scope.tariffSelected)
  595.         .then(
  596.             function(response) {
  597.                 $scope.courseDetails.coursePrice = Math.round(response.coursePrice*100)/100;
  598.                 $scope.courseDetails.pricePerpersonPerDay = response.coursePrice/response.courseDurationDays;
  599.                 $scope.courseDetails.courseDurationDays = response.courseDurationDays;
  600.                 $scope.courseDetails.dateApproved = response.dateApproved;
  601.                 $scope.loader = false;
  602.             },
  603.             function(errResponse){
  604.                 console.error("Error loading Tariffed Course details");
  605.                 $scope.loader = false;
  606.             }
  607.         );
  608.     }
  609.    
  610.     $scope.totalCourse = function(elem){
  611.         elem.total=(elem.noOfTrainee*elem.coursePrice).toFixed(2);
  612.         refreshTotal();
  613.     }
  614.    
  615.     $scope.saveCourseItem = function(item) {
  616.         if(item.coursePrice==undefined || item.coursePrice==null ||
  617.            item.noOfTrainee=="" || item.noOfTrainee==null
  618.            || item.item==undefined || item.item==""){
  619.             return;
  620.         }
  621.  
  622.         if(item.coursePrice == undefined){
  623.             item.coursePrice = 0;
  624.         }
  625.            
  626.         // Saving
  627.         item.saved = true;
  628.         $scope.allItemsSaved = true;
  629.        
  630.         //Aggiornamento totale
  631.         $scope.totalCourse(item);
  632.        
  633.         //shadow copy
  634.         item.item_old=item.item;
  635.         item.noOfTrainee_old=item.noOfTrainee;
  636.         item.coursePrice_old=item.coursePrice;
  637.         // Aggiunta campi per save
  638.         item.pricePerTrainee = item.coursePrice/item.noOfTrainee;
  639.            
  640.         for (var i in $scope.costCentreItems){
  641.             if($scope.costCentreItems[i].costCentre==item.item){
  642.                 item.costCentreId=$scope.costCentreItems[i].costId;
  643.             }
  644.         }
  645.         item.couresDetailsId=$scope.courseDetails.id;
  646.         item.fromDate=null;
  647.         item.toDate=null;
  648.     }
  649.    
  650.     $scope.editCourseItem = function(item) {
  651.         item.saved=false;
  652.         $scope.allItemsSaved= false;
  653.     }
  654.    
  655.     $scope.deleteCourseItem = function(item) {
  656.         if(item.item!='course'){
  657.             $scope.courseItemsList.splice(item.index, 1);
  658.             refreshTotal();
  659.         }
  660.         if(item.id!=null){
  661.             $scope.courseDeletedItemsList.push(item.id);
  662.         }
  663.     }
  664.    
  665.     $scope.newCostItem = function(){
  666.         var allow=true;
  667.         for(var i in $scope.courseItemsList){
  668.             if ($scope.courseItemsList[i].saved == false) allow = false;
  669.         }
  670.         if(allow){
  671.             $scope.allItemsSaved=false;
  672.             $scope.courseItemsList.push({
  673.                 "item" : "",
  674.                 "learners" : "",
  675.                 "price": "",
  676.                 "total": "0",
  677.                 "saved": false,
  678.                 "index": $scope.courseItemsList.length,
  679.                 "item_old": "",
  680.                 "learners_old" : "",
  681.                 "price_old" : 0,
  682.                 "total_old" : 0
  683.             });
  684.         }
  685.         else { // Altro item da salvare prima di crearne un altro
  686.             dialogPop($translate.instant('NSG_SIS_QUOTATION.COURSE_COST_ITEMS.NEW_ERROR'));
  687.         }
  688.     }
  689.    
  690.     //Prende gli items di Cost Centre
  691.     function loadCostCentre() {
  692.         QuotationService.getCostCentre()
  693.         .then(
  694.             function(response) {
  695.                 $scope.costCentreItems = response;
  696.             },
  697.             function(errResponse){
  698.                 console.error("Error loading cost centre items");
  699.             }            
  700.         );
  701.     }
  702.    
  703.     // Annulla le modifiche di un oggetto di courseItemsList
  704.     $scope.discardChanges = function(item){
  705.         for (var i in $scope.courseItemsList){
  706.             if($scope.courseItemsList[i].index==item.index){
  707.                 console.log(item);
  708.                 if(item.price == "" && item.learners == "" && item.item == ""){
  709.                     $scope.courseItemsList.splice($scope.courseItemsList.indexOf(item), 1);
  710.                 }
  711.                 item.coursePrice=item.coursePrice_old;
  712.                 item.noOfTrainee=item.noOfTrainee_old;
  713.                 item.item=item.item_old;
  714.                 item.editMode = false;
  715.             }
  716.         }
  717.     }
  718.    
  719.     // Aggiorna il totale dei Course items (salvato in $scope.totalItems)
  720.     function refreshTotal() {
  721.         var count = parseFloat(0);
  722.         for (var i in $scope.courseItemsList){
  723.             count = count + parseFloat($scope.courseItemsList[i].total);
  724.         }
  725.         $scope.totalItems = parseFloat(parseFloat(parseInt(count*100))/100);
  726.     }
  727.    
  728.     // Restituisce la lista delle quotation dell'utente corrente
  729.     $scope.getUserQuotations = function () {
  730.         $scope.loader = true;
  731.         $scope.size = null;
  732.         QuotationService.getUserQuotations($scope.userName, $scope.size, $scope.searchFilter)
  733.             .then(
  734.                 function(response) {
  735.                     $scope.quotationList = response;
  736.                     for (var i in response) {
  737.                         $scope.quotationList[i].quotationDate = new Date (response[i].quotationDate);
  738.                         $scope.quotationList[i].printStatus = getIconByStatus(String(response[i].status), String(response[i].subStatus));
  739.                                            
  740.                         if($scope.quotationList[i].status == "20" && $scope.quotationList[i].subStatus == "0") {
  741.                             $scope.quotationList[i].classDeleted = "deleted";
  742.                         }
  743.                     }
  744.                     $scope.defaultConfigTableParams =
  745.                         new NgTableParams({
  746.                             page: 1,
  747.                             count: 10
  748.                         }, {
  749.                             dataset: $scope.quotationList
  750.                         });
  751.                     $scope.loader = false;
  752.                 },
  753.                 function(errResponse){
  754.                     $scope.quotationList = [];
  755.                     console.error("Error loading quotations");
  756.                     $scope.loader = false;
  757.                 }            
  758.             );  
  759.     }
  760.    
  761.    
  762.     function getQuotation(quotationId) {
  763.         $scope.loader = true;
  764.         $scope.quotationId = quotationId;
  765.         QuotationService.getQuotation(quotationId)
  766.             .then(
  767.                 function(response) {
  768.                     $scope.quotation = response;
  769.                     $scope.tariffSelected = String(response.tariffId);
  770.                     $scope.quotation.printStatus = getIconByStatus(String($scope.quotation.status), String($scope.quotation.subStatus));
  771.                     if($scope.quotation.clientId != null){
  772.                         getClientDetails($scope.quotation.clientId, $scope.quotation.clientContactId);
  773.                     }
  774.                     if($scope.quotation.courseDetailsId != null) {
  775.                         getCourseDetails($scope.quotation.courseDetailsId, $scope.quotationId, true);
  776.                     }
  777.                     else $scope.tariffSelected = " ";
  778.                     if($scope.quotation.status>=6) {
  779.                         $scope.getQuotationDelegates($scope.quotation.id);
  780.                         getCalendarCourse($scope.quotation.courseDetailsId);
  781.                     }
  782.                     if($scope.quotation.status>=7) {
  783.                         getContractsCourse($scope.quotation.courseDetailsId);
  784.                         if ($scope.quotation.venueId != null)
  785.                             getVenuesDetails($scope.quotation.venueId);
  786.                     }
  787.                 },
  788.                 function(errResponse){
  789.                     console.error("Error loading quotation");
  790.                     $scope.editMode = false;
  791.                     $scope.loader = false;
  792.                 }            
  793.             );  
  794.     }
  795.    
  796.     $scope.declineQuotation = function () {
  797.         $scope.loader = true;
  798.         QuotationService.declineQuotation($scope.quotation.id, $scope.quotation.description, $scope.quotation.comments)
  799.             .then(
  800.                 function(response) {
  801.                     dialogPop($translate.instant('NSG_SIS_QUOTATION.DECLINE_SUCCESS'));
  802.                     $scope.loader = false;
  803.                     $scope.getLastQuotations();
  804.                     $scope.editMode = false;
  805.                 },
  806.                 function(errResponse){
  807.                     dialogPop($translate.instant('NSG_SIS_QUOTATION.DECLINE_ERROR'));
  808.                     $scope.loader = false;
  809.                 }            
  810.             );  
  811.     }
  812.    
  813.     $scope.approveQuotation = function () {
  814.         $scope.loader = true;
  815.         QuotationService.approveQuotation($scope.quotation.id, $scope.quotation.description, $scope.quotation.comments)
  816.             .then(
  817.                 function(response) {
  818.                     $scope.loader = false;
  819.                     dialogPop($translate.instant('NSG_SIS_QUOTATION.APPROVE_SUCCESS'));
  820.                     $scope.getLastQuotations();
  821.                     $scope.editMode = false;
  822.                 },
  823.                 function(errResponse){
  824.                     dialogPop($translate.instant('NSG_SIS_QUOTATION.APPROVE_ERROR'));
  825.                     $scope.loader = false;
  826.                 }            
  827.             );  
  828.     }
  829.    
  830.     $scope.submitAddTentativeBooking = function() {
  831.         $scope.loader = true;
  832.         QuotationService.submitAddTentativeBooking($scope.quotation.id, $scope.quotation.description, $scope.quotation.comments)
  833.             .then(
  834.                 function(response) {
  835.                     $scope.loader = false;
  836.                     dialogPop($translate.instant('NSG_SIS_QUOTATION.SUBMIT_ADD_BOOKING_SUCCESS'));
  837.                     $location.path( "/nsgsisfe/cco/manage-quotations" );
  838.                     $scope.editMode = false;
  839.                     $scope.getUserQuotations();
  840.                 }, function(errResponse) {
  841.                     $scope.loader = false;
  842.                     console.error("Error saving quotation");
  843.                     dialogPop($translate.instant('NSG_SIS_QUOTATION.SUBMIT_ADD_BOOKING_ERROR'));
  844.                 }
  845.             );
  846.     }
  847.    
  848.     $scope.reportQuotation = function (){
  849.         $scope.loader = true;
  850.         QuotationService.reportQuotation($scope.quotation.id)
  851.             .then(
  852.                 function(response) {
  853.                     var a = document.createElement("a");
  854.                     document.body.appendChild(a);
  855.                     a.style = "display: none";
  856.                     var file = new Blob([response.data.data], {type: 'application/pdf'});
  857.                     var fileURL = window.URL.createObjectURL(file);
  858.                     a.href = fileURL;
  859.                     a.download = getFileNameFromHttpResponse(response);
  860.                     a.click();
  861.                     $scope.loader = false;
  862.                 },
  863.                 function(errResponse){
  864.                     $('#printModal').modal('hide');
  865.                     console.error("Error downloading document");
  866.                     dialogPop($translate.instant('NSG_SIS_FORMS.DOWNLOAD_ERROR'));
  867.                     $scope.loader = false;
  868.                 }            
  869.             );
  870.     }
  871.    
  872.     $scope.reportBooking = function (){
  873.         $scope.loader = true;
  874.         QuotationService.reportBooking($scope.quotation.id)
  875.             .then(
  876.                 function(response) {
  877.                     var a = document.createElement("a");
  878.                     document.body.appendChild(a);
  879.                     a.style = "display: none";
  880.                     var file = new Blob([response.data.data], {type: 'application/pdf'});
  881.                     var fileURL = window.URL.createObjectURL(file);
  882.                     a.href = fileURL;
  883.                     a.download = getFileNameFromHttpResponse(response);
  884.                     a.click();
  885.                     $scope.loader = false;
  886.                 },
  887.                 function(errResponse){
  888.                     $('#printModal').modal('hide');
  889.                     console.error("Error downloading document");
  890.                     dialogPop($translate.instant('NSG_SIS_FORMS.DOWNLOAD_ERROR'));
  891.                     $scope.loader = false;
  892.                 }            
  893.             );  
  894.     }
  895.    
  896.     $scope.getDelegatesList = function() {
  897.         //if empty filter
  898.         if(emptyDelegatesFilter($scope.searchFilter)){
  899.             dialogPop($translate.instant('NSG_SIS_QUOTATION.EMPTY_SEARCH'));
  900.             return;
  901.         }
  902.         $scope.delegatesResultEnabled = true;
  903.         $scope.loader = true;
  904.         $scope.copyCase = false;
  905.         if(onlyCase($scope.searchFilter)) $scope.copyCase = true;
  906.        
  907.         //Lista dei delegates appartenenti alla quotation
  908.         $scope.quotationDelegatesConfigTableParams =
  909.             new NgTableParams({
  910.                 page: 1,
  911.                 count: 10
  912.             }, {
  913.                 dataset: $scope.quotationDelegatesList.filter(elem => elem.deleted!=true)
  914.         });
  915.        
  916.         BookingService.getDelegatesList($scope.searchFilter)
  917.             .then(
  918.                 function(response) {
  919.                     $scope.foundDelegatesList = response;
  920.                     $scope.foundDelegatesConfigTableParams =
  921.                         new NgTableParams({
  922.                             page: 1,
  923.                             count: 10
  924.                         }, {
  925.                             dataset: $scope.foundDelegatesList
  926.                     });
  927.                     $scope.loader = false;
  928.                     $scope.foundDelegatesConfigTableParams.reload();
  929.                 }, function(errResponse) {
  930.                     $scope.foundDelegatesList = [];
  931.                     $scope.loader = false;
  932.                 }
  933.             );
  934.     }
  935.    
  936.     function onlyCase(filter){
  937.         if (filter.surname != undefined && filter.surname != null && filter.surname != "") {
  938.             return false;
  939.         }
  940.            
  941.         if (filter.firstName != undefined && filter.firstName != null && filter.firstName != "") {
  942.             return false;
  943.         }
  944.            
  945.         if (filter.persalNumber != undefined && filter.persalNumber != null && filter.persalNumber != "") {
  946.             return false;
  947.         }
  948.            
  949.         if (filter.idNumber != undefined && filter.idNumber != null && filter.idNumber != "") {
  950.             return false;
  951.         }
  952.        
  953.         if(filter.idcase != undefined && filter.idcase != null && filter.idcase != "")
  954.             return true;
  955.         else
  956.             return false;
  957.     }
  958.    
  959.     function emptyDelegatesFilter(filter){
  960.         if (filter.surname != undefined && filter.surname != null && filter.surname != "") {
  961.             return false;
  962.         }
  963.            
  964.         if (filter.name != undefined && filter.name != null && filter.name != "") {
  965.             return false;
  966.         }
  967.            
  968.         if (filter.persalnumber != undefined && filter.persalnumber != null && filter.persalnumber != "") {
  969.             return false;
  970.         }
  971.            
  972.         if (filter.idnumber != undefined && filter.idnumber != null && filter.idnumber != "") {
  973.             return false;
  974.         }
  975.        
  976.         if(filter.idcase != undefined && filter.idcase != null && filter.idcase != "")
  977.             return false;
  978.        
  979.         return true;
  980.     }
  981.    
  982.     $scope.addDelegate = function(elem, copy) {
  983.        
  984.         if(elem.idNumber == null && elem.persalNumber == null && (!copy || copy==null)) {
  985.             dialogPop($translate.instant('NSG_SIS_QUOTATION.DELEGATE.NULL_IDS'));
  986.             return;
  987.         }
  988.        
  989.         if(elem.idNumber != null && !$scope.isValidIdNumber(elem.idNumber) && (!copy || copy==null)) {
  990.             dialogPop($translate.instant('NSG_SIS_QUOTATION.DELEGATE.INVALID_ID'));
  991.             return;
  992.         }
  993.        
  994.         if(copy && ($scope.quotationDelegatesList.length + $scope.foundDelegatesList.length > $scope.courseDetails.noOfTrainee)) {
  995.             dialogPop($translate.instant('NSG_SIS_QUOTATION.LEARNERS_EXCEEDS'));
  996.             return;
  997.         }
  998.         if((!copy || copy==null) && ($scope.quotationDelegatesList.length == $scope.courseDetails.noOfTrainee)){
  999.             dialogPop($translate.instant('NSG_SIS_QUOTATION.LEARNERS_EXCEEDS'));
  1000.             return;
  1001.         }
  1002.        
  1003.         if(delegateAlreadyPresent(elem) && (!copy || copy==null)){
  1004.             dialogPop($translate.instant('NSG_SIS_QUOTATION.DELEGATE.ALREADY_PRESENT'));
  1005.             return;
  1006.         }
  1007.        
  1008.         //element filling
  1009.         elem.added = true;
  1010.         elem.delegateId = null;
  1011.         elem.deleted = false;
  1012.        
  1013.         // adding to list
  1014.         $scope.quotationDelegatesList.push(elem);
  1015.         $scope.quotationDelegatesConfigTableParams =
  1016.             new NgTableParams({
  1017.                 page: 1,
  1018.                 count: 10
  1019.             }, {
  1020.                 dataset: $scope.quotationDelegatesList.filter(elem => elem.deleted!=true)
  1021.         });
  1022.         $scope.quotationDelegatesConfigTableParams.reload();
  1023.         $scope.delegatesNumber++;
  1024.         //Resetting form
  1025.         $scope.cancelDelegateCreation();
  1026.     }
  1027.    
  1028.     $scope.removeDelegate = function(elem) {
  1029.         elem.added = false;
  1030.         elem.deleted = true;
  1031.         $scope.delegatesNumber--;
  1032.         $scope.quotationDelegatesConfigTableParams =
  1033.             new NgTableParams({
  1034.                 page: 1,
  1035.                 count: 10
  1036.             }, {
  1037.                 dataset: $scope.quotationDelegatesList.filter(elem => elem.deleted!=true)
  1038.         });
  1039.         $scope.quotationDelegatesConfigTableParams.reload();
  1040.     }
  1041.    
  1042.     $scope.createDelegate = function() {
  1043.         $scope.addingDelegate = true;
  1044.     }
  1045.    
  1046.     $scope.getInitials = function(names){
  1047.         var names= names.split(' ');
  1048.         var initials="";
  1049.         for(var i=0;i<names.length;i++){
  1050.             names[i].replace(/\s+/g, '');
  1051.             initials = initials + names[i].substring(0,1).toUpperCase();
  1052.         }
  1053.         $scope.delegateDetails.initials = initials;
  1054.     }
  1055.    
  1056.     $scope.cancelDelegateCreation = function(){
  1057.         $scope.addingDelegate = false;
  1058.         $scope.searchFilter = {};
  1059.         $scope.delegateDetails = {};
  1060.     }
  1061.    
  1062.     $scope.addFromCase = function() {
  1063.         if($scope.quotationDelegatesList.length + $scope.foundDelegatesList.length > $scope.courseDetails.noOfTrainee) {
  1064.             dialogPop($translate.instant('NSG_SIS_QUOTATION.LEARNERS_EXCEEDS'));
  1065.         } else{
  1066.             //Copying list elements
  1067.             var terminated = false;
  1068.             var i = 0;
  1069.             for(i=0; i<$scope.foundDelegatesList.length; i++){
  1070.                 if(i==$scope.foundDelegatesList.length -1 )
  1071.                     $scope.addDelegate($scope.foundDelegatesList[i], true);
  1072.                 else
  1073.                     $scope.addDelegate($scope.foundDelegatesList[i], true);
  1074.             }
  1075.         }
  1076.     }
  1077.    
  1078.     $scope.getRaces = function() {
  1079.         BookingService.getRaces()
  1080.         .then(
  1081.             function(response) {
  1082.                 $scope.racesList = response;
  1083.             }, function(errResponse) {
  1084.                 console.error(errResponse);
  1085.             }
  1086.         );
  1087.     }
  1088.    
  1089.     $scope.getQualifications = function() {
  1090.         BookingService.getQualifications()
  1091.         .then(
  1092.             function(response) {
  1093.                 $scope.qualificationsList = response;
  1094.             }, function(errResponse) {
  1095.                 console.error(errResponse);
  1096.             }
  1097.         );
  1098.     }
  1099.    
  1100.     $scope.getQuotationDelegates = function(quotationId) {
  1101.         var filter = {};
  1102.         filter.idcase = quotationId;
  1103.         BookingService.getDelegatesList(filter)
  1104.         .then(
  1105.             function(response) {
  1106.                 $scope.quotationDelegatesList = response;
  1107.                 $scope.quotationDelegatesConfigTableParams =
  1108.                     new NgTableParams({
  1109.                         page: 1,
  1110.                         count: 10
  1111.                     }, {
  1112.                         dataset: $scope.quotationDelegatesList.filter(elem => elem.deleted!=true)
  1113.                 });
  1114.                 $scope.quotationDelegatesConfigTableParams.reload();
  1115.                 $scope.delegatesNumber = $scope.quotationDelegatesList.length;
  1116.             }, function(errResponse) {
  1117.                 $scope.foundDelegatesList = [];
  1118.             }
  1119.         );
  1120.     }
  1121.    
  1122.     $scope.showDelegateDetails = function(delegate) {
  1123.         var message = "";
  1124.         var name= "";
  1125.         var surname = "";
  1126.        
  1127.         if(delegate.firstName != null && delegate.firstName != ""){
  1128.             message = message + $translate.instant('NSG_SIS_QUOTATION.DELEGATE.NAME')+": "+delegate.firstName+"<br />";
  1129.             name = delegate.firstName;
  1130.         }
  1131.         if(delegate.surname != null && delegate.surname != ""){
  1132.             message = message + $translate.instant('NSG_SIS_QUOTATION.DELEGATE.SURNAME')+": "+delegate.surname+"<br />";
  1133.             surname = delegate.surname;
  1134.         }
  1135.         if(delegate.idNumber != null && delegate.idNumber != ""){
  1136.             message = message + $translate.instant('NSG_SIS_QUOTATION.DELEGATE.ID')+": "+delegate.idNumber+"<br />";
  1137.         }
  1138.         if(delegate.persalNumber != null && delegate.persalNumber != ""){
  1139.             message = message + $translate.instant('NSG_SIS_QUOTATION.DELEGATE.PERSAL')+": "+delegate.persalNumber+"<br />";
  1140.         }
  1141.         if(delegate.qualificationId != null && delegate.qualificationId != ""){
  1142.             message = message + $translate.instant('NSG_SIS_QUOTATION.DELEGATE.QUALIFICATION')+": "+getQualificationById(delegate.qualificationId)+"<br />";
  1143.         }
  1144.         if(delegate.rank != null && delegate.rank != ""){
  1145.             message = message + $translate.instant('NSG_SIS_QUOTATION.DELEGATE.RANK')+": "+delegate.rank+"<br />";
  1146.         }
  1147.         if(delegate.gender != null && delegate.gender != ""){
  1148.             message = message + $translate.instant('NSG_SIS_QUOTATION.DELEGATE.GENDER')+": "+getGenderById(delegate.gender)+"<br />";
  1149.         }
  1150.         if(delegate.raceId != null && delegate.raceId != ""){
  1151.             message = message + $translate.instant('NSG_SIS_QUOTATION.DELEGATE.RACE')+": "+getRaceById(delegate.raceId)+"<br />";
  1152.         }
  1153.         if(delegate.disability != null && delegate.disability != ""){
  1154.             message = message + $translate.instant('NSG_SIS_QUOTATION.DELEGATE.DISABILITY')+": "+delegate.disability+"<br />";
  1155.         }
  1156.         if(delegate.email != null && delegate.email != ""){
  1157.             message = message + $translate.instant('NSG_SIS_QUOTATION.EMAIL')+": "+delegate.email+"<br />";
  1158.         }
  1159.         if(delegate.telephone != null && delegate.telephone != ""){
  1160.             message = message + $translate.instant('NSG_SIS_QUOTATION.DELEGATE.TELEPHONE_NUMBER')+": "+delegate.telephone+"<br />";
  1161.         }
  1162.         if(delegate.cellPhone != null && delegate.cellPhone != ""){
  1163.             message = message + $translate.instant('NSG_SIS_QUOTATION.DELEGATE.CELLULAR_NUMBER')+": "+delegate.cellPhone+"<br />";
  1164.         }
  1165.        
  1166.         $mdDialog.show(
  1167.                 $mdDialog.alert()
  1168.                     .clickOutsideToClose(true)
  1169.                     .title(name+" "+surname)
  1170.                     .htmlContent(message)
  1171.                     .ariaLabel("Label")
  1172.                     .ok($translate.instant('NSG_SIS_OK'))
  1173.             );
  1174.     }
  1175.    
  1176.     function getQualificationById(id){
  1177.         for(var i in $scope.qualificationsList){
  1178.             if($scope.qualificationsList[i].qualificationId == id) return $scope.qualificationsList[i].qualName;
  1179.         }
  1180.         return null;
  1181.     }
  1182.    
  1183.     function getRaceById(id) {
  1184.         for(var i in $scope.racesList){
  1185.             if($scope.racesList[i].raceId == id) return $scope.racesList[i].race;
  1186.         }
  1187.         return null;
  1188.     }
  1189.    
  1190.     function getGenderById(id){
  1191.         if(id == 1) return "Male";
  1192.         if(id == 2) return "Female";
  1193.     }    
  1194.    
  1195.     $scope.saveTentativeBooking = function(){
  1196.         $scope.loader = true;
  1197.         var promises = [];
  1198.        
  1199.         for(var i = 0; i<$scope.quotationDelegatesList.length; i++) {
  1200.             $scope.quotationDelegatesList[i].dataId = $scope.quotation.id;
  1201.            
  1202.             //se il delegate non era già in lista e non è stato cancellato, salva
  1203.             if($scope.quotationDelegatesList[i].delegateId == null && $scope.quotationDelegatesList[i].deleted == false) {
  1204.                 promises.push(BookingService.saveDelegate($scope.quotation.id, $scope.quotationDelegatesList[i]));
  1205.             }
  1206.            
  1207.             //se il delegate era già in lista ed è stato cancellato, rimuovi
  1208.             if($scope.quotationDelegatesList[i].delegateId != null && $scope.quotationDelegatesList[i].deleted == true){
  1209.                 promises.push(BookingService.removeDelegate($scope.quotationDelegatesList[i].delegateId));
  1210.             }
  1211.         }
  1212.         //$timeout($scope.saveTentativeBookingCall, 5000);
  1213.         handleDelegatesQueue(promises);
  1214.     }
  1215.    
  1216.     function handleDelegatesQueue(queue) {
  1217.           let promise = queue.shift();
  1218.           if(promise!=undefined)
  1219.               promise.then(
  1220.                       (data) => { // success
  1221.                           if (queue.length > 0) {
  1222.                               handleDelegatesQueue(queue);
  1223.                           }
  1224.                           else {
  1225.                               $scope.saveTentativeBookingCall();
  1226.                           }
  1227.                       }, function(error) { // no success
  1228.                           $scope.unsavedDelegates++;
  1229.                           if (queue.length > 0) {
  1230.                               handleDelegatesQueue(queue);
  1231.                           } else {
  1232.                               $scope.saveTentativeBookingCall();
  1233.                           }
  1234.                       }
  1235.              );
  1236.           else $scope.saveTentativeBookingCall();
  1237.     }
  1238.    
  1239.     $scope.saveTentativeBookingCall = function () {
  1240.         var data = {};
  1241.         data.id = $scope.quotation.id;
  1242.         data.calendarId = $scope.calendarIdSelected;
  1243.         data.groupNo = $scope.quotation.groupNo;
  1244.         data.bookingType = $scope.quotation.groupNo; //bookingType == groupNo
  1245.         data.orderNumber = $scope.quotation.orderNumber;
  1246.         data.comments = $scope.quotation.comments;
  1247.         data.description = $scope.quotation.description;
  1248.        
  1249.         BookingService.saveTentativeBooking(data)
  1250.         .then(
  1251.             function(response) {
  1252.                 // se non c'è stato alcun errore nel salvataggio/cancellazione dei delegates
  1253.                 if($scope.unsavedDelegates==0){
  1254.                     dialogPop($translate.instant('NSG_SIS_QUOTATION.SAVE_SUCCESS'));
  1255.                     $location.path( "/nsgsisfe/cco/manage-quotations" );
  1256.                     $scope.editMode = false;
  1257.                     $scope.getUserQuotations();
  1258.                     $scope.loader = false;
  1259.                 } else { // errore nel salvataggio dei delegates
  1260.                     dialogPop($translate.instant('NSG_SIS_QUOTATION.UNCOMPLETE_SAVE'));
  1261.                     $location.path( "/nsgsisfe/cco/manage-quotations" );
  1262.                     $scope.editMode = false;
  1263.                     $scope.getUserQuotations();
  1264.                     $scope.loader = false;
  1265.                 }
  1266.             }, function(errResponse) { // errore nel salvataggio della quotation
  1267.                 dialogPop($translate.instant('NSG_SIS_QUOTATION.SAVE_ERROR'));
  1268.                 $scope.loader = false;
  1269.             }
  1270.         );
  1271.     }
  1272.    
  1273.     $scope.submitTentativeBooking = function(){
  1274.         $scope.loader = true;
  1275.         var data = {};
  1276.         data.id = $scope.quotation.id;
  1277.         data.calendarId = $scope.calendarIdSelected;
  1278.         data.groupNo = $scope.quotation.groupNo;
  1279.         data.bookingType = $scope.quotation.groupNo; //bookingType == groupNo
  1280.         data.orderNumber = $scope.quotation.orderNumber;
  1281.         data.comments = $scope.quotation.comments;
  1282.         data.description = $scope.quotation.description;
  1283.         BookingService.submitTentativeBooking(data)
  1284.         .then(
  1285.             function(response) {
  1286.                 $scope.loader = false;
  1287.                 dialogPop($translate.instant('NSG_SIS_QUOTATION.TENTATIVE_BOOKING_SUCCESS'));
  1288.                 $location.path( "/nsgsisfe/cco/manage-quotations" );
  1289.                 $scope.editMode = false;
  1290.                 $scope.getUserQuotations();
  1291.             }, function(errResponse) {
  1292.                 dialogPop($translate.instant('NSG_SIS_QUOTATION.TENTATIVE_BOOKING_ERROR'));
  1293.                 $scope.loader = false;
  1294.             }
  1295.         );
  1296.     }
  1297.    
  1298.     $scope.isValidIdNumber = function(idNumber) {
  1299.         var ex = /^(((\d{2}((0[13578]|1[02])(0[1-9]|[12]\d|3[01])|(0[13456789]|1[012])(0[1-9]|[12]\d|30)|02(0[1-9]|1\d|2[0-8])))|([02468][048]|[13579][26])0229))(( |-)(\d{4})( |-)(\d{3})|(\d{7}))/;
  1300.         return ex.test(idNumber);
  1301.     }
  1302.    
  1303.     function delegateAlreadyPresent(elem) {
  1304.         for(var i=0; i<$scope.quotationDelegatesList; i++){
  1305.             if(elem.idNumber == $scope.quotationDelegatesList[i].idNumber ||
  1306.             elem.persalNumber == $scope.quotationDelegatesList[i].persalNumber)
  1307.                 return true;
  1308.         }
  1309.         return false;
  1310.     }
  1311.    
  1312.     $scope.cancel = function() {
  1313.         $scope.editMode = false;
  1314.         $scope.foundDelegatesList = [];
  1315.         $scope.quotationDelegatesList = [];
  1316.         $scope.searchFilter = {};
  1317.     }
  1318.    
  1319.         function getFileNameFromHttpResponse(httpResponse) {
  1320.         var contentDispositionHeader = httpResponse.headers('Content-Disposition');
  1321.         var result = contentDispositionHeader.split(';')[1].trim().split('=')[1];
  1322.         return result.replace(/"/g, '');
  1323.     };
  1324.    
  1325.     // Operazioni dopo la selezione del venue
  1326.     $scope.selectedVenueNameItemChanged = function (item) {
  1327.         if (item !== undefined && item.name !== undefined){
  1328.             $scope.selectedVenueName = item.name;
  1329.             $scope.quotation.venueId = item.venueId;
  1330.             getVenuesDetails($scope.quotation.venueId);
  1331.         }
  1332.         // Clear fields
  1333.         if (item === undefined) {
  1334.             $scope.venueDetails = {};
  1335.         }
  1336.     }
  1337.    
  1338.     function getVenuesDetails(venueId) {
  1339.         $scope.loader = true;
  1340.         BookingService.getVenuesDetails(venueId)
  1341.             .then(
  1342.                 function(response) {
  1343.                     $scope.venueDetails = response;
  1344.                     $scope.venueDetails.streetAddress = "";
  1345.                     if ($scope.venueDetails.address1 != null)
  1346.                         $scope.venueDetails.streetAddress += $scope.venueDetails.address1+"\n";
  1347.                     if ($scope.venueDetails.address2 != null)
  1348.                         $scope.venueDetails.streetAddress += $scope.venueDetails.address2+"\n";
  1349.                     if ($scope.venueDetails.address3 != null)
  1350.                         $scope.venueDetails.streetAddress += $scope.venueDetails.address3+"\n";
  1351.                     if ($scope.venueDetails.address4 != null)
  1352.                         $scope.venueDetails.streetAddress += $scope.venueDetails.address4+"\n";
  1353.                     if ($scope.venueDetails.address5 != null)
  1354.                         $scope.venueDetails.streetAddress += $scope.venueDetails.address5;
  1355.                     $scope.loader = false;
  1356.                 }, function(errResponse) {
  1357.                     consol.error(errResponse);
  1358.                     $scope.loader = false;
  1359.                 }
  1360.             );
  1361.     }
  1362.    
  1363.     $scope.saveCommitProvider = function(){
  1364.         $scope.loader = true;
  1365.         var data = {};
  1366.         data.id = $scope.quotation.id;
  1367.         data.facilitatorId = $scope.quotation.facilitatorId;
  1368.         data.venueId = $scope.quotation.venueId;
  1369.         data.contractId = $scope.quotation.contractId;
  1370.         data.comments = $scope.quotation.comments;
  1371.         data.description = $scope.quotation.description;
  1372.         BookingService.saveCommitProvider(data)
  1373.             .then(
  1374.                 function(response) {
  1375.                     $scope.loader = false;
  1376.                     dialogPop($translate.instant('NSG_SIS_QUOTATION.SAVE_SUCCESS'));
  1377.                     $location.path( "/nsgsisfe/cco/manage-quotations" );
  1378.                     $scope.editMode = false;
  1379.                     $scope.getUserQuotations()
  1380.                 }, function(errResponse) {
  1381.                     dialogPop($translate.instant('NSG_SIS_QUOTATION.SAVE_ERROR'));
  1382.                     $scope.loader = false;
  1383.                 }
  1384.             );
  1385.     }
  1386. }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement