fixed kilo in metals 5
This commit is contained in:
@@ -21,6 +21,7 @@ use App\Http\Controllers\Admin\TahesabController;
|
||||
use App\Models\Setting;
|
||||
use App\Models\User;
|
||||
use App\Services\BalanceReportService;
|
||||
use App\Services\ConfigService;
|
||||
use App\Services\WalletBalanceService;
|
||||
use Hekmatinasser\Verta\Verta;
|
||||
|
||||
@@ -218,6 +219,161 @@ class WalletController extends Controller
|
||||
return response()->json(true);
|
||||
}
|
||||
|
||||
/** گزینههای شارژ کیف پول — نقدی + محصولات فعال */
|
||||
public function chargeTargets()
|
||||
{
|
||||
$configService = app(ConfigService::class);
|
||||
$mainWalletProductIds = $configService->mainWalletProductIds();
|
||||
$balanceReport = app(BalanceReportService::class);
|
||||
|
||||
$targets = [
|
||||
[
|
||||
'key' => 'money',
|
||||
'kind' => 'money',
|
||||
'label' => 'نقدی (ریال)',
|
||||
'unit' => 'ریال',
|
||||
],
|
||||
];
|
||||
|
||||
$products = Product::query()
|
||||
->where('active', 1)
|
||||
->orderBy('metal_id')
|
||||
->orderBy('id')
|
||||
->get(['id', 'title', 'unit', 'metal_id', 'group_type', 'trade_by_quantity', 'amount', 'direct_price']);
|
||||
|
||||
foreach ($products as $product) {
|
||||
$usesMainWallet = in_array((int) $product->id, $mainWalletProductIds, true);
|
||||
$label = (string) $product->title;
|
||||
if ($usesMainWallet) {
|
||||
$label .= ' (کیف اصلی)';
|
||||
}
|
||||
|
||||
$targets[] = [
|
||||
'key' => 'product:' . $product->id,
|
||||
'kind' => 'metal',
|
||||
'product_id' => (int) $product->id,
|
||||
'label' => $label,
|
||||
'unit' => $this->resolveProductChargeUnit($product),
|
||||
'metal_id' => max(1, (int) ($product->metal_id ?? 1)),
|
||||
'uses_main_wallet' => $usesMainWallet,
|
||||
'trade_by_quantity' => (bool) $product->trade_by_quantity,
|
||||
'group_type' => (string) ($product->group_type ?? ''),
|
||||
];
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'targets' => $targets,
|
||||
'catalogMetals' => $balanceReport->catalogMetalsMeta(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* شارژ یکپارچه کیف پول — نقدی یا محصول (با رعایت کیف اصلی برای آبشده).
|
||||
*/
|
||||
public function charge_wallet(Request $request)
|
||||
{
|
||||
$productId = (int) $request->input('product_id', 0);
|
||||
if ($productId > 0) {
|
||||
return $this->chargeProductMetal($request, $productId);
|
||||
}
|
||||
|
||||
return $this->charge($request);
|
||||
}
|
||||
|
||||
private function chargeProductMetal(Request $request, int $productId)
|
||||
{
|
||||
$data = $request->all();
|
||||
$product = Product::findOrFail($productId);
|
||||
$customer = Customer::findOrFail($data['customer_id']);
|
||||
$deposit = ((float) ($data['value'] ?? 0)) > 0;
|
||||
|
||||
$effectiveProductId = Controller::effectiveProductWalletId($productId);
|
||||
$wallet_interface = new WalletInterface(
|
||||
(int) $data['customer_id'],
|
||||
(int) $customer->type,
|
||||
null,
|
||||
$effectiveProductId
|
||||
);
|
||||
$wallet = $wallet_interface->wallet;
|
||||
|
||||
if (!$deposit && $wallet !== null && (float) $wallet->gold < abs((float) $data['value'])) {
|
||||
throw new UnprocessableEntityHttpException('مقدار موجودی کمتر از درخواست شماست');
|
||||
}
|
||||
|
||||
$goldBlock = 0;
|
||||
$user = auth()->user();
|
||||
$unitLabel = $this->resolveProductChargeUnit($product);
|
||||
|
||||
$Withdraw = Withdraw::create([
|
||||
'user_id' => $user->id,
|
||||
'customer_id' => $customer->id,
|
||||
'gold' => $data['value'],
|
||||
'note' => $data['note'] ?? null,
|
||||
'sts' => 4,
|
||||
]);
|
||||
|
||||
new TransactionInterface($wallet_interface, [
|
||||
'cash' => 0,
|
||||
'gold' => $data['value'],
|
||||
'product_id' => $productId,
|
||||
'expire_at' => now(),
|
||||
'note' => $data['note'] ?? null,
|
||||
'withdraw_id' => $Withdraw->id,
|
||||
'active' => true,
|
||||
], 7, $deposit, false, $goldBlock);
|
||||
|
||||
if ($this->Config('send_to_tahesab') == 'true') {
|
||||
try {
|
||||
$data['direction'] = $deposit ? 1 : 0;
|
||||
$tahesab = new TahesabController();
|
||||
$tahesab->walletGold($data);
|
||||
} catch (\Exception $e) {
|
||||
return response($e->getMessage(), 500);
|
||||
}
|
||||
}
|
||||
|
||||
$status = $deposit ? ' شارژ شد ' : ' کسر شد ';
|
||||
$formatted = self::formatSmsGoldWeight($data['value']);
|
||||
|
||||
if ($this->Config('charge_notif') == 'true') {
|
||||
$message = 'کیف پول کاربر ' . $customer->name . ' به مقدار ' . $formatted . ' ' . $unitLabel . ' در تاریخ ' . verta()->format('Y/n/j H:i:s') . $status . 'بابت' . "\n"
|
||||
. ($data['note'] ?? '')
|
||||
. "\n"
|
||||
. $this->Config('name_fa');
|
||||
User::where('send_sms', 1)->get()->each(function ($admin) use ($message) {
|
||||
$this->send($admin->mobile, $message);
|
||||
});
|
||||
}
|
||||
|
||||
$message = 'کیف پول شما به اندازه ' . $formatted . ' ' . $unitLabel . ' در تاریخ ' . verta()->format('Y/n/j H:i:s') . $status . ' بابت' . "\n"
|
||||
. ($data['note'] ?? '')
|
||||
. "\n"
|
||||
. $this->Config('name_fa');
|
||||
$this->Send($customer->mobile, $message);
|
||||
|
||||
return response()->json(true);
|
||||
}
|
||||
|
||||
private function resolveProductChargeUnit(Product $product): string
|
||||
{
|
||||
$payload = Controller::productUnitPayload($product);
|
||||
|
||||
if (
|
||||
Controller::usesQuantityTrade($payload)
|
||||
&& !Controller::isWeightBasedProductUnit($payload['unit'] ?? null)
|
||||
) {
|
||||
$unit = trim((string) ($payload['unit'] ?? ''));
|
||||
|
||||
return $unit !== '' ? $unit : 'عدد';
|
||||
}
|
||||
|
||||
if (Controller::isWeightBasedProductUnit($payload['unit'] ?? null)) {
|
||||
return trim((string) $payload['unit']);
|
||||
}
|
||||
|
||||
return Controller::productStorageWeightUnit($product);
|
||||
}
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$balanceReport = app(BalanceReportService::class);
|
||||
|
||||
Reference in New Issue
Block a user