From b63ba1063c2b7267979b3e049d31d47e0d9b7416 Mon Sep 17 00:00:00 2001 From: ilham Date: Sun, 2 Jan 2022 16:54:24 +0700 Subject: [PATCH] add: api for dashboard --- .../entities/transactions.entity.ts | 10 ++- src/transaction/transaction.controller.ts | 24 +++++- src/transaction/transaction.service.ts | 82 ++++++++++++++++--- 3 files changed, 101 insertions(+), 15 deletions(-) diff --git a/src/transaction/entities/transactions.entity.ts b/src/transaction/entities/transactions.entity.ts index a8b00c0..092050c 100644 --- a/src/transaction/entities/transactions.entity.ts +++ b/src/transaction/entities/transactions.entity.ts @@ -1,4 +1,4 @@ -import { Column, Entity, ManyToOne } from 'typeorm'; +import { Column, Entity, ManyToOne, OneToMany } from 'typeorm'; import { BaseModel } from '../../config/basemodel.entity'; import { statusTransaction, typeTransaction } from '../../helper/enum-list'; import { ProductHistoryPrice } from '../../product/entities/product-history-price.entity'; @@ -65,9 +65,13 @@ export class Transactions extends BaseModel { }) callback_json: string; + @OneToMany( + () => TransactionJournal, + (transaction_journal) => transaction_journal.transaction_head, + ) + transactionJournal: TransactionJournal[]; + mark_up_price: number; userData: UserDetail; - - transactionJournal: TransactionJournal; } diff --git a/src/transaction/transaction.controller.ts b/src/transaction/transaction.controller.ts index ada8c68..2818c7a 100644 --- a/src/transaction/transaction.controller.ts +++ b/src/transaction/transaction.controller.ts @@ -169,7 +169,29 @@ export class TransactionController { @Get('total-order') async findTotalOrder(@Request() req) { - const data = await this.transactionService.getTotalSell(); + const data = await this.transactionService.getTotalSell(req.user); + + return { + data, + statusCode: HttpStatus.OK, + message: 'success', + }; + } + + @Get('total-order-b2b') + async findTotalOrderB2B(@Request() req) { + const data = await this.transactionService.getTotalSellB2B(req.user); + + return { + data, + statusCode: HttpStatus.OK, + message: 'success', + }; + } + + @Get('total-order-partner') + async findTotalOrderPartner(@Request() req) { + const data = await this.transactionService.getTotalSellPartner(req.user); return { data, diff --git a/src/transaction/transaction.service.ts b/src/transaction/transaction.service.ts index ae6b9e1..62951db 100644 --- a/src/transaction/transaction.service.ts +++ b/src/transaction/transaction.service.ts @@ -901,7 +901,7 @@ export class TransactionService { .leftJoin('product_price.product', 'product') .addSelect('transaction.amount', 'price') .addSelect('transaction.destination') - .addSelect('transaction.seri_number','seri_number') + .addSelect('transaction.seri_number', 'seri_number') .addSelect('transaction.supplier_trx_id', 'transaction_code') .addSelect('transaction.status', 'status') .addSelect('transaction.partner_trx_id', 'partner_transaction_code') @@ -910,10 +910,13 @@ export class TransactionService { .addSelect('product.id', 'product_id'); if (startDate && endDate) { - baseQuery.andWhere('transaction.created_at between :startDate and :enDate', { - startDate: new Date(startDate), - enDate: new Date(endDate), - }); + baseQuery.andWhere( + 'transaction.created_at between :startDate and :enDate', + { + startDate: new Date(startDate), + enDate: new Date(endDate), + }, + ); } const data = await baseQuery @@ -1051,22 +1054,79 @@ export class TransactionService { }); } - async getTotalSell() { - const { total_amount } = await this.transactionRepository + async getTotalSell(currentUser) { + const baseQuery = this.transactionRepository .createQueryBuilder('transactions') + .innerJoin('transactions.product_price', 'product_price') + .where('transactions.type = 1 and partner_trx_id is NULL'); + + const data = await baseQuery .select('SUM(transactions.amount) as total_amount') + .addSelect('SUM(product_price.price) as total_modal') + .addSelect('SUM(product_price.mark_up_price) as total_profit') + .addSelect('COUNT(transactions.id) as total_transaction') .getRawOne(); - return parseInt(total_amount); + const { total_expense } = await baseQuery + .select('SUM(transaction_journal.amount) as total_expense') + .innerJoin('transactions.transactionJournal', 'transaction_journal') + .where( + `transaction_journal.type = '0' and transaction_journal.amount < product_price.price`, + ) + .getRawOne(); + + return { + total_amount: parseInt(data.total_amount), + total_transaction: parseInt(data.total_transaction), + total_modal: parseInt(data.total_modal), + total_profit: parseInt(data.total_profit), + total_commission: parseInt(data.total_profit) - parseInt(total_expense), + }; } - async getTotalProfit() { - const { total_amount } = await this.transactionRepository + async getTotalSellB2B(currentUser) { + const baseQuery = this.transactionRepository .createQueryBuilder('transactions') + .innerJoin('transactions.product_price', 'product_price') + .where('transactions.type = 1 and partner_trx_id is not NULL'); + + const data = await baseQuery .select('SUM(transactions.amount) as total_amount') + .addSelect('SUM(product_price.price) as total_modal') + .addSelect('SUM(product_price.mark_up_price) as total_profit') + .addSelect('COUNT(transactions.id) as total_transaction') .getRawOne(); - return parseInt(total_amount); + return { + total_amount: parseInt(data.total_amount), + total_transaction: parseInt(data.total_transaction), + total_modal: parseInt(data.total_modal), + total_profit: parseInt(data.total_profit), + }; + } + + async getTotalSellPartner(currentUser) { + const userData = await this.userService.findByUsername( + currentUser.username, + ); + + const baseQuery = this.transactionRepository + .createQueryBuilder('transactions') + .innerJoin('transactions.product_price', 'product_price') + .where('transactions.type = 1') + .andWhere('transactions.user = :id', { + id: userData.id, + }); + + const data = await baseQuery + .select('SUM(transactions.amount) as total_amount') + .addSelect('COUNT(transactions.id) as total_transaction') + .getRawOne(); + + return { + total_amount: parseInt(data.total_amount), + total_transaction: parseInt(data.total_transaction), + }; } async calculateCommission(data, totalPrice, userData) {