PurchaseController.php 8.0KB

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