Odyssey
2018121400_cucmsresponseAccount.php
1 <?php
2 /**
3  * Notes:
4  * 1. Anything complicated needs to be done with SQL.
5  * 2. Always have a check to know if the migration needs to occur (up or down).
6  * 3. Use up() and down(), not change(), because of the SQL
7  */
8 use Phinx\Migration\AbstractMigration;
9 
10 class cucmsresponseAccount extends AbstractMigration {
11  public function up() {
12  $table = "cucmsresponse";
13 
14  if ($this->hasTable($table)) {
15 
16  $pkey = "cucmsresponse_pkey";
17  $sql = "alter table $table drop constraint $pkey";
18  $this->query($sql);
19 
20  $sql = "alter table $table add constraint $pkey primary key (docsid, cu, user_id, accountnumber)";
21  $this->query($sql);
22 
23  // Now need to update $table table. For users with one account (most), this will be just updating the accountnumber.
24  // For other users, I will delete and insert n records for all accounts. I have to do this by credit union.
25 
26  // Get the docsids for estatement start and estatement stop.
27  $sql = "select a.docsid as startid, b.docsid as stopid
28  from cucmsdocs a cross join cucmsdocs b
29  where a.docsname = 'esTermsStart' and b.docsname = 'esTermsStop'";
30  $row = $this->fetchRow($sql);
31  $startId = intval($row["startid"]);
32  $stopId = intval($row["stopid"]);
33 
34  // First, update the accountnumber to empty string for all the response that AREN'T estatement start or stop AND aren't already empty string.
35  // There are no existing cases of the accountnumber being null.
36  $sql = "update $table set accountnumber = '' where docsid not in ($startId, $stopId) and accountnumber <> ''";
37  $this->query($sql);
38 
39  // Then find all the CUs where <cucode>user, <cucode>useraccounts, and <cucode>memberacct tables exist.
40  $sql = "select upper(a.cu) as cu
41  from (select substring(table_name from 0 for (char_length(table_name) - 9)) as cu from information_schema.tables
42  where table_type = 'BASE TABLE' and table_schema = 'public' and table_name like '%memberacct') a
43  inner join information_schema.tables b on a.cu || 'user' = b.table_name
44  inner join information_schema.tables c on a.cu || 'useraccounts' = c.table_name
45  order by a.cu";
46  $rows = $this->fetchAll($sql);
47 
48  $regex = "select cr.docsid, '{{CU}}'::text as cu, u.user_id, ma1.accountnumber as account1, ma2.accountnumber as account2,
49  row_number() over (partition by cr.docsid, u.user_id, ma1.accountnumber) as rown
50  from {{CU}}useraccounts ua1
51  inner join $table cr on ua1.user_id = cr.user_id and cr.cu = '{{CU}}'::text and cr.docsid in ($startId, $stopId)
52  left join {{CU}}useraccounts ua2 on ua1.user_id = ua2.user_id and ua1.accountnumber <> ua2.accountnumber
53  inner join {{CU}}memberacct ma1 on ua1.accountnumber = ma1.accountnumber
54  left join {{CU}}memberacct ma2 on ua2.accountnumber = ma2.accountnumber
55  inner join {{CU}}user u on ua1.user_id = u.user_id
56  order by cr.docsid, u.user_id, ma1.accountnumber";
57 
58  foreach($rows as $cuRow) {
59  // replace {{CU}} with the CU.
60  $cu = trim($cuRow["cu"]);
61  $cuRegex = str_replace("{{CU}}", $cu, $regex);
62 
63  // Simple case: one accountnumber per user. Just update the accountnumber.
64  // The accountnumber will be updated to one accountnumber for a user with multiple accounts. (Corrected in next step.)
65  $sql = "update $table a set accountnumber = b.accountnumber
66  from (
67  select t.docsid, t.cu, t.user_id, t.account1 as accountnumber from ($cuRegex) t where t.rown = 1
68  ) b where a.docsid = b.docsid and a.cu = b.cu and a.user_id = b.user_id";
69  $this->query($sql);
70 
71  // Complex case: multiple accountnumbers per user. Insert into new records for accounts that the account isn't updated to in the previous step.
72  // Need to get from the table again because I need the same responseon time.
73  $sql = "insert into $table (docsid, cu, accountnumber, responseon, user_id)
74  select t.docsid, t.cu, t.account1, r.responseon, t.user_id from ($cuRegex) t
75  inner join $table r on t.docsid = r.docsid and t.cu = r.cu and t.user_id = r.user_id
76  and t.account1 <> r.accountnumber and t.rown = 1 and t.account2 is not null";
77  $this->query($sql);
78  }
79  }
80  } // End "up" function.
81 
82  public function down() {
83 
84  } // End "down" function
85 }