Spaces:
Running
Running
Andre
deploy: snapshot c96b9ab -- Inventory Journals (Lagerforandringsjournal) na Contabilidade (v1.3.61)
77db408 Download backend/scripts/update_item_dates.py from inwave-gateway/hub: direct link, hf CLI and curl.
- Browser
- Download file 1.89 kB
-
https://huggingface.co/spaces/inwave-gateway/hub/resolve/main/backend/scripts/update_item_dates.py
- Command line
-
hf download hf://spaces/inwave-gateway/hub/backend/scripts/update_item_dates.py
-
curl -L -o update_item_dates.py https://huggingface.co/spaces/inwave-gateway/hub/resolve/main/backend/scripts/update_item_dates.py
1.89 kB
| import sys | |
| sys.path.insert(0, '.') | |
| from database import SessionLocal | |
| import models | |
| import datetime | |
| def update_item_dates(): | |
| db = SessionLocal() | |
| try: | |
| # Get max invoice date per SKU | |
| from sqlalchemy import func | |
| max_dates = db.query( | |
| models.InvoiceLine.sku, | |
| func.max(models.InvoiceLine.date).label('max_date') | |
| ).group_by(models.InvoiceLine.sku).all() | |
| updated_count = 0 | |
| for sku, max_date in max_dates: | |
| if not sku or not max_date: | |
| continue | |
| item = db.query(models.Item).filter(models.Item.sku == sku).first() | |
| if item: | |
| # Convert max_date to string YYYY-MM-DD for last_sale_date | |
| if isinstance(max_date, datetime.datetime): | |
| date_str = max_date.strftime("%Y-%m-%d") | |
| date_dt = max_date.replace(hour=0, minute=0, second=0, microsecond=0) | |
| else: | |
| # Assume it's a date | |
| date_str = max_date.strftime("%Y-%m-%d") | |
| date_dt = datetime.datetime.combine(max_date, datetime.time.min) | |
| # Update last_sale_date if newer | |
| if item.last_sale_date != date_str: | |
| item.last_sale_date = date_str | |
| updated_count += 1 | |
| # Update last_movement_date if newer (for BOMs and other items) | |
| if not item.last_movement_date or item.last_movement_date < date_dt: | |
| item.last_movement_date = date_dt | |
| updated_count += 1 | |
| db.commit() | |
| print(f"Updated {updated_count} items with last sale/movement dates") | |
| except Exception as e: | |
| print(f"Error: {e}") | |
| db.rollback() | |
| finally: | |
| db.close() | |
| if __name__ == "__main__": | |
| update_item_dates() |