Ejemplo n.º 1
0
 protected function processPostData($old_tax)
 {
     if (!waRequest::post()) {
         return $old_tax;
     }
     $tm = $this->tm;
     if (waRequest::post('delete')) {
         if ($old_tax['id']) {
             $tm->deleteById($old_tax['id']);
             $this->trm->deleteByField('tax_id', $old_tax['id']);
             $this->tzcm->deleteByField('tax_id', $old_tax['id']);
         }
         echo json_encode(array('status' => 'ok', 'data' => 'ok'));
         exit;
     }
     //
     // Prepare data for shopTaxes::save()
     //
     $tax_data = waRequest::post('tax');
     if (!is_array($tax_data)) {
         return $old_tax;
     }
     if (!empty($old_tax['id'])) {
         $tax_data['id'] = $old_tax['id'];
     }
     // countries
     $tax_data['countries'] = array();
     $tax_countries = waRequest::post('countries');
     // country global rate: iso3 => float
     if ($tax_countries && is_array($tax_countries)) {
         $tax_country_regions = waRequest::post('country_regions');
         // rates by region: iso3 => region_code => float
         if (!is_array($tax_country_regions)) {
             $tax_country_regions = array();
         }
         foreach ($tax_countries as $country_iso3 => $country_global_rate) {
             $tax_data['countries'][$country_iso3] = array('global_rate' => $country_global_rate);
             if (!empty($tax_country_regions[$country_iso3]) && is_array($tax_country_regions[$country_iso3])) {
                 $tax_data['countries'][$country_iso3]['regions'] = $tax_country_regions[$country_iso3];
             }
         }
     }
     // zip codes
     $tax_data['zip_codes'] = array();
     $zip_codes = waRequest::post('tax_zip_codes');
     $zip_rates = waRequest::post('tax_zip_rates');
     if (is_array($zip_codes) && is_array($zip_rates)) {
         foreach ($zip_codes as $i => $code) {
             if ($code) {
                 $tax_data['zip_codes'][$code] = ifset($zip_rates[$i], 0);
             }
         }
     }
     return shopTaxes::save($tax_data);
 }
