Project.php 2.35 KB
Newer Older
beardedwarrior's avatar
beardedwarrior committed
1 2 3 4 5
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
6
use SimpleSoftwareIO\QrCode\Facades\QrCode;
beardedwarrior's avatar
beardedwarrior committed
7

8
/**
9
 * App\ProjectResource
10 11
 *
 * @property int $id
12
 * @property int $subaddr_index
13 14 15 16 17
 * @property string $target_amount
 * @property string $status
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Deposit[] $deposits
18
 * @property-read mixed $raised_amount
19
 * @property-read string $address_uri
20 21
 * @property-read int $percentage_funded
 * @property-read int $contributions
22
 * @property-read string $qrcode
23 24 25 26 27 28 29
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Project whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Project whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Project wherePaymentId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Project whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Project whereTargetAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Project whereUpdatedAt($value)
 * @mixin \Eloquent
30 31 32 33 34
 * @property string $title
 * @property string|null $commit_sha
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Project whereCommitSha($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Project whereMergeRequestId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Project whereTitle($value)
35
 */
beardedwarrior's avatar
beardedwarrior committed
36 37
class Project extends Model
{
38
    protected $guarded = ['id'];
39

40
    protected $dates = ['created_at', 'updated_at'];
beardedwarrior's avatar
beardedwarrior committed
41 42 43 44 45
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function deposits()
    {
46
        return $this->hasMany(Deposit::class, 'subaddr_index', 'subaddr_index');
beardedwarrior's avatar
beardedwarrior committed
47
    }
48

49
    public function getPercentageFundedAttribute() {
50
        return min(100, round($this->raised_amount / $this->target_amount * 100));
51 52 53 54 55 56
    }

    public function getContributionsAttribute() {
        return $this->deposits->count() ?? 0;
    }

57
    public function generateQrcode() {
58
        return QrCode::format('png')->size(500)->generate($this->address_uri);
59
    }
60 61 62 63 64

    public function getQrCodeSrcAttribute() {
        $encoded = base64_encode($this->generateQrcode());
        return "data:image/png;base64, {$encoded}";
    }
beardedwarrior's avatar
beardedwarrior committed
65
}