PurchaseController.php 9.3KB

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