/**
  * Sends a test notification.
  * 
  * @param int $id Notification id stored in table shop_notification
  * @param array $data Order data array
  * @param string|null $to Recipient address/number. If not specified, recipient address/number from notification parameters is used.
  */
 public static function sendOne($id, $data, $to = null)
 {
     $n = self::getModel()->getOne($id);
     if ($n) {
         $params_model = new shopOrderParamsModel();
         $data['order']['params'] = $params_model->get($data['order']['id']);
         $items_model = new shopOrderItemsModel();
         $data['order']['items'] = $items_model->getItems($data['order']['id']);
         foreach ($data['order']['items'] as &$i) {
             if (!empty($i['file_name'])) {
                 $i['download_link'] = wa()->getRouteUrl('/frontend/myOrderDownload', array('id' => $data['order']['id'], 'code' => $data['order']['params']['auth_code'], 'item' => $i['id']), true);
             }
         }
         unset($i);
         if (!empty($data['order']['params']['shipping_id'])) {
             try {
                 $data['shipping_plugin'] = shopShipping::getPlugin($data['order']['params']['shipping_plugin'], $data['order']['params']['shipping_id']);
             } catch (waException $e) {
             }
         }
         $method = 'send' . ucfirst($n['transport']);
         if (method_exists('shopNotifications', $method)) {
             if ($to !== null) {
                 $n['to'] = $to;
             }
             self::$method($n, $data);
         }
     }
 }
Esempio n. 2
0
 /**
  * Amount of affiliation points given order worths.
  * @param array|int $order_or_id
  * @param float $credit_rate
  * @return float
  */
 public static function calculateBonus($order_or_id, $credit_rate = null)
 {
     if (!self::isEnabled()) {
         return 0;
     }
     if ($credit_rate === null) {
         $credit_rate = wa()->getSetting('affiliate_credit_rate', 0, 'shop');
     }
     if (!$credit_rate) {
         return 0;
     }
     if (wa_is_int($order_or_id)) {
         $om = new shopOrderModel();
         $order = $om->getOrder($order_or_id);
     } else {
         $order = $order_or_id;
     }
     // Convert order total from order currency to default currency
     $curm = new shopCurrencyModel();
     $order_currency = isset($order['currency']) ? $order['currency'] : null;
     $def_cur = wa('shop')->getConfig()->getCurrency(true);
     $affiliatable_total = $curm->convert($order['total'] - ifset($order['shipping'], 0), ifempty($order_currency, $def_cur), $def_cur);
     $product_types = wa()->getSetting('affiliate_product_types', '', 'shop');
     if (!empty($product_types)) {
         //
         // When affiliation program is enabled for certain product types only,
         // we need to calculate total afiliatable amount from order items.
         //
         $product_types = array_fill_keys(explode(',', $product_types), true);
         // Make sure order data contains items
         if (empty($order['items']) && !empty($order['id'])) {
             $oim = new shopOrderItemsModel();
             $order['items'] = $oim->getItems($order['id']);
         }
         if (empty($order['items']) || !is_array($order['items'])) {
             return 0;
         }
         // Fetch product info
         $product_ids = array();
         foreach ($order['items'] as $i) {
             $product_ids[$i['product_id']] = true;
         }
         $pm = new shopProductModel();
         $products = $pm->getById(array_keys($product_ids));
         // Calculate total value of affiliatable order items
         $items_total = 0;
         foreach ($order['items'] as $i) {
             $p = $products[$i['product_id']];
             $type_id = $p['type_id'];
             if ($i['type'] == 'product' && $type_id && !empty($product_types[$type_id])) {
                 $items_total += $curm->convert($i['price'] * $i['quantity'], ifempty($p['currency'], $def_cur), $def_cur);
             }
         }
         if ($affiliatable_total > $items_total) {
             $affiliatable_total = $items_total;
         }
     }
     return $affiliatable_total / $credit_rate;
 }
