Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

127 Zeilen
5.0 KiB

  1. import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
  2. import {ICustomerNote} from '../../../../model/entities/customer-note';
  3. import {NgForm} from '@angular/forms';
  4. import {Factory} from '../../../../factory/factory';
  5. import {ICustomer} from '../../../../model/entities/customer';
  6. import {ICustomerContact} from '../../../../model/entities/customer-contact';
  7. import {Utils} from '../../../../utils/utils';
  8. import {ScrollToService} from '@nicky-lenaers/ngx-scroll-to';
  9. import {FormComponent} from '../../../../components/form-component';
  10. @Component({
  11. selector: 'app-customer-note-edit',
  12. templateUrl: './customer-note-edit.component.html',
  13. styleUrls: ['./customer-note-edit.component.scss']
  14. })
  15. export class CustomerNoteEditComponent extends FormComponent implements OnInit {
  16. @Output() createNote: EventEmitter<ICustomerNote> = new EventEmitter<ICustomerNote>();
  17. @Output() editNote: EventEmitter<ICustomerNote> = new EventEmitter<ICustomerNote>();
  18. @ViewChild('customerNoteForm', { static: true }) customerNoteForm: NgForm;
  19. public customer: ICustomer;
  20. public customerNote: ICustomerNote;
  21. public customerContacts: ICustomerContact[];
  22. public customerContactsById: {};
  23. public isExistingCustomerContact: boolean;
  24. public contactId: string;
  25. constructor(protected scrollToService: ScrollToService) {
  26. super(scrollToService);
  27. }
  28. ngOnInit() {
  29. this.customer = Factory.getEmptyCustomer();
  30. this.customerNote = Factory.getEmptyCustomerNote();
  31. this.customerContacts = [];
  32. this.customerContactsById = {};
  33. this.errorMsg = '';
  34. this.contactId = '';
  35. this.isCreationMode = false;
  36. this.isExistingCustomerContact = false;
  37. }
  38. /**
  39. * Sets data
  40. * @param customer
  41. * @param customerNote
  42. * @param isCreationMode
  43. */
  44. public setData(customer: ICustomer, customerNote: ICustomerNote, isCreationMode = false): void {
  45. this.customer = customer;
  46. this.customerNote = customerNote;
  47. this.customerContacts = this.customer.v_customer_contacts;
  48. this.customerContactsById = Utils.getSortedObjFromArray(this.customerContacts, 'id');
  49. this.customerNote.customer_id = this.customer.id;
  50. this.contactId = '';
  51. this.isCreationMode = isCreationMode;
  52. this.errorMsg = '';
  53. if (null === this.customerNote.customer_contact_id) {
  54. this.customerNote.customer_contact_id = -1;
  55. }
  56. if (this.customerNote.customer_contact_id > 0) {
  57. this.isExistingCustomerContact = true;
  58. }
  59. this.resetFormValidation();
  60. }
  61. /**
  62. * On select contact
  63. * @param e
  64. */
  65. public selectContact(e: any) {
  66. this.customerNote.customer_contact_id = e.target.value;
  67. this.isExistingCustomerContact = false;
  68. this.customerNote.gender = null;
  69. this.customerNote.firstname = null;
  70. this.customerNote.lastname = null;
  71. this.customerNote.department = null;
  72. this.customerNote.email = null;
  73. this.customerNote.phone_no = null;
  74. this.customerNote.mobile_no = null;
  75. this.customerNote.fax_no = null;
  76. if (this.customerNote.customer_contact_id > 0) {
  77. this.customerNote.gender = this.customerContactsById[this.customerNote.customer_contact_id].gender;
  78. this.customerNote.firstname = this.customerContactsById[this.customerNote.customer_contact_id].firstname;
  79. this.customerNote.lastname = this.customerContactsById[this.customerNote.customer_contact_id].lastname;
  80. this.customerNote.department = this.customerContactsById[this.customerNote.customer_contact_id].department;
  81. this.customerNote.email = this.customerContactsById[this.customerNote.customer_contact_id].email;
  82. this.customerNote.phone_no = this.customerContactsById[this.customerNote.customer_contact_id].phone_no;
  83. this.customerNote.mobile_no = this.customerContactsById[this.customerNote.customer_contact_id].mobile_no;
  84. this.customerNote.fax_no = this.customerContactsById[this.customerNote.customer_contact_id].fax_no;
  85. this.isExistingCustomerContact = true;
  86. }
  87. }
  88. /**
  89. * Resets Form Validation
  90. */
  91. public resetFormValidation(): void {
  92. this.customerNoteForm.form.markAsUntouched();
  93. }
  94. /**
  95. * Saves customer
  96. * @param value
  97. * @param valid
  98. */
  99. public onFormSubmit({value, valid}: { value: any, valid: boolean }): void {
  100. this.errorMsg = 'Bitte beheben Sie alle Fehler.';
  101. if (valid) {
  102. if (this.customerNote.customer_contact_id == -1) {
  103. this.customerNote.customer_contact_id = null;
  104. }
  105. if (this.isCreationMode) {
  106. this.createNote.emit(this.customerNote);
  107. } else {
  108. this.editNote.emit(this.customerNote);
  109. }
  110. this.errorMsg = '';
  111. } else {
  112. this.scrollUp(this.isCreationMode ? 'customer-note-modal' : 'customer-note-edit-modal');
  113. }
  114. }
  115. }