catalogMetalIds(); $records = Order::with([ 'customer', 'product' => static function ($q) { $q->withTrashed()->select(['id', 'title', 'unit', 'metal_id', 'group_type', 'amount', 'trade_by_quantity', 'direct_price']); }, 'transactions.product' => static function ($q) { $q->withTrashed()->select(['id', 'title', 'unit', 'metal_id', 'group_type', 'amount', 'trade_by_quantity', 'direct_price']); }, ]); TableEngine::create($records, $request->all()) ->initSearch(['customer.name']) ->select([ '*',Order::$SELECT_STS_STR ])->export($records); if (isset($records['rows'])) { foreach ($records['rows'] as &$row) { $balanceReport->enrichOrderTableRow($row, $catalogMetalIds); } unset($row); } $records['catalogMetals'] = $balanceReport->catalogMetalsMeta(); return response()->json($records); } public function singleOrderTable(Request $request){ $balanceReport = app(BalanceReportService::class); $catalogMetalIds = app(WalletBalanceService::class)->catalogMetalIds(); $records = Order::with([ 'customer', 'product' => static function ($q) { $q->withTrashed()->select(['id', 'title', 'unit', 'metal_id', 'group_type', 'amount', 'trade_by_quantity', 'direct_price']); }, 'transactions.product' => static function ($q) { $q->withTrashed()->select(['id', 'title', 'unit', 'metal_id', 'group_type', 'amount', 'trade_by_quantity', 'direct_price']); }, ])->where('type',2); TableEngine::create($records, $request->all()) ->initSearch(['customer.name']) ->select([ '*',Order::$SELECT_STS_STR ])->export($records); if (isset($records['rows'])) { foreach ($records['rows'] as &$row) { $balanceReport->enrichOrderTableRow($row, $catalogMetalIds); } unset($row); } $records['catalogMetals'] = $balanceReport->catalogMetalsMeta(); return response()->json($records); } /** * سفارش‌هایی که نزد همکار (Montzar) ثبت شده‌اند — فقط وقتی منبع قیمت montzar است. */ public function colleagueTable(Request $request) { $config = app(ConfigService::class); if (!$config->usesMontzarPricing()) { return response()->json([ 'message' => 'این لیست فقط وقتی منبع قیمت montzar است در دسترس است.', ], 422); } $hasProvider = Schema::hasColumn('orders', 'provider_id'); $hasMontzarPrice = Schema::hasColumn('orders', 'total_price_montzar'); if (!$hasProvider && !$hasMontzarPrice) { return response()->json([ 'rows' => [], 'total' => 0, ]); } $records = Order::with([ 'customer:id,name,mobile', 'product' => static function ($q) { $q->withTrashed()->select(['id', 'title', 'unit']); }, ])->where(function ($q) use ($hasProvider, $hasMontzarPrice) { if ($hasProvider) { $q->whereNotNull('provider_id'); } if ($hasMontzarPrice) { if ($hasProvider) { $q->orWhereNotNull('total_price_montzar'); } else { $q->whereNotNull('total_price_montzar'); } } }); TableEngine::create($records, $request->all()) ->initSearch(['customer.name', 'customer.mobile']) ->select([ '*', Order::$SELECT_STS_STR ])->export($records); if (isset($records['rows'])) { foreach ($records['rows'] as &$row) { $amount = (float) ($row['amount'] ?? 0); $serviceCharge = (float) ($row['service_charge'] ?? 0); $feeAsset = (string) ($row['fee_asset'] ?? 'rial'); $montzarUnit = $hasMontzarPrice ? (float) ($row['total_price_montzar'] ?? 0) : 0.0; // وقتی کارمزد از طلا کم شده، مقدار اولیهٔ مشتری = مقدار نزد همکار + کارمزد $customerAmount = $feeAsset === 'gold' ? round($amount + $serviceCharge, 3) : round($amount, 3); $row['customer_amount'] = $customerAmount; $row['colleague_amount'] = round($amount, 3); $row['colleague_unit_price'] = (int) round($montzarUnit); $row['colleague_total_price'] = (int) round($amount * $montzarUnit); $row['direction_label'] = Order::$DIRECTION[(int) ($row['direction'] ?? 0)] ?? (string) ($row['direction'] ?? ''); } unset($row); } return response()->json($records); } }