migrations/Version20250911195613.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\Migrations\AbstractMigration;
  6. /**
  7.  * Auto-generated Migration: Please modify to your needs!
  8.  */
  9. final class Version20250911195613 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return '';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         $this->addSql('CREATE SEQUENCE tourapp.tpay_transaction_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
  18.         $this->addSql('CREATE TABLE tourapp.tpay_transaction (id INT NOT NULL default nextval(\'tourapp.tpay_transaction_id_seq\'::regclass), payment_id INT NOT NULL, status VARCHAR(50) NOT NULL, title VARCHAR(255), transaction_id VARCHAR(255) NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, amount numeric(10,2) NOT NULL, extorderid VARCHAR(255) DEFAULT NULL, update_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, send_mail_date TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, send_mail BOOLEAN DEFAULT false NOT NULL, PRIMARY KEY(id))');
  19.         $this->addSql('ALTER TABLE tourapp.travel ADD opt_tpay boolean DEFAULT false');
  20.         $this->addSql("INSERT INTO tourapp.payments_type (
  21.            payment_type_id, 
  22.            payment_type_name,
  23.            payment_module,
  24.            payment_type_manual
  25.         ) VALUES
  26.             (5, 'tpay', 'tpay', false);
  27.         ");
  28.         $this->addSql('CREATE SEQUENCE tourapp.tpay_refund_transaction_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
  29.         $this->addSql('CREATE TABLE tourapp.tpay_refund_transaction (id INT NOT NULL default nextval(\'tourapp.tpay_refund_transaction_id_seq\'::regclass), payment_id INT NOT NULL, refund_id VARCHAR(255) DEFAULT NULL, title VARCHAR(255) DEFAULT NULL, status VARCHAR NOT NULL, transaction_id VARCHAR(255) NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, amount INT NOT NULL, update_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
  30.         $this->addSql('
  31.             CREATE OR REPLACE FUNCTION tourapp.func_recalculate_refund(_payment_id integer)
  32.              RETURNS text
  33.              LANGUAGE plpgsql
  34.             AS $function$
  35.             declare
  36.                 _payment record;
  37.                 _payu record;
  38.                 _paynow record;
  39.                 _tpay record;
  40.             BEGIN
  41.             
  42.                 SELECT 
  43.                     payment_id, payment_payback as amount
  44.                     into _payment
  45.                 FROM tourapp.payments
  46.                 where payment_id = _payment_id;
  47.                 
  48.                 SELECT 
  49.                     payment_id, sum(coalesce(amount,0)) as amount
  50.                     into _payu
  51.                 FROM tourapp.payu_refund
  52.                 where payment_id = _payment_id and status = \'FINALIZED\'
  53.                 group by payment_id;
  54.                 
  55.                 SELECT 
  56.                     payment_id, sum(coalesce(amount,0)) as amount
  57.                     into _paynow
  58.                 FROM tourapp.paynow_refund
  59.                 where payment_id = _payment_id and status = \'SUCCESSFUL\'
  60.                 group by payment_id;
  61.             
  62.                 select 
  63.                     payment_id,
  64.                     sum(coalesce(amount,0)) as amount
  65.                     into _tpay
  66.                 from tourapp.tpay_refund_transaction
  67.                 where payment_id = _payment_id and status = \'DONE\'
  68.                 group by payment_id;
  69.                 
  70.                 
  71.                 update tourapp.payments set payment_payback_full = round((coalesce(_payment.amount,0) + coalesce(_tpay.amount,0) + coalesce(_payu.amount,0) + coalesce(_paynow.amount,0) ),2)
  72.                 where payment_id = _payment_id;
  73.                 RETURN \'0001\';
  74.             
  75.             EXCEPTION
  76.                 WHEN OTHERS THEN RETURN SQLSTATE || \'|\' || SQLERRM;
  77.             END;
  78.             
  79.             $function$
  80.             ;
  81.         ');
  82.         // Update all variables in email templates to add type "variable"
  83.         $this->addSql('
  84.             UPDATE tourapp.email_template
  85.             SET variables = (
  86.                 SELECT jsonb_object_agg(
  87.                     key,
  88.                     CASE 
  89.                         WHEN jsonb_exists(value, \'type\') THEN value
  90.                         ELSE jsonb_set(value, \'{type}\', \'"variable"\'::jsonb)
  91.                     END
  92.                 )
  93.                 FROM jsonb_each(COALESCE(variables::jsonb, \'{}\'::jsonb)) AS kv(key, value)
  94.             )
  95.             WHERE variables IS NOT NULL
  96.               AND jsonb_typeof(variables::jsonb) = \'object\'
  97.               AND variables::jsonb != \'{}\'::jsonb;
  98.         ');
  99.         $this->addSql('
  100.             UPDATE tourapp.email_template
  101.             SET variables = COALESCE(variables::jsonb, \'{}\'::jsonb) || 
  102.                 \'{"availablePayments.tpay": {"description": "Warunek: treść wyświetlana gdy Tpay jest włączony", "value": null, "type": "condition"}}\'::jsonb ||
  103.                 \'{"availablePayments.paynow": {"description": "Warunek: treść wyświetlana gdy PayNow jest włączony", "value": null, "type": "condition"}}\'::jsonb ||
  104.                 \'{"availablePayments.payu": {"description": "Warunek: treść wyświetlana gdy PayU jest włączony", "value": null, "type": "condition"}}\'::jsonb ||
  105.                 \'{"availablePayments.bank": {"description": "Warunek: treść wyświetlana gdy przelew bankowy jest dostępny", "value": null, "type": "condition"}}\'::jsonb
  106.             WHERE email_template_id IN (7, 2, 6, 13, 8, 4, 3);
  107.         ');
  108.         $this->addSql('
  109.             UPDATE tourapp.email_template
  110.             SET variables = (
  111.                 SELECT (
  112.                     (SELECT json_object_agg(key, val ORDER BY sort_order, key)
  113.                      FROM (
  114.                          SELECT 
  115.                              key,
  116.                              CASE 
  117.                                  WHEN jsonb_exists(value, \'type\') THEN value
  118.                                  ELSE jsonb_set(value, \'{type}\', \'"variable"\'::jsonb)
  119.                              END AS val,
  120.                              CASE 
  121.                                  WHEN (value->>\'type\') = \'variable\' OR NOT jsonb_exists(value, \'type\') THEN 1
  122.                                  WHEN (value->>\'type\') = \'condition\' THEN 3
  123.                                  ELSE 2
  124.                              END AS sort_order
  125.                          FROM jsonb_each(variables::jsonb)
  126.                      ) s
  127.                     )::json
  128.                 )
  129.             )
  130.             WHERE variables IS NOT NULL
  131.               AND jsonb_typeof(variables::jsonb) = \'object\'
  132.               AND variables::jsonb != \'{}\'::jsonb;
  133.         ');
  134.         $this->addSql('
  135.             UPDATE tourapp.email_template
  136.             SET variables = jsonb_set(
  137.                     variables::jsonb,
  138.                     \'{payulink,description}\',
  139.                     \'"Link do zapisu na wycieczkę"\'::jsonb,
  140.                 false
  141.             )
  142.             WHERE email_template_id = 5
  143.                 AND jsonb_exists(variables::jsonb, \'payulink\');
  144.         ');
  145.         $this->addSql('
  146.             UPDATE tourapp.email_template
  147.             SET variables = jsonb_set(
  148.                     variables::jsonb,
  149.                     \'{payulink,description}\',
  150.                     \'"Link do zapisu na wycieczkę"\'::jsonb,
  151.                 false
  152.             )
  153.             WHERE email_template_id = 9
  154.                 AND jsonb_exists(variables::jsonb, \'payulink\');
  155.         ');
  156.     }
  157.     public function down(Schema $schema): void
  158.     {
  159.     }
  160. }