Created
July 3, 2017 06:58
-
-
Save waqasraza123/5f914ff108ecfd11dc0c89c3e55c5344 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Model; | |
use App\Http\Controllers\EventController; | |
use App\User; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Database\Eloquent\SoftDeletes; | |
use App\Price; | |
/** | |
* App\Model\Event. | |
* | |
* @property int $id | |
* @property string $name | |
* @property string $start_date | |
* @property string $end_date | |
* @property string $start_time | |
* @property string $end_time | |
* @property int $location_id | |
* @property int $interest_id | |
* @property string $description | |
* @property bool $is_sponsored | |
* @property string $sponsor_name | |
* @property string $sponsor_logo | |
* @property bool $is_young_member | |
* @property bool $is_international | |
* @property string $website | |
* @property float $price | |
* @property bool $payment_type_id | |
* @property string $status | |
* @property \Carbon\Carbon $created_at | |
* @property \Carbon\Carbon $updated_at | |
* @property string $deleted_at | |
* | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereCreatedAt($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereDeletedAt($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereDescription($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereEndDate($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereEndTime($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereId($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereInterestId($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereIsInternational($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereIsSponsored($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereIsYoungMember($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereLocationId($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereName($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event wherePaymentTypeId($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event wherePrice($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereSponsorLogo($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereSponsorName($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereStartDate($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereStartTime($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereStatus($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereUpdatedAt($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereWebsite($value) | |
* @mixin \Eloquent | |
* | |
* @property int $user_id | |
* @property \App\User $user | |
* | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereUserId($value) | |
* | |
* @property mixed $price_paypal | |
* @property \Illuminate\Database\Eloquent\Collection|\App\Model\Type[] $types | |
* @property \Illuminate\Database\Eloquent\Collection|\App\Model\Interest[] $interests | |
* @property string $homepage_link | |
* @property string $event_host | |
* @property string $event_host_logo | |
* @property bool $is_online | |
* @property string $address_venue | |
* @property string $address_address | |
* @property string $address_address2 | |
* @property int $address_location_id | |
* @property string $address_postcode | |
* @property string $address_state | |
* @property int $address_country_id | |
* @property bool $address_show_map | |
* | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereAddressAddress($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereAddressAddress2($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereAddressCountryId($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereAddressLocationId($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereAddressPostcode($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereAddressShowMap($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereAddressState($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereAddressVenue($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereEventHost($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereEventHostLogo($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereHomepageLink($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereIsOnline($value) | |
* | |
* @property mixed $full_date | |
* @property \App\Model\Country $country | |
* @property \App\Model\Location $location | |
* @property int $view_count | |
* @property int $website_click_count | |
* | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereViewCount($value) | |
* @method static \Illuminate\Database\Query\Builder|\App\Model\Event whereWebsiteClickCount($value) | |
*/ | |
class Event extends Model | |
{ | |
use SoftDeletes; | |
protected $casts = [ | |
'is_international' => 'boolean', | |
'is_sponsored' => 'boolean', | |
'is_young_member' => 'boolean', | |
'is_online' => 'boolean', | |
'price' => 'float', | |
'user_id' => 'integer', | |
]; | |
protected $appends = [ | |
'full_date', | |
]; | |
const STATUS_ACTIVE = 'Published'; | |
const STATUS_REJECTED = 'Unpublished'; | |
const STATUS_WAITING_PAYMENT = 'Payment'; | |
const STATUS_WAITING_APPROVE = 'Waiting'; | |
const PAYMENT_TYPE_PAYPAL = 1; | |
const PAYMENT_TYPE_BANK = 2; | |
const STATUSES = [ | |
'Published' => 'Published', | |
'Unpublished' => 'Rejected', | |
'Payment Pending' => 'Waiting Payment', | |
'Approval Pending' => 'Waiting Approve', | |
]; | |
public function user() | |
{ | |
return $this->belongsTo(User::class); | |
} | |
public function types() | |
{ | |
return $this->belongsToMany(Type::class); | |
} | |
public function interests() | |
{ | |
return $this->belongsToMany(Interest::class); | |
} | |
public function location() | |
{ | |
return $this->belongsTo(Location::class); | |
} | |
public function country() | |
{ | |
return $this->belongsTo(Country::class, 'address_country_id', 'id'); | |
} | |
public function getEventHostLogoAttribute() | |
{ | |
if ($this->attributes['event_host_logo']) { | |
return $this->attributes['event_host_logo']; | |
} elseif ($this->user->logo) { | |
return $this->user->logo; | |
} | |
} | |
public function getFullDateAttribute() | |
{ | |
$string = ''; | |
$string .= $this->start_date; | |
if ($this->start_time) { | |
$string .= ' '.$this->start_time; | |
} | |
if ($this->end_date) { | |
$string .= ' - '.$this->end_date; | |
if ($this->end_time) { | |
$string .= ' '.$this->end_time; | |
} | |
} | |
return $string; | |
} | |
public function getPricePaypalAttribute() | |
{ | |
$eventController = new EventController(); | |
if (!$this->price) { | |
$this->price = $eventController->calculatePrice($this); | |
} | |
if (!$this->price) { | |
return 0; | |
} | |
return $this->price + ($this->price * config('paypal.commision_rate')) + config('paypal.fee_per_transaction'); | |
} | |
public function setStartTimeAttribute($value) | |
{ | |
if (is_array($value)) { | |
if (!empty($value['hour']) && !empty($value['minute'])) { | |
$value = $value['hour'].':'.$value['minute']; | |
} | |
} | |
$this->attributes['start_time'] = $value; | |
} | |
public function setEndTimeAttribute($value) | |
{ | |
if (is_array($value)) { | |
if (!empty($value['hour']) && !empty($value['minute'])) { | |
$value = $value['hour'].':'.$value['minute']; | |
} | |
} | |
$this->attributes['end_time'] = $value; | |
} | |
public function setStartDateAttribute($value) | |
{ | |
/*if (is_string($value)) { | |
$date = new \DateTime($value); | |
if ($value !== $date->format('Y-m-d')) { | |
$newDateTry = \DateTime::createFromFormat('d-m-Y', $value); | |
if ($value === $newDateTry->format('d-m-Y')) { | |
$value = $newDateTry; | |
} | |
} | |
}*/ | |
$this->attributes['start_date'] = $value; | |
} | |
public function setEndDateAttribute($value) | |
{ | |
/*if (is_string($value)) { | |
$date = new \DateTime($value); | |
if ($value !== $date->format('Y-m-d')) { | |
$newDateTry = \DateTime::createFromFormat('d-m-Y', $value); | |
if ($value === $newDateTry->format('d-m-Y')) { | |
$value = $newDateTry; | |
} | |
} | |
}*/ | |
$this->attributes['end_date'] = $value; | |
} | |
/** | |
* calculates and returns the price for an | |
* event to be added or being updated | |
* | |
* @return int | |
*/ | |
public function calculatePrice() | |
{ | |
if ($this->user->role_id === Role::ROLE_INDUSTRY_ID) { | |
// Price Type Free | |
$price = 0; | |
// Price Type One | |
if ($this->is_sponsored == true and $this->is_international == true) { | |
if (!$this->is_young_member) { | |
$dbPrice = Price::where('id', 1)->first(); | |
$discountAllowed = $dbPrice->discount; | |
if($discountAllowed == 1){ | |
$startDate = (new Carbon($dbPrice->start_date))->toDateString(); | |
$endDate = (new Carbon($dbPrice->end_date))->toDateString(); | |
$currentDate = Carbon::now()->toDateString(); | |
if($currentDate >= $startDate && $currentDate <= $endDate){ | |
$price = $dbPrice->discount_amount; | |
} | |
} | |
else{ | |
$price = $dbPrice->price; | |
} | |
// Price Type Two | |
} else { | |
$dbPrice = Price::where('id', 2)->first(); | |
$discountAllowed = $dbPrice->discount; | |
if($discountAllowed == 1){ | |
$startDate = (new Carbon($dbPrice->start_date))->toDateString(); | |
$endDate = (new Carbon($dbPrice->end_date))->toDateString(); | |
$currentDate = Carbon::now()->toDateString(); | |
if($currentDate >= $startDate && $currentDate <= $endDate){ | |
$price = $dbPrice->discount_amount; | |
} | |
} | |
else{ | |
$price = $dbPrice->price; | |
} | |
} | |
} elseif ($this->is_sponsored == true and $this->is_international == false) { | |
if ($this->is_young_member) { | |
//Price Type Free | |
$price = 0; | |
// Price Type Four | |
} else { | |
$dbPrice = Price::where('id', 4)->first(); | |
$discountAllowed = $dbPrice->discount; | |
if($discountAllowed == 1){ | |
$startDate = (new Carbon($dbPrice->start_date))->toDateString(); | |
$endDate = (new Carbon($dbPrice->end_date))->toDateString(); | |
$currentDate = Carbon::now()->toDateString(); | |
if($currentDate >= $startDate && $currentDate <= $endDate){ | |
$price = $dbPrice->discount_amount; | |
} | |
} | |
else{ | |
$price = $dbPrice->price; | |
} | |
} | |
} elseif ($this->is_sponsored == false and $this->is_international == true) { | |
// Price Type Five | |
$dbPrice = Price::where('id', 5)->first(); | |
$discountAllowed = $dbPrice->discount; | |
if($discountAllowed == 1){ | |
$startDate = (new Carbon($dbPrice->start_date))->toDateString(); | |
$endDate = (new Carbon($dbPrice->end_date))->toDateString(); | |
$currentDate = Carbon::now()->toDateString(); | |
if($currentDate >= $startDate && $currentDate <= $endDate){ | |
$price = $dbPrice->discount_amount; | |
} | |
} | |
else{ | |
$price = $dbPrice->price; | |
} | |
} | |
} elseif ($this->user->role_id === Role::ROLE_COMMERICAL_ID) { | |
// Price Type Six | |
$dbPrice = Price::where('id', 6)->first(); | |
$discountAllowed = $dbPrice->discount; | |
if($discountAllowed == 1){ | |
$startDate = (new Carbon($dbPrice->start_date))->toDateString(); | |
$endDate = (new Carbon($dbPrice->end_date))->toDateString(); | |
$currentDate = Carbon::now()->toDateString(); | |
if($currentDate >= $startDate && $currentDate <= $endDate){ | |
$price = $dbPrice->discount_amount; | |
} | |
} | |
else{ | |
$price = $dbPrice->price; | |
} | |
if ($this->is_international) { | |
// Price Type Seven | |
$dbPrice = Price::where('id', 7)->first(); | |
$discountAllowed = $dbPrice->discount; | |
if($discountAllowed == 1){ | |
$startDate = (new Carbon($dbPrice->start_date))->toDateString(); | |
$endDate = (new Carbon($dbPrice->end_date))->toDateString(); | |
$currentDate = Carbon::now()->toDateString(); | |
if($currentDate >= $startDate && $currentDate <= $endDate){ | |
$price = $dbPrice->discount_amount; | |
} | |
} | |
else{ | |
$price = $dbPrice->price; | |
} | |
} | |
} | |
return $price; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment