568 lines
18 KiB
TypeScript
Executable File
568 lines
18 KiB
TypeScript
Executable File
import { IBaseOrder, ICart, IOrder, IOrderCart, IProduct, IProductsState } 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'
|
|
|
|
export const useProducts = defineStore('Products', {
|
|
state: (): IProductsState => ({
|
|
products: [],
|
|
cart: { items: [], totalPrice: 0, totalQty: 0, userId: '' },
|
|
orders: [],
|
|
}),
|
|
|
|
getters: {
|
|
getProducts: (state: IProductsState) => (): IProduct[] => {
|
|
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 && 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
|
|
}
|
|
|
|
},
|
|
|
|
getProduct: (state: IProductsState) => (code: string): IProduct => {
|
|
const prod = state.products.find((prod: IProduct) => prod.code === code)
|
|
return prod ? prod : { active: false, img: '', code: '', name: '', storehouses: [], scontisticas: [] }
|
|
},
|
|
|
|
getCart: (state: IProductsState) => (): ICart => {
|
|
return state.cart
|
|
},
|
|
|
|
getOrdersAllCart: (state: IProductsState) => (): IOrderCart[] => {
|
|
return state.orders
|
|
},
|
|
|
|
getOrdersCart: (state: IProductsState) => (tipoord: number): IOrderCart[] | undefined => {
|
|
console.log('state.orders', state.orders)
|
|
if (tipoord === shared_consts.OrderStat.IN_CORSO.value)
|
|
return state.orders.filter((rec: IOrderCart) => (rec.status ? rec.status : 0) <= shared_consts.OrderStatus.CHECKOUT_SENT)
|
|
else if (tipoord === shared_consts.OrderStat.CONFERMATI.value)
|
|
return state.orders.filter((rec: IOrderCart) => rec.status === shared_consts.OrderStatus.ORDER_CONFIRMED)
|
|
else if (tipoord === shared_consts.OrderStat.PAGATI.value)
|
|
return state.orders.filter((rec: IOrderCart) => rec.status === shared_consts.OrderStatus.PAYED)
|
|
else if (tipoord === shared_consts.OrderStat.DELIVERED.value)
|
|
return state.orders.filter((rec: IOrderCart) => rec.status === shared_consts.OrderStatus.DELIVERED)
|
|
else if (tipoord === shared_consts.OrderStat.SHIPPED.value)
|
|
return state.orders.filter((rec: IOrderCart) => rec.status === shared_consts.OrderStatus.SHIPPED)
|
|
else if (tipoord === shared_consts.OrderStat.RECEIVED.value)
|
|
return state.orders.filter((rec: IOrderCart) => rec.status === shared_consts.OrderStatus.RECEIVED)
|
|
else if (tipoord === shared_consts.OrderStat.COMPLETATI.value)
|
|
return state.orders.filter((rec: IOrderCart) => rec.status === shared_consts.OrderStatus.COMPLETED)
|
|
else if (tipoord === shared_consts.OrderStat.CANCELLATI.value)
|
|
return state.orders.filter((rec: IOrderCart) => rec.status === shared_consts.OrderStatus.CANCELED)
|
|
},
|
|
|
|
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.idProduct === idproduct)
|
|
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
|
|
},
|
|
|
|
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) {
|
|
totalQuantity += item.order.quantity || 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return totalQuantity
|
|
},
|
|
|
|
getOrdersCartByIdProduct: (state: IProductsState) => (idproduct: string): IOrderCart[] | [] => {
|
|
console.log('getOrdersCartByIdProduct')
|
|
// console.log('.cart.items', this.cart.items)
|
|
try {
|
|
if (state.orders) {
|
|
const ris = state.orders.filter((ordercart: IOrderCart) => {
|
|
return ordercart.items!.some(item => {
|
|
if (item.order)
|
|
return item.order.idProduct === idproduct
|
|
})
|
|
})
|
|
console.log('Ordini ', ris)
|
|
return ris ? ris : []
|
|
}
|
|
} catch (e) {
|
|
console.error('Err', e)
|
|
}
|
|
return []
|
|
},
|
|
|
|
updateQuantityAvailable: (state: IProductsState) => (id: string): any => {
|
|
|
|
const indelem = state.products.findIndex((prod: IProduct) => prod._id === id)
|
|
if (indelem >= 0) {
|
|
state.products[indelem].quantityAvailable = state.products[indelem].stockQty
|
|
if (state.products[indelem].QuantitaOrdinateInAttesa! > 0) {
|
|
state.products[indelem].quantityAvailable! -= state.products[indelem].QuantitaOrdinateInAttesa!
|
|
}
|
|
}
|
|
|
|
},
|
|
getRecordEmpty: (state: IProductsState) => (): IProduct => {
|
|
|
|
const tomorrow = tools.getDateNow()
|
|
tomorrow.setDate(tomorrow.getDate() + 1)
|
|
|
|
return {
|
|
// _id: tools.getDateNow().toISOString(), // Create NEW
|
|
active: false,
|
|
idProducer: '',
|
|
idStorehouses: [],
|
|
idScontisticas: [],
|
|
scontisticas: [],
|
|
idProvider: '',
|
|
producer: {},
|
|
storehouses: [],
|
|
provider: {},
|
|
code: '',
|
|
name: '',
|
|
description: '',
|
|
department: '',
|
|
category: '',
|
|
price: 0.0,
|
|
color: '',
|
|
size: '',
|
|
quantityAvailable: 0,
|
|
stockQty: 0,
|
|
canBeShipped: false,
|
|
QuantitaOrdinateInAttesa: 0,
|
|
canBeBuyOnline: false,
|
|
weight: 0,
|
|
unit: 0,
|
|
stars: 0,
|
|
date: tools.getDateNow(),
|
|
icon: '',
|
|
img: '',
|
|
}
|
|
},
|
|
|
|
},
|
|
|
|
actions: {
|
|
getProductsByCategory(category: string): any[] {
|
|
return this.products.filter((rec) => rec.category === category)
|
|
},
|
|
|
|
createOrderByProduct(product: IProduct, order: IOrder): IOrder {
|
|
const userStore = useUserStore()
|
|
const myorder: IOrder = {
|
|
userId: userStore.my._id,
|
|
idapp: process.env.APP_ID,
|
|
idProduct: product._id,
|
|
idProducer: product.idProducer,
|
|
status: shared_consts.OrderStatus.IN_CART,
|
|
price: product.price,
|
|
after_price: product.after_price,
|
|
color: product.color,
|
|
size: product.size,
|
|
weight: product.weight,
|
|
|
|
quantity: order.quantity,
|
|
idStorehouse: order.idStorehouse,
|
|
idScontisticas: product.idScontisticas,
|
|
}
|
|
|
|
if (product.storehouses.length === 1) {
|
|
order.idStorehouse = product.storehouses[0]._id
|
|
}
|
|
|
|
return myorder
|
|
},
|
|
|
|
initcat() {
|
|
|
|
// rec.userId = userStore.my._id
|
|
|
|
return this.getRecordEmpty()
|
|
|
|
},
|
|
|
|
async loadProducts() {
|
|
|
|
const userStore = useUserStore()
|
|
const globalStore = useGlobalStore()
|
|
|
|
// console.log('loadProducts')
|
|
|
|
if (!globalStore.site.confpages.enableEcommerce)
|
|
return null
|
|
|
|
console.log('getProducts', 'userid=', userStore.my._id)
|
|
|
|
// if (userStore.my._id === '') {
|
|
// return new Types.AxiosError(0, null, 0, '')
|
|
// }
|
|
|
|
let ris = null
|
|
|
|
console.log('Ottieni Prodotti')
|
|
ris = await Api.SendReq('/products', 'POST', { userId: userStore.my._id })
|
|
.then((res) => {
|
|
console.log('Prodotti scaricati')
|
|
if (res.data.products) {
|
|
this.products = res.data.products
|
|
} else {
|
|
this.products = []
|
|
}
|
|
|
|
if (res.data.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 loadProduct({ code }: { code: any }) {
|
|
|
|
console.log('loadProduct', code)
|
|
const userStore = useUserStore()
|
|
const globalStore = useGlobalStore()
|
|
|
|
if (!globalStore.site.confpages.enableEcommerce)
|
|
return null
|
|
|
|
console.log('getProduct', 'code', code)
|
|
|
|
// if (userStore.my._id === '') {
|
|
// return new Types.AxiosError(0, null, 0, '')
|
|
// }
|
|
|
|
let ris = null
|
|
|
|
ris = await Api.SendReq('/products/' + code, 'POST', { code })
|
|
.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 (userStore.my._id === '') {
|
|
// return new Types.AxiosError(0, null, 0, '')
|
|
// }
|
|
|
|
let ris = null
|
|
|
|
ris = await Api.SendReq('/cart/' + userStore.my._id, 'GET', null)
|
|
.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 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/' + userStore.my._id, 'DELETE', { orderId: order._id })
|
|
.then((res) => {
|
|
this.updateDataProduct(res)
|
|
|
|
return res
|
|
})
|
|
},
|
|
|
|
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 === 1) {
|
|
// sto per rimuovere l'ultimo pezzo, quindi cancello direttamente
|
|
const risrem = await this.removeFromCart({ order: ordcart })
|
|
|
|
if (risrem) {
|
|
order.quantity = 0
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return await this.addSubQtyToItem({
|
|
addqty,
|
|
subqty: !addqty,
|
|
order: ordcart,
|
|
}).then((res: any) => {
|
|
if (res && res.risult) {
|
|
order.quantity = res.qty
|
|
}
|
|
return res;
|
|
})
|
|
}
|
|
} else {
|
|
if (order.quantity === 0)
|
|
order.quantity = 1
|
|
|
|
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=', userStore.my._id, neworder)
|
|
|
|
let ris = null
|
|
|
|
ris = await Api.SendReq('/cart/' + userStore.my._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, qty: order.quantity }
|
|
})
|
|
.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=', userStore.my._id, order)
|
|
|
|
let ris = null
|
|
|
|
ris = await Api.SendReq('/cart/' + userStore.my._id, 'POST', { addqty, subqty, order })
|
|
.then((res) => {
|
|
this.updateDataProduct(res)
|
|
|
|
return { risult: !!res, qty: res.data.qty }
|
|
})
|
|
.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
|
|
},
|
|
|
|
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
|
|
|
|
let ris = null
|
|
|
|
ris = await Api.SendReq('/cart/' + userStore.my._id + '/createorderscart', 'POST', { cart_id, status, note })
|
|
.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 })
|
|
.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 }: { order_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/' + userStore.my._id + '/ordercartstatus', 'POST', { order_id, status })
|
|
.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
|
|
},
|
|
},
|
|
|
|
})
|
|
|