Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @return bool
- * @throws \yii\db\Exception
- */
- public function save()
- {
- $this->beforeSave();
- $transaction = Yii::$app->db->beginTransaction();
- try {
- $modelRevision = new EstimateRevision();
- $modelRevision->discount = empty($this->discount) ? 0 : $this->discount;
- $modelRevision->discountInPercent = $this->discountInPercent;
- $modelRevision->edited_by = $this->username;
- $modelRevision->nds = empty($this->nds) ? 0 : $this->nds;
- $modelRevision->has_nds = $this->hasNds;
- if (!$this->estimate->isNewRecord) {
- $version = $this->revision->version + 1;
- } else {
- $version = 1;
- }
- if (!$this->estimate->save()) {
- $transaction->rollBack();
- Yii::error('unable to save estimate');
- return false;
- }
- $modelRevision->attributes = [
- 'estimate_id' => $this->estimate->id,
- 'version' => $version,
- ];
- if (!$modelRevision->save()) {
- $transaction->rollBack();
- Yii::error('unable to save estimate revision');
- return false;
- }
- $this->revision = $modelRevision;
- $this->estimate->current_revision_id = $modelRevision->id;
- if (!$this->estimate->save()) {
- $transaction->rollBack();
- Yii::error('unable to save estimate with revision');
- return false;
- }
- foreach ($this->groups as $keyGroup => $group) {
- $group->estimate_id = $modelRevision->id;
- if (!$group->save()) {
- $transaction->rollBack();
- Yii::error('unable to save estimate group');
- return false;
- }
- foreach ($this->resources[$keyGroup] as $res) {
- $res->group_id = $group->id;
- if (!$res->save()) {
- $transaction->rollBack();
- Yii::error('unable to save estimate group resources');
- return false;
- }
- }
- }
- foreach ($this->attrValues as $key => $value) {
- $this->oldEAVAttributes[$key]->save();
- $value->estimate_id = $this->estimate->id;
- $value->attribute_id = $this->oldEAVAttributes[$key]->id;
- if (!$value->save()) {
- $transaction->rollBack();
- Yii::error('unable to save estimate attribute values');
- return false;
- }
- }
- $transaction->commit();
- return true;
- } catch (Exception $ex) {
- $transaction->rollBack();
- Yii::error($ex->getMessage() . "\n" . $ex->getTraceAsString());
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement