Correctly plan orders when creating deliveries and linking them

Adjust delivery creation logic in server/routes.ts and server/storage.ts to set the initial delivery status to 'planned' instead of 'delivered'. This change also ensures that linked orders are updated to a 'planned' status, preventing incorrect 'delivered' status assignments and resolving the issue where orders were being marked as delivered prematurely.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 7c468936-2a88-4686-ac0a-84921a45222d
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/7c468936-2a88-4686-ac0a-84921a45222d/I42UgiS
This commit is contained in:
michaelschal committed 2025-08-13 21:49:47 +00:00
1 parent 00a95954d0
commit 91be24e93a
2 files changed
+38 -11

No files matched your search

+23 -10
View File
@@ -730,15 +730,8 @@ export async function registerRoutes(app: Express): Promise<Server> {
console.log('✅ Delivery updated successfully:', { id, updatedDelivery });
// SYNCHRONISATION AUTOMATIQUE : Si livraison devient "delivered", marquer la commande associée comme "delivered"
if (data.status === 'delivered' && updatedDelivery.orderId) {
try {
console.log(`🔄 Auto-sync: Delivery #${id} marked as delivered, updating order #${updatedDelivery.orderId}`);
await storage.updateOrder(updatedDelivery.orderId, { status: 'delivered' });
console.log(`✅ Auto-sync: Order #${updatedDelivery.orderId} automatically marked as delivered`);
} catch (error) {
console.error(`❌ Auto-sync failed for order #${updatedDelivery.orderId}:`, error);
}
}
// MAIS seulement après validation explicite (pas juste mise à jour status)
// Cette sync sera gérée dans validateDelivery endpoint uniquement
// AUTO-VALIDATION RAPPROCHEMENT AUTOMATIQUE : Si fournisseur en mode automatique, livraison delivered + BL → auto-valider
if (data.status === 'delivered' || data.blNumber) {
@@ -780,11 +773,23 @@ export async function registerRoutes(app: Express): Promise<Server> {
return res.status(404).json({ message: "User not found" });
}
console.log('🚛 Creating delivery with data:', req.body);
// Traiter les données pour orderId - "none" devient null
const processedBody = { ...req.body };
if (processedBody.orderId === "none" || processedBody.orderId === "") {
processedBody.orderId = null;
} else if (processedBody.orderId) {
processedBody.orderId = parseInt(processedBody.orderId);
}
const data = insertDeliverySchema.parse({
...req.body,
...processedBody,
createdBy: user.id,
});
console.log('🚛 Processed delivery data:', data);
// Check if user has access to the group
if (user.role !== 'admin') {
const userGroupIds = user.userGroups.map(ug => ug.groupId);
@@ -794,6 +799,14 @@ export async function registerRoutes(app: Express): Promise<Server> {
}
const delivery = await storage.createDelivery(data);
// Log de liaison avec commande
if (data.orderId) {
console.log(`🔗 Delivery #${delivery.id} linked to order #${data.orderId}`);
} else {
console.log(`🚛 Delivery #${delivery.id} created without order link`);
}
res.json(delivery);
} catch (error) {
console.error("Error creating delivery:", error);
+15 -1
View File
@@ -1677,11 +1677,25 @@ export class MemStorage implements IStorage {
const delivery: Delivery = {
id,
...deliveryData,
status: 'pending',
status: 'planned', // Livraisons créées sont automatiquement planifiées
createdAt: new Date(),
updatedAt: new Date(),
};
this.deliveries.set(id, delivery);
// Si une commande est liée, la marquer comme "planned" (pas delivered!)
if (deliveryData.orderId) {
try {
const order = this.orders.get(deliveryData.orderId);
if (order && order.status === 'pending') {
console.log(`🔗 Delivery #${id} linked to order #${deliveryData.orderId}, updating order status to 'planned'`);
await this.updateOrder(deliveryData.orderId, { status: 'planned' });
}
} catch (error) {
console.error(`❌ Failed to update order #${deliveryData.orderId} status to planned:`, error);
}
}
return delivery;
}