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

use strict;
use warnings;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use Common::Assert;
use BookPub::DB::Item::File;

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

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

sub CanUnmatch {
    my ( $class, $mapID ) = @_;
    assert($mapID);

    # We can unmatch if the map_id doesn't appear in a sale
    # from a closed file.
    #

    my $dbo = Common::RSApp::GetClientDB();
    my $sql =
        "SELECT count(*) as count FROM sale s, file f "
      . "WHERE s.map_id = ? AND s.file_id = f.file_id "
      . "AND f.file_status = ?";
    my @sqlParams = ($mapID, BookPub::DB::Item::File::STATUS_CLOSED());

    my $sth = $dbo->DoCmdWithPlaceholders($sql, \@sqlParams);
    return undef unless $sth;
    my $href = $sth->fetchrow_hashref();
    return undef if ( !defined $href || $href->{count} > 0 );

    return 1;
}


1;
