336 lines
10 KiB
PHP
336 lines
10 KiB
PHP
<?php
|
|
class Contract {
|
|
private $agency_id;
|
|
private $user_id;
|
|
private $db = null;
|
|
private $table = null;
|
|
private $parent = null;
|
|
|
|
public function __construct($db = null)
|
|
{
|
|
if($db){
|
|
$this->db = $db;
|
|
} else {
|
|
$pdo = new MysqlPdo(hst, ndb, user, pass);
|
|
$sql = "SET NAMES 'utf8'";
|
|
$pdo->query($sql);
|
|
$this->db = $pdo;
|
|
}
|
|
}
|
|
|
|
private function setTable($parent_type){
|
|
if($parent_type == 'req'){
|
|
$this->table = 'contracts_req';
|
|
$this->parent = "req_id";
|
|
} else if ($parent_type == 'client') {
|
|
$this->table = 'contracts_client';
|
|
$this->parent = "client_id";
|
|
} else if ($parent_type == 'object') {
|
|
$this->table = 'contracts_object';
|
|
$this->parent = "object_id";
|
|
}
|
|
}
|
|
|
|
public function setAgencyId ($agency_id)
|
|
{
|
|
$this->agency_id = $agency_id;
|
|
}
|
|
|
|
public function setUserId($user_id)
|
|
{
|
|
$this->user_id = $user_id;
|
|
}
|
|
|
|
//Типы договоров
|
|
public function getTypes()
|
|
{
|
|
$common = array();
|
|
$temp_result = array();
|
|
$result = array();
|
|
$sql = "SELECT * FROM contract_types WHERE agency_id = 0";
|
|
$q = $this->db->query($sql);
|
|
while($r = $this->db->fetch_assoc($q)){
|
|
$common[$r['id']] = $r;
|
|
}
|
|
|
|
$sql = "SELECT * FROM contract_types WHERE agency_id = {$this->agency_id}";
|
|
$q = $this->db->query($sql);
|
|
while($r = $this->db->fetch_assoc($q)){
|
|
if($r['parent_id'] > 0){
|
|
$common[$r['parent_id']] = $r;
|
|
} else {
|
|
$common[$r['id']] = $r;
|
|
}
|
|
}
|
|
|
|
foreach($common as $type){
|
|
if($type['del'] == 0){
|
|
$result[] = array('code'=>$type['id'], 'type'=>$type['type'], 'agency_id'=>$type['agency_id']);
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
//Типы договоров
|
|
public function getTypesName()
|
|
{
|
|
$common = array();
|
|
$temp_result = array();
|
|
$result = array();
|
|
$sql = "SELECT * FROM contract_types WHERE agency_id = 0";
|
|
$q = $this->db->query($sql);
|
|
while($r = $this->db->fetch_assoc($q)){
|
|
$common[$r['id']] = $r;
|
|
}
|
|
|
|
$sql = "SELECT * FROM contract_types WHERE agency_id = {$this->agency_id}";
|
|
$q = $this->db->query($sql);
|
|
while($r = $this->db->fetch_assoc($q)){
|
|
|
|
$common[$r['id']] = $r;
|
|
|
|
}
|
|
return $common;
|
|
}
|
|
|
|
//Редактирование/добавление типа договора
|
|
public function editAddType($type_id, $type){
|
|
|
|
if($type_id == 0){
|
|
$sql_in = "INSERT INTO contract_types SET
|
|
agency_id = {$this->agency_id},
|
|
type = '{$type}',
|
|
created_at = NOW(),
|
|
created_by = {$this->user_id}";
|
|
if(!$this->db->query($sql_in)){
|
|
return array('source'=>'error', 'mes'=>$this->db->errorInfo());
|
|
} else {
|
|
return array('source'=>'done');
|
|
}
|
|
} else {
|
|
$sql = "SELECT * FROM contract_types WHERE id = {$type_id}";
|
|
$q = $this->db->query($sql);
|
|
if(!$q){
|
|
return array('source'=>'error', 'mes'=>$this->db->errorInfo());
|
|
} else {
|
|
$r = $this->db->fetch_assoc($q);
|
|
if($r['agency_id'] > 0 && $r['agency_id'] == $this->agency_id){
|
|
$sql_up = "UPDATE contract_types SET type='{$type}', updated_by={$this->user_id}, updated_at=NOW() WHERE id={$type_id}";
|
|
if(!$this->db->query($sql_up)){
|
|
return array('source'=>'error', 'mes'=>$this->db->errorInfo());
|
|
} else {
|
|
return array('source'=>'done');
|
|
}
|
|
} else if ($r['agency_id'] == 0) { //общий тип
|
|
$sql_in = "INSERT INTO contract_types SET
|
|
agency_id = {$this->agency_id},
|
|
parent_id = {$type_id},
|
|
type = '{$type}',
|
|
created_at = NOW(),
|
|
created_by = {$this->user_id}";
|
|
|
|
if(!$this->db->query($sql_in)){
|
|
return array('source'=>'error', 'mes'=>$this->db->errorInfo());
|
|
} else {
|
|
return array('source'=>'done');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
//Удаление типа договора
|
|
public function deletedType($type_id)
|
|
{
|
|
$sql = "SELECT * FROM contract_types WHERE id = {$type_id}";
|
|
$q = $this->db->query($sql);
|
|
if(!$q){
|
|
return array('source'=>'error', 'mes'=>$this->db->errorInfo());
|
|
} else {
|
|
$r = $this->db->fetch_assoc($q);
|
|
if($r['agency_id'] > 0 && $r['agency_id'] == $this->agency_id){
|
|
$sql_up = "UPDATE contract_types SET del=1, updated_by={$this->user_id}, updated_at=NOW() WHERE id={$type_id}";
|
|
if(!$this->db->query($sql_up)){
|
|
return array('source'=>'error', 'mes'=>$this->db->errorInfo());
|
|
} else {
|
|
return array('source'=>'done');
|
|
}
|
|
} else if ($r['agency_id'] == 0) { //общий тип
|
|
$sql_in = "INSERT INTO contract_types SET
|
|
agency_id = {$this->agency_id},
|
|
type = '{$r['type']}',
|
|
parent_id = {$type_id},
|
|
del = 1,
|
|
created_at = NOW(),
|
|
created_by = {$this->user_id}";
|
|
if(!$this->db->query($sql_in)){
|
|
return array('source'=>'error', 'mes'=>$this->db->errorInfo());
|
|
} else {
|
|
return array('source'=>'done');
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
//Редактирование/добавление договора
|
|
public function editAdd($params, $isSynchronization = false)
|
|
{
|
|
$this->setTable($params['parent_type']);
|
|
$parent_id = $params['parent_id'];
|
|
$id = (int)$params['id'];
|
|
$date_start = strtotime($params['date_start']);
|
|
$date_end = strtotime($params['date_end']);
|
|
|
|
if ($params['parent_type'] == 'req')
|
|
$parent_id = (int)str_replace('req', '', $params['parent_id']);
|
|
|
|
if (!empty($this->table)) {
|
|
|
|
if ($id == 0) {
|
|
|
|
$sql = "INSERT INTO {$this->table} SET " .
|
|
"{$this->parent} = {$parent_id}, " .
|
|
"type = {$params['type']}, " .
|
|
"date_start = {$date_start}, " .
|
|
"date_end = {$date_end}, " .
|
|
"number = '{$params['number']}', " .
|
|
"days = {$params['days_mes_end']}, " .
|
|
"send = {$params['no_mes_end']}, " .
|
|
"agency_id = {$this->agency_id} ";
|
|
|
|
|
|
if (isset($params['contract_req_id']))
|
|
$sql .= ", contract_req_id = {$params['contract_req_id']}";
|
|
|
|
} else {
|
|
$sql = "UPDATE {$this->table} SET " .
|
|
"type = {$params['type']}, " .
|
|
"date_start = {$date_start}, " .
|
|
"date_end = {$date_end}, " .
|
|
"number = '{$params['number']}', " .
|
|
"days = {$params['days_mes_end']}, " .
|
|
"send = {$params['no_mes_end']}, " .
|
|
"agency_id = {$this->agency_id} " .
|
|
"WHERE id = {$id}";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->db->query($sql)){
|
|
return array('source' => 'error', 'sql' => $sql, 'mes' => $this->db->errorInfo());
|
|
} else {
|
|
if($id == 0) $id = $this->db->insert_id();
|
|
if(!$isSynchronization){ //Синхронизация заявка-объектт
|
|
if($params['parent_type'] == 'req'){
|
|
|
|
$req_id = (int)$parent_id;
|
|
|
|
$sqlReq = "SELECT *, (SELECT heir FROM requisitions_type WHERE requisitions.type_id = requisitions_type.id) as heir FROM requisitions WHERE id = {$req_id}";
|
|
|
|
$qReq = $this->db->query($sqlReq);
|
|
$req = $this->db->fetch_assoc($qReq);
|
|
if($req['type_id'] == 2 || $req['heir'] == 2){
|
|
if($req['object_id'] > 0){
|
|
$reqClass = new Requisitions(null, true);
|
|
$reqClass->contractInObject($req_id, $req['object_id']);
|
|
}
|
|
}
|
|
} else if ($params['parent_type'] == 'object'){
|
|
$object_id = (int)$parent_id;
|
|
$sql_req = "SELECT id, funnel_id, stage FROM requisitions WHERE object_id = {$object_id} AND (type_id = 2 or type_id in (SELECT id FROM requisitions_type WHERE heir = 2 AND agency_id = {$this->agency_id})) AND deleted = 0 AND cancel = 0 LIMIT 1";
|
|
|
|
$q_req = $this->db->query($sql_req);
|
|
if($this->db->num_rows($q_req) > 0){
|
|
$req = $this->db->fetch_assoc($q_req);
|
|
$reqClass = new Requisitions(null, true);
|
|
|
|
$reqClass->contractInObject($req['id'], $object_id, $id);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
return array('source' => 'done', 'id' => $id);
|
|
}
|
|
|
|
return array('source' => 'error');
|
|
}
|
|
|
|
// Получение контрактов
|
|
public function getContracts($source_id, $parent_type) {
|
|
|
|
$this->setTable($parent_type);
|
|
$sql = "SELECT * FROM {$this->table} WHERE `{$this->parent}` = '{$source_id}'";
|
|
|
|
$results = [];
|
|
if (!empty($sql)) {
|
|
if ($query = $this->db->query($sql)) {
|
|
while ($row = $this->db->fetch_assoc($query)) {
|
|
$row['date_start'] = date('d.m.Y', $row['date_start']);
|
|
$row['date_end'] = date('d.m.Y', $row['date_end']);
|
|
$results[] = $row;
|
|
}
|
|
} else {
|
|
$results = $this->db->errorInfo();
|
|
}
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
|
|
// Получение контракта по id
|
|
public function getContract($id, $parent_type) {
|
|
|
|
$result = array();
|
|
$this->setTable($parent_type);
|
|
|
|
if ($id > 0 && !empty($this->table)) {
|
|
$sql = "SELECT * FROM {$this->table} WHERE id = {$id}";
|
|
$q = $this->db->query($sql);
|
|
$result = $this->db->fetch_assoc($q);
|
|
$result['date_start'] = date('d.m.Y', $result['date_start']);
|
|
$result['date_end'] = date('d.m.Y', $result['date_end']);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
// Получение контрактов по ref_id
|
|
public function getContractsByRef($ref_id, $parent_type) {
|
|
|
|
$result = array();
|
|
$this->setTable($parent_type);
|
|
|
|
if ($ref_id > 0 && !empty($this->table)) {
|
|
|
|
if ($parent_type == 'client')
|
|
$sql = "SELECT * FROM {$this->table} WHERE client_id = {$ref_id}";
|
|
else if ($parent_type == 'req')
|
|
$sql = "SELECT * FROM {$this->table} WHERE req_id = {$ref_id}";
|
|
else if ($parent_type == 'object')
|
|
$sql = "SELECT * FROM {$this->table} WHERE object_id = {$ref_id}";
|
|
|
|
if (!empty($sql)) {
|
|
$q = $this->db->query($sql);
|
|
while($row = $this->db->fetch_assoc($q)) {
|
|
$row['date_start'] = date('d.m.Y', $row['date_start']);
|
|
$row['date_end'] = date('d.m.Y', $row['date_end']);
|
|
$result[] = $row;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function delete($id, $type){
|
|
$this->setTable($type);
|
|
$sql = "DELETE FROM {$this->table} WHERE id = $id";
|
|
$this->db->query($sql);
|
|
}
|
|
|
|
}
|