added some feature for accounting and price

This commit is contained in:
2026-06-28 15:16:39 +03:30
parent 9f92466ada
commit 3801e5ee88
11 changed files with 423 additions and 39 deletions
@@ -0,0 +1,38 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
if (! Schema::hasTable('products')) {
return;
}
if (Schema::hasColumn('products', 'override_trading_hours')) {
return;
}
Schema::table('products', function (Blueprint $table) {
if (Schema::hasColumn('products', 'auto_close_at')) {
$table->boolean('override_trading_hours')->default(false)->after('auto_close_at');
} else {
$table->boolean('override_trading_hours')->default(false);
}
});
}
public function down(): void
{
if (! Schema::hasTable('products') || ! Schema::hasColumn('products', 'override_trading_hours')) {
return;
}
Schema::table('products', function (Blueprint $table) {
$table->dropColumn('override_trading_hours');
});
}
};
@@ -0,0 +1,80 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
/**
* Backfill: هر معاملهٔ گذشته (orders) که هنوز سند دفترکل ندارد را به transactions_admins منتقل می‌کند.
* تا گزارشات به دفترکل کامل تبدیل شود — مستقل از اتصال به حسابداری بیرونی.
*
* idempotent: order_idهایی که از قبل در transactions_admins هستند رد می‌شوند.
*/
return new class extends Migration
{
public function up(): void
{
if (! Schema::hasTable('orders') || ! Schema::hasTable('transactions_admins')) {
return;
}
$hasOrderId = Schema::hasColumn('transactions_admins', 'order_id');
if (! $hasOrderId) {
return;
}
$existing = array_flip(
DB::table('transactions_admins')->whereNotNull('order_id')->pluck('order_id')->all()
);
$now = now();
DB::table('orders')
->when(Schema::hasColumn('orders', 'sts'), function ($q) {
// معاملات ناموفق (sts=3) را وارد دفترکل نکن
$q->where(function ($inner) {
$inner->whereNull('sts')->orWhere('sts', '!=', 3);
});
})
->orderBy('id')
->chunk(500, function ($orders) use (&$existing, $now) {
$rows = [];
foreach ($orders as $order) {
if (isset($existing[$order->id])) {
continue;
}
$direction = (int) ($order->direction ?? 0);
$note = $direction === 1 ? 'خرید آبشده' : ($direction === 2 ? 'فروش آبشده' : 'معامله');
$rows[] = [
'customer_id' => $order->customer_id,
'customer_to_id' => $order->customer_id,
'order_id' => $order->id,
'user_id' => $order->customer_id,
'wallet' => null,
'transaction_id' => null,
'cash' => (int) round((float) ($order->total_price ?? 0)),
'note' => $note,
'type' => 1,
'gold' => (float) ($order->amount ?? 0),
'balance_cash' => 0,
'balance_gold' => 0,
'created_at' => $order->created_at ?? $now,
'updated_at' => $now,
];
$existing[$order->id] = true;
}
if ($rows !== []) {
DB::table('transactions_admins')->insert($rows);
}
});
}
public function down(): void
{
// برگشت‌پذیر نیست: تفکیک ردیف‌های backfill از ردیف‌های واقعی forward مطمئن نیست،
// پس برای جلوگیری از حذف دادهٔ درست، down هیچ کاری نمی‌کند.
}
};