fix profit value after each order 2
This commit is contained in:
@@ -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('orders')) {
|
||||
return;
|
||||
}
|
||||
|
||||
Schema::table('orders', function (Blueprint $table) {
|
||||
if (! Schema::hasColumn('orders', 'product_id')) {
|
||||
if (Schema::hasColumn('orders', 'customer_id')) {
|
||||
$table->unsignedBigInteger('product_id')->nullable()->after('customer_id');
|
||||
} else {
|
||||
$table->unsignedBigInteger('product_id')->nullable();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
if (! Schema::hasTable('orders')) {
|
||||
return;
|
||||
}
|
||||
|
||||
Schema::table('orders', function (Blueprint $table) {
|
||||
if (Schema::hasColumn('orders', 'product_id')) {
|
||||
$table->dropColumn('product_id');
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,42 @@
|
||||
<?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('orders')) {
|
||||
return;
|
||||
}
|
||||
|
||||
Schema::table('orders', function (Blueprint $table) {
|
||||
if (! Schema::hasColumn('orders', 'fee_source')) {
|
||||
$table->string('fee_source', 32)->nullable()->after('total_fee');
|
||||
}
|
||||
if (! Schema::hasColumn('orders', 'fee_source_key')) {
|
||||
$table->string('fee_source_key', 64)->nullable()->after('fee_source');
|
||||
}
|
||||
if (! Schema::hasColumn('orders', 'fee_source_label')) {
|
||||
$table->string('fee_source_label', 255)->nullable()->after('fee_source_key');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
if (! Schema::hasTable('orders')) {
|
||||
return;
|
||||
}
|
||||
|
||||
Schema::table('orders', function (Blueprint $table) {
|
||||
foreach (['fee_source_label', 'fee_source_key', 'fee_source'] as $col) {
|
||||
if (Schema::hasColumn('orders', $col)) {
|
||||
$table->dropColumn($col);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,34 @@
|
||||
<?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('orders')) {
|
||||
return;
|
||||
}
|
||||
|
||||
Schema::table('orders', function (Blueprint $table) {
|
||||
if (! Schema::hasColumn('orders', 'fee_breakdown')) {
|
||||
$table->json('fee_breakdown')->nullable()->after('fee_source_label');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
if (! Schema::hasTable('orders')) {
|
||||
return;
|
||||
}
|
||||
|
||||
Schema::table('orders', function (Blueprint $table) {
|
||||
if (Schema::hasColumn('orders', 'fee_breakdown')) {
|
||||
$table->dropColumn('fee_breakdown');
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,28 @@
|
||||
-- Backfill helpers for orders (run manually after migrate; test on one row first).
|
||||
-- 1) product_id from first related transaction that has product_id
|
||||
UPDATE orders o
|
||||
INNER JOIN (
|
||||
SELECT order_id, MIN(product_id) AS product_id
|
||||
FROM transactions
|
||||
WHERE order_id IS NOT NULL
|
||||
AND product_id IS NOT NULL
|
||||
AND product_id > 0
|
||||
GROUP BY order_id
|
||||
) t ON t.order_id = o.id
|
||||
SET o.product_id = t.product_id
|
||||
WHERE o.product_id IS NULL;
|
||||
|
||||
-- 2) Recalc fee / total_fee / total_price_original from stored prices
|
||||
-- Example dry-run for one order:
|
||||
-- SELECT id, fee, total_fee,
|
||||
-- ABS(price - price_original) AS fee_new,
|
||||
-- ABS(amount * (price - price_original)) AS total_fee_new
|
||||
-- FROM orders WHERE id = ?;
|
||||
|
||||
UPDATE orders
|
||||
SET
|
||||
fee = ABS(price - price_original),
|
||||
total_fee = ABS(amount * (price - price_original)),
|
||||
total_price_original = amount * price_original
|
||||
WHERE price_original IS NOT NULL
|
||||
AND amount IS NOT NULL;
|
||||
Reference in New Issue
Block a user