/**
  * @param Event[] $events
  * @param User $user
  * @param $manual_spent
  * @param $goal
  * @return float
  */
 protected function processDailyBudget($events, $user, $manual_spent, $goal)
 {
     $yesterday = clone $this->goalr->current_date;
     $yesterday->modify('-1 day');
     $tomorrow = clone $this->goalr->current_date;
     $tomorrow->modify('+1 day');
     $forecast_amount_today = $this->purchase_service->getForecastAmountByPeriod($this->goalr->current_date, $this->goalr->current_date, $user);
     $spent = $this->purchase_service->getAmountByPeriod($goal->date_start, $yesterday, $user) + $this->purchase_service->getAmountByPeriod($tomorrow, $goal->date_end, $user) + $forecast_amount_today;
     if ($manual_spent !== null) {
         $spent += $manual_spent;
     }
     $spent_today = $this->purchase_service->getAmountByPeriod($this->goalr->current_date, $this->goalr->current_date, $user) - $forecast_amount_today;
     $daily_budget = $this->goalr->getDailyBudget($goal, $spent, $events);
     return $daily_budget - $spent_today;
 }
 public function testGetById()
 {
     $purchase = new \shina\controlmybudget\Purchase\Purchase();
     $purchase->date = new DateTime('2013-12-25');
     $purchase->place = 'Natalandia';
     $purchase->amount = 300;
     $this->purchase_service->save($purchase, $this->user);
     $purchase = $this->purchase_service->getById($purchase->id);
     $this->assertInstanceOf('\\shina\\controlmybudget\\Purchase', $purchase);
 }
 /**
  * @param int|null $limit
  * @param User $user
  */
 public function import($limit = 3, User $user)
 {
     // making the work of Fetch package
     $messages = imap_sort($this->imap->getImapStream(), SORTARRIVAL, 1, SE_UID, $this->getImapSearch($user));
     if ($limit != null) {
         $messages = array_slice($messages, 0, $limit);
     }
     foreach ($messages as &$message) {
         $message = new Message($message, $this->imap);
     }
     unset($message);
     foreach ($messages as $message) {
         $data = $this->parseData($message);
         foreach ($data as $purchase) {
             try {
                 $this->purchase_service->save($purchase, $user);
             } catch (\Exception $e) {
                 continue;
             }
         }
     }
 }
 public function testGetDailyPeriodBudget()
 {
     $purchase = new \shina\controlmybudget\Purchase\Purchase();
     $purchase->date = new DateTime('2014-01-15');
     $purchase->place = 'Zona Sul';
     $purchase->amount = 2.1;
     $this->purchase_service->save($purchase, $this->user);
     $purchase = new \shina\controlmybudget\Purchase\Purchase();
     $purchase->date = new DateTime('2013-12-25');
     $purchase->place = 'Natalandia';
     $purchase->amount = 300;
     $this->purchase_service->save($purchase, $this->user);
     $purchase = new \shina\controlmybudget\Purchase\Purchase();
     $purchase->date = new DateTime('2014-01-20');
     $purchase->place = 'Casa do carnaval';
     $purchase->amount = 54.7;
     $this->purchase_service->save($purchase, $this->user);
     $purchase = new \shina\controlmybudget\Purchase\Purchase();
     $purchase->date = new DateTime('2014-01-21');
     $purchase->place = 'Bigbi';
     $purchase->amount = 11.0;
     $this->purchase_service->save($purchase, $this->user);
     $events = [];
     $event = new \ebussola\goalr\event\Event();
     $event->name = 'fds';
     $event->date_start = new DateTime('2014-01-01');
     $event->date_end = new DateTime('2014-01-02');
     $event->variation = 50;
     $event->category = 'regular';
     $events[] = $event;
     $event = new \ebussola\goalr\event\Event();
     $event->name = 'fds';
     $event->date_start = new DateTime('2014-01-08');
     $event->date_end = new DateTime('2014-01-09');
     $event->variation = 50;
     $event->category = 'regular';
     $events[] = $event;
     $period_goal = new \shina\controlmybudget\PeriodGoal();
     $period_goal->date_start = new Date('2014-01-01');
     $period_goal->date_end = new Date('2014-01-31');
     $period_goal->amount_goal = 1500;
     $period_goal->events = $events;
     echo $this->budget_control_service->getDailyPeriodBudget($period_goal, $this->user) . PHP_EOL;
 }