|
|
@@ -1,4 +1,5 @@
|
|
|
-import { EntitySubscriberInterface, EventSubscriber, InsertEvent } from 'typeorm';
|
|
|
+import { ID } from 'shared/shared-types';
|
|
|
+import { Connection, EntitySubscriberInterface, EventSubscriber, InsertEvent } from 'typeorm';
|
|
|
|
|
|
import { I18nError } from '../../i18n/i18n-error';
|
|
|
import { AdjustmentSource } from '../adjustment-source/adjustment-source.entity';
|
|
|
@@ -21,10 +22,7 @@ export class ProductVariantSubscriber implements EntitySubscriberInterface<Produ
|
|
|
if (channelId === undefined) {
|
|
|
throw new I18nError(`error.channel-id-not-set`);
|
|
|
}
|
|
|
- const taxCategory = await event.connection.getRepository(AdjustmentSource).findOne(taxCategoryId);
|
|
|
- if (!taxCategory) {
|
|
|
- throw new I18nError(`error.tax-category-not-found`, { id: taxCategoryId });
|
|
|
- }
|
|
|
+ const taxCategory = await this.getTaxCategory(event.connection, taxCategoryId);
|
|
|
const variantPrice = new ProductVariantPrice({ price, channelId });
|
|
|
variantPrice.variant = event.entity;
|
|
|
variantPrice.priceBeforeTax = this.getPriceBeforeTax(price, taxCategory.getTaxCategoryRate());
|
|
|
@@ -42,11 +40,16 @@ export class ProductVariantSubscriber implements EntitySubscriberInterface<Produ
|
|
|
if (!variantPrice) {
|
|
|
throw new I18nError(`error.could-not-find-product-variant-price`);
|
|
|
}
|
|
|
+ let taxCategory = variantPrice.taxCategory;
|
|
|
+ if (event.queryRunner.data.taxCategoryId !== undefined) {
|
|
|
+ taxCategory = await this.getTaxCategory(event.connection, event.queryRunner.data.taxCategoryId);
|
|
|
+ variantPrice.taxCategory = taxCategory;
|
|
|
+ }
|
|
|
|
|
|
variantPrice.price = event.entity.price || 0;
|
|
|
variantPrice.priceBeforeTax = this.getPriceBeforeTax(
|
|
|
variantPrice.price,
|
|
|
- variantPrice.taxCategory.getTaxCategoryRate(),
|
|
|
+ taxCategory.getTaxCategoryRate(),
|
|
|
);
|
|
|
await event.manager.save(variantPrice);
|
|
|
}
|
|
|
@@ -54,4 +57,12 @@ export class ProductVariantSubscriber implements EntitySubscriberInterface<Produ
|
|
|
private getPriceBeforeTax(priceAfterTax: number, taxRatePercentage: number): number {
|
|
|
return Math.round(priceAfterTax / (1 + taxRatePercentage / 100));
|
|
|
}
|
|
|
+
|
|
|
+ private async getTaxCategory(connection: Connection, id: ID): Promise<AdjustmentSource> {
|
|
|
+ const taxCategory = await connection.getRepository(AdjustmentSource).findOne(id);
|
|
|
+ if (!taxCategory) {
|
|
|
+ throw new I18nError(`error.tax-category-not-found`, { id });
|
|
|
+ }
|
|
|
+ return taxCategory;
|
|
|
+ }
|
|
|
}
|