Files
export-sereia-guardia-fastapi/archive/products_sold_parser.py
2025-01-25 15:58:24 -03:00

39 lines
1.1 KiB
Python

import pandas as pd
from db_bk.models import Order, ProductsSold
import os
def parse_csv_to_products_sold(file_path):
products_sold = []
df = pd.read_csv(file_path, delimiter=';', encoding='cp1252', date_format='%d/%m/%Y')
for _, row in df.iterrows():
ps = ProductsSold(
product_id=row['Código produto'],
order_id=row['Código pedido'],
name=row['Nome produto'],
price=row['Preço venda'],
quantity=['Quantidade'],
reference=row['Referência'],
additional_information=row['Informação adicional']
)
products_sold.append(ps)
return products_sold
def load_products_sold_csv(directory_path):
all_products_sold = []
for filename in os.listdir(directory_path):
if filename.endswith('.csv'):
file_path = os.path.join(directory_path, filename)
orders = parse_csv_to_products_sold(file_path)
all_products_sold.extend(orders)
for ps in all_products_sold:
print(ps.product_id, ps.price, ps.name)
load_products_sold_csv('../csv/tray-products-sold/')