Projet

Général

Profil

Révision 74f6bef0

Ajouté par Assos Assos il y a plus de 10 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/commerce_price_flexyformatter/commerce_price_flexyformatter.module
25 25
      'label' => t('Formatted amount with selected components'),
26 26
      'field types' => array('commerce_price'),
27 27
      'settings' => array(
28
        'selection' => array('base_price' => 'base_price'),
29
        'reference' => array(),
28
        'base' => array('selection' => array('base_price' => 'base_price'), 'weight' => 0, 'calc' => 'default'),
29
        'reference1' => array('class' => '.flexy-striked', 'weight' => -10, 'calc' => 'default'),
30
        'reference2' => array('weight' => 10, 'calc' => 'default'),
30 31
        // need this to load all price components, hoping will not be changed
31 32
        'calculation' => 'calculated_sell_price',
32 33
      ),
......
42 43

  
43 44
  $element = array();
44 45
  $defaults = array();
45
  $reference_defaults = array();
46

  
46
  $reference1_defaults = $reference2_defaults = array();
47
  $methods = array('default' => 'actual values',
48
                    'reference1' => 'calculated on reference1 selected components',
49
                    'reference2' => 'calculated on reference2 selected components');
47 50
  // Get all the price component types
48 51
  $options = commerce_price_component_titles();
49 52
  
50
  foreach ($settings['selection'] as $key => $value) {
53
  foreach ($settings['base']['selection'] as $key => $value) {
51 54
    if ($value != '0') { $defaults[] = $key; }
52 55
  }
53
  foreach ($settings['reference']['selection'] as $key => $value) {
54
    if ($value != '0') { $reference_defaults[] = $key; }
56
  foreach ($settings['reference1']['selection'] as $key => $value) {
57
    if ($value != '0') { $reference1_defaults[] = $key; }
58
  }
59
  foreach ($settings['reference2']['selection'] as $key => $value) {
60
    if ($value != '0') { $reference2_defaults[] = $key; }
55 61
  }
56 62

  
57
  $element['selection'] = array(
63
  $element['base']['selection'] = array(
58 64
    '#type' => 'checkboxes',
59 65
    '#options' => $options,
60 66
    '#default_value' => $defaults,
61 67
  );
62
  $element['reference'] = array (
68
  $element['base']['label'] = array(
69
    '#type' => 'textfield',
70
    '#title' => t('Label'),
71
    '#default_value' => $settings['base']['label'],
72
    '#size' => 20,
73
  );
74
  $element['base']['class'] = array(
75
    '#type' => 'textfield',
76
    '#title' => t('Custom class'),
77
    '#default_value' => $settings['base']['class'],
78
    '#size' => 20,
79
  );
80
  $element['base']['calc'] = array(
81
    '#type' => 'radios',
82
    '#options' => $methods,
83
    '#title' => t('Tax calculation method'),
84
    '#default_value' => $settings['base']['calc'],
85
  );
86
  $element['base']['weight'] = array(
87
    '#type' => 'textfield',
88
    '#title' => t('Weight'),
89
    '#default_value' => $settings['base']['weight'],
90
    '#size' => 6,
91
  );
92
  $element['reference1'] = array (
93
    '#type' => 'fieldset',
94
    '#title' => t('1st reference amount'),
95
    '#collapsible' => TRUE,
96
    '#collapsed' => ($settings['reference1']['enabled']? FALSE : TRUE),
97
  );
98
  $element['reference1']['enabled'] = array(
99
    '#title' => t('Enable'),
100
    '#type' => 'checkbox',
101
    '#default_value' => $settings['reference1']['enabled'],
102
  );
103
  $element['reference1']['selection'] = array(
104
    '#type' => 'checkboxes',
105
    '#options' => $options,
106
    '#default_value' => $reference1_defaults,
107
  );
108
  $element['reference1']['label'] = array(
109
    '#type' => 'textfield',
110
    '#title' => t('Label'),
111
    '#default_value' => $settings['reference1']['label'],
112
    '#size' => 20,
113
  );
114
  $element['reference1']['class'] = array(
115
    '#type' => 'textfield',
116
    '#title' => t('Custom class'),
117
    '#default_value' => $settings['reference1']['class'],
118
    '#size' => 20,
119
  );
120
  $element['reference1']['calc'] = array(
121
    '#type' => 'radios',
122
    '#options' => $methods,
123
    '#title' => t('Tax calculation method'),
124
    '#default_value' => $settings['reference1']['calc'],
125
  );
126
  $element['reference1']['weight'] = array(
127
    '#type' => 'textfield',
128
    '#title' => t('Weight'),
129
    '#default_value' => $settings['reference1']['weight'],
130
    '#size' => 6,
131
  );
132
  $element['reference2'] = array (
63 133
    '#type' => 'fieldset',
64
    '#title' => t('Reference amount'),
134
    '#title' => t('2nd reference amount'),
65 135
    '#collapsible' => TRUE,
66
    '#collapsed' => ($settings['reference']['enabled']? FALSE : TRUE),
136
    '#collapsed' => ($settings['reference2']['enabled']? FALSE : TRUE),
67 137
  );
68
  $element['reference']['enabled'] = array(
138
  $element['reference2']['enabled'] = array(
69 139
    '#title' => t('Enable'),
70 140
    '#type' => 'checkbox',
71
    '#default_value' => $settings['reference']['enabled'],
141
    '#default_value' => $settings['reference2']['enabled'],
72 142
  );
73
  $element['reference']['selection'] = array(
143
  $element['reference2']['selection'] = array(
74 144
    '#type' => 'checkboxes',
75 145
    '#options' => $options,
76
    '#default_value' => $reference_defaults,
146
    '#default_value' => $reference2_defaults,
147
  );
148
  $element['reference2']['label'] = array(
149
    '#type' => 'textfield',
150
    '#title' => t('Label'),
151
    '#default_value' => $settings['reference2']['label'],
152
    '#size' => 20,
153
  );
154
  $element['reference2']['class'] = array(
155
    '#type' => 'textfield',
156
    '#title' => t('Custom class'),
157
    '#default_value' => $settings['reference2']['class'],
158
    '#size' => 20,
159
  );
160
  $element['reference2']['calc'] = array(
161
    '#type' => 'radios',
162
    '#options' => $methods,
163
    '#title' => t('Tax calculation method'),
164
    '#default_value' => $settings['reference2']['calc'],
165
  );
166
  $element['reference2']['weight'] = array(
167
    '#type' => 'textfield',
168
    '#title' => t('Weight'),
169
    '#default_value' => $settings['reference2']['weight'],
170
    '#size' => 6,
77 171
  );
78 172

  
79 173
  return $element;
......
88 182

  
89 183
  $summary = array();
90 184
  $types = commerce_price_component_titles();
185
  $methods = array('default' => 'actual values',
186
                    'reference1' => 'calculated on reference1 selected components',
187
                    'reference2' => 'calculated on reference2 selected components');
91 188
  $components['list'] = '';
92 189
  $components['count'] = 0;
93
  foreach($settings['selection'] as $key => $value) {
190
  foreach($settings['base']['selection'] as $key => $value) {
94 191
    if ($value != '0') {
95 192
      $components['count']++;
96 193
      if ($components['count'] > 1) {$components['list'] .= ', ';}
......
101 198
    $summary[] = t('No components selected');
102 199
  }
103 200
  else {
104
    $summary[] = format_plural($components['count'], 'Selected component is !list.', 'Selected components are !list.', array('!list' => $components['list']));
105
    if ($settings['reference']['enabled']) {
201
    $description = ($settings['base']['label']?$settings['base']['label'].' - selected ':'Selected ') .
202
                   format_plural($components['count'], 'component is !list', 'components are !list', array('!list' => $components['list'])) .
203
                   ' - ' . t('weight: ') . $settings['base']['weight'] . ' - ' . t('tax ') . $methods[$settings['base']['calc']] .'.';
204
    $summary[] = $description;
205
    if ($settings['reference1']['enabled']) {
206
      $reference['list'] = '';
207
      $reference['count'] = 0;
208
      foreach($settings['reference1']['selection'] as $key => $value) {
209
        if ($value != '0') {
210
          $reference['count']++;
211
          if ($reference['count'] > 1) {$reference['list'] .= ', ';}
212
          $reference['list'] .= $types[$key];
213
        }
214
      }
215
      $description = ($settings['reference1']['label']?$settings['reference1']['label'].' - selected ':'1st reference amount enabled with selected ') .
216
                     format_plural($reference['count'], 'component is !list', 'components are !list', array('!list' => $reference['list'])) .
217
                     ' - ' . t('weight: ') . $settings['reference1']['weight'] . ' - ' . t('tax ') . 
218
                     $methods[$settings['reference1']['calc']] .'.';
219
     $summary[] = $description;
220
    }
221
    else {
222
      $summary[] = t('1st reference amount not enabled.');
223
    }
224
    if ($settings['reference2']['enabled']) {
106 225
      $reference['list'] = '';
107 226
      $reference['count'] = 0;
108
      foreach($settings['reference']['selection'] as $key => $value) {
227
      foreach($settings['reference2']['selection'] as $key => $value) {
109 228
        if ($value != '0') {
110 229
          $reference['count']++;
111 230
          if ($reference['count'] > 1) {$reference['list'] .= ', ';}
112 231
          $reference['list'] .= $types[$key];
113 232
        }
114 233
      }
115
      $summary[] = t('Reference amount enabled with !list.', array('!list' => $reference['list']));
234
      $description = ($settings['reference2']['label']?$settings['reference2']['label'].' - selected ':'2nd reference amount enabled with selected ') .
235
                     format_plural($reference['count'], 'component is !list', 'components are !list', array('!list' => $reference['list'])) .
236
                     ' - ' . t('weight: ') . $settings['reference2']['weight'] . ' - ' . t('tax ') . 
237
                     $methods[$settings['reference2']['calc']] .'.';
238
     $summary[] = $description;
116 239
    }
117 240
    else {
118
      $summary[] = t('Reference amount not enabled.');
241
      $summary[] = t('2nd reference amount not enabled.');
119 242
    }
120 243
  }
121
  
244

  
122 245
  return implode('<br />', $summary);
123 246
}
124 247
/**
......
139 262
 */
140 263
function commerce_price_flexyformatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
141 264
  $element = array();
142

  
143 265
  // Loop through each price value in this field.
144 266
  foreach ($items as $delta => $item) {
145 267
    // Do not render a price if the amount is NULL (i.e. non-zero empty value).
......
148 270
      // price was not available as opposed to just leaving a blank.
149 271
      continue;
150 272
    }
151

  
152 273
    // Theme the display of the price based on the display type.
153 274
    switch ($display['type']) {
154 275

  
155 276
      case 'commerce_price_flexyformatter_selected_amount':
156 277
        // Build an array of component display titles and their prices.
157
        $components = array();
158
        $amount = $reference_amount = 0;
159
        $difference = $reference_difference = 0;        
278
        $tax_base = array('reference1' => 0, 'reference2' => 0);        
279
        $amount = $reference1_amount = $reference2_amount = 0;
280

  
281
        // calc tax base
160 282
        foreach ($item['data']['components'] as $key => $component) {
161
          if ($display['settings']['selection'][$component['name']] != '0') {
162
            $amount += $component['price']['amount'];
163
            if (isset($component['price']['data']['tax_rate'])) {
164
              $amount -= $component['price']['data']['tax_rate']['rate'] * $difference;
283
          if (!isset($component['price']['data']['tax_rate'])) {
284
           if (isset($display['settings']['reference1']['selection'][$component['name']]) 
285
             && $display['settings']['reference1']['selection'][$component['name']] != '0') {
286
              $tax_base['reference1'] += $component['price']['amount'];
287
           }
288
           if (isset($display['settings']['reference2']['selection'][$component['name']]) 
289
             && $display['settings']['reference2']['selection'][$component['name']] != '0') {
290
              $tax_base['reference2'] += $component['price']['amount'];
165 291
            }
166 292
          }
167
          else {
168
            $difference += $component['price']['amount'];
169
          }
170 293
        }
171
        // Check if reference amount is ebabled
172
        if ($display['settings']['reference']['enabled']) {
173
          $components = array();
174
          // reset($item['data']['components']);
175
          foreach ($item['data']['components'] as $key => $component) {
176
            if ($display['settings']['reference']['selection'][$component['name']] != '0') {
177
              $reference_amount += $component['price']['amount'];
178
              if (isset($component['price']['data']['tax_rate'])) {
179
                $reference_amount -= $component['price']['data']['tax_rate']['rate'] * $reference_difference;
180
              }
181
            }
182
            else {
183
              $reference_difference += $component['price']['amount'];
184
            }
185
          }
294
        $amount = commerce_price_flexyformatter_amount_calc($item['data']['components'],$display['settings']['base'],$tax_base);
295
        if (isset($display['settings']['reference1']['enabled']) && $display['settings']['reference1']['enabled']) {
296
          $reference1_amount = commerce_price_flexyformatter_amount_calc($item['data']['components'],$display['settings']['reference1'],$tax_base);
297
        }
298
        if (isset($display['settings']['reference2']['enabled']) && $display['settings']['reference2']['enabled']) {
299
          $reference2_amount = commerce_price_flexyformatter_amount_calc($item['data']['components'],$display['settings']['reference2'],$tax_base);
186 300
        }
301

  
187 302
        $element[$delta] = array(
188 303
          '#markup' => theme('commerce_price_flexyformatter_selected_amount', 
189
            array('amount' => $amount,
190
              'reference_enabled' => $display['settings']['reference']['enabled'],
191
              'reference_amount' => $reference_amount,
304
            array(
305
              'base' => array(
306
                'amount' => $amount,
307
                'label' => (isset($display['settings']['base']['label'])?$display['settings']['base']['label']:null),
308
                'class' => (isset($display['settings']['base']['class'])?$display['settings']['base']['class']:null),
309
                'weight' => (isset($display['settings']['base']['weight'])?$display['settings']['base']['weight']:null),
310
              ),
311
              'reference1' => array(
312
                'amount' => (isset($reference1_amount)?$reference1_amount:null),
313
                'label' => (isset($display['settings']['reference1']['label'])?$display['settings']['reference1']['label']:null),
314
                'class' => (isset($display['settings']['reference1']['class'])?$display['settings']['reference1']['class']:null),
315
                'weight' => (isset($display['settings']['reference1']['weight'])?$display['settings']['reference1']['weight']:null),
316
                'enabled' => (isset($display['settings']['reference1']['enabled'])?$display['settings']['reference1']['enabled']:0),
317
              ),
318
              'reference2' => array(
319
                'amount' => (isset($reference2_amount)?$reference2_amount:null),
320
                'label' => (isset($display['settings']['reference2']['label'])?$display['settings']['reference2']['label']:null),
321
                'class' => (isset($display['settings']['reference2']['class'])?$display['settings']['reference2']['class']:null),
322
                'weight' => (isset($display['settings']['reference2']['weight'])?$display['settings']['reference2']['weight']:null),
323
                'enabled' => (isset($display['settings']['reference2']['enabled'])?$display['settings']['reference2']['enabled']:0),
324
              ),
192 325
              'currency_code' => $item['currency_code'],
193
              )
194
            ),
326
            )
327
          ),
195 328
        );
196 329
      break;
197 330
    }
......
205 338
 * @param $variables
206 339
 */
207 340
function theme_commerce_price_flexyformatter_selected_amount($variables) {
208
  if ($variables['reference_enabled'] && ($variables['amount'] != $variables['reference_amount'])) {
209
  // Add the CSS styling.
341
  $output = '';
342
  uasort($variables, 'drupal_sort_weight');
210 343
  drupal_add_css(drupal_get_path('module', 'commerce_price_flexyformatter') . '/commerce_price_flexyformatter.css');
211
    $output = '<span class="flexy-reference">' . commerce_currency_format($variables['reference_amount'], $variables['currency_code']) . '</span> ';
212
    $output .= '<span class="flexy-amount has-reference">' . commerce_currency_format($variables['amount'], $variables['currency_code']) . '</span>';
213
  }
214
  else {
215
    $output = '<span class="flexy-amount has-no-reference">' . commerce_currency_format($variables['amount'], $variables['currency_code']) . '</span>';
344
  foreach ($variables as $key => $variable) {
345
   if (is_numeric($variable['weight']) && ($key == 'base' || $variable['enabled'])) {
346
    $output .= '<span class=flexy-item' . ($variable['class']?' ' . $variable['class']:'') . '><span class"flexy-title">' . $variable['label'] . '</span><span class="flexy-amount">' . commerce_currency_format($variable['amount'], $variables['currency_code']) . '</span></span> ';
347
   }
216 348
  }
349

  
217 350
  return $output; 
218 351
}
352
/**
353
 * Calculate amount to be displayed.
354
 *
355
 * @param array $components
356
 * @param array $settings
357
 * @param array $tax_base
358
 */
359

  
360
function commerce_price_flexyformatter_amount_calc($components,$settings,$tax_base) {
361
  $amount = 0;
362
  foreach ($components as $key => $component) {
363
    if (isset($settings['selection'][$component['name']]) && $settings['selection'][$component['name']] != '0') {
364
      if (!isset($component['price']['data']['tax_rate'])
365
        or (isset($settings['calc']) 
366
          and $settings['calc'] == 'default')) {
367
        $amount += $component['price']['amount'];
368
      } else {
369
        $amount += $component['price']['data']['tax_rate']['rate'] * $tax_base[$settings['calc']];
370
      }
371
    }
372
  }
373
  return $amount;
374
}

Formats disponibles : Unified diff