Esempio n. 3
0
 public function getOrder($id, $extend = false, $escape = true)
 {
     $order = $this->getById($id);
     if (!$order) {
         return array();
     }
     $order_params_model = new shopOrderParamsModel();
     $order['params'] = $order_params_model->get($id);
     if ($order['contact_id']) {
         $contact = new waContact($order['contact_id']);
         $order['contact'] = array('id' => $order['contact_id'], 'name' => $contact->getName(), 'email' => $contact->get('email', 'default'), 'phone' => $contact->get('phone', 'default'));
         $config = wa('shop')->getConfig();
         $use_gravatar = $config->getGeneralSettings('use_gravatar');
         $gravatar_default = $config->getGeneralSettings('gravatar_default');
         if (!$contact->get('photo') && $use_gravatar) {
             $order['contact']['photo_50x50'] = shopHelper::getGravatar($order['contact']['email'], 50, $gravatar_default);
         } else {
             $order['contact']['photo_50x50'] = $contact->getPhoto(50);
         }
     } else {
         $order['contact'] = $this->extractConctactInfo($order['params']);
     }
     if (!empty($order['params']['coupon_id'])) {
         $coupon_model = new shopCouponModel();
         $coupon = $coupon_model->getById($order['params']['coupon_id']);
         $order['coupon'] = array();
         if ($coupon) {
             $order['coupon'] = $coupon;
         } else {
             if (!empty($order['params']['coupon_code'])) {
                 $order['coupon']['code'] = $order['params']['coupon_code'];
             }
         }
     }
     $order_items_model = new shopOrderItemsModel();
     $order['items'] = $order_items_model->getItems($id, $extend);
     if ($escape) {
         if (!empty($order['items'])) {
             foreach ($order['items'] as &$product) {
                 if (!empty($product['name'])) {
                     $product['name'] = htmlspecialchars($product['name']);
                 }
                 if (!empty($product['item']['name'])) {
                     $product['item']['name'] = htmlspecialchars($product['item']['name']);
                 }
                 if (!empty($product['skus'])) {
                     foreach ($product['skus'] as &$sku) {
                         if (!empty($sku['name'])) {
                             $sku['name'] = htmlspecialchars($sku['name']);
                         }
                         unset($sku);
                     }
                 }
                 if (!empty($product['services'])) {
                     foreach ($product['services'] as &$service) {
                         if (!empty($service['name'])) {
                             $service['name'] = htmlspecialchars($service['name']);
                         }
                         if (!empty($service['item']['name'])) {
                             $service['item']['name'] = htmlspecialchars($service['item']['name']);
                         }
                         if (!empty($service['variants'])) {
                             foreach ($service['variants'] as &$variant) {
                                 $variant['name'] = htmlspecialchars($variant['name']);
                                 unset($variant);
                             }
                         }
                         unset($service);
                     }
                 }
                 unset($product);
             }
         }
         $order['contact']['name'] = htmlspecialchars($order['contact']['name']);
     }
     return $order;
 }
Esempio n. 4
0
 /**
  * Helper to send one message: used during real sending, as well as for test emails from follow-ups settings page.
  */
 public static function sendOne($f, $o, $customer, $contact, $to, $view = null, $general = null)
 {
     if (!$view) {
         $view = wa()->getView();
     }
     if (!$general) {
         $general = wa('shop')->getConfig()->getGeneralSettings();
     }
     $workflow = new shopWorkflow();
     $items_model = new shopOrderItemsModel();
     $o['items'] = $items_model->getItems($o['id']);
     foreach ($o['items'] as &$i) {
         if (!empty($i['file_name'])) {
             $i['download_link'] = wa()->getRouteUrl('/frontend/myOrderDownload', array('id' => $o['id'], 'code' => $o['params']['auth_code'], 'item' => $i['id']), true);
         }
     }
     unset($i);
     // Assign template vars
     $view->clearAllAssign();
     $view->assign('followup', $f);
     // row from shop_followup
     $view->assign('order', $o);
     // row from shop_order, + 'params' key
     $view->assign('customer', $contact);
     // shopCustomer
     $view->assign('order_url', wa()->getRouteUrl('/frontend/myOrderByCode', array('id' => $o['id'], 'code' => $o['params']['auth_code']), true));
     $view->assign('status', $workflow->getStateById($o['state_id'])->getName());
     // $shipping_address, $billing_address
     foreach (array('shipping', 'billing') as $k) {
         $address = shopHelper::getOrderAddress($o['params'], $k);
         $formatter = new waContactAddressOneLineFormatter(array('image' => false));
         $address = $formatter->format(array('data' => $address));
         $view->assign($k . '_address', $address['value']);
     }
     // Build email from template
     $subject = $view->fetch('string:' . $f['subject']);
     $body = $view->fetch('string:' . $f['body']);
     $from = $general['email'];
     if ($f['from']) {
         $from = $f['from'];
     }
     // Send the message
     $message = new waMailMessage($subject, $body);
     $message->setTo($to);
     $message->setFrom($from, $general['name']);
     return $message->send();
 }