add: api for dashboard

This commit is contained in:
ilham 2022-01-02 16:54:24 +07:00
parent 6d869eee25
commit b63ba1063c
3 changed files with 101 additions and 15 deletions

View File

@ -1,4 +1,4 @@
import { Column, Entity, ManyToOne } from 'typeorm'; import { Column, Entity, ManyToOne, OneToMany } from 'typeorm';
import { BaseModel } from '../../config/basemodel.entity'; import { BaseModel } from '../../config/basemodel.entity';
import { statusTransaction, typeTransaction } from '../../helper/enum-list'; import { statusTransaction, typeTransaction } from '../../helper/enum-list';
import { ProductHistoryPrice } from '../../product/entities/product-history-price.entity'; import { ProductHistoryPrice } from '../../product/entities/product-history-price.entity';
@ -65,9 +65,13 @@ export class Transactions extends BaseModel {
}) })
callback_json: string; callback_json: string;
@OneToMany(
() => TransactionJournal,
(transaction_journal) => transaction_journal.transaction_head,
)
transactionJournal: TransactionJournal[];
mark_up_price: number; mark_up_price: number;
userData: UserDetail; userData: UserDetail;
transactionJournal: TransactionJournal;
} }

View File

@ -169,7 +169,29 @@ export class TransactionController {
@Get('total-order') @Get('total-order')
async findTotalOrder(@Request() req) { 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 { return {
data, data,

View File

@ -901,7 +901,7 @@ export class TransactionService {
.leftJoin('product_price.product', 'product') .leftJoin('product_price.product', 'product')
.addSelect('transaction.amount', 'price') .addSelect('transaction.amount', 'price')
.addSelect('transaction.destination') .addSelect('transaction.destination')
.addSelect('transaction.seri_number','seri_number') .addSelect('transaction.seri_number', 'seri_number')
.addSelect('transaction.supplier_trx_id', 'transaction_code') .addSelect('transaction.supplier_trx_id', 'transaction_code')
.addSelect('transaction.status', 'status') .addSelect('transaction.status', 'status')
.addSelect('transaction.partner_trx_id', 'partner_transaction_code') .addSelect('transaction.partner_trx_id', 'partner_transaction_code')
@ -910,10 +910,13 @@ export class TransactionService {
.addSelect('product.id', 'product_id'); .addSelect('product.id', 'product_id');
if (startDate && endDate) { if (startDate && endDate) {
baseQuery.andWhere('transaction.created_at between :startDate and :enDate', { baseQuery.andWhere(
startDate: new Date(startDate), 'transaction.created_at between :startDate and :enDate',
enDate: new Date(endDate), {
}); startDate: new Date(startDate),
enDate: new Date(endDate),
},
);
} }
const data = await baseQuery const data = await baseQuery
@ -1051,22 +1054,79 @@ export class TransactionService {
}); });
} }
async getTotalSell() { async getTotalSell(currentUser) {
const { total_amount } = await this.transactionRepository const baseQuery = this.transactionRepository
.createQueryBuilder('transactions') .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') .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(); .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() { async getTotalSellB2B(currentUser) {
const { total_amount } = await this.transactionRepository const baseQuery = this.transactionRepository
.createQueryBuilder('transactions') .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') .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(); .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) { async calculateCommission(data, totalPrice, userData) {