Revision 684

Date:
2018/06/05 22:39:06
Author:
ahitrov
Revision Log:
Coupon grouping by unique or multiply property

Files:

Legend:

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

     
    43 43 { 'attr' => 'discount', 'type' => 'string', 'rusname' => 'Скидка на сумму заказа (число или процент)', shortname => 'Скидка',
    44 44 default => 0, column => 2 },
    45 45 { 'attr' => 'min_sum', 'type' => 'string', 'rusname' => 'Минимальная сумма, на которую действует скидка', default => 0 },
    46 { 'attr' => 'summon', 'type' => 'checkbox', 'rusname' => 'Может суммироваться с другими купонами', default => 0 },
    46 47 )
    47 48 }
    48 49
  • utf8/plugins/webshop/lib/webshop/Keeper.pm

     
    537 537 %dopts,
    538 538 );
    539 539 return (0, []) unless @coupons;
    540 my @summoned = grep { $_->discount } @coupons;
    541 warn "We count on ".scalar(@summoned)." coupons\n" if $DEBUG;
    540 my @usable = grep { $_->discount } @coupons;
    541 warn "We count on ".scalar(@usable)." coupons\n" if $DEBUG;
    542 542 my ($total, $sum) = $self->basket_count( \@basket );
    543 return (0, \@summoned) unless $sum;
    543 return (0, \@usable) unless $sum;
    544 my %summoned = ( coupons => [], discount => 0 );
    545 my @groups;
    546 foreach my $coupon ( @usable ) {
    547 if ( $coupon->summon ) {
    548 push @{$summoned{coupons}}, $coupon;
    549 } else {
    550 push @groups, { coupons => [$coupon], discount => 0 };
    551 }
    552 }
    544 553 my $result = 0;
    545 foreach my $coupon ( @summoned ) {
    554 foreach my $coupon ( @usable ) {
    546 555 my $res = $coupon->get_discount( basket => \@basket );
    547 556 $coupon->{result} = $res;
    548 557 }
    549 @summoned = sort { $b->{result} <=> $a->{result} } @summoned;
    550 foreach my $coupon ( @summoned ) {
    551 if ( ($coupon->{result} > 0) && ($sum - $coupon->{result} > 0) ) {
    552 $result += $coupon->{result};
    553 $sum -= $coupon->{result};
    554 $coupon->{afflict_order} = 1;
    555 } else {
    556 $coupon->{afflict_order} = 0;
    558 foreach my $group ( @groups ) {
    559 $group->{discount} = $group->{coupons}->[0]->{result};
    560 $group->{coupons}->[0]->{afflict_order} = 1;
    561 }
    562 if ( @{$summoned{coupons}} ) {
    563 @{$summoned{coupons}} = sort { $b->{result} <=> $a->{result} } @{$summoned{coupons}};
    564 my $check = $sum;
    565 foreach my $coupon ( @{$summoned{coupons}} ) {
    566 if ( ($coupon->{result} > 0) && ($check - $coupon->{result} > 0) ) {
    567 $summoned{discount} += $coupon->{result};
    568 $check -= $coupon->{result};
    569 $coupon->{afflict_order} = 1;
    570 } else {
    571 $coupon->{afflict_order} = 0;
    572 }
    557 573 }
    574 unshift @groups, \%summoned;
    558 575 }
    559 return ($result, \@summoned);
    576
    577 @groups = sort { $b->{discount} <=> $a->{discount} } @groups;
    578 return ($groups[0]->{discount}, $groups[0]->{coupons});
    560 579 }
    561 580
    562 581