partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

PartStockWindow.js (6199B)


      1 /**
      2  * This class defines a window which is used to in- or decrease the stock level for a specific part. Logic and service
      3  * calls are not contained in this window, and need to be implemented from the caller.
      4  */
      5 Ext.define('PartKeepr.PartStockWindow', {
      6     extend: 'Ext.window.Window',
      7 
      8     // Configurations
      9     constrainHeader: true,
     10     width: 305,
     11 
     12     resizable: false,
     13 
     14     // We set the title later
     15     title: "",
     16 
     17     // Window title texts
     18     removePartText: i18n("Remove Part(s)"),
     19     addPartText: i18n("Add Part(s)"),
     20 
     21     layout: 'anchor',
     22     bodyStyle: {
     23         padding: "5px"
     24     },
     25 
     26     /*
     27      * Initializes the window with the quantity and price fields. The price field is hidden when a stock decrease
     28      * happens.
     29      */
     30     initComponent: function ()
     31     {
     32 
     33         this.infoGrid = Ext.create("PartKeepr.Components.Part.PartInfoGrid", {
     34             mode: 'short',
     35             padding: {
     36                 bottom: "5px"
     37             }
     38         });
     39 
     40         this.quantityField = Ext.create("Ext.form.field.Number", {
     41             value: 0, // The initial value is 0, to indicate that this is a number field
     42             minValue: 1, // The minimum value is 1. That way we force the user to enter a value
     43             width: 100,
     44             listeners: {
     45                 specialkey: {
     46                     fn: function (field, e)
     47                     {
     48                         if (e.getKey() == e.ENTER) {
     49                             this.onOKClick();
     50                         }
     51                     },
     52                     scope: this
     53                 }
     54             }
     55         });
     56 
     57         this.priceField = Ext.create("PartKeepr.CurrencyField", {
     58             anchor: '100%',
     59             value: 0,
     60             fieldLabel: i18n("Price"),
     61             listeners: {
     62                 specialkey: {
     63                     fn: function (field, e)
     64                     {
     65                         if (e.getKey() == e.ENTER) {
     66                             this.onOKClick();
     67                         }
     68                     },
     69                     scope: this
     70                 }
     71             }
     72         });
     73 
     74         this.priceCheckbox = Ext.create("Ext.form.field.Checkbox", {
     75             boxLabel: i18n("Price per item"),
     76             hideEmptyLabel: false,
     77             checked: true
     78         });
     79 
     80         this.commentField = Ext.create("Ext.form.field.Text", {
     81             anchor: '100%',
     82             fieldLabel: i18n("Comment"),
     83             maxLength: 255,
     84             enforceMaxLength: true,
     85             listeners: {
     86                 specialkey: {
     87                     fn: function (field, e)
     88                     {
     89                         if (e.getKey() == e.ENTER) {
     90                             this.onOKClick();
     91                         }
     92                     },
     93                     scope: this
     94                 }
     95             }
     96         });
     97 
     98         this.form = Ext.create("Ext.form.Panel", {
     99             border: false,
    100             bodyStyle: 'background-color: transparent',
    101             items: [
    102                 this.infoGrid,
    103                 {
    104                     xtype: 'fieldcontainer',
    105                     fieldLabel: i18n("Quantity"),
    106                     layout: 'hbox',
    107                     items: [
    108                         this.quantityField, {
    109                             width: 75,
    110                             xtype: 'displayfield',
    111                             margin: "0 0 0 5",
    112                             value: this.partUnitName
    113                         }
    114                     ]
    115                 }, this.priceField, this.priceCheckbox, this.commentField
    116             ]
    117         });
    118 
    119         this.items = this.form;
    120 
    121         this.okButton = Ext.create("Ext.button.Button", {
    122             text: i18n("OK"),
    123             handler: this.onOKClick,
    124             scope: this
    125         });
    126 
    127         this.buttons = [
    128             {
    129                 text: i18n("Close"),
    130                 handler: this.onCloseClick,
    131                 iconCls: "web-icon cancel",
    132                 scope: this
    133             }, this.okButton
    134         ];
    135         this.on("show", function ()
    136         {
    137             this.quantityField.focus();
    138             this.quantityField.selectText(0);
    139         }, this, {
    140             delay: 100
    141         });
    142         this.callParent();
    143     },
    144     /**
    145      * Closes the window
    146      */
    147     onCloseClick: function ()
    148     {
    149         this.close();
    150     },
    151     /**
    152      * Checks if the form is valid. If yes, execute the callback.
    153      */
    154     onOKClick: function ()
    155     {
    156         if (this.form.getForm().isValid()) {
    157             var price;
    158             if (this.priceCheckbox.getValue()) {
    159                 price = this.priceField.getValue();
    160             } else {
    161                 price = this.priceField.getValue() / this.quantityField.getValue();
    162             }
    163 
    164             Ext.callback(this.callbackFn,
    165                 this.callbackScope,
    166                 [this.quantityField.getValue(), price, this.commentField.getValue()]);
    167             this.close();
    168         }
    169     },
    170     /**
    171      * Opens the window in "add stock" mode. The target callback receives three parameters: the value of the quantity
    172      * field, the value of the price field and the value of the comment field.
    173      *
    174      * @param fn
    175      *            The callback
    176      * @param scope
    177      *            The scope in which to execute the callback
    178      */
    179     addStock: function (fn, scope)
    180     {
    181         this.callbackFn = fn;
    182         this.callbackScope = scope;
    183         this.setTitle(this.addPartText);
    184         this.okButton.setIconCls("web-icon brick_add");
    185         this.show();
    186     },
    187     /**
    188      * Opens the window in "remove stock" mode. The target callback receives one parameters: the value of the quantity
    189      * field
    190      *
    191      * @param fn
    192      *            The callback
    193      * @param scope
    194      *            The scope in which to execute the callback
    195      */
    196     removeStock: function (fn, scope)
    197     {
    198         this.callbackFn = fn;
    199         this.callbackScope = scope;
    200         this.setTitle(this.removePartText);
    201         this.priceField.hide();
    202         this.priceCheckbox.hide();
    203         this.okButton.setIconCls("web-icon brick_delete");
    204         this.quantityField.maxValue = this.callbackScope.record.data.stockLevel;
    205         this.show();
    206     }
    207 });