/**
  * Executes any command triggered on the admin page.
  */
 protected function processAdminCommand()
 {
     if (isset($_POST['command'], $_POST['id']) && $_POST['command'] === 'delete') {
         $trans = Trans::model()->findbyPk($_POST['id']);
         ChangeLog::addLog('DELETE', 'Trans', $trans->toString());
         $trans->delete();
         // reload the current page to avoid duplicated delete actions
         $this->refresh();
     }
 }
    ?>
</td>
    <td><?php 
    echo CHtml::encode($model->changedBy);
    ?>
</td>
    <td><?php 
    echo CHtml::encode(User::getDateFormatted($model->dateChanged, $cLoc, $dateformatter));
    ?>
</td>
    <td>
      <?php 
    echo CHtml::link(Yii::t('lazy8', 'Update'), array('update', 'id' => $model->id));
    ?>
      <?php 
    if (Trans::model()->find('periodId=' . $model->id) == null || Yii::app()->user->getState('allowReEditingOfTransactions')) {
        $deletebutton = CHtml::linkButton(Yii::t('lazy8', 'Delete'), array('submit' => '', 'params' => array('command' => 'delete', 'id' => $model->id), 'confirm' => Yii::t('lazy8', "Are you sure to delete?") . ' - ' . $model->dateStart . "\n" . Yii::t('lazy8', 'This will delete all transactions in this period')));
    } else {
        $deletebutton = CHtml::linkButton(Yii::t('lazy8', 'Delete'), array('submit' => '', 'params' => array('command' => 'admin', 'id' => $model->id), 'confirm' => Yii::t('lazy8', "You cannot delete this record because other records are dependent on it.")));
    }
    echo $deletebutton;
    ?>
	</td>
  </tr>
<?php 
}
?>
  </tbody>
