#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2011 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#---------------------------------------------------------------
package BookPub::DB::Item::PublisherImprints;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use Common::Assert;

use base 'Common::DB::Item';

#use constant kTable => 'book_product';
use constant kDB => Common::DB::Item::kClientDB;

sub _GenerateClassConfig {
    my ($class) = @_;

    return {
        publisher_id   => { _readOnly => 1 },
        publisher_name => { _readOnly => 1 },
        imprint_id     => { _readOnly => 1 },
        imprint_name   => { _readOnly => 1 },
    };
}

sub GetAllSortedByName {
    my ($class) = @_;

    #my $sql = "SELECT " . kTable . ".publisher_id, publisher.name, " . kTable . ".imprint_id, imprint.name " .
    #          "FROM " . kTable . " " .
    #          "LEFT JOIN publisher USING (publisher_id) " .
    #          "LEFT JOIN imprint ON " . kTable . ".imprint_id = imprint.imprint_id " .
    #          "GROUP BY publisher_id, imprint_id";

    my $sql =
        "SELECT book_product.publisher_id, publisher.name AS publisher_name, book_product.imprint_id, imprint.name AS imprint_name "
      . "FROM book_product "
      . "LEFT JOIN publisher USING (publisher_id) "
      . "LEFT JOIN imprint ON book_product.imprint_id = imprint.imprint_id "
      . "GROUP BY publisher.publisher_id, imprint.imprint_id "
      . "ORDER BY publisher.name, imprint.name";

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

sub GetAllByPublisherID {
    my ( $class, $publisherID ) = @_;
    assert($publisherID);

    my $sql =
        "SELECT book_product.publisher_id, publisher.name AS publisher_name, book_product.imprint_id, imprint.name AS imprint_name "
      . "FROM book_product "
      . "LEFT JOIN publisher USING (publisher_id) "
      . "LEFT JOIN imprint ON book_product.imprint_id = imprint.imprint_id "
      . "WHERE publisher_id = $publisherID "
      . "GROUP BY publisher.name, imprint.name";

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

1;
