/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'show' page. */ public function actionCreate() { $model = new AccountType(); if (isset($_POST['American']) || isset($_POST['Swedish'])) { $filename = "DefaultAccountTypes.US.xml"; if (isset($_POST['Swedish'])) { $filename = "DefaultAccountTypes.SE.xml"; } ChangeLog::addLog('ADD', 'AccountType', 'Added default AccountTypes ' . $filename); $errors = Account::ImportAccounts(dirname(__FILE__) . DIRECTORY_SEPARATOR . $filename); if (isset($errors) && count($errors) > 0) { foreach ($errors as $error) { $model->addError('id', $error); } } else { $this->redirect(array('account/create')); } } elseif (isset($_POST['AccountType'])) { $model->attributes = $_POST['AccountType']; $model->companyId = Yii::app()->user->getState('selectedCompanyId'); if ($model->save()) { ChangeLog::addLog('ADD', 'AccountType', $model->toString()); $this->redirect(array('admin', 'id' => $model->id)); } } $criteria = new CDbCriteria(); $criteria->addSearchCondition('companyId', Yii::app()->user->getState('selectedCompanyId')); $models = AccountType::model()->findAll($criteria); $isAccountTypesInCompany = isset($models) && count($models) > 0; $model->dateChanged = User::getDateFormatted(date('Y-m-d')); $this->render('create', array('model' => $model, 'isAccountTypesInCompany' => $isAccountTypesInCompany)); }
/** * @return array Errors Import Account information */ public static function ImportAccounts($fileNameAndPath) { $errors = array(); $dom = new domDocument(); if (!$dom->load($fileNameAndPath)) { $errors[] = Yii::t('lazy8', 'Upload failed. This is not a valid file.'); $errors[] = Yii::t('lazy8', 'Select a file and try again'); return $errors; } $root = $dom->documentElement; if ($root->nodeName != "lazy8webportaccount") { $errors[] = Yii::t('lazy8', 'Upload failed. This is not a valid file.'); $errors[] = Yii::t('lazy8', 'Select a file and try again'); return $errors; } if ($root->getAttribute('version') > 1.0) { $errors[] = Yii::t('lazy8', 'There maybe problems because this is a file version greater then this programs version'); $errors[] = Yii::t('lazy8', 'Select a file and try again'); } $nodesAccountTypes = $root->getElementsByTagName('accounttype'); foreach ($nodesAccountTypes as $nodeAccountType) { $modelAccountType = new AccountType(); $modelAccountType->companyId = Yii::app()->user->getState('selectedCompanyId'); $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()) { $errors[] = Yii::t('lazy8', 'Could not create the AccountType, bad paramters') . ';name=' . $modelAccountType->name . ';' . serialize($modelAccountType->getErrors()); return $errors; } $nodesAccounts = $nodeAccountType->getElementsByTagName('account'); foreach ($nodesAccounts as $nodeAccount) { $modelAccount = new Account(); $modelAccount->companyId = Yii::app()->user->getState('selectedCompanyId'); $modelAccount->code = $nodeAccount->getAttribute('code'); $modelAccount->accountTypeId = $modelAccountType->id; $modelAccount->name = $nodeAccount->getAttribute('name'); $modelAccount->email = $nodeAccount->getAttribute('email'); $modelAccount->balance_threshold = $nodeAccount->getAttribute('balance_threshold'); $modelAccount->days = $nodeAccount->getAttribute('days'); $modelAccount->report_id = $nodeAccount->getAttribute('report_id'); if (!$modelAccount->save()) { $modelAccountType->delete(); $errors[] = Yii::t('lazy8', 'Could not create the Account, bad paramters') . ';' . serialize($modelAccount->getErrors()); return $errors; } } } return $errors; }
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; }