示例#1
0
 /**
   Write item update file(s) to ePlum
   @param $items [keyed array] of values. Keys correspond to WRITE_ITEM_FIELDS
     $items may also be an array of keyed arrays to write multiple items
     One additional key, ExpandedText, is used to write Expanded Text. This
     is separate from the Write Item operation so it's excluded from that
     set of fields.
   @param $scales [keyed array, optional] List of scales items will be written to
     Must have keys "host", "type", and "dept". 
     May have boolean value with key "new".
 */
 public static function writeItemsToScales($items, $scales = array())
 {
     $config = \FannieConfig::factory();
     if (!isset($items[0])) {
         $items = array($items);
     }
     $new_item = false;
     if (isset($items[0]['RecordType']) && $items[0]['RecordType'] == 'WriteOneItem') {
         $new_item = true;
     }
     $header_line = '';
     $file_prefix = ServiceScaleLib::sessionKey();
     $output_dir = $config->get('EPLUM_DIRECTORY');
     if ($output_dir == '') {
         return false;
     }
     $selected_scales = $scales;
     if (!is_array($scales) || count($selected_scales) == 0) {
         $selected_scales = $config->get('SCALES');
     }
     $scale_model = new \ServiceScalesModel(\FannieDB::get($config->get('OP_DB')));
     $counter = 0;
     $depts = array();
     foreach ($selected_scales as $scale) {
         $scale_model = ServiceScaleLib::getModelByHost($scale['host']);
         // batches run per-department rather than per-scale
         // so duplicates can be skipped
         if ($scale_model === false) {
             continue;
         } elseif (in_array($scale_model->epDeptNo(), $depts)) {
             continue;
         } else {
             $depts[] = $scale_model->epDeptNo();
         }
         $file_name = sys_get_temp_dir() . '/' . $file_prefix . '_writeItem_' . $counter . '.dat';
         $fptr = fopen($file_name, 'w');
         fwrite($fptr, 'BNA' . $file_prefix . '_' . $counter . chr(253) . self::$NEWLINE);
         foreach ($items as $item) {
             $item_line = self::getItemLine($item, $scale_model);
             fwrite($fptr, $item_line . self::$NEWLINE);
             if (isset($item['ExpandedText'])) {
                 $et_line = self::getIngredientLine($item, $scale_model);
                 fwrite($fptr, $et_line . self::$NEWLINE);
             }
         }
         fclose($fptr);
         // move to DGW; cleanup the file in the case of failure
         if (!rename($file_name, $output_dir . '/' . basename($file_name))) {
             unlink($file_name);
         }
         $counter++;
     }
 }