#!/usr/bin/perl -w
#
# This is an example of a subclass of the generated application
#
# You can safely edit this file, any changes that you make will be preserved
# and this file will not be overwritten by the next run of Glade::PerlGenerate
#
#==============================================================================
#=== This is the 'Submain_window' class                              
#==============================================================================
package Submain_window;
require 5.000; use strict 'vars', 'refs', 'subs';
use CSL::Pg;
use DBI;
# UI class 'Submain_window' (version 0.01)
# 
# Copyright (c) Date Tue Oct 29 15:00:44 CST 2002
# Author Nathan Mueller <nmueller\@newton.cs.wisc.edu>
#
# Unspecified copying policy, please contact the author\n#  Nathan Mueller <nmueller\@newton.cs.wisc.edu>
#
#==============================================================================
# This perl source file was automatically generated by 
# Glade::PerlGenerate version 0.56 - Wed Apr 19 02:28:58 BST 2000
# Copyright (c) Author Dermot Musgrove <dermot.musgrove\@virgin.net>
#
# from Glade file /u/n/m/nmueller/work/ccdb/ccdbmark.glade
# Tue Oct 29 15:00:44 CST 2002
#==============================================================================

BEGIN {
    use vars qw( 
                 @ISA
                 %fields
                 $VERSION
             );
    # Existing signal handler modules
    use src::ccdbmark;
    # We need the Gnome bindings as well
    use Gnome;
    # Uncomment the line below to enable gettext checking
#    use Glade::PerlSource;
    # Tell interpreter who we are inheriting from
    @ISA     = qw( main_window );
    # Uncomment the line below to enable gettext checking
#    @ISA      = qw( main_window Glade::PerlSource );
    $VERSION = '0.01';
    # Inherit the AUTOLOAD dynamic methods from main_window
    *AUTOLOAD = \&main_window::AUTOLOAD;
} # End of sub BEGIN

%fields = (
# Insert any extra data access methods that you want to add to 
#   our inherited super-constructor (or overload)
    USERDATA    => undef,
    VERSION     => '0.01',
);

sub DESTROY {
    # This sub will be called on object destruction
} # End of sub DESTROY

#==============================================================================
#=== Below are the overloaded class constructors
#==============================================================================
sub new {
    my $that  = shift;
    # Allow indirect constructor so that we can call eg. 
    #   $window1 = BusFrame->new; $window2 = $window1->new;
    my $class = ref($that) || $that;

    # Call our super-class constructor to get an object and reconsecrate it
    my $self = bless $that->SUPER::new(), $class;

    # Add our own data access methods to the inherited constructor
    my($element);
    foreach $element (keys %fields) {
        $self->{_permitted_fields}->{$element} = $fields{$element};
    }
    @{$self}{keys %fields} = values %fields;
    return $self;
} # End of sub new

