| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
-
- namespace App\Services;
-
- use Carbon\Carbon;
- use Illuminate\Support\Facades\DB;
-
- class DashboardService
- {
- public static function productBestSelling()
- {
- $query = DB::table("sale_details")
- ->selectRaw(
- "products.name AS title,
- SUM(sale_details.qty) AS qty,
- products.profit"
- )
- ->join(
- "products",
- "products.number",
- "=",
- "sale_details.product_number"
- )
- ->groupByRaw("product_number")
- ->orderByRaw("qty DESC")
- ->limit(5)
- ->get();
-
- return [
- "title" => __("words.best_product"),
- "data" => $query,
- ];
- }
-
- public static function productAmount()
- {
- return [
- "data" => [
- self::amount("sales", __("words.sale"), __("words.today")),
- self::amount(
- "purchases",
- __("words.purchase"),
- __("words.today")
- ),
- self::amount(
- "products",
- __("words.product"),
- __("words.today")
- ),
- self::amount(
- "stock_products",
- __("words.stock_product"),
- __("words.today")
- ),
- ],
- ];
- }
-
- public static function salePurchaseAmountStatistic()
- {
- return [
- "title" => __("words.sale_and_purchase"),
- "description" => __("words.period", [
- "number" => now()->translatedFormat("Y"),
- ]),
- "data" => [
- __("words.sale") => self::amountStatistic("sales"),
- __("words.purchase") => self::amountStatistic("purchases"),
- ],
- ];
- }
-
- public static function salePriceStatistic()
- {
- return [
- "title" => __("words.sale"),
- "data" => self::priceStatistic("sale_details"),
- ];
- }
-
- public static function purchasePriceStatistic()
- {
- return [
- "title" => __("words.purchase"),
- "data" => self::priceStatistic("purchase_details"),
- ];
- }
-
- public static function amount(
- string $table,
- string $title,
- string $description
- ) {
- return DB::table($table)
- ->selectRaw(
- "COUNT(*) AS amount, (
- SELECT
- COUNT(*)
- FROM
- $table
- WHERE
- DATE(created_at) = CURDATE()) AS amountToday"
- )
- ->get()
- ->transform(
- fn($table) => [
- "title" => $title,
- "amount" => $table->amount,
- "amountToday" => $table->amountToday,
- "amountTodayDescription" => $description,
- ]
- )
- ->first();
- }
-
- public static function amountStatistic(string $table)
- {
- return DB::table($table)
- ->selectRaw(
- "COUNT(*) AS amount,
- DATE_FORMAT(created_at, '%b') AS month"
- )
- ->whereRaw("YEAR(created_at) = YEAR(CURDATE())")
- ->groupByRaw("month")
- ->orderByRaw("created_at")
- ->get()
- ->pluck("amount", "month");
- }
-
- public static function priceStatistic(string $table)
- {
- return DB::table($table)
- ->selectRaw(
- "price,
- created_at"
- )
- ->orderByRaw("created_at")
- ->get()
- ->groupBy([
- fn($value) => Carbon::parse($value->created_at)->format("Y"),
- fn($value) => Carbon::parse($value->created_at)->format("M"),
- ])
- ->take(2)
- ->transform(
- fn($year) => $year->transform(
- fn($month) => $month->sum(fn($arr) => $arr->price)
- )
- );
- }
-
- public static function dump()
- {
- return dd(
- self::productAmount(),
- self::productBestSelling(),
- self::salePurchaseAmountStatistic(),
- self::salePriceStatistic(),
- self::purchasePriceStatistic()
- );
- }
- }
|