Lines 862-868
subtest "payout() tests" => sub {
Link Here
|
862 |
|
862 |
|
863 |
subtest "reduce() tests" => sub { |
863 |
subtest "reduce() tests" => sub { |
864 |
|
864 |
|
865 |
plan tests => 27; |
865 |
plan tests => 29; |
866 |
|
866 |
|
867 |
$schema->storage->txn_begin; |
867 |
$schema->storage->txn_begin; |
868 |
|
868 |
|
Lines 922-928
subtest "reduce() tests" => sub {
Link Here
|
922 |
|
922 |
|
923 |
my $reduce_params = { |
923 |
my $reduce_params = { |
924 |
interface => 'commandline', |
924 |
interface => 'commandline', |
925 |
reduction_type => 'REFUND', |
925 |
reduction_type => 'DISCOUNT', |
926 |
amount => 5, |
926 |
amount => 5, |
927 |
staff_id => $staff->borrowernumber, |
927 |
staff_id => $staff->borrowernumber, |
928 |
branch => $branchcode |
928 |
branch => $branchcode |
Lines 970-976
subtest "reduce() tests" => sub {
Link Here
|
970 |
'->reduce() cannot reduce more than original amount'; |
970 |
'->reduce() cannot reduce more than original amount'; |
971 |
|
971 |
|
972 |
# Partial Reduction |
972 |
# Partial Reduction |
973 |
# (Refund 5 on debt of 20) |
973 |
# (Discount 5 on debt of 20) |
974 |
my $reduction = $debit1->reduce($reduce_params); |
974 |
my $reduction = $debit1->reduce($reduce_params); |
975 |
|
975 |
|
976 |
is( ref($reduction), 'Koha::Account::Line', |
976 |
is( ref($reduction), 'Koha::Account::Line', |
Lines 980-985
subtest "reduce() tests" => sub {
Link Here
|
980 |
0, "Reduce amountoutstanding is 0" ); |
980 |
0, "Reduce amountoutstanding is 0" ); |
981 |
is( $debit1->amountoutstanding() * 1, |
981 |
is( $debit1->amountoutstanding() * 1, |
982 |
15, "Debit amountoutstanding reduced by 5 to 15" ); |
982 |
15, "Debit amountoutstanding reduced by 5 to 15" ); |
|
|
983 |
is( $debit1->status(), 'DISCOUNTED', "Debit status updated to DISCOUNTED"); |
983 |
is( $account->balance() * 1, -5, "Account balance is -5" ); |
984 |
is( $account->balance() * 1, -5, "Account balance is -5" ); |
984 |
is( $reduction->status(), 'APPLIED', "Reduction status is 'APPLIED'" ); |
985 |
is( $reduction->status(), 'APPLIED', "Reduction status is 'APPLIED'" ); |
985 |
|
986 |
|
Lines 989-1005
subtest "reduce() tests" => sub {
Link Here
|
989 |
my $THE_offset = $offsets->next; |
990 |
my $THE_offset = $offsets->next; |
990 |
is( $THE_offset->amount * 1, |
991 |
is( $THE_offset->amount * 1, |
991 |
-5, 'Correct amount was applied against debit' ); |
992 |
-5, 'Correct amount was applied against debit' ); |
992 |
is( $THE_offset->type, 'REFUND', "Offset type set to 'REFUND'" ); |
993 |
is( $THE_offset->type, 'DISCOUNT', "Offset type set to 'DISCOUNT'" ); |
993 |
|
994 |
|
994 |
# Zero offset created when zero outstanding |
995 |
# Zero offset created when zero outstanding |
995 |
# (Refund another 5 on paid debt of 20) |
996 |
# (Refund another 5 on paid debt of 20) |
996 |
$credit1->apply( { debits => [$debit1] } ); |
997 |
$credit1->apply( { debits => [$debit1] } ); |
997 |
is( $debit1->amountoutstanding + 0, |
998 |
is( $debit1->amountoutstanding + 0, |
998 |
0, 'Debit1 amountoutstanding reduced to 0' ); |
999 |
0, 'Debit1 amountoutstanding reduced to 0' ); |
|
|
1000 |
$reduce_params->{reduction_type} = 'REFUND'; |
999 |
$reduction = $debit1->reduce($reduce_params); |
1001 |
$reduction = $debit1->reduce($reduce_params); |
1000 |
is( $reduction->amount() * 1, -5, "Reduce amount is -5" ); |
1002 |
is( $reduction->amount() * 1, -5, "Reduce amount is -5" ); |
1001 |
is( $reduction->amountoutstanding() * 1, |
1003 |
is( $reduction->amountoutstanding() * 1, |
1002 |
-5, "Reduce amountoutstanding is -5" ); |
1004 |
-5, "Reduce amountoutstanding is -5" ); |
|
|
1005 |
is( $debit1->status(), 'REFUNDED', "Debit status updated to REFUNDED"); |
1003 |
|
1006 |
|
1004 |
$offsets = Koha::Account::Offsets->search( |
1007 |
$offsets = Koha::Account::Offsets->search( |
1005 |
{ credit_id => $reduction->id, debit_id => $debit1->id } ); |
1008 |
{ credit_id => $reduction->id, debit_id => $debit1->id } ); |
Lines 1038-1065
subtest "reduce() tests" => sub {
Link Here
|
1038 |
}; |
1041 |
}; |
1039 |
|
1042 |
|
1040 |
subtest "cancel() tests" => sub { |
1043 |
subtest "cancel() tests" => sub { |
1041 |
plan tests => 9; |
1044 |
plan tests => 16; |
1042 |
|
1045 |
|
1043 |
$schema->storage->txn_begin; |
1046 |
$schema->storage->txn_begin; |
1044 |
|
1047 |
|
1045 |
# Create a borrower |
1048 |
# Create a borrower |
1046 |
my $categorycode = $builder->build({ source => 'Category' })->{ categorycode }; |
1049 |
my $categorycode = |
1047 |
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; |
1050 |
$builder->build( { source => 'Category' } )->{categorycode}; |
|
|
1051 |
my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode}; |
1048 |
|
1052 |
|
1049 |
my $borrower = Koha::Patron->new( { |
1053 |
my $borrower = Koha::Patron->new( |
1050 |
cardnumber => 'dariahall', |
1054 |
{ |
1051 |
surname => 'Hall', |
1055 |
cardnumber => 'dariahall', |
1052 |
firstname => 'Daria', |
1056 |
surname => 'Hall', |
1053 |
} ); |
1057 |
firstname => 'Daria', |
1054 |
$borrower->categorycode( $categorycode ); |
1058 |
} |
1055 |
$borrower->branchcode( $branchcode ); |
1059 |
); |
|
|
1060 |
$borrower->categorycode($categorycode); |
1061 |
$borrower->branchcode($branchcode); |
1056 |
$borrower->store; |
1062 |
$borrower->store; |
1057 |
|
1063 |
|
1058 |
t::lib::Mocks::mock_userenv({ branchcode => $branchcode, borrowernumber => $borrower->borrowernumber }); |
1064 |
my $staff = Koha::Patron->new( |
|
|
1065 |
{ |
1066 |
cardnumber => 'bobby', |
1067 |
surname => 'Bloggs', |
1068 |
firstname => 'Bobby', |
1069 |
} |
1070 |
); |
1071 |
$staff->categorycode($categorycode); |
1072 |
$staff->branchcode($branchcode); |
1073 |
$staff->store; |
1074 |
|
1075 |
t::lib::Mocks::mock_userenv( |
1076 |
{ |
1077 |
branchcode => $branchcode, |
1078 |
borrowernumber => $borrower->borrowernumber |
1079 |
} |
1080 |
); |
1059 |
|
1081 |
|
1060 |
my $account = Koha::Account->new({ patron_id => $borrower->id }); |
1082 |
my $account = Koha::Account->new( { patron_id => $borrower->id } ); |
1061 |
|
1083 |
|
1062 |
my $line1 = Koha::Account::Line->new( |
1084 |
my $debit1 = Koha::Account::Line->new( |
1063 |
{ |
1085 |
{ |
1064 |
borrowernumber => $borrower->borrowernumber, |
1086 |
borrowernumber => $borrower->borrowernumber, |
1065 |
amount => 10, |
1087 |
amount => 10, |
Lines 1068-1074
subtest "cancel() tests" => sub {
Link Here
|
1068 |
debit_type_code => 'OVERDUE', |
1090 |
debit_type_code => 'OVERDUE', |
1069 |
} |
1091 |
} |
1070 |
)->store(); |
1092 |
)->store(); |
1071 |
my $line2 = Koha::Account::Line->new( |
1093 |
my $debit2 = Koha::Account::Line->new( |
1072 |
{ |
1094 |
{ |
1073 |
borrowernumber => $borrower->borrowernumber, |
1095 |
borrowernumber => $borrower->borrowernumber, |
1074 |
amount => 20, |
1096 |
amount => 20, |
Lines 1078-1104
subtest "cancel() tests" => sub {
Link Here
|
1078 |
} |
1100 |
} |
1079 |
)->store(); |
1101 |
)->store(); |
1080 |
|
1102 |
|
1081 |
my $id = $account->pay({ |
1103 |
my $ret = $account->pay( |
1082 |
lines => [$line2], |
1104 |
{ |
1083 |
amount => 5, |
1105 |
lines => [$debit2], |
1084 |
}); |
1106 |
amount => 5, |
|
|
1107 |
} |
1108 |
); |
1109 |
my $credit = Koha::Account::Lines->find({ accountlines_id => $ret->{payment_id} }); |
1085 |
|
1110 |
|
1086 |
is( $account->balance(), 25, "Account balance is 25" ); |
1111 |
is( $account->balance(), 25, "Account balance is 25" ); |
1087 |
is( $line1->amountoutstanding+0, 10, 'First fee has amount outstanding of 10' ); |
1112 |
is( $debit1->amountoutstanding + 0, |
1088 |
is( $line2->amountoutstanding+0, 15, 'Second fee has amount outstanding of 15' ); |
1113 |
10, 'First fee has amount outstanding of 10' ); |
|
|
1114 |
is( $debit2->amountoutstanding + 0, |
1115 |
15, 'Second fee has amount outstanding of 15' ); |
1116 |
throws_ok { |
1117 |
$credit->cancel( |
1118 |
{ staff_id => $staff->borrowernumber, branch => $branchcode } ); |
1119 |
} |
1120 |
'Koha::Exceptions::Account::IsNotDebit', |
1121 |
'->cancel() can only be used with debits'; |
1089 |
|
1122 |
|
1090 |
my $ret = $line1->cancel(); |
1123 |
throws_ok { |
1091 |
is( ref($ret), 'Koha::Account::Line', 'Cancel returns the account line' ); |
1124 |
$debit1->reduce( { staff_id => $staff->borrowernumber } ); |
1092 |
is( $account->balance(), 15, "Account balance is 15" ); |
1125 |
} |
1093 |
is( $line1->amount+0, 0, 'First fee has amount of 0' ); |
1126 |
'Koha::Exceptions::MissingParameter', |
1094 |
is( $line1->amountoutstanding+0, 0, 'First fee has amount outstanding of 0' ); |
1127 |
"->cancel() requires the `branch` parameter is passed"; |
|
|
1128 |
throws_ok { |
1129 |
$debit1->reduce( { branch => $branchcode } ); |
1130 |
} |
1131 |
'Koha::Exceptions::MissingParameter', |
1132 |
"->cancel() requires the `staff_id` parameter is passed"; |
1095 |
|
1133 |
|
1096 |
$ret = $line2->cancel(); |
1134 |
throws_ok { |
1097 |
is ($ret, undef, 'cancel returns undef when line cannot be cancelled'); |
1135 |
$debit2->cancel( |
|
|
1136 |
{ staff_id => $staff->borrowernumber, branch => $branchcode } ); |
1137 |
} |
1138 |
'Koha::Exceptions::Account', |
1139 |
'->cancel() can only be used with debits that have not been offset'; |
1140 |
|
1141 |
my $cancellation = $debit1->cancel( |
1142 |
{ staff_id => $staff->borrowernumber, branch => $branchcode } ); |
1143 |
is( ref($cancellation), 'Koha::Account::Line', |
1144 |
'Cancel returns an account line' ); |
1145 |
is( |
1146 |
$cancellation->amount() * 1, |
1147 |
$debit1->amount * -1, |
1148 |
"Cancellation amount is " . $debit1->amount |
1149 |
); |
1150 |
is( $cancellation->amountoutstanding() * 1, |
1151 |
0, "Cancellation amountoutstanding is 0" ); |
1152 |
is( $debit1->amountoutstanding() * 1, |
1153 |
0, "Debit amountoutstanding reduced to 0" ); |
1154 |
is( $debit1->status(), 'CANCELLED', "Debit status updated to CANCELLED" ); |
1155 |
is( $account->balance() * 1, 15, "Account balance is 15" ); |
1098 |
|
1156 |
|
1099 |
my $account_payment = Koha::Account::Lines->find($id); |
1157 |
my $offsets = Koha::Account::Offsets->search( |
1100 |
$ret = $account_payment->cancel(); |
1158 |
{ credit_id => $cancellation->id, debit_id => $debit1->id } ); |
1101 |
is ($ret, undef, 'payment cannot be cancelled'); |
1159 |
is( $offsets->count, 1, 'Only one offset is generated' ); |
|
|
1160 |
my $THE_offset = $offsets->next; |
1161 |
is( $THE_offset->amount * 1, |
1162 |
-10, 'Correct amount was applied against debit' ); |
1163 |
is( $THE_offset->type, 'CANCELLATION', |
1164 |
"Offset type set to 'CANCELLATION'" ); |
1102 |
|
1165 |
|
1103 |
$schema->storage->txn_rollback; |
1166 |
$schema->storage->txn_rollback; |
1104 |
}; |
1167 |
}; |
1105 |
- |
|
|