add saldo IRS
This commit is contained in:
parent
84fda27e2d
commit
1f44cf484c
15
.vscode/launch.json
vendored
Normal file
15
.vscode/launch.json
vendored
Normal file
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "pwa-chrome",
|
||||
"request": "launch",
|
||||
"name": "Launch Chrome against localhost",
|
||||
"url": "http://localhost:8080",
|
||||
"webRoot": "${workspaceFolder}"
|
||||
}
|
||||
]
|
||||
}
|
|
@ -8,7 +8,6 @@ export enum typeTransaction {
|
|||
DISTRIBUTION,
|
||||
ORDER,
|
||||
DEPOSIT_IRS,
|
||||
|
||||
}
|
||||
|
||||
export enum productType {
|
||||
|
|
|
@ -50,4 +50,22 @@ export class CoaService {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
async findByName(name: string) {
|
||||
try {
|
||||
return await this.coaRepository.findOneOrFail({ name: name });
|
||||
} catch (e) {
|
||||
if (e instanceof EntityNotFoundError) {
|
||||
throw new HttpException(
|
||||
{
|
||||
statusCode: HttpStatus.NOT_FOUND,
|
||||
error: 'COA Data not found',
|
||||
},
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
12
src/transaction/dto/add-saldo-supplier.dto.ts
Normal file
12
src/transaction/dto/add-saldo-supplier.dto.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
import { IsNotEmpty, IsUUID } from 'class-validator';
|
||||
import { balanceType, coaType, statusTransaction, typeTransaction } from 'src/helper/enum-list';
|
||||
import { EntityManager } from 'typeorm';
|
||||
|
||||
|
||||
export class AddSaldoSupplier {
|
||||
@IsNotEmpty()
|
||||
supplier?: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
amount?: number;
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
import { IsNotEmpty, IsUUID } from 'class-validator';
|
||||
import { balanceType, coaType, statusTransaction, typeTransaction } from 'src/helper/enum-list';
|
||||
import { EntityManager } from 'typeorm';
|
||||
import { Transactions } from '../entities/transactions.entity';
|
||||
|
||||
interface JournalEntry {
|
||||
coa_id: string;
|
||||
|
@ -19,7 +20,7 @@ export class CreateJournalDto {
|
|||
userId?: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
transactionId?: string;
|
||||
transaction?: Transactions;
|
||||
|
||||
@IsNotEmpty()
|
||||
type?: typeTransaction;
|
||||
|
|
|
@ -6,11 +6,14 @@ import {
|
|||
Patch,
|
||||
Param,
|
||||
Delete,
|
||||
Request,
|
||||
HttpStatus
|
||||
} from '@nestjs/common';
|
||||
import { TransactionService } from './transaction.service';
|
||||
import { DistributeTransactionDto } from './dto/distribute-transaction.dto';
|
||||
import { OrderTransactionDto } from './dto/order-transaction.dto';
|
||||
import { UpdateTransactionDto } from './dto/update-transaction.dto';
|
||||
import { AddSaldoSupplier } from './dto/add-saldo-supplier.dto';
|
||||
|
||||
@Controller({
|
||||
path: 'transaction',
|
||||
|
@ -24,6 +27,20 @@ export class TransactionController {
|
|||
return this.transactionService.distributeDeposit(createTransactionDto);
|
||||
}
|
||||
|
||||
@Post('add-saldo-supplier')
|
||||
async addSaldoSupplier(
|
||||
@Body() addSaldoSupplier: AddSaldoSupplier,
|
||||
@Request() req
|
||||
) {
|
||||
|
||||
return {
|
||||
data: await this.transactionService.addIRSWallet(addSaldoSupplier,req.user),
|
||||
statusCode: HttpStatus.CREATED,
|
||||
message: 'success',
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Post('order')
|
||||
orderTransaction(@Body() orderTransactionDto: OrderTransactionDto) {
|
||||
return this.transactionService.orderTransaction(orderTransactionDto);
|
||||
|
|
|
@ -20,6 +20,8 @@ import {
|
|||
import { ProductService } from '../product/product.service';
|
||||
import * as irsService from '../helper/irs-service';
|
||||
import { CreateJournalDto } from './dto/create-journal.dto';
|
||||
import { UsersService } from 'src/users/users.service';
|
||||
import { AddSaldoSupplier } from './dto/add-saldo-supplier.dto';
|
||||
|
||||
interface JournalEntry {
|
||||
coa_id: string;
|
||||
|
@ -27,7 +29,6 @@ interface JournalEntry {
|
|||
credit?: string;
|
||||
}
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class TransactionService {
|
||||
constructor(
|
||||
|
@ -41,9 +42,55 @@ export class TransactionService {
|
|||
private coaRepository: Repository<COA>,
|
||||
private coaService: CoaService,
|
||||
private productService: ProductService,
|
||||
private userService: UsersService,
|
||||
private connection: Connection,
|
||||
) {}
|
||||
|
||||
async addIRSWallet(addSaldoSupplier: AddSaldoSupplier,currentUser:any) {
|
||||
// GET COA
|
||||
const coaBank = await this.coaService.findByName(
|
||||
coaType[coaType.BANK]+'-SYSTEM',
|
||||
);
|
||||
|
||||
const coaInventory = await this.coaService.findByName(
|
||||
coaType[coaType.INVENTORY]+'-'+addSaldoSupplier.supplier,
|
||||
);
|
||||
console.log(coaInventory);
|
||||
|
||||
//GET USER
|
||||
const userData = await this.userService.findByUsername(currentUser.username);
|
||||
|
||||
|
||||
await this.connection.transaction(async (manager) => {
|
||||
//INSERT TRANSACTION
|
||||
let transactionData = new Transactions();
|
||||
transactionData.id = uuid.v4();
|
||||
transactionData.amount = addSaldoSupplier.amount,
|
||||
transactionData.user = userData.id,
|
||||
transactionData.status = statusTransaction.SUCCESS,
|
||||
transactionData.type = typeTransaction.DISTRIBUTION,
|
||||
|
||||
await manager.insert(Transactions, transactionData);
|
||||
|
||||
await this.accountingTransaction({
|
||||
createTransaction: false,
|
||||
transactionalEntityManager:manager,
|
||||
transaction: transactionData,
|
||||
amount: transactionData.amount,
|
||||
journals: [{
|
||||
coa_id: coaBank.id,
|
||||
debit: transactionData.amount
|
||||
}, {
|
||||
coa_id: coaInventory.id,
|
||||
credit: transactionData.amount
|
||||
}]
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
async distributeDeposit(distributeTransactionDto: DistributeTransactionDto) {
|
||||
// GET COA
|
||||
const coaSender = await this.coaService.findByUser(
|
||||
|
@ -158,9 +205,6 @@ export class TransactionService {
|
|||
}
|
||||
|
||||
async accountingTransaction(createJournalDto: CreateJournalDto ) {
|
||||
|
||||
createJournalDto.transactionId = createJournalDto.transactionId ?? uuid.v4();
|
||||
|
||||
let creditSum = createJournalDto.journals.map(it => it.credit).filter(it => it).reduce((a, b) => a.plus(b), new Decimal(0));
|
||||
let debitSum = createJournalDto.journals.map(it => it.debit).filter(it => it).reduce((a, b) => a.plus(b), new Decimal(0));
|
||||
let coaIds = uniq(createJournalDto.journals.map(it => it.coa_id));
|
||||
|
@ -171,20 +215,9 @@ export class TransactionService {
|
|||
|
||||
const coas = await this.coaRepository.findByIds(coaIds);
|
||||
|
||||
let transaction: Transactions;
|
||||
console.log("berhasil")
|
||||
|
||||
if(createJournalDto.createTransaction) {
|
||||
transaction = new Transactions();
|
||||
transaction.id = createJournalDto.transactionId;
|
||||
transaction.type = createJournalDto.type;
|
||||
transaction.amount = createJournalDto.amount;
|
||||
transaction.user = createJournalDto.userId;
|
||||
transaction.status = createJournalDto.transactionStatus;
|
||||
|
||||
await this.transactionRepository.save(transaction);
|
||||
} else {
|
||||
transaction = await this.transactionRepository.findOneOrFail(createJournalDto.transactionId);
|
||||
}
|
||||
const transaction = createJournalDto.transaction
|
||||
|
||||
await Promise.all(createJournalDto.journals.map(journal => {
|
||||
const coa = coas.find(it => it.id === journal.coa_id);
|
||||
|
|
Loading…
Reference in New Issue
Block a user