public function execute($params = null)
 {
     $result = array();
     // from payment callback
     if (is_array($params)) {
         $order_id = $params['order_id'];
         $result['text'] = $params['plugin'] . ' (' . $params['view_data'] . ' - ' . $params['amount'] . ' ' . $params['currency_id'] . ')';
         $result['update']['params'] = array('payment_transaction_id' => $params['id']);
     } else {
         $order_id = $params;
         $result['text'] = waRequest::post('text', '');
     }
     $order_model = new shopOrderModel();
     $order = $order_model->getById($order_id);
     $log_model = new waLogModel();
     if (wa()->getEnv() == 'backend') {
         $log_model->add('order_pay', $order_id);
     } else {
         $log_model->add('order_pay_callback', $order_id, $order['contact_id']);
     }
     if (!$order['paid_year']) {
         shopAffiliate::applyBonus($order_id);
         if (wa('shop')->getConfig()->getOption('order_paid_date') == 'create') {
             $time = strtotime($order['create_datetime']);
         } else {
             $time = time();
         }
         $result['update'] = array('paid_year' => date('Y', $time), 'paid_quarter' => floor((date('n', $time) - 1) / 3) + 1, 'paid_month' => date('n', $time), 'paid_date' => date('Y-m-d', $time));
         if (!$order_model->where("contact_id = ? AND paid_date IS NOT NULL", $order['contact_id'])->limit(1)->fetch()) {
             $result['update']['is_first'] = 1;
         }
     }
     return $result;
 }
 public function execute()
 {
     $id = waRequest::get('id');
     $model = new shopNotificationModel();
     $n = $model->getById($id);
     $params_model = new shopNotificationParamsModel();
     $params = $params_model->getParams($id);
     // Orders used as sample data for testing
     $om = new shopOrderModel();
     $test_orders = $om->where("paid_date IS NOT NULL AND state_id <> 'deleted'")->order('id DESC')->limit(10)->fetchAll('id');
     shopHelper::workupOrders($test_orders);
     $im = new shopOrderItemsModel();
     foreach ($im->getByField('order_id', array_keys($test_orders), true) as $i) {
         $test_orders[$i['order_id']]['items'][] = $i;
     }
     foreach ($test_orders as &$o) {
         $o['items'] = ifset($o['items'], array());
         $o['total_formatted'] = waCurrency::format('%{s}', $o['total'], $o['currency']);
     }
     $this->view->assign('n', $n);
     $this->view->assign('params', $params);
     $this->view->assign('transports', self::getTransports());
     $this->view->assign('events', $this->getEvents());
     $this->view->assign('test_orders', $test_orders);
     $this->view->assign('default_email_from', $this->getConfig()->getGeneralSettings('email'));
     $this->view->assign('sms_from', $this->getSmsFrom());
     $this->view->assign('routes', wa()->getRouting()->getByApp('shop'));
 }
 public function execute()
 {
     $contact = wa()->getUser();
     $scm = new shopCustomerModel();
     // Customer orders
     $om = new shopOrderModel();
     $orders = $om->where('contact_id=?', $contact->getId())->order('id DESC')->fetchAll('id');
     // Items for all orders, one query
     $im = new shopOrderItemsModel();
     foreach ($im->getByField('order_id', array_keys($orders), true) as $row) {
         $orders[$row['order_id']]['items'][] = $row;
     }
     // Params for all orders, one query
     $opm = new shopOrderParamsModel();
     foreach ($opm->getByField('order_id', array_keys($orders), true) as $row) {
         $orders[$row['order_id']]['params'][$row['name']] = $row['value'];
     }
     // Prepare order data for template
     $url_tmpl = wa()->getRouteUrl('/frontend/myOrder', array('id' => '%ID%'));
     $workflow = new shopWorkflow();
     foreach ($orders as $k => &$o) {
         if ($o['state_id'] == 'deleted') {
             unset($orders[$k]);
             continue;
         }
         $o['id_str'] = shopHelper::encodeOrderId($o['id']);
         $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'], '');
         $o['state'] = $workflow->getStateById($o['state_id']);
         $o['url'] = str_replace('%ID%', $o['id'], $url_tmpl);
     }
     $this->view->assign('orders', array_values($orders));
     $this->view->assign('my_nav_selected', 'orders');
     // Set up layout and template from theme
     $this->setThemeTemplate('my.orders.html');
     if (!waRequest::isXMLHttpRequest()) {
         $this->setLayout(new shopFrontendLayout());
         $this->getResponse()->setTitle(_w('Orders'));
         $this->view->assign('breadcrumbs', self::getBreadcrumbs());
         $this->layout->assign('nofollow', true);
     }
 }
 public function execute($order_id = null)
 {
     $order_model = new shopOrderModel();
     $order = $order_model->getById($order_id);
     if ($order['paid_year']) {
         return true;
     } else {
         if (wa('shop')->getConfig()->getOption('order_paid_date') == 'create') {
             $time = strtotime($order['create_datetime']);
         } else {
             $time = time();
         }
         shopAffiliate::applyBonus($order_id);
         $result = array('update' => array('paid_year' => date('Y', $time), 'paid_quarter' => floor((date('n', $time) - 1) / 3) + 1, 'paid_month' => date('n', $time), 'paid_date' => date('Y-m-d', $time)));
         if (!$order_model->where("contact_id = ? AND paid_date IS NOT NULL", $order['contact_id'])->limit(1)->fetch()) {
             $result['update']['is_first'] = 1;
         }
         return $result;
     }
 }
