Exemple #1
0
 /**
  * Returns proper tax rate if tax service is running.
  *
  * @param                $taxClass string Tax class.
  * @param OrderInterface $order    Order to calculate taxes for.
  *
  * @return float Tax rate.
  */
 public static function getRate($taxClass, $order)
 {
     if (self::$service !== null) {
         return self::$service->getRate($taxClass, $order);
     }
     return 0;
 }
Exemple #2
0
 public function __construct(Wordpress $wp, \Jigoshop\Core\Options $options, ProductServiceInterface $productService, TaxServiceInterface $taxService)
 {
     $this->wp = $wp;
     $this->options = $options;
     $this->productService = $productService;
     if ($this->options->get('tax.included')) {
         $address = new Customer\Address();
         $address->setCountry($this->options->get('general.country'));
         $address->setState($this->options->get('general.state'));
         $this->taxes = $taxService->getDefinitions($address);
     }
     $wp->addAction('wp_ajax_jigoshop.admin.migration.products', array($this, 'ajaxMigrationProducts'), 10, 0);
 }
Exemple #3
0
 /**
  * Validate and sanitize input values.
  *
  * @param array $settings Input fields.
  *
  * @return array Sanitized and validated output.
  * @throws ValidationException When some items are not valid.
  */
 public function validate($settings)
 {
     // TODO: Re-enable
     //		$settings['included'] = $settings['included'] == 'on';
     $settings['shipping'] = $settings['shipping'] == 'on';
     $classes = $settings['classes'];
     $settings['classes'] = array();
     foreach ($classes['class'] as $key => $class) {
         $settings['classes'][] = array('class' => $class, 'label' => $classes['label'][$key]);
     }
     $settings['defaults']['taxable'] = $settings['defaults']['taxable'] == 'on';
     $settings['defaults']['classes'] = array_filter($settings['defaults']['classes'], function ($class) use($classes) {
         return in_array($class, $classes['class']);
     });
     if (!isset($settings['rules'])) {
         $settings['rules'] = array('id' => array());
     }
     $this->taxService->removeAllExcept($settings['rules']['id']);
     $currentKey = 0;
     foreach ($settings['rules']['id'] as $key => $id) {
         if (empty($id) && $settings['rules']['compound'][$key + 1] == 'on') {
             $currentKey++;
         }
         $this->taxService->save(array('id' => $id, 'rate' => $settings['rules']['rate'][$key], 'is_compound' => $settings['rules']['compound'][$key + $currentKey] == 'on', 'label' => $settings['rules']['label'][$key], 'class' => $settings['rules']['class'][$key], 'country' => $settings['rules']['country'][$key], 'states' => $settings['rules']['states'][$key], 'postcodes' => $settings['rules']['postcodes'][$key]));
     }
     unset($settings['rules']);
     if (!in_array($settings['price_tax'], array('with_tax', 'without_tax'))) {
         $this->messages->addWarning(sprintf(__('Invalid prices option: "%s". Value set to %s.', 'jigoshop'), $settings['price_tax'], __('Without tax', 'jigoshop')));
         $settings['price_tax'] = 'without_tax';
     }
     return $settings;
 }
Exemple #4
0
 /**
  * Migrates data from old format to new one.
  * @param mixed $options
  * @return bool migration options status: success or not
  */
 public function migrate($options = null)
 {
     $wpdb = $this->wp->getWPDB();
     //		Open transaction for save migration emails
     $var_autocommit_sql = $wpdb->get_var("SELECT @@AUTOCOMMIT");
     try {
         $this->checkSql();
         $wpdb->query("SET AUTOCOMMIT=0");
         $this->checkSql();
         $wpdb->query("START TRANSACTION");
         $this->checkSql();
         $options = $this->wp->getOption('jigoshop_options');
         $this->checkSql();
         $transformations = $this->_getTransformations();
         $transformations = $this->_addShippingTransformations($transformations);
         $transformations = $this->_addPaymentTransformations($transformations);
         foreach ($transformations as $old => $new) {
             if (array_key_exists($old, $options)) {
                 $value = $this->_transform($old, $options[$old]);
                 if ($old == 'jigoshop_default_country') {
                     $tmp = explode(':', $value);
                     if (count($tmp) > 1) {
                         $this->options->update('general.state', $tmp[1]);
                         $this->checkSql();
                         $value = $tmp[0];
                     }
                 }
             }
             if ($value !== null) {
                 $this->options->update($new, $value);
                 $this->checkSql();
             }
         }
         // Migrate tax rates
         if (!is_array($options['jigoshop_tax_rates'])) {
             $options['jigoshop_tax_rates'] = array();
         }
         $options['jigoshop_tax_rates'] = array_values($options['jigoshop_tax_rates']);
         for ($i = 0, $endI = count($options['jigoshop_tax_rates']); $i < $endI;) {
             $tax = $options['jigoshop_tax_rates'][$i];
             $rateDate = array('id' => '', 'rate' => $tax['rate'], 'label' => empty($tax['label']) ? __('Tax', 'jigoshop') : $tax['label'], 'class' => $tax['class'] == '*' ? 'standard' : $tax['class'], 'country' => $tax['country'], 'states' => isset($tax['is_all_states']) && $tax['is_all_states'] ? '' : $tax['state'], 'is_compound' => $tax['compound'] == 'yes' ? 1 : 0, 'postcodes' => '');
             $i++;
             $tax = isset($options['jigoshop_tax_rates'][$i]) ? $options['jigoshop_tax_rates'][$i] : '';
             while ($i < $endI && $tax['rate'] == $rateDate['rate'] && $tax['country'] == $rateDate['country']) {
                 if (isset($tax['is_all_states']) && $tax['is_all_states']) {
                     $rateDate['states'] = '';
                 } else {
                     $rateDate['states'] .= empty($tax['state']) ? '' : ',' . $options['jigoshop_tax_rates'][$i]['state'];
                 }
                 $i++;
                 $tax = isset($options['jigoshop_tax_rates'][$i]) ? $options['jigoshop_tax_rates'][$i] : '';
             }
             $this->taxService->save($rateDate);
             $this->checkSql();
         }
         $this->options->saveOptions();
         $this->checkSql();
         //			commit sql transation and restore value of autocommit
         $wpdb->query("COMMIT");
         $wpdb->query("SET AUTOCOMMIT=" . $var_autocommit_sql);
         return true;
     } catch (Exception $e) {
         //          rollback sql transation and restore value of autocommit
         if (WP_DEBUG) {
             \Monolog\Registry::getInstance(JIGOSHOP_LOGGER)->addDebug($e);
         }
         $wpdb->query("ROLLBACK");
         $wpdb->query("SET AUTOCOMMIT=" . $var_autocommit_sql);
         Migration::saveLog(__('Migration options end with error: ', 'jigoshop') . $e);
         return false;
     }
 }