Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 104 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Version010314Date20210828143421 | |
0.00% |
0 / 104 |
|
0.00% |
0 / 4 |
132 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| preSchemaChange | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| changeSchema | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| postSchemaChange | |
0.00% |
0 / 92 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace OCA\Cospend\Migration; |
| 6 | |
| 7 | use Closure; |
| 8 | use OCP\DB\ISchemaWrapper; |
| 9 | use OCP\DB\Types; |
| 10 | use OCP\IL10N; |
| 11 | use OCP\Migration\SimpleMigrationStep; |
| 12 | use OCP\Migration\IOutput; |
| 13 | use OCP\DB\QueryBuilder\IQueryBuilder; |
| 14 | use OCP\IDBConnection; |
| 15 | |
| 16 | /** |
| 17 | * Auto-generated migration step: Please modify to your needs! |
| 18 | */ |
| 19 | class Version010314Date20210828143421 extends SimpleMigrationStep { |
| 20 | |
| 21 | /** @var IDBConnection */ |
| 22 | private $connection; |
| 23 | |
| 24 | /** |
| 25 | * @param IDBConnection $connection |
| 26 | */ |
| 27 | public function __construct(IDBConnection $connection, IL10N $l10n) { |
| 28 | $this->connection = $connection; |
| 29 | $this->trans = $l10n; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @param IOutput $output |
| 34 | * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
| 35 | * @param array $options |
| 36 | */ |
| 37 | public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @param IOutput $output |
| 42 | * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
| 43 | * @param array $options |
| 44 | * @return null|ISchemaWrapper |
| 45 | */ |
| 46 | public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) { |
| 47 | /** @var ISchemaWrapper $schema */ |
| 48 | $schema = $schemaClosure(); |
| 49 | $table = $schema->getTable('cospend_paymentmodes'); |
| 50 | if (!$table->hasColumn('old_id')) { |
| 51 | $table->addColumn('old_id', Types::STRING, [ |
| 52 | 'notnull' => false, |
| 53 | 'length' => 1, |
| 54 | 'default' => null, |
| 55 | ]); |
| 56 | } |
| 57 | return $schema; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param IOutput $output |
| 62 | * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
| 63 | * @param array $options |
| 64 | */ |
| 65 | public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { |
| 66 | $pmNames = [ |
| 67 | $this->trans->t('Credit card'), |
| 68 | $this->trans->t('Cash'), |
| 69 | $this->trans->t('Check'), |
| 70 | $this->trans->t('Transfer'), |
| 71 | $this->trans->t('Online service'), |
| 72 | ]; |
| 73 | $defaultPaymentModes = [ |
| 74 | [ |
| 75 | 'name' => $this->trans->t('Credit card'), |
| 76 | 'icon' => '💳', |
| 77 | 'color' => '#FF7F50', |
| 78 | 'old_id' => 'c', |
| 79 | 'hardcoded_id' => -1, |
| 80 | ], |
| 81 | [ |
| 82 | 'name' => $this->trans->t('Cash'), |
| 83 | 'icon' => '💵', |
| 84 | 'color' => '#556B2F', |
| 85 | 'old_id' => 'b', |
| 86 | 'hardcoded_id' => -2, |
| 87 | ], |
| 88 | [ |
| 89 | 'name' => $this->trans->t('Check'), |
| 90 | 'icon' => '🎫', |
| 91 | 'color' => '#A9A9A9', |
| 92 | 'old_id' => 'f', |
| 93 | 'hardcoded_id' => -3, |
| 94 | ], |
| 95 | [ |
| 96 | 'name' => $this->trans->t('Transfer'), |
| 97 | 'icon' => '⇄', |
| 98 | 'color' => '#00CED1', |
| 99 | 'old_id' => 't', |
| 100 | 'hardcoded_id' => -4, |
| 101 | ], |
| 102 | [ |
| 103 | 'name' => $this->trans->t('Online service'), |
| 104 | 'icon' => '🌎', |
| 105 | 'color' => '#9932CC', |
| 106 | 'old_id' => 'o', |
| 107 | 'hardcoded_id' => -5, |
| 108 | ], |
| 109 | ]; |
| 110 | |
| 111 | $ts = (new \DateTime())->getTimestamp(); |
| 112 | $qb = $this->connection->getQueryBuilder(); |
| 113 | // get project ids |
| 114 | $projectIdList = []; |
| 115 | $qb->select('id') |
| 116 | ->from('cospend_projects'); |
| 117 | $req = $qb->executeQuery(); |
| 118 | |
| 119 | while ($row = $req->fetch()) { |
| 120 | $projectIdList[] = $row['id']; |
| 121 | } |
| 122 | $req->closeCursor(); |
| 123 | $qb = $qb->resetQueryParts(); |
| 124 | |
| 125 | foreach ($projectIdList as $projectId) { |
| 126 | // is there at least one default payment mode already? |
| 127 | $oneDefaultFound = false; |
| 128 | $qb->select('name') |
| 129 | ->from('cospend_paymentmodes') |
| 130 | ->where( |
| 131 | $qb->expr()->eq('projectid', $qb->createNamedParameter($projectId, IQueryBuilder::PARAM_STR)) |
| 132 | ); |
| 133 | $req = $qb->executeQuery(); |
| 134 | |
| 135 | while ($row = $req->fetch()) { |
| 136 | if (in_array($row['name'], $pmNames)) { |
| 137 | $oneDefaultFound = true; |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | $req->closeCursor(); |
| 142 | $qb = $qb->resetQueryParts(); |
| 143 | |
| 144 | // if there is at least one default pm found, do not add default pms |
| 145 | if (!$oneDefaultFound) { |
| 146 | foreach ($defaultPaymentModes as $pm) { |
| 147 | // insert new default pm |
| 148 | $qb->insert('cospend_paymentmodes') |
| 149 | ->values([ |
| 150 | 'projectid' => $qb->createNamedParameter($projectId, IQueryBuilder::PARAM_STR), |
| 151 | 'encoded_icon' => $qb->createNamedParameter(urlencode($pm['icon']), IQueryBuilder::PARAM_STR), |
| 152 | 'color' => $qb->createNamedParameter($pm['color'], IQueryBuilder::PARAM_STR), |
| 153 | 'name' => $qb->createNamedParameter($pm['name'], IQueryBuilder::PARAM_STR), |
| 154 | 'old_id' => $qb->createNamedParameter($pm['old_id'], IQueryBuilder::PARAM_STR), |
| 155 | ]); |
| 156 | $req = $qb->executeStatement(); |
| 157 | $qb = $qb->resetQueryParts(); |
| 158 | $insertedPmId = $qb->getLastInsertId(); |
| 159 | |
| 160 | // convert pm ids in existing bills |
| 161 | $qb->update('cospend_bills') |
| 162 | ->set('paymentmodeid', $qb->createNamedParameter($insertedPmId, IQueryBuilder::PARAM_INT)) |
| 163 | ->set('lastchanged', $qb->createNamedParameter($ts, IQueryBuilder::PARAM_INT)) |
| 164 | ->where( |
| 165 | $qb->expr()->eq('projectid', $qb->createNamedParameter($projectId, IQueryBuilder::PARAM_STR)) |
| 166 | ) |
| 167 | ->andWhere( |
| 168 | $qb->expr()->eq('paymentmodeid', $qb->createNamedParameter($pm['hardcoded_id'], IQueryBuilder::PARAM_INT)) |
| 169 | ); |
| 170 | $qb->executeStatement(); |
| 171 | $qb = $qb->resetQueryParts(); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |