partkeepr

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

AddRemoveStockWindow.js (5147B)


      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.Components.Part.AddRemoveStockWindow', {
      6     extend: 'Ext.window.Window',
      7 
      8     // Configurations
      9     constrainHeader: true,
     10     width: 600,
     11     overflowY: 'auto',
     12     resizable: false,
     13 
     14     // We set the title later
     15     title: i18n("Add/Remove Stock"),
     16 
     17     layout: 'fit',
     18     bodyStyle: {
     19         padding: "5px"
     20     },
     21 
     22     record: null,
     23 
     24     /*
     25      * Initializes the window with the quantity and price fields. The price field is hidden when a stock decrease
     26      * happens.
     27      */
     28     initComponent: function ()
     29     {
     30 
     31         this.quantityField = Ext.create("Ext.form.field.Number", {
     32             value: 0, // The initial value is 0, to indicate that this is a number field
     33             //minValue: 1, // The minimum value is 1. That way we force the user to enter a value
     34             width: 100,
     35             listeners: {
     36                 specialkey: {
     37                     fn: function (field, e)
     38                     {
     39                         if (e.getKey() == e.ENTER) {
     40                             this.onOKClick();
     41                         }
     42                     },
     43                     scope: this
     44                 }
     45             }
     46         });
     47 
     48         this.commentField = Ext.create("Ext.form.field.Text", {
     49             anchor: '100%',
     50             fieldLabel: i18n("Comment"),
     51             maxLength: 255,
     52             enforceMaxLength: true,
     53             listeners: {
     54                 specialkey: {
     55                     fn: function (field, e)
     56                     {
     57                         if (e.getKey() == e.ENTER) {
     58                             this.onOKClick();
     59                         }
     60                     },
     61                     scope: this
     62                 }
     63             }
     64         });
     65 
     66         this.infoGrid = Ext.create("PartKeepr.Components.Part.PartInfoGrid", {
     67             mode: 'short',
     68             padding: {
     69                 bottom: "5px"
     70             }
     71         });
     72 
     73         this.infoGrid.applyFromPart(this.record);
     74 
     75         this.form = Ext.create("Ext.form.Panel", {
     76             border: false,
     77             bodyStyle: 'background-color: transparent',
     78             items: [
     79                 this.infoGrid,
     80                 {
     81                     xtype: 'fieldcontainer',
     82                     fieldLabel: i18n("Quantity"),
     83                     layout: 'hbox',
     84                     items: [
     85                         this.quantityField, {
     86                             width: 75,
     87                             xtype: 'displayfield',
     88                             margin: "0 0 0 5",
     89                             value: this.partUnitName
     90                         }
     91                     ]
     92                 }, this.commentField
     93             ]
     94         });
     95 
     96         this.items = this.form;
     97 
     98         this.addButton = Ext.create("Ext.button.Button", {
     99             text: i18n("Add"),
    100             iconCls: 'web-icon brick_add',
    101             handler: this.onAddClick,
    102             scope: this
    103         });
    104 
    105         this.removeButton = Ext.create("Ext.button.Button", {
    106             text: i18n("Remove"),
    107             iconCls: 'web-icon brick_delete',
    108             handler: this.onRemoveClick,
    109             scope: this
    110         });
    111 
    112         this.buttons = [
    113             {
    114                 text: i18n("Close"),
    115                 handler: this.onCloseClick,
    116                 iconCls: "web-icon cancel",
    117                 scope: this
    118             }, this.addButton, this.removeButton
    119         ];
    120         this.on("show", function ()
    121         {
    122             this.quantityField.focus();
    123             this.quantityField.selectText(0);
    124         }, this, {
    125             delay: 100
    126         });
    127         this.callParent();
    128     },
    129     /**
    130      * Closes the window
    131      */
    132     onCloseClick: function ()
    133     {
    134         this.close();
    135     },
    136     onAddClick: function ()
    137     {
    138         if (this.form.getForm().isValid()) {
    139             var qty = Math.abs(this.quantityField.getValue());
    140 
    141             this.record.callPutAction("addStock", {
    142             quantity: qty,
    143             comment: this.commentField.getValue()
    144         }, null, true);
    145         this.close();
    146         }
    147 
    148 
    149     },
    150     onRemoveClick: function ()
    151     {
    152         if (this.form.getForm().isValid()) {
    153             var qty = Math.abs(this.quantityField.getValue());
    154 
    155             this.record.callPutAction("removeStock", {
    156             quantity: qty,
    157             comment: this.commentField.getValue()
    158         }, null, true);
    159         this.close();
    160         }
    161 
    162 
    163     },
    164     /**
    165      * Checks if the form is valid. If yes, execute the callback.
    166      */
    167     onOKClick: function ()
    168     {
    169         if (this.form.getForm().isValid()) {
    170             var qty = this.quantityField.getValue();
    171 
    172             if (qty < 0) {
    173                 this.onRemoveClick();
    174             } else {
    175                 if (qty > 0) {
    176                     this.onAddClick();
    177                 } else {
    178                     Ext.Msg.alert(i18n("Invalid quantity"), i18n("The quantity must be not equal to 0"));
    179                 }
    180             }
    181         }
    182     }
    183 });