freento-groupautoswitch - Version 1.0.0

Version Notes


  • Automatic event based customer group auto switch

  • Switch group based on lifetime statistic

  • Switch group based on previous group switch date

  • Manual group switch

  • Sending email notifications on group change

  • Downgrade to some group based on customer activity

  • Progress bar based on lifetime statistic and current group purchases statistic

  • Text alert in custmer account for customer groups

  • Ability to see statistic of purchases in current group in customer account

  • Individual email welcome messages for customers

  • Ability to easily filter customers by group in native customer grid

  • Multistore support

  • 100% free extension

Download this release

Release Info

Developer Alex Boone
Extension freento-groupautoswitch
Version 1.0.0
Comparing to
See all releases


Version 1.0.0

Files changed (50) hide show
  1. app/code/community/Freento/GroupAutoSwitch/Block/Adminhtml/Customer/Group/Edit/Form.php +19 -0
  2. app/code/community/Freento/GroupAutoSwitch/Block/Adminhtml/Customer/Group/Edit/Form/Autoswitch.php +61 -0
  3. app/code/community/Freento/GroupAutoSwitch/Block/Adminhtml/Customer/Group/Edit/Form/Conditions.php +35 -0
  4. app/code/community/Freento/GroupAutoSwitch/Block/Adminhtml/Customer/Group/Edit/Form/Downgrade.php +63 -0
  5. app/code/community/Freento/GroupAutoSwitch/Block/Adminhtml/Customer/Group/Edit/Form/GroupSettings.php +30 -0
  6. app/code/community/Freento/GroupAutoSwitch/Block/Adminhtml/Customer/Group/Edit/Form/Main.php +60 -0
  7. app/code/community/Freento/GroupAutoSwitch/Block/Adminhtml/Customer/Group/Edit/Tabs.php +51 -0
  8. app/code/community/Freento/GroupAutoSwitch/Block/Adminhtml/Log.php +17 -0
  9. app/code/community/Freento/GroupAutoSwitch/Block/Adminhtml/Log/Grid.php +56 -0
  10. app/code/community/Freento/GroupAutoSwitch/Block/Adminhtml/System/Config/Form/ForceFill.php +33 -0
  11. app/code/community/Freento/GroupAutoSwitch/Exception/General.php +6 -0
  12. app/code/community/Freento/GroupAutoSwitch/Helper/Data.php +12 -0
  13. app/code/community/Freento/GroupAutoSwitch/Model/Cron.php +50 -0
  14. app/code/community/Freento/GroupAutoSwitch/Model/Group.php +268 -0
  15. app/code/community/Freento/GroupAutoSwitch/Model/Log.php +12 -0
  16. app/code/community/Freento/GroupAutoSwitch/Model/Observer.php +34 -0
  17. app/code/community/Freento/GroupAutoSwitch/Model/Resource/Group.php +9 -0
  18. app/code/community/Freento/GroupAutoSwitch/Model/Resource/Group/Collection.php +28 -0
  19. app/code/community/Freento/GroupAutoSwitch/Model/Resource/Log.php +17 -0
  20. app/code/community/Freento/GroupAutoSwitch/Model/Resource/Log/Collection.php +9 -0
  21. app/code/community/Freento/GroupAutoSwitch/Model/Rule/Condition/Combine.php +28 -0
  22. app/code/community/Freento/GroupAutoSwitch/Model/Rule/Condition/Customer/Attribute/Group.php +46 -0
  23. app/code/community/Freento/GroupAutoSwitch/Model/Rule/Condition/Customer/Statistic/Abstract.php +34 -0
  24. app/code/community/Freento/GroupAutoSwitch/Model/Rule/Condition/Customer/Statistic/CurrentGroupSales.php +40 -0
  25. app/code/community/Freento/GroupAutoSwitch/Model/Rule/Condition/Customer/Statistic/LifetimeSales.php +36 -0
  26. app/code/community/Freento/GroupAutoSwitch/Test/Model/Cron.php +42 -0
  27. app/code/community/Freento/GroupAutoSwitch/Test/Model/Cron/expectations/downgradeCheck.php +6 -0
  28. app/code/community/Freento/GroupAutoSwitch/Test/Model/Cron/expectations/forceFill.php +4 -0
  29. app/code/community/Freento/GroupAutoSwitch/Test/Model/Cron/fixtures/downgradeCheck.php +92 -0
  30. app/code/community/Freento/GroupAutoSwitch/Test/Model/Cron/fixtures/forceFill.php +79 -0
  31. app/code/community/Freento/GroupAutoSwitch/Test/Model/Cron/providers/downgradeCheck.php +6 -0
  32. app/code/community/Freento/GroupAutoSwitch/Test/Model/Cron/providers/forceFill.php +6 -0
  33. app/code/community/Freento/GroupAutoSwitch/Test/Model/Group.php +55 -0
  34. app/code/community/Freento/GroupAutoSwitch/Test/Model/Group/expectations/switchGroup.php +7 -0
  35. app/code/community/Freento/GroupAutoSwitch/Test/Model/Group/fixtures/switchGroup.php +70 -0
  36. app/code/community/Freento/GroupAutoSwitch/Test/Model/Group/providers/switchGroup.php +7 -0
  37. app/code/community/Freento/GroupAutoSwitch/controllers/Adminhtml/Customer/GroupController.php +135 -0
  38. app/code/community/Freento/GroupAutoSwitch/controllers/Adminhtml/LogController.php +22 -0
  39. app/code/community/Freento/GroupAutoSwitch/controllers/Adminhtml/System/ConfigController.php +30 -0
  40. app/code/community/Freento/GroupAutoSwitch/etc/adminhtml.xml +45 -0
  41. app/code/community/Freento/GroupAutoSwitch/etc/config.xml +152 -0
  42. app/code/community/Freento/GroupAutoSwitch/etc/system.xml +42 -0
  43. app/code/community/Freento/GroupAutoSwitch/sql/freento_groupautoswitch_setup/mysql4-install-1.0.0.php +60 -0
  44. app/design/adminhtml/default/default/layout/freento/groupautoswitch.xml +16 -0
  45. app/design/adminhtml/default/default/template/freento_groupautoswitch/system/config/forceFill.phtml +42 -0
  46. app/etc/modules/Freento_GroupAutoSwitch.xml +10 -0
  47. app/locale/en_US/template/email/freento/groupautoswitch/auto_switch.html +18 -0
  48. app/locale/en_US/template/email/freento/groupautoswitch/downgrade.html +18 -0
  49. app/locale/en_US/template/email/freento/groupautoswitch/downgrade_before.html +21 -0
  50. package.xml +46 -0
