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 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 3422-3427 CREATE TABLE IF NOT EXISTS `misc_files` ( -- miscellaneous files attached to rec Link Here
3422
  KEY `record_id` (`record_id`)
3379
  KEY `record_id` (`record_id`)
3423
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3380
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3424
3381
3382
--
3383
-- Table structure for table 'account_credits'
3384
--
3385
DROP TABLE IF EXISTS account_credits;
3386
CREATE TABLE IF account_credits (
3387
    credit_id int(11) NOT NULL AUTO_INCREMENT,  -- The unique id for this credit
3388
    borrowernumber int(11) NOT NULL,            -- The borrower this credit applies to
3389
    `type` varchar(255) NOT NULL,               -- The type of credit this is ( defined by Koha::Accounts::CreditTypes )
3390
    amount_received decimal(28,6) DEFAULT NULL, -- If this was a cash payment, the amount of money given
3391
    amount_paid decimal(28,6) NOT NULL,         -- The actual ammount paid, if less than amount_recieved, change was given back
3392
    amount_remaining decimal(28,6) NOT NULL,    -- The amount of this credit that has not been applied to outstanding debits
3393
    notes text,                                 -- Misc notes for this credit
3394
    branchcode VARCHAR( 10 ) NULL DEFAULT NULL, -- Branchcode where the credit was created ( if any )
3395
    manager_id int(11) DEFAULT NULL,            -- The borrowernumber of the user who created this credit ( if any )
3396
    created_on timestamp NULL DEFAULT NULL,     -- Timestamp for when this credit was created
3397
    updated_on timestamp NULL DEFAULT NULL,     -- Timestamp for when this credit was last modified
3398
    PRIMARY KEY (credit_id),
3399
    KEY borrowernumber (borrowernumber),
3400
    KEY branchcode (branchcode)
3401
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
3402
3403
--
3404
-- Constraints for table `account_credits`
3405
--
3406
ALTER TABLE `account_credits`
3407
  ADD CONSTRAINT account_credits_ibfk_1 FOREIGN KEY (borrowernumber) REFERENCES borrowers (borrowernumber) ON DELETE CASCADE ON UPDATE CASCADE,
3408
  ADD CONSTRAINT account_credits_ibfk_2 FOREIGN KEY (branchcode) REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE;
3409
3410
--
3411
-- Table structure for table 'account_debits'
3412
--
3413
3414
DROP TABLE IF EXISTS account_debits;
3415
CREATE TABLE account_debits (
3416
    debit_id int(11) NOT NULL AUTO_INCREMENT,           -- The unique id for this debit
3417
    borrowernumber int(11) NOT NULL DEFAULT '0',        -- The borrower this debit applies to
3418
    itemnumber int(11) DEFAULT NULL,                    -- The item related to this debit ( for fines, lost fees, etc )
3419
    issue_id int(11) DEFAULT NULL,                      -- The checkout this debit is related to ( again, for fines, lost fees, etc )
3420
    `type` varchar(255) NOT NULL,                       -- The type of debit this is ( defined by Koha::Accounts::DebitTypes )
3421
    accruing tinyint(1) NOT NULL DEFAULT '0',           -- Boolean flag, tells of if this is a fine that is still accruing
3422
    amount_original decimal(28,6) DEFAULT NULL,         -- The total amount of this debit
3423
    amount_outstanding decimal(28,6) DEFAULT NULL,      -- The amount still owed on this debit
3424
    amount_last_increment decimal(28,6) DEFAULT NULL,   -- The amount by which this debit last changed
3425
    description mediumtext,                             -- The description for this debit
3426
    notes text,                                         -- Misc notes for this debit
3427
    branchcode VARCHAR( 10 ) NULL DEFAULT NULL,         -- Branchcode where the debit was created ( if any )
3428
    manager_id int(11) DEFAULT NULL,                    -- The borrowernumber of the user who created this debit ( if any )
3429
    created_on timestamp NULL DEFAULT NULL,             -- Timestamp for when this credit was created
3430
    updated_on timestamp NULL DEFAULT NULL,             -- Timestamp for when this credit was last modified
3431
    PRIMARY KEY (debit_id),
3432
    KEY acctsborridx (borrowernumber),
3433
    KEY itemnumber (itemnumber),
3434
    KEY borrowernumber (borrowernumber),
3435
    KEY issue_id (issue_id),
3436
    KEY branchcode (branchcode)
3437
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
3438
3439
--
3440
-- Constraints for table `account_debits`
3441
--
3442
ALTER TABLE `account_debits`
3443
    ADD CONSTRAINT account_debits_ibfk_1 FOREIGN KEY (borrowernumber) REFERENCES borrowers (borrowernumber) ON DELETE CASCADE ON UPDATE CASCADE,
3444
    ADD CONSTRAINT account_debits_ibfk_2 FOREIGN KEY (branchcode) REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE;
3445
3446
--
3447
-- Table structure for table 'account_offsets'
3448
--
3449
3450
DROP TABLE IF EXISTS account_offsets;
3451
CREATE TABLE account_offsets (
3452
    offset_id int(11) NOT NULL AUTO_INCREMENT,                                              -- Unique id for this offset
3453
    debit_id int(11) DEFAULT NULL,                                                          -- Related debit
3454
    credit_id int(11) DEFAULT NULL,                                                         -- Related credit ( if any )
3455
    `type` varchar(255) DEFAULT NULL,                                                       -- The type of this offset ( defined by Koha::Accounts::OffsetTypes ), if any
3456
    amount decimal(28,6) NOT NULL,                                                          -- The amount of the offset, positive means patron owes more, negative means patron owes less
3457
    created_on timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,    -- Timestamp for when this offset was created
3458
    PRIMARY KEY (offset_id),
3459
    KEY fee_id (debit_id),
3460
    KEY payment_id (credit_id)
3461
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
3462
3463
--
3464
-- Constraints for table `account_offsets`
3465
--
3466
ALTER TABLE `account_offsets`
3467
    ADD CONSTRAINT account_offsets_ibfk_1 FOREIGN KEY (debit_id) REFERENCES account_debits (debit_id) ON DELETE CASCADE ON UPDATE CASCADE,
3468
    ADD CONSTRAINT account_offsets_ibfk_2 FOREIGN KEY (credit_id) REFERENCES account_credits (credit_id) ON DELETE CASCADE ON UPDATE CASCADE;
3469
3470
3425
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
3471
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
3426
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
3472
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
3427
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
3473
/*!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 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 8560-8565 if ( CheckVersion($DBversion) ) { Link Here
8560
    SetVersion($DBversion);
8562
    SetVersion($DBversion);
8561
}
8563
}
8562
8564
8565
$DBversion = "3.17.00.XXX";
8566
if ( CheckVersion($DBversion) ) {
8567
    $dbh->do(q{
8568
        ALTER TABLE old_issues ADD issue_id INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST
8569
    });
8570
    $dbh->do(q{
8571
        ALTER TABLE issues ADD issue_id INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
8572
    });
8573
    $dbh->do(q{
8574
        UPDATE issues SET issue_id = issue_id + ( SELECT COUNT(*) FROM old_issues ) ORDER BY issue_id DESC
8575
    });
8576
8577
    $dbh->do("
8578
        CREATE TABLE IF NOT EXISTS account_credits (
8579
            credit_id int(11) NOT NULL AUTO_INCREMENT,
8580
            borrowernumber int(11) NOT NULL,
8581
            `type` varchar(255) NOT NULL,
8582
            amount_received decimal(28,6) DEFAULT NULL,
8583
            amount_paid decimal(28,6) NOT NULL,
8584
            amount_remaining decimal(28,6) NOT NULL,
8585
            notes text,
8586
            branchcode VARCHAR( 10 ) NULL DEFAULT NULL,
8587
            manager_id int(11) DEFAULT NULL,
8588
            created_on timestamp NULL DEFAULT NULL,
8589
            updated_on timestamp NULL DEFAULT NULL,
8590
            PRIMARY KEY (credit_id),
8591
            KEY borrowernumber (borrowernumber),
8592
            KEY branchcode (branchcode)
8593
        ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
8594
    ");
8595
    $dbh->do("
8596
        CREATE TABLE IF NOT EXISTS account_debits (
8597
            debit_id int(11) NOT NULL AUTO_INCREMENT,
8598
            borrowernumber int(11) NOT NULL DEFAULT '0',
8599
            itemnumber int(11) DEFAULT NULL,
8600
            issue_id int(11) DEFAULT NULL,
8601
            `type` varchar(255) NOT NULL,
8602
            accruing tinyint(1) NOT NULL DEFAULT '0',
8603
            amount_original decimal(28,6) DEFAULT NULL,
8604
            amount_outstanding decimal(28,6) DEFAULT NULL,
8605
            amount_last_increment decimal(28,6) DEFAULT NULL,
8606
            description mediumtext,
8607
            notes text,
8608
            branchcode VARCHAR( 10 ) NULL DEFAULT NULL,
8609
            manager_id int(11) DEFAULT NULL,
8610
            created_on timestamp NULL DEFAULT NULL,
8611
            updated_on timestamp NULL DEFAULT NULL,
8612
            PRIMARY KEY (debit_id),
8613
            KEY acctsborridx (borrowernumber),
8614
            KEY itemnumber (itemnumber),
8615
            KEY borrowernumber (borrowernumber),
8616
            KEY issue_id (issue_id),
8617
            KEY branchcode (branchcode)
8618
        ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
8619
    ");
8620
8621
    $dbh->do("
8622
        CREATE TABLE account_offsets (
8623
            offset_id int(11) NOT NULL AUTO_INCREMENT,
8624
            debit_id int(11) DEFAULT NULL,
8625
            credit_id int(11) DEFAULT NULL,
8626
            `type` varchar(255) DEFAULT NULL,
8627
            amount decimal(28,6) NOT NULL COMMENT 'A positive number here represents a payment, a negative is a increase in a fine.',
8628
            created_on timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
8629
            PRIMARY KEY (offset_id),
8630
            KEY fee_id (debit_id),
8631
            KEY payment_id (credit_id)
8632
        ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
8633
    ");
8634
8635
    $dbh->do("
8636
        ALTER TABLE `account_credits`
8637
          ADD CONSTRAINT account_credits_ibfk_1 FOREIGN KEY (borrowernumber) REFERENCES borrowers (borrowernumber) ON DELETE CASCADE ON UPDATE CASCADE,
8638
          ADD CONSTRAINT account_credits_ibfk_2 FOREIGN KEY (branchcode) REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE;
8639
    ");
8640
    $dbh->do("
8641
        ALTER TABLE `account_debits`
8642
          ADD CONSTRAINT account_debits_ibfk_1 FOREIGN KEY (borrowernumber) REFERENCES borrowers (borrowernumber) ON DELETE CASCADE ON UPDATE CASCADE,
8643
          ADD CONSTRAINT account_debits_ibfk_2 FOREIGN KEY (branchcode) REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE;
8644
    ");
8645
    $dbh->do("
8646
        ALTER TABLE `account_offsets`
8647
          ADD CONSTRAINT account_offsets_ibfk_1 FOREIGN KEY (debit_id) REFERENCES account_debits (debit_id) ON DELETE CASCADE ON UPDATE CASCADE,
8648
          ADD CONSTRAINT account_offsets_ibfk_2 FOREIGN KEY (credit_id) REFERENCES account_credits (credit_id) ON DELETE CASCADE ON UPDATE CASCADE;
8649
    ");
8650
8651
    $dbh->do("
8652
        ALTER TABLE borrowers ADD account_balance DECIMAL( 28, 6 ) NOT NULL;
8653
    ");
8654
8655
    my $schema = Koha::Database->new()->schema;
8656
    my $debit_rs = $schema->resultset('AccountDebit');
8657
    my $credit_rs = $schema->resultset('AccountCredit');
8658
    my $issues_rs = $schema->resultset('Issue');
8659
8660
    use Koha::Accounts::DebitTypes;
8661
    use Koha::Accounts::CreditTypes;
8662
8663
    my $debit_types_map = {
8664
        'A'    => Koha::Accounts::DebitTypes::AccountManagementFee,
8665
        'F'    => Koha::Accounts::DebitTypes::Fine,
8666
        'FU'   => Koha::Accounts::DebitTypes::Fine,
8667
        'L'    => Koha::Accounts::DebitTypes::Lost,
8668
        'M'    => Koha::Accounts::DebitTypes::Sundry,
8669
        'N'    => Koha::Accounts::DebitTypes::NewCard,
8670
        'Rent' => Koha::Accounts::DebitTypes::Rental,
8671
    };
8672
8673
    my $credit_types_map = {
8674
        'FOR' => Koha::Accounts::CreditTypes::Forgiven,
8675
        'LR'  => Koha::Accounts::CreditTypes::Found,
8676
        'Pay' => Koha::Accounts::CreditTypes::Payment,
8677
        'PAY' => Koha::Accounts::CreditTypes::Payment,
8678
        'WO'  => Koha::Accounts::CreditTypes::WriteOff,
8679
        'W'   => Koha::Accounts::CreditTypes::WriteOff,
8680
        'C'   => Koha::Accounts::CreditTypes::Credit,
8681
        'CR'  => Koha::Accounts::CreditTypes::Credit,
8682
    };
8683
8684
    my $sth = $dbh->prepare("SELECT * FROM accountlines");
8685
    $sth->execute();
8686
    while ( my $a = $sth->fetchrow_hashref() ) {
8687
        if ( $debit_types_map->{ $a->{accounttype} } ) {
8688
            $debit_rs->create(
8689
                {
8690
                    borrowernumber     => $a->{borrowernumber},
8691
                    itemnumber         => $a->{itemnumber},
8692
                    amount_original    => $a->{amount},
8693
                    amount_outstanding => $a->{amountoutstanding},
8694
                    created_on         => $a->{timestamp},
8695
                    description        => $a->{description},
8696
                    notes              => $a->{note},
8697
                    manager_id         => $a->{manager_id},
8698
                    accruing           => $a->{accounttype} eq 'FU',
8699
                    type     => $debit_types_map->{ $a->{accounttype} },
8700
                    issue_id => $a->{accounttype} eq 'FU'
8701
                    ? $issues_rs->single(
8702
                        {
8703
                            borrowernumber => $a->{borrowernumber},
8704
                            itemnumber     => $a->{itemnumber},
8705
                        }
8706
                      )->issue_id()
8707
                    : undef,
8708
                }
8709
            );
8710
        }
8711
        elsif ( $credit_types_map->{ $a->{accounttype} } ) {
8712
            $credit_rs->create(
8713
                {
8714
                    borrowernumber   => $a->{borrowernumber},
8715
                    amount_paid      => $a->{amount} * -1,
8716
                    amount_remaining => $a->{amountoutstanding} * -1,
8717
                    created_on       => $a->{timestamp},
8718
                    notes            => $a->{note},
8719
                    manager_id       => $a->{manager_id},
8720
                    type => $credit_types_map->{ $a->{accounttype} },
8721
                }
8722
            );
8723
        }
8724
        else {
8725
            # Everything else must be a MANUAL_INV
8726
            $debit_rs->create(
8727
                {
8728
                    borrowernumber     => $a->{borrowernumber},
8729
                    itemnumber         => $a->{itemnumber},
8730
                    amount_original    => $a->{amount},
8731
                    amount_outstanding => $a->{amountoutstanding},
8732
                    created_on         => $a->{timestamp},
8733
                    description        => $a->{description},
8734
                    notes              => $a->{note},
8735
                    manager_id         => $a->{manager_id},
8736
                    type               => Koha::Accounts::DebitTypes::Sundry,
8737
                }
8738
            );
8739
        }
8740
    }
8741
8742
    print "Upgrade to $DBversion done ( Bug 6427 - Rewrite of the accounts system )\n";
8743
    SetVersion ($DBversion);
8744
}
8745
8563
=head1 FUNCTIONS
8746
=head1 FUNCTIONS
8564
8747
8565
=head2 TableExists($table)
8748
=head2 TableExists($table)
8566
- 

Return to bug 6427