Advertisement
Guest User

Untitled

a guest
Nov 15th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. /**
  2. * @return bool
  3. * @throws \yii\db\Exception
  4. */
  5. public function save()
  6. {
  7. $this->beforeSave();
  8. $transaction = Yii::$app->db->beginTransaction();
  9. try {
  10. $modelRevision = new EstimateRevision();
  11. $modelRevision->discount = empty($this->discount) ? 0 : $this->discount;
  12. $modelRevision->discountInPercent = $this->discountInPercent;
  13. $modelRevision->edited_by = $this->username;
  14.  
  15. $modelRevision->nds = empty($this->nds) ? 0 : $this->nds;
  16. $modelRevision->has_nds = $this->hasNds;
  17.  
  18. if (!$this->estimate->isNewRecord) {
  19. $version = $this->revision->version + 1;
  20. } else {
  21. $version = 1;
  22. }
  23. if (!$this->estimate->save()) {
  24. $transaction->rollBack();
  25. Yii::error('unable to save estimate');
  26. return false;
  27. }
  28.  
  29. $modelRevision->attributes = [
  30. 'estimate_id' => $this->estimate->id,
  31. 'version' => $version,
  32. ];
  33. if (!$modelRevision->save()) {
  34. $transaction->rollBack();
  35. Yii::error('unable to save estimate revision');
  36. return false;
  37. }
  38.  
  39. $this->revision = $modelRevision;
  40. $this->estimate->current_revision_id = $modelRevision->id;
  41. if (!$this->estimate->save()) {
  42. $transaction->rollBack();
  43. Yii::error('unable to save estimate with revision');
  44. return false;
  45. }
  46.  
  47. foreach ($this->groups as $keyGroup => $group) {
  48. $group->estimate_id = $modelRevision->id;
  49. if (!$group->save()) {
  50. $transaction->rollBack();
  51. Yii::error('unable to save estimate group');
  52. return false;
  53. }
  54. foreach ($this->resources[$keyGroup] as $res) {
  55. $res->group_id = $group->id;
  56. if (!$res->save()) {
  57. $transaction->rollBack();
  58. Yii::error('unable to save estimate group resources');
  59. return false;
  60. }
  61. }
  62. }
  63.  
  64. foreach ($this->attrValues as $key => $value) {
  65. $this->oldEAVAttributes[$key]->save();
  66. $value->estimate_id = $this->estimate->id;
  67. $value->attribute_id = $this->oldEAVAttributes[$key]->id;
  68. if (!$value->save()) {
  69. $transaction->rollBack();
  70. Yii::error('unable to save estimate attribute values');
  71. return false;
  72. }
  73. }
  74.  
  75. $transaction->commit();
  76. return true;
  77. } catch (Exception $ex) {
  78. $transaction->rollBack();
  79. Yii::error($ex->getMessage() . "\n" . $ex->getTraceAsString());
  80. return false;
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement