public function getItemEarnings(GcrTransactionItem $item)
 {
     $earnings = 0;
     $purchase = $item->getPurchase();
     if ($purchase->isPayoff()) {
         $payoff = GcrPayoffTable::getInstance()->find($purchase->getPurchaseTypeId());
         if ($payoff->isEschoolPayoff()) {
             $earnings = $item->getAmount();
         }
     } else {
         if ($this->isRemoteItem($item)) {
             $earnings = $item->getDistribution()->getCommission();
         } else {
             $earnings = $item->getDistribution()->getOwner();
         }
     }
     return $earnings;
 }
 public function deleteInstitutionFromSystemEntirely()
 {
     // delete each moodle instance owned by this institution
     foreach ($this->getEschools() as $eschool) {
         $eschool->deleteEschoolFromSystemEntirely();
     }
     // delete this institution's database schema
     GcrDatabaseAccessPostgres::deleteSchemaFromSystem($this);
     // delete moodledata directory for institution
     exec(escapeshellcmd('rm -R ' . gcr::moodledataDir . $this->short_name . '/'));
     // delete password salt history for this institution
     Doctrine_Query::create()->delete('GcrInstitutionSaltHistory')->where('institutionid = ?', $this->id)->execute();
     Doctrine_Query::create()->delete('GcrTrial t')->where('t.organization_id = ?', $this->id)->execute();
     GcrChainedPaymentTable::getInstance()->createQuery('c')->delete()->where('c.user_institution_id = ?', $this->short_name)->execute();
     GcrChatSessionTable::getInstance()->createQuery('c')->delete()->where('c.eschool_id = ?', $this->short_name)->execute();
     GcrChatSessionUsersTable::getInstance()->createQuery('c')->delete()->where('c.user_eschool_id = ?', $this->short_name)->execute();
     GcrChatSessionInviteTable::getInstance()->createQuery('c')->delete()->where('c.user_eschool_id = ?', $this->short_name)->orWhere('c.from_user_eschool_id = ?', $this->short_name)->execute();
     GcrCommissionTable::getInstance()->createQuery('c')->delete()->where('c.institution_id = ?', $this->short_name)->execute();
     GcrEclassroomTable::getInstance()->createQuery('e')->delete()->where('e.user_institution_id = ?', $this->short_name)->execute();
     GcrEschoolMonthlyDataTable::getInstance()->createQuery('e')->delete()->where('e.eschool_id = ?', $this->short_name)->execute();
     GcrUserMonthlyDataTable::getInstance()->createQuery('u')->delete()->where('u.user_institution_id = ?', $this->short_name)->execute();
     GcrPayoffTable::getInstance()->createQuery('p')->delete()->where('p.user_eschool_id = ?', $this->short_name)->execute();
     GcrPurchaseTable::deleteInstitutionRecords($this);
     GcrUserStorageS3Table::deleteBucket($this);
     if ($address = $this->getAddressObject()) {
         $address->delete();
     }
     if ($contact1 = $this->getPersonObject()) {
         $contact1->delete();
     }
     if ($contact2 = $this->getPerson2Object()) {
         $contact2->delete();
     }
     $this->delete();
 }