mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-02-12 19:52:22 +00:00
129 - Total Revenue and a Relationship Bug
This commit is contained in:
@@ -48,7 +48,7 @@ class Concert extends Model
|
|||||||
|
|
||||||
public function orders()
|
public function orders()
|
||||||
{
|
{
|
||||||
return $this->belongsToMany(Order::class, 'tickets');
|
return Order::whereIn('id', $this->tickets()->pluck('order_id'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasOrderFor($customerEmail)
|
public function hasOrderFor($customerEmail)
|
||||||
@@ -114,4 +114,9 @@ class Concert extends Model
|
|||||||
{
|
{
|
||||||
return number_format(($this->ticketsSold() / $this->totalTickets()) * 100, 2);
|
return number_format(($this->ticketsSold() / $this->totalTickets()) * 100, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function revenueInDollars()
|
||||||
|
{
|
||||||
|
return $this->orders()->sum('amount') / 100;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@
|
|||||||
<div class="card-section p-md-l-2 text-center text-md-left">
|
<div class="card-section p-md-l-2 text-center text-md-left">
|
||||||
<h3 class="text-base wt-normal m-xs-b-1">Total Revenue</h3>
|
<h3 class="text-base wt-normal m-xs-b-1">Total Revenue</h3>
|
||||||
<div class="text-jumbo wt-bold">
|
<div class="text-jumbo wt-bold">
|
||||||
$10,353
|
${{ $concert->revenueInDollars() }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -111,10 +111,21 @@ class ConcertTest extends TestCase
|
|||||||
$concert->tickets()->saveMany(factory(Ticket::class, 2)->create(['order_id' => 1]));
|
$concert->tickets()->saveMany(factory(Ticket::class, 2)->create(['order_id' => 1]));
|
||||||
$concert->tickets()->saveMany(factory(Ticket::class, 5)->create(['order_id' => null]));
|
$concert->tickets()->saveMany(factory(Ticket::class, 5)->create(['order_id' => null]));
|
||||||
|
|
||||||
// $this->assertEquals(0.285714286, $concert->percentSoldOut(), '', 0.00001);
|
|
||||||
$this->assertEquals(28.57, $concert->percentSoldOut());
|
$this->assertEquals(28.57, $concert->percentSoldOut());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
function calculating_the_revenue_in_dollars()
|
||||||
|
{
|
||||||
|
$concert = factory(Concert::class)->create();
|
||||||
|
$orderA = factory(Order::class)->create(['amount' => 3850]);
|
||||||
|
$orderB = factory(Order::class)->create(['amount' => 9625]);
|
||||||
|
$concert->tickets()->saveMany(factory(Ticket::class, 2)->create(['order_id' => $orderA->id]));
|
||||||
|
$concert->tickets()->saveMany(factory(Ticket::class, 5)->create(['order_id' => $orderB->id]));
|
||||||
|
|
||||||
|
$this->assertEquals(134.75, $concert->revenueInDollars());
|
||||||
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
function trying_to_reserve_more_tickets_than_remain_throws_an_exception()
|
function trying_to_reserve_more_tickets_than_remain_throws_an_exception()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user