Auto Repeat Schedule HTML in Auto Repeat doctype (#5574)

* Auto Repeat Schedule HTML in Auto Repeat doctype

* [Minor changes] end_date and cur_frm

* [Auto repeat schedule] codacy fixes

* codacy fixes & latest revision in js

* Fixes codacy

* [Minor changes]

* Updated onload method & start_date condition

* changed .html source to doctype folder

* removed template reference from build.json

* fixes codacy
This commit is contained in:
Siddhi Sawant 2018-06-05 10:28:09 +05:30 committed by Rushabh Mehta
parent 1239a38e0e
commit 70a6c231cc
5 changed files with 155 additions and 19 deletions

View file

@ -1,5 +1,6 @@
// Copyright (c) 2018, Frappe Technologies and contributors
// For license information, please see license.txt
frappe.provide("frappe.auto_repeat");
frappe.ui.form.on('Auto Repeat', {
setup: function(frm) {
@ -28,7 +29,9 @@ frappe.ui.form.on('Auto Repeat', {
},
refresh: function(frm) {
if(frm.doc.docstatus == 1) {
let label = __('View {0}', [frm.doc.reference_doctype]);
frm.add_custom_button(__(label),
function() {
@ -54,7 +57,12 @@ frappe.ui.form.on('Auto Repeat', {
}
);
}
if(frm.doc.status!= 0){
frappe.auto_repeat.render_schedule(frm);
}
}
},
stop_resume_auto_repeat: function(frm, status) {
@ -72,4 +80,16 @@ frappe.ui.form.on('Auto Repeat', {
}
});
}
});
});
frappe.auto_repeat.render_schedule = function(frm) {
frappe.call({
method: "get_auto_repeat_schedule",
doc: frm.doc
}).done((r) => {
var wrapper = $(frm.fields_dict["auto_repeat_schedule"].wrapper);
wrapper.html(frappe.render_template ("auto_repeat_schedule", {"schedule_details" : r.message || []} ));
});
frm.refresh_fields() ;
};

View file

@ -175,6 +175,7 @@
"bold": 0,
"collapsible": 0,
"columns": 0,
"default": "Today",
"fieldname": "start_date",
"fieldtype": "Date",
"hidden": 0,
@ -361,7 +362,7 @@
"bold": 0,
"collapsible": 0,
"columns": 0,
"fieldname": "column_break_13",
"fieldname": "column_break_12",
"fieldtype": "Column Break",
"hidden": 0,
"ignore_user_permissions": 0,
@ -448,6 +449,69 @@
"translatable": 0,
"unique": 0
},
{
"allow_bulk_edit": 0,
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"columns": 0,
"fieldname": "section_break_13",
"fieldtype": "Section Break",
"hidden": 0,
"ignore_user_permissions": 0,
"ignore_xss_filter": 0,
"in_filter": 0,
"in_global_search": 0,
"in_list_view": 0,
"in_standard_filter": 0,
"label": "Auto Repeat Schedule",
"length": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"print_hide_if_no_value": 0,
"read_only": 0,
"remember_last_selected_value": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"translatable": 0,
"unique": 0
},
{
"allow_bulk_edit": 0,
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"columns": 0,
"depends_on": "eval: !cur_frm.doc.__islocal",
"fieldname": "auto_repeat_schedule",
"fieldtype": "HTML",
"hidden": 0,
"ignore_user_permissions": 0,
"ignore_xss_filter": 0,
"in_filter": 0,
"in_global_search": 0,
"in_list_view": 0,
"in_standard_filter": 0,
"label": "Auto Repeat Schedule",
"length": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"print_hide_if_no_value": 0,
"read_only": 0,
"remember_last_selected_value": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"translatable": 0,
"unique": 0
},
{
"allow_bulk_edit": 0,
"allow_on_submit": 0,
@ -807,7 +871,7 @@
"issingle": 0,
"istable": 0,
"max_attachments": 0,
"modified": "2018-04-30 18:47:19.458729",
"modified": "2018-05-30 17:47:41.965193",
"modified_by": "Administrator",
"module": "Desk",
"name": "Auto Repeat",
@ -816,6 +880,7 @@
"permissions": [
{
"amend": 0,
"apply_user_permissions": 0,
"cancel": 1,
"create": 1,
"delete": 1,
@ -835,6 +900,7 @@
},
{
"amend": 0,
"apply_user_permissions": 0,
"cancel": 1,
"create": 1,
"delete": 1,
@ -854,6 +920,7 @@
},
{
"amend": 0,
"apply_user_permissions": 0,
"cancel": 1,
"create": 1,
"delete": 1,

View file

@ -17,6 +17,9 @@ month_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6, 'Yearly': 12}
class AutoRepeat(Document):
def onload(self):
self.set_onload("auto_repeat_schedule", self.get_auto_repeat_schedule())
def validate(self):
self.update_status()
self.validate_reference_doctype()
@ -97,6 +100,24 @@ class AutoRepeat(Document):
if status and status != 'Resumed':
self.status = status
def get_auto_repeat_schedule(self):
schedule_details = []
start_date_copy = getdate(self.start_date)
end_date_copy = getdate(self.end_date)
today_copy = frappe.utils.datetime.date.today()
if start_date_copy < today_copy:
start_date_copy = today_copy
if not self.end_date:
end_date_copy = add_days(today_copy, 365)
while (getdate(start_date_copy) < getdate(end_date_copy)):
start_date_copy = get_next_schedule_date(start_date_copy, self.frequency, self.repeat_on_day)
row = {
"reference_document" : self.reference_document,
"frequency" : self.frequency,
"next_scheduled_date" : start_date_copy
}
schedule_details.append(row)
return schedule_details
def get_next_schedule_date(start_date, frequency, repeat_on_day):
mcount = month_map.get(frequency)
@ -107,7 +128,6 @@ def get_next_schedule_date(start_date, frequency, repeat_on_day):
next_date = add_days(start_date, days)
return next_date
def make_auto_repeat_entry(date=None):
date = date or today()
for data in get_auto_repeat_entries(date):
@ -120,7 +140,6 @@ def make_auto_repeat_entry(date=None):
if schedule_date and not frappe.db.get_value('Auto Repeat', data.name, 'disabled'):
frappe.db.set_value('Auto Repeat', data.name, 'next_schedule_date', schedule_date)
def get_auto_repeat_entries(date):
return frappe.db.sql(""" select * from `tabAuto Repeat`
where docstatus = 1 and next_schedule_date <=%s
@ -128,7 +147,6 @@ def get_auto_repeat_entries(date):
and next_schedule_date <= ifnull(end_date, '2199-12-31')
and ifnull(disabled, 0) = 0 and status != 'Stopped' """, (date), as_dict=1)
def create_documents(data, schedule_date):
try:
doc = make_new_document(data, schedule_date)
@ -146,12 +164,10 @@ def create_documents(data, schedule_date):
if data.reference_document and not frappe.flags.in_test:
notify_error_to_user(data)
def disable_auto_repeat(data):
auto_repeat = frappe.get_doc('Auto Repeat', data.name)
auto_repeat.db_set('disabled', 1)
def notify_error_to_user(data):
party = ''
party_type = ''
@ -166,7 +182,6 @@ def notify_error_to_user(data):
notify_errors(data.reference_document, data.reference_doctype, party, data.owner, data.name)
def make_new_document(args, schedule_date):
doc = frappe.get_doc(args.reference_doctype, args.reference_document)
new_doc = frappe.copy_doc(doc, ignore_no_copy=False)
@ -178,7 +193,6 @@ def make_new_document(args, schedule_date):
return new_doc
def update_doc(new_document, reference_doc, args, schedule_date):
new_document.docstatus = 0
if new_document.meta.get_field('set_posting_time'):
@ -208,7 +222,6 @@ def update_doc(new_document, reference_doc, args, schedule_date):
new_document.run_method("on_recurring", reference_doc=reference_doc, auto_repeat_doc=args)
def set_auto_repeat_period(args, mcount, new_document):
if mcount and new_document.meta.get_field('from_date') and new_document.meta.get_field('to_date'):
last_ref_doc = frappe.db.sql("""
@ -233,14 +246,12 @@ def set_auto_repeat_period(args, mcount, new_document):
new_document.set('from_date', from_date)
new_document.set('to_date', to_date)
def get_next_date(dt, mcount, day=None):
dt = getdate(dt)
dt += relativedelta(months=mcount, day=day)
return dt
def send_notification(new_rv, auto_repeat_doc, print_format='Standard'):
"""Notify concerned persons about recurring document generation"""
print_format = print_format
@ -263,7 +274,6 @@ def send_notification(new_rv, auto_repeat_doc, print_format='Standard'):
frappe.sendmail(auto_repeat_doc.recipients,
subject=subject, message=message, attachments=attachments)
def notify_errors(doc, doctype, party, owner, name):
recipients = get_system_managers(only_name=True)
frappe.sendmail(recipients + [frappe.db.get_value("User", owner, "email")],
@ -277,7 +287,6 @@ def notify_errors(doc, doctype, party, owner, name):
assign_task_to_owner(name, "Recurring Documents Failed", recipients)
def assign_task_to_owner(name, msg, users):
for d in users:
args = {
@ -289,7 +298,6 @@ def assign_task_to_owner(name, msg, users):
}
assign_to.add(args)
@frappe.whitelist()
def make_auto_repeat(doctype, docname):
doc = frappe.new_doc('Auto Repeat')
@ -300,7 +308,6 @@ def make_auto_repeat(doctype, docname):
doc.start_date = reference_doc.get('posting_date') or reference_doc.get('transaction_date')
return doc
@frappe.whitelist()
def stop_resume_auto_repeat(auto_repeat, status):
doc = frappe.get_doc('Auto Repeat', auto_repeat)
@ -314,7 +321,6 @@ def stop_resume_auto_repeat(auto_repeat, status):
return doc.status
def auto_repeat_doctype_query(doctype, txt, searchfield, start, page_len, filters):
return frappe.db.sql("""select parent from `tabDocField`
where fieldname = 'auto_repeat'
@ -329,4 +335,4 @@ def auto_repeat_doctype_query(doctype, txt, searchfield, start, page_len, filter
'_txt': txt.replace("%", ""),
'start': start,
'page_len': page_len
})
})

View file

@ -0,0 +1,20 @@
<table class="table table-bordered small">
<thead>
<tr>
<th style="width: 20%">{{ __("Reference Document") }}</th>
<th style="width: 20%">{{ __("Frequency") }}</th>
<th style="width: 20%">{{ __("Next Scheduled Date") }}</th>
</tr>
</thead>
<tbody>
{% for(var i=0; i < schedule_details.length; i++) { %}
<tr>
<td>{{ schedule_details[i].reference_document }}</td>
<td> {{ schedule_details[i].frequency }} </td>
<td> {{ schedule_details[i].next_scheduled_date }} </td>
</tr>
{% } %}
</tbody>
</table>

View file

@ -0,0 +1,23 @@
/* eslint-disable */
// rename this file from _test_[name] to test_[name] to activate
// and remove above this line
QUnit.test("test: Auto Repeat", function (assert) {
let done = assert.async();
// number of asserts
assert.expect(1);
frappe.run_serially([
// insert a new Auto Repeat
() => frappe.tests.make('Auto Repeat', [
// values to be set
{key: 'value'}
]),
() => {
assert.equal(cur_frm.doc.key, 'value');
},
() => done()
]);
});