1175 lines
35 KiB
TypeScript
Executable File
1175 lines
35 KiB
TypeScript
Executable File
import { IBaseOrder, ICart, IOrder, IOrderCart, IProduct, IProductsState, IProductInfo, ICatProd, IUserShort, IGasordine } from 'model'
|
|
|
|
import { Api } from '@api'
|
|
import { serv_constants } from '@src/store/Modules/serv_constants'
|
|
import * as Types from '@src/store/Api/ApiTypes'
|
|
import { static_data } from '@src/db/static_data'
|
|
import { shared_consts } from '@src/common/shared_vuejs'
|
|
import { tools } from '@store/Modules/tools'
|
|
import { defineStore } from 'pinia'
|
|
import { useUserStore } from '@store/UserStore'
|
|
import { toolsext } from '@store/Modules/toolsext'
|
|
import { useGlobalStore } from './globalStore'
|
|
import { ref } from 'vue'
|
|
|
|
import translate from '@src/globalroutines/util'
|
|
|
|
function getRecordOrdersCartEmpty(): IOrderCart {
|
|
return {
|
|
numorder: 0,
|
|
numord_pers: 0,
|
|
userId: '',
|
|
user: null,
|
|
totalQty: 0,
|
|
totalPrice: 0,
|
|
totalPriceCalc: 0,
|
|
status: 0,
|
|
confermato: false,
|
|
consegnato: false,
|
|
pagato: false,
|
|
spedito: false,
|
|
ricevuto: false,
|
|
note: '',
|
|
note_per_gestore: '',
|
|
note_per_admin: '',
|
|
}
|
|
}
|
|
|
|
function getRecordProductInfoEmpty(): IProductInfo {
|
|
return {
|
|
code: '',
|
|
name: '',
|
|
description: '',
|
|
department: '',
|
|
catprods: [],
|
|
subcatprods: [],
|
|
color: '',
|
|
size: '',
|
|
weight: 0,
|
|
unit: 0,
|
|
stars: 0,
|
|
date: tools.getDateNow(),
|
|
icon: '',
|
|
img: '',
|
|
}
|
|
}
|
|
|
|
function getRecordProductEmpty(): IProduct {
|
|
|
|
const tomorrow = tools.getDateNow()
|
|
tomorrow.setDate(tomorrow.getDate() + 1)
|
|
|
|
return {
|
|
productInfo: getRecordProductInfoEmpty(),
|
|
// _id: tools.getDateNow().toISOString(), // Create NEW
|
|
active: false,
|
|
idProducer: '',
|
|
idStorehouses: [],
|
|
idGasordine: '',
|
|
idScontisticas: [],
|
|
scontisticas: [],
|
|
idProvider: '',
|
|
producer: {},
|
|
gasordine: null,
|
|
storehouses: [],
|
|
provider: {},
|
|
price: 0.0,
|
|
quantityAvailable: 0,
|
|
bookableAvailableQty: 0,
|
|
minBuyQty: 1,
|
|
minStepQty: 1,
|
|
maxBookableSinglePersQty: 0,
|
|
|
|
stockQty: 0,
|
|
stockBloccatiQty: 0,
|
|
bookedQtyOrdered: 0,
|
|
bookedQtyConfirmed: 0,
|
|
|
|
qtyToReachForGas: 0,
|
|
|
|
maxbookableGASQty: 0,
|
|
bookedGASQtyOrdered: 0,
|
|
bookedGASQtyConfirmed: 0,
|
|
bookableGASBloccatiQty: 0,
|
|
|
|
canBeShipped: false,
|
|
QuantitaOrdinateInAttesa: 0,
|
|
QuantitaPrenotateInAttesa: 0,
|
|
canBeBuyOnline: false,
|
|
}
|
|
}
|
|
|
|
export const useProducts = defineStore('Products', {
|
|
state: (): IProductsState => ({
|
|
products: [],
|
|
cart: { items: [], totalPrice: 0, totalQty: 0, userId: '' },
|
|
orders: [],
|
|
catprods: [],
|
|
catprods_gas: [],
|
|
subcatprods: [],
|
|
productInfos: [],
|
|
userActive: { username: '', name: '', surname: '', _id: '' },
|
|
}),
|
|
|
|
getters: {
|
|
getCatProds: (state: IProductsState) => (cosa: number): ICatProd[] => {
|
|
if (cosa === shared_consts.PROD.GAS)
|
|
return state.catprods_gas
|
|
else if (cosa === shared_consts.PROD.GAS)
|
|
return state.catprods
|
|
else
|
|
return [...state.catprods, ...state.catprods_gas]
|
|
|
|
},
|
|
|
|
getNumProdTot: (state: IProductsState) => (): number => {
|
|
return state.products.length
|
|
},
|
|
|
|
getProducts: (state: IProductsState) => (cosa?: number): IProduct[] => {
|
|
if (!!cosa) {
|
|
return state.products.filter((rec: IProduct) => {
|
|
const hasGasOrdines = rec.idGasordine
|
|
|
|
if ((cosa === shared_consts.PROD.GAS && hasGasOrdines) ||
|
|
(cosa === shared_consts.PROD.BOTTEGA && (!hasGasOrdines))) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
} else {
|
|
return state.products;
|
|
}
|
|
},
|
|
|
|
updateDataProduct: (state: IProductsState) => (res: any) => {
|
|
if (res && res.data.product) {
|
|
// Update product from server
|
|
const indelem = state.products.findIndex((prod: IProduct) => prod._id === res.data.product._id)
|
|
if (indelem >= 0) {
|
|
state.products[indelem] = { ...res.data.product }
|
|
|
|
/*if (!res.data.orders) {
|
|
// aggiorna anche tutti i product negli ordini !
|
|
let ordcart: IOrderCart
|
|
for (ordcart of state.orders) {
|
|
for (const item of ordcart.items!) {
|
|
if (item.order.idProduct === res.data.product.idProduct)
|
|
item.order.product = res.data.product
|
|
}
|
|
}
|
|
}*/
|
|
}
|
|
}
|
|
if (res && res.data.orders) {
|
|
state.orders = res.data.orders
|
|
}
|
|
|
|
if (res && res.data.cart) { // console.log('RISULTANTE CATEGORIES DAL SERVER = ', res.data.categories)
|
|
state.cart = res.data.cart
|
|
}
|
|
|
|
},
|
|
|
|
getProductById: (state: IProductsState) => (id: string): IProduct => {
|
|
const prod = state.products.find((prod: IProduct) => prod._id === id)
|
|
return prod ? prod : getRecordProductEmpty()
|
|
},
|
|
|
|
getProductByCode: (state: IProductsState) => (code: string): IProduct => {
|
|
if (!code) {
|
|
return getRecordProductEmpty()
|
|
}
|
|
|
|
const prod = state.products.find((prod: IProduct) => {
|
|
if (prod.productInfo.code === code)
|
|
return prod
|
|
else
|
|
return null
|
|
})
|
|
return prod ? prod : getRecordProductEmpty()
|
|
},
|
|
|
|
getCart: (state: IProductsState) => (): ICart => {
|
|
return state.cart
|
|
},
|
|
|
|
getOrdersAllCart: (state: IProductsState) => (idGasordine: string): IOrderCart[] => {
|
|
return state.orders.filter((rec: IOrderCart) => {
|
|
if ((idGasordine && !rec.items?.some(item => item.order && item.order.idGasordine === idGasordine))) {
|
|
return false; // Skip records not matching gasordine condition
|
|
}
|
|
return true
|
|
})
|
|
},
|
|
|
|
getNumOrders: (state: IProductsState) => (): number => {
|
|
return state.orders.length
|
|
},
|
|
|
|
getOrdersCart: (state: IProductsState) => (tipoord: number, hasGasordine: any, idGasordine: string): IOrderCart[] | undefined => {
|
|
return state.orders.filter((rec: IOrderCart) => {
|
|
if (idGasordine && !rec.items?.some(item => item.order && item.order.idGasordine === idGasordine)) {
|
|
return false; // Skip records not matching gasordine condition
|
|
}
|
|
|
|
if (hasGasordine && !rec.items?.some(item => item.order && item.order.idGasordine)) {
|
|
return false; // Skip records not matching gasordine condition
|
|
}
|
|
|
|
switch (tipoord) {
|
|
case shared_consts.OrderStat.IN_CORSO.value:
|
|
return !rec.status || rec.status <= shared_consts.OrderStatus.CHECKOUT_SENT;
|
|
case shared_consts.OrderStat.PREPARED.value:
|
|
return rec.status === shared_consts.OrderStatus.PREPARED;
|
|
case shared_consts.OrderStat.CONFERMATI.value:
|
|
return rec.status === shared_consts.OrderStatus.ORDER_CONFIRMED;
|
|
case shared_consts.OrderStat.PAGATI.value:
|
|
return rec.status === shared_consts.OrderStatus.PAYED;
|
|
case shared_consts.OrderStat.DELIVERED.value:
|
|
return rec.status === shared_consts.OrderStatus.DELIVERED;
|
|
case shared_consts.OrderStat.SHIPPED.value:
|
|
return rec.status === shared_consts.OrderStatus.SHIPPED;
|
|
case shared_consts.OrderStat.RECEIVED.value:
|
|
return rec.status === shared_consts.OrderStatus.RECEIVED;
|
|
case shared_consts.OrderStat.COMPLETATI.value:
|
|
return rec.status === shared_consts.OrderStatus.COMPLETED;
|
|
case shared_consts.OrderStat.CANCELLATI.value:
|
|
return rec.status === shared_consts.OrderStatus.CANCELED;
|
|
default:
|
|
return false; // Invalid tipoord
|
|
}
|
|
});
|
|
},
|
|
|
|
existProductInCart: (state: IProductsState) => (idproduct: string): boolean => {
|
|
// console.log('.cart.items', this.cart.items)
|
|
if (state.cart.items) {
|
|
const ris = state.cart.items.filter((item: IBaseOrder) => item.order.idProduct === idproduct).reduce((sum, rec) => sum + 1, 0)
|
|
return ris > 0
|
|
}
|
|
return false
|
|
},
|
|
getOrderProductInCart: (state: IProductsState) => (idproduct: string): IOrder | null => {
|
|
// console.log('.cart.items', this.cart.items)
|
|
if (state.cart.items) {
|
|
const ris = state.cart.items.find((item: IBaseOrder) => item.order ? (item.order.idProduct === idproduct) : false)
|
|
return ris ? ris.order : null
|
|
}
|
|
return null
|
|
},
|
|
|
|
getOrderProductInOrdersCart: (state: IProductsState) => (idordercart: string, idproduct: string): IOrder | null => {
|
|
// console.log('.cart.items', this.cart.items)
|
|
if (state.orders) {
|
|
const orderscart = state.orders.find((rec: IOrderCart) => rec._id === idordercart)
|
|
if (orderscart) {
|
|
const ris = orderscart.items!.find((item: IBaseOrder) => item.order.idProduct === idproduct)
|
|
return ris ? ris.order : null
|
|
}
|
|
}
|
|
return null
|
|
},
|
|
|
|
getOrdersCartById: (state: IProductsState) => (idordercart: string): IOrderCart => {
|
|
if (state.orders) {
|
|
const orderscart = state.orders.find((rec: IOrderCart) => rec._id === idordercart)
|
|
return orderscart ?? getRecordOrdersCartEmpty()
|
|
}
|
|
return getRecordOrdersCartEmpty()
|
|
},
|
|
|
|
getSumQtyPreOrderInOrdersCart: (state: IProductsState) => (idproduct: string): number => {
|
|
let totalQuantity = 0;
|
|
|
|
if (state.orders) {
|
|
const orderscart = state.orders
|
|
if (orderscart) {
|
|
for (const myord of orderscart) {
|
|
if (myord.items) {
|
|
for (const item of myord.items) {
|
|
if (item.order) {
|
|
if ((item.order.idProduct === idproduct) && (item.order.status! < shared_consts.OrderStatus.CHECKOUT_SENT)) {
|
|
totalQuantity += (item.order.quantitypreordered) || 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return totalQuantity
|
|
},
|
|
|
|
getSumQtyOrderProductInOrdersCart: (state: IProductsState) => (idproduct: string): number => {
|
|
let totalQuantity = 0;
|
|
|
|
if (state.orders) {
|
|
const orderscart = state.orders
|
|
if (orderscart) {
|
|
for (const myord of orderscart) {
|
|
if (myord.items) {
|
|
for (const item of myord.items) {
|
|
if (item.order) {
|
|
if ((item.order.idProduct === idproduct) && (item.order.status! < shared_consts.OrderStatus.CHECKOUT_SENT)) {
|
|
totalQuantity += (item.order.quantity) || 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return totalQuantity
|
|
},
|
|
|
|
getOrdersCartByIdProduct: (state: IProductsState) => (idproduct: string): IOrderCart[] | [] => {
|
|
try {
|
|
if (state.orders) {
|
|
const ris = state.orders.filter((orderscart: IOrderCart) => {
|
|
return orderscart.items!.some(item => {
|
|
if (item.order)
|
|
return (item.order.idProduct === idproduct)
|
|
&& (item.order.status! < shared_consts.OrderStatus.CHECKOUT_SENT)
|
|
})
|
|
})
|
|
// console.log('Ordini ', ris)
|
|
return ris ? ris : []
|
|
}
|
|
} catch (e) {
|
|
console.error('Err', e)
|
|
}
|
|
return []
|
|
},
|
|
|
|
getOrdersCartInAttesaByIdProduct: (state: IProductsState) => (idproduct: string): IOrderCart[] | [] => {
|
|
try {
|
|
if (state.orders) {
|
|
const ris = state.orders.filter((orderscart: IOrderCart) => {
|
|
return orderscart.items!.some(item => {
|
|
if (item.order)
|
|
return (item.order.idProduct === idproduct)
|
|
&& (item.order.status! <= shared_consts.OrderStatus.CHECKOUT_SENT)
|
|
})
|
|
})
|
|
// console.log('Ordini ', ris)
|
|
return ris ? ris : []
|
|
}
|
|
} catch (e) {
|
|
console.error('Err', e)
|
|
}
|
|
return []
|
|
},
|
|
|
|
|
|
getRecordEmpty: (state: IProductsState) => (): IProduct => {
|
|
return getRecordProductEmpty()
|
|
},
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
createOrderByProduct(product: IProduct, order: IOrder): IOrder {
|
|
const userStore = useUserStore()
|
|
const myorder: IOrder = {
|
|
userId: this.userActive._id,
|
|
idapp: process.env.APP_ID,
|
|
status: shared_consts.OrderStatus.IN_CART,
|
|
TotalPriceProduct: 0,
|
|
TotalPriceProductCalc: 0,
|
|
idProduct: product._id,
|
|
product, // Copia tutto l'oggetto Product !
|
|
|
|
// Ordine:
|
|
price: product.price,
|
|
after_price: product.after_price,
|
|
quantity: order.quantity,
|
|
quantitypreordered: order.quantitypreordered,
|
|
idStorehouse: order.idStorehouse,
|
|
idGasordine: order.idGasordine,
|
|
//++Add Fields
|
|
}
|
|
|
|
return myorder
|
|
},
|
|
|
|
initcat() {
|
|
|
|
// rec.userId = this.userActive._id
|
|
|
|
return this.getRecordEmpty()
|
|
|
|
},
|
|
|
|
/*resetProducts() {
|
|
const arrprod = [...this.products]
|
|
this.products = []
|
|
this.products = [...arrprod]
|
|
},*/
|
|
|
|
async loadProducts() {
|
|
|
|
const userStore = useUserStore()
|
|
const globalStore = useGlobalStore()
|
|
|
|
//console.log('loadProducts')
|
|
|
|
if (!globalStore.site.confpages.enableEcommerce)
|
|
return null
|
|
|
|
// console.log('getProducts', 'userid=', this.userActive._id)
|
|
|
|
// if (this.userActive._id === '') {
|
|
// return new Types.AxiosError(0, null, 0, '')
|
|
// }
|
|
|
|
let ris = null
|
|
|
|
let myIdActiveSelected = userStore.my._id
|
|
if (tools.isSeller())
|
|
myIdActiveSelected = tools.getCookie(tools.COOK_SELCART, userStore.my._id, false)
|
|
|
|
const trovato = userStore.usersList.find((user: IUserShort) => user._id === myIdActiveSelected)
|
|
if (trovato)
|
|
this.userActive = trovato
|
|
else
|
|
this.userActive = userStore.my
|
|
|
|
console.log('userActive', this.userActive)
|
|
|
|
ris = await Api.SendReq('/products', 'POST', { userId: this.userActive._id })
|
|
.then((res) => {
|
|
if (res.data.products) {
|
|
// console.log('aggiorna prodotti')
|
|
this.products = []
|
|
this.products = res.data.products
|
|
} else {
|
|
this.products = []
|
|
}
|
|
|
|
if (res.data.orders) {
|
|
this.orders = []
|
|
this.orders = res.data.orders
|
|
} else {
|
|
this.orders = []
|
|
}
|
|
|
|
if (process.env.DEBUG === '1') {
|
|
}
|
|
|
|
return res
|
|
})
|
|
.catch((error) => {
|
|
console.log('error getProducts', error)
|
|
userStore.setErrorCatch(error)
|
|
return new Types.AxiosError(serv_constants.RIS_CODE_ERR, null, toolsext.ERR_GENERICO, error)
|
|
})
|
|
|
|
// ApiTables.aftercalling(ris, checkPending, 'categories')
|
|
|
|
return ris
|
|
},
|
|
|
|
async updateOrderByOrder(idOrdersCart: string, idOrder: string, paramstoupdate: any) {
|
|
|
|
const userStore = useUserStore()
|
|
const globalStore = useGlobalStore()
|
|
|
|
// console.log('loadProducts')
|
|
|
|
if (!globalStore.site.confpages.enableEcommerce)
|
|
return null
|
|
|
|
console.log('updateOrderByOrder', 'userid=', this.userActive._id)
|
|
|
|
let ris = null
|
|
|
|
ris = await Api.SendReq('/orders/updateord', 'POST', { idOrdersCart, idOrder, paramstoupdate })
|
|
.then((res) => {
|
|
let myorderscart = res.data.orderscart
|
|
if (res) {
|
|
myorderscart = res.data && res.data.orderscart ? res.data.orderscart : null
|
|
if (myorderscart) {
|
|
const idord = this.orders.findIndex((ord: IOrderCart) => ord._id === myorderscart._id)
|
|
if (idord >= 0) {
|
|
console.log('aggiorna ordine')
|
|
this.orders[idord] = { ...myorderscart }
|
|
}
|
|
}
|
|
}
|
|
return myorderscart
|
|
})
|
|
.catch((error) => {
|
|
console.log('error updateOrderByOrder', error)
|
|
userStore.setErrorCatch(error)
|
|
return new Types.AxiosError(serv_constants.RIS_CODE_ERR, null, toolsext.ERR_GENERICO, error)
|
|
})
|
|
|
|
// ApiTables.aftercalling(ris, checkPending, 'categories')
|
|
|
|
return ris
|
|
},
|
|
|
|
async updateOrdersCartById(idOrdersCart: string, paramstoupdate: any) {
|
|
|
|
const userStore = useUserStore()
|
|
const globalStore = useGlobalStore()
|
|
|
|
// console.log('loadProducts')
|
|
|
|
if (!globalStore.site.confpages.enableEcommerce)
|
|
return null
|
|
|
|
console.log('updateOrdersCartById', 'userid=', this.userActive._id)
|
|
|
|
let ris = null
|
|
|
|
ris = await Api.SendReq('/orders/update', 'POST', { idOrdersCart, paramstoupdate })
|
|
.then((res) => {
|
|
let myorderscart = res.data.orderscart
|
|
if (res) {
|
|
if (myorderscart) {
|
|
const idord = this.orders.findIndex((ord: IOrderCart) => ord._id === myorderscart._id)
|
|
if (idord >= 0) {
|
|
console.log('aggiorna orderscart')
|
|
this.orders[idord] = myorderscart
|
|
}
|
|
}
|
|
}
|
|
return myorderscart
|
|
})
|
|
.catch((error) => {
|
|
console.log('error updateOrderByOrder', error)
|
|
userStore.setErrorCatch(error)
|
|
return new Types.AxiosError(serv_constants.RIS_CODE_ERR, null, toolsext.ERR_GENERICO, error)
|
|
})
|
|
|
|
// ApiTables.aftercalling(ris, checkPending, 'categories')
|
|
|
|
return ris
|
|
},
|
|
|
|
async loadProductById(id: string) {
|
|
|
|
const userStore = useUserStore()
|
|
const globalStore = useGlobalStore()
|
|
|
|
if (!globalStore.site.confpages.enableEcommerce)
|
|
return null
|
|
|
|
// if (this.userActive._id === '') {
|
|
// return new Types.AxiosError(0, null, 0, '')
|
|
// }
|
|
|
|
let ris = null
|
|
|
|
ris = await Api.SendReq('/products/id/' + id, 'GET', null)
|
|
.then((res) => {
|
|
console.log('product', res.data.product)
|
|
if (res.data.product) { // console.log('RISULTANTE CATEGORIES DAL SERVER = ', res.data.categories)
|
|
this.updateDataProduct(res)
|
|
return res.data.product
|
|
} else {
|
|
return null
|
|
}
|
|
})
|
|
.catch((error) => {
|
|
console.log('error getProduct', error)
|
|
userStore.setErrorCatch(error)
|
|
return new Types.AxiosError(serv_constants.RIS_CODE_ERR, null, toolsext.ERR_GENERICO, error)
|
|
})
|
|
|
|
return ris
|
|
},
|
|
|
|
async loadOrders() {
|
|
|
|
// console.log('loadOrders')
|
|
const userStore = useUserStore()
|
|
const globalStore = useGlobalStore()
|
|
|
|
if (!globalStore.site.confpages.enableEcommerce)
|
|
return null
|
|
|
|
if (!this.userActive._id)
|
|
return null
|
|
|
|
// if (this.userActive._id === '') {
|
|
// return new Types.AxiosError(0, null, 0, '')
|
|
// }
|
|
|
|
let ris = null
|
|
|
|
ris = await Api.SendReq('/cart/' + this.userActive._id, 'GET', null)
|
|
.then((res) => {
|
|
if (res.data && res.data.cart) { // console.log('RISULTANTE CATEGORIES DAL SERVER = ', res.data.categories)
|
|
this.cart = res.data.cart
|
|
} else {
|
|
this.cart = { items: [], totalPrice: 0, totalQty: 0, userId: '' }
|
|
}
|
|
|
|
this.updateDataProduct(res)
|
|
|
|
return res
|
|
})
|
|
.catch((error) => {
|
|
console.log('error loadOrders', error)
|
|
userStore.setErrorCatch(error)
|
|
return new Types.AxiosError(serv_constants.RIS_CODE_ERR, null, toolsext.ERR_GENERICO, error)
|
|
})
|
|
|
|
// ApiTables.aftercalling(ris, checkPending, 'categories')
|
|
return ris
|
|
},
|
|
|
|
|
|
async removeFromCart({ order }: { order: IOrder }) {
|
|
|
|
const userStore = useUserStore()
|
|
|
|
return Api.SendReq('/cart/' + this.userActive._id, 'DELETE', { orderId: order._id })
|
|
.then((res) => {
|
|
this.updateDataProduct(res)
|
|
|
|
return res
|
|
})
|
|
},
|
|
|
|
async getGestoreOrdini({ idGasordine }: { idGasordine: string }) {
|
|
const userStore = useUserStore()
|
|
|
|
let useractive = this.userActive._id
|
|
if (!useractive)
|
|
useractive = userStore.my._id;
|
|
|
|
return Api.SendReq('/cart/' + useractive + '/gestord', 'POST', { idGasordine })
|
|
.then((res) => {
|
|
if (res)
|
|
return res.data.arrout
|
|
else
|
|
return []
|
|
|
|
})
|
|
},
|
|
|
|
async addToCart({ product, order, addqty }: { product: IProduct, order: IOrder, addqty: boolean }) {
|
|
|
|
const userStore = useUserStore()
|
|
const globalStore = useGlobalStore()
|
|
|
|
if (!globalStore.site.confpages.enableEcommerce)
|
|
return null
|
|
|
|
let neworder = null;
|
|
|
|
// Controlla se esiste già nel carrello, allora semplicemente aggiungerò la quantità:
|
|
if (this.existProductInCart(product._id)) {
|
|
const ordcart = this.getOrderProductInCart(product._id)
|
|
if (ordcart) {
|
|
|
|
if (!addqty && ((ordcart.quantity + ordcart.quantitypreordered) === 1)) {
|
|
// sto per rimuovere l'ultimo pezzo, quindi cancello direttamente
|
|
const risrem = await this.removeFromCart({ order: ordcart })
|
|
|
|
if (risrem) {
|
|
order.quantity = 0
|
|
order.quantitypreordered = 0
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return await this.addSubQtyToItem({
|
|
addqty,
|
|
subqty: !addqty,
|
|
order: ordcart,
|
|
}).then((res: any) => {
|
|
if (res && res.msgerr) {
|
|
return res;
|
|
} else if (res && res.risult) {
|
|
order.quantity = res.myord.quantity
|
|
order.quantitypreordered = res.myord.quantitypreordered
|
|
}
|
|
return res;
|
|
})
|
|
}
|
|
} else {
|
|
if (this.isQtyAvailableByProduct(product)) {
|
|
order.quantity = product.minBuyQty || 1
|
|
order.quantitypreordered = 0
|
|
} else {
|
|
if (this.isInPreorderByProduct(product)) {
|
|
order.quantitypreordered = product.minBuyQty || 1
|
|
order.quantity = 0
|
|
}
|
|
}
|
|
|
|
if (!order.idStorehouse) {
|
|
if (product.storehouses.length === 1) {
|
|
order.idStorehouse = product.storehouses[0]._id
|
|
} else {
|
|
order.idStorehouse = globalStore.storehouses ? globalStore.storehouses[0]._id : ''
|
|
}
|
|
}
|
|
if (!order.idGasordine && order.quantitypreordered > 0) {
|
|
if (product.gasordine && product.gasordine._id) {
|
|
order.idGasordine = product.gasordine._id
|
|
} else {
|
|
order.idGasordine = globalStore.gasordines ? globalStore.gasordines[0]._id : ''
|
|
}
|
|
}
|
|
|
|
if (order.idStorehouse) {
|
|
neworder = this.createOrderByProduct(product, order)
|
|
}
|
|
}
|
|
|
|
// if (neworder && !neworder.idStorehouse)
|
|
// return new Types.AxiosError(serv_constants.RIS_CODE_ERR, null, toolsext.ERR_GENERICO, 'Nessuno Store')
|
|
|
|
console.log('addToCart', 'userid=', this.userActive._id, neworder)
|
|
|
|
let ris = null
|
|
|
|
ris = await Api.SendReq('/cart/' + this.userActive._id, 'POST', { order: neworder })
|
|
.then((res) => {
|
|
if (res.data.cart) { // console.log('RISULTANTE CATEGORIES DAL SERVER = ', res.data.categories)
|
|
this.cart = res.data.cart
|
|
} else {
|
|
this.cart = { items: [], totalPrice: 0, totalQty: 0, userId: '' }
|
|
}
|
|
this.updateDataProduct(res)
|
|
|
|
return { risult: !!res, myord: res.data.myord, msgerr: res.data.msgerr }
|
|
})
|
|
.catch((error) => {
|
|
console.log('error addToCart', error)
|
|
userStore.setErrorCatch(error)
|
|
return new Types.AxiosError(serv_constants.RIS_CODE_ERR, null, toolsext.ERR_GENERICO, error)
|
|
})
|
|
|
|
// ApiTables.aftercalling(ris, checkPending, 'categories')
|
|
|
|
return ris
|
|
},
|
|
|
|
async addSubQtyToItem({ addqty, subqty, order }: { addqty: boolean, subqty: boolean, order: IOrder }) {
|
|
|
|
const userStore = useUserStore()
|
|
const globalStore = useGlobalStore()
|
|
|
|
if (!globalStore.site.confpages.enableEcommerce)
|
|
return null
|
|
|
|
// console.log('addSubQtyToItem', 'userid=', this.userActive._id, order)
|
|
|
|
let ris = null
|
|
|
|
ris = await Api.SendReq('/cart/' + this.userActive._id, 'POST', { addqty, subqty, order })
|
|
.then((res: any) => {
|
|
this.updateDataProduct(res)
|
|
|
|
return { risult: !!res, myord: res.data.myord, msgerr: res.msgerr }
|
|
})
|
|
.catch((error) => {
|
|
console.log('error addSubQtyToItem', error)
|
|
userStore.setErrorCatch(error)
|
|
return new Types.AxiosError(serv_constants.RIS_CODE_ERR, null, toolsext.ERR_GENERICO, error)
|
|
})
|
|
|
|
// ApiTables.aftercalling(ris, checkPending, 'categories')
|
|
|
|
return ris
|
|
},
|
|
|
|
getOptions(sendmail: boolean) {
|
|
return {
|
|
sendmail
|
|
}
|
|
},
|
|
|
|
async CreateOrdersCart({ cart_id, status, note }: { cart_id: string, status: number, note: string }) {
|
|
|
|
const userStore = useUserStore()
|
|
const globalStore = useGlobalStore()
|
|
|
|
if (!globalStore.site.confpages.enableEcommerce)
|
|
return null
|
|
|
|
if (!this.userActive._id)
|
|
return null
|
|
|
|
let ris = null
|
|
|
|
ris = await Api.SendReq('/cart/' + this.userActive._id + '/createorderscart', 'POST', { cart_id, status, note, options: this.getOptions(true) })
|
|
.then((res) => {
|
|
|
|
if (res.data.status === shared_consts.OrderStatus.CHECKOUT_SENT) {
|
|
// Cancella il Carrello, ho creato l'ordine !
|
|
this.cart = {}
|
|
}
|
|
|
|
this.updateDataProduct(res)
|
|
|
|
return res.data.recOrderCart
|
|
})
|
|
.catch((error) => {
|
|
console.log('error UpdateStatusCart', error)
|
|
userStore.setErrorCatch(error)
|
|
return new Types.AxiosError(serv_constants.RIS_CODE_ERR, null, toolsext.ERR_GENERICO, error)
|
|
})
|
|
|
|
return ris
|
|
},
|
|
|
|
/*async UpdateStatusCart({ ordercart_id, status }: { ordercart_id: string, status: number }) {
|
|
|
|
const userStore = useUserStore()
|
|
const globalStore = useGlobalStore()
|
|
|
|
if (!globalStore.site.confpages.enableEcommerce)
|
|
return null
|
|
|
|
let ris = null
|
|
|
|
ris = await Api.SendReq('/cart/updatestatuscart', 'POST', { ordercart_id, status, options: this.getOptions() })
|
|
.then((res) => {
|
|
|
|
if (res.data.status === shared_consts.OrderStatus.CHECKOUT_SENT) {
|
|
this.cart = {}
|
|
}
|
|
this.updateDataProduct(res)
|
|
|
|
return res.data.status
|
|
})
|
|
.catch((error) => {
|
|
console.log('error UpdateStatusCart', error)
|
|
userStore.setErrorCatch(error)
|
|
return new Types.AxiosError(serv_constants.RIS_CODE_ERR, null, toolsext.ERR_GENERICO, error)
|
|
})
|
|
|
|
return ris
|
|
},*/
|
|
|
|
async UpdateOrderCartStatus({ order_id, status, sendmail }: { order_id: string, status: number, sendmail: boolean }) {
|
|
|
|
const userStore = useUserStore()
|
|
const globalStore = useGlobalStore()
|
|
|
|
if (!globalStore.site.confpages.enableEcommerce)
|
|
return null
|
|
|
|
let ris = null
|
|
|
|
ris = await Api.SendReq('/cart/' + this.userActive._id + '/ordercartstatus', 'POST', { order_id, status, options: this.getOptions(sendmail) })
|
|
.then((res) => {
|
|
|
|
this.updateDataProduct(res)
|
|
|
|
return res.data.status
|
|
|
|
})
|
|
.catch((error) => {
|
|
console.log('error UpdateOrderCartStatus', error)
|
|
userStore.setErrorCatch(error)
|
|
return new Types.AxiosError(serv_constants.RIS_CODE_ERR, null, toolsext.ERR_GENERICO, error)
|
|
})
|
|
|
|
return ris
|
|
},
|
|
async addtoCartBase({ $q, t, id, order, addqty }: { $q: any, t: any, id: string, order: IOrder, addqty: boolean }) {
|
|
let product = this.getProductById(id)
|
|
|
|
return await this.addToCart({ product, order, addqty })
|
|
.then((ris) => {
|
|
if (ris && ris.msgerr) {
|
|
tools.showNegativeNotif($q, ris.msgerr)
|
|
} else {
|
|
let strprod = t('ecomm.prodotto')
|
|
|
|
let msg = ''
|
|
console.log('ris', ris)
|
|
if (ris && ris.myord == null) {
|
|
msg = t('ecomm.prodotto_tolto')
|
|
tools.showNotif($q, msg)
|
|
return
|
|
}
|
|
if (ris === null || ris.myord == null) {
|
|
msg = t('ecomm.error_cart')
|
|
tools.showNegativeNotif($q, msg)
|
|
return
|
|
} else {
|
|
|
|
let qta = ris.myord.quantity + ris.myord.quantitypreordered
|
|
if (qta > 1 || qta === 0)
|
|
strprod = t('ecomm.prodotti')
|
|
|
|
|
|
if (qta >= 0) {
|
|
let totalPrice = this.cart.totalPrice ? this.cart.totalPrice.toFixed(2) : 0
|
|
let tot = totalPrice + ' €'
|
|
msg = t('ecomm.prod_sul_carrello', { strprod, qty: qta, tot })
|
|
}
|
|
|
|
}
|
|
if (ris === null || ris.myord.quantity === 0)
|
|
tools.showNotif($q, msg)
|
|
else
|
|
tools.showPositiveNotif($q, msg, undefined, 'prodotto')
|
|
|
|
|
|
}
|
|
//updateproduct()
|
|
|
|
return ris
|
|
})
|
|
},
|
|
|
|
getQuantityByOrder($t: any, order: IOrder): string {
|
|
let mystr = '';
|
|
if (order.quantity > 0) {
|
|
mystr += order.quantity
|
|
}
|
|
if ((order.quantitypreordered > 0) && (order.quantity > 0)) {
|
|
mystr += ' ' + $t('ecomm.available')
|
|
mystr += ' + '
|
|
}
|
|
if (order.quantitypreordered > 0) {
|
|
mystr += ' ' + order.quantitypreordered + ' ' + $t('ecomm.preord');
|
|
}
|
|
return mystr
|
|
},
|
|
|
|
isQtyAvailableByProduct(product: IProduct): boolean {
|
|
if (product) {
|
|
return (product.quantityAvailable! > 0)
|
|
}
|
|
return false;
|
|
},
|
|
|
|
isInPreorderByProduct(product: IProduct): boolean {
|
|
if (product) {
|
|
return (product.bookableAvailableQty! > 0)
|
|
}
|
|
return false;
|
|
},
|
|
|
|
isQtyAvailableByOrder(order: IOrder): boolean {
|
|
if (order && order.product) {
|
|
return this.isQtyAvailableByProduct(order.product)
|
|
}
|
|
return false;
|
|
},
|
|
|
|
isInPreorderByOrder(order: IOrder): boolean {
|
|
if (order && order.product) {
|
|
return this.isInPreorderByProduct(order.product)
|
|
}
|
|
return false;
|
|
},
|
|
|
|
getSingleGasordine(order: IOrder, short: boolean): string {
|
|
try {
|
|
const mygas = order.gasordine
|
|
if (mygas) {
|
|
if (short)
|
|
return mygas.name!
|
|
else
|
|
return mygas.name + ' (' + mygas.city + ') ' + translate('gas.dataora_chiusura_ordini') + ': ' + tools.getstrDateShort(mygas.dataora_chiusura_ordini)
|
|
+ ' ' + translate('gas.data_arrivo_merce') + ': ' + tools.getstrDateShort(mygas.data_arrivo_merce)
|
|
+ ' ' + translate('gas.dataora_ritiro') + ': ' + tools.getstrDateShort(mygas.dataora_ritiro)
|
|
} else
|
|
return ''
|
|
} catch (e) {
|
|
return ''
|
|
}
|
|
},
|
|
|
|
getQtyAvailable(myproduct: IProduct): number {
|
|
let qty = myproduct.quantityAvailable!
|
|
return qty
|
|
},
|
|
|
|
getQtyBookableAvailable(myproduct: IProduct): number {
|
|
let qty = myproduct.bookableAvailableQty!
|
|
return qty
|
|
},
|
|
|
|
getQtyBloccataAvailable(myproduct: IProduct): number {
|
|
let qty = myproduct.stockBloccatiQty
|
|
return qty
|
|
},
|
|
|
|
getQtyBloccataBookableAvailable(myproduct: IProduct): number {
|
|
let qty = myproduct.bookableGASBloccatiQty
|
|
return qty
|
|
},
|
|
|
|
enableSubQty(myorder: IOrder): boolean {
|
|
let qty = myorder.quantity + myorder.quantitypreordered
|
|
return qty ? qty > 0 : false
|
|
},
|
|
|
|
CanDeleteIfSub(myorder: IOrder): boolean {
|
|
let qty = myorder.quantity + myorder.quantitypreordered
|
|
|
|
qty = qty - this.qtaNextSub(myorder, myorder.product!)
|
|
return qty === 0
|
|
},
|
|
|
|
enableAddQty(myorder: IOrder, myproduct: IProduct): boolean {
|
|
const globalStore = useGlobalStore()
|
|
if (globalStore.site.ecomm && globalStore.site.ecomm.enablePreOrders) {
|
|
return ((this.getQtyBookableAvailable(myproduct) > 0)
|
|
&& (myproduct.maxBookableSinglePersQty === 0
|
|
|| (myorder.quantitypreordered + 1 < myproduct.maxBookableSinglePersQty))
|
|
)
|
|
|| (this.getQtyAvailable(myproduct) > 0)
|
|
&& (myproduct.maxBookableSinglePersQty === 0
|
|
|| (myorder.quantity + 1 < myproduct.maxBookableSinglePersQty))
|
|
|
|
} else {
|
|
return (this.getQtyAvailable(myproduct) > 0)
|
|
&& (myproduct.maxBookableSinglePersQty === 0
|
|
|| (myorder.quantity + 1 < myproduct.maxBookableSinglePersQty))
|
|
}
|
|
|
|
},
|
|
|
|
qtaNextAdd(myorder: IOrder, myproduct: IProduct): number {
|
|
let step = myproduct.minStepQty
|
|
if (this.getQtyAvailable(myproduct) > 0) {
|
|
if (myorder.quantity === 0)
|
|
step = myproduct.minBuyQty
|
|
} else {
|
|
if (myorder.quantitypreordered === 0)
|
|
step = myproduct.minBuyQty
|
|
}
|
|
|
|
if (step === 0) {
|
|
step = 1
|
|
}
|
|
|
|
return step
|
|
},
|
|
|
|
qtaNextSub(myorder: IOrder, myproduct: IProduct) {
|
|
let step = myproduct.minStepQty
|
|
let minqta = myproduct.minBuyQty
|
|
if (this.getQtyAvailable(myproduct) > 0) {
|
|
if (myorder.quantity === minqta)
|
|
step = minqta
|
|
} else {
|
|
if (myorder.quantitypreordered === minqta)
|
|
step = minqta
|
|
}
|
|
|
|
return step
|
|
},
|
|
|
|
getNumQtaGas() {
|
|
const arrprod = this.getProducts(shared_consts.PROD.GAS)
|
|
return arrprod.length
|
|
},
|
|
|
|
getNumQtaTutti() {
|
|
const arrprod = this.getProducts(shared_consts.PROD.TUTTI)
|
|
return arrprod.length
|
|
},
|
|
|
|
getNumQtaBottega() {
|
|
const arrprod = this.getProducts(shared_consts.PROD.BOTTEGA)
|
|
return arrprod.length
|
|
},
|
|
|
|
changeuserActive(valuerec: any) {
|
|
if (valuerec) {
|
|
console.log('valuerec', valuerec)
|
|
tools.setCookie(tools.COOK_SELCART, valuerec._id)
|
|
this.products = []
|
|
this.orders = []
|
|
this.cart = { items: [], totalPrice: 0, totalQty: 0, userId: '' }
|
|
|
|
this.loadOrders()
|
|
this.loadProducts()
|
|
}
|
|
},
|
|
|
|
async sendMailToTheBuyer(idOrdersCart: string, templemail_id: string, test: boolean) {
|
|
return await Api.SendReq('/orders/sendmail', 'POST', { templemail_id, idOrdersCart, previewonly: tools.isDebug(), test })
|
|
.then((res) => {
|
|
console.log('res', res)
|
|
if (res)
|
|
return res.data
|
|
else
|
|
return null
|
|
})
|
|
},
|
|
|
|
|
|
getStrInfoOrder(t: any, order: IOrder) {
|
|
let mystr = ''
|
|
let qtyrisult: any
|
|
|
|
if (order.product) {
|
|
let qtystr = ''
|
|
let qtynum = 0
|
|
if (order.quantity > 0)
|
|
qtynum += order.quantity;
|
|
if (order.quantitypreordered > 0)
|
|
qtynum += order.quantitypreordered;
|
|
|
|
if (order.product.productInfo.sfuso && order.product.productInfo.weight)
|
|
qtyrisult = qtynum * order.product.productInfo.weight;
|
|
else
|
|
qtyrisult = qtynum + ' x ' + order.product.productInfo.weight;
|
|
|
|
qtystr += qtyrisult + ' ' + tools.getUnitsMeasure(order.product.productInfo.unit, true, order.product.productInfo.weight);
|
|
|
|
if (order.quantitypreordered > 0)
|
|
qtystr += ' Pre-Ordinati';
|
|
|
|
mystr += '✅ [' + qtystr + '] ' + order.product.productInfo.name + ' a ' + order.price + '€ ' + (order.after_price ? order.after_price : '') + '<br>Totale = ' + order.TotalPriceProduct + '€';
|
|
|
|
}
|
|
return mystr
|
|
},
|
|
|
|
getGasordines() {
|
|
const globalStore = useGlobalStore()
|
|
|
|
const myarr: any = [{
|
|
id: 0,
|
|
label: 'Tutti',
|
|
value: ''
|
|
}]
|
|
let ind = 1
|
|
globalStore.gasordines.forEach((gasordine: IGasordine) => {
|
|
myarr.push(
|
|
{
|
|
id: ind,
|
|
label: gasordine.name,
|
|
value: gasordine._id
|
|
})
|
|
|
|
ind++
|
|
})
|
|
|
|
return myarr
|
|
},
|
|
|
|
getGasordinesActives() {
|
|
const globalStore = useGlobalStore()
|
|
|
|
return globalStore.gasordines.filter((rec) => rec.active)
|
|
},
|
|
|
|
|
|
},
|
|
|
|
})
|
|
|