207 lines
6.5 KiB
PHP
207 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace App\Database\Migrations;
|
|
|
|
use CodeIgniter\Database\Migration;
|
|
|
|
class CreateDealsTables extends Migration
|
|
{
|
|
public function up()
|
|
{
|
|
// Таблица этапов сделок
|
|
$this->forge->addField([
|
|
'id' => [
|
|
'type' => 'INT',
|
|
'constraint' => 11,
|
|
'unsigned' => true,
|
|
'auto_increment' => true,
|
|
],
|
|
'organization_id' => [
|
|
'type' => 'INT',
|
|
'constraint' => 11,
|
|
'unsigned' => true,
|
|
],
|
|
'name' => [
|
|
'type' => 'VARCHAR',
|
|
'constraint' => 255,
|
|
],
|
|
'color' => [
|
|
'type' => 'VARCHAR',
|
|
'constraint' => 7,
|
|
'default' => '#6B7280',
|
|
],
|
|
'order_index' => [
|
|
'type' => 'INT',
|
|
'constraint' => 11,
|
|
'default' => 0,
|
|
],
|
|
'type' => [
|
|
'type' => 'ENUM',
|
|
'constraint' => ['progress', 'won', 'lost'],
|
|
'default' => 'progress',
|
|
],
|
|
'probability' => [
|
|
'type' => 'INT',
|
|
'constraint' => 3,
|
|
'unsigned' => true,
|
|
'default' => 0,
|
|
],
|
|
'created_at' => [
|
|
'type' => 'DATETIME',
|
|
'null' => true,
|
|
],
|
|
'updated_at' => [
|
|
'type' => 'DATETIME',
|
|
'null' => true,
|
|
],
|
|
'deleted_at' => [
|
|
'type' => 'DATETIME',
|
|
'null' => true,
|
|
],
|
|
]);
|
|
$this->forge->addKey('id', true);
|
|
$this->forge->addKey('organization_id');
|
|
$this->forge->addForeignKey('organization_id', 'organizations', 'id', 'CASCADE', 'CASCADE');
|
|
$this->forge->createTable('deal_stages');
|
|
|
|
// Таблица сделок
|
|
$this->forge->addField([
|
|
'id' => [
|
|
'type' => 'BIGINT',
|
|
'constraint' => 20,
|
|
'unsigned' => true,
|
|
'auto_increment' => true,
|
|
],
|
|
'organization_id' => [
|
|
'type' => 'INT',
|
|
'constraint' => 11,
|
|
'unsigned' => true,
|
|
],
|
|
'contact_id' => [
|
|
'type' => 'INT',
|
|
'constraint' => 11,
|
|
'unsigned' => true,
|
|
'null' => true,
|
|
],
|
|
'company_id' => [
|
|
'type' => 'INT',
|
|
'constraint' => 11,
|
|
'unsigned' => true,
|
|
'null' => true,
|
|
],
|
|
'title' => [
|
|
'type' => 'VARCHAR',
|
|
'constraint' => 255,
|
|
],
|
|
'description' => [
|
|
'type' => 'TEXT',
|
|
'null' => true,
|
|
],
|
|
'amount' => [
|
|
'type' => 'DECIMAL',
|
|
'constraint' => '15,2',
|
|
'default' => 0.00,
|
|
],
|
|
'currency' => [
|
|
'type' => 'CHAR',
|
|
'constraint' => 3,
|
|
'default' => 'RUB',
|
|
],
|
|
'stage_id' => [
|
|
'type' => 'INT',
|
|
'constraint' => 11,
|
|
'unsigned' => true,
|
|
],
|
|
'assigned_user_id' => [
|
|
'type' => 'INT',
|
|
'constraint' => 11,
|
|
'unsigned' => true,
|
|
'null' => true,
|
|
],
|
|
'expected_close_date' => [
|
|
'type' => 'DATE',
|
|
'null' => true,
|
|
],
|
|
'created_by' => [
|
|
'type' => 'INT',
|
|
'constraint' => 11,
|
|
'unsigned' => true,
|
|
],
|
|
'created_at' => [
|
|
'type' => 'DATETIME',
|
|
'null' => true,
|
|
],
|
|
'updated_at' => [
|
|
'type' => 'DATETIME',
|
|
'null' => true,
|
|
],
|
|
'deleted_at' => [
|
|
'type' => 'DATETIME',
|
|
'null' => true,
|
|
],
|
|
]);
|
|
$this->forge->addKey('id', true);
|
|
$this->forge->addKey('organization_id');
|
|
$this->forge->addKey('stage_id');
|
|
$this->forge->addKey('assigned_user_id');
|
|
$this->forge->addForeignKey('organization_id', 'organizations', 'id', 'CASCADE', 'CASCADE');
|
|
$this->forge->addForeignKey('stage_id', 'deal_stages', 'id', 'CASCADE', 'CASCADE');
|
|
$this->forge->addForeignKey('assigned_user_id', 'users', 'id', 'SET NULL', 'SET NULL');
|
|
$this->forge->createTable('deals');
|
|
|
|
// Таблица истории сделок
|
|
$this->forge->addField([
|
|
'id' => [
|
|
'type' => 'BIGINT',
|
|
'constraint' => 20,
|
|
'unsigned' => true,
|
|
'auto_increment' => true,
|
|
],
|
|
'deal_id' => [
|
|
'type' => 'BIGINT',
|
|
'constraint' => 20,
|
|
'unsigned' => true,
|
|
],
|
|
'user_id' => [
|
|
'type' => 'INT',
|
|
'constraint' => 11,
|
|
'unsigned' => true,
|
|
],
|
|
'action' => [
|
|
'type' => 'VARCHAR',
|
|
'constraint' => 50,
|
|
],
|
|
'field_name' => [
|
|
'type' => 'VARCHAR',
|
|
'constraint' => 50,
|
|
'null' => true,
|
|
],
|
|
'old_value' => [
|
|
'type' => 'TEXT',
|
|
'null' => true,
|
|
],
|
|
'new_value' => [
|
|
'type' => 'TEXT',
|
|
'null' => true,
|
|
],
|
|
'created_at' => [
|
|
'type' => 'DATETIME',
|
|
'null' => true,
|
|
],
|
|
]);
|
|
$this->forge->addKey('id', true);
|
|
$this->forge->addKey('deal_id');
|
|
$this->forge->addKey('user_id');
|
|
$this->forge->addForeignKey('deal_id', 'deals', 'id', 'CASCADE', 'CASCADE');
|
|
$this->forge->addForeignKey('user_id', 'users', 'id', 'CASCADE', 'CASCADE');
|
|
$this->forge->createTable('deal_history');
|
|
}
|
|
|
|
public function down()
|
|
{
|
|
$this->forge->dropTable('deal_history');
|
|
$this->forge->dropTable('deals');
|
|
$this->forge->dropTable('deal_stages');
|
|
}
|
|
}
|