Example #1
0
 public function post(&$errors = array())
 {
     if ($this->hasBeenPosted()) {
         $errors[] = 'Expense has already been posted';
         return false;
     }
     $db = DB::Instance();
     $db->StartTrans();
     $transaction = DataObjectFactory::Factory('ELTransaction');
     $trans_id = $db->GenID('ELTransactions_id_seq');
     //copy across the fields that are needed
     foreach ($this->getFields() as $fieldname => $field) {
         if ($transaction->isField($fieldname)) {
             $transaction->{$fieldname} = $this->{$fieldname};
         }
     }
     $transaction->id = $trans_id;
     $transaction->transaction_type = 'E';
     $transaction->status = 'O';
     $transaction->our_reference = $this->expense_number;
     $prefixes = array('', 'base_', 'twin_');
     //the outstanding (os) values are the gross values to begin with
     foreach ($prefixes as $prefix) {
         $transaction->{$prefix . 'os_value'} = $transaction->{$prefix . 'gross_value'};
     }
     $transaction->transaction_date = $this->expense_date;
     $result = $transaction->save($this, $errors);
     // Create and save the GL Transactions
     if ($result) {
         $gl_transactions = GLTransaction::makeFromExpenseTransaction($transaction, $this, $errors);
         if (!is_array($gl_transactions) || count($errors) > 0) {
             $result = false;
         } else {
             $result = GLTransaction::saveTransactions($gl_transactions, $errors);
         }
     }
     // Update the Expense Claim header status
     if ($result) {
         $this->status = $this->statusAwaitingPayment();
         $result = $this->save();
     }
     if (!$result) {
         $message = $db->ErrorMsg();
         if (!empty($message)) {
             $errors[] = $message;
         }
         $db->FailTrans();
     }
     $db->CompleteTrans();
     return $result;
 }