#!/usr/local/qddb/bin/qwish -f
#
# inventory.tcl --
#     This module handles the Inventory window and transactions
#     to the Inventory relation.
#

set tcl_precision 17
set ISPUTIL /var/lib/ISPutil
lappend auto_path . $qddb_library/fx $blt_library $blt_library/dd_protocols \
	$ISPUTIL/bin/library

#
# package_inventory --
#     Routine for packaging inventory info to drop on the invoice window
#
proc package_inventory {text win} {
    global gv_myattr
    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(Mfr): $gv_myattr(PartNumber)"
    set rv [list $gv_myattr(PartNumber)]
    set tmp [concat $gv_myattr(Mfr) $gv_myattr(Description)]
    lappend rv $tmp
    lappend rv $gv_myattr(Price.SuggestedRetail)
    lappend rv $gv_myattr(QtyOnHand)
}

#
# Inventory Class --
#     Paint the Inventory window and handle events upon the Inventory relation
#     Most of the work is built in via the Qddb Fx library :-)
#             
itcl_class Inventory {
    constructor {config} {
        global DragDrop fx_config_dir
        wm title . $title
        wm withdraw .
        if {[catch "qddb_schema open Inventory" s] != 0} {
            tk_dialog .dialog "Inventory" "Unable to open Schema" info 0 Ok
        }
        set fx_config_dir .inv_config
        Fx:Init $s
        Fx_Menubar menubar -w .mb -schema $s -array gv_myattr \
		-config_dir .inv_config \
                -afteraddmode {wm title . "Inventory: <Add Mode>"} \
                -aftersearchmode  {wm title . "Inventory: <Search Mode>"} \
                -afterchangemode  [list $this afterchangemode] \
                -restrict [glob ~]/.qba_config/Inventory
        set gv_search_entry [menubar SearchForEntry]
        frame .f1 -relief groove -bd 4
        frame .f1.mp
        frame .f1.d 
        frame .f1.cs
        frame .f1.qh
        frame .f1.qr
        Fx_Entry mfr  -w .f1.mp.mfr  -attr Mfr -setschema $s -mandatory 1 \
                -searchfor_entry $gv_search_entry \
		-restrict [glob ~]/.qba_config/Inventory
        Fx_Entry part -w .f1.mp.part -attr PartNumber -mandatory 1
        Fx_Entry desc -w .f1.d.desc  -attr Description
        Fx_Entry cref -w .f1.cs.cref -attr CrossReference
        Fx_Entry qoh  -w .f1.qh.qh   -attr QtyOnHand
        Fx_Entry qbo  -w .f1.qh.qb   -attr QtyBackOrdered
        Fx_Entry qoo  -w .f1.qr.qo   -attr QtyOnOrder
        Fx_Entry qr   -w .f1.qr.qr   -attr QtyReorder
        pack forget .f1.mp.mfr .f1.mp.part .f1.description .f1.cs.cref \
                .f1.qh.qh .f1.qh.qb .f1.qr.qo .f1.qr.qr
        pack .f1                                 -side top -fill x -expand yes
        pack .f1.mp .f1.d .f1.cs .f1.qh .f1.qr   -side top -fill x -expand yes
        pack .f1.mp.mfr                          -side left -fill x -expand yes
        pack .f1.mp.part                         -side left
        pack .f1.d.desc                          -side left
        pack .f1.cs.cref                         -side left
        pack .f1.qh.qh .f1.qh.qb -side left
        pack .f1.qr.qo .f1.qr.qr -side left
        .f1.mp.mfr.f_0.l  configure -text "Manufacturer/Part#:" -width $lwidth
        .f1.mp.part.f_0.l configure -bitmap @$bitmapdir/bluedot.xpm
        .f1.d.desc.f_0.l  configure -text "Description:" -width $lwidth
        .f1.cs.cref.f_0.l configure -text "Cross Reference:" -width $lwidth
        .f1.qh.qh.f_0.l   configure -text "Qty on Hand:" -width $lwidth
        .f1.qh.qh.e       configure -width 7
        .f1.qh.qb.f_0.l   configure -text "Qty on BackOrder:" -width 18
        .f1.qh.qb.e       configure -width 7
        .f1.qr.qo.f_0.l   configure -text "Qty on Order:" -width $lwidth
        .f1.qr.qo.e       configure -width 7
        .f1.qr.qr.f_0.l   configure -text "Reorder Qty:" -width 18
        .f1.qr.qr.e       configure -width 7
        frame .f2 -relief groove -bd 4
        frame .f2.rd
        frame .f2.qc
        Fx_Frame price   -w .f2.rd.price   -attr Price
        Fx_Entry qtyprice -w .f2.rd.qty    -attr Price.QtyPrice
        Fx_Entry retail  -w .f2.rd.retail  -attr Price.SuggestedRetail
        Fx_Entry dealer  -w .f2.rd.dealer  -attr Price.SuggestedDealer
        Fx_Entry distnet -w .f2.rd.distnet -attr Price.DistributorNet
        Fx_Entry price_comment -w .f2.rd.price_comment \
                -attr Price.Comment
        Fx_Frame costs   -w .f2.qc.costs   -attr Costs
        Fx_Entry qty     -w .f2.qc.qty     -attr Costs.OrderQuantity
        Fx_Entry cost    -w .f2.qc.cost    -attr Costs.Cost
        Fx_Entry costs_comment -w .f2.qc.costs_comment \
                -attr Costs.Comment
        pack forget .f2.rd.price .f2.rd.qty .f2.rd.retail .f2.rd.dealer \
                .f2.rd.distnet .f2.rd.price_comment \
                .f2.qc.costs .f2.qc.qty .f2.qc.cost .f2.qc.costs_comment
        pack .f2  -side top -fill x -expand yes
        pack .f2.rd  -side top -fill x -expand yes
        pack .f2.qc -side top -fill x -expand yes
        pack .f2.rd.price .f2.rd.qty .f2.rd.retail .f2.rd.dealer \
                .f2.rd.distnet .f2.rd.price_comment -side left
        pack .f2.qc.costs .f2.qc.qty .f2.qc.cost .f2.qc.costs_comment -side left
        .f2.rd.price.f_0.l  configure \
		-text "Price(Qty/Retail/Dlr/DstNet/Note):" -width $lwidth
        .f2.rd.qty.f_0.l  configure -bitmap @$bitmapdir/bluedot.xpm 
        .f2.rd.retail.f_0.l  configure -bitmap @$bitmapdir/bluedot.xpm 
        .f2.rd.dealer.f_0.l  configure -bitmap @$bitmapdir/bluedot.xpm 
        .f2.rd.distnet.f_0.l configure -bitmap @$bitmapdir/bluedot.xpm 
        .f2.rd.price_comment.f_0.l configure -bitmap @$bitmapdir/bluedot.xpm 
        .f2.rd.qty.e configure -width 7
        .f2.rd.retail.e configure -width 7
        .f2.rd.dealer.e configure -width 7
        .f2.rd.distnet.e configure -width 7
        .f2.rd.price_comment.e configure -width 10
        .f2.qc.costs.f_0.l   configure -text "Costs(Qty/Cost/Note):" \
		-width $lwidth
        .f2.qc.qty.f_0.l     configure -bitmap @$bitmapdir/bluedot.xpm 
        .f2.qc.cost.f_0.l    configure -bitmap @$bitmapdir/bluedot.xpm 
        .f2.qc.costs_comment.f_0.l    configure -bitmap @$bitmapdir/bluedot.xpm 
        .f2.qc.qty.e         configure -width 7
        .f2.qc.cost.e        configure -width 7
        .f2.qc.costs_comment.e        configure -width 10
        frame .f3 -relief groove -bd 4
        Fx_Entry weight -w .f3.weight -attr Weight
        pack forget .f3.weight
        Fx_Entry comments  -w .f3.comments -attr Comments -type Text \
		-height 3 -side top
        pack .f3 -side top -fill x -expand yes
        label .dragme  -text "<<<  Drag Me  >>>" -relief sunken -bd 4 \
		-anchor c -bg bisque
        pack .dragme -side top -fill x -expand yes
        blt::drag&drop source .dragme config \
		-packagecmd {package_inventory inventory}
        blt::drag&drop source .dragme handler inventory dd_send_inventory
        blt::drag&drop errors [list $this dragDropErrorHandler]
        menubar configure -instances [Fx_Entry::GetInstances] \
                -frames [Fx_Frame::GetInstances]
        menubar SearchModeProc
        wm deiconify .
    }
    method afterchangemode {} {
        global gv_myattr
        wm title . "Inventory: $gv_myattr(Mfr) \ $gv_myattr(PartNumber)"
    }
    method dragDropErrorHandler { args } {
    }
    method config {config} {}
    public title "Inventory" {}
    public rootdir /usr/local/Qba/bin {}
    public bitmapdir /var/lib/ISPutil/bitmaps {}
    ##public fx_config_dir .inv_config {}
    public lwidth 34
}

Inventory inventory
