Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, OnInit, Injector } from '@angular/core';
- import { DoctorService} from "../service/doctor.service";
- import { CommonSpecialtiesService } from "../service/common-specialties.service";
- import { Router } from '@angular/router';
- import { FormGroup, FormControl, FormArray, FormBuilder, Validators } from '@angular/forms';
- import { NotificationsService} from "angular2-notifications";
- import {Subject} from 'rxjs/Subject';
- import {debounceTime} from 'rxjs/operator/debounceTime';
- import 'rxjs/add/observable/of';
- import { Observable } from 'rxjs/Observable';
- import { Doctor } from '../model/doctor';
- import * as $ from 'jquery';
- @Component({
- selector: 'app-widget-doctor-quota',
- templateUrl: './widget-doctor-quota.component.html',
- styleUrls: ['./widget-doctor-quota.component.css']
- })
- export class WidgetDoctorQuotaComponent implements OnInit {
- public doctorNameLists: Observable<Doctor[]>;
- public chooseDoctor: Doctor[] = [];
- quotaList: string;
- private doctorId: number;
- private doctorUid: string;
- public query: any;
- public hospitalId:string = localStorage.getItem('hospital_id');
- private listDoctorName: string[] = [];
- private listDoctorId: number[] = [];
- public postQuotaForm: FormGroup;
- private doctors: string;
- // For alert message
- successMessage: string;
- errorMessage: string;
- private _success = new Subject<string>();
- private _error = new Subject<string>();
- private specialtiesOption: string;
- constructor(
- private _service: NotificationsService,
- private router: Router,
- private doctorService: DoctorService,
- private commonSpecialtiesService: CommonSpecialtiesService,
- private injector: Injector,
- private _fb: FormBuilder
- ) {
- // Load doctor list
- this.doctorService.getDoctorList(this.hospitalId).subscribe(
- data => {
- console.log(data)
- if(data == null) {
- console.log("Null");
- }
- else {
- for (var i=0; i<data.length; i++) {
- this.listDoctorName.push(data[i]['doctor_name']);
- this.listDoctorId.push(data[i]['doctor_id']);
- }
- this.doctors = data;
- }
- }
- );
- // Load doctor specialty
- this.commonSpecialtiesService.getSpecialties().subscribe(
- data => {
- if(data == null) {
- console.log("Null");
- }
- else {
- this.specialtiesOption = data;
- }
- }
- );
- // Load doctor quota
- this.doctorService.getDoctorQuotaList(this.hospitalId).subscribe(
- data => {
- if(data == null) {
- console.log("Null");
- }
- else {
- this.quotaList = data;
- console.log(data);
- }
- }
- );
- }
- ngOnInit() {
- this.postQuotaForm = this._fb.group({
- search_doctor_query: ['', []],
- quota_doctor_id: ['', [Validators.required]],
- quota_per_hour: ['', [Validators.required]],
- quota_reservation: ['', [Validators.required]],
- quota_walkin: ['', []],
- quota_walkin_ui: [{value:'',disabled:true}, []],
- // newQuota: this._fb.array([
- // this.initNewQuota(),
- // ])
- });
- this.doctorNameLists = this.doctorService.getDoctorList(this.hospitalId);
- this.doctorNameLists.subscribe (
- data => {
- if (data == null) {
- console.log('Null');
- } else {
- this.chooseDoctor = data;
- }
- },
- error => this.errorMessage = <any>error
- );
- /* For alert */
- this._success.subscribe((message) => this.successMessage = message);
- debounceTime.call(this._success, 500).subscribe(() => this.successMessage = null);
- this._error.subscribe((message) => this.errorMessage = message);
- debounceTime.call(this._error, 2000).subscribe(() => this.errorMessage = null);
- /* For alert */
- }
- refreshDoctorQuotaList() {
- // Load doctor quota
- this.doctorService.getDoctorQuotaList(this.hospitalId).subscribe(
- data => {
- if(data == null) {
- console.log("Null");
- }
- else {
- this.quotaList = data;
- console.log(data);
- }
- }
- );
- }
- // For get form data on add quota
- // public postQuotaForm = new FormGroup({
- // search_doctor_query: new FormControl("search_doctor_query", Validators.required),
- // quota_doctor_id: new FormControl("quota_doctor_id", Validators.required),
- // quota_per_hour: new FormControl("quota_per_hour", Validators.required),
- // quota_reservation: new FormControl("quota_reservation", Validators.required),
- // quota_walkin: new FormControl("quota_walkin", Validators.required),
- // });
- initNewQuota() {
- return this._fb.group({
- quota_doctor_id: ['', []],
- quota_per_hour: ['', []],
- quota_reservation: ['', []],
- quota_walkin: ['', []]
- });
- }
- addNewQuota() {
- const control = <FormArray>this.postQuotaForm.controls['newQuota'];
- control.push(this.initNewQuota());
- }
- validateQuotaPost(qPH, qR, qW, qWui, active_field) {
- let qph:number = qPH.value;
- let qr:number = qR.value;
- let qw:number = qW.value;
- let balance:number = 0;
- if (active_field == 'qph') {
- qW.value = +qph - +qr;
- qWui.value = +qph - +qr;
- if (qph == null) {
- qPH.value = 0;
- }
- let total:number;
- total = +qr + +qw;
- if (qph < total && qr) {
- this.successMessage = '';
- this._error.next(`Quota per hour is less than total quota reservation and walk in.`);
- console.log('QPH = '+qph);
- console.log('QR = '+qr);
- console.log('QW = '+qw);
- console.log('Total = '+total);
- qPH.focus();
- }
- }
- else if (active_field == 'qr') {
- if (!qph) {
- qPH.focus();
- this.successMessage = '';
- this._error.next(`Must set quota per hour first.`);
- }
- else if (qr == null || qr < 0) {
- qR.value = '';
- }
- if (+qr > +qph) {
- this.successMessage = '';
- this._error.next(`Quota reservation is bigger than quota per hour. Set it to less than quota per hour.`);
- qR.value ='';
- qR.focus();
- }
- else if (+qr <= +qph) {
- balance = qph - qr;
- qW.value = balance;
- qWui.value = balance;
- }
- }
- // Quota walk in can't be less than 1
- if (qw == null || +qw < 1) {
- qW.value = 1;
- }
- }
- validateQuotaPostOnBlur(doctorID, qPH, qR, qW, active_field) {
- let qph:number = qPH.value;
- let qr:number = qR.value;
- let qw:number = qW.value;
- let balance:number = 0;
- if (active_field == 'qph') {
- if (qph == null) {
- qPH.value = 0;
- }
- let total:number;
- total = +qr + +qw;
- if (qph < total) {
- this.successMessage = '';
- this._error.next(`Quota per hour is less than total quota reservation and walk in.`);
- console.log('QPH = '+qph);
- console.log('QR = '+qr);
- console.log('QW = '+qw);
- console.log('Total = '+total);
- qPH.focus();
- } else {
- qR.focus();
- }
- }
- else if (active_field == 'qr') {
- if (qr == null || !qr || qr < 0) {
- qR.value ='';
- }
- if (+qr > +qph) {
- this.successMessage = '';
- this._error.next(`Quota reservation is bigger than quota per hour. Set it to less than quota per hour.`);
- qR.value =0;
- qR.focus();
- }
- else if (+qr <= +qph) {
- balance = qph - qr;
- qW.value = balance;
- }
- }
- if (this.postQuotaForm.controls.quota_doctor_id.value != '' && qph && qr) {
- this.save(doctorID, qph, qr, qw);
- }
- }
- validateQuotaKeyup(qPH, qR, qW, active_field) {
- let balance:number = 0;
- if (active_field == 'qph') {
- if (qPH.value <= 0) {
- qPH.value = '';
- qR.value = '';
- qW.value = '';
- }
- else if (qPH.value < qR.value || qPH.value < qW.value) {
- this.successMessage = '';
- this._error.next(`Quota per hour is less than total quota reservation and walk in.`);
- }
- else if (qPH.value >= qR.value+qW.value) {
- if (qW.value > 1) {
- qR.value = qPH.value - qW.value;
- qW.value = qPH.value - qR.value;
- }
- else {
- qW.value = 1;
- qR.value = qPH.value - qW.value;
- }
- }
- }
- else if (active_field == 'qr') {
- if (+qR.value <= 0) {
- qR.value = '';
- }
- else if (+qR.value > +qPH.value) {
- qR.value ='';
- this.successMessage = '';
- this._error.next(`Quota reservation is bigger than quota per hour. Set it to less than quota per hour.`);
- }
- else if (+qR.value <= +qPH.value) {
- if (qPH.value == qW.value || qPH.value == qR.value) {
- qR.value ='';
- this.successMessage = '';
- this._error.next(`Quota walkin must be set at least 1.`);
- }
- else {
- balance = qPH.value - qR.value;
- qW.value = balance;
- }
- }
- }
- }
- validateQuotaBlur(qPH, qR, qW, active_field) {
- let balance:number = 0;
- let total:number = +qR.value + +qW.value;
- if (active_field == 'qph') {
- if (qPH.value <= 0) {
- qPH.value = 0;
- qR.value = 0;
- qW.value = 0;
- }
- else if (qPH.value < total) {
- qPH.focus();
- this.successMessage = '';
- this._error.next(`Quota per hour is less than total quota reservation and walk in.`);
- }
- else if (qPH.value >= qR.value+qW.value) {
- if (qW.value > 1) {
- qR.value = qPH.value - qW.value;
- qW.value = qPH.value - qR.value;
- }
- else {
- qW.value = 1;
- qR.value = qPH.value - qW.value;
- }
- }
- }
- else if (active_field == 'qr') {
- if (+qR.value <= 0) {
- qR.value = 0;
- }
- else if (+qR.value > +qPH.value) {
- qR.value ='';
- this.successMessage = '';
- this._error.next(`Quota reservation is bigger than quota per hour. Set it to less than quota per hour.`);
- }
- else if (+qR.value <= +qPH.value) {
- if (qPH.value == qW.value) {
- qR.value ='';
- this.successMessage = '';
- this._error.next(`Quota walkin must be set at least 1.`);
- }
- else {
- balance = qPH.value - qR.value;
- qW.value = balance;
- }
- }
- }
- }
- validateQuotaChange(qPH, qR, qW, active_field, qID) {
- let balance:number = 0;
- let flag: number = 0;
- let total:number = +qR.value + +qW.value;
- if (active_field == 'qph') {
- if (qPH.value <= 0) {
- qPH.value = 0;
- qR.value = 0;
- qW.value = 0;
- }
- else if (qPH.value < total) {
- console.log(qPH.value);
- console.log(total);
- qPH.focus();
- this.successMessage = '';
- this._error.next(`Quota per hour is less than total quota reservation and walk in.`);
- flag++
- }
- else if (qPH.value >= total) {
- if (qW.value > 1) {
- qR.value = qPH.value - qW.value;
- qW.value = qPH.value - qR.value;
- }
- else {
- qW.value = 1;
- qR.value = qPH.value - qW.value;
- }
- }
- console.log('coba');
- }
- else if (active_field == 'qr') {
- if (+qR.value <= 0) {
- qR.value = 0;
- }
- else if (+qR.value > +qPH.value) {
- qR.value ='';
- this.successMessage = '';
- this._error.next(`Quota reservation is bigger than quota per hour. Set it to less than quota per hour.`);
- flag++
- }
- else if (+qR.value <= +qPH.value) {
- if (qPH.value == qW.value) {
- qR.value ='';
- this.successMessage = '';
- this._error.next(`Quota walkin must be set at least 1.`);
- flag++
- }
- else {
- balance = qPH.value - qR.value;
- qW.value = balance;
- }
- }
- }
- if (flag == 0) {
- this.editQuota(qID, qPH, qR, qW);
- }
- }
- // validateQuotaOnChange(type, q) {
- // let quotaPerHour = this.postQuotaForm.controls.quota_per_hour.value;
- // let quotaReservation = this.postQuotaForm.controls.quota_reservation.value;
- // let quotaWalkin = this.postQuotaForm.controls.quota_walkin.value;
- //
- // if (quotaPerHour == null) {
- // this.postQuotaForm.controls.quota_per_hour.setValue(0);
- // }
- //
- // if (quotaReservation == null) {
- // this.postQuotaForm.controls.quota_reservation.setValue(0);
- // }
- //
- // if (quotaWalkin == null) {
- // this.postQuotaForm.controls.quota_walkin.setValue(0);
- // }
- //
- // if (type == 'quota_reservation') {
- // if (quotaReservation > quotaPerHour) {
- // // alert('Quota reservation is bigger than quota per hour. Set it to less than quota per hour.');
- // this.successMessage = '';
- // this._error.next(`Quota reservation is bigger than quota per hour. Set it to less than quota per hour.`);
- // q.focus();
- // }
- // } else {
- // if (quotaWalkin > quotaPerHour) {
- // this.successMessage = '';
- // this._error.next(`Quota reservation is bigger than quota per hour. Set it to less than quota per hour.`);
- // q.focus();
- // }
- // }
- // }
- //
- // validateQuotaOnFocus(q) {
- // let quotaPerHour = this.postQuotaForm.controls.quota_per_hour.value;
- // if (quotaPerHour == '') {
- // q.focus();
- // // alert('Please set the quota per hour first before you set other quota.');
- // this.successMessage = '';
- // this._error.next(`Please set the quota per hour first before you set other quota.`);
- // }
- // }
- //
- // validateQuotaOnKeyUp(type) {
- // let quotaPerHour = this.postQuotaForm.controls.quota_per_hour.value;
- // let quotaReservation = this.postQuotaForm.controls.quota_reservation.value;
- // let quotaWalkin = this.postQuotaForm.controls.quota_walkin.value;
- //
- // if (type == 'quota_reservation') {
- // let balance = quotaPerHour - quotaReservation;
- // this.postQuotaForm.controls.quota_walkin.setValue(balance);
- // } else {
- // let balance = quotaPerHour - quotaWalkin;
- // this.postQuotaForm.controls.quota_reservation.setValue(balance);
- // }
- // }
- save(doctorID, qPH, qR, qW) {
- let qph:number = qPH;
- let qr:number = qR;
- let qw:number = qW;
- let formData: any = {};
- console.log(qph);
- console.log(qr);
- console.log(qw);
- formData.quota_per_hour = qph.toString();
- formData.quota_reservation = qr.toString();
- formData.quota_walkin = qw.toString();
- console.log(formData);
- this.doctorService.postDoctorQuota(doctorID.value, this.hospitalId, 'OP', formData)
- .subscribe(
- (res : Response) => {
- if (res.status.toString() == "Success") {
- this.errorMessage = '';
- this._success.next(`Add success`);
- this.refreshDoctorQuotaList();
- this.postQuotaForm.reset();
- // this.addNewQuota();
- }
- else {
- this.successMessage = '';
- this._error.next(`Add failed. There's some error while add doctor quota`);
- }
- },
- error => {
- this.successMessage = '';
- this._error.next(`Add failed. The form not filled correctly. Please make sure that the form filled correctly.`);
- });
- }
- // save(model) {
- // let formData: any = {};
- // formData.quota_per_hour = model.controls.quota_per_hour.value.toString();
- // formData.quota_reservation = model.controls.quota_reservation.value.toString();
- // formData.quota_walkin = model.controls.quota_walkin.value.toString();
- // console.log(formData);
- // console.log(model.controls.quota_doctor_id.value);
- // console.log(model.controls.quota_walkin.value)
- //
- // this.doctorService.postDoctorQuota(model.controls.quota_doctor_id.value, this.hospitalId, 'OP', formData)
- // .subscribe(
- // (res : Response) => {
- // if (res.status.toString() == "Success") {
- // this.errorMessage = '';
- // this._success.next(`Add success`);
- // this.refreshDoctorQuotaList();
- // this.postQuotaForm.reset();
- // // this.addNewQuota();
- // }
- // else {
- // this.successMessage = '';
- // this._error.next(`Add failed. There's some error while add doctor quota`);
- // }
- // },
- // error => {
- // this.successMessage = '';
- // this._error.next(`Add failed. The form not filled correctly. Please make sure that the form filled correctly.`);
- // });
- // }
- editQuota(quota_id, quota_per_hour, quota_reservation, quota_walkin) {
- let qID = quota_id.value;
- let qPH = quota_per_hour.value;
- let qR = quota_reservation.value;
- let qW = quota_walkin.value;
- let formDataEdit: any = {};
- formDataEdit.quota_per_hour = qPH;
- formDataEdit.quota_reservation = qR;
- formDataEdit.quota_walkin = qW;
- console.log(qPH);
- console.log(formDataEdit);
- this.doctorService.editDoctorQuota(qID,'OP', formDataEdit)
- .subscribe(
- (res : Response) => {
- if (res.status.toString() == "Success") {
- this.errorMessage = '';
- this._success.next(`Edit success`);
- this.refreshDoctorQuotaList();
- // this.addNewQuota();
- }
- else {
- this.successMessage = '';
- this._error.next(`Edit failed. There's some error while add doctor quota`);
- }
- },
- error => {
- this.successMessage = '';
- this._error.next(`Edit failed. The form not filled correctly. Please make sure that the form filled correctly.`);
- });
- }
- deleteQuota (quota_id) {
- let quotaId = quota_id.value;
- this.doctorService.deleteDoctorQuota(quotaId, 'OP')
- .subscribe(
- (res : Response) => {
- if (res.status.toString() == "Success") {
- // this._service.success(
- // 'Delete success',
- // "Schedule deleted for "+this.doctorName,
- // {
- // showProgressBar: true,
- // pauseOnHover: false,
- // clickToClose: false
- // }
- // );
- this.refreshDoctorQuotaList();
- this.errorMessage = '';
- this._success.next(`Delete success`);
- }
- else {
- // this._service.success(
- // 'Delete failed',
- // "There's some error whiile deleting schedule for "+this.doctorName,
- // {
- // showProgressBar: true,
- // pauseOnHover: false,
- // clickToClose: false
- // }
- // );
- this.successMessage = '';
- this._error.next(`Delete failed`);
- }
- },
- error => {
- // this._service.error(
- // 'Delete failed',
- // "The form not filled correctly.",
- // {
- // showProgressBar: true,
- // pauseOnHover: false,
- // clickToClose: false
- // }
- // );
- this.successMessage = '';
- this._error.next(`Delete failed`);
- });
- // this.router.navigateByUrl('/doctor-schedule/'+this.doctorId);
- }
- filter(query) {
- console.log(query.value);
- // let size = this.quotaList.length;
- // for (let i=0; i<size; i++) {
- // var re = /query.value/gi;
- // console.log(this.quotaList[i].doctor_name);
- // var str = this.quotaList[i].doctor_name;
- // if (str.search(re) == -1 ) {
- // console.log("Does not contain Apples" );
- // } else {
- // console.log("Contains Apples" );
- // }
- // }
- // $('#quota-list').hide();
- // $( "input[value*='Eka']" ).css('display', 'none');
- let q = query.value.toUpperCase();
- $("tr#quota-list").hide();
- $("tr:contains('"+q+"')").show();
- }
- public options = {
- position: ["top", "right"],
- timeOut: 500,
- lastOnBottom: true,
- clickToClose: true
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment