CustomerController.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Customer\StoreCustomerRequest;
  5. use App\Http\Requests\Customer\UpdateCustomerRequest;
  6. use App\Models\Customer;
  7. class CustomerController extends Controller
  8. {
  9. /**
  10. * Display a listing of the resource.
  11. *
  12. * @return \Inertia\Response
  13. */
  14. public function index()
  15. {
  16. return inertia('customer/Index', [
  17. 'filters' => request()->all('search'),
  18. 'customers' => Customer::filter(request()->only('search'))
  19. ->latest()
  20. ->paginate(10)
  21. ->withQueryString()
  22. ->through(fn($customer) => [
  23. 'id' => $customer->id,
  24. 'customer_number' => $customer->customer_number,
  25. 'name' => $customer->name,
  26. 'phone' => $customer->phone,
  27. 'gender' => $customer->gender_id,
  28. ]),
  29. ]);
  30. }
  31. /**
  32. * Show the form for creating a new resource.
  33. *
  34. * @return \Inertia\Response
  35. */
  36. public function create()
  37. {
  38. return inertia('customer/Create', [
  39. 'customer_number' => 'CS' . now()->format('YmdHis'),
  40. 'genders' => [
  41. ['label' => 'Perempuan', 'value' => 1],
  42. ['label' => 'Laki-laki', 'value' => 2],
  43. ],
  44. ]);
  45. }
  46. /**
  47. * Store a newly created resource in storage.
  48. *
  49. * @param \Illuminate\Http\Request $request
  50. * @return \Illuminate\Http\Response
  51. */
  52. public function store(StoreCustomerRequest $request)
  53. {
  54. Customer::create($request->validated());
  55. if ($request->transaction_number) {
  56. return back()->with('success', __('messages.success.store.customer'));
  57. } else {
  58. return to_route('customers.index')->with('success', __('messages.success.store.customer'));
  59. }
  60. }
  61. /**
  62. * Display the specified resource.
  63. *
  64. * @param int $id
  65. * @return \Inertia\Response
  66. */
  67. public function show($id)
  68. {
  69. //
  70. }
  71. /**
  72. * Show the form for editing the specified resource.
  73. *
  74. * @param Customer $customer
  75. * @return \Inertia\Response
  76. */
  77. public function edit(Customer $customer)
  78. {
  79. return inertia('customer/Edit', [
  80. 'customer' => [
  81. 'id' => $customer->id,
  82. 'customer_number' => $customer->customer_number,
  83. 'name' => $customer->name,
  84. 'phone' => $customer->phone,
  85. 'gender_id' => $customer->getRawOriginal('gender_id'),
  86. ],
  87. 'genders' => [
  88. ['label' => 'Perempuan', 'value' => 1],
  89. ['label' => 'Laki-laki', 'value' => 2],
  90. ],
  91. 'transactions' => $customer->transaction()
  92. ->latest()
  93. ->paginate(10)
  94. ->withQueryString()
  95. ->through(fn($transaction) => [
  96. 'id' => $transaction->id,
  97. 'transactionNumber' => $transaction->transaction_number,
  98. 'createdAt' => $transaction->created_at,
  99. 'customer' => [
  100. 'number' => $customer->customer_number,
  101. 'name' => $customer->name,
  102. 'phone' => $customer->phone,
  103. ],
  104. 'price' => $transaction->totalPriceAsFullString(),
  105. 'outlet' => $transaction->outlet->name,
  106. 'transactionStatusName' => $transaction->transactionStatus->name,
  107. 'transactionStatusId' => $transaction->transactionStatus->id,
  108. ]),
  109. ]);
  110. }
  111. /**
  112. * Update the specified resource in storage.
  113. *
  114. * @param \Illuminate\Http\Request $request
  115. * @param Customer $customer
  116. * @return \Illuminate\Http\Response
  117. */
  118. public function update(UpdateCustomerRequest $request, Customer $customer)
  119. {
  120. $customer->update($request->validated());
  121. return back()->with('success', __('messages.success.update.customer'));
  122. }
  123. /**
  124. * Remove the specified resource from storage.
  125. *
  126. * @param Customer $customer
  127. * @return \Illuminate\Http\Response
  128. */
  129. public function destroy(Customer $customer)
  130. {
  131. $customer->delete();
  132. return to_route('customers.index')->with('success', __('messages.success.destroy.customer'));
  133. }
  134. }