25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

128 lines
4.6 KiB

  1. import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
  2. import {
  3. MediaObjectService,
  4. PartnerJsonld,
  5. PartnerService,
  6. ProductService,
  7. SaleJsonld,
  8. SaleService
  9. } from "@app/core/api/v1";
  10. import {ModalStatus} from "@app/_helpers/modal.states";
  11. import {FormGroup} from "@angular/forms";
  12. import {debounceTime, distinctUntilChanged, Observable, OperatorFunction, Subscription, switchMap} from "rxjs";
  13. import {TranslateService} from "@ngx-translate/core";
  14. import {FormGroupInitializer} from "@app/_helpers/formgroup.initializer";
  15. import {saleForm} from "@app/_forms/apiForms";
  16. import {ApiHelperService} from "@app/_helpers/api-helper.service";
  17. import {filter, map} from "rxjs/operators";
  18. @Component({
  19. selector: 'app-new-sale',
  20. templateUrl: './new-sale.component.html',
  21. styleUrl: './new-sale.component.scss'
  22. })
  23. export class NewSaleComponent implements OnInit {
  24. @Input() public sale!: SaleJsonld;
  25. @Output() public submit: EventEmitter<ModalStatus> = new EventEmitter<ModalStatus>();
  26. protected saleForm: FormGroup;
  27. protected saleSub: Subscription;
  28. protected formatter = (apiData: any) => apiData.name;
  29. constructor(
  30. private saleService: SaleService,
  31. private partnerService: PartnerService,
  32. private productService: ProductService,
  33. private translateService: TranslateService,
  34. protected apiHelperService: ApiHelperService,
  35. ) {
  36. this.saleForm = saleForm;
  37. this.saleSub = new Subscription();
  38. }
  39. ngOnInit(): void {
  40. this.saleForm = FormGroupInitializer.initFormGroup(this.saleForm, this.sale);
  41. }
  42. protected searchPartners: OperatorFunction<string, readonly {
  43. id: any;
  44. name: any
  45. }[]> = (text$: Observable<string>) =>
  46. text$.pipe(
  47. debounceTime(200),
  48. distinctUntilChanged(),
  49. filter((term) => term.length >= 2),
  50. switchMap((term) => this.fetchPartners(term)),
  51. map((partners) => partners.slice(0, 10)),
  52. );
  53. protected searchProducts: OperatorFunction<string, readonly {
  54. id: any;
  55. name: any
  56. }[]> = (text$: Observable<string>) =>
  57. text$.pipe(
  58. debounceTime(200),
  59. distinctUntilChanged(),
  60. filter((term) => term.length >= 2),
  61. switchMap((term) => this.fetchProducts(term)),
  62. map((products) => products.slice(0, 10)),
  63. );
  64. protected fetchPartners(term: string): Observable<{ id: any; name: any }[]> {
  65. return this.partnerService.partnersGetCollection(1, 50, undefined, undefined, term).pipe(
  66. map((response) => response['hydra:member'].map(partner => ({id: partner.id, name: partner.name}))),
  67. );
  68. }
  69. protected fetchProducts(term: string): Observable<{ id: any; name: any }[]> {
  70. return this.productService.productsGetCollection(1, 50, term).pipe(
  71. map((response) => response['hydra:member'].map(product => ({id: product.id, name: product.name}))),
  72. );
  73. }
  74. protected onPartnerSelect(selectedItem: any): void {
  75. this.saleForm.get('partner')?.setValue(selectedItem.item.id);
  76. }
  77. protected onProductSelect(selectedItem: any): void {
  78. this.saleForm.get('product')?.setValue(selectedItem.item.id);
  79. }
  80. onSubmit() {
  81. if (this.saleForm.valid) {
  82. if (this.saleForm.get('profit')?.value > this.saleForm.get('turnover')?.value) {
  83. let alertMessage = "";
  84. this.translateService.get('system.profit-larger-turnover').subscribe((translation: string) => {
  85. alertMessage = translation;
  86. });
  87. alert(alertMessage);
  88. return;
  89. }
  90. if (this.sale.id === null || this.sale.id === undefined) {
  91. // Create new sale
  92. this.saleSub = this.saleService.salesPost(
  93. this.saleForm.value as SaleJsonld
  94. ).subscribe(
  95. data => {
  96. this.saleForm.reset();
  97. this.submit.emit(ModalStatus.Submitted);
  98. }
  99. );
  100. } else {
  101. // Edit sale
  102. this.saleSub = this.saleService.salesIdPatch(
  103. this.apiHelperService.extractId(this.sale.id),
  104. this.saleForm.value as SaleJsonld
  105. ).subscribe(
  106. data => {
  107. this.saleForm.reset();
  108. this.submit.emit(ModalStatus.Submitted);
  109. }
  110. );
  111. }
  112. }
  113. }
  114. }