47 lines
1.8 KiB
TypeScript
47 lines
1.8 KiB
TypeScript
import { HttpClient, httpResource, HttpParams } from '@angular/common/http';
|
|
import { inject, Injectable, Signal } from '@angular/core';
|
|
import { Observable } from 'rxjs/internal/Observable';
|
|
import { catchError } from 'rxjs/operators';
|
|
|
|
import { ErrorLoggerService } from '../core/error-logger.service';
|
|
import { ClosingStock } from './closing-stock';
|
|
|
|
const url = '/api/closing-stock';
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class ClosingStockService {
|
|
private http = inject(HttpClient);
|
|
private log = inject(ErrorLoggerService);
|
|
|
|
list(date: Signal<string | null>, costCentre: Signal<string | null>) {
|
|
return httpResource<ClosingStock>(() => {
|
|
const listUrl = date() === null ? url : `${url}/${date()}`;
|
|
const params: Record<string, string> = {};
|
|
const costCentre_val = costCentre();
|
|
if (costCentre_val !== null) params['d'] = costCentre_val;
|
|
return { url: listUrl, params };
|
|
});
|
|
}
|
|
|
|
save(closingStock: ClosingStock): Observable<ClosingStock> {
|
|
return this.http
|
|
.post<ClosingStock>(url, closingStock)
|
|
.pipe(catchError(this.log.handleError('ClosingStockService', 'save'))) as Observable<ClosingStock>;
|
|
}
|
|
|
|
post(date: string, costCentre: string): Observable<ClosingStock> {
|
|
const options = { params: new HttpParams().set('d', costCentre) };
|
|
return this.http
|
|
.post<ClosingStock>(`${url}/${date}`, {}, options)
|
|
.pipe(catchError(this.log.handleError('ClosingStockService', 'Post Voucher'))) as Observable<ClosingStock>;
|
|
}
|
|
|
|
delete(date: string, costCentre: string): Observable<ClosingStock> {
|
|
const options = { params: new HttpParams().set('d', costCentre) };
|
|
return this.http
|
|
.delete<ClosingStock>(`${url}/${date}`, options)
|
|
.pipe(catchError(this.log.handleError('ClosingStockService', 'Delete Voucher'))) as Observable<ClosingStock>;
|
|
}
|
|
}
|