CustomerController.php 5.1KB

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