PurchaseController.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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\PurchaseDetail;
  9. use App\Models\StockProduct;
  10. use App\Models\Supplier;
  11. use App\Services\HelperService;
  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. "name" => $purchase->supplier->name,
  39. "phone" => $purchase->supplier->phone,
  40. "email" => $purchase->supplier->email,
  41. "price" => HelperService::setRupiahFormat(
  42. $purchase->purchaseDetail->sum("price")
  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. "historyProductPurchase" => Inertia::lazy(
  69. fn() => PurchaseDetail::historyProductPurchase(
  70. request()->only("productNumber", "supplierId")
  71. )
  72. ->orderBy("price", "desc")
  73. ->get()
  74. ->transform(
  75. fn($purchaseDetail) => [
  76. "id" => $purchaseDetail->id,
  77. "price" => $purchaseDetail->price,
  78. "qty" => $purchaseDetail->qty,
  79. "ppn" => $purchaseDetail->purchase->ppn,
  80. ]
  81. )
  82. ->first()
  83. ),
  84. ]);
  85. }
  86. /**
  87. * Store a newly created resource in storage.
  88. *
  89. * @param \Illuminate\Http\Request $request
  90. * @return \Illuminate\Http\Response
  91. */
  92. public function store(StorePurchaseRequest $request)
  93. {
  94. DB::beginTransaction();
  95. try {
  96. $validated = $request
  97. ->safe()
  98. ->merge([
  99. "user_id" => auth()->user()->id,
  100. "ppn" => $request->ppn ? true : false,
  101. ])
  102. ->all();
  103. $purchase = Purchase::create($validated);
  104. foreach ($request->products as $product) {
  105. $validated = [
  106. "product_number" => $product["number"],
  107. "price" => $product["price"],
  108. "qty" => $product["qty"],
  109. ];
  110. $purchase->purchaseDetail()->create($validated);
  111. }
  112. DB::commit();
  113. return back()->with(
  114. "success",
  115. __("messages.success.store.purchase")
  116. );
  117. } catch (QueryException $e) {
  118. DB::rollBack();
  119. return back()->with("error", __("messages.error.store.purchase"));
  120. }
  121. }
  122. /**
  123. * Display the specified resource.
  124. *
  125. * @param Purchase $purchase
  126. * @return \Illuminate\Http\Response
  127. */
  128. public function show(Purchase $purchase)
  129. {
  130. return inertia("Purchases/Show", [
  131. "id" => $purchase->id,
  132. "number" => $purchase->number,
  133. "ppn" => Ppn::first()->ppn,
  134. "status" => $purchase->status,
  135. "ppnChecked" => $purchase->ppn ? true : false,
  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->getRawOriginal("price"),
  142. "qty" => $purchase->qty,
  143. "unit" => $purchase->product->unit,
  144. ]
  145. ),
  146. ]);
  147. }
  148. /**
  149. * Show the form for editing the specified resource.
  150. *
  151. * @param Purchase $purchase
  152. * @return \Illuminate\Http\Response
  153. */
  154. public function edit(Purchase $purchase)
  155. {
  156. return inertia("Purchases/Edit", [
  157. "id" => $purchase->id,
  158. "number" => $purchase->number,
  159. "ppn" => Ppn::first()->ppn,
  160. "status" => $purchase->status,
  161. "productNumber" => "PDK" . now()->format("YmdHis"),
  162. "ppnChecked" => $purchase->ppn ? true : false,
  163. "supplier" => $purchase->supplier,
  164. "products" => Inertia::lazy(
  165. fn() => Product::filter(["search" => request("product")])->get()
  166. ),
  167. "purchaseDetail" => $purchase->purchaseDetail->transform(
  168. fn($purchase) => [
  169. "id" => $purchase->id,
  170. "number" => $purchase->product_number,
  171. "name" => $purchase->product->name,
  172. "price" => $purchase->getRawOriginal("price"),
  173. "qty" => $purchase->qty,
  174. "unit" => $purchase->product->unit,
  175. ]
  176. ),
  177. "historyProductPurchase" => Inertia::lazy(
  178. fn() => PurchaseDetail::historyProductPurchase(
  179. request()->only("productNumber", "supplierId")
  180. )
  181. ->orderBy("price", "desc")
  182. ->get()
  183. ->transform(
  184. fn($purchaseDetail) => [
  185. "id" => $purchaseDetail->id,
  186. "price" => $purchaseDetail->price,
  187. "qty" => $purchaseDetail->qty,
  188. "ppn" => $purchaseDetail->purchase->ppn,
  189. ]
  190. )
  191. ->first()
  192. ),
  193. ]);
  194. }
  195. /**
  196. * Update the specified resource in storage.
  197. *
  198. * @param \Illuminate\Http\Request $request
  199. * @param Purchase $purchase
  200. * @return \Illuminate\Http\Response
  201. */
  202. public function update(UpdatePurchaseRequest $request, Purchase $purchase)
  203. {
  204. DB::beginTransaction();
  205. try {
  206. $validated = $request
  207. ->safe()
  208. ->merge([
  209. "ppn" => $request->ppn ? true : false,
  210. ])
  211. ->all();
  212. $purchase->update($validated);
  213. foreach ($request->products as $product) {
  214. $validated = [
  215. "product_number" => $product["number"],
  216. "price" => $product["price"],
  217. "qty" => $product["qty"],
  218. ];
  219. if (!empty($product["label"])) {
  220. if ($product["label"] == "add") {
  221. $purchase->purchaseDetail()->create($validated);
  222. }
  223. if ($product["label"] == "edit") {
  224. $purchase->purchaseDetail
  225. ->find($product["id"])
  226. ->update($validated);
  227. }
  228. if ($product["label"] == "delete") {
  229. $purchase->purchaseDetail
  230. ->find($product["id"])
  231. ->delete();
  232. }
  233. }
  234. if ($request->status == "success") {
  235. $stockProduct = StockProduct::where(
  236. "product_number",
  237. $product["number"]
  238. );
  239. if ($stockProduct->exists()) {
  240. $validated = [
  241. "price" =>
  242. $stockProduct->first()->price >
  243. $product["price"]
  244. ? $stockProduct->first()->price
  245. : $product["price"],
  246. "ppn" => $request->ppn ? true : false,
  247. ];
  248. $stockProduct->increment(
  249. "qty",
  250. $product["qty"],
  251. $validated
  252. );
  253. } else {
  254. $validated = [
  255. "price" => $product["price"],
  256. "ppn" => $request->ppn ? true : false,
  257. "qty" => $product["qty"],
  258. "product_number" => $product["number"],
  259. ];
  260. StockProduct::create($validated);
  261. }
  262. }
  263. }
  264. DB::commit();
  265. if ($request->status == "success") {
  266. return to_route("purchases.show", $purchase)->with(
  267. "success",
  268. __("messages.success.update.purchase")
  269. );
  270. } else {
  271. return back()->with(
  272. "success",
  273. __("messages.success.update.purchase")
  274. );
  275. }
  276. } catch (QueryException $e) {
  277. DB::rollBack();
  278. return back()->with("error", __("messages.error.update.purchase"));
  279. }
  280. }
  281. /**
  282. * Remove the specified resource from storage.
  283. *
  284. * @param Purchase $purchase
  285. * @return \Illuminate\Http\Response
  286. */
  287. public function destroy(Purchase $purchase)
  288. {
  289. //
  290. }
  291. }