206 lines
8.0 KiB
PHP
206 lines
8.0 KiB
PHP
<?php
|
|||
|
|
namespace App\Controller\Manager;
|
||
|
|
use Cake\Event\Event;
|
||
|
|
|
||
|
|
use App\Controller\AppController;
|
||
|
|
use Cake\ORM\TableRegistry;
|
||
|
|
use Cake\Core\Configure;
|
||
|
|
|
||
|
|
class CalcDoorsOrdersController extends AppController {
|
||
|
|
public function beforeFilter(Event $event) {
|
||
|
|
parent::beforeFilter($event);
|
||
|
|
$this->viewBuilder()->setLayout('manager');
|
||
|
|
|
||
|
|
$this->set('isAuthorized', $this->isAuthorized([], ["calcDoors"]));
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
function index($clientId = null) {
|
||
|
|
$this->set('jsConfig', $this->getAppJsConfig("calcDoors", true, null, $clientId));
|
||
|
|
}
|
||
|
|
|
||
|
|
function getOrders() {
|
||
|
|
$result = [
|
||
|
|
'status' => 'success'
|
||
|
|
];
|
||
|
|
|
||
|
|
$data = $this->request->getData();
|
||
|
|
$result['orders'] = $this->getOrdersList("calcDoors", $data);
|
||
|
|
|
||
|
|
echo $this->jsonSimpleResponse($result);
|
||
|
|
}
|
||
|
|
|
||
|
|
function downloadOrderFiles($orderId = null) {
|
||
|
|
if (!empty($orderId)) {
|
||
|
|
$ordersTable = TableRegistry::getTableLocator()->get('CalcDoorsOrders');
|
||
|
|
$order = $ordersTable->find()->where(['id' => $orderId, 'order_type' => 'order'])->first();
|
||
|
|
|
||
|
|
if (!empty($order)) {
|
||
|
|
$orderFileName = $order->order_num . '.zip';
|
||
|
|
$orderFileDir = ROOT . '/files/calc_doors_orders/';
|
||
|
|
$fullOrderFileName = $orderFileDir.$orderFileName;
|
||
|
|
|
||
|
|
if (!is_dir($orderFileDir) || !file_exists($fullOrderFileName)) {
|
||
|
|
} else {
|
||
|
|
header('Content-Length: ' . filesize($fullOrderFileName));
|
||
|
|
header('Content-Type: ' . mime_content_type($fullOrderFileName));
|
||
|
|
header('Content-Disposition: attachment; filename="' . $orderFileName . '"');
|
||
|
|
header('Content-Transfer-Encoding: binary');
|
||
|
|
readfile($fullOrderFileName);
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->redirect(['controller' => 'CalcDoorsOrders', 'action' => 'index', 'prefix' => 'manager']);
|
||
|
|
}
|
||
|
|
|
||
|
|
function takeOrder() {
|
||
|
|
$result = [
|
||
|
|
'status' => 'success'
|
||
|
|
];
|
||
|
|
|
||
|
|
$data = $this->request->getData();
|
||
|
|
if (!empty($data['id']) && !empty($data['ready_date'])) {
|
||
|
|
$ordersTable = TableRegistry::getTableLocator()->get('CalcDoorsOrders');
|
||
|
|
$order = $ordersTable->find()->where(['id' => $data['id'], 'order_type' => 'order'])->first();
|
||
|
|
|
||
|
|
if (!empty($order)) {
|
||
|
|
if (!empty($order->manager_id)) {
|
||
|
|
if ($order->manager_id == $this->Auth->user('id')) {
|
||
|
|
$result = [
|
||
|
|
'status' => 'error',
|
||
|
|
'warning' => 'Заказ уже взят Вами на исполнение'
|
||
|
|
];
|
||
|
|
} else {
|
||
|
|
$user = TableRegistry::getTableLocator()->get('Users')->get($order->manager_id);
|
||
|
|
|
||
|
|
$result = [
|
||
|
|
'status' => 'error',
|
||
|
|
'warning' => 'Заказ уже взят на исполнение пользователем ' . $user['name']
|
||
|
|
];
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$result["manager_id"] = $order->manager_id = $this->Auth->user('id');
|
||
|
|
$order->taken_date = $result["taken_date"] = date('Y-m-d H:i:s'); //todo
|
||
|
|
|
||
|
|
if (is_string($data['ready_date'])) {
|
||
|
|
$data['ready_date'] = strtotime($data['ready_date']);
|
||
|
|
} else {
|
||
|
|
$data['ready_date'] = $data['ready_date'] / 1000;
|
||
|
|
}
|
||
|
|
|
||
|
|
$order->ready_date = $result["ready_date"] = date('Y-m-d H:i:s', $data['ready_date']);
|
||
|
|
$result["taken_date"] = $this->patchTimestampForJS($result['taken_date'], 0);
|
||
|
|
$result["ready_date"] = date_timestamp_get($this->patchTimestampForJS($result['ready_date'], 0, false)) * 1000;
|
||
|
|
|
||
|
|
$ordersTable->save($order);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$result = [
|
||
|
|
'status' => 'error',
|
||
|
|
'error' => 'Заказ не найден'
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
echo $this->jsonSimpleResponse($result);
|
||
|
|
}
|
||
|
|
|
||
|
|
function adminDeleteOrder() {
|
||
|
|
$result = [
|
||
|
|
'status' => 'success'
|
||
|
|
];
|
||
|
|
|
||
|
|
if ($this->isAuthorized(['admin'])) {
|
||
|
|
$data = $this->request->getData();
|
||
|
|
if (!empty($data['id'])) {
|
||
|
|
$ordersTable = TableRegistry::getTableLocator()->get('CalcDoorsOrders');
|
||
|
|
$order = $ordersTable->find()->where(['id' => $data['id'], 'order_type' => 'order'])->first();
|
||
|
|
|
||
|
|
if (!empty($order)) {
|
||
|
|
$ordersTable->delete($order);
|
||
|
|
@unlink(ROOT . '/files/calc_doors_orders/'.$order->order_num . '.zip');
|
||
|
|
|
||
|
|
//$ordersTable->revertOrderDbStatistics($order);
|
||
|
|
} else {
|
||
|
|
$result = [
|
||
|
|
'status' => 'error',
|
||
|
|
'warning' => 'Заказ не найден'
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$result = [
|
||
|
|
'status' => 'error',
|
||
|
|
'error' => 'Недостаточно прав для удаления данного заказа'
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
echo $this->jsonSimpleResponse($result);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
function adminTakeOrder() {
|
||
|
|
$result = [
|
||
|
|
'status' => 'success'
|
||
|
|
];
|
||
|
|
|
||
|
|
if ($this->isAuthorized(['admin'])) {
|
||
|
|
$data = $this->request->getData();
|
||
|
|
if (!empty($data['id'])) {
|
||
|
|
$ordersTable = TableRegistry::getTableLocator()->get('CalcDoorsOrders');
|
||
|
|
$order = $ordersTable->find()->where(['id' => $data['id'], 'order_type' => 'order'])->first();
|
||
|
|
|
||
|
|
if (!empty($order)) {
|
||
|
|
if (empty($data['manager_id'])) {
|
||
|
|
$data['manager_id'] = null;
|
||
|
|
$data['ready_date'] = null;
|
||
|
|
} else {
|
||
|
|
if ($data['manager_id'] != $order->manager_id) {
|
||
|
|
$manager = TableRegistry::getTableLocator()->get('Users')->find()->where(['id' => $data['manager_id']])->first();
|
||
|
|
if (!empty($manager)) {
|
||
|
|
$ordersTable->resendOrderEmailNotify($order, $manager['email'], $this->config);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
$result["manager_id"] = $order->manager_id = $data['manager_id'];
|
||
|
|
$order->taken_date = $result["taken_date"] = date('Y-m-d H:i:s');
|
||
|
|
|
||
|
|
if ($data['ready_date'] != null) {
|
||
|
|
if (is_string($data['ready_date'])) {
|
||
|
|
$data['ready_date'] = strtotime($data['ready_date']);
|
||
|
|
} else {
|
||
|
|
$data['ready_date'] = $data['ready_date'] / 1000;
|
||
|
|
}
|
||
|
|
|
||
|
|
$order->ready_date = $result["ready_date"] = date('Y-m-d H:i:s', $data['ready_date']);
|
||
|
|
} else {
|
||
|
|
$order->ready_date = $result["ready_date"] = null;
|
||
|
|
}
|
||
|
|
|
||
|
|
$result["taken_date"] = $this->patchTimestampForJS($result['taken_date'], 0);
|
||
|
|
|
||
|
|
if ($data['ready_date'] != null) {
|
||
|
|
$result["ready_date"] = date_timestamp_get($this->patchTimestampForJS($result['ready_date'], 0, false)) * 1000;
|
||
|
|
}
|
||
|
|
|
||
|
|
$ordersTable->save($order);
|
||
|
|
} else {
|
||
|
|
$result = [
|
||
|
|
'status' => 'error',
|
||
|
|
'warning' => 'Заказ не найден'
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
$result = [
|
||
|
|
'status' => 'error',
|
||
|
|
'error' => 'Недостаточно прав для изменения данного заказа'
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
echo $this->jsonSimpleResponse($result);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|