$(function () {
function dialog(message, yesCallback, notCallback) {
$("#dialog_delete .modal-title").html(message);
$("#dialog_delete").modal('show');
$("#btnYes").click(function (e) {
e.preventDefault();
yesCallback();
$("#dialog_delete").modal('hide');
});
$("#btnNot").click(function (e) {
e.preventDefault();
notCallback();
$("#dialog_delete").modal('hide');
});
}
var form = document.getElementById('message_form');
var url = '/chat/create/';
$("#upload-document-team").bind('fileuploadsubmit', function (e, data) {
data.formData = {
sender: $("#team-chat-form #senderTeamId").val(),
recipent: $("#team-chat-form #recipentTeamId").val(),
order: $("#team-chat-form #orderTeamId").val(),
team: $("#team-chat-form #teamId").val(),
}
console.log(data.formData);
});
//Загрузка документов
$('#upload-document-team').fileupload({
url: url,
crossDomain: false,
beforeSend: function (xhr, settings) {
$('#progress .progress-bar').css(
'width',
'0%'
);
if (!csrfSafeMethod(settings.type)) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
},
dataType: 'json',
done: function (e, data) {
$.each(data.result.files, function (index, file) {
var currentValue = $("#documentSendIds").val();
currentValue += file.id + ';';
$("#documentSendIds").val(currentValue);
var htmlImg = '' + file.name + '';
var document_send = $(htmlImg).appendTo("#document-send");
});
},
fail: function (e) {
console.log(e);
},
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#progress .progress-bar').css(
'width',
progress + '%'
);
}
}).prop('disabled', !$.support.fileInput)
.parent().addClass($.support.fileInput ? undefined : 'disabled');
// Согласование этапов
$("#order-stages").on('click', "#approve-stages", function (e) {
e.preventDefault();
var orderId = $(this).attr('data-order-id');
var senderId = $(this).attr('data-sender-id');
var recipentId = $(this).attr('data-recipent-id');
var caption = "Вы действительно хотите согласовать этапы?";
confirm(caption, function () {
$(".stage-block-approve").each(function () {
var stageId = $(this).attr('data-id');
$.ajax({
url: '/api/stages/' + stageId + '/',
type: 'PATCH',
beforeSend: function (xhr) {
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'))
},
//data: "status=in_process",
data: "status=send_approve",
dataType: 'json',
done: function (json) {
console.log(json);
},
fail: function (e) {
console.log('error');
console.log(e);
}
});
});
$('.btns-approve-stages').hide();
socket.send_stages_approve({
"format_type": "approve_stages",
"data": {
"sender_id": senderId,
"recipent_id": recipentId,
"order_id": orderId,
"msg": "Исполнитель согласовал этапы для заказа " + orderId,
}
});
}.bind(orderId, senderId, recipentId), caption);
});
// Отказаться от этапов
$("#order-stages").on('click', "#cancel-stages", function (e) {
e.preventDefault();
var senderId = $(this).attr('data-sender-id');
var recipentId = $(this).attr('data-recipent-id');
var orderId = $(this).attr('data-order-id');
var caption = "Вы действительно хотите отказаться от этапов?";
confirm(caption,function() {
$(".stage-block-approve").each(function () {
var stageId = $(this).attr('data-id');
$.ajax({
url: '/api/stages/' + stageId + '/',
type: 'PATCH',
beforeSend: function (xhr) {
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'))
},
//data: "status=cancel_approve",
data: "status=send_approve",
dataType: 'json',
done: function (json) {
console.log(json);
},
fail: function (e) {
console.log('error');
console.log(e);
}
});
});
$('.btns-approve-stages').hide();
socket.send_stages_approve({
"format_type": "approve_stages",
"data": {
"sender_id": senderId,
"recipent_id": recipentId,
"order_id": orderId,
"msg": "Исполнитель отказался от текущих этапов " + orderId,
}
});
}.bind(),caption);
});
$(".team-chat-user").on('click', function (e) {
e.stopPropagation();
var recipentId = $(this).attr('data-id');
$("#team-chat-form #recipentTeamId").val(recipentId);
});
$(".team-order-block").on('click', function () {
$('.team-order-block, .team-block').each(function () {
$(this).removeClass('orAct');
});
$(this).addClass('orAct');
var teamIds = '';
$.each($(this).find('.team-chat-user'), function (i, v) {
teamIds += $(this).attr('data-id') + ";";
});
$("#team-chat-form #teamIds").val(teamIds);
var teamId = $(this).attr('data-team-id');
var orderId = $(this).attr('data-order-id');
location.hash = '#teamorder' + orderId;
$("#team-chat-form #teamId").val(teamId);
$("#team-chat-form #recipentTeamId").val("");
$("#team-chat-form #orderTeamId").val(orderId);
$("#add-form-team-note #teamNote").val(teamId);
$("#add-form-team-note #orderNote").val(orderId);
var inbox = document.getElementById('message-chat-team-space');
inbox.innerHTML = '';
var docList = document.getElementById('documentTeamSpace');
docList.innerHTML = '';
$.ajax({
url: '/api/message',
type: 'GET',
data: {csrfmiddlewaretoken: csrftoken, 'team': teamId, 'order': orderId},
dataType: 'json',
success: function (json) {
$.each(json.results, function (i, v) {
var senderName = 'Вы';
var className = 'youChat';
if (v.sender.id !== userId) {
senderName = v.sender.username;
className = '';
}
inbox.innerHTML += '
' +
'
' + senderName + '
' + v.created + '' +
'
' + v.text + '
';
});
var height = inbox.scrollHeight;
inbox.scrollTop = height;
}
});
$.ajax({
url: '/api/documents',
type: 'GET',
data: {
csrfmiddlewaretoken: csrftoken,
'order': orderId,
'team': teamId,
'is_delete': false,
'is_send': true,
},
dataType: 'json',
success: function (json) {
$.each(json.results, function (i, v) {
docList.innerHTML += '' + v.file + '';
});
},
error: function (e) {
console.log(e);
}
});
$.ajax({
url: '/api/note/',
type: 'GET',
data: {csrfmiddlewaretoken: csrftoken, 'order': orderId, 'team': teamId},
dataType: 'json',
success: function (json) {
console.log(json.results);
var noteHtmlInbox = '';
$.each(json.results, function (i, v) {
noteHtmlInbox += '' + v.text + '';
});
$(".team-notes-block").html(noteHtmlInbox);
}
});
});
$('.team-block').on('click', function () {
$('.team-order-block, .team-block').each(function () {
$(this).removeClass('orAct');
});
$(this).addClass('orAct');
var teamIds = '';
$.each($(this).find('.team-chat-user'), function (i, v) {
teamIds += $(this).attr('data-id') + ";";
});
$("#team-chat-form #teamIds").val(teamIds);
var inbox = document.getElementById('message-chat-team-space');
inbox.innerHTML = '';
var docList = document.getElementById('documentTeamSpace');
docList.innerHTML = '';
var teamId = $(this).attr('data-team-id');
location.hash = '#myteam' + teamId;
$("#team-chat-form #teamId").val(teamId);
$("#add-form-team-note #teamNote").val(teamId);
$("#team-chat-form #recipentTeamId").val("");
$("#team-chat-form #orderTeamId").val("");
$("#add-form-team-note #orderNote").val("");
$.ajax({
url: '/api/message',
type: 'GET',
data: {csrfmiddlewaretoken: csrftoken, 'team': teamId, 'order__isnull': 'true'},
dataType: 'json',
success: function (json) {
$.each(json.results, function (i, v) {
var senderName = 'Вы';
var className = 'youChat';
if (v.sender.id !== userId) {
senderName = v.sender.username;
className = '';
}
inbox.innerHTML += '' +
'
' + senderName + '
' + v.created + '' +
'
' + v.text + '
';
});
var height = inbox.scrollHeight;
inbox.scrollTop = height;
}
});
$.ajax({
url: '/api/documents',
type: 'GET',
data: {
csrfmiddlewaretoken: csrftoken,
'team': teamId,
'is_delete': false,
'is_send': true,
},
dataType: 'json',
success: function (json) {
$.each(json.results, function (i, v) {
docList.innerHTML += '' + v.file + '';
});
},
error: function (e) {
console.log(e);
}
});
$.ajax({
url: '/api/note/',
type: 'GET',
data: {csrfmiddlewaretoken: csrftoken, 'team': teamId},
dataType: 'json',
success: function (json) {
console.log(json.results);
var noteHtmlInbox = '';
$.each(json.results, function (i, v) {
noteHtmlInbox += '' + v.text + '';
});
$(".team-notes-block").html(noteHtmlInbox);
}
});
});
// Вытащить сообщения для чата заказа
$('.order-block').on('click', function () {
$('.order-block').each(function () {
$(this).removeClass('orAct');
});
$(this).addClass('orAct');
var orderId = $(this).attr('data-id');
location.hash = '#order' + orderId;
var recipentId = $(this).attr('data-recipent-id');
var projectId = $(this).attr('data-project-id');
$("#chat-order-add #orderId").val(orderId);
$("#add-form-order-note #orderNote").val(orderId);
$("#orderArbitrationId").val(orderId);
$("#projectReviewId").val(projectId);
$("#chat-order-add #recipentId").val(recipentId);
$("#targetCustomerId").val(recipentId);
$("#add-form-order-note #recipentNote").val(recipentId);
var docList = document.getElementById('documentOrderSpace');
var inbox = document.getElementById('message-chat-order-space');
inbox.innerHTML = '';
docList.innerHTML = '';
$.ajax({
url: '/api/message',
type: 'GET',
data: {csrfmiddlewaretoken: csrftoken, 'order': orderId, 'team__isnull': 'true'},
dataType: 'json',
success: function (json) {
$.each(json.results, function (i, v) {
var senderName = 'Вы';
var className = 'youChat';
if (v.sender.id !== userId) {
senderName = v.sender.username;
className = '';
}
inbox.innerHTML += '' +
'
' + senderName + '
' + v.created + '' +
'
' + v.text + '
';
});
var height = inbox.scrollHeight;
inbox.scrollTop = height;
}
});
$.ajax({
url: '/api/documents',
type: 'GET',
data: {
csrfmiddlewaretoken: csrftoken,
'order': orderId,
'is_delete': false,
'is_send': true,
},
dataType: 'json',
success: function (json) {
$.each(json.results, function (i, v) {
docList.innerHTML += '' + v.file + '';
});
},
error: function (e) {
console.log(e);
}
});
$.ajax({
url: '/api/note/',
type: 'GET',
data: {csrfmiddlewaretoken: csrftoken, 'order': orderId},
dataType: 'json',
success: function (json) {
console.log(json.results);
var noteHtmlInbox = '';
$.each(json.results, function (i, v) {
noteHtmlInbox += '' + v.text + '';
});
$(".order-notes-block").html(noteHtmlInbox);
}
});
$.ajax({
url: '/api/orders/' + orderId + '/',
type: 'GET',
data: {csrfmiddlewaretoken: csrftoken},
dataType: 'json',
}).then(function (data) {
var htmlInbox = "";
var stagesReservedHtml = "";
var stagesPaidProcess = [];
var stagesInWork = [];
var stagesResults = data.stages;
var statusNotAgreed = false;
var statusSendApprove = false;
var stagesCompleted = [];
if (stagesResults.length > 0) {
$.each(stagesResults, function (i, v) {
if (v.status == "completed") {
stagesCompleted.push(v);
}
if (v.status == "not_agreed" || v.status == "send_approve") {
statusNotAgreed = true;
}
if (!data.secure) {
if (v.status == "in_process") {
stagesInWork.push(v);
}
} else if ((v.status == "in_process") && (v.is_paid)) {
stagesPaidProcess.push(v);
stagesInWork.push(v);
}
if (data.secure) {
if (v.is_paid) {
stagesReservedHtml += 'Сумма за этап ' + v.pos + '.Зарезервирована.';
} else {
stagesReservedHtml += 'Сумма за этап ' + v.pos + '.Не зарезервирована.';
}
}
var statusName = '';
switch (v.status) {
case 'not_agreed':
statusName = 'Не согласован';
break;
case 'send_approve':
statusName = 'На согласовании';
break;
case 'cancel_approve':
statusName = 'Исполнитель отказался';
break;
case 'in_process':
statusName = 'В процессе';
break;
case 'completed':
statusName = 'Завершен';
break;
}
if (v.status == 'completed') {
statusName = 'Завершен';
}
htmlInbox += '' +
'
Этап ' + v.pos + '' + v.name + '
' +
'
Результаты этапа:' + v.result + '
' +
'
Срок до ' + v.term + '
' + v.cost + ' ' +
'
Cрок заказа рассчитывается с момента резервирования средств
' +
'
';
});
if (statusNotAgreed) {
htmlInbox += '';
}
}
$("#order-stages").html(htmlInbox);
$(".stages-paid").html(stagesReservedHtml);
if (data.secure) {
$("#reserveSpace").show();
} else {
$("#reserveSpace").hide();
}
if (stagesInWork.length > 0) {
$("#completeWork").show();
var stage = stagesInWork[0];
var stageWork = '' +
'
В работе ' + stage.name + '
' +
'
Результат этапа : ' + stage.result + '
' +
'
Срок сдачи ' + stage.term + ' ' + stage.cost + '
';
if (!stage.close_contractor) {
stageWork += '';
} else {
stageWork += 'Этап ожидает завершения статуса от заказчика
';
}
if (data.secure) {
stageWork += '
';
}
$("#stagesWork").html(stageWork);
} else {
$("#completeWork").hide();
}
if (stagesCompleted.length == stagesResults.length && stagesCompleted.length > 0) {
$("#leaveReview").show();
}
if (data.status == 'completed') {
$("#leaveReview").hide();
}
});
});
$('#tab2').on('click', '.closeStage', function (e) {
e.preventDefault();
var stageId = $(this).attr('data-stage-id');
var _this = $(this);
$.ajax({
url: '/api/stages/' + stageId + '/',
type: 'PATCH',
beforeSend: function (xhr) {
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'))
},
data: "close_contractor=True",
dataType: 'json',
success: function (json) {
socket.send_stages_approve({
"format_type": "approve_stages",
"data": {
"sender_id": _this.attr('data-sender-id'),
"recipent_id": _this.attr('data-recipent-id'),
"order_id": _this.attr('data-order-id'),
"msg": "Исполнитель завершил этап " + json.name,
}
});
console.log(json);
},
error: function (e) {
console.log('error');
console.log(e);
}
});
});
//Добавить сообщение для исполнителей в группе
$("#add-team-chat-message").on('click', function (e) {
e.preventDefault();
var chatMessage = $("#team-chat-form #chatText").val();
var recipentId = $("#team-chat-form #recipentTeamId").val();
var senderId = $("#team-chat-form #senderTeamId").val();
var teamId = $("#team-chat-form #teamId").val();
var orderId = $("#team-chat-form #orderTeamId").val();
var documentSendIds = $("#documentSendIds").val();
var teamIds = $("#team-chat-form #teamIds").val();
if (chatMessage) {
var sendLinks = $("#document-send a");
var sendLinkIds = "";
var documentLinks = "";
var documentAttachFiles = "";
$.each(sendLinks, function (i, v) {
sendLinkIds += $(this).attr('data-id') + ';';
documentLinks += 'Входящий файл:
' + $(this).text() + '';
documentAttachFiles += '
' +
'' + $(this).text() + '' +
'';
});
socket.send_message({
"format_type": "add_message_team",
"data": {
"sender_id": senderId,
"recipent_id": recipentId,
"chat_message": chatMessage,
"team_id": teamId,
"team_ids": teamIds,
"order_id": orderId,
"document_send_links": sendLinkIds,
"document_data": {
"document_links": documentLinks,
"document_attach_files": documentAttachFiles,
}
}
});
$("#team-chat-form #chatText").val("");
$("#document-send").html("");
$("#documentSendIds").val("");
}
});
});