Revision 787

Date:
2020/03/20 15:57:09
Author:
ahitrov
Revision Log:
sum_discount method gets payment discount too

Files:

Legend:

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

     
    488 488 my @basket = grep { exists $_->{item} && $_->{item} } @$basket;
    489 489 return 0 unless @basket;
    490 490
    491 my $payment = delete $opts{payment};
    492 if ( $payment && !ref $payment ) {
    493 if ( $payment =~ /^\d+$/ ) {
    494 $payment = $keeper->get_document_by_id($payment, class => 'webshop::Payment');
    495 }
    496 }
    497
    491 498 my $now = Contenido::DateTime->new;
    492 499 my @discounts = $keeper->get_documents(
    493 500 class => 'webshop::Discount',
     
    495 502 interval=> [$now, $now],
    496 503 %dopts,
    497 504 );
    498 return 0 unless @discounts;
    499 505 my @summoned = sort { $b->min_sum <=> $a->min_sum } grep { $_->min_sum && $_->discount && $_->min_sum =~ /^\d+$/ } @discounts;
    500 506 my ($total, $sum) = $self->basket_count( \@basket );
    501 507 return 0 unless $sum;
     
    506 512 $result = $res if $res > $result;
    507 513 }
    508 514 }
    515
    516 my $summarize = 0;
    517 if ( ref $payment && $payment->discount && $payment->discount > 0 ) {
    518 my $payment_discount = $payment->discount / 100;
    519 my $payment_discount_sum = 0;
    520 if ( $payment->exclude_specials ) {
    521 foreach my $item ( @basket ) {
    522 next if $item->special_price;
    523 $payment_discount_sum += $item->price;
    524 }
    525 $payment_discount_sum = int($payment_discount_sum * $payment_discount);
    526 } else {
    527 $payment_discount_sum = int($sum * $payment_discount);
    528 }
    529 if ( $payment->summarize ) {
    530 $summarize = 1;
    531 $result += $payment_discount_sum if $payment_discount_sum + $result < $sum;
    532 } else {
    533 $result = $payment_discount_sum if $payment_discount_sum > $result;
    534 }
    535 }
    536
    509 537 $result = 0 if $result >= $sum;
    510 return $result;
    538 return ($result, $summarize);
    511 539 }
    512 540
    513 541