import { Component, Injector, ViewChild, ViewEncapsulation } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { appModuleAnimation } from '@shared/animations/routerTransition'; import { AppComponentBase } from '@shared/common/app-component-base'; import { OrderServiceProxy, OrderListDto, LocationServiceProxy, LocationListDto, UpdateOrderV2Input, DestinationDetailListDto, ContactListDto, AddressListDto, TagTypeListDto, TagListDto, EmailListDto, PhoneListDto, PostalCodeInAddressListDto, NoteListDto, OrderPackageListDto, ShippingPackageListDto, TagTypeServiceProxy, PostalCodeServiceProxy, ShippingPackageServiceProxy, OrderPackageServiceProxy } from '@shared/service-proxies/service-proxies'; import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent'; import { Paginator } from 'primeng/components/paginator/paginator'; import { Table } from 'primeng/components/table/table'; import { finalize } from 'rxjs/operators'; import * as moment from 'moment'; import { CreateOrderModalComponent } from './create-order-modal.component'; import { PrimengTableHelper } from '@shared/helpers/PrimengTableHelper'; import * as _ from 'lodash'; declare var KTWizard: any; declare var $: any; declare var KTBootstrapDaterangepicker: any; declare let swal: any; @Component({ templateUrl: './update-order.component.html', encapsulation: ViewEncapsulation.None, animations: [appModuleAnimation()] }) export class UpdateOrderComponent extends AppComponentBase { // @ViewChild('dataTable', {static: true}) dataTable: Table; // @ViewChild('paginator', {static: true}) paginator: Paginator; // @ViewChild('createOrderModal') createOrderModal: CreateOrderModalComponent; active = false; saving = false; forUpdateInput: UpdateOrderV2Input = new UpdateOrderV2Input(); orderInput: OrderListDto = new OrderListDto(); // createInput: CreateOrderV2Input = new CreateOrderV2Input(); pickupDetailInput: DestinationDetailListDto = new DestinationDetailListDto(); pickupContactInput: ContactListDto = new ContactListDto(); pickupContactAddress: AddressListDto = new AddressListDto; destinationDetailInput: DestinationDetailListDto = new DestinationDetailListDto; destinationContactInput: ContactListDto = new ContactListDto; destinationContactAddress: AddressListDto = new AddressListDto; locationFilter: LocationListDto = new LocationListDto(); filteredLocation: any; tagTypeFilter: TagTypeListDto = new TagTypeListDto(); filteredTagType: any; tag: TagListDto = new TagListDto(); pickupEmail: EmailListDto = new EmailListDto() pickupPhone: PhoneListDto = new PhoneListDto() deliveryEmail: EmailListDto = new EmailListDto() deliveryPhone: EmailListDto = new EmailListDto() pickupEta: Date; deliveryEta: Date; pickUpTo: Date; pickUpFrom: Date; deliveryFrom: Date; deliveryTo: Date; pickuppostalId: PostalCodeInAddressListDto = new PostalCodeInAddressListDto(); destinationpostalId: PostalCodeInAddressListDto = new PostalCodeInAddressListDto(); noteInput: NoteListDto = new NoteListDto(); orderPackages: Array = []; packages: any = []; packageFilter: ShippingPackageListDto = new ShippingPackageListDto(); filteredCountries: any; filteredCities: any; filteredStates: any; filteredPostal: any; filteredDestinationCountries: any; filteredDestinationStates: any; filteredDestinationCities: any; filteredDestinationPostal: any; filteredPackages: any; pickupFilterChange = false; destinationFilterChange = false; orderPackageInput: Object = new Object(); addPackage: any; packageQuantity: number; packageNameMessageError: boolean; packageQtyMessageError: boolean; locationInput: any; id: any; toAdd: OrderPackageListDto; toAddP: ShippingPackageListDto; selectedLocation: any; selectedTagType: any; selectedPickupCountry: any; selectedPickupPostalCode: any; selectedPickupState: any; selectedPickupCity: any; selectedDestinationCountry: any; selectedDestinationPostalCode: any; selectedDestinationState: any; selectedDestinationCity: any; selectedPickupPostalCodeId: number; filteredPackage: any; constructor( injector: Injector, private _orderAppService: OrderServiceProxy, private _locationAppService: LocationServiceProxy, private _tagTypeAppService: TagTypeServiceProxy, private _postalCodeService: PostalCodeServiceProxy, // private _cityService: CityServiceProxy, // private _countryService: CountryServiceProxy, // private _stateService: StateServiceProxy, private _packageService: ShippingPackageServiceProxy, private _orderPackageService: OrderPackageServiceProxy, private route: ActivatedRoute, private router: Router ) { super(injector); } ngOnInit() { // this.ktwizard2(); this.route.paramMap.subscribe(params => { this.id = params.get("id") }) this.packages = new ShippingPackageListDto(); this.packageQuantity = undefined; this._orderAppService.getOrderForUpdate(this.id) .pipe() .subscribe(result => { this.orderInput = result; this.pickupDetailInput = result.pickupDetail; this.pickupContactInput = result.pickupDetail.contact; this.pickupContactAddress = result.pickupDetail.contact.address; this.destinationDetailInput = result.deliveryDetail; this.destinationContactInput = result.deliveryDetail.contact; this.destinationContactAddress = result.deliveryDetail.contact.address; this.locationFilter = result.location; this.pickupEmail = result.pickupDetail.contact.emails; this.pickupPhone = result.pickupDetail.contact.phones; this.deliveryEmail = result.deliveryDetail.contact.emails; this.deliveryPhone = result.deliveryDetail.contact.phones; this.pickupEta = moment.utc(result.pickupDetail.etaEnd).toDate(); this.pickUpTo = moment.utc(result.pickupDetail.etaEnd).toDate(); this.pickUpFrom = moment.utc(result.pickupDetail.etaStart).toDate(); this.deliveryEta = moment.utc(result.deliveryDetail.etaEnd).toDate(); this.deliveryTo = moment.utc(result.deliveryDetail.etaEnd).toDate(); this.deliveryFrom = moment.utc(result.deliveryDetail.etaStart).toDate(); this.pickuppostalId = result.pickupDetail.contact.address.postalCode; this.destinationpostalId = result.deliveryDetail.contact.address.postalCode; this.selectedLocation = result.location.name; this.selectedPickupPostalCode = result.pickupDetail.contact.address.postalCode.value; this.selectedPickupPostalCodeId = result.pickupDetail.contact.address.postalCode.id; this.selectedDestinationPostalCode = result.deliveryDetail.contact.address.postalCode.value; this.noteInput = result.note; this.orderPackages = result.orderPackages; $('.kt-select2').select2({ placeholder: "Select.." }); // this.filterLocation(); this.filterTagType(); this.filterPostal(); // this.filterPackage(); }); var that = this; $("#location").change(function () { this.selectedLocation = $("#location option:selected").text(); $("#selectedLocation").text("select"); }); $("#TagType").change(function () { this.selectedTagType = $("#TagType option:selected").text(); $("#selectedTagType").text(this.selectedTagType); }); $("#pickupCountryInput").change(function () { this.selectedPickupCountry = $("#pickupCountryInput option:selected").text(); $("#selectedPickupCountry").text(this.selectedPickupCountry); }); $("#pickupPostalInput").change(function () { this.selectedPickupPostalCode = $("#pickupPostalInput option:selected").text(); $("#selectedPickupPostalCode").text(this.selectedPickupPostalCode); this.selectedPickupPostalCode = this.selectedPickupPostalCode.trim(); that.pickupFilterChange = true; that.filterCountries(this.selectedPickupPostalCode) that.filterStates(this.selectedPickupPostalCode) that.filterCities(this.selectedPickupPostalCode) }); $("#pickupStateInput").change(function () { this.selectedPickupState = $("#pickupStateInput option:selected").text(); $("#selectedPickupState").text(this.selectedPickupState); }); $("#pickupCityInput").change(function () { this.selectedPickupCity = $("#pickupCityInput option:selected").text(); $("#selectedPickupCity").text(this.selectedPickupCity); }); $("#destinationCountryInput").change(function () { this.selectedDestinationCountry = $("#destinationCountryInput option:selected").text(); $("#selectedDestinationCountry").text(this.selectedDestinationCountry); }); $("#destinationPostalInput").change(function () { this.selectedDestinationPostalCode = $("#destinationPostalInput option:selected").text(); $("#selectedDestinationPostalCode").text(this.selectedDestinationPostalCode); this.selectedDestinationPostalCode = this.selectedDestinationPostalCode.trim() that.destinationFilterChange = true that.filterDestinationCountries(this.selectedDestinationPostalCode) that.filterDestinationStates(this.selectedDestinationPostalCode) that.filterDestinationCities(this.selectedDestinationPostalCode) }); $("#destinationStateInput").change(function () { this.selectedDestinationState = $("#destinationStateInput option:selected").text(); $("#selectedDestinationState").text(this.selectedDestinationState); }); $("#destinationCityInput").change(function () { this.selectedDestinationCity = $("#destinationCityInput option:selected").text(); $("#selectedDestinationCity").text(this.selectedDestinationCity); }); var KTWizard2 = function () { var e, r, i; $(".pickUpFrom input").attr("name", "pickUpFrom"); $(".pickUpTo input").attr("name", "pickUpTo"); // $(".pickUpTo").attr("name","pickUpTo"); // $(".pickUpTo").attr("name","pickUpTo"); return { init: function () { var t; KTUtil.get("kt_wizard_v2"), e = $("#kt_form"), (i = new KTWizard("kt_wizard_v2", { startStep: 1, clickableSteps: !0 })).on("beforeNext", function (e) { !0 !== r.form() && e.stop() }), i.on("beforePrev", function (e) { !0 !== r.form() && e.stop() }), i.on("change", function (e) { KTUtil.scrollTop() }), r = e.validate({ ignore: ":hidden", rules: { pickupFirstNameInput: { required: !0 }, pickupLastNameInput: { required: !0 }, pickupEmailInput: { required: !0, email: !0 }, PickupEta: { required: !0 }, pickUpFrom: { required: !0 }, pickUpTo: { required: !0 } }, invalidHandler: function (e, r) { KTUtil.scrollTop(), swal.fire({ title: "", text: "There are some errors in your submission. Please correct them.", type: "error", confirmButtonClass: "btn btn-secondary" }) }, submitHandler: function (e) { } }) } } }(); jQuery(document).ready(function () { KTWizard2.init() }); } ngAfterViewInit() { } save(): void { this.saving = true; this.forUpdateInput.id = this.orderInput.id; this.forUpdateInput.userId = this.orderInput.user.id; this.forUpdateInput.location = this.locationFilter; this.forUpdateInput.location.id = Number((document.getElementById('location')).value); this.forUpdateInput.pickupDetails = this.pickupDetailInput; this.forUpdateInput.pickupDetails.contact.address = this.pickupContactAddress; this.forUpdateInput.pickupDetails.contact.address.postalCode.id = Number((document.getElementById('pickupPostalInput')).value); // this.forUpdateInput.pickupDetails.contact.address.city.id = this.pickupcityId.id; // this.forUpdateInput.pickupDetails.contact.address.country.id = this.pickupcountryId.id; // this.forUpdateInput.pickupDetails.contact.address.state.id = this.pickupstateId.id; // this.forUpdateInput.pickupDetails.contact.address.postalCode.id = this.pickuppostalId.id; this.forUpdateInput.pickupDetails.contact.emails = this.pickupEmail; this.forUpdateInput.pickupDetails.contact.phones = this.pickupPhone; this.forUpdateInput.pickupDetails.etaStart = moment.utc(moment(this.pickupEta).format('YYYY-MM-DD') + ' ' + moment(this.pickUpFrom).format('HH:mm:ss')); this.forUpdateInput.pickupDetails.etaEnd = moment.utc(moment(this.pickupEta).format('YYYY-MM-DD') + ' ' + moment(this.pickUpTo).format('HH:mm:ss')); this.forUpdateInput.deliveryDetails = this.destinationDetailInput; this.forUpdateInput.deliveryDetails.contact.address = this.destinationContactAddress; this.forUpdateInput.deliveryDetails.contact.address.postalCode.id = Number((document.getElementById('destinationPostalInput')).value); // this.forUpdateInput.deliveryDetails.contact.address.city.id = this.destinationcityId.id; // this.forUpdateInput.deliveryDetails.contact.address.country.id = this.destinationcountryId.id; // this.forUpdateInput.deliveryDetails.contact.address.state.id = this.destinationstateId.id; // this.forUpdateInput.deliveryDetails.contact.address.postalCode.id = this.destinationpostalId.id; this.forUpdateInput.deliveryDetails.contact.emails = this.deliveryEmail; this.forUpdateInput.deliveryDetails.contact.phones = this.deliveryPhone; this.forUpdateInput.deliveryDetails.etaStart = moment.utc(moment(this.deliveryEta).format('YYYY-MM-DD') + ' ' + moment(this.deliveryFrom).format('HH:mm:ss')); this.forUpdateInput.deliveryDetails.etaEnd = moment.utc(moment(this.deliveryEta).format('YYYY-MM-DD') + ' ' + moment(this.deliveryTo).format('HH:mm:ss')); this.forUpdateInput.note = this.noteInput; this.forUpdateInput.tag = this.tag; this.forUpdateInput.tag.tagType.id = Number((document.getElementById('TagType')).value); this.forUpdateInput.orderPackages = this.orderPackages; this._orderAppService.updateOrderV2(this.forUpdateInput) .pipe(finalize(() => { this.saving = false; })) .subscribe(() => { this.notify.info(this.l('SavedSuccessfully')); this.saving = false; this.router.navigateByUrl("/app/sprintship/order"); }); } filterLocation(): void { this._locationAppService.getLocation( undefined, undefined, undefined, undefined, undefined, undefined, undefined ).subscribe(result => { this.filteredLocation = result.items; }); } filterTagType(): void { this._tagTypeAppService.getTagType( undefined, undefined, undefined, undefined, undefined ).subscribe(result => { this.filteredTagType = result.items; }); } filterPackage(): void { this._packageService.getShippingPackage( undefined, undefined, undefined, undefined, undefined, undefined ).subscribe(result => { this.filteredPackage = result.items; }); } async addPackages(): Promise { var packages_id = Number((document.getElementById('packages')).value); if ((packages_id === 0 || packages_id == null) && (this.packageQuantity === 0 || this.packageQuantity == null)) { this.packageNameMessageError = true; this.packageQtyMessageError = true; } else if ((packages_id === 0 || packages_id == null)) { this.packageNameMessageError = true; this.packageQtyMessageError = false; } else if ((this.packageQuantity === 0 || this.packageQuantity == null)) { this.packageNameMessageError = false; this.packageQtyMessageError = true; } else { this.toAdd = new OrderPackageListDto(); this.toAddP = new ShippingPackageListDto(); this.packages = new ShippingPackageListDto(); // this.packageQuantity = undefined; this.toAdd.packageId = Number((document.getElementById('packages')).value); this.toAdd.quantity = this.packageQuantity; this.getpackageName(this.toAdd.packageId); // this.toAddP.name = String(await this.getpackageName(this.toAdd.packageId)); // this.toAdd.packageId = this.packages.id; // this.toAddP.name = this.packages.name; // this.toAdd.quantity = this.packageQuantity; this.toAdd.package = this.toAddP; console.log(this.toAdd); this.packages = ''; this.packageQuantity = undefined; this.orderPackages.push(this.toAdd); } }; removePackage(toDel): void { _.remove(this.orderPackages, toDel); this._orderPackageService.deleteOrderPackage(toDel.id).subscribe(result => { }); }; getpackageName(id) { this._packageService.getShippingPackage( id, undefined, undefined, undefined, undefined, undefined ).subscribe(result => { this.toAddP.name = result.items[0].name; console.log(result); }); } // Class definition filterPostal(): void { this._postalCodeService.getDistinctPostalCode( undefined ).subscribe(result => { this.filteredPostal = result.items; this.filteredDestinationPostal = result.items }); } filterCountries(postalCode): void { this._postalCodeService.getDistinctCountry( postalCode, undefined ).subscribe(result => { this.filteredCountries = result.items; $("#selectedPickupCountry").text(this.filteredCountries[0]['country']); }); } filterCities(postalCode): void { this._postalCodeService.getDistinctCity( postalCode, undefined ).subscribe(result => { this.filteredCities = result.items; $("#selectedPickupCity").text(this.filteredCities[0]['city']); }); } filterStates(postalCode): void { this._postalCodeService.getDistinctState( postalCode, undefined ).subscribe(result => { this.filteredStates = result.items; $("#selectedPickupState").text(this.filteredStates[0]['state']); }); } filterDestinationCountries(postalCode): void { this._postalCodeService.getDistinctCountry( postalCode, undefined ).subscribe(result => { this.filteredDestinationCountries = result.items; $("#selectedDestinationCountry").text(this.filteredDestinationCountries[0]['country']) }); } filterDestinationCities(postalCode): void { this._postalCodeService.getDistinctCity( postalCode, undefined ).subscribe(result => { this.filteredDestinationCities = result.items; $("#selectedDestinationCity").text(this.filteredDestinationCities[0]['city']) }); } filterDestinationStates(postalCode): void { this._postalCodeService.getDistinctState( postalCode, undefined ).subscribe(result => { this.filteredDestinationStates = result.items; $("#selectedDestinationState").text(this.filteredDestinationStates[0]['state']) }); } }