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')); }
public function execute() { $contact_id = waRequest::post('contact_id', 0, 'int'); $amount = (double) str_replace(',', '.', waRequest::post('amount', '0')); $comment = trim(waRequest::post('comment', '')); if (!$contact_id || !$amount) { return; } if (!$comment) { if ($amount < 0) { $comment = _w('Bonus pay out'); $this->logAction('affiliate_payout', -$amount, $contact_id); } else { $comment = _w('Bonus credit'); $this->logAction('affiliate_credit', $amount, $contact_id); } } $atm = new shopAffiliateTransactionModel(); $atm->applyBonus($contact_id, $amount, null, ifempty($comment), $amount > 0 ? shopAffiliateTransactionModel::TYPE_DEPOSIT : shopAffiliateTransactionModel::TYPE_WITHDRAWAL); }
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; }