PurchaseController.php 12KB

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