Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 KB | None | 0 0
  1. <script>
  2. import Datepicker from "vuejs-datepicker";
  3. import { sumBy, find, forEach, isEmpty } from "lodash-es";
  4. import humps from "humps";
  5. import restaurantPackageMixin from "lib/restaurantPackageMixin";
  6. import accounting from "accounting";
  7. import CancelComponent from "./CancelComponent.vue";
  8. import { getJSON, patchJSON, postJSON } from "lib/myAjax";
  9.  
  10. const $ = window.$;
  11.  
  12. export default {
  13. mixins: [restaurantPackageMixin],
  14. template: "#reservations-form",
  15. components: {
  16. Datepicker
  17. },
  18. data() {
  19. return {
  20. ui: {
  21. showTableField: false,
  22. showSpecialRequestField: false
  23. },
  24. reservation: {}
  25. };
  26. },
  27. watch: {
  28. // eslint-disable-next-line
  29. "reservation.state": function(newVal, _) {
  30. switch (newVal) {
  31. case "pending":
  32. this.$set(this.$data.reservation, "arrived", false);
  33. this.$set(this.$data.reservation, "active", true);
  34. this.$set(this.$data.reservation, "noShow", false);
  35. break;
  36. case "arrive":
  37. this.$set(this.$data.reservation, "arrived", true);
  38. this.$set(this.$data.reservation, "active", true);
  39. this.$set(this.$data.reservation, "noShow", false);
  40. break;
  41. case "cancel":
  42. this.$set(this.$data.reservation, "arrived", false);
  43. this.$set(this.$data.reservation, "active", false);
  44. this.$set(this.$data.reservation, "noShow", false);
  45. break;
  46. case "no_show":
  47. this.$set(this.$data.reservation, "arrived", false);
  48. this.$set(this.$data.reservation, "active", true);
  49. this.$set(this.$data.reservation, "noShow", true);
  50. break;
  51. default:
  52. window.alert("unknown reservation status");
  53. }
  54. }
  55. },
  56. computed: {
  57. basePrice() {
  58. const { selectedPackages } = this.$data;
  59. let pack;
  60. if (selectedPackages.length === 0) {
  61. return "";
  62. }
  63.  
  64. let basePriceInt = 0;
  65. let currency = "";
  66. forEach(selectedPackages, selectedPack => {
  67. pack = find(this.$data.allPackages, p => p.id === selectedPack.id);
  68. if (!pack) {
  69. return "";
  70. }
  71. if (pack.useCustomPrice) {
  72. basePriceInt += pack.customPriceCents / 100;
  73. currency = pack.customPriceCurrency;
  74. } else {
  75. if (pack.pricingMode === "multiple") {
  76. const { adult } = this.$data.reservation;
  77. const pricing = find(pack.pricing, p => {
  78. return adult >= p.minSeat && adult <= p.maxSeat;
  79. });
  80. if (pricing) {
  81. basePriceInt += pricing.priceInt;
  82. currency = pricing.currencySymbol;
  83. }
  84. } else {
  85. basePriceInt += pack.pricing.priceInt;
  86. currency = pack.pricing.currencySymbol;
  87. }
  88. }
  89. });
  90. return this.formatMoney(basePriceInt, currency);
  91. },
  92. totalPacks() {
  93. return sumBy(this.$data.selectedPackages, p => p.quantity);
  94. },
  95. totalPrice() {
  96. let total = 0;
  97. let currency = null;
  98. const { adult, kids } = this.$data.reservation;
  99.  
  100. forEach(this.$data.selectedPackages, selectedPack => {
  101. const pack = find(
  102. this.$data.allPackages,
  103. p => p.id === selectedPack.id
  104. );
  105. if (!pack) {
  106. return "";
  107. }
  108.  
  109. if (pack.useCustomPrice) {
  110. total += pack.customPriceCents / 100;
  111. currency = pack.customPriceCurrency;
  112. } else {
  113. if (pack.pricingMode === "multiple") {
  114. const price = find(pack.pricing, p => {
  115. return adult >= p.minSeat && adult <= p.maxSeat;
  116. });
  117. if (price) {
  118. total = adult * price.priceInt + (kids * price.priceInt) / 2;
  119. currency = price.currencySymbol;
  120. }
  121. } else {
  122. total += selectedPack.quantity * pack.pricing.priceInt;
  123. currency = pack.pricing.currencySymbol;
  124. }
  125. }
  126. });
  127.  
  128. if (total === 0 || currency === null) {
  129. return "";
  130. }
  131. return this.formatMoney(total, currency);
  132. }
  133. },
  134. mounted() {
  135. const data = humps.camelizeKeys(window.data);
  136. const { reservation } = data;
  137. this.$set(this.$data, "reservation", reservation);
  138. if (reservation.table) {
  139. this.$set(this.$data.ui, "showTableField", true);
  140. }
  141. if (reservation.specialRequest) {
  142. this.$set(this.$data.ui, "showSpecialRequestField", true);
  143. }
  144. this.$set(this.$data, "allPackages", data.allPackages);
  145. const hasPackages = reservation.selectedPackages.length > 0;
  146. if (hasPackages) {
  147. this.chooseSelectedPackage(reservation.selectedPackages, false);
  148. if (window.data.customPackagePricing) {
  149. this.applyCustomPackagePricing(window.data.customPackagePricing);
  150. }
  151. }
  152. },
  153. methods: {
  154. checkInventoryStatus(onSuccess, onError) {
  155. const url = `/admin/restaurants/923/check_inventory_status.json`;
  156. const payload = {
  157. date: this.$data.reservation.date,
  158. startTime: this.$data.reservation.startTime,
  159. adult: this.$data.reservation.adult,
  160. kids: this.$data.reservation.kids
  161. };
  162. postJSON(
  163. url,
  164. { reservation: humps.decamelizeKeys(payload) },
  165. response => {
  166. if (response.available) {
  167. onSuccess();
  168. } else {
  169. const message = `restaurant says: ${response.message}`;
  170. const ask = confirm(message);
  171. if (ask === true) {
  172. console.log("confirm true");
  173. onSuccess();
  174. }
  175. }
  176. },
  177. error => {
  178. onError()
  179. }
  180. );
  181. },
  182. submitForm() {
  183. if (this.$data.selectedPackages.length < 1) {
  184. window.alert("Please select package");
  185. return false;
  186. }
  187. if (
  188. this.$data.reservation.state === "cancel" &&
  189. isEmpty(this.$data.reservation.cancelReason)
  190. ) {
  191. this.showCancelReasonModal();
  192. return false;
  193. }
  194.  
  195. const onSuccess = () => {
  196. this.$refs.mainForm.submit();
  197. }
  198. const onError = () => {
  199. window.alert("Something went wrong");
  200. }
  201. this.checkInventoryStatus(
  202. onSuccess,
  203. onError
  204. );
  205.  
  206. },
  207. showCancelReasonModal() {
  208. this.$modal.show(
  209. CancelComponent,
  210. {
  211. onSubmit: () => this.submitForm(),
  212. onReasonChange: reason =>
  213. this.$set(this.$data.reservation, "cancelReason", reason)
  214. },
  215. {
  216. height: "auto"
  217. }
  218. );
  219. },
  220. cancelForm() {
  221. window.location = window.links.reservationDashboardPath;
  222. },
  223. formatMoney(price, currency) {
  224. return accounting.formatMoney(price, currency, 0, ",", ".", "%v%s");
  225. }
  226. }
  227. };
  228. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement