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