~fabiocbalbuquerque/sahana-agasti/web-services

« back to all changes in this revision

Viewing changes to apps/frontend/lib/util/agFacilityImportNormalization.class.php

  • Committer: Chad Heuschober
  • Date: 2011-08-03 03:01:29 UTC
  • mto: (1.26.1 push-trunk)
  • mto: This revision was merged to the branch mainline in revision 25.
  • Revision ID: chad.heuschober@mail.cuny.edu-20110803030129-8bs5t3dqbv1n5ou9
Completed new staff export code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
103
103
            ->from('agFacilityResourceAllocationStatus fras')
104
104
            ->execute(array(), agDoctrineQuery::HYDRATE_KEY_VALUE_PAIR);
105
105
    $this->facRscAllocStatusIds = array_change_key_case($this->facRscAllocStatusIds, CASE_LOWER);
106
 
 
107
106
  }
108
107
 
109
108
  public function __setScenario($scenarioId)
304
303
      'throwOnError' => TRUE,
305
304
      'method' => 'setResourceRequirements',
306
305
    );
 
306
 
307
307
  }
308
308
 
309
309
  /**
485
485
                             'facility_capacity' => NULL
486
486
                            );
487
487
 
 
488
    $uniqFacRscTypes = array();
488
489
    // Check for valid facility resource type abbr and facility resource status provided.
489
490
    // Meanwhile also build the $rawFacilityResources array for later use.
490
491
    foreach ($this->importData AS $rowId => $rowData)
543
544
      $rawFacilityResources[$rowId]['facility_id'] = $rowData['primaryKeys']['facility_id'];
544
545
      $facRscTypeId = $facRscTypeAbbrIds[strtolower($rowData['_rawData']['facility_resource_type_abbr'])];
545
546
      $rawFacilityResources[$rowId]['facility_resource_type_id'] = $facRscTypeId;
 
547
      $uniqFacRscTypes[$facRscTypeId] = TRUE;
546
548
    }
547
549
 
548
550
    // Query for related facility resource in a doctrine collection.
622
624
    }
623
625
    $facilityResourceColl->free(TRUE);
624
626
    unset($facilityResourceColl);
 
627
 
 
628
    $defaultScenarioFacilityResourceTypesTable = $conn->getTable('agDefaultScenarioFacilityResourceType');
 
629
    $defaultScenarioFacilityResourceTypes = agDoctrineQuery::create($conn)
 
630
      ->select('dsfrt.*')
 
631
        ->from('agDefaultScenarioFacilityResourceType dsfrt INDEXBY dsfrt.facility_resource_type_id')
 
632
        ->where('dsfrt.scenario_id = ?', $this->scenarioId)
 
633
        ->execute(array());
 
634
 
 
635
    foreach ($uniqFacRscTypes as $id => $value) {
 
636
      if (!isset($defaultScenarioFacilityResourceTypes[$id])) {
 
637
        $defaultScenarioFacilityResourceType = new agDefaultScenarioFacilityResourceType($defaultScenarioFacilityResourceTypesTable, TRUE);
 
638
        $defaultScenarioFacilityResourceType['scenario_id'] = $this->scenarioId;
 
639
        $defaultScenarioFacilityResourceType['facility_resource_type_id'] = $id;
 
640
        $defaultScenarioFacilityResourceTypes->add($defaultScenarioFacilityResourceType, $id);
 
641
      }
 
642
    }
 
643
    $defaultScenarioFacilityResourceTypes->save($conn);
 
644
    $defaultScenarioFacilityResourceTypes->free(TRUE);
 
645
    unset($defaultScenarioFacilityResourceType);
 
646
    unset($defaultScenarioFacilityResourceTypes);
 
647
    unset($uniqFacRscTypes);
625
648
  }
626
649
 
627
650
  /**
969
992
  protected function setResourceRequirements($throwOnError, Doctrine_Connection $conn)
970
993
  {
971
994
    $err = FALSE;
 
995
 
 
996
    if (empty($this->staffResourceTypes)) {
 
997
      $this->staffResourceTypes = agDoctrineQuery::create()
 
998
      ->select('srt.id')
 
999
          ->addSelect('srt.staff_resource_type_abbr')
 
1000
        ->from('agStaffResourceType srt')
 
1001
      ->execute(array(),agDoctrineQuery::HYDRATE_KEY_VALUE_PAIR);
 
1002
      foreach ($this->staffResourceTypes as $id => &$abbr) {
 
1003
        $abbr = $this->cleanColumnName($abbr);
 
1004
        $minSpec = $abbr . '_min';
 
1005
        $maxSpec = $abbr . '_max';
 
1006
 
 
1007
        if (isset($this->importSpec[$minSpec]) && !isset($this->importSpec[$maxSpec])) {
 
1008
          $errMsg = 'Missing paired column ' . $maxSpec . ' from import file.';
 
1009
          $err = TRUE;
 
1010
        } elseif (!isset($this->importSpec[$minSpec]) && isset($this->importSpec[$maxSpec])) {
 
1011
          $errMsg = 'Missing paired column ' . $minSpec . ' from import file.';
 
1012
          $err = TRUE;
 
1013
        }
 
1014
 
 
1015
        if ($err) {
 
1016
          $this->eh->logErr($errMsg);
 
1017
 
 
1018
          if ($throwOnError) {
 
1019
            throw new Exception($errMsg);
 
1020
          }
 
1021
        }
 
1022
      }
 
1023
      unset($abbr);
 
1024
      $this->staffResourceTypes = array_flip($this->staffResourceTypes);
 
1025
    }
 
1026
 
 
1027
    $err = FALSE;
972
1028
    $missingColumn = 0;
973
1029
    $negativeNum = 0;
974
1030
 
1002
1058
      }
1003
1059
    }
1004
1060
 
 
1061
    $defaultStaffResourceTypesTable = $conn->getTable('agDefaultScenarioStaffResourceType');
1005
1062
    $defaultStaffResourceTypes = agDoctrineQuery::create($conn)
1006
1063
      ->select('dssrt.*')
1007
 
        ->from('agDefaultScenarioStaffResouceType dssrt INDEXBY dssrt.staff_resource_type_id')
 
1064
        ->from('agDefaultScenarioStaffResourceType dssrt INDEXBY dssrt.staff_resource_type_id')
1008
1065
        ->where('dssrt.scenario_id = ?', $this->scenarioId)
1009
1066
        ->execute(array());
1010
1067
 
1011
1068
    $rscReqs = array();
 
1069
    $uniqResourceTypes = array();
1012
1070
    foreach ($this->importData as $rowId => $rowData) {
1013
1071
      $rawData = $rowData['_rawData'];
1014
1072
      $scFacRscId = $rowData['primaryKeys']['scenario_facility_resource_id'];
1022
1080
          $min = $rawData[$minSpec];
1023
1081
          $max = $rawData[$maxSpec];
1024
1082
 
1025
 
          if (ctype_digit($min) && ctype($max) && $min <= $max) {
1026
 
            $tmpRscTypes[$abbr] = array($min, $max);
 
1083
          if (ctype_digit($min) && ctype_digit($max) && $min <= $max) {
 
1084
            $tmpRscReqs[$abbr] = array($min, $max);
 
1085
            $uniqResourceTypes[$abbr] = $id;
1027
1086
          } else {
1028
1087
            $negativeNum++;
1029
1088
          }
1030
1089
 
1031
 
        } elseif (isset($rawData[$minSpec]) || $rawData[$maxSpec]) {
 
1090
        } elseif (isset($rawData[$minSpec]) || isset($rawData[$maxSpec])) {
1032
1091
          $missingColumn++;
1033
1092
        }
1034
 
        
1035
1093
      }
1036
1094
      $rscReqs[$scFacRscId] = $tmpRscReqs;
1037
1095
    }
1040
1098
 
1041
1099
    $facilityStaffResources = agDoctrineQuery::create($conn)
1042
1100
      ->select('fsr.*')
1043
 
        ->from('agFacilityStaffResource')
1044
 
        ->whereIn('scenario_facility_resource_id', array_keys($scFacRscIds))
 
1101
        ->from('agFacilityStaffResource fsr')
 
1102
        ->whereIn('fsr.scenario_facility_resource_id', array_keys($rscReqs))
1045
1103
        ->execute(array());
 
1104
    $facilityStaffResourceTable = $facilityStaffResources->getTable();
 
1105
    $facilityStaffResourceTable->setConnection($conn);
1046
1106
 
1047
1107
    foreach ($facilityStaffResources as $index => $facilityStaffResource) {
1048
1108
      $scFacRscId = $facilityStaffResource['scenario_facility_resource_id'];
1065
1125
    unset($facilityStaffResource);
1066
1126
    unset($facilityStaffResources);
1067
1127
 
1068
 
    $facilityStaffResourceTable = $conn->getTable('agFacilityStaffResource');
1069
1128
    $facilityStaffResources = new Doctrine_Collection('agFacilityStaffResource');
1070
1129
    foreach ($rscReqs as $scFacRscId => $rscReq) {
1071
1130
      foreach ($rscReq as $abbr => $values) {
1072
1131
        if ($values[0] == 0 && $values[1] == 0) {
1073
1132
          continue;
1074
1133
        }
 
1134
        $rscTypeId = $this->staffResourceTypes[$abbr];
 
1135
 
1075
1136
        $facilityStaffResource = new agFacilityStaffResource($facilityStaffResourceTable, TRUE);
1076
1137
        $facilityStaffResource['scenario_facility_resource_id'] = $scFacRscId;
1077
 
        $facilityStaffResource['staff_resource_type_id'] = $this->staffResourceTypes[$abbr];
 
1138
        $facilityStaffResource['staff_resource_type_id'] = $rscTypeId;
1078
1139
        $facilityStaffResource['minimum_staff'] = $values[0];
1079
1140
        $facilityStaffResource['maximum_staff'] = $values[1];
1080
1141
        $facilityStaffResources->add($facilityStaffResource);
1086
1147
    unset($facilityStaffResource);
1087
1148
    unset($facilityStaffResources);
1088
1149
 
 
1150
    foreach ($uniqResourceTypes as $abbr => $rscTypeId) {
 
1151
      if (!isset($defaultStaffResourceTypes[$rscTypeId])) {
 
1152
        $defaultStaffResourceType = new agDefaultScenarioStaffResourceType($defaultStaffResourceTypesTable, TRUE);
 
1153
        $defaultStaffResourceType['scenario_id'] = $this->scenarioId;
 
1154
        $defaultStaffResourceType['staff_resource_type_id'] = $rscTypeId;
 
1155
        $defaultStaffResourceTypes->add($defaultStaffResourceType, $rscTypeId);
 
1156
      }
 
1157
    }
 
1158
 
 
1159
    $defaultStaffResourceTypes->save($conn);
 
1160
    $defaultStaffResourceTypes->free(TRUE);
 
1161
    unset($defaultStaffResourceType);
 
1162
    unset($defaultStaffResourceTypes);
 
1163
    
1089
1164
    if ($missingColumn > 0) {
1090
1165
      $warnMsg = $missingColumn . ' cells missing paired min/max data. Please review your staff ' .
1091
1166
        'resource requirements to ensure data consistency.';
1092
 
      $this->eh->logWarn($warnMsg);
 
1167
      $this->eh->logWarning($warnMsg);
1093
1168
    }
1094
1169
    if ($negativeNum > 0) {
1095
1170
      $warnMsg = $negativeNum . ' cells with negative resource requirements or switched min/max ' .
1096
1171
      'values Please review your staff resource requirements to ensure data consistency.';
1097
 
      $this->eh->logWarn($warnMsg);
 
1172
      $this->eh->logWarning($warnMsg);
1098
1173
    }
1099
1174
  }
1100
1175