package API::DB::Item::PortalPayee;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::DB::ItemCollection;
use Common::DB::Item;
use base 'Common::DB::Item';

use constant kTable => 'portal_payee';
use constant kDB    => Common::DB::Item::kCommonDB;

sub GetAll {
    my $class = shift;
    my $sql   = "SELECT * FROM " . kTable . " ORDER BY id";
    return $class->SUPER::GetAll($sql);
}

sub GetByInviteKey {
    my ( $class, $inviteKey ) = @_;
    my $dbo = Common::RSApp::GetCommonDB();
    my $sql   = "SELECT * FROM " . kTable . " WHERE invite_key = " . $dbo->DBQuote($inviteKey);
    return $class->SUPER::GetAll($sql);
}

sub GetByInvitee {
    my ( $class, $invitee ) = @_;
    my $dbo = Common::RSApp::GetCommonDB();
    my $sql = "SELECT * FROM " . kTable . " WHERE invitee = " . $dbo->DBQuote($invitee);
    return $class->SUPER::GetAll($sql);
}

sub GetByUserID {
    my ( $class, $userID ) = @_;
    my $dbo = Common::RSApp::GetCommonDB();
    my $sql   = "SELECT * FROM " . kTable . " WHERE user_id = " . $dbo->DBQuote($userID);
    return $class->SUPER::GetAll($sql);
}

sub GetAllConfirmedByUserIDPayeeType {
    my ( $class, $userID, $payeeType ) = @_;

    my $dbo = Common::RSApp::GetCommonDB();

    $userID = $dbo->DBQuote($userID);
    $payeeType = $dbo->DBQuote($payeeType);

    my $sql = qq/
        SELECT *
        FROM portal_payee
        WHERE date_confirmed != 0
            AND user_id = $userID
            AND payee_type = $payeeType
    /;

    return $class->SUPER::GetAll($sql);
}

sub Delete {
    my ( $class, %args ) = @_;
    return unless %args;

    my $dbo = Common::RSApp::GetCommonDB();

    my $where = '';
    while ( my($k, $v) = each %args ) {
        next unless defined $k && defined $v;
        $v = $dbo->DBQuote($v);
        $where .= " AND $k = $v";
    }

    return $dbo->DoCmd("DELETE FROM " . kTable . " WHERE 1 = 1 $where");
}

1;
