@questwork/q-store-model 0.1.32 → 0.1.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/index.min.cjs +1413 -1389
  2. package/dist/index.min.js +1413 -1389
  3. package/package.json +8 -8
@@ -1485,7 +1485,6 @@ class Product {
1485
1485
  this.productCode = product_setCode(options, 'productCode')
1486
1486
  this.productGroupName = (typeof options.productGroupName !== 'undefined') ? options.productGroupName.toUpperCase() : options.productGroupName || 'DEFAULT'
1487
1487
  this.productOptions = this._ItemOption.initOnlyValidFromArray(options.productOptions)
1488
- this.productType = options.productType || 'Product'
1489
1488
  this.requiredProductCode = options.requiredProductCode
1490
1489
  this.remarks = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(options.remarks)
1491
1490
  this.stock = options.stock || 0
@@ -1526,6 +1525,9 @@ class Product {
1526
1525
  get isValid() {
1527
1526
  return !!this.tenantCode && !!this.name
1528
1527
  }
1528
+ get isBadge() {
1529
+ return this.productGroupName === 'BADGE'
1530
+ }
1529
1531
  get isPrintable() {
1530
1532
  return this.printLayoutMappingCodes.length > 0
1531
1533
  }
@@ -2347,7 +2349,6 @@ class Status {
2347
2349
  this.delivered = options.delivered || null
2348
2350
  this.expired = options.expired || null
2349
2351
  this.failed = options.failed || null
2350
- this.modified = options.modified || (new Date()).valueOf()
2351
2352
  this.onHold = options.onHold || null
2352
2353
  this.processing = options.processing || null
2353
2354
  this.paid = options.paid || null
@@ -2461,17 +2462,40 @@ class Status {
2461
2462
  return this.waived !== null
2462
2463
  }
2463
2464
  get lastStatus() {
2464
- let max = 0
2465
- let newStatus = ''
2466
- if (!(this instanceof Status)) return newStatus
2467
- newStatus = Object.keys(this).reduce((acc, key) => {
2468
- if (this[key] > max) {
2469
- max = this[key]
2470
- acc = key
2465
+ return this.modified
2466
+ // let max = 0
2467
+ // let newStatus = ''
2468
+ // if (!(this instanceof Status)) return newStatus
2469
+ // newStatus = Object.keys(this).reduce((acc, key) => {
2470
+ // if (this[key] > max) {
2471
+ // max = this[key]
2472
+ // acc = key
2473
+ // }
2474
+ // return acc
2475
+ // }, '')
2476
+ // return newStatus
2477
+ }
2478
+ get modified() {
2479
+ return this.statusLast ? this.statusLast.value : ''
2480
+ }
2481
+ get statusLast() {
2482
+ if (this.statusList.length === 0) {
2483
+ return null
2484
+ }
2485
+ return this.statusList[this.statusList.length - 1]
2486
+ }
2487
+ get statusList() {
2488
+ const keys = Object.keys(this)
2489
+ return keys.reduce((acc, key) => {
2490
+ if (this[key]) {
2491
+ acc.push({
2492
+ key, value: this[key]
2493
+ })
2471
2494
  }
2472
2495
  return acc
2473
- }, '')
2474
- return newStatus
2496
+ }, []).sort((a, b) => {
2497
+ return a.value > b.value ? 1 : -1
2498
+ })
2475
2499
  }
2476
2500
 
2477
2501
  // instance methods
@@ -2485,102 +2509,98 @@ class Status {
2485
2509
  // setters
2486
2510
  setAbsent(value) {
2487
2511
  this.absent = value || (new Date()).valueOf()
2488
- return this.setModified()
2512
+ return this
2489
2513
  }
2490
2514
  setAssigned(value) {
2491
2515
  this.assigned = value || (new Date()).valueOf()
2492
- return this.setModified()
2516
+ return this
2493
2517
  }
2494
2518
  setCancelled(value) {
2495
2519
  this.cancelled = value || (new Date()).valueOf()
2496
- return this.setModified()
2520
+ return this
2497
2521
  }
2498
2522
  setCompleted(value) {
2499
2523
  this.completed = value || (new Date()).valueOf()
2500
2524
  // this.unsetCancelled()
2501
- return this.setModified()
2525
+ return this
2502
2526
  }
2503
2527
  setConfirmed(value) {
2504
2528
  this.confirmed = value || (new Date()).valueOf()
2505
2529
  // this.unsetCancelled()
2506
- return this.setModified()
2530
+ return this
2507
2531
  }
2508
2532
  setDelivered(value) {
2509
2533
  this.delivered = value || (new Date()).valueOf()
2510
- return this.setModified()
2534
+ return this
2511
2535
  }
2512
2536
  setExpired(value) {
2513
2537
  this.expired = value || (new Date()).valueOf()
2514
- return this.setModified()
2538
+ return this
2515
2539
  }
2516
2540
  setFailed(value) {
2517
2541
  this.failed = value || (new Date()).valueOf()
2518
- return this.setModified()
2519
- }
2520
- setModified() {
2521
- this.modified = (new Date()).valueOf()
2522
2542
  return this
2523
2543
  }
2524
2544
  setOnHold() {
2525
2545
  this.onHold = (new Date()).valueOf()
2526
- return this.setModified()
2546
+ return this
2527
2547
  }
2528
2548
  setProcessing() {
2529
2549
  this.processing = (new Date()).valueOf()
2530
- return this.setModified()
2550
+ return this
2531
2551
  }
2532
2552
  setPaid(value) {
2533
2553
  this.paid = value || (new Date()).valueOf()
2534
2554
  this.waived = null
2535
- return this.setModified()
2555
+ return this
2536
2556
  }
2537
2557
  setPending(value) {
2538
2558
  this.pending = value || (new Date()).valueOf()
2539
- return this.setModified()
2559
+ return this
2540
2560
  }
2541
2561
  setRedeemed(value) {
2542
2562
  this.redeemed = value || (new Date()).valueOf()
2543
- return this.setModified()
2563
+ return this
2544
2564
  }
2545
2565
  setRefunded(value) {
2546
2566
  this.refunded = value || (new Date()).valueOf()
2547
- return this.setModified()
2567
+ return this
2548
2568
  }
2549
2569
  setRefundRequested(value) {
2550
2570
  this.refundRequested = value || (new Date()).valueOf()
2551
- return this.setModified()
2571
+ return this
2552
2572
  }
2553
2573
  setRejected(value) {
2554
2574
  this.rejected = value || (new Date()).valueOf()
2555
- return this.setModified()
2575
+ return this
2556
2576
  }
2557
2577
  setShared(value) {
2558
2578
  this.shared = value || (new Date()).valueOf()
2559
- return this.setModified()
2579
+ return this
2560
2580
  }
2561
2581
  setSubmitted(value) {
2562
2582
  this.submitted = value || (new Date()).valueOf()
2563
- return this.setModified()
2583
+ return this
2564
2584
  }
2565
2585
  setUsed(value) {
2566
2586
  this.used = value || (new Date()).valueOf()
2567
2587
  this.delivered = this.delivered || this.used
2568
- return this.setModified()
2588
+ return this
2569
2589
  }
2570
2590
  setWaived(value) {
2571
2591
  this.waived = value || (new Date()).valueOf()
2572
2592
  this.paid = null
2573
- return this.setModified()
2593
+ return this
2574
2594
  }
2575
2595
  // unsetAll() {
2576
2596
  // this.completed = null
2577
2597
  // this.confirmed = null
2578
2598
  // this.pending = null
2579
- // return this.setModified()
2599
+ // return this
2580
2600
  // }
2581
2601
  // unsetCancelled() {
2582
2602
  // this.cancelled = null
2583
- // return this.setModified()
2603
+ // return this
2584
2604
  // }
2585
2605
  update(update) {
2586
2606
  Object.keys(update).forEach((key) => {
@@ -2588,74 +2608,59 @@ class Status {
2588
2608
  this[key] = update[key]
2589
2609
  }
2590
2610
  })
2591
- return this.setModified()
2611
+ return this
2592
2612
  }
2593
2613
  }
2594
2614
 
2595
2615
 
2596
2616
 
2597
- ;// ./lib/models/invoiceLine/invoiceLine.js
2598
-
2617
+ ;// ./lib/models/cart/cart.js
2599
2618
 
2600
2619
 
2601
2620
 
2602
2621
 
2603
2622
 
2623
+ // import { WalletItem } from '../walletItem/index.js'
2604
2624
 
2605
- const invoiceLine_updateAllowedProps = [
2625
+ const cart_updateAllowedProps = [
2606
2626
  'active',
2607
- 'amount',
2608
- 'deduction',
2609
- 'deleted',
2610
- 'description',
2611
- 'discount',
2612
- 'purchaseOptions',
2613
- 'qty',
2614
2627
  'remarks',
2615
- 'unitPrice'
2628
+ 'deleted',
2616
2629
  ]
2617
2630
 
2618
- class InvoiceLine {
2631
+ class Cart {
2619
2632
  constructor(options = {}) {
2620
2633
  options = options || {}
2621
- const { _Amount, _Invoice, _Merchandise, _Status } = options._constructor || {}
2622
- this._Amount = _Amount && (_Amount._superclass === Amount._superclass) ? _Amount : Amount
2623
- this._Invoice = _Invoice && (_Invoice._superclass === Invoice._superclass) ? _Invoice : Invoice
2634
+ const { _CartItem, _Merchandise, _Status } = options._constructor || {}
2635
+ this._CartItem = _CartItem && (_CartItem._superclass === CartItem._superclass) ? _CartItem : CartItem
2624
2636
  this._Merchandise = _Merchandise && (_Merchandise._superclass === Merchandise._superclass) ? _Merchandise : Merchandise
2625
2637
  this._Status = _Status && (_Status._superclass === Status._superclass) ? _Status : Status
2626
-
2627
- this._invoice = options._invoice
2628
- this._merchandise = options._merchandise
2638
+ // this._WalletItem = _WalletItem && (_WalletItem._superclass === WalletItem._superclass) ? _WalletItem : WalletItem
2629
2639
 
2630
2640
  const id = options._id || options.id
2631
- this.id = invoiceLine_setId(id)
2632
- this._type = options._type || 'InvoiceLine'
2641
+ this.id = cart_setId(id)
2642
+
2643
+ this._merchandises = options._merchandises
2644
+ this._type = options._type || 'Cart'
2645
+
2633
2646
  this.active = (typeof options.active !== 'undefined') ? !!options.active : true
2634
- this.amount = this._Amount.init(options.amount)
2647
+ this.cartCode = cart_setCode(options, 'cartCode')
2648
+ this.cartItems = this._CartItem.initOnlyValidFromArray(options.cartItems)
2635
2649
  this.created = options.created || (new Date()).valueOf()
2636
2650
  this.creator = options.creator
2637
- this.deduction = this._Amount.init(options.deduction)
2638
2651
  this.deleted = options.deleted || false
2639
- this.description = options.description
2640
- this.discount = options.discount || 0
2641
- this.invoiceCode = options.invoiceCode
2642
- this.invoiceLineCode = invoiceLine_setCode(options, 'invoiceLineCode')
2643
- this.merchandiseCode = options.merchandiseCode
2652
+ this.metadata = q_utilities_namespaceObject.Metadata.initOnlyValidFromArray(options.metadata)
2644
2653
  this.modified = options.modified || (new Date()).valueOf()
2645
- this.purchaseOptions = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(options.purchaseOptions)
2646
- this.qty = options.qty || 1
2654
+ this.owner = options.owner
2647
2655
  this.remarks = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(options.remarks)
2648
2656
  this.status = this._Status.init(options.status)
2649
2657
  this.tenantCode = options.tenantCode
2650
- this.unitPrice = this._Amount.init(options.unitPrice)
2651
- this.waived = options.waived || 0
2652
2658
  }
2653
2659
 
2654
2660
  // Class Methods
2655
2661
  static dummyData() {
2656
2662
  return {
2657
- amount: Amount.dummyData(),
2658
- invoiceCode: 'invoiceCode',
2663
+ cartItems: [CartItem.dummyData()],
2659
2664
  tenantCode: 'tenantCode'
2660
2665
  }
2661
2666
  }
@@ -2676,207 +2681,341 @@ class InvoiceLine {
2676
2681
  return this.initFromArray(arr).filter((i) => i)
2677
2682
  }
2678
2683
  static get _classname() {
2679
- return 'InvoiceLine'
2684
+ return 'Cart'
2680
2685
  }
2681
2686
  static get _superclass() {
2682
- return 'InvoiceLine'
2687
+ return 'Cart'
2683
2688
  }
2684
2689
 
2685
2690
  // getters
2686
2691
  get isValid() {
2687
- return !!this.amount && !!this.invoiceCode && !!this.tenantCode
2692
+ return !!this.tenantCode
2688
2693
  }
2689
- get invoice() {
2690
- return this._Invoice.init(this._invoice)
2694
+ get isActive() {
2695
+ return !!this.active
2691
2696
  }
2692
- get merchandise() {
2693
- return this._Merchandise.init(this._merchandise)
2697
+ get merchandises() {
2698
+ return this._Merchandise.initOnlyValidFromArray(this._merchandises || [])
2699
+ }
2700
+ get merchandisesSummary() {
2701
+ return this.merchandises.reduce((acc, m) => {
2702
+ const found = acc.find((a) => {
2703
+ return a.merchandiseCode === m.merchandiseCode
2704
+ })
2705
+ if (found) {
2706
+ found.qty += 1
2707
+ } else {
2708
+ acc.push({
2709
+ ...m.summary,
2710
+ qty: 1
2711
+ })
2712
+ }
2713
+ return acc
2714
+ }, [])
2694
2715
  }
2695
2716
 
2696
2717
  // instance methods
2697
- setModified() {
2698
- this.modified = (new Date()).valueOf()
2699
- return this
2718
+ checkoutAvailable({ ownedWalletItems, selectCartItems, isCoordinator = false }) {
2719
+ try {
2720
+ if (!isCoordinator) {
2721
+ return this.checkHasStock({ selectCartItems }) && !this.checkWalletItemReachLimit({ ownedWalletItems, selectCartItems }) && !this.checkMerchandiseReachLimit({ selectCartItems })
2722
+ }
2723
+ return this.checkHasStock({ selectCartItems })
2724
+ } catch (err) {
2725
+ throw err
2726
+ }
2700
2727
  }
2701
-
2702
- update(update) {
2703
- Object.keys(update).forEach((key) => {
2704
- if (invoiceLine_updateAllowedProps.includes(key)) {
2705
- if (key === 'amount' || key === 'unitPrice' || key === 'deduction') {
2706
- this[key] = this._Amount.init(update[key])
2707
- } else if (key === 'purchaseOptions') {
2708
- this[key] = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(update[key])
2709
- } else {
2710
- this[key] = update[key]
2728
+ checkHasStock({ selectCartItems }) {
2729
+ try {
2730
+ selectCartItems.forEach((cartItem) => {
2731
+ const { qty } = cartItem
2732
+ if (this.merchandises && this.merchandises.length === 0) {
2733
+ throw new Error('not have merchandises array object')
2711
2734
  }
2712
- }
2713
- })
2714
- return this.setModified()
2735
+ const [merchandise] = this.merchandises.filter((item) => item.merchandiseCode === cartItem.merchandiseCode)
2736
+ const { products } = merchandise // merchandise is populated
2737
+ if (products && products.length === 0) {
2738
+ throw new Error('merchandise not have products array object')
2739
+ }
2740
+ if (!merchandise.checkHasStock(qty)) {
2741
+ throw new Error('merchandise not enough stock')
2742
+ }
2743
+ products.forEach((product) => {
2744
+ if (!product.hasStock(qty)) {
2745
+ throw new Error('product not enough stock')
2746
+ }
2747
+ })
2748
+ })
2749
+ return true
2750
+ } catch (err) {
2751
+ throw err
2752
+ }
2715
2753
  }
2716
- }
2717
-
2718
- function invoiceLine_setCode(options, key) {
2719
- const copyOptions = options || {}
2720
- if (copyOptions[key]) {
2721
- return copyOptions[key]
2754
+ checkWalletItemReachLimit({ ownedWalletItems, selectCartItems }) {
2755
+ try {
2756
+ selectCartItems.forEach((cartItem) => {
2757
+ const { qty } = cartItem
2758
+ const [merchandise] = this.merchandises.filter((item) => item.merchandiseCode === cartItem.merchandiseCode)
2759
+ const { products } = merchandise
2760
+ if (products && products.length === 0) {
2761
+ throw new Error('merchandise not have products array object')
2762
+ }
2763
+ products.forEach((product) => {
2764
+ const hasWalletItems = ownedWalletItems.filter((walletItem) => walletItem.productCode === product.productCode)
2765
+ const hasQty = hasWalletItems.length || 0
2766
+ if (!product.ableToHave(hasQty + qty)) {
2767
+ throw new Error('walletItem reach limit')
2768
+ }
2769
+ })
2770
+ })
2771
+ return false
2772
+ } catch (err) {
2773
+ throw err
2774
+ }
2722
2775
  }
2723
- return stringHelper.setCode()
2724
- }
2725
-
2726
- function invoiceLine_setId(id) {
2727
- if (id && typeof id.toString === 'function') {
2728
- return id.toString()
2776
+ checkMerchandiseReachLimit({ selectCartItems }) {
2777
+ try {
2778
+ selectCartItems.forEach((cartItem) => {
2779
+ const { qty } = cartItem
2780
+ const [merchandise] = this.merchandises.filter((item) => item.merchandiseCode === cartItem.merchandiseCode)
2781
+ if (qty > merchandise.max) {
2782
+ throw new Error('merchandise reach limit')
2783
+ }
2784
+ })
2785
+ return false
2786
+ } catch (err) {
2787
+ throw err
2788
+ }
2729
2789
  }
2730
- return id
2731
- }
2732
-
2733
-
2734
-
2735
- ;// ./lib/models/invoiceLine/invoiceLineRepo.js
2736
-
2737
-
2738
-
2739
- class InvoiceLineRepo extends q_utilities_namespaceObject.Repo {
2740
- constructor(options = {}) {
2741
- options = options || {}
2742
- super(options)
2743
- const { _InvoiceLine } = options._constructor || {}
2744
- this._InvoiceLine = _InvoiceLine && (_InvoiceLine._superclass === InvoiceLine._superclass) ? _InvoiceLine : InvoiceLine
2790
+ clearCartItems() {
2791
+ this.cartItems = []
2792
+ return this
2745
2793
  }
2746
- static get _classname() {
2747
- return 'InvoiceLineRepo'
2794
+ createInvoiceLines() {
2795
+ return this.cartItems.map((cartItem) => {
2796
+ return Object.assign(
2797
+ cartItem.createInvoiceLine(),
2798
+ { cartCode: this.cartCode, tenantCode: this.tenantCode }
2799
+ )
2800
+ })
2748
2801
  }
2749
- init(options) {
2750
- return this._InvoiceLine.init(options)
2802
+ getCartItems() {
2803
+ return this.cartItems
2804
+ }
2805
+ getCartItemCodes() {
2806
+ return this.cartItems.map((cartItem) => cartItem.cartItemCode)
2807
+ }
2808
+ getCurrencyCode() {
2809
+ return this.cartItems.length > 0
2810
+ ? this.cartItems[0].getCurrencyCode()
2811
+ : null
2812
+ }
2813
+ getMerchandiseAvailableInfos({ cartItems, currency, roleCodes }) {
2814
+ if ((this.cartItems.length > 0 && this.merchandises.length === 0)) {
2815
+ throw new Error('No merchandises')
2816
+ }
2817
+ const self = this
2818
+ cartItems = cartItems || self.cartItems
2819
+ cartItems = this._CartItem.initOnlyValidFromArray(cartItems)
2820
+ if (cartItems.length === 0) {
2821
+ throw new Error('No cartItems')
2822
+ }
2823
+ return self.merchandises.reduce((acc, merchandise) => {
2824
+ const filteredCartItems = this._CartItem.getByMerchandiseCode(cartItems, merchandise.merchandiseCode)
2825
+ const info = merchandise.getAvailableInfo({ cartItems: filteredCartItems, currency, roleCodes })
2826
+ acc.push(info)
2827
+ return acc
2828
+ }, [])
2829
+ }
2830
+ // initWalletItemsFromCartItems(walletItemData) {
2831
+ // if (this.cartItems.length === 0) {
2832
+ // throw new Error('No cartItems')
2833
+ // }
2834
+ // const initWalletItems = this.cartItems.reduce((acc, cartItem) => {
2835
+ // const { merchandiseCode, qty } = cartItem
2836
+ // const [merchandise] = this.merchandises.filter((merchandiseItem) => merchandiseItem.merchandiseCode === merchandiseCode)
2837
+ // merchandise.productCodes.forEach((productCode) => {
2838
+ // const walletItems = Array.from({ length: qty }, (item, index) => {
2839
+ // const data = this._WalletItem.init({
2840
+ // ...walletItemData,
2841
+ // qty: 1,
2842
+ // productCode
2843
+ // })
2844
+ // data.setPending()
2845
+ // return data
2846
+ // })
2847
+ // acc = acc.concat(walletItems)
2848
+ // })
2849
+ // return acc
2850
+ // }, [])
2851
+ // return initWalletItems
2852
+ // }
2853
+ setCartItems(cartItems) {
2854
+ this.cartItems = this._CartItem.initOnlyValidFromArray(cartItems)
2855
+ return this
2856
+ }
2857
+ setCompleted() {
2858
+ this.status.setCompleted()
2859
+ return this.setModified()
2860
+ }
2861
+ setModified() {
2862
+ this.modified = (new Date()).valueOf()
2863
+ return this
2864
+ }
2865
+ updateAvailable({ isCoordinator, selectCartItems }) {
2866
+ try {
2867
+ if (!isCoordinator) {
2868
+ return this.checkHasStock({ selectCartItems }) && !this.checkMerchandiseReachLimit({ selectCartItems })
2869
+ }
2870
+ return this.checkHasStock({ selectCartItems })
2871
+ } catch (err) {
2872
+ throw err
2873
+ }
2874
+ }
2875
+ update(update) {
2876
+ Object.keys(update).forEach((key) => {
2877
+ if (cart_updateAllowedProps.includes(key)) {
2878
+ if (key === 'metadata') {
2879
+ this[key] = q_utilities_namespaceObject.Metadata.initOnlyValidFromArray(update[key])
2880
+ } else if (key === 'remarks') {
2881
+ this[key] = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(update[key])
2882
+ } else {
2883
+ this[key] = update[key]
2884
+ }
2885
+ }
2886
+ })
2887
+ return this.setModified()
2888
+ }
2889
+ updateStock() {
2890
+ if (this.cartItems.length === 0) {
2891
+ throw new Error('No cartItems')
2892
+ }
2893
+ let newProducts = []
2894
+ let newMerchandises = []
2895
+ this.cartItems.forEach((cartItem) => {
2896
+ const { merchandiseCode, qty } = cartItem
2897
+ const [merchandise] = this.merchandises.filter((item) => item.merchandiseCode === merchandiseCode)
2898
+ const newMerchandiseStock = merchandise.stock - qty
2899
+ const { products } = merchandise
2900
+ products.forEach((product) => {
2901
+ const newProductStock = product.stock - cartItem.qty
2902
+ product.updateProduct({ stock: newProductStock })
2903
+ })
2904
+ newProducts = newProducts.concat(products)
2905
+ merchandise.updateMerchandise({ stock: newMerchandiseStock })
2906
+ newMerchandises = newMerchandises.concat(merchandise)
2907
+ })
2908
+ return { newProducts, newMerchandises }
2751
2909
  }
2752
2910
  }
2753
2911
 
2912
+ function cart_setCode(options, key) {
2913
+ const copyOptions = options || {}
2914
+ if (copyOptions[key]) {
2915
+ return copyOptions[key]
2916
+ }
2917
+ return stringHelper.setCode()
2918
+ }
2754
2919
 
2920
+ function cart_setId(id) {
2921
+ if (id && typeof id.toString === 'function') {
2922
+ return id.toString()
2923
+ }
2924
+ return id
2925
+ }
2755
2926
 
2756
- ;// ./lib/models/invoiceLine/index.js
2757
-
2927
+ // function updateCartItems(cartItems = []) {
2928
+ // if (cartItems.length === 0) {
2929
+ // return cartItems
2930
+ // }
2931
+ // let copyCartItems = CartItem.initOnlyValidFromArray(cartItems)
2932
+ // copyCartItems = copyCartItems.sort((a, b) => b.created - a.created)
2933
+ // return CartItem.mergeDuplicateCartItems(copyCartItems)
2934
+ // }
2758
2935
 
2759
2936
 
2760
2937
 
2938
+ ;// ./lib/models/cart/cartRepo.js
2761
2939
 
2762
2940
 
2763
- ;// ./lib/models/issuer/issuer.js
2764
2941
 
2765
- class Issuer {
2942
+ class CartRepo extends q_utilities_namespaceObject.Repo {
2766
2943
  constructor(options = {}) {
2767
2944
  options = options || {}
2768
- this.bankAccount = options.bankAccount
2769
- this.contactInformation = options.contactInformation
2770
- this.name = options.name
2771
- this.logo = options.logo
2772
- }
2773
-
2774
- // Class Methods
2775
- static dummyData() {
2776
- return {
2777
- name: 'name'
2778
- }
2779
- }
2780
- static init(options = {}) {
2781
- if (options instanceof this) {
2782
- return options
2783
- }
2784
- const instance = new this(options)
2785
- return instance.isValid ? instance : null
2786
- }
2787
- static initFromArray(arr = []) {
2788
- if (Array.isArray(arr)) {
2789
- return arr.map((a) => this.init(a))
2790
- }
2791
- return []
2792
- }
2793
- static initOnlyValidFromArray(arr = []) {
2794
- return this.initFromArray(arr).filter((i) => i)
2945
+ super(options)
2946
+ const { _Cart } = options._constructor || {}
2947
+ this._Cart = _Cart && (_Cart._superclass === Cart._superclass)
2948
+ ? _Cart
2949
+ : Cart
2795
2950
  }
2796
2951
  static get _classname() {
2797
- return 'Issuer'
2798
- }
2799
- static get _superclass() {
2800
- return 'Issuer'
2801
- }
2802
-
2803
- // getters
2804
- get isValid() {
2805
- return !!this.name
2952
+ return 'CartRepo'
2806
2953
  }
2807
-
2808
- // instance methods
2809
- updateIssuer(update) {
2810
- Object.keys(update).forEach((key) => {
2811
- this[key] = update[key]
2812
- })
2813
- return this
2954
+ init(options) {
2955
+ return this._Cart.init(options)
2814
2956
  }
2815
2957
  }
2816
2958
 
2817
2959
 
2818
2960
 
2819
- ;// ./lib/models/invoice/invoice.js
2961
+ ;// ./lib/models/cart/index.js
2820
2962
 
2821
2963
 
2822
2964
 
2823
2965
 
2824
2966
 
2967
+ ;// ./lib/models/category/category.js
2825
2968
 
2826
2969
 
2827
2970
 
2828
- const invoice_updateAllowedProps = [
2971
+
2972
+ const category_updateAllowedProps = [
2829
2973
  'active',
2830
- 'checkoutDateTime',
2974
+ 'codes',
2831
2975
  'deleted',
2832
2976
  'description',
2833
- 'invoiceDate',
2834
- 'issuer',
2835
- 'metadata',
2977
+ 'max',
2978
+ 'min',
2979
+ 'name',
2980
+ 'owner',
2981
+ 'priority',
2982
+ 'productCodes',
2836
2983
  'remarks',
2837
- 'revisionNumber',
2838
- 'status'
2839
2984
  ]
2840
2985
 
2841
- class Invoice {
2986
+ class Category {
2842
2987
  constructor(options) {
2843
2988
  options = options || {}
2844
- const { _Amount, _Cart, _InvoiceLine, _Issuer, _Status } = options._constructor || {}
2845
- this._Amount = _Amount && (_Amount._superclass === Amount._superclass) ? _Amount : Amount
2846
- this._Cart = _Cart && (_Cart._superclass === Cart._superclass) ? _Cart : Cart
2847
- this._InvoiceLine = _InvoiceLine && (_InvoiceLine._superclass === InvoiceLine._superclass) ? _InvoiceLine : InvoiceLine
2848
- this._Issuer = _Issuer && (_Issuer._superclass === Issuer._superclass) ? _Issuer : Issuer
2849
- this._Status = _Status && (_Status._superclass === Status._superclass) ? _Status : Status
2989
+ const { _Product } = options._constructor || {}
2990
+ this._Product = _Product && (_Product._superclass === Product._superclass) ? _Product : Product
2991
+
2992
+ this._products = options._products
2993
+ this._type = options._type || 'Category'
2850
2994
 
2851
- this._cart = options._cart
2852
- this._invoiceLines = options._invoiceLines
2853
-
2854
2995
  const id = options._id || options.id
2855
- this.id = invoice_setId(id)
2856
- this._type = options._type || 'Invoice'
2996
+ this.id = category_setId(id)
2857
2997
  this.active = (typeof options.active !== 'undefined') ? !!options.active : true
2858
- this.amount = this._Amount.init(options.amount)
2859
- this.cartCode = options.cartCode
2860
- this.checkoutDateTime = options.checkoutDateTime || (new Date()).valueOf()
2998
+ this.categoryCode = category_setCode(options, 'categoryCode')
2999
+ this.codes = options.codes || []
2861
3000
  this.created = options.created || (new Date()).valueOf()
2862
3001
  this.creator = options.creator
2863
3002
  this.deleted = options.deleted || false
2864
3003
  this.description = options.description
2865
- this.invoiceCode = invoice_setCode(options, 'invoiceCode')
2866
- this.invoiceDate = options.invoiceDate || (new Date()).valueOf()
2867
- this.invoiceNumber = options.invoiceNumber
2868
- this.issuer = this._Issuer.init(options.issuer)
2869
- this.metadata = q_utilities_namespaceObject.Metadata.initOnlyValidFromArray(options.metadata)
3004
+ this.max = options.max || 1
3005
+ this.min = options.min || 0
2870
3006
  this.modified = options.modified || (new Date()).valueOf()
3007
+ this.name = options.name
2871
3008
  this.owner = options.owner
3009
+ this.priority = options.priority || 10
3010
+ this.productCodes = options.productCodes || []
2872
3011
  this.remarks = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(options.remarks)
2873
- this.revisionNumber = options.revisionNumber || 1
2874
- this.status = this._Status.init(options.status)
2875
3012
  this.tenantCode = options.tenantCode
2876
3013
  }
2877
3014
 
3015
+ // class methods
2878
3016
  static dummyData() {
2879
3017
  return {
3018
+ name: 'category',
2880
3019
  tenantCode: 'tenantCode'
2881
3020
  }
2882
3021
  }
@@ -2897,138 +3036,41 @@ class Invoice {
2897
3036
  return this.initFromArray(arr).filter((i) => i)
2898
3037
  }
2899
3038
  static get _classname() {
2900
- return 'Invoice'
3039
+ return 'Category'
2901
3040
  }
2902
3041
  static get _superclass() {
2903
- return 'Invoice'
3042
+ return 'Category'
2904
3043
  }
2905
3044
 
3045
+ // getters
2906
3046
  get isValid() {
2907
- return !!this.tenantCode
3047
+ return !!this.name && !!this.tenantCode && this.max > this. min
2908
3048
  }
2909
- get cart() {
2910
- return this._Cart.init(this._cart)
3049
+ get products() {
3050
+ return this._Product.initOnlyValidFromArray(this._products)
2911
3051
  }
2912
- get invoiceLines() {
2913
- return this._InvoiceLine.initOnlyValidFromArray(this._invoiceLines || [])
3052
+
3053
+ // instance methods
3054
+ setModified() {
3055
+ this.modified = (new Date()).valueOf()
3056
+ return this
2914
3057
  }
2915
- get merchandises() {
2916
- return (this.invoiceLines || []).map((i) => {
2917
- return i.merchandise
2918
- }).filter((i) => i)
2919
- }
2920
- get merchandisesSummary() {
2921
- return this.merchandises.reduce((acc, m) => {
2922
- const found = acc.find((a) => {
2923
- return a.merchandiseCode === m.merchandiseCode
2924
- })
2925
- if (found) {
2926
- found.qty += 1
2927
- } else {
2928
- acc.push({
2929
- ...m.summary,
2930
- qty: 1
2931
- })
2932
- }
2933
- return acc
2934
- }, [])
2935
- }
2936
- get products() {
2937
- return (this.merchandises || []).reduce((acc, m) => {
2938
- if (m.products) {
2939
- acc = acc.concat(m.products)
2940
- }
2941
- return acc
2942
- }, [])
2943
- }
2944
- get productsSummary() {
2945
- return this.products.reduce((acc, p) => {
2946
- const found = acc.find((a) => {
2947
- return a.productCode === p.productCode
2948
- })
2949
- if (found) {
2950
- found.qty += 1
2951
- } else {
2952
- acc.push({
2953
- ...p.summary,
2954
- qty: 1
2955
- })
2956
- }
2957
- return acc
2958
- }, [])
2959
- }
2960
-
2961
- allowCancel() {
2962
- return this.status.allowCancel()
2963
- }
2964
-
2965
- // instance methods
2966
- getAmount() {
2967
- return this.amount
2968
- }
2969
-
2970
- getCurrencyCode() {
2971
- return this.amount ? this.amount.getCurrencyCode() : null
2972
- }
2973
-
2974
- getFullInvoiceNumber(delimiter = '.') {
2975
- return `${this.invoiceNumber}${delimiter}${this.revisionNumber}`
2976
- }
2977
-
2978
- increaseRevisionNumber() {
2979
- this.revisionNumber += 1
2980
- return this
2981
- }
2982
-
2983
- setCancelled() {
2984
- return this.status.setCancelled()
2985
- }
2986
-
2987
- setCheckoutDateTime(timestamp) {
2988
- if (typeof timestamp === 'number') {
2989
- this.checkoutDateTime = timestamp
2990
- }
2991
- return this
2992
- }
2993
-
2994
- setModified() {
2995
- this.modified = (new Date()).valueOf()
2996
- return this
2997
- }
2998
-
2999
- setCompleted() {
3000
- this.status.setCompleted()
3001
- return this.setModified()
3002
- }
3003
-
3004
- setWaived() {
3005
- this.status.setWaived()
3006
- return this.setModified()
3007
- }
3008
-
3009
- update(update) {
3010
- Object.keys(update).forEach((key) => {
3011
- if (invoice_updateAllowedProps.includes(key)) {
3012
- if (key === 'remarks') {
3013
- this[key] = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(update[key])
3014
- } else if (key === 'amount') {
3015
- this[key] = this._Amount.init(update[key])
3016
- } else if (key === 'metadata') {
3017
- this[key] = q_utilities_namespaceObject.Metadata.initOnlyValidFromArray(update[key])
3018
- } else if (key === 'issuer') {
3019
- this[key] = this._Issuer.init(update[key])
3020
- } else if (key === 'status') {
3021
- this[key] = this._Status.init(update[key])
3022
- } else {
3023
- this[key] = update[key]
3024
- }
3025
- }
3026
- })
3027
- return this.setModified()
3058
+
3059
+ update(update) {
3060
+ Object.keys(update).forEach((key) => {
3061
+ if (category_updateAllowedProps.includes(key)) {
3062
+ if (key === 'remarks') {
3063
+ this[key] = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(update[key])
3064
+ } else {
3065
+ this[key] = update[key]
3066
+ }
3067
+ }
3068
+ })
3069
+ return this.setModified()
3028
3070
  }
3029
3071
  }
3030
3072
 
3031
- function invoice_setCode(options, key) {
3073
+ function category_setCode(options, key) {
3032
3074
  const copyOptions = options || {}
3033
3075
  if (copyOptions[key]) {
3034
3076
  return copyOptions[key]
@@ -3036,7 +3078,7 @@ function invoice_setCode(options, key) {
3036
3078
  return stringHelper.setCode()
3037
3079
  }
3038
3080
 
3039
- function invoice_setId(id) {
3081
+ function category_setId(id) {
3040
3082
  if (id && typeof id.toString === 'function') {
3041
3083
  return id.toString()
3042
3084
  }
@@ -3045,75 +3087,86 @@ function invoice_setId(id) {
3045
3087
 
3046
3088
 
3047
3089
 
3048
- ;// ./lib/models/invoice/invoiceRepo.js
3090
+ ;// ./lib/models/category/categoryRepo.js
3049
3091
 
3050
3092
 
3051
3093
 
3052
- class InvoiceRepo extends q_utilities_namespaceObject.Repo {
3094
+ class CategoryRepo extends q_utilities_namespaceObject.Repo {
3053
3095
  constructor(options = {}) {
3054
3096
  options = options || {}
3055
3097
  super(options)
3056
- const { _Invoice } = options._constructor || {}
3057
- this._Invoice = _Invoice && (_Invoice._superclass === Invoice._superclass) ? _Invoice : Invoice
3098
+ const { _Category } = options._constructor || {}
3099
+ this._Category = _Category && (_Category._superclass === Category._superclass)
3100
+ ? _Category
3101
+ : Category
3058
3102
  }
3059
3103
  static get _classname() {
3060
- return 'InvoiceRepo'
3104
+ return 'CategoryRepo'
3061
3105
  }
3062
3106
  init(options) {
3063
- return this._Invoice.init(options)
3107
+ return this._Category.init(options)
3064
3108
  }
3065
3109
  }
3066
3110
 
3067
3111
 
3068
3112
 
3069
- ;// ./lib/models/invoice/index.js
3113
+ ;// ./lib/models/category/index.js
3070
3114
 
3071
3115
 
3072
3116
 
3073
3117
 
3074
3118
 
3075
3119
 
3076
- ;// ./lib/models/paymentGateway/paymentGateway.js
3120
+ ;// ./lib/models/creditNoteLine/creditNoteLine.js
3077
3121
 
3078
3122
 
3079
- const paymentGateway_updateAllowedProps = [
3123
+
3124
+
3125
+ const creditNoteLine_updateAllowedProps = [
3080
3126
  'active',
3127
+ 'amount',
3128
+ // 'deduction',
3081
3129
  'deleted',
3082
- 'label',
3083
- 'logoUrl',
3084
- 'name',
3085
- 'owner',
3086
- 'sandbox',
3087
- 'setting'
3130
+ 'description',
3131
+ // 'discount',
3132
+ 'qty',
3133
+ 'remarks',
3134
+ // 'unitPrice'
3088
3135
  ]
3089
3136
 
3090
-
3091
- class PaymentGateway {
3137
+ class CreditNoteLine {
3092
3138
  constructor(options = {}) {
3093
3139
  options = options || {}
3140
+ const { _Amount } = options._constructor || {}
3141
+ this._Amount = _Amount && (_Amount._superclass === Amount._superclass) ? _Amount : Amount
3142
+
3143
+ this._creditNote = options._creditNote
3144
+ this._type = options._type || 'CreditNoteLine'
3145
+
3094
3146
  const id = options._id || options.id
3095
- this.id = paymentGateway_setId(id)
3096
- this._type = options._type || 'PaymentGateway'
3147
+ this.id = creditNoteLine_setId(id)
3097
3148
  this.active = (typeof options.active !== 'undefined') ? !!options.active : true
3149
+ this.amount = this._Amount.init(options.amount)
3098
3150
  this.created = options.created || (new Date()).valueOf()
3099
3151
  this.creator = options.creator
3152
+ this.creditNoteCode = options.creditNoteCode
3153
+ this.creditNoteLineCode = creditNoteLine_setCode(options, 'creditNoteLineCode')
3154
+ // this.deduction = this._Amount.init(options.deduction)
3100
3155
  this.deleted = options.deleted || false
3101
- this.hasWebhook = options.hasWebhook || false
3102
- this.label = options.label
3103
- this.logoUrl = options.logoUrl
3156
+ this.description = options.description
3157
+ // this.discount = options.discount || 0
3104
3158
  this.modified = options.modified || (new Date()).valueOf()
3105
- this.name = options.name || ''
3106
3159
  this.owner = options.owner
3107
- this.paymentGatewayCode = paymentGateway_setCode(options, 'paymentGatewayCode')
3108
- this.paymentGatewayType = options.paymentGatewayType || 'PaymentGateway'
3109
- this.paymentResultType = options.paymentResultType || 'PaymentResult'
3110
- this.sandbox = options.sandbox || false
3111
- this.setting = options.setting || null
3160
+ this.qty = options.qty || 1
3161
+ this.remarks = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(options.remarks)
3112
3162
  this.tenantCode = options.tenantCode
3163
+ // this.unitPrice = Amount.init(options.unitPrice)
3113
3164
  }
3165
+
3166
+ // Class Methods
3114
3167
  static dummyData() {
3115
3168
  return {
3116
- name: 'name',
3169
+ creditNoteCode: 'creditNoteCode',
3117
3170
  tenantCode: 'tenantCode'
3118
3171
  }
3119
3172
  }
@@ -3134,53 +3187,48 @@ class PaymentGateway {
3134
3187
  return this.initFromArray(arr).filter((i) => i)
3135
3188
  }
3136
3189
  static get _classname() {
3137
- return 'PaymentGateway'
3190
+ return 'CreditNoteLine'
3138
3191
  }
3139
3192
  static get _superclass() {
3140
- return 'PaymentGateway'
3193
+ return 'CreditNoteLine'
3141
3194
  }
3142
3195
 
3196
+
3143
3197
  // getters
3144
3198
  get isValid() {
3145
- return !!this.name && !!this.tenantCode
3199
+ return !!this.creditNoteCode && !!this.tenantCode
3146
3200
  }
3147
3201
 
3148
3202
  // instance methods
3149
- async createPayment() {
3150
- throw new Error(`${this._classname} subclass should implement createPayment`)
3151
- }
3152
- async getAppPayParams() {
3153
- throw new Error(`${this._classname} subclass should implement getAppPayParams`)
3154
- }
3155
- async updatePayment() {
3156
- throw new Error(`${this._classname} subclass should implement updatePayment`)
3157
- }
3158
- async query() {
3159
- throw new Error(`${this._classname} subclass should implement query`)
3160
- }
3161
- async submit() {
3162
- throw new Error(`${this._classname} subclass should implement submit`)
3163
- }
3164
-
3165
- setCompletedRelatedStatus(status) {
3166
- throw new Error(`${this.name} subclass should implement setCompletedRelatedStatus`)
3203
+ setAmount(amount) {
3204
+ const a = this._Amount.init(amount)
3205
+ if (a) {
3206
+ this.amount = a
3207
+ }
3208
+ return this
3167
3209
  }
3168
-
3169
3210
  setModified() {
3170
3211
  this.modified = (new Date()).valueOf()
3171
3212
  return this
3172
3213
  }
3214
+
3173
3215
  update(update) {
3174
3216
  Object.keys(update).forEach((key) => {
3175
- if (paymentGateway_updateAllowedProps.includes(key)) {
3176
- this[key] = update[key]
3217
+ if (creditNoteLine_updateAllowedProps.includes(key)) {
3218
+ if (key === 'amount' || key === 'unitPrice' || key === 'deduction') {
3219
+ this[key] = this._Amount.init(update[key])
3220
+ } else if (key === 'purchaseOptions') {
3221
+ this[key] = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(update[key])
3222
+ } else {
3223
+ this[key] = update[key]
3224
+ }
3177
3225
  }
3178
3226
  })
3179
3227
  return this.setModified()
3180
3228
  }
3181
3229
  }
3182
3230
 
3183
- function paymentGateway_setCode(options, key) {
3231
+ function creditNoteLine_setCode(options, key) {
3184
3232
  const copyOptions = options || {}
3185
3233
  if (copyOptions[key]) {
3186
3234
  return copyOptions[key]
@@ -3188,7 +3236,7 @@ function paymentGateway_setCode(options, key) {
3188
3236
  return stringHelper.setCode()
3189
3237
  }
3190
3238
 
3191
- function paymentGateway_setId(id) {
3239
+ function creditNoteLine_setId(id) {
3192
3240
  if (id && typeof id.toString === 'function') {
3193
3241
  return id.toString()
3194
3242
  }
@@ -3197,35 +3245,36 @@ function paymentGateway_setId(id) {
3197
3245
 
3198
3246
 
3199
3247
 
3200
- ;// ./lib/models/paymentGateway/paymentGatewayRepo.js
3248
+ ;// ./lib/models/creditNoteLine/creditNoteLineRepo.js
3201
3249
 
3202
3250
 
3203
3251
 
3204
- class PaymentGatewayRepo extends q_utilities_namespaceObject.Repo {
3252
+ class CreditNoteLineRepo extends q_utilities_namespaceObject.Repo {
3205
3253
  constructor(options = {}) {
3206
3254
  options = options || {}
3207
3255
  super(options)
3208
- const { _PaymentGateway } = options._constructor || {}
3209
- this._PaymentGateway = _PaymentGateway && (_PaymentGateway._superclass === PaymentGateway._superclass) ? _PaymentGateway : PaymentGateway
3256
+ const { _CreditNoteLine } = options._constructor || {}
3257
+ this._CreditNoteLine = _CreditNoteLine && (_CreditNoteLine._superclass === CreditNoteLine._superclass)
3258
+ ? _CreditNoteLine
3259
+ : CreditNoteLine
3210
3260
  }
3211
3261
  static get _classname() {
3212
- return 'PaymentGatewayRepo'
3262
+ return 'CreditNoteLineRepo'
3213
3263
  }
3214
3264
  init(options) {
3215
- return this._PaymentGateway.init(options)
3265
+ return this._CreditNoteLine.init(options)
3216
3266
  }
3217
3267
  }
3218
3268
 
3219
3269
 
3220
3270
 
3221
- ;// ./lib/models/paymentGateway/index.js
3222
-
3271
+ ;// ./lib/models/creditNoteLine/index.js
3223
3272
 
3224
3273
 
3225
3274
 
3226
3275
 
3227
3276
 
3228
- ;// ./lib/models/transaction/transaction.js
3277
+ ;// ./lib/models/creditNote/creditNote.js
3229
3278
 
3230
3279
 
3231
3280
 
@@ -3233,56 +3282,47 @@ class PaymentGatewayRepo extends q_utilities_namespaceObject.Repo {
3233
3282
 
3234
3283
 
3235
3284
 
3236
- const transaction_updateAllowedProps = [
3285
+ const creditNote_updateAllowedProps = [
3237
3286
  'active',
3238
- 'amount',
3239
- 'couponCodes',
3240
3287
  'deleted',
3241
- // 'refundedAmount',
3288
+ 'description',
3242
3289
  'remarks',
3243
3290
  'status'
3244
3291
  ]
3245
3292
 
3246
- class Transaction {
3293
+ class CreditNote {
3247
3294
  constructor(options = {}) {
3248
3295
  options = options || {}
3249
- const { _Amount, _Invoice, _PaymentGateway, _Status } = options._constructor || {}
3296
+ const { _Amount, _CreditNoteLine } = options._constructor || {}
3250
3297
  this._Amount = _Amount && (_Amount._superclass === Amount._superclass) ? _Amount : Amount
3251
- this._Invoice = _Invoice && (_Invoice._superclass === Invoice._superclass) ? _Invoice : Invoice
3252
- this._PaymentGateway = _PaymentGateway && (_PaymentGateway._superclass === PaymentGateway._superclass) ? _PaymentGateway : PaymentGateway
3298
+ this._CreditNoteLine = _CreditNoteLine && (_CreditNoteLine._superclass === CreditNoteLine._superclass) ? _CreditNoteLine : CreditNoteLine
3253
3299
  this._Status = _Status && (_Status._superclass === Status._superclass) ? _Status : Status
3254
3300
 
3255
- this._coupons = options._coupons
3256
- this._creditNotes = options._creditNotes
3257
- this._invoice = options._invoice
3258
- this._paymentGateway = options._paymentGateway
3301
+ this._type = options._type || 'CreditNote'
3259
3302
 
3260
3303
  const id = options._id || options.id
3261
- this.id = transaction_setId(id)
3262
- this._type = options._type || 'Transaction'
3304
+ this.id = creditNote_setId(id)
3263
3305
  this.active = (typeof options.active !== 'undefined') ? !!options.active : true
3264
3306
  this.amount = this._Amount.init(options.amount)
3265
- this.couponCodes = options.couponCodes || []
3266
3307
  this.created = options.created || (new Date()).valueOf()
3267
3308
  this.creator = options.creator
3268
- this.creditNoteCodes = options.creditNoteCodes || []
3309
+ this.creditNoteCode = creditNote_setCode(options, 'creditNoteCode')
3310
+ this.creditNoteLines = this._CreditNoteLine.initOnlyValidFromArray(options.creditNoteLines)
3269
3311
  this.deleted = options.deleted || false
3312
+ this.description = options.description
3270
3313
  this.invoiceCode = options.invoiceCode
3271
3314
  this.metadata = q_utilities_namespaceObject.Metadata.initOnlyValidFromArray(options.metadata)
3272
3315
  this.modified = options.modified || (new Date()).valueOf()
3273
3316
  this.owner = options.owner
3274
- this.paymentGatewayCode = options.paymentGatewayCode
3275
- this.paymentResultRef = options.paymentResultRef
3276
3317
  this.remarks = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(options.remarks)
3277
3318
  this.status = this._Status.init(options.status)
3278
3319
  this.tenantCode = options.tenantCode
3279
- this.transactionCode = transaction_setCode(options, 'transactionCode')
3280
3320
  }
3281
3321
 
3282
- // class method
3322
+ // Class Methods
3283
3323
  static dummyData() {
3284
3324
  return {
3285
- status: Status.dummyData(),
3325
+ invoiceCode: 'invoiceCode',
3286
3326
  tenantCode: 'tenantCode'
3287
3327
  }
3288
3328
  }
@@ -3303,233 +3343,47 @@ class Transaction {
3303
3343
  return this.initFromArray(arr).filter((i) => i)
3304
3344
  }
3305
3345
  static get _classname() {
3306
- return 'Transaction'
3346
+ return 'CreditNote'
3307
3347
  }
3308
3348
  static get _superclass() {
3309
- return 'Transaction'
3349
+ return 'CreditNote'
3310
3350
  }
3311
3351
 
3312
3352
  // getters
3313
- get isValid() {
3314
- return !!this.tenantCode
3315
- }
3316
- get invoice() {
3317
- return this._Invoice.init(this._invoice)
3318
- }
3319
3353
  get isActive() {
3320
- return this.active === true
3321
- }
3322
- get isAssigned() {
3323
- return this.status.isAssigned
3324
- }
3325
- get isCancelled() {
3326
- return this.status.isCancelled
3327
- }
3328
- get isConfirmed() {
3329
- return this.status.isConfirmed
3330
- }
3331
- get isCompleted() {
3332
- return this.status.isCompleted
3333
- }
3334
- get isExpired() {
3335
- return this.status.isExpired
3336
- }
3337
- get isFailed() {
3338
- return this.status.isFailed
3339
- }
3340
- get isOnHold() {
3341
- return this.status.isOnHold
3342
- }
3343
- get isPaid() {
3344
- return this.status.isPaid
3354
+ return !!this.active
3345
3355
  }
3356
+
3346
3357
  get isPending() {
3347
3358
  return this.status.isPending
3348
3359
  }
3349
- get isProcessing() {
3350
- return this.status.isProcessing
3351
- }
3352
- get isRedeemed() {
3353
- return this.status.isRedeemed
3354
- }
3355
- get isRefunded() {
3356
- return this.status.isRefunded
3357
- }
3358
- get isShared() {
3359
- return this.status.isShared
3360
- }
3361
- // get isSettled() {
3362
- // return this.payments.reduce((acc, payment) => {
3363
- // return acc || payment.isSettled
3364
- // }, false)
3365
- // }
3366
- get isSubmitted() {
3367
- return this.status.isSubmitted
3368
- }
3369
- get isWaived() {
3370
- return this.status.isWaived
3371
- }
3372
- get paymentGateway() {
3373
- return this._PaymentGateway.init(this._paymentGateway)
3374
- }
3375
3360
 
3376
- // get totalAmount() {
3377
- // return this.total.getAmount()
3378
- // }
3379
- // instance methods
3380
- // appendPayment({ paymentGateway }) {
3381
- // this.payments = this.payments.map((payment) => {
3382
- // payment.unsetAll()
3383
- // payment.setCancelled()
3384
- // return payment
3385
- // })
3386
- // // const totalAmount = Service.getTotalAmount(this.getServices())
3387
- // const payment = Payment.init({
3388
- // amount: this.total.getAmount(),
3389
- // currency: this.total.getCurrency(),
3390
- // paymentGatewayCode: paymentGateway.paymentGatewayCode,
3391
- // paymentStatus: {
3392
- // pending: true
3393
- // }
3394
- // })
3395
- // this.payments.push(payment)
3396
- // return this
3397
- // }
3398
- // getCurrency() {
3399
- // return this.total.getCurrency()
3400
- // }
3401
- getId() {
3402
- return this.id ? this.id.toString() : this.id
3361
+ get isValid() {
3362
+ return !!this.tenantCode && !!this.invoiceCode
3403
3363
  }
3404
- getLatestStatus() {
3405
- const status = {
3406
- ...this.status,
3407
- created: null,
3408
- modified: null
3364
+
3365
+ setAmount(amount) {
3366
+ const a = this_Amount.init(amount)
3367
+ if (this.isPending && a) {
3368
+ this.amount = a
3409
3369
  }
3410
- return Object.keys(status).reduce((a, b) => status[a] > status[b] ? a : b)
3411
- }
3412
- getCode() {
3413
- return this.transactionCode
3414
- }
3415
- // getPaymentByCode(paymentCode) {
3416
- // const payments = this.payments || []
3417
- // const filteredPayment = payments.filter((payment) => {
3418
- // return payment.isSame(paymentCode)
3419
- // })
3420
- // if (filteredPayment.length === 1) {
3421
- // return filteredPayment[0]
3422
- // }
3423
- // return null
3424
- // }
3425
- // getPendingPayment() {
3426
- // const payments = this.payments.filter((payment) => {
3427
- // return payment.canPay()
3428
- // })
3429
- // if (payments.length === 1) {
3430
- // return payments[0]
3431
- // }
3432
- // if (payments.length === 0) {
3433
- // throw Error('pending payment not found')
3434
- // }
3435
- // throw Error('duplicate pendingPayment found')
3436
- // }
3437
- isAllowDelete() {
3438
- return this.active && !this.deleted && !this.isCancelled && !this.isCompleted && !this.isPaid && !this.isWaived // && !this.isSettled
3439
- }
3440
- isAllowPay() {
3441
- return this.active && !this.deleted && !this.isCancelled && !this.isCompleted && !this.isPaid && !this.isWaived && !this.isExpired // && !this.isSettled
3442
- }
3443
- setAssigned() {
3444
- this.status.setAssigned()
3445
- return this
3446
- }
3447
- setCancelled() {
3448
- this.status.setCancelled()
3449
3370
  return this
3450
3371
  }
3451
3372
  setCompleted() {
3452
3373
  this.status.setCompleted()
3453
- return this
3454
- }
3455
- setFailed() {
3456
- this.status.setFailed()
3457
- return this
3374
+ return this.setModified()
3458
3375
  }
3459
3376
  setModified() {
3460
3377
  this.modified = (new Date()).valueOf()
3461
3378
  return this
3462
3379
  }
3463
- setOnHold() {
3464
- this.status.setOnHold()
3465
- return this
3466
- }
3467
- setPaid() {
3468
- this.status.setPaid()
3469
- return this
3470
- }
3471
- setPaymentResultRef(paymentResultRef) {
3472
- this.paymentResultRef = paymentResultRef
3473
- return this
3474
- }
3475
- setPending() {
3476
- this.status.setPending()
3477
- return this
3478
- }
3479
- setProcessing() {
3480
- this.status.setProcessing()
3481
- return this
3482
- }
3483
- setRedeemed() {
3484
- this.status.setRedeemed()
3485
- return this
3486
- }
3487
- setRefunded() {
3488
- this.status.setRefunded()
3489
- return this
3490
- }
3491
- setSettled() {
3492
- this.status.setSettled()
3493
- return this
3494
- }
3495
- setShared() {
3496
- this.status.setShared()
3497
- return this
3498
- }
3499
- setWaived() {
3500
- this.status.setWaived()
3501
- return this
3502
- }
3503
- updateStatus(status) {
3504
- switch (status) {
3505
- case 'pending':
3506
- this.setPending()
3507
- break
3508
- case 'on-hold':
3509
- this.setOnHold()
3510
- break
3511
- case 'failed':
3512
- this.setFailed()
3513
- break
3514
- case 'processing':
3515
- this.setProcessing()
3516
- break
3517
- case 'cancelled':
3518
- this.setCancelled()
3519
- break
3520
- case 'completed':
3521
- this.setCompleted()
3522
- break
3523
- case 'refunded':
3524
- this.setRefunded()
3525
- break
3526
- default:
3527
- break
3528
- }
3380
+ setPaid(t) {
3381
+ this.status.setPaid(t)
3382
+ return this.setModified()
3529
3383
  }
3530
3384
  update(update) {
3531
3385
  Object.keys(update).forEach((key) => {
3532
- if (transaction_updateAllowedProps.includes(key)) {
3386
+ if (creditNote_updateAllowedProps.includes(key)) {
3533
3387
  if (key === 'amount') {
3534
3388
  this[key] = this._Amount.init(update[key])
3535
3389
  } else if (key === 'metadata') {
@@ -3538,8 +3392,6 @@ class Transaction {
3538
3392
  this[key] = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(update[key])
3539
3393
  } else if (key === 'status') {
3540
3394
  this[key] = this._Status.init(update[key])
3541
- // } else if (key === 'refundedAmount') {
3542
- // this[key] = this.updateTotal(key, update[key])
3543
3395
  } else {
3544
3396
  this[key] = update[key]
3545
3397
  }
@@ -3547,19 +3399,9 @@ class Transaction {
3547
3399
  })
3548
3400
  return this.setModified()
3549
3401
  }
3550
- // updatePayment(payment = {}) {
3551
- // this.payments = Payment.updatePaymentItem(this.payments, payment)
3552
- // return this
3553
- // }
3554
- // updateTotal(key, obj) {
3555
- // if (obj instanceof Total) {
3556
- // return this[key].updateTotal(obj)
3557
- // }
3558
- // return setTotal(obj, key)
3559
- // }
3560
3402
  }
3561
3403
 
3562
- function transaction_setCode(options, key) {
3404
+ function creditNote_setCode(options, key) {
3563
3405
  const copyOptions = options || {}
3564
3406
  if (copyOptions[key]) {
3565
3407
  return copyOptions[key]
@@ -3567,105 +3409,86 @@ function transaction_setCode(options, key) {
3567
3409
  return stringHelper.setCode()
3568
3410
  }
3569
3411
 
3570
- function transaction_setId(id) {
3412
+ function creditNote_setId(id) {
3571
3413
  if (id && typeof id.toString === 'function') {
3572
3414
  return id.toString()
3573
3415
  }
3574
3416
  return id
3575
3417
  }
3576
3418
 
3577
- // function setTotal(obj, label) {
3578
- // obj = obj || {}
3579
- // return Total.init({ ...obj, label: obj.label || label })
3580
- // }
3581
-
3582
3419
 
3583
3420
 
3584
- ;// ./lib/models/transaction/transactionRepo.js
3421
+ ;// ./lib/models/creditNote/creditNoteRepo.js
3585
3422
 
3586
3423
 
3587
3424
 
3588
- class TransactionRepo extends q_utilities_namespaceObject.Repo {
3425
+ class CreditNoteRepo extends q_utilities_namespaceObject.Repo {
3589
3426
  constructor(options = {}) {
3590
3427
  options = options || {}
3591
3428
  super(options)
3592
- const { _Transaction } = options._constructor || {}
3593
- this._Transaction = _Transaction && (_Transaction._superclass === Transaction._superclass) ? _Transaction : Transaction
3429
+ const { _CreditNote } = options._constructor || {}
3430
+ this._CreditNote = _CreditNote && (_CreditNote._superclass === CreditNote._superclass) ? _CreditNote : CreditNote
3594
3431
  }
3595
3432
  static get _classname() {
3596
- return 'TransactionRepo'
3433
+ return 'CreditNoteRepo'
3597
3434
  }
3598
3435
  init(options) {
3599
- return this._Transaction.init(options)
3436
+ return this._CreditNote.init(options)
3600
3437
  }
3601
3438
  }
3602
3439
 
3603
3440
 
3604
3441
 
3605
- ;// ./lib/models/transaction/index.js
3606
-
3607
-
3608
-
3442
+ ;// ./lib/models/creditNote/index.js
3609
3443
 
3610
3444
 
3611
3445
 
3612
- ;// ./lib/models/walletItem/walletItem.js
3613
3446
 
3614
3447
 
3448
+ ;// ./lib/models/currency/currency.js
3615
3449
 
3616
3450
 
3617
3451
 
3618
3452
 
3619
- const walletItem_updateAllowedProps = [
3453
+ const currency_updateAllowedProps = [
3620
3454
  'active',
3621
3455
  'deleted',
3622
- 'owner',
3623
- 'purchaseOptions',
3624
- 'qty',
3456
+ 'description',
3457
+ 'name',
3625
3458
  'remarks',
3626
- 'status',
3459
+ 'symbol'
3627
3460
  ]
3628
3461
 
3629
- class WalletItem {
3462
+ class Currency {
3630
3463
  constructor(options = {}) {
3631
3464
  options = options || {}
3632
- const { _Product, _Status, _Transaction } = options._constructor || {}
3633
- this._Product = _Product && (_Product._superclass === Product._superclass) ? _Product : Product
3634
- this._Status = _Status && (_Status._superclass === Status._superclass) ? _Status : Status
3635
- this._Transaction = _Transaction && (_Transaction._superclass === Transaction._superclass) ? _Transaction : Transaction
3636
-
3637
- this._product = options._product
3638
- this._transaction = options._transaction
3639
-
3640
3465
  const id = options._id || options.id
3641
- this.id = walletItem_setId(id)
3466
+ this.id = currency_setId(id)
3467
+
3468
+ this._type = options._type || 'Currency'
3642
3469
  this.active = (typeof options.active !== 'undefined') ? !!options.active : true
3470
+ this.code = options.code // 'HKD'
3643
3471
  this.created = options.created || (new Date()).valueOf()
3644
3472
  this.creator = options.creator
3473
+ this.currencyCode = currency_setCode(options, 'currencyCode')
3645
3474
  this.deleted = options.deleted || false
3646
- this.metadata = Metadata.initOnlyValidFromArray(options.metadata)
3475
+ this.description = options.description
3476
+ this.factor = options.factor || 1 // 100
3647
3477
  this.modified = options.modified || (new Date()).valueOf()
3478
+ this.name = options.name
3648
3479
  this.owner = options.owner
3649
- this.productCode = options.productCode
3650
- this.purchaseOptions = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(options.purchaseOptions)
3651
- this.qty = options.qty || 1
3652
3480
  this.remarks = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(options.remarks)
3653
- this.status = this._Status.init(options.status)
3654
- this.tenant = options.tenant
3481
+ this.symbol = options.symbol || null
3655
3482
  this.tenantCode = options.tenantCode
3656
- this.transactionCode = options.transactionCode
3657
- this.walletItemCode = walletItem_setCode(options, 'walletItemCode')
3483
+ this.unit = options.unit
3658
3484
  }
3659
-
3660
- // Class methods
3661
3485
  static dummyData() {
3662
3486
  return {
3663
- // companyCode: 'demo',
3664
- productCode: '5cd4f5430c2b874ad7ed8cfd',
3665
- qty: 1,
3666
- status: Status.dummyData(),
3667
- tenantCode: 'tenantCode',
3668
- transactionCode: 'transactionCode'
3487
+ code: 'USD',
3488
+ factor: 100,
3489
+ name: 'US dollar',
3490
+ tenantCode: 'platform',
3491
+ unit: 'Cent',
3669
3492
  }
3670
3493
  }
3671
3494
  static init(options = {}) {
@@ -3685,177 +3508,30 @@ class WalletItem {
3685
3508
  return this.initFromArray(arr).filter((i) => i)
3686
3509
  }
3687
3510
  static get _classname() {
3688
- return 'WalletItem'
3511
+ return 'Currency'
3689
3512
  }
3690
3513
  static get _superclass() {
3691
- return 'WalletItem'
3514
+ return 'Currency'
3692
3515
  }
3693
3516
 
3694
- // getters
3695
3517
  get isValid() {
3696
- return (!!this.companyCode || !!this.tenantCode) && !!this.productCode && this.qty > 0
3697
- }
3698
- get isAssigned() {
3699
- return this.status.isAssigned
3700
- }
3701
- get isCancelled() {
3702
- return this.status.isCancelled
3703
- }
3704
- get isCompleted() {
3705
- return this.status.isCompleted
3706
- }
3707
- get isConfirmed() {
3708
- return this.status.isConfirmed
3709
- }
3710
- get isDelivered() {
3711
- return this.status.isDelivered
3712
- }
3713
- get isOwned() {
3714
- return (
3715
- !this.isCancelled && !this.isRejected && !this.isRefunded
3716
- && (
3717
- (this.isPaid && !this.isAssigned && !this.isConfirmed)
3718
- || (this.isWaived && !this.isAssigned && !this.isConfirmed)
3719
- || (this.isRedeemed && !this.isAssigned && !this.isConfirmed)
3720
- || (this.isAssigned && this.isConfirmed)
3721
- )
3722
- )
3723
- }
3724
- get isPaid() {
3725
- return this.status.isPaid
3726
- }
3727
- get isPending() {
3728
- return this.status.isPending
3729
- }
3730
- get isRedeemed() {
3731
- return this.status.redeemed
3732
- }
3733
- get isRefunded() {
3734
- return this.status.isRefunded
3735
- }
3736
- get isRejected() {
3737
- return this.status.rejected
3738
- }
3739
- get isShared() {
3740
- return this.status.shared
3741
- }
3742
- get isSubmitted() {
3743
- return this.status.isSubmitted
3744
- }
3745
- get isUsed() {
3746
- return this.status.isUsed
3747
- }
3748
- get isWaived() {
3749
- return this.status.isWaived
3750
- }
3751
- get product() {
3752
- return this._Product.init(this._product)
3753
- }
3754
- get transaction() {
3755
- return this._Transaction.init(this._transaction)
3756
- }
3757
- get usedTimestamp() {
3758
- return this.status.used
3518
+ return !!this.code && typeof this.factor === 'number' && !!this.name && !!this.unit && !!this.tenantCode
3759
3519
  }
3760
3520
 
3761
- // instance methods
3762
- formatFlexcel() {
3763
- const obj = {
3764
- ...this,
3765
- ...this.status,
3766
- used: this.isUsed,
3767
- productName: this.product && this.product.name ? this.product.name : ''
3768
- }
3769
- return obj
3770
- }
3771
- formatOutput({ format }) {
3772
- switch (format) {
3773
- case 'flexcel':
3774
- return this.formatFlexcel()
3775
- default:
3776
- return this
3777
- }
3778
- }
3779
- isSameOwner(userId) {
3780
- return userId
3781
- ? (userId === this.owner)
3782
- : false
3783
- }
3784
- setAssigned(value) {
3785
- this.status.setAssigned(value)
3786
- return this
3787
- }
3788
- setConfirmed(value) {
3789
- this.status.setConfirmed(value)
3790
- return this
3791
- }
3792
- setDelivered(value) {
3793
- this.status.setDelivered(value)
3794
- return this
3521
+ fromCurrencyValue(value) {
3522
+ return value / this.factor
3795
3523
  }
3524
+
3796
3525
  setModified() {
3797
3526
  this.modified = (new Date()).valueOf()
3798
3527
  return this
3799
3528
  }
3800
- setPaid() {
3801
- this.status.setPaid()
3802
- return this
3803
- }
3804
- setPaidTimestamp(paidTimestamp) {
3805
- this.status.setPaid(paidTimestamp)
3806
- // this.statusTimestamp.setPaid(paidTimestamp)
3807
- return this
3808
- }
3809
- setPending() {
3810
- this.status.setPending()
3811
- return this
3812
- }
3813
- setRedeemed() {
3814
- this.status.setRedeemed()
3815
- return this
3816
- }
3817
- setRejected() {
3818
- this.status.setRejected()
3819
- return this
3820
- }
3821
- setShared() {
3822
- this.status.setShared()
3823
- return this
3824
- }
3825
- setUsedTimestamp() {
3826
- this.status.setUsed()
3827
- // this.statusTimestamp.setUsed()
3828
- return this
3829
- }
3830
- setWaived() {
3831
- this.status.setWaived()
3832
- return this
3833
- }
3834
- // toTicket() {
3835
- // return {
3836
- // delivered: this.status.delivered,
3837
- // eventRegistrationCode: this.eventRegistrationCode,
3838
- // eventShortCode: this.eventShortCode,
3839
- // name: this.product.name,
3840
- // printLayoutCodes: this.product.printLayoutCodes,
3841
- // productCategories: this.product.productCategories,
3842
- // productCode: this.productCode,
3843
- // settled: this.status.settled,
3844
- // takenTime: this.statusTimestamp.delivered,
3845
- // tenantCode: this.tenantCode,
3846
- // waived: this.status.waived,
3847
- // walletItemCode: this.walletItemCode
3848
- // }
3849
- // }
3529
+
3850
3530
  update(update) {
3851
3531
  Object.keys(update).forEach((key) => {
3852
- if (walletItem_updateAllowedProps.includes(key)) {
3853
- if (key === 'metadata') {
3854
- this[key] = Metadata.initOnlyValidFromArray(update[key])
3855
- } else if (key === 'purchaseOptions' || key === 'remarks') {
3532
+ if (currency_updateAllowedProps.includes(key)) {
3533
+ if (key === 'remarks') {
3856
3534
  this[key] = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(update[key])
3857
- } else if (key === 'status') {
3858
- this[key] = this._Status.init(update[key])
3859
3535
  } else {
3860
3536
  this[key] = update[key]
3861
3537
  }
@@ -3865,14 +3541,7 @@ class WalletItem {
3865
3541
  }
3866
3542
  }
3867
3543
 
3868
- function walletItem_setId(id) {
3869
- if (id && typeof id.toString === 'function') {
3870
- return id.toString()
3871
- }
3872
- return id
3873
- }
3874
-
3875
- function walletItem_setCode(options, key) {
3544
+ function currency_setCode(options, key) {
3876
3545
  const copyOptions = options || {}
3877
3546
  if (copyOptions[key]) {
3878
3547
  return copyOptions[key]
@@ -3880,82 +3549,107 @@ function walletItem_setCode(options, key) {
3880
3549
  return stringHelper.setCode()
3881
3550
  }
3882
3551
 
3552
+ function currency_setId(id) {
3553
+ if (id && typeof id.toString === 'function') {
3554
+ return id.toString()
3555
+ }
3556
+ return id
3557
+ }
3558
+
3883
3559
 
3884
3560
 
3885
- ;// ./lib/models/walletItem/walletItemRepo.js
3561
+ ;// ./lib/models/currency/currencyRepo.js
3886
3562
 
3887
3563
 
3888
3564
 
3889
- class WalletItemRepo extends q_utilities_namespaceObject.Repo {
3565
+ class CurrencyRepo extends q_utilities_namespaceObject.Repo {
3890
3566
  constructor(options = {}) {
3891
3567
  options = options || {}
3892
3568
  super(options)
3893
- const { _WalletItem } = options._constructor || {}
3894
- this._WalletItem = _WalletItem && (_WalletItem._superclass === WalletItem._superclass) ? _WalletItem : WalletItem
3569
+ const { _Currency } = options._constructor || {}
3570
+ this._Currency = _Currency && (_Currency._superclass === Currency._superclass)
3571
+ ? _Currency
3572
+ : Currency
3895
3573
  }
3896
3574
  static get _classname() {
3897
- return 'WalletItemRepo'
3575
+ return 'CurrencyRepo'
3898
3576
  }
3899
3577
  init(options) {
3900
- return this._WalletItem.init(options)
3578
+ return this._Currency.init(options)
3901
3579
  }
3902
3580
  }
3903
3581
 
3904
3582
 
3905
3583
 
3906
- ;// ./lib/models/walletItem/index.js
3584
+ ;// ./lib/models/currency/index.js
3907
3585
 
3908
3586
 
3909
3587
 
3910
3588
 
3911
3589
 
3912
- ;// ./lib/models/cart/cart.js
3913
3590
 
3591
+ ;// ./lib/models/invoiceLine/invoiceLine.js
3914
3592
 
3915
3593
 
3916
3594
 
3917
3595
 
3918
3596
 
3919
3597
 
3920
- const cart_updateAllowedProps = [
3598
+
3599
+ const invoiceLine_updateAllowedProps = [
3921
3600
  'active',
3922
- 'remarks',
3601
+ 'amount',
3602
+ 'deduction',
3923
3603
  'deleted',
3604
+ 'description',
3605
+ 'discount',
3606
+ 'purchaseOptions',
3607
+ 'qty',
3608
+ 'remarks',
3609
+ 'unitPrice'
3924
3610
  ]
3925
3611
 
3926
- class Cart {
3612
+ class InvoiceLine {
3927
3613
  constructor(options = {}) {
3928
3614
  options = options || {}
3929
- const { _CartItem, _Merchandise, _Status, _WalletItem } = options._constructor || {}
3930
- this._CartItem = _CartItem && (_CartItem._superclass === CartItem._superclass) ? _CartItem : CartItem
3615
+ const { _Amount, _Invoice, _Merchandise, _Status } = options._constructor || {}
3616
+ this._Amount = _Amount && (_Amount._superclass === Amount._superclass) ? _Amount : Amount
3617
+ this._Invoice = _Invoice && (_Invoice._superclass === Invoice._superclass) ? _Invoice : Invoice
3931
3618
  this._Merchandise = _Merchandise && (_Merchandise._superclass === Merchandise._superclass) ? _Merchandise : Merchandise
3932
3619
  this._Status = _Status && (_Status._superclass === Status._superclass) ? _Status : Status
3933
- this._WalletItem = _WalletItem && (_WalletItem._superclass === WalletItem._superclass) ? _WalletItem : WalletItem
3934
3620
 
3935
- const id = options._id || options.id
3936
- this.id = cart_setId(id)
3621
+ this._invoice = options._invoice
3622
+ this._merchandise = options._merchandise
3937
3623
 
3938
- this._merchandises = options._merchandises
3939
- this._type = options._type || 'Cart'
3940
-
3624
+ const id = options._id || options.id
3625
+ this.id = invoiceLine_setId(id)
3626
+ this._type = options._type || 'InvoiceLine'
3941
3627
  this.active = (typeof options.active !== 'undefined') ? !!options.active : true
3942
- this.cartCode = cart_setCode(options, 'cartCode')
3943
- this.cartItems = this._CartItem.initOnlyValidFromArray(options.cartItems)
3628
+ this.amount = this._Amount.init(options.amount)
3944
3629
  this.created = options.created || (new Date()).valueOf()
3945
3630
  this.creator = options.creator
3631
+ this.deduction = this._Amount.init(options.deduction)
3946
3632
  this.deleted = options.deleted || false
3947
- this.metadata = q_utilities_namespaceObject.Metadata.initOnlyValidFromArray(options.metadata)
3633
+ this.description = options.description
3634
+ this.discount = options.discount || 0
3635
+ this.invoiceCode = options.invoiceCode
3636
+ this.invoiceLineCode = invoiceLine_setCode(options, 'invoiceLineCode')
3637
+ this.merchandiseCode = options.merchandiseCode
3948
3638
  this.modified = options.modified || (new Date()).valueOf()
3949
- this.owner = options.owner
3639
+ this.purchaseOptions = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(options.purchaseOptions)
3640
+ this.qty = options.qty || 1
3950
3641
  this.remarks = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(options.remarks)
3951
3642
  this.status = this._Status.init(options.status)
3952
3643
  this.tenantCode = options.tenantCode
3644
+ this.unitPrice = this._Amount.init(options.unitPrice)
3645
+ this.waived = options.waived || 0
3953
3646
  }
3954
3647
 
3955
3648
  // Class Methods
3956
3649
  static dummyData() {
3957
3650
  return {
3958
- cartItems: [CartItem.dummyData()],
3651
+ amount: Amount.dummyData(),
3652
+ invoiceCode: 'invoiceCode',
3959
3653
  tenantCode: 'tenantCode'
3960
3654
  }
3961
3655
  }
@@ -3976,203 +3670,35 @@ class Cart {
3976
3670
  return this.initFromArray(arr).filter((i) => i)
3977
3671
  }
3978
3672
  static get _classname() {
3979
- return 'Cart'
3673
+ return 'InvoiceLine'
3980
3674
  }
3981
3675
  static get _superclass() {
3982
- return 'Cart'
3676
+ return 'InvoiceLine'
3983
3677
  }
3984
3678
 
3985
3679
  // getters
3986
3680
  get isValid() {
3987
- return !!this.tenantCode
3988
- }
3989
- get isActive() {
3990
- return !!this.active
3681
+ return !!this.amount && !!this.invoiceCode && !!this.tenantCode
3991
3682
  }
3992
- get merchandises() {
3993
- return this._Merchandise.initOnlyValidFromArray(this._merchandises || [])
3683
+ get invoice() {
3684
+ return this._Invoice.init(this._invoice)
3994
3685
  }
3995
- get merchandisesSummary() {
3996
- return this.merchandises.reduce((acc, m) => {
3997
- const found = acc.find((a) => {
3998
- return a.merchandiseCode === m.merchandiseCode
3999
- })
4000
- if (found) {
4001
- found.qty += 1
4002
- } else {
4003
- acc.push({
4004
- ...m.summary,
4005
- qty: 1
4006
- })
4007
- }
4008
- return acc
4009
- }, [])
3686
+ get merchandise() {
3687
+ return this._Merchandise.init(this._merchandise)
4010
3688
  }
4011
3689
 
4012
3690
  // instance methods
4013
- checkoutAvailable({ ownedWalletItems, selectCartItems, isCoordinator = false }) {
4014
- try {
4015
- if (!isCoordinator) {
4016
- return this.checkHasStock({ selectCartItems }) && !this.checkWalletItemReachLimit({ ownedWalletItems, selectCartItems }) && !this.checkMerchandiseReachLimit({ selectCartItems })
4017
- }
4018
- return this.checkHasStock({ selectCartItems })
4019
- } catch (err) {
4020
- throw err
4021
- }
4022
- }
4023
- checkHasStock({ selectCartItems }) {
4024
- try {
4025
- selectCartItems.forEach((cartItem) => {
4026
- const { qty } = cartItem
4027
- if (this.merchandises && this.merchandises.length === 0) {
4028
- throw new Error('not have merchandises array object')
4029
- }
4030
- const [merchandise] = this.merchandises.filter((item) => item.merchandiseCode === cartItem.merchandiseCode)
4031
- const { products } = merchandise // merchandise is populated
4032
- if (products && products.length === 0) {
4033
- throw new Error('merchandise not have products array object')
4034
- }
4035
- if (!merchandise.checkHasStock(qty)) {
4036
- throw new Error('merchandise not enough stock')
4037
- }
4038
- products.forEach((product) => {
4039
- if (!product.hasStock(qty)) {
4040
- throw new Error('product not enough stock')
4041
- }
4042
- })
4043
- })
4044
- return true
4045
- } catch (err) {
4046
- throw err
4047
- }
4048
- }
4049
- checkWalletItemReachLimit({ ownedWalletItems, selectCartItems }) {
4050
- try {
4051
- selectCartItems.forEach((cartItem) => {
4052
- const { qty } = cartItem
4053
- const [merchandise] = this.merchandises.filter((item) => item.merchandiseCode === cartItem.merchandiseCode)
4054
- const { products } = merchandise
4055
- if (products && products.length === 0) {
4056
- throw new Error('merchandise not have products array object')
4057
- }
4058
- products.forEach((product) => {
4059
- const hasWalletItems = ownedWalletItems.filter((walletItem) => walletItem.productCode === product.productCode)
4060
- const hasQty = hasWalletItems.length || 0
4061
- if (!product.ableToHave(hasQty + qty)) {
4062
- throw new Error('walletItem reach limit')
4063
- }
4064
- })
4065
- })
4066
- return false
4067
- } catch (err) {
4068
- throw err
4069
- }
4070
- }
4071
- checkMerchandiseReachLimit({ selectCartItems }) {
4072
- try {
4073
- selectCartItems.forEach((cartItem) => {
4074
- const { qty } = cartItem
4075
- const [merchandise] = this.merchandises.filter((item) => item.merchandiseCode === cartItem.merchandiseCode)
4076
- if (qty > merchandise.max) {
4077
- throw new Error('merchandise reach limit')
4078
- }
4079
- })
4080
- return false
4081
- } catch (err) {
4082
- throw err
4083
- }
4084
- }
4085
- clearCartItems() {
4086
- this.cartItems = []
4087
- return this
4088
- }
4089
- createInvoiceLines() {
4090
- return this.cartItems.map((cartItem) => {
4091
- return Object.assign(
4092
- cartItem.createInvoiceLine(),
4093
- { cartCode: this.cartCode, tenantCode: this.tenantCode }
4094
- )
4095
- })
4096
- }
4097
- getCartItems() {
4098
- return this.cartItems
4099
- }
4100
- getCartItemCodes() {
4101
- return this.cartItems.map((cartItem) => cartItem.cartItemCode)
4102
- }
4103
- getCurrencyCode() {
4104
- return this.cartItems.length > 0
4105
- ? this.cartItems[0].getCurrencyCode()
4106
- : null
4107
- }
4108
- getMerchandiseAvailableInfos({ cartItems, currency, roleCodes }) {
4109
- if ((this.cartItems.length > 0 && this.merchandises.length === 0)) {
4110
- throw new Error('No merchandises')
4111
- }
4112
- const self = this
4113
- cartItems = cartItems || self.cartItems
4114
- cartItems = this._CartItem.initOnlyValidFromArray(cartItems)
4115
- if (cartItems.length === 0) {
4116
- throw new Error('No cartItems')
4117
- }
4118
- return self.merchandises.reduce((acc, merchandise) => {
4119
- const filteredCartItems = this._CartItem.getByMerchandiseCode(cartItems, merchandise.merchandiseCode)
4120
- const info = merchandise.getAvailableInfo({ cartItems: filteredCartItems, currency, roleCodes })
4121
- acc.push(info)
4122
- return acc
4123
- }, [])
4124
- }
4125
- initWalletItemsFromCartItems(walletItemData) {
4126
- if (this.cartItems.length === 0) {
4127
- throw new Error('No cartItems')
4128
- }
4129
- const initWalletItems = this.cartItems.reduce((acc, cartItem) => {
4130
- const { merchandiseCode, qty } = cartItem
4131
- const [merchandise] = this.merchandises.filter((merchandiseItem) => merchandiseItem.merchandiseCode === merchandiseCode)
4132
- merchandise.productCodes.forEach((productCode) => {
4133
- const walletItems = Array.from({ length: qty }, (item, index) => {
4134
- const data = this._WalletItem.init({
4135
- ...walletItemData,
4136
- qty: 1,
4137
- productCode
4138
- })
4139
- data.setPending()
4140
- return data
4141
- })
4142
- acc = acc.concat(walletItems)
4143
- })
4144
- return acc
4145
- }, [])
4146
- return initWalletItems
4147
- }
4148
- setCartItems(cartItems) {
4149
- this.cartItems = this._CartItem.initOnlyValidFromArray(cartItems)
4150
- return this
4151
- }
4152
- setCompleted() {
4153
- this.status.setCompleted()
4154
- return this.setModified()
4155
- }
4156
3691
  setModified() {
4157
3692
  this.modified = (new Date()).valueOf()
4158
3693
  return this
4159
3694
  }
4160
- updateAvailable({ isCoordinator, selectCartItems }) {
4161
- try {
4162
- if (!isCoordinator) {
4163
- return this.checkHasStock({ selectCartItems }) && !this.checkMerchandiseReachLimit({ selectCartItems })
4164
- }
4165
- return this.checkHasStock({ selectCartItems })
4166
- } catch (err) {
4167
- throw err
4168
- }
4169
- }
3695
+
4170
3696
  update(update) {
4171
3697
  Object.keys(update).forEach((key) => {
4172
- if (cart_updateAllowedProps.includes(key)) {
4173
- if (key === 'metadata') {
4174
- this[key] = q_utilities_namespaceObject.Metadata.initOnlyValidFromArray(update[key])
4175
- } else if (key === 'remarks') {
3698
+ if (invoiceLine_updateAllowedProps.includes(key)) {
3699
+ if (key === 'amount' || key === 'unitPrice' || key === 'deduction') {
3700
+ this[key] = this._Amount.init(update[key])
3701
+ } else if (key === 'purchaseOptions') {
4176
3702
  this[key] = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(update[key])
4177
3703
  } else {
4178
3704
  this[key] = update[key]
@@ -4181,136 +3707,470 @@ class Cart {
4181
3707
  })
4182
3708
  return this.setModified()
4183
3709
  }
4184
- updateStock() {
4185
- if (this.cartItems.length === 0) {
4186
- throw new Error('No cartItems')
4187
- }
4188
- let newProducts = []
4189
- let newMerchandises = []
4190
- this.cartItems.forEach((cartItem) => {
4191
- const { merchandiseCode, qty } = cartItem
4192
- const [merchandise] = this.merchandises.filter((item) => item.merchandiseCode === merchandiseCode)
4193
- const newMerchandiseStock = merchandise.stock - qty
4194
- const { products } = merchandise
4195
- products.forEach((product) => {
4196
- const newProductStock = product.stock - cartItem.qty
4197
- product.updateProduct({ stock: newProductStock })
4198
- })
4199
- newProducts = newProducts.concat(products)
4200
- merchandise.updateMerchandise({ stock: newMerchandiseStock })
4201
- newMerchandises = newMerchandises.concat(merchandise)
4202
- })
4203
- return { newProducts, newMerchandises }
4204
- }
4205
3710
  }
4206
3711
 
4207
- function cart_setCode(options, key) {
3712
+ function invoiceLine_setCode(options, key) {
4208
3713
  const copyOptions = options || {}
4209
3714
  if (copyOptions[key]) {
4210
3715
  return copyOptions[key]
4211
3716
  }
4212
- return stringHelper.setCode()
3717
+ return stringHelper.setCode()
3718
+ }
3719
+
3720
+ function invoiceLine_setId(id) {
3721
+ if (id && typeof id.toString === 'function') {
3722
+ return id.toString()
3723
+ }
3724
+ return id
3725
+ }
3726
+
3727
+
3728
+
3729
+ ;// ./lib/models/invoiceLine/invoiceLineRepo.js
3730
+
3731
+
3732
+
3733
+ class InvoiceLineRepo extends q_utilities_namespaceObject.Repo {
3734
+ constructor(options = {}) {
3735
+ options = options || {}
3736
+ super(options)
3737
+ const { _InvoiceLine } = options._constructor || {}
3738
+ this._InvoiceLine = _InvoiceLine && (_InvoiceLine._superclass === InvoiceLine._superclass) ? _InvoiceLine : InvoiceLine
3739
+ }
3740
+ static get _classname() {
3741
+ return 'InvoiceLineRepo'
3742
+ }
3743
+ init(options) {
3744
+ return this._InvoiceLine.init(options)
3745
+ }
3746
+ }
3747
+
3748
+
3749
+
3750
+ ;// ./lib/models/invoiceLine/index.js
3751
+
3752
+
3753
+
3754
+
3755
+
3756
+
3757
+ ;// ./lib/models/issuer/issuer.js
3758
+
3759
+ class Issuer {
3760
+ constructor(options = {}) {
3761
+ options = options || {}
3762
+ this.bankAccount = options.bankAccount
3763
+ this.contactInformation = options.contactInformation
3764
+ this.name = options.name
3765
+ this.logo = options.logo
3766
+ }
3767
+
3768
+ // Class Methods
3769
+ static dummyData() {
3770
+ return {
3771
+ name: 'name'
3772
+ }
3773
+ }
3774
+ static init(options = {}) {
3775
+ if (options instanceof this) {
3776
+ return options
3777
+ }
3778
+ const instance = new this(options)
3779
+ return instance.isValid ? instance : null
3780
+ }
3781
+ static initFromArray(arr = []) {
3782
+ if (Array.isArray(arr)) {
3783
+ return arr.map((a) => this.init(a))
3784
+ }
3785
+ return []
3786
+ }
3787
+ static initOnlyValidFromArray(arr = []) {
3788
+ return this.initFromArray(arr).filter((i) => i)
3789
+ }
3790
+ static get _classname() {
3791
+ return 'Issuer'
3792
+ }
3793
+ static get _superclass() {
3794
+ return 'Issuer'
3795
+ }
3796
+
3797
+ // getters
3798
+ get isValid() {
3799
+ return !!this.name
3800
+ }
3801
+
3802
+ // instance methods
3803
+ updateIssuer(update) {
3804
+ Object.keys(update).forEach((key) => {
3805
+ this[key] = update[key]
3806
+ })
3807
+ return this
3808
+ }
3809
+ }
3810
+
3811
+
3812
+
3813
+ ;// ./lib/models/walletItem/walletItem.js
3814
+
3815
+
3816
+
3817
+
3818
+ // import { Transaction } from '../transaction/index.js'
3819
+
3820
+ const walletItem_updateAllowedProps = [
3821
+ 'active',
3822
+ 'deleted',
3823
+ 'owner',
3824
+ 'purchaseOptions',
3825
+ 'qty',
3826
+ 'remarks',
3827
+ 'status',
3828
+ ]
3829
+
3830
+ class WalletItem {
3831
+ constructor(options = {}) {
3832
+ options = options || {}
3833
+ const { _Product, _Status } = options._constructor || {}
3834
+ this._Product = _Product && (_Product._superclass === Product._superclass) ? _Product : Product
3835
+ this._Status = _Status && (_Status._superclass === Status._superclass) ? _Status : Status
3836
+ // this._Transaction = _Transaction && (_Transaction._superclass === Transaction._superclass) ? _Transaction : Transaction
3837
+
3838
+ this._product = options._product
3839
+ this._tenant = options._tenant
3840
+ this._transaction = options._transaction
3841
+
3842
+ const id = options._id || options.id
3843
+ this.id = walletItem_setId(id)
3844
+ this.active = (typeof options.active !== 'undefined') ? !!options.active : true
3845
+ this.created = options.created || (new Date()).valueOf()
3846
+ this.creator = options.creator
3847
+ this.deleted = options.deleted || false
3848
+ this.metadata = q_utilities_namespaceObject.Metadata.initOnlyValidFromArray(options.metadata)
3849
+ this.modified = options.modified || (new Date()).valueOf()
3850
+ this.owner = options.owner
3851
+ this.productCode = options.productCode
3852
+ this.purchaseOptions = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(options.purchaseOptions)
3853
+ this.qty = options.qty || 1
3854
+ this.remarks = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(options.remarks)
3855
+ this.status = this._Status.init(options.status)
3856
+ this.tenantCode = options.tenantCode
3857
+ this.transactionCode = options.transactionCode
3858
+ this.walletItemCode = walletItem_setCode(options, 'walletItemCode')
3859
+ }
3860
+
3861
+ // Class methods
3862
+ static dummyData() {
3863
+ return {
3864
+ // companyCode: 'demo',
3865
+ productCode: '5cd4f5430c2b874ad7ed8cfd',
3866
+ qty: 1,
3867
+ status: Status.dummyData(),
3868
+ tenantCode: 'tenantCode',
3869
+ transactionCode: 'transactionCode'
3870
+ }
3871
+ }
3872
+ static init(options = {}) {
3873
+ if (options instanceof this) {
3874
+ return options
3875
+ }
3876
+ const instance = new this(options)
3877
+ return instance.isValid ? instance : null
3878
+ }
3879
+ static initFromArray(arr = []) {
3880
+ if (Array.isArray(arr)) {
3881
+ return arr.map((a) => this.init(a))
3882
+ }
3883
+ return []
3884
+ }
3885
+ static initOnlyValidFromArray(arr = []) {
3886
+ return this.initFromArray(arr).filter((i) => i)
3887
+ }
3888
+ static get _classname() {
3889
+ return 'WalletItem'
3890
+ }
3891
+ static get _superclass() {
3892
+ return 'WalletItem'
3893
+ }
3894
+
3895
+ // getters
3896
+ get isValid() {
3897
+ return !!this.tenantCode && !!this.productCode
3898
+ }
3899
+ get isAssigned() {
3900
+ return this.status.isAssigned
3901
+ }
3902
+ get isCancelled() {
3903
+ return this.status.isCancelled
3904
+ }
3905
+ get isCompleted() {
3906
+ return this.status.isCompleted
3907
+ }
3908
+ get isConfirmed() {
3909
+ return this.status.isConfirmed
3910
+ }
3911
+ get isDelivered() {
3912
+ return this.status.isDelivered
3913
+ }
3914
+ get isOwned() {
3915
+ return (
3916
+ !this.isCancelled && !this.isRejected && !this.isRefunded
3917
+ && (
3918
+ (this.isPaid && !this.isAssigned && !this.isConfirmed)
3919
+ || (this.isWaived && !this.isAssigned && !this.isConfirmed)
3920
+ || (this.isRedeemed && !this.isAssigned && !this.isConfirmed)
3921
+ || (this.isAssigned && this.isConfirmed)
3922
+ )
3923
+ )
3924
+ }
3925
+ get isPaid() {
3926
+ return this.status.isPaid
3927
+ }
3928
+ get isPending() {
3929
+ return this.status.isPending
3930
+ }
3931
+ get isRedeemed() {
3932
+ return this.status.redeemed
3933
+ }
3934
+ get isRefunded() {
3935
+ return this.status.isRefunded
3936
+ }
3937
+ get isRejected() {
3938
+ return this.status.rejected
3939
+ }
3940
+ get isShared() {
3941
+ return this.status.shared
3942
+ }
3943
+ get isSubmitted() {
3944
+ return this.status.isSubmitted
3945
+ }
3946
+ get isUsed() {
3947
+ return this.status.isUsed
3948
+ }
3949
+ get isWaived() {
3950
+ return this.status.isWaived
3951
+ }
3952
+ get product() {
3953
+ return this._Product.init(this._product)
3954
+ }
3955
+ get tenant() {
3956
+ return this._tenant
3957
+ }
3958
+ // get transaction() {
3959
+ // return this._Transaction.init(this._transaction)
3960
+ // }
3961
+ get usedTimestamp() {
3962
+ return this.status.used
3963
+ }
3964
+
3965
+ // instance methods
3966
+ formatFlexcel() {
3967
+ const obj = {
3968
+ ...this,
3969
+ ...this.status,
3970
+ used: this.isUsed,
3971
+ productName: this.product && this.product.name ? this.product.name : ''
3972
+ }
3973
+ return obj
3974
+ }
3975
+ formatOutput({ format }) {
3976
+ switch (format) {
3977
+ case 'flexcel':
3978
+ return this.formatFlexcel()
3979
+ default:
3980
+ return this
3981
+ }
3982
+ }
3983
+ isSameOwner(userId) {
3984
+ return userId
3985
+ ? (userId === this.owner)
3986
+ : false
3987
+ }
3988
+ setAssigned(value) {
3989
+ this.status.setAssigned(value)
3990
+ return this
3991
+ }
3992
+ setConfirmed(value) {
3993
+ this.status.setConfirmed(value)
3994
+ return this
3995
+ }
3996
+ setDelivered(value) {
3997
+ this.status.setDelivered(value)
3998
+ return this
3999
+ }
4000
+ setModified() {
4001
+ this.modified = (new Date()).valueOf()
4002
+ return this
4003
+ }
4004
+ setPaid() {
4005
+ this.status.setPaid()
4006
+ return this
4007
+ }
4008
+ setPaidTimestamp(paidTimestamp) {
4009
+ this.status.setPaid(paidTimestamp)
4010
+ // this.statusTimestamp.setPaid(paidTimestamp)
4011
+ return this
4012
+ }
4013
+ setPending() {
4014
+ this.status.setPending()
4015
+ return this
4016
+ }
4017
+ setRedeemed() {
4018
+ this.status.setRedeemed()
4019
+ return this
4020
+ }
4021
+ setRejected() {
4022
+ this.status.setRejected()
4023
+ return this
4024
+ }
4025
+ setShared() {
4026
+ this.status.setShared()
4027
+ return this
4028
+ }
4029
+ setUsedTimestamp() {
4030
+ this.status.setUsed()
4031
+ // this.statusTimestamp.setUsed()
4032
+ return this
4033
+ }
4034
+ setWaived() {
4035
+ this.status.setWaived()
4036
+ return this
4037
+ }
4038
+ // toTicket() {
4039
+ // return {
4040
+ // delivered: this.status.delivered,
4041
+ // eventRegistrationCode: this.eventRegistrationCode,
4042
+ // eventShortCode: this.eventShortCode,
4043
+ // name: this.product.name,
4044
+ // printLayoutCodes: this.product.printLayoutCodes,
4045
+ // productCategories: this.product.productCategories,
4046
+ // productCode: this.productCode,
4047
+ // settled: this.status.settled,
4048
+ // takenTime: this.statusTimestamp.delivered,
4049
+ // tenantCode: this.tenantCode,
4050
+ // waived: this.status.waived,
4051
+ // walletItemCode: this.walletItemCode
4052
+ // }
4053
+ // }
4054
+ update(update) {
4055
+ Object.keys(update).forEach((key) => {
4056
+ if (walletItem_updateAllowedProps.includes(key)) {
4057
+ if (key === 'metadata') {
4058
+ this[key] = q_utilities_namespaceObject.Metadata.initOnlyValidFromArray(update[key])
4059
+ } else if (key === 'purchaseOptions' || key === 'remarks') {
4060
+ this[key] = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(update[key])
4061
+ } else if (key === 'status') {
4062
+ this[key] = this._Status.init(update[key])
4063
+ } else {
4064
+ this[key] = update[key]
4065
+ }
4066
+ }
4067
+ })
4068
+ return this.setModified()
4069
+ }
4213
4070
  }
4214
4071
 
4215
- function cart_setId(id) {
4072
+ function walletItem_setId(id) {
4216
4073
  if (id && typeof id.toString === 'function') {
4217
4074
  return id.toString()
4218
4075
  }
4219
4076
  return id
4220
4077
  }
4221
4078
 
4222
- // function updateCartItems(cartItems = []) {
4223
- // if (cartItems.length === 0) {
4224
- // return cartItems
4225
- // }
4226
- // let copyCartItems = CartItem.initOnlyValidFromArray(cartItems)
4227
- // copyCartItems = copyCartItems.sort((a, b) => b.created - a.created)
4228
- // return CartItem.mergeDuplicateCartItems(copyCartItems)
4229
- // }
4079
+ function walletItem_setCode(options, key) {
4080
+ const copyOptions = options || {}
4081
+ if (copyOptions[key]) {
4082
+ return copyOptions[key]
4083
+ }
4084
+ return stringHelper.setCode()
4085
+ }
4230
4086
 
4231
4087
 
4232
4088
 
4233
- ;// ./lib/models/cart/cartRepo.js
4089
+ ;// ./lib/models/walletItem/walletItemRepo.js
4234
4090
 
4235
4091
 
4236
4092
 
4237
- class CartRepo extends q_utilities_namespaceObject.Repo {
4093
+ class WalletItemRepo extends q_utilities_namespaceObject.Repo {
4238
4094
  constructor(options = {}) {
4239
4095
  options = options || {}
4240
4096
  super(options)
4241
- const { _Cart } = options._constructor || {}
4242
- this._Cart = _Cart && (_Cart._superclass === Cart._superclass)
4243
- ? _Cart
4244
- : Cart
4097
+ const { _WalletItem } = options._constructor || {}
4098
+ this._WalletItem = _WalletItem && (_WalletItem._superclass === WalletItem._superclass) ? _WalletItem : WalletItem
4245
4099
  }
4246
4100
  static get _classname() {
4247
- return 'CartRepo'
4101
+ return 'WalletItemRepo'
4248
4102
  }
4249
4103
  init(options) {
4250
- return this._Cart.init(options)
4104
+ return this._WalletItem.init(options)
4251
4105
  }
4252
4106
  }
4253
4107
 
4254
4108
 
4255
4109
 
4256
- ;// ./lib/models/cart/index.js
4110
+ ;// ./lib/models/walletItem/index.js
4257
4111
 
4258
4112
 
4259
4113
 
4260
4114
 
4261
4115
 
4262
- ;// ./lib/models/category/category.js
4116
+ ;// ./lib/models/transaction/transaction.js
4263
4117
 
4264
4118
 
4265
4119
 
4266
4120
 
4267
- const category_updateAllowedProps = [
4121
+
4122
+
4123
+ const transaction_updateAllowedProps = [
4268
4124
  'active',
4269
- 'codes',
4125
+ 'amount',
4126
+ 'couponCodes',
4270
4127
  'deleted',
4271
- 'description',
4272
- 'max',
4273
- 'min',
4274
- 'name',
4275
- 'owner',
4276
- 'priority',
4277
- 'productCodes',
4128
+ // 'refundedAmount',
4278
4129
  'remarks',
4130
+ 'status'
4279
4131
  ]
4280
4132
 
4281
- class Category {
4282
- constructor(options) {
4133
+ class Transaction {
4134
+ constructor(options = {}) {
4283
4135
  options = options || {}
4284
- const { _Product } = options._constructor || {}
4285
- this._Product = _Product && (_Product._superclass === Product._superclass) ? _Product : Product
4136
+ const { _Amount, _Status, _WalletItem } = options._constructor || {}
4137
+ this._Amount = _Amount && (_Amount._superclass === Amount._superclass) ? _Amount : Amount
4138
+ this._Status = _Status && (_Status._superclass === Status._superclass) ? _Status : Status
4139
+ this._WalletItem = _WalletItem && (_WalletItem._superclass === WalletItem._superclass) ? _WalletItem : WalletItem
4286
4140
 
4287
- this._products = options._products
4288
- this._type = options._type || 'Category'
4141
+ this._coupons = options._coupons
4142
+ this._creditNotes = options._creditNotes
4143
+ this._invoice = options._invoice
4144
+ this._paymentGateway = options._paymentGateway
4145
+ this._paymentResults = options._paymentResults
4146
+ this._walletItems = options._walletItems
4289
4147
 
4290
4148
  const id = options._id || options.id
4291
- this.id = category_setId(id)
4149
+ this.id = transaction_setId(id)
4150
+ this._type = options._type || 'Transaction'
4292
4151
  this.active = (typeof options.active !== 'undefined') ? !!options.active : true
4293
- this.categoryCode = category_setCode(options, 'categoryCode')
4294
- this.codes = options.codes || []
4152
+ this.amount = this._Amount.init(options.amount)
4153
+ this.couponCodes = options.couponCodes || []
4295
4154
  this.created = options.created || (new Date()).valueOf()
4296
4155
  this.creator = options.creator
4156
+ this.creditNoteCodes = options.creditNoteCodes || []
4297
4157
  this.deleted = options.deleted || false
4298
- this.description = options.description
4299
- this.max = options.max || 1
4300
- this.min = options.min || 0
4158
+ this.invoiceCode = options.invoiceCode
4159
+ this.metadata = q_utilities_namespaceObject.Metadata.initOnlyValidFromArray(options.metadata)
4301
4160
  this.modified = options.modified || (new Date()).valueOf()
4302
- this.name = options.name
4303
4161
  this.owner = options.owner
4304
- this.priority = options.priority || 10
4305
- this.productCodes = options.productCodes || []
4162
+ this.paymentGatewayCode = options.paymentGatewayCode
4163
+ this.paymentResultRef = options.paymentResultRef
4306
4164
  this.remarks = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(options.remarks)
4165
+ this.status = this._Status.init(options.status)
4307
4166
  this.tenantCode = options.tenantCode
4167
+ this.transactionCode = transaction_setCode(options, 'transactionCode')
4308
4168
  }
4309
4169
 
4310
- // class methods
4170
+ // class method
4311
4171
  static dummyData() {
4312
4172
  return {
4313
- name: 'category',
4173
+ status: Status.dummyData(),
4314
4174
  tenantCode: 'tenantCode'
4315
4175
  }
4316
4176
  }
@@ -4331,31 +4191,220 @@ class Category {
4331
4191
  return this.initFromArray(arr).filter((i) => i)
4332
4192
  }
4333
4193
  static get _classname() {
4334
- return 'Category'
4194
+ return 'Transaction'
4335
4195
  }
4336
4196
  static get _superclass() {
4337
- return 'Category'
4197
+ return 'Transaction'
4338
4198
  }
4339
4199
 
4340
4200
  // getters
4341
4201
  get isValid() {
4342
- return !!this.name && !!this.tenantCode && this.max > this. min
4202
+ return !!this.tenantCode
4343
4203
  }
4344
- get products() {
4345
- return this._Product.initOnlyValidFromArray(this._products)
4204
+ get isActive() {
4205
+ return this.active === true
4206
+ }
4207
+ get isAssigned() {
4208
+ return this.status.isAssigned
4209
+ }
4210
+ get isCancelled() {
4211
+ return this.status.isCancelled
4212
+ }
4213
+ get isConfirmed() {
4214
+ return this.status.isConfirmed
4215
+ }
4216
+ get isCompleted() {
4217
+ return this.status.isCompleted
4218
+ }
4219
+ get isExpired() {
4220
+ return this.status.isExpired
4221
+ }
4222
+ get isFailed() {
4223
+ return this.status.isFailed
4224
+ }
4225
+ get isOnHold() {
4226
+ return this.status.isOnHold
4227
+ }
4228
+ get isPaid() {
4229
+ return this.status.isPaid
4230
+ }
4231
+ get isPending() {
4232
+ return this.status.isPending
4233
+ }
4234
+ get isProcessing() {
4235
+ return this.status.isProcessing
4236
+ }
4237
+ get isRedeemed() {
4238
+ return this.status.isRedeemed
4239
+ }
4240
+ get isRefunded() {
4241
+ return this.status.isRefunded
4242
+ }
4243
+ get isShared() {
4244
+ return this.status.isShared
4245
+ }
4246
+ // get isSettled() {
4247
+ // return this.payments.reduce((acc, payment) => {
4248
+ // return acc || payment.isSettled
4249
+ // }, false)
4250
+ // }
4251
+ get isSubmitted() {
4252
+ return this.status.isSubmitted
4253
+ }
4254
+ get isWaived() {
4255
+ return this.status.isWaived
4256
+ }
4257
+ get walletItems() {
4258
+ return this._WalletItem.initOnlyValidFromArray(this._walletItems)
4259
+ }
4260
+ // get totalAmount() {
4261
+ // return this.total.getAmount()
4262
+ // }
4263
+ // instance methods
4264
+ // appendPayment({ paymentGateway }) {
4265
+ // this.payments = this.payments.map((payment) => {
4266
+ // payment.unsetAll()
4267
+ // payment.setCancelled()
4268
+ // return payment
4269
+ // })
4270
+ // // const totalAmount = Service.getTotalAmount(this.getServices())
4271
+ // const payment = Payment.init({
4272
+ // amount: this.total.getAmount(),
4273
+ // currency: this.total.getCurrency(),
4274
+ // paymentGatewayCode: paymentGateway.paymentGatewayCode,
4275
+ // paymentStatus: {
4276
+ // pending: true
4277
+ // }
4278
+ // })
4279
+ // this.payments.push(payment)
4280
+ // return this
4281
+ // }
4282
+ // getCurrency() {
4283
+ // return this.total.getCurrency()
4284
+ // }
4285
+ getId() {
4286
+ return this.id ? this.id.toString() : this.id
4287
+ }
4288
+ getLatestStatus() {
4289
+ return this.status?.statusLast?.key || ''
4290
+ // const status = {
4291
+ // ...this.status,
4292
+ // created: null,
4293
+ // modified: null
4294
+ // }
4295
+ // return Object.keys(status).reduce((a, b) => status[a] > status[b] ? a : b)
4296
+ }
4297
+ getCode() {
4298
+ return this.transactionCode
4299
+ }
4300
+ // getPaymentByCode(paymentCode) {
4301
+ // const payments = this.payments || []
4302
+ // const filteredPayment = payments.filter((payment) => {
4303
+ // return payment.isSame(paymentCode)
4304
+ // })
4305
+ // if (filteredPayment.length === 1) {
4306
+ // return filteredPayment[0]
4307
+ // }
4308
+ // return null
4309
+ // }
4310
+ // getPendingPayment() {
4311
+ // const payments = this.payments.filter((payment) => {
4312
+ // return payment.canPay()
4313
+ // })
4314
+ // if (payments.length === 1) {
4315
+ // return payments[0]
4316
+ // }
4317
+ // if (payments.length === 0) {
4318
+ // throw Error('pending payment not found')
4319
+ // }
4320
+ // throw Error('duplicate pendingPayment found')
4321
+ // }
4322
+ isAllowDelete() {
4323
+ return this.active && !this.deleted && !this.isCancelled && !this.isCompleted && !this.isPaid && !this.isWaived // && !this.isSettled
4324
+ }
4325
+ isAllowPay() {
4326
+ return this.active && !this.deleted && !this.isCancelled && !this.isCompleted && !this.isPaid && !this.isWaived && !this.isExpired // && !this.isSettled
4327
+ }
4328
+ setAssigned() {
4329
+ this.status.setAssigned()
4330
+ return this
4331
+ }
4332
+ setCancelled() {
4333
+ return this.status.setCancelled()
4334
+ }
4335
+ setCompleted() {
4336
+ return this.status.setCompleted()
4337
+ }
4338
+ setFailed() {
4339
+ return this.status.setFailed()
4340
+ }
4341
+ setModified() {
4342
+ return this.modified = (new Date()).valueOf()
4343
+ }
4344
+ setOnHold() {
4345
+ return this.status.setOnHold()
4346
+ }
4347
+ setPaid() {
4348
+ return this.status.setPaid()
4349
+ }
4350
+ setPaymentResultRef(paymentResultRef) {
4351
+ this.paymentResultRef = paymentResultRef
4352
+ return this
4353
+ }
4354
+ setPending() {
4355
+ return this.status.setPending()
4356
+ }
4357
+ setProcessing() {
4358
+ return this.status.setProcessing()
4359
+ }
4360
+ setRedeemed() {
4361
+ return this.status.setRedeemed()
4362
+ }
4363
+ setRefunded() {
4364
+ return this.status.setRefunded()
4365
+ }
4366
+ setSettled() {
4367
+ return this.status.setSettled()
4368
+ }
4369
+ setShared() {
4370
+ return this.status.setShared()
4371
+ }
4372
+ setWaived() {
4373
+ return this.status.setWaived()
4346
4374
  }
4347
-
4348
- // instance methods
4349
- setModified() {
4350
- this.modified = (new Date()).valueOf()
4351
- return this
4375
+ updateStatus(status) {
4376
+ switch (status) {
4377
+ case 'cancelled':
4378
+ return this.setCancelled()
4379
+ case 'completed':
4380
+ return this.setCompleted()
4381
+ case 'failed':
4382
+ return this.setFailed()
4383
+ case 'on-hold':
4384
+ return this.setOnHold()
4385
+ case 'pending':
4386
+ return this.setPending()
4387
+ case 'processing':
4388
+ return this.setProcessing()
4389
+ case 'refunded':
4390
+ return this.setRefunded()
4391
+ default:
4392
+ return this
4393
+ }
4352
4394
  }
4353
-
4354
4395
  update(update) {
4355
4396
  Object.keys(update).forEach((key) => {
4356
- if (category_updateAllowedProps.includes(key)) {
4357
- if (key === 'remarks') {
4397
+ if (transaction_updateAllowedProps.includes(key)) {
4398
+ if (key === 'amount') {
4399
+ this[key] = this._Amount.init(update[key])
4400
+ } else if (key === 'metadata') {
4401
+ this[key] = q_utilities_namespaceObject.Metadata.initOnlyValidFromArray(update[key])
4402
+ } else if (key === 'remarks') {
4358
4403
  this[key] = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(update[key])
4404
+ } else if (key === 'status') {
4405
+ this[key] = this._Status.init(update[key])
4406
+ // } else if (key === 'refundedAmount') {
4407
+ // this[key] = this.updateTotal(key, update[key])
4359
4408
  } else {
4360
4409
  this[key] = update[key]
4361
4410
  }
@@ -4363,9 +4412,19 @@ class Category {
4363
4412
  })
4364
4413
  return this.setModified()
4365
4414
  }
4415
+ // updatePayment(payment = {}) {
4416
+ // this.payments = Payment.updatePaymentItem(this.payments, payment)
4417
+ // return this
4418
+ // }
4419
+ // updateTotal(key, obj) {
4420
+ // if (obj instanceof Total) {
4421
+ // return this[key].updateTotal(obj)
4422
+ // }
4423
+ // return setTotal(obj, key)
4424
+ // }
4366
4425
  }
4367
4426
 
4368
- function category_setCode(options, key) {
4427
+ function transaction_setCode(options, key) {
4369
4428
  const copyOptions = options || {}
4370
4429
  if (copyOptions[key]) {
4371
4430
  return copyOptions[key]
@@ -4373,95 +4432,112 @@ function category_setCode(options, key) {
4373
4432
  return stringHelper.setCode()
4374
4433
  }
4375
4434
 
4376
- function category_setId(id) {
4435
+ function transaction_setId(id) {
4377
4436
  if (id && typeof id.toString === 'function') {
4378
4437
  return id.toString()
4379
4438
  }
4380
4439
  return id
4381
4440
  }
4382
4441
 
4442
+ // function setTotal(obj, label) {
4443
+ // obj = obj || {}
4444
+ // return Total.init({ ...obj, label: obj.label || label })
4445
+ // }
4446
+
4383
4447
 
4384
4448
 
4385
- ;// ./lib/models/category/categoryRepo.js
4449
+ ;// ./lib/models/transaction/transactionRepo.js
4386
4450
 
4387
4451
 
4388
4452
 
4389
- class CategoryRepo extends q_utilities_namespaceObject.Repo {
4453
+ class TransactionRepo extends q_utilities_namespaceObject.Repo {
4390
4454
  constructor(options = {}) {
4391
4455
  options = options || {}
4392
4456
  super(options)
4393
- const { _Category } = options._constructor || {}
4394
- this._Category = _Category && (_Category._superclass === Category._superclass)
4395
- ? _Category
4396
- : Category
4457
+ const { _Transaction } = options._constructor || {}
4458
+ this._Transaction = _Transaction && (_Transaction._superclass === Transaction._superclass) ? _Transaction : Transaction
4397
4459
  }
4398
4460
  static get _classname() {
4399
- return 'CategoryRepo'
4461
+ return 'TransactionRepo'
4400
4462
  }
4401
4463
  init(options) {
4402
- return this._Category.init(options)
4464
+ return this._Transaction.init(options)
4403
4465
  }
4404
4466
  }
4405
4467
 
4406
4468
 
4407
4469
 
4408
- ;// ./lib/models/category/index.js
4470
+ ;// ./lib/models/transaction/index.js
4409
4471
 
4410
4472
 
4411
4473
 
4412
4474
 
4413
4475
 
4414
4476
 
4415
- ;// ./lib/models/creditNoteLine/creditNoteLine.js
4477
+ ;// ./lib/models/invoice/invoice.js
4416
4478
 
4417
4479
 
4418
4480
 
4419
4481
 
4420
- const creditNoteLine_updateAllowedProps = [
4482
+
4483
+
4484
+
4485
+
4486
+
4487
+ const invoice_updateAllowedProps = [
4421
4488
  'active',
4422
- 'amount',
4423
- // 'deduction',
4489
+ 'checkoutDateTime',
4424
4490
  'deleted',
4425
4491
  'description',
4426
- // 'discount',
4427
- 'qty',
4492
+ 'invoiceDate',
4493
+ 'issuer',
4494
+ 'metadata',
4428
4495
  'remarks',
4429
- // 'unitPrice'
4496
+ 'revisionNumber',
4497
+ 'status'
4430
4498
  ]
4431
4499
 
4432
- class CreditNoteLine {
4433
- constructor(options = {}) {
4500
+ class Invoice {
4501
+ constructor(options) {
4434
4502
  options = options || {}
4435
- const { _Amount } = options._constructor || {}
4503
+ const { _Amount, _Cart, _InvoiceLine, _Issuer, _Status, _Transaction } = options._constructor || {}
4436
4504
  this._Amount = _Amount && (_Amount._superclass === Amount._superclass) ? _Amount : Amount
4505
+ this._Cart = _Cart && (_Cart._superclass === Cart._superclass) ? _Cart : Cart
4506
+ this._InvoiceLine = _InvoiceLine && (_InvoiceLine._superclass === InvoiceLine._superclass) ? _InvoiceLine : InvoiceLine
4507
+ this._Issuer = _Issuer && (_Issuer._superclass === Issuer._superclass) ? _Issuer : Issuer
4508
+ this._Status = _Status && (_Status._superclass === Status._superclass) ? _Status : Status
4509
+ this._Transaction = _Transaction && (_Transaction._superclass === Transaction._superclass) ? _Transaction : Transaction
4437
4510
 
4438
- this._creditNote = options._creditNote
4439
- this._type = options._type || 'CreditNoteLine'
4440
-
4511
+ this._cart = options._cart
4512
+ this._invoiceLines = options._invoiceLines
4513
+ this._transactions = options._transactions
4514
+
4441
4515
  const id = options._id || options.id
4442
- this.id = creditNoteLine_setId(id)
4516
+ this.id = invoice_setId(id)
4517
+ this._type = options._type || 'Invoice'
4443
4518
  this.active = (typeof options.active !== 'undefined') ? !!options.active : true
4444
4519
  this.amount = this._Amount.init(options.amount)
4520
+ this.cartCode = options.cartCode
4521
+ this.checkoutDateTime = options.checkoutDateTime || (new Date()).valueOf()
4445
4522
  this.created = options.created || (new Date()).valueOf()
4446
4523
  this.creator = options.creator
4447
- this.creditNoteCode = options.creditNoteCode
4448
- this.creditNoteLineCode = creditNoteLine_setCode(options, 'creditNoteLineCode')
4449
- // this.deduction = this._Amount.init(options.deduction)
4450
4524
  this.deleted = options.deleted || false
4451
4525
  this.description = options.description
4452
- // this.discount = options.discount || 0
4526
+ this.invoiceCode = invoice_setCode(options, 'invoiceCode')
4527
+ this.invoiceDate = options.invoiceDate || (new Date()).valueOf()
4528
+ this.invoiceNumber = options.invoiceNumber
4529
+ this.issuer = this._Issuer.init(options.issuer)
4530
+ this.metadata = q_utilities_namespaceObject.Metadata.initOnlyValidFromArray(options.metadata)
4453
4531
  this.modified = options.modified || (new Date()).valueOf()
4454
4532
  this.owner = options.owner
4455
- this.qty = options.qty || 1
4456
4533
  this.remarks = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(options.remarks)
4534
+ this.revisionNumber = options.revisionNumber || 1
4535
+ this.status = this._Status.init(options.status)
4457
4536
  this.tenantCode = options.tenantCode
4458
- // this.unitPrice = Amount.init(options.unitPrice)
4459
4537
  }
4460
4538
 
4461
- // Class Methods
4462
4539
  static dummyData() {
4463
4540
  return {
4464
- creditNoteCode: 'creditNoteCode',
4465
4541
  tenantCode: 'tenantCode'
4466
4542
  }
4467
4543
  }
@@ -4482,209 +4558,137 @@ class CreditNoteLine {
4482
4558
  return this.initFromArray(arr).filter((i) => i)
4483
4559
  }
4484
4560
  static get _classname() {
4485
- return 'CreditNoteLine'
4561
+ return 'Invoice'
4486
4562
  }
4487
4563
  static get _superclass() {
4488
- return 'CreditNoteLine'
4564
+ return 'Invoice'
4489
4565
  }
4490
4566
 
4491
-
4492
- // getters
4493
4567
  get isValid() {
4494
- return !!this.creditNoteCode && !!this.tenantCode
4495
- }
4496
-
4497
- // instance methods
4498
- setAmount(amount) {
4499
- const a = this._Amount.init(amount)
4500
- if (a) {
4501
- this.amount = a
4502
- }
4503
- return this
4504
- }
4505
- setModified() {
4506
- this.modified = (new Date()).valueOf()
4507
- return this
4508
- }
4509
-
4510
- update(update) {
4511
- Object.keys(update).forEach((key) => {
4512
- if (creditNoteLine_updateAllowedProps.includes(key)) {
4513
- if (key === 'amount' || key === 'unitPrice' || key === 'deduction') {
4514
- this[key] = this._Amount.init(update[key])
4515
- } else if (key === 'purchaseOptions') {
4516
- this[key] = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(update[key])
4517
- } else {
4518
- this[key] = update[key]
4519
- }
4520
- }
4521
- })
4522
- return this.setModified()
4523
- }
4524
- }
4525
-
4526
- function creditNoteLine_setCode(options, key) {
4527
- const copyOptions = options || {}
4528
- if (copyOptions[key]) {
4529
- return copyOptions[key]
4530
- }
4531
- return stringHelper.setCode()
4532
- }
4533
-
4534
- function creditNoteLine_setId(id) {
4535
- if (id && typeof id.toString === 'function') {
4536
- return id.toString()
4568
+ return !!this.tenantCode
4537
4569
  }
4538
- return id
4539
- }
4540
-
4541
-
4542
-
4543
- ;// ./lib/models/creditNoteLine/creditNoteLineRepo.js
4544
-
4545
-
4546
-
4547
- class CreditNoteLineRepo extends q_utilities_namespaceObject.Repo {
4548
- constructor(options = {}) {
4549
- options = options || {}
4550
- super(options)
4551
- const { _CreditNoteLine } = options._constructor || {}
4552
- this._CreditNoteLine = _CreditNoteLine && (_CreditNoteLine._superclass === CreditNoteLine._superclass)
4553
- ? _CreditNoteLine
4554
- : CreditNoteLine
4570
+ get cart() {
4571
+ return this._Cart.init(this._cart)
4555
4572
  }
4556
- static get _classname() {
4557
- return 'CreditNoteLineRepo'
4573
+ get invoiceLines() {
4574
+ return this._InvoiceLine.initOnlyValidFromArray(this._invoiceLines || [])
4558
4575
  }
4559
- init(options) {
4560
- return this._CreditNoteLine.init(options)
4576
+ get merchandises() {
4577
+ return (this.invoiceLines || []).map((i) => {
4578
+ return i.merchandise
4579
+ }).filter((i) => i)
4561
4580
  }
4562
- }
4563
-
4564
-
4565
-
4566
- ;// ./lib/models/creditNoteLine/index.js
4567
-
4568
-
4569
-
4570
-
4571
-
4572
- ;// ./lib/models/creditNote/creditNote.js
4573
-
4574
-
4575
-
4576
-
4577
-
4578
-
4579
-
4580
- const creditNote_updateAllowedProps = [
4581
- 'active',
4582
- 'deleted',
4583
- 'description',
4584
- 'remarks',
4585
- 'status'
4586
- ]
4587
-
4588
- class CreditNote {
4589
- constructor(options = {}) {
4590
- options = options || {}
4591
- const { _Amount, _CreditNoteLine } = options._constructor || {}
4592
- this._Amount = _Amount && (_Amount._superclass === Amount._superclass) ? _Amount : Amount
4593
- this._CreditNoteLine = _CreditNoteLine && (_CreditNoteLine._superclass === CreditNoteLine._superclass) ? _CreditNoteLine : CreditNoteLine
4594
- this._Status = _Status && (_Status._superclass === Status._superclass) ? _Status : Status
4595
-
4596
- this._type = options._type || 'CreditNote'
4597
-
4598
- const id = options._id || options.id
4599
- this.id = creditNote_setId(id)
4600
- this.active = (typeof options.active !== 'undefined') ? !!options.active : true
4601
- this.amount = this._Amount.init(options.amount)
4602
- this.created = options.created || (new Date()).valueOf()
4603
- this.creator = options.creator
4604
- this.creditNoteCode = creditNote_setCode(options, 'creditNoteCode')
4605
- this.creditNoteLines = this._CreditNoteLine.initOnlyValidFromArray(options.creditNoteLines)
4606
- this.deleted = options.deleted || false
4607
- this.description = options.description
4608
- this.invoiceCode = options.invoiceCode
4609
- this.metadata = q_utilities_namespaceObject.Metadata.initOnlyValidFromArray(options.metadata)
4610
- this.modified = options.modified || (new Date()).valueOf()
4611
- this.owner = options.owner
4612
- this.remarks = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(options.remarks)
4613
- this.status = this._Status.init(options.status)
4614
- this.tenantCode = options.tenantCode
4581
+ get merchandisesSummary() {
4582
+ return this.merchandises.reduce((acc, m) => {
4583
+ const found = acc.find((a) => {
4584
+ return a.merchandiseCode === m.merchandiseCode
4585
+ })
4586
+ if (found) {
4587
+ found.qty += 1
4588
+ } else {
4589
+ acc.push({
4590
+ ...m.summary,
4591
+ qty: 1
4592
+ })
4593
+ }
4594
+ return acc
4595
+ }, [])
4615
4596
  }
4616
-
4617
- // Class Methods
4618
- static dummyData() {
4619
- return {
4620
- invoiceCode: 'invoiceCode',
4621
- tenantCode: 'tenantCode'
4622
- }
4597
+ get products() {
4598
+ return (this.merchandises || []).reduce((acc, m) => {
4599
+ if (m.products) {
4600
+ acc = acc.concat(m.products)
4601
+ }
4602
+ return acc
4603
+ }, [])
4623
4604
  }
4624
- static init(options = {}) {
4625
- if (options instanceof this) {
4626
- return options
4627
- }
4628
- const instance = new this(options)
4629
- return instance.isValid ? instance : null
4605
+ get productsSummary() {
4606
+ return this.products.reduce((acc, p) => {
4607
+ const found = acc.find((a) => {
4608
+ return a.productCode === p.productCode
4609
+ })
4610
+ if (found) {
4611
+ found.qty += 1
4612
+ } else {
4613
+ acc.push({
4614
+ ...p.summary,
4615
+ qty: 1
4616
+ })
4617
+ }
4618
+ return acc
4619
+ }, [])
4630
4620
  }
4631
- static initFromArray(arr = []) {
4632
- if (Array.isArray(arr)) {
4633
- return arr.map((a) => this.init(a))
4634
- }
4635
- return []
4621
+ get transactions() {
4622
+ return this._Transaction.initOnlyValidFromArray(this._transactions)
4636
4623
  }
4637
- static initOnlyValidFromArray(arr = []) {
4638
- return this.initFromArray(arr).filter((i) => i)
4624
+ get walletItems() {
4625
+ return (this.transactions || []).reduce((acc, t) => {
4626
+ if (t.walletItems) {
4627
+ acc = acc.concat(t.walletItems)
4628
+ }
4629
+ return acc
4630
+ }, [])
4639
4631
  }
4640
- static get _classname() {
4641
- return 'CreditNote'
4632
+
4633
+ allowCancel() {
4634
+ return this.status.allowCancel()
4642
4635
  }
4643
- static get _superclass() {
4644
- return 'CreditNote'
4636
+
4637
+ // instance methods
4638
+ getAmount() {
4639
+ return this.amount
4645
4640
  }
4646
4641
 
4647
- // getters
4648
- get isActive() {
4649
- return !!this.active
4642
+ getCurrencyCode() {
4643
+ return this.amount ? this.amount.getCurrencyCode() : null
4650
4644
  }
4651
4645
 
4652
- get isPending() {
4653
- return this.status.isPending
4646
+ getFullInvoiceNumber(delimiter = '.') {
4647
+ return `${this.invoiceNumber}${delimiter}${this.revisionNumber}`
4654
4648
  }
4655
4649
 
4656
- get isValid() {
4657
- return !!this.tenantCode && !!this.invoiceCode
4650
+ increaseRevisionNumber() {
4651
+ this.revisionNumber += 1
4652
+ return this
4658
4653
  }
4659
4654
 
4660
- setAmount(amount) {
4661
- const a = this_Amount.init(amount)
4662
- if (this.isPending && a) {
4663
- this.amount = a
4655
+ setCancelled() {
4656
+ return this.status.setCancelled()
4657
+ }
4658
+
4659
+ setCheckoutDateTime(timestamp) {
4660
+ if (typeof timestamp === 'number') {
4661
+ this.checkoutDateTime = timestamp
4664
4662
  }
4665
4663
  return this
4666
4664
  }
4667
- setCompleted() {
4668
- this.status.setCompleted()
4669
- return this.setModified()
4670
- }
4665
+
4671
4666
  setModified() {
4672
4667
  this.modified = (new Date()).valueOf()
4673
4668
  return this
4674
4669
  }
4675
- setPaid(t) {
4676
- this.status.setPaid(t)
4670
+
4671
+ setCompleted() {
4672
+ this.status.setCompleted()
4677
4673
  return this.setModified()
4678
4674
  }
4675
+
4676
+ setWaived() {
4677
+ this.status.setWaived()
4678
+ return this.setModified()
4679
+ }
4680
+
4679
4681
  update(update) {
4680
4682
  Object.keys(update).forEach((key) => {
4681
- if (creditNote_updateAllowedProps.includes(key)) {
4682
- if (key === 'amount') {
4683
+ if (invoice_updateAllowedProps.includes(key)) {
4684
+ if (key === 'remarks') {
4685
+ this[key] = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(update[key])
4686
+ } else if (key === 'amount') {
4683
4687
  this[key] = this._Amount.init(update[key])
4684
4688
  } else if (key === 'metadata') {
4685
4689
  this[key] = q_utilities_namespaceObject.Metadata.initOnlyValidFromArray(update[key])
4686
- } else if (key === 'remarks') {
4687
- this[key] = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(update[key])
4690
+ } else if (key === 'issuer') {
4691
+ this[key] = this._Issuer.init(update[key])
4688
4692
  } else if (key === 'status') {
4689
4693
  this[key] = this._Status.init(update[key])
4690
4694
  } else {
@@ -4696,7 +4700,7 @@ class CreditNote {
4696
4700
  }
4697
4701
  }
4698
4702
 
4699
- function creditNote_setCode(options, key) {
4703
+ function invoice_setCode(options, key) {
4700
4704
  const copyOptions = options || {}
4701
4705
  if (copyOptions[key]) {
4702
4706
  return copyOptions[key]
@@ -4704,7 +4708,7 @@ function creditNote_setCode(options, key) {
4704
4708
  return stringHelper.setCode()
4705
4709
  }
4706
4710
 
4707
- function creditNote_setId(id) {
4711
+ function invoice_setId(id) {
4708
4712
  if (id && typeof id.toString === 'function') {
4709
4713
  return id.toString()
4710
4714
  }
@@ -4713,77 +4717,79 @@ function creditNote_setId(id) {
4713
4717
 
4714
4718
 
4715
4719
 
4716
- ;// ./lib/models/creditNote/creditNoteRepo.js
4720
+ ;// ./lib/models/invoice/invoiceRepo.js
4717
4721
 
4718
4722
 
4719
4723
 
4720
- class CreditNoteRepo extends q_utilities_namespaceObject.Repo {
4724
+ class InvoiceRepo extends q_utilities_namespaceObject.Repo {
4721
4725
  constructor(options = {}) {
4722
4726
  options = options || {}
4723
4727
  super(options)
4724
- const { _CreditNote } = options._constructor || {}
4725
- this._CreditNote = _CreditNote && (_CreditNote._superclass === CreditNote._superclass) ? _CreditNote : CreditNote
4728
+ const { _Invoice } = options._constructor || {}
4729
+ this._Invoice = _Invoice && (_Invoice._superclass === Invoice._superclass) ? _Invoice : Invoice
4726
4730
  }
4727
4731
  static get _classname() {
4728
- return 'CreditNoteRepo'
4732
+ return 'InvoiceRepo'
4729
4733
  }
4730
4734
  init(options) {
4731
- return this._CreditNote.init(options)
4735
+ return this._Invoice.init(options)
4732
4736
  }
4733
4737
  }
4734
4738
 
4735
4739
 
4736
4740
 
4737
- ;// ./lib/models/creditNote/index.js
4741
+ ;// ./lib/models/invoice/index.js
4738
4742
 
4739
4743
 
4740
4744
 
4741
4745
 
4742
4746
 
4743
- ;// ./lib/models/currency/currency.js
4747
+
4748
+ ;// ./lib/models/keyValueObject/index.js
4744
4749
 
4745
4750
 
4751
+ ;// ./lib/models/paymentGateway/paymentGateway.js
4746
4752
 
4747
4753
 
4748
- const currency_updateAllowedProps = [
4754
+ const paymentGateway_updateAllowedProps = [
4749
4755
  'active',
4750
4756
  'deleted',
4751
- 'description',
4757
+ 'label',
4758
+ 'logoUrl',
4752
4759
  'name',
4753
- 'remarks',
4754
- 'symbol'
4760
+ 'owner',
4761
+ 'sandbox',
4762
+ 'setting'
4755
4763
  ]
4756
4764
 
4757
- class Currency {
4765
+
4766
+ class PaymentGateway {
4758
4767
  constructor(options = {}) {
4759
4768
  options = options || {}
4760
4769
  const id = options._id || options.id
4761
- this.id = currency_setId(id)
4762
-
4763
- this._type = options._type || 'Currency'
4770
+ this.id = paymentGateway_setId(id)
4771
+ this._type = options._type || 'PaymentGateway'
4764
4772
  this.active = (typeof options.active !== 'undefined') ? !!options.active : true
4765
- this.code = options.code // 'HKD'
4766
4773
  this.created = options.created || (new Date()).valueOf()
4767
4774
  this.creator = options.creator
4768
- this.currencyCode = currency_setCode(options, 'currencyCode')
4769
4775
  this.deleted = options.deleted || false
4770
- this.description = options.description
4771
- this.factor = options.factor || 1 // 100
4776
+ this.hasWebhook = options.hasWebhook || false
4777
+ this.label = options.label
4778
+ this.logoUrl = options.logoUrl
4772
4779
  this.modified = options.modified || (new Date()).valueOf()
4773
- this.name = options.name
4780
+ this.name = options.name || ''
4774
4781
  this.owner = options.owner
4775
- this.remarks = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(options.remarks)
4776
- this.symbol = options.symbol || null
4782
+ this.paymentGatewayCode = paymentGateway_setCode(options, 'paymentGatewayCode')
4783
+ this.paymentGatewayType = options.paymentGatewayType || 'PaymentGateway'
4784
+ this.paymentResultType = options.paymentResultType || 'PaymentResult'
4785
+ this.sandbox = options.sandbox || false
4786
+ this.setting = options.setting || null
4777
4787
  this.tenantCode = options.tenantCode
4778
- this.unit = options.unit
4779
4788
  }
4780
4789
  static dummyData() {
4781
4790
  return {
4782
- code: 'USD',
4783
- factor: 100,
4784
- name: 'US dollar',
4785
- tenantCode: 'platform',
4786
- unit: 'Cent',
4791
+ name: 'name',
4792
+ tenantCode: 'tenantCode'
4787
4793
  }
4788
4794
  }
4789
4795
  static init(options = {}) {
@@ -4803,40 +4809,53 @@ class Currency {
4803
4809
  return this.initFromArray(arr).filter((i) => i)
4804
4810
  }
4805
4811
  static get _classname() {
4806
- return 'Currency'
4812
+ return 'PaymentGateway'
4807
4813
  }
4808
4814
  static get _superclass() {
4809
- return 'Currency'
4815
+ return 'PaymentGateway'
4810
4816
  }
4811
4817
 
4818
+ // getters
4812
4819
  get isValid() {
4813
- return !!this.code && typeof this.factor === 'number' && !!this.name && !!this.unit && !!this.tenantCode
4820
+ return !!this.name && !!this.tenantCode
4814
4821
  }
4815
4822
 
4816
- fromCurrencyValue(value) {
4817
- return value / this.factor
4823
+ // instance methods
4824
+ async createPayment() {
4825
+ throw new Error(`${this._classname} subclass should implement createPayment`)
4826
+ }
4827
+ async getAppPayParams() {
4828
+ throw new Error(`${this._classname} subclass should implement getAppPayParams`)
4829
+ }
4830
+ async updatePayment() {
4831
+ throw new Error(`${this._classname} subclass should implement updatePayment`)
4832
+ }
4833
+ async query() {
4834
+ throw new Error(`${this._classname} subclass should implement query`)
4835
+ }
4836
+ async submit() {
4837
+ throw new Error(`${this._classname} subclass should implement submit`)
4838
+ }
4839
+
4840
+ setCompletedRelatedStatus(status) {
4841
+ throw new Error(`${this.name} subclass should implement setCompletedRelatedStatus`)
4818
4842
  }
4819
4843
 
4820
4844
  setModified() {
4821
4845
  this.modified = (new Date()).valueOf()
4822
4846
  return this
4823
4847
  }
4824
-
4825
4848
  update(update) {
4826
4849
  Object.keys(update).forEach((key) => {
4827
- if (currency_updateAllowedProps.includes(key)) {
4828
- if (key === 'remarks') {
4829
- this[key] = q_utilities_namespaceObject.KeyValueObject.initOnlyValidFromArray(update[key])
4830
- } else {
4831
- this[key] = update[key]
4832
- }
4850
+ if (paymentGateway_updateAllowedProps.includes(key)) {
4851
+ this[key] = update[key]
4833
4852
  }
4834
4853
  })
4835
4854
  return this.setModified()
4836
4855
  }
4837
4856
  }
4838
4857
 
4839
- function currency_setCode(options, key) {
4858
+ function paymentGateway_setCode(options, key) {
4840
4859
  const copyOptions = options || {}
4841
4860
  if (copyOptions[key]) {
4842
4861
  return copyOptions[key]
@@ -4844,7 +4863,7 @@ function currency_setCode(options, key) {
4844
4863
  return stringHelper.setCode()
4845
4864
  }
4846
4865
 
4847
- function currency_setId(id) {
4866
+ function paymentGateway_setId(id) {
4848
4867
  if (id && typeof id.toString === 'function') {
4849
4868
  return id.toString()
4850
4869
  }
@@ -4853,37 +4872,32 @@ function currency_setId(id) {
4853
4872
 
4854
4873
 
4855
4874
 
4856
- ;// ./lib/models/currency/currencyRepo.js
4875
+ ;// ./lib/models/paymentGateway/paymentGatewayRepo.js
4857
4876
 
4858
4877
 
4859
4878
 
4860
- class CurrencyRepo extends q_utilities_namespaceObject.Repo {
4879
+ class PaymentGatewayRepo extends q_utilities_namespaceObject.Repo {
4861
4880
  constructor(options = {}) {
4862
4881
  options = options || {}
4863
4882
  super(options)
4864
- const { _Currency } = options._constructor || {}
4865
- this._Currency = _Currency && (_Currency._superclass === Currency._superclass)
4866
- ? _Currency
4867
- : Currency
4883
+ const { _PaymentGateway } = options._constructor || {}
4884
+ this._PaymentGateway = _PaymentGateway && (_PaymentGateway._superclass === PaymentGateway._superclass) ? _PaymentGateway : PaymentGateway
4868
4885
  }
4869
4886
  static get _classname() {
4870
- return 'CurrencyRepo'
4887
+ return 'PaymentGatewayRepo'
4871
4888
  }
4872
4889
  init(options) {
4873
- return this._Currency.init(options)
4890
+ return this._PaymentGateway.init(options)
4874
4891
  }
4875
4892
  }
4876
4893
 
4877
4894
 
4878
4895
 
4879
- ;// ./lib/models/currency/index.js
4880
-
4881
-
4896
+ ;// ./lib/models/paymentGateway/index.js
4882
4897
 
4883
4898
 
4884
4899
 
4885
4900
 
4886
- ;// ./lib/models/keyValueObject/index.js
4887
4901
 
4888
4902
 
4889
4903
  ;// ./lib/models/paymentResult/paymentResult.js
@@ -4933,7 +4947,8 @@ class PaymentResult {
4933
4947
  paymentResultType: 'PaymentResult',
4934
4948
  result: { id: 'resultId' },
4935
4949
  resultType: 'resultType',
4936
- tenantCode: 'tenantCode'
4950
+ tenantCode: 'tenantCode',
4951
+ transactionCode: 'transactionCode'
4937
4952
  }
4938
4953
  }
4939
4954
  static init(options = {}) {
@@ -4961,7 +4976,8 @@ class PaymentResult {
4961
4976
 
4962
4977
  // getters
4963
4978
  get isValid() {
4964
- return !!this.result && !!this.resultType && !!this.tenantCode
4979
+ return !!this.resultType && !!this.tenantCode && !!this.transactionCode
4980
+ // return !!this.result && !!this.resultType && !!this.tenantCode
4965
4981
  }
4966
4982
  get amount() {
4967
4983
  throw new Error(`${this._classname} subclass should implement get amount`)
@@ -4977,6 +4993,15 @@ class PaymentResult {
4977
4993
  // getPaymentId() {
4978
4994
  // throw new Error(`${this.paymentResultType} subclass should implement getPaymentId`)
4979
4995
  // }
4996
+ addMetadata(key, value) {
4997
+ q_utilities_namespaceObject.Metadata.addItem(this.metadata, key, value)
4998
+ return this
4999
+ }
5000
+ addRemark(key, value) {
5001
+ q_utilities_namespaceObject.KeyValueObject.addItem(this.remarks, key, value)
5002
+ return this
5003
+ }
5004
+
4980
5005
  compareStatus(paymentResult) {
4981
5006
  throw new Error(`${this.paymentResultType} subclass should implement compareStatus`)
4982
5007
  }
@@ -5025,18 +5050,17 @@ class PaymentResult {
5025
5050
  this.modified = (new Date()).valueOf()
5026
5051
  return this
5027
5052
  }
5053
+ setupMetadata() {
5054
+ const amount = this.amount
5055
+ const paymentMethod = this.paymentMethod
5056
+ q_utilities_namespaceObject.Metadata.addItem(this.metadata, 'AMOUNT', amount)
5057
+ q_utilities_namespaceObject.Metadata.addItem(this.metadata, 'PAYMENT_METHOD', paymentMethod)
5058
+ q_utilities_namespaceObject.KeyValueObject.addItem(this.remarks, 'amount', amount)
5059
+ q_utilities_namespaceObject.KeyValueObject.addItem(this.remarks, 'paymentMethod', paymentMethod)
5060
+ return this
5061
+ }
5028
5062
  setupRemarks() {
5029
- this.remarks = [
5030
- ...this.remarks,
5031
- {
5032
- key: 'amount',
5033
- value: this.amount
5034
- },
5035
- {
5036
- key: 'paymentMethod',
5037
- value: this.paymentMethod
5038
- }
5039
- ]
5063
+ return this.setupMetadata()
5040
5064
  }
5041
5065
  update(update) {
5042
5066
  Object.keys(update).forEach((key) => {