diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 84282f4..744889d 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,10 +4,17 @@ - + - + + + + + + + + @@ -324,15 +331,7 @@ - - - - - 1769097476629 - - - - 1769097476629 + @@ -718,7 +717,15 @@ 1774283204849 - + + + 1774285464091 + + + + 1774285464091 + + @@ -768,7 +775,6 @@ - @@ -793,7 +799,8 @@ - + + diff --git a/frontend/components/reception/reception-form.vue b/frontend/components/reception/reception-form.vue index e2602fc..02053e5 100644 --- a/frontend/components/reception/reception-form.vue +++ b/frontend/components/reception/reception-form.vue @@ -1,5 +1,5 @@ - + Réception (null) const form = reactive({ licensePlate: '', @@ -217,7 +218,7 @@ onMounted(async () => { await loadVehicles() }) -async function validate() { +const buildPayload = () => { const normalizedLicensePlate = form.licensePlate.trim() const normalizedReceptionDate = form.receptionDate.trim() const normalizedReceptionTypeId = form.receptionTypeId.trim() @@ -236,7 +237,7 @@ async function validate() { const carrierIri = normalizedCarrierId ? `/api/carriers/${normalizedCarrierId}` : null const driverIri = normalizedDriverId ? `/api/drivers/${normalizedDriverId}` : null - const basePayload = { + return { licensePlate: normalizedLicensePlate, receptionDate: normalizedReceptionDate, receptionType: receptionTypeIri, @@ -244,13 +245,35 @@ async function validate() { supplier: supplierIri, address: addressIri, truck: truckIri, - carrier: carrierIri - } - - const payload = { - ...basePayload, + carrier: carrierIri, ...(isLiotCarrier.value && driverIri ? { driver: driverIri } : {}) } +} + +const saveDraft = async () => { + const payload = buildPayload() + if (!receptionStore.current) { + await receptionStore.createReception({ + currentStep: 0, + ...payload + }) + return + } + await receptionStore.updateReception(receptionStore.current.id, { + currentStep: receptionStore.current.currentStep, + ...payload + }) +} + +const validateFields = () => { + submitted.value = true + return formRef.value?.reportValidity() ?? false +} + +defineExpose({ saveDraft, validateFields }) + +async function validate() { + const payload = buildPayload() if (!receptionStore.current) { const created = await receptionStore.createReception({ diff --git a/frontend/components/shipment/shipment-form.vue b/frontend/components/shipment/shipment-form.vue index 093e072..093962e 100644 --- a/frontend/components/shipment/shipment-form.vue +++ b/frontend/components/shipment/shipment-form.vue @@ -1,5 +1,5 @@ - + Expédition (null) const form = reactive({ userId: '', @@ -286,7 +287,12 @@ const saveDraft = async () => { }) } -defineExpose({ saveDraft }) +const validateFields = () => { + submitted.value = true + return formRef.value?.reportValidity() ?? false +} + +defineExpose({ saveDraft, validateFields }) const validate = async () => { const payload = buildPayload() diff --git a/frontend/components/workflow/workflow-weight.vue b/frontend/components/workflow/workflow-weight.vue index ae3115f..295ebf7 100644 --- a/frontend/components/workflow/workflow-weight.vue +++ b/frontend/components/workflow/workflow-weight.vue @@ -60,6 +60,7 @@ const { title, fetchWeight, saveWeight, + saveWeightDraft, showGenerateReceipt, printReceipt } = useWeighingStep({ @@ -75,4 +76,6 @@ const { clearEntity: props.clearEntity, buildReceiptFilename: props.buildReceiptFilename }) + +defineExpose({ saveWeightDraft }) diff --git a/frontend/composables/steps/useWeighingStep.ts b/frontend/composables/steps/useWeighingStep.ts index 24c4f0b..916f019 100644 --- a/frontend/composables/steps/useWeighingStep.ts +++ b/frontend/composables/steps/useWeighingStep.ts @@ -29,7 +29,8 @@ export const useWeighingStep = (options: UseWeighingStepOptions) => { title, showLoadingBox, fetchWeight, - saveWeight + saveWeight, + saveWeightDraft } = useWeighing({ mode: options.mode, entity: options.entity, @@ -71,6 +72,7 @@ export const useWeighingStep = (options: UseWeighingStepOptions) => { showLoadingBox, fetchWeight, saveWeight, + saveWeightDraft, showGenerateReceipt, printReceipt } diff --git a/frontend/composables/useWeighing.ts b/frontend/composables/useWeighing.ts index bfc22d8..2b92130 100644 --- a/frontend/composables/useWeighing.ts +++ b/frontend/composables/useWeighing.ts @@ -90,6 +90,38 @@ export const useWeighing = ({ } } + const saveWeightDraft = async () => { + if (!entity.value) return + if (!weightData.value && !currentWeightEntry.value) return + + const existingEntry = currentWeightEntry.value + const baseDsd = weightData.value?.dsd ?? existingEntry?.dsd ?? null + const baseWeight = weightData.value?.weight ?? existingEntry?.weight ?? null + const baseWeighedAt = weightData.value?.weighedAt ?? existingEntry?.weighedAt ?? null + + if (baseWeight === null) return + + const relationPayload: Record = {} + relationPayload[entityName] = `/api/${apiResource}/${entity.value.id}` + + if (existingEntry?.id) { + await updateWeight(existingEntry.id, { + type: mode, + dsd: baseDsd, + weight: baseWeight, + weighedAt: baseWeighedAt + }) + } else { + await createWeight({ + ...relationPayload, + type: mode, + dsd: baseDsd, + weight: baseWeight, + weighedAt: baseWeighedAt + }) + } + } + return { weightData, currentWeightEntry, @@ -98,7 +130,8 @@ export const useWeighing = ({ title, showLoadingBox, fetchWeight, - saveWeight + saveWeight, + saveWeightDraft } } diff --git a/frontend/composables/useWorkflowSteps.ts b/frontend/composables/useWorkflowSteps.ts index dd37886..8d9bf2c 100644 --- a/frontend/composables/useWorkflowSteps.ts +++ b/frontend/composables/useWorkflowSteps.ts @@ -48,7 +48,8 @@ export const useWorkflowSteps = (config: WorkflowConfig, store: WorkflowStore) = return } const datePayload: Record = {} - datePayload[config.dateField] = store.current[config.dateField] + const rawDate = store.current[config.dateField] + datePayload[config.dateField] = rawDate ? rawDate.slice(0, 10) : rawDate await store[updateMethod](store.current.id, { currentStep: store.current.currentStep, licensePlate: store.current.licensePlate, diff --git a/frontend/pages/reception/[[id]].vue b/frontend/pages/reception/[[id]].vue index 7cec0ce..d1b9f79 100644 --- a/frontend/pages/reception/[[id]].vue +++ b/frontend/pages/reception/[[id]].vue @@ -14,9 +14,10 @@ >Mettre en attente - + Promise, validateFields: () => boolean } | null>(null) +const grossWeightRef = ref<{ saveWeightDraft: () => Promise } | null>(null) +const tareWeightRef = ref<{ saveWeightDraft: () => Promise } | null>(null) -const { stepLabels, saveAndHold, handleStepSelect } = useWorkflowSteps(receptionConfig, receptionStore) +const { stepLabels, handleStepSelect } = useWorkflowSteps(receptionConfig, receptionStore) + +const router = useRouter() + +const saveAndHold = async () => { + if (receptionFormRef.value) { + if (!receptionFormRef.value.validateFields()) return + await receptionFormRef.value.saveDraft() + } else { + if (grossWeightRef.value) await grossWeightRef.value.saveWeightDraft() + if (tareWeightRef.value) await tareWeightRef.value.saveWeightDraft() + } + await router.push('/') +} // Init route watcher const route = useRoute() diff --git a/frontend/pages/shipment/[[id]].vue b/frontend/pages/shipment/[[id]].vue index efd5a54..36d7323 100644 --- a/frontend/pages/shipment/[[id]].vue +++ b/frontend/pages/shipment/[[id]].vue @@ -18,6 +18,7 @@ Promise } | null>(null) +const shipmentFormRef = ref<{ saveDraft: () => Promise, validateFields: () => boolean } | null>(null) +const grossWeightRef = ref<{ saveWeightDraft: () => Promise } | null>(null) +const tareWeightRef = ref<{ saveWeightDraft: () => Promise } | null>(null) const { stepLabels, handleStepSelect } = useWorkflowSteps(shipmentConfig, shipmentStore) @@ -83,7 +87,11 @@ watch( const saveAndHold = async () => { if (shipmentFormRef.value) { + if (!shipmentFormRef.value.validateFields()) return await shipmentFormRef.value.saveDraft() + } else { + if (grossWeightRef.value) await grossWeightRef.value.saveWeightDraft() + if (tareWeightRef.value) await tareWeightRef.value.saveWeightDraft() } await router.push('/') }