70 lines
3.5 KiB
PHP
70 lines
3.5 KiB
PHP
<?php
|
|
|
|
use Phinx\Migration\AbstractMigration;
|
|
|
|
/**
|
|
* Crea le tabelle per gli abbonamenti dei saloni:
|
|
* - subscription_plans: i piani disponibili (gestiti dall'admin)
|
|
* - shop_subscriptions: l'abbonamento corrente di ogni salone
|
|
*
|
|
* Modello: l'abbonamento è il "motore" che accende/spegne shops.active.
|
|
* expires_at è la data chiave: superata, il cron mette status=expired e spegne il salone.
|
|
*/
|
|
class CreateSubscriptions extends AbstractMigration
|
|
{
|
|
public function change(): void
|
|
{
|
|
// ---- subscription_plans ----
|
|
$plans = $this->table('subscription_plans', [
|
|
'id' => false,
|
|
'primary_key' => ['id'],
|
|
'engine' => 'InnoDB',
|
|
'encoding' => 'utf8mb4',
|
|
'collation' => 'utf8mb4_unicode_ci',
|
|
]);
|
|
|
|
$plans
|
|
->addColumn('id', 'integer', ['identity' => true, 'signed' => false])
|
|
->addColumn('name', 'string', ['limit' => 80])
|
|
->addColumn('description', 'string', ['limit' => 255, 'null' => true])
|
|
->addColumn('price', 'decimal', ['precision' => 8, 'scale' => 2, 'default' => '0.00'])
|
|
->addColumn('billing_period', 'enum', ['values' => ['monthly', 'yearly'], 'default' => 'monthly'])
|
|
->addColumn('stripe_price_id', 'string', ['limit' => 120, 'null' => true])
|
|
->addColumn('is_active', 'boolean', ['default' => true])
|
|
->addColumn('created_at', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'null' => true])
|
|
->addColumn('updated_at', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'update' => 'CURRENT_TIMESTAMP', 'null' => true])
|
|
->create();
|
|
|
|
// ---- shop_subscriptions ----
|
|
$subs = $this->table('shop_subscriptions', [
|
|
'id' => false,
|
|
'primary_key' => ['id'],
|
|
'engine' => 'InnoDB',
|
|
'encoding' => 'utf8mb4',
|
|
'collation' => 'utf8mb4_unicode_ci',
|
|
]);
|
|
|
|
$subs
|
|
->addColumn('id', 'biginteger', ['identity' => true, 'signed' => false])
|
|
->addColumn('shop_id', 'biginteger', ['signed' => false])
|
|
->addColumn('plan_id', 'integer', ['signed' => false, 'null' => true])
|
|
->addColumn('status', 'enum', [
|
|
'values' => ['active', 'expired', 'blocked', 'trialing', 'pending'],
|
|
'default' => 'pending',
|
|
])
|
|
->addColumn('started_at', 'date', ['null' => true])
|
|
->addColumn('expires_at', 'date', ['null' => true])
|
|
->addColumn('block_reason', 'string', ['limit' => 255, 'null' => true])
|
|
->addColumn('stripe_customer_id', 'string', ['limit' => 120, 'null' => true])
|
|
->addColumn('stripe_subscription_id', 'string', ['limit' => 120, 'null' => true])
|
|
->addColumn('created_at', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'null' => true])
|
|
->addColumn('updated_at', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'update' => 'CURRENT_TIMESTAMP', 'null' => true])
|
|
->addIndex(['shop_id'], ['unique' => true, 'name' => 'uniq_shop'])
|
|
->addIndex(['status'], ['name' => 'idx_status'])
|
|
->addIndex(['expires_at'], ['name' => 'idx_expires'])
|
|
->addForeignKey('shop_id', 'shops', 'id', ['delete' => 'CASCADE', 'update' => 'NO_ACTION', 'constraint' => 'fk_sub_shop'])
|
|
->addForeignKey('plan_id', 'subscription_plans', 'id', ['delete' => 'SET_NULL', 'update' => 'NO_ACTION', 'constraint' => 'fk_sub_plan'])
|
|
->create();
|
|
}
|
|
}
|