public function editTaxRate($tax_rate_id, $data) { $fields = array('location_id', 'zone_id', 'priority', 'rate_prefix', 'threshold_condition'); $update = array('date_modified = NOW()'); foreach ($fields as $f) { if (isset($data[$f])) { $update[] = "{$f} = '" . $this->db->escape($data[$f]) . "'"; } } $update[] = "rate = '" . preformatFloat($data['rate'], $this->language->get('decimal_point')) . "'"; $update[] = "threshold = '" . preformatFloat($data['threshold'], $this->language->get('decimal_point')) . "'"; if (!empty($update)) { $this->db->query("UPDATE `" . DB_PREFIX . "tax_rates`\n\t\t\t\t\t\t\t\tSET " . implode(',', $update) . "\n\t\t\t\t\t\t\t\tWHERE tax_rate_id = '" . (int) $tax_rate_id . "'"); $this->cache->delete('tax_class'); $this->cache->delete('location'); } if (count($data['tax_rate'])) { foreach ($data['tax_rate'] as $language_id => $value) { $this->language->replaceDescriptions('tax_rate_descriptions', array('tax_rate_id' => (int) $tax_rate_id), array($language_id => array('description' => $value['description']))); } $this->cache->delete('tax_class'); $this->cache->delete('location'); } }
/** * @param int $product_id * @param array $data */ private function _clone_product_options($product_id, $data) { //Do not use before close review. //Note: This is done only after product cloning. This is not to be used on existing product. $this->db->query("DELETE FROM " . $this->db->table("product_options") . " WHERE product_id = '" . (int) $product_id . "'"); $this->db->query("DELETE FROM " . $this->db->table("product_option_descriptions") . " WHERE product_id = '" . (int) $product_id . "'"); $this->db->query("DELETE FROM " . $this->db->table("product_option_values") . " WHERE product_id = '" . (int) $product_id . "'"); $this->db->query("DELETE FROM " . $this->db->table("product_option_value_descriptions") . " WHERE product_id = '" . (int) $product_id . "'"); if (isset($data['product_option'])) { foreach ($data['product_option'] as $product_option) { $sql = "INSERT INTO " . $this->db->table("product_options") . " \n\t\t\t\t\t\tSET product_id = '" . (int) $product_id . "',\n\t\t\t\t\t\t\tsort_order = '" . (int) $product_option['sort_order'] . "'"; if ($product_option['attribute_id']) { $sql .= ", attribute_id = '" . (int) $product_option['attribute_id'] . "'"; } if ($product_option['group_id']) { $sql .= ", group_id = '" . (int) $product_option['group_id'] . "'"; } if ($product_option['element_type']) { $sql .= ", element_type = '" . $this->db->escape($product_option['element_type']) . "'"; } if ($product_option['required']) { $sql .= ", required = '" . (int) $product_option['required'] . "'"; } if ($product_option['regexp_pattern']) { $sql .= ", regexp_pattern = '" . $this->db->escape($product_option['regexp_pattern']) . "'"; } $this->db->query($sql); $product_option_id = $this->db->getLastId(); foreach ($product_option['language'] as $language_id => $language) { $this->language->replaceDescriptions('product_option_descriptions', array('product_option_id' => (int) $product_option_id, 'product_id' => (int) $product_id), array($language_id => array('name' => $language['name'], 'error_text' => $language['error_text']))); } if (isset($product_option['product_option_value'])) { //get product resources $rm = new AResourceManager(); foreach ($product_option['product_option_value'] as $pd_opt_vals) { $pd_opt_vals['price'] = str_replace(" ", "", $pd_opt_vals['price']); $this->db->query("INSERT INTO " . $this->db->table("product_option_values") . " \n\t\t\t\t\t\t\t\t\t\t\tSET product_option_id = '" . (int) $product_option_id . "',\n\t\t\t\t\t\t\t\t\t\t\t\tproduct_id = '" . (int) $product_id . "',\n\t\t\t\t\t\t\t\t\t\t\t\tsku = '" . $this->db->escape($pd_opt_vals['sku']) . "',\n\t\t\t\t\t\t\t\t\t\t\t\tquantity = '" . (int) $pd_opt_vals['quantity'] . "',\n\t\t\t\t\t\t\t\t\t\t\t\tsubtract = '" . (int) $pd_opt_vals['subtract'] . "',\n\t\t\t\t\t\t\t\t\t\t\t\tprice = '" . preformatFloat($pd_opt_vals['price'], $this->language->get('decimal_point')) . "',\n\t\t\t\t\t\t\t\t\t\t\t\tprefix = '" . $this->db->escape($pd_opt_vals['prefix']) . "',\n\t\t\t\t\t\t\t\t\t\t\t\tattribute_value_id = '" . $this->db->escape($pd_opt_vals['attribute_value_id']) . "',\n\t\t\t\t\t\t\t\t\t\t\t\tgrouped_attribute_data = '" . $this->db->escape($pd_opt_vals['grouped_attribute_data']) . "',\n\t\t\t\t\t\t\t\t\t\t\t\tgroup_id = '" . $this->db->escape($pd_opt_vals['group_id']) . "',\n\t\t\t\t\t\t\t\t\t\t\t\tsort_order = '" . (int) $pd_opt_vals['sort_order'] . "',\n\t\t\t\t\t\t\t\t\t\t\t\t`default` = '" . (int) $pd_opt_vals['default'] . "'"); $pd_opt_val_id = $this->db->getLastId(); // clone resources of option value if ($pd_opt_vals['product_option_value_id']) { $resources = $rm->getResourcesList(array('object_name' => 'product_option_value', 'object_id' => $pd_opt_vals['product_option_value_id'])); foreach ($resources as $r) { $rm->mapResource('product_option_value', $pd_opt_val_id, $r['resource_id']); } } foreach ($pd_opt_vals['language'] as $language_id => $lang_data) { $grouped_attribute_names = serialize($lang_data['children_options_names']); $this->language->replaceDescriptions('product_option_value_descriptions', array('product_option_value_id' => (int) $pd_opt_val_id, 'product_id' => (int) $product_id), array($language_id => array('name' => $lang_data['name'], 'grouped_attribute_names' => $grouped_attribute_names))); } } } } } $this->cache->remove('product'); }
/** * @param int $order_id * @param array $data */ public function editOrderProduct($order_id, $data) { $order_id = (int) $order_id; $order_product_id = (int) $data['order_product_id']; $product_id = (int) $data['product_id']; if (!$product_id || !$order_id) { return false; } $this->load->model('catalog/product'); $product_info = $this->model_catalog_product->getProduct($product_id); $order_info = $this->getOrder($order_id); $elements_with_options = HtmlElementFactory::getElementsWithOptions(); if (isset($data['product'])) { foreach ($data['product'] as $product) { if ($product['quantity'] <= 0) { // stupid situation return false; } //check is product exists $exists = $this->db->query("SELECT op.product_id, op.quantity\n\t\t\t\t\t\t\t\t\t\t\t\t FROM " . $this->db->table("order_products") . " op\n\t\t\t\t\t\t\t\t\t\t\t\t WHERE op.order_id = '" . (int) $order_id . "'\n\t\t\t\t\t\t\t\t\t\t\t\t\t AND op.product_id='" . (int) $product_id . "'\n\t\t\t\t\t\t\t\t\t\t\t\t\t AND op.order_product_id = '" . (int) $order_product_id . "'"); if ($exists->num_rows) { //update order quantity $this->db->query("UPDATE " . $this->db->table("order_products") . "\n\t\t\t\t\t\t\t\t\t\t SET price = '" . $this->db->escape(preformatFloat($product['price'], $this->language->get('decimal_point')) / $order_info['value']) . "',\n\t\t\t\t\t\t\t\t\t\t \t total = '" . $this->db->escape(preformatFloat($product['total'], $this->language->get('decimal_point')) / $order_info['value']) . "',\n\t\t\t\t\t\t\t\t\t\t\t quantity = '" . $this->db->escape($product['quantity']) . "'\n\t\t\t\t\t\t\t\t\t\t WHERE order_id = '" . (int) $order_id . "' AND order_product_id = '" . (int) $order_product_id . "'"); //update stock quantity $old_qnt = $exists->row['quantity']; $stock_qnt = $product_info['quantity']; $qnt_diff = $old_qnt - $product['quantity']; if ($qnt_diff != 0) { if ($qnt_diff < 0) { $new_qnt = $stock_qnt - abs($qnt_diff); } else { $new_qnt = $stock_qnt + $qnt_diff; } if ($product_info['subtract']) { $this->db->query("UPDATE " . $this->db->table("products") . "\n\t\t\t\t\t\t\t\t\t\t\t SET quantity = '" . $new_qnt . "'\n\t\t\t\t\t\t\t\t\t\t\t WHERE product_id = '" . (int) $product_id . "' AND subtract = 1"); } } } else { // add new product into order $product_query = $this->db->query("SELECT *, p.product_id\n\t\t\t\t\t\t\t FROM " . $this->db->table("products") . " p\n\t\t\t\t\t\t\t LEFT JOIN " . $this->db->table("product_descriptions") . " pd\n\t\t\t\t\t\t\t ON (p.product_id = pd.product_id AND pd.language_id=" . $this->language->getContentLanguageID() . ")\n\t\t\t\t\t\t\t WHERE p.product_id='" . (int) $product_id . "'"); $this->db->query("INSERT INTO " . $this->db->table("order_products") . "\n\t\t\t\t\t\t\tSET order_id = '" . (int) $order_id . "',\n\t\t\t\t\t\t\t\tproduct_id = '" . (int) $product_id . "',\n\t\t\t\t\t\t\t\tname = '" . $this->db->escape($product_query->row['name']) . "',\n\t\t\t\t\t\t\t\tmodel = '" . $this->db->escape($product_query->row['model']) . "',\n\t\t\t\t\t\t\t\tprice = '" . $this->db->escape(preformatFloat($product['price'], $this->language->get('decimal_point')) / $order_info['value']) . "',\n\t\t\t\t\t\t\t\ttotal = '" . $this->db->escape(preformatFloat($product['total'], $this->language->get('decimal_point')) / $order_info['value']) . "',\n\t\t\t\t\t\t\t\tquantity = '" . (int) $product['quantity'] . "'"); $order_product_id = $this->db->getLastId(); //update stock quantity $qnt_diff = -$product['quantity']; $stock_qnt = $product_query->row['quantity']; $new_qnt = $stock_qnt - (int) $product['quantity']; if ($product_info['subtract']) { $this->db->query("UPDATE " . $this->db->table("products") . "\n\t\t\t\t\t\t\t\t\t\t SET quantity = '" . $new_qnt . "'\n\t\t\t\t\t\t\t\t\t\t WHERE product_id = '" . (int) $product_id . "' AND subtract = 1"); } } if ($product['option']) { //first of all find previous order options // if empty result - order products just added $order_product_options = $this->getOrderOptions($order_id, $order_product_id); $prev_subtract_options = array(); //array with previous option values with enabled stock tracking foreach ($order_product_options as $old_value) { if (!$old_value['subtract']) { continue; } $prev_subtract_options[(int) $old_value['product_option_id']][] = (int) $old_value['product_option_value_id']; } $po_ids = array(); foreach ($product['option'] as $k => $option) { $po_ids[] = (int) $k; } //get all data of given product options from db $sql = "SELECT *, pov.product_option_value_id, povd.name as option_value_name, pod.name as option_name\n\t\t\t\t\t\t\t\tFROM " . $this->db->table('product_options') . " po\n\t\t\t\t\t\t\t\tLEFT JOIN " . $this->db->table('product_option_descriptions') . " pod\n\t\t\t\t\t\t\t\t\tON (pod.product_option_id = po.product_option_id AND pod.language_id=" . $this->language->getContentLanguageID() . ")\n\t\t\t\t\t\t\t\tLEFT JOIN " . $this->db->table('product_option_values') . " pov\n\t\t\t\t\t\t\t\t\tON po.product_option_id = pov.product_option_id\n\t\t\t\t\t\t\t\tLEFT JOIN " . $this->db->table('product_option_value_descriptions') . " povd\n\t\t\t\t\t\t\t\t ON (povd.product_option_value_id = pov.product_option_value_id AND povd.language_id=" . $this->language->getContentLanguageID() . ")\n\t\t\t\t\t\t\t\tWHERE po.product_option_id IN (" . implode(',', $po_ids) . ")\n\t\t\t\t\t\t\t\tORDER BY po.product_option_id"; $result = $this->db->query($sql); $exclude_list = array(); //list of option value that we do not resave $option_value_info = array(); foreach ($result->rows as $row) { //skip files if (in_array($row['element_type'], array('U'))) { $exclude_list[] = (int) $row['product_option_value_id']; } $option_value_info[$row['product_option_id'] . '_' . $row['product_option_value_id']] = $row; //compond key for cases when val_id is null $option_types[$row['product_option_id']] = $row['element_type']; } //delete old options and then insert new $sql = "DELETE FROM " . $this->db->table('order_options') . "\n\t\t\t\t\t\t\t\tWHERE order_id = " . $order_id . " AND order_product_id=" . (int) $order_product_id; if ($exclude_list) { $sql .= " AND product_option_value_id NOT IN (" . implode(', ', $exclude_list) . ")"; } $this->db->query($sql); foreach ($product['option'] as $opt_id => $values) { if (!is_array($values)) { // for non-multioptional elements //do not save empty inputs and texareas if (in_array($option_types[$opt_id], array('I', 'T')) && $values == '') { continue; } elseif ($option_types[$opt_id] == 'S') { $values = array($values); } else { foreach ($option_value_info as $o) { if ($o['product_option_id'] == $opt_id) { if (!in_array($option_types[$opt_id], $elements_with_options)) { $option_value_info[$o['product_option_id'] . '_' . $o['product_option_value_id']]['option_value_name'] = $values; } $values = array($o['product_option_value_id']); break; } } } } foreach ($values as $value) { $arr_key = $opt_id . '_' . $value; $sql = "INSERT INTO " . $this->db->table('order_options') . "\n\t\t\t\t\t\t\t\t\t\t\t(`order_id`,\n\t\t\t\t\t\t\t\t\t\t\t`order_product_id`,\n\t\t\t\t\t\t\t\t\t\t\t`product_option_value_id`,\n\t\t\t\t\t\t\t\t\t\t\t`name`,\n\t\t\t\t\t\t\t\t\t\t\t`value`,\n\t\t\t\t\t\t\t\t\t\t\t`price`,\n\t\t\t\t\t\t\t\t\t\t\t`prefix`)\n\t\t\t\t\t\t\t\t\t\tVALUES\t('" . $order_id . "',\n\t\t\t\t\t\t\t\t\t\t\t\t'" . (int) $order_product_id . "',\n\t\t\t\t\t\t\t\t\t\t\t\t'" . (int) $value . "',\n\t\t\t\t\t\t\t\t\t\t\t\t'" . $this->db->escape($option_value_info[$arr_key]['option_name']) . "',\n\t\t\t\t\t\t\t\t\t\t\t\t'" . $this->db->escape($option_value_info[$arr_key]['option_value_name']) . "',\n\t\t\t\t\t\t\t\t\t\t\t\t'" . $this->db->escape($option_value_info[$arr_key]['price']) . "',\n\t\t\t\t\t\t\t\t\t\t\t\t'" . $this->db->escape($option_value_info[$arr_key]['prefix']) . "')"; $this->db->query($sql); if ($option_value_info[$arr_key]['subtract']) { $curr_subtract_options[(int) $opt_id][] = (int) $value; } } //reduce product quantity for option value that not assigned to product anymore $prev_arr = has_value($prev_subtract_options[$opt_id]) ? $prev_subtract_options[$opt_id] : array(); $curr_arr = has_value($curr_subtract_options[$opt_id]) ? $curr_subtract_options[$opt_id] : array(); if ($prev_arr || $curr_arr) { //increase qnt for old option values foreach ($prev_arr as $v) { if (!in_array($v, $curr_arr)) { $sql = "UPDATE " . $this->db->table("product_option_values") . "\n\t\t\t\t\t\t\t\t\t\t SET quantity = (quantity + " . $product['quantity'] . ")\n\t\t\t\t\t\t\t\t\t\t WHERE product_option_value_id = '" . (int) $v . "'\n\t\t\t\t\t\t\t\t\t\t AND subtract = 1"; $this->db->query($sql); } } //decrease qnt for new option values foreach ($curr_arr as $v) { if (!in_array($v, $prev_arr)) { $sql = "UPDATE " . $this->db->table("product_option_values") . "\n\t\t\t\t\t\t\t\t\t\t SET quantity = (quantity - " . $product['quantity'] . ")\n\t\t\t\t\t\t\t\t\t\t WHERE product_option_value_id = '" . (int) $v . "'\n\t\t\t\t\t\t\t\t\t\t AND subtract = 1"; $this->db->query($sql); } } //if qnt changed for the same option values $intersect = array_intersect($curr_arr, $prev_arr); if ($intersect && $qnt_diff != 0) { if ($qnt_diff < 0) { $sql_incl = "(quantity - " . abs($qnt_diff) . ")"; } else { $sql_incl = "(quantity + " . abs($qnt_diff) . ")"; } foreach ($intersect as $v) { $sql = "UPDATE " . $this->db->table("product_option_values") . "\n\t\t\t\t\t\t\t\t\t\t SET quantity = " . $sql_incl . "\n\t\t\t\t\t\t\t\t\t\t WHERE product_option_value_id = '" . (int) $v . "'\n\t\t\t\t\t\t\t\t\t\t AND subtract = 1"; $this->db->query($sql); } } } } } //end processing options } } //fix order total and subtotal $sql = "SELECT SUM(total) as subtotal\n\t\t\t\tFROM " . $this->db->table('order_products') . "\n\t\t\t\tWHERE order_id=" . $order_id; $result = $this->db->query($sql); $subtotal = $result->row['subtotal']; $text = $this->currency->format($subtotal, $order_info['currency'], $order_info['value']); $sql = "UPDATE " . $this->db->table('order_totals') . "\n\t\t SET `value`='" . $subtotal . "', `text` = '" . $text . "'\n\t\t\t\tWHERE order_id=" . $order_id . " AND type='subtotal'"; $this->db->query($sql); $sql = "SELECT SUM(`value`) as total\n\t\t\t\tFROM " . $this->db->table('order_totals') . "\n\t\t\t\tWHERE order_id=" . $order_id . " AND type<>'total'"; $result = $this->db->query($sql); $total = $result->row['total']; $text = $this->currency->format($total, $order_info['currency'], $order_info['value']); $sql = "UPDATE " . $this->db->table('order_totals') . "\n\t\t SET `value`='" . $subtotal . "', `text` = '" . $text . "'\n\t\t\t\tWHERE order_id=" . $order_id . " AND type='total'"; $this->db->query($sql); $this->cache->delete('product'); }
public function update() { //init controller data $this->extensions->hk_InitData($this, __FUNCTION__); $this->document->setTitle($this->language->get('heading_title')); $this->view->assign('success', $this->session->data['success']); if (isset($this->session->data['success'])) { unset($this->session->data['success']); } $this->load->library('json'); if ($this->request->is_POST() && $this->_validateForm()) { if (has_value($this->request->post['date_start'])) { $this->request->post['date_start'] = dateDisplay2ISO($this->request->post['date_start'], $this->language->get('date_format_short')); } if (has_value($this->request->post['date_end'])) { $this->request->post['date_end'] = dateDisplay2ISO($this->request->post['date_end'], $this->language->get('date_format_short')); if (strtotime($this->request->post['date_end']) < time()) { $this->request->post['status'] = 0; } } $this->request->post['discount'] = preformatFloat($this->request->post['discount'], $this->language->get('decimal_point')); $this->request->post['total'] = preformatFloat($this->request->post['total'], $this->language->get('decimal_point')); $this->model_sale_coupon->editCoupon($this->request->get['coupon_id'], $this->request->post); $this->model_sale_coupon->editCouponProducts($this->request->get['coupon_id'], $this->request->post); $this->session->data['success'] = $this->language->get('text_success'); $this->redirect($this->html->getSecureURL('sale/coupon/update', '&coupon_id=' . $this->request->get['coupon_id'])); } $this->_getForm(); $this->view->assign('form_language_switch', $this->html->getContentLanguageSwitcher()); //update controller data $this->extensions->hk_UpdateData($this, __FUNCTION__); }
private function _validateForm() { if (!$this->user->canModify('catalog/product')) { $this->error['warning'] = $this->language->get_error('error_permission'); } $len = mb_strlen($this->request->post['product_description']['name']); if ($len < 1 || $len > 255) { $this->error['name'] = $this->language->get_error('error_name'); } if (mb_strlen($this->request->post['model']) > 64) { $this->error['model'] = $this->language->get_error('error_model'); } if ($error_text = $this->html->isSEOkeywordExists('product_id=' . $this->request->get['product_id'], $this->request->post['keyword'])) { $this->error['keyword'] = $error_text; } foreach (array('length', 'width', 'height', 'weight') as $name) { $this->request->post[$name] = abs($this->request->post[$name]); $v = preformatFloat($this->request->post[$name], $this->language->get('decimal_point')); if ($v >= 1000) { $this->error[$name] = $this->language->get('error_measure_value'); } } $this->extensions->hk_ValidateData($this, __FUNCTION__); if (!$this->error) { return TRUE; } else { if (!isset($this->error['warning'])) { $this->error['warning'] = $this->language->get_error('error_required_data'); } return FALSE; } }
private function _validateField($field, $value) { $err = ''; switch ($field) { case 'product_description': if (isset($value['name']) && (mb_strlen($value['name']) < 1 || mb_strlen($value['name']) > 255)) { $err = $this->language->get('error_name'); } break; case 'model': if (mb_strlen($value) > 64) { $err = $this->language->get('error_model'); } break; case 'keyword': $err = $this->html->isSEOkeywordExists('product_id=' . $this->request->get['id'], $value); break; case 'length': case 'width': case 'height': case 'weight': $v = preformatFloat(abs($value), $this->language->get('decimal_point')); if ($v >= 1000) { $err = $this->language->get('error_measure_value'); } break; } return $err; }
/** * update only one field * * @return void */ public function update_field() { //init controller data $this->extensions->hk_InitData($this, __FUNCTION__); $this->loadLanguage('sale/coupon'); $this->loadModel('sale/coupon'); if (!$this->user->canModify('listing_grid/coupon')) { $error = new AError(''); return $error->toJSONResponse('NO_PERMISSIONS_402', array('error_text' => sprintf($this->language->get('error_permission_modify'), 'listing_grid/coupon'), 'reset_value' => true)); } if (isset($this->request->get['id'])) { foreach ($this->request->post as $field => $value) { if ($field == 'uses_total' && $value == '' || $field == 'uses_customer' && $value == '') { $value = -1; } $err = $this->_validateForm($field, $value); if (in_array($field, array('date_start', 'date_end'))) { $value = dateDisplay2ISO($value); } if (in_array($field, array('discount', 'total'))) { $value = preformatFloat($value, $this->language->get('decimal_point')); } if (!$err) { $this->model_sale_coupon->editCoupon($this->request->get['id'], array($field => $value)); } else { $error = new AError(''); return $error->toJSONResponse('VALIDATION_ERROR_406', array('error_text' => $err)); } //save products to coupon if ($this->request->post['coupon_product']) { $this->model_sale_coupon->editCouponProducts($this->request->get['id'], $this->request->post); } } return null; } //request sent from jGrid. ID is key of array foreach ($this->request->post as $field => $value) { foreach ($value as $k => $v) { $err = $this->_validateForm($field, $v); if (!$err) { $this->model_sale_coupon->editCoupon($k, array($field => $v)); } else { $error = new AError(''); return $error->toJSONResponse('VALIDATION_ERROR_406', array('error_text' => $err)); } } } //update controller data $this->extensions->hk_UpdateData($this, __FUNCTION__); }