#!/usr/local/qddb/bin/qwish -f
#
# clients.tcl --
#
# This module manages clients.  There are two itcl classes: 
#
#     Customer ......... paints the client window and interacts with
#                        the "Clients" qddb database.
#     Customer_History.. paints the customer history window invoked
#                        from a menu pulldown on the client window.
#

#
# package_customer --
#     This routine collects the currently displayed customer
#     information into the return value used by the blt drag/drop
#     feature.  See the blt documentation for details on how this
#     works.  The customer information can be drag/dropped onto the
#     invoice window to populate the invoice's client information.
#


set ISPUTIL /var/lib/ISPutil

set tcl_precision 17
lappend auto_path . $qddb_library/fx $blt_library $ISPUTIL/bin/library


proc package_customer {text win} {
    global gv_myattr v
    set found_billing 0
    set found_shipping 0
    if {[winfo children $win] == ""} {
	label $win.label -text $text
	pack append $win $win.label top
    }
    $win.label config -background bisque -fg black -bd 6 \
	    -text "$gv_myattr(Company.Name)"
    if { $gv_myattr(Company.Address.Desc) == "Billing" } {
	set found_billing 1
	set b1 [list $gv_myattr(Company.Address.Street)] 
	set b2 [list $gv_myattr(Company.Address.City)]
	set b3 [list $gv_myattr(Company.Address.State)]
	set b4 [list $gv_myattr(Company.Address.Zip)]
    } elseif { $gv_myattr(Company.Address.Desc) == "Shipping" } {
	set found_shipping 1
	set s1 [list $gv_myattr(Company.Address.Street)]
	set s2 [list $gv_myattr(Company.Address.City)] 
	set s3 [list $gv_myattr(Company.Address.State)]
	set s4 [list $gv_myattr(Company.Address.Zip)]
    }
    set v [menubar info public view -value]
    set current_instance [qddb_instance current $v Company.Address]
    set c [qddb_instance maxnum $v Company.Address]
    for { set i 1 } { $i <= $c } { incr i } {
	qddb_instance switch $v Company.Address $i
	set r [qddb_view get $v]
	set desc [qddb_rows getval {Company.Address.Desc} $r]
	if { $desc == "Billing" && $found_billing == 0 } {
	    set found_billing 1
	    set b1 [qddb_rows getval Company.Address.Street $r]
	    set b2 [qddb_rows getval Company.Address.City $r]
	    set b3 [qddb_rows getval Company.Address.State $r]
	    set b4 [qddb_rows getval Company.Address.Zip $r]
	} elseif { $desc == "Shipping" } {
	    set found_shipping 1
	    set s1 [qddb_rows getval Company.Address.Street $r]
	    set s2 [qddb_rows getval Company.Address.City $r]
	    set s3 [qddb_rows getval Company.Address.State $r]
	    set s4 [qddb_rows getval Company.Address.Zip $r]
	}
    }
    qddb_instance switch $v Company.Address $current_instance
    if { $found_billing == 0 } {
	set b1 [list $gv_myattr(Company.Address.Street)] 
	set b2 [list $gv_myattr(Company.Address.City)]
	set b3 [list $gv_myattr(Company.Address.State)]
	set b4 [list $gv_myattr(Company.Address.Zip)]
    } 
    if { $found_shipping == 0 } {
	set s1 [list $gv_myattr(Company.Address.Street)]
	set s2 [list $gv_myattr(Company.Address.City)] 
	set s3 [list $gv_myattr(Company.Address.State)]
	set s4 [list $gv_myattr(Company.Address.Zip)]
    }
    set rv ""
    lappend rv $gv_myattr(Company.Name) \
	    $gv_myattr(Company.Contact.First) \
	    $gv_myattr(Company.Contact.Last) \
	    $gv_myattr(Company.Contact.ID) \
	    $gv_myattr(Company.Contact.Username) \
	    $gv_myattr(Company.Contact.StartDate) \
	    $gv_myattr(Company.Contact.Months) \
	    $gv_myattr(Company.Contact.Hours) \
	    $gv_myattr(Company.Contact.WebPages) \
	    $gv_myattr(Company.Contact.MonthlyRate) \
	    $gv_myattr(Company.Contact.Phone.Desc) \
	    $gv_myattr(Company.Contact.Phone.Area) \
	    $gv_myattr(Company.Contact.Phone.Number) \
	    $b1 $b2 $b3 $b4 \
	    $s1 $s2 $s3 $s4 \
	    $gv_myattr(CustomerType) \
	    $gv_myattr(ResellerNumber) \
	    $gv_myattr(CreditCard) \
	    $gv_myattr(DateOfExp) \
	    $gv_myattr(ManualCreditHold) \
	    $gv_myattr(SalesRep)
}

