PurchaseController.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. "status" => $purchase->status,
  130. "productNumber" => "PDK" . now()->format("YmdHis"),
  131. "ppnChecked" => $purchase->ppn ? true : false,
  132. "supplier" => $purchase->supplier,
  133. "products" => Inertia::lazy(
  134. fn() => Product::filter(["search" => request("product")])->get()
  135. ),
  136. "purchaseDetail" => $purchase->purchaseDetail->transform(
  137. fn($purchase) => [
  138. "id" => $purchase->id,
  139. "number" => $purchase->product_number,
  140. "name" => $purchase->product->name,
  141. "price" => $purchase->price,
  142. "qty" => $purchase->qty,
  143. "unit" => $purchase->product->unit,
  144. ]
  145. ),
  146. ]);
  147. }
  148. /**
  149. * Update the specified resource in storage.
  150. *
  151. * @param \Illuminate\Http\Request $request
  152. * @param Purchase $purchase
  153. * @return \Illuminate\Http\Response
  154. */
  155. public function update(UpdatePurchaseRequest $request, Purchase $purchase)
  156. {
  157. DB::beginTransaction();
  158. try {
  159. $ppn = Ppn::first()->ppn;
  160. $validated = $request
  161. ->safe()
  162. ->merge([
  163. "ppn" => $request->ppn ? $ppn : 0,
  164. ])
  165. ->all();
  166. $purchase->update($validated);
  167. foreach ($request->products as $product) {
  168. $validated = [
  169. "product_number" => $product["number"],
  170. "price" => $product["price"],
  171. "qty" => $product["qty"],
  172. ];
  173. if (!empty($product["label"])) {
  174. if ($product["label"] == "add") {
  175. $purchase->purchaseDetail()->create($validated);
  176. }
  177. if ($product["label"] == "edit") {
  178. $purchase->purchaseDetail
  179. ->find($product["id"])
  180. ->update($validated);
  181. }
  182. if ($product["label"] == "delete") {
  183. $purchase->purchaseDetail
  184. ->find($product["id"])
  185. ->delete();
  186. }
  187. }
  188. if ($request->status == "success") {
  189. $validated = [
  190. "purchase_number" => $purchase->number,
  191. "price" => $product["price"],
  192. "qty" => $product["qty"],
  193. "product_number" => $product["number"],
  194. ];
  195. StockProduct::create($validated);
  196. }
  197. }
  198. DB::commit();
  199. return back()->with(
  200. "success",
  201. __("messages.success.update.purchase")
  202. );
  203. } catch (QueryException $e) {
  204. DB::rollBack();
  205. return back()->with("error", __("messages.error.update.purchase"));
  206. }
  207. }
  208. /**
  209. * Remove the specified resource from storage.
  210. *
  211. * @param Purchase $purchase
  212. * @return \Illuminate\Http\Response
  213. */
  214. public function destroy(Purchase $purchase)
  215. {
  216. //
  217. }
  218. }