PurchaseController.php 11KB

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