PurchaseController.php 7.2KB

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