@@ -, +, @@ --- C4/Accounts.pm | 51 ++++++-- admin/itemtypes.pl | 28 ++-- ...-replacement_cost_processing_fee_management.sql | 2 + installer/data/mysql/sysprefs.sql | 2 + koha-tmpl/intranet-tmpl/prog/css/staff-global.css | 3 + .../prog/en/modules/admin/itemtypes.tt | 14 +- .../en/modules/admin/preferences/circulation.pref | 11 ++ t/db_dependent/Accounts.t | 143 ++++++++++++++++++++- t/db_dependent/Circulation.t | 3 +- t/db_dependent/Circulation/Chargelostitem.t | 84 ++++++++++++ 10 files changed, 314 insertions(+), 27 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_12768-replacement_cost_processing_fee_management.sql create mode 100644 t/db_dependent/Circulation/Chargelostitem.t --- a/C4/Accounts.pm +++ a/C4/Accounts.pm @@ -28,6 +28,7 @@ use C4::Log qw(logaction); use Koha::Account; use Koha::Account::Lines; use Koha::Account::Offsets; +use Koha::Items; use Data::Dumper qw(Dumper); @@ -134,7 +135,15 @@ FIXME : if no replacement price, borrower just doesn't get charged? sub chargelostitem{ my $dbh = C4::Context->dbh(); my ($borrowernumber, $itemnumber, $amount, $description) = @_; - + my $itype = Koha::ItemTypes->find({ itemtype => Koha::Items->find($itemnumber)->effective_itemtype() }); + my $replacementprice = $amount; + my $defaultreplacecost = $itype->defaultreplacecost; + my $processfee = $itype->processfee; + my $usedefaultreplacementcost = C4::Context->preference("useDefaultReplacementCost"); + my $processingfeenote = C4::Context->preference("ProcessingFeeNote"); + if ($usedefaultreplacementcost && $amount == 0 && $defaultreplacecost){ + $replacementprice = $defaultreplacecost; + } # first make sure the borrower hasn't already been charged for this item my $existing_charges = Koha::Account::Lines->search( { @@ -189,6 +198,14 @@ sub chargelostitem{ })); } + #add processing fee + if ($processfee && $processfee > 0){ + manualinvoice($borrowernumber, $itemnumber, $description, 'PF', $processfee, $processingfeenote, 1); + } + #add replace cost + if ($replacementprice > 0){ + manualinvoice($borrowernumber, $itemnumber, $description, 'L', $replacementprice, undef, 1); + } } } @@ -219,7 +236,7 @@ should be the empty string. # sub manualinvoice { - my ( $borrowernumber, $itemnum, $desc, $type, $amount, $note ) = @_; + my ( $borrowernumber, $itemnum, $desc, $type, $amount, $note, $skip_notify ) = @_; my $manager_id = 0; $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv; my $dbh = C4::Context->dbh; @@ -227,6 +244,7 @@ sub manualinvoice { my $insert; my $accountno = getnextacctno($borrowernumber); my $amountleft = $amount; + $skip_notify //= 0; if ( ( $type eq 'L' ) or ( $type eq 'F' ) @@ -234,7 +252,7 @@ sub manualinvoice { or ( $type eq 'N' ) or ( $type eq 'M' ) ) { - $notifyid = 1; + $notifyid = 1 unless $skip_notify; } my $accountline = Koha::Account::Line->new( @@ -281,19 +299,26 @@ sub manualinvoice { } sub getcharges { - my ( $borrowerno, $timestamp, $accountno ) = @_; - my $dbh = C4::Context->dbh; - my $timestamp2 = $timestamp - 1; - my $query = ""; - my $sth = $dbh->prepare( - "SELECT * FROM accountlines WHERE borrowernumber=? AND accountno = ?" - ); - $sth->execute( $borrowerno, $accountno ); + my ( $borrowerno, $accountno ) = @_; + my $dbh = C4::Context->dbh; + + my @params; + + my $query = "SELECT * FROM accountlines WHERE borrowernumber = ?"; + push( @params, $borrowerno ); + + if ( $accountno ) { + $query .= " AND accountno = ?"; + push( @params, $accountno ); + } + + my $sth = $dbh->prepare( $query ); + $sth->execute( @params ); my @results; while ( my $data = $sth->fetchrow_hashref ) { - push @results,$data; - } + push @results,$data; + } return (@results); } --- a/admin/itemtypes.pl +++ a/admin/itemtypes.pl @@ -72,6 +72,8 @@ if ( $op eq 'add_form' ) { my $itemtype = Koha::ItemTypes->find($itemtype_code); my $description = $input->param('description'); my $rentalcharge = $input->param('rentalcharge'); + my $defaultreplacecost = $input->param('defaultreplacecost'); + my $processfee = $input->param('processfee'); my $image = $input->param('image') || q||; my $notforloan = $input->param('notforloan') ? 1 : 0; @@ -90,6 +92,8 @@ if ( $op eq 'add_form' ) { if ( $itemtype and $is_a_modif ) { # it's a modification $itemtype->description($description); $itemtype->rentalcharge($rentalcharge); + $itemtype->defaultreplacecost($defaultreplacecost); + $itemtype->processfee($processfee); $itemtype->notforloan($notforloan); $itemtype->imageurl($imageurl); $itemtype->summary($summary); @@ -108,17 +112,19 @@ if ( $op eq 'add_form' ) { } } elsif ( not $itemtype and not $is_a_modif ) { my $itemtype = Koha::ItemType->new( - { itemtype => $itemtype_code, - description => $description, - rentalcharge => $rentalcharge, - notforloan => $notforloan, - imageurl => $imageurl, - summary => $summary, - checkinmsg => $checkinmsg, - checkinmsgtype => $checkinmsgtype, - sip_media_type => $sip_media_type, - hideinopac => $hideinopac, - searchcategory => $searchcategory, + { itemtype => $itemtype_code, + description => $description, + rentalcharge => $rentalcharge, + defaultreplacecost => $defaultreplacecost, + processfee => $processfee, + notforloan => $notforloan, + imageurl => $imageurl, + summary => $summary, + checkinmsg => $checkinmsg, + checkinmsgtype => $checkinmsgtype, + sip_media_type => $sip_media_type, + hideinopac => $hideinopac, + searchcategory => $searchcategory, } ); eval { $itemtype->store; }; --- a/installer/data/mysql/atomicupdate/bug_12768-replacement_cost_processing_fee_management.sql +++ a/installer/data/mysql/atomicupdate/bug_12768-replacement_cost_processing_fee_management.sql @@ -0,0 +1,2 @@ +INSERT INTO systempreferences (variable,value,explanation,type) VALUES ('useDefaultReplacementCost',0,'default replacement cost defined in item type','YesNo'); +INSERT INTO systempreferences (variable,value,explanation,type) VALUES ('ProcessingFeeNote','','Set the text to be recorded in the column note, table accountlines when the processing fee (defined in item type) is applied','textarea'); --- a/installer/data/mysql/sysprefs.sql +++ a/installer/data/mysql/sysprefs.sql @@ -433,6 +433,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('previousIssuesDefaultSortOrder','asc','asc|desc','Specify the sort order of Previous Issues on the circulation page','Choice'), ('printcirculationslips','1','','If ON, enable printing circulation receipts','YesNo'), ('PrintNoticesMaxLines','0','','If greater than 0, sets the maximum number of lines an overdue notice will print. If the number of items is greater than this number, the notice will end with a warning asking the borrower to check their online account for a full list of overdue items.','Integer'), +('ProcessingFeeNote', '', NULL, 'Set the text to be recorded in the column note, table accountlines when the processing fee (defined in item type) is applied', 'textarea'), ('QueryAutoTruncate','1',NULL,'If ON, query truncation is enabled by default','YesNo'), ('QueryFuzzy','1',NULL,'If ON, enables fuzzy option for searches','YesNo'), ('QueryStemming','1',NULL,'If ON, enables query stemming','YesNo'), @@ -572,6 +573,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('UseControlNumber','0','','If ON, record control number (w subfields) and control number (001) are used for linking of bibliographic records.','YesNo'), ('UseCourseReserves','0',NULL,'Enable the course reserves feature.','YesNo'), ('useDaysMode','Calendar','Calendar|Days|Datedue','Choose the method for calculating due date: select Calendar to use the holidays module, and Days to ignore the holidays module','Choice'), +('useDefaultReplacementCost', '0', NULL, 'default replacement cost defined in item type', 'YesNo'), ('useDischarge','','','Allows librarians to discharge borrowers and borrowers to request a discharge','YesNo'), ('UseICU','0','1','Tell Koha if ICU indexing is in use for Zebra or not.','YesNo'), ('UseKohaPlugins','0','','Enable or disable the ability to use Koha Plugins.','YesNo'), --- a/koha-tmpl/intranet-tmpl/prog/css/staff-global.css +++ a/koha-tmpl/intranet-tmpl/prog/css/staff-global.css @@ -687,6 +687,9 @@ fieldset.rows fieldset { .yui-b fieldset.rows td label, .yui-b fieldset.rows td span.label { width: auto; } +.yui-b fieldset.rows ol.oladditemtype label, .yui-b fieldset.rows ol.oladditemtype span.label { + width: 13em; +} .yui-b fieldset.rows div.hint { margin-left : 10.5em; --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt @@ -235,7 +235,7 @@ Item types administration [% END %] -
    +
    1. [% IF ( itemtype.hideinopac ) %] @@ -259,6 +259,14 @@ Item types administration
    2. + + +
    3. +
    4. + + +
    5. +
    6. @@ -348,6 +356,8 @@ Item types administration Not for loan Hide in OPAC Charge + Default replacement cost + Processing fee (when lost) Checkin message Actions @@ -387,6 +397,8 @@ Item types administration [% itemtype.rentalcharge | $Price %] [% END %] + [% itemtype.defaultreplacecost | $Price %] + [% itemtype.processfee | $Price %] [% itemtype.checkinmsg | html | html_line_break %] Edit --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref @@ -740,6 +740,17 @@ Circulation: any_time_is_placed: "any time a hold is placed." not_always: "only if all items are checked out and the record has at least one hold already." any_time_is_collected: "any time a hold is collected." + - pref: useDefaultReplacementCost + choices: + yes: use + no: "Don't use" + - the default replacement cost defined in item type. + - + - "Set the text to be recorded in the column 'note', table 'accountlines' when the processing fee (defined in item type) is applied." + - pref: ProcessingFeeNote + type: textarea + class: code + Self Checkout: - - "Include the following JavaScript on all pages in the web-based self checkout:" --- a/t/db_dependent/Accounts.t +++ a/t/db_dependent/Accounts.t @@ -18,11 +18,12 @@ use Modern::Perl; -use Test::More tests => 22; +use Test::More tests => 23; use Test::MockModule; use Test::Warn; use t::lib::TestBuilder; +use t::lib::Mocks; use Koha::Account; use Koha::Account::Lines; @@ -487,3 +488,143 @@ subtest 'balance' => sub { $patron->delete; }; +subtest "Koha::Account::chargelostitem tests" => sub { + plan tests => 32; + + my $lostfine; + my $procfee; + + my $itype_no_replace_no_fee = $builder->build({ source => 'Itemtype', value => { + rentalcharge => 0, + defaultreplacecost => undef, + processfee => undef, + }}); + my $itype_replace_no_fee = $builder->build({ source => 'Itemtype', value => { + rentalcharge => 0, + defaultreplacecost => 16.32, + processfee => undef, + }}); + my $itype_no_replace_fee = $builder->build({ source => 'Itemtype', value => { + rentalcharge => 0, + defaultreplacecost => undef, + processfee => 8.16, + }}); + my $itype_replace_fee = $builder->build({ source => 'Itemtype', value => { + rentalcharge => 0, + defaultreplacecost => 4.08, + processfee => 2.04, + }}); + my $cli_borrowernumber = $builder->build({ source => 'Borrower' })->{'borrowernumber'}; + my $cli_itemnumber1 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_no_fee->{itemtype} } })->{'itemnumber'}; + my $cli_itemnumber2 = $builder->build({ source => 'Item', value => { itype => $itype_replace_no_fee->{itemtype} } })->{'itemnumber'}; + my $cli_itemnumber3 = $builder->build({ source => 'Item', value => { itype => $itype_no_replace_fee->{itemtype} } })->{'itemnumber'}; + my $cli_itemnumber4 = $builder->build({ source => 'Item', value => { itype => $itype_replace_fee->{itemtype} } })->{'itemnumber'}; + my $duck = Koha::Items->find({itemnumber=>$cli_itemnumber1}); + + t::lib::Mocks::mock_preference('item-level_itypes', '1'); + t::lib::Mocks::mock_preference('useDefaultReplacementCost', '0'); + + C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor"); + $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'L' }); + $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' }); + ok( !$lostfine, "No lost fine if no replacementcost or default when pref off"); + ok( !$procfee, "No processing fee if no processing fee"); + C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor"); + $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'L' }); + $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' }); + ok( $lostfine->amount == 6.12, "Lost fine equals replacementcost when pref off and no default set"); + ok( !$procfee, "No processing fee if no processing fee"); + $lostfine->delete(); + + C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor"); + $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'L' }); + $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' }); + ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off"); + ok( !$procfee, "No processing fee if no processing fee"); + C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor"); + $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'L' }); + $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' }); + ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set"); + ok( !$procfee, "No processing fee if no processing fee"); + $lostfine->delete(); + + C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor"); + $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'L' }); + $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' }); + ok( !$lostfine, "No lost fine if no replacementcost and no default set when pref off"); + ok( $procfee->amount == 8.16, "Processing fee if processing fee"); + $procfee->delete(); + C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor"); + $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'L' }); + $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' }); + ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and no default set"); + ok( $procfee->amount == 8.16, "Processing fee if processing fee"); + $lostfine->delete(); + $procfee->delete(); + + C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor"); + $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' }); + $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' }); + ok( !$lostfine, "No lost fine if no replacementcost but default set when pref off"); + ok( $procfee->amount == 2.04, "Processing fee if processing fee"); + $procfee->delete(); + C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor"); + $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' }); + $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' }); + ok( $lostfine->amount == 6.12 , "Lost fine equals replacementcost when pref off and default set"); + ok( $procfee->amount == 2.04, "Processing fee if processing fee"); + $lostfine->delete(); + $procfee->delete(); + + t::lib::Mocks::mock_preference('useDefaultReplacementCost', '1'); + + C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 0, "Perdedor"); + $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'L' }); + $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' }); + ok( !$lostfine, "No lost fine if no replacementcost or default when pref on"); + ok( !$procfee, "No processing fee if no processing fee"); + C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber1, 6.12, "Perdedor"); + $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'L' }); + $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber1, accounttype => 'PF' }); + is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set"); + ok( !$procfee, "No processing fee if no processing fee"); + + C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 0, "Perdedor"); + $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'L' }); + $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' }); + is( $lostfine->amount(), "16.320000", "Lost fine is default if no replacementcost but default set when pref on"); + ok( !$procfee, "No processing fee if no processing fee"); + $lostfine->delete(); + C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber2, 6.12, "Perdedor"); + $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'L' }); + $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber2, accounttype => 'PF' }); + is( $lostfine->amount, "6.120000" , "Lost fine equals replacementcost when pref on and default set"); + ok( !$procfee, "No processing fee if no processing fee"); + + C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 0, "Perdedor"); + $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'L' }); + $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' }); + ok( !$lostfine, "No lost fine if no replacementcost and default not set when pref on"); + is( $procfee->amount, "8.160000", "Processing fee if processing fee"); + $procfee->delete(); + C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber3, 6.12, "Perdedor"); + $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'L' }); + $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber3, accounttype => 'PF' }); + is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and no default set"); + is( $procfee->amount, "8.160000", "Processing fee if processing fee"); + + C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 0, "Perdedor"); + $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' }); + $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' }); + is( $lostfine->amount, "4.080000", "Lost fine is default if no replacementcost but default set when pref on"); + is( $procfee->amount, "2.040000", "Processing fee if processing fee"); + $lostfine->delete(); + $procfee->delete(); + C4::Accounts::chargelostitem( $cli_borrowernumber, $cli_itemnumber4, 6.12, "Perdedor"); + $lostfine = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'L' }); + $procfee = Koha::Account::Lines->find({ borrowernumber => $cli_borrowernumber, itemnumber => $cli_itemnumber4, accounttype => 'PF' }); + is( $lostfine->amount, "6.120000", "Lost fine equals replacementcost when pref on and default set"); + is( $procfee->amount, "2.040000", "Processing fee if processing fee"); +}; + +1; --- a/t/db_dependent/Circulation.t +++ a/t/db_dependent/Circulation.t @@ -60,7 +60,7 @@ my $library2 = $builder->build({ }); my $itemtype = $builder->build( { source => 'Itemtype', - value => { notforloan => undef, rentalcharge => 0 } + value => { notforloan => undef, rentalcharge => 0, defaultreplacecost => undef, processfee => undef } } )->{itemtype}; my $patron_category = $builder->build({ source => 'Category', value => { categorycode => 'NOT_X', category_type => 'P', enrolmentfee => 0 } }); @@ -778,6 +778,7 @@ C4::Context->dbh->do("DELETE FROM accountlines"); is( $offset->type, 'Fine', 'Account offset type is Fine' ); is( $offset->amount, '15.000000', 'Account offset amount is 15.00' ); + ModItem({ itype => 'BK' }, $biblionumber, $itemnumber, 1); LostItem( $itemnumber, 1 ); my $item = Koha::Database->new()->schema()->resultset('Item')->find($itemnumber); --- a/t/db_dependent/Circulation/Chargelostitem.t +++ a/t/db_dependent/Circulation/Chargelostitem.t @@ -0,0 +1,84 @@ +#!/usr/bin/perl + +use Modern::Perl; + +use Test::MockModule; +use C4::Biblio; +use C4::Items; +use C4::Members; +use C4::Branch; +use C4::Category; +use C4::Circulation; +use MARC::Record; +use Test::More tests => 7; + +BEGIN { + use_ok('C4::Accounts'); +} + +my $dbh = C4::Context->dbh; +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; +$dbh->do(q|DELETE FROM accountlines|); + +my $branchcode; +my $branch_created; +my @branches = keys %{ GetBranches() }; +if (@branches) { + $branchcode = $branches[0]; +} else { + $branchcode = 'B'; + ModBranch({ add => 1, branchcode => $branchcode, branchname => 'Branch' }); + $branch_created = 1; +} + +my %item_branch_infos = ( + homebranch => $branchcode, + holdingbranch => $branchcode, +); + +my ($biblionumber1) = AddBiblio(MARC::Record->new, ''); +my $itemnumber1 = AddItem({ barcode => '0101', %item_branch_infos }, $biblionumber1); +my $itemnumber2 = AddItem({ barcode => '0102', %item_branch_infos }, $biblionumber1); + +my ($biblionumber2) = AddBiblio(MARC::Record->new, ''); +my $itemnumber3 = AddItem({ barcode => '0203', %item_branch_infos }, $biblionumber2); + +my $categorycode; +my $category_created; +my @categories = C4::Category->all; +if (@categories) { + $categorycode = $categories[0]->{categorycode} +} else { + $categorycode = 'C'; + $dbh->do( + "INSERT INTO categories(categorycode) VALUES(?)", undef, $categorycode); + $category_created = 1; +} + +my $borrowernumber = AddMember(categorycode => $categorycode, branchcode => $branchcode); +my $borrower = GetMember(borrowernumber => $borrowernumber); + +# Need to mock userenv for AddIssue +my $module = new Test::MockModule('C4::Context'); +$module->mock('userenv', sub { { branch => $branchcode } }); +AddIssue($borrower, '0101'); +AddIssue($borrower, '0203'); + +# Begin tests... +my $processfee = 10; +my $issues; +$issues = C4::Circulation::GetIssues({biblionumber => $biblionumber1}); +my $issue=$issues->[0]; +$issue->{'processfee'} = $processfee; +C4::Accounts::chargelostitem($issue, 'test'); + +my @accountline = C4::Accounts::getcharges($borrowernumber); + +is( scalar(@accountline), 1, 'accountline should have 1 row' ); +is( int($accountline[0]->{amount}), $processfee, "The accountline amount should be precessfee value " ); +is( $accountline[0]->{accounttype}, 'PF', "The accountline accounttype should be PF " ); +is( $accountline[0]->{borrowernumber}, $borrowernumber, "The accountline borrowernumber should be the example borrowernumber" ); +my $itemnumber = C4::Items::GetItemnumberFromBarcode('0101'); +is( $accountline[0]->{itemnumber}, $itemnumber, "The accountline itemnumber should the linked with barcode '0101'" ); +is( $accountline[0]->{description}, 'test ' . $issue->{itemnumber}, "The accountline description should be 'test'" ); --