diff --git a/src/transaction/excel.controller.ts b/src/transaction/excel.controller.ts index 9879c23..b3bc43e 100644 --- a/src/transaction/excel.controller.ts +++ b/src/transaction/excel.controller.ts @@ -5,7 +5,9 @@ import { Header, Post, Body, - Request, Param, ParseUUIDPipe, + Request, + Param, + ParseUUIDPipe, } from '@nestjs/common'; import { Response } from 'express'; import { TransactionService } from './transaction.service'; @@ -29,6 +31,7 @@ export class ExcelController { const result = await this.transactionService.exportDataExcel( exportTransactionDto.dateStart, id, + req.user, ); res.download(`${result}`); diff --git a/src/transaction/transaction.service.ts b/src/transaction/transaction.service.ts index 32f8469..83bf98d 100644 --- a/src/transaction/transaction.service.ts +++ b/src/transaction/transaction.service.ts @@ -46,6 +46,7 @@ import { CallbackPartner } from './entities/callback-partner.entity'; import { doAuthorizeHemat } from '../helper/sihemat-authorization'; import { ProductHistoryStatusService } from '../product/history-status/history-status.service'; import { Workbook } from 'exceljs'; +import moment from "moment"; @Injectable() export class TransactionService { @@ -1878,7 +1879,6 @@ export class TransactionService { .addSelect('product.name', 'name') .addSelect('product_price.price', 'product_price') .addSelect('product_price.mark_up_price', 'mark_up_price') - .addSelect('product.name', 'supplier_name') .addSelect('supplier.name', 'supplier_name') .orderBy('transaction.created_at', 'DESC'); @@ -2140,7 +2140,7 @@ export class TransactionService { }); } - async exportDataExcel(startDate, user: any) { + async exportDataExcel(startDate, user: any, currentUser: any) { const userData = await this.userService.findExist(user); let userBySupperior = []; @@ -2194,6 +2194,7 @@ export class TransactionService { .addSelect('product.name', 'product_name') .addSelect('product.code', 'product_code') + .addSelect('supplier.name', 'supplier_name') .addSelect('transaction.amount', 'price') .addSelect('transaction.balance_remaining', 'balance_remaining') .addSelect('userData.name', 'buyer') @@ -2251,21 +2252,38 @@ export class TransactionService { // now do stuff with each item const moment = require("moment"); - - dataSorted.push({ - product_name: item.product_name, - product_code: item.product_code, - price: item.price, - balance_remaining: item.balance_remaining, - buyer: item.buyer, - destination: item.destination, - transaction_code: item.transaction_code, - status: item.status, - serial_number: item.serial_number, - partner_trx_id: item.partner_trx_id, - transaction_date: moment(new Date (item.transaction_date).toISOString().replace('Z', ' ').replace('T', ' ')).format("MM-DD-YYYY HH:mm:ss"), - failed_reason: item.failed_reason, - }); + if (currentUser.username == 'admin') { + dataSorted.push({ + product_name: item.product_name, + product_code: item.product_code, + supplier_name: item.supplier_name, + price: item.price, + balance_remaining: item.balance_remaining, + buyer: item.buyer, + destination: item.destination, + transaction_code: item.transaction_code, + status: item.status, + serial_number: item.serial_number, + partner_trx_id: item.partner_trx_id, + transaction_date: moment(new Date (item.transaction_date).toISOString().replace('Z', ' ').replace('T', ' ')).format("MM-DD-YYYY HH:mm:ss"), + failed_reason: item.failed_reason, + }); + } else { + dataSorted.push({ + product_name: item.product_name, + product_code: item.product_code, + price: item.price, + balance_remaining: item.balance_remaining, + buyer: item.buyer, + destination: item.destination, + transaction_code: item.transaction_code, + status: item.status, + serial_number: item.serial_number, + partner_trx_id: item.partner_trx_id, + transaction_date: moment(new Date(item.transaction_date).toISOString().replace('Z', ' ').replace('T', ' ')).format("MM-DD-YYYY HH:mm:ss"), + failed_reason: item.failed_reason, + }); + } } dataSorted.forEach((doc) => { @@ -2286,20 +2304,38 @@ export class TransactionService { sheet.addRows(rows); //customize column - sheet.columns = [ - {header: 'Nama Produk', key: 'product_name'}, - {header: 'Kode Produk', key: 'product_code'}, - {header: 'Harga', key: 'price'}, - {header: 'Sisa Saldo', key: 'balance_remaining'}, - {header: 'Pembeli', key: 'buyer'}, - {header: 'Tujuan', key: 'destination'}, - {header: 'Kode Transaksi', key: 'transaction_code'}, - {header: 'Status', key: 'status'}, - {header: 'No Seri', key: 'serial_number'}, - {header: 'IDTrx Mitra', key: 'partner_trx_id'}, - {header: 'Tanggal Transaksi', key: 'transaction_date'}, - {header: 'Alasan Gagal', key: 'failed_reason'}, - ]; + if (currentUser.username == 'admin') { + sheet.columns = [ + {header: 'Nama Produk', key: 'product_name'}, + {header: 'Kode Produk', key: 'product_code'}, + {header: 'Supplier', key: 'supplier_name'}, + {header: 'Harga', key: 'price'}, + {header: 'Sisa Saldo', key: 'balance_remaining'}, + {header: 'Pembeli', key: 'buyer'}, + {header: 'Tujuan', key: 'destination'}, + {header: 'Kode Transaksi', key: 'transaction_code'}, + {header: 'Status', key: 'status'}, + {header: 'No Seri', key: 'serial_number'}, + {header: 'IDTrx Mitra', key: 'partner_trx_id'}, + {header: 'Tanggal Transaksi', key: 'transaction_date'}, + {header: 'Alasan Gagal', key: 'failed_reason'}, + ]; + } else { + sheet.columns = [ + {header: 'Nama Produk', key: 'product_name'}, + {header: 'Kode Produk', key: 'product_code'}, + {header: 'Harga', key: 'price'}, + {header: 'Sisa Saldo', key: 'balance_remaining'}, + {header: 'Pembeli', key: 'buyer'}, + {header: 'Tujuan', key: 'destination'}, + {header: 'Kode Transaksi', key: 'transaction_code'}, + {header: 'Status', key: 'status'}, + {header: 'No Seri', key: 'serial_number'}, + {header: 'IDTrx Mitra', key: 'partner_trx_id'}, + {header: 'Tanggal Transaksi', key: 'transaction_date'}, + {header: 'Alasan Gagal', key: 'failed_reason'}, + ]; + } this.styleSheet(sheet)