Exemplo n.º 1
0
 public function process(Event $event, RuleConfig $config)
 {
     $entries = new ArrayCollection();
     $configRange = new DateRange($config->start, $config->end);
     $eventRange = new DateRange($event->start, $event->end);
     if ($configRange->isEmpty()) {
         return $entries;
     }
     $amountPerRange = new Money($config->amount, $config->currency);
     $configDays = (int) $configRange->getStart()->diff($configRange->getEnd())->format('%R%a') + 1;
     $totalDays = (int) $eventRange->getStart()->diff($eventRange->getEnd())->format('%R%a') + 1;
     // If we match up nicely, just bill at the fixed value and we're done
     if ($eventRange->equals($configRange)) {
         $rangeTotal = $amountPerRange;
         $entry = new Entry();
         $entry->amount = $rangeTotal->round(2);
         $entry->currency = $rangeTotal->getCurrency();
         $entry->code = $config->code;
         $entry->description = sprintf('%s to %s (%s %s) @ %s for the entire range', $event->start->format('Y-m-d'), $event->end->format('Y-m-d'), $totalDays, $totalDays == 1 ? 'day' : 'days', $amountPerRange->format());
         $entries->add($entry);
     } else {
         // (amount per fixed range / days in fixed range) * days billed
         $proratedTotal = $amountPerRange->div($configDays)->mul($totalDays);
         $entry = new Entry();
         $entry->amount = $proratedTotal->round(2);
         $entry->currency = $proratedTotal->getCurrency();
         $entry->code = $config->code;
         $entry->description = sprintf('%s to %s (%s %s) @ %s for part of %s to %s (%s %s) (prorated daily)', $event->start->format('Y-m-d'), $event->end->format('Y-m-d'), $totalDays, $totalDays == 1 ? 'day' : 'days', $amountPerRange->format(), $configRange->getStart()->format('Y-m-d'), $configRange->getEnd()->format('Y-m-d'), $configDays, $configDays == 1 ? 'day' : 'days');
         $entries->add($entry);
     }
     return $entries;
 }
Exemplo n.º 2
0
 public function testEquals()
 {
     $eq1 = new DateRange(new DateTime('2010-01-02'), new DateTime('2010-01-03'));
     $eq2 = new DateRange(new DateTime('2010-01-02'), new DateTime('2010-01-03'));
     $this->assertTrue($eq1->equals($eq2));
     $this->assertTrue($eq2->equals($eq1));
     $ne1 = new DateRange(new DateTime('2010-02-01'), new DateTime('2010-01-03'));
     $ne2 = new DateRange(new DateTime('2010-01-31'), new DateTime('2010-01-03'));
     $this->assertFalse($ne1->equals($ne2));
     $this->assertFalse($ne2->equals($ne1));
 }