Esempio n. 1
0
 protected function writeLog($_data, &$errors = array())
 {
     $db = DB::Instance();
     $db->StartTrans();
     $filename = $_data['name'];
     $_data['external_system_id'] = $this->external_system_id;
     $_data['data_definition_id'] = $this->id;
     $edilog = new EDITransactionLog();
     if (empty($_data['id'])) {
         $edilog->loadBy(array('name', 'status', 'data_definition_id'), array($filename, 'N', $this->id));
         if (!$edilog->isLoaded()) {
             $edilog->loadBy(array('name', 'status', 'data_definition_id'), array($filename, 'V', $this->id));
         }
         if (!$edilog->isLoaded()) {
             $edilog->loadBy(array('name', 'status', 'data_definition_id'), array($filename, 'E', $this->id));
             if ($edilog->isLoaded() && ($_data['action'] == 'AD' || $_data['action'] == 'AE')) {
                 return;
             }
         }
         if (!$edilog->isLoaded()) {
             $cc = new ConstraintChain();
             $cc->add(new Constraint('name', '=', $filename));
             $cc->add(new Constraint('status', '=', 'C'));
             $cc->add(new Constraint('data_definition_id', '=', $this->id));
             $cc->add(new Constraint('action', 'not in', "('I', 'S')"));
             $edilog->loadBy($cc);
         }
         if ($edilog->isLoaded()) {
             $_data['id'] = $edilog->id;
             foreach ($edilog->getFields() as $fieldname => $field) {
                 if (!isset($_data[$fieldname]) && $fieldname != 'status') {
                     $_data[$fieldname] = $edilog->{$fieldname};
                 }
             }
         }
     }
     if (count($errors) > 0) {
         $_data['status'] = 'E';
         $flash = Flash::Instance();
         $flash->save();
         $errors = array_merge_recursive($errors, $flash->getMessages('warnings'));
         $_data['message'] = implode(chr(10), $errors);
     } else {
         $_data['status'] = empty($_data['status']) ? EDITransactionLog::getStatusByAction($_data['action']) : $_data['status'];
         // If no message input, blank out the message field
         $_data['message'] = isset($_data['message']) ? $_data['message'] : '';
     }
     if (!$edilog->isLoaded() || $edilog->isLoaded() && ($edilog->action != $_data['action'] || $edilog->status != $_data['status'] || $edilog->message != $_data['message'])) {
         $edierrors = array();
         $edilog = EDITransactionLog::Factory($_data, $edierrors, 'EDITransactionLog');
         if ($edilog && count($edierrors) == 0) {
             if (!$edilog->save()) {
                 $edierrors[] = 'Error saving log entry : ' . $db->ErrorMsg();
             } else {
                 // Archive the existing entry to the history log table
                 $_data['edi_transactions_log_id'] = $edilog->id;
                 $_data['action'] = $edilog->action;
                 unset($_data['id']);
                 $ediloghistory = EDITransactionLogHistory::Factory($_data, $edierrors, 'EDITransactionLogHistory');
                 if (!$ediloghistory || !$ediloghistory->save()) {
                     $edierrors[] = 'Error saving log history entry : ' . $db->ErrorMsg();
                 }
             }
         }
     }
     if (count($edierrors) > 0) {
         $errors = array_merge_recursive($errors, $edierrors);
     }
     $db->CompleteTrans();
 }