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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

use crate::{
    orders::TaxStatus,
    products::{BackordersStatus, Dimensions, ProductStatus, StockStatus},
    MetaData,
};

use super::products::{DefaultAttributeDTO, DownloadDTO, ImageDTO};

// use crate::models::{
//     products::{BackordersStatus, Dimensions, ProductStatus, StockStatus, TaxStatus},
//     MetaData,
// };

// use super::products::{DefaultAttributeDTO, DownloadDTO, ImageDTO};

#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProductVariationCreate {
    description: Option<String>,
    sku: Option<String>,
    regular_price: Option<String>,
    sale_price: Option<String>,
    date_on_sale_from: Option<NaiveDateTime>,
    date_on_sale_to: Option<NaiveDateTime>,
    status: Option<ProductStatus>,
    #[serde(rename = "virtual")]
    is_virtual: Option<bool>,
    downloadable: Option<bool>,
    downloads: Option<Vec<DownloadDTO>>,
    download_limit: Option<i32>,
    download_expiry: Option<i32>,
    tax_status: Option<TaxStatus>,
    tax_class: Option<String>,
    manage_stock: Option<bool>,
    stock_quantity: Option<i32>,
    stock_status: Option<StockStatus>,
    backorders: Option<BackordersStatus>,
    weight: Option<String>,
    dimensions: Option<Dimensions>,
    shipping_class: Option<String>,
    image: Option<ImageDTO>,
    attributes: Option<Vec<DefaultAttributeDTO>>,
    menu_order: Option<i32>,
    meta_data: Option<Vec<MetaData>>,
}
#[derive(Default)]
pub struct ProductVariationCreateBuilder {
    description: Option<String>,
    sku: Option<String>,
    regular_price: Option<String>,
    sale_price: Option<String>,
    date_on_sale_from: Option<NaiveDateTime>,
    date_on_sale_to: Option<NaiveDateTime>,
    status: Option<ProductStatus>,
    is_virtual: Option<bool>,
    downloadable: Option<bool>,
    downloads: Option<Vec<DownloadDTO>>,
    download_limit: Option<i32>,
    download_expiry: Option<i32>,
    tax_status: Option<TaxStatus>,
    tax_class: Option<String>,
    manage_stock: Option<bool>,
    stock_quantity: Option<i32>,
    stock_status: Option<StockStatus>,
    backorders: Option<BackordersStatus>,
    weight: Option<String>,
    dimensions: Option<Dimensions>,
    shipping_class: Option<String>,
    image: Option<ImageDTO>,
    attributes: Option<Vec<DefaultAttributeDTO>>,
    menu_order: Option<i32>,
    meta_data: Option<Vec<MetaData>>,
}
impl ProductVariationCreateBuilder {
    /// Variation description.
    pub fn description(&mut self, description: impl Into<String>) -> &mut Self {
        let _ = self.description.insert(description.into());
        self
    }
    /// Unique identifier.
    pub fn sku(&mut self, sku: impl Into<String>) -> &mut Self {
        let _ = self.sku.insert(sku.into());
        self
    }
    /// Variation regular price.
    pub fn regular_price(&mut self, regular_price: impl Into<String>) -> &mut Self {
        let _ = self.regular_price.insert(regular_price.into());
        self
    }
    /// Variation sale price.
    pub fn sale_price(&mut self, sale_price: impl Into<String>) -> &mut Self {
        let _ = self.sale_price.insert(sale_price.into());
        self
    }
    /// Start date of sale price, in the site's timezone.
    pub fn date_on_sale_from(&mut self, year: i32, month: u32, day: u32) -> &mut Self {
        let dt = chrono::NaiveDate::from_ymd_opt(year, month, day)
            .unwrap()
            .and_hms_opt(0, 0, 0)
            .unwrap();
        let _ = self.date_on_sale_from.insert(dt);
        self
    }
    /// End date of sale price, in the site's timezone.
    pub fn date_on_sale_to(&mut self, year: i32, month: u32, day: u32) -> &mut Self {
        let dt = chrono::NaiveDate::from_ymd_opt(year, month, day)
            .unwrap()
            .and_hms_opt(0, 0, 0)
            .unwrap();
        let _ = self.date_on_sale_to.insert(dt);
        self
    }
    /// Variation status. Options: draft, pending, private and publish. Default is publish.
    pub fn status(&mut self, status: ProductStatus) -> &mut Self {
        let _ = self.status.insert(status);
        self
    }
    /// If the variation is virtual. Default is false.
    pub fn is_virtual(&mut self) -> &mut Self {
        let _ = self.is_virtual.insert(true);
        self
    }
    /// If the variation is downloadable. Default is false.
    pub fn downloadable(&mut self) -> &mut Self {
        let _ = self.downloadable.insert(true);
        self
    }
    /// List of downloadable files.
    pub fn downloads(&mut self, file_src: impl Into<String>) -> &mut Self {
        let f = DownloadDTO {
            file: file_src.into(),
        };
        self.downloads.get_or_insert(vec![]).push(f);
        self
    }
    /// Number of times downloadable files can be downloaded after purchase. Default is -1.
    pub fn download_limit(&mut self, download_limit: i32) -> &mut Self {
        let _ = self.download_limit.insert(download_limit);
        self
    }
    /// Number of days until access to downloadable files expires. Default is -1.
    pub fn download_expiry(&mut self, download_expiry: i32) -> &mut Self {
        let _ = self.download_expiry.insert(download_expiry);
        self
    }
    /// Tax status. Options: taxable, shipping and none. Default is taxable.
    pub fn tax_status(&mut self, tax_status: TaxStatus) -> &mut Self {
        let _ = self.tax_status.insert(tax_status);
        self
    }
    /// Tax class.
    pub fn tax_class(&mut self, tax_class: impl Into<String>) -> &mut Self {
        let _ = self.tax_class.insert(tax_class.into());
        self
    }
    /// Stock management at variation level. Default is false.
    pub fn manage_stock(&mut self) -> &mut Self {
        let _ = self.manage_stock.insert(true);
        self
    }
    /// Stock quantity.
    pub fn stock_quantity(&mut self, stock_quantity: i32) -> &mut Self {
        let _ = self.stock_quantity.insert(stock_quantity);
        self
    }
    /// Controls the stock status of the product. Options: instock, outofstock, onbackorder. Default is instock.
    pub fn stock_status(&mut self, stock_status: StockStatus) -> &mut Self {
        let _ = self.stock_status.insert(stock_status);
        self
    }
    /// If managing stock, this controls if backorders are allowed. Options: no, notify and yes. Default is no.
    pub fn backorders(&mut self, backorders: BackordersStatus) -> &mut Self {
        let _ = self.backorders.insert(backorders);
        self
    }
    /// Variation weight.
    pub fn weight(&mut self, weight: impl Into<String>) -> &mut Self {
        let _ = self.weight.insert(weight.into());
        self
    }
    /// Variation dimensions. See Product variation - Dimensions properties
    pub fn dimensions(
        &mut self,
        length: impl Into<String>,
        width: impl Into<String>,
        height: impl Into<String>,
    ) -> &mut Self {
        let d = Dimensions {
            length: length.into(),
            width: width.into(),
            height: height.into(),
        };
        let _ = self.dimensions.insert(d);
        self
    }
    /// Shipping class slug.
    pub fn shipping_class(&mut self, shipping_class: impl Into<String>) -> &mut Self {
        let _ = self.shipping_class.insert(shipping_class.into());
        self
    }
    /// Variation image data.
    pub fn image(&mut self, img_src: impl Into<String>) -> &mut Self {
        let _ = self.image.insert(ImageDTO {
            src: img_src.into(),
        });
        self
    }
    /// List of attributes.
    pub fn attributes(
        &mut self,
        id: Option<i32>,
        name: impl Into<String>,
        option: impl Into<String>,
    ) -> &mut Self {
        self.attributes
            .get_or_insert(vec![])
            .push(DefaultAttributeDTO {
                id,
                name: name.into(),
                option: option.into(),
            });
        self
    }
    /// Menu order, used to custom sort products.
    pub fn menu_order(&mut self, menu_order: i32) -> &mut Self {
        let _ = self.menu_order.insert(menu_order);
        self
    }
    /// Meta data.
    pub fn meta_data(&mut self, key: impl Into<String>, value: impl Serialize) -> &mut Self {
        self.meta_data.get_or_insert(vec![]).push(MetaData {
            id: None,
            key: key.into(),
            value: serde_json::json!(value),
        });
        self
    }
    pub fn build(&self) -> ProductVariationCreate {
        ProductVariationCreate {
            description: self.description.to_owned(),
            sku: self.sku.to_owned(),
            regular_price: self.regular_price.to_owned(),
            sale_price: self.sale_price.to_owned(),
            date_on_sale_from: self.date_on_sale_from,
            date_on_sale_to: self.date_on_sale_to,
            status: self.status.to_owned(),
            is_virtual: self.is_virtual,
            downloadable: self.downloadable,
            downloads: self.downloads.to_owned(),
            download_limit: self.download_limit,
            download_expiry: self.download_expiry,
            tax_status: self.tax_status.to_owned(),
            tax_class: self.tax_class.to_owned(),
            manage_stock: self.manage_stock,
            stock_quantity: self.stock_quantity,
            stock_status: self.stock_status.to_owned(),
            backorders: self.backorders.to_owned(),
            weight: self.weight.to_owned(),
            dimensions: self.dimensions.to_owned(),
            shipping_class: self.shipping_class.to_owned(),
            image: self.image.to_owned(),
            attributes: self.attributes.to_owned(),
            menu_order: self.menu_order,
            meta_data: self.meta_data.to_owned(),
        }
    }
}
#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProductVariationUpdate {
    id: Option<i32>,
    description: Option<String>,
    sku: Option<String>,
    regular_price: Option<String>,
    sale_price: Option<String>,
    date_on_sale_from: Option<NaiveDateTime>,
    date_on_sale_to: Option<NaiveDateTime>,
    status: Option<ProductStatus>,
    #[serde(rename = "virtual")]
    is_virtual: Option<bool>,
    downloadable: Option<bool>,
    downloads: Option<Vec<DownloadDTO>>,
    download_limit: Option<i32>,
    download_expiry: Option<i32>,
    tax_status: Option<TaxStatus>,
    tax_class: Option<String>,
    manage_stock: Option<bool>,
    stock_quantity: Option<i32>,
    stock_status: Option<StockStatus>,
    backorders: Option<BackordersStatus>,
    weight: Option<String>,
    dimensions: Option<Dimensions>,
    shipping_class: Option<String>,
    image: Option<ImageDTO>,
    attributes: Option<Vec<DefaultAttributeDTO>>,
    menu_order: Option<i32>,
    meta_data: Option<Vec<MetaData>>,
}
#[derive(Default)]
pub struct ProductVariationUpdateBuilder {
    id: Option<i32>,
    description: Option<String>,
    sku: Option<String>,
    regular_price: Option<String>,
    sale_price: Option<String>,
    date_on_sale_from: Option<NaiveDateTime>,
    date_on_sale_to: Option<NaiveDateTime>,
    status: Option<ProductStatus>,
    is_virtual: Option<bool>,
    downloadable: Option<bool>,
    downloads: Option<Vec<DownloadDTO>>,
    download_limit: Option<i32>,
    download_expiry: Option<i32>,
    tax_status: Option<TaxStatus>,
    tax_class: Option<String>,
    manage_stock: Option<bool>,
    stock_quantity: Option<i32>,
    stock_status: Option<StockStatus>,
    backorders: Option<BackordersStatus>,
    weight: Option<String>,
    dimensions: Option<Dimensions>,
    shipping_class: Option<String>,
    image: Option<ImageDTO>,
    attributes: Option<Vec<DefaultAttributeDTO>>,
    menu_order: Option<i32>,
    meta_data: Option<Vec<MetaData>>,
}
impl ProductVariationUpdateBuilder {
    /// Unique identifier for the resource.
    pub fn id(&mut self, id: i32) -> &mut Self {
        let _ = self.id.insert(id);
        self
    }
    /// Variation description.
    pub fn description(&mut self, description: impl Into<String>) -> &mut Self {
        let _ = self.description.insert(description.into());
        self
    }
    /// Unique identifier.
    pub fn sku(&mut self, sku: impl Into<String>) -> &mut Self {
        let _ = self.sku.insert(sku.into());
        self
    }
    /// Variation regular price.
    pub fn regular_price(&mut self, regular_price: impl Into<String>) -> &mut Self {
        let _ = self.regular_price.insert(regular_price.into());
        self
    }
    /// Variation sale price.
    pub fn sale_price(&mut self, sale_price: impl Into<String>) -> &mut Self {
        let _ = self.sale_price.insert(sale_price.into());
        self
    }
    /// Start date of sale price, in the site's timezone.
    pub fn date_on_sale_from(&mut self, year: i32, month: u32, day: u32) -> &mut Self {
        let dt = chrono::NaiveDate::from_ymd_opt(year, month, day)
            .unwrap()
            .and_hms_opt(0, 0, 0)
            .unwrap();
        let _ = self.date_on_sale_from.insert(dt);
        self
    }
    /// End date of sale price, in the site's timezone.
    pub fn date_on_sale_to(&mut self, year: i32, month: u32, day: u32) -> &mut Self {
        let dt = chrono::NaiveDate::from_ymd_opt(year, month, day)
            .unwrap()
            .and_hms_opt(0, 0, 0)
            .unwrap();
        let _ = self.date_on_sale_to.insert(dt);
        self
    }
    /// Variation status. Options: draft, pending, private and publish. Default is publish.
    pub fn status(&mut self, status: ProductStatus) -> &mut Self {
        let _ = self.status.insert(status);
        self
    }
    /// If the variation is virtual. Default is false.
    pub fn is_virtual(&mut self) -> &mut Self {
        let _ = self.is_virtual.insert(true);
        self
    }
    /// If the variation is downloadable. Default is false.
    pub fn downloadable(&mut self) -> &mut Self {
        let _ = self.downloadable.insert(true);
        self
    }
    /// List of downloadable files.
    pub fn downloads(&mut self, file_src: impl Into<String>) -> &mut Self {
        let f = DownloadDTO {
            file: file_src.into(),
        };
        self.downloads.get_or_insert(vec![]).push(f);
        self
    }
    /// Number of times downloadable files can be downloaded after purchase. Default is -1.
    pub fn download_limit(&mut self, download_limit: i32) -> &mut Self {
        let _ = self.download_limit.insert(download_limit);
        self
    }
    /// Number of days until access to downloadable files expires. Default is -1.
    pub fn download_expiry(&mut self, download_expiry: i32) -> &mut Self {
        let _ = self.download_expiry.insert(download_expiry);
        self
    }
    /// Tax status. Options: taxable, shipping and none. Default is taxable.
    pub fn tax_status(&mut self, tax_status: TaxStatus) -> &mut Self {
        let _ = self.tax_status.insert(tax_status);
        self
    }
    /// Tax class.
    pub fn tax_class(&mut self, tax_class: impl Into<String>) -> &mut Self {
        let _ = self.tax_class.insert(tax_class.into());
        self
    }
    /// Stock management at variation level. Default is false.
    pub fn manage_stock(&mut self) -> &mut Self {
        let _ = self.manage_stock.insert(true);
        self
    }
    /// Stock quantity.
    pub fn stock_quantity(&mut self, stock_quantity: i32) -> &mut Self {
        let _ = self.stock_quantity.insert(stock_quantity);
        self
    }
    /// Controls the stock status of the product. Options: instock, outofstock, onbackorder. Default is instock.
    pub fn stock_status(&mut self, stock_status: StockStatus) -> &mut Self {
        let _ = self.stock_status.insert(stock_status);
        self
    }
    /// If managing stock, this controls if backorders are allowed. Options: no, notify and yes. Default is no.
    pub fn backorders(&mut self, backorders: BackordersStatus) -> &mut Self {
        let _ = self.backorders.insert(backorders);
        self
    }
    /// Variation weight.
    pub fn weight(&mut self, weight: impl Into<String>) -> &mut Self {
        let _ = self.weight.insert(weight.into());
        self
    }
    /// Variation dimensions. See Product variation - Dimensions properties
    pub fn dimensions(
        &mut self,
        length: impl Into<String>,
        width: impl Into<String>,
        height: impl Into<String>,
    ) -> &mut Self {
        let d = Dimensions {
            length: length.into(),
            width: width.into(),
            height: height.into(),
        };
        let _ = self.dimensions.insert(d);
        self
    }
    /// Shipping class slug.
    pub fn shipping_class(&mut self, shipping_class: impl Into<String>) -> &mut Self {
        let _ = self.shipping_class.insert(shipping_class.into());
        self
    }
    /// Variation image data.
    pub fn image(&mut self, img_src: impl Into<String>) -> &mut Self {
        let _ = self.image.insert(ImageDTO {
            src: img_src.into(),
        });
        self
    }
    /// List of attributes.
    pub fn attributes(
        &mut self,
        id: Option<i32>,
        name: impl Into<String>,
        option: impl Into<String>,
    ) -> &mut Self {
        self.attributes
            .get_or_insert(vec![])
            .push(DefaultAttributeDTO {
                id,
                name: name.into(),
                option: option.into(),
            });
        self
    }
    /// Menu order, used to custom sort products.
    pub fn menu_order(&mut self, menu_order: i32) -> &mut Self {
        let _ = self.menu_order.insert(menu_order);
        self
    }
    /// Meta data.
    pub fn meta_data(&mut self, key: impl Into<String>, value: impl Serialize) -> &mut Self {
        self.meta_data.get_or_insert(vec![]).push(MetaData {
            id: None,
            key: key.into(),
            value: serde_json::json!(value),
        });
        self
    }
    pub fn build(&self) -> ProductVariationUpdate {
        ProductVariationUpdate {
            id: self.id,
            description: self.description.to_owned(),
            sku: self.sku.to_owned(),
            regular_price: self.regular_price.to_owned(),
            sale_price: self.sale_price.to_owned(),
            date_on_sale_from: self.date_on_sale_from,
            date_on_sale_to: self.date_on_sale_to,
            status: self.status.to_owned(),
            is_virtual: self.is_virtual,
            downloadable: self.downloadable,
            downloads: self.downloads.to_owned(),
            download_limit: self.download_limit,
            download_expiry: self.download_expiry,
            tax_status: self.tax_status.to_owned(),
            tax_class: self.tax_class.to_owned(),
            manage_stock: self.manage_stock,
            stock_quantity: self.stock_quantity,
            stock_status: self.stock_status.to_owned(),
            backorders: self.backorders.to_owned(),
            weight: self.weight.to_owned(),
            dimensions: self.dimensions.to_owned(),
            shipping_class: self.shipping_class.to_owned(),
            image: self.image.to_owned(),
            attributes: self.attributes.to_owned(),
            menu_order: self.menu_order,
            meta_data: self.meta_data.to_owned(),
        }
    }
}
#[cfg(test)]
mod tests {
    use crate::{product_variations::ProductVariation, ApiClient, Entity, SubEntity};

