342 lines
8.6 KiB
TypeScript
Executable File
342 lines
8.6 KiB
TypeScript
Executable File
import { defineComponent, ref, toRef, computed, PropType, watch, onMounted, reactive } from 'vue'
|
|
import { useI18n } from '@src/boot/i18n'
|
|
import { useUserStore } from '@store/UserStore'
|
|
import { useGlobalStore } from '@store/globalStore'
|
|
import { useQuasar } from 'quasar'
|
|
|
|
import { CTitleBanner } from '../CTitleBanner'
|
|
import { CCardState } from '../CCardState'
|
|
import { CCopyBtn } from '../CCopyBtn'
|
|
|
|
import { func_tools, toolsext } from '@store/Modules/toolsext'
|
|
|
|
import { IBaseOrder, IGasordine, IOrder, IOrderCart, IProduct } from '@src/model'
|
|
import { tools } from '@store/Modules/tools'
|
|
import { useProducts } from '@store/Products'
|
|
|
|
import { shared_consts } from '@src/common/shared_vuejs'
|
|
|
|
export default defineComponent({
|
|
name: 'CProductCard',
|
|
props: {
|
|
product: {
|
|
type: Object as PropType<IProduct | null>,
|
|
required: false,
|
|
default: null,
|
|
},
|
|
code: {
|
|
type: String,
|
|
required: false,
|
|
default: '',
|
|
},
|
|
cosa: {
|
|
type: Number,
|
|
required: false,
|
|
default: 0,
|
|
},
|
|
complete: {
|
|
type: Boolean,
|
|
required: false,
|
|
default: false,
|
|
},
|
|
},
|
|
components: { CTitleBanner, CCardState, CCopyBtn },
|
|
setup(props, { emit }) {
|
|
const $q = useQuasar()
|
|
const { t } = useI18n()
|
|
const userStore = useUserStore()
|
|
const globalStore = useGlobalStore()
|
|
const products = useProducts()
|
|
|
|
const listord = ref(<IOrderCart[]>[])
|
|
const sumval = ref(0)
|
|
|
|
const site = ref(globalStore.site)
|
|
|
|
const myorder = reactive(<IOrder>{
|
|
idapp: process.env.APP_ID,
|
|
quantity: 0,
|
|
quantitypreordered: 0,
|
|
idStorehouse: '',
|
|
idProvider: ''
|
|
})
|
|
|
|
const storeSelected = ref('')
|
|
const gasordineSelected = ref('')
|
|
const arrordersCart = ref(<IOrderCart[]>[])
|
|
|
|
const openlistorders = ref(false)
|
|
const endload = ref(false)
|
|
const myproduct = ref(<IProduct>{})
|
|
|
|
const valoriopt = computed(() => (item: any, addall: boolean, addnone: boolean) => {
|
|
// console.log('valoriopt', item.table)
|
|
return globalStore.getTableJoinByName(item.table, addall, addnone, item.filter)
|
|
})
|
|
|
|
/*const myproduct = computed((): IProduct => {
|
|
console.log('getproduct computed')
|
|
const ris = products.getProduct(props.code)
|
|
console.log(' received', ris)
|
|
return ris
|
|
})*/
|
|
|
|
|
|
function iconWhishlist(order: IProduct) {
|
|
if (true) {
|
|
return 'far fa-heart'
|
|
} else {
|
|
return 'fas fa-heart'
|
|
}
|
|
}
|
|
async function addtoCart(add: boolean) {
|
|
|
|
if (!userStore.isLogged) {
|
|
tools.showNeutralNotif($q, t('ecomm.area_personale'))
|
|
globalStore.rightDrawerOpen = true
|
|
return false
|
|
}
|
|
|
|
const ris = await products.addtoCartBase({ $q, t, code: myproduct.value.code!, order: myorder, addqty: add })
|
|
updateproduct()
|
|
if (ris && ris.myord) {
|
|
|
|
}
|
|
}
|
|
|
|
function getnumstore() {
|
|
if (myproduct.value) {
|
|
if (myproduct.value.storehouses)
|
|
return myproduct.value.storehouses.length
|
|
else
|
|
return 0
|
|
}
|
|
return 0
|
|
}
|
|
|
|
function getSingleStorehouse() {
|
|
try {
|
|
const mystore = myproduct.value.storehouses[0]
|
|
if (mystore)
|
|
return mystore.name + ' (' + mystore.city + ')'
|
|
else
|
|
return ''
|
|
} catch (e) {
|
|
return ''
|
|
}
|
|
}
|
|
|
|
function getSingleGasordine(gasordine: IGasordine) {
|
|
try {
|
|
const mygas = gasordine
|
|
if (mygas)
|
|
return mygas.name + ' (' + mygas.city + ') ' + t('gas.dataora_chiusura_ordini') + ': ' + tools.getstrDateShort(mygas.dataora_chiusura_ordini)
|
|
+ ' ' + t('gas.dataora_ritiro') + ': ' + tools.getstrDateShort(mygas.dataora_ritiro)
|
|
else
|
|
return ''
|
|
} catch (e) {
|
|
return ''
|
|
}
|
|
}
|
|
|
|
function updateproduct() {
|
|
myproduct.value = products.getProductByCode(props.code)
|
|
// products.updateQuantityAvailable(myproduct.value._id)
|
|
}
|
|
|
|
function getStorehouses() {
|
|
if (!myproduct.value)
|
|
return []
|
|
|
|
const myarr: any = []
|
|
let ind = 1
|
|
myproduct.value.storehouses.forEach((store) => {
|
|
myarr.push(
|
|
{
|
|
id: ind,
|
|
label: store.name + ' (' + store.city + ')',
|
|
value: store._id
|
|
})
|
|
|
|
ind++
|
|
})
|
|
|
|
// console.log('arraystore', myarr)
|
|
return myarr
|
|
}
|
|
|
|
function getGasordines() {
|
|
if (!myproduct.value)
|
|
return []
|
|
|
|
const myarr: any = []
|
|
let ind = 1
|
|
myproduct.value.gasordines.forEach((gasordine: IGasordine) => {
|
|
myarr.push(
|
|
{
|
|
id: ind,
|
|
label: getSingleGasordine(gasordine),
|
|
value: gasordine._id
|
|
})
|
|
|
|
ind++
|
|
})
|
|
|
|
return myarr
|
|
}
|
|
|
|
function checkifCartDisable() {
|
|
// return !myorder.idStorehouse
|
|
return false
|
|
}
|
|
|
|
function getQtyAvailable() {
|
|
let qty = myproduct.value.quantityAvailable!
|
|
return qty
|
|
}
|
|
|
|
function getQtyBookableAvailable() {
|
|
let qty = myproduct.value.bookableAvailableQty!
|
|
return qty
|
|
}
|
|
|
|
function getQtyWarn() {
|
|
if (myorder.quantity > 0) {
|
|
return t('ecomm.di_cui_x_in_carrello', { qty: myorder.quantity })
|
|
}
|
|
return ''
|
|
}
|
|
|
|
function getQtyWarnPreOrdered() {
|
|
if (myorder.quantitypreordered > 0) {
|
|
return t('ecomm.di_cui_x_in_carrello', { qty: myorder.quantitypreordered })
|
|
}
|
|
return ''
|
|
}
|
|
|
|
watch(() => props.code, (newval, oldval) => {
|
|
console.log('change code')
|
|
load()
|
|
})
|
|
|
|
watch(() => storeSelected.value, (newval, oldval) => {
|
|
myorder.idStorehouse = newval
|
|
})
|
|
|
|
watch(() => gasordineSelected.value, (newval, oldval) => {
|
|
myorder.idGasordine = newval
|
|
})
|
|
|
|
function load() {
|
|
updateproduct()
|
|
// console.log('Load', myproduct.value.name)
|
|
// console.log('created Cproductcard', code)
|
|
|
|
arrordersCart.value = products.getOrdersCartInAttesaByIdProduct(myproduct.value._id)
|
|
|
|
if (!!myproduct.value) {
|
|
if (myproduct.value.storehouses && myproduct.value.storehouses.length === 1) {
|
|
myorder.idStorehouse = myproduct.value.storehouses[0]._id
|
|
}
|
|
|
|
if (myproduct.value.gasordines && myproduct.value.gasordines.length === 1) {
|
|
myorder.idGasordine = myproduct.value.gasordines[0]._id
|
|
}
|
|
|
|
|
|
const ord = products.getOrderProductInCart(myproduct.value._id)
|
|
if (ord) {
|
|
myorder.quantity = ord.quantity
|
|
myorder.quantitypreordered = ord.quantitypreordered
|
|
|
|
if (ord.idGasordine)
|
|
gasordineSelected.value = ord.idGasordine!
|
|
|
|
}
|
|
}
|
|
|
|
// console.log('°°° ENDLOAD °°°')
|
|
endload.value = true
|
|
}
|
|
|
|
function getmycardcl() {
|
|
return (props.complete) ? 'my-card-big' : 'my-card-prod'
|
|
}
|
|
|
|
function getclimgproduct() {
|
|
return 'myimgproduct centermydiv'
|
|
}
|
|
|
|
function enableSubQty() {
|
|
let qty = myorder.quantity + myorder.quantitypreordered
|
|
return qty ? qty > 0 : false
|
|
}
|
|
|
|
function enableAddQty() {
|
|
if (site.value.ecomm && site.value.ecomm.enablePreOrders) {
|
|
return getQtyBookableAvailable() > 0 || getQtyAvailable() > 0
|
|
} else {
|
|
return getQtyAvailable() > 0
|
|
}
|
|
|
|
}
|
|
|
|
function visuListDisponibili() {
|
|
openlistorders.value = true
|
|
sumval.value = products.getSumQtyOrderProductInOrdersCart(myproduct.value._id)
|
|
|
|
listord.value = arrordersCart.value.filter((ordercart: IOrderCart) => ordercart.items!.reduce((accumulator, item) => {
|
|
return accumulator + item.order.quantity
|
|
}, 0))
|
|
}
|
|
|
|
function visuListBookable() {
|
|
openlistorders.value = true
|
|
sumval.value = products.getSumQtyPreOrderInOrdersCart(myproduct.value._id)
|
|
|
|
listord.value = arrordersCart.value.filter((ordercart: IOrderCart) => ordercart.items!.reduce((accumulator, item) => {
|
|
return accumulator + item.order.quantitypreordered
|
|
}, 0))
|
|
}
|
|
|
|
onMounted(load)
|
|
|
|
return {
|
|
visuListDisponibili,
|
|
visuListBookable,
|
|
addtoCart,
|
|
iconWhishlist,
|
|
getmycardcl,
|
|
getclimgproduct,
|
|
getnumstore,
|
|
getSingleStorehouse,
|
|
getSingleGasordine,
|
|
getStorehouses,
|
|
checkifCartDisable,
|
|
myproduct,
|
|
myorder,
|
|
tools,
|
|
t,
|
|
storeSelected,
|
|
gasordineSelected,
|
|
enableSubQty,
|
|
enableAddQty,
|
|
getQtyAvailable,
|
|
getQtyWarn,
|
|
openlistorders,
|
|
func_tools,
|
|
toolsext,
|
|
products,
|
|
arrordersCart,
|
|
endload,
|
|
shared_consts,
|
|
site,
|
|
getQtyWarnPreOrdered,
|
|
getQtyBookableAvailable,
|
|
listord,
|
|
sumval,
|
|
getGasordines,
|
|
}
|
|
}
|
|
})
|