Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
28.57% |
8 / 28 |
|
62.50% |
5 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Stock | |
28.57% |
8 / 28 |
|
62.50% |
5 / 8 |
46.44 | |
0.00% |
0 / 1 |
| product | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| category | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| color | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| size | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| origin | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| brand | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| boot | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| stockManage | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Models\admin; |
| 4 | |
| 5 | use App\Models\Brand; |
| 6 | use App\Models\Category; |
| 7 | use App\Models\Color; |
| 8 | use App\Models\Origin; |
| 9 | use App\Models\Size; |
| 10 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
| 11 | use Illuminate\Database\Eloquent\Model; |
| 12 | use Illuminate\Database\Eloquent\Relations\BelongsTo; |
| 13 | |
| 14 | class Stock extends Model |
| 15 | { |
| 16 | use HasFactory; |
| 17 | |
| 18 | protected $table = 'stocks'; |
| 19 | |
| 20 | protected $fillable = [ |
| 21 | 'product_id', |
| 22 | 'brand_id', |
| 23 | 'color_id', |
| 24 | 'size_id', |
| 25 | 'origin_id', |
| 26 | 'category_id', |
| 27 | 'stock_in', |
| 28 | 'stock_out', |
| 29 | 'created_by', |
| 30 | 'sku', |
| 31 | 'qr_code', |
| 32 | 'status', |
| 33 | 'product_price', |
| 34 | 'selling_price', |
| 35 | 'discount', |
| 36 | ]; |
| 37 | |
| 38 | public function product(): BelongsTo |
| 39 | { |
| 40 | return $this->belongsTo(Product::class, 'product_id')->with('category'); |
| 41 | } |
| 42 | |
| 43 | public function category(): BelongsTo |
| 44 | { |
| 45 | return $this->belongsTo(Category::class, 'category_id'); |
| 46 | } |
| 47 | |
| 48 | public function color(): BelongsTo |
| 49 | { |
| 50 | return $this->belongsTo(Color::class, 'color_id'); |
| 51 | } |
| 52 | |
| 53 | public function size(): BelongsTo |
| 54 | { |
| 55 | return $this->belongsTo(Size::class, 'size_id'); |
| 56 | } |
| 57 | |
| 58 | public function origin(): BelongsTo |
| 59 | { |
| 60 | return $this->belongsTo(Origin::class, 'origin_id'); |
| 61 | } |
| 62 | |
| 63 | public function brand(): BelongsTo |
| 64 | { |
| 65 | return $this->belongsTo(Brand::class, 'brand_id'); |
| 66 | } |
| 67 | |
| 68 | protected static function boot() |
| 69 | { |
| 70 | parent::boot(); |
| 71 | |
| 72 | static::creating(function ($query) { |
| 73 | $query->created_by = auth()->id(); |
| 74 | }); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * stock manage method start |
| 79 | * param 1: $val contain all need stock table information |
| 80 | * param 2: $stockIn_or_stockOut_column contain stock table column name |
| 81 | * note: if $stockIn_or_stockOut_column value stock_in than update or insert stock_in column, |
| 82 | * if find stock_out update or insert stock out column |
| 83 | */ |
| 84 | public static function stockManage($payloads, $stockIn_or_stockOut_column = 'stock_in') |
| 85 | { |
| 86 | $query = Stock::query() |
| 87 | ->where([ |
| 88 | 'product_id' => $payloads['product_id'], |
| 89 | 'color_id' => $payloads['color_id'], |
| 90 | 'brand_id' => $payloads['brand_id'], |
| 91 | 'size_id' => $payloads['size_id'], |
| 92 | |
| 93 | ]); |
| 94 | |
| 95 | if ($query->exists()) { |
| 96 | $stock_in = $query->first(); |
| 97 | $s = $stock_in->update([$stockIn_or_stockOut_column => $payloads['qty'] + ($stockIn_or_stockOut_column == 'stock_in' ? $stock_in->stock_in : $stock_in->stock_out)]); |
| 98 | } else { |
| 99 | $s = self::query()->create([ |
| 100 | 'product_id' => $payloads['product_id'], |
| 101 | 'color_id' => $payloads['color_id'], |
| 102 | 'brand_id' => $payloads['brand_id'], |
| 103 | 'size_id' => $payloads['size_id'], |
| 104 | $stockIn_or_stockOut_column => $payloads['qty'], |
| 105 | ]); |
| 106 | } |
| 107 | |
| 108 | return true; |
| 109 | } |
| 110 | } |