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

(-)a/installer/data/mysql/kohastructure.sql (-44 / +91 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 2702-2751 CREATE TABLE `messages` ( -- circulation messages left via the patron's check ou Link Here
2702
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2703
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2703
2704
2704
--
2705
--
2705
-- Table structure for table `accountlines`
2706
--
2707
2708
DROP TABLE IF EXISTS `accountlines`;
2709
CREATE TABLE `accountlines` (
2710
  `accountlines_id` int(11) NOT NULL AUTO_INCREMENT,
2711
  `borrowernumber` int(11) NOT NULL default 0,
2712
  `accountno` smallint(6) NOT NULL default 0,
2713
  `itemnumber` int(11) default NULL,
2714
  `date` date default NULL,
2715
  `amount` decimal(28,6) default NULL,
2716
  `description` mediumtext,
2717
  `dispute` mediumtext,
2718
  `accounttype` varchar(5) default NULL,
2719
  `amountoutstanding` decimal(28,6) default NULL,
2720
  `lastincrement` decimal(28,6) default NULL,
2721
  `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
2722
  `notify_id` int(11) NOT NULL default 0,
2723
  `notify_level` int(2) NOT NULL default 0,
2724
  `note` text NULL default NULL,
2725
  `manager_id` int(11) NULL,
2726
  PRIMARY KEY (`accountlines_id`),
2727
  KEY `acctsborridx` (`borrowernumber`),
2728
  KEY `timeidx` (`timestamp`),
2729
  KEY `itemnumber` (`itemnumber`),
2730
  CONSTRAINT `accountlines_ibfk_1` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE ON UPDATE CASCADE,
2731
  CONSTRAINT `accountlines_ibfk_2` FOREIGN KEY (`itemnumber`) REFERENCES `items` (`itemnumber`) ON DELETE SET NULL ON UPDATE SET NULL
2732
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2733
2734
--
2735
-- Table structure for table `accountoffsets`
2736
--
2737
2738
DROP TABLE IF EXISTS `accountoffsets`;
2739
CREATE TABLE `accountoffsets` (
2740
  `borrowernumber` int(11) NOT NULL default 0,
2741
  `accountno` smallint(6) NOT NULL default 0,
2742
  `offsetaccount` smallint(6) NOT NULL default 0,
2743
  `offsetamount` decimal(28,6) default NULL,
2744
  `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
2745
  CONSTRAINT `accountoffsets_ibfk_1` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE ON UPDATE CASCADE
2746
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2747
2748
--
2749
-- Table structure for table `action_logs`
2706
-- Table structure for table `action_logs`
2750
--
2707
--
2751
2708
Lines 3453-3458 CREATE TABLE IF NOT EXISTS columns_settings ( Link Here
3453
    PRIMARY KEY(module, page, tablename, columnname)
3410
    PRIMARY KEY(module, page, tablename, columnname)
3454
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3411
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3455
3412
3413
--
3414
-- Table structure for table 'account_credits'
3415
--
3416
DROP TABLE IF EXISTS account_credits;
3417
CREATE TABLE IF account_credits (
3418
    credit_id int(11) NOT NULL AUTO_INCREMENT,     -- The unique id for this credit
3419
    borrowernumber int(11) NOT NULL,               -- The borrower this credit applies to
3420
    `type` varchar(255) NOT NULL,                  -- The type of credit this is ( defined by Koha::Accounts::CreditTypes )
3421
    amount_received decimal(28,6) DEFAULT NULL,    -- If this was a cash payment, the amount of money given
3422
    amount_paid decimal(28,6) NOT NULL,            -- The actual ammount paid, if less than amount_recieved, change was given back
3423
    amount_remaining decimal(28,6) NOT NULL,       -- The amount of this credit that has not been applied to outstanding debits
3424
    amount_voided decimal(28,6) NULL DEFAULT NULL, -- The amount of this credit was for before it was voided
3425
    notes text,                                    -- Misc notes for this credit
3426
    branchcode VARCHAR( 10 ) NULL DEFAULT NULL,    -- Branchcode where the credit was created ( if any )
3427
    manager_id int(11) DEFAULT NULL,               -- The borrowernumber of the user who created this credit ( if any )
3428
    created_on timestamp NULL DEFAULT NULL,        -- Timestamp for when this credit was created
3429
    updated_on timestamp NULL DEFAULT NULL,        -- Timestamp for when this credit was last modified
3430
    PRIMARY KEY (credit_id),
3431
    KEY borrowernumber (borrowernumber),
3432
    KEY branchcode (branchcode)
3433
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
3434
3435
--
3436
-- Constraints for table `account_credits`
3437
--
3438
ALTER TABLE `account_credits`
3439
  ADD CONSTRAINT account_credits_ibfk_1 FOREIGN KEY (borrowernumber) REFERENCES borrowers (borrowernumber) ON DELETE CASCADE ON UPDATE CASCADE,
3440
  ADD CONSTRAINT account_credits_ibfk_2 FOREIGN KEY (branchcode) REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE;
3441
3442
--
3443
-- Table structure for table 'account_debits'
3444
--
3445
3446
DROP TABLE IF EXISTS account_debits;
3447
CREATE TABLE account_debits (
3448
    debit_id int(11) NOT NULL AUTO_INCREMENT,           -- The unique id for this debit
3449
    borrowernumber int(11) NOT NULL DEFAULT '0',        -- The borrower this debit applies to
3450
    itemnumber int(11) DEFAULT NULL,                    -- The item related to this debit ( for fines, lost fees, etc )
3451
    issue_id int(11) DEFAULT NULL,                      -- The checkout this debit is related to ( again, for fines, lost fees, etc )
3452
    `type` varchar(255) NOT NULL,                       -- The type of debit this is ( defined by Koha::Accounts::DebitTypes )
3453
    accruing tinyint(1) NOT NULL DEFAULT '0',           -- Boolean flag, tells of if this is a fine that is still accruing
3454
    amount_original decimal(28,6) DEFAULT NULL,         -- The total amount of this debit
3455
    amount_outstanding decimal(28,6) DEFAULT NULL,      -- The amount still owed on this debit
3456
    amount_last_increment decimal(28,6) DEFAULT NULL,   -- The amount by which this debit last changed
3457
    description mediumtext,                             -- The description for this debit
3458
    notes text,                                         -- Misc notes for this debit
3459
    branchcode VARCHAR( 10 ) NULL DEFAULT NULL,         -- Branchcode where the debit was created ( if any )
3460
    manager_id int(11) DEFAULT NULL,                    -- The borrowernumber of the user who created this debit ( if any )
3461
    created_on timestamp NULL DEFAULT NULL,             -- Timestamp for when this credit was created
3462
    updated_on timestamp NULL DEFAULT NULL,             -- Timestamp for when this credit was last modified
3463
    PRIMARY KEY (debit_id),
3464
    KEY acctsborridx (borrowernumber),
3465
    KEY itemnumber (itemnumber),
3466
    KEY borrowernumber (borrowernumber),
3467
    KEY issue_id (issue_id),
3468
    KEY branchcode (branchcode)
3469
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
3470
3471
--
3472
-- Constraints for table `account_debits`
3473
--
3474
ALTER TABLE `account_debits`
3475
    ADD CONSTRAINT account_debits_ibfk_1 FOREIGN KEY (borrowernumber) REFERENCES borrowers (borrowernumber) ON DELETE CASCADE ON UPDATE CASCADE,
3476
    ADD CONSTRAINT account_debits_ibfk_2 FOREIGN KEY (branchcode) REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE;
3477
3478
--
3479
-- Table structure for table 'account_offsets'
3480
--
3481
3482
DROP TABLE IF EXISTS account_offsets;
3483
CREATE TABLE account_offsets (
3484
    offset_id int(11) NOT NULL AUTO_INCREMENT,                                              -- Unique id for this offset
3485
    debit_id int(11) DEFAULT NULL,                                                          -- Related debit
3486
    credit_id int(11) DEFAULT NULL,                                                         -- Related credit ( if any )
3487
    `type` varchar(255) DEFAULT NULL,                                                       -- The type of this offset ( defined by Koha::Accounts::OffsetTypes ), if any
3488
    amount decimal(28,6) NOT NULL,                                                          -- The amount of the offset, positive means patron owes more, negative means patron owes less
3489
    created_on timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,    -- Timestamp for when this offset was created
3490
    PRIMARY KEY (offset_id),
3491
    KEY fee_id (debit_id),
3492
    KEY payment_id (credit_id)
3493
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
3494
3495
--
3496
-- Constraints for table `account_offsets`
3497
--
3498
ALTER TABLE `account_offsets`
3499
    ADD CONSTRAINT account_offsets_ibfk_1 FOREIGN KEY (debit_id) REFERENCES account_debits (debit_id) ON DELETE CASCADE ON UPDATE CASCADE,
3500
    ADD CONSTRAINT account_offsets_ibfk_2 FOREIGN KEY (credit_id) REFERENCES account_credits (credit_id) ON DELETE CASCADE ON UPDATE CASCADE;
3501
3502
3456
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
3503
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
3457
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
3504
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
3458
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
3505
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
(-)a/installer/data/mysql/updatedatabase.pl (-1 / +184 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 7257-7262 if ( CheckVersion($DBversion) ) { Link Here
7257
7258
7258
    $dbh->{AutoCommit} = 1;
7259
    $dbh->{AutoCommit} = 1;
7259
    $dbh->{RaiseError} = 0;
7260
    $dbh->{RaiseError} = 0;
7261
   SetVersion ($DBversion);
7260
}
7262
}
7261
7263
7262
$DBversion = "3.13.00.031";
7264
$DBversion = "3.13.00.031";
Lines 8742-8747 if ( CheckVersion($DBversion) ) { Link Here
8742
    SetVersion($DBversion);
8744
    SetVersion($DBversion);
8743
}
8745
}
8744
8746
8747
$DBversion = "3.17.00.XXX";
8748
if ( CheckVersion($DBversion) ) {
8749
    $dbh->do(q{
8750
        ALTER TABLE old_issues ADD issue_id INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST
8751
    });
8752
    $dbh->do(q{
8753
        ALTER TABLE issues ADD issue_id INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
8754
    });
8755
    $dbh->do(q{
8756
        UPDATE issues SET issue_id = issue_id + ( SELECT COUNT(*) FROM old_issues ) ORDER BY issue_id DESC
8757
    });
8758
8759
    $dbh->do("
8760
        CREATE TABLE IF NOT EXISTS account_credits (
8761
            credit_id int(11) NOT NULL AUTO_INCREMENT,
8762
            borrowernumber int(11) NOT NULL,
8763
            `type` varchar(255) NOT NULL,
8764
            amount_received decimal(28,6) DEFAULT NULL,
8765
            amount_paid decimal(28,6) NOT NULL,
8766
            amount_remaining decimal(28,6) NOT NULL,
8767
            amount_voided decimal(28,6) NULL DEFAULT NULL,
8768
            notes text,
8769
            branchcode VARCHAR( 10 ) NULL DEFAULT NULL,
8770
            manager_id int(11) DEFAULT NULL,
8771
            created_on timestamp NULL DEFAULT NULL,
8772
            updated_on timestamp NULL DEFAULT NULL,
8773
            PRIMARY KEY (credit_id),
8774
            KEY borrowernumber (borrowernumber),
8775
            KEY branchcode (branchcode)
8776
        ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
8777
    ");
8778
    $dbh->do("
8779
        CREATE TABLE IF NOT EXISTS account_debits (
8780
            debit_id int(11) NOT NULL AUTO_INCREMENT,
8781
            borrowernumber int(11) NOT NULL DEFAULT '0',
8782
            itemnumber int(11) DEFAULT NULL,
8783
            issue_id int(11) DEFAULT NULL,
8784
            `type` varchar(255) NOT NULL,
8785
            accruing tinyint(1) NOT NULL DEFAULT '0',
8786
            amount_original decimal(28,6) DEFAULT NULL,
8787
            amount_outstanding decimal(28,6) DEFAULT NULL,
8788
            amount_last_increment decimal(28,6) DEFAULT NULL,
8789
            description mediumtext,
8790
            notes text,
8791
            branchcode VARCHAR( 10 ) NULL DEFAULT NULL,
8792
            manager_id int(11) DEFAULT NULL,
8793
            created_on timestamp NULL DEFAULT NULL,
8794
            updated_on timestamp NULL DEFAULT NULL,
8795
            PRIMARY KEY (debit_id),
8796
            KEY acctsborridx (borrowernumber),
8797
            KEY itemnumber (itemnumber),
8798
            KEY borrowernumber (borrowernumber),
8799
            KEY issue_id (issue_id),
8800
            KEY branchcode (branchcode)
8801
        ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
8802
    ");
8803
8804
    $dbh->do("
8805
        CREATE TABLE account_offsets (
8806
            offset_id int(11) NOT NULL AUTO_INCREMENT,
8807
            debit_id int(11) DEFAULT NULL,
8808
            credit_id int(11) DEFAULT NULL,
8809
            `type` varchar(255) DEFAULT NULL,
8810
            amount decimal(28,6) NOT NULL COMMENT 'A positive number here represents a payment, a negative is a increase in a fine.',
8811
            created_on timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
8812
            PRIMARY KEY (offset_id),
8813
            KEY fee_id (debit_id),
8814
            KEY payment_id (credit_id)
8815
        ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
8816
    ");
8817
8818
    $dbh->do("
8819
        ALTER TABLE `account_credits`
8820
          ADD CONSTRAINT account_credits_ibfk_1 FOREIGN KEY (borrowernumber) REFERENCES borrowers (borrowernumber) ON DELETE CASCADE ON UPDATE CASCADE,
8821
          ADD CONSTRAINT account_credits_ibfk_2 FOREIGN KEY (branchcode) REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE;
8822
    ");
8823
    $dbh->do("
8824
        ALTER TABLE `account_debits`
8825
          ADD CONSTRAINT account_debits_ibfk_1 FOREIGN KEY (borrowernumber) REFERENCES borrowers (borrowernumber) ON DELETE CASCADE ON UPDATE CASCADE,
8826
          ADD CONSTRAINT account_debits_ibfk_2 FOREIGN KEY (branchcode) REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE;
8827
    ");
8828
    $dbh->do("
8829
        ALTER TABLE `account_offsets`
8830
          ADD CONSTRAINT account_offsets_ibfk_1 FOREIGN KEY (debit_id) REFERENCES account_debits (debit_id) ON DELETE CASCADE ON UPDATE CASCADE,
8831
          ADD CONSTRAINT account_offsets_ibfk_2 FOREIGN KEY (credit_id) REFERENCES account_credits (credit_id) ON DELETE CASCADE ON UPDATE CASCADE;
8832
    ");
8833
8834
    $dbh->do("
8835
        ALTER TABLE borrowers ADD account_balance DECIMAL( 28, 6 ) NOT NULL;
8836
    ");
8837
8838
    my $schema = Koha::Database->new()->schema;
8839
    my $debit_rs = $schema->resultset('AccountDebit');
8840
    my $credit_rs = $schema->resultset('AccountCredit');
8841
    my $issues_rs = $schema->resultset('Issue');
8842
8843
    use Koha::Accounts::DebitTypes;
8844
    use Koha::Accounts::CreditTypes;
8845
8846
    my $debit_types_map = {
8847
        'A'    => Koha::Accounts::DebitTypes::AccountManagementFee,
8848
        'F'    => Koha::Accounts::DebitTypes::Fine,
8849
        'FU'   => Koha::Accounts::DebitTypes::Fine,
8850
        'L'    => Koha::Accounts::DebitTypes::Lost,
8851
        'M'    => Koha::Accounts::DebitTypes::Sundry,
8852
        'N'    => Koha::Accounts::DebitTypes::NewCard,
8853
        'Rent' => Koha::Accounts::DebitTypes::Rental,
8854
    };
8855
8856
    my $credit_types_map = {
8857
        'FOR' => Koha::Accounts::CreditTypes::Forgiven,
8858
        'LR'  => Koha::Accounts::CreditTypes::Found,
8859
        'Pay' => Koha::Accounts::CreditTypes::Payment,
8860
        'PAY' => Koha::Accounts::CreditTypes::Payment,
8861
        'WO'  => Koha::Accounts::CreditTypes::WriteOff,
8862
        'W'   => Koha::Accounts::CreditTypes::WriteOff,
8863
        'C'   => Koha::Accounts::CreditTypes::Credit,
8864
        'CR'  => Koha::Accounts::CreditTypes::Credit,
8865
    };
8866
8867
    my $sth = $dbh->prepare("SELECT * FROM accountlines");
8868
    $sth->execute();
8869
    while ( my $a = $sth->fetchrow_hashref() ) {
8870
        if ( $debit_types_map->{ $a->{accounttype} } ) {
8871
            $debit_rs->create(
8872
                {
8873
                    borrowernumber     => $a->{borrowernumber},
8874
                    itemnumber         => $a->{itemnumber},
8875
                    amount_original    => $a->{amount},
8876
                    amount_outstanding => $a->{amountoutstanding},
8877
                    created_on         => $a->{timestamp},
8878
                    description        => $a->{description},
8879
                    notes              => $a->{note},
8880
                    manager_id         => $a->{manager_id},
8881
                    accruing           => $a->{accounttype} eq 'FU',
8882
                    type     => $debit_types_map->{ $a->{accounttype} },
8883
                    issue_id => $a->{accounttype} eq 'FU'
8884
                    ? $issues_rs->single(
8885
                        {
8886
                            borrowernumber => $a->{borrowernumber},
8887
                            itemnumber     => $a->{itemnumber},
8888
                        }
8889
                      )->issue_id()
8890
                    : undef,
8891
                }
8892
            );
8893
        }
8894
        elsif ( $credit_types_map->{ $a->{accounttype} } ) {
8895
            $credit_rs->create(
8896
                {
8897
                    borrowernumber   => $a->{borrowernumber},
8898
                    amount_paid      => $a->{amount} * -1,
8899
                    amount_remaining => $a->{amountoutstanding} * -1,
8900
                    created_on       => $a->{timestamp},
8901
                    notes            => $a->{note},
8902
                    manager_id       => $a->{manager_id},
8903
                    type => $credit_types_map->{ $a->{accounttype} },
8904
                }
8905
            );
8906
        }
8907
        else {
8908
            # Everything else must be a MANUAL_INV
8909
            $debit_rs->create(
8910
                {
8911
                    borrowernumber     => $a->{borrowernumber},
8912
                    itemnumber         => $a->{itemnumber},
8913
                    amount_original    => $a->{amount},
8914
                    amount_outstanding => $a->{amountoutstanding},
8915
                    created_on         => $a->{timestamp},
8916
                    description        => $a->{description},
8917
                    notes              => $a->{note},
8918
                    manager_id         => $a->{manager_id},
8919
                    type               => Koha::Accounts::DebitTypes::Sundry,
8920
                }
8921
            );
8922
        }
8923
    }
8924
8925
    print "Upgrade to $DBversion done ( Bug 6427 - Rewrite of the accounts system )\n";
8926
    SetVersion ($DBversion);
8927
}
8928
8745
=head1 FUNCTIONS
8929
=head1 FUNCTIONS
8746
8930
8747
=head2 TableExists($table)
8931
=head2 TableExists($table)
8748
- 

Return to bug 6427