150 lines
4.9 KiB
PHP
150 lines
4.9 KiB
PHP
<?php
|
|
namespace App\Controller\Manager;
|
|
use Cake\Core\Configure;
|
|
use Cake\Event\Event;
|
|
use Cake\ORM\TableRegistry;
|
|
use App\Controller\AppController;
|
|
|
|
|
|
class OrdersInfoController extends AppController {
|
|
|
|
public function beforeFilter(Event $event) {
|
|
parent::beforeFilter($event);
|
|
$this->viewBuilder()->setLayout('manager');
|
|
|
|
$this->set('isAuthorized', $this->isAuthorized([], ["ordersInfo"]));
|
|
}
|
|
|
|
public function beforeRender(Event $event) {
|
|
}
|
|
|
|
private function searchOrdersInfoOrders($result, $data) {
|
|
$search = preg_replace("/%{2,}/", "%", "%" . preg_replace("/[^\w\d]/ui", "%" , $data['search']) . "%");
|
|
|
|
$searchPhone = "%".cleanupPhone(trim($data['search']))."%";
|
|
//if (strlen($searchPhone) == 10) {
|
|
// $searchPhone = "7" . $searchPhone;
|
|
//}
|
|
//$searchPhone = formatPhone($searchPhone);
|
|
|
|
$conditions = [];
|
|
if (!empty($search)) {
|
|
$conditions[] = ['OR' => [
|
|
'nom LIKE' => $search,
|
|
'fio LIKE' => $search,
|
|
'tel LIKE' => $searchPhone,
|
|
//'tel' => $searchPhone,
|
|
//'(tel LIKE "' . $search . '") OR (tel LIKE "' => $searchPhone . '")'
|
|
]];
|
|
}
|
|
|
|
$result['totalOrders'] = 0;
|
|
$conditions = array_merge($conditions, ['isDeleted' => false]);
|
|
|
|
$currentPage = (!empty($data['currentPage']) ? (int)$data['currentPage']: 1);
|
|
$numPerPage = (!empty($data['numPerPage']) ? (int)$data['numPerPage']: 25);
|
|
|
|
$sort = [
|
|
'field' => 'created',
|
|
'direction' => 'desc'
|
|
];
|
|
|
|
if (!empty($data['sortFields'])) {
|
|
switch($data['sortFields']['field']) {
|
|
case 'nom':
|
|
case 'fio':
|
|
case 'tel':
|
|
case 'totalSum':
|
|
case 'created':
|
|
$sort['field'] = $data['sortFields']['field'];
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!in_array($data['sortFields']['direction'], ['asc', 'desc'])) {
|
|
$sort['direction'] = 'desc';
|
|
} else {
|
|
$sort['direction'] = $data['sortFields']['direction'];
|
|
}
|
|
|
|
$ordersInfoTable = TableRegistry::getTableLocator()->get("OrdersInfo");
|
|
$ordersList = $ordersInfoTable->find('all')->where($conditions)->order([$sort['field'] => $sort['direction']])->page($currentPage, $numPerPage)->all();
|
|
|
|
if (!empty($ordersList)) {
|
|
$ordersList = $ordersList->toArray();
|
|
foreach($ordersList as $key => $order) {
|
|
$order['data'] = json_decode($order['data'], true);
|
|
$order['tel'] = formatPhone($order['tel']);
|
|
$order['created'] = $this->patchTimestampForJS($order['created'], 0, true, 'd.m.Y');
|
|
}
|
|
|
|
$result['totalOrders'] = $ordersInfoTable->find('all')->where($conditions)->count();
|
|
$result['ordersTotalSum'] = str_replace(".00", "", toFixed2Price($ordersInfoTable->find('all')->where($conditions)->sumOf('totalSum')));
|
|
}
|
|
|
|
$result['ordersList'] = (!empty($ordersList) ? $ordersList : []);
|
|
|
|
$result['status'] = "success";
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function index() {
|
|
$jsConfig = $this->getAppJsConfig("ordersInfo", true);
|
|
|
|
$data = $this->request->getData();
|
|
if (!empty($data['action'])) {
|
|
$result = [
|
|
'status' => 'error',
|
|
'errors' => []
|
|
];
|
|
|
|
switch($data['action']) {
|
|
case "searchOrders":
|
|
$result = $this->searchOrdersInfoOrders($result, $data);
|
|
|
|
break;
|
|
}
|
|
|
|
echo $this->jsonSimpleResponse($result);
|
|
} else {
|
|
$this->set('jsConfig', $jsConfig);
|
|
}
|
|
}
|
|
|
|
|
|
function adminDeleteOrder() {
|
|
$result = [
|
|
'status' => 'success'
|
|
];
|
|
|
|
if ($this->isAuthorized(['admin'])) {
|
|
$data = $this->request->getData();
|
|
if (!empty($data['id'])) {
|
|
$ordersInfoTable = TableRegistry::getTableLocator()->get("OrdersInfo");
|
|
$order = $ordersInfoTable->find()->where(['id' => $data['id']])->first();
|
|
|
|
if (!empty($order)) {
|
|
$order->isDeleted = true;
|
|
$ordersInfoTable->save($order);
|
|
|
|
$result = $this->searchOrdersInfoOrders($result, $data);
|
|
} else {
|
|
$result = [
|
|
'status' => 'error',
|
|
'warning' => 'Счет не найден'
|
|
];
|
|
}
|
|
}
|
|
} else {
|
|
$result = [
|
|
'status' => 'error',
|
|
'error' => 'Недостаточно прав для удаления данного счета'
|
|
];
|
|
}
|
|
|
|
echo $this->jsonSimpleResponse($result);
|
|
}
|
|
|
|
}
|