Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
8.82% |
3 / 34 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| Sale | |
8.82% |
3 / 34 |
|
0.00% |
0 / 10 |
292.62 | |
0.00% |
0 / 1 |
| auditName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| auditLabel | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| customer | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| sale_details | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| sale_payment | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| boot | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
1.02 | |||
| getById | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| store | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| getAll | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| getSearcheData | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
72 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Models\admin; |
| 4 | |
| 5 | use App\Support\Auditable; |
| 6 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
| 7 | use Illuminate\Database\Eloquent\Model; |
| 8 | |
| 9 | class Sale extends Model |
| 10 | { |
| 11 | use Auditable, HasFactory; |
| 12 | |
| 13 | protected function auditName(): string |
| 14 | { |
| 15 | return 'sale'; |
| 16 | } |
| 17 | |
| 18 | protected function auditLabel(): ?string |
| 19 | { |
| 20 | return $this->ref; |
| 21 | } |
| 22 | |
| 23 | protected $table = 'sales'; |
| 24 | |
| 25 | protected $fillable = [ |
| 26 | 'ref', |
| 27 | 'customer_id', |
| 28 | 'note', |
| 29 | 'status', |
| 30 | 'order_date', |
| 31 | 'deliver_date', |
| 32 | 'created_by', |
| 33 | 'status', |
| 34 | ]; |
| 35 | |
| 36 | public function customer() |
| 37 | { |
| 38 | return $this->belongsTo(Customer::class, 'customer_id'); |
| 39 | } |
| 40 | |
| 41 | public function sale_details() |
| 42 | { |
| 43 | return $this->hasMany(SaleDetalis::class); |
| 44 | } |
| 45 | |
| 46 | public function sale_payment() |
| 47 | { |
| 48 | return $this->hasOne(SalePayment::class); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * ########################################### |
| 53 | * # Repository Methods Start # |
| 54 | * ########################################### |
| 55 | * */ |
| 56 | protected static function boot() |
| 57 | { |
| 58 | parent::boot(); |
| 59 | |
| 60 | static::creating(function ($query) { |
| 61 | $query->created_by = auth()->id(); |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | public static function getById($id, $relation = false) |
| 66 | { |
| 67 | $query = self::query(); |
| 68 | |
| 69 | ($relation ? $query->with(['customer', 'sale_details', 'sale_payment']) : $query); |
| 70 | |
| 71 | return $query->findOrFail($id); |
| 72 | } |
| 73 | |
| 74 | public static function store($data) |
| 75 | { |
| 76 | // dd($data); |
| 77 | return self::create([ |
| 78 | 'customer_id' => $data['customer_id'], |
| 79 | 'note' => $data['note'], |
| 80 | 'status' => $data['status'] ?? '', |
| 81 | 'order_date' => today(), |
| 82 | 'deliver_date' => $data['deliver_date'], |
| 83 | 'ref' => $data['ref'], |
| 84 | ]); |
| 85 | } |
| 86 | |
| 87 | public static function getAll($order_by = false) |
| 88 | { |
| 89 | $query = self::query(); |
| 90 | |
| 91 | return ($order_by ? $query->orderBy('id', $order_by) : $query)->with(['customer', 'sale_details', 'sale_payment'])->get(); |
| 92 | |
| 93 | } |
| 94 | |
| 95 | public static function getSearcheData($data) |
| 96 | { |
| 97 | $query = Sale::query()->with(['customer', 'sale_details', 'sale_payment']); |
| 98 | |
| 99 | if (! empty($data['customer_id'])) { |
| 100 | $query->where('customer_id', $data['customer_id']); |
| 101 | } |
| 102 | |
| 103 | // Both dates given -> a range. Only one -> an open-ended bound. Neither |
| 104 | // -> everything. ($from/$to used to be unconditionally built as |
| 105 | // " 00:00:00"/" 23:59:59" strings when the keys were missing, and the |
| 106 | // `isset` check below was always true, so the one-sided branches were |
| 107 | // dead and a no-filter call silently returned an empty set.) |
| 108 | $from = ! empty($data['from']) ? $data['from'] . ' 00:00:00' : null; |
| 109 | $to = ! empty($data['to']) ? $data['to'] . ' 23:59:59' : null; |
| 110 | |
| 111 | if ($from && $to) { |
| 112 | $query->whereBetween('created_at', [$from, $to]); |
| 113 | } elseif ($from) { |
| 114 | $query->where('created_at', '>=', $from); |
| 115 | } elseif ($to) { |
| 116 | $query->where('created_at', '<=', $to); |
| 117 | } |
| 118 | |
| 119 | return $query->latest()->get(); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * ########################################### |
| 124 | * # Repository Methods END # |
| 125 | * ########################################### |
| 126 | * */ |
| 127 | } |