</table>
<br/>
<?php 
 private function exportCompany($companyId = null)
 {
     $compName = "AllCompanies";
     if ($companyId != null) {
         $compName = CompanyController::replace_bad_filename_chars(Company::model()->findbyPk($companyId)->name);
     }
     // set headers
     header("Pragma: no-cache");
     header("Expires: 0");
     header("Content-Description: File Transfer");
     header("Content-Type: text/xml");
     header("Content-Disposition: attachment; filename=\"lazy8webExport.Company." . $compName . "." . date('Y-m-d_H.i.s') . ".xml\"");
     header("Content-Transfer-Encoding: binary");
     //safari can't deal with this header length zero
     //		header("Content-Length: " );
     $writer = new XMLWriter();
     // Output directly to the user
     //dirname(__FILE__).DIRECTORY_SEPARATOR.'../..'.'/assets/upload.sql'
     $writer->openURI('php://output');
     $writer->startDocument('1.0', 'utf-8');
     $writer->setIndent(4);
     $writer->startElement('lazy8webport');
     $writer->writeAttribute('version', '1.00');
     $companies = array();
     if (isset($companyId)) {
         $companies[] = Company::model()->findbyPk($companyId);
     } else {
         $companies = Company::model()->findAll(array('order' => 'code'));
     }
     foreach ($companies as $company) {
         ChangeLog::addLog('OTHER', 'Company', 'Exported company ' . $company->toString());
         $writer->startElement('company');
         $writer->writeAttribute("code", $company->code);
         $writer->writeAttribute("name", $company->name);
         $writer->writeAttribute("lastAbsTransNum", $company->lastAbsTransNum);
         $allAccounts = array();
         $accountTypes = AccountType::model()->findAll(array('condition' => 'companyId=' . $company->id, 'order' => 'code'));
         foreach ($accountTypes as $accountType) {
             $writer->startElement('accounttype');
             $writer->writeAttribute("code", $accountType->code);
             $writer->writeAttribute("name", $accountType->name);
             $writer->writeAttribute("sortorder", $accountType->sortOrder);
             $writer->writeAttribute("isinbalance", $accountType->isInBalance);
             $accounts = Account::model()->findAll(array('condition' => 'companyId=' . $company->id . ' AND accountTypeId=' . $accountType->id, 'order' => 'code'));
             foreach ($accounts as $account) {
                 $allAccounts[$account->id] = $account->code;
                 $writer->startElement('account');
                 $writer->writeAttribute("code", $account->code);
                 $writer->writeAttribute("name", $account->name);
                 $writer->endElement();
             }
             $writer->endElement();
         }
         $customers = Customer::model()->findAll(array('condition' => 'companyId=' . $company->id, 'order' => 'code'));
         $allCustomers = array();
         foreach ($customers as $customer) {
             $writer->startElement('customer');
             $allCustomers[$customer->id] = $customer->code;
             $writer->writeAttribute("code", $customer->code);
             $writer->writeAttribute("name", $customer->name);
             $writer->writeAttribute("desc", $customer->desc);
             if (isset($allAccounts[$customer->accountId])) {
                 $writer->writeAttribute("accountcode", $allAccounts[$customer->accountId]);
             } else {
                 $writer->writeAttribute("accountcode", 0);
             }
             $writer->endElement();
         }
         $periods = Period::model()->findAll(array('condition' => 'companyId=' . $company->id, 'order' => 'dateStart'));
         foreach ($periods as $period) {
             $writer->startElement('period');
             $writer->writeAttribute("datestart", $period->dateStart);
             $writer->writeAttribute("dateend", $period->dateEnd);
             $writer->writeAttribute("lastperiodtransnum", $period->lastPeriodTransNum);
             $transactions = Trans::model()->findAll(array('condition' => 'companyId=' . $company->id . ' AND periodId=' . $period->id, 'order' => 'companyNum'));
             foreach ($transactions as $transaction) {
                 $writer->startElement('transaction');
                 $writer->writeAttribute("code", $transaction->companyNum);
                 $writer->writeAttribute("periodnum", $transaction->periodNum);
                 $writer->writeAttribute("regdate", $transaction->regDate);
                 $writer->writeAttribute("invdate", $transaction->invDate);
                 $writer->writeAttribute("notes", $transaction->notes);
                 $writer->writeAttribute("fileinfo", $transaction->fileInfo);
                 $transrows = TransRow::model()->findAll(array('condition' => 'transId=' . $transaction->id, 'order' => 'amount DESC'));
                 foreach ($transrows as $transrow) {
                     $writer->startElement('amount');
                     $writer->writeAttribute("accountcode", isset($allAccounts[$transrow->accountId]) ? $allAccounts[$transrow->accountId] : 0);
                     $writer->writeAttribute("customercode", isset($allCustomers[$transrow->customerId]) ? $allCustomers[$transrow->customerId] : 0);
                     $writer->writeAttribute("notes", $transrow->notes);
                     $writer->writeAttribute("amount", $transrow->amount);
                     $writer->endElement();
                 }
                 $writer->endElement();
             }
             $writer->endElement();
         }
         yii::app()->onExport(new Lazy8Event(array('exportobject' => 'Company', 'writer' => $writer), $company->id));
         $writer->endElement();
     }
     $writer->endElement();
     $writer->endDocument();
     $writer->flush();
     return;
     //we may not send any more to the screen or it will mess up the file we just sent!
 }
 /**
  * Executes any command triggered on the admin page.
  */
 protected function processAdminCommand()
 {
     if (isset($_POST['command'], $_POST['id']) && $_POST['command'] === 'delete') {
         $deletePeriod = $this->loadPeriod($_POST['id']);
         if (Yii::app()->user->getState('allowReEditingOfTransactions') || Trans::model()->find('periodId=' . $_POST['id']) == null) {
             ChangeLog::addLog('DELETE', 'Period', $deletePeriod->toString());
             $deletePeriod->delete();
             $usersModel = User::model()->findbyPk(Yii::app()->user->id);
             $usersModel->setStates(true);
         }
         // reload the current page to avoid duplicated delete actions
         $this->refresh();
     }
 }
 public function delete()
 {
     parent::delete();
     yii::app()->onDeletePost(new Lazy8Event('Company', $this->id));
     $this->dbConnection->createCommand("DELETE FROM Customer WHERE companyId={$this->id}")->execute();
     $this->dbConnection->createCommand("DELETE FROM Period WHERE companyId={$this->id}")->execute();
     $this->dbConnection->createCommand("DELETE FROM Account WHERE companyId={$this->id}")->execute();
     $this->dbConnection->createCommand("DELETE FROM AccountType WHERE companyId={$this->id}")->execute();
     //Trans::model()->deleteAll('companyId=:companyId', array(':companyId'=> $this->id));
     $criteria = new CDbCriteria();
     $criteria->limit = 100;
     $criteria->condition = 'companyId=:companyId';
     $criteria->params = array(':companyId' => $this->id);
     do {
         $transList = Trans::model()->findAll($criteria);
         if ($transList != null) {
             foreach ($transList as $model) {
                 $model->delete();
             }
         }
     } while ($transList != null);
     /*$transList=Trans::model()->findAll('companyId=:companyId', array(':companyId'=> $this->id));
     		if($transList!=null){
     			foreach($transList as $model){
     				$model->delete();
     			}
     		}*/
     $this->dbConnection->createCommand("DELETE FROM Options WHERE companyId={$this->id}")->execute();
     $this->dbConnection->createCommand("DELETE FROM ChangeLog WHERE companyId={$this->id}")->execute();
     $reportList = Report::model()->findAll('companyId=:companyId', array(':companyId' => $this->id));
     if ($reportList != null) {
         foreach ($reportList as $model) {
             $model->delete();
         }
     }
     $this->dbConnection->createCommand("DELETE FROM CompanyUser WHERE companyId={$this->id}")->execute();
 }