CustomerController.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 back()->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' => (int) $customer->getRawOriginal('gender_id'),
  86. 'relation' => $customer->transaction()->exists(),
  87. ],
  88. 'genders' => [
  89. ['label' => 'Perempuan', 'value' => 1],
  90. ['label' => 'Laki-laki', 'value' => 2],
  91. ],
  92. 'transactions' => $customer->transaction()
  93. ->latest()
  94. ->paginate(10)
  95. ->withQueryString()
  96. ->through(fn($transaction) => [
  97. 'id' => $transaction->id,
  98. 'transactionNumber' => $transaction->transaction_number,
  99. 'createdAt' => $transaction->created_at,
  100. 'customer' => [
  101. 'number' => $customer->customer_number,
  102. 'name' => $customer->name,
  103. 'phone' => $customer->phone,
  104. ],
  105. 'price' => $transaction->totalPriceAsFullString(),
  106. 'outlet' => $transaction->outlet->name,
  107. 'transactionStatusName' => $transaction->transactionStatus->name,
  108. 'transactionStatusId' => $transaction->transactionStatus->id,
  109. ]),
  110. ]);
  111. }
  112. /**
  113. * Update the specified resource in storage.
  114. *
  115. * @param \Illuminate\Http\Request $request
  116. * @param Customer $customer
  117. * @return \Illuminate\Http\Response
  118. */
  119. public function update(UpdateCustomerRequest $request, Customer $customer)
  120. {
  121. $customer->update($request->validated());
  122. return back()->with('success', __('messages.success.update.customer'));
  123. }
  124. /**
  125. * Remove the specified resource from storage.
  126. *
  127. * @param Customer $customer
  128. * @return \Illuminate\Http\Response
  129. */
  130. public function destroy(Customer $customer)
  131. {
  132. $customer->delete();
  133. return to_route('customers.index')->with('success', __('messages.success.destroy.customer'));
  134. }
  135. }