PurchaseController.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\Purchase\StorePurchaseRequest;
  4. use App\Http\Requests\Purchase\UpdatePurchaseRequest;
  5. use App\Models\Ppn;
  6. use App\Models\Product;
  7. use App\Models\Purchase;
  8. use App\Models\StockProduct;
  9. use App\Models\Supplier;
  10. use Illuminate\Database\QueryException;
  11. use Illuminate\Support\Facades\DB;
  12. use Inertia\Inertia;
  13. class PurchaseController extends Controller
  14. {
  15. public function __construct()
  16. {
  17. $this->authorizeResource(Purchase::class);
  18. }
  19. /**
  20. * Display a listing of the resource.
  21. *
  22. * @return \Illuminate\Http\Response
  23. */
  24. public function index()
  25. {
  26. return inertia('Purchases/Index', [
  27. 'initialSearch' => request('search'),
  28. 'purchases' => Purchase::filter(request()->only('search'))
  29. ->latest()
  30. ->paginate(10)
  31. ->withQueryString()
  32. ->through(fn($purchase) => [
  33. 'id' => $purchase->id,
  34. 'updatedAt' => $purchase->updated_at,
  35. 'number' => $purchase->number,
  36. 'status' => $purchase->status,
  37. 'price' => $purchase->purchaseDetail->price,
  38. 'ppn' => $purchase->purchaseDetail->ppn,
  39. 'qty' => $purchase->purchaseDetail->qty,
  40. 'productName' => $purchase->product->name,
  41. 'productNumber' => $purchase->product->number
  42. ])
  43. ]);
  44. }
  45. /**
  46. * Show the form for creating a new resource.
  47. *
  48. * @return \Illuminate\Http\Response
  49. */
  50. public function create()
  51. {
  52. return inertia('Purchases/Create', [
  53. 'number' => 'PBN' . now()->format('YmdHis'),
  54. 'ppn' => Ppn::first()->ppn,
  55. 'productNumber' => Inertia::lazy(
  56. fn() => 'PDK' . now()->format('YmdHis')
  57. ),
  58. 'suppliers' => Inertia::lazy(
  59. fn() => Supplier::filter(['search' => request('supplier')])
  60. ->get()
  61. ),
  62. 'products' => Inertia::lazy(
  63. fn() => Product::filter(['search' => request('product')])
  64. ->get()
  65. )
  66. ]);
  67. }
  68. /**
  69. * Store a newly created resource in storage.
  70. *
  71. * @param \Illuminate\Http\Request $request
  72. * @return \Illuminate\Http\Response
  73. */
  74. public function store(StorePurchaseRequest $request)
  75. {
  76. DB::beginTransaction();
  77. try {
  78. $validated = $request->safe()->merge([
  79. 'user_id' => auth()->user()->id,
  80. 'ppn' => Ppn::first()->getRawOriginal('ppn')
  81. ])->all();
  82. $purchase = Purchase::create($validated);
  83. $purchase->purchaseDetail()->create($validated);
  84. DB::commit();
  85. return back()->with('success', __('messages.success.store.purchase'));
  86. } catch (QueryException $e) {
  87. DB::rollBack();
  88. return back()->with('error', __('messages.error.store.purchase'));
  89. }
  90. }
  91. /**
  92. * Display the specified resource.
  93. *
  94. * @param Purchase $purchase
  95. * @return \Illuminate\Http\Response
  96. */
  97. public function show(Purchase $purchase)
  98. {
  99. //
  100. }
  101. /**
  102. * Show the form for editing the specified resource.
  103. *
  104. * @param Purchase $purchase
  105. * @return \Illuminate\Http\Response
  106. */
  107. public function edit(Purchase $purchase)
  108. {
  109. return inertia('Purchases/Edit', [
  110. 'purchase' => [
  111. 'id' => $purchase->id,
  112. 'number' => $purchase->number,
  113. 'status' => $purchase->status,
  114. 'price' => $purchase->purchaseDetail->getRawOriginal('price'),
  115. 'qty' => $purchase->purchaseDetail->qty,
  116. 'ppn' => $purchase->purchaseDetail->ppn,
  117. 'supplier' => $purchase->supplier,
  118. 'product' => $purchase->product
  119. ]
  120. ]);
  121. }
  122. /**
  123. * Update the specified resource in storage.
  124. *
  125. * @param \Illuminate\Http\Request $request
  126. * @param Purchase $purchase
  127. * @return \Illuminate\Http\Response
  128. */
  129. public function update(UpdatePurchaseRequest $request, Purchase $purchase)
  130. {
  131. DB::beginTransaction();
  132. try {
  133. $purchase->update($request->validated());
  134. $purchase->purchaseDetail()->update($request->safe()->except('status'));
  135. if ($request->status === 'success') {
  136. StockProduct::create([
  137. 'purchase_number' => $purchase->number,
  138. 'qty' => $request->qty,
  139. 'product_number' => $purchase->purchaseDetail->product_number
  140. ]);
  141. }
  142. DB::commit();
  143. return back()->with('success', __('messages.success.update.purchase'));
  144. } catch (QueryException $e) {
  145. DB::rollBack();
  146. return back()->with('error', __('messages.error.update.purchase'));
  147. }
  148. }
  149. /**
  150. * Remove the specified resource from storage.
  151. *
  152. * @param Purchase $purchase
  153. * @return \Illuminate\Http\Response
  154. */
  155. public function destroy(Purchase $purchase)
  156. {
  157. //
  158. }
  159. }