/** * Get the number of placements of a selected seller by marketplace and lang made during a month */ public function sellersDetailsAction() { $this->view->disable(); if ($this->request->isAjax()) { $user_id = $this->request->get('user_id'); $month_and_year = $this->request->get('month_and_year'); $first_day_of_month = strtotime("01.{$month_and_year}"); $first_day_of_next_month = strtotime("01.{$month_and_year} + 1 month"); $table = new MPlacement(); $sql = "SELECT pl.title, COUNT(mp.id) AS count FROM mplacement AS mp\n JOIN marketplace AS pl ON mp.marketplace_id = pl.id\n WHERE mp.created BETWEEN {$first_day_of_month} AND {$first_day_of_next_month}\n AND mp.user_id = {$user_id}\n GROUP BY mp.marketplace_id\n ORDER BY pl.title"; $mplaces = new Resultset(null, $table, $table->getReadConnection()->query($sql)); $mplaces = $mplaces->toArray(); $langs = MPlacement::count(["created BETWEEN {$first_day_of_month} AND {$first_day_of_next_month} AND user_id = {$user_id}", 'group' => 'langcode'])->toArray(); $array['mplaces'] = $mplaces; $array['langs'] = $langs; echo json_encode($array); } }
public function addByIDAction() { $request = $this->request; $response = $this->response; $flash = $this->flash; if ($request->isPost()) { $product_id = $request->getPost('product_id', 'int'); $mplace_id = $request->getPost('mplace_id', 'int'); $lang = $request->getPost('lang', 'string'); $product = PProductMain::findFirst($product_id); $mplace = Marketplace::findFirst($mplace_id); // Validations if (!$product) { $flash->error('Ошибка: товар не найден!'); return $response->redirect('seller/addbyid'); } if ($product->hold == 1) { $flash->error('Ошибка: товар на холде!'); return $response->redirect('seller/addbyid'); } if (!$mplace) { $flash->error('Ошибка: площадка не найдена!'); return $response->redirect('seller/addbyid'); } if (!preg_match("/{$lang}/", $mplace->prefs)) { $flash->error('Ошибка: язык не соответствует площадке!'); return $response->redirect('seller/addbyid'); } // Check for appropriate marketseller $marketseller = Marketseller::findFirst("user_id={$this->auth->id} AND marketplace_id={$mplace_id} AND tmaterial_id={$product->tmaterial_id} AND langcode LIKE '%{$lang}%'"); // If there is no marketseller or if the product is already placed create a new marketseller if (!$marketseller or MPlacement::findFirst("marketseller_id={$marketseller->id} AND product_id={$product->id}")) { $mseller = new Marketseller(); $mseller->id = $mseller->id(); $mseller->created = time(); $mseller->updated = time(); $mseller->fio = "{$product->PCategoryGroup->title} на {$mplace->title}"; $mseller->user_id = $this->auth->id; $mseller->marketplace_id = $mplace_id; $mseller->tmaterial_id = $product->tmaterial_id; $mseller->langcode = $mplace->prefs; if (!$mseller->create()) { $flash->error('Ошибка при создании нового виртуального магазина!'); return $response->redirect('seller/addbyid'); } else { return $response->redirect("seller/product/{$mseller->id}/{$product_id}/{$lang}"); } } else { return $response->redirect("seller/product/{$marketseller->id}/{$product_id}/{$lang}"); } } }
public function sendAction() { $today_timestamp = strtotime('today'); $yesterday_timestamp = strtotime("yesterday"); $yesterday = date('d.m.Y', $yesterday_timestamp); $messages = []; $roles = $this->roles; // Specifics for each role foreach ($roles as $role) { switch ($role) { case 'seller': $status = 3; $role_title = "Менеджер размещений"; $role_lang = false; $role_line = "Всего выполнено за день размещений:"; $role_subject = "Отчет менеджера размещений за {$yesterday}"; break; case 'translator': $status = 3; $role_title = "Переводчик"; $role_lang = true; $role_line = "Всего выполнено за день новых товаров:"; $role_subject = "Отчет переводчика за {$yesterday}"; break; case 'moderatorTranslating': $status = 5; $role_title = "Модератор перевода"; $role_lang = true; $role_line = "Всего отмодерировано за день товаров:"; $role_subject = "Отчет модератора перевода за {$yesterday}"; break; } // Find all users' emails for a role in mail list $table = new PMailList(); $sql = "SELECT DISTINCT(ac.email) FROM p_mail_list AS ml\n LEFT JOIN p_roles AS r\n ON ml.role_id = r.id\n JOIN p_acc_roles AS accr\n LEFT JOIN p_accounts AS ac\n ON accr.user_id = ac.id\n WHERE r.alias = '{$role}' AND accr.user_id = ml.user_id;"; $emails_mail_list = new Resultset(null, $table, $table->getReadConnection()->query($sql)); // Find all users with a specific role $table = new Roles(); $sql = "SELECT ac.id, ac.name, ac.email, ac.prefs FROM p_roles AS r\n LEFT JOIN p_acc_roles AS accr\n ON r.id = accr.role_id\n LEFT JOIN p_accounts AS ac\n ON accr.user_id = ac.id\n WHERE r.alias = '{$role}' AND ac.name IS NOT NULL\n ORDER BY ac.name;"; $all_users = new Resultset(null, $table, $table->getReadConnection()->query($sql)); // List of emails for mail list $emails_batch = []; foreach ($emails_mail_list as $email) { if ($email->email != null) { $emails_batch[] = $email->email; } } $emails_batch = join(', ', $emails_batch); // Find users stats if ($role == 'seller') { $users = MPlacement::count(["created BETWEEN {$yesterday_timestamp} AND {$today_timestamp}", 'group' => 'user_id'])->toArray(); } else { $users = PTasks::count(["tstart BETWEEN {$yesterday_timestamp} AND {$today_timestamp}\n AND tstatus = {$status}\n AND trole = '{$role}'", 'group' => 'tassignee'])->toArray(); } // Total count for a day $total_count = 0; foreach ($users as $user) { $total_count += $user['rowcount']; } $htmlbody_batch = []; $api_stats = []; foreach ($all_users as $one_user) { if (!empty($one_user->name) && !empty($one_user->email)) { foreach ($users as $user) { // The number of user's contributions if (isset($user['user_id'])) { // It is a seller $count[$user['user_id']] = in_array($one_user->id, $user) ? $user['rowcount'] : 0; } else { $count[$user['tassignee']] = in_array($one_user->id, $user) ? $user['rowcount'] : 0; } } $user_count = isset($count[$one_user->id]) ? (int) $count[$one_user->id] : 0; // Check if it is an API $is_api = preg_match('/^API/', $one_user->name); $htmlbody_one = null; $htmlbody_one_table = null; // Check if a user has a language assigned $lang = $role_lang ? " ({$one_user->prefs})" : ""; if (!$is_api) { $htmlbody_one = "{$role_title} {$one_user->name}{$lang}. {$role_line} <strong>{$user_count}</strong><br>"; $htmlbody_one_table = "<tr><td>{$one_user->name}{$lang}</td><td width='50px' align='right'>{$user_count}</td></tr>"; } elseif ($is_api && $user_count > 0) { $api_stats[] = "<tr><td>{$one_user->name}</td><td width='50px' align='right'>{$user_count}</td></tr>"; } $textbody_one = strip_tags($htmlbody_one); // Send an email to a user only if he/she made a contribution the previous day if ($user_count > 0 && !$is_api) { // Compose an email for every user $msg_one = ['To' => $one_user->email, 'From' => POSTMARK_FROM, 'Subject' => "{$role_subject}", 'TextBody' => $textbody_one, 'HtmlBody' => "<html><body>{$htmlbody_one}</body></html>"]; $messages[] = $msg_one; } $htmlbody_batch[] = $htmlbody_one_table; } } $api_stats = join('', $api_stats); $htmlbody_batch = join('', $htmlbody_batch); if ($role == 'seller') { $htmlbody_batch .= $api_stats; } $htmlbody_batch .= "<tr><td> </td></tr><tr><td><strong>Всего за день:</strong></td><td width='50px' align='right'><strong>{$total_count}</strong></td></tr>"; $htmlbody_batch = "<table width='330px'>{$htmlbody_batch}</table>"; $textbody_batch = strip_tags($htmlbody_batch, '<br>'); $textbody_batch = str_replace("<br>", "\n", $textbody_batch); // $this->view->zzz = "<table width='400px'>$htmlbody_batch</table>"; // Compose an email for mail list if (!empty($emails_batch)) { $msg_batch = ['To' => $emails_batch, 'From' => POSTMARK_FROM, 'Subject' => "{$role_subject}", 'TextBody' => $textbody_batch, 'HtmlBody' => "<html><body>{$htmlbody_batch}</body></html>"]; $messages[] = $msg_batch; } } $client = new PostmarkClient(POSTMARK_API); // var_dump($messages); print_r($messages); // $send = $client->sendEmailBatch($messages); }