TransactionController.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Transaction\StoreTransactionRequest;
  5. use App\Http\Requests\Transaction\UpdateTransactionRequest;
  6. use App\Models\Customer;
  7. use App\Models\Discount;
  8. use App\Models\Laundry;
  9. use App\Models\Outlet;
  10. use App\Models\Product;
  11. use App\Models\Transaction;
  12. use App\Models\TransactionStatus;
  13. use Exception;
  14. use Illuminate\Support\Facades\Auth;
  15. use Illuminate\Support\Facades\DB;
  16. use Inertia\Inertia;
  17. class TransactionController extends Controller
  18. {
  19. /**
  20. * Display a listing of the resource.
  21. *
  22. * @return \Inertia\Response
  23. */
  24. public function index()
  25. {
  26. if (Auth::user()->hasRole('Admin')) {
  27. $transactions = Transaction::latest();
  28. } else {
  29. $transactions = Transaction::where('outlet_id', Auth::user()->outlet_id)->latest();
  30. }
  31. return inertia('transaction/Index', [
  32. 'filters' => request()->all('search', 'startDate', 'endDate', 'outlet'),
  33. 'transactions' => $transactions
  34. ->filter(request()->only('search', 'startDate', 'endDate', 'outlet'))
  35. ->latest()
  36. ->paginate(10)
  37. ->withQueryString()
  38. ->through(fn($transaction) => [
  39. 'id' => $transaction->id,
  40. 'transactionNumber' => $transaction->transaction_number,
  41. 'createdAt' => $transaction->created_at,
  42. 'customer' => [
  43. 'number' => $transaction->customer->customer_number,
  44. 'name' => $transaction->customer->name,
  45. 'phone' => $transaction->customer->phone,
  46. ],
  47. 'price' => $transaction->totalPriceAsFullString(),
  48. 'outlet' => $transaction->outlet->name,
  49. 'transactionStatusName' => $transaction->transactionStatus->name,
  50. 'transactionStatusId' => $transaction->transactionStatus->id,
  51. ]),
  52. 'transactionsStatus' => TransactionStatus::all()
  53. ->transform(fn($transactionStatus) => [
  54. 'label' => $transactionStatus->name,
  55. 'value' => $transactionStatus->id,
  56. ]),
  57. 'outlets' => Outlet::all()
  58. ->transform(fn($outlet) => [
  59. 'label' => $outlet->name,
  60. 'value' => $outlet->id,
  61. ]),
  62. ]);
  63. }
  64. /**
  65. * Show the form for creating a new resource.
  66. *
  67. * @return \Inertia\Response
  68. */
  69. public function create()
  70. {
  71. return inertia('transaction/Create', [
  72. 'transactionNumber' => 'TS' . now()->format('YmdHis'),
  73. 'customers' => Inertia::lazy(
  74. fn() => Customer::filter(request('customer'))
  75. ->latest()
  76. ->get()
  77. ->transform(fn($customer) => [
  78. 'name' => $customer->name,
  79. 'customerNumber' => $customer->customer_number,
  80. 'phone' => $customer->phone,
  81. 'checkTransaction' => $customer->checkTransaction(),
  82. ])
  83. ),
  84. 'discount' => Discount::first()->discount,
  85. 'laundries' => Inertia::lazy(
  86. fn() => Laundry::filter(request('laundry'))
  87. ->latest()
  88. ->get()
  89. ->transform(fn($laundry) => [
  90. 'id' => $laundry->id,
  91. 'name' => $laundry->name,
  92. 'unit' => $laundry->unit,
  93. 'price' => $laundry->getRawOriginal('price'),
  94. ])
  95. ),
  96. 'products' => Inertia::lazy(
  97. fn() => Product::filter(request('product'))
  98. ->get()
  99. ->transform(fn($product) => [
  100. 'id' => $product->id,
  101. 'name' => $product->name,
  102. 'unit' => $product->unit,
  103. 'price' => $product->getRawOriginal('price'),
  104. ])
  105. ),
  106. 'customerNumber' => fn() => 'CS' . now()->format('YmdHis'),
  107. 'genders' => [
  108. ['label' => 'Perempuan', 'value' => 1],
  109. ['label' => 'Laki-laki', 'value' => 2],
  110. ],
  111. ]);
  112. }
  113. /**
  114. * Store a newly created resource in storage.
  115. *
  116. * @param \Illuminate\Http\Request $request
  117. * @return \Illuminate\Http\Response
  118. */
  119. public function store(StoreTransactionRequest $request)
  120. {
  121. DB::beginTransaction();
  122. try {
  123. $transaction = Transaction::create([
  124. 'transaction_number' => $request->transaction_number,
  125. 'discount' => $request->discount,
  126. 'transaction_status_id' => 1,
  127. 'customer_number' => $request->customer_number,
  128. 'user_id' => $request->user()->id,
  129. 'outlet_id' => $request->user()->outlet_id,
  130. ]);
  131. if ($request->laundries) {
  132. foreach ($request->laundries as $laundry) {
  133. $transaction->transactionDetails()->create([
  134. 'price' => $laundry['price'],
  135. 'discount' => $laundry['discount'],
  136. 'quantity' => $laundry['quantity'],
  137. 'laundry_id' => $laundry['id'],
  138. ]);
  139. }
  140. }
  141. if ($request->products) {
  142. foreach ($request->products as $product) {
  143. $transaction->transactionDetails()->create([
  144. 'price' => $product['price'],
  145. 'discount' => $product['discount'],
  146. 'quantity' => $product['quantity'],
  147. 'product_id' => $product['id'],
  148. ]);
  149. }
  150. }
  151. if ($request->discount) {
  152. $transaction->mutation()->create([
  153. 'type' => 1,
  154. 'amount' => $transaction->totalPrice() < 0 ? $transaction->subTotal() : $transaction->totalPrice(),
  155. 'outlet_id' => $request->user()->outlet_id,
  156. ]);
  157. $transaction->mutation()->create([
  158. 'type' => 2,
  159. 'amount' => $transaction->totalPrice() < 0 ? $transaction->subTotal() : $transaction->totalPrice(),
  160. 'outlet_id' => $request->user()->outlet_id,
  161. ]);
  162. } else {
  163. $transaction->mutation()->create([
  164. 'type' => 1,
  165. 'amount' => $transaction->totalPrice(),
  166. 'outlet_id' => $request->user()->outlet_id,
  167. ]);
  168. }
  169. DB::commit();
  170. // $transaction = Transaction::with(['outlet', 'customer', 'transactionDetails.laundry'])->latest()->first();
  171. // $discount = $transaction->discount();
  172. // $subTotalAsString = $transaction->subTotalAsString();
  173. // $totalPriceAsString = $transaction->totalPriceAsString();
  174. // foreach ($transaction->transactionDetails as $transactionDetail) {
  175. // $totalPriceAsStringDetail = $transactionDetail->totalPriceAsString();
  176. // $transactionDetail->totalPriceAsString = $totalPriceAsStringDetail;
  177. // }
  178. // $transaction->discount = $discount;
  179. // $transaction->subTotalAsString = $subTotalAsString;
  180. // $transaction->totalPriceAsString = $totalPriceAsString;
  181. // try {
  182. // $socket = new WebsocketClient(
  183. // new SocketClient('ws://127.0.0.1:5544')
  184. // );
  185. // $socket->setHost('escpos-server');
  186. // $socket->connect();
  187. // $socket->send(json_encode($transaction));
  188. // $socket->close();
  189. // } catch (Exception $e) {
  190. // return back()->with('error', __('messages.error.store.transaction'));
  191. // }
  192. // Http::post('https://gerbangchatapi.dijitalcode.com/chat/send?id=bambslaundry', [
  193. // 'receiver' => $transaction->customer->phone,
  194. // 'message' => 'Terima kasih sudah mempercayakan layanan laundry kepada Bamb\'s Laundry. Nomor transaksi Anda adalah *' . $request->transaction_number . '*',
  195. // ]);
  196. return back()->with('success', __('messages.success.store.transaction'));
  197. } catch (Exception $e) {
  198. DB::rollBack();
  199. return back()->with('error', __('messages.error.store.transaction'));
  200. }
  201. }
  202. /**
  203. * Display the specified resource.
  204. *
  205. * @param Transaction $transaction
  206. * @return \Inertia\Response
  207. */
  208. public function show(Transaction $transaction)
  209. {
  210. return inertia('transaction/Show', [
  211. 'transaction' => [
  212. 'number' => $transaction->transaction_number,
  213. 'statusId' => $transaction->transactionStatus->id,
  214. 'status' => $transaction->transactionStatus->name,
  215. 'discount' => $transaction->discount,
  216. 'price' => $transaction->totalPriceAsFullString(),
  217. 'dateLaundry' => $transaction->created_at,
  218. ],
  219. 'user' => [
  220. 'name' => $transaction->user->name,
  221. 'phone' => $transaction->user->phone,
  222. 'email' => $transaction->user->email,
  223. ],
  224. 'customer' => [
  225. 'number' => $transaction->customer->customer_number,
  226. 'name' => $transaction->customer->name,
  227. 'phone' => $transaction->customer->phone,
  228. 'address' => $transaction->customer->address,
  229. ],
  230. 'outlet' => [
  231. 'name' => $transaction->outlet->name,
  232. 'address' => $transaction->outlet->address,
  233. ],
  234. 'transactionDetails' => $transaction->transactionDetails
  235. ->transform(fn($transactionDetail) => [
  236. 'item' => $transactionDetail->laundry->name ?? $transactionDetail->product->name,
  237. 'unit' => $transactionDetail->laundry->unit ?? $transactionDetail->product->unit,
  238. 'quantity' => $transactionDetail->quantity,
  239. 'discount' => $transactionDetail->discount,
  240. 'price' => $transactionDetail->price,
  241. 'totalPrice' => $transactionDetail->totalPriceAsFullString(),
  242. ]),
  243. ]);
  244. }
  245. /**
  246. * Show the form for editing the specified resource.
  247. *
  248. * @param Int $id
  249. * @return \Inertia\Response
  250. */
  251. public function edit($id)
  252. {
  253. //
  254. }
  255. /**
  256. * Update the specified resource in storage.
  257. *
  258. * @param \Illuminate\Http\Request $request
  259. * @param Transaction $transaction
  260. * @return \Illuminate\Http\Response
  261. */
  262. public function update(UpdateTransactionRequest $request, Transaction $transaction)
  263. {
  264. $transaction->update($request->validated());
  265. // if ($transaction->transaction_status_id == 2) {
  266. // $statusMessage = " sudah diproses, harap menunggu sampai pemberitahuan selanjutnya.";
  267. // } else if ($transaction->transaction_status_id == 3) {
  268. // $statusMessage = " sudah selesai, silahkan diambil.";
  269. // } else {
  270. // $statusMessage = " sudah diambil. Silahkan datang lagi di kemudian hari jika ingin laundry kembali, terima kasih.";
  271. // }
  272. // Http::post('https://gerbangchatapi.dijitalcode.com/chat/send?id=bambslaundry', [
  273. // 'receiver' => $transaction->customer->phone,
  274. // 'message' => 'Layanan laundry Anda dengan nomor transaksi *' . $transaction->transaction_number . '*' . $statusMessage,
  275. // ]);
  276. return back()->with('success', __('messages.success.update.transaction_status'));
  277. }
  278. /**
  279. * Remove the specified resource from storage.
  280. *
  281. * @param Int $id
  282. * @return \Illuminate\Http\Response
  283. */
  284. public function destroy($id)
  285. {
  286. //
  287. }
  288. }