Files
salvato.newfreeplanet/src/views/ecommerce/checkOut/checkOut.ts

244 lines
6.4 KiB
TypeScript
Raw Normal View History

import { defineComponent, onMounted, ref, computed } from 'vue'
2023-11-27 16:27:05 +01:00
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'
2023-12-27 02:58:23 +01:00
import { ICart, IOrder, IOrderCart, IProduct, IShareWithUs } from '@src/model/Products'
2023-11-27 14:40:42 +01:00
2023-11-27 16:27:05 +01:00
import { shared_consts } from '@src/common/shared_vuejs'
2023-11-27 14:40:42 +01:00
import { CSingleCart } from '../../../components/CSingleCart'
import { CTitleBanner } from '@components'
2023-11-27 16:27:05 +01:00
export default defineComponent({
2023-11-27 14:40:42 +01:00
name: 'checkOut',
2023-11-27 16:27:05 +01:00
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>{})
2023-11-27 16:27:05 +01:00
const myrec = ref(<any[string]>[])
const oldrec = ref(<any[string]>[])
const note = ref('')
const endload = ref(false)
2023-12-13 19:18:00 +01:00
const recOrderCart = ref(<IOrderCart>{})
const search = ref('')
const statusnow = computed(() => (): number => {
if (recOrderCart.value) {
return recOrderCart.value.status
}
return 0
})
2023-11-27 16:27:05 +01:00
function mounted() {
// Inizializza
load()
2023-11-27 14:40:42 +01:00
}
2023-11-27 16:27:05 +01:00
function getItemsCart() {
const cart = productStore.getCart()
return cart.items || null
}
2023-11-28 14:12:45 +01:00
function getNumItems(): number {
2023-11-27 16:27:05 +01:00
const cart = productStore.getCart()
if (!!cart.items)
return cart.items.length || 0
else
return 0
}
2023-11-28 14:12:45 +01:00
function getCart(): ICart {
2023-11-27 16:27:05 +01:00
return productStore.getCart()
}
2023-11-27 16:27:05 +01:00
function getNote() {
const cart = productStore.getCart()
return cart.note
}
2023-11-27 16:27:05 +01:00
function change_field(fieldname: string) {
if (myrec.value[fieldname] !== oldrec.value[fieldname]) {
myrec.value[fieldname] = oldrec.value[fieldname]
2023-11-27 16:27:05 +01:00
const mydata = {
[fieldname]: myrec.value[fieldname]
}
2023-11-27 16:27:05 +01:00
const aggiorna = fieldname !== 'status'
tools.saveFieldToServer($q, 'carts', mycart.value._id, mydata, aggiorna)
2023-11-28 14:12:45 +01:00
oldrec.value[fieldname] = myrec.value[fieldname]
2023-11-27 14:40:42 +01:00
}
}
2023-11-28 14:12:45 +01:00
function myTotalPrice(): string {
2023-11-27 16:27:05 +01:00
if (productStore.cart && productStore.cart.totalPrice) {
return productStore.cart.totalPrice.toFixed(2)
} else {
2023-11-28 14:12:45 +01:00
return '0'
2023-11-27 16:27:05 +01:00
}
2023-11-27 14:40:42 +01:00
}
2023-11-28 14:12:45 +01:00
function myTotalQty(): number {
2023-11-27 16:27:05 +01:00
if (productStore.cart) {
2023-11-28 14:12:45 +01:00
return productStore.cart.totalQty!
2023-11-27 16:27:05 +01:00
} else {
return 0
}
2023-11-27 14:40:42 +01:00
}
2023-11-27 16:27:05 +01:00
async function load() {
mycart.value = getCart()
myrec.value = Object.keys(mycart)
2023-11-28 14:12:45 +01:00
oldrec.value = myrec.value
2023-11-27 16:27:05 +01:00
note.value = mycart.value.note!
2023-12-13 19:18:00 +01:00
if (mycart.value) {
recOrderCart.value = await productStore.CreateOrdersCart({ cart_id: mycart.value._id, status: 0, note: note.value })
2023-12-13 19:18:00 +01:00
}
2023-11-27 16:27:05 +01:00
console.log('myrec', myrec.value)
endload.value = true
2023-11-27 14:40:42 +01:00
}
2023-11-27 16:27:05 +01:00
function CanBeShipped() {
return productStore.cart.items ? productStore.cart.items.filter((rec: any) => rec.order.product.canBeShipped).length : false
2023-11-27 14:40:42 +01:00
}
2023-11-27 16:27:05 +01:00
function CanBeBuyOnline() {
return productStore.cart.items ? productStore.cart.items.filter((rec: any) => rec.order.product.canBeBuyOnline).length : false
}
2023-11-27 16:27:05 +01:00
function getnumsteps() {
let numsteps = 1
2023-11-27 16:27:05 +01:00
if (CanBeShipped())
numsteps++
if (CanBeBuyOnline())
numsteps++
2023-11-27 16:27:05 +01:00
return numsteps
}
2023-11-27 16:27:05 +01:00
function docheckout() {
2023-11-27 16:27:05 +01:00
// Può essere spedito?
2023-11-27 16:27:05 +01:00
if (CanBeShipped()) {
// mostra form di spedizione
}
2023-11-27 16:27:05 +01:00
if (CanBeBuyOnline()) {
// mostra form di acquisto Online
2023-11-27 14:40:42 +01:00
}
2023-11-27 16:27:05 +01:00
}
2023-11-27 16:27:05 +01:00
function completeOrder() {
$q.dialog({
message: t('ecomm.conferma_acq', { qty: myTotalQty() }),
2023-11-27 16:27:05 +01:00
ok: {
label: t('dialog.yes'),
push: true
},
cancel: {
label: t('dialog.cancel')
},
title: t('ecomm.order')
2023-11-27 16:27:05 +01:00
}).onOk(async () => {
const status = shared_consts.OrderStatus.CHECKOUT_SENT
recOrderCart.value = await productStore.CreateOrdersCart({ cart_id: mycart.value._id, status, note: note.value })
// statusnow.value = myordercart ? myordercart.status : 0
if (recOrderCart.value.status === status) {
tools.showPositiveNotif($q, t('ecomm.ord_confirmed'))
2023-11-27 16:27:05 +01:00
setTimeout(() => {
$router.push('/orderinfo')
}, 2000)
2023-12-13 19:18:00 +01:00
} else {
tools.showNegativeNotif($q, t('ecomm.ord_not_confirmed'))
2023-11-27 16:27:05 +01:00
}
// change_field('status')
// change_field('status')
})
}
2023-12-27 02:58:23 +01:00
function getActualIdStorehouse(myprod: IProduct) {
// Ottieni il negozio attualmente selezionato:
// Se ce n'è solo 1 allora prendi quello !
if (myprod.storehouses.length === 1) {
return myprod.storehouses[0]._id
} else {
// Ottieni il negozio attualmente scelto !
return ''
}
}
function getActualGasordine(myprod: IProduct) {
// Ottieni il negozio attualmente selezionato:
// Se ce n'è solo 1 allora prendi quello !
if (myprod.gasordines.length === 1) {
return myprod.gasordines[0]._id
} else {
// Ottieni il gasordine attualmente scelto !
return ''
}
}
async function insertArticolo() {
let lowerSearchText = search.value.trim();
const myprod = productStore.getProductByCode(lowerSearchText);
if (myprod && myprod.active) {
2023-12-27 02:58:23 +01:00
let myorder: IOrder = { quantity: 1, quantitypreordered: 0,
TotalPriceProduct: 0, price: 0,
idStorehouse: getActualIdStorehouse(myprod),
idGasordine: getActualGasordine(myprod),
}
await productStore.addtoCartBase({ $q, t, id: myprod._id, order: myorder, addqty: true })
search.value = ''
load()
}
}
2023-11-27 16:27:05 +01:00
onMounted(mounted)
return {
userStore,
costanti,
tools,
toolsext,
completeOrder,
getNumItems,
myTotalPrice,
getItemsCart,
getNote,
change_field,
note,
statusnow,
shared_consts,
myTotalQty,
2023-12-13 19:18:00 +01:00
recOrderCart,
mycart,
endload,
search,
insertArticolo,
2023-11-27 16:27:05 +01:00
}
2023-11-27 14:40:42 +01:00
}
2023-11-27 16:27:05 +01:00
})