All files / components/Admin/AdminOrders/OrdersTable/CreateOrder index.vue

100% Statements 7/7
100% Branches 2/2
100% Functions 1/1
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168                                                                                                                                  1x   1x   1x   1x 1x 4x                                                                                                                                                             1x                              
<template>
  <div class="row justify-content-center w-100 m-0">
    <div class="col-10 mt-3">
      <div class="d-flex w-100 mb-3 d-flex align-items-center">
        <button v-on:click="$emit('setIsTable')" class="mr-3">
          <BIconArrowLeft />
        </button>
        <h6 class="m-0">Create order</h6>
      </div>
      <form
        autocomplete="off"
        id="add-customer-form"
        v-on:submit.prevent="submit"
        class="mb-3"
      >
        <div class="row">
          <div class="col-6">
            <div class="block p-3 mb-3">
              <label class="d-flex" for="products"> Products </label>
              <ProductsBlock v-on:setProducts="setProducts" id="products" />
            </div>
          </div>
          <div class="col-6">
            <div class="block p-3 mb-3">
              <label class="d-flex" for="bundles"> Bundles </label>
              <BundlesBlock v-on:setBundles="setBundles" id="bundles" />
            </div>
          </div>
        </div>
        <div class="block p-3 mb-3">
          <div class="d-flex justify-content-between align-items-center">
            <label class="d-flex" for="customer"> Customer </label>
            <a
              href="#"
              id="customer"
              v-if="!order.customer"
              v-on:click.prevent="
                !order.customer &&
                  $root.$emit('bv::show::modal', 'create-order-modal')
              "
              >Add customer</a
            >
          </div>
          <CustomersBlock v-on:setCustomer="setCustomer" />
        </div>
        <div class="block p-3 mb-3">
          <label class="d-flex" for="payment"> Payment </label>
          <div id="payment">
            <div class="d-flex justify-content-between">
              <p>Subtotal</p>
              <p>$ {{ order.total | formatNumber }}</p>
            </div>
            <div class="d-flex justify-content-between">
              <p>Total</p>
              <p>$ {{ order.total | formatNumber }}</p>
            </div>
          </div>
        </div>
        <button class="btn btn-success w-100" type="submit">
          Create order
        </button>
      </form>
    </div>
  </div>
</template>
 
<script>
import { mapActions } from "vuex";
 
import "~/utils/filters";
 
import BundlesBlock from "~/components/Admin/common/BundlesBlock.vue";
import ProductsBlock from "~/components/Admin/common/ProductsBlock.vue";
import CustomersBlock from "./CustomersBlock.vue";
 
export default {
  name: "CreateOrder",
  components: {
    BundlesBlock,
    ProductsBlock,
    CustomersBlock,
  },
  data: () => ({
    order: {
      order_items: [],
      order_bundles: [],
      customer: null,
      total: 0,
    },
  }),
  methods: {
    ...mapActions({ createOrder: "admin_orders/createOrder" }),
    setProducts: function (data) {
      this.order.order_items = data;
      this.order.total = this.calcTotal();
    },
    setBundles: function (data) {
      this.order.order_bundles = data;
      this.order.total = this.calcTotal();
    },
    setCustomer: function (customer) {
      this.order.customer = customer;
    },
    calcTotal: function () {
      const totalProducts = this.order.order_items.reduce(
        (acc, item) => (acc += item.total),
        0
      );
 
      const totalBundles = this.order.order_bundles.reduce(
        (acc, item) => (acc += item.bundle.price),
        0
      );
 
      return totalProducts + totalBundles;
    },
    getCustomerName: function (customer) {
      const { firstName, lastName } = orer.customer;
      return `${firstName} ${lastName}`;
    },
    addCustomer: function (customer) {
      this.order.customer = customer;
    },
    submit: async function () {
      const { order_items, order_bundles, customer, total } = this.order;
 
      if (!customer) {
        return;
      }
 
      const { id } = customer;
 
      const order = {
        order_items,
        order_bundles,
        customer: id,
        total,
        paid: false,
        order_status: 1,
        order_date: new Date(),
      };
 
      await this.createOrder(order);
 
      this.order = {
        order_items: [],
        order_bundles: [],
        customer: null,
        total: 0,
      };
    },
  },
};
</script>
 
<style scoped>
input {
  position: relative;
  border: 1px solid #000;
  border-radius: 5px;
  padding-left: 30px;
}
 
.search-icon {
  z-index: 1;
  left: 6px;
}
</style>