/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'show' page.
  */
 public function actionCreate()
 {
     $model = new TransRow();
     if (isset($_POST['TransRow'])) {
         $model->attributes = $_POST['TransRow'];
         if ($model->save()) {
             $this->redirect(array('show', 'id' => $model->id));
         }
     }
     $this->render('create', array('model' => $model));
 }
 private function copyTempTransToNewTrans($periodNum, $companyNum, $isUpdateRegDate = false)
 {
     //everything is ok.  Make the new transaction
     $models = $this->getFromTempTrans();
     $cLoc = CLocale::getInstance('en');
     // CHECK IF USER CAN COMMIT NEGAIVE ASSET TRANSACTIONS
     $negativeAssetTrans = Yii::app()->user->getState('negativeAssetTrans');
     if ($negativeAssetTrans == 0) {
         foreach ($models as $model) {
             // only if account is 399999 or less
             $sql = "SELECT Account.code AS accountcode\n\t\t\t\t\t\t\t\tFROM Account\n\t\t\t\t\t\t\t\tWHERE Account.id = " . $model->accountId;
             $accounts = Yii::app()->db->createCommand($sql)->queryAll();
             if (!empty($accounts)) {
                 $account_code = intval($accounts[0]['accountcode']);
                 if ($account_code < 399999) {
                     $amountcredit = $this->parseNumber($model->amountcredit, $cLoc);
                     if ($amountcredit != 0) {
                         $pass = $this->checkAccountNegative($model->accountId, $amountcredit);
                         if (!$pass) {
                             return NULL;
                         }
                     }
                 }
             }
         }
     }
     $trans = new Trans();
     $tmpDate = date_create_from_format('d M Y', $models[0]->invDate);
     $trans->invDate = date_format($tmpDate, 'Y-m-d');
     // $trans->invDate=User::parseDate($models[0]->invDate,$cLoc);
     if ($isUpdateRegDate) {
         $trans->regDate = date('Y-m-d');
     } else {
         $tmpDate = date_create_from_format('d M Y', $models[0]->regDate);
         $trans->regDate = date_format($tmpDate, 'Y-m-d');
     }
     $trans->periodNum = $periodNum;
     $trans->companyNum = $companyNum;
     $trans->notes = $models[0]->notesheader;
     $trans->fileInfo = $models[0]->fileInfo;
     $trans->reconciled = $models[0]->reconciled;
     $trans->companyId = Yii::app()->user->getState('selectedCompanyId');
     $trans->periodId = Yii::app()->user->getState('selectedPeriodId');
     $trans->save();
     foreach ($models as $model) {
         $amountdebit = $this->parseNumber($model->amountdebit, $cLoc);
         $amountcredit = $this->parseNumber($model->amountcredit, $cLoc);
         if ($amountdebit != 0 || $amountcredit != 0) {
             $transrow = new TransRow();
             $transrow->transId = $trans->id;
             $transrow->accountId = $model->accountId;
             $transrow->customerId = $model->customerId;
             $transrow->donorId = $model->donorId;
             $transrow->notes = $model->notes;
             $transrow->amount = $amountdebit > 0 ? $amountdebit : -$amountcredit;
             $transrow->save();
         }
     }
     return $trans;
 }
 private function copyTempTransToNewTrans($models, $template, $periodNum, $companyNum, $rowcount, $isUpdateRegDate = false)
 {
     //everything is ok.  Make the new transaction
     $models = $this->getFromTempTrans($template, $rowcount);
     $this->finalAdjustTransFromTemplate($models, $template);
     $trans = new Trans();
     $cLoc = CLocale::getInstance('en');
     $trans->invDate = User::parseDate($models[0]->invDate, $cLoc);
     if ($isUpdateRegDate) {
         $trans->regDate = date('Y-m-d');
     } else {
         $trans->regDate = User::parseDate($models[0]->regDate, $cLoc);
     }
     $trans->periodNum = $periodNum;
     $trans->companyNum = $companyNum;
     $trans->notes = $models[0]->notesheader;
     $trans->reconciled = $models[0]->reconciled;
     $trans->fileInfo = $models[0]->fileInfo;
     $trans->companyId = Yii::app()->user->getState('selectedCompanyId');
     $trans->periodId = Yii::app()->user->getState('selectedPeriodId');
     $trans->save();
     foreach ($models as $n => $model) {
         $amountdebit = TransController::parseNumber($model->amountdebit, $cLoc);
         $amountcredit = TransController::parseNumber($model->amountcredit, $cLoc);
         if ($amountdebit != 0 || $amountcredit != 0) {
             $transrow = new TransRow();
             $transrow->transId = $trans->id;
             $transrow->accountId = $model->accountId;
             $transrow->customerId = $model->customerId;
             $transrow->donorId = $model->donorId;
             $transrow->notes = $model->notes;
             $transrow->amount = $amountdebit > 0 ? $amountdebit : -$amountcredit;
             $transrow->save();
         }
     }
     return $trans;
 }
 private function importCompany($fileNameAndPath)
 {
     $allAccounts = array();
     $dom = new domDocument();
     if (!$dom->load($fileNameAndPath)) {
         throw new CException(Yii::t('lazy8', 'input file could not be xml parsed'));
     }
     $root = $dom->documentElement;
     if ($root->nodeName != "lazy8webport") {
         $this->hasErrors = true;
         $this->errors = array(array(Yii::t('lazy8', 'Upload failed.  This is not a valid file.'), Yii::t('lazy8', 'Select a file and try again')));
         $this->render('showimport');
         return 0;
     }
     if ($root->getAttribute('version') > 1.0) {
         $this->errors = array(array(Yii::t('lazy8', 'There maybe problems because this is a file version greater then this programs version'), Yii::t('lazy8', 'Select a file and try again')));
     }
     $nodeCompanys = $root->getElementsByTagName('company');
     unset($root);
     unset($dom);
     $this->lastImportedPeriod = 0;
     foreach ($nodeCompanys as $nodeCompany) {
         //make sure the company code is unique
         $modelCompany = new Company();
         $code = $nodeCompany->getAttribute('code');
         $code--;
         //make sure the company code is valid. Change if not.
         do {
             $code++;
             $comptest = Company::model()->find(array('condition' => 'code=' . $code));
         } while ($comptest !== null);
         //create the company
         $modelCompany = new Company();
         $modelCompany->code = $code;
         $modelCompany->name = $nodeCompany->getAttribute('name');
         $modelCompany->lastAbsTransNum = $nodeCompany->getAttribute('lastAbsTransNum');
         if (!$modelCompany->save()) {
             throw new CException(Yii::t('lazy8', 'Could not create the company, bad paramters') . ';' . var_export($modelCompany->getErrors()));
         }
         try {
             $allAccounts = array();
             $nodesAccountTypes = $nodeCompany->getElementsByTagName('accounttype');
             foreach ($nodesAccountTypes as $nodeAccountType) {
                 $modelAccountType = new AccountType();
                 $modelAccountType->companyId = $modelCompany->id;
                 $modelAccountType->code = $nodeAccountType->getAttribute('code');
                 $modelAccountType->name = $nodeAccountType->getAttribute('name');
                 $modelAccountType->sortOrder = $nodeAccountType->getAttribute('sortorder');
                 $modelAccountType->isInBalance = $nodeAccountType->getAttribute('isinbalance') == "1" ? 1 : 0;
                 if (!$modelAccountType->save()) {
                     $modelCompany->delete();
                     throw new CException(Yii::t('lazy8', 'Could not create the AccountType, bad paramters') . ';name=' . $modelAccountType->name . ';' . serialize($modelAccountType->getErrors()));
                 }
                 $nodesAccounts = $nodeAccountType->getElementsByTagName('account');
                 foreach ($nodesAccounts as $nodeAccount) {
                     $modelAccount = new Account();
                     $modelAccount->companyId = $modelCompany->id;
                     $modelAccount->code = $nodeAccount->getAttribute('code');
                     $modelAccount->accountTypeId = $modelAccountType->id;
                     $modelAccount->name = $nodeAccount->getAttribute('name');
                     if (!$modelAccount->save()) {
                         $modelCompany->delete();
                         throw new CException(Yii::t('lazy8', 'Could not create the Account, bad paramters') . ';' . serialize($modelAccount->getErrors()));
                     }
                     $allAccounts[$modelAccount->code] = $modelAccount->id;
                     unset($nodeAccount);
                     unset($modelAccount);
                 }
                 unset($modelAccountType);
                 unset($nodeAccountType);
             }
             unset($nodesAccountTypes);
             $allCustomers = array();
             $nodesCustomers = $nodeCompany->getElementsByTagName('customer');
             foreach ($nodesCustomers as $nodeCustomer) {
                 $modelCustomer = new Customer();
                 $modelCustomer->companyId = $modelCompany->id;
                 $modelCustomer->code = $nodeCustomer->getAttribute('code');
                 $modelCustomer->accountId = $this->FindAccountIdFromCode($nodeCustomer->getAttribute('accountcode'), $modelCompany->id, $allAccounts, 'customercode=' . $modelCustomer->code, $this->errors, true);
                 $modelCustomer->name = $nodeCustomer->getAttribute('name');
                 $modelCustomer->desc = $nodeCustomer->getAttribute('desc');
                 if (!$modelCustomer->save()) {
                     $modelCompany->delete();
                     throw new CException(Yii::t('lazy8', 'Could not create the Customer, bad paramters') . ';' . serialize($modelCustomer->getErrors()));
                 }
                 $allCustomers[$modelCustomer->code] = $modelCustomer->id;
                 unset($modelCustomer);
                 unset($nodeCustomer);
             }
             unset($nodesCustomers);
             $nodesPeriods = $nodeCompany->getElementsByTagName('period');
             foreach ($nodesPeriods as $nodePeriod) {
                 $modelPeriod = new Period();
                 $modelPeriod->companyId = $modelCompany->id;
                 $modelPeriod->dateStart = $nodePeriod->getAttribute('datestart');
                 $modelPeriod->dateEnd = $nodePeriod->getAttribute('dateend');
                 $modelPeriod->lastPeriodTransNum = $nodePeriod->getAttribute('lastperiodtransnum');
                 if (!$modelPeriod->save()) {
                     $modelCompany->delete();
                     throw new CException(Yii::t('lazy8', 'Could not create the period, bad paramters') . ';' . serialize($modelPeriod->getErrors()));
                 }
                 $this->lastImportedPeriod = $modelPeriod->id;
                 $nodesTransactions = $nodePeriod->getElementsByTagName('transaction');
                 foreach ($nodesTransactions as $nodeTransaction) {
                     $modelTransaction = new Trans();
                     $modelTransaction->companyId = $modelCompany->id;
                     $modelTransaction->companyNum = $nodeTransaction->getAttribute('code');
                     $modelTransaction->periodId = $modelPeriod->id;
                     $modelTransaction->periodNum = $nodeTransaction->getAttribute('periodnum');
                     $modelTransaction->regDate = $nodeTransaction->getAttribute('regdate');
                     $modelTransaction->invDate = $nodeTransaction->getAttribute('invdate');
                     $modelTransaction->notes = $nodeTransaction->getAttribute('notes');
                     $modelTransaction->fileInfo = $nodeTransaction->getAttribute('fileinfo');
                     if (!$modelTransaction->save()) {
                         $modelCompany->delete();
                         throw new CException(Yii::t('lazy8', 'Could not create the Transaction, bad paramters') . ';' . serialize($modelTransaction->getErrors()));
                     }
                     $nodesTransactionAmounts = $nodeTransaction->getElementsByTagName('amount');
                     foreach ($nodesTransactionAmounts as $nodeTransactionAmount) {
                         $modelTransRow = new TransRow();
                         $modelTransRow->transId = $modelTransaction->id;
                         $modelTransRow->accountId = $this->FindAccountIdFromCode($nodeTransactionAmount->getAttribute('accountcode'), $modelCompany->id, $allAccounts, 'TransCode=' . $modelTransaction->companyNum, $this->errors, true, true);
                         $modelTransRow->customerId = $this->FindCustomerIdFromCode($nodeTransactionAmount->getAttribute('customercode'), $modelCompany->id, $allCustomers, 'TransCode=' . $modelTransaction->companyNum, $this->errors, true);
                         $modelTransRow->notes = $nodeTransactionAmount->getAttribute('notes');
                         $modelTransRow->amount = $nodeTransactionAmount->getAttribute('amount');
                         if (!$modelTransRow->save()) {
                             $modelCompany->delete();
                             throw new CException(Yii::t('lazy8', 'Could not create the TransactionAmount, bad paramters') . ';' . serialize($modelTransRow->getErrors()));
                         }
                         unset($modelTransRow);
                     }
                     unset($modelTransaction);
                     unset($nodesTransactionAmounts);
                     unset($nodeTransaction);
                 }
                 unset($modelPeriod);
                 unset($nodePeriod);
                 unset($nodesTransactions);
             }
             unset($nodesPeriods);
         } catch (Exception $e) {
             $modelCompany->delete();
             throw $e;
         }
         $errors = array();
         //we ignore the errors...
         yii::app()->onImport(new Lazy8Event(array('importobject' => 'Company', 'root' => $nodeCompany, 'errors' => $errors), $modelCompany->id));
         unset($nodeCompany);
     }
     unset($nodeCompanys);
     ChangeLog::addLog('ADD', 'Company', 'Imported company ' . $modelCompany->toString());
     return $modelCompany->id;
 }