View | Details | Raw Unified | Return to bug 6427
Collapse All | Expand All

(-)a/installer/data/mysql/kohastructure.sql (-44 / +90 lines)
Lines 265-270 CREATE TABLE `borrowers` ( -- this table includes information about your patrons Link Here
265
  `altcontactphone` varchar(50) default NULL, -- the phone number for the alternate contact for the patron/borrower
265
  `altcontactphone` varchar(50) default NULL, -- the phone number for the alternate contact for the patron/borrower
266
  `smsalertnumber` varchar(50) default NULL, -- the mobile phone number where the patron/borrower would like to receive notices (if SNS turned on)
266
  `smsalertnumber` varchar(50) default NULL, -- the mobile phone number where the patron/borrower would like to receive notices (if SNS turned on)
267
  `privacy` integer(11) DEFAULT '1' NOT NULL, -- patron/borrower's privacy settings related to their reading history
267
  `privacy` integer(11) DEFAULT '1' NOT NULL, -- patron/borrower's privacy settings related to their reading history
268
  `account_balance` decimal(28,6) NOT NULL,
268
  UNIQUE KEY `cardnumber` (`cardnumber`),
269
  UNIQUE KEY `cardnumber` (`cardnumber`),
269
  PRIMARY KEY `borrowernumber` (`borrowernumber`),
270
  PRIMARY KEY `borrowernumber` (`borrowernumber`),
270
  KEY `categorycode` (`categorycode`),
271
  KEY `categorycode` (`categorycode`),
Lines 2677-2726 CREATE TABLE `messages` ( -- circulation messages left via the patron's check ou Link Here
2677
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2678
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2678
2679
2679
--
2680
--
2680
-- Table structure for table `accountlines`
2681
--
2682
2683
DROP TABLE IF EXISTS `accountlines`;
2684
CREATE TABLE `accountlines` (
2685
  `accountlines_id` int(11) NOT NULL AUTO_INCREMENT,
2686
  `borrowernumber` int(11) NOT NULL default 0,
2687
  `accountno` smallint(6) NOT NULL default 0,
2688
  `itemnumber` int(11) default NULL,
2689
  `date` date default NULL,
2690
  `amount` decimal(28,6) default NULL,
2691
  `description` mediumtext,
2692
  `dispute` mediumtext,
2693
  `accounttype` varchar(5) default NULL,
2694
  `amountoutstanding` decimal(28,6) default NULL,
2695
  `lastincrement` decimal(28,6) default NULL,
2696
  `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
2697
  `notify_id` int(11) NOT NULL default 0,
2698
  `notify_level` int(2) NOT NULL default 0,
2699
  `note` text NULL default NULL,
2700
  `manager_id` int(11) NULL,
2701
  PRIMARY KEY (`accountlines_id`),
2702
  KEY `acctsborridx` (`borrowernumber`),
2703
  KEY `timeidx` (`timestamp`),
2704
  KEY `itemnumber` (`itemnumber`),
2705
  CONSTRAINT `accountlines_ibfk_1` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE ON UPDATE CASCADE,
2706
  CONSTRAINT `accountlines_ibfk_2` FOREIGN KEY (`itemnumber`) REFERENCES `items` (`itemnumber`) ON DELETE SET NULL ON UPDATE SET NULL
2707
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2708
2709
--
2710
-- Table structure for table `accountoffsets`
2711
--
2712
2713
DROP TABLE IF EXISTS `accountoffsets`;
2714
CREATE TABLE `accountoffsets` (
2715
  `borrowernumber` int(11) NOT NULL default 0,
2716
  `accountno` smallint(6) NOT NULL default 0,
2717
  `offsetaccount` smallint(6) NOT NULL default 0,
2718
  `offsetamount` decimal(28,6) default NULL,
2719
  `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
2720
  CONSTRAINT `accountoffsets_ibfk_1` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE ON UPDATE CASCADE
2721
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2722
2723
--
2724
-- Table structure for table `action_logs`
2681
-- Table structure for table `action_logs`
2725
--
2682
--
2726
2683
Lines 3378-3383 CREATE TABLE IF NOT EXISTS marc_modification_template_actions ( Link Here
3378
  CONSTRAINT `mmta_ibfk_1` FOREIGN KEY (`template_id`) REFERENCES `marc_modification_templates` (`template_id`) ON DELETE CASCADE ON UPDATE CASCADE
3335
  CONSTRAINT `mmta_ibfk_1` FOREIGN KEY (`template_id`) REFERENCES `marc_modification_templates` (`template_id`) ON DELETE CASCADE ON UPDATE CASCADE
3379
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
3336
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
3380
3337
3338
--
3339
-- Table structure for table 'account_credits'
3340
--
3341
DROP TABLE IF EXISTS account_credits;
3342
CREATE TABLE IF account_credits (
3343
    credit_id int(11) NOT NULL AUTO_INCREMENT,  -- The unique id for this credit
3344
    borrowernumber int(11) NOT NULL,            -- The borrower this credit applies to
3345
    `type` varchar(255) NOT NULL,               -- The type of credit this is ( defined by Koha::Accounts::CreditTypes )
3346
    amount_received decimal(28,6) DEFAULT NULL, -- If this was a cash payment, the amount of money given
3347
    amount_paid decimal(28,6) NOT NULL,         -- The actual ammount paid, if less than amount_recieved, change was given back
3348
    amount_remaining decimal(28,6) NOT NULL,    -- The amount of this credit that has not been applied to outstanding debits
3349
    notes text,                                 -- Misc notes for this credit
3350
    branchcode VARCHAR( 10 ) NULL DEFAULT NULL, -- Branchcode where the credit was created ( if any )
3351
    manager_id int(11) DEFAULT NULL,            -- The borrowernumber of the user who created this credit ( if any )
3352
    created_on timestamp NULL DEFAULT NULL,     -- Timestamp for when this credit was created
3353
    updated_on timestamp NULL DEFAULT NULL,     -- Timestamp for when this credit was last modified
3354
    PRIMARY KEY (credit_id),
3355
    KEY borrowernumber (borrowernumber),
3356
    KEY branchcode (branchcode)
3357
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
3358
3359
--
3360
-- Constraints for table `account_credits`
3361
--
3362
ALTER TABLE `account_credits`
3363
  ADD CONSTRAINT account_credits_ibfk_1 FOREIGN KEY (borrowernumber) REFERENCES borrowers (borrowernumber) ON DELETE CASCADE ON UPDATE CASCADE,
3364
  ADD CONSTRAINT account_credits_ibfk_2 FOREIGN KEY (branchcode) REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE;
3365
3366
--
3367
-- Table structure for table 'account_debits'
3368
--
3369
3370
DROP TABLE IF EXISTS account_debits;
3371
CREATE TABLE account_debits (
3372
    debit_id int(11) NOT NULL AUTO_INCREMENT,           -- The unique id for this debit
3373
    borrowernumber int(11) NOT NULL DEFAULT '0',        -- The borrower this debit applies to
3374
    itemnumber int(11) DEFAULT NULL,                    -- The item related to this debit ( for fines, lost fees, etc )
3375
    issue_id int(11) DEFAULT NULL,                      -- The checkout this debit is related to ( again, for fines, lost fees, etc )
3376
    `type` varchar(255) NOT NULL,                       -- The type of debit this is ( defined by Koha::Accounts::DebitTypes )
3377
    accruing tinyint(1) NOT NULL DEFAULT '0',           -- Boolean flag, tells of if this is a fine that is still accruing
3378
    amount_original decimal(28,6) DEFAULT NULL,         -- The total amount of this debit
3379
    amount_outstanding decimal(28,6) DEFAULT NULL,      -- The amount still owed on this debit
3380
    amount_last_increment decimal(28,6) DEFAULT NULL,   -- The amount by which this debit last changed
3381
    description mediumtext,                             -- The description for this debit
3382
    notes text,                                         -- Misc notes for this debit
3383
    branchcode VARCHAR( 10 ) NULL DEFAULT NULL,         -- Branchcode where the debit was created ( if any )
3384
    manager_id int(11) DEFAULT NULL,                    -- The borrowernumber of the user who created this debit ( if any )
3385
    created_on timestamp NULL DEFAULT NULL,             -- Timestamp for when this credit was created
3386
    updated_on timestamp NULL DEFAULT NULL,             -- Timestamp for when this credit was last modified
3387
    PRIMARY KEY (debit_id),
3388
    KEY acctsborridx (borrowernumber),
3389
    KEY itemnumber (itemnumber),
3390
    KEY borrowernumber (borrowernumber),
3391
    KEY issue_id (issue_id),
3392
    KEY branchcode (branchcode)
3393
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
3394
3395
--
3396
-- Constraints for table `account_debits`
3397
--
3398
ALTER TABLE `account_debits`
3399
    ADD CONSTRAINT account_debits_ibfk_1 FOREIGN KEY (borrowernumber) REFERENCES borrowers (borrowernumber) ON DELETE CASCADE ON UPDATE CASCADE,
3400
    ADD CONSTRAINT account_debits_ibfk_2 FOREIGN KEY (branchcode) REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE;
3401
3402
--
3403
-- Table structure for table 'account_offsets'
3404
--
3405
3406
DROP TABLE IF EXISTS account_offsets;
3407
CREATE TABLE account_offsets (
3408
    offset_id int(11) NOT NULL AUTO_INCREMENT,                                              -- Unique id for this offset
3409
    debit_id int(11) DEFAULT NULL,                                                          -- Related debit
3410
    credit_id int(11) DEFAULT NULL,                                                         -- Related credit ( if any )
3411
    `type` varchar(255) DEFAULT NULL,                                                       -- The type of this offset ( defined by Koha::Accounts::OffsetTypes ), if any
3412
    amount decimal(28,6) NOT NULL,                                                          -- The amount of the offset, positive means patron owes more, negative means patron owes less
3413
    created_on timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,    -- Timestamp for when this offset was created
3414
    PRIMARY KEY (offset_id),
3415
    KEY fee_id (debit_id),
3416
    KEY payment_id (credit_id)
3417
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
3418
3419
--
3420
-- Constraints for table `account_offsets`
3421
--
3422
ALTER TABLE `account_offsets`
3423
    ADD CONSTRAINT account_offsets_ibfk_1 FOREIGN KEY (debit_id) REFERENCES account_debits (debit_id) ON DELETE CASCADE ON UPDATE CASCADE,
3424
    ADD CONSTRAINT account_offsets_ibfk_2 FOREIGN KEY (credit_id) REFERENCES account_credits (credit_id) ON DELETE CASCADE ON UPDATE CASCADE;
3425
3426
3381
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
3427
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
3382
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
3428
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
3383
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
3429
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
(-)a/installer/data/mysql/updatedatabase.pl (-1 / +183 lines)
Lines 37-42 use Getopt::Long; Link Here
37
use C4::Context;
37
use C4::Context;
38
use C4::Installer;
38
use C4::Installer;
39
use C4::Dates;
39
use C4::Dates;
40
use Koha::Database;
40
41
41
use MARC::Record;
42
use MARC::Record;
42
use MARC::File::XML ( BinaryEncoding => 'utf8' );
43
use MARC::File::XML ( BinaryEncoding => 'utf8' );
Lines 7259-7264 if ( CheckVersion($DBversion) ) { Link Here
7259
7260
7260
    $dbh->{AutoCommit} = 1;
7261
    $dbh->{AutoCommit} = 1;
7261
    $dbh->{RaiseError} = 0;
7262
    $dbh->{RaiseError} = 0;
7263
   SetVersion ($DBversion);
7262
}
7264
}
7263
7265
7264
$DBversion = "3.13.00.031";
7266
$DBversion = "3.13.00.031";
Lines 8129-8134 if(CheckVersion($DBversion)) { Link Here
8129
    SetVersion($DBversion);
8131
    SetVersion($DBversion);
8130
}
8132
}
8131
8133
8134
$DBversion = "3.15.00.XXX";
8135
if ( CheckVersion($DBversion) ) {
8136
    $dbh->do(q{
8137
        ALTER TABLE old_issues ADD issue_id INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST
8138
    });
8139
    $dbh->do(q{
8140
        ALTER TABLE issues ADD issue_id INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
8141
    });
8142
    $dbh->do(q{
8143
        UPDATE issues SET issue_id = issue_id + ( SELECT COUNT(*) FROM old_issues ) ORDER BY issue_id DESC
8144
    });
8145
8146
    $dbh->do("
8147
        CREATE TABLE IF NOT EXISTS account_credits (
8148
            credit_id int(11) NOT NULL AUTO_INCREMENT,
8149
            borrowernumber int(11) NOT NULL,
8150
            `type` varchar(255) NOT NULL,
8151
            amount_received decimal(28,6) DEFAULT NULL,
8152
            amount_paid decimal(28,6) NOT NULL,
8153
            amount_remaining decimal(28,6) NOT NULL,
8154
            notes text,
8155
            branchcode VARCHAR( 10 ) NULL DEFAULT NULL,
8156
            manager_id int(11) DEFAULT NULL,
8157
            created_on timestamp NULL DEFAULT NULL,
8158
            updated_on timestamp NULL DEFAULT NULL,
8159
            PRIMARY KEY (credit_id),
8160
            KEY borrowernumber (borrowernumber),
8161
            KEY branchcode (branchcode)
8162
        ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
8163
    ");
8164
    $dbh->do("
8165
        CREATE TABLE IF NOT EXISTS account_debits (
8166
            debit_id int(11) NOT NULL AUTO_INCREMENT,
8167
            borrowernumber int(11) NOT NULL DEFAULT '0',
8168
            itemnumber int(11) DEFAULT NULL,
8169
            issue_id int(11) DEFAULT NULL,
8170
            `type` varchar(255) NOT NULL,
8171
            accruing tinyint(1) NOT NULL DEFAULT '0',
8172
            amount_original decimal(28,6) DEFAULT NULL,
8173
            amount_outstanding decimal(28,6) DEFAULT NULL,
8174
            amount_last_increment decimal(28,6) DEFAULT NULL,
8175
            description mediumtext,
8176
            notes text,
8177
            branchcode VARCHAR( 10 ) NULL DEFAULT NULL,
8178
            manager_id int(11) DEFAULT NULL,
8179
            created_on timestamp NULL DEFAULT NULL,
8180
            updated_on timestamp NULL DEFAULT NULL,
8181
            PRIMARY KEY (debit_id),
8182
            KEY acctsborridx (borrowernumber),
8183
            KEY itemnumber (itemnumber),
8184
            KEY borrowernumber (borrowernumber),
8185
            KEY issue_id (issue_id),
8186
            KEY branchcode (branchcode)
8187
        ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
8188
    ");
8189
8190
    $dbh->do("
8191
        CREATE TABLE account_offsets (
8192
            offset_id int(11) NOT NULL AUTO_INCREMENT,
8193
            debit_id int(11) DEFAULT NULL,
8194
            credit_id int(11) DEFAULT NULL,
8195
            `type` varchar(255) DEFAULT NULL,
8196
            amount decimal(28,6) NOT NULL COMMENT 'A positive number here represents a payment, a negative is a increase in a fine.',
8197
            created_on timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
8198
            PRIMARY KEY (offset_id),
8199
            KEY fee_id (debit_id),
8200
            KEY payment_id (credit_id)
8201
        ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
8202
    ");
8203
8204
    $dbh->do("
8205
        ALTER TABLE `account_credits`
8206
          ADD CONSTRAINT account_credits_ibfk_1 FOREIGN KEY (borrowernumber) REFERENCES borrowers (borrowernumber) ON DELETE CASCADE ON UPDATE CASCADE,
8207
          ADD CONSTRAINT account_credits_ibfk_2 FOREIGN KEY (branchcode) REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE;
8208
    ");
8209
    $dbh->do("
8210
        ALTER TABLE `account_debits`
8211
          ADD CONSTRAINT account_debits_ibfk_1 FOREIGN KEY (borrowernumber) REFERENCES borrowers (borrowernumber) ON DELETE CASCADE ON UPDATE CASCADE,
8212
          ADD CONSTRAINT account_debits_ibfk_2 FOREIGN KEY (branchcode) REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE;
8213
    ");
8214
    $dbh->do("
8215
        ALTER TABLE `account_offsets`
8216
          ADD CONSTRAINT account_offsets_ibfk_1 FOREIGN KEY (debit_id) REFERENCES account_debits (debit_id) ON DELETE CASCADE ON UPDATE CASCADE,
8217
          ADD CONSTRAINT account_offsets_ibfk_2 FOREIGN KEY (credit_id) REFERENCES account_credits (credit_id) ON DELETE CASCADE ON UPDATE CASCADE;
8218
    ");
8219
8220
    $dbh->do("
8221
        ALTER TABLE borrowers ADD account_balance DECIMAL( 28, 6 ) NOT NULL;
8222
    ");
8223
8224
    my $schema = Koha::Database->new()->schema;
8225
    my $debit_rs = $schema->resultset('AccountDebit');
8226
    my $credit_rs = $schema->resultset('AccountCredit');
8227
    my $issues_rs = $schema->resultset('Issue');
8228
8229
    use Koha::Accounts::DebitTypes;
8230
    use Koha::Accounts::CreditTypes;
8231
8232
    my $debit_types_map = {
8233
        'A'    => Koha::Accounts::DebitTypes::AccountManagementFee,
8234
        'F'    => Koha::Accounts::DebitTypes::Fine,
8235
        'FU'   => Koha::Accounts::DebitTypes::Fine,
8236
        'L'    => Koha::Accounts::DebitTypes::Lost,
8237
        'M'    => Koha::Accounts::DebitTypes::Sundry,
8238
        'N'    => Koha::Accounts::DebitTypes::NewCard,
8239
        'Rent' => Koha::Accounts::DebitTypes::Rental,
8240
    };
8241
8242
    my $credit_types_map = {
8243
        'FOR' => Koha::Accounts::CreditTypes::Forgiven,
8244
        'LR'  => Koha::Accounts::CreditTypes::Found,
8245
        'Pay' => Koha::Accounts::CreditTypes::Payment,
8246
        'PAY' => Koha::Accounts::CreditTypes::Payment,
8247
        'WO'  => Koha::Accounts::CreditTypes::WriteOff,
8248
        'W'   => Koha::Accounts::CreditTypes::WriteOff,
8249
        'C'   => Koha::Accounts::CreditTypes::Credit,
8250
        'CR'  => Koha::Accounts::CreditTypes::Credit,
8251
    };
8252
8253
    my $sth = $dbh->prepare("SELECT * FROM accountlines");
8254
    $sth->execute();
8255
    while ( my $a = $sth->fetchrow_hashref() ) {
8256
        if ( $debit_types_map->{ $a->{accounttype} } ) {
8257
            $debit_rs->create(
8258
                {
8259
                    borrowernumber     => $a->{borrowernumber},
8260
                    itemnumber         => $a->{itemnumber},
8261
                    amount_original    => $a->{amount},
8262
                    amount_outstanding => $a->{amountoutstanding},
8263
                    created_on         => $a->{timestamp},
8264
                    description        => $a->{description},
8265
                    notes              => $a->{note},
8266
                    manager_id         => $a->{manager_id},
8267
                    accruing           => $a->{accounttype} eq 'FU',
8268
                    type     => $debit_types_map->{ $a->{accounttype} },
8269
                    issue_id => $a->{accounttype} eq 'FU'
8270
                    ? $issues_rs->single(
8271
                        {
8272
                            borrowernumber => $a->{borrowernumber},
8273
                            itemnumber     => $a->{itemnumber},
8274
                        }
8275
                      )->issue_id()
8276
                    : undef,
8277
                }
8278
            );
8279
        }
8280
        elsif ( $credit_types_map->{ $a->{accounttype} } ) {
8281
            $credit_rs->create(
8282
                {
8283
                    borrowernumber   => $a->{borrowernumber},
8284
                    amount_paid      => $a->{amount} * -1,
8285
                    amount_remaining => $a->{amountoutstanding} * -1,
8286
                    created_on       => $a->{timestamp},
8287
                    notes            => $a->{note},
8288
                    manager_id       => $a->{manager_id},
8289
                    type => $credit_types_map->{ $a->{accounttype} },
8290
                }
8291
            );
8292
        }
8293
        else {
8294
            # Everything else must be a MANUAL_INV
8295
            $debit_rs->create(
8296
                {
8297
                    borrowernumber     => $a->{borrowernumber},
8298
                    itemnumber         => $a->{itemnumber},
8299
                    amount_original    => $a->{amount},
8300
                    amount_outstanding => $a->{amountoutstanding},
8301
                    created_on         => $a->{timestamp},
8302
                    description        => $a->{description},
8303
                    notes              => $a->{note},
8304
                    manager_id         => $a->{manager_id},
8305
                    type               => Koha::Accounts::DebitTypes::Sundry,
8306
                }
8307
            );
8308
        }
8309
    }
8310
8311
    print "Upgrade to $DBversion done ( Bug 6427 - Rewrite of the accounts system )\n";
8312
    SetVersion ($DBversion);
8313
}
8314
8132
=head1 FUNCTIONS
8315
=head1 FUNCTIONS
8133
8316
8134
=head2 TableExists($table)
8317
=head2 TableExists($table)
8135
- 

Return to bug 6427