Anons79 Mini Shell

Directory : /home/aplikasiposinfo/public_html/duriancell.fixmate.id/app/Models/
Upload File :
Current File : /home/aplikasiposinfo/public_html/duriancell.fixmate.id/app/Models/Penjualan.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Penjualan extends Model
{
    use HasFactory;
    use SoftDeletes;

    protected $fillable = [
        'pengguna_id',
        'nomor_nota',
        'uang_bayar',
        'metode_bayar',
        'pelanggan_id',
        'tanggal_penjualan',
        'status_pembayaran',
        'uang_masuk',
        'sale_id',
        'status',
        'status_pesan',
        'uang_awal',
        'jam_selesai',
        'bukti_transfer',

    ];
    public function refund()
    {
        return $this->hasMany(RefundPenjualan::class);
    }

    public function pengguna()
    {
        return $this->belongsTo(Pengguna::class);
    }
    public function sales()
    {
        return $this->belongsTo(Pengguna::class, 'sale_id', 'id');
    }


    public function pelanggan()
    {
        return $this->belongsTo(Pelanggan::class);
    }

    public function detail_penjualan()
    {
        return $this->hasMany(DetailPenjualan::class);
    }

    public function pembayaran_piutang()
    {
        return $this->hasMany(PembayaranPiutang::class, 'penjualan_id', 'id');
    }

    public function getTotalAttribute()
    {
        $total = 0;
        foreach ($this->detail_penjualan as $detail) {
            $total += $detail->total;
        }
        return $total;
    }
    public function scopeTanggal($query, $begin, $end)
    {
        return $query->whereBetween("created_at", [$begin->format('Y-m-d'), $end->format('Y-m-d')]);
    }
    public function scopeSelesai($query, $begin, $end)
    {
        return $query->whereBetween("jam_selesai", [$begin->format('Y-m-d'), $end->format('Y-m-d')]);
    }

    public function getTotalPembayaranPiutangAttribute()
    {
        $total = 0;

        $total = $this->uang_bayar;

        return $total;
    }

    public function getJumlahBarangAttribute()
    {
        $total = 0;
        foreach ($this->detail_penjualan as $detail) {
            $total += $detail->qty;
        }
        return $total;
    }

    public function getStatusLunasAttribute()
    {
        if ($this->total_pembayaran_piutang < $this->total) {
            return false;
        } else if ($this->total_pembayaran_piutang >= $this->total) {
            return true;
        }
    }

    public function getTotalModalAttribute()
    {
        $total = 0;
        foreach ($this->detail_penjualan as $d) {
            $total += $d->modal_item;
        }

        return $total;
    }

    // public function scopeLunas($query)
    // {
    //     // return $this->where()
    // }

    public static function boot()
    {
        parent::boot();
        self::deleting(function ($penjualan) {
            $penjualan->detail_penjualan()->each(function ($detail_penjualan) {
                $detail_penjualan->delete();
            });
            $penjualan->pembayaran_piutang()->each(function ($pembayaran_piutang) {
                $pembayaran_piutang->delete();
            });
            $penjualan->refund()->each(function ($refund) {
                $refund->delete();
            });
        });
    }

    public function tokos()
    {
        return $this->morphToMany(Toko::class, 'tokoable', 'tokoables', 'tokoable_id', 'toko_id')
            ->where('tokoable_type', Penjualan::class);
    }

    public function scopeFilterToko($query, $toko_id = null)
    {
        if ($toko_id == null) {
            return $query->whereHas("tokos", function ($q) {
                $q->whereIn('tokos.id',auth()->user()->role->tokos->pluck('id')->toArray());
            });
        } else {
            return $query->whereHas("tokos", function ($q)use ($toko_id) {
                $q->where('tokos.id',$toko_id);
            });
        }
    }
}

Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]