Skip to content

Commit 06fea61

Browse files
committed
Modifider csv de comande-Le csv contient Barcode, Nom, Quantités Reçues (de colis), Quantités Promises (de colis), Nombre d'éléments par colis, Commentaire, Anomalie
1 parent 04699e7 commit 06fea61

4 files changed

Lines changed: 46 additions & 7 deletions

File tree

app/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ def create_app():
2828
# Initialisation des extensions
2929
from .extensions import db, migrate, oauth
3030
db.init_app(app)
31+
from app.models.received_order import ReceivedOrder
32+
33+
with app.app_context():
34+
db.create_all()
35+
3136
migrate.init_app(app, db)
3237
oauth.init_app(app)
3338

app/main/routes.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from app.services.email_service import EmailPreparer, EmailSender
1010
import urllib.parse
1111
from app.auth.pkce_auth import requires_auth
12+
from app.models.received_order import ReceivedOrder
13+
from app.extensions import db
1214
# Configuration du logger
1315
logger = logging.getLogger(__name__)
1416

@@ -105,7 +107,7 @@ def process_inbound_order(po_reference):
105107
has_errors = False
106108

107109
for item in data['products']:
108-
if 'barcode' not in item or 'ok' not in item or (not item['ok'] and 'received' not in item):
110+
if 'barcode' not in item or 'ok' not in item or (not item['ok'] and 'received_quantity' not in item):
109111
has_errors = True
110112
continue
111113

@@ -119,13 +121,13 @@ def process_inbound_order(po_reference):
119121
processed_products.append({
120122
"barcode": item['barcode'],
121123
"name": item.get("name", ""), # optionnel : ajouter nom produit si disponible
122-
"parcels": item.get("parcels", 1),
123-
"packSize": item.get("packSize", 1),
124+
"received_quantity": item.get("received_quantity", ""),
125+
"ordered_quantity": item.get("ordered_quantity", ""),
126+
"packsize": item.get("packsize", ""),
124127
"ok": item['ok'],
125128
"comment": item.get("comment", ""),
126129
"status": status,
127130
"message": message,
128-
"received_quantity": item.get('received')
129131
})
130132

131133
if has_errors:
@@ -150,6 +152,17 @@ def process_inbound_order(po_reference):
150152
}
151153
}
152154

155+
156+
# --- Marquer la commande comme réceptionnée ---
157+
already_received = ReceivedOrder.query.filter_by(po=po_reference).first()
158+
159+
if not already_received:
160+
db.session.add(ReceivedOrder(po=po_reference))
161+
db.session.commit()
162+
logging.info(f"Commande {po_reference} marquée comme réceptionnée")
163+
else:
164+
logging.info(f"Commande {po_reference} déjà réceptionnée")
165+
153166
logging.info(f"Traitement de la commande {po_reference} terminé avec succès")
154167
return jsonify(response), 200
155168

@@ -218,6 +231,7 @@ def get_product_info():
218231
except Exception as e:
219232
return jsonify({"error": str(e)}), 500
220233

234+
221235
@main_bp.route("/orders/inbound", methods=["GET", "OPTIONS"])
222236
@requires_auth()
223237
def get_inbound_orders():
@@ -253,6 +267,15 @@ def get_inbound_orders():
253267
odoo = current_app.extensions['odoo']
254268
purchase_orders = asyncio.run(odoo.fetch_waiting_purchase_orders())
255269

270+
271+
# Récupération des PO déjà réceptionnées (1 requête DB)
272+
received_pos = {
273+
po for (po,) in db.session.query(ReceivedOrder.po).all()
274+
}
275+
276+
print("Received POS", received_pos)
277+
278+
256279
# Formatage de la réponse selon le format attendu
257280
response = []
258281

@@ -263,7 +286,8 @@ def get_inbound_orders():
263286
"po": po.name,
264287
"provider": po.partner_name if po.partner_name else "Inconnu",
265288
"date": po.date_order.strftime("%Y-%m-%dT%H:%M:%S") if po.date_order else None,
266-
"n_products": po.lines_count
289+
"n_products": po.lines_count,
290+
"received": True if po.name in received_pos else False
267291
}
268292
response.append(po_data)
269293

app/models/received_order.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from datetime import datetime
2+
from app.extensions import db # adapte l'import si besoin
3+
4+
class ReceivedOrder(db.Model):
5+
__tablename__ = "received_orders"
6+
7+
id = db.Column(db.Integer, primary_key=True)
8+
po = db.Column(db.String(50), unique=True, nullable=False, index=True)
9+
received_at = db.Column(db.DateTime, default=datetime.utcnow)

app/services/email_service.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,15 @@ def prepare_purchase_order_email(purchase_order, lines, sender, recipient):
8484
writer = csv.writer(output)
8585
# En-têtes
8686
writer.writerow([
87-
"Code Barre", "Nom Produit", "Quantité", "Pack Size", "Statut", "Commentaire"
87+
"Code Barre", "Nom Produit", "Quantité reçue","Quantité commandée", "Pack Size", "Statut", "Commentaire"
8888
])
8989
# Lignes
9090
for line in lines:
9191
writer.writerow([
9292
line.get("barcode"),
9393
line.get("name"),
94-
line.get("parcels", 0) * line.get("packSize", 1),
94+
line.get("received_quantity", 0),
95+
line.get("ordered_quantity", 0),
9596
line.get("packSize", 1),
9697
"OK" if line.get("ok") else "Anomalie",
9798
line.get("comment", "")

0 commit comments

Comments
 (0)