Files
myprojplanet_vite/src/views/ecommerce/orderInfo/orderInfo.ts

405 lines
9.5 KiB
TypeScript
Raw Normal View History

2023-12-28 21:00:10 +01:00
import { defineComponent, onMounted, ref, watch } from 'vue'
2023-11-28 08:45:52 +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'
import { shared_consts } from '@src/common/shared_vuejs'
// import MixinBase from '@src/mixins/mixin-base'
2024-01-09 15:32:21 +01:00
import { serv_constants } from '@store/Modules/serv_constants'
import { fieldsTable } from '@store/Modules/fieldsTable'
2023-11-27 14:40:42 +01:00
import { CSingleCart } from '../../../components/CSingleCart'
2024-01-09 15:32:21 +01:00
import { CTitleBanner, CMyFieldDb } from '@components'
2023-12-15 23:36:48 +01:00
import { ICart, IOrder, IOrderCart } from '@src/model'
2023-11-27 14:40:42 +01:00
2023-11-28 08:45:52 +01:00
export default defineComponent({
name: 'OrderInfo',
2024-01-09 15:32:21 +01:00
components: { CSingleCart, CTitleBanner, CMyFieldDb },
2023-11-28 08:45:52 +01:00
props: {},
setup() {
const userStore = useUserStore()
const globalStore = useGlobalStore()
const productStore = useProducts()
const $router = useRouter()
const $q = useQuasar()
2023-12-27 02:58:23 +01:00
const { t } = useI18n()
2023-11-28 08:45:52 +01:00
const myorderscart = ref(<IOrderCart[] | undefined>[])
const myarrrec = ref(<any>{})
const myoldrec = ref(<any>{})
2024-01-09 15:32:21 +01:00
const templemail = ref('')
2023-11-28 08:45:52 +01:00
2023-12-28 21:00:10 +01:00
const cosa = ref(0)
const initialPagination = ref({
sortBy: 'desc',
descending: false,
rowsPerPage: 10,
// rowsNumber: xx if getting data from a server
})
2023-11-28 08:45:52 +01:00
const conferma_carrello = ref(false)
const conferma_ordine = ref(false)
const endload = ref(false)
2023-11-28 08:45:52 +01:00
2023-12-13 19:18:00 +01:00
const taborders = ref(shared_consts.OrderStat.IN_CORSO.value)
2023-11-28 08:45:52 +01:00
const statusnow = ref(0)
const arrnumstatus = ref(<any[]>[])
const columns = ref(<any>[
2024-01-09 15:32:21 +01:00
{
name: 'apri',
align: 'center',
required: false,
label: 'Apri',
field: 'apri',
sortable: false
},
2023-12-10 15:15:27 +01:00
{
name: 'numorder',
required: true,
align: 'left',
2023-12-15 23:36:48 +01:00
label: 'Ordine N.',
2023-12-10 15:15:27 +01:00
field: 'numorder',
sortable: true
},
{
name: 'created_at',
required: true,
align: 'center',
label: 'Effettuato il',
field: 'created_at',
sortable: true
},
{
name: 'items',
required: true,
label: '',
2023-12-10 15:15:27 +01:00
field: 'items',
sortable: true
},
{
name: 'totalPrice',
required: true,
label: 'Totale',
field: 'totalPrice',
sortable: true
},
{
name: 'status',
align: 'center',
required: true,
label: 'Stato',
field: 'status',
sortable: true
}
])
const columns_Admin = ref(<any>[
2024-01-09 15:32:21 +01:00
{
name: 'apri',
align: 'center',
required: false,
label: 'Apri',
field: 'apri',
sortable: false
},
{
name: 'comandi',
align: 'center',
required: false,
label: 'Comandi',
field: 'comandi',
sortable: false
},
2023-11-28 08:45:52 +01:00
{
name: 'numorder',
required: true,
align: 'left',
2023-12-15 23:36:48 +01:00
label: 'Ordine N.',
2023-11-28 08:45:52 +01:00
field: 'numorder',
sortable: true
},
{
name: 'nameSurname',
required: true,
align: 'left',
label: 'Nome',
field: 'nameSurname',
sortable: true
},
{
name: 'created_at',
required: true,
align: 'center',
label: 'Effettuato il',
field: 'created_at',
sortable: true
},
{
name: 'items',
required: true,
label: 'Articoli',
field: 'items',
sortable: true
},
{
name: 'totalPrice',
required: true,
label: 'Totale',
field: 'totalPrice',
sortable: true
},
{
name: 'status',
align: 'center',
required: true,
label: 'Stato',
field: 'status',
sortable: true
2023-12-10 15:15:27 +01:00
},
2023-11-28 08:45:52 +01:00
])
2023-11-27 14:40:42 +01:00
2023-12-28 21:00:10 +01:00
watch(() => cosa.value, (newval, oldval) => {
2024-01-09 15:32:21 +01:00
2023-12-28 21:00:10 +01:00
})
2023-11-28 08:45:52 +01:00
// const { setValDb, getValDb } = MixinBase()
2023-11-27 14:40:42 +01:00
2023-11-28 08:45:52 +01:00
function getCols(props: any) {
return props.cols.filter((col: any) => col.name !== 'desc')
}
2023-11-27 14:40:42 +01:00
2023-11-28 08:45:52 +01:00
function getOrdersCart(): IOrderCart[] | undefined {
2023-12-28 21:00:10 +01:00
const hasGasordine: any = (cosa.value === shared_consts.PROD.TUTTI) ? undefined : (cosa.value === shared_consts.PROD.GAS)
const ris = productStore.getOrdersCart(taborders.value, hasGasordine)
2024-01-09 15:32:21 +01:00
return ris
2023-11-28 08:45:52 +01:00
}
2023-11-27 14:40:42 +01:00
2023-12-15 23:36:48 +01:00
function getOrdersCartWithTotals(): any[] {
const orderscart = getOrdersCart();
if (orderscart) {
// Calculate totals
const totals = {
created_at: new Date(),
items: null,
2024-01-09 15:32:21 +01:00
user: {
2023-12-15 23:36:48 +01:00
name: 'TOTALI',
surname: '',
2024-01-09 15:32:21 +01:00
email: '',
2023-12-15 23:36:48 +01:00
},
status: 0,
// Add other properties as needed
totalPrice: orderscart.reduce((total: number, rec: any) => total + rec.totalPrice, 0),
totalPriceCalc: orderscart.reduce((total: number, rec: any) => total + rec.totalPriceCalc, 0),
2023-12-15 23:36:48 +01:00
};
let rowsWithTotals: any = [...orderscart];
if (tools.isManager()) {
// Combine orders and totals
rowsWithTotals.push(totals);
}
2023-12-15 23:36:48 +01:00
return rowsWithTotals;
} else {
return []
}
}
2023-11-28 08:45:52 +01:00
function getAllOrdersCart() {
return productStore.getOrdersAllCart()
}
2023-11-27 14:40:42 +01:00
2023-11-28 08:45:52 +01:00
function updateorders() {
myorderscart.value = getOrdersCart()
if (myorderscart.value) {
2024-01-13 00:29:02 +01:00
for (const orderscart of myorderscart.value) {
myarrrec.value[orderscart._id] = Object.keys(orderscart)
}
2023-11-27 14:40:42 +01:00
}
2023-11-28 08:45:52 +01:00
const allorders = getAllOrdersCart()
for (const status of [15, 2, 3, 4, 5, 6, 7, 8, 10]) {
2023-11-28 08:45:52 +01:00
arrnumstatus.value[status] = allorders.filter((rec) => (rec.status === status)).reduce((sum, item) => sum + 1, 0)
}
2023-11-27 14:40:42 +01:00
selectfirstavailable()
2023-11-27 14:40:42 +01:00
}
2023-11-27 14:40:42 +01:00
function selectfirstavailable() {
2023-12-13 19:18:00 +01:00
Object.keys(arrnumstatus.value).forEach((stat: any) => {
if (arrnumstatus.value[stat] > 0) {
if (taborders.value === -1)
taborders.value = parseInt(stat)
}
})
}
async function mounted() {
2024-01-09 15:32:21 +01:00
await userStore.newsletterload(false)
await productStore.loadProducts()
taborders.value = -1
updateorders()
2023-12-13 19:18:00 +01:00
if (taborders.value === -1)
taborders.value = shared_consts.OrderStat.IN_CORSO.value
2023-11-27 14:40:42 +01:00
2023-12-10 15:15:27 +01:00
if (tools.isManager()) {
columns.value = [...columns_Admin.value]
}
endload.value = true
2023-11-28 08:45:52 +01:00
}
2023-11-27 14:40:42 +01:00
2023-11-28 08:45:52 +01:00
function CanBeShipped() {
return productStore.cart.items!.filter((rec) => rec.order.product!.canBeShipped).length
}
2023-11-27 14:40:42 +01:00
2023-11-28 08:45:52 +01:00
function CanBeBuyOnline() {
return productStore.cart.items!.filter((rec) => rec.order.product!.canBeBuyOnline).length
}
2023-11-27 14:40:42 +01:00
2023-11-28 08:45:52 +01:00
function getnumsteps() {
let numsteps = 1
2023-11-27 14:40:42 +01:00
2023-11-28 08:45:52 +01:00
if (CanBeShipped())
numsteps++
if (CanBeBuyOnline())
numsteps++
2023-11-27 14:40:42 +01:00
2023-11-28 08:45:52 +01:00
return numsteps
}
2023-11-27 14:40:42 +01:00
2023-11-28 08:45:52 +01:00
function docheckout() {
2023-11-27 14:40:42 +01:00
2023-11-28 08:45:52 +01:00
// Può essere spedito?
2023-11-27 14:40:42 +01:00
2023-11-28 08:45:52 +01:00
if (CanBeShipped()) {
// mostra form di spedizione
}
2023-11-27 14:40:42 +01:00
2023-11-28 08:45:52 +01:00
if (CanBeBuyOnline()) {
// mostra form di acquisto Online
}
2023-11-27 14:40:42 +01:00
}
2023-11-28 08:45:52 +01:00
function nextstep() {
return 0
2023-11-27 14:40:42 +01:00
}
2023-11-28 08:45:52 +01:00
function clickFunz(order: IOrderCart, status: number) {
2023-11-27 14:40:42 +01:00
2023-11-28 08:45:52 +01:00
if (status === shared_consts.OrderStatus.ORDER_CONFIRMED) {
// Conferma Ordine
}
2023-11-27 14:40:42 +01:00
2023-11-28 08:45:52 +01:00
const statusStr = shared_consts.getStatusStr(status)
$q.dialog({
message: 'Impostare l\'ordine n. ' + order.numorder + ' ' + statusStr + ' ?',
ok: {
label: t('dialog.yes'),
push: true
},
cancel: {
label: t('dialog.cancel')
},
title: 'Ordine'
}).onOk(async () => {
2024-01-03 23:19:24 +01:00
statusnow.value = await productStore.UpdateOrderCartStatus({ order_id: order._id, status })
2023-11-28 08:45:52 +01:00
if (statusnow.value === status) {
order.status = statusnow.value
updateorders()
tools.showPositiveNotif($q, 'Ordine ' + statusStr)
}
// change_field('status')
// change_field('status')
})
}
2023-11-27 14:40:42 +01:00
2024-01-09 15:32:21 +01:00
async function sendMailToAll() {
const orders = getOrdersCartWithTotals()
let res = null
let inviate = 0
for (const ord of orders) {
res = await productStore.sendMailToTheBuyer(ord._id, templemail.value, false)
if (res && res.emailsend)
inviate++
}
if (inviate > 0) {
tools.showPositiveNotif($q, t('orderscart.email_sent', { inviate }))
} else {
tools.showNegativeNotif($q, t('orderscart.email_not_send'))
}
}
async function sendMailTest() {
const orders = getOrdersCartWithTotals()
if (orders && orders.length > 0) {
const res = await productStore.sendMailToTheBuyer(orders[0]._id, templemail.value, true)
if (res && res.emailsend) {
tools.showPositiveNotif($q, t('reset.email_sent'))
} else {
tools.showNegativeNotif($q, t('orderscart.email_not_send'))
}
}
}
2023-11-28 08:45:52 +01:00
onMounted(mounted)
return {
userStore,
costanti,
tools,
toolsext,
shared_consts,
2024-01-09 15:32:21 +01:00
globalStore,
2023-11-28 08:45:52 +01:00
columns,
taborders,
arrnumstatus,
clickFunz,
getOrdersCart,
getAllOrdersCart,
updateorders,
getCols,
endload,
2023-12-15 23:36:48 +01:00
getOrdersCartWithTotals,
2023-12-20 21:56:30 +01:00
productStore,
2023-12-27 02:58:23 +01:00
t,
2023-12-28 21:00:10 +01:00
initialPagination,
cosa,
2024-01-09 15:32:21 +01:00
sendMailToAll,
sendMailTest,
templemail,
fieldsTable,
2023-11-28 08:45:52 +01:00
}
2023-11-27 14:40:42 +01:00
}
2023-11-28 08:45:52 +01:00
})
2023-11-27 14:40:42 +01:00