public function delete()
 {
     parent::delete();
     $rowList = TemplateRowAccount::model()->findAll('templateRowId=:templateRowId', array(':templateRowId' => $this->id));
     if ($rowList != null) {
         foreach ($rowList as $model) {
             $model->delete();
         }
     }
 }
 private function updateRows(&$model)
 {
     $valid = true;
     if (isset($_POST['save']) && count($model->templateRows) < 2) {
         $model->addError('defaultAccountId', Yii::t('lazy8', 'You must have at least 2 rows in a template.'));
         $valid = false;
     }
     if ($model->templateRows !== null && count($model->templateRows) > 0) {
         //first time through just see if the data makes sense
         $repParams = $model->templateRows;
         $numBalances = 0;
         $multiLineRows = 0;
         foreach ($repParams as $n => $repParam) {
             //see if there is new data to be added..
             if (isset($_POST['save'])) {
                 $accounts = TemplateRowAccount::model()->findAll(array('condition' => 'templateRowId=' . $repParam->id));
                 if ($accounts == null || count($accounts) == 0) {
                     $model->addError('defaultAccountId', '#' . ($n + 1) . '--' . Yii::t('lazy8', 'You must have at least one account for each row.'));
                     $valid = false;
                 }
             }
             if ($_POST['TemplateRow'][$n]) {
                 $repParam->attributes = $_POST['TemplateRow'][$n];
             }
             if ($repParam->isFinalBalance) {
                 $numBalances++;
             }
             if ($repParam->allowRepeatThisRow) {
                 $multiLineRows++;
             }
             if ($repParam->isFinalBalance) {
                 //all others should be zero
                 if (strlen($repParam->defaultValue) > 0) {
                     $model->addError('[0]defaultValue', '#' . ($n + 1) . '--' . Yii::t('lazy8', 'Balance rows cannot have a default value.'));
                     $valid = false;
                 }
                 if ($repParam->allowMinus) {
                     $model->addError('[0]allowMinus', '#' . ($n + 1) . '--' . Yii::t('lazy8', 'Balance rows cannot have a minus value'));
                     $valid = false;
                 }
                 if (strlen($repParam->phpFieldCalc) > 0) {
                     $model->addError('[0]phpFieldCalc', '#' . ($n + 1) . '--' . Yii::t('lazy8', 'Balance rows cannot also be a php calculated field'));
                     $valid = false;
                 }
                 if ($repParam->allowChangeValue) {
                     $model->addError('[0]allowChangeValue', '#' . ($n + 1) . '--' . Yii::t('lazy8', 'Balance rows cannot allow to change value manually'));
                     $valid = false;
                 }
                 if ($repParam->allowRepeatThisRow) {
                     $model->addError('[0]allowRepeatThisRow', '#' . ($n + 1) . '--' . Yii::t('lazy8', 'Balance rows cannot be a repeatable row'));
                     $valid = false;
                 }
             }
             if (strlen($repParam->phpFieldCalc) > 0) {
                 //most others should be zero
                 if (strlen($repParam->defaultValue) > 0) {
                     $model->addError('[0]defaultValue', '#' . ($n + 1) . '--' . Yii::t('lazy8', 'Php calculated rows cannot have a default value.'));
                     $valid = false;
                 }
                 if ($repParam->allowMinus) {
                     $model->addError('[0]allowMinus', '#' . ($n + 1) . '--' . Yii::t('lazy8', 'Php calculated rows cannot have a minus value'));
                     $valid = false;
                 }
                 if ($repParam->allowChangeValue) {
                     $model->addError('[0]allowChangeValue', '#' . ($n + 1) . '--' . Yii::t('lazy8', 'Php calculated rows cannot allow to change value manually'));
                     $valid = false;
                 }
                 if ($repParam->allowRepeatThisRow) {
                     $model->addError('[0]allowRepeatThisRow', '#' . ($n + 1) . '--' . Yii::t('lazy8', 'Php calculated rows cannot be a repeatable row'));
                     $valid = false;
                 }
             }
             if ($repParam->allowMinus && !$repParam->allowChangeValue) {
                 $model->addError('[0]allowChangeValue', '#' . ($n + 1) . '--' . Yii::t('lazy8', 'Allow minus values and not allowing changing values does not make sense.'));
                 $valid = false;
             }
         }
         if ($numBalances > 1) {
             $model->addError('[0]isFinalBalance', Yii::t('lazy8', 'Only one row is allowed to be a balance row.'));
             $valid = false;
         }
         if ($multiLineRows > 1) {
             $model->addError('[0]allowRepeatThisRow', Yii::t('lazy8', 'Only one row is allowed to be a repeatable row.'));
             $valid = false;
         }
         if ($valid) {
             //now save it all
             foreach ($repParams as $n => $repParam) {
                 if (!$repParam->save()) {
                     return false;
                 }
             }
         } else {
             return $valid;
         }
     }
     if (isset($_POST['AddRow'])) {
         $rowparam = new TemplateRow();
         $rowparam->templateId = $model->id;
         $rowparam->allowChangeValue = 1;
         $rowparam->save();
     }
     if (isset($_POST['editrow'])) {
         $edits = $_POST['editrow'];
         //there is only one item in this array, but I don't know
         //any other way to get at it without doing this..
         foreach ($edits as $key => $transrow) {
             $this->redirect(array('editaccounts', 'id' => $model->templateRows[$key]->id, 'companyId' => $model->companyId));
             break;
         }
     }
     if (isset($_POST['deleterow'])) {
         $deletes = $_POST['deleterow'];
         //there is only one item in this array, but I don't know
         //any other way to get at it without doing this..
         foreach ($deletes as $key => $transrow) {
             $model->templateRows[$key]->delete();
             break;
         }
     }
     return $valid;
 }
 /**
  * @return array Errors  Import Template information
  */
 public static function importTemplates($root, $companyId, &$errors)
 {
     $nodeTemplates = $root->getElementsByTagName('template');
     foreach ($nodeTemplates as $nodeTemplate) {
         if ($root->getAttribute('version') > 1.0) {
             $errors[] = '*TemplateVersion*' . 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');
         }
         $modelTemplate = new Template();
         $modelTemplate->companyId = $companyId;
         $modelTemplate->name = $nodeTemplate->getAttribute('name');
         $modelTemplate->desc = $nodeTemplate->getAttribute('desc');
         $modelTemplate->sortOrder = $nodeTemplate->getAttribute('sortorder');
         $modelTemplate->allowAccountingView = $nodeTemplate->getAttribute('allowaccountingview') == '1' ? 1 : 0;
         $modelTemplate->allowFreeTextField = $nodeTemplate->getAttribute('allowfreetextfield') == '1' ? 1 : 0;
         $modelTemplate->freeTextFieldDefault = $nodeTemplate->getAttribute('freetextfielddefault');
         $modelTemplate->allowFilingTextField = $nodeTemplate->getAttribute('allowfilingtextfield') == '1' ? 1 : 0;
         $modelTemplate->filingTextFieldDefault = $nodeTemplate->getAttribute('filingtextfielddefault');
         $modelTemplate->forceDateToday = $nodeTemplate->getAttribute('forcedatetoday');
         if (!$modelTemplate->save()) {
             $errors[] = Yii::t('lazy8', 'Could not create the modelTemplate, bad paramters') . ';name=' . $modelTemplate->name . ';' . serialize($modelTemplate->getErrors());
             return $errors;
         }
         $nodesTemplateRows = $nodeTemplate->getElementsByTagName('templaterow');
         foreach ($nodesTemplateRows as $nodesTemplateRow) {
             $modelTemplateRow = new TemplateRow();
             $modelTemplateRow->templateId = $modelTemplate->id;
             $modelTemplateRow->name = $nodesTemplateRow->getAttribute('name');
             $modelTemplateRow->desc = $nodesTemplateRow->getAttribute('desc');
             $modelTemplateRow->sortOrder = $nodesTemplateRow->getAttribute('sortorder');
             $modelTemplateRow->isDebit = $nodesTemplateRow->getAttribute('isdebit') == '1' ? 1 : 0;
             $modelTemplateRow->defaultAccountId = Template::FindAccountId($nodesTemplateRow->getAttribute('defaultaccount'), $modelTemplate->companyId);
             $modelTemplateRow->defaultValue = $nodesTemplateRow->getAttribute('defaultvalue');
             $modelTemplateRow->allowMinus = $nodesTemplateRow->getAttribute('allowminus') == '1' ? 1 : 0;
             $modelTemplateRow->phpFieldCalc = $nodesTemplateRow->getAttribute('phpfieldcalc');
             $modelTemplateRow->allowChangeValue = $nodesTemplateRow->getAttribute('allowchangevalue') == '1' ? 1 : 0;
             $modelTemplateRow->allowRepeatThisRow = $nodesTemplateRow->getAttribute('allowrepeatthisrow') == '1' ? 1 : 0;
             $modelTemplateRow->allowCustomer = $nodesTemplateRow->getAttribute('allowcustomer') == '1' ? 1 : 0;
             $modelTemplateRow->allowNotes = $nodesTemplateRow->getAttribute('allownotes') == '1' ? 1 : 0;
             $modelTemplateRow->isFinalBalance = $nodesTemplateRow->getAttribute('isfinalbalance') == '1' ? 1 : 0;
             if (!$modelTemplateRow->save()) {
                 $modelTemplate->delete();
                 $errors[] = Yii::t('lazy8', 'Could not create the TemplateRow, bad paramters') . ';' . serialize($modelTemplateRow->getErrors());
                 return;
             }
             $nodesTemplateRowAccounts = $nodesTemplateRow->getElementsByTagName('templaterowaccount');
             foreach ($nodesTemplateRowAccounts as $nodesTemplateRowAccount) {
                 $modelTemplateRowAccount = new TemplateRowAccount();
                 $modelTemplateRowAccount->templateRowId = $modelTemplateRow->id;
                 $modelTemplateRowAccount->accountId = Template::FindAccountId($nodesTemplateRowAccount->getAttribute('code'), $modelTemplate->companyId);
                 if ($modelTemplateRowAccount->accountId != 0) {
                     if (!$modelTemplateRowAccount->save()) {
                         $modelTemplate->delete();
                         $errors[] = Yii::t('lazy8', 'Could not create the TemplateRowAccount, bad paramters') . ';' . serialize($modelTemplateRowAccount->getErrors());
                         return;
                     }
                 } else {
                     $errors[] = Yii::t('lazy8', 'Could not create the Account, bad account number') . ';' . $nodesTemplateRowAccount->getAttribute('code');
                 }
             }
         }
     }
 }