Revision 675
- Date:
- 2018/03/26 17:27:44
- Files:
Legend:
- Added
- Removed
- Modified
-
utf8/plugins/webshop/lib/webshop/Keeper.pm
560 560 } 561 561 562 562 563 sub register_coupon { 564 my $self = shift; 565 my ($code, $session) = @_; 566 my %result; 567 my %opts; 568 569 if ( ref $session ) { 570 if ( $code ) { 571 $code =~ s/([%_\\])/\\$1/g; 572 if ( $session->id ) { 573 $opts{uid} = $session->{id}; 574 } else { 575 $opts{session} = $session->_session_id; 576 } 577 my $basket = $self->get_basket ( 578 %opts, 579 with_products => 1 580 ); 581 my $now = Contenido::DateTime->new; 582 my @reglinks = $keeper->get_links ( 583 class => 'webshop::OrderCouponLink', 584 source_id => 0, 585 %opts, 586 ); 587 my %cids = map { $_->dest_id => 1 } @reglinks; 588 my @cids = keys %cids; 589 my @registered = @cids ? $keeper->get_documents( 590 id => \@cids, 591 class => 'webshop::Coupon', 592 ) : (); 593 my ($coupon) = grep { $_->code eq $code } @registered; 594 @registered = grep { 595 my $bt = Contenido::DateTime->new( postgres => $_->dtime ); 596 my $et = Contenido::DateTime->new( postgres => $_->etime ); 597 $now >= $bt && $now <= $et && $_->status == 1 598 } @registered; 599 $result{coupons} = \@registered; 600 if ( ref $coupon ) { 601 $result{error} = 'Такой купон уже зарегистрирован'; 602 $result{found} = $coupon; 603 } else { 604 ($coupon) = $keeper->get_documents( 605 class => 'webshop::Coupon', 606 code => $code, 607 uid => 0, 608 status => [1,3], 609 interval => [$now, $now], 610 ilike => 1, 611 ); 612 if ( $session->id && !ref $coupon ) { 613 ($coupon) = $keeper->get_documents( 614 class => 'webshop::Coupon', 615 code => $code, 616 uid => $session->id, 617 status => [1,3], 618 interval => [$now, $now], 619 ilike => 1, 620 ); 621 } 622 if ( ref $coupon ) { 623 if ( $coupon->uid && $coupon->status == 3 ) { 624 $result{error} = 'Купон уже использован'; 625 } 626 unless ( exists $result{error} && $result{error} ) { 627 my $coupon_link = webshop::OrderCouponLink->new( $keeper ); 628 $coupon_link->status( 0 ); 629 if ( $session->id ) { 630 $coupon_link->uid( $session->id ); 631 } else { 632 $coupon_link->uid( 0 ); 633 $coupon_link->session( $session->_session_id ); 634 } 635 $coupon_link->dest_id( $coupon->id ); 636 $coupon_link->dest_class( $coupon->class ); 637 $coupon_link->source_id( 0 ); 638 $coupon_link->source_class( 'webshop::Order' ); 639 $coupon_link->store; 640 $result{created} = $coupon; 641 push @registered, $coupon; 642 } 643 } else { 644 $result{error} = 'Купон не найден'; 645 } 646 } 647 } else { 648 $result{error} = 'Вы не указали код купона'; 649 } 650 } else { 651 $result{error} = 'Фатальная ошибка. Не работают сессии! Обратитесь в службу поддержки магазина'; 652 } 653 654 return \%result; 655 } 656 657 563 658 sub price_format { 564 659 my $self = shift; 565 660 my $price = shift;