PurchaseController.php 5.7KB

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