<?php
/*
  $Id: weight_classes_grid.php $
  TomatoCart Open Source Shopping Cart Solutions
  http://www.tomatocart.com

  Copyright (c) 2009 Wuxi Elootec Technology Co., Ltd

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License v2 (1991)
  as published by the Free Software Foundation.
*/
  
?>

Toc.weight_classes.WeightClassesGrid = function (config) {

  config = config || {};
  
  config.border = false;
  config.viewConfig = {emptyText: TocLanguage.gridNoRecords};
  
  config.ds = new Ext.data.Store({
    url: Toc.CONF.CONN_URL,
    baseParams: {
      module: 'weight_classes',
      action: 'list_weight_classes'
    },
    reader: new Ext.data.JsonReader({
      root: Toc.CONF.JSON_READER_ROOT,
      totalProperty: Toc.CONF.JSON_READER_TOTAL_PROPERTY,
      id: 'weight_class_id'
    }, [
      'weight_class_id',
      'weight_class_title',
      'weight_class_key'
    ]),
    autoLoad: true
  });
  
  var rowActions = new Ext.ux.grid.RowActions({
    actions: [
      {iconCls: 'icon-edit-record', qtip: TocLanguage.tipEdit},
      {iconCls: 'icon-delete-record', qtip: TocLanguage.tipDelete}
    ],
    widthIntercept: Ext.isSafari ? 4: 2
  });
  rowActions.on('action', this.onRowAction, this);
  config.plugins = rowActions;
  
  config.sm = new Ext.grid.CheckboxSelectionModel();
  config.cm = new Ext.grid.ColumnModel([
    config.sm,
    { id: 'weight_class_title', header: '<?php echo $osC_Language->get("table_heading_weight_classes"); ?>', dataIndex: 'weight_class_title'}, 
    { header: '<?php echo $osC_Language->get("table_heading_unit"); ?>', dataIndex: 'weight_class_key', align: 'center'}, 
    rowActions
  ]);
  config.autoExpandColumn = 'weight_class_title';
  
  config.tbar = [
    {
      text: TocLanguage.btnAdd,
      iconCls: 'add',
      handler: this.onAdd,
      scope: this
    }, 
    '-', 
    {
      text: TocLanguage.btnDelete,
      iconCls: 'remove',
      handler: this.onBatchDelete,
      scope: this
    }, 
    '-', 
    {
      text: TocLanguage.btnRefresh,
      iconCls: 'refresh',
      handler: this.onRefresh,
      scope: this
    }
  ];
  
  var thisObj = this;
  config.bbar = new Ext.PageToolbar({
    pageSize: Toc.CONF.GRID_PAGE_SIZE,
    store: config.ds,
    steps: Toc.CONF.GRID_STEPS,
    btnsConfig:[
      {
        text: TocLanguage.btnAdd,
        iconCls:'add',
        handler: function() {
          thisObj.onAdd();
        }
      },
      {
        text: TocLanguage.btnDelete,
        iconCls:'remove',
        handler: function() {
          thisObj.onBatchDelete();
        }
      }
    ],
    beforePageText: TocLanguage.beforePageText,
    firstText: TocLanguage.firstText,
    lastText: TocLanguage.lastText,
    nextText: TocLanguage.nextText,
    prevText: TocLanguage.prevText,
    afterPageText: TocLanguage.afterPageText,
    refreshText: TocLanguage.refreshText,
    displayInfo: true,
    displayMsg: TocLanguage.displayMsg,
    emptyMsg: TocLanguage.emptyMsg,
    prevStepText: TocLanguage.prevStepText,
    nextStepText: TocLanguage.nextStepText
  });
  
  Toc.weight_classes.WeightClassesGrid.superclass.constructor.call(this, config);
};

Ext.extend(Toc.weight_classes.WeightClassesGrid, Ext.grid.GridPanel, {
  onAdd: function () {
    var dlg = this.owner.createWeightClassesDialog();

    dlg.on('saveSuccess', function() {
      this.onRefresh();
    }, this);

    dlg.show();
  },
  
  onEdit: function (record) {
    var dlg = this.owner.createWeightClassesDialog();
    dlg.setTitle('<?php echo $osC_Language->get("action_heading_new_weight_class"); ?>');

    dlg.on('saveSuccess', function() {
      this.onRefresh();
    }, this);
    
    dlg.show(record.get('weight_class_id'));
  },
  
  onDelete: function (record) {
    var weightClassId = record.get('weight_class_id');
    
    Ext.MessageBox.confirm(
      TocLanguage.msgWarningTitle, 
      TocLanguage.msgDeleteConfirm, 
      function (btn) {
        if (btn == 'yes') {
          Ext.Ajax.request({
            waitMsg: TocLanguage.formSubmitWaitMsg,
            url: Toc.CONF.CONN_URL,
            params: {
              module: 'weight_classes',
              action: 'delete_weight_class',
              weight_classes_id: weightClassId
            },
            callback: function (options, success, response) {
              var result = Ext.decode(response.responseText);
              
              if (result.success == true) {
                this.owner.app.showNotification({
                  title: TocLanguage.msgSuccessTitle,
                  html: result.feedback
               });
                
                this.onRefresh();
              } else {
                Ext.MessageBox.alert(TocLanguage.msgErrTitle, result.feedback);
              }
            },
            scope: this
          });
        }
      },
      this
    );
  },
  
  onBatchDelete: function () {
    var keys = this.getSelectionModel().selections.keys;
    
    if (keys.length > 0) {
      var batch = keys.join(',');
      
      Ext.MessageBox.confirm(
        TocLanguage.msgWarningTitle, 
        TocLanguage.msgDeleteConfirm, 
        function (btn) {
          if (btn == 'yes') {
            Ext.Ajax.request({
              waitMsg: TocLanguage.formSubmitWaitMsg,
              url: Toc.CONF.CONN_URL,
              params: {
                module: 'weight_classes',
                action: 'delete_weight_classes',
                batch: batch
              },
              callback: function (options, success, response) {
                var result = Ext.decode(response.responseText);
                
                if (result.success == true) {
                  this.owner.app.showNotification({
                    title: TocLanguage.msgSuccessTitle,
                    html: result.feedback
                  });
                  
                  this.onRefresh();
                } else {
                  Ext.MessageBox.alert(TocLanguage.msgWarningTitle, result.feedback);
                }
              },
              scope: this
            });
          }
        },
        this
      );
    } else {
      Ext.MessageBox.alert(TocLanguage.msgInfoTitle, TocLanguage.msgMustSelectOne);
    }
  },
  
  onRefresh: function () {
    this.getStore().reload();
  },
  
  onRowAction: function (grid, record, action, row, col) {
    switch (action) {
      case 'icon-delete-record':
        this.onDelete(record);
        break;
      case 'icon-edit-record':
        this.onEdit(record);
        break;
    }
  }
}
);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     <?php
/*
  $Id: tax_rates_dialog.php $
  TomatoCart Open Source Shopping Cart Solutions
  http://www.tomatocart.com

  Copyright (c) 2009 Wuxi Elootec Technology Co., Ltd

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License v2 (1991)
  as published by the Free Software Foundation.
*/

?>

Toc.tax_classes.TaxRatesDialog = function(config) {

  config = config || {}; 
  
  config.id = 'tax-rate-dialog-win';
  config.title = '<?php echo $osC_Language->get('action_heading_new_tax_rate'); ?>';
  config.width = 500;
  config.modal = true;
  config.items = this.buildForm();
  
  config.buttons = [
    {
      text: TocLanguage.btnSave,
      handler: function(){
        this.submitForm();
      },
      scope:this
    }, 
    {
      text: TocLanguage.btnClose,
      handler: function() {
        this.close();
      },
      scope:this
    }
  ];

  this.addEvents({'saveSuccess': true});  
  
  Toc.tax_classes.TaxRatesDialog.superclass.constructor.call(this, config);
}

Ext.extend(Toc.tax_classes.TaxRatesDialog, Ext.Window, {  

  show: function (taxClassId, ratesId) {
    this.taxClassId = taxClassId || null;
    var taxRatesId = ratesId || null; 
    
    this.frmTaxRate.form.reset();
    this.frmTaxRate.form.baseParams['tax_class_id'] = this.taxClassId;
    this.frmTaxRate.form.baseParams['tax_rates_id'] = taxRatesId;

    if (taxRatesId > 0) {
      this.frmTaxRate.load({
        url: Toc.CONF.CONN_URL,
        params: {
          module: 'tax_classes',
          action: 'load_tax_rate',
          tax_rates_id: taxRatesId
        },
        success: function(form, action) {
          Toc.tax_classes.TaxRatesDialog.superclass.show.call(this);
        },
        failure: function() {
          Ext.Msg.alert(TocLanguage.msgErrTitle, TocLanguage.msgErrLoadData)
        },
        scope: this       
      });
    } else {   
      Toc.tax_classes.TaxRatesDialog.superclass.show.call(this);
    }
  },
    
  buildForm: function() {
    var dsGeoZone = new Ext.data.Store({
      url: Toc.CONF.CONN_URL,
      baseParams: {
        module: 'tax_classes', 
        action: 'list_geo_zones'
      },
      reader: new Ext.data.JsonReader({
        root: Toc.CONF.JSON_READER_ROOT,
        fields: ['geo_zone_id', 'geo_zone_name']
      }),
      autoLoad: true                                                                                    
    });
    
    this.cobZoneGroup = new Ext.form.ComboBox({
      name: 'geo_zone',
      fieldLabel: '<?php echo $osC_Language->get('field_tax_rate_zone_group'); ?>', 
      store: dsGeoZone, 
      valueField: 'geo_zone_id', 
      displayField: 'geo_zone_name', 
      hiddenName: 'geo_zone_id', 
      editable: false, 
      triggerAction: 'all', 
      allowBlank: false
    });
    
    this.frmTaxRate = new Ext.form.FormPanel({ 
      url: Toc.CONF.CONN_URL,
      baseParams: {  
        module: 'tax_classes',
        action: 'save_tax_rate'
      }, 
      border: false,
      layoutConfig: {
        labelSeparator: ''
      },
      defaults: {
        anchor: '97%'
      },
      items: [
        this.cobZoneGroup,
        {xtype: 'numberfield', fieldLabel: '<?php echo $osC_Language->get('field_tax_rate'); ?>', name: 'tax_rate', decimalPrecision: 4, width:300},
        {xtype: 'textfield', fieldLabel: '<?php echo $osC_Language->get('field_tax_rate_description'); ?>', name: 'tax_description', width:300},
        {xtype: 'textfield', fieldLabel: '<?php echo $osC_Language->get('field_tax_rate_priority'); ?>', name: 'tax_priority', width:300}
      ]
    });
    
    return this.frmTaxRate;
  },

  submitForm: function() {
    this.frmTaxRate.form.submit({
      waitMsg: TocLanguage.formSubmitWaitMsg,
      success:function(form, action) {
        this.fireEvent('saveSuccess', action.result.feedback); 
        this.close();
      },
      failure: function(form, action) {
        if (action.failureType != 'client') {         
          Ext.MessageBox.alert(TocLanguage.msgErrTitle, action.result.feedback);      
        }         
      },
      scope: this
    });   
  }
});                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               