Ejemplo n.º 1
0
 public function save($runValidation = true, $attributeNames = null)
 {
     $date = date('Y-m-d', strtotime($this->transaction->date . " +1 day"));
     if ($this->isNewRecord) {
         if ($date >= '2015-11-01') {
             $balance = $this->account->getBalance($date);
             if ($balance->date != $date) {
                 $balance = new Balance();
                 $balance->date = $date;
                 $balance->account_id = $this->account->id;
             }
             $balance->sum = $this->account->getBalance($date)->sum - $this->sum;
             $balance->save();
             $balance->updateFuture(-$this->sum);
         } else {
             // For previous records
             $balance = $this->account->getBalance($date);
             if (!$balance) {
                 $balance = $this->account->getClosestFutureBalance($date);
             }
             if ($balance->date != $date) {
                 $balance = new Balance();
                 $balance->date = $date;
                 $balance->account_id = $this->account->id;
             }
             $balance->sum = $this->account->getBalance($date)->sum;
             $balance->save();
             $balance->updatePast($this->sum);
         }
     } else {
         if ($date >= '2015-11-01') {
             // Substract previos from next balances
             // Add new to next balances
             $balance = $this->account->getBalance($date);
             if ($balance->date != $date) {
                 $balance = new Balance();
                 $balance->date = $date;
                 $balance->account_id = $this->account->id;
             }
             $balance->sum = $this->account->getBalance($date)->sum - $this->sum + $this->oldAttributes['sum'];
             $balance->save();
             $balance->updateFuture(-$this->sum + $this->oldAttributes['sum']);
         } else {
             //
         }
     }
     return parent::save($runValidation, $attributeNames);
 }
Ejemplo n.º 2
0
 /**
  * Creates a new Account model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Account();
     $model->scenario = 'insert';
     $balance = new Balance();
     $balance->scenario = 'insert';
     $loaded = $model->load(Yii::$app->request->post()) && ($model->virtual || $balance->load(Yii::$app->request->post()));
     $valid = false;
     if ($loaded) {
         $valid = $model->validate();
         $valid = $valid && ($model->virtual || $balance->validate(['sum']));
     }
     if ($valid) {
         $model->save();
         if (!$model->virtual) {
             $balance->date = date('Y-m-d', strtotime('first day of this month'));
             $balance->account_id = $model->id;
             $balance->save();
         }
         return $this->redirect(['view', 'id' => $model->id]);
     }
     return $this->render('create', ['model' => $model, 'balance' => $balance]);
 }