sub run {
    my ($class) = @_;
    Gnome->init('ccdbmark', '0.01');
    # Uncomment the line below to enable gettext checking
#    $class->check_gettext_strings;
    my $window = $class->new;
    # Insert your subclass user data key/value pairs 
    $window->USERDATA({
#        'Key1'   => 'Value1',
#        'Key2'   => 'Value2',
#        'Key3'   => 'Value3',
    });
    $window->TOPLEVEL->show;
#    my $window2 = $window->new;
#    $window2->TOPLEVEL->show;

    # Set up signal handlers
    $window->{FORM}->{order_view_item0}->signal_connect('activate',
							\&on_order_view_clicked, '', 'order_view_mode', 'main_window-1');
    $window->{FORM}->{order_view_item1}->signal_connect('activate',
							\&on_order_view_clicked, '', 'order_view_mode', 'main_window-1');
    
    # Connect to the database
    $window->{FORM}->{dbh} = DBI->connect("dbi:Pg:dbname=csdb;host=onnophris;port=49170");

    # Some stuff that can't be done in glade
    $window->{FORM}->{order_list}->set_column_justification(2, "right");

    # Load incomplete orders
    $window->{FORM}->{sort} = "number";
    $window->{FORM}->{type} = "incomplete";
    &load_orders($window->{FORM});

    Gtk->main;
    # Uncomment the line below to enable gettext checking
#    $window->write_gettext_strings("__", '/afs/cs.wisc.edu/u/n/m/nmueller/work/ccdb/src/ccdbmark.pot');
    return $window;
} # End of sub run
#===============================================================================
#=== Below are (overloaded) default signal handlers for 'main_window' class 
#===============================================================================
sub about_Form {
    my ($class) = @_;
    my $gtkversion = 
        Gtk->major_version.".".
        Gtk->minor_version.".".
        Gtk->micro_version;
    my $name = $0;
    #
    # Create a Gnome::About '$ab'
    my $ab = new Gnome::About(
        "Subccdbmark", 
        "0.01", 
        _("Copyright")." Tue Oct 29 15:00:44 CST 2002", 
        "Nathan Mueller <nmueller\@newton.cs.wisc.edu>", 
        _("No description")."\n".
        "Gtk ".     _("version").": $gtkversion\n".
        "Gtk-Perl "._("version").": $Gtk::VERSION\n".
        _("run from file").": $name\n".
        "# Unspecified copying policy, please contact the author\n# ", 
        "$Glade::PerlRun::pixmaps_directory/glade2perl_logo.xpm", 
    );
    $ab->set_title(_("About")." Subccdbmark" );
    $ab->position('mouse' );
    $ab->set_policy(1, 1, 0 );
    $ab->set_modal(1 );
    $ab->show;
} # End of sub about_Form

sub destroy_Form {
    my ($class, $data, $object, $instance) = @_;
    Gtk->main_quit; 
} # End of sub destroy_Form

sub toplevel_hide    { shift->get_toplevel->hide    }
sub toplevel_close   { shift->get_toplevel->close   }
sub toplevel_destroy { shift->get_toplevel->destroy }


#==============================================================================
#=== Below are (overloaded) signal handlers for 'main_window' class 
#==============================================================================
sub on_order_list_click_column {
    my ($class, $data, $object, $instance, $event) = @_;
    my $me = __PACKAGE__."->on_order_list_click_column";
    # Get ref to hash of all widgets on our form
    my $form = $__PACKAGE__::all_forms->{$instance};

    $form->{sort} = qw(number value cost)[$event];
    &load_orders($form);

} # End of sub on_order_list_click_column

