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

(-)a/Koha/CirculationRules.pm (-37 / +10 lines)
Lines 577-585 sub get_onshelfholds_policy { Link Here
577
577
578
=head3 get_lostreturn_policy
578
=head3 get_lostreturn_policy
579
579
580
  my $lostrefund_policy = Koha::CirculationRules->get_lostreturn_policy( { return_branch => $return_branch, item => $item } );
580
  my $lost_proc_refund_policy = Koha::CirculationRules->get_lostreturn_policy( { return_branch => $return_branch, item => $item } );
581
581
582
Return values are:
582
lostreturn return values are:
583
583
584
=over 2
584
=over 2
585
585
Lines 593-629 Return values are: Link Here
593
593
594
=back
594
=back
595
595
596
=cut
596
processing return return values are:
597
598
sub get_lostreturn_policy {
599
    my ( $class, $params ) = @_;
600
601
    my $item   = $params->{item};
602
603
    my $behaviour = C4::Context->preference( 'RefundLostOnReturnControl' ) // 'CheckinLibrary';
604
    my $behaviour_mapping = {
605
        CheckinLibrary    => $params->{'return_branch'} // $item->homebranch,
606
        ItemHomeBranch    => $item->homebranch,
607
        ItemHoldingBranch => $item->holdingbranch
608
    };
609
610
    my $branch = $behaviour_mapping->{ $behaviour };
611
612
    my $rule = Koha::CirculationRules->get_effective_rule(
613
        {
614
            branchcode => $branch,
615
            rule_name  => 'lostreturn',
616
        }
617
    );
618
619
    return $rule ? $rule->rule_value : 'refund';
620
}
621
622
=head3 get_processingreturn_policy
623
624
  my $processingrefund_policy = Koha::CirculationRules->get_processingreturn_policy( { return_branch => $return_branch, item => $item } );
625
626
Return values are:
627
597
628
=over 2
598
=over 2
629
599
Lines 637-645 Return values are: Link Here
637
607
638
=back
608
=back
639
609
610
640
=cut
611
=cut
641
612
642
sub get_processingreturn_policy {
613
sub get_lostreturn_policy {
643
    my ( $class, $params ) = @_;
614
    my ( $class, $params ) = @_;
644
615
645
    my $item   = $params->{item};
616
    my $item   = $params->{item};
Lines 653-666 sub get_processingreturn_policy { Link Here
653
624
654
    my $branch = $behaviour_mapping->{ $behaviour };
625
    my $branch = $behaviour_mapping->{ $behaviour };
655
626
656
    my $rule = Koha::CirculationRules->get_effective_rule(
627
    my $rules = Koha::CirculationRules->get_effective_rules(
657
        {
628
        {
658
            branchcode => $branch,
629
            branchcode => $branch,
659
            rule_name  => 'processingreturn',
630
            rules  => ['lostreturn','processingreturn']
660
        }
631
        }
661
    );
632
    );
662
633
663
    return $rule ? $rule->rule_value : 'refund';
634
    $rules->{lostreturn} //= 'refund';
635
    $rules->{processingreturn} //= 'refund';
636
    return $rules;
664
}
637
}
665
638
666
=head3 article_requestable_rules
639
=head3 article_requestable_rules
(-)a/Koha/Item.pm (-9 / +3 lines)
Lines 1159-1165 sub _set_found_trigger { Link Here
1159
        return $self unless $lost_age_in_days < $no_refund_after_days;
1159
        return $self unless $lost_age_in_days < $no_refund_after_days;
1160
    }
1160
    }
1161
1161
1162
    my $lostreturn_policy = Koha::CirculationRules->get_lostreturn_policy(
1162
    my $lost_proc_return_policy = Koha::CirculationRules->get_lostreturn_policy(
1163
        {
1163
        {
1164
            item          => $self,
1164
            item          => $self,
1165
            return_branch => C4::Context->userenv
1165
            return_branch => C4::Context->userenv
Lines 1167-1172 sub _set_found_trigger { Link Here
1167
            : undef,
1167
            : undef,
1168
        }
1168
        }
1169
      );
1169
      );
1170
    my $lostreturn_policy = $lost_proc_return_policy->{lostreturn};
1170
1171
1171
    if ( $lostreturn_policy ) {
1172
    if ( $lostreturn_policy ) {
1172
1173
Lines 1324-1337 sub _set_found_trigger { Link Here
1324
        }
1325
        }
1325
    }
1326
    }
1326
1327
1327
    my $processingreturn_policy = Koha::CirculationRules->get_processingreturn_policy(
1328
    my $processingreturn_policy = $lost_proc_return_policy->{lostreturn};
1328
        {
1329
            item          => $self,
1330
            return_branch => C4::Context->userenv
1331
            ? C4::Context->userenv->{'branch'}
1332
            : undef,
1333
        }
1334
      );
1335
1329
1336
    if ( $processingreturn_policy ) {
1330
    if ( $processingreturn_policy ) {
1337
1331
(-)a/t/db_dependent/Koha/CirculationRules.t (-140 / +82 lines)
Lines 20-26 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Benchmark;
22
use Benchmark;
23
use Test::More tests => 8;
23
use Test::More tests => 7;
24
use Test::Deep qw( cmp_methods );
24
use Test::Deep qw( cmp_methods );
25
use Test::Exception;
25
use Test::Exception;
26
26
Lines 29-34 use Koha::Database; Link Here
29
29
30
use t::lib::Mocks;
30
use t::lib::Mocks;
31
use t::lib::TestBuilder;
31
use t::lib::TestBuilder;
32
use Koha::Cache::Memory::Lite;
32
33
33
my $schema = Koha::Database->new->schema;
34
my $schema = Koha::Database->new->schema;
34
my $builder = t::lib::TestBuilder->new;
35
my $builder = t::lib::TestBuilder->new;
Lines 687-751 subtest 'get_lostreturn_policy() tests' => sub { Link Here
687
688
688
    $schema->resultset('CirculationRule')->search()->delete;
689
    $schema->resultset('CirculationRule')->search()->delete;
689
690
690
    my $default_rule_charge = $builder->build(
691
    my $default_proc_rule_charge = $builder->build(
691
        {
692
        {
692
            source => 'CirculationRule',
693
            source => 'CirculationRule',
693
            value  => {
694
            value  => {
694
                branchcode   => undef,
695
                branchcode   => undef,
695
                categorycode => undef,
696
                categorycode => undef,
696
                itemtype     => undef,
697
                itemtype     => undef,
697
                rule_name    => 'lostreturn',
698
                rule_name    => 'processingreturn',
698
                rule_value   => 'charge'
699
                rule_value   => 'charge'
699
            }
700
            }
700
        }
701
        }
701
    );
702
    );
702
    my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
703
    my $default_lost_rule_charge = $builder->build(
703
    my $specific_rule_false = $builder->build(
704
        {
704
        {
705
            source => 'CirculationRule',
705
            source => 'CirculationRule',
706
            value  => {
706
            value  => {
707
                branchcode   => $branchcode,
707
                branchcode   => undef,
708
                categorycode => undef,
708
                categorycode => undef,
709
                itemtype     => undef,
709
                itemtype     => undef,
710
                rule_name    => 'lostreturn',
710
                rule_name    => 'lostreturn',
711
                rule_value   => 0
711
                rule_value   => 'charge'
712
            }
712
            }
713
        }
713
        }
714
    );
714
    );
715
    my $branchcode2 = $builder->build( { source => 'Branch' } )->{branchcode};
715
    my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
716
    my $specific_rule_refund = $builder->build(
716
    my $specific_lost_rule_false = $builder->build(
717
        {
717
        {
718
            source => 'CirculationRule',
718
            source => 'CirculationRule',
719
            value  => {
719
            value  => {
720
                branchcode   => $branchcode2,
720
                branchcode   => $branchcode,
721
                categorycode => undef,
721
                categorycode => undef,
722
                itemtype     => undef,
722
                itemtype     => undef,
723
                rule_name    => 'lostreturn',
723
                rule_name    => 'lostreturn',
724
                rule_value   => 'refund'
724
                rule_value   => 0
725
            }
725
            }
726
        }
726
        }
727
    );
727
    );
728
    my $branchcode3 = $builder->build( { source => 'Branch' } )->{branchcode};
728
    my $specific_proc_rule_false = $builder->build(
729
    my $specific_rule_restore = $builder->build(
730
        {
729
        {
731
            source => 'CirculationRule',
730
            source => 'CirculationRule',
732
            value  => {
731
            value  => {
733
                branchcode   => $branchcode3,
732
                branchcode   => $branchcode,
734
                categorycode => undef,
733
                categorycode => undef,
735
                itemtype     => undef,
734
                itemtype     => undef,
736
                rule_name    => 'lostreturn',
735
                rule_name    => 'processingreturn',
737
                rule_value   => 'restore'
736
                rule_value   => 0
738
            }
737
            }
739
        }
738
        }
740
    );
739
    );
741
740
    my $branchcode2 = $builder->build( { source => 'Branch' } )->{branchcode};
742
    # Make sure we have an unused branchcode
741
    my $specific_lost_rule_refund = $builder->build(
743
    my $branchcode4 = $builder->build( { source => 'Branch' } )->{branchcode};
744
    my $specific_rule_dummy = $builder->build(
745
        {
742
        {
746
            source => 'CirculationRule',
743
            source => 'CirculationRule',
747
            value  => {
744
            value  => {
748
                branchcode   => $branchcode4,
745
                branchcode   => $branchcode2,
749
                categorycode => undef,
746
                categorycode => undef,
750
                itemtype     => undef,
747
                itemtype     => undef,
751
                rule_name    => 'lostreturn',
748
                rule_name    => 'lostreturn',
Lines 753-892 subtest 'get_lostreturn_policy() tests' => sub { Link Here
753
            }
750
            }
754
        }
751
        }
755
    );
752
    );
756
    my $branch_without_rule = $specific_rule_dummy->{ branchcode };
753
    my $specific_proc_rule_refund = $builder->build(
757
    Koha::CirculationRules
758
        ->search(
759
            {
760
                branchcode   => $branch_without_rule,
761
                categorycode => undef,
762
                itemtype     => undef,
763
                rule_name    => 'lostreturn',
764
                rule_value   => 'refund'
765
            }
766
          )
767
        ->next
768
        ->delete;
769
770
    my $item = $builder->build_sample_item(
771
        {
772
            homebranch    => $specific_rule_restore->{branchcode},
773
            holdingbranch => $specific_rule_false->{branchcode}
774
        }
775
    );
776
    my $params = {
777
        return_branch => $specific_rule_refund->{ branchcode },
778
        item          => $item
779
    };
780
781
    # Specific rules
782
    t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
783
    is( Koha::CirculationRules->get_lostreturn_policy( $params ),
784
        'refund','Specific rule for checkin branch is applied (refund)');
785
786
    t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
787
    is( Koha::CirculationRules->get_lostreturn_policy( $params ),
788
         'restore','Specific rule for home branch is applied (restore)');
789
790
    t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
791
    is( Koha::CirculationRules->get_lostreturn_policy( $params ),
792
         0,'Specific rule for holding branch is applied (false)');
793
794
    # Default rule check
795
    t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
796
    $params->{return_branch} = $branch_without_rule;
797
    is( Koha::CirculationRules->get_lostreturn_policy( $params ),
798
         'charge','No rule for branch, global rule applied (charge)');
799
800
    # Change the default value just to try
801
    Koha::CirculationRules->search({ branchcode => undef, rule_name => 'lostreturn' })->next->rule_value(0)->store;
802
    is( Koha::CirculationRules->get_lostreturn_policy( $params ),
803
         0,'No rule for branch, global rule applied (false)');
804
805
    # No default rule defined check
806
    Koha::CirculationRules
807
        ->search(
808
            {
809
                branchcode   => undef,
810
                categorycode => undef,
811
                itemtype     => undef,
812
                rule_name    => 'lostreturn'
813
            }
814
          )
815
        ->next
816
        ->delete;
817
    is( Koha::CirculationRules->get_lostreturn_policy( $params ),
818
         'refund','No rule for branch, no default rule, fallback default (refund)');
819
820
    # Fallback to ItemHoldBranch if CheckinLibrary is undefined
821
    $params->{return_branch} = undef;
822
    is( Koha::CirculationRules->get_lostreturn_policy( $params ),
823
         'restore','return_branch undefined, fallback to ItemHomeBranch rule (restore)');
824
825
    $schema->storage->txn_rollback;
826
};
827
828
subtest 'get_processingreturn_policy() tests' => sub {
829
    plan tests => 7;
830
831
    $schema->storage->txn_begin;
832
833
    $schema->resultset('CirculationRule')->search()->delete;
834
835
    my $default_rule_charge = $builder->build(
836
        {
754
        {
837
            source => 'CirculationRule',
755
            source => 'CirculationRule',
838
            value  => {
756
            value  => {
839
                branchcode   => undef,
757
                branchcode   => $branchcode2,
840
                categorycode => undef,
758
                categorycode => undef,
841
                itemtype     => undef,
759
                itemtype     => undef,
842
                rule_name    => 'processingreturn',
760
                rule_name    => 'processingreturn',
843
                rule_value   => 'charge'
761
                rule_value   => 'refund'
844
            }
762
            }
845
        }
763
        }
846
    );
764
    );
847
    my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
765
    my $branchcode3 = $builder->build( { source => 'Branch' } )->{branchcode};
848
    my $specific_rule_false = $builder->build(
766
    my $specific_lost_rule_restore = $builder->build(
849
        {
767
        {
850
            source => 'CirculationRule',
768
            source => 'CirculationRule',
851
            value  => {
769
            value  => {
852
                branchcode   => $branchcode,
770
                branchcode   => $branchcode3,
853
                categorycode => undef,
771
                categorycode => undef,
854
                itemtype     => undef,
772
                itemtype     => undef,
855
                rule_name    => 'processingreturn',
773
                rule_name    => 'lostreturn',
856
                rule_value   => 0
774
                rule_value   => 'restore'
857
            }
775
            }
858
        }
776
        }
859
    );
777
    );
860
    my $branchcode2 = $builder->build( { source => 'Branch' } )->{branchcode};
778
    my $specific_proc_rule_restore = $builder->build(
861
    my $specific_rule_refund = $builder->build(
862
        {
779
        {
863
            source => 'CirculationRule',
780
            source => 'CirculationRule',
864
            value  => {
781
            value  => {
865
                branchcode   => $branchcode2,
782
                branchcode   => $branchcode3,
866
                categorycode => undef,
783
                categorycode => undef,
867
                itemtype     => undef,
784
                itemtype     => undef,
868
                rule_name    => 'processingreturn',
785
                rule_name    => 'processingreturn',
869
                rule_value   => 'refund'
786
                rule_value   => 'restore'
870
            }
787
            }
871
        }
788
        }
872
    );
789
    );
873
    my $branchcode3 = $builder->build( { source => 'Branch' } )->{branchcode};
790
874
    my $specific_rule_restore = $builder->build(
791
    # Make sure we have an unused branchcode
792
    my $branchcode4 = $builder->build( { source => 'Branch' } )->{branchcode};
793
    my $specific_lost_rule_dummy = $builder->build(
875
        {
794
        {
876
            source => 'CirculationRule',
795
            source => 'CirculationRule',
877
            value  => {
796
            value  => {
878
                branchcode   => $branchcode3,
797
                branchcode   => $branchcode4,
879
                categorycode => undef,
798
                categorycode => undef,
880
                itemtype     => undef,
799
                itemtype     => undef,
881
                rule_name    => 'processingreturn',
800
                rule_name    => 'lostreturn',
882
                rule_value   => 'restore'
801
                rule_value   => 'refund'
883
            }
802
            }
884
        }
803
        }
885
    );
804
    );
886
805
    my $specific_proc_rule_dummy = $builder->build(
887
    # Make sure we have an unused branchcode
888
    my $branchcode4 = $builder->build( { source => 'Branch' } )->{branchcode};
889
    my $specific_rule_dummy = $builder->build(
890
        {
806
        {
891
            source => 'CirculationRule',
807
            source => 'CirculationRule',
892
            value  => {
808
            value  => {
Lines 898-904 subtest 'get_processingreturn_policy() tests' => sub { Link Here
898
            }
814
            }
899
        }
815
        }
900
    );
816
    );
901
    my $branch_without_rule = $specific_rule_dummy->{ branchcode };
817
    my $branch_without_rule = $specific_lost_rule_dummy->{ branchcode };
818
    Koha::CirculationRules
819
        ->search(
820
            {
821
                branchcode   => $branch_without_rule,
822
                categorycode => undef,
823
                itemtype     => undef,
824
                rule_name    => 'lostreturn',
825
                rule_value   => 'refund'
826
            }
827
          )
828
        ->next
829
        ->delete;
902
    Koha::CirculationRules
830
    Koha::CirculationRules
903
        ->search(
831
        ->search(
904
            {
832
            {
Lines 914-953 subtest 'get_processingreturn_policy() tests' => sub { Link Here
914
842
915
    my $item = $builder->build_sample_item(
843
    my $item = $builder->build_sample_item(
916
        {
844
        {
917
            homebranch    => $specific_rule_restore->{branchcode},
845
            homebranch    => $specific_lost_rule_restore->{branchcode},
918
            holdingbranch => $specific_rule_false->{branchcode}
846
            holdingbranch => $specific_lost_rule_false->{branchcode}
919
        }
847
        }
920
    );
848
    );
921
    my $params = {
849
    my $params = {
922
        return_branch => $specific_rule_refund->{ branchcode },
850
        return_branch => $specific_lost_rule_refund->{ branchcode },
923
        item          => $item
851
        item          => $item
924
    };
852
    };
925
853
926
    # Specific rules
854
    # Specific rules
927
    t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
855
    t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
928
    is( Koha::CirculationRules->get_processingreturn_policy( $params ),
856
    is_deeply( Koha::CirculationRules->get_lostreturn_policy( $params ),
929
        'refund','Specific rule for checkin branch is applied (refund)');
857
        { lostreturn => 'refund', processingreturn => 'refund' },'Specific rule for checkin branch is applied (refund)');
930
858
931
    t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
859
    t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
932
    is( Koha::CirculationRules->get_processingreturn_policy( $params ),
860
    is_deeply( Koha::CirculationRules->get_lostreturn_policy( $params ),
933
         'restore','Specific rule for home branch is applied (restore)');
861
         { lostreturn => 'restore', processingreturn => 'restore' },'Specific rule for home branch is applied (restore)');
934
862
935
    t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
863
    t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
936
    is( Koha::CirculationRules->get_processingreturn_policy( $params ),
864
    is_deeply( Koha::CirculationRules->get_lostreturn_policy( $params ),
937
         0,'Specific rule for holding branch is applied (false)');
865
         { lostreturn => 0, processingreturn => 0 },'Specific rule for holding branch is applied (false)');
938
866
939
    # Default rule check
867
    # Default rule check
940
    t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
868
    t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
941
    $params->{return_branch} = $branch_without_rule;
869
    $params->{return_branch} = $branch_without_rule;
942
    is( Koha::CirculationRules->get_processingreturn_policy( $params ),
870
    is_deeply( Koha::CirculationRules->get_lostreturn_policy( $params ),
943
         'charge','No rule for branch, global rule applied (charge)');
871
         { lostreturn => 'charge', processingreturn => 'charge' },'No rule for branch, global rule applied (charge)');
944
872
945
    # Change the default value just to try
873
    # Change the default value just to try
874
    Koha::CirculationRules->search({ branchcode => undef, rule_name => 'lostreturn' })->next->rule_value(0)->store;
946
    Koha::CirculationRules->search({ branchcode => undef, rule_name => 'processingreturn' })->next->rule_value(0)->store;
875
    Koha::CirculationRules->search({ branchcode => undef, rule_name => 'processingreturn' })->next->rule_value(0)->store;
947
    is( Koha::CirculationRules->get_processingreturn_policy( $params ),
876
    my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
948
         0,'No rule for branch, global rule applied (false)');
877
    $memory_cache->flush();
878
    is_deeply( Koha::CirculationRules->get_lostreturn_policy( $params ),
879
         { lostreturn => 0, processingreturn => 0 },'No rule for branch, global rule applied (false)');
949
880
950
    # No default rule defined check
881
    # No default rule defined check
882
    Koha::CirculationRules
883
        ->search(
884
            {
885
                branchcode   => undef,
886
                categorycode => undef,
887
                itemtype     => undef,
888
                rule_name    => 'lostreturn'
889
            }
890
          )
891
        ->next
892
        ->delete;
893
    # No default rule defined check
951
    Koha::CirculationRules
894
    Koha::CirculationRules
952
        ->search(
895
        ->search(
953
            {
896
            {
Lines 959-971 subtest 'get_processingreturn_policy() tests' => sub { Link Here
959
          )
902
          )
960
        ->next
903
        ->next
961
        ->delete;
904
        ->delete;
962
    is( Koha::CirculationRules->get_processingreturn_policy( $params ),
905
    is_deeply( Koha::CirculationRules->get_lostreturn_policy( $params ),
963
         'refund','No rule for branch, no default rule, fallback default (refund)');
906
         { lostreturn => 'refund', processingreturn => 'refund' },'No rule for branch, no default rule, fallback default (refund)');
964
907
965
    # Fallback to ItemHoldBranch if CheckinLibrary is undefined
908
    # Fallback to ItemHoldBranch if CheckinLibrary is undefined
966
    $params->{return_branch} = undef;
909
    $params->{return_branch} = undef;
967
    is( Koha::CirculationRules->get_processingreturn_policy( $params ),
910
    is_deeply( Koha::CirculationRules->get_lostreturn_policy( $params ),
968
         'restore','return_branch undefined, fallback to ItemHomeBranch rule (restore)');
911
         { lostreturn => 'restore', processingreturn => 'restore' },'return_branch undefined, fallback to ItemHomeBranch rule (restore)');
969
912
970
    $schema->storage->txn_rollback;
913
    $schema->storage->txn_rollback;
971
};
914
};
972
- 

Return to bug 23012