diff --git a/packages/models/src/contract/ContractItem/ContractItem.ts b/packages/models/src/contract/ContractItem/ContractItem.ts index d4fc7729..624e80ab 100644 --- a/packages/models/src/contract/ContractItem/ContractItem.ts +++ b/packages/models/src/contract/ContractItem/ContractItem.ts @@ -4,6 +4,7 @@ import { DataModel, ReferenceModel } from "../../base/index.js"; import { provideReact } from "../../react/index.js"; import { config } from "../../config/config.js"; import assertObjectFound from "../../base/assertObjectFound.js"; +import { ContractItemArticleDetailed } from "../ContractItemArticle/index.js"; export class ContractItem extends ReferenceModel { public static ofId(contractId: string, id: string): ContractItem { @@ -44,7 +45,7 @@ export class ContractItem extends ReferenceModel { } } -export class ContractItemDetailed extends classes( +export class ContractItemCommon extends classes( DataModel, ContractItem, ) { @@ -52,3 +53,16 @@ export class ContractItemDetailed extends classes( super([data], [contractId, data.itemId]); } } + +export class ContractItemDetailed extends ContractItemCommon { + public readonly articles: ContractItemArticleDetailed[]; + + public constructor(contractId: string, data: ContractItemData) { + super(contractId, data); + + this.articles = data.articles.map( + (articleData) => + new ContractItemArticleDetailed(contractId, this.id, articleData), + ); + } +} diff --git a/packages/models/src/contract/ContractItemArticle/ContractItemArticle.ts b/packages/models/src/contract/ContractItemArticle/ContractItemArticle.ts new file mode 100644 index 00000000..2b2d181b --- /dev/null +++ b/packages/models/src/contract/ContractItemArticle/ContractItemArticle.ts @@ -0,0 +1,78 @@ +import { DataModel, ListDataModel, ReferenceModel } from "../../base/index.js"; +import { ContractItemArticleData } from "./types.js"; +import { classes } from "polytype"; + +export class ContractItemArticle extends ReferenceModel { + public static ofId( + id: string, + contractId: string, + contractItemId: string, + ): ContractItemArticle { + return new ContractItemArticle(id, contractId, contractItemId); + } + + public readonly contractId: string; + public readonly contractItemId: string; + + public constructor(id: string, contractId: string, contractItemId: string) { + super(id); + this.contractId = contractId; + this.contractItemId = contractItemId; + } +} + +class ContractItemArticleCommon extends classes( + DataModel, + ContractItemArticle, +) { + public constructor( + contractId: string, + contractItemId: string, + data: ContractItemArticleData, + ) { + super([data], [data.id, contractId, contractItemId]); + } +} + +export class ContractItemArticleDetailed extends ContractItemArticleCommon { + public constructor( + contractId: string, + contractItemId: string, + data: ContractItemArticleData, + ) { + super(contractId, contractItemId, data); + } + + public get totalPrice(): number { + return this.data.amount * this.data.unitPrice.value; + } + + public get displayName(): string { + return `${this.data.name} (${this.data.description})`; + } +} + +export class ContractItemArticleListItem extends ContractItemArticleCommon { + public constructor( + contractId: string, + contractItemId: string, + data: ContractItemArticleData, + ) { + super(contractId, contractItemId, data); + } +} + +export class ContractItemArticleList extends ListDataModel { + public readonly contractId: string; + public readonly contractItemId: string; + + public constructor( + contractId: string, + contractItemId: string, + items: ContractItemArticleListItem[], + ) { + super(items, items.length); + this.contractId = contractId; + this.contractItemId = contractItemId; + } +} diff --git a/packages/models/src/contract/ContractItemArticle/index.ts b/packages/models/src/contract/ContractItemArticle/index.ts new file mode 100644 index 00000000..2c002f10 --- /dev/null +++ b/packages/models/src/contract/ContractItemArticle/index.ts @@ -0,0 +1,2 @@ +export * from "./ContractItemArticle.js"; +export * from "./types.js"; diff --git a/packages/models/src/contract/ContractItemArticle/types.ts b/packages/models/src/contract/ContractItemArticle/types.ts new file mode 100644 index 00000000..1918143c --- /dev/null +++ b/packages/models/src/contract/ContractItemArticle/types.ts @@ -0,0 +1,11 @@ +export type ContractItemArticleData = { + amount: number; + articleTemplateId: string; + description?: string; + id: string; + name: string; + unitPrice: { + currency: string; + value: number; + }; +}; diff --git a/packages/models/src/contract/index.ts b/packages/models/src/contract/index.ts index e0c81bef..88a83c26 100644 --- a/packages/models/src/contract/index.ts +++ b/packages/models/src/contract/index.ts @@ -1,2 +1,3 @@ export * from "./Contract/index.js"; export * from "./ContractItem/index.js"; +export * from "./ContractItemArticle/index.js";