104 lines
3.8 KiB
TypeScript
104 lines
3.8 KiB
TypeScript
import { inject, Injectable } from '@angular/core';
|
|
import { environment } from '../../../../environments/environment';
|
|
import { HttpClient } from '@angular/common/http';
|
|
import { filter, map, Observable } from 'rxjs';
|
|
import { UserService } from '../common/user.service';
|
|
import { Shipping } from '../../models/carnet/shipping';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class ShippingService {
|
|
private apiUrl = environment.apiUrl;
|
|
private apiDb = environment.apiDb;
|
|
|
|
private http = inject(HttpClient);
|
|
private userService = inject(UserService);
|
|
|
|
getShippingData(headerid: number): Shipping | any {
|
|
return this.http.get<any[]>(`${this.apiUrl}/${this.apiDb}/GetShipPaymentDetailstoEdit/${this.userService.getUserSpid()}/${this.userService.getUser()}/${headerid}`).pipe(
|
|
filter(response => response.length > 0),
|
|
map(response => this.mapToShippingData(response?.[0])));
|
|
}
|
|
|
|
private mapToShippingData(shippingDetails: any): Shipping {
|
|
|
|
let shippingData: Shipping = {
|
|
shipTo: shippingDetails.SHIPADDRTYPE,
|
|
deliveryType: shippingDetails.DELIVERYTYPE,
|
|
deliveryMethod: shippingDetails.DELIVERYMETHOD,
|
|
courierAccount: shippingDetails.CUSTCOURIERNO,
|
|
paymentMethod: shippingDetails.PAYMENTMETHOD,
|
|
|
|
notes: shippingDetails.NOTES,
|
|
needsBond: shippingDetails.INSPROTECTION === 'Y',
|
|
needsInsurance: shippingDetails.LDIPROTECTION === 'Y',
|
|
needsLostDocProtection: shippingDetails.LDIPROTECTION === 'Y'
|
|
};
|
|
|
|
if (shippingDetails.address) {
|
|
shippingData.address = {
|
|
address1: shippingDetails.ADDRESS1,
|
|
address2: shippingDetails.ADDRESS2,
|
|
city: shippingDetails.CITY,
|
|
state: shippingDetails.STATE,
|
|
zip: shippingDetails.ZIP,
|
|
country: shippingDetails.COUNTRY
|
|
}
|
|
}
|
|
|
|
if (shippingDetails.contact) {
|
|
shippingData.contact = {
|
|
firstName: shippingDetails.FIRSTNAME,
|
|
lastName: shippingDetails.LASTNAME,
|
|
middleInitial: shippingDetails.MIDDLEINITIAL,
|
|
title: shippingDetails.TITLE,
|
|
phone: shippingDetails.PHONENO,
|
|
mobile: shippingDetails.MOBILENO,
|
|
fax: shippingDetails.FAXNO,
|
|
email: shippingDetails.EMAILADDRESS,
|
|
refNumber: shippingDetails.REFNO,
|
|
}
|
|
}
|
|
|
|
return shippingData;
|
|
}
|
|
|
|
saveShippingDetails(headerid: number, shippingData: Shipping): Observable<any> {
|
|
|
|
const shippingDetails = {
|
|
P_HEADERID: headerid,
|
|
P_SHIPTOTYPE: shippingData.shipTo,
|
|
P_DELIVERYTYPE: shippingData.deliveryType,
|
|
P_DELIVERYMETHOD: shippingData.deliveryMethod,
|
|
P_CUSTCOURIERNO: shippingData.courierAccount,
|
|
P_PAYMENTMETHOD: shippingData.paymentMethod,
|
|
P_NOTES: shippingData.notes,
|
|
|
|
P_FORMOFSECURITY: shippingData.needsBond ? 'Y' : 'N',
|
|
P_INSPROTECTION: shippingData.needsInsurance ? 'Y' : 'N',
|
|
P_LDIPROTECTION: shippingData.needsLostDocProtection ? 'Y' : 'N',
|
|
|
|
address1: shippingData.address?.address1 || '',
|
|
address2: shippingData.address?.address2 || null,
|
|
city: shippingData.address?.city || '',
|
|
state: shippingData.address?.state || '',
|
|
zip: shippingData.address?.zip || '',
|
|
country: shippingData.address?.country || '',
|
|
|
|
firstName: shippingData.contact?.firstName || '',
|
|
lastName: shippingData.contact?.lastName || '',
|
|
middleInitial: shippingData.contact?.middleInitial || null,
|
|
title: shippingData.contact?.title || '',
|
|
phone: shippingData.contact?.phone || '',
|
|
mobile: shippingData.contact?.mobile || '',
|
|
fax: shippingData.contact?.fax || null,
|
|
emailAddress: shippingData.contact?.email || '',
|
|
refNo: shippingData.contact?.refNumber || '',
|
|
P_USERID: this.userService.getUser()
|
|
}
|
|
|
|
return this.http.patch(`${this.apiUrl}/${this.apiDb}/UpdateShippingDetails`, shippingDetails);
|
|
}
|
|
}
|