#
# CustomerHistory Class --
#     Paint the customer history window to allow user to
#     view a customer's purchasing history.
#
itcl_class CustomerHistory {
    constructor {config} {
	set _w .${this}_customerHistory
	toplevel $_w
	wm title $_w "Customer History"
	set widths2 { 5 8 4 8 25 8 5 3 8 }
	set headings { Invoice Date Qty Part# Description Price Disc. \
		GST Total }
	set alignment { left left left left left right right right right}
	set separators { {} {} {} {} {} {} {} {} {} }
	set f $_w.f2
	frame $f
	pack $f -side bottom -fill x
	button $f.dismiss -text Dismiss -command [list $this dismiss]
	pack $f.dismiss -side left -padx 10m -pady 2m -ipadx 10m -ipady 2m
	Fx_MultiColumnListBox _search_results -w $_w.f -widths $widths2 \
		-headings $headings -numcols [llength $headings] \
		-align $alignment \
		-exportselection off \
		-separators $separators \
		-single_select off \
		-height 10
	$this buildList
	_search_results AppendRows $_res
	_search_results SortContents {}
	_search_results Format
	_search_results Display
	tkwait window $_w.f
	update idletasks                                    
    }
    method config {config} {}
    method buildList {} {
	global gv_myattr invoice_schema arr
	if {[catch "qddb_schema open Invoices" invoice_schema] != 0} {
	    puts "Cannot open Schema for Invoices, please contact HSD"; exit 1
	}
	set v [menubar info public view -value]
	set max [qddb_instance maxnum $v Orders]
	set _res {}
	for { set i 1 } { $i <= $max } { incr i } {
	    qddb_instance switch $v Orders [expr $i] 
	    set k [qddb_search $invoice_schema -prunebyattr InvoiceNumber \
		    word $gv_myattr(Orders)]
	    set k1 [qddb_keylist get $k]
	    set t [isUniqueTuple $k1 $invoice_schema]
	    if { $t != "" } {
		set v2 [qddb_view define $t {
		    { InvoiceNumber        arr(InvoiceNumber) }
		    { DateOfInvoice        arr(DateOfInvoice) }
		    { Items.Qty            arr(Items.Qty) }
		    { Items.Part           arr(Items.Part) }
		    { Items.Desc           arr(Items.Desc) }
		    { Items.UnitPrice      arr(Items.UnitPrice) }
		    { Items.DiscountRate   arr(Items.DiscountRate) }
		    { Items.Tax            arr(Items.Tax) }
		    { Items.Total          arr(Items.Total) }
		}]
		set max2 [qddb_instance maxnum $v2 Items]
		for { set j 1 } { $j <= $max2 } { incr j } {
		    qddb_instance switch $v2 Items [expr $j] 
		    lappend _res [list \
			    $arr(InvoiceNumber) \
			    $arr(DateOfInvoice) \
			    $arr(Items.Qty) \
			    $arr(Items.Part) \
			    $arr(Items.Desc) \
			    $arr(Items.UnitPrice) \
			    $arr(Items.DiscountRate) \
			    $arr(Items.Tax) \
			    $arr(Items.Total)]
		}
		qddb_keylist delete $k
		qddb_keylist delete $k1
		qddb_tuple delete $t
            }	    
        }
	qddb_schema close $invoice_schema
    }
    method dismiss {} {	
	_search_results delete ; destroy $_w ; $this delete
    }
    protected _w
    protected _res
    protected _search_results
}


