StockProductController.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Ppn;
  4. use App\Models\Product;
  5. use App\Models\StockProduct;
  6. use Illuminate\Http\Request;
  7. use App\Services\FunctionService;
  8. use App\Models\HistoryStockProduct;
  9. use App\Exports\HistoryStockProductExport;
  10. class StockProductController extends Controller
  11. {
  12. public function __construct()
  13. {
  14. $this->authorizeResource(StockProduct::class);
  15. }
  16. /**
  17. * Display a listing of the resource.
  18. *
  19. * @return \Illuminate\Http\Response
  20. */
  21. public function index()
  22. {
  23. return inertia("StockProducts/Index", [
  24. "initialFilters" => request()->only("search", "category"),
  25. "stockProducts" => StockProduct::filter(
  26. request()->only("search", "category")
  27. )
  28. ->latest()
  29. ->paginate(10)
  30. ->withQueryString()
  31. ->through(
  32. fn($stockProduct) => [
  33. "id" => $stockProduct->id,
  34. "productId" => $stockProduct->product->id,
  35. "productNumber" => $stockProduct->product->number,
  36. "updatedAt" => $stockProduct->updated_at,
  37. "name" => $stockProduct->product->name,
  38. "price" => FunctionService::rupiahFormat(
  39. $stockProduct->price
  40. ),
  41. "qty" => $stockProduct->qty,
  42. "unit" => $stockProduct->product->unit,
  43. ]
  44. ),
  45. ]);
  46. }
  47. /**
  48. * Show the form for creating a new resource.
  49. *
  50. * @return \Illuminate\Http\Response
  51. */
  52. public function create()
  53. {
  54. //
  55. }
  56. /**
  57. * Store a newly created resource in storage.
  58. *
  59. * @param \Illuminate\Http\Request $request
  60. * @return \Illuminate\Http\Response
  61. */
  62. public function store(Request $request)
  63. {
  64. //
  65. }
  66. /**
  67. * Display the specified resource.
  68. *
  69. * @param int $id
  70. * @return \Illuminate\Http\Response
  71. */
  72. public function show($id)
  73. {
  74. //
  75. }
  76. /**
  77. * Show the form for editing the specified resource.
  78. *
  79. * @param int $id
  80. * @return \Illuminate\Http\Response
  81. */
  82. public function edit($id)
  83. {
  84. //
  85. }
  86. /**
  87. * Update the specified resource in storage.
  88. *
  89. * @param \Illuminate\Http\Request $request
  90. * @param int $id
  91. * @return \Illuminate\Http\Response
  92. */
  93. public function update(Request $request, $id)
  94. {
  95. //
  96. }
  97. /**
  98. * Remove the specified resource from storage.
  99. *
  100. * @param int $id
  101. * @return \Illuminate\Http\Response
  102. */
  103. public function destroy($id)
  104. {
  105. //
  106. }
  107. public function history(Product $product)
  108. {
  109. $ppn = Ppn::first()->ppn;
  110. return inertia("StockProducts/Show", [
  111. "initialFilters" => request()->only(
  112. "start_date",
  113. "end_date",
  114. "category"
  115. ),
  116. "productId" => $product->id,
  117. "productNumber" => $product->number,
  118. "historyStockProducts" => HistoryStockProduct::where(
  119. "product_number",
  120. $product->number
  121. )
  122. ->filter(request()->only("start_date", "end_date", "category"))
  123. ->latest()
  124. ->paginate(10)
  125. ->withQueryString()
  126. ->through(
  127. fn($historyStockProduct) => [
  128. "createdAt" => $historyStockProduct->created_at,
  129. "name" => $historyStockProduct->product->name,
  130. "qty" => $historyStockProduct->qty,
  131. "ppn" => $historyStockProduct->ppn ? $ppn : 0,
  132. "unit" => $historyStockProduct->product->unit,
  133. "price" => FunctionService::rupiahFormat(
  134. $historyStockProduct->price *
  135. $historyStockProduct->qty
  136. ),
  137. "category" => $historyStockProduct->purchase_number
  138. ? __("words.addition")
  139. : __("words.reduction"),
  140. ]
  141. ),
  142. ]);
  143. }
  144. public function historyExcel()
  145. {
  146. $ppn = Ppn::first()->ppn;
  147. return new HistoryStockProductExport([
  148. "historyStockProducts" => HistoryStockProduct::where(
  149. "product_number",
  150. request("product_number")
  151. )
  152. ->filter(request()->only("start_date", "end_date", "category"))
  153. ->latest()
  154. ->get()
  155. ->map(
  156. fn($historyStockProduct) => [
  157. "createdAt" => $historyStockProduct->created_at,
  158. "name" => $historyStockProduct->product->name,
  159. "qty" => $historyStockProduct->qty,
  160. "ppn" => $historyStockProduct->ppn ? $ppn : 0,
  161. "unit" => $historyStockProduct->product->unit,
  162. "price" =>
  163. $historyStockProduct->price *
  164. $historyStockProduct->qty,
  165. "category" => $historyStockProduct->purchase_number
  166. ? __("words.addition")
  167. : __("words.reduction"),
  168. ]
  169. ),
  170. ]);
  171. }
  172. }