Maintenance schedule: get no of visits and period validation. Fixes #1591

This commit is contained in:
Nabin Hait 2014-05-06 18:52:36 +05:30
parent 50e962b279
commit ce4a87ce83
2 changed files with 61 additions and 56 deletions

View File

@ -38,6 +38,42 @@ erpnext.support.MaintenanceSchedule = frappe.ui.form.Controller.extend({
});
}
},
start_date: function(doc, cdt, cdn) {
this.set_no_of_visits(doc, cdt, cdn);
},
end_date: function(doc, cdt, cdn) {
this.set_no_of_visits(doc, cdt, cdn);
},
periodicity: function(doc, cdt, cdn) {
this.set_no_of_visits(doc, cdt, cdn);
},
set_no_of_visits: function(doc, cdt, cdn) {
var item = frappe.get_doc(cdt, cdn);
if (item.start_date && item.end_date && item.periodicity) {
if(item.start_date > item.end_date) {
msgprint(__("Row {0}:Start Date must be before End Date", [item.idx]));
return;
}
var date_diff = frappe.datetime.get_diff(item.end_date, item.start_date) + 1;
var days_in_period = {
"Weekly": 7,
"Monthly": 30,
"Quarterly": 91,
"Half Yearly": 182,
"Yearly": 365
}
var no_of_visits = cint(date_diff / days_in_period[item.periodicity]);
frappe.model.set_value(item.doctype, item.name, "no_of_visits", no_of_visits);
}
},
});
$.extend(cur_frm.cscript, new erpnext.support.MaintenanceSchedule({frm: cur_frm}));
@ -62,7 +98,7 @@ cur_frm.fields_dict['contact_person'].get_query = function(doc, cdt, cdn) {
}
}
//
cur_frm.fields_dict['item_maintenance_detail'].grid.get_field('item_code').get_query = function(doc, cdt, cdn) {
return {
filters:{ 'is_service_item': "Yes" }
@ -78,20 +114,6 @@ cur_frm.cscript.item_code = function(doc, cdt, cdn) {
}
}
cur_frm.cscript.periodicity = function(doc, cdt, cdn){
var d = locals[cdt][cdn];
if(d.start_date && d.end_date) {
arg = {}
arg.start_date = d.start_date;
arg.end_date = d.end_date;
arg.periodicity = d.periodicity;
return get_server_fields('get_no_of_visits', docstring(arg),
'item_maintenance_detail', doc, cdt, cdn, 1);
} else {
msgprint(__("Please enter Start Date and End Date"));
}
}
cur_frm.cscript.generate_schedule = function(doc, cdt, cdn) {
if (!doc.__islocal) {
return $c('runserverobj', args={'method':'generate_schedule', 'docs':doc},

View File

@ -133,40 +133,22 @@ class MaintenanceSchedule(TransactionBase):
return schedule_date
def validate_period(self, arg):
args = eval(arg)
if getdate(args['start_date']) >= getdate(args['end_date']):
throw(_("Start date should be less than end date."))
def validate_dates_with_periodicity(self):
for d in self.get("item_maintenance_detail"):
if d.start_date and d.end_date and d.periodicity:
date_diff = (getdate(d.end_date) - getdate(d.start_date)).days + 1
days_in_period = {
"Weekly": 7,
"Monthly": 30,
"Quarterly": 90,
"Half Yearly": 180,
"Yearly": 365
}
period = (getdate(args['end_date']) - getdate(args['start_date'])).days + 1
if (args['periodicity'] == 'Yearly' or args['periodicity'] == 'Half Yearly' or
args['periodicity'] == 'Quarterly') and period < 90:
throw(_("Period is too short"))
elif args['periodicity'] == 'Monthly' and period < 30:
throw(_("Period is too short"))
elif args['periodicity'] == 'Weekly' and period < 7:
throw(_("Period is too short"))
def get_no_of_visits(self, arg):
args = eval(arg)
self.validate_period(arg)
period = (getdate(args['end_date']) - getdate(args['start_date'])).days + 1
count = 0
if args['periodicity'] == 'Weekly':
count = period/7
elif args['periodicity'] == 'Monthly':
count = period/30
elif args['periodicity'] == 'Quarterly':
count = period/91
elif args['periodicity'] == 'Half Yearly':
count = period/182
elif args['periodicity'] == 'Yearly':
count = period/365
ret = {'no_of_visits' : count}
return ret
if date_diff < days_in_period[d.periodicity]:
throw(_("Row {0}: To set {1} periodicity, difference between from and to date \
must be greater than or equal to {2}")
.format(d.idx, d.periodicity, days_in_period[d.periodicity]))
def validate_maintenance_detail(self):
if not self.get('item_maintenance_detail'):
@ -196,6 +178,7 @@ class MaintenanceSchedule(TransactionBase):
def validate(self):
self.validate_maintenance_detail()
self.validate_dates_with_periodicity()
self.validate_sales_order()
def on_update(self):