PurchaseController.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exports\PurchaseDetailsExport;
  4. use App\Models\Ppn;
  5. use Inertia\Inertia;
  6. use App\Models\Product;
  7. use App\Models\Purchase;
  8. use App\Models\Supplier;
  9. use App\Models\StockProduct;
  10. use App\Models\PurchaseDetail;
  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. use App\Models\Company;
  17. use App\Models\User;
  18. use App\Services\FunctionService;
  19. use App\Services\PurchaseService;
  20. class PurchaseController extends Controller
  21. {
  22. public function __construct()
  23. {
  24. $this->authorizeResource(Purchase::class);
  25. }
  26. /**
  27. * Display a listing of the resource.
  28. *
  29. * @return \Illuminate\Http\Response
  30. */
  31. public function index()
  32. {
  33. return inertia("Purchases/Index", [
  34. "initialFilters" => request()->only("search"),
  35. "purchases" => Purchase::search(request()->only("search"))
  36. ->latest()
  37. ->paginate(10)
  38. ->withQueryString()
  39. ->through(
  40. fn($purchase) => [
  41. "id" => $purchase->id,
  42. "updatedAt" => $purchase->updated_at,
  43. "name" => $purchase->supplier->name,
  44. "phone" => $purchase->supplier->phone,
  45. "email" => $purchase->supplier->email,
  46. "price" => FunctionService::rupiahFormat(
  47. PurchaseService::totalPrice($purchase)
  48. ),
  49. "status" => $purchase->status,
  50. ]
  51. ),
  52. ]);
  53. }
  54. /**
  55. * Show the form for creating a new resource.
  56. *
  57. * @return \Illuminate\Http\Response
  58. */
  59. public function create()
  60. {
  61. return inertia("Purchases/Create", [
  62. "number" => "PBN" . now()->format("YmdHis"),
  63. "ppn" => Ppn::first()->ppn,
  64. "productNumber" => "PDK" . now()->format("YmdHis"),
  65. "suppliers" => Inertia::lazy(
  66. fn() => Supplier::search([
  67. "search" => request("supplier"),
  68. ])->get()
  69. ),
  70. "products" => Inertia::lazy(
  71. fn() => Product::search(["search" => request("product")])->get()
  72. ),
  73. "productPurchase" => Inertia::lazy(
  74. fn() => PurchaseDetail::historyProductPurchase(
  75. request()->only("product_number", "supplier_id")
  76. )
  77. ->orderBy("price", "desc")
  78. ->get()
  79. ->transform(
  80. fn($purchaseDetail) => [
  81. "number" => $purchaseDetail->product_number,
  82. "price" => $purchaseDetail->getRawOriginal("price"),
  83. "qty" => $purchaseDetail->qty,
  84. "ppn" => $purchaseDetail->purchase->ppn,
  85. ]
  86. )
  87. ->first()
  88. ),
  89. ]);
  90. }
  91. /**
  92. * Store a newly created resource in storage.
  93. *
  94. * @param \Illuminate\Http\Request $request
  95. * @return \Illuminate\Http\Response
  96. */
  97. public function store(StorePurchaseRequest $request)
  98. {
  99. DB::beginTransaction();
  100. try {
  101. $validated = $request
  102. ->safe()
  103. ->merge([
  104. "user_id" => auth()->user()->id,
  105. "ppn" => $request->ppn ? true : false,
  106. ])
  107. ->all();
  108. $purchase = Purchase::create($validated);
  109. foreach ($request->products as $product) {
  110. $validated = [
  111. "product_number" => $product["number"],
  112. "price" => $product["price"],
  113. "qty" => $product["qty"],
  114. ];
  115. $purchase->purchaseDetail()->create($validated);
  116. }
  117. DB::commit();
  118. return back()->with(
  119. "success",
  120. __("messages.success.store.purchase")
  121. );
  122. } catch (QueryException $e) {
  123. DB::rollBack();
  124. return back()->with("error", __("messages.error.store.purchase"));
  125. }
  126. }
  127. /**
  128. * Display the specified resource.
  129. *
  130. * @param Purchase $purchase
  131. * @return \Illuminate\Http\Response
  132. */
  133. public function show(Purchase $purchase)
  134. {
  135. return inertia("Purchases/Show", [
  136. "id" => $purchase->id,
  137. "number" => $purchase->number,
  138. "ppn" => Ppn::first()->ppn,
  139. "status" => $purchase->status,
  140. "ppnChecked" => $purchase->ppn ? true : false,
  141. "supplier" => $purchase->supplier,
  142. "purchaseDetail" => $purchase->purchaseDetail->transform(
  143. fn($purchase) => [
  144. "id" => $purchase->id,
  145. "number" => $purchase->product_number,
  146. "name" => $purchase->product->name,
  147. "price" => $purchase->getRawOriginal("price"),
  148. "qty" => $purchase->qty,
  149. "unit" => $purchase->product->unit,
  150. ]
  151. ),
  152. ]);
  153. }
  154. /**
  155. * Show the form for editing the specified resource.
  156. *
  157. * @param Purchase $purchase
  158. * @return \Illuminate\Http\Response
  159. */
  160. public function edit(Purchase $purchase)
  161. {
  162. return inertia("Purchases/Edit", [
  163. "id" => $purchase->id,
  164. "number" => $purchase->number,
  165. "ppn" => Ppn::first()->ppn,
  166. "status" => $purchase->status,
  167. "productNumber" => "PDK" . now()->format("YmdHis"),
  168. "ppnChecked" => $purchase->ppn ? true : false,
  169. "supplier" => $purchase->supplier,
  170. "products" => Inertia::lazy(
  171. fn() => Product::search(["search" => request("product")])->get()
  172. ),
  173. "purchaseDetail" => $purchase->purchaseDetail->transform(
  174. fn($purchase) => [
  175. "id" => $purchase->id,
  176. "number" => $purchase->product_number,
  177. "name" => $purchase->product->name,
  178. "price" => $purchase->getRawOriginal("price"),
  179. "qty" => $purchase->qty,
  180. "unit" => $purchase->product->unit,
  181. ]
  182. ),
  183. "productPurchase" => Inertia::lazy(
  184. fn() => PurchaseDetail::historyProductPurchase(
  185. request()->only("product_number", "supplier_id")
  186. )
  187. ->orderBy("price", "desc")
  188. ->get()
  189. ->transform(
  190. fn($purchaseDetail) => [
  191. "number" => $purchaseDetail->product_number,
  192. "price" => $purchaseDetail->getRawOriginal("price"),
  193. "qty" => $purchaseDetail->qty,
  194. "ppn" => $purchaseDetail->purchase->ppn,
  195. ]
  196. )
  197. ->first()
  198. ),
  199. ]);
  200. }
  201. /**
  202. * Update the specified resource in storage.
  203. *
  204. * @param \Illuminate\Http\Request $request
  205. * @param Purchase $purchase
  206. * @return \Illuminate\Http\Response
  207. */
  208. public function update(UpdatePurchaseRequest $request, Purchase $purchase)
  209. {
  210. DB::beginTransaction();
  211. try {
  212. $validated = $request
  213. ->safe()
  214. ->merge([
  215. "ppn" => $request->ppn ? true : false,
  216. ])
  217. ->all();
  218. $purchase->update($validated);
  219. foreach ($request->products as $product) {
  220. $validated = [
  221. "product_number" => $product["number"],
  222. "price" => $product["price"],
  223. "qty" => $product["qty"],
  224. ];
  225. if (!empty($product["label"])) {
  226. match ($product["label"]) {
  227. "add" => $purchase
  228. ->purchaseDetail()
  229. ->create($validated),
  230. "edit" => $purchase->purchaseDetail
  231. ->find($product["id"])
  232. ->update($validated),
  233. "delete" => $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. $this->authorize("viewAny", Purchase::class);
  302. $ppn = Ppn::first()->ppn;
  303. $company = Company::first();
  304. $pdf = Pdf::loadView(
  305. "PDF.Purchases.Invoice",
  306. compact("purchase", "company", "ppn")
  307. );
  308. return $pdf->stream();
  309. }
  310. public function deliveryOrder(Purchase $purchase)
  311. {
  312. $this->authorize("viewAny", Purchase::class);
  313. $company = Company::first();
  314. $pdf = Pdf::loadView(
  315. "PDF.Purchases.Do",
  316. compact("purchase", "company")
  317. );
  318. $pdf->setPaper("a3", "landscape");
  319. return $pdf->stream();
  320. }
  321. public function report()
  322. {
  323. $this->authorize("viewAny", User::class);
  324. return inertia("Purchases/Report", [
  325. "initialFilters" => request()->only(
  326. "start_date",
  327. "end_date",
  328. "status"
  329. ),
  330. "purchases" => PurchaseDetail::filter(
  331. request()->only("start_date", "end_date", "status")
  332. )
  333. ->latest()
  334. ->paginate(10)
  335. ->withQueryString()
  336. ->through(
  337. fn($purchaseDetail) => [
  338. "updatedAt" => $purchaseDetail->updated_at,
  339. "totalPrice" => FunctionService::rupiahFormat(
  340. $purchaseDetail->price * $purchaseDetail->qty
  341. ),
  342. "qty" => $purchaseDetail->qty,
  343. "status" => $purchaseDetail->purchase->status,
  344. ]
  345. ),
  346. ]);
  347. }
  348. public function reportExcel()
  349. {
  350. $this->authorize("viewAny", User::class);
  351. return new PurchaseDetailsExport([
  352. "purchases" => PurchaseDetail::filter(
  353. request()->only("start_date", "end_date", "status")
  354. )
  355. ->latest()
  356. ->get()
  357. ->map(
  358. fn($purchaseDetail) => [
  359. "updatedAt" => $purchaseDetail->updated_at,
  360. "qty" => $purchaseDetail->qty,
  361. "status" => $purchaseDetail->purchase->status,
  362. "price" =>
  363. $purchaseDetail->price * $purchaseDetail->qty,
  364. ]
  365. ),
  366. ]);
  367. }
  368. }