Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';
- import {
- AfterViewInit,
- ChangeDetectionStrategy,
- ChangeDetectorRef,
- Component,
- Inject,
- OnDestroy,
- OnInit,
- ViewChild,
- } from '@angular/core';
- import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
- import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
- import { Router } from '@angular/router';
- import { BehaviorSubject, Observable } from 'rxjs';
- import { filter, take, takeUntil } from 'rxjs/operators';
- import { CreateItemCodeComponent } from '../../asn/create-item-code/create-item-code.component';
- import { Customer } from '../../interface/customer.interface';
- import { ItemCode } from '../../interface/item-code.interface';
- import { Location } from '../../interface/location.interface';
- import {
- ArrangeProcessList,
- ArrangeProcessQCList,
- CreateProcess,
- ProcessType,
- } from '../../interface/master-process.interface';
- import { ModeOfDelivery } from '../../interface/mode-of-delivery.interface';
- import { CustomerApiService } from '../../services/customer-api.service';
- import { ItemCodeApiService } from '../../services/item-code.service';
- import { LocationApiService } from '../../services/location-api.service';
- import { MasterApiService } from '../../services/master-api.service';
- import { OperationTypeApiService } from '../../services/operation-type-api.service';
- import { WarehouseOperatorsApiService } from '../../services/warehouse-operators-api.service';
- import { SnackBarService } from '../../shared/snackbar.service';
- import { DestroyableComponent } from '../../utilities/destroyable.component';
- import { PutAwaySettingsComponent } from '../put-away-settings/put-away-settings.component';
- import { QcSettingsComponent } from '../qc-settings/qc-settings.component';
- import { VasSettingsComponent } from '../vas-settings/vas-settings.component';
- @Component({
- selector: 'warehouse-app-create-master-process',
- templateUrl: './create-master-process.component.html',
- styleUrls: ['../../shared/custom-scss/bg-light.scss'],
- styles: ['.max-height { max-height: 60vh; }'],
- changeDetection: ChangeDetectionStrategy.OnPush,
- })
- export class CreateMasterProcessComponent extends DestroyableComponent implements OnInit, OnDestroy, AfterViewInit {
- @ViewChild('itemSelectDropDown') public itemSelectDropDown;
- public locations$$ = new BehaviorSubject<Location[]>(null);
- public customers$$ = new BehaviorSubject<Customer[]>(null);
- public operators$$ = new BehaviorSubject(null);
- public itemCodes$$ = new BehaviorSubject<ItemCode[]>(null);
- public submitting$$ = new BehaviorSubject(false);
- public filteredItemCodes$$ = new BehaviorSubject<ItemCode[]>(null);
- public typeOfOperation$: Observable<ModeOfDelivery[]>;
- public selectAllLocationsControl = new FormControl('', []);
- public selectAllItemCodes = new FormControl(false, []);
- public filterForm: FormGroup;
- public itemCodeSearch = new FormControl(null, []);
- public count = 0;
- public arrangeProcessList = new Array<ArrangeProcessList>();
- public list = ['QC', 'VAS'];
- public modes = {} as { modeOfDeliveryId: number; criteria: string; sortItems: boolean };
- public notItemChecks = ['extraFields', 'minPercentagePacketCheck', 'modeOfDeliveryIds'];
- constructor(
- @Inject(MAT_DIALOG_DATA) public data,
- private fb: FormBuilder,
- private masterApiService: MasterApiService,
- private customerApiService: CustomerApiService,
- private warehouseOperatorsApiService: WarehouseOperatorsApiService,
- private locationApiService: LocationApiService,
- private itemCodeApiService: ItemCodeApiService,
- private snackBar: SnackBarService,
- private cdr: ChangeDetectorRef,
- private dialog: MatDialog,
- private matDialogRef: MatDialogRef<CreateMasterProcessComponent>,
- private router: Router,
- private operationTypeApiService: OperationTypeApiService,
- ) {
- super();
- }
- public ngOnInit(): void {
- console.log(this.data);
- this.createForm();
- this.customerApiService.customers$
- .pipe(takeUntil(this.destroy$$))
- .subscribe((customer) => this.customers$$.next([...customer]));
- this.locationApiService.locations$
- .pipe(takeUntil(this.destroy$$))
- .subscribe((location) => this.locations$$.next(location));
- this.arrangeProcessList = this.data?.process?.processDetails?.length
- ? this.data?.process?.processDetails
- : [
- {
- process: 'incoming',
- ordering: 1,
- },
- {
- process: 'put-away',
- ordering: 2,
- },
- {
- process: 'dispatch',
- ordering: 3,
- },
- ];
- this.modes = {
- criteria: this.data?.process?.criteria,
- modeOfDeliveryId: this.data?.process?.modeOfDeliveryId,
- sortItems: null,
- };
- if (this.data?.process?.processDetails?.length) {
- this.arrangeProcessList.forEach((process) => {
- if (process.process === 'QC' && process?.qc?.visualInspection) {
- this.modes.sortItems = true;
- }
- });
- }
- this.warehouseOperatorsApiService
- .getWarehouseOperators$()
- .pipe(takeUntil(this.destroy$$))
- .subscribe((operator) => {
- this.operators$$.next(operator);
- if (operator.length === 1 && !this.data?.warehouseId) {
- this.filterForm.get('operatorId').setValue(operator[0].tcUserAuthId);
- }
- });
- this.itemCodeApiService.itemCodes$.pipe(takeUntil(this.destroy$$)).subscribe((itemCodes) => {
- this.itemCodes$$.next([...itemCodes]);
- this.filteredItemCodes$$.next([...(itemCodes?.length ? itemCodes : [])]);
- });
- this.filterForm
- .get('itemCodeIds')
- .valueChanges.pipe(
- filter((itemCodeIds) => !!itemCodeIds?.length),
- takeUntil(this.destroy$$),
- )
- .subscribe((itemCodes) => {
- this.selectAllItemCodes.setValue(itemCodes?.length === this.itemCodes$$.value?.length, { emitEvent: false });
- });
- this.filterForm
- .get('customerId')
- .valueChanges.pipe(
- filter((customerId) => !!customerId),
- takeUntil(this.destroy$$),
- )
- .subscribe((customerId) => {
- if (this.filterForm.value.itemCodeIds && !this.data?.itemCodeIds?.length) {
- this.filterForm.get('itemCodeIds').setValue(null);
- }
- this.setItemCode(customerId, customerId === this.data.customerId ? this.data?.itemCodeIds : null);
- });
- this.itemCodeSearch.valueChanges.pipe(takeUntil(this.destroy$$)).subscribe((searchText: string) => {
- this.filteredItemCodes$$.next([
- ...(searchText
- ? this.itemCodes$$.value.filter(
- (itemCode) =>
- itemCode.code.toLowerCase().includes(searchText.toLowerCase()) ||
- itemCode.description.toLowerCase().includes(searchText.toLowerCase()),
- )
- : this.itemCodes$$.value),
- ]);
- });
- this.typeOfOperation$ = this.operationTypeApiService.operationTypes$;
- this.filterForm.patchValue({
- customerId: this.data?.customerId,
- operatorId: this.data?.warehouseId,
- locationIds: [this.data?.origin],
- incomingOperationsId: this.data?.incomingOperationsId,
- outgoingOperationsId: this.data?.outgoingOperationsId,
- name: this.data?.process?.name,
- description: this.data?.process?.description,
- });
- this.selectAllItemCodes.valueChanges.pipe(takeUntil(this.destroy$$)).subscribe((value) => {
- this.filterForm
- .get('itemCodeIds')
- .setValue(value ? [...(this.itemCodes$$.value.map((itemCode) => itemCode.id) || [])] : []);
- });
- }
- public ngOnDestroy() {
- super.ngOnDestroy();
- }
- public ngAfterViewInit() {
- if (this.data?.itemCodeIds) {
- // this.setItemCode(this.data?.customerId, [...this.data?.itemCodeIds]);
- }
- }
- private createForm() {
- this.filterForm = this.fb.group({
- incomingOperationsId: [null, [Validators.required]],
- outgoingOperationsId: [null, [Validators.required]],
- operatorId: [null, []],
- customerId: [null, Validators.required],
- locationIds: [null, [Validators.required]],
- itemCodeIds: [null, []],
- name: [null, [Validators.required]],
- description: [null, Validators.required],
- });
- }
- // tslint:disable-next-line:cyclomatic-complexity
- public drop(event: CdkDragDrop<any>) {
- if (event.previousContainer === event.container) {
- if (['put-away', 'incoming', 'dispatch'].includes(event.container.data[event.previousIndex].process)) {
- this.snackBar.showError('Default process cannot be changed');
- } else {
- if (['incoming', 'dispatch'].includes(event.container.data[event.currentIndex].process)) {
- this.snackBar.showError('Default process cannot be changed');
- } else {
- moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
- const index = event.container.data.indexOf(event.container.data.find((data) => data.process === 'put-away'));
- if (event.currentIndex < index) {
- if (this.data.isFromDO) {
- const action = event.container.data[event.currentIndex];
- event.container.data.splice(event.currentIndex, 1);
- event.container.data.splice(event.previousIndex, 0, action);
- this.snackBar.showError('Can not change incoming process in DO');
- }
- }
- }
- }
- } else {
- if (event.currentIndex === 0) {
- event.currentIndex = event.currentIndex + 1;
- } else if (event.currentIndex === event.container.data.length) {
- event.currentIndex = event.currentIndex - 1;
- }
- transferArrayItem(event.previousContainer.data, event.container.data, event.previousIndex, event.currentIndex);
- if (event.container.connectedTo[0].id === 'list') {
- const index = event.container.data.indexOf(event.container.data.find((data) => data.process === 'put-away'));
- if (event.currentIndex < index) {
- if (['QC', 'VAS'].indexOf(event.container.data[event.currentIndex]) !== -1) {
- if (!this.data.isFromDO) {
- this.openSettingDialog(event.container.data[event.currentIndex], event.currentIndex);
- switch (event.container.data[event.currentIndex]) {
- case 'VAS':
- this.list.push('VAS');
- break;
- case 'QC':
- this.list.push('QC');
- }
- } else {
- // if (event.currentIndex > this.putAwayIndex) {
- // this.openSettingDialog(event.container.data[event.currentIndex], event.currentIndex);
- // switch (event.container.data[event.currentIndex]) {
- // case 'VAS':
- // this.list.push('VAS');
- // break;
- // case 'QC':
- // this.list.push('QC');
- // }
- // } else {
- // if (this.data.isFromDO && event.currentIndex < this.putAwayIndex) {
- // this.snackBar.showError('Can not change Incoming Process in DO');
- // this.list.push(event.container.data[event.currentIndex]);
- // event.container.data.splice(event.currentIndex, 1);
- // }
- // }
- if (this.data.isFromDO && event.currentIndex < this.putAwayIndex) {
- this.snackBar.showError('Can not change Incoming Process in DO');
- this.list.push(event.container.data[event.currentIndex]);
- event.container.data.splice(event.currentIndex, 1);
- }
- }
- // } else if (this.data.isFromDO && event.currentIndex > this.putAwayIndex) {
- // this.openSettingDialog(event.container.data[event.currentIndex], event.currentIndex);
- // switch (event.container.data[event.currentIndex]) {
- // case 'VAS':
- // this.list.push('VAS');
- // break;
- // case 'QC':
- // this.list.push('QC');
- // }
- // } else {
- // if (this.data.isFromDO && event.currentIndex < this.putAwayIndex) {
- // this.snackBar.showError('Can not change Incoming Process in DO');
- // this.list.push(event.container.data[event.currentIndex]);
- // event.container.data.splice(event.currentIndex, 1);
- // }
- // }
- }
- if (!this.data.isFromDO) {
- this.arrangeProcessList[event.currentIndex] = {
- process: event.container.data[event.currentIndex].includes('VAS')
- ? 'VAS ' + this.vasCount()
- : event.container.data[event.currentIndex],
- ordering: event.currentIndex + 1,
- };
- }
- } else {
- // const action = event.container.data[event.currentIndex];
- //
- // if (
- // (outgoingQcExists > 1 && event.container.data[event.currentIndex] === 'QC') ||
- // (outgoingVasExists > 1 && event.container.data[event.currentIndex] === 'VAS')
- // ) {
- // event.container.data.splice(event.currentIndex, 1);
- // this.snackBar.showError('Outgoing Process can contain only one ' + action);
- // this.list.push(action);
- // } else {
- if (['QC', 'VAS'].indexOf(event.container.data[event.currentIndex]) !== -1) {
- this.openSettingDialog(event.container.data[event.currentIndex], event.currentIndex);
- switch (event.container.data[event.currentIndex]) {
- case 'VAS':
- this.list.push('VAS');
- break;
- case 'QC':
- this.list.push('QC');
- }
- }
- this.arrangeProcessList[event.currentIndex] = {
- process: event.container.data[event.currentIndex].includes('VAS')
- ? 'VAS'
- : event.container.data[event.currentIndex],
- ordering: event.currentIndex + 1,
- };
- // }
- }
- } else {
- const process: string = (event.container.data[event.currentIndex] as any).process;
- if (!this.data?.isFromDO) {
- if (process.includes('VAS') || process.includes('QC')) {
- this.list.splice(event.currentIndex, 1);
- } else {
- this.arrangeProcessList.splice(event.previousIndex, 0, {
- process,
- ordering: event.previousIndex + 1,
- });
- this.list.splice(event.currentIndex, 1);
- this.snackBar.showError('Cannot change default process');
- }
- } else {
- if (process.includes('VAS') || process.includes('QC')) {
- this.list.splice(event.currentIndex, 1);
- } else {
- this.arrangeProcessList.splice(event.previousIndex, 0, {
- process,
- ordering: event.previousIndex + 1,
- });
- this.list.splice(event.currentIndex, 1);
- this.snackBar.showError('Cannot change default process');
- }
- // if (event.previousIndex < this.putAwayIndex) {
- // this.arrangeProcessList.splice(event.previousIndex, 0, {
- // process,
- // ordering: event.previousIndex + 1
- // });
- // this.list.splice(event.currentIndex, 1);
- // this.snackBar.showError('Can not change incoming process in DO');
- // } else {
- // if (process.includes('VAS') || process.includes('QC')) {
- // this.list.splice(event.currentIndex, 1);
- // } else {
- // this.arrangeProcessList.splice(event.previousIndex, 0, {
- // process,
- // ordering: event.previousIndex + 1,
- // });
- // this.list.splice(event.currentIndex, 1);
- // this.snackBar.showError('Cannot change default process');
- // }
- // }
- }
- }
- }
- this.arrangeProcessList = [
- ...this.arrangeProcessList.map((process, index) => {
- return {
- ...process,
- ordering: index + 1,
- };
- }),
- ];
- }
- public vasCount(): number {
- let count = Math.max(
- ...(this.arrangeProcessList.reduce(
- (acc, cur, index) => {
- if (typeof cur === 'object' && cur.process.includes('VAS ')) {
- return [...acc, +cur.process.split(' ')[1]];
- } else {
- return [...acc];
- }
- },
- [0],
- ) || [0]),
- );
- return ++count;
- }
- private setItemCode(customerId: string, itemCodeIds?: number[]) {
- this.itemCodeApiService.setCustomerId(customerId);
- if (itemCodeIds?.length) {
- this.filterForm.get('itemCodeIds').setValue(null);
- this.itemCodes$$.pipe(takeUntil(this.destroy$$)).subscribe((itemCodes) => {
- this.filterForm.get('itemCodeIds').setValue([...itemCodeIds]);
- });
- } else {
- this.itemCodes$$.pipe(takeUntil(this.destroy$$)).subscribe((itemCodes) => {
- this.filterForm.get('itemCodeIds').setValue(itemCodes?.length ? [+itemCodes[0].id] : []);
- });
- }
- }
- public toggleAllLocations(control?: FormControl) {
- if (control) {
- control.setValue(!control.value);
- this.filterForm.get('locationIds').setValue(control.value ? [...this.locations$$.value.map((v) => +v.id)] : []);
- } else {
- this.filterForm
- .get('locationIds')
- .setValue(!this.selectAllLocationsControl.value ? [...this.locations$$.value.map((v) => +v.id)] : []);
- }
- }
- public openSettingDialog(processName, index: number) {
- if (processName.toLowerCase().includes('vas')) {
- this.dialog
- .open(VasSettingsComponent, {
- data: this.arrangeProcessList[index],
- })
- .afterClosed()
- .pipe(take(1))
- .subscribe((vasProcess) => {
- if (vasProcess?.length && !vasProcess?.process) {
- const count = this.vasCount() - 1;
- this.arrangeProcessList[index] = {
- process: `VAS ${count > 0 ? count : ''}`,
- ordering: index + 1,
- vas: vasProcess,
- };
- } else if (vasProcess?.process) {
- const count = this.vasCount() - 1;
- this.arrangeProcessList[index] = {
- process: `VAS ${count > 0 ? count : ''}`,
- ordering: index + 1,
- vas: vasProcess.vas,
- };
- } else {
- this.arrangeProcessList.splice(index, 1);
- this.cdr.detectChanges();
- this.snackBar.showError('Empty VAS is not allowed');
- }
- });
- } else if (processName.toLowerCase().includes('qc')) {
- this.dialog
- .open(QcSettingsComponent, {
- data: this.arrangeProcessList[index],
- })
- .afterClosed()
- .pipe(take(1))
- .subscribe((qcCheckList) => {
- let qcItems = false;
- if (!qcCheckList?.process) {
- for (const item in qcCheckList) {
- if (!this.notItemChecks.includes(item)) {
- if (qcCheckList[item] === true) {
- qcItems = true;
- break;
- }
- }
- }
- } else {
- qcItems = true;
- }
- if (qcItems) {
- if (qcCheckList?.process) {
- index < this.putAwayIndex && this.modes.sortItems
- ? (qcCheckList.qc.visualInspection = true)
- : (qcCheckList.qc.visualInspection = qcCheckList.qc?.visualInspection);
- this.arrangeProcessList[index] = {
- process: processName,
- ordering: index + 1,
- qc: qcCheckList?.qc,
- };
- } else {
- index < this.putAwayIndex && this.modes.sortItems
- ? (qcCheckList.visualInspection = true)
- : (qcCheckList.visualInspection = qcCheckList.visualInspection);
- this.arrangeProcessList[index] = {
- process: processName,
- ordering: index + 1,
- qc: qcCheckList,
- };
- }
- } else {
- this.arrangeProcessList.splice(index, 1);
- this.cdr.detectChanges();
- this.snackBar.showError('Empty QC is not allowed');
- }
- });
- } else {
- this.dialog
- .open(PutAwaySettingsComponent, {
- data: this.modes,
- })
- .afterClosed()
- .pipe(take(1))
- .subscribe((strategies) => {
- if (strategies) {
- this.modes = { ...strategies };
- for (let j = index - 1; j >= 0; j--) {
- if (this.arrangeProcessList[j].process === 'QC') {
- this.arrangeProcessList[j].qc.visualInspection = this.modes?.sortItems;
- }
- }
- }
- });
- }
- }
- public get putAwayIndex(): number {
- const index = this.arrangeProcessList.indexOf(this.arrangeProcessList.find((data) => data.process === 'put-away'));
- return index;
- }
- public createProcess() {
- if (this.filterForm.valid) {
- if (this.modes?.modeOfDeliveryId && this.modes?.criteria) {
- this.submitting$$.next(true);
- delete this.modes?.sortItems;
- if (this.arrangeProcessList.length) {
- const requestBody: CreateProcess = {
- ...this.filterForm.value,
- ...this.modes,
- ...(this.data?.process?.id
- ? { processId: this.data.process.id }
- : this.data?.process?.processId
- ? { processId: this.data.process.processId }
- : {}),
- processDetails: [
- ...this.arrangeProcessList.map((list, index) => {
- const process = {
- ordering: index + 1,
- process: list.process.includes('VAS')
- ? 'VAS'
- : list.process === 'Incoming'
- ? list.process.toLowerCase()
- : list.process,
- ...(list?.id
- ? { processDetailsId: list?.id }
- : list?.processDetailsId
- ? { processDetailsId: list.processDetailsId }
- : {}),
- };
- if (process.process === ProcessType.VAS) {
- return {
- ...process,
- vas: list?.vas?.length
- ? list.vas.map((vas) => {
- return {
- ordering: vas.ordering,
- type: vas.type,
- description: vas.description,
- ...(vas?.id ? { vasId: vas.id } : vas?.vasId ? { vasId: vas?.vasId } : {}),
- };
- })
- : [],
- };
- } else if (process.process === ProcessType.QC) {
- const qcDetails = {
- ...process,
- qc: list?.qc
- ? {
- ...list.qc,
- ...(list.qc?.id ? { qcId: list.qc.id } : list.qc.qcId ? { qcId: list.qc.qcId } : {}),
- minPercentagePacketCheck: list.qc?.minPercentagePacketCheck
- ? list.qc?.minPercentagePacketCheck
- : list.qc?.minPercentageP,
- visualInspection: list?.qc?.visualInspection
- ? list?.qc?.visualInspection
- : list.qc?.visualInspecti,
- }
- : ({} as ArrangeProcessQCList),
- };
- delete qcDetails.qc?.createdAt;
- delete qcDetails.qc?.deletedAt;
- delete qcDetails.qc?.minPercentageP;
- delete qcDetails.qc?.processDetails;
- delete qcDetails?.qc?.visualInspecti;
- delete qcDetails.qc?.processDetailsId;
- delete qcDetails.qc?.processDeta;
- delete qcDetails.qc?.id;
- delete qcDetails.qc?.updatedAt;
- return qcDetails;
- } else {
- return process;
- }
- }),
- ],
- };
- delete requestBody.operatorId;
- if (this.data?.notFromMaster) {
- this.submitting$$.next(false);
- this.closeDialog(requestBody);
- return;
- }
- console.log(requestBody);
- this.masterApiService.createProcess$(requestBody).subscribe(
- (resp) => {
- this.submitting$$.next(false);
- this.snackBar.showMessage(this.data.isDialog ? 'Process Updated' : 'Process Created');
- this.data?.isDialog ? this.closeDialog() : this.router.navigate(['master', 'view-master-process']);
- },
- (error) => {
- this.submitting$$.next(false);
- },
- );
- } else {
- this.snackBar.showError('Please arrange the process before creating it');
- }
- } else {
- this.snackBar.showError('Select Storage Strategy in Put-away before saving process');
- }
- } else {
- this.snackBar.showError('Form is not valid. Please check and try again');
- this.filterForm.markAllAsTouched();
- }
- }
- private closeDialog(result?: CreateProcess) {
- this.matDialogRef.close(result);
- }
- public toggleAllItemCodes() {
- this.selectAllItemCodes.setValue(!this.selectAllItemCodes.value);
- }
- public openNewItemCodeModal() {
- const d = this.dialog.open(CreateItemCodeComponent, {
- data: {
- customerId: this.filterForm.value.customerId,
- itemCodes: this.itemCodes$$.value,
- },
- });
- d.afterClosed().subscribe((value) => {
- if (value) {
- this.setItemCode(this.filterForm.value.customerId, [+value.id]);
- this.filterForm.get('itemCodeIds').setValue([+value.id]);
- this.itemSelectDropDown.close();
- }
- });
- }
- public getSelectedItems(id: number) {
- if (this.filterForm.get('itemCodeIds')?.value?.length) {
- this.filterForm.get('itemCodeIds')?.setValue([...this.filterForm.get('itemCodeIds').value, id]);
- } else {
- this.filterForm.get('itemCodeIds')?.setValue([id]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement