- Categorie
- ProductInfo
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { IBaseOrder, ICart, IOrder, IOrderCart, IProduct, IProductsState } from 'model'
|
||||
import { IBaseOrder, ICart, IOrder, IOrderCart, IProduct, IProductsState, IProductInfo, ICatProd } from 'model'
|
||||
|
||||
import { Api } from '@api'
|
||||
import { serv_constants } from '@src/store/Modules/serv_constants'
|
||||
@@ -14,25 +14,69 @@ import { ref } from 'vue'
|
||||
|
||||
import translate from '@src/globalroutines/util'
|
||||
|
||||
function getRecordProductInfoEmpty(): IProductInfo {
|
||||
return {
|
||||
code: '',
|
||||
name: '',
|
||||
description: '',
|
||||
department: '',
|
||||
catprods: [],
|
||||
color: '',
|
||||
size: '',
|
||||
weight: 0,
|
||||
unit: 0,
|
||||
stars: 0,
|
||||
date: tools.getDateNow(),
|
||||
icon: '',
|
||||
img: '',
|
||||
}
|
||||
}
|
||||
|
||||
function getRecordProductEmpty() {
|
||||
return {
|
||||
active: false,
|
||||
productInfo: {
|
||||
img: '', code: '', name: '',
|
||||
|
||||
},
|
||||
storehouses: [], scontisticas: [],
|
||||
price: 0, stockQty: 0, bookableQty: 0, gasordines: [],
|
||||
idGasordines: [], minBuyQty: 1, maxBookableQty: 0,
|
||||
}
|
||||
}
|
||||
|
||||
export const useProducts = defineStore('Products', {
|
||||
state: (): IProductsState => ({
|
||||
products: [],
|
||||
cart: { items: [], totalPrice: 0, totalQty: 0, userId: '' },
|
||||
orders: [],
|
||||
catprods: [],
|
||||
productInfos: [],
|
||||
}),
|
||||
|
||||
getters: {
|
||||
getCatProds: (state: IProductsState) => (): ICatProd[] => {
|
||||
return state.catprods
|
||||
},
|
||||
|
||||
getNumProdTot: (state: IProductsState) => (): number => {
|
||||
return state.products.length
|
||||
},
|
||||
|
||||
getProducts: (state: IProductsState) => (cosa?: number): IProduct[] => {
|
||||
if (!!cosa) {
|
||||
let ris = null
|
||||
if (cosa === shared_consts.PROD.GAS) {
|
||||
ris = state.products.filter((rec) => (rec.idGasordines && rec.idGasordines.length > 0))
|
||||
} else if (cosa === shared_consts.PROD.BOTTEGA) {
|
||||
ris = state.products.filter((rec) => (!rec.idGasordines || (rec.idGasordines && rec.idGasordines.length === 0)))
|
||||
}
|
||||
return ris ? ris : []
|
||||
return state.products.filter((rec: IProduct) => {
|
||||
const hasGasOrdines = rec.idGasordines && rec.idGasordines.length > 0;
|
||||
|
||||
if ((cosa === shared_consts.PROD.GAS && hasGasOrdines) ||
|
||||
(cosa === shared_consts.PROD.BOTTEGA && ((!hasGasOrdines || (hasGasOrdines && rec.idGasordines?.length === 0))))) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
} else {
|
||||
return state.products
|
||||
return state.products;
|
||||
}
|
||||
},
|
||||
|
||||
@@ -65,18 +109,24 @@ export const useProducts = defineStore('Products', {
|
||||
|
||||
},
|
||||
|
||||
|
||||
getProductById: (state: IProductsState) => (id: string): IProduct => {
|
||||
const prod = state.products.find((prod: IProduct) => prod._id === id)
|
||||
return prod ? prod : { active: false, img: '', code: '', name: '', storehouses: [], scontisticas: [], price: 0, stockQty: 0, bookableQty: 0, gasordines: [], idGasordines: [], minBuyQty: 1 }
|
||||
return prod ? prod : getRecordProductEmpty()
|
||||
},
|
||||
|
||||
getProductByCode: (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: [], price: 0,
|
||||
gasordines: [], idGasordines: [],
|
||||
stockQty: 0, bookableQty: 0, minBuyQty: 1
|
||||
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 => {
|
||||
@@ -218,12 +268,14 @@ export const useProducts = defineStore('Products', {
|
||||
return []
|
||||
},
|
||||
|
||||
|
||||
getRecordEmpty: (state: IProductsState) => (): IProduct => {
|
||||
|
||||
const tomorrow = tools.getDateNow()
|
||||
tomorrow.setDate(tomorrow.getDate() + 1)
|
||||
|
||||
return {
|
||||
productInfo: getRecordProductInfoEmpty(),
|
||||
// _id: tools.getDateNow().toISOString(), // Create NEW
|
||||
active: false,
|
||||
idProducer: '',
|
||||
@@ -236,63 +288,40 @@ export const useProducts = defineStore('Products', {
|
||||
producer: {},
|
||||
storehouses: [],
|
||||
provider: {},
|
||||
code: '',
|
||||
name: '',
|
||||
description: '',
|
||||
department: '',
|
||||
category: '',
|
||||
price: 0.0,
|
||||
color: '',
|
||||
size: '',
|
||||
quantityAvailable: 0,
|
||||
bookableAvailableQty: 0,
|
||||
stockQty: 0,
|
||||
minBuyQty: 1,
|
||||
maxBookableQty: 0,
|
||||
bookableQty: 0,
|
||||
canBeShipped: false,
|
||||
QuantitaOrdinateInAttesa: 0,
|
||||
QuantitaPrenotateInAttesa: 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,
|
||||
TotalPriceProduct: 0,
|
||||
idProduct: product._id,
|
||||
product, // Copia tutto l'oggetto Product !
|
||||
|
||||
// Ordine:
|
||||
price: product.price,
|
||||
after_price: product.after_price,
|
||||
color: product.color,
|
||||
size: product.size,
|
||||
weight: product.weight,
|
||||
|
||||
quantity: order.quantity,
|
||||
quantitypreordered: order.quantitypreordered,
|
||||
idStorehouse: order.idStorehouse,
|
||||
idGasordine: order.idGasordine,
|
||||
idScontisticas: product.idScontisticas,
|
||||
}
|
||||
|
||||
if (product.storehouses.length === 1) {
|
||||
myorder.idStorehouse = product.storehouses[0]._id
|
||||
idStorehouse: order.idStorehouse,
|
||||
}
|
||||
|
||||
return myorder
|
||||
@@ -306,6 +335,12 @@ export const useProducts = defineStore('Products', {
|
||||
|
||||
},
|
||||
|
||||
/*resetProducts() {
|
||||
const arrprod = [...this.products]
|
||||
this.products = []
|
||||
this.products = [...arrprod]
|
||||
},*/
|
||||
|
||||
async loadProducts() {
|
||||
|
||||
const userStore = useUserStore()
|
||||
@@ -484,11 +519,11 @@ export const useProducts = defineStore('Products', {
|
||||
}
|
||||
} else {
|
||||
if (this.isQtyAvailableByProduct(product)) {
|
||||
order.quantity = 1
|
||||
order.quantity = product.minBuyQty | 1
|
||||
order.quantitypreordered = 0
|
||||
} else {
|
||||
if (this.isInPreorderByProduct(product)) {
|
||||
order.quantitypreordered = 1
|
||||
order.quantitypreordered = product.minBuyQty | 1
|
||||
order.quantity = 0
|
||||
}
|
||||
}
|
||||
@@ -650,8 +685,8 @@ export const useProducts = defineStore('Products', {
|
||||
|
||||
return ris
|
||||
},
|
||||
async addtoCartBase({ $q, t, code, order, addqty }: { $q: any, t: any, code: string, order: IOrder, addqty: boolean }) {
|
||||
let product = this.getProductByCode(code)
|
||||
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) => {
|
||||
|
||||
Reference in New Issue
Block a user