Esempio n. 5
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));
     }
 }
 public function execute()
 {
     $id = waRequest::request('id');
     $fm = new shopFollowupModel();
     // Save data when POST came
     if ($id && waRequest::post()) {
         if (waRequest::post('delete')) {
             $f = $fm->getById($id);
             if ($f) {
                 /**
                  * @event followup_delete
                  *
                  * Notify plugins about deleted followup
                  *
                  * @param array[string]int $params['id'] followup_id
                  * @return void
                  */
                 wa()->event('followup_delete', $f);
                 $fm->deleteById($id);
             }
             exit;
         }
         $followup = waRequest::post('followup');
         if ($followup && is_array($followup)) {
             $empty_row = $fm->getEmptyRow();
             $followup = array_intersect_key($followup, $empty_row) + $empty_row;
             unset($followup['id']);
             $followup['delay'] = (double) str_replace(',', '.', ifempty($followup['delay'], '3')) * 24 * 3600;
             if (empty($followup['name'])) {
                 $followup['name'] = _w('<no name>');
             }
             $followup['from'] = $followup['from'] ? $followup['from'] : null;
             $followup['source'] = $followup['source'] ? $followup['source'] : null;
             if ($followup['from'] === 'other') {
                 $followup['from'] = waRequest::post('from');
             }
             if ($id && $id !== 'new') {
                 unset($followup['last_cron_time']);
                 $fm->updateById($id, $followup);
                 $just_created = false;
             } else {
                 $followup['last_cron_time'] = date('Y-m-d H:i:s');
                 $id = $fm->insert($followup);
                 $just_created = true;
             }
             $f = $fm->getById($id);
             if ($f) {
                 $f['just_created'] = $just_created;
                 /**
                  * Notify plugins about created or modified followup
                  * @event followup_save
                  * @param array[string]int $params['id'] followup_id
                  * @param array[string]bool $params['just_created']
                  * @return void
                  */
                 wa()->event('followup_save', $f);
             }
         }
     }
     // List of all follow-ups
     $followups = $fm->getAll('id');
     // Get data to show in form
     $followup = null;
     if ($id) {
         if (empty($followups[$id])) {
             if ($followups) {
                 $followup = reset($followups);
             }
         } else {
             $followup = $followups[$id];
         }
     }
     $test_orders = array();
     if (empty($followup)) {
         $followup = $fm->getEmptyRow();
         $followup['body'] = self::getDefaultBody();
     } else {
         // Orders used as sample data for testing
         $om = new shopOrderModel();
         $test_orders = $om->where("paid_date IS NOT NULL AND state_id <> 'deleted'")->order('id DESC')->limit(10)->fetchAll('id');
         shopHelper::workupOrders($test_orders);
         $im = new shopOrderItemsModel();
         foreach ($im->getByField('order_id', array_keys($test_orders), true) as $i) {
             $test_orders[$i['order_id']]['items'][] = $i;
         }
         foreach ($test_orders as &$o) {
             $o['items'] = ifset($o['items'], array());
             $o['total_formatted'] = waCurrency::format('%{s}', $o['total'], $o['currency']);
         }
     }
     $this->view->assign('followup', $followup);
     $this->view->assign('followups', $followups);
     $this->view->assign('test_orders', $test_orders);
     $this->view->assign('last_cron', wa()->getSetting('last_followup_cli'));
     $this->view->assign('cron_ok', wa()->getSetting('last_followup_cli') + 3600 * 36 > time());
     $this->view->assign('cron_command', 'php ' . wa()->getConfig()->getRootPath() . '/cli.php shop followup');
     $this->view->assign('default_email_from', $this->getConfig()->getGeneralSettings('email'));
     $this->view->assign('routes', wa()->getRouting()->getByApp('shop'));
 }