Invoice.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { appApi } from 'src/boot/axios.js'
  2. import APIRepository from '../classes/APIRepository.js'
  3. import { Invoice, InvoiceList } from 'src/models/Invoice.js'
  4. export default class InvoiceAPI extends APIRepository {
  5. constructor() {
  6. super('invoice', appApi)
  7. this.APIAdresses = {
  8. base: '/accounting/invoices',
  9. instalments: '/accounting/instalments',
  10. byId: (id) => '/accounting/invoices/' + id,
  11. pay: (id) => '/accounting/invoices/' + id + '/pay'
  12. }
  13. this.CacheList = {
  14. base: this.name + this.APIAdresses.base
  15. }
  16. this.restUrl = (id) => this.APIAdresses.base + '/' + id
  17. /* Setting the callback functions for the CRUD operations. */
  18. this.setCrudCallbacks({
  19. get: (response) => { return new Invoice(response.data) },
  20. post: (response) => { return new Invoice(response.data) },
  21. put: (response) => { return new Invoice(response.data) },
  22. delete: (response) => { return response.data }
  23. })
  24. }
  25. index(data) {
  26. return this.sendRequest({
  27. apiMethod: 'get',
  28. api: this.api,
  29. request: this.APIAdresses.base,
  30. data: this.getNormalizedSendData({
  31. page: 1 // Number
  32. }, data),
  33. resolveCallback: (response) => {
  34. const paginate = response.data
  35. const results = response.data.results
  36. delete paginate.results
  37. return {
  38. list: new InvoiceList(results),
  39. paginate
  40. // {
  41. // "count": 1,
  42. // "num_pages": 1,
  43. // "per_page": 10,
  44. // "current": 1,
  45. // "next": null,
  46. // "previous": null,
  47. // }
  48. }
  49. },
  50. rejectCallback: (error) => {
  51. return error
  52. }
  53. })
  54. }
  55. instalments(data) {
  56. return this.sendRequest({
  57. apiMethod: 'get',
  58. api: this.api,
  59. request: this.APIAdresses.base,
  60. data: this.getNormalizedSendData({
  61. type: null, // String -> overdue - ...
  62. page: 1 // Number
  63. }, data),
  64. resolveCallback: (response) => {
  65. const paginate = response.data
  66. const results = response.data.results
  67. delete paginate.results
  68. return {
  69. list: new InvoiceList(results),
  70. paginate
  71. // {
  72. // "count": 1,
  73. // "num_pages": 1,
  74. // "per_page": 10,
  75. // "current": 1,
  76. // "next": null,
  77. // "previous": null,
  78. // }
  79. }
  80. },
  81. rejectCallback: (error) => {
  82. return error
  83. }
  84. })
  85. }
  86. pay(invoiceId) {
  87. return this.sendRequest({
  88. apiMethod: 'put',
  89. api: this.api,
  90. request: this.APIAdresses.pay(invoiceId),
  91. resolveCallback: (response) => {
  92. return response.data.detail // String : message
  93. },
  94. rejectCallback: (error) => {
  95. return error
  96. }
  97. })
  98. }
  99. createService (products = []) {
  100. return this.sendRequest({
  101. apiMethod: 'post',
  102. api: this.api,
  103. request: this.APIAdresses.base,
  104. data: {
  105. type: 'SERVICE', // String
  106. products // Array of Object
  107. },
  108. resolveCallback: (response) => {
  109. return new Invoice(response.data)
  110. },
  111. rejectCallback: (error) => {
  112. return error
  113. }
  114. })
  115. }
  116. createBasket (basketType, basketId) {
  117. return this.sendRequest({
  118. apiMethod: 'post',
  119. api: this.api,
  120. request: this.APIAdresses.base,
  121. data: {
  122. type: basketType.toUpperCase(), // String
  123. products: [
  124. {
  125. product_type: basketType + '_basket',
  126. product_id: basketId,
  127. count: 1
  128. }
  129. ]
  130. },
  131. resolveCallback: (response) => {
  132. return new Invoice(response.data)
  133. },
  134. rejectCallback: (error) => {
  135. return error
  136. }
  137. })
  138. }
  139. create (type, products = []) {
  140. return this.sendRequest({
  141. apiMethod: 'post',
  142. api: this.api,
  143. request: this.APIAdresses.base,
  144. data: {
  145. type, // String
  146. products // Array of Object
  147. },
  148. resolveCallback: (response) => {
  149. return new Invoice(response.data)
  150. },
  151. rejectCallback: (error) => {
  152. return error
  153. }
  154. })
  155. }
  156. }