add: transaction billing

This commit is contained in:
ilham 2022-05-03 19:09:12 +07:00
parent 49e22932b6
commit 52839223e6
5 changed files with 198 additions and 2 deletions

View File

@ -11,6 +11,9 @@ export class UpdatePriceProductDto {
@IsNotEmpty() @IsNotEmpty()
type: productType; type: productType;
@IsNotEmpty()
productType: string;
startDate: Date; startDate: Date;
endDate: Date; endDate: Date;

View File

@ -28,6 +28,11 @@ export class Product extends BaseModel {
}) })
basePrice: number; basePrice: number;
@Column({
default: 'prepaid',
})
type: string;
@ManyToOne( @ManyToOne(
() => { () => {
return ProductSubCategories; return ProductSubCategories;

View File

@ -124,6 +124,7 @@ export class ProductService {
status: it[5] == 'active' ? 'ACTIVE' : 'NOT ACTIVE', status: it[5] == 'active' ? 'ACTIVE' : 'NOT ACTIVE',
sub_categories: subCategories, sub_categories: subCategories,
supplier: supplierData, supplier: supplierData,
type: it[7] == 'postpaid' ? 'postpaid' : 'prepaid',
}); });
return await this.productHistoryPrice.insert({ return await this.productHistoryPrice.insert({
@ -355,12 +356,13 @@ export class ProductService {
}; };
} }
async findOne(code: string) { async findOne(code: string, type: string) {
try { try {
return await this.productRepository.findOneOrFail({ return await this.productRepository.findOneOrFail({
relations: ['supplier'], relations: ['supplier'],
where: { where: {
code: code, code: code,
type: type,
}, },
}); });
} catch (e) { } catch (e) {
@ -436,7 +438,7 @@ export class ProductService {
code: string, code: string,
updatePriceProductDto: UpdatePriceProductDto, updatePriceProductDto: UpdatePriceProductDto,
) { ) {
const product = await this.findOne(code); const product = await this.findOne(code, updatePriceProductDto.productType);
await this.productHistoryPrice.insert({ await this.productHistoryPrice.insert({
product: product, product: product,

View File

@ -87,6 +87,21 @@ export class TransactionController {
}; };
} }
@Post('order-bill-prod')
async orderTransactioBillnProd(
@Body() orderTransactionDto: OrderTransactionDto,
@Request() req,
) {
return {
data: await this.transactionService.orderTransactionBillProd(
orderTransactionDto,
req.user,
),
statusCode: HttpStatus.CREATED,
message: 'success',
};
}
@Post('check-bill') @Post('check-bill')
async checkBill( async checkBill(
@Body() orderTransactionDto: OrderTransactionDto, @Body() orderTransactionDto: OrderTransactionDto,

View File

@ -312,6 +312,7 @@ export class TransactionService {
//GET PRODUCT //GET PRODUCT
const product = await this.productService.findOne( const product = await this.productService.findOne(
orderTransactionDto.productCode, orderTransactionDto.productCode,
'prepaid'
); );
const product_price = await this.productHistoryPriceService.findOne( const product_price = await this.productHistoryPriceService.findOne(
@ -446,6 +447,7 @@ export class TransactionService {
//GET PRODUCT AND PRICE //GET PRODUCT AND PRICE
const product = await this.productService.findOne( const product = await this.productService.findOne(
orderTransactionDto.productCode, orderTransactionDto.productCode,
'prepaid',
); );
const supplier = await this.supplierService.findByCode( const supplier = await this.supplierService.findByCode(
@ -620,6 +622,174 @@ export class TransactionService {
}; };
} }
async orderTransactionBillProd(
orderTransactionDto: OrderTransactionDto,
currentUser: any,
) {
let status;
const amount = 0;
//GET USER DATA
const userData = await this.userService.findByUsername(
currentUser.username,
);
//GET PRODUCT AND PRICE
const product = await this.productService.findOne(
orderTransactionDto.productCode,
'postpaid',
);
const supplier = await this.supplierService.findByCode(
product.supplier.code,
);
let product_price = await this.productHistoryPriceService.findOne(
product.id,
userData.partner?.id,
);
//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`,
);
if (coaAccount.amount < product_price.mark_up_price + product_price.price) {
throw new HttpException(
{
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
error: `Transaction Failed because saldo not enough`,
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
//HIT API SUPPLIER
const trxId = Array(6)
.fill(null)
.map(() => {
return Math.round(Math.random() * 16).toString(16);
})
.join('');
let hitSupplier = await doTransaction(
orderTransactionDto.productCode,
orderTransactionDto.destination,
trxId,
supplier,
);
if (supplier.code != 'IRS') {
const parsingResponse = hitSupplier.split(' ');
hitSupplier = {
success: hitSupplier.include('diproses'),
harga: parseInt(
parsingResponse[parsingResponse.length - 2].replaceAll('.', ''),
),
msg: hitSupplier,
};
}
// const hitSupplier = {
// harga: 2000,
// success: true,
// msg: 'Berhasil',
// };
this.logger.log({
responseAPISupplier: hitSupplier,
});
let costInventory = product_price.price;
if (hitSupplier.harga != product_price.price) {
product_price.endDate = new Date();
costInventory = hitSupplier.harga;
}
try {
//TRANSACTION DATA
await this.connection.transaction(async (manager) => {
const transactionData = new Transactions();
transactionData.id = uuid.v4();
transactionData.amount =
product_price.mark_up_price + product_price.price;
transactionData.user = userData.id;
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;
if (!hitSupplier.success) {
transactionData.status = statusTransaction.FAILED;
status = statusTransaction[transactionData.status];
await this.transactionRepository.insert(transactionData);
throw new HttpException(
{
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
error: hitSupplier.msg,
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
} else {
transactionData.status = statusTransaction.PENDING;
status = statusTransaction[transactionData.status];
}
await manager.insert(Transactions, transactionData);
await this.accountingTransaction({
createTransaction: false,
transactionalEntityManager: manager,
transaction: transactionData,
amount: transactionData.amount,
journals: [
{
coa_id: coaInventory.id,
credit: costInventory,
},
{
coa_id: coaCostOfSales.id,
debit: costInventory,
},
{
coa_id: coaAccount.id,
debit: product_price.mark_up_price + costInventory,
},
{
// eslint-disable-next-line camelcase
coa_id: coaSales.id,
credit: product_price.mark_up_price + costInventory,
},
],
});
});
} catch (e) {
throw e;
}
return {
trx_id: trxId,
client_trx_id: orderTransactionDto.trx_id,
product: orderTransactionDto.productCode,
amount: product_price.mark_up_price + product_price.price,
status: status,
};
}
async checkBill(orderTransactionDto: OrderTransactionDto, currentUser: any) { async checkBill(orderTransactionDto: OrderTransactionDto, currentUser: any) {
//GET USER DATA //GET USER DATA
const userData = await this.userService.findByUsername( const userData = await this.userService.findByUsername(
@ -629,6 +799,7 @@ export class TransactionService {
//GET PRODUCT AND PRICE //GET PRODUCT AND PRICE
const product = await this.productService.findOne( const product = await this.productService.findOne(
orderTransactionDto.productCode, orderTransactionDto.productCode,
'postpaid',
); );
const supplier = await this.supplierService.findByCode( const supplier = await this.supplierService.findByCode(