183 lines
4.6 KiB
TypeScript
Executable File
183 lines
4.6 KiB
TypeScript
Executable File
import { defineComponent, onMounted, ref } from 'vue'
|
|
import { tools } from '@store/Modules/tools'
|
|
import { useUserStore } from '@store/UserStore'
|
|
import { useRouter } from 'vue-router'
|
|
import { useGlobalStore } from '@store/globalStore'
|
|
import { useProducts } from '@store/Products'
|
|
import { useI18n } from '@/boot/i18n'
|
|
import { toolsext } from '@store/Modules/toolsext'
|
|
import { useQuasar } from 'quasar'
|
|
import { costanti } from '@costanti'
|
|
import { ICart, IOrderCart, IShareWithUs } from '@src/model/Products'
|
|
|
|
import { shared_consts } from '@src/common/shared_vuejs'
|
|
|
|
import { CSingleCart } from '../../../components/CSingleCart'
|
|
import { CTitleBanner } from '@components'
|
|
|
|
export default defineComponent({
|
|
name: 'checkOut',
|
|
components: { CSingleCart, CTitleBanner },
|
|
props: {},
|
|
setup() {
|
|
const userStore = useUserStore()
|
|
const globalStore = useGlobalStore()
|
|
const productStore = useProducts()
|
|
const $router = useRouter()
|
|
const $q = useQuasar()
|
|
const { t } = useI18n();
|
|
|
|
const mycart = ref(<ICart>{})
|
|
const myrec = ref(<any[string]>[])
|
|
const oldrec = ref(<any[string]>[])
|
|
const note = ref('')
|
|
const statusnow = ref(shared_consts.OrderStatus.NONE)
|
|
|
|
function mounted() {
|
|
// Inizializza
|
|
load()
|
|
}
|
|
|
|
function getItemsCart() {
|
|
const cart = productStore.getCart()
|
|
return cart.items || null
|
|
}
|
|
|
|
function getNumItems() {
|
|
const cart = productStore.getCart()
|
|
if (!!cart.items)
|
|
return cart.items.length || 0
|
|
else
|
|
return 0
|
|
}
|
|
|
|
function getCart() {
|
|
return productStore.getCart()
|
|
}
|
|
|
|
function getNote() {
|
|
const cart = productStore.getCart()
|
|
return cart.note
|
|
}
|
|
|
|
function change_field(fieldname: string) {
|
|
if (myrec.value[fieldname] !== oldrec.value[fieldname]) {
|
|
myrec.value[fieldname] = oldrec.value[fieldname]
|
|
|
|
const mydata = {
|
|
[fieldname]: myrec.value[fieldname]
|
|
}
|
|
|
|
const aggiorna = fieldname !== 'status'
|
|
tools.saveFieldToServer($q, 'carts', mycart.value._id, mydata, aggiorna)
|
|
myrec.value = oldrec.value
|
|
}
|
|
}
|
|
|
|
function myTotalPrice() {
|
|
if (productStore.cart && productStore.cart.totalPrice) {
|
|
return productStore.cart.totalPrice.toFixed(2)
|
|
} else {
|
|
return 0
|
|
}
|
|
}
|
|
|
|
function myTotalQty() {
|
|
if (productStore.cart) {
|
|
return productStore.cart.totalQty
|
|
} else {
|
|
return 0
|
|
}
|
|
}
|
|
|
|
async function load() {
|
|
mycart.value = getCart()
|
|
myrec.value = Object.keys(mycart)
|
|
note.value = mycart.value.note!
|
|
|
|
if (mycart.value)
|
|
statusnow.value = await productStore.UpdateStatusCart({ cart_id: mycart.value._id, status: 0 })
|
|
|
|
console.log('myrec', myrec.value)
|
|
}
|
|
|
|
function CanBeShipped() {
|
|
return productStore.cart.items ? productStore.cart.items.filter((rec: any) => rec.order.product.canBeShipped).length : false
|
|
}
|
|
|
|
function CanBeBuyOnline() {
|
|
return productStore.cart.items ? productStore.cart.items.filter((rec: any) => rec.order.product.canBeBuyOnline).length : false
|
|
}
|
|
|
|
function getnumsteps() {
|
|
let numsteps = 1
|
|
|
|
if (CanBeShipped())
|
|
numsteps++
|
|
if (CanBeBuyOnline())
|
|
numsteps++
|
|
|
|
return numsteps
|
|
}
|
|
|
|
function docheckout() {
|
|
|
|
// Può essere spedito?
|
|
|
|
if (CanBeShipped()) {
|
|
// mostra form di spedizione
|
|
}
|
|
|
|
if (CanBeBuyOnline()) {
|
|
// mostra form di acquisto Online
|
|
}
|
|
}
|
|
|
|
function completeOrder() {
|
|
$q.dialog({
|
|
message: 'Confermare l\'ordine di acquisto di ' + myTotalQty() + ' prodotti ?',
|
|
ok: {
|
|
label: t('dialog.yes'),
|
|
push: true
|
|
},
|
|
cancel: {
|
|
label: t('dialog.cancel')
|
|
},
|
|
title: 'Ordine'
|
|
}).onOk(async () => {
|
|
const status = shared_consts.OrderStatus.CHECKOUT_SENT
|
|
statusnow.value = await productStore.UpdateStatusCart({ cart_id: mycart.value._id, status })
|
|
|
|
if (statusnow.value === status) {
|
|
tools.showPositiveNotif($q, 'Ordine Confermato')
|
|
setTimeout(() => {
|
|
$router.push('/orderinfo')
|
|
}, 2000)
|
|
}
|
|
// change_field('status')
|
|
// change_field('status')
|
|
})
|
|
}
|
|
|
|
|
|
onMounted(mounted)
|
|
|
|
return {
|
|
userStore,
|
|
costanti,
|
|
tools,
|
|
toolsext,
|
|
completeOrder,
|
|
getNumItems,
|
|
myTotalPrice,
|
|
getItemsCart,
|
|
getNote,
|
|
change_field,
|
|
note,
|
|
statusnow,
|
|
shared_consts,
|
|
myTotalQty,
|
|
}
|
|
}
|
|
})
|