Merge branch 'development' of https://gitlab.com/empatnusabangsa/ppob/ppob-backend into devops-staging
This commit is contained in:
commit
ffdf888d24
|
@ -299,140 +299,157 @@ export class TransactionService {
|
||||||
orderTransactionDto: OrderTransactionDto,
|
orderTransactionDto: OrderTransactionDto,
|
||||||
currentUser: any,
|
currentUser: any,
|
||||||
) {
|
) {
|
||||||
const trxId = Array(6)
|
|
||||||
.fill(null)
|
|
||||||
.map(() => {
|
|
||||||
return Math.round(Math.random() * 16).toString(16);
|
|
||||||
})
|
|
||||||
.join('');
|
|
||||||
|
|
||||||
//GET USER
|
const productData = await this.productService.findOne(
|
||||||
const userData = await this.userService.findByUsername(
|
orderTransactionDto.productCode,
|
||||||
currentUser.username,
|
'prepaid',
|
||||||
);
|
);
|
||||||
|
|
||||||
//GET PRODUCT
|
if (productData.status == 'NOT ACTIVE') {
|
||||||
const product = await this.productService.findOne(
|
|
||||||
orderTransactionDto.productCode,
|
|
||||||
'prepaid'
|
|
||||||
);
|
|
||||||
|
|
||||||
const product_price = await this.productHistoryPriceService.findOne(
|
|
||||||
product.id,
|
|
||||||
userData.partner?.id,
|
|
||||||
);
|
|
||||||
|
|
||||||
let supervisorData = [];
|
|
||||||
let profit = product_price.mark_up_price;
|
|
||||||
|
|
||||||
//GET COA
|
|
||||||
const coaAccount = await this.coaService.findByUser(
|
|
||||||
userData.id,
|
|
||||||
coaType.WALLET,
|
|
||||||
);
|
|
||||||
|
|
||||||
const coaInventory = await this.coaService.findByName(
|
|
||||||
`${coaType[coaType.INVENTORY]}-${product.supplier.code}`,
|
|
||||||
);
|
|
||||||
|
|
||||||
const coaCostOfSales = await this.coaService.findByName(
|
|
||||||
`${coaType[coaType.COST_OF_SALES]}-${product.supplier.code}`,
|
|
||||||
);
|
|
||||||
|
|
||||||
const coaSales = await this.coaService.findByName(
|
|
||||||
`${coaType[coaType.SALES]}-SYSTEM`,
|
|
||||||
);
|
|
||||||
|
|
||||||
const coaExpense = await this.coaService.findByName(
|
|
||||||
`${coaType[coaType.EXPENSE]}-SYSTEM`,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!userData.partner) {
|
|
||||||
//GET SALES
|
|
||||||
supervisorData = await this.calculateCommission(
|
|
||||||
supervisorData,
|
|
||||||
profit,
|
|
||||||
userData,
|
|
||||||
);
|
|
||||||
profit = supervisorData
|
|
||||||
.map((item) => {
|
|
||||||
return item.credit;
|
|
||||||
})
|
|
||||||
.reduce((prev, curr) => {
|
|
||||||
return prev + curr;
|
|
||||||
}, 0);
|
|
||||||
|
|
||||||
supervisorData = supervisorData.concat([
|
|
||||||
{
|
|
||||||
coa_id: coaExpense.id,
|
|
||||||
debit: profit,
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (coaAccount.amount < product_price.mark_up_price + product_price.price) {
|
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
{
|
{
|
||||||
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
|
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
error: `Transaction Failed because saldo not enough`,
|
error: `Transaction Failed because product is not active`,
|
||||||
},
|
},
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
);
|
);
|
||||||
}
|
} else {
|
||||||
|
|
||||||
try {
|
const trxId = Array(6)
|
||||||
await this.connection.transaction(async (manager) => {
|
.fill(null)
|
||||||
const transactionData = new Transactions();
|
.map(() => {
|
||||||
|
return Math.round(Math.random() * 16).toString(16);
|
||||||
|
})
|
||||||
|
.join('');
|
||||||
|
|
||||||
transactionData.id = uuid.v4();
|
//GET USER
|
||||||
transactionData.amount =
|
const userData = await this.userService.findByUsername(
|
||||||
product_price.mark_up_price + product_price.price;
|
currentUser.username,
|
||||||
transactionData.user = userData.id;
|
);
|
||||||
transactionData.status = statusTransaction.SUCCESS;
|
|
||||||
transactionData.type = typeTransaction.ORDER;
|
|
||||||
transactionData.product_price = product_price;
|
|
||||||
transactionData.destination = orderTransactionDto.destination;
|
|
||||||
transactionData.partner_trx_id = orderTransactionDto.trx_id;
|
|
||||||
transactionData.supplier_trx_id = trxId;
|
|
||||||
await manager.insert(Transactions, transactionData);
|
|
||||||
|
|
||||||
await this.accountingTransaction({
|
//GET PRODUCT
|
||||||
createTransaction: false,
|
const product = await this.productService.findOne(
|
||||||
transactionalEntityManager: manager,
|
orderTransactionDto.productCode,
|
||||||
transaction: transactionData,
|
'prepaid'
|
||||||
amount: transactionData.amount,
|
);
|
||||||
journals: [
|
|
||||||
|
const product_price = await this.productHistoryPriceService.findOne(
|
||||||
|
product.id,
|
||||||
|
userData.partner?.id,
|
||||||
|
);
|
||||||
|
|
||||||
|
let supervisorData = [];
|
||||||
|
let profit = product_price.mark_up_price;
|
||||||
|
|
||||||
|
//GET COA
|
||||||
|
const coaAccount = await this.coaService.findByUser(
|
||||||
|
userData.id,
|
||||||
|
coaType.WALLET,
|
||||||
|
);
|
||||||
|
|
||||||
|
const coaInventory = await this.coaService.findByName(
|
||||||
|
`${coaType[coaType.INVENTORY]}-${product.supplier.code}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
const coaCostOfSales = await this.coaService.findByName(
|
||||||
|
`${coaType[coaType.COST_OF_SALES]}-${product.supplier.code}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
const coaSales = await this.coaService.findByName(
|
||||||
|
`${coaType[coaType.SALES]}-SYSTEM`,
|
||||||
|
);
|
||||||
|
|
||||||
|
const coaExpense = await this.coaService.findByName(
|
||||||
|
`${coaType[coaType.EXPENSE]}-SYSTEM`,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!userData.partner) {
|
||||||
|
//GET SALES
|
||||||
|
supervisorData = await this.calculateCommission(
|
||||||
|
supervisorData,
|
||||||
|
profit,
|
||||||
|
userData,
|
||||||
|
);
|
||||||
|
profit = supervisorData
|
||||||
|
.map((item) => {
|
||||||
|
return item.credit;
|
||||||
|
})
|
||||||
|
.reduce((prev, curr) => {
|
||||||
|
return prev + curr;
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
supervisorData = supervisorData.concat([
|
||||||
|
{
|
||||||
|
coa_id: coaExpense.id,
|
||||||
|
debit: profit,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (coaAccount.amount < product_price.mark_up_price + product_price.price) {
|
||||||
|
throw new HttpException(
|
||||||
{
|
{
|
||||||
coa_id: coaInventory.id,
|
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
credit: product_price.price,
|
error: `Transaction Failed because saldo not enough`,
|
||||||
},
|
},
|
||||||
{
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
coa_id: coaCostOfSales.id,
|
);
|
||||||
debit: product_price.price,
|
}
|
||||||
},
|
|
||||||
{
|
try {
|
||||||
coa_id: coaAccount.id,
|
await this.connection.transaction(async (manager) => {
|
||||||
debit: product_price.mark_up_price + product_price.price,
|
const transactionData = new Transactions();
|
||||||
},
|
|
||||||
{
|
transactionData.id = uuid.v4();
|
||||||
// eslint-disable-next-line camelcase
|
transactionData.amount =
|
||||||
coa_id: coaSales.id,
|
product_price.mark_up_price + product_price.price;
|
||||||
credit: product_price.mark_up_price + product_price.price,
|
transactionData.user = userData.id;
|
||||||
},
|
transactionData.status = statusTransaction.SUCCESS;
|
||||||
].concat(supervisorData),
|
transactionData.type = typeTransaction.ORDER;
|
||||||
|
transactionData.product_price = product_price;
|
||||||
|
transactionData.destination = orderTransactionDto.destination;
|
||||||
|
transactionData.partner_trx_id = orderTransactionDto.trx_id;
|
||||||
|
transactionData.supplier_trx_id = trxId;
|
||||||
|
await manager.insert(Transactions, transactionData);
|
||||||
|
|
||||||
|
await this.accountingTransaction({
|
||||||
|
createTransaction: false,
|
||||||
|
transactionalEntityManager: manager,
|
||||||
|
transaction: transactionData,
|
||||||
|
amount: transactionData.amount,
|
||||||
|
journals: [
|
||||||
|
{
|
||||||
|
coa_id: coaInventory.id,
|
||||||
|
credit: product_price.price,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
coa_id: coaCostOfSales.id,
|
||||||
|
debit: product_price.price,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
coa_id: coaAccount.id,
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
].concat(supervisorData),
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
} catch (e) {
|
||||||
} catch (e) {
|
throw e;
|
||||||
throw e;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
trx_id: trxId,
|
trx_id: trxId,
|
||||||
client_trx_id: orderTransactionDto.trx_id,
|
client_trx_id: orderTransactionDto.trx_id,
|
||||||
product: orderTransactionDto.productCode,
|
product: orderTransactionDto.productCode,
|
||||||
amount: product_price.mark_up_price + product_price.price,
|
amount: product_price.mark_up_price + product_price.price,
|
||||||
status: statusTransaction[statusTransaction.SUCCESS],
|
status: statusTransaction[statusTransaction.SUCCESS],
|
||||||
};
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async orderTransactionProd(
|
async orderTransactionProd(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user