public function execute()
 {
     $order_id = waRequest::get('id', null, waRequest::TYPE_INT);
     $form = null;
     $order = array();
     $shipping_address = array();
     // Existing order?
     if ($order_id) {
         $order = $this->getOrder($order_id);
         $currency = $order['currency'];
         if ($order['contact_id']) {
             $has_contacts_rights = shopHelper::getContactRights($order['contact_id']);
             $shipping_address = shopHelper::getOrderAddress($order['params'], 'shipping');
             if (!empty($order['contact_id'])) {
                 try {
                     $c = new waContact($order['contact_id']);
                     if ($shipping_address) {
                         $c['address.shipping'] = $shipping_address;
                     }
                     $form = shopHelper::getCustomerForm($c);
                 } catch (waException $e) {
                     // Contact does not exist; ignore. When $form is null, customer data saved in order is shown.
                 }
             }
         } else {
             $has_contacts_rights = shopHelper::getContactRights();
         }
     } else {
         $currency = $this->getConfig()->getCurrency();
         $has_contacts_rights = shopHelper::getContactRights();
         $form = shopHelper::getCustomerForm();
     }
     $stock_model = new shopStockModel();
     $stocks = $stock_model->getAll('id');
     $tax_model = new shopTaxModel();
     $taxes_count = $tax_model->countAll();
     $count_new = $this->order_model->getStateCounters('new');
     /**
      * Backend order edit page
      * @event backend_order_edit
      * @param array $order
      * @return array[string][string] $return[%plugin_id%] html output
      */
     $this->view->assign('backend_order_edit', wa()->event('backend_order_edit', $order));
     $this->view->assign(array('form' => $form, 'order' => $order, 'stocks' => $stocks, 'currency' => $currency, 'count_new' => $count_new, 'taxes_count' => $taxes_count, 'shipping_address' => $shipping_address, 'has_contacts_rights' => $has_contacts_rights, 'customer_validation_disabled' => wa()->getSetting('disable_backend_customer_form_validation'), 'ignore_stock_count' => wa()->getSetting('ignore_stock_count')));
 }
 public function execute()
 {
     $this->tm = $tm = new shopTaxModel();
     $taxes = $tm->getAll('id');
     $tax_id = waRequest::request('id');
     if (!$tax_id) {
         $tax_id = $taxes ? key($taxes) : 'new';
     }
     if (!empty($taxes[$tax_id])) {
         $tax = $taxes[$tax_id];
     } else {
         if ($tax_id == 'new') {
             $tax = $tm->getEmptyRow();
             $tax_id = null;
         } else {
             throw new waException('Tax record not found.', 404);
         }
     }
     $this->trm = $trm = new shopTaxRegionsModel();
     $this->tzcm = $tzcm = new shopTaxZipCodesModel();
     $countries = $this->getCountryList();
     $tax = $this->processPostData($tax);
     if ($tax['id'] && !$tax_id) {
         $tax_id = $tax['id'];
     }
     if ($tax_id) {
         $taxes[$tax_id] = $tax;
     }
     uasort($taxes, wa_lambda('$a,$b', 'return strcmp($a["name"], $b["name"]);'));
     $this->view->assign('tax_countries', $this->getTaxCountries($tax, $countries));
     $this->view->assign('tax_zip_codes', $this->getTaxZipCodes($tax));
     $this->view->assign('countries', $countries);
     $this->view->assign('taxes', $taxes);
     $this->view->assign('tax', $tax);
     $checkout_settings = $this->getConfig()->getCheckoutSettings();
     $this->view->assign('billing_address_required', isset($checkout_settings['contactinfo']['fields']['address.billing']));
 }
Exemple #3
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;
 }
 private function findTax(&$data)
 {
     static $taxes;
     static $tax_model;
     if (ifset($data['tax_name'])) {
         if (empty($data['tax_name'])) {
             unset($data['tax_id']);
         } else {
             $tax = mb_strtolower(self::flatData($data['tax_name']));
             if (!isset($taxes[$tax])) {
                 if (!$tax_model) {
                     $tax_model = new shopTaxModel();
                 }
                 if ($tax_row = $tax_model->getByName($tax)) {
                     $taxes[$tax] = $tax_row['id'];
                 } else {
                     $taxes[$tax] = null;
                 }
             }
             $data['tax_id'] = $taxes[$tax];
         }
         unset($data['tax_name']);
     }
 }
 public function getTaxes()
 {
     $tax_model = new shopTaxModel();
     return $tax_model->getAll('id');
 }
 public function execute()
 {
     $product = new shopProduct(waRequest::get('id', 0, waRequest::TYPE_INT));
     if (!$product->id) {
         if (waRequest::get('id') == 'new') {
             $product->name = '';
             $product->id = 'new';
             $product->status = 1;
         } else {
             throw new waException("Product not found", 404);
         }
     }
     $counters = array('reviews' => 0, 'images' => 0, 'pages' => 0, 'services' => 0);
     $sidebar_counters = array();
     $config = $this->getConfig();
     /**
      * @var shopConfig $config
      */
     #load product types
     $type_model = new shopTypeModel();
     $product_types = $type_model->getTypes(true);
     $product_types_count = count($product_types);
     if (intval($product->id)) {
         # 1 fill extra product data
         # 1.1 fill product reviews
         $product_reviews_model = new shopProductReviewsModel();
         $product['reviews'] = $product_reviews_model->getReviews($product->id, 0, $config->getOption('reviews_per_page_product'), 'datetime DESC', array('is_new' => true));
         $counters['reviews'] = $product_reviews_model->count($product->id);
         $sidebar_counters['reviews'] = array('new' => $product_reviews_model->countNew());
         $counters['images'] = count($product['images']);
         $product_pages_model = new shopProductPagesModel();
         $counters['pages'] = $product_pages_model->count($product->id);
         $product_services_model = new shopProductServicesModel();
         $counters['services'] = $product_services_model->countServices($product->id);
         $product_stocks_log_model = new shopProductStocksLogModel();
         $counters['stocks_log'] = $product_stocks_log_model->countByField('product_id', $product->id);
         $this->view->assign('edit_rights', $product->checkRights());
     } else {
         $counters += array_fill_keys(array('images', 'services', 'pages', 'reviews'), 0);
         $product['images'] = array();
         reset($product_types);
         $product->type_id = 0;
         if ($product_types_count) {
             if (!$product_types) {
                 throw new waRightsException(_w("Access denied"));
             } else {
                 reset($product_types);
                 $product->type_id = key($product_types);
             }
         } elseif (!$product->checkRights()) {
             throw new waRightsException(_w("Access denied"));
         }
         $this->view->assign('edit_rights', true);
         $product['skus'] = array('-1' => array('id' => -1, 'sku' => '', 'available' => 1, 'name' => '', 'price' => 0.0, 'purchase_price' => 0.0, 'count' => null, 'stock' => array(), 'virtual' => 0));
         $product->currency = $config->getCurrency();
     }
     $this->assignReportsData($product);
     $stock_model = new shopStockModel();
     $taxes_mode = new shopTaxModel();
     $this->view->assign('stocks', $stock_model->getAll('id'));
     $this->view->assign(array('use_product_currency' => wa()->getSetting('use_product_currency'), 'currencies' => $this->getCurrencies(), 'primary_currency' => $config->getCurrency(), 'taxes' => $taxes_mode->getAll()));
     $category_model = new shopCategoryModel();
     $categories = $category_model->getFullTree('id, name, depth, url, full_url, parent_id', true);
     $frontend_urls = array();
     if (intval($product->id)) {
         $routing = wa()->getRouting();
         $domain_routes = $routing->getByApp($this->getAppId());
         foreach ($domain_routes as $domain => $routes) {
             foreach ($routes as $r) {
                 if (!empty($r['private'])) {
                     continue;
                 }
                 if (empty($r['type_id']) || in_array($product->type_id, (array) $r['type_id'])) {
                     $routing->setRoute($r, $domain);
                     $params = array('product_url' => $product->url);
                     if ($product->category_id && isset($categories[$product->category_id])) {
                         if (!empty($r['url_type']) && $r['url_type'] == 1) {
                             $params['category_url'] = $categories[$product->category_id]['url'];
                         } else {
                             $params['category_url'] = $categories[$product->category_id]['full_url'];
                         }
                     }
                     $frontend_url = $routing->getUrl('/frontend/product', $params, true);
                     $frontend_urls[] = array('url' => $frontend_url);
                 }
             }
         }
     } else {
         $frontend_urls[] = array('url' => wa()->getRouteUrl('/frontend/product', array('product_url' => '%product_url%'), true));
     }
     $stuff = intval($product->id) ? $product->url : '%product_url%';
     foreach ($frontend_urls as &$frontend_url) {
         $pos = strrpos($frontend_url['url'], $stuff);
         $frontend_url['base'] = $pos !== false ? rtrim(substr($frontend_url['url'], 0, $pos), '/') . '/' : $frontend_url['url'];
     }
     unset($frontend_url);
     $product_model = new shopProductModel();
     $this->view->assign('storefront_map', $product_model->getStorefrontMap($product->id));
     /**
      * Backend product profile page
      * UI hook allow extends product profile page
      * @event backend_product
      * @param shopProduct $entry
      * @return array[string][string]string $return[%plugin_id%]['title_suffix'] html output
      * @return array[string][string]string $return[%plugin_id%]['action_button'] html output
      * @return array[string][string]string $return[%plugin_id%]['toolbar_section'] html output
      * @return array[string][string]string $return[%plugin_id%]['image_li'] html output
      */
     $this->view->assign('backend_product', wa()->event('backend_product', $product));
     /**
      * @event backend_product_edit
      */
     $this->view->assign('backend_product_edit', wa()->event('backend_product_edit', $product));
     $this->view->assign('categories', $categories);
     $this->view->assign('counters', $counters);
     $this->view->assign('product', $product);
     $this->view->assign('current_author', shopProductReviewsModel::getAuthorInfo(wa()->getUser()->getId()));
     $this->view->assign('reply_allowed', true);
     $this->view->assign('review_allowed', true);
     $this->view->assign('sidebar_counters', $sidebar_counters);
     $this->view->assign('lang', substr(wa()->getLocale(), 0, 2));
     $this->view->assign('frontend_urls', $frontend_urls);
     $tag_model = new shopTagModel();
     $this->view->assign('popular_tags', $tag_model->popularTags());
     $counts = array();
     // Selectable features
     $features_selectable = $product->features_selectable;
     if (is_array($features_selectable)) {
         foreach ($features_selectable as $f) {
             if ($f['selected']) {
                 $counts[] = $f['selected'];
             }
         }
     }
     $feature_model = new shopTypeFeaturesModel();
     $features_selectable_types = $feature_model->getSkuTypeSelectableTypes();
     foreach ($product_types as $type_id => &$type) {
         $type['sku_type'] = empty($features_selectable_types[$type_id]) ? shopProductModel::SKU_TYPE_FLAT : shopProductModel::SKU_TYPE_SELECTABLE;
     }
     $this->view->assign('features', $features_selectable);
     $this->view->assign('duble', '???');
     $this->view->assign('features_counts', $counts);
     #load product types
     $this->view->assign('product_types', $product_types);
     $this->view->assign('sidebar_width', $config->getSidebarWidth());
 }