| Server IP : 34.87.99.72 / Your IP : 216.73.217.31 Web Server : Apache/2.4.46 (Unix) OpenSSL/1.1.1n System : Linux zlock-bitnami-lamp-prod-v2-vm 4.19.0-16-cloud-amd64 #1 SMP Debian 4.19.181-1 (2021-03-19) x86_64 User : bitnami ( 1000) PHP Version : 7.4.18 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /opt/bitnami/apache2/htdocs/app/app/Models/ |
Upload File : |
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
use Pylon\Traits\Models\HasSearchScope;
use Carbon\Carbon;
use App\Classes\Polarity;
use Illuminate\Support\Facades\Crypt;
use Auth;
use App\Models\UserBoxBooking;
class UserBoxInventory extends Model
{
use HasSearchScope;
protected $fillable = [
"shipment_id",
"collect_num",
"reserved_minute",
"expired_at",
"collected_at",
];
protected $casts = [
"reserved_minute" => 'integer',
"meta" => AsArrayObject::class,
];
const REMINDER_MINUTE = 60;
const COLLECTOR_REMINDER_MINUTE = 2880;
const DROPPER_AND_COLLECTOR = 1;
const DROPPER = 2;
const COLLECTOR = 3;
public const TYPES = [
self::DROPPER_AND_COLLECTOR => "Dropper & Collector",
self::DROPPER => "Dropper",
self::COLLECTOR => "Collector",
];
const END_BY_SYSTEM = 1;
const END_BY_DROPPER = 2;
const END_BY_COLLECTOR = 3;
const END_BY_STAFF = 4;
public const ENDED_TYPES = [
self::END_BY_SYSTEM => "System",
self::END_BY_DROPPER => "Dropper",
self::END_BY_COLLECTOR => "Collector",
self::END_BY_STAFF => "Staff",
0 => null,
];
const DEFAULT_RESERVED_MINUTES = 60;
// append
protected $appends = ['status', 'booking_type', 'unique_id'];
// accessor
public function getInventoryTypeAttribute()
{
return self::TYPES[$this->attributes['inventory_type_id']] ?? 'N/A';
}
public function getStatusAttribute()
{
return self::computeStatus();
}
public function getUniqueIdAttribute()
{
return $this->userBoxBooking ? $this->userBoxBooking->unique_id : null;
}
public function getBookingTypeAttribute()
{
return 'inventory';
}
public function getTotalBookingFeeAttribute()
{
return self::computeTotalBookingFee();
}
public function getTotalBookingFeePartnerAttribute()
{
return self::computeTotalBookingFeePartner();
}
public function getTotalUsageTimeAttribute()
{
return self::computeTotalUsageTime();
}
// relationship
public function user()
{
return $this->belongsTo('App\Models\User', 'user_id');
}
public function collector()
{
return $this->belongsTo('App\Models\User', 'collector_num','mobile_num');
}
public function box()
{
return $this->belongsTo('App\Models\LockerBox', 'box_id');
}
public function locker()
{
return $this->belongsToMany('App\Models\Locker', 'locker_boxes', 'id', 'locker_id', 'box_id');
}
public function recordedLocker()
{
return $this->belongsTo('App\Models\Locker', 'recorded_locker_id');
}
public function userBoxBooking()
{
return $this->morphOne('App\Models\UserBoxBooking', 'orderable');
}
// functions
public function getInventoryType($user){
$item = $this;
$item->is_collector = ($item->collector_num == $user->mobile_num);
$is_owner = ($item->user_id == $user->id);
if($item->is_collector && $is_owner){
$item->inventory_type_id = UserBoxInventory::DROPPER_AND_COLLECTOR;
}else{
$item->inventory_type_id = $item->is_collector ? UserBoxInventory::COLLECTOR : UserBoxInventory::DROPPER ;
}
}
public function getQrCode()
{
$item = $this;
$type = null;
$user = Auth::user();
$item->qr_code = null;
$item->cancel_qr_code = null;
//Get collect/drop off qr code
if($item->isCollect($user->mobile_num))
$type = UserBoxBooking::COLLECT_ACTION;
else if($item->isDropOff($user->id))
$type = UserBoxBooking::DROP_OFF_ACTION;
if($type != null)
{
$json_string = json_encode([
'id' => $item->id,
'booking_type' => $item->booking_type,
'type' => $type,
'user_id' => $user->id,
]);
$item->qr_code = base64_encode($json_string);
}
//Get cancel order qr code
if($item->isCancelQr($user->id))
{
$json_string = json_encode([
'id' => $item->id,
'booking_type' => $item->booking_type,
'type' => UserBoxBooking::CANCEL_ORDER_ACTION,
'user_id' => $user->id,
]);
$item->cancel_qr_code = base64_encode($json_string);
}
}
public function dropper()
{
return $this->user();
}
public function isExpired($now = null ){
if($now == null){
$now = now();
}
$expired_at = new Carbon($this->attributes['expired_at']);
return $now->greaterThan($expired_at);
}
public function computeStatus()
{
$item = $this->attributes;
if($item['collected_at'] != null){
$polar_id = Polarity::NEGATIVE;
$message = "N/A";
switch ($item['ended_type']) {
case self::END_BY_SYSTEM:
$message = "Cancelled by system";
break;
case self::END_BY_DROPPER:
$message = "Cancelled by dropper";
break;
case self::END_BY_COLLECTOR:
$polar_id = Polarity::POSITIVE;
$message = "Completed";
break;
case self::END_BY_STAFF:
$message = "Cancelled by staff";
break;
}
return [ "polar_id" => $polar_id, "message" => $message ];
}
$polar_id = Polarity::NEUTRAL;
if($item['dirty_at'] == null){
$pending_msg = "Pending to drop off";
}else{
$pending_msg = "Pending to collect";
}
if($this->isExpired()){
$polar_id = Polarity::NEGATIVE;
$pending_msg = $pending_msg."(Expired)";
}
return [
"polar_id" => $polar_id,
"message" => $pending_msg
];
}
public function computeTotalUsageTime()
{
$item = $this->attributes;
if($item['collected_at'] == null){
return null;
}
$startTime = Carbon::parse($item['created_at']);
$finishTime = Carbon::parse($item['collected_at']);
return $finishTime->diffInMinutes($startTime);
}
public function computeTotalBookingFee()
{
$item = json_decode($this->attributes['meta']);
$total_price = (float)((!$this->user_id && (isset($item->overtime_charge) ? ((float)$item->overtime_charge > 0 ? true : false) : false)) ? 0 : $item->final_price) + (isset($item->overtime_charge) ? $item->overtime_charge : 0);
return number_format(($total_price), 2, '.', '');
}
public function computeTotalBookingFeePartner()
{
$item = json_decode($this->attributes['meta']);
$has_penalty = false;
$total_booking_fee = self::computeTotalBookingFee();
if(isset($item->has_penalty))
{
$has_penalty = $item->has_penalty;
}
return number_format($has_penalty ? $total_booking_fee - 1 : $total_booking_fee, 2, '.', '');
}
public function isCollect($mobile_num)
{
$this->is_collector = ($this->collector_num == $mobile_num);
return $this->is_collector && $this->dirty_at != null;
}
public function isDropOff($user_id)
{
return $this->dirty_at == null && $this->user_id == $user_id;
}
public function isCancelQr($user_id)
{
return $this->dirty_at != null && $this->user_id == $user_id;
}
}