fix: deposit_return and fix transction
This commit is contained in:
@@ -28,7 +28,7 @@ import { AddSaldoSupplier } from './dto/add-saldo-supplier.dto';
|
||||
import { SupplierService } from '../users/supplier/supplier.service';
|
||||
import { ProductHistoryPriceService } from '../product/history-price/history-price.service';
|
||||
import { CommissionService } from '../configurable/commission.service';
|
||||
import { catchError } from 'rxjs';
|
||||
import { DepositReturnDto } from './dto/deposit_return.dto';
|
||||
|
||||
interface JournalEntry {
|
||||
coa_id: string;
|
||||
@@ -88,7 +88,7 @@ export class TransactionService {
|
||||
transactionData.amount = addSaldoSupplier.amount;
|
||||
transactionData.user = userData.id;
|
||||
transactionData.status = statusTransaction.SUCCESS;
|
||||
transactionData.type = typeTransaction.DEPOSIT_IRS;
|
||||
transactionData.type = typeTransaction.DEPOSIT_SUPPLIER;
|
||||
|
||||
await manager.insert(Transactions, transactionData);
|
||||
|
||||
@@ -130,21 +130,18 @@ export class TransactionService {
|
||||
currentUser.username,
|
||||
);
|
||||
|
||||
if (userData.roles.name != 'Admin') {
|
||||
throw new HttpException(
|
||||
{
|
||||
statusCode: HttpStatus.NOT_ACCEPTABLE,
|
||||
error: 'Roles Not Admin',
|
||||
},
|
||||
HttpStatus.NOT_ACCEPTABLE,
|
||||
);
|
||||
}
|
||||
const supplier = await this.supplierService.findByActive();
|
||||
|
||||
try {
|
||||
//GET Supplier
|
||||
const supplier = await this.supplierService.findByCode(
|
||||
distributeTransactionDto.supplier,
|
||||
);
|
||||
if (userData.roles.name != 'Admin') {
|
||||
throw new HttpException(
|
||||
{
|
||||
statusCode: HttpStatus.NOT_ACCEPTABLE,
|
||||
error: 'Roles Not Admin',
|
||||
},
|
||||
HttpStatus.NOT_ACCEPTABLE,
|
||||
);
|
||||
}
|
||||
|
||||
// GET COA
|
||||
const coaAR = await this.coaService.findByTwoUser(
|
||||
@@ -171,6 +168,7 @@ export class TransactionService {
|
||||
transactionData.id = uuid.v4();
|
||||
transactionData.amount = distributeTransactionDto.amount;
|
||||
transactionData.user = userData.id;
|
||||
transactionData.user_destination = distributeTransactionDto.destination;
|
||||
transactionData.status = statusTransaction.SUCCESS;
|
||||
transactionData.type = typeTransaction.DISTRIBUTION;
|
||||
|
||||
@@ -201,11 +199,11 @@ export class TransactionService {
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
return true;
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
async distributeDeposit(
|
||||
@@ -246,6 +244,7 @@ export class TransactionService {
|
||||
transactionData.id = uuid.v4();
|
||||
transactionData.amount = distributeTransactionDto.amount;
|
||||
transactionData.user = userData.id;
|
||||
transactionData.user_destination = distributeTransactionDto.destination;
|
||||
transactionData.status = statusTransaction.SUCCESS;
|
||||
transactionData.type = typeTransaction.DISTRIBUTION;
|
||||
|
||||
@@ -301,7 +300,7 @@ export class TransactionService {
|
||||
|
||||
let supervisorData = [];
|
||||
|
||||
const profit = product_price.mark_up_price - product_price.price;
|
||||
let profit = product_price.mark_up_price - product_price.price;
|
||||
|
||||
if (!userData.partner) {
|
||||
//GET SALES
|
||||
@@ -310,6 +309,9 @@ export class TransactionService {
|
||||
profit,
|
||||
userData,
|
||||
);
|
||||
profit = supervisorData
|
||||
.map((item) => item.credit)
|
||||
.reduce((prev, curr) => prev + curr, 0);
|
||||
}
|
||||
|
||||
//GET COA
|
||||
@@ -380,7 +382,7 @@ export class TransactionService {
|
||||
},
|
||||
{
|
||||
coa_id: coaExpense.id,
|
||||
credit: userData.partner ? 0 : profit,
|
||||
debit: userData.partner ? 0 : profit,
|
||||
},
|
||||
].concat(supervisorData),
|
||||
});
|
||||
@@ -392,6 +394,154 @@ export class TransactionService {
|
||||
return true;
|
||||
}
|
||||
|
||||
async createDepositReturn(currentUser, depositReturnDto: DepositReturnDto) {
|
||||
const userData = await this.userService.findByUsername(
|
||||
currentUser.username,
|
||||
);
|
||||
|
||||
try {
|
||||
const transactionData = new Transactions();
|
||||
|
||||
transactionData.id = uuid.v4();
|
||||
transactionData.amount = depositReturnDto.amount;
|
||||
transactionData.user = userData.id;
|
||||
transactionData.user_destination = depositReturnDto.destination;
|
||||
transactionData.status = statusTransaction.PENDING;
|
||||
transactionData.type = typeTransaction.DEPOSIT_RETURN;
|
||||
await this.connection.transaction(async (manager) => {
|
||||
await manager.insert(Transactions, transactionData);
|
||||
});
|
||||
|
||||
return transactionData;
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
async confirmationDepositReturn(
|
||||
id: string,
|
||||
userData,
|
||||
statusApproval: string,
|
||||
) {
|
||||
const transactionData = await this.findApprovalDepositReturn(id);
|
||||
|
||||
const coaSenderWallet = await this.coaService.findByUser(
|
||||
transactionData.user_destination,
|
||||
coaType.WALLET,
|
||||
);
|
||||
|
||||
const coaAP = await this.coaService.findByUserWithRelated(
|
||||
userData.userId,
|
||||
transactionData.user_destination,
|
||||
coaType.ACCOUNT_PAYABLE,
|
||||
);
|
||||
|
||||
const coaReceiverWallet = await this.coaService.findByUser(
|
||||
transactionData.user_destination,
|
||||
coaType.WALLET,
|
||||
);
|
||||
|
||||
const coaAR = await this.coaService.findByUserWithRelated(
|
||||
userData.userId,
|
||||
transactionData.user_destination,
|
||||
coaType.ACCOUNT_RECEIVABLE,
|
||||
);
|
||||
|
||||
try {
|
||||
await this.connection.transaction(async (manager) => {
|
||||
transactionData.status =
|
||||
statusApproval === 'Accept'
|
||||
? statusTransaction.APPROVED
|
||||
: statusTransaction.REJECTED;
|
||||
|
||||
await manager.save(transactionData);
|
||||
|
||||
await this.accountingTransaction({
|
||||
createTransaction: false,
|
||||
transactionalEntityManager: manager,
|
||||
transaction: transactionData,
|
||||
amount: transactionData.amount,
|
||||
journals: [
|
||||
{
|
||||
coa_id: coaSenderWallet.id,
|
||||
credit: transactionData.amount,
|
||||
},
|
||||
{
|
||||
coa_id: coaReceiverWallet.id,
|
||||
debit: transactionData.amount,
|
||||
},
|
||||
{
|
||||
coa_id: coaAR.id,
|
||||
credit: transactionData.amount,
|
||||
},
|
||||
{
|
||||
coa_id: coaAP.id,
|
||||
debit: transactionData.amount,
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
return transactionData;
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
return transactionData;
|
||||
}
|
||||
|
||||
async confirmationAdminDepositReturn(
|
||||
id: string,
|
||||
userData,
|
||||
statusApproval: string,
|
||||
) {
|
||||
const transactionData = await this.findApprovalDepositReturn(id);
|
||||
|
||||
const coaAR = await this.coaService.findByUserWithRelated(
|
||||
userData.userId,
|
||||
transactionData.user_destination,
|
||||
coaType.ACCOUNT_RECEIVABLE,
|
||||
);
|
||||
|
||||
const coaBank = await this.coaService.findByName(
|
||||
`${coaType[coaType.BANK]}-SYSTEM`,
|
||||
);
|
||||
|
||||
try {
|
||||
await this.connection.transaction(async (manager) => {
|
||||
transactionData.status =
|
||||
statusApproval === 'Accept'
|
||||
? statusTransaction.APPROVED
|
||||
: statusTransaction.REJECTED;
|
||||
|
||||
await manager.save(transactionData);
|
||||
|
||||
await this.accountingTransaction({
|
||||
createTransaction: false,
|
||||
transactionalEntityManager: manager,
|
||||
transaction: transactionData,
|
||||
amount: transactionData.amount,
|
||||
journals: [
|
||||
{
|
||||
coa_id: coaAR.id,
|
||||
credit: transactionData.amount,
|
||||
},
|
||||
{
|
||||
coa_id: coaBank.id,
|
||||
debit: transactionData.amount,
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
return transactionData;
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
return transactionData;
|
||||
}
|
||||
|
||||
async transactionHistoryByUser(page: number, user: string) {
|
||||
const baseQuery = this.transactionRepository
|
||||
.createQueryBuilder('transaction')
|
||||
@@ -422,6 +572,58 @@ export class TransactionService {
|
||||
};
|
||||
}
|
||||
|
||||
async findApprovalDepositReturn(id: string) {
|
||||
try {
|
||||
return await this.transactionRepository.findOneOrFail({
|
||||
where: {
|
||||
id: id,
|
||||
type: typeTransaction.DEPOSIT_RETURN,
|
||||
status: statusTransaction.PENDING,
|
||||
},
|
||||
});
|
||||
} catch (e) {
|
||||
if (e instanceof EntityNotFoundError) {
|
||||
throw new HttpException(
|
||||
{
|
||||
statusCode: HttpStatus.NOT_FOUND,
|
||||
error: 'Return Deposit not found',
|
||||
},
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async getAllDepositReturnFromUser(user: string, page: number) {
|
||||
return this.transactionRepository.findAndCount({
|
||||
skip: page * 10,
|
||||
take: 10,
|
||||
where: {
|
||||
user: user,
|
||||
type: typeTransaction.DEPOSIT_RETURN,
|
||||
},
|
||||
order: {
|
||||
createdAt: 'DESC',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async getAllDepositReturnToUser(user: string, page: number) {
|
||||
return this.transactionRepository.findAndCount({
|
||||
skip: page * 10,
|
||||
take: 10,
|
||||
where: {
|
||||
user_destination: user,
|
||||
type: typeTransaction.DEPOSIT_RETURN,
|
||||
},
|
||||
order: {
|
||||
createdAt: 'DESC',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async calculateCommission(data, totalPrice, userData) {
|
||||
const supervisorData = [];
|
||||
|
||||
@@ -436,25 +638,21 @@ export class TransactionService {
|
||||
),
|
||||
);
|
||||
|
||||
//GET Admin
|
||||
supervisorData.push(
|
||||
await this.userService.findByUsername(
|
||||
supervisorData[1].superior.username,
|
||||
),
|
||||
return Promise.all(
|
||||
supervisorData.map(async (it) => {
|
||||
const coaAccount = await this.coaService.findByUser(
|
||||
it.id,
|
||||
coaType.WALLET,
|
||||
);
|
||||
const commissionValue = await this.commissionService.findOne(
|
||||
it.roles.id,
|
||||
);
|
||||
return {
|
||||
coa_id: coaAccount.id,
|
||||
credit: (totalPrice * commissionValue.commission) / 100,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
return supervisorData.map(async (it) => {
|
||||
const coaAccount = await this.coaService.findByUser(
|
||||
it.id,
|
||||
coaType.WALLET,
|
||||
);
|
||||
const commissionValue = await this.commissionService.findOne(it.role.id);
|
||||
|
||||
return {
|
||||
coa_id: coaAccount.id,
|
||||
credit: totalPrice * commissionValue.commission,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
async accountingTransaction(createJournalDto: CreateJournalDto) {
|
||||
|
||||
Reference in New Issue
Block a user