/
product-variant.entity.ts
177 lines (146 loc) · 5.97 KB
/
product-variant.entity.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { CurrencyCode, GlobalFlag } from '@vendure/common/lib/generated-types';
import { DeepPartial, ID } from '@vendure/common/lib/shared-types';
import { Column, Entity, Index, JoinTable, ManyToMany, ManyToOne, OneToMany } from 'typeorm';
import { Calculated } from '../../common/calculated-decorator';
import { roundMoney } from '../../common/round-money';
import { ChannelAware, SoftDeletable } from '../../common/types/common-types';
import { LocaleString, Translatable, Translation } from '../../common/types/locale-types';
import { HasCustomFields } from '../../config/custom-field/custom-field-types';
import { Asset } from '../asset/asset.entity';
import { VendureEntity } from '../base/base.entity';
import { Channel } from '../channel/channel.entity';
import { Collection } from '../collection/collection.entity';
import { CustomProductVariantFields } from '../custom-entity-fields';
import { EntityId } from '../entity-id.decorator';
import { FacetValue } from '../facet-value/facet-value.entity';
import { OrderLine } from '../order-line/order-line.entity';
import { Product } from '../product/product.entity';
import { ProductOption } from '../product-option/product-option.entity';
import { StockLevel } from '../stock-level/stock-level.entity';
import { StockMovement } from '../stock-movement/stock-movement.entity';
import { TaxCategory } from '../tax-category/tax-category.entity';
import { TaxRate } from '../tax-rate/tax-rate.entity';
import { ProductVariantAsset } from './product-variant-asset.entity';
import { ProductVariantPrice } from './product-variant-price.entity';
import { ProductVariantTranslation } from './product-variant-translation.entity';
/**
* @description
* A ProductVariant represents a single stock keeping unit (SKU) in the store's inventory.
* Whereas a {@link Product} is a "container" of variants, the variant itself holds the
* data on price, tax category etc. When one adds items to their cart, they are adding
* ProductVariants, not Products.
*
* @docsCategory entities
*/
@Entity()
export class ProductVariant
extends VendureEntity
implements Translatable, HasCustomFields, SoftDeletable, ChannelAware
{
constructor(input?: DeepPartial<ProductVariant>) {
super(input);
}
@Column({ type: Date, nullable: true })
deletedAt: Date | null;
name: LocaleString;
@Column({ default: true })
enabled: boolean;
@Column()
sku: string;
/**
* Calculated at run-time
*/
listPrice: number;
/**
* Calculated at run-time
*/
listPriceIncludesTax: boolean;
/**
* Calculated at run-time
*/
currencyCode: CurrencyCode;
@Calculated({
expression: 'productvariant__productVariantPrices.price',
})
get price(): number {
if (this.listPrice == null) {
return 0;
}
return roundMoney(
this.listPriceIncludesTax ? this.taxRateApplied.netPriceOf(this.listPrice) : this.listPrice,
);
}
@Calculated({
// Note: this works fine for sorting by priceWithTax, but filtering will return inaccurate
// results due to this expression not taking taxes into account. This is because the tax
// rate is calculated at run-time in the application layer based on the current context,
// and is unknown to the database.
expression: 'productvariant__productVariantPrices.price',
})
get priceWithTax(): number {
if (this.listPrice == null) {
return 0;
}
return roundMoney(
this.listPriceIncludesTax ? this.listPrice : this.taxRateApplied.grossPriceOf(this.listPrice),
);
}
/**
* Calculated at run-time
*/
taxRateApplied: TaxRate;
@Index()
@ManyToOne(type => Asset, asset => asset.featuredInVariants, { onDelete: 'SET NULL' })
featuredAsset: Asset;
@OneToMany(type => ProductVariantAsset, productVariantAsset => productVariantAsset.productVariant, {
onDelete: 'SET NULL',
})
assets: ProductVariantAsset[];
@Index()
@ManyToOne(type => TaxCategory, taxCategory => taxCategory.productVariants)
taxCategory: TaxCategory;
@OneToMany(type => ProductVariantPrice, price => price.variant, { eager: true })
productVariantPrices: ProductVariantPrice[];
@OneToMany(type => ProductVariantTranslation, translation => translation.base, { eager: true })
translations: Array<Translation<ProductVariant>>;
@Index()
@ManyToOne(type => Product, product => product.variants)
product: Product;
@EntityId({ nullable: true })
productId: ID;
/**
* @description
* Specifies the value of stockOnHand at which the ProductVariant is considered
* out of stock.
*/
@Column({ default: 0 })
outOfStockThreshold: number;
/**
* @description
* When true, the `outOfStockThreshold` value will be taken from the GlobalSettings and the
* value set on this ProductVariant will be ignored.
*/
@Column({ default: true })
useGlobalOutOfStockThreshold: boolean;
@Column({ type: 'varchar', default: GlobalFlag.INHERIT })
trackInventory: GlobalFlag;
@OneToMany(type => StockLevel, stockLevel => stockLevel.productVariant)
stockLevels: StockLevel[];
@OneToMany(type => StockMovement, stockMovement => stockMovement.productVariant)
stockMovements: StockMovement[];
@ManyToMany(type => ProductOption, productOption => productOption.productVariants)
@JoinTable()
options: ProductOption[];
@ManyToMany(type => FacetValue, facetValue => facetValue.productVariants)
@JoinTable()
facetValues: FacetValue[];
@Column(type => CustomProductVariantFields)
customFields: CustomProductVariantFields;
@ManyToMany(type => Collection, collection => collection.productVariants)
collections: Collection[];
@ManyToMany(type => Channel, channel => channel.productVariants)
@JoinTable()
channels: Channel[];
@OneToMany(type => OrderLine, orderLine => orderLine.productVariant)
lines: OrderLine[];
}