import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core'; import { MediaObjectService, PartnerJsonld, PartnerService, ProductService, SaleJsonld, SaleService } from "@app/core/api/v1"; import {ModalStatus} from "@app/_helpers/modal.states"; import {FormGroup} from "@angular/forms"; import {debounceTime, distinctUntilChanged, Observable, OperatorFunction, Subscription, switchMap} from "rxjs"; import {TranslateService} from "@ngx-translate/core"; import {FormGroupInitializer} from "@app/_helpers/formgroup.initializer"; import {saleForm} from "@app/_forms/apiForms"; import {ApiHelperService} from "@app/_helpers/api-helper.service"; import {filter, map} from "rxjs/operators"; @Component({ selector: 'app-new-sale', templateUrl: './new-sale.component.html', styleUrl: './new-sale.component.scss' }) export class NewSaleComponent implements OnInit { @Input() public sale!: SaleJsonld; @Output() public submit: EventEmitter = new EventEmitter(); protected saleForm: FormGroup; protected saleSub: Subscription; protected formatter = (apiData: any) => apiData.name; constructor( private saleService: SaleService, private partnerService: PartnerService, private productService: ProductService, private translateService: TranslateService, protected apiHelperService: ApiHelperService, ) { this.saleForm = saleForm; this.saleSub = new Subscription(); } ngOnInit(): void { this.saleForm = FormGroupInitializer.initFormGroup(this.saleForm, this.sale); } protected searchPartners: OperatorFunction = (text$: Observable) => text$.pipe( debounceTime(200), distinctUntilChanged(), filter((term) => term.length >= 2), switchMap((term) => this.fetchPartners(term)), map((partners) => partners.slice(0, 10)), ); protected searchProducts: OperatorFunction = (text$: Observable) => text$.pipe( debounceTime(200), distinctUntilChanged(), filter((term) => term.length >= 2), switchMap((term) => this.fetchProducts(term)), map((products) => products.slice(0, 10)), ); protected fetchPartners(term: string): Observable<{ id: any; name: any }[]> { return this.partnerService.partnersGetCollection(1, 50, undefined, undefined, term).pipe( map((response) => response['hydra:member'].map(partner => ({id: partner.id, name: partner.name}))), ); } protected fetchProducts(term: string): Observable<{ id: any; name: any }[]> { return this.productService.productsGetCollection(1, 50, term).pipe( map((response) => response['hydra:member'].map(product => ({id: product.id, name: product.name}))), ); } protected onPartnerSelect(selectedItem: any): void { this.saleForm.get('partner')?.setValue(selectedItem.item.id); } protected onProductSelect(selectedItem: any): void { this.saleForm.get('product')?.setValue(selectedItem.item.id); } onSubmit() { if (this.saleForm.valid) { if (this.saleForm.get('profit')?.value > this.saleForm.get('turnover')?.value) { let alertMessage = ""; this.translateService.get('system.profit-larger-turnover').subscribe((translation: string) => { alertMessage = translation; }); alert(alertMessage); return; } if (this.sale.id === null || this.sale.id === undefined) { // Create new sale this.saleSub = this.saleService.salesPost( this.saleForm.value as SaleJsonld ).subscribe( data => { this.saleForm.reset(); this.submit.emit(ModalStatus.Submitted); } ); } else { // Edit sale this.saleSub = this.saleService.salesIdPatch( this.apiHelperService.extractId(this.sale.id), this.saleForm.value as SaleJsonld ).subscribe( data => { this.saleForm.reset(); this.submit.emit(ModalStatus.Submitted); } ); } } } }