danielsantoso

widget doctor quota ts

Jan 3rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.30 KB | None | 0 0
  1. import { Component, OnInit, Injector } from '@angular/core';
  2. import { DoctorService} from "../service/doctor.service";
  3. import { CommonSpecialtiesService } from "../service/common-specialties.service";
  4. import { Router } from '@angular/router';
  5. import { FormGroup, FormControl, FormArray, FormBuilder, Validators } from '@angular/forms';
  6. import { NotificationsService} from "angular2-notifications";
  7. import {Subject} from 'rxjs/Subject';
  8. import {debounceTime} from 'rxjs/operator/debounceTime';
  9. import 'rxjs/add/observable/of';
  10. import { Observable } from 'rxjs/Observable';
  11. import { Doctor } from '../model/doctor';
  12. import * as $ from 'jquery';
  13.  
  14. @Component({
  15. selector: 'app-widget-doctor-quota',
  16. templateUrl: './widget-doctor-quota.component.html',
  17. styleUrls: ['./widget-doctor-quota.component.css']
  18. })
  19. export class WidgetDoctorQuotaComponent implements OnInit {
  20.  
  21. public doctorNameLists: Observable<Doctor[]>;
  22. public chooseDoctor: Doctor[] = [];
  23. quotaList: string;
  24. private doctorId: number;
  25. private doctorUid: string;
  26. public query: any;
  27. public hospitalId:string = localStorage.getItem('hospital_id');
  28. private listDoctorName: string[] = [];
  29. private listDoctorId: number[] = [];
  30. public postQuotaForm: FormGroup;
  31. private doctors: string;
  32. // For alert message
  33. successMessage: string;
  34. errorMessage: string;
  35. private _success = new Subject<string>();
  36. private _error = new Subject<string>();
  37. private specialtiesOption: string;
  38.  
  39. constructor(
  40. private _service: NotificationsService,
  41. private router: Router,
  42. private doctorService: DoctorService,
  43. private commonSpecialtiesService: CommonSpecialtiesService,
  44. private injector: Injector,
  45. private _fb: FormBuilder
  46. ) {
  47.  
  48. // Load doctor list
  49. this.doctorService.getDoctorList(this.hospitalId).subscribe(
  50. data => {
  51. console.log(data)
  52. if(data == null) {
  53. console.log("Null");
  54. }
  55. else {
  56. for (var i=0; i<data.length; i++) {
  57. this.listDoctorName.push(data[i]['doctor_name']);
  58. this.listDoctorId.push(data[i]['doctor_id']);
  59. }
  60. this.doctors = data;
  61. }
  62. }
  63. );
  64.  
  65. // Load doctor specialty
  66. this.commonSpecialtiesService.getSpecialties().subscribe(
  67. data => {
  68. if(data == null) {
  69. console.log("Null");
  70. }
  71. else {
  72. this.specialtiesOption = data;
  73. }
  74. }
  75. );
  76.  
  77. // Load doctor quota
  78. this.doctorService.getDoctorQuotaList(this.hospitalId).subscribe(
  79. data => {
  80. if(data == null) {
  81. console.log("Null");
  82. }
  83. else {
  84. this.quotaList = data;
  85. console.log(data);
  86. }
  87. }
  88. );
  89. }
  90.  
  91.  
  92.  
  93. ngOnInit() {
  94.  
  95. this.postQuotaForm = this._fb.group({
  96. search_doctor_query: ['', []],
  97. quota_doctor_id: ['', [Validators.required]],
  98. quota_per_hour: ['', [Validators.required]],
  99. quota_reservation: ['', [Validators.required]],
  100. quota_walkin: ['', []],
  101. quota_walkin_ui: [{value:'',disabled:true}, []],
  102. // newQuota: this._fb.array([
  103. // this.initNewQuota(),
  104. // ])
  105. });
  106.  
  107. this.doctorNameLists = this.doctorService.getDoctorList(this.hospitalId);
  108. this.doctorNameLists.subscribe (
  109. data => {
  110. if (data == null) {
  111. console.log('Null');
  112. } else {
  113. this.chooseDoctor = data;
  114. }
  115. },
  116. error => this.errorMessage = <any>error
  117. );
  118.  
  119. /* For alert */
  120. this._success.subscribe((message) => this.successMessage = message);
  121. debounceTime.call(this._success, 500).subscribe(() => this.successMessage = null);
  122.  
  123. this._error.subscribe((message) => this.errorMessage = message);
  124. debounceTime.call(this._error, 2000).subscribe(() => this.errorMessage = null);
  125. /* For alert */
  126. }
  127.  
  128. refreshDoctorQuotaList() {
  129. // Load doctor quota
  130. this.doctorService.getDoctorQuotaList(this.hospitalId).subscribe(
  131. data => {
  132. if(data == null) {
  133. console.log("Null");
  134. }
  135. else {
  136. this.quotaList = data;
  137. console.log(data);
  138. }
  139. }
  140. );
  141. }
  142.  
  143. // For get form data on add quota
  144. // public postQuotaForm = new FormGroup({
  145. // search_doctor_query: new FormControl("search_doctor_query", Validators.required),
  146. // quota_doctor_id: new FormControl("quota_doctor_id", Validators.required),
  147. // quota_per_hour: new FormControl("quota_per_hour", Validators.required),
  148. // quota_reservation: new FormControl("quota_reservation", Validators.required),
  149. // quota_walkin: new FormControl("quota_walkin", Validators.required),
  150. // });
  151.  
  152. initNewQuota() {
  153. return this._fb.group({
  154. quota_doctor_id: ['', []],
  155. quota_per_hour: ['', []],
  156. quota_reservation: ['', []],
  157. quota_walkin: ['', []]
  158. });
  159. }
  160.  
  161. addNewQuota() {
  162. const control = <FormArray>this.postQuotaForm.controls['newQuota'];
  163. control.push(this.initNewQuota());
  164. }
  165.  
  166. validateQuotaPost(qPH, qR, qW, qWui, active_field) {
  167. let qph:number = qPH.value;
  168. let qr:number = qR.value;
  169. let qw:number = qW.value;
  170. let balance:number = 0;
  171.  
  172. if (active_field == 'qph') {
  173. qW.value = +qph - +qr;
  174. qWui.value = +qph - +qr;
  175.  
  176. if (qph == null) {
  177. qPH.value = 0;
  178. }
  179.  
  180. let total:number;
  181. total = +qr + +qw;
  182.  
  183. if (qph < total && qr) {
  184. this.successMessage = '';
  185. this._error.next(`Quota per hour is less than total quota reservation and walk in.`);
  186. console.log('QPH = '+qph);
  187. console.log('QR = '+qr);
  188. console.log('QW = '+qw);
  189. console.log('Total = '+total);
  190. qPH.focus();
  191. }
  192. }
  193. else if (active_field == 'qr') {
  194. if (!qph) {
  195. qPH.focus();
  196. this.successMessage = '';
  197. this._error.next(`Must set quota per hour first.`);
  198. }
  199. else if (qr == null || qr < 0) {
  200. qR.value = '';
  201. }
  202.  
  203. if (+qr > +qph) {
  204. this.successMessage = '';
  205. this._error.next(`Quota reservation is bigger than quota per hour. Set it to less than quota per hour.`);
  206. qR.value ='';
  207. qR.focus();
  208. }
  209. else if (+qr <= +qph) {
  210. balance = qph - qr;
  211. qW.value = balance;
  212. qWui.value = balance;
  213. }
  214. }
  215.  
  216. // Quota walk in can't be less than 1
  217. if (qw == null || +qw < 1) {
  218. qW.value = 1;
  219. }
  220. }
  221.  
  222. validateQuotaPostOnBlur(doctorID, qPH, qR, qW, active_field) {
  223. let qph:number = qPH.value;
  224. let qr:number = qR.value;
  225. let qw:number = qW.value;
  226. let balance:number = 0;
  227.  
  228. if (active_field == 'qph') {
  229. if (qph == null) {
  230. qPH.value = 0;
  231. }
  232.  
  233. let total:number;
  234. total = +qr + +qw;
  235. if (qph < total) {
  236. this.successMessage = '';
  237. this._error.next(`Quota per hour is less than total quota reservation and walk in.`);
  238. console.log('QPH = '+qph);
  239. console.log('QR = '+qr);
  240. console.log('QW = '+qw);
  241. console.log('Total = '+total);
  242. qPH.focus();
  243. } else {
  244. qR.focus();
  245. }
  246. }
  247. else if (active_field == 'qr') {
  248.  
  249. if (qr == null || !qr || qr < 0) {
  250. qR.value ='';
  251. }
  252.  
  253. if (+qr > +qph) {
  254. this.successMessage = '';
  255. this._error.next(`Quota reservation is bigger than quota per hour. Set it to less than quota per hour.`);
  256. qR.value =0;
  257. qR.focus();
  258. }
  259. else if (+qr <= +qph) {
  260. balance = qph - qr;
  261. qW.value = balance;
  262. }
  263. }
  264.  
  265. if (this.postQuotaForm.controls.quota_doctor_id.value != '' && qph && qr) {
  266. this.save(doctorID, qph, qr, qw);
  267. }
  268. }
  269.  
  270. validateQuotaKeyup(qPH, qR, qW, active_field) {
  271. let balance:number = 0;
  272.  
  273. if (active_field == 'qph') {
  274. if (qPH.value <= 0) {
  275. qPH.value = '';
  276. qR.value = '';
  277. qW.value = '';
  278. }
  279. else if (qPH.value < qR.value || qPH.value < qW.value) {
  280. this.successMessage = '';
  281. this._error.next(`Quota per hour is less than total quota reservation and walk in.`);
  282. }
  283. else if (qPH.value >= qR.value+qW.value) {
  284. if (qW.value > 1) {
  285. qR.value = qPH.value - qW.value;
  286. qW.value = qPH.value - qR.value;
  287. }
  288. else {
  289. qW.value = 1;
  290. qR.value = qPH.value - qW.value;
  291. }
  292. }
  293. }
  294. else if (active_field == 'qr') {
  295. if (+qR.value <= 0) {
  296. qR.value = '';
  297. }
  298. else if (+qR.value > +qPH.value) {
  299. qR.value ='';
  300. this.successMessage = '';
  301. this._error.next(`Quota reservation is bigger than quota per hour. Set it to less than quota per hour.`);
  302. }
  303. else if (+qR.value <= +qPH.value) {
  304. if (qPH.value == qW.value || qPH.value == qR.value) {
  305. qR.value ='';
  306. this.successMessage = '';
  307. this._error.next(`Quota walkin must be set at least 1.`);
  308. }
  309. else {
  310. balance = qPH.value - qR.value;
  311. qW.value = balance;
  312. }
  313. }
  314. }
  315. }
  316.  
  317. validateQuotaBlur(qPH, qR, qW, active_field) {
  318. let balance:number = 0;
  319. let total:number = +qR.value + +qW.value;
  320.  
  321. if (active_field == 'qph') {
  322. if (qPH.value <= 0) {
  323. qPH.value = 0;
  324. qR.value = 0;
  325. qW.value = 0;
  326. }
  327. else if (qPH.value < total) {
  328. qPH.focus();
  329. this.successMessage = '';
  330. this._error.next(`Quota per hour is less than total quota reservation and walk in.`);
  331. }
  332. else if (qPH.value >= qR.value+qW.value) {
  333. if (qW.value > 1) {
  334. qR.value = qPH.value - qW.value;
  335. qW.value = qPH.value - qR.value;
  336. }
  337. else {
  338. qW.value = 1;
  339. qR.value = qPH.value - qW.value;
  340. }
  341. }
  342. }
  343. else if (active_field == 'qr') {
  344. if (+qR.value <= 0) {
  345. qR.value = 0;
  346. }
  347. else if (+qR.value > +qPH.value) {
  348. qR.value ='';
  349. this.successMessage = '';
  350. this._error.next(`Quota reservation is bigger than quota per hour. Set it to less than quota per hour.`);
  351. }
  352. else if (+qR.value <= +qPH.value) {
  353. if (qPH.value == qW.value) {
  354. qR.value ='';
  355. this.successMessage = '';
  356. this._error.next(`Quota walkin must be set at least 1.`);
  357. }
  358. else {
  359. balance = qPH.value - qR.value;
  360. qW.value = balance;
  361. }
  362. }
  363. }
  364. }
  365.  
  366. validateQuotaChange(qPH, qR, qW, active_field, qID) {
  367. let balance:number = 0;
  368. let flag: number = 0;
  369. let total:number = +qR.value + +qW.value;
  370.  
  371. if (active_field == 'qph') {
  372. if (qPH.value <= 0) {
  373. qPH.value = 0;
  374. qR.value = 0;
  375. qW.value = 0;
  376. }
  377. else if (qPH.value < total) {
  378. console.log(qPH.value);
  379. console.log(total);
  380. qPH.focus();
  381. this.successMessage = '';
  382. this._error.next(`Quota per hour is less than total quota reservation and walk in.`);
  383. flag++
  384. }
  385. else if (qPH.value >= total) {
  386. if (qW.value > 1) {
  387. qR.value = qPH.value - qW.value;
  388. qW.value = qPH.value - qR.value;
  389. }
  390. else {
  391. qW.value = 1;
  392. qR.value = qPH.value - qW.value;
  393. }
  394. }
  395. console.log('coba');
  396. }
  397. else if (active_field == 'qr') {
  398. if (+qR.value <= 0) {
  399. qR.value = 0;
  400. }
  401. else if (+qR.value > +qPH.value) {
  402. qR.value ='';
  403. this.successMessage = '';
  404. this._error.next(`Quota reservation is bigger than quota per hour. Set it to less than quota per hour.`);
  405. flag++
  406. }
  407. else if (+qR.value <= +qPH.value) {
  408. if (qPH.value == qW.value) {
  409. qR.value ='';
  410. this.successMessage = '';
  411. this._error.next(`Quota walkin must be set at least 1.`);
  412. flag++
  413. }
  414. else {
  415. balance = qPH.value - qR.value;
  416. qW.value = balance;
  417. }
  418. }
  419. }
  420.  
  421. if (flag == 0) {
  422. this.editQuota(qID, qPH, qR, qW);
  423. }
  424. }
  425.  
  426. // validateQuotaOnChange(type, q) {
  427. // let quotaPerHour = this.postQuotaForm.controls.quota_per_hour.value;
  428. // let quotaReservation = this.postQuotaForm.controls.quota_reservation.value;
  429. // let quotaWalkin = this.postQuotaForm.controls.quota_walkin.value;
  430. //
  431. // if (quotaPerHour == null) {
  432. // this.postQuotaForm.controls.quota_per_hour.setValue(0);
  433. // }
  434. //
  435. // if (quotaReservation == null) {
  436. // this.postQuotaForm.controls.quota_reservation.setValue(0);
  437. // }
  438. //
  439. // if (quotaWalkin == null) {
  440. // this.postQuotaForm.controls.quota_walkin.setValue(0);
  441. // }
  442. //
  443. // if (type == 'quota_reservation') {
  444. // if (quotaReservation > quotaPerHour) {
  445. // // alert('Quota reservation is bigger than quota per hour. Set it to less than quota per hour.');
  446. // this.successMessage = '';
  447. // this._error.next(`Quota reservation is bigger than quota per hour. Set it to less than quota per hour.`);
  448. // q.focus();
  449. // }
  450. // } else {
  451. // if (quotaWalkin > quotaPerHour) {
  452. // this.successMessage = '';
  453. // this._error.next(`Quota reservation is bigger than quota per hour. Set it to less than quota per hour.`);
  454. // q.focus();
  455. // }
  456. // }
  457. // }
  458. //
  459. // validateQuotaOnFocus(q) {
  460. // let quotaPerHour = this.postQuotaForm.controls.quota_per_hour.value;
  461. // if (quotaPerHour == '') {
  462. // q.focus();
  463. // // alert('Please set the quota per hour first before you set other quota.');
  464. // this.successMessage = '';
  465. // this._error.next(`Please set the quota per hour first before you set other quota.`);
  466. // }
  467. // }
  468. //
  469. // validateQuotaOnKeyUp(type) {
  470. // let quotaPerHour = this.postQuotaForm.controls.quota_per_hour.value;
  471. // let quotaReservation = this.postQuotaForm.controls.quota_reservation.value;
  472. // let quotaWalkin = this.postQuotaForm.controls.quota_walkin.value;
  473. //
  474. // if (type == 'quota_reservation') {
  475. // let balance = quotaPerHour - quotaReservation;
  476. // this.postQuotaForm.controls.quota_walkin.setValue(balance);
  477. // } else {
  478. // let balance = quotaPerHour - quotaWalkin;
  479. // this.postQuotaForm.controls.quota_reservation.setValue(balance);
  480. // }
  481. // }
  482.  
  483. save(doctorID, qPH, qR, qW) {
  484. let qph:number = qPH;
  485. let qr:number = qR;
  486. let qw:number = qW;
  487. let formData: any = {};
  488. console.log(qph);
  489. console.log(qr);
  490. console.log(qw);
  491. formData.quota_per_hour = qph.toString();
  492. formData.quota_reservation = qr.toString();
  493. formData.quota_walkin = qw.toString();
  494. console.log(formData);
  495.  
  496. this.doctorService.postDoctorQuota(doctorID.value, this.hospitalId, 'OP', formData)
  497. .subscribe(
  498. (res : Response) => {
  499. if (res.status.toString() == "Success") {
  500. this.errorMessage = '';
  501. this._success.next(`Add success`);
  502. this.refreshDoctorQuotaList();
  503. this.postQuotaForm.reset();
  504. // this.addNewQuota();
  505. }
  506. else {
  507. this.successMessage = '';
  508. this._error.next(`Add failed. There's some error while add doctor quota`);
  509. }
  510. },
  511. error => {
  512. this.successMessage = '';
  513. this._error.next(`Add failed. The form not filled correctly. Please make sure that the form filled correctly.`);
  514. });
  515. }
  516.  
  517.  
  518. // save(model) {
  519. // let formData: any = {};
  520. // formData.quota_per_hour = model.controls.quota_per_hour.value.toString();
  521. // formData.quota_reservation = model.controls.quota_reservation.value.toString();
  522. // formData.quota_walkin = model.controls.quota_walkin.value.toString();
  523. // console.log(formData);
  524. // console.log(model.controls.quota_doctor_id.value);
  525. // console.log(model.controls.quota_walkin.value)
  526. //
  527. // this.doctorService.postDoctorQuota(model.controls.quota_doctor_id.value, this.hospitalId, 'OP', formData)
  528. // .subscribe(
  529. // (res : Response) => {
  530. // if (res.status.toString() == "Success") {
  531. // this.errorMessage = '';
  532. // this._success.next(`Add success`);
  533. // this.refreshDoctorQuotaList();
  534. // this.postQuotaForm.reset();
  535. // // this.addNewQuota();
  536. // }
  537. // else {
  538. // this.successMessage = '';
  539. // this._error.next(`Add failed. There's some error while add doctor quota`);
  540. // }
  541. // },
  542. // error => {
  543. // this.successMessage = '';
  544. // this._error.next(`Add failed. The form not filled correctly. Please make sure that the form filled correctly.`);
  545. // });
  546. // }
  547.  
  548. editQuota(quota_id, quota_per_hour, quota_reservation, quota_walkin) {
  549. let qID = quota_id.value;
  550. let qPH = quota_per_hour.value;
  551. let qR = quota_reservation.value;
  552. let qW = quota_walkin.value;
  553. let formDataEdit: any = {};
  554. formDataEdit.quota_per_hour = qPH;
  555. formDataEdit.quota_reservation = qR;
  556. formDataEdit.quota_walkin = qW;
  557. console.log(qPH);
  558. console.log(formDataEdit);
  559.  
  560. this.doctorService.editDoctorQuota(qID,'OP', formDataEdit)
  561. .subscribe(
  562. (res : Response) => {
  563. if (res.status.toString() == "Success") {
  564. this.errorMessage = '';
  565. this._success.next(`Edit success`);
  566. this.refreshDoctorQuotaList();
  567. // this.addNewQuota();
  568. }
  569. else {
  570. this.successMessage = '';
  571. this._error.next(`Edit failed. There's some error while add doctor quota`);
  572. }
  573. },
  574. error => {
  575. this.successMessage = '';
  576. this._error.next(`Edit failed. The form not filled correctly. Please make sure that the form filled correctly.`);
  577. });
  578. }
  579.  
  580. deleteQuota (quota_id) {
  581. let quotaId = quota_id.value;
  582.  
  583. this.doctorService.deleteDoctorQuota(quotaId, 'OP')
  584. .subscribe(
  585. (res : Response) => {
  586. if (res.status.toString() == "Success") {
  587. // this._service.success(
  588. // 'Delete success',
  589. // "Schedule deleted for "+this.doctorName,
  590. // {
  591. // showProgressBar: true,
  592. // pauseOnHover: false,
  593. // clickToClose: false
  594. // }
  595. // );
  596. this.refreshDoctorQuotaList();
  597. this.errorMessage = '';
  598. this._success.next(`Delete success`);
  599. }
  600. else {
  601. // this._service.success(
  602. // 'Delete failed',
  603. // "There's some error whiile deleting schedule for "+this.doctorName,
  604. // {
  605. // showProgressBar: true,
  606. // pauseOnHover: false,
  607. // clickToClose: false
  608. // }
  609. // );
  610. this.successMessage = '';
  611. this._error.next(`Delete failed`);
  612. }
  613. },
  614. error => {
  615. // this._service.error(
  616. // 'Delete failed',
  617. // "The form not filled correctly.",
  618. // {
  619. // showProgressBar: true,
  620. // pauseOnHover: false,
  621. // clickToClose: false
  622. // }
  623. // );
  624. this.successMessage = '';
  625. this._error.next(`Delete failed`);
  626. });
  627.  
  628. // this.router.navigateByUrl('/doctor-schedule/'+this.doctorId);
  629. }
  630.  
  631. filter(query) {
  632. console.log(query.value);
  633. // let size = this.quotaList.length;
  634. // for (let i=0; i<size; i++) {
  635. // var re = /query.value/gi;
  636. // console.log(this.quotaList[i].doctor_name);
  637. // var str = this.quotaList[i].doctor_name;
  638. // if (str.search(re) == -1 ) {
  639. // console.log("Does not contain Apples" );
  640. // } else {
  641. // console.log("Contains Apples" );
  642. // }
  643. // }
  644. // $('#quota-list').hide();
  645. // $( "input[value*='Eka']" ).css('display', 'none');
  646. let q = query.value.toUpperCase();
  647. $("tr#quota-list").hide();
  648. $("tr:contains('"+q+"')").show();
  649. }
  650.  
  651. public options = {
  652. position: ["top", "right"],
  653. timeOut: 500,
  654. lastOnBottom: true,
  655. clickToClose: true
  656. };
  657.  
  658. }
Advertisement
Add Comment
Please, Sign In to add comment