459 lines
19 KiB
PHP
459 lines
19 KiB
PHP
<?php
|
||
|
||
class WebAppRaiserOrder {
|
||
private $woid;
|
||
private $order;
|
||
|
||
public static function valid_fields()
|
||
{
|
||
// valid_fields format: 'form data field' => 'DB data field'
|
||
return array(
|
||
'woid'=> 'woid',
|
||
'api_id' => 'api_id',
|
||
//'externalId'=> 'object_id', // внешний ID при запросе информации
|
||
//'orderId'=> 'object_id', // внешний ID при создании заказа
|
||
'object_id'=> 'object_id', // ID объекта при загрузке из базы
|
||
'id'=> 'order_id', // возникает после отправки заказа
|
||
'order_id'=> 'order_id',
|
||
'number'=> 'number', // нет на форме
|
||
'state'=> 'state', // нет на форме
|
||
'serviceType'=> 'serviceType',
|
||
'objectType'=> 'objectType',
|
||
'reportUsageType'=> 'reportUsageType',
|
||
'preliminaryCalculationValue'=> 'preliminaryCalculationValue', // нет на форме
|
||
'address' => 'address',
|
||
'customerName' => 'customerName',
|
||
'customerPhone'=> 'customerPhone', // нет в информации
|
||
'customerEmail' => 'customerEmail', // нет в информации
|
||
'сontactName' => 'сontactName', // ФИО контактного лица
|
||
'сontactPhone' => 'сontactPhone', // Номер телефона контактного лица
|
||
'сontactEmail' => 'сontactEmail', // Email контактного лица
|
||
'bankId' => 'bankId', // нет в информации
|
||
'bankManagerName'=> 'bankManagerName', // нет в информации
|
||
'bankManagerEmail'=> 'bankManagerEmail', // нет в информации
|
||
'managerComment'=> 'managerComment', // нет в информации
|
||
'managerEmail'=> 'managerEmail', // нет в информации
|
||
//'appraisalCompanyId'=> 'appraisalCompanyId',
|
||
'contractCost'=> 'contractCost', // нет в информации
|
||
'calculationId'=> 'calculationId', // нет в информации
|
||
|
||
'requestDate' => 'requestDate', // нет на форме
|
||
'payed' => 'payed', // нет на форме
|
||
'reportPrinted' => 'reportPrinted', // нет на форме
|
||
'reportReady' => 'reportReady',
|
||
'deliveryRequired' => 'deliveryRequired',
|
||
'deliveryAddress' => 'deliveryAddress',
|
||
'documentsChecked' => 'documentsChecked',
|
||
'inspectionRequired' => 'inspectionRequired',
|
||
'inspectionConfirmed' => 'inspectionConfirmed',
|
||
'inspectionDate' => 'inspectionDate',
|
||
'inspectionContactName' => 'inspectionContactName',
|
||
'inspectionContactPhone' => 'inspectionContactPhone',
|
||
'inspected'=> 'inspected',
|
||
'appraisalManagerName'=> 'appraisalManagerName',
|
||
'updateDate'=> 'updateDate',
|
||
);
|
||
}
|
||
|
||
public function __construct(array $fields)
|
||
{
|
||
if(isset($fields["woid"])) {
|
||
$this->woid = $fields["woid"];
|
||
}
|
||
else {
|
||
$this->woid = 0;
|
||
}
|
||
if(isset($fields['object_id'])) {
|
||
$match = WebAppRaiserDB::matchFieldsToSave($fields, self::valid_fields(), 'woid');
|
||
$this->order = $match['fields_to_save'];
|
||
}
|
||
//TODO сделать дополнительные запросы цены и картинок
|
||
}
|
||
|
||
|
||
public static function load($object_id, WebAppRaiserAPI $api)
|
||
{
|
||
$ordersData = array();
|
||
$orders = WebAppRaiserDB::select('war_objects', 'object_id', $object_id);
|
||
//msg('ORDERS', $orders);
|
||
$check_point = time() - (5*60); // не чаще одного запроса в 5 минут
|
||
foreach ($orders as $order) {
|
||
if($order['api_id'] == $api->get('war_id')){
|
||
// Загружаем заказы только если подключено правильное API
|
||
if($order['updateDate'] < $check_point){
|
||
$api->getOrder($order);
|
||
$fields = WebAppRaiserDB::select('war_objects', 'woid', $order['woid']);
|
||
$ordersData[$order['woid']] = new WebAppRaiserOrder($fields[0]);
|
||
}
|
||
else {
|
||
$ordersData[$order['woid']] = new WebAppRaiserOrder($order);
|
||
}
|
||
}
|
||
}
|
||
return $ordersData;
|
||
}
|
||
|
||
public static function create(array $fields)
|
||
{
|
||
//$valid_fields = self::valid_fields();
|
||
//$match = WebAppRaiserDB::matchFieldsToSave($fields, $valid_fields, 'woid');
|
||
$order = new WebAppRaiserOrder($fields);
|
||
return $order;
|
||
}
|
||
|
||
public static function save($fields)
|
||
{
|
||
$valid_fields = self::valid_fields();
|
||
$woid = WebAppRaiserDB::save('war_objects', $fields, $valid_fields, 'woid');
|
||
return $woid;
|
||
}
|
||
|
||
public static function delete($order_id)
|
||
{
|
||
return WebAppRaiserDB::delete('war_objects', 'order_id', $order_id);
|
||
|
||
/*if(isset($this->order['woid'])) {
|
||
return WebAppRaiserDB::delete('war_objects', 'woid', $this->order['woid']);
|
||
}
|
||
elseif(isset($this->order['order_id'])) {
|
||
return WebAppRaiserDB::delete('war_objects', 'order_id', $this->order['order_id']);
|
||
}
|
||
else {
|
||
return false;
|
||
} // */
|
||
}
|
||
|
||
|
||
|
||
// Отображение заказа на главной странице заказов.
|
||
public function render() {
|
||
$order = $this->order;
|
||
$header = '<div style="border-bottom: 2px solid rgb(67, 160, 71); margin: 2em 0;"><h2 style="float:none;">%s</h2></div>';
|
||
$button = function($title, $url, $style = '') {
|
||
return '<a class="fr-btn edit-btn steel i-btn very-small" style="-webkit-box-sizing:initial;'.$style.'" href="'.$url.'">'.$title.'</a>';
|
||
};
|
||
$button2 = function($text, $title, $url, $style = '') {
|
||
return '<div class="jw__action-button">
|
||
<a title="'.$title.'" class="action-button actionJW" style="display:block; '.$style.'" href="'.$url.'">'.$text.'</a></div>'.PHP_EOL;
|
||
};
|
||
$button3 = function($text, $title, $url, $style = '') {
|
||
return '<a title="'.$title.'" class="action-button actionJW" style="display:inline-block; '.$style.'" href="'.$url.'">'.$text.'</a>'.PHP_EOL;
|
||
};
|
||
$view_field = function($field) use($order) {return $this->viewField($field, $order[$field]);};
|
||
$html_field = function($title, $field) use($view_field) {
|
||
$html = ' <div style="float: left;font-weight: bold;padding-right: 10px;width:33%;min-height: 28px;padding-top: 6px;">'.$title.'</div>'.PHP_EOL;
|
||
$html .= ' <div style="margin-left: 33%;min-height: 28px;padding-top: 6px;">'.$view_field($field).'</div>'.PHP_EOL;
|
||
$html .= ' <div style="clear: both;border-bottom:1px dashed;margin-bottom: 8px;"></div>'.PHP_EOL;
|
||
return $html;
|
||
};
|
||
$output = '<div class="jw__object" style="padding: 0 15px;"><div class="row"><div class="col col-md-12">'.sprintf($header, 'Заказ / '.$this->viewField('number', $order['number'].' ['.$order['order_id'].']')).'</div></div>'.PHP_EOL;
|
||
// Сведения о заказе
|
||
$output .= '<div class="row">'.PHP_EOL;
|
||
$output .= ' <div class="col col-md-4">'.PHP_EOL;
|
||
$output .= $html_field('Тип заказа', 'serviceType');
|
||
$output .= $html_field('Объект оценки', 'objectType');
|
||
$output .= $html_field('Цель оценки', 'reportUsageType');
|
||
$output .= $html_field('Адрес объекта', 'address');
|
||
$output .= $html_field('Банк', 'bankId');
|
||
$output .= ' </div>'.PHP_EOL;
|
||
$output .= ' <div class="col col-md-4">'.PHP_EOL;
|
||
$output .= $html_field('Стоимость в договоре', 'contractCost');
|
||
$output .= $html_field('Email менеджера', 'managerEmail');
|
||
$output .= $html_field('Комментарий менеджера', 'managerComment');
|
||
|
||
$output .= ' </div>'.PHP_EOL;
|
||
$output .= ' <div class="col col-md-4" style="border-left: 2px solid rgb(67, 160, 71);">'.PHP_EOL;
|
||
$output .= $html_field('Номер', 'number');
|
||
$output .= $html_field('Дата', 'requestDate');
|
||
$output .= $html_field('Статус', 'state');
|
||
$output .= $html_field('Документы проверены', 'documentsChecked');
|
||
$output .= $html_field('Оплата', 'payed');
|
||
$output .= $html_field('Отчет готов', 'reportReady');
|
||
$output .= $html_field('Отчет распечатан', 'reportPrinted');
|
||
|
||
$output .= ' </div>'.PHP_EOL;
|
||
$output .= ' <div class="col col-md-12">'.PHP_EOL;
|
||
if($order['serviceType'] == 'CALCULATION'){
|
||
$output .= $html_field('Стоимость, полученная в результате расчета', 'preliminaryCalculationValue');
|
||
}
|
||
$output .= $html_field('ФИО менеджера оценочной компании', 'appraisalManagerName');
|
||
$output .= ' </div>'.PHP_EOL;
|
||
$output .= '</div>'.PHP_EOL;
|
||
// Сведения о заказчике / заемщике
|
||
$output .= '<div class="row">'.PHP_EOL;
|
||
$output .= ' <div class="col col-md-6">'.PHP_EOL;
|
||
$output .= sprintf($header, 'Заказчик / заемщик').PHP_EOL;
|
||
$output .= $html_field('ФИО клиента', 'customerName');
|
||
$output .= $html_field('Контактный телефон клиента', 'customerPhone');
|
||
$output .= $html_field('Email клиента', 'customerEmail');
|
||
$output .= ' </div>'.PHP_EOL;
|
||
// Менеджер банка
|
||
$output .= ' <div class="col col-md-6">'.PHP_EOL;
|
||
$output .= sprintf($header, 'Менеджер банка').PHP_EOL;
|
||
$output .= $html_field('ФИО менеджера банка', 'bankManagerName');
|
||
$output .= $html_field('Email менеджера банка', 'bankManagerEmail');
|
||
$output .= ' </div>'.PHP_EOL;
|
||
$output .= '</div>'.PHP_EOL;
|
||
// Осмотр
|
||
$output .= '<div class="row">'.PHP_EOL;
|
||
$output .= ' <div class="col col-md-6">'.PHP_EOL;
|
||
$output .= sprintf($header, 'Осмотр').PHP_EOL;
|
||
$output .= $html_field('Требуется осмотр', 'inspectionRequired');
|
||
$output .= $html_field('Осмотр согласован', 'inspectionConfirmed');
|
||
$output .= $html_field('Дата осмотра', 'inspectionDate');
|
||
$output .= $html_field('ФИО, ответственного за осмотр', 'inspectionContactName');
|
||
$output .= $html_field('Контактный телефон, ответственного за осмотр', 'inspectionContactPhone');
|
||
$output .= $html_field('Осмотр проведен', 'inspected');
|
||
$output .= ' </div>'.PHP_EOL;
|
||
// Доставка
|
||
$output .= ' <div class="col col-md-6">'.PHP_EOL;
|
||
$output .= sprintf($header, 'Доставка').PHP_EOL;
|
||
$output .= $html_field('Требуется доставка', 'deliveryRequired');
|
||
if($order['deliveryRequired']){
|
||
$output .= $html_field('Адрес доставки', 'deliveryAddress');
|
||
$output .= $html_field('Контактное лицо', 'deliveryContactName');
|
||
$output .= $html_field('Номер телефона контакта', 'deliveryContactPhone');
|
||
}
|
||
$output .= ' </div>'.PHP_EOL;
|
||
$output .= '</div>'.PHP_EOL;
|
||
|
||
// Ответственный агент
|
||
$output .= '<div class="row">'.PHP_EOL;
|
||
$output .= ' <div class="col col-md-12">'.PHP_EOL;
|
||
$output .= sprintf($header, 'Ответственный агент').PHP_EOL;
|
||
$output .= $html_field('ФИО контактного лица', 'сontactName');
|
||
$output .= $html_field('Номер телефона контактного лица', 'сontactPhone');
|
||
$output .= $html_field('Email контактного лица', 'сontactEmail');
|
||
$output .= ' </div>'.PHP_EOL;
|
||
$output .= '</div>'.PHP_EOL;
|
||
|
||
// Статус заказа, документы, оплата, осмотр, отчет
|
||
$output .= '<div class="row"><div class="col col-md-12">'.sprintf($header, 'Операции с заказом').'</div><div class="jw__action-button" style="width: 98%;margin: 0 12px 14px;">'.PHP_EOL;
|
||
$output .= $button3('Документы', 'Отправить документы', '/war-object-report.php?op=order_docs&id='.$order['object_id'].'&order_id='.$order['order_id'], 'color: #237ef7 !important; border-color: #3a8efa !important;').' '.PHP_EOL;
|
||
|
||
$output .= $button3('Сообщения', 'Отправить сообщение', '/war-object-report.php?op=msg_list&id='.$order['object_id'].'&order_id='.$order['order_id'], '').' '.PHP_EOL;
|
||
|
||
if($order['reportReady']){
|
||
$output .= $button3('Отчет', 'Скачать отчет', '/war-object-report.php?op=order_report&id='.$order['object_id'].'&order_id='.$order['order_id'], 'color: #4CAF50; border-color: rgb(67, 160, 71);').' '.PHP_EOL;
|
||
}
|
||
|
||
if($order['state'] == 'SENT') { //
|
||
$output .= $button3('Удалить', 'Удалить заказ', '/war-object-report.php?op=order_del&id='.$order['object_id'].'&order_id='.$order['order_id'], 'background: rgba(226, 87, 76, .85); border-color: rgba(148, 54, 41, .37) !important; color: #ffffff !important;float: right !important;').' '.PHP_EOL;
|
||
}
|
||
//$output .= '"'.$order['state'].'"';
|
||
|
||
$output .= '</div></div></div><div style="clear:both;"> </div>'.PHP_EOL;
|
||
|
||
return $output;
|
||
}
|
||
|
||
|
||
public function viewField($field_name, $field_value)
|
||
{
|
||
switch($field_name) {
|
||
case 'state':
|
||
$colors = WebAppRaiserAPI::getDict('ORDER_STATE_COLOR');
|
||
if(empty($field_value)) {
|
||
return '<span style="color:'.$colors['CREATED'][0].'">Не определен</span>';
|
||
}
|
||
else {
|
||
$state_text = WebAppRaiserAPI::getDict('ORDER_STATE', $field_value);
|
||
return '<span style="color:'.$colors[$field_value][0].'">'.$state_text.'</span>';
|
||
}
|
||
break;
|
||
|
||
case 'number':
|
||
if(empty($field_value)) {
|
||
return 'Новый';
|
||
}
|
||
else {
|
||
return $field_value;
|
||
}
|
||
break;
|
||
|
||
case 'type':
|
||
case 'messageType':
|
||
return WebAppRaiserAPI::getDict('MESSAGE_TYPE', $field_value);
|
||
break;
|
||
|
||
case 'serviceType':
|
||
return WebAppRaiserAPI::getDict('APPRAISAL_SERVICE_TYPE', $field_value);
|
||
break;
|
||
|
||
case 'objectType':
|
||
return WebAppRaiserAPI::getDict('OBJECT_TYPE', $field_value);
|
||
break;
|
||
|
||
case 'reportUsageType':
|
||
return WebAppRaiserAPI::getDict('REPORT_USAGE_TYPE', $field_value);
|
||
break;
|
||
|
||
case 'bankId':
|
||
global $WebAppRaiserAPI;
|
||
$banks = $WebAppRaiserAPI->loadBanks();
|
||
if(isset($banks[$field_value])) {
|
||
return $banks[$field_value];
|
||
}
|
||
else {
|
||
return 'Код банка не найден: '.$field_value;
|
||
}
|
||
break;
|
||
|
||
case 'preliminaryCalculationValue':
|
||
case 'contractCost':
|
||
return number_format($field_value, 0, ',', ' ') . ' Руб.';
|
||
break;
|
||
|
||
case 'requestDate':
|
||
case 'inspectionDate':
|
||
case 'updateDate':
|
||
case 'date':
|
||
if(is_numeric($field_value) && $field_value > 0) {
|
||
return date('d.m.Y', $field_value);
|
||
}
|
||
elseif(empty($field_value)){
|
||
return '';
|
||
}
|
||
else {
|
||
return $field_value;
|
||
}
|
||
break;
|
||
|
||
case 'payed':
|
||
case 'reportPrinted':
|
||
case 'reportReady':
|
||
case 'deliveryRequired':
|
||
case 'documentsChecked':
|
||
case 'inspectionRequired':
|
||
case 'inspectionConfirmed':
|
||
case 'inspected':
|
||
if($field_value == 1){
|
||
return '<span style="color:#4CAF50">Да</span>'; // зеленый
|
||
}
|
||
elseif($field_value == 0) {
|
||
return '<span style="color:#e2574cd9">Нет</span>'; // красный
|
||
}
|
||
else {
|
||
return '';
|
||
}
|
||
break;
|
||
|
||
default:
|
||
return $field_value;
|
||
break;
|
||
}
|
||
}
|
||
|
||
public static function updateMessages(WebAppRaiserAPI $api)
|
||
{
|
||
//todo получить список сообщений
|
||
$messages = $api->getMessages();
|
||
if ($messages['status'] == 'success'){
|
||
//msg('messages', $messages);
|
||
foreach($messages['result'] as $item){
|
||
$item_result = $api->getMessage($item['id']);
|
||
//msg('Item Result', $item_result);
|
||
if($item_result['status'] == 'success'){
|
||
//$api->deleteMessage($item['id']);
|
||
}
|
||
}
|
||
}
|
||
//todo понять какие сообщения уже есть в базе данных
|
||
//todo удалить сообщения, которые уже есть
|
||
//todo загрузить сообщения, которых еще нет в базе
|
||
//todo как альтернатива - удалять сообщения которые были сохранены
|
||
}
|
||
|
||
public static function msgTable($object_id, $order_id, $is_chat = true)
|
||
{
|
||
//return 'В разработке.';
|
||
$output = '';
|
||
$records = array();
|
||
$operator = ($is_chat)?'=':'<>';
|
||
$sql = 'SELECT * FROM war_msg WHERE orderId = '.$order_id.' AND type '.$operator.' "CHAT_MESSAGE" ORDER BY date ASC';
|
||
$res = mysql_query($sql);
|
||
while ($row = mysql_fetch_assoc($res)) {
|
||
$records[] = $row;
|
||
}
|
||
if(empty($records)) {
|
||
$output .= '<p style="margin-bottom:2em;">Записей нет.</p>';
|
||
}
|
||
else {
|
||
//msg('RECORDS', $records);
|
||
$output .= '<div class="jw__NB"><div class="jw__NB__flat-list" style="">'.PHP_EOL;
|
||
$output .= ' <table>'.PHP_EOL;
|
||
$output .= ' <tr>'.PHP_EOL;
|
||
$output .= ' <th style="padding:6px;background-color: #F5F5F5;">Дата</th>'.PHP_EOL;
|
||
if($is_chat){
|
||
$output .= ' <th style="padding:6px;background-color: #F5F5F5;">Отправитель</th>'.PHP_EOL;
|
||
$output .= ' <th style="padding:6px;background-color: #F5F5F5;">Имя</th>'.PHP_EOL;
|
||
}
|
||
else {
|
||
$output .= ' <th style="padding:6px;background-color: #F5F5F5;">Тип события</th>'.PHP_EOL;
|
||
}
|
||
$output .= ' <th style="padding:6px;background-color: #F5F5F5;"></th>'.PHP_EOL;
|
||
$output .= ' </tr>'.PHP_EOL;
|
||
foreach($records as $record){
|
||
if($record['type'] == 'REQUEST_STATE_UPDATED'){
|
||
continue;
|
||
}
|
||
$fields = unserialize(base64_decode($record['fields']));
|
||
//msg('fields', [$record['fields'], $fields]);
|
||
$text = '';
|
||
foreach(['comment', 'value', 'text'] as $key){
|
||
if(isset($fields[$key]))
|
||
$text .= ' '.$fields[$key];
|
||
}
|
||
$output .= ' <tr>'.PHP_EOL;
|
||
$output .= ' <td style="padding:6px;">'.date('d.m.Y H:i', $record['date']).'</td>'.PHP_EOL;
|
||
if(isset($fields['sourceType'])){
|
||
$output .= ' <td style="padding:6px;">'.WebAppRaiserAPI::getDict('ACCOUNT_TYPE', $fields['sourceType']).'</td>'.PHP_EOL;
|
||
$output .= ' <td style="padding:6px;">'.$fields['userName'].'</td>'.PHP_EOL;
|
||
}
|
||
else {
|
||
$output .= ' <td style="padding:6px;">'.WebAppRaiserAPI::getDict('MESSAGE_TYPE', $record['type']).'</td>'.PHP_EOL;
|
||
}
|
||
if(mb_strlen($text) > 120){
|
||
$pre_text = mb_substr($text, 0, 120);
|
||
$post_text = mb_substr($text, 0);
|
||
$element_id = 'rec-'.$record['id'];
|
||
$output .= ' <td style="padding:6px;"><div class="'.$element_id.'">'.$pre_text.'... <a style="color:rgb(67, 160, 71);" href="#" onclick="$(\'.'.$element_id.'\').toggle();return false;">подробнее</a></div>';
|
||
$output .= ' <div style="display:none;" class="'.$element_id.'">'. $post_text .'<div><a style="color:rgb(67, 160, 71);" href="#" onclick="$(\'.'.$element_id.'\').toggle();return false;">скрыть</a></div></div>';
|
||
$output .= ' </td>'.PHP_EOL;
|
||
}
|
||
else {
|
||
$output .= ' <td style="padding:6px;">'.$text.'</td>'.PHP_EOL;
|
||
}
|
||
$output .= ' </tr>'.PHP_EOL;
|
||
}
|
||
|
||
$output .= ' </table></div></div>'.PHP_EOL;
|
||
}
|
||
return $output;
|
||
}
|
||
|
||
public static function msgTableStatus($object_id, $order_id){
|
||
$output = '';
|
||
$records = array();
|
||
$sql = 'SELECT * FROM war_msg WHERE orderId = ' . $order_id . ' AND type <> "CHAT_MESSAGE_RECEIVED"';
|
||
$res = mysql_query($sql);
|
||
while ($row = mysql_fetch_assoc($res)) {
|
||
$records[] = $row;
|
||
}
|
||
if(empty($records)) {
|
||
$output .= '<p>Записей нет.</p>';
|
||
}
|
||
}
|
||
|
||
public function get($name){
|
||
if(isset($this->$name)) {
|
||
return $this->$name;
|
||
}
|
||
else {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
}
|
||
|