add: api for dashboard
This commit is contained in:
parent
6d869eee25
commit
b63ba1063c
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -910,10 +910,13 @@ export class TransactionService {
|
|||
.addSelect('product.id', 'product_id');
|
||||
|
||||
if (startDate && endDate) {
|
||||
baseQuery.andWhere('transaction.created_at between :startDate and :enDate', {
|
||||
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) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user