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 2684-2733 CREATE TABLE `messages` ( -- circulation messages left via the patron's check ou Link Here
2684
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2685
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2685
2686
2686
--
2687
--
2687
-- Table structure for table `accountlines`
2688
--
2689
2690
DROP TABLE IF EXISTS `accountlines`;
2691
CREATE TABLE `accountlines` (
2692
  `accountlines_id` int(11) NOT NULL AUTO_INCREMENT,
2693
  `borrowernumber` int(11) NOT NULL default 0,
2694
  `accountno` smallint(6) NOT NULL default 0,
2695
  `itemnumber` int(11) default NULL,
2696
  `date` date default NULL,
2697
  `amount` decimal(28,6) default NULL,
2698
  `description` mediumtext,
2699
  `dispute` mediumtext,
2700
  `accounttype` varchar(5) default NULL,
2701
  `amountoutstanding` decimal(28,6) default NULL,
2702
  `lastincrement` decimal(28,6) default NULL,
2703
  `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
2704
  `notify_id` int(11) NOT NULL default 0,
2705
  `notify_level` int(2) NOT NULL default 0,
2706
  `note` text NULL default NULL,
2707
  `manager_id` int(11) NULL,
2708
  PRIMARY KEY (`accountlines_id`),
2709
  KEY `acctsborridx` (`borrowernumber`),
2710
  KEY `timeidx` (`timestamp`),
2711
  KEY `itemnumber` (`itemnumber`),
2712
  CONSTRAINT `accountlines_ibfk_1` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE ON UPDATE CASCADE,
2713
  CONSTRAINT `accountlines_ibfk_2` FOREIGN KEY (`itemnumber`) REFERENCES `items` (`itemnumber`) ON DELETE SET NULL ON UPDATE SET NULL
2714
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2715
2716
--
2717
-- Table structure for table `accountoffsets`
2718
--
2719
2720
DROP TABLE IF EXISTS `accountoffsets`;
2721
CREATE TABLE `accountoffsets` (
2722
  `borrowernumber` int(11) NOT NULL default 0,
2723
  `accountno` smallint(6) NOT NULL default 0,
2724
  `offsetaccount` smallint(6) NOT NULL default 0,
2725
  `offsetamount` decimal(28,6) default NULL,
2726
  `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
2727
  CONSTRAINT `accountoffsets_ibfk_1` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE ON UPDATE CASCADE
2728
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2729
2730
--
2731
-- Table structure for table `action_logs`
2688
-- Table structure for table `action_logs`
2732
--
2689
--
2733
2690
Lines 3393-3398 CREATE TABLE IF NOT EXISTS marc_modification_template_actions ( Link Here
3393
  CONSTRAINT `mmta_ibfk_1` FOREIGN KEY (`template_id`) REFERENCES `marc_modification_templates` (`template_id`) ON DELETE CASCADE ON UPDATE CASCADE
3350
  CONSTRAINT `mmta_ibfk_1` FOREIGN KEY (`template_id`) REFERENCES `marc_modification_templates` (`template_id`) ON DELETE CASCADE ON UPDATE CASCADE
3394
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
3351
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
3395
3352
3353
--
3354
-- Table structure for table 'account_credits'
3355
--
3356
DROP TABLE IF EXISTS account_credits;
3357
CREATE TABLE IF account_credits (
3358
    credit_id int(11) NOT NULL AUTO_INCREMENT,  -- The unique id for this credit
3359
    borrowernumber int(11) NOT NULL,            -- The borrower this credit applies to
3360
    `type` varchar(255) NOT NULL,               -- The type of credit this is ( defined by Koha::Accounts::CreditTypes )
3361
    amount_received decimal(28,6) DEFAULT NULL, -- If this was a cash payment, the amount of money given
3362
    amount_paid decimal(28,6) NOT NULL,         -- The actual ammount paid, if less than amount_recieved, change was given back
3363
    amount_remaining decimal(28,6) NOT NULL,    -- The amount of this credit that has not been applied to outstanding debits
3364
    notes text,                                 -- Misc notes for this credit
3365
    branchcode VARCHAR( 10 ) NULL DEFAULT NULL, -- Branchcode where the credit was created ( if any )
3366
    manager_id int(11) DEFAULT NULL,            -- The borrowernumber of the user who created this credit ( if any )
3367
    created_on timestamp NULL DEFAULT NULL,     -- Timestamp for when this credit was created
3368
    updated_on timestamp NULL DEFAULT NULL,     -- Timestamp for when this credit was last modified
3369
    PRIMARY KEY (credit_id),
3370
    KEY borrowernumber (borrowernumber),
3371
    KEY branchcode (branchcode)
3372
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
3373
3374
--
3375
-- Constraints for table `account_credits`
3376
--
3377
ALTER TABLE `account_credits`
3378
  ADD CONSTRAINT account_credits_ibfk_1 FOREIGN KEY (borrowernumber) REFERENCES borrowers (borrowernumber) ON DELETE CASCADE ON UPDATE CASCADE,
3379
  ADD CONSTRAINT account_credits_ibfk_2 FOREIGN KEY (branchcode) REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE;
3380
3381
--
3382
-- Table structure for table 'account_debits'
3383
--
3384
3385
DROP TABLE IF EXISTS account_debits;
3386
CREATE TABLE account_debits (
3387
    debit_id int(11) NOT NULL AUTO_INCREMENT,           -- The unique id for this debit
3388
    borrowernumber int(11) NOT NULL DEFAULT '0',        -- The borrower this debit applies to
3389
    itemnumber int(11) DEFAULT NULL,                    -- The item related to this debit ( for fines, lost fees, etc )
3390
    issue_id int(11) DEFAULT NULL,                      -- The checkout this debit is related to ( again, for fines, lost fees, etc )
3391
    `type` varchar(255) NOT NULL,                       -- The type of debit this is ( defined by Koha::Accounts::DebitTypes )
3392
    accruing tinyint(1) NOT NULL DEFAULT '0',           -- Boolean flag, tells of if this is a fine that is still accruing
3393
    amount_original decimal(28,6) DEFAULT NULL,         -- The total amount of this debit
3394
    amount_outstanding decimal(28,6) DEFAULT NULL,      -- The amount still owed on this debit
3395
    amount_last_increment decimal(28,6) DEFAULT NULL,   -- The amount by which this debit last changed
3396
    description mediumtext,                             -- The description for this debit
3397
    notes text,                                         -- Misc notes for this debit
3398
    branchcode VARCHAR( 10 ) NULL DEFAULT NULL,         -- Branchcode where the debit was created ( if any )
3399
    manager_id int(11) DEFAULT NULL,                    -- The borrowernumber of the user who created this debit ( if any )
3400
    created_on timestamp NULL DEFAULT NULL,             -- Timestamp for when this credit was created
3401
    updated_on timestamp NULL DEFAULT NULL,             -- Timestamp for when this credit was last modified
3402
    PRIMARY KEY (debit_id),
3403
    KEY acctsborridx (borrowernumber),
3404
    KEY itemnumber (itemnumber),
3405
    KEY borrowernumber (borrowernumber),
3406
    KEY issue_id (issue_id),
3407
    KEY branchcode (branchcode)
3408
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
3409
3410
--
3411
-- Constraints for table `account_debits`
3412
--
3413
ALTER TABLE `account_debits`
3414
    ADD CONSTRAINT account_debits_ibfk_1 FOREIGN KEY (borrowernumber) REFERENCES borrowers (borrowernumber) ON DELETE CASCADE ON UPDATE CASCADE,
3415
    ADD CONSTRAINT account_debits_ibfk_2 FOREIGN KEY (branchcode) REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE;
3416
3417
--
3418
-- Table structure for table 'account_offsets'
3419
--
3420
3421
DROP TABLE IF EXISTS account_offsets;
3422
CREATE TABLE account_offsets (
3423
    offset_id int(11) NOT NULL AUTO_INCREMENT,                                              -- Unique id for this offset
3424
    debit_id int(11) DEFAULT NULL,                                                          -- Related debit
3425
    credit_id int(11) DEFAULT NULL,                                                         -- Related credit ( if any )
3426
    `type` varchar(255) DEFAULT NULL,                                                       -- The type of this offset ( defined by Koha::Accounts::OffsetTypes ), if any
3427
    amount decimal(28,6) NOT NULL,                                                          -- The amount of the offset, positive means patron owes more, negative means patron owes less
3428
    created_on timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,    -- Timestamp for when this offset was created
3429
    PRIMARY KEY (offset_id),
3430
    KEY fee_id (debit_id),
3431
    KEY payment_id (credit_id)
3432
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
3433
3434
--
3435
-- Constraints for table `account_offsets`
3436
--
3437
ALTER TABLE `account_offsets`
3438
    ADD CONSTRAINT account_offsets_ibfk_1 FOREIGN KEY (debit_id) REFERENCES account_debits (debit_id) ON DELETE CASCADE ON UPDATE CASCADE,
3439
    ADD CONSTRAINT account_offsets_ibfk_2 FOREIGN KEY (credit_id) REFERENCES account_credits (credit_id) ON DELETE CASCADE ON UPDATE CASCADE;
3440
3441
3396
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
3442
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
3397
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
3443
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
3398
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
3444
/*!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 7945-7950 if (CheckVersion($DBversion)) { Link Here
7945
    SetVersion($DBversion);
7947
    SetVersion($DBversion);
7946
}
7948
}
7947
7949
7950
$DBversion = "3.15.00.XXX";
7951
if ( CheckVersion($DBversion) ) {
7952
    $dbh->do(q{
7953
        ALTER TABLE old_issues ADD issue_id INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST
7954
    });
7955
    $dbh->do(q{
7956
        ALTER TABLE issues ADD issue_id INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
7957
    });
7958
    $dbh->do(q{
7959
        UPDATE issues SET issue_id = issue_id + ( SELECT COUNT(*) FROM old_issues ) ORDER BY issue_id DESC
7960
    });
7961
7962
    $dbh->do("
7963
        CREATE TABLE IF NOT EXISTS account_credits (
7964
            credit_id int(11) NOT NULL AUTO_INCREMENT,
7965
            borrowernumber int(11) NOT NULL,
7966
            `type` varchar(255) NOT NULL,
7967
            amount_received decimal(28,6) DEFAULT NULL,
7968
            amount_paid decimal(28,6) NOT NULL,
7969
            amount_remaining decimal(28,6) NOT NULL,
7970
            notes text,
7971
            branchcode VARCHAR( 10 ) NULL DEFAULT NULL,
7972
            manager_id int(11) DEFAULT NULL,
7973
            created_on timestamp NULL DEFAULT NULL,
7974
            updated_on timestamp NULL DEFAULT NULL,
7975
            PRIMARY KEY (credit_id),
7976
            KEY borrowernumber (borrowernumber),
7977
            KEY branchcode (branchcode)
7978
        ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
7979
    ");
7980
    $dbh->do("
7981
        CREATE TABLE IF NOT EXISTS account_debits (
7982
            debit_id int(11) NOT NULL AUTO_INCREMENT,
7983
            borrowernumber int(11) NOT NULL DEFAULT '0',
7984
            itemnumber int(11) DEFAULT NULL,
7985
            issue_id int(11) DEFAULT NULL,
7986
            `type` varchar(255) NOT NULL,
7987
            accruing tinyint(1) NOT NULL DEFAULT '0',
7988
            amount_original decimal(28,6) DEFAULT NULL,
7989
            amount_outstanding decimal(28,6) DEFAULT NULL,
7990
            amount_last_increment decimal(28,6) DEFAULT NULL,
7991
            description mediumtext,
7992
            notes text,
7993
            branchcode VARCHAR( 10 ) NULL DEFAULT NULL,
7994
            manager_id int(11) DEFAULT NULL,
7995
            created_on timestamp NULL DEFAULT NULL,
7996
            updated_on timestamp NULL DEFAULT NULL,
7997
            PRIMARY KEY (debit_id),
7998
            KEY acctsborridx (borrowernumber),
7999
            KEY itemnumber (itemnumber),
8000
            KEY borrowernumber (borrowernumber),
8001
            KEY issue_id (issue_id),
8002
            KEY branchcode (branchcode)
8003
        ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
8004
    ");
8005
8006
    $dbh->do("
8007
        CREATE TABLE account_offsets (
8008
            offset_id int(11) NOT NULL AUTO_INCREMENT,
8009
            debit_id int(11) DEFAULT NULL,
8010
            credit_id int(11) DEFAULT NULL,
8011
            `type` varchar(255) DEFAULT NULL,
8012
            amount decimal(28,6) NOT NULL COMMENT 'A positive number here represents a payment, a negative is a increase in a fine.',
8013
            created_on timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
8014
            PRIMARY KEY (offset_id),
8015
            KEY fee_id (debit_id),
8016
            KEY payment_id (credit_id)
8017
        ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
8018
    ");
8019
8020
    $dbh->do("
8021
        ALTER TABLE `account_credits`
8022
          ADD CONSTRAINT account_credits_ibfk_1 FOREIGN KEY (borrowernumber) REFERENCES borrowers (borrowernumber) ON DELETE CASCADE ON UPDATE CASCADE,
8023
          ADD CONSTRAINT account_credits_ibfk_2 FOREIGN KEY (branchcode) REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE;
8024
    ");
8025
    $dbh->do("
8026
        ALTER TABLE `account_debits`
8027
          ADD CONSTRAINT account_debits_ibfk_1 FOREIGN KEY (borrowernumber) REFERENCES borrowers (borrowernumber) ON DELETE CASCADE ON UPDATE CASCADE,
8028
          ADD CONSTRAINT account_debits_ibfk_2 FOREIGN KEY (branchcode) REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE;
8029
    ");
8030
    $dbh->do("
8031
        ALTER TABLE `account_offsets`
8032
          ADD CONSTRAINT account_offsets_ibfk_1 FOREIGN KEY (debit_id) REFERENCES account_debits (debit_id) ON DELETE CASCADE ON UPDATE CASCADE,
8033
          ADD CONSTRAINT account_offsets_ibfk_2 FOREIGN KEY (credit_id) REFERENCES account_credits (credit_id) ON DELETE CASCADE ON UPDATE CASCADE;
8034
    ");
8035
8036
    $dbh->do("
8037
        ALTER TABLE borrowers ADD account_balance DECIMAL( 28, 6 ) NOT NULL;
8038
    ");
8039
8040
    my $schema = Koha::Database->new()->schema;
8041
    my $debit_rs = $schema->resultset('AccountDebit');
8042
    my $credit_rs = $schema->resultset('AccountCredit');
8043
    my $issues_rs = $schema->resultset('Issue');
8044
8045
    use Koha::Accounts::DebitTypes;
8046
    use Koha::Accounts::CreditTypes;
8047
8048
    my $debit_types_map = {
8049
        'A'    => Koha::Accounts::DebitTypes::AccountManagementFee,
8050
        'F'    => Koha::Accounts::DebitTypes::Fine,
8051
        'FU'   => Koha::Accounts::DebitTypes::Fine,
8052
        'L'    => Koha::Accounts::DebitTypes::Lost,
8053
        'M'    => Koha::Accounts::DebitTypes::Sundry,
8054
        'N'    => Koha::Accounts::DebitTypes::NewCard,
8055
        'Rent' => Koha::Accounts::DebitTypes::Rental,
8056
    };
8057
8058
    my $credit_types_map = {
8059
        'FOR' => Koha::Accounts::CreditTypes::Forgiven,
8060
        'LR'  => Koha::Accounts::CreditTypes::Found,
8061
        'Pay' => Koha::Accounts::CreditTypes::Payment,
8062
        'PAY' => Koha::Accounts::CreditTypes::Payment,
8063
        'WO'  => Koha::Accounts::CreditTypes::WriteOff,
8064
        'W'   => Koha::Accounts::CreditTypes::WriteOff,
8065
        'C'   => Koha::Accounts::CreditTypes::Credit,
8066
        'CR'  => Koha::Accounts::CreditTypes::Credit,
8067
    };
8068
8069
    my $sth = $dbh->prepare("SELECT * FROM accountlines");
8070
    $sth->execute();
8071
    while ( my $a = $sth->fetchrow_hashref() ) {
8072
        if ( $debit_types_map->{ $a->{accounttype} } ) {
8073
            $debit_rs->create(
8074
                {
8075
                    borrowernumber     => $a->{borrowernumber},
8076
                    itemnumber         => $a->{itemnumber},
8077
                    amount_original    => $a->{amount},
8078
                    amount_outstanding => $a->{amountoutstanding},
8079
                    created_on         => $a->{timestamp},
8080
                    description        => $a->{description},
8081
                    notes              => $a->{note},
8082
                    manager_id         => $a->{manager_id},
8083
                    accruing           => $a->{accounttype} eq 'FU',
8084
                    type     => $debit_types_map->{ $a->{accounttype} },
8085
                    issue_id => $a->{accounttype} eq 'FU'
8086
                    ? $issues_rs->single(
8087
                        {
8088
                            borrowernumber => $a->{borrowernumber},
8089
                            itemnumber     => $a->{itemnumber},
8090
                        }
8091
                      )->issue_id()
8092
                    : undef,
8093
                }
8094
            );
8095
        }
8096
        elsif ( $credit_types_map->{ $a->{accounttype} } ) {
8097
            $credit_rs->create(
8098
                {
8099
                    borrowernumber   => $a->{borrowernumber},
8100
                    amount_paid      => $a->{amount} * -1,
8101
                    amount_remaining => $a->{amountoutstanding} * -1,
8102
                    created_on       => $a->{timestamp},
8103
                    notes            => $a->{note},
8104
                    manager_id       => $a->{manager_id},
8105
                    type => $credit_types_map->{ $a->{accounttype} },
8106
                }
8107
            );
8108
        }
8109
        else {
8110
            # Everything else must be a MANUAL_INV
8111
            $debit_rs->create(
8112
                {
8113
                    borrowernumber     => $a->{borrowernumber},
8114
                    itemnumber         => $a->{itemnumber},
8115
                    amount_original    => $a->{amount},
8116
                    amount_outstanding => $a->{amountoutstanding},
8117
                    created_on         => $a->{timestamp},
8118
                    description        => $a->{description},
8119
                    notes              => $a->{note},
8120
                    manager_id         => $a->{manager_id},
8121
                    type               => Koha::Accounts::DebitTypes::Sundry,
8122
                }
8123
            );
8124
        }
8125
    }
8126
8127
    print "Upgrade to $DBversion done ( Bug 6427 - Rewrite of the accounts system )\n";
8128
    SetVersion ($DBversion);
8129
}
8130
7948
=head1 FUNCTIONS
8131
=head1 FUNCTIONS
7949
8132
7950
=head2 TableExists($table)
8133
=head2 TableExists($table)
7951
- 

Return to bug 6427