Attendize/app/Models/EventStats.php

105 lines
2.2 KiB
PHP
Raw Normal View History

2016-03-05 00:18:10 +00:00
<?php
2016-02-29 15:59:36 +00:00
2016-03-05 00:18:10 +00:00
namespace App\Models;
2016-02-29 15:59:36 +00:00
2016-03-05 00:18:10 +00:00
use Cookie;
use DB;
2016-02-29 15:59:36 +00:00
2016-03-05 00:18:10 +00:00
class EventStats extends \Illuminate\Database\Eloquent\Model
{
2016-03-14 16:37:38 +00:00
/**
* Indicates if the model should be timestamped.
*
* @var bool $timestamps
*/
2016-02-29 15:59:36 +00:00
public $timestamps = false;
public static $unguarded = true;
/**
* @todo This shouldn't be in a view.
2016-03-05 00:18:10 +00:00
* Update the amount of revenue a ticket has earned.
*
* @param int $ticket_id
2016-02-29 15:59:36 +00:00
* @param float $amount
2016-03-05 00:18:10 +00:00
* @param bool $deduct
*
2016-02-29 15:59:36 +00:00
* @return bool
*/
2016-03-05 00:18:10 +00:00
public function updateTicketRevenue($ticket_id, $amount, $deduct = false)
{
2016-02-29 15:59:36 +00:00
$ticket = Ticket::find($ticket_id);
2016-03-05 00:18:10 +00:00
if ($deduct) {
2016-02-29 15:59:36 +00:00
$amount = $amount * -1;
}
2016-03-05 00:18:10 +00:00
$ticket->sales_volume = $ticket->sales_volume + $amount;
2016-03-05 00:18:10 +00:00
2016-02-29 15:59:36 +00:00
return $ticket->save();
}
2016-04-22 11:01:48 +00:00
2016-02-29 15:59:36 +00:00
2016-03-14 16:37:38 +00:00
/**
* Update the amount of views a ticket has earned.
*
* @param $event_id
*
* @return bool
*/
2016-03-05 00:18:10 +00:00
public function updateViewCount($event_id)
{
2016-02-29 15:59:36 +00:00
$stats = $this->firstOrNew([
'event_id' => $event_id,
2016-04-20 17:41:21 +00:00
'date' => DB::raw('CURRENT_DATE'),
2016-02-29 15:59:36 +00:00
]);
2016-03-05 00:18:10 +00:00
2016-02-29 15:59:36 +00:00
$cookie_name = 'visitTrack_'.$event_id.'_'.date('dmy');
2016-03-05 00:18:10 +00:00
if (!Cookie::get($cookie_name)) {
2016-02-29 15:59:36 +00:00
Cookie::queue($cookie_name, true, 60 * 24 * 14);
++$stats->unique_views;
}
2016-03-05 00:18:10 +00:00
2016-02-29 15:59:36 +00:00
++$stats->views;
return $stats->save();
}
2016-03-05 00:18:10 +00:00
2016-03-14 16:37:38 +00:00
/**
* @todo: Missing amount?
* Updates the sales volume earned by an event.
*
2016-02-29 15:59:36 +00:00
*/
2016-03-05 00:18:10 +00:00
public function updateSalesVolume($event_id)
{
2016-02-29 15:59:36 +00:00
$stats = $this->firstOrNew([
'event_id' => $event_id,
2016-04-20 17:41:21 +00:00
'date' => DB::raw('CURRENT_DATE'),
2016-02-29 15:59:36 +00:00
]);
2016-03-05 00:18:10 +00:00
2016-02-29 15:59:36 +00:00
$stats->sales_volume = $stats->sales_volume + $amount;
2016-03-05 00:18:10 +00:00
2016-02-29 15:59:36 +00:00
return $stats->save();
}
2016-03-14 16:37:38 +00:00
/**
* Updates the number of tickets sold for the event.
*
* @param $event_id
* @param $count
*
* @return bool
*/
2016-03-05 00:18:10 +00:00
public function updateTicketsSoldCount($event_id, $count)
{
2016-02-29 15:59:36 +00:00
$stats = $this->firstOrNew([
'event_id' => $event_id,
2016-04-20 17:41:21 +00:00
'date' => DB::raw('CURRENT_DATE'),
2016-02-29 15:59:36 +00:00
]);
2016-03-05 00:18:10 +00:00
2016-02-29 15:59:36 +00:00
$stats->increment('tickets_sold', $count);
return $stats->save();
}
}