public function execute() { $service_model = new shopServiceModel(); $service_product_model = new shopProductServicesModel(); $id = waRequest::get('id', null, waRequest::TYPE_INT); $edit = waRequest::get('edit', null, waRequest::TYPE_STRING_TRIM); if ($edit == 'name') { $service_model->updateById($id, array('name' => waRequest::post('name', '', waRequest::TYPE_STRING_TRIM))); return; } if ($id) { $service = $service_model->getById($id); if (!$service) { $this->errors[] = _w("Unknown service to update"); return; } } if ($id) { // delete products $delete_products = waRequest::post('delete_product', array(), waRequest::TYPE_ARRAY_INT); $service_product_model->deleteByProducts($delete_products, $id); } $id = $service_model->save($this->getData(), $id, true); $this->response = array('id' => $id); }
public function move($service_id, $id, $before_id = null) { $service_id = (int) $service_id; $service_model = new shopServiceModel(); $service = $service_model->getById($service_id); if (!$service) { return false; } $id = (int) $id; if (!$before_id) { $item = $this->getById($id); if (!$item) { return false; } $sort = $this->query("SELECT MAX(sort) sort FROM {$this->table} WHERE service_id = {$service_id}")->fetchField('sort') + 1; $this->updateById($id, array('sort' => $sort)); } else { $before_id = $this->escape($before_id); $items = $this->query("SELECT * FROM {$this->table} WHERE id IN ('{$id}', '{$before_id}')")->fetchAll('id'); if (!$items || count($items) != 2) { return false; } $sort = $items[$before_id]['sort']; $this->query("UPDATE {$this->table} SET sort = sort + 1 WHERE sort >= {$sort} AND service_id = {$service_id}"); $this->updateById($id, array('sort' => $sort)); } return true; }
public function moveServices() { $id = waRequest::post('id', null, waRequest::TYPE_INT); $before_id = waRequest::post('before_id', null, waRequest::TYPE_INT); $service_model = new shopServiceModel(); $service_model->move($id, $before_id); }
public function execute() { $id = waRequest::get('id', null, waRequest::TYPE_INT); if (!$id) { $this->errors[] = _w("Unknown service to delete"); return; } $service_model = new shopServiceModel(); $service_model->delete($id); }
public function execute() { $id = waRequest::get('id', null, waRequest::TYPE_INT); $service_model = new shopServiceModel(); $service = array(); $services = $service_model->getAll('id'); if ($id !== 0) { if (!empty($services)) { if ($id && isset($services[$id])) { $service = $services[$id]; } else { $service = current($services); } } } // blank area for adding new service if (!$service) { $services[] = $this->getEmptyService(); $type_model = new shopTypeModel(); $this->assign(array('services' => $services, 'types' => $type_model->getAll(), 'count' => $service_model->countAll(), 'taxes' => $this->getTaxes())); return; } $service_variants_model = new shopServiceVariantsModel(); $variants = $service_variants_model->get($service['id']); $type_services_model = new shopTypeServicesModel(); $types = $type_services_model->getTypes($service['id']); $products_count = 0; $selected_types = array(); foreach ($types as $type) { if ($type['type_id']) { $selected_types[] = $type['type_id']; } } if (!empty($selected_types)) { $product_model = new shopProductModel(); $products_count = $product_model->countByField(array('type_id' => $selected_types)); } $product_services_model = new shopProductServicesModel(); $this->assign(array('services' => $services, 'service' => $service, 'products' => $product_services_model->getProducts($service['id']), 'types' => $types, 'products_count' => $products_count, 'variants' => $variants, 'count' => $service_model->countAll(), 'taxes' => $this->getTaxes())); }
public function execute() { if (!$this->getUser()->isAdmin('shop') && !wa()->getUser()->getRights('shop', 'type.%')) { throw new waRightsException('Access denied'); } $this->setLayout(new shopBackendLayout()); $this->getResponse()->setTitle(_w('Products')); $this->view->assign('categories', new shopCategories()); $tag_model = new shopTagModel(); $this->view->assign('cloud', $tag_model->getCloud()); $set_model = new shopSetModel(); $this->view->assign('sets', $set_model->getAll()); $collapse_types = wa()->getUser()->getSettings('shop', 'collapse_types'); if (empty($collapse_types)) { $type_model = new shopTypeModel(); $this->view->assign('types', $type_model->getTypes()); } else { $this->view->assign('types', false); } $product_model = new shopProductModel(); $this->view->assign('count_all', $product_model->countAll()); $review_model = new shopProductReviewsModel(); $this->view->assign('count_reviews', array('all' => $review_model->count(null, false), 'new' => $review_model->countNew(true))); $product_services = new shopServiceModel(); $this->view->assign('count_services', $product_services->countAll()); $config = $this->getConfig(); $this->view->assign('default_view', $config->getOption('products_default_view')); /* * @event backend_products * @return array[string]array $return[%plugin_id%] array of html output * @return array[string][string]string $return[%plugin_id%]['sidebar_top_li'] html output * @return array[string][string]string $return[%plugin_id%]['sidebar_section'] html output */ $this->view->assign('backend_products', wa()->event('backend_products')); $this->view->assign('sidebar_width', $config->getSidebarWidth()); $this->view->assign('lang', substr(wa()->getLocale(), 0, 2)); }
public function getByCode($code, $full_info = false, $hierarchy = true) { if (!$code) { return array(); } $sql = "SELECT * FROM " . $this->table . " WHERE code = s:0 ORDER BY parent_id"; $items = $this->query($sql, $code)->fetchAll('id'); if ($full_info) { $product_ids = $sku_ids = $service_ids = $variant_ids = array(); foreach ($items as $item) { $product_ids[] = $item['product_id']; $sku_ids[] = $item['sku_id']; if ($item['type'] == 'service') { $service_ids[] = $item['service_id']; if ($item['service_variant_id']) { $variant_ids[] = $item['service_variant_id']; } } } $product_model = new shopProductModel(); $products = $product_model->getByField('id', $product_ids, 'id'); $sku_model = new shopProductSkusModel(); $skus = $sku_model->getByField('id', $sku_ids, 'id'); $service_model = new shopServiceModel(); $services = $service_model->getByField('id', $service_ids, 'id'); $service_variants_model = new shopServiceVariantsModel(); $variants = $service_variants_model->getByField('id', $variant_ids, 'id'); $product_services_model = new shopProductServicesModel(); $rows = $product_services_model->getByProducts($product_ids); $product_services = $sku_services = array(); foreach ($rows as $row) { if ($row['sku_id'] && !in_array($row['sku_id'], $sku_ids)) { continue; } $service_ids[] = $row['service_id']; if (!$row['sku_id']) { $product_services[$row['product_id']][$row['service_variant_id']] = $row; } if ($row['sku_id']) { $sku_services[$row['sku_id']][$row['service_variant_id']] = $row; } } foreach ($items as $item_key => &$item) { if ($item['type'] == 'product' && isset($products[$item['product_id']])) { $item['product'] = $products[$item['product_id']]; if (!isset($skus[$item['sku_id']])) { unset($items[$item_key]); continue; } $sku = $skus[$item['sku_id']]; $item['sku_code'] = $sku['sku']; $item['purchase_price'] = $sku['purchase_price']; $item['sku_name'] = $sku['name']; $item['currency'] = $item['product']['currency']; $item['price'] = $sku['price']; $item['name'] = $item['product']['name']; if ($item['sku_name']) { $item['name'] .= ' (' . $item['sku_name'] . ')'; } } elseif ($item['type'] == 'service' && isset($services[$item['service_id']])) { $item['name'] = $item['service_name'] = $services[$item['service_id']]['name']; $item['currency'] = $services[$item['service_id']]['currency']; $item['service'] = $services[$item['service_id']]; $item['variant_name'] = $variants[$item['service_variant_id']]['name']; if ($item['variant_name']) { $item['name'] .= ' (' . $item['variant_name'] . ')'; } $item['price'] = $variants[$item['service_variant_id']]['price']; if (isset($product_services[$item['product_id']][$item['service_variant_id']])) { if ($product_services[$item['product_id']][$item['service_variant_id']]['price'] !== null) { $item['price'] = $product_services[$item['product_id']][$item['service_variant_id']]['price']; } } if (isset($sku_services[$item['sku_id']][$item['service_variant_id']])) { if ($sku_services[$item['sku_id']][$item['service_variant_id']]['price'] !== null) { $item['price'] = $sku_services[$item['sku_id']][$item['service_variant_id']]['price']; } } if ($item['currency'] == '%') { $p = $items[$item['parent_id']]; $item['price'] = $item['price'] * $p['price'] / 100; $item['currency'] = $p['currency']; } } } unset($item); } // sort foreach ($items as $item_id => $item) { if ($item['parent_id']) { $items[$item['parent_id']]['services'][] = $item; unset($items[$item_id]); } } if (!$hierarchy) { $result = array(); foreach ($items as $item_id => $item) { if (isset($item['services'])) { $i = $item; unset($i['services']); $result[$item_id] = $i; foreach ($item['services'] as $s) { $result[$s['id']] = $s; } } else { $result[$item_id] = $item; } } $items = $result; } return $items; }
public function execute($data = null) { $order_model = new shopOrderModel(); $order = $order_model->getById($data['id']); $subtotal = 0; $services = $products = array(); foreach ($data['items'] as $item) { if ($item['service_id']) { $services[] = $item['service_id']; } else { $products[] = $item['product_id']; } } $service_model = new shopServiceModel(); $product_model = new shopProductModel(); $services = $service_model->getById($services); $products = $product_model->getById($products); foreach ($data['items'] as &$item) { $item['currency'] = $order['currency']; $item['price'] = $this->price($item['price']); if ($item['service_id']) { $item['service'] = $services[$item['service_id']]; } else { $item['product'] = $products[$item['product_id']]; } $subtotal += $item['price'] * $item['quantity']; } unset($item); foreach (array('shipping', 'discount') as $k) { if (!isset($data[$k])) { $data[$k] = 0; } } $contact = new waContact($order['contact_id']); $shipping_address = $contact->getFirst('address.shipping'); if (!$shipping_address) { $shipping_address = $contact->getFirst('address'); } $shipping_address = $shipping_address ? $shipping_address['data'] : array(); $billing_address = $contact->getFirst('address.billing'); if (!$billing_address) { $billing_address = $contact->getFirst('address'); } $billing_address = $billing_address ? $billing_address['data'] : array(); $discount_rate = $subtotal ? $data['discount'] / $subtotal : 0; $taxes = shopTaxes::apply($data['items'], array('shipping' => $shipping_address, 'billing' => $billing_address, 'discount_rate' => $discount_rate), $order['currency']); $tax = $tax_included = 0; foreach ($taxes as $t) { if (isset($t['sum'])) { $tax += $t['sum']; } if (isset($t['sum_included'])) { $tax_included += $t['sum_included']; } } $data['tax'] = $tax_included + $tax; $data['total'] = $subtotal + $tax + $this->price($data['shipping']) - $this->price($data['discount']); // for logging changes in stocks shopProductStocksLogModel::setContext(shopProductStocksLogModel::TYPE_ORDER, 'Order %s was edited', array('order_id' => $data['id'])); // update $order_model->update($data, $data['id']); $log_model = new waLogModel(); $log_model->add('order_edit', $data['id']); shopProductStocksLogModel::clearContext(); if (!empty($data['params'])) { $params_model = new shopOrderParamsModel(); $params_model->set($data['id'], $data['params'], false); } return true; }
public function cart() { $this->getResponse()->addHeader("Cache-Control", "no-store, no-cache, must-revalidate"); $this->getResponse()->addHeader("Expires", date("r")); if (waRequest::method() == 'post') { $data = wa()->getStorage()->get('shop/checkout', array()); if ($coupon_code = waRequest::post('coupon_code')) { $data['coupon_code'] = $coupon_code; } elseif (isset($data['coupon_code'])) { unset($data['coupon_code']); } if (($use = waRequest::post('use_affiliate')) !== null) { if ($use) { $data['use_affiliate'] = 1; } elseif (isset($data['use_affiliate'])) { unset($data['use_affiliate']); } } if ($coupon_code || $use) { wa()->getStorage()->set('shop/checkout', $data); wa()->getStorage()->remove('shop/cart'); } } $cart_model = new shopCartItemsModel(); $cart = new shopCart(); $code = $cart->getCode(); $errors = array(); if (waRequest::post('checkout')) { $saved_quantity = $cart_model->select('id,quantity')->where("type='product' AND code = s:code", array('code' => $code))->fetchAll('id'); $quantity = waRequest::post('quantity'); foreach ($quantity as $id => $q) { if ($q != $saved_quantity[$id]) { $cart->setQuantity($id, $q); } } $not_available_items = $cart_model->getNotAvailableProducts($code, !wa()->getSetting('ignore_stock_count')); foreach ($not_available_items as $row) { if ($row['sku_name']) { $row['name'] .= ' (' . $row['sku_name'] . ')'; } if ($row['available']) { $errors[$row['id']] = sprintf(_w('Only %d pcs of %s are available, and you already have all of them in your shopping cart.'), $row['count'], $row['name']); } else { $errors[$row['id']] = _w('Oops! %s is not available for purchase at the moment. Please remove this product from your shopping cart to proceed.'); } } if (!$errors) { $this->redirect(wa()->getRouteUrl('/frontend/checkout')); } } $items = $cart_model->where('code= ?', $code)->order('parent_id')->fetchAll('id'); $product_ids = $sku_ids = $service_ids = $type_ids = array(); foreach ($items as $item) { $product_ids[] = $item['product_id']; $sku_ids[] = $item['sku_id']; } $product_ids = array_unique($product_ids); $sku_ids = array_unique($sku_ids); $product_model = new shopProductModel(); if (waRequest::param('url_type') == 2) { $products = $product_model->getWithCategoryUrl($product_ids); } else { $products = $product_model->getById($product_ids); } $sku_model = new shopProductSkusModel(); $skus = $sku_model->getByField('id', $sku_ids, 'id'); $image_model = new shopProductImagesModel(); $delete_items = array(); foreach ($items as $item_id => &$item) { if (!isset($skus[$item['sku_id']])) { unset($items[$item_id]); $delete_items[] = $item_id; continue; } if ($item['type'] == 'product') { $item['product'] = $products[$item['product_id']]; $sku = $skus[$item['sku_id']]; if ($sku['image_id'] && $sku['image_id'] != $item['product']['image_id']) { $img = $image_model->getById($sku['image_id']); if ($img) { $item['product']['image_id'] = $sku['image_id']; $item['product']['ext'] = $img['ext']; } } $item['sku_name'] = $sku['name']; $item['sku_code'] = $sku['sku']; $item['price'] = $sku['price']; $item['compare_price'] = $sku['compare_price']; $item['currency'] = $item['product']['currency']; $type_ids[] = $item['product']['type_id']; if (isset($errors[$item_id])) { $item['error'] = $errors[$item_id]; if (strpos($item['error'], '%s') !== false) { $item['error'] = sprintf($item['error'], $item['product']['name'] . ($item['sku_name'] ? ' (' . $item['sku_name'] . ')' : '')); } } } } unset($item); if ($delete_items) { $cart_model->deleteByField(array('code' => $code, 'id' => $delete_items)); } $type_ids = array_unique($type_ids); // get available services for all types of products $type_services_model = new shopTypeServicesModel(); $rows = $type_services_model->getByField('type_id', $type_ids, true); $type_services = array(); foreach ($rows as $row) { $service_ids[] = $row['service_id']; $type_services[$row['type_id']][$row['service_id']] = true; } // get services for all products $product_services_model = new shopProductServicesModel(); $rows = $product_services_model->getByProducts($product_ids); $product_services = $sku_services = array(); foreach ($rows as $row) { if ($row['sku_id'] && !in_array($row['sku_id'], $sku_ids)) { continue; } $service_ids[] = $row['service_id']; if (!$row['sku_id']) { $product_services[$row['product_id']][$row['service_id']]['variants'][$row['service_variant_id']] = $row; } if ($row['sku_id']) { $sku_services[$row['sku_id']][$row['service_id']]['variants'][$row['service_variant_id']] = $row; } } $service_ids = array_unique($service_ids); $service_model = new shopServiceModel(); $variant_model = new shopServiceVariantsModel(); $services = $service_model->getByField('id', $service_ids, 'id'); foreach ($services as &$s) { unset($s['id']); } unset($s); $rows = $variant_model->getByField('service_id', $service_ids, true); foreach ($rows as $row) { $services[$row['service_id']]['variants'][$row['id']] = $row; unset($services[$row['service_id']]['variants'][$row['id']]['id']); } foreach ($items as $item_id => $item) { if ($item['type'] == 'product') { $p = $item['product']; $item_services = array(); // services from type settings if (isset($type_services[$p['type_id']])) { foreach ($type_services[$p['type_id']] as $service_id => &$s) { $item_services[$service_id] = $services[$service_id]; } } // services from product settings if (isset($product_services[$item['product_id']])) { foreach ($product_services[$item['product_id']] as $service_id => $s) { if (!isset($s['status']) || $s['status']) { if (!isset($item_services[$service_id])) { $item_services[$service_id] = $services[$service_id]; } // update variants foreach ($s['variants'] as $variant_id => $v) { if ($v['status']) { if ($v['price'] !== null) { $item_services[$service_id]['variants'][$variant_id]['price'] = $v['price']; } } else { unset($item_services[$service_id]['variants'][$variant_id]); } } } elseif (isset($item_services[$service_id])) { // remove disabled service unset($item_services[$service_id]); } } } // services from sku settings if (isset($sku_services[$item['sku_id']])) { foreach ($sku_services[$item['sku_id']] as $service_id => $s) { if (!isset($s['status']) || $s['status']) { // update variants foreach ($s['variants'] as $variant_id => $v) { if ($v['status']) { if ($v['price'] !== null) { $item_services[$service_id]['variants'][$variant_id]['price'] = $v['price']; } } else { unset($item_services[$service_id]['variants'][$variant_id]); } } } elseif (isset($item_services[$service_id])) { // remove disabled service unset($item_services[$service_id]); } } } foreach ($item_services as $s_id => &$s) { if (!$s['variants']) { unset($item_services[$s_id]); continue; } if ($s['currency'] == '%') { foreach ($s['variants'] as $v_id => $v) { $s['variants'][$v_id]['price'] = $v['price'] * $item['price'] / 100; } $s['currency'] = $item['currency']; } if (count($s['variants']) == 1) { $v = reset($s['variants']); $s['price'] = $v['price']; unset($s['variants']); } } unset($s); uasort($item_services, array('shopServiceModel', 'sortServices')); $items[$item_id]['services'] = $item_services; } else { $items[$item['parent_id']]['services'][$item['service_id']]['id'] = $item['id']; if (isset($item['service_variant_id'])) { $items[$item['parent_id']]['services'][$item['service_id']]['variant_id'] = $item['service_variant_id']; } unset($items[$item_id]); } } foreach ($items as $item_id => $item) { $price = shop_currency($item['price'] * $item['quantity'], $item['currency'], null, false); if (isset($item['services'])) { foreach ($item['services'] as $s) { if (!empty($s['id'])) { if (isset($s['variants'])) { $price += shop_currency($s['variants'][$s['variant_id']]['price'] * $item['quantity'], $s['currency'], null, false); } else { $price += shop_currency($s['price'] * $item['quantity'], $s['currency'], null, false); } } } } $items[$item_id]['full_price'] = $price; } $total = $cart->total(false); $order = array('total' => $total, 'items' => $items); $order['discount'] = $discount = shopDiscounts::calculate($order); $order['total'] = $total = $total - $order['discount']; $data = wa()->getStorage()->get('shop/checkout'); $this->view->assign('cart', array('items' => $items, 'total' => $total, 'count' => $cart->count())); $this->view->assign('coupon_code', isset($data['coupon_code']) ? $data['coupon_code'] : ''); if (shopAffiliate::isEnabled()) { $affiliate_bonus = 0; if ($this->getUser()->isAuth()) { $customer_model = new shopCustomerModel(); $customer = $customer_model->getById($this->getUser()->getId()); $affiliate_bonus = $customer ? round($customer['affiliate_bonus'], 2) : 0; } $this->view->assign('affiliate_bonus', $affiliate_bonus); $use = !empty($data['use_affiliate']); $this->view->assign('use_affiliate', $use); if ($use) { $discount -= shop_currency(shopAffiliate::convertBonus($order['params']['affiliate_bonus']), $this->getConfig()->getCurrency(true), null, false); $this->view->assign('used_affiliate_bonus', $order['params']['affiliate_bonus']); } $order['currency'] = $this->getConfig()->getCurrency(false); $add_affiliate_bonus = shopAffiliate::calculateBonus($order); $this->view->assign('add_affiliate_bonus', round($add_affiliate_bonus, 2)); } $this->view->assign('discount', $discount); /** * @event frontend_cart * @return array[string]string $return[%plugin_id%] html output */ $this->view->assign('frontend_cart', wa()->event('frontend_cart')); $checkout_flow = new shopCheckoutFlowModel(); $checkout_flow->add(array('code' => $code, 'step' => 0, 'description' => null)); }
public function execute() { $this->setLayout(new shopFrontendLayout()); if ($this->params) { $product = $this->params; } else { $product_model = new shopProductModel(); $product = $product_model->getByField('url', waRequest::param('product_url')); } if (!$product) { throw new waException(_w('Product not found'), 404); } if ($types = waRequest::param('type_id')) { if (!in_array($product['type_id'], (array) $types)) { throw new waException(_w('Product not found'), 404); } } $is_cart = waRequest::get('cart'); if ($is_cart) { $this->setLayout(null); } $product = new shopProduct($product); if (!$is_cart) { $this->getBreadcrumbs($product); } // check url $product['num'] = 141; if ($product['url'] !== urldecode(waRequest::param('product_url'))) { $url_params = array('product_url' => $product['url']); if ($product['category_id']) { $url_params['category_url'] = $product['category_url']; } $q = waRequest::server('QUERY_STRING'); $this->redirect(wa()->getRouteUrl('/frontend/product', $url_params) . ($q ? '?' . $q : ''), 301); } $this->prepareProduct($product); $this->addCanonical(); // get services $type_services_model = new shopTypeServicesModel(); $services = $type_services_model->getServiceIds($product['type_id']); $service_model = new shopServiceModel(); $product_services_model = new shopProductServicesModel(); $services = array_merge($services, $product_services_model->getServiceIds($product['id'])); $services = array_unique($services); $services = $service_model->getById($services); $variants_model = new shopServiceVariantsModel(); $rows = $variants_model->getByField('service_id', array_keys($services), true); foreach ($rows as $row) { if (!$row['price']) { $row['price'] = $services[$row['service_id']]['price']; } $services[$row['service_id']]['variants'][$row['id']] = $row; } $rows = $product_services_model->getByField('product_id', $product['id'], true); $skus_services = array(); foreach ($product['skus'] as $sku) { $skus_services[$sku['id']] = array(); } foreach ($rows as $row) { if (!$row['sku_id']) { // remove disabled services and variantsimg if (!$row['status']) { unset($services[$row['service_id']]['variants'][$row['service_variant_id']]); } elseif ($row['price'] !== null) { // update price $services[$row['service_id']]['variants'][$row['service_variant_id']]['price'] = $row['price']; } if ($row['status'] == shopProductServicesModel::STATUS_DEFAULT) { // update default $services[$row['service_id']]['variant_id'] = $row['service_variant_id']; } } else { if (!$row['status']) { $skus_services[$row['sku_id']][$row['service_id']][$row['service_variant_id']] = false; } else { $skus_services[$row['sku_id']][$row['service_id']][$row['service_variant_id']] = $row['price']; } } } foreach ($skus_services as $sku_id => &$sku_services) { $sku_price = $product['skus'][$sku_id]['price']; foreach ($services as $service_id => $service) { if (isset($sku_services[$service_id])) { if ($sku_services[$service_id]) { foreach ($service['variants'] as $v) { if (!isset($sku_services[$service_id][$v['id']]) || $sku_services[$service_id][$v['id']] === null) { $sku_services[$service_id][$v['id']] = array($v['name'], $this->getPrice($v['price'], $service['currency'], $sku_price, $product['currency'])); } elseif ($sku_services[$service_id][$v['id']]) { $sku_services[$service_id][$v['id']] = array($v['name'], $this->getPrice($sku_services[$service_id][$v['id']], $service['currency'], $sku_price, $product['currency'])); } } } } else { foreach ($service['variants'] as $v) { $sku_services[$service_id][$v['id']] = array($v['name'], $this->getPrice($v['price'], $service['currency'], $sku_price, $product['currency'])); } } } } unset($sku_services); // disable service if all variants disabled foreach ($skus_services as $sku_id => $sku_services) { foreach ($sku_services as $service_id => $service) { if (is_array($service)) { $disabled = true; foreach ($service as $v) { if ($v !== false) { $disabled = false; break; } } if ($disabled) { $skus_services[$sku_id][$service_id] = false; } } } } foreach ($services as $s_id => &$s) { if (!$s['variants']) { unset($services[$s_id]); continue; } if ($s['currency'] == '%') { foreach ($s['variants'] as $v_id => $v) { $s['variants'][$v_id]['price'] = $v['price'] * $product['skus'][$product['sku_id']]['price'] / 100; } $s['currency'] = $product['currency']; } if (count($s['variants']) == 1) { $v = reset($s['variants']); if ($v['name']) { $s['name'] .= ' ' . $v['name']; } $s['variant_id'] = $v['id']; $s['price'] = $v['price']; unset($s['variants']); foreach ($skus_services as $sku_id => $sku_services) { if (isset($sku_services[$s_id]) && isset($sku_services[$s_id][$v['id']])) { $skus_services[$sku_id][$s_id] = $sku_services[$s_id][$v['id']][1]; } } } } unset($s); uasort($services, array('shopServiceModel', 'sortServices')); $this->view->assign('sku_services', $skus_services); $this->view->assign('services', $services); $compare = waRequest::cookie('shop_compare', array(), waRequest::TYPE_ARRAY_INT); $this->view->assign('compare', in_array($product['id'], $compare) ? $compare : array()); if (!$is_cart) { $this->view->assign('reviews', $this->getTopReviews($product['id'])); $this->view->assign('rates', $this->reviews_model->getProductRates($product['id'])); $this->view->assign('reviews_total_count', $this->getReviewsTotalCount($product['id'])); $meta_fields = $this->getMetafields($product); $title = $meta_fields['meta_title'] ? $meta_fields['meta_title'] : $product['name']; wa()->getResponse()->setTitle($title); wa()->getResponse()->setMeta('keywords', $meta_fields['meta_keywords']); wa()->getResponse()->setMeta('description', $meta_fields['meta_description']); $feature_codes = array_keys($product->features); $feature_model = new shopFeatureModel(); $features = $feature_model->getByCode($feature_codes); $this->view->assign('features', $features); } $this->view->assign('currency_info', $this->getCurrencyInfo()); /** * @event frontend_product * @param shopProduct $product * @return array[string][string]string $return[%plugin_id%]['menu'] html output * @return array[string][string]string $return[%plugin_id%]['cart'] html output * @return array[string][string]string $return[%plugin_id%]['block_aux'] html output * @return array[string][string]string $return[%plugin_id%]['block'] html output */ $this->view->assign('frontend_product', wa()->event('frontend_product', $product, array('menu', 'cart', 'block_aux', 'block'))); $sku_stocks = array(); foreach ($product->skus as $sku) { $sku_stocks[$sku_id] = array($sku['count'], $sku['stock']); } $stock_model = new shopStockModel(); $this->view->assign('stocks', $stock_model->getAll('id')); $duble = db_query("SELECT * FROM shop_product where id=" . $product['id']); $dubles = mysql_fetch_assoc($duble); $dubles_sku = db_query("SELECT * FROM shop_product_skus where product_id=" . $product['id'] . " and id=" . $dubles['sku_id']); $dubl_sku = mysql_fetch_assoc($dubles_sku); $dubles_prod = db_query("SELECT * FROM shop_product_skus where product_id!=" . $product['id'] . " and sku='" . $dubl_sku['sku'] . "' Group by product_id"); while ($dubl_product = mysql_fetch_array($dubles_prod)) { $dubles_img_pr = db_query("SELECT * FROM shop_product p, shop_product_images img where p.id=" . $dubl_product['product_id'] . " and img.product_id=" . $dubl_product['product_id'] . ""); $dubles_img = mysql_fetch_assoc($dubles_img_pr); if ($dubles_img['status'] != 0) { $koldz = substr($dubles_img['product_id'], -2, 2); $kolds = str_replace($koldz, '', $dubles_img['product_id']); if (preg_match_all("#\\d#", $kolds) < 2) { $kolds = '0' . $kolds; } if ($kolds == 0) { $kolds = '00'; } $alt_test = db_query("SELECT * FROM shop_product_features_selectable where feature_id=12 and product_id=" . $dubles_img['product_id'] . ""); $alt_t = mysql_fetch_assoc($alt_test); $alt_value = db_query("SELECT * FROM shop_feature_values_color where id=" . $alt_t['value_id'] . ""); $alt = mysql_fetch_assoc($alt_value); $dubl_p = $dubl_p . ' <a class="duble_prod" href="http://' . $_SERVER['HTTP_HOST'] . '/index.php/' . $dubles_img['url'] . '" > <img alt="' . $alt['value'] . '" title="' . $alt['value'] . '" style="width:50px" src="/wa-data/public/shop/products/' . $koldz . '/' . $kolds . '/' . $dubles_img['product_id'] . '/images/' . $dubles_img['id'] . '/' . $dubles_img['id'] . '.96x96.jpg"/></a>'; $dubl_p2 = $dubl_p2 . '<a onclick="doubl(' . $dubles_img['product_id'] . ')" class="duble_prod" > <img alt="' . $alt['value'] . '" title="' . $alt['value'] . '" style="width:50px" src="/wa-data/public/shop/products/' . $koldz . '/' . $kolds . '/' . $dubles_img['product_id'] . '/images/' . $dubles_img['id'] . '/' . $dubles_img['id'] . '.96x96.jpg"/></a>'; } $dubl_p = $dubl_p; $dubl_p2 = $dubl_p2; } $this->view->assign('duble', $dubl_p); $this->view->assign('duble2', $dubl_p2); $this->view->assign('duble_list', $dubl_p); $product['num'] = $_GET['num']; $this->view->assign('numis', $_GET['num']); $this->setThemeTemplate($is_cart ? 'product.cart.html' : 'product.html'); }
/** * @param $data */ protected function addService($data) { $item = $this->cart_model->getById($data['parent_id']); if (!$item) { $this->errors = _w('Error'); return; } unset($item['id']); $item['parent_id'] = $data['parent_id']; $item['type'] = 'service'; $item['service_id'] = $data['service_id']; if (isset($data['service_variant_id'])) { $item['service_variant_id'] = $data['service_variant_id']; } else { $service_model = new shopServiceModel(); $service = $service_model->getById($data['service_id']); $item['service_variant_id'] = $service['variant_id']; } $id = $this->cart->addItem($item); $total = $this->cart->total(); $discount = $this->cart->discount(); $this->response['id'] = $id; $this->response['total'] = $this->currencyFormat($total); $this->response['count'] = $this->cart->count(); $this->response['discount'] = $this->currencyFormat($discount); $item_total = $this->cart->getItemTotal($data['parent_id']); $this->response['item_total'] = $this->currencyFormat($item_total); if (shopAffiliate::isEnabled()) { $add_affiliate_bonus = shopAffiliate::calculateBonus(array('total' => $total, 'discount' => $discount, 'items' => $this->cart->items(false))); $this->response['add_affiliate_bonus'] = sprintf(_w("This order will add +%s points to your affiliate bonus."), round($add_affiliate_bonus, 2)); } }
/** * Save * @param int $product_id * @param int $service_id * @param array $data * * Exmple of format * * array ( * 75 => // id of variant * array ( * 'price' => 500, // stirng|float|null * 'status' => '1', * 'skus' => * array ( * 933 => * array ( * 'price' => NULL, * 'status' => '1', * ), * 934 => * array ( * 'price' => NULL, * 'status' => '1', * ), * ), * ), * 76 => * array ( * 'price' => NULL, * 'status' => '2', * 'skus' => * array ( * 933 => * array ( * 'price' => NULL, * 'status' => '1', * ), * 934 => * array ( * 'price' => NULL, * 'status' => '1', * ), * ), * ), * ) * * @return boolean */ public function save($product_id, $service_id, $data) { $product_id = (int) $product_id; $service_id = (int) $service_id; if (!$product_id || !$service_id) { return false; } $key = array('product_id' => $product_id, 'service_id' => $service_id); $add = array(); $update = array(); $variants = $this->getVariants($product_id, $service_id); foreach ($variants as $v_id => $variant) { if (!isset($data[$v_id])) { continue; } $key['service_variant_id'] = $v_id; $ps_id = $variant['ps_id']; $item = $key + $data[$v_id]; unset($item['skus']); if ($ps_id === null) { $item['sku_id'] = null; ksort($item); $add[] = $item; } else { $update[$ps_id] = $item; } if (!empty($data[$v_id]['skus'])) { foreach ($data[$v_id]['skus'] as $sku_id => $sku) { $item = $key + $sku; $item['sku_id'] = $sku_id; if (empty($variant['skus'][$sku_id])) { ksort($item); $add[] = $item; } else { $update[$variant['skus'][$sku_id]['ps_id']] = $item; } } } } if ($update) { foreach ($update as $id => $item) { $this->updateById($id, $item); } } if ($add) { $this->multipleInsert($add); } $service_model = new shopServiceModel(); $currency = $service_model->select('currency')->where('id = i:id', array('id' => $service_id))->fetchField(); if ($currency != '%') { $this->convertPrimaryPrices($product_id, $service_id); } return true; }
public function execute() { list($start_date, $end_date, $group_by) = shopReportsSalesAction::getTimeframeParams(); $mode = waRequest::request('mode', 'sales'); if ($mode !== 'sales') { $mode = 'profit'; } // Top products $pm = new shopProductModel(); $top_products = $pm->getTop(10, $mode, $start_date, $end_date)->fetchAll('id'); $max_val = 0; $product_total_val = 0; foreach ($top_products as &$p) { $p['profit'] = $p['sales'] - $p['purchase']; $p['val'] = $p[$mode]; $max_val = max($p['val'], $max_val); $product_total_val += $p['val']; } foreach ($top_products as &$p) { $p['val_percent'] = round($p['val'] * 100 / ifempty($max_val, 1)); } unset($p); // Top services $sm = new shopServiceModel(); $top_services = $sm->getTop(10, $start_date, $end_date)->fetchAll('id'); $max_val = 0; $service_total_val = 0; foreach ($top_services as $s) { $max_val = max($s['total'], $max_val); $service_total_val += $s['total']; } foreach ($top_services as &$s) { $s['total_percent'] = round($s['total'] * 100 / ifempty($max_val, 1)); } unset($s); // Total sales or pofit for the period $om = new shopOrderModel(); if ($mode == 'sales') { $total_val = $om->getTotalSales($start_date, $end_date); } else { $total_val = $om->getTotalProfit($start_date, $end_date); } // Total sales by product type $sales_by_type = array(); $tm = new shopTypeModel(); $pie_total = 0; foreach ($tm->getSales($start_date, $end_date) as $row) { $sales_by_type[] = array($row['name'], (double) $row['sales']); $pie_total += $row['sales']; } $sales_by_type[] = array(_w('Services'), $service_total_val); $pie_total += $service_total_val; if ($pie_total) { foreach ($sales_by_type as &$row) { $row[0] .= ' (' . round($row[1] * 100 / ifempty($pie_total, 1), 1) . '%)'; } unset($row); } $def_cur = wa()->getConfig()->getCurrency(); $this->view->assign('mode', $mode); $this->view->assign('def_cur', $def_cur); $this->view->assign('total_val', $total_val); $this->view->assign('top_products', $top_products); $this->view->assign('top_services', $top_services); $this->view->assign('product_total_val', $product_total_val); $this->view->assign('service_total_val', $service_total_val); $this->view->assign('pie_data', array($sales_by_type)); }
protected function getServiceVars($product) { $type_services_model = new shopTypeServicesModel(); $services = $type_services_model->getServiceIds($product['type_id']); // Fetch services $service_model = new shopServiceModel(); $product_services_model = new shopProductServicesModel(); $services = array_merge($services, $product_services_model->getServiceIds($product['id'])); $services = array_unique($services); $services = $service_model->getById($services); shopRounding::roundServices($services); // Convert service.price from default currency to service.currency foreach ($services as &$s) { $s['price'] = shop_currency($s['price'], null, $s['currency'], false); } unset($s); // Fetch service variants $variants_model = new shopServiceVariantsModel(); $rows = $variants_model->getByField('service_id', array_keys($services), true); shopRounding::roundServiceVariants($rows, $services); foreach ($rows as $row) { if (!$row['price']) { $row['price'] = $services[$row['service_id']]['price']; } else { if ($services[$row['service_id']]['variant_id'] == $row['id']) { $services[$row['service_id']]['price'] = $row['price']; } } $services[$row['service_id']]['variants'][$row['id']] = $row; } // Fetch service prices for specific products and skus $rows = $product_services_model->getByField('product_id', $product['id'], true); shopRounding::roundServiceVariants($rows, $services); $skus_services = array(); // sku_id => [service_id => price] $frontend_currency = wa('shop')->getConfig()->getCurrency(false); foreach ($product['skus'] as $sku) { $skus_services[$sku['id']] = array(); } foreach ($rows as $row) { if (!$row['sku_id']) { if (!$row['status']) { // remove disabled services and variants unset($services[$row['service_id']]['variants'][$row['service_variant_id']]); } elseif ($row['price'] !== null) { // update price for service variant, when it is specified for this product $services[$row['service_id']]['variants'][$row['service_variant_id']]['price'] = $row['price']; // !!! also set other keys related to price } if ($row['status'] == shopProductServicesModel::STATUS_DEFAULT) { // default variant is different for this product $services[$row['service_id']]['variant_id'] = $row['service_variant_id']; } } else { if (!$row['status']) { $skus_services[$row['sku_id']][$row['service_id']][$row['service_variant_id']] = false; } else { $skus_services[$row['sku_id']][$row['service_id']][$row['service_variant_id']] = $row['price']; } } } // Fill in gaps in $skus_services foreach ($skus_services as $sku_id => &$sku_services) { $sku_price = $product['skus'][$sku_id]['price']; foreach ($services as $service_id => $service) { if (isset($sku_services[$service_id])) { if ($sku_services[$service_id]) { foreach ($service['variants'] as $v) { if (!isset($sku_services[$service_id][$v['id']]) || $sku_services[$service_id][$v['id']] === null) { $sku_services[$service_id][$v['id']] = array($v['name'], $this->getPrice($v['price'], $service['currency'], $sku_price, $product['currency'])); } elseif ($sku_services[$service_id][$v['id']]) { $sku_services[$service_id][$v['id']] = array($v['name'], $this->getPrice($sku_services[$service_id][$v['id']], $service['currency'], $sku_price, $product['currency'])); } } } } else { foreach ($service['variants'] as $v) { $sku_services[$service_id][$v['id']] = array($v['name'], $this->getPrice($v['price'], $service['currency'], $sku_price, $product['currency'])); } } } } unset($sku_services); // disable service if all variants are disabled foreach ($skus_services as $sku_id => $sku_services) { foreach ($sku_services as $service_id => $service) { if (is_array($service)) { $disabled = true; foreach ($service as $v) { if ($v !== false) { $disabled = false; break; } } if ($disabled) { $skus_services[$sku_id][$service_id] = false; } } } } // Calculate prices for %-based services, // and disable variants selector when there's only one value available. foreach ($services as $s_id => &$s) { if (!$s['variants']) { unset($services[$s_id]); continue; } if ($s['currency'] == '%') { foreach ($s['variants'] as $v_id => $v) { $s['variants'][$v_id]['price'] = $v['price'] * $product['skus'][$product['sku_id']]['price'] / 100; } $s['currency'] = $product['currency']; } if (count($s['variants']) == 1) { $v = reset($s['variants']); if ($v['name']) { $s['name'] .= ' ' . $v['name']; } $s['variant_id'] = $v['id']; $s['price'] = $v['price']; unset($s['variants']); foreach ($skus_services as $sku_id => $sku_services) { if (isset($sku_services[$s_id]) && isset($sku_services[$s_id][$v['id']])) { $skus_services[$sku_id][$s_id] = $sku_services[$s_id][$v['id']][1]; } } } } unset($s); uasort($services, array('shopServiceModel', 'sortServices')); return array($services, $skus_services); }
/** * @param $service_ids * @return array */ private function getServices($service_ids) { $service_model = new shopServiceModel(); $variant_model = new shopServiceVariantsModel(); $services = $service_model->getByField('id', $service_ids, 'id'); foreach ($services as &$s) { unset($s['id']); } unset($s); $rows = $variant_model->getByField('service_id', $service_ids, true); foreach ($rows as $row) { $services[$row['service_id']]['variants'][$row['id']] = $row; unset($services[$row['service_id']]['variants'][$row['id']]['id']); } return $services; }