public function indexAction()
 {
     if ($this->request->isPost()) {
         $currencyrates = filter_input(INPUT_POST, 'currencyrate', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_REQUIRE_ARRAY | FILTER_FLAG_ALLOW_FRACTION);
         foreach ($currencyrates as $title => $rate) {
             $crosscurr = PCrosscurrency::findFirst("title = '{$title}'");
             if ($crosscurr->currencyrate != $rate) {
                 $old = $crosscurr->currencyrate;
                 $crosscurr->currencyrate = $rate;
                 if (!$crosscurr->save()) {
                     $this->flash->error("Произошла ошибка при изменении курса {$title}");
                 } else {
                     $this->modelService->setStat('currency_manipulation', 'currencyrates', 'ru', 0, null, json_encode(['action' => 'rate changed', 'currency' => $title, 'old' => $old, 'new' => $crosscurr->currencyrate]));
                 }
             }
         }
         $this->flash->success("Сохренено!");
     }
     $this->view->rates = PCrosscurrency::find(['order' => 'title']);
     $this->view->rates_switcher = PCrosscurrency::find(["siteswitcher > 0", 'order' => 'siteswitcher']);
 }
 /**
  * Makers list
  */
 public function indexAction()
 {
     $request = $this->request;
     $persistent = $this->persistent;
     // Reset
     $reset = $this->request->getQuery('reset', 'int');
     if (isset($reset)) {
         $persistent->makersParams = null;
     }
     // Persistent parameters
     if (!is_array($persistent->makersParams)) {
         $persistent->makersParams = ['qry' => ['conditions' => "id IS NOT NULL", 'order' => 'updated DESC'], 'sort' => ['page' => 1, 'perpage' => 50]];
     }
     $parameters = $persistent->makersParams;
     $parameters['sort']['perpage'] = isset($parameters['sort']['perpage']) ? $parameters['sort']['perpage'] : 50;
     $parameters['sort']['page'] = isset($parameters['sort']['page']) ? $parameters['sort']['page'] : 1;
     if ($request->getQuery('page', 'int')) {
         $parameters['sort']['page'] = $request->getQuery('page', 'int');
     }
     if ($request->getQuery('perpage', 'int')) {
         $parameters['sort']['perpage'] = $request->getQuery('perpage', 'int');
     }
     // Search
     if ($request->isPost()) {
         $parameters['search_region'] = null;
         $parameters['search_approved'] = null;
         // Placer
         if (!empty($_POST['search_region'])) {
             $search_region_get = $request->getPost('search_region', 'int');
             $parameters['search_region'] = $search_region_get;
             $search_region = " AND geo_id = {$search_region_get}";
         } else {
             $search_region = null;
         }
         // Name or ID
         if (!empty($_POST['search_name'])) {
             $search_name_get = $request->getPost('search_name', 'string');
             if (preg_match('/^[0-9,\\s]+$/', $request->getPost('search_name', 'string'))) {
                 $search_split = preg_split('/[,\\s]/', $search_name_get, 0, PREG_SPLIT_NO_EMPTY);
                 $search_name = implode(', ', $search_split);
                 $search_name = " AND id IN ({$search_name})";
             } else {
                 $search_name = " AND name LIKE '%{$search_name_get}%'";
             }
         } else {
             $search_name_get = null;
             $search_name = null;
         }
         // Email
         if (!empty($_POST['search_email'])) {
             $search_email_get = $request->getPost('search_email', 'string');
             $search_email = " AND email LIKE '%{$search_email_get}%'";
         } else {
             $search_email_get = null;
             $search_email = null;
         }
         // Phone
         if (!empty($_POST['search_phone'])) {
             $search_phone_get = $request->getPost('search_phone', 'string');
             $search_phone = " AND phone LIKE '%{$search_phone_get}%' OR phone2 LIKE '%{$search_phone_get}%'";
         } else {
             $search_phone_get = null;
             $search_phone = null;
         }
         // Approved status
         if (!empty($_POST['search_approved'])) {
             $search_approved_get = $request->getPost('search_approved', 'int');
             $parameters['search_approved'] = $search_approved_get;
             $search_approved = " AND approved = {$search_approved_get}";
         } else {
             $search_approved = null;
         }
         // Search parameters
         $parameters['qry'] = ['conditions' => "id IS NOT NULL\n                    {$search_region}\n                    {$search_name}\n                    {$search_email}\n                    {$search_phone}\n                    {$search_approved}"];
     }
     // Other parameters
     $parameters['count'] = PMaker::count($parameters['qry']);
     $parameters['qry']['order'] = "updated DESC";
     $parameters['qry']['limit'] = 10000;
     $persistent->makersParams = $parameters;
     //        var_dump($parameters);
     $makers = PMaker::find($parameters['qry']);
     // Paginator
     $paginator = new Paginator(["data" => $makers, "limit" => $parameters['sort']['perpage'], "page" => $parameters['sort']['page']]);
     $this->view->page = $paginator->getPaginate();
     // Set selected search values
     $this->view->selected_region = isset($parameters['search_region']) && !empty($parameters['search_region']) ? $parameters['search_region'] : null;
     $this->view->selected_approved = isset($parameters['search_approved']) && !empty($parameters['search_approved']) ? $parameters['search_approved'] : null;
     $this->view->makers_count = $parameters['count'];
     // All regions
     $this->view->regions_list = array_column(Geo::find(['order' => 'title ASC'])->toArray(), 'title', 'id');
     // Currencies
     $currencies_array = array_column(PCrosscurrency::find()->toArray(), 'title', 'title');
     $this->view->currencies_list = ['UAH' => $currencies_array['UAH']] + $currencies_array;
     // Countries
     $countries_array = array_column(Countries::find("active=1")->toArray(), 'title', 'id');
     $ukraine = array_search('Украина', $countries_array);
     $this->view->countries_list = [$ukraine => $countries_array[$ukraine]] + $countries_array;
     $this->view->users_list = Accounts::getUsersByRole(['researcher', 'mobilegroup'], false, 'name ASC');
     $this->view->can_edit = AccRoles::findFirst("user_id={$this->auth->id} AND (role_id=991 OR role_id=5)");
 }
 public function autoplaceAction($seller_id = null, $product_id = null)
 {
     // Find languages for a marketplace
     $seller = Marketseller::findFirst($seller_id);
     $langs = $seller->Marketplace->prefs;
     $langs = preg_split('/[,\\s]/', $langs, 0, PREG_SPLIT_NO_EMPTY);
     $products = [];
     foreach ($langs as $lang) {
         // Check for placement
         if (!($placement = MPlacement::findFirst("marketseller_id={$seller_id} AND product_id={$product_id} AND langcode='{$lang}' AND (active=1 OR active IS NULL)"))) {
             // Check for product existence
             if ($prod = PSelectionSeller::findFirst("id={$product_id} AND lang='{$lang}' AND tmaterial_id={$seller->tmaterial_id} AND hold=0")) {
                 $products[$lang] = $prod;
             }
             // Set one product to represent product information
             if (!isset($product_info) and isset($products[$lang])) {
                 $product_info = $products[$lang];
             }
         }
     }
     if (empty($products)) {
         $this->flash->error("Все товары размещены!");
         return $this->response->redirect("seller/id/{$seller_id}/all");
     }
     $this->view->products = $products;
     $this->view->prod = $product_info;
     $this->view->seller_id = $seller_id;
     $this->view->crosscurr = array_column(PCrosscurrency::find()->toArray(), 'currencyrate', 'title');
     $this->view->delivery_zones = [0 => 'Восточная Азия, Африка, Южная и Центральная Америка', 1 => 'Северная Америка', 2 => 'Россия, Европа, Центральная Азия и Ближний Восток', 3 => 'Австралия и Океания'];
     $qrcodeimg = PProductMain::findFirst("id={$product_id}")->QRcode;
     $this->view->qrcodeimg = $qrcodeimg ? $qrcodeimg : null;
     $this->view->placed_today = $this->modelService->getStat('done', 'seller', 'all', 'day');
     $this->view->placed_month = $this->modelService->getStat('done', 'seller', 'all', 'month');
 }
 /**
  * View all orders
  */
 public function indexAction()
 {
     $request = $this->request;
     $response = $this->response;
     $persistent = $this->persistent;
     // Reset
     $reset = $this->request->getQuery('reset', 'int');
     if (isset($reset)) {
         $persistent->searchOrderParams = null;
     }
     // Persistent parameters
     if (!is_array($persistent->searchOrderParams)) {
         $persistent->searchOrderParams = ['qry' => ['conditions' => "id IS NOT NULL", 'order' => 'created DESC'], 'sort' => ['page' => 1, 'perpage' => 50, 'sortField' => 'created', 'sortOrder' => 'DESC']];
     }
     $parameters = $persistent->searchOrderParams;
     $parameters['sort']['perpage'] = isset($parameters['sort']['perpage']) ? $parameters['sort']['perpage'] : 50;
     $parameters['sort']['page'] = isset($parameters['sort']['page']) ? $parameters['sort']['page'] : 1;
     if ($request->getQuery('sort', 'string')) {
         $parameters['sort']['sortField'] = $request->getQuery('sort', 'string');
         $parameters['sort']['sortOrder'] = $request->getQuery('order', 'string');
     }
     if ($request->getQuery('page', 'int')) {
         $parameters['sort']['page'] = $request->getQuery('page', 'int');
     }
     if ($request->getQuery('perpage', 'int')) {
         $parameters['sort']['perpage'] = $request->getQuery('perpage', 'int');
     }
     $sortField = $parameters['sort']['sortField'];
     $sortOrder = $parameters['sort']['sortOrder'];
     // If this is a post request
     if ($request->isPost()) {
         $parameters['search_id'] = null;
         $parameters['search_country'] = null;
         $parameters['search_currency'] = null;
         $parameters['search_status'] = null;
         $parameters['search_date_from'] = null;
         $parameters['search_date_to'] = null;
         $parameters['search_active'] = null;
         // ID
         if (!empty($_POST['search_id'])) {
             $search_id_get = $request->getPost('search_id');
             if (preg_match('/^\\d+$/', $search_id_get)) {
                 $search_id = " AND id = {$search_id_get}";
             } elseif (preg_match('/@/', $search_id_get)) {
                 $search_id = " AND email LIKE '%{$search_id_get}%'";
             } else {
                 $search_id = null;
             }
             $parameters['search_id'] = $search_id_get;
         } else {
             $search_id = null;
         }
         // Country
         if (!empty($_POST['search_country'])) {
             $search_country_get = $request->getPost('search_country', 'int');
             $parameters['search_country'] = $search_country_get;
             $search_country = " AND countries_id = {$search_country_get}";
         } else {
             $search_country = null;
         }
         // Currency
         if (!empty($_POST['search_currency'])) {
             $search_currency_get = $request->getPost('search_currency', 'string');
             $parameters['search_currency'] = $search_currency_get;
             $search_currency = " AND currency = '{$search_currency_get}'";
         } else {
             $search_currency = null;
         }
         // Status
         if (!empty($_POST['search_status'])) {
             $search_status_get = $request->getPost('search_status', 'string');
             $parameters['search_status'] = $search_status_get;
             switch ($search_status_get) {
                 case 'y':
                     $search_status = " AND payed = 1";
                     break;
                 case 'n':
                     $search_status = " AND payed = 0";
                     break;
                 case 'c':
                     $search_status = " AND cancelled = 1";
                     break;
             }
         } else {
             $search_status = null;
         }
         // Date from
         if (!empty($_POST['search_date_from'])) {
             $search_date_from_get = strtotime($request->getPost('search_date_from', 'string'));
             $parameters['search_date_from'] = $search_date_from_get;
             $search_date_from = " AND created > {$search_date_from_get}";
         } else {
             $search_date_from = null;
         }
         // Date to
         if (!empty($_POST['search_date_to'])) {
             $search_date_to_get = strtotime($request->getPost('search_date_to', 'string')) + 86399;
             $parameters['search_date_to'] = $search_date_to_get;
             $search_date_to = " AND created < {$search_date_to_get}";
         } else {
             $search_date_to = null;
         }
         // Active
         if (!empty($_POST['search_active'])) {
             $search_active_get = (int) $request->getPost('search_active', 'int');
             $parameters['search_active'] = $search_active_get;
             $search_active = " AND deactivated IS NULL OR deactivated = 0";
         } else {
             $search_active = null;
         }
         // Search parameters
         $parameters['qry'] = ['conditions' => "id IS NOT NULL\n                    {$search_id}\n                    {$search_country}\n                    {$search_currency}\n                    {$search_status}\n                    {$search_date_from}\n                    {$search_date_to}\n                    {$search_active}"];
     }
     // Other parameters
     $parameters['count'] = POrder::count($parameters['qry']);
     $parameters['qry']['order'] = "{$sortField} {$sortOrder}";
     $parameters['qry']['limit'] = 1000;
     $persistent->searchOrderParams = $parameters;
     //        var_dump($parameters);
     // Find all products according to set parameters
     $orders = POrder::find($parameters['qry']);
     // Paginator
     $paginator = new Paginator(["data" => $orders, "limit" => $parameters['sort']['perpage'], "page" => $parameters['sort']['page']]);
     // Sorting and table headers mapping
     $headMapping = ['id' => 'Номер', 'created' => 'Дата', 'firstName' => 'Имя', 'lastName' => 'Фамилия', '__country' => 'Страна', 'price' => 'Сумма', 'currency' => 'Валюта', '__payed' => 'Статус', 'paySystem' => 'Способ оплаты'];
     // Sorting header and order arrow
     $headings = array();
     foreach ($headMapping as $field => $name) {
         $headings[$field] = ['field' => $field, 'name' => $name, 'sort' => $sortField == $field ? $sortOrder == 'desc' ? 'asc' : 'desc' : false, 'arrow' => $sortField == $field ? $sortOrder == 'desc' ? '&nbsp;&#8595;' : '&nbsp;&#8593;' : false];
     }
     $this->view->headings = $headings;
     $this->view->page = $paginator->getPaginate();
     // Show total count
     $this->view->count = $parameters['count'];
     // Set selected search values
     $this->view->selected_id = isset($parameters['search_id']) && !empty($parameters['search_id']) ? $parameters['search_id'] : null;
     $this->view->selected_country = isset($parameters['search_country']) && !empty($parameters['search_country']) ? $parameters['search_country'] : null;
     $this->view->selected_currency = isset($parameters['search_currency']) && !empty($parameters['search_currency']) ? $parameters['search_currency'] : null;
     $this->view->selected_status = isset($parameters['search_status']) && !empty($parameters['search_status']) ? $parameters['search_status'] : null;
     $this->view->selected_date_from = isset($parameters['search_date_from']) && !empty($parameters['search_date_from']) ? $parameters['search_date_from'] : null;
     $this->view->selected_date_to = isset($parameters['search_date_to']) && !empty($parameters['search_date_to']) ? $parameters['search_date_to'] : null;
     $this->view->selected_active = isset($parameters['search_active']) && !empty($parameters['search_active']);
     // List of countries
     $this->view->countries = array_column(Countries::find(['order' => 'title'])->toArray(), 'title', 'id');
     // List of currencies
     $this->view->currencies = array_column(PCrosscurrency::find(['conditions' => 'siteswitcher != 0', 'order' => 'siteswitcher'])->toArray(), 'title', 'title');
 }