<?php
declare(strict_types=1);
namespace GocardlessBundleMigrations;
use App\Services\ObjectSerialize;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Symfony\Component\Uid\UuidV6;
final class Version20221207092347 extends AbstractMigration
{
private ObjectSerialize $objectSerialize;
public function setObjectSerialize(ObjectSerialize $objectSerialize)
{
$this->objectSerialize = $objectSerialize;
}
public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE gocardless_bundle__customer_mapping (id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', customer_id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', created_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', updated_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', deleted_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', reference VARCHAR(32) NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, deleted_at DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_7D092DE99395C3F3 (customer_id), INDEX IDX_7D092DE9B03A8386 (created_by_id), INDEX IDX_7D092DE9896DBBDE (updated_by_id), INDEX IDX_7D092DE9C76F1F52 (deleted_by_id), INDEX reference (reference), INDEX deleted_at (deleted_at), INDEX created_at (created_at), INDEX updated_at (updated_at), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE gocardless_bundle__direct_debit (id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', customer_id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', invoice_id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', document_payment_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', created_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', updated_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', deleted_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', mandate_reference VARCHAR(255) NOT NULL, payment_number VARCHAR(255) NOT NULL, state VARCHAR(255) NOT NULL, payment_date DATE NOT NULL, amount NUMERIC(20, 6) NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, deleted_at DATETIME DEFAULT NULL, INDEX IDX_59F696BA9395C3F3 (customer_id), INDEX IDX_59F696BA2989F1FD (invoice_id), UNIQUE INDEX UNIQ_59F696BAA50008ED (document_payment_id), INDEX IDX_59F696BAB03A8386 (created_by_id), INDEX IDX_59F696BA896DBBDE (updated_by_id), INDEX IDX_59F696BAC76F1F52 (deleted_by_id), INDEX mandate_reference (mandate_reference), INDEX payment_number (payment_number), INDEX state (state), INDEX payment_date (payment_date), INDEX amount (amount), INDEX deleted_at (deleted_at), INDEX created_at (created_at), INDEX updated_at (updated_at), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE gocardless_bundle__payout (id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', currency_id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', created_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', updated_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', deleted_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', reference VARCHAR(255) NOT NULL, creditor VARCHAR(255) NOT NULL, date DATE NOT NULL, amount NUMERIC(20, 6) NOT NULL, commission NUMERIC(20, 6) NOT NULL, net_amount NUMERIC(20, 6) NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, deleted_at DATETIME DEFAULT NULL, INDEX IDX_9DEBA9C938248176 (currency_id), INDEX IDX_9DEBA9C9B03A8386 (created_by_id), INDEX IDX_9DEBA9C9896DBBDE (updated_by_id), INDEX IDX_9DEBA9C9C76F1F52 (deleted_by_id), INDEX reference (reference), INDEX creditor (creditor), INDEX date (date), INDEX amount (amount), INDEX commission (commission), INDEX net_amount (net_amount), INDEX deleted_at (deleted_at), INDEX created_at (created_at), INDEX updated_at (updated_at), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE gocardless_bundle__payout_detail (id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', payout_id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', direct_debit_id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', created_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', updated_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', deleted_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', amount VARCHAR(255) NOT NULL, commission VARCHAR(255) NOT NULL, net_amount VARCHAR(255) NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, deleted_at DATETIME DEFAULT NULL, INDEX IDX_A08DCDD9C6D61B7F (payout_id), INDEX IDX_A08DCDD9ED508722 (direct_debit_id), INDEX IDX_A08DCDD9B03A8386 (created_by_id), INDEX IDX_A08DCDD9896DBBDE (updated_by_id), INDEX IDX_A08DCDD9C76F1F52 (deleted_by_id), INDEX amount (amount), INDEX commission (commission), INDEX net_amount (net_amount), INDEX deleted_at (deleted_at), INDEX created_at (created_at), INDEX updated_at (updated_at), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE gocardless_bundle__customer_mapping ADD CONSTRAINT FK_7D092DE99395C3F3 FOREIGN KEY (customer_id) REFERENCES customers_bundle__customer (id)');
$this->addSql('ALTER TABLE gocardless_bundle__customer_mapping ADD CONSTRAINT FK_7D092DE9B03A8386 FOREIGN KEY (created_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE gocardless_bundle__customer_mapping ADD CONSTRAINT FK_7D092DE9896DBBDE FOREIGN KEY (updated_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE gocardless_bundle__customer_mapping ADD CONSTRAINT FK_7D092DE9C76F1F52 FOREIGN KEY (deleted_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE gocardless_bundle__direct_debit ADD CONSTRAINT FK_59F696BA9395C3F3 FOREIGN KEY (customer_id) REFERENCES customers_bundle__customer (id)');
$this->addSql('ALTER TABLE gocardless_bundle__direct_debit ADD CONSTRAINT FK_59F696BA2989F1FD FOREIGN KEY (invoice_id) REFERENCES sales_bundle__invoice (id)');
$this->addSql('ALTER TABLE gocardless_bundle__direct_debit ADD CONSTRAINT FK_59F696BAA50008ED FOREIGN KEY (document_payment_id) REFERENCES sales_bundle__document_payment (id)');
$this->addSql('ALTER TABLE gocardless_bundle__direct_debit ADD CONSTRAINT FK_59F696BAB03A8386 FOREIGN KEY (created_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE gocardless_bundle__direct_debit ADD CONSTRAINT FK_59F696BA896DBBDE FOREIGN KEY (updated_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE gocardless_bundle__direct_debit ADD CONSTRAINT FK_59F696BAC76F1F52 FOREIGN KEY (deleted_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE gocardless_bundle__payout ADD CONSTRAINT FK_9DEBA9C938248176 FOREIGN KEY (currency_id) REFERENCES currency (id)');
$this->addSql('ALTER TABLE gocardless_bundle__payout ADD CONSTRAINT FK_9DEBA9C9B03A8386 FOREIGN KEY (created_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE gocardless_bundle__payout ADD CONSTRAINT FK_9DEBA9C9896DBBDE FOREIGN KEY (updated_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE gocardless_bundle__payout ADD CONSTRAINT FK_9DEBA9C9C76F1F52 FOREIGN KEY (deleted_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE gocardless_bundle__payout_detail ADD CONSTRAINT FK_A08DCDD9C6D61B7F FOREIGN KEY (payout_id) REFERENCES gocardless_bundle__payout (id)');
$this->addSql('ALTER TABLE gocardless_bundle__payout_detail ADD CONSTRAINT FK_A08DCDD9ED508722 FOREIGN KEY (direct_debit_id) REFERENCES gocardless_bundle__direct_debit (id)');
$this->addSql('ALTER TABLE gocardless_bundle__payout_detail ADD CONSTRAINT FK_A08DCDD9B03A8386 FOREIGN KEY (created_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE gocardless_bundle__payout_detail ADD CONSTRAINT FK_A08DCDD9896DBBDE FOREIGN KEY (updated_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE gocardless_bundle__payout_detail ADD CONSTRAINT FK_A08DCDD9C76F1F52 FOREIGN KEY (deleted_by_id) REFERENCES user (id)');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE gocardless_bundle__customer_mapping DROP FOREIGN KEY FK_7D092DE99395C3F3');
$this->addSql('ALTER TABLE gocardless_bundle__customer_mapping DROP FOREIGN KEY FK_7D092DE9B03A8386');
$this->addSql('ALTER TABLE gocardless_bundle__customer_mapping DROP FOREIGN KEY FK_7D092DE9896DBBDE');
$this->addSql('ALTER TABLE gocardless_bundle__customer_mapping DROP FOREIGN KEY FK_7D092DE9C76F1F52');
$this->addSql('ALTER TABLE gocardless_bundle__direct_debit DROP FOREIGN KEY FK_59F696BA9395C3F3');
$this->addSql('ALTER TABLE gocardless_bundle__direct_debit DROP FOREIGN KEY FK_59F696BA2989F1FD');
$this->addSql('ALTER TABLE gocardless_bundle__direct_debit DROP FOREIGN KEY FK_59F696BAA50008ED');
$this->addSql('ALTER TABLE gocardless_bundle__direct_debit DROP FOREIGN KEY FK_59F696BAB03A8386');
$this->addSql('ALTER TABLE gocardless_bundle__direct_debit DROP FOREIGN KEY FK_59F696BA896DBBDE');
$this->addSql('ALTER TABLE gocardless_bundle__direct_debit DROP FOREIGN KEY FK_59F696BAC76F1F52');
$this->addSql('ALTER TABLE gocardless_bundle__payout DROP FOREIGN KEY FK_9DEBA9C938248176');
$this->addSql('ALTER TABLE gocardless_bundle__payout DROP FOREIGN KEY FK_9DEBA9C9B03A8386');
$this->addSql('ALTER TABLE gocardless_bundle__payout DROP FOREIGN KEY FK_9DEBA9C9896DBBDE');
$this->addSql('ALTER TABLE gocardless_bundle__payout DROP FOREIGN KEY FK_9DEBA9C9C76F1F52');
$this->addSql('ALTER TABLE gocardless_bundle__payout_detail DROP FOREIGN KEY FK_A08DCDD9C6D61B7F');
$this->addSql('ALTER TABLE gocardless_bundle__payout_detail DROP FOREIGN KEY FK_A08DCDD9ED508722');
$this->addSql('ALTER TABLE gocardless_bundle__payout_detail DROP FOREIGN KEY FK_A08DCDD9B03A8386');
$this->addSql('ALTER TABLE gocardless_bundle__payout_detail DROP FOREIGN KEY FK_A08DCDD9896DBBDE');
$this->addSql('ALTER TABLE gocardless_bundle__payout_detail DROP FOREIGN KEY FK_A08DCDD9C76F1F52');
$this->addSql('DROP TABLE gocardless_bundle__customer_mapping');
$this->addSql('DROP TABLE gocardless_bundle__direct_debit');
$this->addSql('DROP TABLE gocardless_bundle__payout');
$this->addSql('DROP TABLE gocardless_bundle__payout_detail');
}
public function postUp(Schema $schema): void
{
parent::postUp($schema);
$mail = '
<p>Bonjour,</p>
<p>Veuillez cliquer sur ce [mandate_link]lien[/mandate_link] pour valider votre mandat SEPA.</p>
<br>
<p>Cordialement</p>
';
$now = (new \DateTime())->format('Y-m-d H:i:s');
$configurations = [
'gocardless_bundle__mandate_scheme' => 'sepa_core',
'gocardless_bundle__mandate_verification' => 'recommended',
'gocardless_bundle__mandate_mail_creation' => $mail
];
foreach ($configurations as $configuration => $value) {
$this->connection->insert('configuration', [
'id' => (new UuidV6())->toBinary(),
'name' => $configuration,
'context_id' => null,
'value' => $this->objectSerialize->add((object) ['value' => $value]),
'created_at' => $now,
'updated_at' => $now
]);
}
$languages = $this->connection->executeQuery('SELECT * FROM language WHERE deleted_at IS NULL')
->fetchAllAssociative();
$rootLocale = 'en';
$defaultLocale = $this->connection
->executeQuery('SELECT locale FROM language WHERE deleted_at IS NULL AND is_default = 1')
->fetchOne() ?: 'en';
$paymentMethodNames = [
'en' => 'Direct Debit',
'fr' => 'Prélèvement automatique'
];
$paymentMethodId = new UuidV6();
$this->connection->insert('sales_bundle__payment_method', [
'id' => $paymentMethodId->toBinary(),
'name' => !empty($paymentMethodNames[$defaultLocale]) ?
$paymentMethodNames[$defaultLocale] : $paymentMethodNames[$rootLocale],
'is_invoice' => 0,
'is_credit_note' => 0,
'is_active' => 1,
'created_at' => $now,
'updated_at' => $now
]);
foreach ($languages as $language) {
$localeTo = $rootLocale;
if (!empty($paymentMethodNames[$language['locale']])) {
$localeTo = $language['locale'];
} elseif (!empty($paymentMethodNames[$defaultLocale])) {
$localeTo = $defaultLocale;
}
$this->connection->insert('sales_bundle__payment_method_translations', [
'object_id' => $paymentMethodId->toBinary(),
'locale' => $language['locale'],
'field' => 'name',
'content' => $paymentMethodNames[$localeTo]
]);
}
$this->connection->insert('configuration', [
'id' => (new UuidV6())->toBinary(),
'name' => 'gocardless_bundle__payment_method',
'context_id' => null,
'value' => $this->objectSerialize->add((object) ['value' => $paymentMethodId]),
'created_at' => $now,
'updated_at' => $now
]);
$this->connection->insert('cron_job', [
'name' => 'gocardless-synchronisation',
'command' => 'bluue-bundle:gocardless:sync-gocardless',
'schedule' => '30 10,14 * * *',
'description' => '',
'enabled' => 1
]);
}
}