PurchaseController.php 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. $validated = $request
  81. ->safe()
  82. ->merge([
  83. "user_id" => auth()->user()->id,
  84. "ppn" => $request->ppn ? true : false,
  85. ])
  86. ->all();
  87. $purchase = Purchase::create($validated);
  88. foreach ($request->products as $product) {
  89. $validated = [
  90. "product_number" => $product["number"],
  91. "price" => $product["price"],
  92. "qty" => $product["qty"],
  93. ];
  94. $purchase->purchaseDetail()->create($validated);
  95. }
  96. DB::commit();
  97. return back()->with(
  98. "success",
  99. __("messages.success.store.purchase")
  100. );
  101. } catch (QueryException $e) {
  102. DB::rollBack();
  103. return back()->with("error", __("messages.error.store.purchase"));
  104. }
  105. }
  106. /**
  107. * Display the specified resource.
  108. *
  109. * @param Purchase $purchase
  110. * @return \Illuminate\Http\Response
  111. */
  112. public function show(Purchase $purchase)
  113. {
  114. return inertia("Purchases/Show", [
  115. "id" => $purchase->id,
  116. "number" => $purchase->number,
  117. "ppn" => Ppn::first()->ppn,
  118. "status" => $purchase->status,
  119. "ppnChecked" => $purchase->ppn ? true : false,
  120. "purchaseDetail" => $purchase->purchaseDetail->transform(
  121. fn($purchase) => [
  122. "id" => $purchase->id,
  123. "number" => $purchase->product_number,
  124. "name" => $purchase->product->name,
  125. "price" => $purchase->price,
  126. "qty" => $purchase->qty,
  127. "unit" => $purchase->product->unit,
  128. ]
  129. ),
  130. ]);
  131. }
  132. /**
  133. * Show the form for editing the specified resource.
  134. *
  135. * @param Purchase $purchase
  136. * @return \Illuminate\Http\Response
  137. */
  138. public function edit(Purchase $purchase)
  139. {
  140. return inertia("Purchases/Edit", [
  141. "id" => $purchase->id,
  142. "number" => $purchase->number,
  143. "ppn" => Ppn::first()->ppn,
  144. "status" => $purchase->status,
  145. "productNumber" => "PDK" . now()->format("YmdHis"),
  146. "ppnChecked" => $purchase->ppn ? true : false,
  147. "supplier" => $purchase->supplier,
  148. "products" => Inertia::lazy(
  149. fn() => Product::filter(["search" => request("product")])->get()
  150. ),
  151. "purchaseDetail" => $purchase->purchaseDetail->transform(
  152. fn($purchase) => [
  153. "id" => $purchase->id,
  154. "number" => $purchase->product_number,
  155. "name" => $purchase->product->name,
  156. "price" => $purchase->price,
  157. "qty" => $purchase->qty,
  158. "unit" => $purchase->product->unit,
  159. ]
  160. ),
  161. ]);
  162. }
  163. /**
  164. * Update the specified resource in storage.
  165. *
  166. * @param \Illuminate\Http\Request $request
  167. * @param Purchase $purchase
  168. * @return \Illuminate\Http\Response
  169. */
  170. public function update(UpdatePurchaseRequest $request, Purchase $purchase)
  171. {
  172. DB::beginTransaction();
  173. try {
  174. $validated = $request
  175. ->safe()
  176. ->merge([
  177. "ppn" => $request->ppn ? true : false,
  178. ])
  179. ->all();
  180. $purchase->update($validated);
  181. foreach ($request->products as $product) {
  182. $validated = [
  183. "product_number" => $product["number"],
  184. "price" => $product["price"],
  185. "qty" => $product["qty"],
  186. ];
  187. if (!empty($product["label"])) {
  188. if ($product["label"] == "add") {
  189. $purchase->purchaseDetail()->create($validated);
  190. }
  191. if ($product["label"] == "edit") {
  192. $purchase->purchaseDetail
  193. ->find($product["id"])
  194. ->update($validated);
  195. }
  196. if ($product["label"] == "delete") {
  197. $purchase->purchaseDetail
  198. ->find($product["id"])
  199. ->delete();
  200. }
  201. }
  202. if ($request->status == "success") {
  203. $stockProduct = StockProduct::where(
  204. "product_number",
  205. $product["number"]
  206. );
  207. if ($stockProduct->exists()) {
  208. $validated = [
  209. "price" =>
  210. $stockProduct->first()->price >
  211. $product["price"]
  212. ? $stockProduct->first()->price
  213. : $product["price"],
  214. "ppn" => $request->ppn ? true : false,
  215. ];
  216. $stockProduct->increment(
  217. "qty",
  218. $product["qty"],
  219. $validated
  220. );
  221. } else {
  222. $validated = [
  223. "price" => $product["price"],
  224. "ppn" => $request->ppn ? true : false,
  225. "qty" => $product["qty"],
  226. "product_number" => $product["number"],
  227. ];
  228. StockProduct::create($validated);
  229. }
  230. }
  231. }
  232. DB::commit();
  233. if ($request->status == "success") {
  234. return to_route("purchases.show", $purchase)->with(
  235. "success",
  236. __("messages.success.update.purchase")
  237. );
  238. } else {
  239. return back()->with(
  240. "success",
  241. __("messages.success.update.purchase")
  242. );
  243. }
  244. } catch (QueryException $e) {
  245. DB::rollBack();
  246. return back()->with("error", __("messages.error.update.purchase"));
  247. }
  248. }
  249. /**
  250. * Remove the specified resource from storage.
  251. *
  252. * @param Purchase $purchase
  253. * @return \Illuminate\Http\Response
  254. */
  255. public function destroy(Purchase $purchase)
  256. {
  257. //
  258. }
  259. }