Ejemplo n.º 2
0
 /**
 * Creates new or modifies existing tax.
 *
 * Examples:
 *
 
    // Pass id to modify existing tax:
    shopTaxes::save(array(
        'id' => 100500,
        ... see below ...
    ));
 
    // Pass no id to create new tax:
    shopTaxes::save(array(
        'name' => '...',
        'included' => true,           // true for taxes included in price, false for added
        'address_type' => 'shipping', // shipping|billing
 
        // rates by zip code: zip code => %
        'zip_codes' => array(
            '1234*' => 1.11,
            '123*' => 2.22,
        ),
 
        // rates by country and/or country regions
        'countries' => array(
 
            // Country in simple mode: global rate only, no by-region setup
            'rus' => array(
                'global_rate' => 10.1, // %
            ),
 
            // Country in advanced mode: rate by region
            'usa' => array(
                'regions' => array(
                    'AK' => 2.34, // %
                    'AL' => 3.45,
                ),
            ),
 
            // Country in super-advanced mode: global rate and by-region modifiers
            'can' => array(
                'global_rate' => 5,
                'regions' => array(
                    'BC' => array(
                        'name' => 'ASDF',
                        'tax_value' => 1.23, // %
                        'tax_value_modifier' => '+',
                    ),
                    'MB' => array(
                        'name' => 'QWER',
                        'tax_value' => 1.05,
                        'tax_value_modifier' => '*',
                    ),
                    'NB' => array(
                        'name' => 'ZXCV',
                        'tax_value' => 6.15,
                        'tax_value_modifier' => '', // replaces global rate
                    ),
                ),
            ),
 
            // Use special codes instead of country-iso3 for groups:
            // '%AL' = All countries
            // '%EU' = All european countries
            // '%RW' = Rest of the world
        ),
    ));
 
 *
 * @param array $tax_data
 * @return array DB row from shop_tax, including id (useful for new record)
 */
 public static function save($tax_data)
 {
     if (!is_array($tax_data)) {
         throw new waException('$tax_data must be an array.');
     }
     $tm = new shopTaxModel();
     //
     // shop_tax
     //
     $tax = $tm->getEmptyRow();
     unset($tax['id']);
     $tax = array_intersect_key($tax_data, $tax) + $tax;
     // Default values (instead of validation)
     if (!strlen(ifset($tax['name'], ''))) {
         $tax['name'] = _w('<no name>');
     }
     $tax['included'] = $tax['included'] ? 1 : 0;
     $tax['address_type'] = $tax['address_type'] === 'shipping' ? 'shipping' : 'billing';
     // Save into shop_tax
     if (empty($tax_data['id'])) {
         $tax['id'] = $tm->insert($tax);
     } else {
         $tm->updateById($tax_data['id'], $tax);
         $tax['id'] = $tax_data['id'];
     }
     //
     // shop_tax_zip_codes
     //
     $tzcm = new shopTaxZipCodesModel();
     $tzcm->deleteByField('tax_id', $tax['id']);
     if (!empty($tax_data['zip_codes']) && is_array($tax_data['zip_codes'])) {
         $rows = array();
         foreach ($tax_data['zip_codes'] as $code => $rate) {
             if (!$code) {
                 continue;
             }
             $code = str_replace('*', '%', $code);
             $rate = (double) str_replace(',', '.', ifempty($rate, '0'));
             $rows[$code] = array('tax_id' => $tax['id'], 'zip_expr' => $code, 'tax_value' => $rate, 'sort' => count($rows));
         }
         if ($rows) {
             $tzcm->multipleInsert(array_values($rows));
         }
     }
     //
     // shop_tax_regions
     //
     $trm = new shopTaxRegionsModel();
     $trm->deleteByField('tax_id', $tax['id']);
     if (!empty($tax_data['countries']) && is_array($tax_data['countries'])) {
         $region_rates = array();
         foreach ($tax_data['countries'] as $country_iso3 => $country_data) {
             $country_global_rate = (double) str_replace(',', '.', ifempty($country_data['global_rate'], '0'));
             $no_region_added = true;
             $params_added = false;
             if (!empty($country_data['regions']) && is_array($country_data['regions'])) {
                 foreach ($country_data['regions'] as $region_code => $region_data) {
                     if (is_array($region_data)) {
                         $tax_value_modifier = ifempty($region_data['tax_value_modifier']);
                         $tax_value = (double) str_replace(',', '.', ifempty($region_data['tax_value'], '0'));
                         $tax_name = ifempty($region_data['name']);
                     } else {
                         $tax_name = null;
                         $tax_value_modifier = '';
                         $tax_value = (double) str_replace(',', '.', $region_data);
                     }
                     $params = null;
                     if ($tax_value_modifier) {
                         $params = serialize(array('tax_value_modifier' => $tax_value_modifier, 'tax_value' => $tax_value));
                         switch ($tax_value_modifier) {
                             case '*':
                                 $tax_value *= $country_global_rate;
                                 break;
                             case '+':
                                 if ($country_global_rate) {
                                     $tax_value += $country_global_rate;
                                 } else {
                                     $params = null;
                                 }
                                 break;
                         }
                         if ($tax_value == $country_global_rate) {
                             continue;
                         }
                     }
                     $region_rates[] = array('tax_id' => $tax['id'], 'tax_name' => $tax_name, 'country_iso3' => $country_iso3, 'region_code' => $region_code, 'tax_value' => $tax_value, 'params' => $params);
                     if ($params) {
                         $params_added = true;
                     }
                     $no_region_added = false;
                 }
             }
             if ($no_region_added || $params_added || $country_global_rate > 0) {
                 $region_rates[] = array('tax_id' => $tax['id'], 'tax_name' => null, 'country_iso3' => $country_iso3, 'region_code' => null, 'tax_value' => $country_global_rate, 'params' => null);
             }
         }
         if ($region_rates) {
             $trm->multipleInsert($region_rates);
         }
     }
     return $tax;
 }