sub on_order_list_select_row {
    my ($class, $data, $object, $instance, $event) = @_;
    my $me = __PACKAGE__."->on_order_list_select_row";
    # Get ref to hash of all widgets on our form
    my $form = $__PACKAGE__::all_forms->{$instance};

    # Toggle the pvs_button
    my $number = &get_current_order($form);
    my $query = $form->{dbh}->prepare("select * from req_requests,
    req_attributes where number = ? and master = 't' and
    req_attributes.request = req_requests.request and
    req_attributes.attribute = 'Purchase Checked'");
    $query->execute($number);
    if ($query->fetchrow)
    {
	$form->{pvs_button_label}->set_text("PVS Incomplete");
    }
    else
    {
	$form->{pvs_button_label}->set_text("PVS Complete");
    }
    
    $query->finish;

} # End of sub on_order_list_select_row

sub on_order_view_clicked {
    my ($class, $data, $object, $instance, $event) = @_;
    my $me = __PACKAGE__."->on_order_view_clicked";
    # Get ref to hash of all widgets on our form
    my $form = $__PACKAGE__::all_forms->{$instance};

    if ($form->{order_view}->get_menu->get_active ==
    $form->{order_view_item0})
    {
	$form->{type} = "incomplete";
    }
    else
    {
	$form->{type} = "complete";
    }

    &load_orders($form);

} # End of sub on_order_view_clicked

sub on_pvs_button_clicked {
    my ($class, $data, $object, $instance, $event) = @_;
    my $me = __PACKAGE__."->on_pvs_button_clicked";
    # Get ref to hash of all widgets on our form
    my $form = $__PACKAGE__::all_forms->{$instance};

    if ($form->{pvs_button_label}->get eq "PVS Complete")
    {
	my $query = $form->{dbh}->prepare("insert into req_attributes select request,
'Purchase Checked', 'Yes' from req_requests where number = ? and
master = 't'");
	$query->execute(&get_current_order($form));
    }
    else
    {
	my $query = $form->{dbh}->prepare("delete from req_attributes where request =
(select request from req_requests where number = ? and master = 't')
    and attribute = 'Purchase Checked'");
	$query->execute(&get_current_order($form));
    }

    $form->{order_list}->remove($form->{order_list}->selection);

} # End of sub on_pvs_button_clicked

sub on_quit_button_clicked {
    my ($class, $data, $object, $instance, $event) = @_;
    my $me = __PACKAGE__."->on_quit_button_clicked";
    # Get ref to hash of all widgets on our form
    my $form = $__PACKAGE__::all_forms->{$instance};

    Gtk->exit(0);

} # End of sub on_quit_button_clicked

sub get_current_order
{
    my ($form) = @_;

    my $row = $form->{order_list}->selection;
    return $form->{order_list}->get_text($row, 0);
}

sub load_orders
{
    my ($form) = @_;

    my $query;
    if ($form->{type} eq "complete")
    {
	$query = "select req_requests.number, v.value, sum(cost*quantity) as
cost, req_requests.request from req_requests, ccdb_parts, req_attributes v,
req_attributes m where req_requests.request = v.request and
v.attribute = 'Purchase Req' and ccdb_parts.purchase =
req_requests.request and m.request = v.request and m.attribute =
'Purchase Checked' group by req_requests.number, req_requests.request, v.value";
    }
    else
    {
	$query = "select req_requests.number, v.value,
    sum(cost*quantity) as
cost, req_requests.request from req_requests, ccdb_parts, req_attributes v where req_requests.request = v.request and
v.attribute = 'Purchase Req' and ccdb_parts.purchase =
req_requests.request and number not in (select number from
    req_requests, req_attributes where req_requests.request =
    req_attributes.request and attribute = 'Purchase Checked') group by req_requests.number, req_requests.request, v.value";
    }

    $query .= " order by $form->{sort}";

    my $fund_query = $form->{dbh}->prepare("select fund, amount from
    ccdb_funds where purchase = ?");
    $query = $form->{dbh}->prepare($query);
    $query->execute;

    $form->{order_list}->freeze;
    $form->{order_list}->clear;
    while (my $row = $query->fetchrow_arrayref)
    {
	my $node = $form->{order_list}->insert_node(undef, undef, 
						    [$row->[0],
						     $row->[1], 
						     "\$$row->[2]"], 
						    0, undef, undef,
						    undef, undef, 0, 0);
	$fund_query->execute($row->[3]);
	while ($row = $fund_query->fetchrow_arrayref)
	{
	    $form->{order_list}->insert_node($node, undef, 
					     [undef,
					      $row->[0],
					      "\$$row->[1]"], 
					     0, undef, undef, 
					     undef, undef, 1, 0);
	}
    }
    $form->{order_list}->thaw;
}









1;

__END__

#===============================================================================
#==== Documentation
#===============================================================================
=pod

=head1 NAME

Subccdbmark - version 0.01 Tue Oct 29 15:00:44 CST 2002

No description

=head1 SYNOPSIS

 use Subccdbmark;

 if ($we_want_to_subclass_this_class) {
   # Inherit the AUTOLOAD dynamic methods from Submain_window
   *AUTOLOAD = \&Submain_window::AUTOLOAD;

   # Tell interpreter who we are inheriting from
   use vars qw( @ISA ); @ISA = qw( Submain_window );
 }
 
 To construct the window object and show it call
 
 Gtk->init;
 my $window = Submain_window->new;
 $window->TOPLEVEL->show;
 Gtk->main;
 
 OR use the shorthand for the above calls
 
 Submain_window->run;

=head1 DESCRIPTION

Unfortunately, the author has not yet written any documentation :-(

=head1 AUTHOR

Nathan Mueller <nmueller\@newton.cs.wisc.edu>

=cut
