- added filter by user and by supplier for export

This commit is contained in:
Muhammad Fadli 2023-06-21 20:01:20 +07:00
parent 86b0bd8e68
commit 9c2143d214
3 changed files with 277 additions and 1 deletions

View File

@ -6,4 +6,11 @@ export class ExportTransactionDto {
@IsNotEmpty()
dateEnd: string;
@IsOptional()
supplier: string;
@IsOptional()
buyer: string;
}

View File

@ -23,7 +23,7 @@ export class ExcelController {
@Post('history-user/export/:id')
@Header('Content-Type', 'text/xlsx')
async exportTransactionHistory(
@Param('id', ParseUUIDPipe) id: string,
@Param('id') id: string,
@Body() exportTransactionDto: ExportTransactionDto,
@Request() req,
@Res() res: Response,
@ -37,4 +37,22 @@ export class ExcelController {
res.download(`${result}`);
}
@Post('history-user/export-all')
@Header('Content-Type', 'text/xlsx')
async exportTransactionHistoryAll(
@Body() exportTransactionDto: ExportTransactionDto,
@Request() req,
@Res() res: Response,
) {
const result = await this.transactionService.exportDataExcelAll(
exportTransactionDto.dateStart,
exportTransactionDto.dateEnd,
exportTransactionDto.supplier,
exportTransactionDto.buyer,
req.user,
);
res.download(`${result}`);
}
}

View File

@ -2475,6 +2475,257 @@ export class TransactionService {
}
}
async exportDataExcelAll(startDate, endDate, supplier, buyer, currentUser: any) {
let userBySupperior = [];
if (buyer != 'all') {
const userData = await this.userService.findExist(buyer);
if (
userData.roles.id != 'e4dfb6a3-2338-464a-8fb8-5cbc089d4209' &&
userData.roles.id != '21dceea2-416e-4b55-b74c-12605e1f8d1b'
) {
let roleNumber;
if (userData.roles.id == '3196cdf4-ae5f-4677-9bcd-98be35c72321') {
roleNumber = 3;
} else if (userData.roles.id == '3196cdf4-ae5f-4677-9bcd-98be35c72322') {
roleNumber = 2;
} else if (userData.roles.id == 'e4dfb6a3-2348-464a-8fb8-5cbc089d4209') {
roleNumber = 1;
}
const listUser = await this.userService.findAllSubordinate(
userData.id,
roleNumber,
);
if (listUser.length < 1) {
userBySupperior.push(userData.id);
} else {
userBySupperior = listUser;
}
} else {
userBySupperior.push(userData.id);
}
}
const baseQuery = this.transactionRepository
.createQueryBuilder('transaction')
.select('transaction.id', 'id')
.addSelect('transaction.created_at', 'created_at')
.where('transaction.type = 1')
.leftJoinAndMapOne(
'transaction.userData',
UserDetail,
'userData',
'userData.user = transaction.user',
).select(['userData.user'])
.leftJoin('transaction.product_price', 'product_price')
.leftJoin('product_price.product', 'product')
.leftJoin('product.supplier', 'supplier')
.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')
.addSelect('transaction.destination', 'destination')
.addSelect('transaction.supplier_trx_id', 'transaction_code')
.addSelect(`CASE
WHEN "transaction"."status" = 1 THEN 'Success'
WHEN "transaction"."status" = 2 THEN 'Failed'
ELSE 'Pending'
END`, 'status')
.addSelect('transaction.seri_number', 'serial_number')
.addSelect('transaction.partner_trx_id', 'partner_trx_id')
.addSelect('transaction.created_at', 'transaction_date')
.addSelect('transaction.failed_reason', 'failed_reason')
.orderBy('transaction.created_at', 'DESC');
if (buyer != 'all') {
baseQuery.andWhere(
'transaction.user IN (:...id)',
{
id: userBySupperior
},
);
}
if (startDate) {
baseQuery.andWhere(
'transaction.created_at between :startDate and :enDate',
{
startDate: new Date (`${startDate} 00:00:00`),
enDate: new Date (`${endDate} 23:59:59`),
},
);
}
if (supplier) {
baseQuery.andWhere(
'supplier.id = :supplierId',
{
supplierId: supplier
},
);
}
const data = await baseQuery.getRawMany();
// return {
// data,
// };
try {
let rows = [];
// First create the array of keys/net_total so that we can sort it:
var sort_array = [];
for (var key in data) {
sort_array.push({key: key, product_name: data[key].product_name});
}
// Now sort it:
sort_array.sort((x, y) => {
return x.product_name - y.product_name;
});
let dataSorted = [];
// Now process that object with it:
for (var i = 0; i < sort_array.length; i++) {
var item = data[sort_array[i].key];
// now do stuff with each item
const moment = require("moment");
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) => {
rows.push(Object.values(doc));
});
//creating a workbook
let book = new Workbook();
//adding a worksheet to workbook
let sheet = book.addWorksheet('Mutasi Transaksi');
//add the header
rows.unshift(Object.keys(dataSorted[0]));
//adding multiple rows in the sheet
sheet.addRows(rows);
//customize column
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)
const tmp = require('tmp');
let File = await new Promise((resolve, reject) => {
tmp.file(
{
discardDescriptor: true,
prefix: `Mutasi Transaksi ${startDate} - ${endDate}`,
postfix: '.xlsx',
mode: parseInt('0600', 8),
},
async (err, file) => {
if (err) throw new BadRequestException(err);
//writing temporary file
book.xlsx
.writeFile(file)
.then((_) => {
resolve(file);
})
.catch((err) => {
throw new BadRequestException(err);
});
},
);
});
//returning the path of file
return File;
} catch (e) {
throw new HttpException('No data to export', HttpStatus.NOT_FOUND);
}
}
async getTotalSell(currentUser) {
const baseQuery = this.transactionRepository
.createQueryBuilder('transactions')