Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
50.00% |
9 / 18 |
|
54.55% |
6 / 11 |
CRAP | |
0.00% |
0 / 1 |
| Purchase | |
50.00% |
9 / 18 |
|
54.55% |
6 / 11 |
26.12 | |
0.00% |
0 / 1 |
| auditName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| auditLabel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| supplier | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| user | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| branch | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| warehouse | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| purchase_details | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| purchase_payment | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| boot | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| storePurchase | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| getAll | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Models\admin; |
| 4 | |
| 5 | use App\Models\Branch; |
| 6 | use App\Models\User; |
| 7 | use App\Models\Warehouse; |
| 8 | use App\Support\Auditable; |
| 9 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
| 10 | use Illuminate\Database\Eloquent\Model; |
| 11 | |
| 12 | class Purchase extends Model |
| 13 | { |
| 14 | use Auditable, HasFactory; |
| 15 | |
| 16 | protected function auditName(): string |
| 17 | { |
| 18 | return 'purchase'; |
| 19 | } |
| 20 | |
| 21 | protected function auditLabel(): ?string |
| 22 | { |
| 23 | return $this->ref; |
| 24 | } |
| 25 | |
| 26 | protected $table = 'purchases'; |
| 27 | |
| 28 | protected $fillable = [ |
| 29 | 'warehouse_id', |
| 30 | 'branch_id', |
| 31 | 'supplier_id', |
| 32 | 'ref', |
| 33 | 'status', |
| 34 | 'date', |
| 35 | 'created_by', |
| 36 | 'subtotal', |
| 37 | 'discount_amount', |
| 38 | 'tax_amount', |
| 39 | 'shipping_charge', |
| 40 | 'grand_total', |
| 41 | 'note', |
| 42 | ]; |
| 43 | |
| 44 | public function supplier() |
| 45 | { |
| 46 | return $this->belongsTo(Supplier::class); |
| 47 | } |
| 48 | |
| 49 | public function user() |
| 50 | { |
| 51 | return $this->belongsTo(User::class, 'created_by'); |
| 52 | } |
| 53 | |
| 54 | public function branch() |
| 55 | { |
| 56 | return $this->belongsTo(Branch::class); |
| 57 | } |
| 58 | |
| 59 | public function warehouse() |
| 60 | { |
| 61 | return $this->belongsTo(Warehouse::class); |
| 62 | } |
| 63 | |
| 64 | public function purchase_details() |
| 65 | { |
| 66 | // category + origin are read per-row in details/invoice views — eager |
| 67 | // load them here too, otherwise it's one extra query per line item. |
| 68 | return $this->hasMany(PurchaseDetails::class)->with(['color', 'size', 'product', 'brand', 'category', 'origin']); |
| 69 | } |
| 70 | |
| 71 | public function purchase_payment() |
| 72 | { |
| 73 | return $this->hasOne(PurchasePayment::class); |
| 74 | } |
| 75 | |
| 76 | protected static function boot() |
| 77 | { |
| 78 | parent::boot(); |
| 79 | |
| 80 | static::creating(function ($query) { |
| 81 | $query->created_by = auth()->id(); |
| 82 | }); |
| 83 | } |
| 84 | |
| 85 | public static function storePurchase($purchase) |
| 86 | { |
| 87 | return self::query()->create([ |
| 88 | 'supplier_id' => $purchase['supplier_id'], |
| 89 | 'status' => $purchase['status'], |
| 90 | 'date' => $purchase['date'], |
| 91 | ]); |
| 92 | } |
| 93 | |
| 94 | public static function getAll() |
| 95 | { |
| 96 | return 'hello'; |
| 97 | } |
| 98 | } |