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

0% Statements 0/8
0% Branches 0/2
0% Functions 0/1
0% Lines 0/8

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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183                                                                                                                                                                                                                                                                                                                                                                             
<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>
          <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>
          <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 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.user"
              v-on:click.prevent="
                !order.user &&
                  $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 { warn } from "~/utils/warn";
 
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: [],
      user: 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 (user) {
      this.order.user = user;
    },
    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 () {
      const { user } = this.order;
 
      if (user) {
        const { username, first_name, last_name } = user;
 
        if (first_name) {
          return `${first_name} ${last_name}`;
        }
        return username;
      }
 
      return "undefined";
    },
    addCustomer: function (user) {
      this.order.user = user;
    },
    submit: async function () {
      const { order_items, order_bundles, user, total } = this.order;
 
      if (!order_items.length && order_bundles.length) {
        warn("Chooose product or bundle");
        return;
      }
 
      if (!user) {
        warn("Add or create customer");
        return;
      }
 
      const { id } = user;
 
      const order = {
        order_items,
        order_bundles,
        user: id,
        total,
        paid: false,
        order_status: 1,
        order_date: new Date(),
      };
 
      await this.createOrder(order);
 
      this.order = {
        order_items: [],
        order_bundles: [],
        user: 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>