services-entry-page.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const app = require('../app')
  2. const moment = require('moment-immutable')
  3. const { editIcon } = require('../assets')
  4. app.component('appServicesEntryPage', {
  5. template: html`
  6. <app-user-area title-text="{{ctrl.terminalKey}} Service Entry">
  7. <app-breadcrumb links="[
  8. { text: 'Home', link: '/dashboard' },
  9. { text: ctrl.terminalKey + ' Services', link: '/services/' + ctrl.terminalKey },
  10. { text: ctrl.startDate.format('L') + ' - ' + ctrl.endDate.format('L'), link: '/services/' + ctrl.terminalKey + '/' + ctrl.startDate.format('YYYY-MM-DD') }
  11. ]"></app-breadcrumb>
  12. <p>For week of {{::ctrl.startDate.format('LL')}} to {{::ctrl.endDate.format('LL')}}</p>
  13. <form name="form" ng-submit="ctrl.submit()">
  14. <table md-table md-progress="ctrl.promise">
  15. <colgroup>
  16. <col style="width: 15%" />
  17. <col ng-repeat="weekday in ::ctrl.weekdays" style="width: 12%" />
  18. </colgroup>
  19. <thead md-head>
  20. <tr md-row>
  21. <th md-column>Client</th>
  22. <th md-column ng-repeat="weekday in ::ctrl.weekdays">
  23. <span hide show-xs>{{::weekday.min}}</span>
  24. <span hide show-sm>{{::weekday.short}}</span>
  25. <span hide show-gt-sm>{{::weekday.name}}</span>
  26. </th>
  27. </tr>
  28. </thead>
  29. <tbody md-body>
  30. <tr md-row ng-repeat="sfl in ::ctrl.clientServices track by sfl.id">
  31. <td md-cell>
  32. {{::ctrl.clients[sfl.id].name}}
  33. </td>
  34. <td md-cell ng-repeat="day in ::sfl.days track by $index">
  35. <input
  36. class="hour-input md-button md-raised"
  37. ng-model="day.cartons"
  38. tabindex="{{$index * ctrl.staffMemberLabor.length + ($parent.$index) + 1}}"
  39. type="number" min="0" step="1">
  40. </td>
  41. </tr>
  42. </tbody>
  43. </table>
  44. <div>
  45. <md-button type="submit" class="md-raised md-primary">Submit</md-button>
  46. </div>
  47. </form>
  48. </app-user-area>
  49. `,
  50. controllerAs: 'ctrl',
  51. controller: function(api, $routeParams, weekdays, $mdToast) {
  52. this.weekdays = weekdays
  53. this.terminalKey = $routeParams.terminal
  54. const week = moment($routeParams.week)
  55. if (!week.isSame(week.startOf('week'))) throw new Error('Date is not start of week')
  56. this.startDate = week
  57. this.endDate = week.endOf('week')
  58. api.clientDictionary().then(clients => {
  59. this.clients = clients
  60. })
  61. this.promise = api.get(`/api/services/${$routeParams.terminal}/${$routeParams.week}`).then(({workdays}) => {
  62. this.model = workdays
  63. const clientIds = workdays[0].services.map(x => x.clientId)
  64. this.clientServices = clientIds.map((id, i) => ({
  65. id,
  66. days: this.model.map(wd => wd.services[i])
  67. }))
  68. })
  69. this.submit = async () => {
  70. const model = {
  71. workdays: this.model.map(workday => ({
  72. services: workday.services.map(service => ({
  73. clientId: service.clientId,
  74. cartons: service.cartons || null
  75. }))
  76. }))
  77. }
  78. try {
  79. await api.patch(`/api/services/${$routeParams.terminal}/${$routeParams.week}`, model)
  80. $mdToast.showSimple('Services saved.')
  81. } catch (err) {
  82. window.err = err
  83. console.error(err)
  84. $mdToast.showSimple(`Could not save Services: ${err.message || err.statusText || err}`)
  85. }
  86. }
  87. }
  88. })