forked from mohsinalimat/Important-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrm_activities.js
More file actions
401 lines (361 loc) · 10.7 KB
/
crm_activities.js
File metadata and controls
401 lines (361 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
frappe.provide("erpnext");
erpnext.utils.CRMActivities = class extends erpnext.utils.CRMActivities {
constructor(opts) {
super(opts)
$.extend(this, opts);
}
refresh() {
var me = this;
$(this.open_activities_wrapper).empty();
let cur_form_footer = this.form_wrapper.find(".form-footer");
// all activities
if (!$(this.all_activities_wrapper).find(".form-footer").length) {
this.all_activities_wrapper.empty();
$(cur_form_footer).appendTo(this.all_activities_wrapper);
// remove frappe-control class to avoid absolute position for action-btn
$(this.all_activities_wrapper).removeClass("frappe-control");
// hide new event button
$(".timeline-actions").find(".btn-default").hide();
// hide new comment box
$(".comment-box").hide();
// show only communications by default
$($(".timeline-content").find(".nav-link")[0]).tab("show");
}
// open activities
frappe.call({
method: "erpnext.crm.utils.get_open_activities",
args: {
ref_doctype: this.frm.doc.doctype,
ref_docname: this.frm.doc.name,
},
callback: (r) => {
if (!r.exc) {
if (r.message.tasks) {
var tasksHtml = ActivityGenerator.generateTasksHtml(r.message.tasks);
$(tasksHtml).appendTo(cur_frm.fields_dict.open_activities_html.wrapper);
}
if (r.message.events) {
var eventsHtml = ActivityGenerator.generateEventsHtml(r.message.events);
$(eventsHtml).appendTo(cur_frm.fields_dict.open_activities_html.wrapper);
}
me.create_task();
me.create_event();
}
},
});
}
create_task() {
const me = this;
const _create_task = () => {
const args = {
doc: me.frm.doc,
frm: me.frm,
title: __("New Task"),
};
const composer = new frappe.views.InteractionComposer(args);
composer.dialog.get_field("interaction_type").set_value("ToDo");
// hide column having interaction type field
$(composer.dialog.get_field("interaction_type").wrapper).closest(".form-column").hide();
// hide summary field
$(composer.dialog.get_field("summary").wrapper).closest(".form-section").hide();
};
$(".new-task-btn").click(_create_task);
}
create_event() {
let me = this;
let _create_event = async () => {
try {
let reference_doctype = me.frm.doctype;
let reference_docname = me.frm.doc.name;
let new_event = frappe.model.get_new_doc('Event');
let new_participant = frappe.model.add_child(new_event, 'Event Participants', 'event_participants'); // Ensure correct child table DocType and fieldname
new_participant.reference_doctype = reference_doctype;
new_participant.reference_docname = reference_docname;
frappe.set_route('Form', 'Event', new_event.name);
} catch (error) {
frappe.msgprint(__('Failed to create event: ' + error.message));
}
};
$(".new-event-btn").click(_create_event);
}
async update_status(input_field, doctype) {
let completed = $(input_field).prop("checked") ? 1 : 0;
let docname = $(input_field).attr("name");
if (completed) {
await frappe.db.set_value(doctype, docname, "status", "Closed");
this.refresh();
}
}
};
class ActivityGenerator {
static generateTasksHtml(tasks) {
var html = `
<div class="open-activities">
<div class="new-btn pb-3">
<span>
<button class="btn btn-sm small new-task-btn mr-1" style="font-weight:bold;border: 2px solid lightgrey;">
<svg class="icon icon-sm">
<use href="#icon-small-message"></use>
</svg>
New Task
</button>
</span>
</div>`;
if (tasks.length > 0) {
tasks.forEach(task => {
html += `<div class="single-activity card mb-2">`;
html += `
<div class="flex justify-between mb-2">
<div class="label-area font-md ml-1">
<span class="mr-2">
<svg class="icon icon-sm">
<use href="#icon-small-message"></use>
</svg>
</span>
<a href="/app/todo/${task.name}" title="Open Task" class="task-description">${task.description}</a>
</div>
</div>`;
if (task.date) {
html += `<div class="text-muted ml-1">${frappe.datetime.global_date_format(task.date)}</div>`;
}
if (task.allocated_to) {
html += `<div class="text-muted ml-1">Allocated To: ${task.allocated_to}</div>`;
}
html += `</div>`;
});
} else {
html += `<div class="single-activity no-activity text-muted">No open task</div>`;
}
html += `</div>`;
return html;
}
static generateEventsHtml(events) {
var html = `
<div class="open-activities">
<div class="new-btn pb-3">
<span>
<button class="btn btn-sm small new-event-btn mr-1" style="font-weight:bold;border: 2px solid lightgrey;">
<svg class="icon icon-sm">
<use href="#icon-calendar"></use>
</svg>
New Event
</button>
</span>
</div>`;
if (events.length > 0) {
events.forEach(event => {
html += `<div class="single-activity card mb-2">`;
html += `
<div class="flex justify-between mb-2">
<div class="label-area font-md ml-1">
<span class="mr-2">
<svg class="icon icon-sm">
<use href="#icon-small-message"></use>
</svg>
</span>
<a href="/app/event/${event.name}" title="Open Event" class="event-subject">${event.subject}</a>
</div>
</div>`;
if (event.starts_on) {
html += `<div class="text-muted ml-1">${frappe.datetime.global_date_format(event.starts_on)}</div>`;
}
html += `</div>`;
});
} else {
html += `<div class="single-activity no-activity text-muted">No open event</div>`;
}
html += `</div>`;
return html;
}
}
const style = document.createElement('style');
style.textContent = `
.open-activities {
min-height: 50px;
padding-left: 0px;
padding-bottom: 15px !important;
}
.open-activities .new-btn {
text-align: right;
}
.single-activity {
min-height: 90px;
border: 1px solid var(--border-color);
padding: 10px;
border-bottom: 0;
padding-right: 10px; /* Adjust padding to ensure content stays within card */
background: #fff;
margin-bottom: 10px;
box-sizing: border-box; /* Ensure padding and border are included in element's total width and height */
}
.single-activity.card {
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
.single-activity:last-child {
border-bottom: 1px solid var(--border-color);
}
.single-activity:hover .completion-checkbox {
display: block;
}
.completion-checkbox {
vertical-align: middle;
display: none;
}
.open-tasks {
width: 50%;
}
.open-tasks:first-child {
border-right: 0;
}
.open-section-head {
background-color: var(--bg-color);
min-height: 30px;
border-bottom: 1px solid var(--border-color);
padding: 10px;
font-weight: bold;
}
.no-activity {
text-align: center;
padding-top: 30px;
}
.form-footer {
background-color: var(--bg-color);
}
.task-description, .event-subject {
display: inline-block;
max-width: 100%;
overflow: hidden;
text-overflow: ellipsis;
vertical-align: bottom;
}
.label-area {
width: calc(100% - 20px);
box-sizing: border-box;
}
`;
document.head.appendChild(style);
erpnext.utils.CRMNotes = class CRMNotes {
constructor(opts) {
$.extend(this, opts);
}
refresh() {
var me = this;
this.notes_wrapper.find(".notes-section").remove();
let notes = this.frm.doc.notes || [];
notes.sort(function (a, b) {
return new Date(b.added_on) - new Date(a.added_on);
});
let notes_html = frappe.render_template("crm_notes", {
notes: notes,
});
$(notes_html).appendTo(this.notes_wrapper);
this.add_note();
$(".notes-section")
.find(".edit-note-btn")
.on("click", function () {
me.edit_note(this);
});
$(".notes-section")
.find(".delete-note-btn")
.on("click", function () {
me.delete_note(this);
});
}
add_note() {
let me = this;
let _add_note = () => {
var d = new frappe.ui.Dialog({
title: __("Add a Note"),
fields: [
{
label: "Note",
fieldname: "note",
fieldtype: "Text Editor",
reqd: 1,
enable_mentions: true,
},
],
primary_action: function () {
var data = d.get_values();
frappe.call({
method: "add_note",
doc: me.frm.doc,
args: {
note: data.note,
},
freeze: true,
callback: function (r) {
if (!r.exc) {
me.frm.refresh_field("notes");
me.refresh();
}
d.hide();
},
});
},
primary_action_label: __("Add"),
});
d.show();
};
$(".new-note-btn").click(_add_note);
}
edit_note(edit_btn) {
var me = this;
let row = $(edit_btn).closest(".comment-content");
let row_id = row.attr("name");
let row_content = $(row).find(".content").html();
if (row_content) {
var d = new frappe.ui.Dialog({
title: __("Edit Note"),
fields: [
{
label: "Note",
fieldname: "note",
fieldtype: "Text Editor",
default: row_content,
},
],
primary_action: function () {
var data = d.get_values();
frappe.call({
method: "edit_note",
doc: me.frm.doc,
args: {
note: data.note,
row_id: row_id,
},
freeze: true,
callback: function (r) {
if (!r.exc) {
me.frm.refresh_field("notes");
me.refresh();
d.hide();
}
},
});
},
primary_action_label: __("Done"),
});
d.show();
}
}
delete_note(delete_btn) {
var me = this;
let row_id = $(delete_btn).closest(".comment-content").attr("name");
frappe.call({
method: "delete_note",
doc: me.frm.doc,
args: {
row_id: row_id,
},
freeze: true,
callback: function (r) {
if (!r.exc) {
me.frm.refresh_field("notes");
me.refresh();
}
},
});
}
};