265 lines
8.0 KiB
PHP
265 lines
8.0 KiB
PHP
<?php
|
||
|
||
namespace App\v2\Controller;
|
||
|
||
class ContractsController extends ApiController
|
||
{
|
||
|
||
public function getContracts($app, $get = null, $post = null) {
|
||
|
||
$start_time = microtime(true);
|
||
$error = false;
|
||
$errors = [];
|
||
|
||
$reference_id = null;
|
||
if (isset($get['reference_id']))
|
||
$reference_id = $get['reference_id'];
|
||
|
||
$sources_ids = null;
|
||
if (isset($get['sources_ids']))
|
||
$sources_ids = $get['sources_ids'];
|
||
|
||
$section = $get['section'];
|
||
|
||
$type = null;
|
||
if ($section == "clients")
|
||
$type = 'client';
|
||
else if ($section == "requisitions")
|
||
$type = 'req';
|
||
else if ($section == "objects")
|
||
$type = 'object';
|
||
|
||
$contracts = [];
|
||
if (($user_id = $_SESSION['id']) && $type && (!empty($reference_id) || ($sources_ids && is_array($sources_ids)))) {
|
||
|
||
// Открываем соединение с БД
|
||
$mdb = $app->container->get('mysql');
|
||
$pdo = $app->container->get('mypdo');
|
||
|
||
$contracts_ins = new \Contract();
|
||
|
||
if ($reference_id) {
|
||
$contracts = $contracts_ins->getContractsByRef($reference_id, $type);
|
||
} else {
|
||
foreach ($sources_ids as $source_id) {
|
||
|
||
$contract = $contracts_ins->getContract($source_id, $type);
|
||
|
||
if (is_array($contract)) {
|
||
|
||
if (isset($contract['date_start']))
|
||
$contract['date_start'] = date('Y-m-d', strtotime($contract['date_start']));
|
||
|
||
if (isset($contract['date_end']))
|
||
$contract['date_end'] = date('Y-m-d', strtotime($contract['date_end']));
|
||
|
||
}
|
||
$contracts[] = $contract;
|
||
}
|
||
}
|
||
|
||
$app->response->header('Content-Type', 'application/json');
|
||
$app->response->setStatus(200);
|
||
$app->response->setBody(json_encode([
|
||
'success' => !$error,
|
||
'user_id' => (int)$user_id,
|
||
'overage_time' => round((microtime(true) - $start_time), 3) . " sec.",
|
||
'list' => $contracts
|
||
], JSON_UNESCAPED_UNICODE));
|
||
$app->stop();
|
||
}
|
||
|
||
$app->response->header('Content-Type', 'application/json');
|
||
$app->response->setStatus(200);
|
||
$app->response->setBody(json_encode([
|
||
'success' => false,
|
||
'user_id' => $_SESSION['id'],
|
||
'errors' => $errors,
|
||
'overage_time' => round((microtime(true) - $start_time), 3) . " sec.",
|
||
], JSON_UNESCAPED_UNICODE));
|
||
$app->stop();
|
||
}
|
||
|
||
public function getContract($app, $get = null, $post = null) {
|
||
$error = false;
|
||
$errors = [];
|
||
$source_id = intval($get['source_id']);
|
||
$section = $get['section'];
|
||
|
||
$type = null;
|
||
if ($section == "clients")
|
||
$type = 'client';
|
||
else if ($section == "requisitions")
|
||
$type = 'req';
|
||
else if ($section == "objects")
|
||
$type = 'object';
|
||
|
||
if (($user_id = $_SESSION['id']) && ($source_id && $source_id != 0) && $type) {
|
||
|
||
// Открываем соединение с БД
|
||
$mdb = $app->container->get('mysql');
|
||
$pdo = $app->container->get('mypdo');
|
||
|
||
$contracts_ins = new \Contract();
|
||
$contract = $contracts_ins->getContract($source_id, $type);
|
||
|
||
if (isset($contract['date_start']))
|
||
$contract['date_start'] = date('Y-m-d', strtotime($contract['date_start']));
|
||
|
||
if (isset($contract['date_end']))
|
||
$contract['date_end'] = date('Y-m-d', strtotime($contract['date_end']));
|
||
|
||
$app->response->header('Content-Type', 'application/json');
|
||
$app->response->setStatus(200);
|
||
$app->response->setBody(json_encode([
|
||
'success' => !$error,
|
||
'user_id' => (int)$user_id,
|
||
'item' => $contract
|
||
], JSON_UNESCAPED_UNICODE));
|
||
$app->stop();
|
||
}
|
||
|
||
$app->response->header('Content-Type', 'application/json');
|
||
$app->response->setStatus(200);
|
||
$app->response->setBody(json_encode([
|
||
'success' => false,
|
||
'user_id' => $_SESSION['id'],
|
||
'errors' => $errors,
|
||
], JSON_UNESCAPED_UNICODE));
|
||
$app->stop();
|
||
}
|
||
|
||
public function addEditContract($app, $get = null, $post = null, $need_return = false) {
|
||
|
||
$error = false;
|
||
$errors = [];
|
||
|
||
if (!empty($post)) {
|
||
$data = $post;
|
||
} else {
|
||
$body = $app->request->getBody();
|
||
$data = json_decode($body, true);
|
||
}
|
||
|
||
$section = $data['section'];
|
||
$source_id = intval($data['source_id']);
|
||
$parent_id = (isset($data['parent_id'])) ? trim($data['parent_id']) : null;
|
||
|
||
$parent_type = null;
|
||
if ($section == "clients")
|
||
$parent_type = 'client';
|
||
else if ($section == "requisitions")
|
||
$parent_type = 'req';
|
||
else if ($section == "objects")
|
||
$parent_type = 'object';
|
||
|
||
// Договор на найденном листинге парсера запрещён — листинг read-only,
|
||
// сначала копирование объекта в свои.
|
||
// Offset-free: после снятия смещения id листинга (= реальный external_listings.id) коллизит
|
||
// с objects.id, поэтому различить листинг по id нельзя. Признак — явный флаг is_external
|
||
// из запроса (фронт знает, что договор оформляют на найденном листинге).
|
||
if ($parent_type == 'object' && !empty($data['is_external'])) {
|
||
if ($need_return)
|
||
return false;
|
||
$app->response->header('Content-Type', 'application/json');
|
||
$app->response->setStatus(403);
|
||
$app->response->setBody(json_encode([
|
||
'success' => false,
|
||
'errors' => ['Действие недоступно для найденного листинга. Скопируйте объект в свои, чтобы управлять им.'],
|
||
], JSON_UNESCAPED_UNICODE));
|
||
$app->stop();
|
||
}
|
||
|
||
if (($user_id = $_SESSION['id']) && $parent_type) {
|
||
|
||
// Открываем соединение с БД
|
||
$mdb = $app->container->get('mysql');
|
||
$pdo = $app->container->get('mypdo');
|
||
|
||
$id = (isset($data['id'])) ? trim($data['id']) : null;
|
||
$type = (isset($data['type'])) ? $data['type'] : null;
|
||
$number = (isset($data['number'])) ? trim($data['number']) : "";
|
||
$date_start = (isset($data['date_start'])) ? trim($data['date_start']) : null;
|
||
$date_end = (isset($data['date_end'])) ? trim($data['date_end']) : null;
|
||
$notify_days = (isset($data['notify_days'])) ? intval($data['notify_days']) : 0;
|
||
$is_notified = (isset($data['is_notified'])) ? intval($data['is_notified']) : 0;
|
||
|
||
$object = (isset($data['object'])) ? $data['object'] : [];
|
||
$requisitions = (isset($data['requisitions'])) ? $data['requisitions'] : [];
|
||
|
||
$contract = [
|
||
'id' => $id,
|
||
'parent_id' => $parent_id,
|
||
'parent_type' => $parent_type,
|
||
'number' => $number,
|
||
'type' => $type,
|
||
'date_start' => $date_start,
|
||
'date_end' => $date_end,
|
||
'days_mes_end' => $notify_days,
|
||
'no_mes_end' => $is_notified,
|
||
//'contracts_object' => json_encode($object),
|
||
//'contract_req_id' => json_encode($requisitions),
|
||
];
|
||
|
||
if ($section == "clients")
|
||
$contract['parent_id'] = $source_id;
|
||
else if ($section == "requisitions")
|
||
$contract['parent_id'] = $source_id;
|
||
else if ($section == "objects")
|
||
$contract['parent_id'] = $source_id;
|
||
|
||
$contracts_ins = new \Contract();
|
||
|
||
if (!isset($contract['id']) && isset($results['id']))
|
||
$contract['id'] = (int)$results['id'];
|
||
|
||
if (!isset($contract['date_start']))
|
||
$contract['date_start'] = null;
|
||
|
||
if (!isset($contract['date_end']))
|
||
$contract['date_end'] = null;
|
||
|
||
$agency_id = \User::getAgencyIdForUser($user_id);
|
||
$contracts_ins->setAgencyId($agency_id);
|
||
$results = $contracts_ins->editAdd($contract);
|
||
|
||
if ($need_return) {
|
||
if ($results['source'] == "done") {
|
||
return $contract;
|
||
} else {
|
||
return false;
|
||
}
|
||
} else {
|
||
if ($results['source'] == "done") {
|
||
|
||
$app->response->header('Content-Type', 'application/json');
|
||
$app->response->setStatus(200);
|
||
$app->response->setBody(json_encode([
|
||
'success' => true,
|
||
'user_id' => (int)$user_id,
|
||
'item' => $contract,
|
||
], JSON_UNESCAPED_UNICODE));
|
||
$app->stop();
|
||
} else {
|
||
$error = true;
|
||
$errors[] = $results['sql'];
|
||
$errors[] = $results['mes'];
|
||
}
|
||
}
|
||
}
|
||
|
||
if ($need_return) {
|
||
return false;
|
||
} else {
|
||
$app->response->header('Content-Type', 'application/json');
|
||
$app->response->setStatus(200);
|
||
$app->response->setBody(json_encode([
|
||
'success' => false,
|
||
'user_id' => $_SESSION['id'],
|
||
'errors' => $errors,
|
||
], JSON_UNESCAPED_UNICODE));
|
||
$app->stop();
|
||
}
|
||
}
|
||
}
|