Revision 589

Date:
2016/09/12 09:48:55
Author:
ahitrov
Revision Log:
Coupon discount refactored

Files:

Legend:

 
Added
 
Removed
 
Modified
  • utf8/plugins/webshop/lib/webshop/Coupon.pm

     
    89 89 return 0 unless exists $opts{basket} || exists $opts{uid} && $opts{uid} || exists $opts{session} && $opts{session};
    90 90 return 0 unless $self->discount;
    91 91
    92 my $basket = exists $opts{basket} ? delete $opts{basket} : $keeper->{webshop}->get_basket ( %opts );
    92 my $basket = exists $opts{basket} ? delete $opts{basket} : $keeper->{webshop}->get_basket ( %opts, with_products => 1 );
    93 93 return 0 unless ref $basket eq 'ARRAY' && @$basket;
    94 94
    95 95 my ($number, $sum_total) = (0, 0);
    96 map { my $price = exists $_->{item} ? $_->{item}->price : $_->price; $number += $_->number; $sum_total += $_->number * $price } @$basket;
    96 my @basket = grep { exists $_->{item} && $_->{item} } @$basket;
    97 return 0 unless @basket;
    98 map { my $price = $_->{item}->price; $number += $_->number; $sum_total += $_->number * $price } @basket;
    97 99 warn "BASKET: $number Items of $sum_total Value\n";
    98 100
    99 101 my $discount_counted = 0;
    100 my $items;
    101 if ( $self->groups ) {
    102 $items = $self->keeper->get_documents (
    103 s => [$self->groups],
    102 my $items = $self->keeper->get_documents (
    104 103 class => $state->{webshop}->{item_document_class},
    105 light => 1,
    106 return_mode => 'array_ref',
    107 );
    108 } else {
    109 $items = $self->keeper->get_documents (
    110 class => $state->{webshop}->{item_document_class},
    111 104 lclass => 'webshop::CouponItemLink',
    112 105 lsource => $self->id,
    113 106 light => 1,
    114 return_mode => 'array_ref',
    107 return_mode => 'hash_ref',
    115 108 );
    109
    110 if ( $self->groups ) {
    111 my %groups = map { $_ => 1 } $self->groups;
    112 @basket = grep { scalar (grep { exists $groups{$_} } $_->sections) > 0 || exists $items->{$_->{item}->id} } @basket;
    113 } elsif ( keys %$items ) {
    114 @basket = grep { exists $items->{$_->{item}->id} } @basket;
    116 115 }
    116 return 0 unless @basket;
    117 117
    118 if ( ref $items eq 'ARRAY' && scalar @$items ) {
    119 my $found_sum = 0;
    120 foreach my $bi ( @$basket ) {
    121 warn "BASKET: Basket item id [".$bi->item_id."]\n" if $DEBUG;
    122 next if exists $bi->{item} && $bi->{item}->special_price || !exists $bi->{item} && $bi->special_price;
    123 next if exists $bi->{item} && (!$bi->{item}->price || !$bi->{item}->storage);
    124 foreach my $item ( @$items) {
    125 if ( $bi->item_id == $item->id ) {
    126 warn "BASKET: Product [".$item->name."] id [".$item->id."]\n" if $DEBUG;
    127 $found_sum += $bi->number * (exists $bi->{item} ? $bi->{item}->price : $bi->price);
    128 last;
    129 }
    130 }
    131 }
    132 warn "Sum found: [$found_sum]\n" if $DEBUG;
    133 return $self->can_discount( $found_sum );
    134 } elsif ( $self->groups ) {
    135 return 0;
    136 } else {
    137 my $found_sum = 0;
    138 foreach my $bi ( @$basket ) {
    139 warn "BASKET: Basket item id [".$bi->item_id."]\n" if $DEBUG;
    140 next if exists $bi->{item} && $bi->{item}->special_price || !exists $bi->{item} && $bi->special_price;
    141 next if exists $bi->{item} && (!$bi->{item}->price || !$bi->{item}->storage);
    142 $found_sum += $bi->number * (exists $bi->{item} ? $bi->{item}->price : $bi->price);
    143 }
    144 warn "Sum found: [$found_sum]\n" if $DEBUG;
    145 return $self->can_discount( $found_sum );
    118 my $found_sum = 0;
    119 foreach my $bi ( @basket ) {
    120 warn "BASKET: Basket item id [".$bi->item_id."]\n" if $DEBUG;
    121 next if $bi->{item}->special_price;
    122 next unless $bi->{item}->price > 0 && $bi->{item}->storage;
    123 $found_sum += $bi->number * $bi->{item}->price;
    146 124 }
    147 return 0;
    125 return $found_sum > 0 ? $self->can_discount( $found_sum ) : 0;
    148 126 }
    149 127
    150 128