import { Component, OnInit, ViewChild } from '@angular/core'; import { CommonService } from '../../../services/common.service'; import { Subject } from 'rxjs'; import { DataTableDirective } from 'angular-datatables'; import { ActivatedRoute } from "@angular/router"; import { FormBuilder, FormGroup } from '@angular/forms'; import { OrderListService } from '../../../services/order/order.service'; import { OrderDetails } from '../../../models/orderdetails/orderdetails'; import { ArchivedOrder, ArchiveOrderModelData } from '../../../models/order-list/archive-order'; import { Input } from '@angular/core'; import { Output } from '@angular/core'; import { EventEmitter } from '@angular/core'; import { ElementRef } from '@angular/core'; import { CommonEnum } from 'src/app/models/common-enum'; import { CookieService } from 'ngx-cookie-service'; declare var $: any; @Component({ selector: 'app-order-details', templateUrl: './order-details.component.html', styleUrls: ['./order-details.component.css'] }) export class OrderDetailsComponent implements OnInit { OrderDetailForm: FormGroup; modelOrderDetail = new ArchiveOrderModelData(); lstArchivedOrder: ArchivedOrder[] = []; archivedOrderlist: any; dtOptions: any = {}; typesofOrders: string = ''; addressline1: string = ''; addressline2: string = ''; shiptoaddressdata: string = ''; OrderDetailAPIResponse: any; ResultData: any[]; ResultDatas: OrderDetails[]; formData: FormData = new FormData(); dtTrigger: Subject = new Subject(); OrderItemCount: number = 0; UserId: number = 1; @ViewChild(DataTableDirective) dtElement: DataTableDirective; @ViewChild('line') line: ElementRef; @ViewChild('item') item: ElementRef; @ViewChild('itemDesc') itemDesc: ElementRef; @ViewChild('status') status: ElementRef; @ViewChild('dateShipped') dateShipped: ElementRef; @ViewChild('quantity') quantity: ElementRef; @ViewChild('uom') uom: ElementRef; @ViewChild('unitPrice') unitPrice: ElementRef; @ViewChild('extAmt') extAmt: ElementRef; @ViewChild('branchname') branchname: ElementRef; @ViewChild('invoice') invoice: ElementRef; @ViewChild('invoiceDate') invoiceDate: ElementRef; @ViewChild('shiptoaddress') shiptoaddress: ElementRef; @ViewChild('requestedshipdate') requestedshipdate: ElementRef; @ViewChild('btnClose') btnClose: ElementRef; @Input() _paramId: number = 0; @Input() _paramPoId: string; @Output() data: EventEmitter = new EventEmitter(); CustomerNumber: string = ''; customernumber: string; email: string = sessionStorage.getItem('Email'); foldername: string = '2021'; selectedColumn: string = ''; isArchiveOrder: boolean = false; isMultipleData: boolean = false; @Output() submitforms = new EventEmitter(); getDashboard: string = sessionStorage.getItem('IsDashboard'); getcustomerno: string = sessionStorage.getItem('CustomerNo'); getponumber: string = sessionStorage.getItem('CustomerPo').trim(); getcustomernumber: string = sessionStorage.getItem('CustomerNumber'); getmapingcustomer: string = sessionStorage.getItem('MapingCustomer'); state = { "data": null, "gridData": null, "dateData": null }; showPricingColumn: boolean = Number(sessionStorage.getItem(CommonEnum.ViewPricing)) == 0 ? false : true; loggedInUserType: string = this.common.b64_to_utf8(this.cookieService.get('InternalOrExternal')); constructor(private common: CommonService, private service: OrderListService, private route: ActivatedRoute, private formBuilder: FormBuilder, private cookieService : CookieService) { } total: number = 0; value: any; ngOnInit(): void { this._paramId = Number(this.route.snapshot.params.id); this._paramPoId = String(this.route.snapshot.params.id); let historyState = sessionStorage.getItem("state") != null ? JSON.parse(sessionStorage.getItem("state")) : history.state; this.state = { "data": historyState.data, "gridData": historyState.gridData, "dateData": historyState.dateData }; if (sessionStorage.getItem("state") != null) { sessionStorage.removeItem("state"); } this.fillGrid(); // this.getArchivedOrderList(); this.dtOptions = this.common.dataTableOptionsForSN(true, true, false, true, true, false, false); $.fn.DataTable.ext.pager.numbers_length = 7; } fillGrid() { this.service.getById(this._paramId, this.getcustomerno, this.email, encodeURIComponent(this.getponumber),this.loggedInUserType,Number(sessionStorage.getItem('GlobalSearchCustomerID')), data => { this.OrderDetailAPIResponse = data; this.ResultDatas = this.OrderDetailAPIResponse.objResponse.responseContent; this.ResultData = this.OrderDetailAPIResponse.objResponse.responseContent.ItemOrderList; this.findSumExtendedAmount(this.ResultData); this.findSumUnitPrice(this.ResultData); this.OrderItemCount = this.ResultData?.length; this.dtTrigger.next(); var typeoforder = this.ResultData.map(function (obj) { return obj.branchname }); typeoforder = typeoforder.filter(function (v, i) { return typeoforder.indexOf(v) == i; }); this.typesofOrders = typeoforder.join(', '); var address1 = this.ResultData.map(function (obj) { return obj.AddressLine1 }); address1 = address1.filter(function (v, i) { return address1.indexOf(v) == i; }); this.addressline1 = address1.join(); var address2 = this.ResultData.map(function (obj) { return obj.AddressLine2 }); address2 = address2.filter(function (v, i) { return address2.indexOf(v) == i; }); this.addressline2 = address2.join(); var address3 = this.ResultData.map(function (obj) { return obj.AddressLine3 }); address3 = address3.filter(function (v, i) { return address3.indexOf(v) == i; }); this.addressline2 = address3.join(); var orderaddress = this.ResultData.map(function (obj) { return obj.shiptoaddress }); orderaddress = orderaddress.filter(function (v, i) { return orderaddress.indexOf(v) == i; }); this.shiptoaddressdata = orderaddress.join(); }, (error) => { console.error('Error: ' + error); return error(error); }); } clear() { this.btnClose.nativeElement.click(); } getArchivedOrderList() { this.service.getCustomerArchiveContents(this.getmapingcustomer, data => { this.archivedOrderlist = data.objResponse.responseContent.ArchivedOrderTree; if (this.archivedOrderlist != undefined && this.archivedOrderlist != null) this.isArchivedOrder(); }, (error) => { console.error('Error: ' + error); return error(error); }); } isArchivedOrder() { let length; if (this.archivedOrderlist != undefined && this.archivedOrderlist != null) { length = this.archivedOrderlist.filter(x => x.Jobid == this._paramId)?.length; if (length > 0) this.isArchiveOrder = true; else this.isArchiveOrder = false;; } } setHeaderBg(columnName) { if (columnName == 'line') { this.line.nativeElement.classList.add('change_header_sorting'); this.branchname.nativeElement.classList.remove('change_header_sorting'); this.item.nativeElement.classList.remove('change_header_sorting'); this.itemDesc.nativeElement.classList.remove('change_header_sorting'); this.status.nativeElement.classList.remove('change_header_sorting'); this.dateShipped.nativeElement.classList.remove('change_header_sorting'); this.quantity.nativeElement.classList.remove('change_header_sorting'); this.uom.nativeElement.classList.remove('change_header_sorting'); this.unitPrice.nativeElement.classList.remove('change_header_sorting'); this.extAmt.nativeElement.classList.remove('change_header_sorting'); this.invoice.nativeElement.classList.remove('change_header_sorting'); this.invoiceDate.nativeElement.classList.remove('change_header_sorting'); this.requestedshipdate.nativeElement.classList.remove('change_header_sorting'); } else if (columnName == 'branchname') { this.line.nativeElement.classList.remove('change_header_sorting'); this.branchname.nativeElement.classList.add('change_header_sorting'); this.item.nativeElement.classList.remove('change_header_sorting'); this.itemDesc.nativeElement.classList.remove('change_header_sorting'); this.status.nativeElement.classList.remove('change_header_sorting'); this.dateShipped.nativeElement.classList.remove('change_header_sorting'); this.quantity.nativeElement.classList.remove('change_header_sorting'); this.uom.nativeElement.classList.remove('change_header_sorting'); this.unitPrice.nativeElement.classList.remove('change_header_sorting'); this.extAmt.nativeElement.classList.remove('change_header_sorting'); this.invoice.nativeElement.classList.remove('change_header_sorting'); this.invoiceDate.nativeElement.classList.remove('change_header_sorting'); this.requestedshipdate.nativeElement.classList.remove('change_header_sorting'); } else if (columnName == 'item') { this.line.nativeElement.classList.remove('change_header_sorting'); this.branchname.nativeElement.classList.remove('change_header_sorting'); this.item.nativeElement.classList.add('change_header_sorting'); this.itemDesc.nativeElement.classList.remove('change_header_sorting'); this.status.nativeElement.classList.remove('change_header_sorting'); this.dateShipped.nativeElement.classList.remove('change_header_sorting'); this.quantity.nativeElement.classList.remove('change_header_sorting'); this.uom.nativeElement.classList.remove('change_header_sorting'); this.unitPrice.nativeElement.classList.remove('change_header_sorting'); this.extAmt.nativeElement.classList.remove('change_header_sorting'); this.invoice.nativeElement.classList.remove('change_header_sorting'); this.invoiceDate.nativeElement.classList.remove('change_header_sorting'); this.requestedshipdate.nativeElement.classList.remove('change_header_sorting'); } else if (columnName == 'itemDesc') { this.line.nativeElement.classList.remove('change_header_sorting'); this.branchname.nativeElement.classList.remove('change_header_sorting'); this.item.nativeElement.classList.remove('change_header_sorting'); this.itemDesc.nativeElement.classList.add('change_header_sorting'); this.status.nativeElement.classList.remove('change_header_sorting'); this.dateShipped.nativeElement.classList.remove('change_header_sorting'); this.quantity.nativeElement.classList.remove('change_header_sorting'); this.uom.nativeElement.classList.remove('change_header_sorting'); this.unitPrice.nativeElement.classList.remove('change_header_sorting'); this.extAmt.nativeElement.classList.remove('change_header_sorting'); this.invoice.nativeElement.classList.remove('change_header_sorting'); this.invoiceDate.nativeElement.classList.remove('change_header_sorting'); this.requestedshipdate.nativeElement.classList.remove('change_header_sorting'); } else if (columnName == 'status') { this.line.nativeElement.classList.remove('change_header_sorting'); this.branchname.nativeElement.classList.remove('change_header_sorting'); this.item.nativeElement.classList.remove('change_header_sorting'); this.itemDesc.nativeElement.classList.remove('change_header_sorting'); this.status.nativeElement.classList.add('change_header_sorting'); this.dateShipped.nativeElement.classList.remove('change_header_sorting'); this.quantity.nativeElement.classList.remove('change_header_sorting'); this.uom.nativeElement.classList.remove('change_header_sorting'); this.unitPrice.nativeElement.classList.remove('change_header_sorting'); this.extAmt.nativeElement.classList.remove('change_header_sorting'); this.invoice.nativeElement.classList.remove('change_header_sorting'); this.invoiceDate.nativeElement.classList.remove('change_header_sorting'); this.requestedshipdate.nativeElement.classList.remove('change_header_sorting'); } else if (columnName == 'dateShipped') { this.line.nativeElement.classList.remove('change_header_sorting'); this.branchname.nativeElement.classList.remove('change_header_sorting'); this.item.nativeElement.classList.remove('change_header_sorting'); this.itemDesc.nativeElement.classList.remove('change_header_sorting'); this.status.nativeElement.classList.remove('change_header_sorting'); this.dateShipped.nativeElement.classList.add('change_header_sorting'); this.quantity.nativeElement.classList.remove('change_header_sorting'); this.uom.nativeElement.classList.remove('change_header_sorting'); this.unitPrice.nativeElement.classList.remove('change_header_sorting'); this.extAmt.nativeElement.classList.remove('change_header_sorting'); this.invoice.nativeElement.classList.remove('change_header_sorting'); this.invoiceDate.nativeElement.classList.remove('change_header_sorting'); this.requestedshipdate.nativeElement.classList.remove('change_header_sorting'); } else if (columnName == 'quantity') { this.line.nativeElement.classList.remove('change_header_sorting'); this.branchname.nativeElement.classList.remove('change_header_sorting'); this.item.nativeElement.classList.remove('change_header_sorting'); this.itemDesc.nativeElement.classList.remove('change_header_sorting'); this.status.nativeElement.classList.remove('change_header_sorting'); this.dateShipped.nativeElement.classList.remove('change_header_sorting'); this.quantity.nativeElement.classList.add('change_header_sorting'); this.uom.nativeElement.classList.remove('change_header_sorting'); this.unitPrice.nativeElement.classList.remove('change_header_sorting'); this.extAmt.nativeElement.classList.remove('change_header_sorting'); this.invoice.nativeElement.classList.remove('change_header_sorting'); this.invoiceDate.nativeElement.classList.remove('change_header_sorting'); this.requestedshipdate.nativeElement.classList.remove('change_header_sorting'); } else if (columnName == 'uom') { this.line.nativeElement.classList.remove('change_header_sorting'); this.branchname.nativeElement.classList.remove('change_header_sorting'); this.item.nativeElement.classList.remove('change_header_sorting'); this.itemDesc.nativeElement.classList.remove('change_header_sorting'); this.status.nativeElement.classList.remove('change_header_sorting'); this.dateShipped.nativeElement.classList.remove('change_header_sorting'); this.quantity.nativeElement.classList.remove('change_header_sorting'); this.uom.nativeElement.classList.add('change_header_sorting'); this.unitPrice.nativeElement.classList.remove('change_header_sorting'); this.extAmt.nativeElement.classList.remove('change_header_sorting'); this.invoice.nativeElement.classList.remove('change_header_sorting'); this.invoiceDate.nativeElement.classList.remove('change_header_sorting'); this.requestedshipdate.nativeElement.classList.remove('change_header_sorting'); } else if (columnName == 'unitPrice') { this.line.nativeElement.classList.remove('change_header_sorting'); this.branchname.nativeElement.classList.remove('change_header_sorting'); this.item.nativeElement.classList.remove('change_header_sorting'); this.itemDesc.nativeElement.classList.remove('change_header_sorting'); this.status.nativeElement.classList.remove('change_header_sorting'); this.dateShipped.nativeElement.classList.remove('change_header_sorting'); this.quantity.nativeElement.classList.remove('change_header_sorting'); this.uom.nativeElement.classList.remove('change_header_sorting'); this.unitPrice.nativeElement.classList.add('change_header_sorting'); this.extAmt.nativeElement.classList.remove('change_header_sorting'); this.invoice.nativeElement.classList.remove('change_header_sorting'); this.invoiceDate.nativeElement.classList.remove('change_header_sorting'); this.requestedshipdate.nativeElement.classList.remove('change_header_sorting'); } else if (columnName == 'extAmt') { this.line.nativeElement.classList.remove('change_header_sorting'); this.branchname.nativeElement.classList.remove('change_header_sorting'); this.item.nativeElement.classList.remove('change_header_sorting'); this.itemDesc.nativeElement.classList.remove('change_header_sorting'); this.status.nativeElement.classList.remove('change_header_sorting'); this.dateShipped.nativeElement.classList.remove('change_header_sorting'); this.quantity.nativeElement.classList.remove('change_header_sorting'); this.uom.nativeElement.classList.remove('change_header_sorting'); this.unitPrice.nativeElement.classList.remove('change_header_sorting'); this.extAmt.nativeElement.classList.add('change_header_sorting'); this.invoice.nativeElement.classList.remove('change_header_sorting'); this.invoiceDate.nativeElement.classList.remove('change_header_sorting'); this.requestedshipdate.nativeElement.classList.remove('change_header_sorting'); } else if (columnName == 'invoice') { this.line.nativeElement.classList.remove('change_header_sorting'); this.branchname.nativeElement.classList.remove('change_header_sorting'); this.item.nativeElement.classList.remove('change_header_sorting'); this.itemDesc.nativeElement.classList.remove('change_header_sorting'); this.status.nativeElement.classList.remove('change_header_sorting'); this.dateShipped.nativeElement.classList.remove('change_header_sorting'); this.quantity.nativeElement.classList.remove('change_header_sorting'); this.uom.nativeElement.classList.remove('change_header_sorting'); this.unitPrice.nativeElement.classList.remove('change_header_sorting'); this.extAmt.nativeElement.classList.remove('change_header_sorting'); this.invoice.nativeElement.classList.add('change_header_sorting'); this.invoiceDate.nativeElement.classList.remove('change_header_sorting'); this.requestedshipdate.nativeElement.classList.remove('change_header_sorting'); } else if (columnName == 'invoiceDate') { this.line.nativeElement.classList.remove('change_header_sorting'); this.branchname.nativeElement.classList.remove('change_header_sorting'); this.item.nativeElement.classList.remove('change_header_sorting'); this.itemDesc.nativeElement.classList.remove('change_header_sorting'); this.status.nativeElement.classList.remove('change_header_sorting'); this.dateShipped.nativeElement.classList.remove('change_header_sorting'); this.quantity.nativeElement.classList.remove('change_header_sorting'); this.uom.nativeElement.classList.remove('change_header_sorting'); this.unitPrice.nativeElement.classList.remove('change_header_sorting'); this.extAmt.nativeElement.classList.remove('change_header_sorting'); this.invoice.nativeElement.classList.remove('change_header_sorting'); this.invoiceDate.nativeElement.classList.add('change_header_sorting'); this.requestedshipdate.nativeElement.classList.remove('change_header_sorting'); } else if (columnName == 'requestedshipdate') { this.line.nativeElement.classList.remove('change_header_sorting'); this.branchname.nativeElement.classList.remove('change_header_sorting'); this.item.nativeElement.classList.remove('change_header_sorting'); this.itemDesc.nativeElement.classList.remove('change_header_sorting'); this.status.nativeElement.classList.remove('change_header_sorting'); this.dateShipped.nativeElement.classList.remove('change_header_sorting'); this.quantity.nativeElement.classList.remove('change_header_sorting'); this.uom.nativeElement.classList.remove('change_header_sorting'); this.unitPrice.nativeElement.classList.remove('change_header_sorting'); this.extAmt.nativeElement.classList.remove('change_header_sorting'); this.invoice.nativeElement.classList.remove('change_header_sorting'); this.invoiceDate.nativeElement.classList.remove('change_header_sorting'); this.requestedshipdate.nativeElement.classList.add('change_header_sorting'); } } findSumExtendedAmount(data) { this.value = data; for (let j = 0; j < data?.length; j++) { this.total += this.value[j].extendedamount; } } findSumUnitPrice(data) { this.value = data; for (let j = 0; j < data?.length; j++) { this.total += this.value[j].unitprice; } } rerender() { return new Promise((resolve, reject) => { this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => { // Destroy the table first dtInstance.destroy(); this.fillGrid(); }); resolve(); }); } ngOnDestroy(): void { // Do not forget to unsubscribe the event this.dtTrigger.unsubscribe(); } setFormBuilder() { this.OrderDetailForm = this.formBuilder.group({ //ARCHIVEID: [''], ORDERNUMBER: [''], //ORDERNUMBERS: [''], //ISARCHIVED: [''], //ARCHIVEDDATE: [''], CUSTOMERNUMBER: [''], //JOB: [''], //ORDERTYPE: [''], //ORDERDATE: [''], }); } /* Method Uncommit and change CustomerNumber */ setAddForm() { this.OrderDetailForm.setValue({ ORDERNUMBER: 0, CUSTOMERNUMBER: this.getcustomernumber, }); } setModelBeforeSubmit() { /* Method Uncommit and change CustomerNumber */ this.lstArchivedOrder = []; if (this.isArchiveOrder) { this.lstArchivedOrder.push({ ARCHIVEID: 0, ORDERNUMBER: Number(this._paramId), ORDERNAME: '', ORDERNUMBERS: '', ISARCHIVED: 0, ARCHIVEDDATE: '', CUSTOMERNUMBER: Number(this.getcustomerno), JOB: 0, ORDERTYPE: '', ORDERDATE: '', PATH: '2021', FOLDERNAME: '', ORDERARCHIVEFOLDERID: null, CUSTOMERPO: '', SHIPTOLOCATION: '', ITEMS: 0, ORDERTOTAL: '', SHIPTOADDRESS: '' }) } else { this.lstArchivedOrder.push({ ARCHIVEID: 0, ORDERNUMBER: Number(this._paramId), ORDERNUMBERS: '', ORDERNAME: '', ISARCHIVED: 1, ARCHIVEDDATE: '', CUSTOMERNUMBER: Number(this.getcustomerno), JOB: 0, ORDERTYPE: '', ORDERDATE: '', PATH: '2021', FOLDERNAME: '', ORDERARCHIVEFOLDERID: null, CUSTOMERPO: '', SHIPTOLOCATION: '', ITEMS: 0, ORDERTOTAL: '', SHIPTOADDRESS: '' }) } } submitform() { this.setModelBeforeSubmit(); this.service.SaveOrder(this.lstArchivedOrder, this.email, this.foldername, data => { this.OrderDetailAPIResponse = data; if (this.OrderDetailAPIResponse.objResponse.statusCode == "200") { if (this.isArchiveOrder) { this.getArchivedOrderList(); this.common.showSuccess("The order was successfully unarchived.", "Success") } else { this.getArchivedOrderList(); this.common.showSuccess("The order was successfully archived.", "Success") } } else { if (this.isArchiveOrder) { this.common.showError("The order was not successfully unarchived.", "Archivedorder Fail") } else { this.common.showError("The order was not successfully archived.", "Archivedorder Fail") } } }, (error) => { console.error('Error: ' + error); return error(error); }); this.submitforms.emit(); } download() { const link = document.createElement('a'); link.setAttribute('target', '_blank'); link.setAttribute('href', '../assets/download/Cameron_985169_PDF.pdf'); link.setAttribute('download', `Cameron_985169_PDF.pdf`); document.body.appendChild(link); link.click(); link.remove(); } reinitialize_Pagination() { this.rerender().then(res => this.printPage()).then(res => this.rerender()).then(res => { sessionStorage.setItem("state", JSON.stringify(this.state)); window.location.reload() }); } printPage() { return new Promise((resolve, reject) => { this.dtOptions = this.common.dataTableOptionsForSN(false, false, false, false, false, false, false); let printContents = document.getElementById("printable").innerHTML; document.body.innerHTML = printContents; window.print(); resolve(); }); } }