public function execute() { $order_id = waRequest::request('order_id', 0, 'int'); $id = waRequest::request('id', 0, 'int'); $to = waRequest::request('to'); $nm = new shopNotificationModel(); $n = $nm->getById($id); if (!$n) { $this->errors = sprintf_wp('%s entry not found', _w('Notification')); return; } $om = new shopOrderModel(); $o = $om->getById($order_id); if (!$o) { $this->errors = _w('Order not found'); return; } shopHelper::workupOrders($o, true); $opm = new shopOrderParamsModel(); $o['params'] = $opm->get($order_id); try { $contact = $o['contact_id'] ? new shopCustomer($o['contact_id']) : wa()->getUser(); $contact->getName(); } catch (Exception $e) { $contact = new shopCustomer(wa()->getUser()->getId()); } $cm = new shopCustomerModel(); $customer = $cm->getById($contact->getId()); if (!$customer) { $customer = $cm->getEmptyRow(); } $workflow = new shopWorkflow(); // send notifications shopNotifications::sendOne($id, array('order' => $o, 'customer' => $contact, 'status' => $workflow->getStateById($o['state_id'])->getName()), $to); }
public function execute() { $order_id = waRequest::post('order_id', null, waRequest::TYPE_INT); if ($order_id) { $order_model = new shopOrderModel(); $order = $order_model->getOrder($order_id); $customer_model = new shopCustomerModel(); $customer = $customer_model->getById($order['contact_id']); $customer_model->updateById($order['contact_id'], array('is_spamer' => 1)); $plugin = waSystem::getInstance()->getPlugin('orderantispam'); $action_id = $plugin->getSettings('action_id'); $workflow = new shopWorkflow(); $action = $workflow->getActionById($action_id); $action->run($order_id); // counters $state_counters = $order_model->getStateCounters(); $pending_counters = (!empty($state_counters['new']) ? $state_counters['new'] : 0) + (!empty($state_counters['processing']) ? $state_counters['processing'] : 0) + (!empty($state_counters['paid']) ? $state_counters['paid'] : 0); // update app coutner wa('shop')->getConfig()->setCount($state_counters['new']); $script = "<script>"; $script .= "\$.order_list.updateCounters(" . json_encode(array('state_counters' => $state_counters, 'common_counters' => array('pending_counters' => $pending_counters))) . ");"; $script .= "\$.order.reload();</script>"; $this->response['script'] = $script; } }
public function execute() { if (!shopAffiliate::isEnabled()) { throw new waException(_w('Unknown page'), 404); } $scm = new shopCustomerModel(); $customer = $scm->getById(wa()->getUser()->getId()); $atm = new shopAffiliateTransactionModel(); $affiliate_history = $atm->getByContact(wa()->getUser()->getId()); $url_tmpl = wa()->getRouteUrl('/frontend/myOrder', array('id' => '%ID%')); foreach ($affiliate_history as &$row) { if ($row['order_contact_id'] == $row['contact_id']) { $row['order_url'] = str_replace('%ID%', $row['order_id'], $url_tmpl); } } $this->view->assign('customer', $customer); $this->view->assign('affiliate_history', $affiliate_history); // Set up layout and template from theme $this->setThemeTemplate('my.affiliate.html'); $this->view->assign('my_nav_selected', 'affiliate'); if (!waRequest::isXMLHttpRequest()) { $this->setLayout(new shopFrontendLayout()); $this->getResponse()->setTitle(_w('Affiliate program')); $this->view->assign('breadcrumbs', self::getBreadcrumbs()); $this->layout->assign('nofollow', true); } /** * * @event frontend_my_affiliate * @return array[string]string $return[%plugin_id%] html output */ $this->view->assign('frontend_my_affiliate', wa()->event('frontend_my_affiliate')); }
protected function getCustomerData($name = null) { if ($this->customer_data == null) { $customer_model = new shopCustomerModel(); $this->customer_data = $customer_model->getById($this->getId()); } if ($name) { return isset($this->customer_data[$name]) ? $this->customer_data[$name] : null; } return $this->customer_data; }
public function orderActionCreate($params) { if ($this->getSettings('status') && waRequest::cookie('uuid')) { $customer_model = new shopCustomerModel(); $is_spamer = $customer_model->getByField(array('uuid' => waRequest::cookie('uuid'), 'is_spamer' => 1)); $update = array(); if ($is_spamer && $this->getSettings('action_id')) { $update['is_spamer'] = 1; $action_id = $this->getSettings('action_id'); $workflow = new shopWorkflow(); $action = $workflow->getActionById($action_id); $action->run($params['order_id']); } $customer = $customer_model->getById($params['contact_id']); if (!$customer['uuid']) { $update['uuid'] = waRequest::cookie('uuid'); $customer_model->updateById($params['contact_id'], $update); } } }
public function execute() { $order_id = waRequest::request('order_id', 0, 'int'); $followup_id = waRequest::request('followup_id', 0, 'int'); $email = waRequest::request('email'); $fm = new shopFollowupModel(); $f = $fm->getById($followup_id); if (!$f) { $this->errors = sprintf_wp('%s entry not found', _w('Follow-up')); return; } $om = new shopOrderModel(); $o = $om->getById($order_id); if (!$o) { $this->errors = _w('Order not found'); return; } shopHelper::workupOrders($o, true); $opm = new shopOrderParamsModel(); $o['params'] = $opm->get($order_id); try { $contact = $o['contact_id'] ? new shopCustomer($o['contact_id']) : wa()->getUser(); $contact->getName(); } catch (Exception $e) { $contact = new shopCustomer(wa()->getUser()->getId()); } $cm = new shopCustomerModel(); $customer = $cm->getById($contact->getId()); if (!$customer) { $customer = $cm->getEmptyRow(); } $to = array($email => $contact->getName()); if (!shopFollowupCli::sendOne($f, $o, $customer, $contact, $to)) { $this->errors = "Unable to send follow-up #{$f['id']} for order #{$o['id']}: waMessage->send() returned FALSE."; return; } $this->response = 'ok'; }
/** Discounts by amount of money previously spent by this customer. */ protected static function byCustomerTotal($order, $contact, $apply) { if (!$contact || !$contact->getId()) { return 0; } $cm = new shopCustomerModel(); $customer = $cm->getById($contact->getId()); if ($customer && $customer['total_spent'] > 0) { $dbsm = new shopDiscountBySumModel(); return max(0.0, min(100.0, (double) $dbsm->getDiscount('customer_total', $customer['total_spent']))) * $order['total'] / 100.0; } return 0.0; }
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() { $order = $this->getOrder(); if (!$order) { $this->view->assign('order', $order); return; } $workflow = new shopWorkflow(); $actions = $workflow->getStateById($order['state_id'])->getActions(); $bottom_buttons = $top_buttons = $buttons = array(); foreach ($actions as $action) { /** * @var shopWorkflowAction $action */ if ($action->getOption('top') || $action->getOption('position') == 'top') { $top_buttons[] = $action->getButton(); } elseif ($action->getOption('position') == 'bottom') { $bottom_buttons[] = $action->getButton(); } else { $buttons[] = $action->getButton(); } } $config = $this->getConfig(); $last_action_datetime = null; $log_model = new shopOrderLogModel(); $log = $log_model->getLog($order['id']); foreach ($log as &$l) { if ($l['action_id']) { $l['action'] = $workflow->getActionById($l['action_id']); } if ($order['state_id'] == $l['after_state_id']) { $last_action_datetime = $l['datetime']; } } $params = $order['params']; $tracking = ''; if (!empty($params['shipping_id'])) { try { $plugin = shopShipping::getPlugin(null, $params['shipping_id']); if (!empty($params['tracking_number'])) { $tracking = $plugin->tracking($params['tracking_number']); } if ($custom_fields = $plugin->customFields(new waOrder())) { foreach ($custom_fields as $k => $v) { if (!empty($params['shipping_params_' . $k])) { $custom_fields[$k]['value'] = $params['shipping_params_' . $k]; } else { unset($custom_fields[$k]); } } $this->view->assign('custom_fields', $custom_fields); } } catch (waException $ex) { $tracking = $ex->getMessage(); } } $this->view->assign('tracking', $tracking); $settings = wa('shop')->getConfig()->getCheckoutSettings(); $form_fields = ifset($settings['contactinfo']['fields'], array()); $formatter = new waContactAddressSeveralLinesFormatter(); $shipping_address = shopHelper::getOrderAddress($params, 'shipping'); $this->view->assign('shipping_address_text', shopHelper::getShippingAddressText($params)); $shipping_address = $formatter->format(array('data' => $shipping_address)); $shipping_address = $shipping_address['value']; if (isset($form_fields['address.billing'])) { $billing_address = shopHelper::getOrderAddress($params, 'billing'); $billing_address = $formatter->format(array('data' => $billing_address)); $billing_address = $billing_address['value']; if ($billing_address === $shipping_address) { $billing_address = null; } } else { $billing_address = null; } $customer_model = new shopCustomerModel(); $customer = $customer_model->getById($order['contact_id']); $customer_contact = new waContact($order['contact_id']); // Customer info $main_contact_info = array(); foreach (array('email', 'phone', 'im') as $f) { if ($v = $customer_contact->get($f, 'top,html')) { $main_contact_info[] = array('id' => $f, 'name' => waContactFields::get($f)->getName(), 'value' => is_array($v) ? implode(', ', $v) : $v); } } $this->view->assign(array('customer' => $customer, 'customer_contact' => $customer_contact, 'main_contact_info' => $main_contact_info, 'currency' => $config->getCurrency(), 'order' => $order, 'params' => $params, 'log' => $log, 'last_action_datetime' => $last_action_datetime, 'bottom_buttons' => $bottom_buttons, 'top_buttons' => $top_buttons, 'buttons' => $buttons, 'filter_params' => $this->getParams(), 'filter_params_str' => $this->getParams(true), 'count_new' => $this->getModel()->getStateCounters('new'), 'timeout' => $config->getOption('orders_update_list'), 'printable_docs' => shopHelper::getPrintForms(array_merge($order, array('params' => $params))), 'billing_address' => $billing_address, 'shipping_address' => $shipping_address, 'shipping_id' => ifset($params['shipping_id'], '') . '.' . ifset($params['shipping_rate_id'], ''), 'offset' => $this->getModel()->getOffset($order['id'], $this->getParams(), true))); /** * Backend order profile page * UI hook allow extends order profile page * @event backend_order * @param array $order * @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%]['action_link'] html output * @return array[string][string]string $return[%plugin_id%]['info_section'] html output */ $this->view->assign('backend_order', wa()->event('backend_order', $order, array('title_suffix', 'action_button', 'action_link', 'info_section'))); }
public function execute() { $id = waRequest::request('id', null, waRequest::TYPE_INT); $scm = new shopCustomerModel(); $customer = $scm->getById($id); try { $contact = new waContact($id); $contact->getName(); } catch (waException $e) { // !!! What to do when shop_customer exists, but no wa_contact found? throw $e; } $ccsm = new waContactCategoriesModel(); $contact_categories = $ccsm->getContactCategories($id); $contacts_url = wa()->getAppUrl('contacts'); // Info above tabs $top = array(); foreach (array('email', 'phone', 'im') as $f) { if ($v = $contact->get($f, 'top,html')) { $top[] = array('id' => $f, 'name' => waContactFields::get($f)->getName(), 'value' => is_array($v) ? implode(', ', $v) : $v); } } // Get photo $photo = $contact->get('photo'); $config = $this->getConfig(); $use_gravatar = $config->getGeneralSettings('use_gravatar'); $gravatar_default = $config->getGeneralSettings('gravatar_default'); if (!$photo && $use_gravatar) { $photo = shopHelper::getGravatar($contact->get('email', 'default'), 96, $gravatar_default); } else { $photo = $contact->getPhoto(96); } $contact['photo'] = $photo; // Customer orders $im = new shopOrderItemsModel(); $orders_collection = new shopOrdersCollection('search/contact_id=' . $id); $total_count = $orders_collection->count(); $orders = $orders_collection->getOrders('*,items,params', 0, $total_count); shopHelper::workupOrders($orders); foreach ($orders as &$o) { $o['total_formatted'] = waCurrency::format('%{s}', $o['total'], $o['currency']); $o['shipping_name'] = ifset($o['params']['shipping_name'], ''); $o['payment_name'] = ifset($o['params']['payment_name'], ''); // !!! TODO: shipping and payment icons } // Customer reviews $prm = new shopProductReviewsModel(); $reviews = $prm->getList('*,is_new,product', array('escape' => false, 'where' => array('contact_id' => $id), 'limit' => false)); // Customer affiliate transactions history $atm = new shopAffiliateTransactionModel(); $affiliate_history = $atm->getByContact($id); $this->view->assign('top', $top); $this->view->assign('orders', $orders); $this->view->assign('reviews', $reviews); $this->view->assign('contact', $contact); $this->view->assign('customer', $customer); $this->view->assign('contacts_url', $contacts_url); $this->view->assign('affiliate_history', $affiliate_history); $this->view->assign('contact_categories', $contact_categories); $this->view->assign('def_cur_tmpl', str_replace('0', '%s', waCurrency::format('%{s}', 0, wa()->getConfig()->getCurrency()))); $this->view->assign('point_rate', str_replace(',', '.', (double) str_replace(',', '.', wa()->getSetting('affiliate_usage_rate')))); $fields = waContactFields::getAll('person'); if (isset($fields['name'])) { unset($fields['name']); } $this->view->assign('fields', $fields); $this->view->assign('orders_default_view', $config->getOption('orders_default_view')); /* * @event backend_customer * @return array[string]array $return[%plugin_id%] array of html output * @return array[string][string]string $return[%plugin_id%]['info_section'] html output * @return array[string][string]string $return[%plugin_id%]['name_suffix'] html output * @return array[string][string]string $return[%plugin_id%]['header'] html output * @return array[string][string]string $return[%plugin_id%]['action_link'] html output */ $this->view->assign('backend_customer', wa()->event('backend_customer', $customer)); }
public static function discount(&$order, $contact, $apply, $other_discounts) { if (!$contact || !$contact->getId()) { return 0; } $checkout_data = wa()->getStorage()->read('shop/checkout'); if (empty($checkout_data['use_affiliate'])) { return 0; // !!! Will this fail when recalculating existing order? } $usage_rate = (double) wa()->getSetting('affiliate_usage_rate', 0, 'shop'); if ($usage_rate <= 0) { return 0; } $cm = new shopCustomerModel(); $customer = $cm->getById($contact->getId()); if (!$customer || $customer['affiliate_bonus'] <= 0) { return 0; } $order_total = $order['total'] - $other_discounts; $max_bonus = $customer['affiliate_bonus']; if (!empty($order['params']['affiliate_bonus'])) { // Recalculating existing order: take old discount into account $max_bonus += $order['params']['affiliate_bonus']; } $crm = new shopCurrencyModel(); $discount = (double) $crm->convert($max_bonus * $usage_rate, wa()->getConfig()->getCurrency(true), wa()->getConfig()->getCurrency(false)); if ($discount > $order_total) { $discount = $order_total; } if ($discount < $order_total) { $bonus_used = $max_bonus; } else { $bonus_used = (double) $crm->convert($discount, wa()->getConfig()->getCurrency(false), wa()->getConfig()->getCurrency(true)) / $usage_rate; } if (empty($order['params'])) { $order['params'] = array(); } $order['params']['affiliate_bonus'] = $bonus_used; if ($apply) { $balance_change = $max_bonus - $bonus_used - $customer['affiliate_bonus']; if (abs($balance_change) > 0.0001) { if (!empty($order['params']['affiliate_bonus'])) { $message = sprintf_wp('Recalculation of order total, new discount: %s', waCurrency::format('%{s}', $discount, wa()->getConfig()->getCurrency())); } else { $message = sprintf_wp('Discount of %s', waCurrency::format('%{s}', $discount, wa()->getConfig()->getCurrency())); } $atm = new shopAffiliateTransactionModel(); $atm->applyBonus($contact->getId(), $balance_change, ifset($order['id']), $message); } } return $discount; }
public function execute() { $fm = new shopFollowupModel(); $opm = new shopOrderParamsModel(); $asm = new waAppSettingsModel(); $olm = new shopOrderLogModel(); $cm = new shopCustomerModel(); $om = new shopOrderModel(); $asm->set('shop', 'last_followup_cli', time()); $view = wa()->getView(); $empty_customer = $cm->getEmptyRow(); $general = wa('shop')->getConfig()->getGeneralSettings(); foreach ($fm->getAllEnabled() as $f) { $between_from = date('Y-m-d', strtotime($f['last_cron_time']) - 24 * 3600); $between_to = date('Y-m-d 23:59:59', time() - $f['delay'] - 10 * 3600); $orders = $om->where('paid_date >= ? AND paid_date < ?', $between_from, $between_to)->fetchAll('id'); if ($orders) { $f_param_key = 'followup_' . $f['id']; // Params for all orders with one query $params = $opm->get(array_keys($orders)); // Customer data for all orders with one query $cids = array(); foreach ($orders as $o) { $cids[] = $o['contact_id']; } $customers = $cm->getById($cids); $sent_count = 0; foreach ($orders as $o) { try { // Is there a recipient in the first place? if (empty($o['contact_id'])) { if (waSystemConfig::isDebug()) { waLog::log("Unable to send follow-up #{$f['id']} for order #{$o['id']}: no contact_id"); } continue; } // Check that this is the first order of this customer if ($f['first_order_only']) { $first_order_id = $om->select('MIN(id)')->where('contact_id=? AND paid_date IS NOT NULL', $o['contact_id'])->fetchField(); if ($first_order_id != $o['id']) { if (waSystemConfig::isDebug()) { waLog::log("Skipping follow-up #{$f['id']} for order #{$o['id']}: not the first order of a customer."); } continue; } } $o['params'] = ifset($params[$o['id']], array()); $source = 'backend'; if (!empty($o['params']['storefront'])) { $source = rtrim($o['params']['storefront'], '/') . '/*'; } if ($f['source'] && $f['source'] != $source) { continue; } // Make sure we have not send follow-up for this order yet if (isset($o['params'][$f_param_key])) { if (waSystemConfig::isDebug()) { waLog::log("Skipping follow-up #{$f['id']} for order #{$o['id']}: already sent before."); } continue; } shopHelper::workupOrders($o, true); // Recipient info $customer = ifset($customers[$o['contact_id']], $empty_customer); $contact = new shopCustomer($o['contact_id']); $email = $contact->get('email', 'default'); // this with throw exception if contact does not exist; that's ok if (!$email) { if (waSystemConfig::isDebug()) { waLog::log("Unable to send follow-up #{$f['id']} for order #{$o['id']}: contact has no email"); } continue; } $to = array($email => $contact->getName()); if (self::sendOne($f, $o, $customer, $contact, $to, $view, $general)) { $sent_count++; // Write to order log $olm->add(array('order_id' => $o['id'], 'contact_id' => null, 'action_id' => '', 'text' => sprintf_wp("Follow-up <strong>%s</strong> (%s) sent to customer.", htmlspecialchars($f['name']), $f['id']), 'before_state_id' => $o['state_id'], 'after_state_id' => $o['state_id'])); // Write to order params $opm->insert(array('order_id' => $o['id'], 'name' => $f_param_key, 'value' => date('Y-m-d H:i:s'))); } else { waLog::log("Unable to send follow-up #{$f['id']} for order #{$o['id']}: waMessage->send() returned FALSE."); } } catch (Exception $e) { waLog::log("Unable to send follow-up #{$f['id']} for order #{$o['id']}:\n" . $e); } } /** * Notify plugins about sending followup * @event followup_send * @param array[string]int $params['sent_count'] number of emails successfully sent * @param array[string]int $params['id'] followup_id * @return void */ $event_params = $f; $event_params['sent_count'] = $sent_count; wa()->event('followup_send', $event_params); } $fm->updateById($f['id'], array('last_cron_time' => $between_to)); } }