cart.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { Partner } from "../../../odoo/models/res.partner";
  2. import { ProductProduct } from "../../../odoo/models/product.product";
  3. import { CartItem } from "./cart-item"
  4. export class Cart {
  5. customer: Partner;
  6. items: Array<CartItem>;
  7. total: number;
  8. constructor(
  9. customer?: Partner,
  10. items?: Array<CartItem>,
  11. total?: number
  12. ) {
  13. this.customer = customer;
  14. this.items = items ? items : [];
  15. this.total = total ? total : 0;
  16. }
  17. /**
  18. *
  19. */
  20. getCustomer(): Partner {
  21. return this.customer;
  22. }
  23. /**
  24. *
  25. */
  26. getItems(): Array<CartItem> {
  27. return this.items;
  28. }
  29. /**
  30. *
  31. */
  32. getTotal(): number {
  33. return this.total;
  34. }
  35. /**
  36. *
  37. * @param customer
  38. */
  39. setCustomer(customer: Partner): void {
  40. this.customer = customer;
  41. }
  42. /**
  43. *
  44. * @param items
  45. */
  46. setItems(items: Array<CartItem>): void {
  47. this.items = items;
  48. }
  49. /**
  50. *
  51. * @param total
  52. */
  53. setTotal(total: number): void {
  54. this.total = total;
  55. }
  56. /**
  57. *
  58. * @param index
  59. */
  60. getItem(index: number): CartItem {
  61. return this.getItems()[index];
  62. }
  63. /**
  64. *
  65. */
  66. size(): number {
  67. return this.getItems().length;
  68. }
  69. /**
  70. *
  71. * @param item
  72. */
  73. add(product: ProductProduct, price: number, quantity: number): void {
  74. let index = this.exists(product);
  75. let item: CartItem = null;
  76. if (index === -1) {
  77. item = new CartItem();
  78. item.setProduct(product);
  79. item.setPrice(price);
  80. item.updateQuantity(quantity)
  81. this.getItems().push(item);
  82. } else {
  83. let item = this.getItem(index);
  84. item.setPrice(price);
  85. item.setQuantity(item.getQuantity() + quantity);
  86. item.computeSubtotal();
  87. }
  88. this.updateTotal();
  89. }
  90. /**
  91. *
  92. * @param item
  93. */
  94. remove(item: CartItem): void {
  95. let index = this.exists(item.getProduct());
  96. this.getItems().splice(index, 1);
  97. this.updateTotal();
  98. }
  99. /**
  100. *
  101. * @param items
  102. */
  103. exists(product: ProductProduct): number {
  104. for (let i = 0; i < this.size(); i++) {
  105. if (product._id === this.getItem(i).getProduct()._id) {
  106. return i;
  107. }
  108. }
  109. return -1;
  110. }
  111. /**
  112. *
  113. */
  114. updateTotal(): void {
  115. let sum = 0;
  116. for (let i = 0; i < this.size(); i++) {
  117. sum = sum + this.getItem(i).getSubtotal();
  118. }
  119. this.setTotal(sum);
  120. }
  121. /**
  122. *
  123. */
  124. empty(): boolean {
  125. return this.size() === 0;
  126. }
  127. /**
  128. *
  129. */
  130. isValid(): boolean {
  131. return this.getCustomer() && !!this.size();
  132. }
  133. }