Files
myprojplanet_vite/src/views/ecommerce/orderInfo/orderInfo.ts
2024-02-15 18:59:13 +01:00

448 lines
11 KiB
TypeScript
Executable File

import { defineComponent, onMounted, ref, watch } 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 { shared_consts } from '@src/common/shared_vuejs'
// import MixinBase from '@src/mixins/mixin-base'
import { serv_constants } from '@store/Modules/serv_constants'
import { fieldsTable } from '@store/Modules/fieldsTable'
import { CSingleCart } from '../../../components/CSingleCart'
import { CTitleBanner, CMyFieldDb } from '@components'
import { ICart, IOrder, IOrderCart } from '@src/model'
export default defineComponent({
name: 'OrderInfo',
components: { CSingleCart, CTitleBanner, CMyFieldDb },
props: {},
setup() {
const userStore = useUserStore()
const globalStore = useGlobalStore()
const productStore = useProducts()
const $router = useRouter()
const $q = useQuasar()
const { t } = useI18n()
const myorderscart = ref(<IOrderCart[] | undefined>[])
const myarrrec = ref(<any>{})
const myoldrec = ref(<any>{})
const templemail = ref('')
const cosa = ref(0)
const arrout = ref(<any[]>[])
const initialPagination = ref({
sortBy: 'desc',
descending: false,
rowsPerPage: 10,
// rowsNumber: xx if getting data from a server
})
const conferma_carrello = ref(false)
const conferma_ordine = ref(false)
const endload = ref(false)
const storeGasordine = ref(<any>'')
const taborders = ref(shared_consts.OrderStat.IN_CORSO.value)
const statusnow = ref(0)
const arrnumstatus = ref(<any[]>[])
const columns = ref(<any>[
{
name: 'ind',
required: true,
align: 'left',
label: 'Ind',
field: 'ind',
sortable: true
},
{
name: 'apri',
align: 'center',
required: false,
label: 'Apri',
field: 'apri',
sortable: false
},
{
name: 'numorder',
required: true,
align: 'left',
label: 'Ordine N.',
field: 'numorder',
sortable: true
},
{
name: 'created_at',
required: true,
align: 'center',
label: 'Effettuato il',
field: 'created_at',
sortable: true
},
{
name: 'items',
required: true,
label: '',
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>[
{
name: 'ind',
required: true,
align: 'left',
label: 'Ind',
field: 'ind',
sortable: true
},
{
name: 'apri',
align: 'center',
required: false,
label: 'Apri',
field: 'apri',
sortable: false
},
{
name: 'comandi',
align: 'center',
required: false,
label: 'Comandi',
field: 'comandi',
sortable: false
},
{
name: 'numorder',
required: true,
align: 'left',
label: 'Ordine N.',
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
},
])
watch(() => cosa.value, (newval, oldval) => {
updateorders(true)
})
watch(() => taborders.value, (newval, oldval) => {
updateorders(false)
})
watch(() => storeGasordine.value, (newval, oldval) => {
tools.setCookie(tools.COOK_SELGAS, storeGasordine.value)
updateorders(true)
})
// const { setValDb, getValDb } = MixinBase()
function getCols(props: any) {
return props.cols.filter((col: any) => col.name !== 'desc')
}
function getOrdersCart(): IOrderCart[] | undefined {
const hasGasordine: any = (cosa.value === shared_consts.PROD.TUTTI) ? undefined : (cosa.value === shared_consts.PROD.GAS)
const ris = productStore.getOrdersCart(taborders.value, hasGasordine, storeGasordine.value)
return ris
}
function getOrdersCartWithTotals(): any[] {
const orderscart = getOrdersCart();
if (orderscart && taborders.value > 0) {
// Calculate totals
const totals = {
created_at: new Date(),
items: null,
user: {
name: 'TOTALI',
surname: '',
email: '',
},
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),
};
let rowsWithTotals: any = [...orderscart];
if (tools.isManager()) {
// Combine orders and totals
rowsWithTotals.push(totals);
}
// Supponendo che rowsWithTotals sia il tuo array di record
rowsWithTotals.forEach((record: any, index: number) => {
record.ind = index;
});
return rowsWithTotals;
} else {
return []
}
}
function getAllOrdersCart() {
return productStore.getOrdersAllCart(storeGasordine.value)
}
function updateorders(updatetab: boolean) {
arrout.value = []
myorderscart.value = getOrdersCart()
if (myorderscart.value) {
for (const orderscart of myorderscart.value) {
myarrrec.value[orderscart._id] = Object.keys(orderscart)
}
}
if (updatetab) {
const allorders = getAllOrdersCart()
for (const status of [15, 2, 3, 4, 5, 6, 7, 8, 10]) {
arrnumstatus.value[status] = allorders.filter((rec) => (rec.status === status)).reduce((sum, item) => sum + 1, 0)
}
selectfirstavailable()
}
arrout.value = getOrdersCartWithTotals()
}
function selectfirstavailable() {
taborders.value = -1
Object.keys(arrnumstatus.value).forEach((stat: any) => {
if (arrnumstatus.value[stat] > 0) {
if (taborders.value === -1)
taborders.value = parseInt(stat)
}
})
}
async function mounted() {
taborders.value = -1
storeGasordine.value = tools.getCookie(tools.COOK_SELGAS, '')
await userStore.newsletterload(false)
await productStore.loadProducts()
updateorders(true)
if (taborders.value === -1)
taborders.value = shared_consts.OrderStat.IN_CORSO.value
if (tools.isManager()) {
columns.value = [...columns_Admin.value]
}
endload.value = true
}
function CanBeShipped() {
return productStore.cart.items!.filter((rec) => rec.order.product!.canBeShipped).length
}
function CanBeBuyOnline() {
return productStore.cart.items!.filter((rec) => rec.order.product!.canBeBuyOnline).length
}
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 nextstep() {
return 0
}
function clickFunz(order: IOrderCart, status: number) {
if (status === shared_consts.OrderStatus.ORDER_CONFIRMED) {
// Conferma Ordine
}
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 () => {
statusnow.value = await productStore.UpdateOrderCartStatus({ order_id: order._id, status })
if (statusnow.value === status) {
order.status = statusnow.value
updateorders(true)
tools.showPositiveNotif($q, 'Ordine ' + statusStr)
}
// change_field('status')
// change_field('status')
})
}
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'))
}
}
}
onMounted(mounted)
return {
userStore,
costanti,
tools,
toolsext,
shared_consts,
globalStore,
columns,
taborders,
arrnumstatus,
clickFunz,
getOrdersCart,
getAllOrdersCart,
updateorders,
getCols,
endload,
getOrdersCartWithTotals,
productStore,
t,
initialPagination,
cosa,
sendMailToAll,
sendMailTest,
templemail,
fieldsTable,
storeGasordine,
arrout,
}
}
})