public function postExecute($order_id = null, $result = null) { if (!$result) { return; } $order_model = new shopOrderModel(); if (is_array($order_id)) { $order = $order_id; $order_id = $order['id']; } else { $order = $order_model->getById($order_id); } $data = is_array($result) ? $result : array(); $data['order_id'] = $order_id; $data['action_id'] = $this->getId(); $data['before_state_id'] = $order['state_id']; if ($this->state_id) { $data['after_state_id'] = $this->state_id; } else { $data['after_state_id'] = $order['state_id']; } $order_log_model = new shopOrderLogModel(); $data['id'] = $order_log_model->add($data); $update = isset($result['update']) ? $result['update'] : array(); $update['update_datetime'] = date('Y-m-d H:i:s'); $data['update'] = $update; if ($this->state_id) { $update['state_id'] = $this->state_id; } $order_model->updateById($order['id'], $update); $order_params_model = new shopOrderParamsModel(); if (isset($update['params'])) { $order_params_model->set($order['id'], $update['params'], false); } $order['params'] = $order_params_model->get($order_id); // send notifications shopNotifications::send('order.' . $this->getId(), array('order' => $order, 'customer' => new waContact($order['contact_id']), 'status' => $this->getWorkflow()->getStateById($data['after_state_id'])->getName(), 'action_data' => $data)); /** * @event order_action.callback * @event order_action.pay * @event order_action.ship * @event order_action.process * @event order_action.delete * @event order_action.restore * @event order_action.complete * @event order_action.comment * * @param array[string]mixed $data * @param array[string]int $data['order_id'] * @param array[string]int $data['action_id'] * @param array[string]int $data['before_state_id'] * @param array[string]int $data['after_state_id'] * @param array[string]int $data['id'] Order log record id */ wa('shop')->event('order_action.' . $this->getId(), $data); return $data; }
protected static function sendSms($n, $data) { $general = wa('shop')->getConfig()->getGeneralSettings(); /** * @var waContact $customer */ $customer = $data['customer']; if ($n['to'] == 'customer') { $to = $customer->get('phone', 'default'); $log = sprintf(_w("Notification <strong>%s</strong> sent to customer."), $n['name']); } elseif ($n['to'] == 'admin') { $to = $general['phone']; $log = sprintf(_w("Notification <strong>%s</strong> sent to store admin."), $n['name']); } else { $to = $n['to']; $log = sprintf(_w("Notification <strong>%s</strong> sent to %s."), $n['name'], $n['to']); } if (!$to) { return; } $view = wa()->getView(); foreach (array('shipping', 'billing') as $k) { $address = shopHelper::getOrderAddress($data['order']['params'], $k); $formatter = new waContactAddressOneLineFormatter(array('image' => false)); $address = $formatter->format(array('data' => $address)); $view->assign($k . '_address', $address['value']); } $order_id = $data['order']['id']; $data['order']['id'] = shopHelper::encodeOrderId($order_id); $view->assign('order_url', wa()->getRouteUrl('/frontend/myOrderByCode', array('id' => $order_id, 'code' => $data['order']['params']['auth_code']), true)); $view->assign($data); $text = $view->fetch('string:' . $n['text']); $sms = new waSMS(); if ($sms->send($to, $text, isset($n['from']) ? $n['from'] : null)) { $order_log_model = new shopOrderLogModel(); $order_log_model->add(array('order_id' => $order_id, 'contact_id' => null, 'action_id' => '', 'text' => '<i class="icon16 mobile"></i> ' . $log, 'before_state_id' => $data['order']['state_id'], 'after_state_id' => $data['order']['state_id'])); } }
public function postExecute($order_id = null, $result = null) { $order_id = $result['order_id']; $data = is_array($result) ? $result : array(); $data['order_id'] = $order_id; $data['action_id'] = $this->getId(); $data['before_state_id'] = ''; $data['after_state_id'] = 'new'; $order_log_model = new shopOrderLogModel(); $order_log_model->add($data); /** * @event order_action.create */ wa('shop')->event('order_action.create', $data); $order_model = new shopOrderModel(); $order = $order_model->getById($order_id); $params_model = new shopOrderParamsModel(); $order['params'] = $params_model->get($order_id); // send notifications shopNotifications::send('order.' . $this->getId(), array('order' => $order, 'customer' => new waContact($order['contact_id']), 'status' => $this->getWorkflow()->getStateById($data['after_state_id'])->getName(), 'action_data' => $data)); // Update stock count, but take into account 'update_stock_count_on_create_order'-setting $app_settings_model = new waAppSettingsModel(); if ($app_settings_model->get('shop', 'update_stock_count_on_create_order')) { // for logging changes in stocks shopProductStocksLogModel::setContext(shopProductStocksLogModel::TYPE_ORDER, 'Order %s was placed', array('order_id' => $order_id)); $order_model->reduceProductsFromStocks($order_id); shopProductStocksLogModel::clearContext(); } return $order_id; }
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() { $modelNotifierConfig = new shopNotifierConfigModel(); $modelNotifierLog = new shopNotifierLogModel(); $n_date = array('w' => 'week', 'd' => 'days', 'm' => 'hour', 'h' => 'minute'); $all_notifications = $modelNotifierConfig->getAll(); foreach ($all_notifications as $notification) { $last_event_date = strtotime($modelNotifierLog->getLastDateByConfigId($notification['id'])); $last_event = strtotime('+' . $notification['repeat_number_time'] . ' ' . $n_date[$notification['repeat_period']], $last_event_date); if (date('Y-m-d H:i:s', $last_event) < date('Y-m-d H:i:s')) { $states = (array) json_decode($notification['state_name']); $last_time = strtotime('-' . $notification['number_time'] . ' ' . $n_date[$notification['period']], $last_event_date); $orders = array(); $orders_new = array(); foreach ($states as $s) { if ($s == 'new') { $collection = new shopOrdersCollection('search/state_id=new&create_datetime<' . date('Y-m-d H:i:s', $last_time)); $orders_new = $collection->getOrders('*,params,items,contact'); } else { $states_without_new[] = $s; } } $state_for_collection = is_array($states_without_new) ? implode('||', $states_without_new) : $states_without_new[0]; $collection = new shopOrdersCollection('search/state_id=' . $state_for_collection . '&update_datetime<' . date('Y-m-d H:i:s', $last_time)); $orders = $collection->getOrders('*,params,items,contact'); if (is_array($orders_new)) { $orders = array_merge($orders, $orders_new); } $emails = array(); $notification['data_contact'] = json_decode($notification['data_contact']); foreach ($notification['data_contact']->contact as $contact) { $user = new waContact($contact); // $email = array(); $email = $user->get('email'); $emails[$email[0]['value']] = $user->get('name'); } if (!empty($notification['data_contact']->group)) { $modelContactCategory = new waContactCategoryModel(); foreach ($notification['data_contact']->group as $group) { $contacts = $modelContactCategory->getByField('category_id', $group); // ->query("SELECT * FROM wa_contact_categories WHERE category_id = '".$gr."'")->fetchAll('contact_id'); foreach ($contacts as $key => $contact) { $user = new waContact($key); $email = array(); $email = $user->get('email'); $emails[$email[0]['value']] = $user->get('name'); } } } $view = wa()->getView(); $shop_config = $general = wa('shop')->getConfig()->getGeneralSettings(); $from = $shop_config['email']; if (!$from) { $from = '*****@*****.**'; } if (!empty($notification['send_email']) && self::isValidEmail($notification['send_email'])) { $from = $notification['send_email']; } $body = file_get_contents(shopNotifierPlugin::path($notification['template'])); //TODO: File read error if ($notification['group_senders'] == 1) { if ($notification['save_to_order_log'] == 1) { $order_log_model = new shopOrderLogModel(); foreach ($orders as $order) { $order_log_model->add(array('order_id' => $order['id'], 'contact_id' => wa()->getUser()->getId(), 'before_state_id' => $order['state_id'], 'after_state_id' => $order['state_id'], 'text' => 'Отправлено уведомление на адреса из оповещания ' . $notification['config_name'], 'action_id' => 'comment')); } } $view->clearAllAssign(); $view->assign('orders', $orders); $subject_string = 'Обратите внимание на заказы'; $subject = $view->fetch('string:' . $subject_string); $body = $view->fetch('string:' . $body); $message = new waMailMessage($subject, $body); $message->setTo($emails); $message->setFrom($from); $message->send(); } else { foreach ($orders as $order) { if ($notification['save_to_order_log'] == 1) { $order_log_model = new shopOrderLogModel(); $order_log_model->add(array('order_id' => $order['id'], 'contact_id' => wa()->getUser()->getId(), 'before_state_id' => $order['state_id'], 'after_state_id' => $order['state_id'], 'text' => 'Отослано уведомление на емайлы из оповещания ' . $notification['config_name'], 'action_id' => 'comment')); } $view->clearAllAssign(); $view->assign('order', $order); $subject_string = 'Заказ ' . shopHelper::encodeOrderId($order['id']); $subject = $view->fetch('string:' . $subject_string); $body = $view->fetch('string:' . $body); $message = new waMailMessage($subject, $body); $message->setTo($emails); $message->setFrom($from); $message->send(); } } $modelNotifierLog->add(array('config_id' => $notification['id'])); } } }