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')); }