Esempio n. 1
0
 /**
  * Delete list rows
  */
 public function index_onDelete()
 {
     $successful = true;
     if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) {
         foreach ($checkedIds as $recordId) {
             if (!($record = Discount::find($recordId))) {
                 continue;
             }
             if (!$record->delete()) {
                 $successful = FALSE;
             }
         }
     }
     if ($successful) {
         Flash::success('Successfully deleted discounts.');
     }
     return $this->listRefresh();
 }
 public function test_timeable_scopes()
 {
     $past = Factory::create(new Discount(), ['start_at' => Carbon::now()->subDays(10), 'end_at' => Carbon::now()->subDays(5)]);
     $present = Factory::create(new Discount(), ['start_at' => Carbon::now()->subDays(5), 'end_at' => Carbon::now()->addDays(5)]);
     $future = Factory::create(new Discount(), ['start_at' => Carbon::now()->addDays(5), 'end_at' => Carbon::now()->addDays(10)]);
     $expired = Discount::isExpired()->get();
     $this->assertEquals(1, count($expired));
     $this->assertEquals($past->id, $expired->first()->id);
     $running = Discount::isRunning()->get();
     $this->assertEquals(1, count($running));
     $this->assertEquals($present->id, $running->first()->id);
     $upcoming = Discount::isUpcoming()->get();
     $this->assertEquals(1, count($upcoming));
     $this->assertEquals($future->id, $upcoming->first()->id);
     $runningOrUpcoming = Discount::isRunningOrUpcoming()->get();
     $this->assertEquals(2, count($runningOrUpcoming));
     $this->assertTrue($runningOrUpcoming->contains('id', $present->id));
     $this->assertTrue($runningOrUpcoming->contains('id', $future->id));
 }
 public function test_selecting_discount_statuses()
 {
     $expired1 = Factory::create(new Discount(), ['start_at' => null, 'end_at' => Carbon::yesterday()]);
     $expired2 = Factory::create(new Discount(), ['start_at' => Carbon::now()->subDays(5), 'end_at' => Carbon::yesterday()]);
     $running1 = Factory::create(new Discount(), ['start_at' => null, 'end_at' => null]);
     $running2 = Factory::create(new Discount(), ['start_at' => Carbon::yesterday(), 'end_at' => null]);
     $running3 = Factory::create(new Discount(), ['start_at' => null, 'end_at' => Carbon::tomorrow()]);
     $running4 = Factory::create(new Discount(), ['start_at' => Carbon::yesterday(), 'end_at' => Carbon::tomorrow()]);
     $upcoming1 = Factory::create(new Discount(), ['start_at' => Carbon::tomorrow(), 'end_at' => null]);
     $upcoming2 = Factory::create(new Discount(), ['start_at' => Carbon::tomorrow(), 'end_at' => Carbon::now()->addDays(5)]);
     $discounts = Discount::selectStatus()->get();
     $this->assertEquals(-1, $discounts->where('id', $expired1->id)->first()->status);
     $this->assertEquals(-1, $discounts->where('id', $expired2->id)->first()->status);
     $this->assertEquals(0, $discounts->where('id', $running1->id)->first()->status);
     $this->assertEquals(0, $discounts->where('id', $running2->id)->first()->status);
     $this->assertEquals(0, $discounts->where('id', $running3->id)->first()->status);
     $this->assertEquals(0, $discounts->where('id', $running4->id)->first()->status);
     $this->assertEquals(1, $discounts->where('id', $upcoming1->id)->first()->status);
     $this->assertEquals(1, $discounts->where('id', $upcoming2->id)->first()->status);
 }
Esempio n. 4
0
 /**
  * Syncs all non-expired discounts
  *
  * @return void
  */
 public static function syncAll()
 {
     $discounts = Discount::isRunningOrUpcoming()->get();
     foreach ($discounts as $discount) {
         $discount->syncPrices();
     }
 }
Esempio n. 5
0
 /**
  * Update the entire inheritance tree
  *
  * @param  array    $tree
  * @return void
  */
 public static function updateInheritanceTree($tree)
 {
     $categories = self::all();
     // Update parent_id and sort_order values
     foreach ($tree as $i => $node) {
         $category = $categories->whereLoose('id', $node['id'])->first();
         $category->parent_id = $node['parent_id'];
         $category->sort_order = $i + 1;
         // Save the category, but don't update it's inheritance. Executing
         // an update here would cause a bunch of unecessary queries. We
         // can be more efficient by doing these together at the end.
         if ($category->isDirty()) {
             $category->updateInheritanceAfterSaving = false;
             $category->save();
         }
     }
     // Update inheritance
     foreach ($categories as $category) {
         $category->updateInheritance($categories);
     }
     // Sync all active discounts
     Discount::syncAll();
 }
Esempio n. 6
0
 /**
  * Run a query to search for discount collisions
  * @param   string  $scope ('categories' or 'discounts')
  */
 private function validateDiscountCollisions($scope)
 {
     // Skip the collision check if our discount has already ended
     if ($this->end_date && strtotime($this->end_date) < time()) {
         return;
     }
     // Load our discount_id and discountable_id values
     $discountableIds = post('Discount')[$scope];
     $discountId = $this->id ? $this->id : 0;
     // Query the database and look for collisions
     $collisions = Discount::where('id', '<>', $discountId)->isActiveDuring($this->start_date, $this->end_date)->where(function ($query) {
         $query->whereNull('end_date')->orWhere('end_date', '>=', date('Y-m-d H:i:s'));
     })->whereHas($scope, function ($query) use($discountableIds) {
         $query->whereIn('id', $discountableIds);
     })->lists('name');
     // If collisions were found, throw an exception
     if (count($collisions) > 0) {
         $collisionString = count($collisions == 1) ? "This discount has {$scope} that overlap with the discount {$collisions['0']}." : "This discount has {$scope} that overlap with the following discounts...\n" . implode(', ', $collisions);
         Flash::error($collisionString);
         throw new ValidationException($collisionString);
     }
 }