#
# Customer Class --
#     Paint the main Customer window and handle the events.
# 
itcl_class Customer {
    constructor {config} {
	global fx_config_dir
	wm title . "Customer: <Search Mode>"
	wm withdraw .
	if {[catch "qddb_schema open Clients" s] != 0} {
	    puts "Cannot open Schema for Clients, please contact HSD"; exit 1
	}
	set fx_config_dir .client_config
	Fx:Init $s
	Fx_Menubar menubar -w .mb -schema $s -array gv_myattr \
		-config_dir .client_config \
		-beforesave       [list $this beforeSave] \
		-afteraddmode     [list $this afterAddMode] \
		-aftersearchmode  [list $this afterSearchMode] \
		-afterchangemode  [list $this afterChangeMode] \
		-afterreadonlymode  [list $this afterReadOnlyMode] \
	        -afterpost_view   [list $this afterpost_view] \
		-restrict [glob ~]/.qba_config/Customer
	.mb.view.menu add sep
	.mb.view.menu add command -label "Customer History" \
		-command [list $this history]
	set gv_search_entry [menubar SearchForEntry]
	frame .f1  -relief groove -bd 4
	frame .f1.a -relief groove -bd 2
	frame .f1.b -relief groove -bd 2
	frame .f1.a.l1 
	frame .f1.b.l2
	frame .f1.b.l3
	frame .f1.b.l4
	frame .f1.b.l5
	frame .f1.b.l6
	frame .f1.b.l7
 	Fx_Entry company     -w .f1.a.l1.company  -attr Company.Name \
	-setschema $s -searchfor_entry $gv_search_entry \
		-restrict [glob ~]/.qba_config/Customer
	Fx_Frame contact     -w .f1.b.l2.contact  -attr Company.Contact  \
		-setschema $s 
	Fx_Entry jobtitle    -w .f1.b.l3.jobtitle -attr Company.Contact.JobTitle
	Fx_Entry title       -w .f1.b.l4.title    -attr Company.Contact.Title \
		-type Radiobutton -default_values "Mr. Mrs. Ms. Miss Dr."
	Fx_Entry first       -w .f1.b.l5.first    -attr Company.Contact.First
	Fx_Entry last        -w .f1.b.l5.last     -attr Company.Contact.Last
	Fx_Entry id          -w .f1.b.l5.id       -attr Company.Contact.ID \
		-read_only 1
	Fx_Entry username   -w .f1.b.l6.username \
		-attr Company.Contact.Username
	Fx_Entry startdate   -w .f1.b.l6.startdate \
		-attr Company.Contact.StartDate
	Fx_Entry months   -w .f1.b.l6.months \
		-attr Company.Contact.Months
	Fx_Entry hours  -w .f1.b.l7.hours \
		-attr Company.Contact.Hours
	Fx_Entry webpages    -w .f1.b.l7.webpages \
		-attr Company.Contact.WebPages
	Fx_Entry monthlyrate -w .f1.b.l7.monthlyrate \
		-attr Company.Contact.MonthlyRate
	label .dragme  -text "<<<  Drag Me  >>>" -relief sunken -bd 4 \
		-anchor c -bg bisque
	pack .dragme -side bottom -fill x -expand yes
	pack [frame .f2] -side bottom -expand on -fill both
	tixNoteBook .f2.nb
	set w .f2.nb
	$w add addr -label "Addresses/Phones" -underline 0
	$w add info -label "Information" -underline 9
	$w add cred -label "Credit/Balance"   -underline 9
	$w add comment -label "Comments" -underline 5
	pack $w -padx 5m -side top -expand on -fill x
	set f1 [$w subwidget addr]

	pack [frame $f1.f1] -side top -expand on -fill x
	frame $f1.f1.c -relief groove -bd 2
	frame $f1.f1.c.l6a
	frame $f1.f1.c.l6b
	frame $f1.f1.c.l6
	frame $f1.f1.c.l7
	frame $f1.f1.c.l8

	Fx_Frame address -w $f1.f1.c.l6a.address -attr Company.Address
	Fx_Entry adesc   -w $f1.f1.c.l6b.adesc   -attr Company.Address.Desc
	Fx_Entry street  -w $f1.f1.c.l6.street   -attr Company.Address.Street
	Fx_Entry city    -w $f1.f1.c.l7.city     -attr Company.Address.City
	Fx_Entry state   -w $f1.f1.c.l7.state    -attr Company.Address.State
 	Fx_Entry zip     -w $f1.f1.c.l7.zip      -attr Company.Address.Zip
	Fx_Frame phone   -w $f1.f1.c.l8.phone    -attr Company.Contact.Phone
 	Fx_Entry desc   -w $f1.f1.c.l8.desc     -attr Company.Contact.Phone.Desc
 	Fx_Entry area   -w $f1.f1.c.l8.area     -attr Company.Contact.Phone.Area
 	Fx_Entry number -w $f1.f1.c.l8.number -attr Company.Contact.Phone.Number
	pack forget .f1.a.l1.company .f1.b.l2.contact .f1.b.l3.jobtitle \
		.f1.b.l4.title .f1.b.l5.first .f1.b.l5.last .f1.b.l5.id \
		.f1.b.16.username .f1.b.l6.startdate .f1.b.l6.months \
		.f1.b.l7.hours .f1.b.l7.webpages .f1.b.l7.monthlyrate \
		$f1.f1.c.l6a.address $f1.f1.c.l6b.adesc \
		$f1.f1.c.l6.street $f1.f1.c.l7.city $f1.f1.c.l7.state \
		$f1.f1.c.l7.zip \
		$f1.f1.c.l8.phone $f1.f1.c.l8.desc $f1.f1.c.l8.area \
		$f1.f1.c.l8.number
	pack .f1.a.l1 .f1.b.l2 .f1.b.l3 .f1.b.l4 .f1.b.l5 .f1.b.l6 \
		.f1.b.l7 \
		$f1.f1.c.l6a $f1.f1.c.l6b $f1.f1.c.l6 \
		$f1.f1.c.l7 $f1.f1.c.l8 \
		-side top -fill x -expand yes
	pack .f1 -side top -fill x -expand yes
	pack .f1.a .f1.b $f1.f1.c -side top -fill x -expand yes
	pack .f1.a.l1.company   -side top -fill x -expand yes
	pack .f1.b.l2.contact   -side top -fill x -expand yes
	pack .f1.b.l3.jobtitle  -side top -anchor w
	pack .f1.b.l4.title     -side top -anchor w
	pack .f1.b.l5.first     -side left
	pack .f1.b.l5.last      -side left -fill x -expand yes
	pack .f1.b.l5.id        -side left
	pack .f1.b.l6.username  -side left
	pack .f1.b.l6.startdate -side left
	pack .f1.b.l6.months -side left
	pack .f1.b.l7.hours -side left
	pack .f1.b.l7.webpages  -side left
	pack .f1.b.l7.monthlyrate -side left
	pack $f1.f1.c.l6a.address  -side left -fill x -expand yes
	pack $f1.f1.c.l6b.adesc     -side left -fill x -expand yes
	pack $f1.f1.c.l6.street    -side top -fill x -expand yes
	pack $f1.f1.c.l7.city      -side left -fill x -expand yes
	pack $f1.f1.c.l7.state     -side left
	pack $f1.f1.c.l7.zip       -side left
	pack $f1.f1.c.l8.phone     -side left
	pack $f1.f1.c.l8.desc      -side left -fill x -expand yes
	pack $f1.f1.c.l8.area      -side left
	pack $f1.f1.c.l8.number    -side left
	.f1.a.l1.company.f_0.l    configure -text "Company:"   -width $lwidth
	.f1.b.l3.jobtitle.f_0.l   configure -text "Position:"  -width $lwidth
	.f1.b.l4.title.f_0.l      configure -text "Title:"     -width $lwidth
	.f1.b.l5.first.f_0.l      configure -text "First/Last/ID:" \
		-width $lwidth
	.f1.b.l5.last.f_0.l       configure -bitmap @$bitmapdir/bluedot.xpm
	.f1.b.l5.id.f_0.l         configure -bitmap @$bitmapdir/bluedot.xpm
	.f1.b.l6.username.f_0.l   configure -text "Username:"  -width $lwidth
	.f1.b.l6.startdate.f_0.l  configure -text "Start:"
	.f1.b.l6.months.f_0.l     configure -text "Months:"
	.f1.b.l7.hours.f_0.l configure -text "Hours:"  -width $lwidth
	.f1.b.l7.webpages.f_0.l   configure -text "WebPages:"
	.f1.b.l7.monthlyrate.f_0.l   configure -text "Rate:"
	$f1.f1.c.l6b.adesc.f_0.l     configure -text "Description:" \
		-width $lwidth
	$f1.f1.c.l6.street.f_0.l     configure -text "Street:" -width $lwidth
	$f1.f1.c.l7.city.f_0.l       configure -text "City/State/Zip:" \
		-width $lwidth
	$f1.f1.c.l7.state.f_0.l      configure -bitmap @$bitmapdir/bluedot.xpm
	$f1.f1.c.l7.zip.f_0.l        configure -bitmap @$bitmapdir/bluedot.xpm
	$f1.f1.c.l8.phone.f_0.l      configure -text "Phone(Desc/Area/Number):"\
		-width $lwidth
	$f1.f1.c.l8.desc.f_0.l       configure -bitmap @$bitmapdir/bluedot.xpm
	$f1.f1.c.l8.area.f_0.l       configure -bitmap @$bitmapdir/bluedot.xpm
	$f1.f1.c.l8.number.f_0.l     configure -bitmap @$bitmapdir/bluedot.xpm
	$f1.f1.c.l8.area.e           configure -width 3
	$f1.f1.c.l8.number.e         configure -width 8
	$f1.f1.c.l7.zip.e            configure -width 10

	set f2 [$w subwidget info].f
	frame $f2 -relief groove -bd 4
	frame $f2.a -relief groove -bd 2
	frame $f2.b -relief groove -bd 2
	frame $f2.c -relief groove -bd 2
	frame $f2.a.l1 
	frame $f2.a.l2
	frame $f2.b.l4
	frame $f2.c.l5
 	Fx_Entry reseller     -w $f2.a.l1.reseller -attr ResellerNumber
 	Fx_Entry ch           -w $f2.a.l2.ch       -attr ManualCreditHold \
		-type Radiobutton -default_values {Yes No}
 	Fx_Entry ct           -w $f2.b.l4.ct       -attr CustomerType \
		-type Radiobutton \
		-default_values {COD "COD-Cash only" Net10 Net30 Staff Admin Daemon}
	Fx_Entry cclass       -w $f2.c.l5.cclass -attr CustomerClass \
		-type Radiobutton \
		-default_values {Not Web Internet Hardware \
			Fullservice Knotwork Other}
	pack forget $f2.a.l1.reseller $f2.a.l2.ch $f2.b.l4.ct $f2.c.l5.cclass \
		-anchor w
	pack $f2 $f2.a $f2.b $f2.c -side top -fill x -expand yes
	pack $f2.a.l1 -side left -anchor w
	pack $f2.a.l2 -side left -anchor w
	pack $f2.b.l4 -side left -anchor w
	pack $f2.c.l5 -side left -anchor w
	pack $f2.a.l1.reseller -side left -anchor w
	pack $f2.a.l2.ch -side left -anchor w
	pack $f2.b.l4.ct -side left -anchor w
	pack $f2.c.l5.cclass -side left -anchor w
	$f2.a.l1.reseller.f_0.l     configure -text "GST exempt #:" \
		-width $lwidth
	$f2.a.l2.ch.f_0.l           configure -text "Credit Hold:"  -width 15
	$f2.b.l4.ct.f_0.l           configure -text "Customer Type:" \
		-width $lwidth
	$f2.c.l5.cclass.f_0.l           configure -text "Customer Class:" \
		-width $lwidth
	set f4 [$w subwidget cred].f
	frame $f4 -relief groove -bd 4
	frame $f4.ss
	frame $f4.cc
	frame $f4.ba -bd 2
	frame $f4.lp -bd 2
	Fx_Entry ssn    -w $f4.ss.ssn       -attr SSN
	Fx_Entry cl     -w $f4.ss.cl        -attr CreditLimit
	Fx_Entry de     -w $f4.ss.de        -attr DateOfEntry -width 9
	Fx_Entry ccard  -w $f4.cc.ccard     -attr CreditCard
	Fx_Entry doe    -w $f4.cc.doe       -attr DateOfExp
	Fx_Entry rb     -w $f4.rb           -attr ReferredBy -type Radiobutton \
		-default_values { "Yellow Pages" "Word of Mouth" \
		"Promotion" "Other" }
	Fx_Entry sp     -w $f4.sp           -attr SalesRep
	Fx_Entry b0     -w $f4.ba.b0           -attr CurrentBalance -read_only 1
	Fx_Entry bre    -w $f4.ba.bre          -attr Recent         -read_only 1
	Fx_Entry b30    -w $f4.ba.b30          -attr Balance30      -read_only 1
	Fx_Entry b60    -w $f4.ba.b60          -attr Balance60      -read_only 1
	Fx_Entry b90    -w $f4.ba.b90          -attr Balance90      -read_only 1
	Fx_Entry lp     -w $f4.lp.lp           -attr LastPayment    -read_only 1
	pack forget $f4.ss.ssn $f4.ss.cl $f4.ss.de $f4.cc.ccard $f4.cc.doe \
		$f4.sp $f4.rb \
		$f4.ba.b0 $f4.ba.bre $f4.ba.b30 $f4.ba.b60 $f4.ba.b90 $f4.lp.lp
	pack $f4              -side top -fill x -expand yes
	pack $f4.ss           -side top -anchor w
	pack $f4.cc           -side top -anchor w
	pack $f4.ba           -side top -fill x -expand yes
	pack $f4.lp           -side top -fill x -expand yes
	pack $f4.rb           -side top -anchor w
	pack $f4.sp         -side top -fill x -expand yes
	pack $f4.ss.ssn       -side left
	pack $f4.ss.cl        -side left
	pack $f4.ss.de        -side left -anchor e
	pack $f4.cc.ccard -side left -anchor w
	pack $f4.cc.doe -side right
	pack $f4.ba.b0 -side left
	pack $f4.ba.bre -side left
	pack $f4.ba.b30 -side left
	pack $f4.ba.b60 -side left -padx 0.2i
	pack $f4.ba.b90 -side left
	$f4.ss.ssn.f_0.l      configure -text "Social Security #:"
	$f4.ss.ssn.e          configure -width 14
	$f4.ss.cl.f_0.l       configure -text "Credit Limit:"
	$f4.ss.cl.e           configure -width 10
	$f4.cc.ccard.f_0.l    configure -text "Credit Card #:" -width $lwidth
	$f4.cc.ccard.e        configure -width 20
	$f4.cc.doe.f_0.l      configure -text "Date of Expiration:" \
		-width $lwidth
	$f4.cc.doe.e          configure -width 8
	$f4.rb.f_0.l          configure -text "Referral:" -width $lwidth
	$f4.sp.f_0.l          configure -text "Sales Rep:" -width $lwidth
	$f4.ba.b0.f_0.l       configure -text "Total/Recent/30/60/90):" \
		-width $lwidth
	$f4.ba.bre.f_0.l      configure -bitmap @$bitmapdir/bluedot.xpm
	$f4.ba.b30.f_0.l      configure -bitmap @$bitmapdir/bluedot.xpm
	$f4.ba.b60.f_0.l      configure -bitmap @$bitmapdir/bluedot.xpm
	$f4.ba.b90.f_0.l      configure -bitmap @$bitmapdir/bluedot.xpm
	$f4.lp.lp.f_0.l       configure -text "Last Payment:" -width $lwidth
	$f4.ba.b0.e           configure -width 10
	$f4.ba.b30.e          configure -width 10
	$f4.ba.b60.e          configure -width 10
	$f4.ba.b90.e          configure -width 10

	#
	# Create the dummy Orders attr for read-only, used in displaying
	# the Customer History window(s)
	#
	Fx_Entry d1 -w .d1  -attr Orders                    -read_only 1
	Fx_Entry d2 -w .d2  -attr Transaction.Date          -read_only 1
	Fx_Entry d3 -w .d3  -attr Transaction.TType         -read_only 1
	Fx_Entry d4 -w .d4  -attr Transaction.InvoiceNumber -read_only 1
	Fx_Entry d5 -w .d5  -attr Transaction.Amount        -read_only 1
	pack forget .d1 .d2 .d3 .d4 .d5

	set f3 [$w subwidget comment].f
	frame $f3 -bd 4 -relief groove
	pack $f3 -side top -expand yes -fill both
	Fx_Entry comments -w $f3.comments -attr Comments  -type Text \
		-height 3 -side top
	pack $f3.comments -expand on -fill both
	blt::drag&drop source .dragme config \
		-packagecmd {package_customer customer}
	blt::drag&drop source .dragme handler inventory dd_send_customer
	menubar configure -instances [Fx_Entry::GetInstances] \
		-frames [Fx_Frame::GetInstances]
	menubar SearchModeProc
	wm deiconify .
    }
    method config {config} {}
    method beforeSave {} {
	global gv_myattr fx:add_modeval fx:mode_variable
	if {${fx:mode_variable} == ${fx:add_modeval}} {
	    ::set gv_myattr(Company.Contact.ID) [getClientNumber]
	}
    }
    method getClientNumber {} {
	global fx:status_variable gv_myattr gv_tmpattr
	set s [qddb_schema open Setup]
	set k [qddb_search $s -prunebyattr ClientID r .*]
	set t {}
	foreach i [qddb_keylist get $k] {
	    set t [qddb_tuple read $s $i]
	    if {[llength $t] > 0} {
		break
	    }
	}
	if {[string compare $t {}] == 0} {
	    set fx:status_variable \
		    "You must set up fields in the Setup screen first"
	    return
	}
	qddb_keylist delete $k
	while {[qddb_tuple lock $t] == 0} {
	    set fx:status_variable "Waiting for Setup screen to be closed...."
	    update idletasks
	    exec sleep 1
	    set fx:status_variable ""
	    update idletasks
	}
	set v [qddb_view define $t {
	    {ClientID gv_tmpattr(clid)}
	}]
        ::set gv_myattr(Company.Contact.ID) $gv_tmpattr(clid)
        uplevel \#0 [list incr gv_tmpattr(clid)]
	qddb_tuple write $t
	catch "qddb_tuple delete $t"
	qddb_schema close $s
	return $gv_tmpattr(clid)
    }
    method history {} {
	blt_busy hold .
	CustomerHistory ${this}_history
	blt_busy release .
    }
    method afterChangeMode {} {
	global gv_myattr
	wm title . "Customer: $gv_myattr(Company.Contact.First) \ $gv_myattr(Company.Contact.Last)"
    }
    method afterReadOnlyMode {} {
	global gv_myattr
	wm title . "Customer: $gv_myattr(Company.Contact.First) \ $gv_myattr(Company.Contact.Last)"
    }
    method afterSearchMode {} {
	global gv_myattr
	wm title . "Customer: <Search Mode>"
    }
    method afterAddMode {} {
	global gv_myattr
	wm title . "Customer: <Add Mode>"
    }
    method afterpost_view {} {
	global fx:mode_variable
	if { ${fx:mode_variable} == "Change Mode" || \
		${fx:mode_variable} == "Read-only Mode" } {
	    .mb.view.menu entryconfigure 2 -state normal
	} else {
	    .mb.view.menu entryconfigure 2 -state disabled
	}
    }
    #
    # For now, do nothing on error handling for drag-n-drop
    #
    method dragDropErrorHandler { args } {
    }
    public title "QBA: Client" {}
    public rootdir /var/lib/ISPutil/bin {}
    public bitmapdir /var/lib/ISPutil/bitmaps {}
    ## public fx_config_dir .client_config
    public lwidth 25 {}
}

Customer customer
