fix: order history
This commit is contained in:
@@ -10,12 +10,7 @@ import { CoaService } from './coa.service';
|
||||
import * as uuid from 'uuid';
|
||||
import { uniq } from 'lodash';
|
||||
import { Decimal } from 'decimal.js';
|
||||
import {
|
||||
balanceType,
|
||||
coaType,
|
||||
statusTransaction,
|
||||
typeTransaction,
|
||||
} from '../helper/enum-list';
|
||||
import { balanceType, coaType, statusTransaction, typeTransaction } from '../helper/enum-list';
|
||||
import { ProductService } from '../product/product.service';
|
||||
import { CreateJournalDto } from './dto/create-journal.dto';
|
||||
import { UsersService } from 'src/users/users.service';
|
||||
@@ -28,12 +23,6 @@ import { UserDetail } from '../users/entities/user_detail.entity';
|
||||
import { doTransaction } from '../helper/irs-api';
|
||||
import { ProductHistoryPrice } from '../product/entities/product-history-price.entity';
|
||||
|
||||
interface JournalEntry {
|
||||
coa_id: string;
|
||||
debit?: string;
|
||||
credit?: string;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class TransactionService {
|
||||
private readonly logger = new Logger(TransactionService.name);
|
||||
@@ -387,7 +376,7 @@ export class TransactionService {
|
||||
transactionData.status = statusTransaction.SUCCESS;
|
||||
transactionData.type = typeTransaction.ORDER;
|
||||
transactionData.product_price = product_price;
|
||||
transactionData.phone_number = orderTransactionDto.phoneNumber;
|
||||
transactionData.destination = orderTransactionDto.destination;
|
||||
await manager.insert(Transactions, transactionData);
|
||||
|
||||
await this.accountingTransaction({
|
||||
@@ -409,6 +398,7 @@ export class TransactionService {
|
||||
debit: product_price.mark_up_price + product_price.price,
|
||||
},
|
||||
{
|
||||
// eslint-disable-next-line camelcase
|
||||
coa_id: coaSales.id,
|
||||
credit: product_price.mark_up_price + product_price.price,
|
||||
},
|
||||
@@ -444,12 +434,14 @@ export class TransactionService {
|
||||
//TODO HIT API SUPPLIER
|
||||
const trxId = Array(6)
|
||||
.fill(null)
|
||||
.map(() => Math.round(Math.random() * 16).toString(16))
|
||||
.map(() => {
|
||||
return Math.round(Math.random() * 16).toString(16);
|
||||
})
|
||||
.join('');
|
||||
|
||||
const hitSupplier = await doTransaction(
|
||||
orderTransactionDto.productCode,
|
||||
orderTransactionDto.phoneNumber,
|
||||
orderTransactionDto.destination,
|
||||
trxId,
|
||||
);
|
||||
|
||||
@@ -465,7 +457,7 @@ export class TransactionService {
|
||||
transactionData.user = userData.id;
|
||||
transactionData.type = typeTransaction.ORDER;
|
||||
transactionData.product_price = product_price;
|
||||
transactionData.phone_number = orderTransactionDto.phoneNumber;
|
||||
transactionData.destination = orderTransactionDto.destination;
|
||||
transactionData.supplier_trx_id = trxId;
|
||||
|
||||
if (!hitSupplier.success) {
|
||||
@@ -487,6 +479,7 @@ export class TransactionService {
|
||||
product_price.endDate = new Date();
|
||||
|
||||
let newProductPrice = new ProductHistoryPrice();
|
||||
|
||||
newProductPrice = product_price;
|
||||
newProductPrice.id = uuid.v4();
|
||||
newProductPrice.price = hitSupplier.harga;
|
||||
@@ -653,6 +646,7 @@ export class TransactionService {
|
||||
supplier_trx_id: supplier_trx_id,
|
||||
},
|
||||
});
|
||||
|
||||
dataTransaction.status = statusTransaction.FAILED;
|
||||
dataTransaction.callback_json = callback;
|
||||
|
||||
@@ -666,6 +660,7 @@ export class TransactionService {
|
||||
},
|
||||
relations: ['product_price'],
|
||||
});
|
||||
|
||||
dataTransaction.status = statusTransaction.FAILED;
|
||||
dataTransaction.callback_json = callback;
|
||||
|
||||
@@ -762,6 +757,51 @@ export class TransactionService {
|
||||
}
|
||||
}
|
||||
|
||||
async withdrawBenefit(user) {
|
||||
const userData = await this.userService.findExist(user);
|
||||
|
||||
const coaProfit = await this.coaService.findByUser(user, coaType.PROFIT);
|
||||
|
||||
const coaBank = await this.coaService.findByName(
|
||||
`${coaType[coaType.BANK]}-SYSTEM`,
|
||||
);
|
||||
|
||||
try {
|
||||
const transactionData = new Transactions();
|
||||
|
||||
transactionData.id = uuid.v4();
|
||||
transactionData.amount = coaProfit.amount;
|
||||
transactionData.user = userData.id;
|
||||
transactionData.status = statusTransaction.APPROVED;
|
||||
transactionData.type = typeTransaction.WITHDRAW;
|
||||
|
||||
await this.connection.transaction(async (manager) => {
|
||||
await manager.insert(Transactions, transactionData);
|
||||
|
||||
await this.accountingTransaction({
|
||||
createTransaction: false,
|
||||
transactionalEntityManager: manager,
|
||||
transaction: transactionData,
|
||||
amount: transactionData.amount,
|
||||
journals: [
|
||||
{
|
||||
coa_id: coaBank.id,
|
||||
credit: transactionData.amount,
|
||||
},
|
||||
{
|
||||
coa_id: coaProfit.id,
|
||||
debit: transactionData.amount,
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
return transactionData;
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
async transactionHistoryByUser(
|
||||
page: number,
|
||||
user: string,
|
||||
@@ -770,26 +810,48 @@ export class TransactionService {
|
||||
pageSize?: number,
|
||||
) {
|
||||
const userData = await this.userService.findExist(user);
|
||||
let filterTransactionDate;
|
||||
|
||||
let userBySupperior = [];
|
||||
if(userData.roles.id != 'e4dfb6a3-2338-464a-8fb8-5cbc089d4209' && userData.roles.id != '21dceea2-416e-4b55-b74c-12605e1f8d1b'){
|
||||
const getListUser = await this.userService.findBySuperrior(userData.id,100)
|
||||
userBySupperior = getListUser.data.map(x => x.id)
|
||||
|
||||
if (
|
||||
userData.roles.id != 'e4dfb6a3-2338-464a-8fb8-5cbc089d4209' &&
|
||||
userData.roles.id != '21dceea2-416e-4b55-b74c-12605e1f8d1b'
|
||||
) {
|
||||
let roleNumber;
|
||||
|
||||
if (userData.roles.id == '3196cdf4-ae5f-4677-9bcd-98be35c72321') {
|
||||
roleNumber = 3;
|
||||
} else if (userData.roles.id == '3196cdf4-ae5f-4677-9bcd-98be35c72322') {
|
||||
roleNumber = 2;
|
||||
} else if (userData.roles.id == 'e4dfb6a3-2348-464a-8fb8-5cbc089d4209') {
|
||||
roleNumber = 1;
|
||||
}
|
||||
|
||||
userBySupperior = await this.userService.findAllSubordinate(
|
||||
userData.id,
|
||||
roleNumber,
|
||||
);
|
||||
} else {
|
||||
userBySupperior.push(user)
|
||||
userBySupperior.push(user);
|
||||
}
|
||||
|
||||
const baseQuery = this.transactionRepository
|
||||
.createQueryBuilder('transaction')
|
||||
.select('transaction.id', 'id')
|
||||
.addSelect('transaction.created_at', 'created_at')
|
||||
.where('transaction.user = IN (:...id) and transaction.type = 1', {
|
||||
.where('transaction.user IN (:...id) and transaction.type = 1', {
|
||||
id: userBySupperior,
|
||||
})
|
||||
.leftJoinAndMapOne(
|
||||
'transaction.userData',
|
||||
UserDetail,
|
||||
'userData',
|
||||
'userData.user = transaction.user',
|
||||
)
|
||||
.leftJoin('transaction.product_price', 'product_price')
|
||||
.leftJoin('product_price.product', 'product')
|
||||
.addSelect('transaction.amount', 'mark_up_price')
|
||||
.addSelect('transaction.amount', 'price')
|
||||
.addSelect('userData.name', 'buyer')
|
||||
.addSelect('product.name', 'name')
|
||||
.addSelect('product.id', 'product_id');
|
||||
|
||||
@@ -921,6 +983,7 @@ export class TransactionService {
|
||||
data,
|
||||
count: totalData,
|
||||
};
|
||||
|
||||
return this.transactionRepository.findAndCount({
|
||||
skip: page * (pageSize || 10),
|
||||
take: pageSize || 10,
|
||||
@@ -934,51 +997,6 @@ export class TransactionService {
|
||||
});
|
||||
}
|
||||
|
||||
async withdrawBenefit(user) {
|
||||
const userData = await this.userService.findExist(user);
|
||||
|
||||
const coaProfit = await this.coaService.findByUser(user, coaType.PROFIT);
|
||||
|
||||
const coaBank = await this.coaService.findByName(
|
||||
`${coaType[coaType.BANK]}-SYSTEM`,
|
||||
);
|
||||
|
||||
try {
|
||||
const transactionData = new Transactions();
|
||||
|
||||
transactionData.id = uuid.v4();
|
||||
transactionData.amount = coaProfit.amount;
|
||||
transactionData.user = userData.id;
|
||||
transactionData.status = statusTransaction.APPROVED;
|
||||
transactionData.type = typeTransaction.WITHDRAW;
|
||||
|
||||
await this.connection.transaction(async (manager) => {
|
||||
await manager.insert(Transactions, transactionData);
|
||||
|
||||
await this.accountingTransaction({
|
||||
createTransaction: false,
|
||||
transactionalEntityManager: manager,
|
||||
transaction: transactionData,
|
||||
amount: transactionData.amount,
|
||||
journals: [
|
||||
{
|
||||
coa_id: coaBank.id,
|
||||
credit: transactionData.amount,
|
||||
},
|
||||
{
|
||||
coa_id: coaProfit.id,
|
||||
debit: transactionData.amount,
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
return transactionData;
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
async getTotalSell() {
|
||||
const { total_amount } = await this.transactionRepository
|
||||
.createQueryBuilder('transactions')
|
||||
|
||||
Reference in New Issue
Block a user