-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from phi-rakib/30-implement-crud-operations-fo…
…r-adjustment 30 implement crud operations for adjustment
- Loading branch information
Showing
13 changed files
with
670 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Requests\StoreAdjustmentRequest; | ||
use App\Models\Adjustment; | ||
use App\Models\Product; | ||
use App\Models\Warehouse; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Gate; | ||
|
||
class AdjustmentController extends Controller | ||
{ | ||
public function index() | ||
{ | ||
Gate::authorize('viewAny', Adjustment::class); | ||
|
||
return Adjustment::latest()->with(['warehouse', 'products'])->paginate(20); | ||
} | ||
|
||
public function show(Adjustment $adjustment) | ||
{ | ||
Gate::authorize('view', $adjustment); | ||
|
||
return $adjustment->load(['warehouse', 'products']); | ||
} | ||
|
||
public function store(StoreAdjustmentRequest $request) | ||
{ | ||
Gate::authorize('create', Adjustment::class); | ||
|
||
DB::transaction(function () use ($request) { | ||
|
||
$warehouseId = $request->input('warehouse_id'); | ||
$adjustments = $request->input('adjustment_items'); | ||
|
||
// update product_warehouse | ||
foreach ($adjustments as $adjustment) { | ||
Product::find($adjustment['product_id']) | ||
->warehouses() | ||
->updateExistingPivot($warehouseId, [ | ||
'quantity' => Product::find($adjustment['product_id']) | ||
->warehouses() | ||
->where('warehouse_id', $warehouseId) | ||
->first() | ||
->pivot | ||
->quantity + ($adjustment['type'] == 'addition' ? $adjustment['quantity'] : (-1) * $adjustment['quantity']), | ||
]); | ||
} | ||
|
||
$items = []; | ||
foreach ($adjustments as $adjustment) { | ||
$items[$adjustment['product_id']] = [ | ||
'quantity' => $adjustment['quantity'], | ||
'type' => $adjustment['type'], | ||
]; | ||
} | ||
|
||
// create adjustment | ||
$adjustment = Adjustment::create([ | ||
'warehouse_id' => $warehouseId, | ||
'reason' => $request->input('reason'), | ||
'adjustment_date' => $request->input('adjustment_date'), | ||
]); | ||
|
||
// create adjustment_product | ||
$adjustment->products()->attach($items); | ||
}); | ||
|
||
return response()->json(['message' => 'Adjustment created successfully'], 201); | ||
} | ||
|
||
public function update(StoreAdjustmentRequest $request, Adjustment $adjustment) | ||
{ | ||
Gate::authorize('update', $adjustment); | ||
|
||
DB::transaction(function () use ($adjustment, $request) { | ||
|
||
$warehouseId = $request->input('warehouse_id'); | ||
$adjustments = $request->input('adjustment_items'); | ||
$reason = $request->input('reason'); | ||
$adjustment_date = $request->input('adjustment_date'); | ||
|
||
$adjustmentProductItems = []; | ||
foreach ($adjustments as $item) { | ||
$adjustmentProductItems[$item['product_id']] = [ | ||
'quantity' => $item['quantity'], | ||
'type' => $item['type'], | ||
]; | ||
} | ||
|
||
//update adjustment | ||
$adjustment->update([ | ||
'warehouse_id' => $warehouseId, | ||
'reason' => $reason, | ||
'adjustment_date' => $adjustment_date, | ||
]); | ||
|
||
// update product_warehouse(rollback to previous state) | ||
foreach ($adjustment->products as $item) { | ||
Product::find($item->pivot->product_id) | ||
->warehouses() | ||
->updateExistingPivot($warehouseId, [ | ||
'quantity' => Product::find($item->pivot->product_id) | ||
->warehouses() | ||
->where('warehouse_id', $warehouseId) | ||
->first() | ||
->pivot | ||
->quantity + ($item->pivot->type == 'addition' ? (-1) * $item->pivot->quantity : $item->pivot->quantity), | ||
]); | ||
} | ||
|
||
// update product_warehouse | ||
foreach ($adjustments as $item) { | ||
Product::find($item['product_id']) | ||
->warehouses() | ||
->updateExistingPivot($warehouseId, [ | ||
'quantity' => Product::find($item['product_id']) | ||
->warehouses() | ||
->where('warehouse_id', $warehouseId) | ||
->first() | ||
->pivot | ||
->quantity + ($item['type'] == 'addition' ? $item['quantity'] : (-1) * $item['quantity']), | ||
]); | ||
} | ||
|
||
// update adjustment_product | ||
$adjustment->products()->sync($adjustmentProductItems); | ||
}); | ||
|
||
return response()->json(['message' => 'Adjustment updated successfully'], 200); | ||
} | ||
|
||
public function destroy(Adjustment $adjustment) | ||
{ | ||
Gate::authorize('delete', $adjustment); | ||
|
||
$warehouseId = $adjustment->warehouse_id; | ||
|
||
foreach ($adjustment->products as $item) { | ||
Product::find($item->pivot->product_id) | ||
->warehouses() | ||
->updateExistingPivot($warehouseId, [ | ||
'quantity' => Product::find($item->pivot->product_id) | ||
->warehouses() | ||
->where('warehouse_id', $warehouseId) | ||
->first() | ||
->pivot | ||
->quantity + ($item->pivot->type == 'addition' ? (-1) * $item->pivot->quantity : $item->pivot->quantity), | ||
]); | ||
} | ||
|
||
$adjustment->products()->detach(); | ||
|
||
$adjustment->delete(); | ||
|
||
return response()->json(['message' => 'Adjustment deleted successfully'], 204); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class StoreAdjustmentRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
*/ | ||
public function authorize(): bool | ||
{ | ||
return Auth::check(); | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'warehouse_id' => 'required|integer|exists:warehouses,id', | ||
'adjustment_date' => 'required|date', | ||
'reason' => 'required', | ||
'adjustment_items' => 'required|array', | ||
'adjustment_items.*.product_id' => 'required|integer|exists:products,id', | ||
'adjustment_items.*.quantity' => 'required|integer', | ||
'adjustment_items.*.type' => 'required|string|in:addition,subtraction', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Adjustment extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $fillable = [ | ||
'warehouse_id', | ||
'adjustment_date', | ||
'reason', | ||
]; | ||
|
||
public function warehouse() | ||
{ | ||
return $this->belongsTo(Warehouse::class); | ||
} | ||
|
||
public function products() | ||
{ | ||
return $this->belongsToMany(Product::class, 'adjustment_product', 'adjustment_id', 'product_id')->withPivot(['quantity', 'type']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace App\Observers; | ||
|
||
use App\Models\Adjustment; | ||
|
||
class AdjustmentObserver | ||
{ | ||
public function creating(Adjustment $adjustment) | ||
{ | ||
$adjustment->created_by = auth()->user()->id; | ||
} | ||
|
||
public function updating(Adjustment $adjustment) | ||
{ | ||
$adjustment->updated_by = auth()->user()->id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace App\Policies; | ||
|
||
use App\Models\Adjustment; | ||
use App\Models\User; | ||
|
||
class AdjustmentPolicy | ||
{ | ||
/** | ||
* Determine whether the user can view any models. | ||
*/ | ||
public function viewAny(User $user): bool | ||
{ | ||
return $user->can('adjustment-list'); | ||
} | ||
|
||
/** | ||
* Determine whether the user can view the model. | ||
*/ | ||
public function view(User $user, Adjustment $adjustment): bool | ||
{ | ||
return $user->can('adjustment-list'); | ||
} | ||
|
||
/** | ||
* Determine whether the user can create models. | ||
*/ | ||
public function create(User $user): bool | ||
{ | ||
return $user->can('adjustment-create'); | ||
} | ||
|
||
/** | ||
* Determine whether the user can update the model. | ||
*/ | ||
public function update(User $user, Adjustment $adjustment): bool | ||
{ | ||
return $user->can('adjustment-edit'); | ||
} | ||
|
||
/** | ||
* Determine whether the user can delete the model. | ||
*/ | ||
public function delete(User $user, Adjustment $adjustment): bool | ||
{ | ||
return $user->can('adjustment-delete'); | ||
} | ||
|
||
/** | ||
* Determine whether the user can restore the model. | ||
*/ | ||
public function restore(User $user, Adjustment $adjustment): bool | ||
{ | ||
return $user->can('adjustment-restore'); | ||
} | ||
|
||
/** | ||
* Determine whether the user can permanently delete the model. | ||
*/ | ||
public function forceDelete(User $user, Adjustment $adjustment): bool | ||
{ | ||
return $user->can('adjustment-force-delete'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Database\Factories; | ||
|
||
use App\Models\Adjustment; | ||
use App\Models\Product; | ||
use App\Models\Warehouse; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
/** | ||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Adjustment> | ||
*/ | ||
class AdjustmentFactory extends Factory | ||
{ | ||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function definition(): array | ||
{ | ||
return [ | ||
'warehouse_id' => Warehouse::factory(), | ||
'adjustment_date' => fake()->date(), | ||
'reason' => fake()->sentence(), | ||
]; | ||
} | ||
|
||
public function configure(): static | ||
{ | ||
return $this->afterCreating(function (Adjustment $adjustment) { | ||
$products = Product::factory(10)->create(); | ||
|
||
$products->each(function ($product) use ($adjustment) { | ||
$product->warehouses()->attach($adjustment->warehouse_id, [ | ||
'quantity' => rand(1, 10), | ||
]); | ||
}); | ||
|
||
$warehouseProducts = Warehouse::with(['products'])->where('id', $adjustment->warehouse_id)->first()->products; | ||
|
||
$adjustmentItems = $warehouseProducts->pluck('id')->map(function ($id) { | ||
return [ | ||
'product_id' => $id, | ||
'quantity' => rand(1, 10), | ||
'type' => rand(0, 1) ? 'addition' : 'subtraction', | ||
]; | ||
}); | ||
|
||
foreach ($adjustmentItems as $item) { | ||
$adjustment->products()->attach($item['product_id'], $item); | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.