var app = new Vue({ el: '#app', data: { subject_select: parseInt('0', 10), duration_select: 0, age_select: parseInt('0', 10), category_select: parseInt('0', 10), cost: parseInt('0', 10), when_exam: 1, subjects: [], ages: [], durations: [], categories: [], subject_age: [], days: [0,1,2,3,4,5] }, computed: { ages_computed() { if (this.subject_select || this.category_select) { let current_ages = this.category_select ? this.subject_age[this.category_select] : this.subject_age[this.subject_select]; let current = this.ages.filter(function(age) { return current_ages.indexOf(age.id) !== -1; }, this); current.unshift({ id: 0, value: 'Выберите возрастную категорию ...' }); return current; } else { return []; } }, cost_select() { if (this.isNumber(this.cost)) { return parseInt(this.cost, 10); } else { return 0; } }, subject_current() { return this.subjects.find(subject => subject.id === this.subject_select); }, age_current() { return this.ages.find(age => age.id === this.age_select); }, category_of_subject() { let current = this.categories.filter(function(category) { return category.parent_id === this.subject_select; }, this); current.unshift({ id: 0, value: 'Выберите категорию предмета ...' }); return current; }, category_current() { return this.categories.find(categories => categories.id === this.category_select); }, duration_current() { return this.durations.find(durations => durations.id === this.duration_select); }, isDisplay() { if (this.isSubjectCostAge) { if (this.requireCategory) { if (this.isCategory) { if (this.requireExam) { if (this.isExam) { return true; } } else { return true; } } } else { return true; } } return false; }, shares() { return this.days.map(function(day) { return this.getShare(day).toFixed(2); }, this); }, amounts() { return this.days.map(function(day) { return Math.round(this.getAmount(day)); }, this); }, isSubjectCostAge() { return this.subject_select > 0 && this.age_select > 0 && this.duration_select > 0 && this.cost_select > 0; }, requireCategory() { let cat = this.categories.find(categories => categories.parent_id === this.subject_select); return (typeof cat === 'object'); }, isCategory() { return this.category_select > 0; }, isAge() { return this.age_select > 0; }, isDuration() { return this.duration_select > 0; }, requireExam() { return this.category_current ? this.category_current.k31 > 1 : false; }, isExam() { return this.when_exam > 0; }, isCredit() { return this.commission > 2000; }, credit_value() { return (this.commission / 2).toFixed(0); }, credit_date() { let d = new Date(); d.setDate(d.getDate() + 21); return (d).toLocaleDateString() }, ratio() { let items = [1]; if (this.isCategory && this.category_current) { items.push(this.category_current.k1) } else { items.push(this.subject_current.k1) } if (this.isDuration && this.duration_current) { items.push(60 / this.duration_current.value) } if (this.isAge && this.age_current) { items.push(this.age_current.k) } if (this.isExam && this.ratio_age) { items.push(this.ratio_age) } return items.reduce(function(a, b) { return a * b; }); }, commission() { let min = 1200; let k4 = 1; let result = (this.cost * this.ratio).toFixed(0); if (this.cost && this.ratio) { if (this.isCategory && this.category_current) { k4 = this.category_current.k4; } else { k4 = this.subject_current.k4; } return (result * k4).toFixed(0) } return 0; }, ratio_age() { if (this.when_exam && this.category_current) { if (this.when_exam >= 18) { return this.category_current.k31; } else if (this.when_exam >= 12) { return this.category_current.k32; } else if (this.when_exam >= 6) { return this.category_current.k33; } else if (this.when_exam >= 5) { return this.category_current.k34; } else if (this.when_exam >= 4) { return this.category_current.k35; } else if (this.when_exam >= 3) { return this.category_current.k36; } else if (this.when_exam >= 2) { return this.category_current.k37; } else if (this.when_exam < 2) { return this.category_current.k38; } } return 1; } }, methods: { getReturn(day) { let OstatokPrep = 0.6 * this.cost * day; let FinRezDoVozvr = this.cost * day - this.commission; let diff = OstatokPrep - FinRezDoVozvr; return (Math.abs(diff) + diff); }, getAmount(day) { if (day === 0) { return this.commission; } if (day === 1) { return 0.9 * this.getReturn(day) / 2; } if (day === 2) { return 0.85 * this.getReturn(day) / 2; } if (day === 3) { return 0.6 * this.getReturn(day) / 2; } if (day === 4) { return this.commission * this.getShare(4) / 100; } return 0; }, getShare(day) { if (day === 0) { return 100; } if (day === 1) { return 100 * this.getAmount(1) / this.commission; } if (day === 2) { return 100 * this.getAmount(2) / this.commission; } if (day === 3) { return 100 * this.getAmount(3) / this.commission; } if (day === 4) { return 0.5 * this.getShare(3); } return 0; }, isNumber(num) { return !isNaN(parseInt(num, 10)); } }, created: function() { axios.get('/refund/subjects-json') .then((response) => { this.subjects = response.data; this.subjects.sort(compare('value')); this.subjects.unshift({ id: 0, value: 'Выберите предмет ...' }); }), axios.get('/refund/ages-json') .then((response) => { this.ages = response.data; this.ages.unshift({ id: 0, value: 'Выберите возрастную категорию ...' }); }), axios.get('/refund/duration-json') .then((response) => { this.durations = response.data; this.durations.unshift({ id: 0, text: 'Выберите продолжительность одного занятия ...' }); }), axios.get('/refund/category-json') .then((response) => { this.categories = response.data; }), axios.get('/refund/subject-age-json') .then((response) => { this.subject_age = response.data.reduce(function (r, a) { r[a.subject_id] = r[a.subject_id] || []; r[a.subject_id].push(a.age_id); return r; }, Object.create(null)); }) }, }) function compare(field, order) { var len = arguments.length; if(len === 0) { return (a, b) => (a < b && -1) || (a > b && 1) || 0; } if(len === 1) { switch(typeof field) { case 'number': return field < 0 ? ((a, b) => (a < b && 1) || (a > b && -1) || 0) : ((a, b) => (a < b && -1) || (a > b && 1) || 0); case 'string': return (a, b) => (a[field] < b[field] && -1) || (a[field] > b[field] && 1) || 0; } } if(len === 2 && typeof order === 'number') { return order < 0 ? ((a, b) => (a[field] < b[field] && 1) || (a[field] > b[field] && -1) || 0) : ((a, b) => (a[field] < b[field] && -1) || (a[field] > b[field] && 1) || 0); } var fields, orders; if(typeof field === 'object') { fields = Object.getOwnPropertyNames(field); orders = fields.map(key => field[key]); len = fields.length; } else { fields = new Array(len); orders = new Array(len); for(let i = len; i--;) { fields[i] = arguments[i]; orders[i] = 1; } } return (a, b) => { for(let i = 0; i < len; i++) { if(a[fields[i]] < b[fields[i]]) return orders[i]; if(a[fields[i]] > b[fields[i]]) return -orders[i]; } return 0; }; }