    #[tokio::test]
    async fn test_list_all_product_variations() {
        let client = ApiClient::from_env().unwrap();
        let result: Vec<ProductVariation> = client
            .list_all_subentities(Entity::Product, 3918, SubEntity::ProductVariation)
            .await
            .unwrap();
        assert!(!result.is_empty());
    }
    #[tokio::test]
    async fn test_retrieve_product_variation() {
        let client = ApiClient::from_env().unwrap();
        let result: ProductVariation = client
            .retrieve_subentity(Entity::Product, 3918, SubEntity::ProductVariation, 4058)
            .await
            .unwrap();
        assert_eq!(4058, result.id);
    }
    #[tokio::test]
    async fn test_create_product_variation() {
        let client = ApiClient::from_env().unwrap();
        let variation_to_create = ProductVariation::create().sku("test-sku").build();
        let created: ProductVariation = client
            .create_subentity(
                Entity::Product,
                3918,
                SubEntity::ProductVariation,
                variation_to_create,
            )
            .await
            .unwrap();
        assert_eq!("test-sku", created.sku);
        let id = created.id;
        let _deleted: ProductVariation = client
            .delete_subentity(Entity::Product, 3918, SubEntity::ProductVariation, id)
            .await
            .unwrap();
    }
    #[tokio::test]
    async fn test_update_product_variation() {
        let client = ApiClient::from_env().unwrap();
        let update = ProductVariation::update().regular_price("4000").build();
        let updated: ProductVariation = client
            .update_subentity(
                Entity::Product,
                3918,
                SubEntity::ProductVariation,
                4058,
                update,
            )
            .await
            .unwrap();
        assert_eq!("4000", updated.regular_price);
    }
}