/**
  * @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;
 }
Exemplo n.º 2
0
 /**
  * IT'S HARD TO TEST THIS!!
  */
 public function testGetDaily1()
 {
     $goal = new \ebussola\goalr\goal\Goal();
     $goal->id = 1;
     $goal->date_start = new DateTime('2014-07-01');
     $goal->date_end = new DateTime('2014-07-31');
     $goal->total_budget = 1300;
     $events = array();
     $event = new \ebussola\goalr\event\Event();
     $event->id = 1;
     $event->name = 'FDS';
     $event->date_start = new DateTime('2014-07-05');
     $event->date_end = new DateTime('2014-07-10');
     $event->variation = -20;
     $event->category = 'fds';
     $events[] = $event;
     $dateTime = clone $goal->date_end;
     $dateTime->setTime(0, 0, 0)->add(new DateInterval('P1D'));
     $period = new DatePeriod($goal->date_start->setTime(0, 0, 0), new DateInterval('P1D'), $dateTime);
     $spent = 0;
     foreach ($period as $date) {
         $this->goalr->current_date = $date;
         $daily_budget = $this->goalr->getDailyBudget($goal, $spent, $events);
         if ($date < $event->date_start) {
             //                $this->assertLessThan(41, $daily_budget);
         } else {
             if ($date >= $event->date_start && $date <= $event->date_end) {
                 //                $this->assertEquals(82, floor($daily_budget));
             } else {
                 //                $this->assertEquals(41, floor($daily_budget));
             }
         }
         $spent += $daily_budget;
     }
 }