Advertisement
ankit_saiyan

Create Master

Aug 9th, 2022
917
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';
  2. import {
  3.   AfterViewInit,
  4.   ChangeDetectionStrategy,
  5.   ChangeDetectorRef,
  6.   Component,
  7.   Inject,
  8.   OnDestroy,
  9.   OnInit,
  10.   ViewChild,
  11. } from '@angular/core';
  12. import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
  13. import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
  14. import { Router } from '@angular/router';
  15. import { BehaviorSubject, Observable } from 'rxjs';
  16. import { filter, take, takeUntil } from 'rxjs/operators';
  17. import { CreateItemCodeComponent } from '../../asn/create-item-code/create-item-code.component';
  18. import { Customer } from '../../interface/customer.interface';
  19. import { ItemCode } from '../../interface/item-code.interface';
  20. import { Location } from '../../interface/location.interface';
  21. import {
  22.   ArrangeProcessList,
  23.   ArrangeProcessQCList,
  24.   CreateProcess,
  25.   ProcessType,
  26. } from '../../interface/master-process.interface';
  27. import { ModeOfDelivery } from '../../interface/mode-of-delivery.interface';
  28. import { CustomerApiService } from '../../services/customer-api.service';
  29. import { ItemCodeApiService } from '../../services/item-code.service';
  30. import { LocationApiService } from '../../services/location-api.service';
  31. import { MasterApiService } from '../../services/master-api.service';
  32. import { OperationTypeApiService } from '../../services/operation-type-api.service';
  33. import { WarehouseOperatorsApiService } from '../../services/warehouse-operators-api.service';
  34. import { SnackBarService } from '../../shared/snackbar.service';
  35. import { DestroyableComponent } from '../../utilities/destroyable.component';
  36. import { PutAwaySettingsComponent } from '../put-away-settings/put-away-settings.component';
  37. import { QcSettingsComponent } from '../qc-settings/qc-settings.component';
  38. import { VasSettingsComponent } from '../vas-settings/vas-settings.component';
  39.  
  40. @Component({
  41.   selector: 'warehouse-app-create-master-process',
  42.   templateUrl: './create-master-process.component.html',
  43.   styleUrls: ['../../shared/custom-scss/bg-light.scss'],
  44.   styles: ['.max-height { max-height: 60vh; }'],
  45.   changeDetection: ChangeDetectionStrategy.OnPush,
  46. })
  47. export class CreateMasterProcessComponent extends DestroyableComponent implements OnInit, OnDestroy, AfterViewInit {
  48.   @ViewChild('itemSelectDropDown') public itemSelectDropDown;
  49.   public locations$$ = new BehaviorSubject<Location[]>(null);
  50.   public customers$$ = new BehaviorSubject<Customer[]>(null);
  51.   public operators$$ = new BehaviorSubject(null);
  52.   public itemCodes$$ = new BehaviorSubject<ItemCode[]>(null);
  53.   public submitting$$ = new BehaviorSubject(false);
  54.   public filteredItemCodes$$ = new BehaviorSubject<ItemCode[]>(null);
  55.   public typeOfOperation$: Observable<ModeOfDelivery[]>;
  56.  
  57.   public selectAllLocationsControl = new FormControl('', []);
  58.  
  59.   public selectAllItemCodes = new FormControl(false, []);
  60.   public filterForm: FormGroup;
  61.   public itemCodeSearch = new FormControl(null, []);
  62.  
  63.   public count = 0;
  64.   public arrangeProcessList = new Array<ArrangeProcessList>();
  65.   public list = ['QC', 'VAS'];
  66.   public modes = {} as { modeOfDeliveryId: number; criteria: string; sortItems: boolean };
  67.   public notItemChecks = ['extraFields', 'minPercentagePacketCheck', 'modeOfDeliveryIds'];
  68.  
  69.   constructor(
  70.     @Inject(MAT_DIALOG_DATA) public data,
  71.     private fb: FormBuilder,
  72.     private masterApiService: MasterApiService,
  73.     private customerApiService: CustomerApiService,
  74.     private warehouseOperatorsApiService: WarehouseOperatorsApiService,
  75.     private locationApiService: LocationApiService,
  76.     private itemCodeApiService: ItemCodeApiService,
  77.     private snackBar: SnackBarService,
  78.     private cdr: ChangeDetectorRef,
  79.     private dialog: MatDialog,
  80.     private matDialogRef: MatDialogRef<CreateMasterProcessComponent>,
  81.     private router: Router,
  82.     private operationTypeApiService: OperationTypeApiService,
  83.   ) {
  84.     super();
  85.   }
  86.  
  87.   public ngOnInit(): void {
  88.     console.log(this.data);
  89.  
  90.     this.createForm();
  91.  
  92.     this.customerApiService.customers$
  93.       .pipe(takeUntil(this.destroy$$))
  94.       .subscribe((customer) => this.customers$$.next([...customer]));
  95.  
  96.     this.locationApiService.locations$
  97.       .pipe(takeUntil(this.destroy$$))
  98.       .subscribe((location) => this.locations$$.next(location));
  99.  
  100.     this.arrangeProcessList = this.data?.process?.processDetails?.length
  101.       ? this.data?.process?.processDetails
  102.       : [
  103.           {
  104.             process: 'incoming',
  105.             ordering: 1,
  106.           },
  107.           {
  108.             process: 'put-away',
  109.             ordering: 2,
  110.           },
  111.           {
  112.             process: 'dispatch',
  113.             ordering: 3,
  114.           },
  115.         ];
  116.  
  117.     this.modes = {
  118.       criteria: this.data?.process?.criteria,
  119.       modeOfDeliveryId: this.data?.process?.modeOfDeliveryId,
  120.       sortItems: null,
  121.     };
  122.  
  123.     if (this.data?.process?.processDetails?.length) {
  124.       this.arrangeProcessList.forEach((process) => {
  125.         if (process.process === 'QC' && process?.qc?.visualInspection) {
  126.           this.modes.sortItems = true;
  127.         }
  128.       });
  129.     }
  130.  
  131.     this.warehouseOperatorsApiService
  132.       .getWarehouseOperators$()
  133.       .pipe(takeUntil(this.destroy$$))
  134.       .subscribe((operator) => {
  135.         this.operators$$.next(operator);
  136.         if (operator.length === 1 && !this.data?.warehouseId) {
  137.           this.filterForm.get('operatorId').setValue(operator[0].tcUserAuthId);
  138.         }
  139.       });
  140.  
  141.     this.itemCodeApiService.itemCodes$.pipe(takeUntil(this.destroy$$)).subscribe((itemCodes) => {
  142.       this.itemCodes$$.next([...itemCodes]);
  143.       this.filteredItemCodes$$.next([...(itemCodes?.length ? itemCodes : [])]);
  144.     });
  145.  
  146.     this.filterForm
  147.       .get('itemCodeIds')
  148.       .valueChanges.pipe(
  149.         filter((itemCodeIds) => !!itemCodeIds?.length),
  150.         takeUntil(this.destroy$$),
  151.       )
  152.       .subscribe((itemCodes) => {
  153.         this.selectAllItemCodes.setValue(itemCodes?.length === this.itemCodes$$.value?.length, { emitEvent: false });
  154.       });
  155.  
  156.     this.filterForm
  157.       .get('customerId')
  158.       .valueChanges.pipe(
  159.         filter((customerId) => !!customerId),
  160.         takeUntil(this.destroy$$),
  161.       )
  162.       .subscribe((customerId) => {
  163.         if (this.filterForm.value.itemCodeIds && !this.data?.itemCodeIds?.length) {
  164.           this.filterForm.get('itemCodeIds').setValue(null);
  165.         }
  166.         this.setItemCode(customerId, customerId === this.data.customerId ? this.data?.itemCodeIds : null);
  167.       });
  168.  
  169.     this.itemCodeSearch.valueChanges.pipe(takeUntil(this.destroy$$)).subscribe((searchText: string) => {
  170.       this.filteredItemCodes$$.next([
  171.         ...(searchText
  172.           ? this.itemCodes$$.value.filter(
  173.               (itemCode) =>
  174.                 itemCode.code.toLowerCase().includes(searchText.toLowerCase()) ||
  175.                 itemCode.description.toLowerCase().includes(searchText.toLowerCase()),
  176.             )
  177.           : this.itemCodes$$.value),
  178.       ]);
  179.     });
  180.  
  181.     this.typeOfOperation$ = this.operationTypeApiService.operationTypes$;
  182.  
  183.     this.filterForm.patchValue({
  184.       customerId: this.data?.customerId,
  185.       operatorId: this.data?.warehouseId,
  186.       locationIds: [this.data?.origin],
  187.       incomingOperationsId: this.data?.incomingOperationsId,
  188.       outgoingOperationsId: this.data?.outgoingOperationsId,
  189.       name: this.data?.process?.name,
  190.       description: this.data?.process?.description,
  191.     });
  192.  
  193.     this.selectAllItemCodes.valueChanges.pipe(takeUntil(this.destroy$$)).subscribe((value) => {
  194.       this.filterForm
  195.         .get('itemCodeIds')
  196.         .setValue(value ? [...(this.itemCodes$$.value.map((itemCode) => itemCode.id) || [])] : []);
  197.     });
  198.   }
  199.  
  200.   public ngOnDestroy() {
  201.     super.ngOnDestroy();
  202.   }
  203.  
  204.   public ngAfterViewInit() {
  205.     if (this.data?.itemCodeIds) {
  206.       // this.setItemCode(this.data?.customerId, [...this.data?.itemCodeIds]);
  207.     }
  208.   }
  209.  
  210.   private createForm() {
  211.     this.filterForm = this.fb.group({
  212.       incomingOperationsId: [null, [Validators.required]],
  213.       outgoingOperationsId: [null, [Validators.required]],
  214.       operatorId: [null, []],
  215.       customerId: [null, Validators.required],
  216.       locationIds: [null, [Validators.required]],
  217.       itemCodeIds: [null, []],
  218.       name: [null, [Validators.required]],
  219.       description: [null, Validators.required],
  220.     });
  221.   }
  222.  
  223.   // tslint:disable-next-line:cyclomatic-complexity
  224.   public drop(event: CdkDragDrop<any>) {
  225.     if (event.previousContainer === event.container) {
  226.       if (['put-away', 'incoming', 'dispatch'].includes(event.container.data[event.previousIndex].process)) {
  227.         this.snackBar.showError('Default process cannot be changed');
  228.       } else {
  229.         if (['incoming', 'dispatch'].includes(event.container.data[event.currentIndex].process)) {
  230.           this.snackBar.showError('Default process cannot be changed');
  231.         } else {
  232.           moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
  233.           const index = event.container.data.indexOf(event.container.data.find((data) => data.process === 'put-away'));
  234.           if (event.currentIndex < index) {
  235.             if (this.data.isFromDO) {
  236.               const action = event.container.data[event.currentIndex];
  237.               event.container.data.splice(event.currentIndex, 1);
  238.               event.container.data.splice(event.previousIndex, 0, action);
  239.               this.snackBar.showError('Can not change incoming process in DO');
  240.             }
  241.           }
  242.         }
  243.       }
  244.     } else {
  245.       if (event.currentIndex === 0) {
  246.         event.currentIndex = event.currentIndex + 1;
  247.       } else if (event.currentIndex === event.container.data.length) {
  248.         event.currentIndex = event.currentIndex - 1;
  249.       }
  250.       transferArrayItem(event.previousContainer.data, event.container.data, event.previousIndex, event.currentIndex);
  251.       if (event.container.connectedTo[0].id === 'list') {
  252.         const index = event.container.data.indexOf(event.container.data.find((data) => data.process === 'put-away'));
  253.         if (event.currentIndex < index) {
  254.           if (['QC', 'VAS'].indexOf(event.container.data[event.currentIndex]) !== -1) {
  255.             if (!this.data.isFromDO) {
  256.               this.openSettingDialog(event.container.data[event.currentIndex], event.currentIndex);
  257.  
  258.               switch (event.container.data[event.currentIndex]) {
  259.                 case 'VAS':
  260.                   this.list.push('VAS');
  261.                   break;
  262.                 case 'QC':
  263.                   this.list.push('QC');
  264.               }
  265.             } else {
  266.               // if (event.currentIndex > this.putAwayIndex) {
  267.               //   this.openSettingDialog(event.container.data[event.currentIndex], event.currentIndex);
  268.               //   switch (event.container.data[event.currentIndex]) {
  269.               //     case 'VAS':
  270.               //       this.list.push('VAS');
  271.               //       break;
  272.               //     case 'QC':
  273.               //       this.list.push('QC');
  274.               //   }
  275.               // } else {
  276.               //   if (this.data.isFromDO && event.currentIndex < this.putAwayIndex) {
  277.               //     this.snackBar.showError('Can not change Incoming Process in DO');
  278.               //     this.list.push(event.container.data[event.currentIndex]);
  279.               //     event.container.data.splice(event.currentIndex, 1);
  280.               //   }
  281.               // }
  282.               if (this.data.isFromDO && event.currentIndex < this.putAwayIndex) {
  283.                 this.snackBar.showError('Can not change Incoming Process in DO');
  284.                 this.list.push(event.container.data[event.currentIndex]);
  285.                 event.container.data.splice(event.currentIndex, 1);
  286.               }
  287.             }
  288.             // } else if (this.data.isFromDO && event.currentIndex > this.putAwayIndex) {
  289.             //   this.openSettingDialog(event.container.data[event.currentIndex], event.currentIndex);
  290.             //   switch (event.container.data[event.currentIndex]) {
  291.             //     case 'VAS':
  292.             //       this.list.push('VAS');
  293.             //       break;
  294.             //     case 'QC':
  295.             //       this.list.push('QC');
  296.             //   }
  297.             // } else {
  298.             //   if (this.data.isFromDO && event.currentIndex < this.putAwayIndex) {
  299.             //     this.snackBar.showError('Can not change Incoming Process in DO');
  300.             //     this.list.push(event.container.data[event.currentIndex]);
  301.             //     event.container.data.splice(event.currentIndex, 1);
  302.             //   }
  303.             // }
  304.           }
  305.  
  306.           if (!this.data.isFromDO) {
  307.             this.arrangeProcessList[event.currentIndex] = {
  308.               process: event.container.data[event.currentIndex].includes('VAS')
  309.                 ? 'VAS ' + this.vasCount()
  310.                 : event.container.data[event.currentIndex],
  311.               ordering: event.currentIndex + 1,
  312.             };
  313.           }
  314.         } else {
  315.           // const action = event.container.data[event.currentIndex];
  316.           //
  317.           // if (
  318.           //   (outgoingQcExists > 1 && event.container.data[event.currentIndex] === 'QC') ||
  319.           //   (outgoingVasExists > 1 && event.container.data[event.currentIndex] === 'VAS')
  320.           // ) {
  321.           //   event.container.data.splice(event.currentIndex, 1);
  322.           //   this.snackBar.showError('Outgoing Process can contain only one ' + action);
  323.           //   this.list.push(action);
  324.           // } else {
  325.           if (['QC', 'VAS'].indexOf(event.container.data[event.currentIndex]) !== -1) {
  326.             this.openSettingDialog(event.container.data[event.currentIndex], event.currentIndex);
  327.             switch (event.container.data[event.currentIndex]) {
  328.               case 'VAS':
  329.                 this.list.push('VAS');
  330.                 break;
  331.               case 'QC':
  332.                 this.list.push('QC');
  333.             }
  334.           }
  335.  
  336.           this.arrangeProcessList[event.currentIndex] = {
  337.             process: event.container.data[event.currentIndex].includes('VAS')
  338.               ? 'VAS'
  339.               : event.container.data[event.currentIndex],
  340.             ordering: event.currentIndex + 1,
  341.           };
  342.           // }
  343.         }
  344.       } else {
  345.         const process: string = (event.container.data[event.currentIndex] as any).process;
  346.         if (!this.data?.isFromDO) {
  347.           if (process.includes('VAS') || process.includes('QC')) {
  348.             this.list.splice(event.currentIndex, 1);
  349.           } else {
  350.             this.arrangeProcessList.splice(event.previousIndex, 0, {
  351.               process,
  352.               ordering: event.previousIndex + 1,
  353.             });
  354.             this.list.splice(event.currentIndex, 1);
  355.             this.snackBar.showError('Cannot change default process');
  356.           }
  357.         } else {
  358.           if (process.includes('VAS') || process.includes('QC')) {
  359.             this.list.splice(event.currentIndex, 1);
  360.           } else {
  361.             this.arrangeProcessList.splice(event.previousIndex, 0, {
  362.               process,
  363.               ordering: event.previousIndex + 1,
  364.             });
  365.             this.list.splice(event.currentIndex, 1);
  366.             this.snackBar.showError('Cannot change default process');
  367.           }
  368.           // if (event.previousIndex < this.putAwayIndex) {
  369.           //   this.arrangeProcessList.splice(event.previousIndex, 0, {
  370.           //     process,
  371.           //     ordering: event.previousIndex + 1
  372.           //   });
  373.           //   this.list.splice(event.currentIndex, 1);
  374.           //   this.snackBar.showError('Can not change incoming process in DO');
  375.           // } else {
  376.           //   if (process.includes('VAS') || process.includes('QC')) {
  377.           //     this.list.splice(event.currentIndex, 1);
  378.           //   } else {
  379.           //     this.arrangeProcessList.splice(event.previousIndex, 0, {
  380.           //       process,
  381.           //       ordering: event.previousIndex + 1,
  382.           //     });
  383.           //     this.list.splice(event.currentIndex, 1);
  384.           //     this.snackBar.showError('Cannot change default process');
  385.           //   }
  386.           // }
  387.         }
  388.       }
  389.     }
  390.  
  391.     this.arrangeProcessList = [
  392.       ...this.arrangeProcessList.map((process, index) => {
  393.         return {
  394.           ...process,
  395.           ordering: index + 1,
  396.         };
  397.       }),
  398.     ];
  399.   }
  400.  
  401.   public vasCount(): number {
  402.     let count = Math.max(
  403.       ...(this.arrangeProcessList.reduce(
  404.         (acc, cur, index) => {
  405.           if (typeof cur === 'object' && cur.process.includes('VAS ')) {
  406.             return [...acc, +cur.process.split(' ')[1]];
  407.           } else {
  408.             return [...acc];
  409.           }
  410.         },
  411.         [0],
  412.       ) || [0]),
  413.     );
  414.     return ++count;
  415.   }
  416.  
  417.   private setItemCode(customerId: string, itemCodeIds?: number[]) {
  418.     this.itemCodeApiService.setCustomerId(customerId);
  419.     if (itemCodeIds?.length) {
  420.       this.filterForm.get('itemCodeIds').setValue(null);
  421.       this.itemCodes$$.pipe(takeUntil(this.destroy$$)).subscribe((itemCodes) => {
  422.         this.filterForm.get('itemCodeIds').setValue([...itemCodeIds]);
  423.       });
  424.     } else {
  425.       this.itemCodes$$.pipe(takeUntil(this.destroy$$)).subscribe((itemCodes) => {
  426.         this.filterForm.get('itemCodeIds').setValue(itemCodes?.length ? [+itemCodes[0].id] : []);
  427.       });
  428.     }
  429.   }
  430.  
  431.   public toggleAllLocations(control?: FormControl) {
  432.     if (control) {
  433.       control.setValue(!control.value);
  434.       this.filterForm.get('locationIds').setValue(control.value ? [...this.locations$$.value.map((v) => +v.id)] : []);
  435.     } else {
  436.       this.filterForm
  437.         .get('locationIds')
  438.         .setValue(!this.selectAllLocationsControl.value ? [...this.locations$$.value.map((v) => +v.id)] : []);
  439.     }
  440.   }
  441.  
  442.   public openSettingDialog(processName, index: number) {
  443.     if (processName.toLowerCase().includes('vas')) {
  444.       this.dialog
  445.         .open(VasSettingsComponent, {
  446.           data: this.arrangeProcessList[index],
  447.         })
  448.         .afterClosed()
  449.         .pipe(take(1))
  450.         .subscribe((vasProcess) => {
  451.           if (vasProcess?.length && !vasProcess?.process) {
  452.             const count = this.vasCount() - 1;
  453.             this.arrangeProcessList[index] = {
  454.               process: `VAS ${count > 0 ? count : ''}`,
  455.               ordering: index + 1,
  456.               vas: vasProcess,
  457.             };
  458.           } else if (vasProcess?.process) {
  459.             const count = this.vasCount() - 1;
  460.             this.arrangeProcessList[index] = {
  461.               process: `VAS ${count > 0 ? count : ''}`,
  462.               ordering: index + 1,
  463.               vas: vasProcess.vas,
  464.             };
  465.           } else {
  466.             this.arrangeProcessList.splice(index, 1);
  467.             this.cdr.detectChanges();
  468.             this.snackBar.showError('Empty VAS is not allowed');
  469.           }
  470.         });
  471.     } else if (processName.toLowerCase().includes('qc')) {
  472.       this.dialog
  473.         .open(QcSettingsComponent, {
  474.           data: this.arrangeProcessList[index],
  475.         })
  476.         .afterClosed()
  477.         .pipe(take(1))
  478.         .subscribe((qcCheckList) => {
  479.           let qcItems = false;
  480.           if (!qcCheckList?.process) {
  481.             for (const item in qcCheckList) {
  482.               if (!this.notItemChecks.includes(item)) {
  483.                 if (qcCheckList[item] === true) {
  484.                   qcItems = true;
  485.                   break;
  486.                 }
  487.               }
  488.             }
  489.           } else {
  490.             qcItems = true;
  491.           }
  492.           if (qcItems) {
  493.             if (qcCheckList?.process) {
  494.               index < this.putAwayIndex && this.modes.sortItems
  495.                 ? (qcCheckList.qc.visualInspection = true)
  496.                 : (qcCheckList.qc.visualInspection = qcCheckList.qc?.visualInspection);
  497.               this.arrangeProcessList[index] = {
  498.                 process: processName,
  499.                 ordering: index + 1,
  500.                 qc: qcCheckList?.qc,
  501.               };
  502.             } else {
  503.               index < this.putAwayIndex && this.modes.sortItems
  504.                 ? (qcCheckList.visualInspection = true)
  505.                 : (qcCheckList.visualInspection = qcCheckList.visualInspection);
  506.               this.arrangeProcessList[index] = {
  507.                 process: processName,
  508.                 ordering: index + 1,
  509.                 qc: qcCheckList,
  510.               };
  511.             }
  512.           } else {
  513.             this.arrangeProcessList.splice(index, 1);
  514.             this.cdr.detectChanges();
  515.             this.snackBar.showError('Empty QC is not allowed');
  516.           }
  517.         });
  518.     } else {
  519.       this.dialog
  520.         .open(PutAwaySettingsComponent, {
  521.           data: this.modes,
  522.         })
  523.         .afterClosed()
  524.         .pipe(take(1))
  525.         .subscribe((strategies) => {
  526.           if (strategies) {
  527.             this.modes = { ...strategies };
  528.             for (let j = index - 1; j >= 0; j--) {
  529.               if (this.arrangeProcessList[j].process === 'QC') {
  530.                 this.arrangeProcessList[j].qc.visualInspection = this.modes?.sortItems;
  531.               }
  532.             }
  533.           }
  534.         });
  535.     }
  536.   }
  537.  
  538.   public get putAwayIndex(): number {
  539.     const index = this.arrangeProcessList.indexOf(this.arrangeProcessList.find((data) => data.process === 'put-away'));
  540.     return index;
  541.   }
  542.  
  543.   public createProcess() {
  544.     if (this.filterForm.valid) {
  545.       if (this.modes?.modeOfDeliveryId && this.modes?.criteria) {
  546.         this.submitting$$.next(true);
  547.         delete this.modes?.sortItems;
  548.         if (this.arrangeProcessList.length) {
  549.           const requestBody: CreateProcess = {
  550.             ...this.filterForm.value,
  551.             ...this.modes,
  552.             ...(this.data?.process?.id
  553.               ? { processId: this.data.process.id }
  554.               : this.data?.process?.processId
  555.               ? { processId: this.data.process.processId }
  556.               : {}),
  557.             processDetails: [
  558.               ...this.arrangeProcessList.map((list, index) => {
  559.                 const process = {
  560.                   ordering: index + 1,
  561.                   process: list.process.includes('VAS')
  562.                     ? 'VAS'
  563.                     : list.process === 'Incoming'
  564.                     ? list.process.toLowerCase()
  565.                     : list.process,
  566.                   ...(list?.id
  567.                     ? { processDetailsId: list?.id }
  568.                     : list?.processDetailsId
  569.                     ? { processDetailsId: list.processDetailsId }
  570.                     : {}),
  571.                 };
  572.  
  573.                 if (process.process === ProcessType.VAS) {
  574.                   return {
  575.                     ...process,
  576.                     vas: list?.vas?.length
  577.                       ? list.vas.map((vas) => {
  578.                           return {
  579.                             ordering: vas.ordering,
  580.                             type: vas.type,
  581.                             description: vas.description,
  582.                             ...(vas?.id ? { vasId: vas.id } : vas?.vasId ? { vasId: vas?.vasId } : {}),
  583.                           };
  584.                         })
  585.                       : [],
  586.                   };
  587.                 } else if (process.process === ProcessType.QC) {
  588.                   const qcDetails = {
  589.                     ...process,
  590.                     qc: list?.qc
  591.                       ? {
  592.                           ...list.qc,
  593.                           ...(list.qc?.id ? { qcId: list.qc.id } : list.qc.qcId ? { qcId: list.qc.qcId } : {}),
  594.                           minPercentagePacketCheck: list.qc?.minPercentagePacketCheck
  595.                             ? list.qc?.minPercentagePacketCheck
  596.                             : list.qc?.minPercentageP,
  597.                           visualInspection: list?.qc?.visualInspection
  598.                             ? list?.qc?.visualInspection
  599.                             : list.qc?.visualInspecti,
  600.                         }
  601.                       : ({} as ArrangeProcessQCList),
  602.                   };
  603.  
  604.                   delete qcDetails.qc?.createdAt;
  605.                   delete qcDetails.qc?.deletedAt;
  606.                   delete qcDetails.qc?.minPercentageP;
  607.                   delete qcDetails.qc?.processDetails;
  608.                   delete qcDetails?.qc?.visualInspecti;
  609.                   delete qcDetails.qc?.processDetailsId;
  610.                   delete qcDetails.qc?.processDeta;
  611.                   delete qcDetails.qc?.id;
  612.                   delete qcDetails.qc?.updatedAt;
  613.  
  614.                   return qcDetails;
  615.                 } else {
  616.                   return process;
  617.                 }
  618.               }),
  619.             ],
  620.           };
  621.  
  622.           delete requestBody.operatorId;
  623.  
  624.           if (this.data?.notFromMaster) {
  625.             this.submitting$$.next(false);
  626.             this.closeDialog(requestBody);
  627.             return;
  628.           }
  629.  
  630.           console.log(requestBody);
  631.           this.masterApiService.createProcess$(requestBody).subscribe(
  632.             (resp) => {
  633.               this.submitting$$.next(false);
  634.               this.snackBar.showMessage(this.data.isDialog ? 'Process Updated' : 'Process Created');
  635.               this.data?.isDialog ? this.closeDialog() : this.router.navigate(['master', 'view-master-process']);
  636.             },
  637.             (error) => {
  638.               this.submitting$$.next(false);
  639.             },
  640.           );
  641.         } else {
  642.           this.snackBar.showError('Please arrange the process before creating it');
  643.         }
  644.       } else {
  645.         this.snackBar.showError('Select Storage Strategy in Put-away before saving process');
  646.       }
  647.     } else {
  648.       this.snackBar.showError('Form is not valid. Please check and try again');
  649.       this.filterForm.markAllAsTouched();
  650.     }
  651.   }
  652.  
  653.   private closeDialog(result?: CreateProcess) {
  654.     this.matDialogRef.close(result);
  655.   }
  656.  
  657.   public toggleAllItemCodes() {
  658.     this.selectAllItemCodes.setValue(!this.selectAllItemCodes.value);
  659.   }
  660.  
  661.   public openNewItemCodeModal() {
  662.     const d = this.dialog.open(CreateItemCodeComponent, {
  663.       data: {
  664.         customerId: this.filterForm.value.customerId,
  665.         itemCodes: this.itemCodes$$.value,
  666.       },
  667.     });
  668.  
  669.     d.afterClosed().subscribe((value) => {
  670.       if (value) {
  671.         this.setItemCode(this.filterForm.value.customerId, [+value.id]);
  672.         this.filterForm.get('itemCodeIds').setValue([+value.id]);
  673.         this.itemSelectDropDown.close();
  674.       }
  675.     });
  676.   }
  677.  
  678.   public getSelectedItems(id: number) {
  679.     if (this.filterForm.get('itemCodeIds')?.value?.length) {
  680.       this.filterForm.get('itemCodeIds')?.setValue([...this.filterForm.get('itemCodeIds').value, id]);
  681.     } else {
  682.       this.filterForm.get('itemCodeIds')?.setValue([id]);
  683.     }
  684.   }
  685. }
  686.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement