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()
 {
     if (!$this->getUser()->getId()) {
         $this->errors[] = sprintf_wp("Please %ssign in%s to be able to vote for photos", '<a href="' . wa()->getRouteUrl('/login', null, true) . '">', '</a>');
         return;
     }
     $plugin = wa()->getPlugin('publicgallery');
     $photo_id = waRequest::post('photo_id', null, waRequest::TYPE_ARRAY_INT);
     $allowed_photo_id = $this->filterAllowedPhotoIds($photo_id);
     if (!$allowed_photo_id) {
         return;
     }
     $vote_model = new photosPublicgalleryVoteModel();
     $photo_model = new photosPhotoModel();
     if (wa()->getEnv() == 'frontend' && !$plugin->getSettings('self_vote')) {
         $photo = $photo_model->getById($allowed_photo_id);
         if (!$photo) {
             $this->errors[] = _w("Photo not found");
             return;
         }
         $photo = reset($photo);
         if ($photo && $photo['contact_id'] == wa()->getUser()->getId()) {
             $this->errors[] = _wp("You may not vote for your own photos");
             return;
         }
     }
     $vote = (int) waRequest::post('rate', 0);
     if ($vote > 0) {
         $vote_model->vote($allowed_photo_id, $vote);
     } else {
         $vote_model->clearVote($allowed_photo_id);
     }
     $this->response['photos'] = $photo_model->select('id, rate, votes_count')->where("id IN (" . implode(',', $photo_id) . ")")->fetchAll();
     foreach ($this->response['photos'] as &$p) {
         if ($p['votes_count']) {
             $p['votes_count_text'] = _wp('%d vote', '%d votes', $p['votes_count']);
         } else {
             $p['votes_count_text'] = '';
         }
     }
     unset($p);
     $this->response['count'] = $photo_model->countRated();
     if (count($photo_id) == 1) {
         $this->response['you_voted'] = (int) $vote_model->getByField(array('photo_id' => $photo_id[0], 'contact_id' => wa()->getUser()->getId()));
     }
 }
 public function validateSettings(&$errors)
 {
     $valid = true;
     if (isset($this->settings['replace'])) {
         if (!empty($this->settings['replace']['search'])) {
             $replace = $this->settings['replace'];
             foreach ($replace['search'] as $id => $search) {
                 if (!empty($replace['is_regexp']) && !empty($replace['is_regexp'][$id])) {
                     $error = self::verifyRegexp($search);
                     $replace['search'][$id] = $search;
                     $this->settings['replace'] = $replace;
                     if ($error !== true) {
                         $valid = false;
                         $errors['replace][search][:' . $id] = sprintf_wp('Invalid regexp pattern: %s', $error);
                     }
                 }
                 unset($search);
             }
         }
     }
     return $valid;
 }
 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';
 }
 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()
 {
     $category_id = waRequest::request('category', 0, 'int');
     $search = waRequest::request('search');
     $start = waRequest::request('start', 0, 'int');
     $limit = 50;
     $order = waRequest::request('order', '!last_order');
     $config = $this->getConfig();
     $use_gravatar = $config->getGeneralSettings('use_gravatar');
     $gravatar_default = $config->getGeneralSettings('gravatar_default');
     // Get customers
     $scm = new shopCustomerModel();
     list($customers, $total) = $scm->getList($category_id, $search, $start, $limit, $order);
     $has_more = $start + count($customers) < $total;
     $countries = array();
     foreach ($customers as &$c) {
         $c['affiliate_bonus'] = (double) $c['affiliate_bonus'];
         if (!$c['photo'] && $use_gravatar) {
             $c['photo'] = shopHelper::getGravatar(!empty($c['email']) ? $c['email'] : '', 50, $gravatar_default);
         } else {
             $c['photo'] = waContact::getPhotoUrl($c['id'], $c['photo'], 50, 50);
         }
         $c['categories'] = array();
         if (!empty($c['address']['region']) && !empty($c['address']['country'])) {
             $countries[$c['address']['country']] = array();
         }
     }
     unset($c);
     // Add region names to addresses
     if ($countries) {
         $rm = new waRegionModel();
         foreach ($rm->where('country_iso3 IN (?)', array_keys($countries))->query() as $row) {
             $countries[$row['country_iso3']][$row['code']] = $row['name'];
         }
         foreach ($customers as &$c) {
             if (!empty($c['address']['region']) && !empty($c['address']['country'])) {
                 $country = $c['address']['country'];
                 $region = $c['address']['region'];
                 if (!empty($countries[$country]) && !empty($countries[$country][$region])) {
                     $c['address']['region_formatted'] = $countries[$country][$region];
                 }
             }
         }
         unset($c);
     }
     // Contact categories
     $ccm = new waContactCategoryModel();
     $categories = $ccm->getAll('id');
     if ($customers) {
         $ccsm = new waContactCategoriesModel();
         foreach ($ccsm->getContactsCategories(array_keys($customers)) as $c_id => $list) {
             foreach ($list as $cat_id) {
                 if (!empty($categories[$cat_id])) {
                     $customers[$c_id]['categories'][$cat_id] = $categories[$cat_id];
                 }
             }
         }
     }
     // Set up lazy loading
     if (!$has_more) {
         // Do not trigger lazy loading, show total count at end of list
         $total_customers_number = $start + count($customers);
     } else {
         $total_customers_number = null;
         // trigger lazy loading
     }
     // List title and other params depending on list type
     if ($search) {
         $title = _w('Search results');
         $hash_start = '#/search/0/' . urlencode($search) . '/';
         $discount = null;
     } else {
         if ($category_id) {
             if (!empty($categories[$category_id])) {
                 $title = $categories[$category_id]['name'];
             } else {
                 $title = _w('Unknown category') . ' ' . $category_id;
             }
             $hash_start = '#/category/' . $category_id . '/';
             if (wa()->getSetting('discount_category')) {
                 $ccdm = new shopContactCategoryDiscountModel();
                 $discount = sprintf_wp('%s%% discount', $ccdm->getDiscount($category_id));
             } else {
                 $discount = null;
             }
         } else {
             $title = _w('All customers');
             $hash_start = '#/all/0/';
             $discount = null;
         }
     }
     $lazy_loading_params = array('limit=' . $limit, 'start=' . ($start + $limit), 'order=' . $order);
     if ($search) {
         $lazy_loading_params[] = 'search=' . $search;
     } else {
         if ($category_id) {
             $lazy_loading_params[] = 'category=' . $category_id;
         }
     }
     $lazy_loading_params = implode('&', $lazy_loading_params);
     $this->view->assign('cols', self::getCols());
     $this->view->assign('title', $title);
     $this->view->assign('order', $order);
     $this->view->assign('total', $total);
     $this->view->assign('discount', $discount);
     $this->view->assign('customers', $customers);
     $this->view->assign('hash_start', $hash_start);
     $this->view->assign('category_id', $category_id);
     $this->view->assign('lazy_loading_params', $lazy_loading_params);
     $this->view->assign('total_customers_number', $total_customers_number);
 }
Beispiel #7
0
 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));
     }
 }