You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Models/Bought.php

62 lines
1.3 KiB
PHP

<?php
namespace App\Models;
class Bought extends Model
{
private const PURCHASE_META_FORMAT = 'package_v1';
protected $connection = "default";
protected $table = "bought";
public function datetime()
{
return date("Y-m-d H:i:s", $this->attributes['datetime']);
}
public function user()
{
return User::where('id', $this->attributes['userid'])->first();
}
public function shop()
{
return Shop::where("id", $this->attributes['shopid'])->first();
}
public function coupon_code()
{
$metadata = $this->purchase_metadata();
if ($metadata !== null) {
return (string) ($metadata['coupon_code'] ?? '');
}
return (string) $this->attributes['coupon'];
}
public function is_refund()
{
return (float) $this->attributes['price'] < 0;
}
public function settlement_amount()
{
return number_format(abs((float) $this->attributes['price']), 2, '.', '');
}
private function purchase_metadata()
{
$metadata = json_decode((string) $this->attributes['coupon'], true);
if (!is_array($metadata) || ($metadata['_format'] ?? '') !== self::PURCHASE_META_FORMAT) {
return null;
}
return $metadata;
}
}