PurchaseController.php 6.0KB

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