diff --git a/app/Http/Controllers/Admin/SaleController.php b/app/Http/Controllers/Admin/SaleController.php index 2f5876f..69d9a4d 100644 --- a/app/Http/Controllers/Admin/SaleController.php +++ b/app/Http/Controllers/Admin/SaleController.php @@ -3,16 +3,40 @@ namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; +use App\Models\Location; use App\Models\Sale; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; class SaleController extends Controller { public function index(Request $request) { + $customer_with_deposits = Sale::with('customer') + ->whereHas('customer', function ($query) { + $query->whereHas('locationFavorites', function ($query) { + $query->whereIn('id', Location::all()->pluck('id')->toArray()); + }); + }) + ->whereBetween('created_at', [now()->startOfMonth(), now()->endOfMonth()]) + ->where('payed_with', Sale::PAYED_WITH_DEPOSIT) + ->groupBy('customer_id') + ->selectRaw('SUM(amount) as total, customer_id') + ->orderBy('total', 'desc') + ->limit(10) + ->get(); + + $customer_with_paylaters = Sale::with('customer') + ->whereBetween('created_at', [now()->startOfMonth(), now()->endOfMonth()]) + ->where('payed_with', Sale::PAYED_WITH_PAYLATER) + ->groupBy('customer_id') + ->selectRaw('SUM(amount) as total, customer_id') + ->orderBy('total', 'desc') + ->limit(10) + ->get(); + $query = Sale::with(['items', 'customer.level']) - ->withCount(['items']) - ->orderBy('updated_at', 'desc'); + ->withCount(['items']); if ($request->q != '') { $query->where('code', 'ilike', "%$request->q%") @@ -23,8 +47,40 @@ class SaleController extends Controller }); } + if ($request->location_ids != '') { + $query->whereHas('customer', function ($q) use ($request) { + $q->whereHas('locationFavorites', function ($q) use ($request) { + $q->whereIn('location_id', $request->location_ids); + }); + }); + } + + if ($request->payment != '') { + $query->where('payed_with', $request->payment); + } else { + $query->whereIn('payed_with', [Sale::PAYED_WITH_PAYLATER, Sale::PAYED_WITH_DEPOSIT]); + } + + if ($request->startDate != '' && $request->endDate != '') { + $query->whereBetween(DB::raw('DATE(created_at)'), [$request->startDate, $request->endDate]); + } + + $sortBy = 'created_at'; + $sortRule = 'desc'; + if ($request->sortBy != '' && $request->sortRule != '') { + $sortBy = $request->sortBy; + $sortRule = $request->sortRule; + } + + $query->orderBy($sortBy, $sortRule); + return inertia('Sale/Index', [ 'query' => $query->paginate(), + 'customer_with_deposit' => $customer_with_deposits, + 'customer_with_paylaters' => $customer_with_paylaters, + '_q' => $request->q, + '_sortBy' => $sortBy, + '_sortRule' => $sortRule, ]); } diff --git a/app/Models/Sale.php b/app/Models/Sale.php index 382fb11..d2533f1 100644 --- a/app/Models/Sale.php +++ b/app/Models/Sale.php @@ -35,6 +35,7 @@ class Sale extends Model 'format_human_created_at', 'format_created_at', 'display_amount', + 'payment_with', ]; protected static function booted(): void @@ -77,6 +78,21 @@ class Sale extends Model }); } + public function paymentWith(): Attribute + { + return Attribute::make(get: function () { + if ($this->payed_with != null) { + return [ + self::PAYED_WITH_DEPOSIT => 'Deposit', + self::PAYED_WITH_PAYLATER => 'Hutang', + self::PAYED_WITH_POIN => 'Penukaran Poin', + ][$this->payed_with]; + } + + return ''; + }); + } + public function create_notification() { if ($this->payed_with == self::PAYED_WITH_POIN) { diff --git a/resources/js/Components/FormInputDateRange.jsx b/resources/js/Components/FormInputDateRange.jsx index fbf008e..4ba18d0 100644 --- a/resources/js/Components/FormInputDateRange.jsx +++ b/resources/js/Components/FormInputDateRange.jsx @@ -13,13 +13,14 @@ export default function FormInputDateRanger({
- Elsoft © {new Date().getFullYear()} + SesuaiHarapanDev © {new Date().getFullYear()}{' '} + {new Date().getFullYear() !== 2023 + ? '- ' + new Date().getFullYear() + : ''}
diff --git a/resources/js/Pages/Location/SelectionInput.jsx b/resources/js/Pages/Location/SelectionInput.jsx index 940beb6..8d5b344 100644 --- a/resources/js/Pages/Location/SelectionInput.jsx +++ b/resources/js/Pages/Location/SelectionInput.jsx @@ -19,6 +19,7 @@ export default function SelectionInput(props) { placeholder = '', error = '', all = 0, + type = 'id', } = props const [showItems, setShowItem] = useState([]) @@ -45,7 +46,11 @@ export default function SelectionInput(props) { const handleSelectItem = (item) => { setIsSelected(true) - onItemSelected(item.id) + if (type === 'id') { + onItemSelected(item.id) + } else { + onItemSelected(item) + } setSelected(item.name) setIsOpen(false) } diff --git a/resources/js/Pages/Sale/Detail.jsx b/resources/js/Pages/Sale/Detail.jsx index b4eb0d1..8a74271 100644 --- a/resources/js/Pages/Sale/Detail.jsx +++ b/resources/js/Pages/Sale/Detail.jsx @@ -100,7 +100,6 @@ export default function Detail(props) {- Customer + Total Beli | + + + + {customer_with_deposit.map((sale) => ( +|
---|---|
+ + {sale.customer.name} + + | ++ Rp. {formatIDR(sale.total)} + | +
- Tanggal + Nama | - Voucher + Total Beli | +
---|---|
+ + {sale.customer.name} + + | ++ Rp. {formatIDR(sale.total)} + | +
- Total + Voucher | +{sale.format_created_at} + | + {sale.payment_with} + |
{formatIDR(
sale.items_count
@@ -145,6 +319,7 @@ export default function Info(props) {
+ Lokasi:
+
+
+
+ {locations.map((location) => (
+
+
+
+ ))}
+ {location.name}
+ handleRemoveLocation(location)}>
+
+
+
+ Pembayaran :
+
+
+
+ |
---|