fix: get tranasction history by user
This commit is contained in:
parent
53575f36d6
commit
8c5e944cc8
18
src/product/history-price/history-price.service.spec.ts
Normal file
18
src/product/history-price/history-price.service.spec.ts
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
import { HistoryPriceService } from './history-price.service';
|
||||||
|
|
||||||
|
describe('HistoryPriceService', () => {
|
||||||
|
let service: HistoryPriceService;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
providers: [HistoryPriceService],
|
||||||
|
}).compile();
|
||||||
|
|
||||||
|
service = module.get<HistoryPriceService>(HistoryPriceService);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be defined', () => {
|
||||||
|
expect(service).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
37
src/product/history-price/history-price.service.ts
Normal file
37
src/product/history-price/history-price.service.ts
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
|
import { EntityNotFoundError, IsNull, Repository } from 'typeorm';
|
||||||
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
|
import { ProductCategories } from '../entities/product-category.entity';
|
||||||
|
import { ProductHistoryPrice } from '../entities/product-history-price.entity';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class ProductHistoryPriceService {
|
||||||
|
constructor(
|
||||||
|
@InjectRepository(ProductHistoryPrice)
|
||||||
|
private productHistoryPriceService: Repository<ProductHistoryPrice>,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async findOne(product: string, partner: string) {
|
||||||
|
try {
|
||||||
|
return await this.productHistoryPriceService.findOneOrFail({
|
||||||
|
where: {
|
||||||
|
product: product,
|
||||||
|
endDate: IsNull(),
|
||||||
|
partner: partner ? partner : IsNull(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof EntityNotFoundError) {
|
||||||
|
throw new HttpException(
|
||||||
|
{
|
||||||
|
statusCode: HttpStatus.NOT_FOUND,
|
||||||
|
error: 'Data not found',
|
||||||
|
},
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,6 +12,8 @@ import {
|
||||||
} from 'typeorm';
|
} 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 { Partner } from '../../users/entities/partner.entity';
|
||||||
|
import { ProductHistoryPrice } from '../../product/entities/product-history-price.entity';
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
export class Transactions extends BaseModel {
|
export class Transactions extends BaseModel {
|
||||||
|
@ -31,4 +33,9 @@ export class Transactions extends BaseModel {
|
||||||
nullable: true,
|
nullable: true,
|
||||||
})
|
})
|
||||||
user_destination: string;
|
user_destination: string;
|
||||||
|
|
||||||
|
@ManyToOne(() => ProductHistoryPrice, (product) => product.id)
|
||||||
|
product_price: ProductHistoryPrice;
|
||||||
|
|
||||||
|
mark_up_price: number;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import {
|
||||||
Delete,
|
Delete,
|
||||||
Request,
|
Request,
|
||||||
HttpStatus,
|
HttpStatus,
|
||||||
|
Query,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { TransactionService } from './transaction.service';
|
import { TransactionService } from './transaction.service';
|
||||||
import { DistributeTransactionDto } from './dto/distribute-transaction.dto';
|
import { DistributeTransactionDto } from './dto/distribute-transaction.dto';
|
||||||
|
@ -73,4 +74,18 @@ export class TransactionController {
|
||||||
req.user,
|
req.user,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Get('history')
|
||||||
|
async findByCategories(@Query('page') page: number, @Request() req) {
|
||||||
|
const data = await this.transactionService.transactionHistoryByUser(
|
||||||
|
page,
|
||||||
|
req.user.userId,
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...data,
|
||||||
|
statusCode: HttpStatus.OK,
|
||||||
|
message: 'success',
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -123,6 +123,7 @@ export class TransactionService {
|
||||||
const userData = await this.userService.findByUsername(
|
const userData = await this.userService.findByUsername(
|
||||||
currentUser.username,
|
currentUser.username,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (userData.roles.name != 'Admin') {
|
if (userData.roles.name != 'Admin') {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
{
|
{
|
||||||
|
@ -289,11 +290,13 @@ export class TransactionService {
|
||||||
|
|
||||||
let supervisorData = [];
|
let supervisorData = [];
|
||||||
|
|
||||||
|
const profit = product_price.mark_up_price - product_price.price;
|
||||||
|
|
||||||
if (!userData.partner) {
|
if (!userData.partner) {
|
||||||
//GET SALES
|
//GET SALES
|
||||||
supervisorData = await this.calculateCommission(
|
supervisorData = await this.calculateCommission(
|
||||||
supervisorData,
|
supervisorData,
|
||||||
product_price.mark_up_price - product_price.price,
|
profit,
|
||||||
userData,
|
userData,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -339,6 +342,7 @@ export class TransactionService {
|
||||||
transactionData.user = userData.id;
|
transactionData.user = userData.id;
|
||||||
transactionData.status = statusTransaction.SUCCESS;
|
transactionData.status = statusTransaction.SUCCESS;
|
||||||
transactionData.type = typeTransaction.ORDER;
|
transactionData.type = typeTransaction.ORDER;
|
||||||
|
transactionData.product_price = product_price;
|
||||||
await manager.insert(Transactions, transactionData);
|
await manager.insert(Transactions, transactionData);
|
||||||
|
|
||||||
await this.accountingTransaction({
|
await this.accountingTransaction({
|
||||||
|
@ -365,9 +369,7 @@ export class TransactionService {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
coa_id: coaExpense.id,
|
coa_id: coaExpense.id,
|
||||||
credit: userData.partner
|
credit: userData.partner ? 0 : profit,
|
||||||
? 0
|
|
||||||
: product_price.mark_up_price - product_price.price,
|
|
||||||
},
|
},
|
||||||
].concat(supervisorData),
|
].concat(supervisorData),
|
||||||
});
|
});
|
||||||
|
@ -379,8 +381,39 @@ export class TransactionService {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async transactionHistoryByUser(page: number, user: string) {
|
||||||
|
const baseQuery = this.transactionRepository
|
||||||
|
.createQueryBuilder('transaction')
|
||||||
|
.select('transaction.id', 'id')
|
||||||
|
.addSelect('transaction.created_at', 'created_at')
|
||||||
|
.where('transaction.user = :id and transaction.type = 1', {
|
||||||
|
id: user,
|
||||||
|
})
|
||||||
|
.leftJoin('transaction.product_price', 'product_price')
|
||||||
|
.leftJoin('product_price.product', 'product')
|
||||||
|
.addSelect('product_price.mark_up_price', 'mark_up_price')
|
||||||
|
.addSelect('product.name', 'name')
|
||||||
|
.addSelect('product.id', 'product_id');
|
||||||
|
|
||||||
|
// .leftJoinAndSelect('transaction.product_price', 'product_price')
|
||||||
|
// .leftJoinAndSelect('product_price.product', 'product');
|
||||||
|
|
||||||
|
const data = await baseQuery
|
||||||
|
.skip(page * 10)
|
||||||
|
.take(10)
|
||||||
|
.getRawMany();
|
||||||
|
|
||||||
|
const totalData = await baseQuery.getCount();
|
||||||
|
|
||||||
|
return {
|
||||||
|
data,
|
||||||
|
count: totalData,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async calculateCommission(data, totalPrice, userData) {
|
async calculateCommission(data, totalPrice, userData) {
|
||||||
let supervisorData = [];
|
const supervisorData = [];
|
||||||
|
|
||||||
supervisorData.push(
|
supervisorData.push(
|
||||||
await this.userService.findByUsername(userData.superior.username),
|
await this.userService.findByUsername(userData.superior.username),
|
||||||
);
|
);
|
||||||
|
@ -498,17 +531,20 @@ export class TransactionService {
|
||||||
return a.plus(b);
|
return a.plus(b);
|
||||||
}, new Decimal(0));
|
}, new Decimal(0));
|
||||||
|
|
||||||
let coa = coas.find(
|
const coa = coas.find((it) => {
|
||||||
(it) => it.id.toLowerCase() === coaId.toLowerCase(),
|
return it.id.toLowerCase() === coaId.toLowerCase();
|
||||||
);
|
});
|
||||||
|
|
||||||
let balance = new Decimal(coa.amount);
|
let balance = new Decimal(coa.amount);
|
||||||
|
|
||||||
if (coa.balanceType == balanceType.DEBIT) {
|
if (coa.balanceType == balanceType.DEBIT) {
|
||||||
balance = balance.plus(debitSum.minus(creditSum));
|
balance = balance.plus(debitSum.minus(creditSum));
|
||||||
} else if (coa.balanceType == balanceType.CREDIT) {
|
} else if (coa.balanceType == balanceType.CREDIT) {
|
||||||
balance = balance.plus(creditSum.minus(debitSum));
|
balance = balance.plus(creditSum.minus(debitSum));
|
||||||
}
|
}
|
||||||
|
|
||||||
const diff = balance.minus(new Decimal(coa.amount));
|
const diff = balance.minus(new Decimal(coa.amount));
|
||||||
|
|
||||||
return createJournalDto.transactionalEntityManager
|
return createJournalDto.transactionalEntityManager
|
||||||
.createQueryBuilder()
|
.createQueryBuilder()
|
||||||
.update(COA)
|
.update(COA)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user