Ejemplo n.º 1
0
 protected static function updateDBByData(&$arrData)
 {
     $arrLog = [];
     Yii::log("updateDBByData start", "info");
     foreach ($arrData as $k => $v) {
         // найти запись в {{company}}
         // блокировка её FOR UPDATE
         $Company = Company::model()->findByAttributes(['INN' => $v['inn']]);
         if (!is_null($Company)) {
             $transaction = Yii::app()->getDb()->beginTransaction();
             try {
                 C2goActiveRecord::logStart();
                 $sqlCompanyBlock = 'SELECT * FROM ' . $Company->tableName() . ' WHERE `id`=:id  FOR UPDATE';
                 $CompanyById = Company::model()->findBySql($sqlCompanyBlock, ['id' => $Company->id]);
                 //$sqlCheckStatementExist = 'SELECT * FROM {{statement}} WHERE `tnd`=:tnd AND `sum`=:sum AND `inn`=:inn AND `date`=:date';
                 $arrParams = ['tnd' => $v['tnd'], 'sum' => $v['sum'], 'date' => static::convertXMLDate($v['date']), 'inn' => $v['inn']];
                 $StatementCheck = Statement::model()->findByAttributes($arrParams);
                 if (is_null($StatementCheck)) {
                     // добавить
                     $Statement = new Statement();
                     $Statement->tnd = $v['tnd'];
                     $Statement->sum = $v['sum'];
                     $Statement->date = static::convertXMLDate($v['date']);
                     $Statement->inn = $v['inn'];
                     $Statement->name = $v['name'];
                     if (!$Statement->save()) {
                         throw new CException(Yii::t('StatementModule.default', 'Не могу сохранить запись в Statement,errors', ['errors' => print_r($Statement->getErrors(), true)]));
                     }
                     Yii::log("updateDBByData p3 ", "info");
                     //@todo: log.processed
                     static::addToStatementLog($Statement, StatementLog::PROCESSED, $CompanyById);
                     Yii::log("updateDBByData p4 ", "info");
                     // verify
                     if (!$CompanyById->verify || !$CompanyById->validate) {
                         $CompanyById->verify = 1;
                         $CompanyById->validate = 1;
                         if ($CompanyById->save()) {
                             //@todo: log.verified
                             static::addToStatementLog($Statement, StatementLog::VERIFIED, $CompanyById);
                         } else {
                             throw new CException(Yii::t('StatementModule.default', 'Не могу в Company сделать verify=1,validate=1 ,errors', ['errors' => print_r($CompanyById->getErrors(), true)]));
                         }
                     }
                     // company.balance
                     $CompanyById->balance += $v['sum'];
                     if ($CompanyById->save()) {
                         $AccountMotion = new AccountMotion();
                         $AccountMotion->company_id = $CompanyById->id;
                         $AccountMotion->price = $v['sum'];
                         $AccountMotion->notice = "import statement Statement.id=[{$Statement->id}]";
                         if ($AccountMotion->save()) {
                             //@todo: log.enrolled
                             static::addToStatementLog($Statement, StatementLog::ENROLLED, $CompanyById);
                             //sleep(5);
                             $transaction->commit();
                             $arrLog[] = ['success'];
                             C2goActiveRecord::logEndAndWrite();
                         } else {
                             throw new CException(Yii::t('StatementModule.default', 'Не могу добавить в AccountMotion ,errors', ['errors' => print_r($AccountMotion->getErrors(), true)]));
                         }
                     } else {
                         throw new CException(Yii::t('StatementModule.default', 'Не могу в Company изменить balance ,errors', ['errors' => print_r($CompanyById->getErrors(), true)]));
                     }
                 } else {
                     throw new CException(Yii::t('StatementModule.default', 'Statement запись уже есть'));
                 }
             } catch (Exception $e) {
                 $transaction->rollback();
                 $strException = $e->__toString();
                 Yii::log("updateDBByData exception [{$strException}]", "error");
                 $arrLog[] = ['error', $e->getMessage()];
                 C2goActiveRecord::logEmptyMessages();
             } finally {
                 C2goActiveRecord::logEnd();
             }
         } else {
             $arrLog[] = ['error', Yii::t('StatementModule.default', 'Не найдена компания по INN, inn_value', ['inn_value' => $v['inn']])];
         }
         //@todo: delete
         //break;
     }
     Yii::log("updateDBByData end", "info");
     return $arrLog;
 }