Revision 432

Date:
2014/03/29 08:27:35
Author:
ahitrov
Revision Log:
New coupon registering and accounting algorythm
Files:

Legend:

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

     
    86 86 my $self = shift;
    87 87
    88 88 my (%opts) = @_;
    89 my $basket = delete $opts{basket};
    90 return 0 unless exists $opts{uid} && $opts{uid} || exists $opts{session} && $opts{session};
    89 return 0 unless exists $opts{basket} || exists $opts{uid} && $opts{uid} || exists $opts{session} && $opts{session};
    91 90 return 0 unless $self->discount;
    92 91
    93 $basket ||= $keeper->{webshop}->get_basket ( %opts );
    92 my $basket = exists $opts{basket} ? delete $opts{basket} : $keeper->{webshop}->get_basket ( %opts );
    94 93 return 0 unless ref $basket eq 'ARRAY' && @$basket;
    95 94
    96 95 my ($number, $sum_total) = (0, 0);
     
    116 115 );
    117 116 }
    118 117
    119 if ( ref $items eq 'ARRAY' && @$items ) {
    118 if ( ref $items eq 'ARRAY' && scalar @$items ) {
    120 119 my $found_sum = 0;
    121 120 foreach my $bi ( @$basket ) {
    122 warn "BASKET: Item id [".$bi->item_id."]\n" if $DEBUG;
    121 warn "BASKET: Basket item id [".$bi->item_id."]\n" if $DEBUG;
    123 122 next if $bi->special_price;
    124 123 foreach my $item ( @$items) {
    125 124 if ( $bi->item_id == $item->id ) {
    126 warn "BASKET: Item [".$item->name."] id [".$item->id."]\n" if $DEBUG;
    125 warn "BASKET: Product [".$item->name."] id [".$item->id."]\n" if $DEBUG;
    127 126 $found_sum += $bi->number * $bi->price;
    128 127 last;
    129 128 }
     
    136 135 } else {
    137 136 my $found_sum = 0;
    138 137 foreach my $bi ( @$basket ) {
    139 warn "BASKET: Item id [".$bi->item_id."]\n" if $DEBUG;
    138 warn "BASKET: Basket item id [".$bi->item_id."]\n" if $DEBUG;
    140 139 next if $bi->special_price;
    141 140 $found_sum += $bi->number * $bi->price;
    142 141 }
     
    164 163 $count = $discount;
    165 164 }
    166 165 my $rest = $sum - $count;
    167 $count = 0 if $rest < $min_sum || $rest == 0;
    166 warn "Min Sum: $min_sum. Rest: $rest\n" if $DEBUG;
    167 $count = 0 if ($min_sum && $sum < $min_sum) || $rest <= 0;
    168 warn "Count: $count\n" if $DEBUG;
    168 169 return $count;
    169 170 }
    170 171
  • utf8/plugins/webshop/lib/webshop/Discount.pm

     
    94 94
    95 95 my ($number, $sum_total) = (0, 0);
    96 96 map { $number += $_->number; $sum_total += $_->number * $_->{item}->price } @basket;
    97 warn "BASKET: $number Items of $sum_total Value\n";
    97 warn "BASKET: $number Items of $sum_total Value\n" if $DEBUG;
    98 98
    99 99 my %item_props = map { $_->{attr} => $_ } $basket[0]->{item}->structure;
    100 100 if ( exists $item_props{special_price} ) {
  • utf8/plugins/webshop/lib/webshop/Keeper.pm

     
    426 426 my $self = shift;
    427 427 my (%opts) = @_;
    428 428
    429 warn "Check discount begin: ".Dumper(\%opts)."\n" if $DEBUG;
    429 warn "Check discount begin:\n" if $DEBUG;
    430 430 my %dopts;
    431 431 if ( exists $opts{uid} && $opts{uid} ) {
    432 432 $dopts{uid} = [0,1];
     
    461 461 }
    462 462
    463 463
    464 sub check_coupons {
    465 my $self = shift;
    466 my (%opts) = @_;
    467
    468 warn "Check coupons begin:\n" if $DEBUG;
    469 my %dopts;
    470 if ( exists $opts{uid} && $opts{uid} ) {
    471 $dopts{luid} = $opts{uid};
    472 } else {
    473 $dopts{lsession} = $opts{session};
    474 }
    475 my $basket = exists $opts{basket} ? $opts{basket} : $self->get_basket( %opts, with_products => 1 );
    476 return (0, []) unless ref $basket eq 'ARRAY' && @$basket;
    477 my @basket = grep { exists $_->{item} && $_->{item} } @$basket;
    478 return (0, []) unless @basket;
    479
    480 my $now = Contenido::DateTime->new;
    481 my @coupons = exists $opts{coupons} && ref $opts{coupons} eq 'ARRAY' ? @{$opts{coupons}} : $keeper->get_documents(
    482 class => 'webshop::Coupon',
    483 lclass => 'webshop::OrderCouponLink',
    484 lsource => 0,
    485 status => 1,
    486 interval=> [$now->ymd, $now->ymd],
    487 %dopts,
    488 );
    489 return (0, []) unless @coupons;
    490 my @summoned = grep { $_->discount } @coupons;
    491 warn "We count on ".scalar(@summoned)." coupons\n" if $DEBUG;
    492 my ($total, $sum) = $self->basket_count( \@basket );
    493 return (0, \@summoned) unless $sum;
    494 my $result = 0;
    495 foreach my $coupon ( @summoned ) {
    496 my $res = $coupon->get_discount( basket => \@basket );
    497 $coupon->{result} = $res;
    498 }
    499 @summoned = sort { $b->{result} <=> $a->{result} } @summoned;
    500 foreach my $coupon ( @summoned ) {
    501 if ( ($coupon->{result} > 0) && ($sum - $coupon->{result} > 0) ) {
    502 $result += $coupon->{result};
    503 $sum -= $coupon->{result};
    504 $coupon->{afflict_order} = 1;
    505 } else {
    506 $coupon->{afflict_order} = 0;
    507 }
    508 }
    509 return ($result, \@summoned);
    510 }
    511
    512
    464 513 sub price_format {
    465 514 my $self = shift;
    466 515 my $price = shift;
  • utf8/plugins/webshop/lib/webshop/SQL/CouponsTable.pm

     
    151 151 sub _interval_filter {
    152 152 my ($self,%opts)=@_;
    153 153 return undef unless ( exists $opts{interval} && ref $opts{interval} eq 'ARRAY' && scalar @{$opts{interval}} == 2 );
    154 return undef unless ref $opts{interval}->[0] && ref $opts{interval}->[1];
    155 return undef if DateTime->compare($opts{interval}->[0], $opts{interval}->[1]) == 1;
    154 return undef unless $opts{interval}->[0] && $opts{interval}->[1];
    155 my $date1 = ref $opts{interval}->[0] ? $opts{interval}->[0] : Contenido::DateTime->new( postgres => $opts{interval}->[0].( $opts{interval}->[0] =~ /\d{2}\:\d{2}\:\d{2}/ ? '' : ' 00:00:00' ) );
    156 my $date2 = ref $opts{interval}->[1] ? $opts{interval}->[1] : Contenido::DateTime->new( postgres => $opts{interval}->[1].( $opts{interval}->[1] =~ /\d{2}\:\d{2}\:\d{2}/ ? '' : ' 00:00:00' ) );
    157 return undef if DateTime->compare($date1, $date2) == 1;
    156 158
    157 159 my $wheres = "date_trunc('day', d.dtime) <= ? and date_trunc('day', d.etime) >= ?";
    158 my @values = ($opts{interval}->[1]->ymd('-'), $opts{interval}->[0]->ymd('-'));
    160 my @values = ($date2->ymd('-'), $date1->ymd('-'));
    159 161
    160 162 return ($wheres, \@values);
    161 163 }