PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/shop/vatrules.php

https://github.com/eeggenberger/ezpublish
PHP | 199 lines | 142 code | 32 blank | 25 comment | 39 complexity | cd28d65567f84f19628517bedd5ac798 MD5 | raw file
  1. <?php
  2. /**
  3. * @copyright Copyright (C) 1999-2011 eZ Systems AS. All rights reserved.
  4. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  5. * @version //autogentag//
  6. * @package kernel
  7. */
  8. /**
  9. * Find errors in VAT charging rules.
  10. *
  11. * \return list of errors, or false if no errors found.
  12. */
  13. function findErrors( $vatRules )
  14. {
  15. $errors = false;
  16. // 1. Check if default rule exists.
  17. $defaultRuleExists = false;
  18. foreach ( $vatRules as $rule )
  19. {
  20. if ( $rule->attribute( 'country' ) == '*' &&
  21. !$rule->attribute( 'product_categories' ) )
  22. {
  23. $defaultRuleExists = true;
  24. break;
  25. }
  26. }
  27. if ( !$defaultRuleExists && count( $vatRules ) > 0 )
  28. $errors[] = ezpI18n::tr( 'kernel/shop/vatrules', 'No default rule found. ' .
  29. 'Please add rule having "Any" country and "Any" category.' );
  30. // 2. Check for conflicting rules.
  31. // Conflicting rules are those having the same country and equal or intersecting categories sets.
  32. $vatRulesCount = count( $vatRules );
  33. for ( $i=0; $i < $vatRulesCount; $i++ )
  34. {
  35. $iRule = $vatRules[$i];
  36. $iCountry = $iRule->attribute( 'country_code' );
  37. $iCategories = $iRule->attribute( 'product_categories_names' );
  38. for ( $j=$i+1; $j < $vatRulesCount; $j++ )
  39. {
  40. $jRule = $vatRules[$j];
  41. $jCountry = $jRule->attribute( 'country_code' );
  42. if ( $iCountry != $jCountry )
  43. continue;
  44. $jCategories = $jRule->attribute( 'product_categories_names' );
  45. // Multiple default rules.
  46. if ( !$iCategories && !$jCategories )
  47. {
  48. if ( $iCountry == '*' )
  49. {
  50. $errorMessage = "Conflict: There are multiple default rules.";
  51. $errors[] = ezpI18n::tr( 'kernel/shop/vatrules', $errorMessage );
  52. }
  53. else
  54. {
  55. $errorMessage = "Conflict: There are multiple default rules for country '%1'.";
  56. $errors[] = ezpI18n::tr( 'kernel/shop/vatrules', $errorMessage, null, array( $iCountry ) );
  57. }
  58. }
  59. // Intersecting rules.
  60. elseif ( $iCategories && $jCategories && $commonCategories = array_intersect( $iCategories, $jCategories ) )
  61. {
  62. if ( $iCountry == '*' )
  63. {
  64. $errorMessage = "Conflict: The following categories for any country are mentioned in multiple rules: %2.";
  65. $errors[] = ezpI18n::tr( 'kernel/shop/vatrules', $errorMessage, null, array( $iCountry, join( ',', $commonCategories ) ) );
  66. }
  67. else
  68. {
  69. $errorMessage = "Conflict: The following categories for country '%1' are mentioned in multiple rules: %2.";
  70. $errors[] = ezpI18n::tr( 'kernel/shop/vatrules', $errorMessage, null, array( $iCountry, join( ',', $commonCategories ) ) );
  71. }
  72. }
  73. }
  74. }
  75. if ( is_array( $errors ) )
  76. {
  77. // Remove duplicated error messages.
  78. $errors = array_unique( $errors );
  79. sort( $errors );
  80. }
  81. return $errors;
  82. }
  83. /**
  84. * Auxiliary function used to sort VAT rules.
  85. *
  86. * Rules are sorted by country and categories.
  87. * Any specific categories list or country is considered less than '*' (Any).
  88. */
  89. function compareVatRules($a, $b)
  90. {
  91. // Compare countries.
  92. $aCountry = $a->attribute( 'country' );
  93. $bCountry = $b->attribute( 'country' );
  94. if ( $aCountry != $bCountry )
  95. {
  96. if ( $aCountry == '*' )
  97. return 1;
  98. if ( $bCountry == '*' )
  99. return -1;
  100. return ( $aCountry < $bCountry ? -1 : 1 );
  101. }
  102. // Ok, countries are equal. Let's compare categories then.
  103. if ( $a->attribute( 'product_categories' ) )
  104. $aCategory = $a->attribute( 'product_categories_string' );
  105. else
  106. $aCategory = '*';
  107. if ( $b->attribute( 'product_categories' ) )
  108. $bCategory = $b->attribute( 'product_categories_string' );
  109. else
  110. $bCategory = '*';
  111. if ( $aCategory != $bCategory )
  112. {
  113. if ( $aCategory == '*' )
  114. return 1;
  115. if ( $bCategory == '*' )
  116. return -1;
  117. return ( $aCategory < $bCategory ? -1 : 1 );
  118. }
  119. return 0;
  120. }
  121. $module = $Params['Module'];
  122. $http = eZHTTPTool::instance();
  123. $tpl = eZTemplate::factory();
  124. if ( $http->hasPostVariable( "AddRuleButton" ) )
  125. {
  126. return $module->redirectTo( $module->functionURI( "editvatrule" ) );
  127. }
  128. if ( $http->hasPostVariable( "RemoveRuleButton" ) )
  129. {
  130. if ( !$http->hasPostVariable( "RuleIDList" ) )
  131. $ruleIDList = array();
  132. else
  133. $ruleIDList = $http->postVariable( "RuleIDList" );
  134. $db = eZDB::instance();
  135. $db->begin();
  136. foreach ( $ruleIDList as $ruleID )
  137. eZVatRule::removeVatRule( $ruleID );
  138. $db->commit();
  139. }
  140. if ( $http->hasPostVariable( "SaveCategoriesButton" ) )
  141. {
  142. $db = eZDB::instance();
  143. $db->begin();
  144. foreach ( $productCategories as $cat )
  145. {
  146. $id = $cat->attribute( 'id' );
  147. if ( !$http->hasPostVariable( "category_name_" . $id ) )
  148. continue;
  149. $name = $http->postVariable( "category_name_" . $id );
  150. $cat->setAttribute( 'name', $name );
  151. $cat->store();
  152. }
  153. $db->commit();
  154. return $module->redirectTo( $module->functionURI( "productcategories" ) );
  155. }
  156. $vatRules = eZVatRule::fetchList();
  157. $errors = findErrors( $vatRules );
  158. usort( $vatRules, 'compareVatRules' );
  159. $tpl->setVariable( 'rules', $vatRules );
  160. $tpl->setVariable( 'errors', $errors );
  161. $path = array();
  162. $path[] = array( 'text' => ezpI18n::tr( 'kernel/shop/vatrules', 'VAT rules' ),
  163. 'url' => false );
  164. $Result = array();
  165. $Result['path'] = $path;
  166. $Result['content'] = $tpl->fetch( "design:shop/vatrules.tpl" );
  167. ?>