Exemplo n.º 1
0
 public static function Reverse($txid)
 {
     try {
         $sql = 'SELECT * FROM transactions WHERE id = ' . $txid;
         // Execute the query and return the results
         $tx = DatabaseHandler::GetRow($sql);
         if (empty($tx['id'])) {
             return false;
         }
         $entries = AccountEntry::GetTransactionEntries($txid);
         foreach ($entries as $entry) {
             if ($entry['status'] == 3) {
                 return false;
             }
         }
     } catch (Exception $e) {
         Logger::Log('Transaction', 'Exception', $e->getMessage());
         return false;
     }
     $amount = new Money($tx['amount'], Currency::Get('KES'));
     $transaction = new FinancialTransaction($amount, 'Reversal for TX id: ' . $tx['id'], new Reversal());
     $cr = 0.0;
     $dr = 0.0;
     foreach ($entries as $entry) {
         switch ($entry['ledger_name']) {
             case 'Creditors':
                 $table = 'suppliers';
                 break;
             case 'Debtors':
                 $table = 'clients';
                 break;
             case 'Payroll':
                 $table = 'employees';
                 break;
             default:
                 break;
         }
         if ($entry['effect'] == 'cr') {
             $dr = $dr + $entry['amount'];
             $dramt = new Money(floatval($entry['amount']), Currency::Get('KES'));
             if ($entry['ledger_id'] == $entry['account_no']) {
                 $account = Account::GetLedger($entry['ledger_id']);
             } else {
                 $account = Account::GetAccountByNo($entry['account_no'], $table, $entry['ledger_name']);
             }
             $entry = new AccountEntry($transaction, $account, $dramt, $transaction->date, 'dr');
             $entry->reversal = 3;
             $transaction->add($entry);
         } else {
             if ($entry['effect'] == 'dr') {
                 $cr = $cr + $entry['amount'];
                 $cramt = new Money(floatval($entry['amount']), Currency::Get('KES'));
                 if ($entry['ledger_id'] == $entry['account_no']) {
                     $account = Account::GetLedger($entry['ledger_id']);
                 } else {
                     $account = Account::GetAccountByNo($entry['account_no'], $table, $entry['ledger_name']);
                 }
                 $entry = new AccountEntry($transaction, $account, $cramt, $transaction->date, 'cr');
                 $entry->reversal = 3;
                 $transaction->add($entry);
             }
         }
     }
     if ($cr - $dr == 0.0) {
         foreach ($transaction->entries as $entry) {
             $entry->post();
         }
         try {
             $sql = 'UPDATE transactions SET status = 1, entries = ' . count($transaction->entries) . ', user = "******" WHERE id = ' . $transaction->transactionId;
             DatabaseHandler::Execute($sql);
             $transaction->posted = true;
             $sqla = 'UPDATE general_ledger_entries SET status = 3 WHERE transaction_id = ' . $txid;
             DatabaseHandler::Execute($sqla);
             Logger::Log('Transaction', 'Ok', 'Transaction id:' . $transaction->transactionId . ' posted by ' . SessionManager::GetUsername());
             return true;
         } catch (Exception $e) {
         }
     } else {
         //throw new Exception("The entries are not conservative. Probable system leak!");
         Logger::Log('Transaction', 'Exception', 'The entries are not conservative. Probable system leak! CR: ' . $cr . ', DR: ' . $dr);
         return false;
     }
     //verify account and entry is of the same resource type;
     //enter the
 }