Files
pleno-vue/src/views/dashboards/superUserDashboard/department/SuperUserSelectedDepartmentObject.vue
T

164 lines
4.8 KiB
Vue

<script>
import { ref } from 'vue';
import { authenticatedRequest } from "@/components/session/authenticatedRequest.vue";
import Swal from "sweetalert2";
// Define the department id
export const departmentId = ref(0);
export const CUSTOM_PRICING_MISSING_PRICE = 999999;
const default_department = {
id: null,
name: null,
description: null,
branding: null,
custom_pricing_only: false,
created_at: null,
updated_at: null,
prices: []
};
const default_department_functions = {
isLoaded() {
return this.id !== null;
},
getBranding() {
const brandingId = parseInt(this.branding);
return Number.isFinite(brandingId) && brandingId > 0 ? brandingId : null;
},
hasBranding() {
return this.getBranding() !== null;
}
};
// Define the department object
export const department = {
id: ref(''),
name: ref(''),
description: ref(''),
custom_pricing_only: ref(false),
created_at: ref(''),
updated_at: ref(''),
prices: ref([])
}
export const departmentAdvanced = ref({
...default_department,
...default_department_functions
});
const isDepartmentPricesLoaded = ref(false);
// Set the department id
export const setDepartment = (id) => {
departmentId.value = id;
isDepartmentPricesLoaded.value = false;
department.prices.value = [];
department.custom_pricing_only.value = false;
departmentAdvanced.value = {
...default_department,
...default_department_functions
};
// Load the department data
return getDepartmentData();
}
// Get the department data
export const getDepartmentData = async () => {
return await authenticatedRequest(`/superuser/department?department_id=${departmentId.value}`, "GET")
.then((response) => {
department.id.value = response.data.data.id;
department.name.value = response.data.data.name;
department.description.value = response.data.data.description;
department.custom_pricing_only.value = Boolean(response.data.data.custom_pricing_only);
department.created_at.value = response.data.data.created_at;
department.updated_at.value = response.data.data.updated_at;
departmentAdvanced.value = {
...default_department,
...response.data.data,
custom_pricing_only: Boolean(response.data.data.custom_pricing_only),
...default_department_functions
};
})
}
export const getDepartmentPrices = async () => {
return await authenticatedRequest(`/superuser/department/prices?department_id=${departmentId.value}`, "GET")
.then((response) => {
department.prices.value = response.data.data;
})
.catch((error) => {
department.prices.value = [];
isDepartmentPricesLoaded.value = false;
});
}
export const getDepartmentPrice = (product) => {
// If the department prices aren't loaded, load them
if (!isDepartmentPricesLoaded.value) {
isDepartmentPricesLoaded.value = true;
getDepartmentPrices();
}
if (isDepartmentPricesLoaded.value) {
const price = getExplicitDepartmentPrice(product);
if (price !== null) {
return price;
}
return isCustomPricingOnly() ? CUSTOM_PRICING_MISSING_PRICE : product.price;
}
return product.price;
}
export const getExplicitDepartmentPrice = (product) => {
const price = department.prices.value.find((price) => price.product_id === product.id);
return price ? price.price : null;
}
export const isCustomPricingOnly = () => Boolean(departmentAdvanced.value.custom_pricing_only || department.custom_pricing_only.value);
export const updateCustomPricingOnly = async (enabled) => {
return authenticatedRequest(`/departments`, "PUT", {
id: departmentId.value,
custom_pricing_only: Boolean(enabled)
}).then(async () => {
department.custom_pricing_only.value = Boolean(enabled);
departmentAdvanced.value = {
...departmentAdvanced.value,
custom_pricing_only: Boolean(enabled)
};
await getDepartmentData();
});
}
export const editDepartmentPrice = (product) => {
const explicitPrice = getExplicitDepartmentPrice(product);
Swal.fire({
title: product.name + ' ( product: ' + product.id + ' )',
input: 'number',
inputValue: explicitPrice === null ? '' : explicitPrice,
inputLabel: 'Price',
inputAttributes: {
autocapitalize: 'off'
},
showCancelButton: true,
confirmButtonText: 'Save',
showLoaderOnConfirm: true,
preConfirm: (price) => {
return authenticatedRequest(`/superuser/department/prices`, "POST", {
department_id: departmentId.value,
product_id: product.id,
price: price
})
.then((response) => {
console.log(response);
getDepartmentPrices();
})
.catch((error) => {
console.log(error);
});
},
allowOutsideClick: () => !Swal.isLoading()
});
}
</script>