app/code/community/Freento/GroupAutoSwitch/Block/Adminhtml/Customer/Group/Edit/Form.php ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_GroupAutoSwitch_Block_Adminhtml_Customer_Group_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
4
+ {
5
+ protected function _prepareForm()
6
+ {
7
+ $form = new Varien_Data_Form(array(
8
+ 'id' => 'edit_form',
9
+ 'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
10
+ 'method' => 'post',
11
+ 'enctype' => 'multipart/form-data'
12
+ )
13
+ );
14
+
15
+ $form->setUseContainer(true);
16
+ $this->setForm($form);
17
+ return parent::_prepareForm();
18
+ }
19
+ }
app/code/community/Freento/GroupAutoSwitch/Block/Adminhtml/Customer/Group/Edit/Form/Autoswitch.php ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_GroupAutoSwitch_Block_Adminhtml_Customer_Group_Edit_Form_Autoswitch extends Mage_Adminhtml_Block_Widget_Form
4
+ {
5
+ protected function _prepareForm()
6
+ {
7
+ $customerGroup = Mage::registry('current_group');
8
+ $model = Mage::helper('freento_groupautoswitch')->getAutoSwitchInfo($customerGroup);
9
+ $form = new Varien_Data_Form();
10
+
11
+ $fieldset = $form->addFieldset('base_fieldset', array('legend'=>Mage::helper('freento_groupautoswitch')->__('Autoswitch Configuration')));
12
+
13
+ $fieldset->addField('is_enabled', 'select', array(
14
+ 'label' => Mage::helper('freento_groupautoswitch')->__('Enable'),
15
+ 'title' => Mage::helper('freento_groupautoswitch')->__('Enable'),
16
+ 'name' => 'is_enabled',
17
+ 'options' => array(
18
+ '1' => Mage::helper('freento_groupautoswitch')->__('Yes'),
19
+ '0' => Mage::helper('freento_groupautoswitch')->__('No'),
20
+ ),
21
+ ));
22
+
23
+ if (! Mage::app()->isSingleStoreMode()) {
24
+ $field = $fieldset->addField('store_id', 'select', [
25
+ 'name' => 'stores[]',
26
+ 'label' => $this->__('Store View'),
27
+ 'title' => $this->__('Store View'),
28
+ 'required' => true,
29
+ 'values' => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true),
30
+ ]);
31
+ $field->setRenderer($this->getLayout()->createBlock('adminhtml/store_switcher_form_renderer_fieldset_element'));
32
+ } else {
33
+ $fieldset->addField('store_id', 'hidden', [
34
+ 'name' => 'stores[]',
35
+ 'value' => Mage::app()->getStore(true)->getId()
36
+ ]);
37
+ }
38
+
39
+ $fieldset->addField('priority', 'text', array(
40
+ 'label' => Mage::helper('freento_groupautoswitch')->__('Rules Priority'),
41
+ 'title' => Mage::helper('freento_groupautoswitch')->__('Rules Priority'),
42
+ 'name' => 'priority',
43
+ ));
44
+
45
+ $fieldset->addField('stop_further_rules', 'select', array(
46
+ 'label' => Mage::helper('freento_groupautoswitch')->__('Stop Further Rules Processing'),
47
+ 'title' => Mage::helper('freento_groupautoswitch')->__('Stop Further Rules Processing'),
48
+ 'name' => 'stop_further_rules',
49
+ 'options' => array(
50
+ '1' => Mage::helper('freento_groupautoswitch')->__('Yes'),
51
+ '0' => Mage::helper('freento_groupautoswitch')->__('No'),
52
+ ),
53
+ ));
54
+
55
+ $form->setValues($model);
56
+
57
+ $this->setForm($form);
58
+
59
+ return parent::_prepareForm();
60
+ }
61
+ }
app/code/community/Freento/GroupAutoSwitch/Block/Adminhtml/Customer/Group/Edit/Form/Conditions.php ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_GroupAutoSwitch_Block_Adminhtml_Customer_Group_Edit_Form_Conditions extends Mage_Adminhtml_Block_Widget_Form
4
+ {
5
+ protected function _prepareForm()
6
+ {
7
+ $helper = Mage::helper('freento_groupautoswitch');
8
+ $customerGroup = Mage::registry('current_group');
9
+ $model = Mage::helper('freento_groupautoswitch')->getAutoSwitchInfo($customerGroup);
10
+
11
+ $form = new Varien_Data_Form();
12
+
13
+ $form->setHtmlIdPrefix('rule_');
14
+
15
+ $renderer = Mage::getBlockSingleton('adminhtml/widget_form_renderer_fieldset')
16
+ ->setTemplate('promo/fieldset.phtml')
17
+ ->setNewChildUrl($this->getUrl('*/*/newConditionHtml/form/rule_conditions_fieldset'));
18
+
19
+ $fieldset = $form->addFieldset('conditions_fieldset', array(
20
+ 'legend' => $helper->__('Conditions'))
21
+ )->setRenderer($renderer);
22
+
23
+ $fieldset->addField('conditions', 'text', array(
24
+ 'name' => 'conditions',
25
+ 'label' => $helper->__('Conditions'),
26
+ 'title' => $helper->__('Conditions'),
27
+ 'required' => true,
28
+ ))->setRule($model)->setRenderer(Mage::getBlockSingleton('rule/conditions'));
29
+
30
+ $form->setValues($model);
31
+
32
+ $this->setForm($form);
33
+ return parent::_prepareForm();
34
+ }
35
+ }
app/code/community/Freento/GroupAutoSwitch/Block/Adminhtml/Customer/Group/Edit/Form/Downgrade.php ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_GroupAutoSwitch_Block_Adminhtml_Customer_Group_Edit_Form_Downgrade extends Mage_Adminhtml_Block_Widget_Form
4
+ {
5
+ protected function _prepareForm()
6
+ {
7
+ $customerGroup = Mage::registry('current_group');
8
+ $model = Mage::helper('freento_groupautoswitch')->getAutoSwitchInfo($customerGroup);
9
+ $form = new Varien_Data_Form();
10
+
11
+ $fieldset = $form->addFieldset('base_fieldset', array('legend'=>Mage::helper('freento_groupautoswitch')->__('Downgrade')));
12
+
13
+ $fieldset->addField('downgrade_after', 'text', array(
14
+ 'label' => Mage::helper('freento_groupautoswitch')->__('Downgrade After (days without purchase)'),
15
+ 'title' => Mage::helper('freento_groupautoswitch')->__('Downgrade After (days without purchase)'),
16
+ 'name' => 'downgrade_after',
17
+ ));
18
+
19
+ $fieldset->addField('downgrade_group', 'select', array(
20
+ 'label' => Mage::helper('freento_groupautoswitch')->__('Downgrade to Group'),
21
+ 'title' => Mage::helper('freento_groupautoswitch')->__('Downgrade to Group'),
22
+ 'name' => 'downgrade_group',
23
+ 'values' => $this->_getAllCustomerGroupOptions()
24
+ ));
25
+
26
+ $fieldset->addField('downgrade_template', 'select', array(
27
+ 'label' => Mage::helper('freento_groupautoswitch')->__('Downgrade Email Template'),
28
+ 'title' => Mage::helper('freento_groupautoswitch')->__('Downgrade Email Template'),
29
+ 'name' => 'downgrade_template',
30
+ 'values' => Mage::getModel('adminhtml/system_config_source_email_template')->toOptionArray()
31
+ ));
32
+
33
+ $fieldset->addField('notify_customer_before_downgrade', 'text', array(
34
+ 'label' => Mage::helper('freento_groupautoswitch')->__('Notify Customer Before Downgrade (days)'),
35
+ 'title' => Mage::helper('freento_groupautoswitch')->__('Notify Customer Before Downgrade (days)'),
36
+ 'name' => 'notify_customer_before_downgrade',
37
+ ));
38
+
39
+ $fieldset->addField('notify_customer_before_downgrade_template', 'select', array(
40
+ 'label' => Mage::helper('freento_groupautoswitch')->__('Notify Customer Before Downgrade Template'),
41
+ 'title' => Mage::helper('freento_groupautoswitch')->__('Notify Customer Before Downgrade Template'),
42
+ 'name' => 'notify_customer_before_downgrade_template',
43
+ 'values' => Mage::getModel('adminhtml/system_config_source_email_template')->toOptionArray()
44
+ ));
45
+
46
+ $form->setValues($model);
47
+ $this->setForm($form);
48
+ return parent::_prepareForm();
49
+ }
50
+
51
+ protected function _getAllCustomerGroupOptions(){
52
+ $all = Mage::getModel('customer/group')->getCollection();
53
+ $options = array('-1' => 'No changes');
54
+ foreach($all as $group){
55
+ /* Skip Not logged in group */
56
+ if ($group->getId() == 0) {
57
+ continue;
58
+ }
59
+ $options[$group->getId()] = $group->getData('customer_group_code');
60
+ }
61
+ return $options;
62
+ }
63
+ }
app/code/community/Freento/GroupAutoSwitch/Block/Adminhtml/Customer/Group/Edit/Form/GroupSettings.php ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_GroupAutoSwitch_Block_Adminhtml_Customer_Group_Edit_Form_GroupSettings extends Mage_Adminhtml_Block_Widget_Form
4
+ {
5
+ protected function _prepareForm()
6
+ {
7
+ $customerGroup = Mage::registry('current_group');
8
+ $model = Mage::helper('freento_groupautoswitch')->getAutoSwitchInfo($customerGroup);
9
+ $form = new Varien_Data_Form();
10
+
11
+ $fieldset = $form->addFieldset('base_fieldset', array('legend'=>Mage::helper('freento_groupautoswitch')->__('Group Settings')));
12
+
13
+ $fieldset->addField('notify_on_group_change_template', 'select', array(
14
+ 'label' => Mage::helper('freento_groupautoswitch')->__('Notify Customer After Group Change Template'),
15
+ 'title' => Mage::helper('freento_groupautoswitch')->__('Notify Customer After Group Change Template'),
16
+ 'name' => 'notify_on_group_change_template',
17
+ 'values' => Mage::getModel('adminhtml/system_config_source_email_template')->toOptionArray()
18
+ ));
19
+
20
+ $fieldset->addField('text_for_customer', 'text', array(
21
+ 'label' => Mage::helper('freento_groupautoswitch')->__('Text for Customer'),
22
+ 'title' => Mage::helper('freento_groupautoswitch')->__('Text for Customer'),
23
+ 'name' => 'text_for_customer',
24
+ ));
25
+
26
+ $form->setValues($model);
27
+ $this->setForm($form);
28
+ return parent::_prepareForm();
29
+ }
30
+ }
app/code/community/Freento/GroupAutoSwitch/Block/Adminhtml/Customer/Group/Edit/Form/Main.php ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_GroupAutoSwitch_Block_Adminhtml_Customer_Group_Edit_Form_Main extends Mage_Adminhtml_Block_Widget_Form
4
+ {
5
+ protected function _prepareForm()
6
+ {
7
+ parent::_prepareLayout();
8
+ $form = new Varien_Data_Form();
9
+ $customerGroup = Mage::registry('current_group');
10
+
11
+ $fieldset = $form->addFieldset('base_fieldset', array('legend'=>Mage::helper('customer')->__('Group Information')));
12
+
13
+ $validateClass = sprintf('required-entry validate-length maximum-length-%d',
14
+ Mage_Customer_Model_Group::GROUP_CODE_MAX_LENGTH);
15
+ $name = $fieldset->addField('customer_group_code', 'text',
16
+ array(
17
+ 'name' => 'code',
18
+ 'label' => Mage::helper('customer')->__('Group Name'),
19
+ 'title' => Mage::helper('customer')->__('Group Name'),
20
+ 'note' => Mage::helper('customer')->__('Maximum length must be less then %s symbols', Mage_Customer_Model_Group::GROUP_CODE_MAX_LENGTH),
21
+ 'class' => $validateClass,
22
+ 'required' => true,
23
+ )
24
+ );
25
+
26
+ if ($customerGroup->getId()==0 && $customerGroup->getCustomerGroupCode() ) {
27
+ $name->setDisabled(true);
28
+ }
29
+
30
+ $fieldset->addField('tax_class_id', 'select',
31
+ array(
32
+ 'name' => 'tax_class',
33
+ 'label' => Mage::helper('customer')->__('Tax Class'),
34
+ 'title' => Mage::helper('customer')->__('Tax Class'),
35
+ 'class' => 'required-entry',
36
+ 'required' => true,
37
+ 'values' => Mage::getSingleton('tax/class_source_customer')->toOptionArray()
38
+ )
39
+ );
40
+
41
+ if (!is_null($customerGroup->getId())) {
42
+ // If edit add id
43
+ $form->addField('id', 'hidden',
44
+ array(
45
+ 'name' => 'id',
46
+ 'value' => $customerGroup->getId(),
47
+ )
48
+ );
49
+ }
50
+
51
+ if( Mage::getSingleton('adminhtml/session')->getCustomerGroupData() ) {
52
+ $form->addValues(Mage::getSingleton('adminhtml/session')->getCustomerGroupData());
53
+ Mage::getSingleton('adminhtml/session')->setCustomerGroupData(null);
54
+ } else {
55
+ $form->addValues($customerGroup->getData());
56
+ }
57
+
58
+ $this->setForm($form);
59
+ }
60
+ }
app/code/community/Freento/GroupAutoSwitch/Block/Adminhtml/Customer/Group/Edit/Tabs.php ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_Groupautoswitch_Block_Adminhtml_Customer_Group_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
4
+ {
5
+ public function __construct()
6
+ {
7
+ parent::__construct();
8
+ $this->setId('id');
9
+ $this->setDestElementId('edit_form');
10
+ $this->setTitle(Mage::helper('freento_groupautoswitch')->__('Customer Group'));
11
+ }
12
+
13
+ protected function _beforeToHtml()
14
+ {
15
+ $helper = Mage::helper('freento_groupautoswitch');
16
+
17
+ $this->addTab('main_section', array(
18
+ 'label' => $helper->__('Main'),
19
+ 'title' => $helper->__('Main'),
20
+ 'content' => $this->getLayout()->createBlock('freento_groupautoswitch/adminhtml_customer_group_edit_form_main')->toHtml(),
21
+ 'active' => true
22
+ ));
23
+
24
+ $this->addTab('autoswitch_section', array(
25
+ 'label' => $helper->__('Autoswitch Configuration'),
26
+ 'title' => $helper->__('Autoswitch Configuration'),
27
+ 'content' => $this->getLayout()->createBlock('freento_groupautoswitch/adminhtml_customer_group_edit_form_autoswitch')->toHtml(),
28
+ ));
29
+
30
+ $this->addTab('conditions_section', array(
31
+ 'label' => $helper->__('Conditions'),
32
+ 'title' => $helper->__('Conditions'),
33
+ 'content' => $this->getLayout()->createBlock('freento_groupautoswitch/adminhtml_customer_group_edit_form_conditions')->toHtml(),
34
+ ));
35
+
36
+ $this->addTab('group_settings', array(
37
+ 'label' => $helper->__('Group Settings'),
38
+ 'title' => $helper->__('Group Settings'),
39
+ 'content' => $this->getLayout()->createBlock('freento_groupautoswitch/adminhtml_customer_group_edit_form_groupSettings')->toHtml(),
40
+ ));
41
+
42
+ $this->addTab('downgrade', array(
43
+ 'label' => $helper->__('Downgrade'),
44
+ 'title' => $helper->__('Downgrade'),
45
+ 'content' => $this->getLayout()->createBlock('freento_groupautoswitch/adminhtml_customer_group_edit_form_downgrade')->toHtml(),
46
+ ));
47
+
48
+ return parent::_beforeToHtml();
49
+ }
50
+
51
+ }
app/code/community/Freento/GroupAutoSwitch/Block/Adminhtml/Log.php ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_GroupAutoSwitch_Block_Adminhtml_Log extends Mage_Core_Block_Template
4
+ {
5
+
6
+ public function __construct()
7
+ {
8
+ $this->_blockGroup = 'freento_groupautoswitch';
9
+ $this->_controller = 'adminhtml_log';
10
+ $this->_headerText = Mage::helper('freento_groupautoswitch')->__('Group auto swicth log');
11
+
12
+ parent::__construct();
13
+
14
+ $this->_removeButton('add');
15
+ }
16
+
17
+ }
app/code/community/Freento/GroupAutoSwitch/Block/Adminhtml/Log/Grid.php ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_GroupAutoSwitch_Block_Adminhtml_Log_Grid extends Mage_Adminhtml_Block_Widget_Grid
4
+ {
5
+
6
+ public function __construct()
7
+ {
8
+ parent::__construct();
9
+ $this->setId('freentoGroupAutoSwitchLogGrid');
10
+ $this->setDefaultSort('id');
11
+ $this->setDefaultDir('DESC');
12
+ $this->setSaveParametersInSession(true);
13
+ $this->setUseAjax(true);
14
+ }
15
+
16
+ protected function _prepareCollection()
17
+ {
18
+ $this->setCollection(Mage::getModel('freento_groupautoswitch/log')->getCollection());
19
+
20
+ return parent::_prepareCollection();
21
+ }
22
+
23
+ protected function _prepareColumns()
24
+ {
25
+ $this->addColumn('id', array(
26
+ 'header' => $this->__('ID'),
27
+ 'type' => 'number',
28
+ 'index' => 'id',
29
+ 'align' => 'right',
30
+ 'width' => '50px',
31
+ ));
32
+
33
+ $this->addColumn('details', array(
34
+ 'header' => $this->__('Details'),
35
+ 'type' => 'text',
36
+ 'index' => 'details',
37
+ ));
38
+
39
+ $this->addColumn('created_at', array(
40
+ 'header' => $this->__('Created At'),
41
+ 'align' => 'right',
42
+ 'width' => '150px',
43
+ 'type' => 'datetime',
44
+ 'index' => 'created_at',
45
+ 'filter_index' => 'created_at',
46
+ ));
47
+
48
+ return $this;
49
+ }
50
+
51
+ public function getGridUrl()
52
+ {
53
+ return $this->getUrl('*/*/grid', array('_current'=>true));
54
+ }
55
+
56
+ }
app/code/community/Freento/GroupAutoSwitch/Block/Adminhtml/System/Config/Form/ForceFill.php ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_GroupAutoSwitch_Block_Adminhtml_System_Config_Form_ForceFill extends Mage_Adminhtml_Block_System_Config_Form_Field
4
+ {
5
+
6
+ protected function _prepareLayout()
7
+ {
8
+ parent::_prepareLayout();
9
+ if (!$this->getTemplate()) {
10
+ $this->setTemplate('freento_groupautoswitch/system/config/forceFill.phtml');
11
+ }
12
+ return $this;
13
+ }
14
+
15
+ public function render(Varien_Data_Form_Element_Abstract $element)
16
+ {
17
+ $element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
18
+ return parent::render($element);
19
+ }
20
+
21
+ protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
22
+ {
23
+ $originalData = $element->getOriginalData();
24
+ $this->addData(array(
25
+ 'button_label' => Mage::helper('freento_groupautoswitch')->__($originalData['button_label']),
26
+ 'html_id' => $element->getHtmlId(),
27
+ 'ajax_url' => Mage::getSingleton('adminhtml/url')->getUrl('freento_groupautoswitch/adminhtml_system_config/forceFill')
28
+ ));
29
+
30
+ return $this->_toHtml();
31
+ }
32
+
33
+ }
app/code/community/Freento/GroupAutoSwitch/Exception/General.php ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_GroupAutoSwitch_Exception_General extends Mage_Core_Exception
4
+ {
5
+
6
+ }
app/code/community/Freento/GroupAutoSwitch/Helper/Data.php ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_Groupautoswitch_Helper_Data extends Mage_Core_Helper_Data
4
+ {
5
+ public function getAutoSwitchInfo($customerGroup)
6
+ {
7
+ if (!$customerGroup->getAutoSwitchInfo()) {
8
+ $customerGroup->setAutoSwitchInfo(Mage::getModel('freento_groupautoswitch/group')->load($customerGroup->getId(), 'group_id'));
9
+ }
10
+ return $customerGroup->getAutoSwitchInfo();
11
+ }
12
+ }
app/code/community/Freento/GroupAutoSwitch/Model/Cron.php ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_Groupautoswitch_Model_Cron
4
+ {
5
+
6
+ const FORCE_FILL_LAST_ID_PATH = 'freento_groupautoswitch/force_fill/last_id';
7
+ const FORCE_FILL_VALIDATE_CUSTOMERS_QTY = 100;
8
+
9
+ public function downgradeCheck()
10
+ {
11
+ $collection = Mage::getModel('freento_groupautoswitch/group')->getCollection()->addFieldToFilter('is_enabled', 1);
12
+ foreach ($collection as $item) {
13
+ $item->downgradeCheck();
14
+ }
15
+ }
16
+
17
+ public function downgradeBeforeEmail()
18
+ {
19
+ $collection = Mage::getModel('freento_groupautoswitch/group')->getCollection()->addFieldToFilter('is_enabled', 1);
20
+ foreach ($collection as $item) {
21
+ $item->downgradeBeforeEmail();
22
+ }
23
+ }
24
+
25
+ public function forceFill() {
26
+ $config = Mage::getModel('core/config_data')->load(self::FORCE_FILL_LAST_ID_PATH, 'path');
27
+
28
+ if((int)$config->getValue() < 0) {
29
+ return;
30
+ }
31
+
32
+ $customersCollection = Mage::getModel('customer/customer')->getCollection()
33
+ ->setPageSize(self::FORCE_FILL_VALIDATE_CUSTOMERS_QTY)
34
+ ->setCurPage(1)
35
+ ->addFieldToFilter('entity_id', array('gt' => $config->getValue()));
36
+
37
+ if($customersCollection->getSize() == 0) {
38
+ $config->setValue('-1')->save();
39
+ return;
40
+ }
41
+
42
+ foreach($customersCollection as $customer) {
43
+ $observerObject = new Varien_Object(array('customer' => $customer));
44
+ Mage::getModel('freento_groupautoswitch/group')->getCollection()->validateAndSwitch($observerObject);
45
+ $config->setValue($customer->getId())->save();
46
+ }
47
+
48
+ }
49
+
50
+ }
app/code/community/Freento/GroupAutoSwitch/Model/Group.php ADDED
@@ -0,0 +1,268 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_Groupautoswitch_Model_Group extends Mage_Rule_Model_Rule
4
+ {
5
+ const GROUP_AUTO_SWITCH_EMAIL_TEMPLATE = 'freento_groupautoswitch_group_auto_switch';
6
+ const GROUP_DOWNGRADE_BEFORE_EMAIL_TEMPLATE = 'freento_groupautoswitch_group_downgrade_before';
7
+ const GROUP_DOWNGRADE_EMAIL_TEMPLATE = 'freento_groupautoswitch_group_downgrade';
8
+
9
+ protected function _construct()
10
+ {
11
+ parent::_construct();
12
+ $this->_init('freento_groupautoswitch/group');
13
+ }
14
+
15
+ public function getConditionsInstance()
16
+ {
17
+ return Mage::getModel('freento_groupautoswitch/rule_condition_combine');
18
+ }
19
+
20
+ public function downgradeBeforeEmail()
21
+ {
22
+ if ($this->getData('downgrade_after')) {
23
+ $customerIdsWithOrders = $this->_getCustomerIdsWithOrders($this->getData('downgrade_after') - $this->getData('notify_customer_before_downgrade'));
24
+ if ($this->getData('downgrade_group') > 0) {
25
+ $downgradeCustomerIds = $this->_getCustomerIdsWithoutOrders($customerIdsWithOrders, $this->getData('downgrade_after') - $this->getData('notify_customer_before_downgrade'));
26
+ if (count($downgradeCustomerIds)) {
27
+ $this->downgradeByCustomerIds($downgradeCustomerIds, $this->getData('downgrade_group'));
28
+ $this->_sendDowngradeEmail($downgradeCustomerIds);
29
+ }
30
+ }
31
+ }
32
+
33
+ }
34
+
35
+ public function downgradeCheck()
36
+ {
37
+ if ($this->getData('downgrade_after')) {
38
+ $customerIdsWithOrders = $this->_getCustomerIdsWithOrders($this->getData('downgrade_after'));
39
+ if ($this->getData('downgrade_group') > 0) {
40
+ $downgradeCustomerIds = $this->_getCustomerIdsWithoutOrders($customerIdsWithOrders, $this->getData('downgrade_after'));
41
+ if (count($downgradeCustomerIds)) {
42
+ $this->_sendDowngradeBeforeEmail($downgradeCustomerIds);
43
+ }
44
+ }
45
+ }
46
+ }
47
+
48
+ /**
49
+ * Downgrade customers to some group
50
+ * @param array $downgradeCustomerIds
51
+ * @param int $downgradeGroupId
52
+ */
53
+ public function downgradeByCustomerIds($downgradeCustomerIds, $downgradeGroupId)
54
+ {
55
+ $query = 'UPDATE ' . Mage::getSingleton('core/resource')->getTableName('customer/entity') . ' as customer' .
56
+ ' inner join ' . Mage::getSingleton('core/resource')->getTableName('customer/entity') . '_datetime' . ' as customer_datetime' .
57
+ ' ON customer.entity_id = customer_datetime.entity_id' .
58
+ ' set group_id = ' . $downgradeGroupId .
59
+ ', customer_datetime.value = "' . Mage::getModel('core/date')->gmtDate() . '"' .
60
+ ' where customer.entity_id in (' . implode(',', $downgradeCustomerIds) . ')';
61
+
62
+ $groupModel = Mage::getModel('customer/group');
63
+ $previousGroup = $groupModel->load($this->getData('group_id'))->getCustomerGroupCode();
64
+ $newGroup = $groupModel->load($downgradeGroupId)->getCustomerGroupCode();
65
+ $messages = array();
66
+ foreach ($downgradeCustomerIds as $id) {
67
+ $messages[] = Mage::helper('freento_groupautoswitch')->__('Downgrade. Customer ID: %s, previous group: %s, new group: %s', $id, $previousGroup, $newGroup);
68
+ }
69
+ $this->_log($messages);
70
+ Mage::getSingleton('core/resource')->getConnection('core_write')->query($query);
71
+ }
72
+
73
+ /**
74
+ * Returns customers with orders based on customers without orders ($customerWithOrdersIds) and
75
+ * changed group earlier than N days ($lastDays)
76
+ * @param array $customerWithOrdersIds
77
+ * @param int $lastDays
78
+ */
79
+ protected function _getCustomerIdsWithoutOrders($customerWithOrdersIds, $lastDays)
80
+ {
81
+ $downgradeDatetime = $this->_getDateBeforeNow($lastDays);
82
+ $customerGroupLastChangeDateAttribute = Mage::getModel('eav/entity_attribute')->loadByCode('customer', 'group_last_change_date');
83
+ /* Get customers for downgrade (without sales from current group) ased on customers without orders */
84
+ $query = 'SELECT customer.entity_id as customer_id from ' . Mage::getSingleton('core/resource')->getTableName('customer/entity') . ' as customer' .
85
+ ' INNER JOIN ' . Mage::getSingleton('core/resource')->getTableName('customer/entity') . '_datetime' . ' as customer_datetime' .
86
+ ' ON customer.entity_id = customer_datetime.entity_id' .
87
+ ' where';
88
+ if (count($customerWithOrdersIds)) {
89
+ $query .= ' customer.entity_id not in (' . implode(',', $customerWithOrdersIds) . ') AND';
90
+ }
91
+ $query .= ' customer_datetime.value <= "' . $downgradeDatetime . '" AND';
92
+ $query .= ' customer_datetime.attribute_id = ' . $customerGroupLastChangeDateAttribute->getId() . ' AND';
93
+ $query .= ' group_id = ' . $this->getData('group_id');
94
+
95
+ return Mage::getSingleton('core/resource')->getConnection('core_write')->fetchCol($query);
96
+ }
97
+
98
+ /**
99
+ * Returns customers from current group that have orders for the last days ($lastDays)
100
+ * @param int $lastDays
101
+ */
102
+ protected function _getCustomerIdsWithOrders($lastDays)
103
+ {
104
+ $downgradeDatetime = $this->_getDateBeforeNow($lastDays);
105
+ $orderCollection = Mage::getModel('sales/order')
106
+ ->getCollection()
107
+ ->addAttributeToFilter('customer_group_id', array('eq' => $this->getData('group_id')));
108
+ $orderCollection
109
+ ->getSelect()
110
+ ->where('created_at > ?', $downgradeDatetime)
111
+ ->having('count(entity_id) > 0')
112
+ ->group('customer_id');
113
+ $customerIds = array();
114
+ foreach ($orderCollection as $order) {
115
+ if ($order->getCustomerId()) {
116
+ $customerIds[] = $order->getCustomerId();
117
+ }
118
+ }
119
+ return $customerIds;
120
+ }
121
+
122
+ protected function _getDateBeforeNow($days)
123
+ {
124
+ return date(Varien_Date::DATETIME_PHP_FORMAT, strtotime('-' . $days . ' days', strtotime(Mage::getModel('core/date')->gmtDate())));
125
+ }
126
+
127
+ public function switchGroup($customer)
128
+ {
129
+ if (!$this->getData('is_enabled')) {
130
+ throw new Freento_GroupAutoSwitch_Exception_General('Auto Switch is disabled');
131
+ }
132
+ $groupModel = Mage::getModel('customer/group');
133
+ $previousGroup = $groupModel->load($customer->getGroupId())->getCustomerGroupCode();
134
+ $newGroup = $groupModel->load($this->getGroupId())->getCustomerGroupCode();
135
+ $customer->setGroupId($this->getGroupId())->save();
136
+ $this->_sendSwitchGroupEmail($customer, $newGroup);
137
+ $message = Mage::helper('freento_groupautoswitch')->__('Switch Group. Customer ID: %s, email: %s, previous group: %s, new group: %s', $customer->getId(), $customer->getEmail(), $previousGroup, $newGroup);
138
+ $this->_log(array($message));
139
+ }
140
+
141
+ protected function _sendSwitchGroupEmail($customer, $newGroup)
142
+ {
143
+ $result = false;
144
+ if ($customer->getId()) {
145
+ $templateId = $this->getData('notify_on_group_change_template');
146
+ if(!$templateId) {
147
+ $templateId = self::GROUP_AUTO_SWITCH_EMAIL_TEMPLATE;
148
+ }
149
+
150
+ $customerName = $customer->getName();
151
+ $emailTemplate = Mage::getModel('core/email_template')->loadDefault($templateId);
152
+ $emailTemplateVariables = array(
153
+ 'customer_name' => $customerName,
154
+ 'old_group' => Mage::getModel('customer/group')->load($customer->getGroupId())->getCustomerGroupCode(),
155
+ 'new_group' => $newGroup,
156
+ 'store_name' => Mage::getStoreConfig('general/store_information/name'),
157
+ );
158
+
159
+ $emailTemplate->setSenderName(Mage::getStoreConfig('trans_email/ident_general/name'));
160
+ $emailTemplate->setSenderEmail(Mage::getStoreConfig('trans_email/ident_general/email'));
161
+
162
+ $result = $emailTemplate->send($customer->getEmail(), $customerName, $emailTemplateVariables);
163
+ }
164
+ return $result;
165
+ }
166
+
167
+ public function _sendDowngradeEmail(array $customerIds)
168
+ {
169
+ if(!is_array($customerIds)) {
170
+ return false;
171
+ }
172
+
173
+ $customerCollection = Mage::getResourceModel('customer/customer_collection')
174
+ ->addNameToSelect();
175
+ $customerCollection->addFieldToFilter('entity_id', array('in' => $customerIds));
176
+
177
+ if($templateId = $this->getData('downgrade_template')) {
178
+ $emailTemplate = Mage::getModel('core/email_template')->load($templateId);
179
+ } else {
180
+ $emailTemplate = Mage::getModel('core/email_template')->loadDefault(self::GROUP_DOWNGRADE_EMAIL_TEMPLATE);
181
+ }
182
+ $groupModel = Mage::getModel('customer/group');
183
+
184
+ foreach ($customerCollection as $customer) {
185
+ $customerName = $customer->getName();
186
+ $newGroup = $groupModel->load($customer->getGroupId())->getCustomerGroupCode();
187
+
188
+ $emailTemplateVariables = array(
189
+ 'customer_name' => $customerName,
190
+ 'store_name' => Mage::getStoreConfig('general/store_information/name'),
191
+ 'new_group' => $newGroup,
192
+ );
193
+
194
+ $emailTemplate->setSenderName(Mage::getStoreConfig('trans_email/ident_general/name'));
195
+ $emailTemplate->setSenderEmail(Mage::getStoreConfig('trans_email/ident_general/email'));
196
+
197
+ $emailTemplate->send($customer->getEmail(), $customerName, $emailTemplateVariables);
198
+ }
199
+ return true;
200
+ }
201
+
202
+ protected function _sendDowngradeBeforeEmail(array $customerIds)
203
+ {
204
+ if(!is_array($customerIds)) {
205
+ return false;
206
+ }
207
+
208
+ $customerCollection = Mage::getResourceModel('customer/customer_collection')
209
+ ->addNameToSelect();
210
+ $customerCollection->addFieldToFilter('entity_id', array('in' => $customerIds));
211
+
212
+ if($templateId = $this->getData('notify_customer_before_downgrade_template')) {
213
+ $emailTemplate = Mage::getModel('core/email_template')->load($templateId);
214
+ } else {
215
+ $emailTemplate = Mage::getModel('core/email_template')->loadDefault(self::GROUP_DOWNGRADE_BEFORE_EMAIL_TEMPLATE);
216
+ }
217
+
218
+ foreach ($customerCollection as $customer) {
219
+ $customerName = $customer->getName();
220
+
221
+ $emailTemplateVariables = array(
222
+ 'customer_name' => $customerName,
223
+ 'store_name' => Mage::getStoreConfig('general/store_information/name'),
224
+ 'days_to_downgrade' => $this->getData('downgrade_after'),
225
+ );
226
+
227
+ $emailTemplate->setSenderName(Mage::getStoreConfig('trans_email/ident_general/name'));
228
+ $emailTemplate->setSenderEmail(Mage::getStoreConfig('trans_email/ident_general/email'));
229
+
230
+ $emailTemplate->send($customer->getEmail(), $customerName, $emailTemplateVariables);
231
+ }
232
+ return true;
233
+ }
234
+
235
+ public function validate(Varien_Object $object)
236
+ {
237
+ $group = Mage::getModel('customer/group')->load($this->getGroupId());
238
+ $result = parent::validate($object);
239
+ $message = Mage::helper('freento_groupautoswitch')->__(
240
+ 'Validation. Customer: id %s, email %s, group: %s switch. Validation result: %s',
241
+ $object->getCustomer()->getId() ? $object->getCustomer()->getId() : 0,
242
+ $object->getCustomer()->getEmail(),
243
+ $group->getCustomerGroupCode(),
244
+ $result ? 'true' : 'false'
245
+ );
246
+ $this->_log(array($message));
247
+ return $result;
248
+ }
249
+
250
+ protected function _log(array $messages)
251
+ {
252
+ $writeConnection = Mage::getSingleton('core/resource')->getConnection('core_write');
253
+ $table = Mage::getSingleton('core/resource')->getTableName('freento_groupautoswitch/log');
254
+ $query = 'INSERT INTO `' . $table . '` (`details`, `created_at`) VALUES ' ;
255
+
256
+ $now = Mage::getSingleton('core/date')->gmtDate();
257
+ $rows = array();
258
+ foreach($messages as $message) {
259
+ $rows[] = "('$message', '$now')";
260
+ }
261
+
262
+ if(!empty($rows)) {
263
+ $query .= implode(', ', $rows);
264
+ $writeConnection->query($query);
265
+ }
266
+ }
267
+
268
+ }
app/code/community/Freento/GroupAutoSwitch/Model/Log.php ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_Groupautoswitch_Model_Log extends Mage_Core_Model_Abstract
4
+ {
5
+
6
+ public function _construct()
7
+ {
8
+ parent::_construct();
9
+ $this->_init('freento_groupautoswitch/log');
10
+ }
11
+
12
+ }
app/code/community/Freento/GroupAutoSwitch/Model/Observer.php ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_Groupautoswitch_Model_Observer
4
+ {
5
+ public function fillLastGroupChangeDate($observer)
6
+ {
7
+ $customer = $observer->getCustomer();
8
+ if ($customer->getData('group_id') != $customer->getOrigData('group_id')) {
9
+ $customer->setData('group_last_change_date', Mage::getModel('core/date')->gmtDate());
10
+ }
11
+ }
12
+
13
+ public function checkGroupAutoSwitch($observer)
14
+ {
15
+ try {
16
+ $order = $observer->getOrder();
17
+ /* If order invoiced sum changed, then run validation */
18
+ if ($order->getData('base_total_invoiced') != $order->getOrigData('base_total_invoiced')) {
19
+ $customer = Mage::getModel('customer/customer')->load($order->getCustomerId());
20
+
21
+ if(!$customer->getEmail()) {
22
+ $customer->setEmail($order->getCustomerEmail());
23
+ }
24
+ $observerObject = new Varien_Object(array('customer' => $customer));
25
+
26
+ Mage::getModel('freento_groupautoswitch/group')
27
+ ->getCollection()
28
+ ->validateAndSwitch($observerObject);
29
+ }
30
+ } catch (Exception $ex) { /* TODO: Add new Exception type for extension and catch it */
31
+
32
+ }
33
+ }
34
+ }
app/code/community/Freento/GroupAutoSwitch/Model/Resource/Group.php ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_Groupautoswitch_Model_Resource_Group extends Mage_Core_Model_Resource_Db_Abstract
4
+ {
5
+ protected function _construct()
6
+ {
7
+ $this->_init('freento_groupautoswitch/group', 'id');
8
+ }
9
+ }
app/code/community/Freento/GroupAutoSwitch/Model/Resource/Group/Collection.php ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_Groupautoswitch_Model_Resource_Group_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
4
+ {
5
+ public function _construct()
6
+ {
7
+ $this->_init('freento_groupautoswitch/group');
8
+ }
9
+
10
+ public function validateAndSwitch($observerObject)
11
+ {
12
+ $customer = $observerObject->getCustomer();
13
+ $this
14
+ ->addFieldToFilter('is_enabled', 1)
15
+ ->setOrder('priority', 'desc');
16
+ foreach ($this as $autoswitchGroup) {
17
+ if ($customer->getGroupId() != $autoswitchGroup->getGroupId()) {
18
+ if ($autoswitchGroup->validate($observerObject)) {
19
+ $autoswitchGroup->switchGroup($customer);
20
+ if ($autoswitchGroup->getData('stop_further_rules')) {
21
+ break;
22
+ }
23
+ }
24
+ }
25
+ }
26
+ return $this;
27
+ }
28
+ }
app/code/community/Freento/GroupAutoSwitch/Model/Resource/Log.php ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_Groupautoswitch_Model_Resource_Log extends Mage_Core_Model_Resource_Db_Abstract
4
+ {
5
+ protected function _construct()
6
+ {
7
+ $this->_init('freento_groupautoswitch/log', 'id');
8
+ }
9
+
10
+ protected function _beforeSave(Mage_Core_Model_Abstract $object)
11
+ {
12
+ if (!$object->hasCreatedAt()) {
13
+ $object->setCreatedAt(Mage::getSingleton('core/date')->gmtDate());
14
+ }
15
+ }
16
+
17
+ }
app/code/community/Freento/GroupAutoSwitch/Model/Resource/Log/Collection.php ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_Groupautoswitch_Model_Resource_Log_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
4
+ {
5
+ public function _construct()
6
+ {
7
+ $this->_init('freento_groupautoswitch/Log');
8
+ }
9
+ }
app/code/community/Freento/GroupAutoSwitch/Model/Rule/Condition/Combine.php ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_GroupAutoSwitch_Model_Rule_Condition_Combine extends Mage_Rule_Model_Condition_Combine
4
+ {
5
+ public function __construct()
6
+ {
7
+ parent::__construct();
8
+ $this->setType('freento_groupautoswitch/rule_condition_combine');
9
+ }
10
+
11
+ public function getNewChildSelectOptions()
12
+ {
13
+ $customerStatistic = array();
14
+ $customerStatistic[] = array('value' => 'freento_groupautoswitch/rule_condition_customer_statistic_lifetimeSales', 'label' => Mage::helper('freento_groupautoswitch')->__('Customer Lifetime Sales'));
15
+ $customerStatistic[] = array('value' => 'freento_groupautoswitch/rule_condition_customer_statistic_currentGroupSales', 'label' => Mage::helper('freento_groupautoswitch')->__('Customer Current Group Sales'));
16
+
17
+ $customerAttributes = array();
18
+ $customerAttributes[] = array('value' => 'freento_groupautoswitch/rule_condition_customer_attribute_group', 'label' => Mage::helper('freento_groupautoswitch')->__('Group'));
19
+
20
+ $conditions = parent::getNewChildSelectOptions();
21
+ $conditions = array_merge_recursive($conditions, array(
22
+ array('value' => 'freento_groupautoswitch/rule_condition_combine', 'label' => Mage::helper('catalogrule')->__('Conditions Combination')),
23
+ array('label' => Mage::helper('freento_groupautoswitch')->__('Customer Statistic'), 'value' => $customerStatistic),
24
+ array('label' => Mage::helper('freento_groupautoswitch')->__('Customer Attributes'), 'value' => $customerAttributes),
25
+ ));
26
+ return $conditions;
27
+ }
28
+ }
app/code/community/Freento/GroupAutoSwitch/Model/Rule/Condition/Customer/Attribute/Group.php ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_GroupAutoSwitch_Model_Rule_Condition_Customer_Attribute_Group extends Mage_Rule_Model_Condition_Abstract
4
+ {
5
+ public function __construct()
6
+ {
7
+ parent::__construct();
8
+ $this->setType('freento_groupautoswitch/rule_condition_customer_attribute_group')
9
+ ->setValue(null);
10
+ }
11
+
12
+ public function loadOperatorOptions()
13
+ {
14
+ $this->setOperatorOption(array(
15
+ '()' => Mage::helper('rule')->__('is one of'),
16
+ '!()' => Mage::helper('rule')->__('is not one of'),
17
+ ));
18
+ return $this;
19
+ }
20
+
21
+ public function loadAttributeOptions()
22
+ {
23
+ $hlp = Mage::helper('freento_groupautoswitch');
24
+ $this->setAttributeOption(array(
25
+ 'group' => $hlp->__('Customer Group'),
26
+ ));
27
+ return $this;
28
+ }
29
+
30
+ public function validate(Varien_Object $observerObject)
31
+ {
32
+ $customer = $observerObject->getCustomer();
33
+
34
+ return $this->validateAttribute($customer->getGroupId());
35
+ }
36
+
37
+ public function getValueElementType()
38
+ {
39
+ return 'multiselect';
40
+ }
41
+
42
+ public function getValueSelectOptions()
43
+ {
44
+ return Mage::getResourceModel('customer/group_collection')->toOptionArray();
45
+ }
46
+ }
app/code/community/Freento/GroupAutoSwitch/Model/Rule/Condition/Customer/Statistic/Abstract.php ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_GroupAutoSwitch_Model_Rule_Condition_Customer_Statistic_Abstract extends Mage_Rule_Model_Condition_Abstract
4
+ {
5
+ public function getInputType()
6
+ {
7
+ return 'string';
8
+ }
9
+
10
+ public function loadOperatorOptions()
11
+ {
12
+ $this->setOperatorOption(array(
13
+ '==' => Mage::helper('rule')->__('is'),
14
+ '!=' => Mage::helper('rule')->__('is not'),
15
+ '>=' => Mage::helper('rule')->__('equals or greater than'),
16
+ '<=' => Mage::helper('rule')->__('equals or less than'),
17
+ '>' => Mage::helper('rule')->__('greater than'),
18
+ '<' => Mage::helper('rule')->__('less than'),
19
+ ));
20
+ return $this;
21
+ }
22
+
23
+ public function getValueElementType()
24
+ {
25
+ return 'text';
26
+ }
27
+
28
+ public function getAttributeElement()
29
+ {
30
+ $element = parent::getAttributeElement();
31
+ $element->setShowAsText(true);
32
+ return $element;
33
+ }
34
+ }
app/code/community/Freento/GroupAutoSwitch/Model/Rule/Condition/Customer/Statistic/CurrentGroupSales.php ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_GroupAutoSwitch_Model_Rule_Condition_Customer_Statistic_CurrentGroupSales extends Freento_GroupAutoSwitch_Model_Rule_Condition_Customer_Statistic_Abstract
4
+ {
5
+ public function __construct()
6
+ {
7
+ parent::__construct();
8
+ $this->setType('freento_groupautoswitch/rule_condition_customer_statistic_currentGroupSales')
9
+ ->setValue(null);
10
+ }
11
+
12
+ public function loadAttributeOptions()
13
+ {
14
+ $hlp = Mage::helper('freento_groupautoswitch');
15
+ $this->setAttributeOption(array(
16
+ 'currentGroupSales' => $hlp->__('Customer Current Group Sales'),
17
+ ));
18
+ return $this;
19
+ }
20
+
21
+ public function validate(Varien_Object $observerObject)
22
+ {
23
+ $customer = $observerObject->getCustomer();
24
+
25
+ $orderCollection = Mage::getModel('sales/order')->getCollection();
26
+ $select = $orderCollection->getSelect();
27
+ $select
28
+ ->where('customer_id = ?', $customer->getId())
29
+ ->columns(array('sum_invoiced' => 'sum(base_total_invoiced)'))
30
+ ->columns(array('sum_refunded' => 'sum(base_total_refunded)'))
31
+ ->group('customer_id');
32
+ if ($customer->getData('group_last_change_date')) {
33
+ $groupLastChangePlusMinute = date(Varien_Date::DATETIME_PHP_FORMAT, strtotime('+1 minutes', strtotime($customer->getData('group_last_change_date'))));
34
+ $select->where('created_at >= ?', $groupLastChangePlusMinute);
35
+ }
36
+ $statistic = $orderCollection->getFirstItem();
37
+
38
+ return $this->validateAttribute($statistic->getSumInvoiced() - $statistic->getSumRefunded());
39
+ }
40
+ }
app/code/community/Freento/GroupAutoSwitch/Model/Rule/Condition/Customer/Statistic/LifetimeSales.php ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_GroupAutoSwitch_Model_Rule_Condition_Customer_Statistic_LifetimeSales extends Freento_GroupAutoSwitch_Model_Rule_Condition_Customer_Statistic_Abstract
4
+ {
5
+ public function __construct()
6
+ {
7
+ parent::__construct();
8
+ $this->setType('freento_groupautoswitch/rule_condition_customer_statistic_lifetimeSales')
9
+ ->setValue(null);
10
+ }
11
+
12
+ public function loadAttributeOptions()
13
+ {
14
+ $hlp = Mage::helper('freento_groupautoswitch');
15
+ $this->setAttributeOption(array(
16
+ 'lifetimeSales' => $hlp->__('Customer Lifetime Sales'),
17
+ ));
18
+ return $this;
19
+ }
20
+
21
+ public function validate(Varien_Object $observerObject)
22
+ {
23
+ $customer = $observerObject->getCustomer();
24
+
25
+ $orderCollection = Mage::getModel('sales/order')->getCollection();
26
+ $orderCollection
27
+ ->getSelect()
28
+ ->where('customer_id = ?', $customer->getId())
29
+ ->columns(array('sum_invoiced' => 'sum(base_total_invoiced)'))
30
+ ->columns(array('sum_refunded' => 'sum(base_total_refunded)'))
31
+ ->group('customer_id');
32
+ $statistic = $orderCollection->getFirstItem();
33
+
34
+ return $this->validateAttribute($statistic->getSumInvoiced() - $statistic->getSumRefunded());
35
+ }
36
+ }
app/code/community/Freento/GroupAutoSwitch/Test/Model/Cron.php ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_GroupAutoSwitch_Test_Model_Cron extends EcomDev_PHPUnit_Test_Case
4
+ {
5
+ /**
6
+ * Check cron forceFill() method
7
+ *
8
+ * @test
9
+ * @loadFixture
10
+ * @loadExpectation
11
+ * @dataProvider dataProvider
12
+ */
13
+ public function forceFill($testId, $customerId)
14
+ {
15
+ /* @var $autoSwitchCron Freento_Groupautoswitch_Model_Cron */
16
+ $autoSwitchCron = Mage::getModel('freento_groupautoswitch/cron');
17
+ $autoSwitchCron->forceFill();
18
+
19
+ $customer = Mage::getModel('customer/customer')->load($customerId);
20
+ $this->assertEquals( $this->_getExpectations()->getData($testId . '/new_group_id'), $customer->getGroupId() );
21
+ $this->assertEquals( $this->_getExpectations()->getData($testId . '/last_id'), $customer->getId() );
22
+ }
23
+
24
+ /**
25
+ * Check cron downgradeCheck() method
26
+ *
27
+ * @test
28
+ * @loadFixture
29
+ * @loadExpectation
30
+ * @dataProvider dataProvider
31
+ */
32
+ public function downgradeCheck($testId, $customerId)
33
+ {
34
+
35
+ /* @var $autoSwitchCron Freento_Groupautoswitch_Model_Cron */
36
+ $autoSwitchCron = Mage::getModel('freento_groupautoswitch/cron');
37
+ $autoSwitchCron->downgradeCheck();
38
+
39
+ $customer = Mage::getModel('customer/customer')->load($customerId);
40
+ $this->assertEquals( $this->_getExpectations()->getData($testId . '/new_group_id'), $customer->getGroupId() );
41
+ }
42
+ }
app/code/community/Freento/GroupAutoSwitch/Test/Model/Cron/expectations/downgradeCheck.php ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
1
+ <?php
2
+
3
+ return
4
+ array(
5
+ 'test1' => array('new_group_id' => 1),
6
+ );
app/code/community/Freento/GroupAutoSwitch/Test/Model/Cron/expectations/forceFill.php ADDED
@@ -0,0 +1,4 @@
 
 
 
 
1
+ <?php
2
+
3
+ return
4
+ array('test1' => array('new_group_id' => 10, 'last_id' => 1));
app/code/community/Freento/GroupAutoSwitch/Test/Model/Cron/fixtures/downgradeCheck.php ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ return
4
+ array(
5
+ 'customer_entity' => array(
6
+ array(
7
+ 'entity_id' => '1',
8
+ 'entity_type_id' => '1',
9
+ 'attribute_set_id' => '0',
10
+ 'website_id' => '1',
11
+ 'email' => 'test@test.com',
12
+ 'group_id' => '10',
13
+ 'increment_id' => NULL,
14
+ 'store_id' => '1',
15
+ 'created_at' => '2015-04-02 20:54:56',
16
+ 'updated_at' => '2015-04-02 20:54:56',
17
+ 'is_active' => '1',
18
+ 'disable_auto_group_change' => '0'
19
+ )
20
+ ),
21
+ 'customer_group' => array(
22
+ array(
23
+ 'customer_group_id' => '1',
24
+ 'customer_group_code' => 'General',
25
+ 'tax_class_id' => '3'
26
+ ),
27
+ array(
28
+ 'customer_group_id' => '10',
29
+ 'customer_group_code' => 'Level 1',
30
+ 'tax_class_id' => '3'
31
+ ),
32
+ ),
33
+ 'freento_groupautoswitch_group' => array(
34
+ array(
35
+ 'id' => '1',
36
+ 'is_enabled' => '1',
37
+ 'group_id' => '10',
38
+ 'priority' => '0',
39
+ 'conditions_serialized' => 'a:6:{s:4:"type";s:41:"rc_groupautoswitch/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}',
40
+ 'stop_further_rules' => '0',
41
+ 'downgrade_template' => '',
42
+ 'notify_on_group_change_template' => '',
43
+ 'text_for_customer' => '',
44
+ 'downgrade_after' => '10',
45
+ 'downgrade_group' => '1',
46
+ 'notify_customer_before_downgrade' => '1',
47
+ 'notify_customer_before_downgrade_template' => ''
48
+ ),
49
+ ),
50
+ 'eav_attribute' => array(
51
+ array(
52
+ 'attribute_id' => '961',
53
+ 'entity_type_id' => '1',
54
+ 'attribute_code' => 'group_last_change_date',
55
+ 'attribute_model' => NULL,
56
+ 'backend_model' => 'eav/entity_attribute_backend_datetime',
57
+ 'backend_type' => 'datetime',
58
+ 'backend_table' => NULL,
59
+ 'frontend_model' => 'eav/entity_attribute_frontend_datetime',
60
+ 'frontend_input' => 'date',
61
+ 'frontend_label' => 'Customer Group Last Change Date',
62
+ 'frontend_class' => NULL,
63
+ 'source_model' => NULL,
64
+ 'is_required' => '0',
65
+ 'is_user_defined' => '0',
66
+ 'default_value' => NULL,
67
+ 'is_unique' => '0',
68
+ 'note' => NULL
69
+ )
70
+ ),
71
+ 'customer_eav_attribute' => array(
72
+ array(
73
+ 'attribute_id' => '961',
74
+ 'is_visible' => '1',
75
+ 'input_filter' => NULL,
76
+ 'multiline_count' => '1',
77
+ 'validate_rules' => NULL,
78
+ 'is_system' => '0',
79
+ 'sort_order' => '0',
80
+ 'data_model' => NULL
81
+ )
82
+ ),
83
+ 'customer_entity_datetime' => array(
84
+ array(
85
+ 'value_id' => '1',
86
+ 'entity_type_id' => '1',
87
+ 'attribute_id' => '961',
88
+ 'entity_id' => '1',
89
+ 'value' => '2015-04-04 07:25:10'
90
+ )
91
+ )
92
+ );
app/code/community/Freento/GroupAutoSwitch/Test/Model/Cron/fixtures/forceFill.php ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ return
4
+ array('tables' => array(
5
+ 'customer_entity' => array(
6
+ array(
7
+ 'entity_id' => '1',
8
+ 'entity_type_id' => '1',
9
+ 'attribute_set_id' => '0',
10
+ 'website_id' => '1',
11
+ 'email' => 'test@test.com',
12
+ 'group_id' => '1',
13
+ 'increment_id' => NULL,
14
+ 'store_id' => '1',
15
+ 'created_at' => '2015-04-02 20:54:56',
16
+ 'updated_at' => '2015-04-02 20:54:56',
17
+ 'is_active' => '1',
18
+ 'disable_auto_group_change' => '0'
19
+ )
20
+ ),
21
+ 'customer_group' => array(
22
+ array(
23
+ 'customer_group_id' => '1',
24
+ 'customer_group_code' => 'General',
25
+ 'tax_class_id' => '3'
26
+ ),
27
+ array(
28
+ 'customer_group_id' => '10',
29
+ 'customer_group_code' => 'Level 1',
30
+ 'tax_class_id' => '3'
31
+ ),
32
+ array(
33
+ 'customer_group_id' => '11',
34
+ 'customer_group_code' => 'Level 10',
35
+ 'tax_class_id' => '3'
36
+ )
37
+ ),
38
+ 'freento_groupautoswitch_group' => array(
39
+ array(
40
+ 'id' => '1',
41
+ 'is_enabled' => 1,
42
+ 'group_id' => '10',
43
+ 'priority' => '0',
44
+ 'conditions_serialized' => 'a:6:{s:4:"type";s:46:"freento_groupautoswitch/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}',
45
+ 'stop_further_rules' => '0',
46
+ 'downgrade_template' => '',
47
+ 'notify_on_group_change_template' => '',
48
+ 'text_for_customer' => '',
49
+ 'downgrade_after' => '0',
50
+ 'downgrade_group' => '-1',
51
+ 'notify_customer_before_downgrade' => '0',
52
+ 'notify_customer_before_downgrade_template' => ''
53
+ ),
54
+ array(
55
+ 'id' => '2',
56
+ 'is_enabled' => 0,
57
+ 'group_id' => '11',
58
+ 'priority' => '0',
59
+ 'conditions_serialized' => 'a:7:{s:4:"type";s:46:"freento_groupautoswitch/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";s:10:"conditions";a:2:{i:0;a:5:{s:4:"type";s:63:"freento_groupautoswitch/rule_condition_customer_attribute_group";s:9:"attribute";s:5:"group";s:8:"operator";s:2:"==";s:5:"value";a:2:{i:0;s:1:"1";i:1;s:1:"2";}s:18:"is_value_processed";b:0;}i:1;a:5:{s:4:"type";s:71:"freento_groupautoswitch/rule_condition_customer_statistic_lifetimeSales";s:9:"attribute";s:13:"lifetimeSales";s:8:"operator";s:2:">=";s:5:"value";s:2:"50";s:18:"is_value_processed";b:0;}}}',
60
+ 'stop_further_rules' => '0',
61
+ 'downgrade_template' => '',
62
+ 'notify_on_group_change_template' => '',
63
+ 'text_for_customer' => '',
64
+ 'downgrade_after' => '0',
65
+ 'downgrade_group' => '-1',
66
+ 'notify_customer_before_downgrade' => '0',
67
+ 'notify_customer_before_downgrade_template' => ''
68
+ ),
69
+ ),
70
+ 'core_config_data' => array(
71
+ array(
72
+ 'config_id' => '8',
73
+ 'scope' => 'default',
74
+ 'scope_id' => '0',
75
+ 'path' => 'freento_groupautoswitch/force_fill/last_id',
76
+ 'value' => '0'
77
+ )
78
+ )
79
+ ));
app/code/community/Freento/GroupAutoSwitch/Test/Model/Cron/providers/downgradeCheck.php ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
1
+ <?php
2
+
3
+ return
4
+ array(
5
+ array('test1', 1),
6
+ );
app/code/community/Freento/GroupAutoSwitch/Test/Model/Cron/providers/forceFill.php ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
1
+ <?php
2
+
3
+ return
4
+ array(
5
+ array('test1', 1),
6
+ );
app/code/community/Freento/GroupAutoSwitch/Test/Model/Group.php ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_GroupAutoSwitch_Test_Model_Group extends EcomDev_PHPUnit_Test_Case
4
+ {
5
+ /**
6
+ * Switch group for existing customer
7
+ *
8
+ * @test
9
+ * @loadFixture
10
+ * @loadExpectation
11
+ * @dataProvider dataProvider
12
+ */
13
+ public function switchGroup($testId, $customerId, $newGroupId)
14
+ {
15
+
16
+ if($this->_getExpectations()->getData($testId . '/email_sent')) {
17
+ $this->_checkEmailSent('once');
18
+ $this->_checkEmailTemplate($testId);
19
+ } else {
20
+ $this->_checkEmailSent('never');
21
+ }
22
+
23
+ try {
24
+ $customer = Mage::getModel('customer/customer')->load($customerId);
25
+ $autoSwitchGroup = Mage::getModel('freento_groupautoswitch/group')->load($newGroupId, 'group_id');
26
+
27
+ $autoSwitchGroup->switchGroup($customer);
28
+
29
+ $customer = Mage::getModel('customer/customer')->load($customerId);
30
+ } catch (Freento_GroupAutoSwitch_Exception_General $ex) {
31
+ $this->assertEquals( $this->_getExpectations()->getData($testId . '/exception_message'), $ex->getMessage() );
32
+ }
33
+ $this->assertEquals( $this->_getExpectations()->getData($testId . '/new_group_id'), $customer->getGroupId() );
34
+ }
35
+
36
+ protected function _checkEmailSent($repeat)
37
+ {
38
+ $emailMock = $this->getModelMock('core/email_template', array('send'));
39
+ $emailMock->expects($this->{$repeat}())
40
+ ->method('send')
41
+ ->will($this->returnCallback(function(){return true;}));
42
+ $this->replaceByMock('model', 'core/email_template', $emailMock);
43
+ }
44
+
45
+
46
+ protected function _checkEmailTemplate($testId)
47
+ {
48
+ $emailMock = $this->getModelMock('core/email_template', array('loadDefault'));
49
+ $emailMock->expects($this->once())
50
+ ->method('loadDefault')
51
+ ->will($this->returnCallback(array(Mage::getModel('core/email_template'), 'loadDefault')))
52
+ ->with($this->equalTo($this->_getExpectations()->getData($testId . '/email_template')));
53
+ $this->replaceByMock('model', 'core/email_template', $emailMock);
54
+ }
55
+ }
app/code/community/Freento/GroupAutoSwitch/Test/Model/Group/expectations/switchGroup.php ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ return
4
+ array(
5
+ 'test1' => array('new_group_id' => 10, 'email_sent' => true, 'email_template' => 'freento_groupautoswitch_group_auto_switch'),
6
+ 'test2' => array('new_group_id' => 1, 'exception_message' => 'Auto Switch is disabled', 'email_sent' => false),
7
+ );
app/code/community/Freento/GroupAutoSwitch/Test/Model/Group/fixtures/switchGroup.php ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ return
4
+ array('tables' => array(
5
+ 'customer_entity' => array(
6
+ array(
7
+ 'entity_id' => '1',
8
+ 'entity_type_id' => '1',
9
+ 'attribute_set_id' => '0',
10
+ 'website_id' => '1',
11
+ 'email' => 'test@test.com',
12
+ 'group_id' => '1',
13
+ 'increment_id' => NULL,
14
+ 'store_id' => '1',
15
+ 'created_at' => '2015-04-02 20:54:56',
16
+ 'updated_at' => '2015-04-02 20:54:56',
17
+ 'is_active' => '1',
18
+ 'disable_auto_group_change' => '0'
19
+ )
20
+ ),
21
+ 'customer_group' => array(
22
+ array(
23
+ 'customer_group_id' => '1',
24
+ 'customer_group_code' => 'General',
25
+ 'tax_class_id' => '3'
26
+ ),
27
+ array(
28
+ 'customer_group_id' => '10',
29
+ 'customer_group_code' => 'Level 1',
30
+ 'tax_class_id' => '3'
31
+ ),
32
+ array(
33
+ 'customer_group_id' => '11',
34
+ 'customer_group_code' => 'Level 10',
35
+ 'tax_class_id' => '3'
36
+ )
37
+ ),
38
+ 'freento_groupautoswitch_group' => array(
39
+ array(
40
+ 'id' => '1',
41
+ 'is_enabled' => 1,
42
+ 'group_id' => '10',
43
+ 'priority' => '0',
44
+ 'conditions_serialized' => 'a:7:{s:4:"type";s:41:"freento_groupautoswitch/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";s:10:"conditions";a:2:{i:0;a:5:{s:4:"type";s:58:"freento_groupautoswitch/rule_condition_customer_attribute_group";s:9:"attribute";s:5:"group";s:8:"operator";s:2:"==";s:5:"value";a:2:{i:0;s:1:"1";i:1;s:1:"2";}s:18:"is_value_processed";b:0;}i:1;a:5:{s:4:"type";s:66:"freento_groupautoswitch/rule_condition_customer_statistic_lifetimeSales";s:9:"attribute";s:13:"lifetimeSales";s:8:"operator";s:2:">=";s:5:"value";s:2:"50";s:18:"is_value_processed";b:0;}}}',
45
+ 'stop_further_rules' => '0',
46
+ 'downgrade_template' => '',
47
+ 'notify_on_group_change_template' => '',
48
+ 'text_for_customer' => '',
49
+ 'downgrade_after' => '0',
50
+ 'downgrade_group' => '-1',
51
+ 'notify_customer_before_downgrade' => '0',
52
+ 'notify_customer_before_downgrade_template' => ''
53
+ ),
54
+ array(
55
+ 'id' => '2',
56
+ 'is_enabled' => 0,
57
+ 'group_id' => '11',
58
+ 'priority' => '0',
59
+ 'conditions_serialized' => 'a:7:{s:4:"type";s:41:"freento_groupautoswitch/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";s:10:"conditions";a:2:{i:0;a:5:{s:4:"type";s:58:"freento_groupautoswitch/rule_condition_customer_attribute_group";s:9:"attribute";s:5:"group";s:8:"operator";s:2:"==";s:5:"value";a:2:{i:0;s:1:"1";i:1;s:1:"2";}s:18:"is_value_processed";b:0;}i:1;a:5:{s:4:"type";s:66:"freento_groupautoswitch/rule_condition_customer_statistic_lifetimeSales";s:9:"attribute";s:13:"lifetimeSales";s:8:"operator";s:2:">=";s:5:"value";s:2:"50";s:18:"is_value_processed";b:0;}}}',
60
+ 'stop_further_rules' => '0',
61
+ 'downgrade_template' => '',
62
+ 'notify_on_group_change_template' => '',
63
+ 'text_for_customer' => '',
64
+ 'downgrade_after' => '0',
65
+ 'downgrade_group' => '-1',
66
+ 'notify_customer_before_downgrade' => '0',
67
+ 'notify_customer_before_downgrade_template' => ''
68
+ ),
69
+ )
70
+ ));
app/code/community/Freento/GroupAutoSwitch/Test/Model/Group/providers/switchGroup.php ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ return
4
+ array(
5
+ array('test1', '1', '10'),
6
+ array('test2', '1', '11'),
7
+ );
app/code/community/Freento/GroupAutoSwitch/controllers/Adminhtml/Customer/GroupController.php ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_GroupAutoSwitch_Adminhtml_Customer_GroupController extends Mage_Adminhtml_Controller_Action
4
+ {
5
+ protected function _initGroup()
6
+ {
7
+ $this->_title($this->__('Customers'))->_title($this->__('Customer Groups'));
8
+
9
+ Mage::register('current_group', Mage::getModel('customer/group'));
10
+ $groupId = $this->getRequest()->getParam('id');
11
+ if (!is_null($groupId)) {
12
+ Mage::registry('current_group')->load($groupId);
13
+ }
14
+
15
+ }
16
+
17
+ /**
18
+ * Edit or create customer group.
19
+ */
20
+ public function newAction()
21
+ {
22
+ $this->_initGroup();
23
+ $this->loadLayout();
24
+ $this->_setActiveMenu('customer/group');
25
+ $this->_addBreadcrumb(Mage::helper('customer')->__('Customers'), Mage::helper('customer')->__('Customers'));
26
+ $this->_addBreadcrumb(Mage::helper('customer')->__('Customer Groups'), Mage::helper('customer')->__('Customer Groups'), $this->getUrl('*/customer_group'));
27
+
28
+ $currentGroup = Mage::registry('current_group');
29
+ $currentGroupAutoswitch = Mage::helper('freento_groupautoswitch')->getAutoSwitchInfo($currentGroup);
30
+
31
+ $currentGroupAutoswitch->getConditions()->setJsFormObject('rule_conditions_fieldset');
32
+
33
+ if (!is_null($currentGroup->getId())) {
34
+ $this->_addBreadcrumb(Mage::helper('customer')->__('Edit Group'), Mage::helper('customer')->__('Edit Customer Groups'));
35
+ } else {
36
+ $this->_addBreadcrumb(Mage::helper('customer')->__('New Group'), Mage::helper('customer')->__('New Customer Groups'));
37
+ }
38
+
39
+ $this->_title($currentGroup->getId() ? $currentGroup->getCode() : $this->__('New Group'));
40
+
41
+ /*Load needed js to head of layout*/
42
+ $this->getLayout()->getBlock('head')
43
+ ->setCanLoadExtJs(true)
44
+ ->setCanLoadRulesJs(true);
45
+
46
+ $this->getLayout()->getBlock('content')
47
+ ->append($this->getLayout()->createBlock('adminhtml/customer_group_edit', 'group')
48
+ ->setEditMode((bool)Mage::registry('current_group')->getId()));
49
+
50
+ $this->renderLayout();
51
+ }
52
+
53
+ /**
54
+ * Edit customer group action. Forward to new action.
55
+ */
56
+ public function editAction()
57
+ {
58
+ $this->_forward('new');
59
+ }
60
+
61
+ public function newConditionHtmlAction()
62
+ {
63
+ $id = $this->getRequest()->getParam('id');
64
+ $typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
65
+ $type = $typeArr[0];
66
+
67
+ $model = Mage::getModel($type)
68
+ ->setId($id)
69
+ ->setType($type)
70
+ ->setRule(Mage::getModel('freento_groupautoswitch/group'))
71
+ ->setPrefix('conditions');
72
+ if (!empty($typeArr[1])) {
73
+ $model->setAttribute($typeArr[1]);
74
+ }
75
+
76
+ if ($model instanceof Mage_Rule_Model_Condition_Abstract) {
77
+ $model->setJsFormObject($this->getRequest()->getParam('form'));
78
+ $html = $model->asHtmlRecursive();
79
+ } else {
80
+ $html = '';
81
+ }
82
+ $this->getResponse()->setBody($html);
83
+ }
84
+
85
+ /**
86
+ * Create or save customer group.
87
+ */
88
+ public function saveAction()
89
+ {
90
+ $customerGroup = Mage::getModel('customer/group');
91
+ $id = $this->getRequest()->getParam('id');
92
+ if (!is_null($id)) {
93
+ $customerGroup->load((int)$id);
94
+ }
95
+
96
+ $taxClass = (int)$this->getRequest()->getParam('tax_class');
97
+
98
+ if ($taxClass) {
99
+ try {
100
+ $customerGroupCode = (string)$this->getRequest()->getParam('code');
101
+
102
+ if (!empty($customerGroupCode)) {
103
+ $customerGroup->setCode($customerGroupCode);
104
+ }
105
+
106
+ $customerGroup->setTaxClassId($taxClass)->save();
107
+
108
+ $currentGroupAutoswitch = Mage::helper('freento_groupautoswitch')->getAutoSwitchInfo($customerGroup);
109
+ $data = $this->getRequest()->getPost();
110
+ if (isset($data['id'])) {
111
+ $data['group_id'] = $data['id'];
112
+ unset($data['id']);
113
+ }
114
+ $data['conditions'] = $data['rule']['conditions'];
115
+ unset($data['rule']);
116
+
117
+ $currentGroupAutoswitch
118
+ ->loadPost($data)
119
+ ->save();
120
+
121
+ Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('customer')->__('The customer group has been saved.'));
122
+ $this->getResponse()->setRedirect($this->getUrl('*/customer_group'));
123
+ return;
124
+ } catch (Exception $e) {
125
+ Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
126
+ Mage::getSingleton('adminhtml/session')->setCustomerGroupData($customerGroup->getData());
127
+ $this->getResponse()->setRedirect($this->getUrl('*/customer_group/edit', array('id' => $id)));
128
+ return;
129
+ }
130
+ } else {
131
+ $this->_forward('new');
132
+ }
133
+ }
134
+
135
+ }
app/code/community/Freento/GroupAutoSwitch/controllers/Adminhtml/LogController.php ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_GroupAutoSwitch_Adminhtml_LogController extends Mage_Adminhtml_Controller_Action
4
+ {
5
+
6
+ public function indexAction()
7
+ {
8
+ $this
9
+ ->loadLayout()
10
+ ->_setActiveMenu('customer')
11
+ ->renderLayout();
12
+ }
13
+
14
+
15
+ public function gridAction()
16
+ {
17
+ $this->loadLayout();
18
+ $this->getResponse()->setBody(
19
+ $this->getLayout()->createBlock('freento_groupautoswitch/adminhtml_log_grid')->toHtml()
20
+ );
21
+ }
22
+ }
app/code/community/Freento/GroupAutoSwitch/controllers/Adminhtml/System/ConfigController.php ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Freento_GroupAutoSwitch_Adminhtml_System_ConfigController extends Mage_Adminhtml_Controller_Action
4
+ {
5
+
6
+ public function forceFillAction()
7
+ {
8
+ $config = Mage::getModel('core/config_data')->load(Freento_GroupAutoSwitch_Model_Cron::FORCE_FILL_LAST_ID_PATH, 'path');
9
+
10
+ if(!$config->getConfigId()) {
11
+ $config->setPath(Freento_GroupAutoSwitch_Model_Cron::FORCE_FILL_LAST_ID_PATH)->setValue('-1')->save();
12
+ }
13
+
14
+ if($config->getValue() >= 0) {
15
+ $result = array(
16
+ 'error' => 1,
17
+ 'message' => Mage::helper('freento_groupautoswitch')->__('Customer validation already running')
18
+ );
19
+ } else {
20
+ $config->setValue(0)->save();
21
+ $result = array(
22
+ 'error' => 0,
23
+ 'message' => Mage::helper('freento_groupautoswitch')->__('Customer validation started, it will be finished soon')
24
+ );
25
+ }
26
+
27
+ echo json_encode($result);
28
+ }
29
+
30
+ }
app/code/community/Freento/GroupAutoSwitch/etc/adminhtml.xml ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <menu>
4
+ <customer>
5
+ <children>
6
+ <freento_groupautoswitch_log>
7
+ <title>Group auto swicth log</title>
8
+ <action>freento_groupautoswitch/adminhtml_log</action>
9
+ <sort_order>200</sort_order>
10
+ </freento_groupautoswitch_log>
11
+ </children>
12
+ </customer>
13
+ </menu>
14
+ <acl>
15
+ <resources>
16
+ <all>
17
+ <title>Allow Everything</title>
18
+ </all>
19
+ <admin>
20
+ <children>
21
+ <customer>
22
+ <children>
23
+ <freento_groupautoswitch_log>
24
+ <title>Group auto swicth log</title>
25
+ <action>freento_groupautoswitch/adminhtml_log</action>
26
+ <sort_order>200</sort_order>
27
+ </freento_groupautoswitch_log>
28
+ </children>
29
+ </customer>
30
+ <system>
31
+ <children>
32
+ <config>
33
+ <children>
34
+ <freento_groupautoswitch>
35
+ <title>Group auto swicth log</title>
36
+ </freento_groupautoswitch>
37
+ </children>
38
+ </config>
39
+ </children>
40
+ </system>
41
+ </children>
42
+ </admin>
43
+ </resources>
44
+ </acl>
45
+ </config>
app/code/community/Freento/GroupAutoSwitch/etc/config.xml ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <modules>
4
+ <Freento_GroupAutoSwitch>
5
+ <version>1.0.0</version>
6
+ </Freento_GroupAutoSwitch>
7
+ </modules>
8
+ <global>
9
+ <models>
10
+ <freento_groupautoswitch>
11
+ <class>Freento_GroupAutoSwitch_Model</class>
12
+ <resourceModel>freento_groupautoswitch_resource</resourceModel>
13
+ </freento_groupautoswitch>
14
+ <freento_groupautoswitch_resource>
15
+ <class>Freento_GroupAutoSwitch_Model_Resource</class>
16
+ <entities>
17
+ <group>
18
+ <table>freento_groupautoswitch_group</table>
19
+ </group>
20
+ <log>
21
+ <table>freento_groupautoswitch_log</table>
22
+ </log>
23
+ </entities>
24
+ </freento_groupautoswitch_resource>
25
+ </models>
26
+ <blocks>
27
+ <freento_groupautoswitch>
28
+ <class>Freento_GroupAutoSwitch_Block</class>
29
+ </freento_groupautoswitch>
30
+ <adminhtml>
31
+ <rewrite>
32
+ <customer_group_edit_form>Freento_GroupAutoSwitch_Block_Adminhtml_Customer_Group_Edit_Form</customer_group_edit_form>
33
+ </rewrite>
34
+ </adminhtml>
35
+ </blocks>
36
+ <helpers>
37
+ <freento_groupautoswitch>
38
+ <class>Freento_GroupAutoSwitch_Helper</class>
39
+ </freento_groupautoswitch>
40
+ </helpers>
41
+ <resources>
42
+ <freento_groupautoswitch_setup>
43
+ <setup>
44
+ <module>Freento_GroupAutoSwitch</module>
45
+ </setup>
46
+ <connection>
47
+ <use>core_setup</use>
48
+ </connection>
49
+ </freento_groupautoswitch_setup>
50
+ <freento_groupautoswitch_write>
51
+ <connection>
52
+ <use>core_write</use>
53
+ </connection>
54
+ </freento_groupautoswitch_write>
55
+ <freento_groupautoswitch_read>
56
+ <connection>
57
+ <use>core_read</use>
58
+ </connection>
59
+ </freento_groupautoswitch_read>
60
+ </resources>
61
+ <events>
62
+ <customer_save_before>
63
+ <observers>
64
+ <freento_groupautoswitch_customer_save_after>
65
+ <type>singleton</type>
66
+ <class>freento_groupautoswitch/observer</class>
67
+ <method>fillLastGroupChangeDate</method>
68
+ </freento_groupautoswitch_customer_save_after>
69
+ </observers>
70
+ </customer_save_before>
71
+ <sales_order_save_after>
72
+ <observers>
73
+ <freento_groupautoswitch_check_autoswitch>
74
+ <type>singleton</type>
75
+ <class>freento_groupautoswitch/observer</class>
76
+ <method>checkGroupAutoSwitch</method>
77
+ </freento_groupautoswitch_check_autoswitch>
78
+ </observers>
79
+ </sales_order_save_after>
80
+ </events>
81
+ <template>
82
+ <email>
83
+ <freento_groupautoswitch_group_auto_switch translate="label" module="freento_groupautoswitch">
84
+ <label>Group Auto Switch Email Template</label>
85
+ <file>freento/groupautoswitch/auto_switch.html</file>
86
+ <type>html</type>
87
+ </freento_groupautoswitch_group_auto_switch>
88
+ <freento_groupautoswitch_group_downgrade_before translate="label" module="freento_groupautoswitch">
89
+ <label>Group Downgrade Before Email Template</label>
90
+ <file>freento/groupautoswitch/downgrade_before.html</file>
91
+ <type>html</type>
92
+ </freento_groupautoswitch_group_downgrade_before>
93
+ <freento_groupautoswitch_group_downgrade translate="label" module="freento_groupautoswitch">
94
+ <label>Group Downgrade Email Template</label>
95
+ <file>freento/groupautoswitch/downgrade.html</file>
96
+ <type>html</type>
97
+ </freento_groupautoswitch_group_downgrade>
98
+ </email>
99
+ </template>
100
+ </global>
101
+ <adminhtml>
102
+ <layout>
103
+ <updates>
104
+ <freento_groupautoswitch>
105
+ <file>freento/groupautoswitch.xml</file>
106
+ </freento_groupautoswitch>
107
+ </updates>
108
+ </layout>
109
+ </adminhtml>
110
+ <admin>
111
+ <routers>
112
+ <adminhtml>
113
+ <use>admin</use>
114
+ <args>
115
+ <modules>
116
+ <freento_groupautoswitch_admin before="Mage_Adminhtml">Freento_GroupAutoSwitch_Adminhtml</freento_groupautoswitch_admin>
117
+ </modules>
118
+ </args>
119
+ </adminhtml>
120
+ <freento_groupautoswitch>
121
+ <use>admin</use>
122
+ <args>
123
+ <module>Freento_GroupAutoSwitch</module>
124
+ <frontName>freento_groupautoswitch</frontName>
125
+ </args>
126
+ </freento_groupautoswitch>
127
+ </routers>
128
+ </admin>
129
+ <crontab>
130
+ <jobs>
131
+ <!--freento_groupautoswitch_downgrade_check>
132
+ <schedule><cron_expr>15 * * * *</cron_expr></schedule>
133
+ <run><model>freento_groupautoswitch/cron::downgradeCheck</model></run>
134
+ </freento_groupautoswitch_downgrade_check-->
135
+ <!--freento_groupautoswitch_downgrade_before_email>
136
+ <schedule><cron_expr>25 2 * * *</cron_expr></schedule>
137
+ <run><model>freento_groupautoswitch/cron::downgradeBeforeEmail</model></run>
138
+ </freento_groupautoswitch_downgrade_before_email-->
139
+ <!--freento_groupautoswitch_force_fill>
140
+ <schedule><cron_expr>5 * * * *</cron_expr></schedule>
141
+ <run><model>freento_groupautoswitch/cron::forceFill</model></run>
142
+ </freento_groupautoswitch_force_fill-->
143
+ </jobs>
144
+ </crontab>
145
+ <phpunit>
146
+ <suite>
147
+ <modules>
148
+ <Freento_GroupAutoSwitch />
149
+ </modules>
150
+ </suite>
151
+ </phpunit>
152
+ </config>
app/code/community/Freento/GroupAutoSwitch/etc/system.xml ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <tabs>
4
+ <freento translate="label">
5
+ <label>Freento</label>
6
+ <sort_order>1000</sort_order>
7
+ </freento>
8
+ </tabs>
9
+ <sections>
10
+ <freento_groupautoswitch translate="label">
11
+ <tab>freento</tab>
12
+ <label>Customer Group Auto Switch</label>
13
+ <sort_order>100</sort_order>
14
+ <show_in_default>1</show_in_default>
15
+ <show_in_website>1</show_in_website>
16
+ <show_in_store>1</show_in_store>
17
+ <groups translate="label">
18
+ <general>
19
+ <label>Configuration</label>
20
+ <expanded>1</expanded>
21
+ <frontend_type>text</frontend_type>
22
+ <show_in_default>1</show_in_default>
23
+ <show_in_website>1</show_in_website>
24
+ <show_in_store>1</show_in_store>
25
+ <sort_order>10</sort_order>
26
+ <fields>
27
+ <forceFill translate="label">
28
+ <label></label>
29
+ <button_label>Force fill</button_label>
30
+ <frontend_type>button</frontend_type>
31
+ <frontend_model>freento_groupautoswitch/adminhtml_system_config_form_forceFill</frontend_model>
32
+ <sort_order>10</sort_order>
33
+ <show_in_default>1</show_in_default>
34
+ <show_in_website>0</show_in_website>
35
+ <show_in_store>0</show_in_store>
36
+ </forceFill>
37
+ </fields>
38
+ </general>
39
+ </groups>
40
+ </freento_groupautoswitch>
41
+ </sections>
42
+ </config>
app/code/community/Freento/GroupAutoSwitch/sql/freento_groupautoswitch_setup/mysql4-install-1.0.0.php ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ $this->startSetup();
4
+
5
+ $this->run("
6
+
7
+ CREATE TABLE IF NOT EXISTS {$this->getTable('freento_groupautoswitch/group')} (
8
+ `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
9
+ `is_enabled` tinyint(4) NOT NULL,
10
+ `group_id` int(10) unsigned NOT NULL,
11
+ `priority` int(10) unsigned NOT NULL,
12
+ `conditions_serialized` text NOT NULL,
13
+ `stop_further_rules` tinyint(4) NOT NULL,
14
+ `downgrade_template` varchar(255) NOT NULL,
15
+ `notify_on_group_change_template` varchar(255) NOT NULL,
16
+ `text_for_customer` text NOT NULL,
17
+ `downgrade_after` int(11) NOT NULL,
18
+ `downgrade_group` int(11) NOT NULL,
19
+ `notify_customer_before_downgrade` int(11) NOT NULL,
20
+ `notify_customer_before_downgrade_template` varchar(255) NOT NULL,
21
+ PRIMARY KEY (`id`)
22
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
23
+
24
+ CREATE TABLE IF NOT EXISTS {$this->getTable('freento_groupautoswitch/log')} (
25
+ `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
26
+ `details` text,
27
+ `created_at` datetime DEFAULT NULL,
28
+ PRIMARY KEY (`id`)
29
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
30
+
31
+ ");
32
+
33
+ $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
34
+
35
+ $entityTypeId = $setup->getEntityTypeId('customer');
36
+ $attributeSetId = $setup->getDefaultAttributeSetId($entityTypeId);
37
+ $attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
38
+
39
+ $setup->addAttribute('customer', 'group_last_change_date', array(
40
+ 'type' => 'datetime',
41
+ 'input' => 'date',
42
+ 'validate_rules' => 'a:1:{s:16:"input_validation";s:4:"date";}',
43
+ 'visible' => true,
44
+ 'required' => false,
45
+ 'label' => 'Customer Group Last Change Date',
46
+ ));
47
+
48
+ $setup->addAttributeToGroup(
49
+ $entityTypeId,
50
+ $attributeSetId,
51
+ $attributeGroupId,
52
+ 'group_last_change_date',
53
+ '1000'
54
+ );
55
+
56
+ $oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'group_last_change_date');
57
+ $oAttribute->setData('used_in_forms', array('adminhtml_customer'));
58
+ $oAttribute->save();
59
+
60
+ $this->endSetup();
app/design/adminhtml/default/default/layout/freento/groupautoswitch.xml ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <layout>
3
+ <adminhtml_customer_group_new>
4
+ <reference name="left">
5
+ <block type="freento_groupautoswitch/adminhtml_customer_group_edit_tabs" name="freento_groupautoswitch.customergroup.tabs" />
6
+ </reference>
7
+ <reference name="content">
8
+
9
+ </reference>
10
+ </adminhtml_customer_group_new>
11
+ <freento_groupautoswitch_adminhtml_log_index>
12
+ <reference name="content">
13
+ <block type="freento_groupautoswitch/adminhtml_log_grid" name="freento_groupautoswitch.log_grid" />
14
+ </reference>
15
+ </freento_groupautoswitch_adminhtml_log_index>
16
+ </layout>
app/design/adminhtml/default/default/template/freento_groupautoswitch/system/config/forceFill.phtml ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script>
2
+ //<![CDATA[
3
+
4
+ function forceFill() {
5
+ var elem = $('<?php echo $this->getHtmlId() ?>');
6
+
7
+ if(confirm('<?php echo $this->__('Are you sure? Customer groups may change significantly, we recommend to do backup before this action') ?>')) {
8
+ new Ajax.Request('<?php echo $this->getAjaxUrl() ?>', {
9
+ onSuccess: function(response) {
10
+
11
+ var result = 'none';
12
+ try {
13
+ result = response.responseText.evalJSON();
14
+ } catch (e) {
15
+ elem.removeClassName('success').addClassName('fail')
16
+ }
17
+
18
+ if(result.error) {
19
+ $$('#forceFill-results > li')[0].writeAttribute('class', 'error-msg');
20
+ $$('#forceFill-results li.message')[0].update(result.message);
21
+ } else {
22
+ $$('#forceFill-results > li')[0].writeAttribute('class', 'success-msg');
23
+ $$('#forceFill-results li.message')[0].update(result.message);
24
+ }
25
+ $$('#forceFill-results > li')[0].show();
26
+ }
27
+ });
28
+ }
29
+ }
30
+
31
+ //]]>
32
+ </script>
33
+ <ul class="messages" id="forceFill-results">
34
+ <li style="display: none">
35
+ <ul>
36
+ <li class="message"></li>
37
+ </ul>
38
+ </li>
39
+ </ul>
40
+ <button onclick="javascript:forceFill(); return false;" class="scalable" type="button" id="<?php echo $this->getHtmlId() ?>">
41
+ <span><?php echo $this->escapeHtml($this->getButtonLabel()) ?></span>
42
+ </button>
app/etc/modules/Freento_GroupAutoSwitch.xml ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+
3
+ <config>
4
+ <modules>
5
+ <Freento_GroupAutoSwitch>
6
+ <active>true</active>
7
+ <codePool>community</codePool>
8
+ </Freento_GroupAutoSwitch>
9
+ </modules>
10
+ </config>
app/locale/en_US/template/email/freento/groupautoswitch/auto_switch.html ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!--@subject Your level have been changed @-->
2
+
3
+ <!--@vars
4
+ {"store url=\"\"":"Store Url",
5
+ "var logo_url":"Email Logo Image Url",
6
+ "htmlescape var=$customer.name":"Customer Name",
7
+ "store url=\"customer/account/\"":"Customer Account Url",
8
+ "var customer.email":"Customer Email",
9
+ "htmlescape var=$customer.password":"Customer Password"}
10
+ @-->
11
+
12
+ {{template config_path="design/email/header"}}
13
+ {{inlinecss file="email-inline.css"}}
14
+
15
+ <p>Dear {{var customer_name}},</p>
16
+ <p>Your level have been changed to {{var new_group}}. You can see your current level details on the account dashboard.</p>
17
+
18
+ {{template config_path="design/email/footer"}}
app/locale/en_US/template/email/freento/groupautoswitch/downgrade.html ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!--@subject Your level have been changed @-->
2
+
3
+ <!--@vars
4
+ {"store url=\"\"":"Store Url",
5
+ "var logo_url":"Email Logo Image Url",
6
+ "htmlescape var=$customer.name":"Customer Name",
7
+ "store url=\"customer/account/\"":"Customer Account Url",
8
+ "var customer.email":"Customer Email",
9
+ "htmlescape var=$customer.password":"Customer Password"}
10
+ @-->
11
+
12
+ {{template config_path="design/email/header"}}
13
+ {{inlinecss file="email-inline.css"}}
14
+
15
+ <p>Dear {{var customer_name}},</p>
16
+ <p>Your level have been changed to {{var new_group}}. You can see your current level details on the account dashboard.</p>
17
+
18
+ {{template config_path="design/email/footer"}}
app/locale/en_US/template/email/freento/groupautoswitch/downgrade_before.html ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!--@subject Caution! Prevent Your Level Lowering @-->
2
+
3
+ <!--@vars
4
+ {"store url=\"\"":"Store Url",
5
+ "var logo_url":"Email Logo Image Url",
6
+ "htmlescape var=$customer.name":"Customer Name",
7
+ "store url=\"customer/account/\"":"Customer Account Url",
8
+ "var customer.email":"Customer Email",
9
+ "htmlescape var=$customer.password":"Customer Password"}
10
+ @-->
11
+
12
+ {{template config_path="design/email/header"}}
13
+ {{inlinecss file="email-inline.css"}}
14
+
15
+ <p>Dear {{var customer_name}},</p>
16
+ <p>
17
+ To save your current level, you should make a purchase in {{var store_name}} store.
18
+ Please note that if you will not make a purchase in {{var days_to_downgrade}} days, you will be automatically moved to a lower level.
19
+ </p>
20
+
21
+ {{template config_path="design/email/footer"}}
package.xml ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Freento_GroupAutoSwitch</name>
4
+ <version>1.0.0</version>
5
+ <stability>stable</stability>
6
+ <license>Open Software License (OSL 3.0)</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Free Magento extension for automatic customer group switching</summary>
10
+ <description>&lt;ul&gt;&#xD;
11
+ &lt;li&gt;Automatic event based customer group auto switch&lt;/li&gt;&#xD;
12
+ &lt;li&gt;Switch group based on lifetime statistic&lt;/li&gt;&#xD;
13
+ &lt;li&gt;Switch group based on previous group switch date&lt;/li&gt;&#xD;
14
+ &lt;li&gt;Manual group switch&lt;/li&gt;&#xD;
15
+ &lt;li&gt;Sending email notifications on group change&lt;/li&gt;&#xD;
16
+ &lt;li&gt;Downgrade to some group based on customer activity&lt;/li&gt;&#xD;
17
+ &lt;li&gt;Progress bar based on lifetime statistic and current group purchases statistic&lt;/li&gt;&#xD;
18
+ &lt;li&gt;Text alert in custmer account for customer groups&lt;/li&gt;&#xD;
19
+ &lt;li&gt;Ability to see statistic of purchases in current group in customer account&lt;/li&gt;&#xD;
20
+ &lt;li&gt;Individual email welcome messages for customers&lt;/li&gt;&#xD;
21
+ &lt;li&gt;Ability to easily filter customers by group in native customer grid&lt;/li&gt;&#xD;
22
+ &lt;li&gt;Multistore support&lt;/li&gt;&#xD;
23
+ &lt;li&gt;100% free extension&lt;/li&gt;&#xD;
24
+ &lt;/ul&gt;</description>
25
+ <notes>&lt;ul&gt;&#xD;
26
+ &lt;li&gt;Automatic event based customer group auto switch&lt;/li&gt;&#xD;
27
+ &lt;li&gt;Switch group based on lifetime statistic&lt;/li&gt;&#xD;
28
+ &lt;li&gt;Switch group based on previous group switch date&lt;/li&gt;&#xD;
29
+ &lt;li&gt;Manual group switch&lt;/li&gt;&#xD;
30
+ &lt;li&gt;Sending email notifications on group change&lt;/li&gt;&#xD;
31
+ &lt;li&gt;Downgrade to some group based on customer activity&lt;/li&gt;&#xD;
32
+ &lt;li&gt;Progress bar based on lifetime statistic and current group purchases statistic&lt;/li&gt;&#xD;
33
+ &lt;li&gt;Text alert in custmer account for customer groups&lt;/li&gt;&#xD;
34
+ &lt;li&gt;Ability to see statistic of purchases in current group in customer account&lt;/li&gt;&#xD;
35
+ &lt;li&gt;Individual email welcome messages for customers&lt;/li&gt;&#xD;
36
+ &lt;li&gt;Ability to easily filter customers by group in native customer grid&lt;/li&gt;&#xD;
37
+ &lt;li&gt;Multistore support&lt;/li&gt;&#xD;
38
+ &lt;li&gt;100% free extension&lt;/li&gt;&#xD;
39
+ &lt;/ul&gt;</notes>
40
+ <authors><author><name>Alexey Bordachyov</name><user>alex</user><email>alex@freento.com</email></author></authors>
41
+ <date>2016-04-06</date>
42
+ <time>09:19:48</time>
43
+ <contents><target name="magecommunity"><dir name="Freento"><dir name="GroupAutoSwitch"><dir name="Block"><dir name="Adminhtml"><dir name="Customer"><dir name="Group"><dir name="Edit"><dir name="Form"><file name="Autoswitch.php" hash="08da64c21efc3ad6c012848c262b14e7"/><file name="Conditions.php" hash="bc9042435fa87e0a965dffeedc915232"/><file name="Downgrade.php" hash="e112afe52d92e89e835366ff06847830"/><file name="GroupSettings.php" hash="e42f9786569539bb56a69e37a6413fb0"/><file name="Main.php" hash="6e6cd1374d22e524bb310626e70a4a81"/></dir><file name="Form.php" hash="50de39e003457a99842cfd4b130bd834"/><file name="Tabs.php" hash="b976216fb8d6cd8a52521632cd0baee9"/></dir></dir></dir><dir name="Log"><file name="Grid.php" hash="e52501ba190cca79e762eda44c4a1e70"/></dir><file name="Log.php" hash="f096d995e5da87dfbc2dbf2d129bebc1"/><dir name="System"><dir name="Config"><dir name="Form"><file name="ForceFill.php" hash="147c3c65474f306944b668d0c82dc2dd"/></dir></dir></dir></dir></dir><dir name="Exception"><file name="General.php" hash="617043870401288f49ef6a896d034bf2"/></dir><dir name="Helper"><file name="Data.php" hash="d9176fa3eb7afbdeca4bd8d2bb4864a0"/></dir><dir name="Model"><file name="Cron.php" hash="14927760f0132581ed675cfc9259a78f"/><file name="Group.php" hash="4976dedd88f61b3f1fe9c750dbf6cadd"/><file name="Log.php" hash="2d5ce86ca7ae3e90574976b8d3cd081d"/><file name="Observer.php" hash="bff3e531e8726a5326f900213b6b1001"/><dir name="Resource"><dir name="Group"><file name="Collection.php" hash="5646f620c1775f3f006bb77a3c90d821"/></dir><file name="Group.php" hash="ffe248d7aa2b9f667b93505635429660"/><dir name="Log"><file name="Collection.php" hash="ca29a102398066b9de8a6b01fc7aef84"/></dir><file name="Log.php" hash="be66832bfcd29c21c22cbf1e70d2b804"/></dir><dir name="Rule"><dir name="Condition"><file name="Combine.php" hash="aee27cdc2d1677a7100d2f09ef5990b0"/><dir name="Customer"><dir name="Attribute"><file name="Group.php" hash="7b0f6a175ec309ebda496a26fb9337f4"/></dir><dir name="Statistic"><file name="Abstract.php" hash="78cdf63201a12128fce241f7ac9f0f93"/><file name="CurrentGroupSales.php" hash="8c74a85435f17f88abe78e82eafed5ea"/><file name="LifetimeSales.php" hash="94472df9560f747e198a0da60395c040"/></dir></dir></dir></dir></dir><dir name="Test"><dir name="Model"><dir name="Cron"><dir name="expectations"><file name="downgradeCheck.php" hash="e92b0eb7d8d54f731412facd922dde46"/><file name="forceFill.php" hash="351301b821b5d62ba46803dec633ec62"/></dir><dir name="fixtures"><file name="downgradeCheck.php" hash="23fddefcb714ba283c98064484536c66"/><file name="forceFill.php" hash="c7de55cb855ae518c24dfccad6fe1dbd"/></dir><dir name="providers"><file name="downgradeCheck.php" hash="14d3c11893e08c90596769b37ac55a9e"/><file name="forceFill.php" hash="e0efca150b0cb1b020cde5b3e38053f9"/></dir></dir><file name="Cron.php" hash="7b38690fa5cb4442465e14d2b093b112"/><dir name="Group"><dir name="expectations"><file name="switchGroup.php" hash="ed4a7487674df2f93e8a93c4b6eace85"/></dir><dir name="fixtures"><file name="switchGroup.php" hash="15cc865c7c1e44235ea8ea43e24195a7"/></dir><dir name="providers"><file name="switchGroup.php" hash="e9144a7a95ebaef4d851f14201a2a126"/></dir></dir><file name="Group.php" hash="10d19866cb4fdb0f8756f09b4ccf615a"/></dir></dir><dir name="controllers"><dir name="Adminhtml"><dir name="Customer"><file name="GroupController.php" hash="6a89673d0d7b8e13ed702df2bccc4b72"/></dir><file name="LogController.php" hash="be1bfb0c98fb9064cdfc067fae70f674"/><dir name="System"><file name="ConfigController.php" hash="8bcce53cd5f8202020cd2488835fb21c"/></dir></dir></dir><dir name="etc"><file name="adminhtml.xml" hash="f4ec111db6cbed826987e369b4c3a779"/><file name="config.xml" hash="4e3cacac8ce40e4d7e7a9b984cadaf13"/><file name="system.xml" hash="a512e179e14703bd8d49298568b1e8b9"/></dir><dir name="sql"><dir name="freento_groupautoswitch_setup"><file name="mysql4-install-1.0.0.php" hash="b32c4d6cc1f27ca5d626b8d377baa761"/></dir></dir></dir></dir></target><target name="magedesign"><dir name="adminhtml"><dir name="default"><dir name="default"><dir name="layout"><dir name="freento"><file name="groupautoswitch.xml" hash="98670633c5f24bc037591ad238451fef"/></dir></dir><dir name="template"><dir name="freento_groupautoswitch"><dir name="system"><dir name="config"><file name="forceFill.phtml" hash="af149d17a7bf808a63b8822b3bd0adcf"/></dir></dir></dir></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Freento_GroupAutoSwitch.xml" hash="7542ef677d1ccc3444931ce45738a342"/></dir></target><target name="magelocale"><dir name="en_US"><dir name="template"><dir name="email"><dir name="freento"><dir name="groupautoswitch"><file name="auto_switch.html" hash="b3ddd85ee862798513ea6e3d987f47ae"/><file name="downgrade.html" hash="b3ddd85ee862798513ea6e3d987f47ae"/><file name="downgrade_before.html" hash="a6a90d4e653afbe3a507025ead4dafc0"/></dir></dir></dir></dir></dir></target></contents>
44
+ <compatible/>
45
+ <dependencies><required><php><min>5.4.0</min><max>7.0.0</max></php></required></dependencies>
46
+ </package>