fix: order product
This commit is contained in:
@@ -9,11 +9,13 @@ import { Transactions } from './entities/transactions.entity';
|
||||
import { CoaService } from './coa.service';
|
||||
import { ProductModule } from '../product/product.module';
|
||||
import { UsersModule } from 'src/users/users.module';
|
||||
import { ConfigurableModule } from '../configurable/configurable.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([COA, TransactionJournal, Transactions]),
|
||||
ProductModule,
|
||||
ConfigurableModule,
|
||||
forwardRef(() => UsersModule),
|
||||
],
|
||||
controllers: [TransactionController, PpobCallbackController],
|
||||
|
||||
@@ -21,6 +21,8 @@ import { CreateJournalDto } from './dto/create-journal.dto';
|
||||
import { UsersService } from 'src/users/users.service';
|
||||
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';
|
||||
|
||||
interface JournalEntry {
|
||||
coa_id: string;
|
||||
@@ -39,7 +41,9 @@ export class TransactionService {
|
||||
private coaRepository: Repository<COA>,
|
||||
private coaService: CoaService,
|
||||
private productService: ProductService,
|
||||
private productHistoryPriceService: ProductHistoryPriceService,
|
||||
private userService: UsersService,
|
||||
private commissionService: CommissionService,
|
||||
private supplierService: SupplierService,
|
||||
private connection: Connection,
|
||||
) {}
|
||||
@@ -268,33 +272,30 @@ export class TransactionService {
|
||||
orderTransactionDto: OrderTransactionDto,
|
||||
currentUser: any,
|
||||
) {
|
||||
//GET USER
|
||||
const userData = await this.userService.findByUsername(
|
||||
currentUser.username,
|
||||
);
|
||||
|
||||
//GET PRODUCT
|
||||
const product = await this.productService.findOne(
|
||||
orderTransactionDto.productCode,
|
||||
);
|
||||
|
||||
//GET USER
|
||||
const userData = await this.userService.findByUsername(
|
||||
currentUser.username,
|
||||
const product_price = await this.productHistoryPriceService.findOne(
|
||||
product.id,
|
||||
userData.partner?.id,
|
||||
);
|
||||
|
||||
let supervisorData = [];
|
||||
|
||||
if (userData.superior != null) {
|
||||
supervisorData.push(
|
||||
await this.userService.findByUsername(currentUser.username),
|
||||
if (!userData.partner) {
|
||||
//GET SALES
|
||||
supervisorData = await this.calculateCommission(
|
||||
supervisorData,
|
||||
product_price.mark_up_price - product_price.price,
|
||||
userData,
|
||||
);
|
||||
|
||||
if (supervisorData[0].superior != null) {
|
||||
supervisorData.push(
|
||||
await this.userService.findByUsername(currentUser.username),
|
||||
);
|
||||
|
||||
if (supervisorData[0].superior != null) {
|
||||
supervisorData.push(
|
||||
await this.userService.findByUsername(currentUser.username),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//GET COA
|
||||
@@ -304,11 +305,11 @@ export class TransactionService {
|
||||
);
|
||||
|
||||
const coaInventory = await this.coaService.findByName(
|
||||
`${coaType[coaType.INVENTORY]}-IRS`,
|
||||
`${coaType[coaType.INVENTORY]}-${product.supplier.code}`,
|
||||
);
|
||||
|
||||
const coaCostOfSales = await this.coaService.findByName(
|
||||
`${coaType[coaType.COST_OF_SALES]}-SYSTEM`,
|
||||
`${coaType[coaType.COST_OF_SALES]}-${product.supplier.code}`,
|
||||
);
|
||||
|
||||
const coaSales = await this.coaService.findByName(
|
||||
@@ -319,18 +320,6 @@ export class TransactionService {
|
||||
`${coaType[coaType.EXPENSE]}-SYSTEM`,
|
||||
);
|
||||
|
||||
supervisorData = supervisorData.map(async (it) => {
|
||||
const coaAccount = await this.coaService.findByUser(
|
||||
it.id,
|
||||
coaType.WALLET,
|
||||
);
|
||||
|
||||
return {
|
||||
coa_id: coaAccount.id,
|
||||
credit: 0,
|
||||
};
|
||||
});
|
||||
|
||||
if (coaAccount.amount <= product.price) {
|
||||
throw new HttpException(
|
||||
{
|
||||
@@ -346,10 +335,10 @@ export class TransactionService {
|
||||
const transactionData = new Transactions();
|
||||
|
||||
transactionData.id = uuid.v4();
|
||||
transactionData.amount = product.price;
|
||||
transactionData.amount = product_price.mark_up_price;
|
||||
transactionData.user = userData.id;
|
||||
transactionData.status = statusTransaction.SUCCESS;
|
||||
transactionData.type = typeTransaction.DISTRIBUTION;
|
||||
transactionData.type = typeTransaction.ORDER;
|
||||
await manager.insert(Transactions, transactionData);
|
||||
|
||||
await this.accountingTransaction({
|
||||
@@ -360,23 +349,25 @@ export class TransactionService {
|
||||
journals: [
|
||||
{
|
||||
coa_id: coaInventory.id,
|
||||
credit: product.basePrice,
|
||||
credit: product_price.price,
|
||||
},
|
||||
{
|
||||
coa_id: coaCostOfSales.id,
|
||||
debit: product.basePrice,
|
||||
debit: product_price.price,
|
||||
},
|
||||
{
|
||||
coa_id: coaAccount.id,
|
||||
debit: product.price,
|
||||
debit: product_price.mark_up_price,
|
||||
},
|
||||
{
|
||||
coa_id: coaSales.id,
|
||||
credit: product.price,
|
||||
credit: product_price.mark_up_price,
|
||||
},
|
||||
{
|
||||
coa_id: coaExpense.id,
|
||||
credit: 0,
|
||||
credit: userData.partner
|
||||
? 0
|
||||
: product_price.mark_up_price - product_price.price,
|
||||
},
|
||||
].concat(supervisorData),
|
||||
});
|
||||
@@ -388,6 +379,40 @@ export class TransactionService {
|
||||
return true;
|
||||
}
|
||||
|
||||
async calculateCommission(data, totalPrice, userData) {
|
||||
let supervisorData = [];
|
||||
supervisorData.push(
|
||||
await this.userService.findByUsername(userData.superior.username),
|
||||
);
|
||||
|
||||
//GET Supervisor
|
||||
supervisorData.push(
|
||||
await this.userService.findByUsername(
|
||||
supervisorData[0].superior.username,
|
||||
),
|
||||
);
|
||||
|
||||
//GET Admin
|
||||
supervisorData.push(
|
||||
await this.userService.findByUsername(
|
||||
supervisorData[1].superior.username,
|
||||
),
|
||||
);
|
||||
|
||||
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) {
|
||||
const creditSum = createJournalDto.journals
|
||||
.map((it) => {
|
||||
|
||||
Reference in New Issue
Block a user