Watch
1
0
Fork
You've already forked arc
0
forked from hesabix/arc
arc/hesabixAPI/adapters/api/v1/products.py

6574 lines
256 KiB
Python
Executable file
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Removed __future__ annotations to fix OpenAPI schema generation
from typing import Annotated, Dict, Any, Optional, List
from datetime import date as date_type
from fastapi import APIRouter, Depends, Request, Body, Query, BackgroundTasks
from sqlalchemy.orm import Session
from sqlalchemy import and_
from adapters.db.session import get_db
from app.core.auth_dependency import get_current_user, AuthContext
from app.core.permissions import (
require_business_access,
require_business_permission_dep,
require_business_permission_by_entity_dep,
has_business_permission_for_business,
)
from app.core.responses import success_response, ApiError, format_datetime_fields
from app.core.cache import get_cache
from adapters.api.v1.schemas import QueryInfo
from adapters.api.v1.schema_models.product import (
ProductCreateRequest,
ProductUpdateRequest,
ProductResponse,
ProductListResponse,
BulkPriceUpdateRequest,
BulkPriceUpdatePreviewResponse,
BulkProductPriceSheetApplyRequest,
BulkProductPriceSheetExcelExportRequest,
BulkProductPriceSheetItemsRequest,
BulkDefaultWarehouseRequest,
BulkDefaultWarehousePreviewResponse,
BulkDefaultWarehouseApplyResponse,
)
from adapters.api.v1.schema_models.common import SuccessResponse, ErrorResponse
from app.services.product_bulk_upsert_service import bulk_upsert_products_integration
from app.services.product_service import (
create_product,
list_products,
list_recent_sales_invoice_products,
get_product,
update_product,
delete_product,
check_product_has_related_documents,
preview_bulk_default_warehouse_update,
apply_bulk_default_warehouse_update,
get_item_movements_report,
get_sales_by_product_report,
get_inventory_kardex_report,
get_inventory_stock_report,
invalidate_products_cache,
)
from app.services.product_opening_balance_service import (
apply_product_opening_balance_changes,
create_product_with_opening_balance,
update_product_with_opening_balance,
validate_product_opening_balance_allowed,
)
from app.services.product_excel_import_opening_balance import (
WAREHOUSE_CODE_KEY,
WAREHOUSE_NAME_KEY,
WarehouseImportIndex,
opening_balance_columns_mapped,
prepare_opening_balance_for_import_row,
resolve_import_default_warehouse,
warehouse_columns_mapped,
)
from app.services.product_excel_import_spec import (
ALL_COLUMN_KEYS,
BOOLEAN_KEYS,
DECIMAL_KEYS,
INT_KEYS,
MAX_PRODUCT_IMPORT_DATA_ROWS,
MAX_PRODUCT_IMPORT_FILE_BYTES,
)
from app.services.product_excel_import_normalize import (
api_error_message,
build_create_payload,
build_update_payload,
find_existing_product_in_index,
format_pydantic_errors,
is_sample_import_row,
load_business_currency_index,
load_existing_product_match_index,
map_headers,
parse_bool_strict,
parse_inventory_mode,
product_match_key,
provided_keys_from_raw,
resolve_fx_currency,
select_products_import_worksheet,
)
from app.services.public_catalog_service import invalidate_public_catalog_caches
from app.services.product_excel_import_template_service import (
build_products_import_template,
category_full_path,
)
from app.services.product_commercial_insights_service import get_product_commercial_insights
from app.services.bulk_price_update_service import (
preview_bulk_price_update,
apply_bulk_price_update,
)
from app.services.bulk_product_prices_sheet_service import (
apply_bulk_product_price_sheet,
export_bulk_product_price_sheet_excel,
import_bulk_product_price_sheet_from_excel,
list_bulk_price_sheet_items,
)
from adapters.db.models.business import Business
from adapters.db.models.product import Product
from adapters.db.models.product_attribute_link import ProductAttributeLink
from adapters.db.models.price_list import PriceList, PriceItem
from adapters.db.models.currency import Currency
from app.core.i18n import negotiate_locale
from fastapi import UploadFile, File, Form, HTTPException
from fastapi.responses import Response
from adapters.api.v1.helpers.product_request_helper import (
process_product_request,
_upload_product_image,
_validate_image_extension,
)
import os
router = APIRouter(prefix="/products", tags=["محصولات و کالاها", "انبارداری"])
def _apply_pricing_visibility_for_user(
auth_context: AuthContext,
db: Session,
business_id: int,
item: Dict[str, Any],
) -> Dict[str, Any]:
can_view_sales = (
has_business_permission_for_business(auth_context, db, business_id, "pricing", "sales_price_view")
or has_business_permission_for_business(auth_context, db, business_id, "products", "view")
)
can_view_purchase = has_business_permission_for_business(
auth_context, db, business_id, "pricing", "purchase_price_view"
)
masked = dict(item or {})
if not can_view_sales:
masked["base_sales_price"] = None
if not can_view_purchase:
masked["base_purchase_price"] = None
return masked
async def _get_products_search_query_info(request: Request) -> QueryInfo:
"""
خواندن پارامترهای جستجوی محصولات از body (JSON) یا در صورت خالی بودن از query string.
برای سازگاری با پروکسی/کلاینت‌هایی که body را در query می‌فرستند.
"""
import json
body_bytes = await request.body()
if body_bytes and body_bytes.strip():
try:
data = json.loads(body_bytes)
if isinstance(data, dict) and data:
_allowed = (
"take", "skip", "sort_by", "sort_desc", "sort", "search",
"search_fields", "searchFields",
"category_ids", "categoryIds",
"filters", "include_inventory", "inventory_as_of_date",
)
return QueryInfo(**{k: v for k, v in data.items() if k in _allowed})
except (json.JSONDecodeError, TypeError, ValueError):
pass
q = request.query_params
def _int(name: str, default: int) -> int:
v = q.get(name)
if v is None:
return default
try:
return int(v)
except (ValueError, TypeError):
return default
def _bool(name: str, default: bool) -> bool:
v = q.get(name)
if v is None:
return default
return str(v).lower() in ("1", "true", "yes", "on")
filters_val = q.get("filters")
filters_parsed = None
if filters_val:
try:
filters_parsed = json.loads(filters_val)
if not isinstance(filters_parsed, list):
filters_parsed = None
except (json.JSONDecodeError, TypeError):
pass
sort_param = q.get("sort")
sort_parsed = None
if sort_param:
try:
sp = json.loads(sort_param)
if isinstance(sp, list):
sort_parsed = sp
except (json.JSONDecodeError, TypeError, ValueError):
pass
return QueryInfo(
take=max(1, min(1000, _int("take", 20))),
skip=max(0, _int("skip", 0)),
sort_by=q.get("sort_by") or None,
sort_desc=_bool("sort_desc", False),
sort=sort_parsed,
search=q.get("search") or None,
search_fields=None,
filters=filters_parsed,
include_inventory=_bool("include_inventory", False),
inventory_as_of_date=q.get("inventory_as_of_date") or None,
)
def _ensure_products_pagination(result: Dict[str, Any], take: int, skip: int) -> None:
"""
اطمینان از وجود فیلدهای pagination (total_count, has_more) در پاسخ.
برای پاسخ تازه از list_products معمولاً وجود دارند؛ برای کش قدیمی ممکن است نباشند.
"""
if not isinstance(result, dict):
return
items = result.get("items") or []
if "total_count" not in result:
result["total_count"] = len(items)
if "has_more" not in result:
total = result.get("total_count", len(items))
result["has_more"] = (skip + take) < total
from app.services.product_excel_export_service import (
PRODUCT_EXPORT_INVENTORY_KEYS as _PRODUCT_EXPORT_INVENTORY_KEYS,
body_truthy as _body_truthy,
export_columns_need_inventory as _export_columns_need_inventory,
should_include_inventory_for_product_export as _should_include_inventory_for_product_export,
enrich_product_export_items as _enrich_product_export_items,
build_products_excel_export,
upload_export_bytes,
)
def _enqueue_products_excel_export(
*,
business_id: int,
user_id: int,
body: dict,
calendar_type: str,
accept_language: str,
background: Optional[BackgroundTasks],
request: Request,
):
"""صف RQ یا JobManager؛ در صورت شکست None برمی‌گرداند تا sync اجرا شود."""
from app.core.queue import get_queue_service, QUEUE_EXPORTS
from app.services.jobs.export_job import export_products_excel_job
from app.services.job_manager import JobManager
job_body = dict(body or {})
job_body.pop("async", None)
job_body.pop("async_mode", None)
queue_service = get_queue_service()
if queue_service and queue_service.enabled:
job = queue_service.enqueue(
export_products_excel_job,
business_id=business_id,
user_id=user_id,
body=job_body,
calendar_type=calendar_type,
accept_language=accept_language,
queue_name=QUEUE_EXPORTS,
timeout=1800,
result_ttl=7200,
)
if job:
try:
job.meta = dict(job.meta or {})
job.meta.update({
"user_id": int(user_id),
"business_id": int(business_id),
"export_type": "products",
})
job.save_meta()
except Exception:
pass
return success_response({
"async": True,
"job_id": job.id,
"status": "queued",
"queue": QUEUE_EXPORTS,
}, request=request, message="PRODUCTS_EXPORT_QUEUED")
# Fallback: JobManager + BackgroundTasks
if background is None:
return None
jm = JobManager.instance()
job_id = jm.create("Products excel export queued")
def _task():
from adapters.db.session import get_db_session
try:
jm.update(job_id, 10, "Building excel")
with get_db_session() as db_sess:
data, filename, record_count = build_products_excel_export(
db_sess,
business_id,
job_body,
calendar_type=calendar_type,
accept_language=accept_language,
)
jm.update(job_id, 80, "Uploading file")
saved = upload_export_bytes(
db_sess,
business_id=business_id,
user_id=user_id,
data=data,
filename=filename,
)
jm.succeed(
job_id,
{
"success": True,
"export_type": "products",
"format": "xlsx",
"business_id": business_id,
"filename": filename,
"file_id": saved.get("file_id"),
"file": saved,
"record_count": record_count,
"file_size": len(data),
},
"Products excel export completed",
)
except Exception as e:
jm.fail(job_id, str(e))
background.add_task(_task)
return success_response({
"async": True,
"job_id": job_id,
"status": "queued",
"queue": "job_manager",
}, request=request, message="PRODUCTS_EXPORT_QUEUED")
@router.post(
"/business/{business_id}",
summary="ایجاد محصول جدید",
description="""
ایجاد یک محصول یا خدمت جدید برای کسب‌وکار
### ویژگی‌ها:
- پشتیبانی از آپلود تصویر (multipart/form-data یا JSON)
- پشتیبانی از 60+ فیلد شامل: قیمت‌گذاری، موجودی، مالیات، ویژگی‌ها
- اگر کد محصول ارسال نشود، به صورت خودکار تولید می‌شود
### نکات مهم:
- برای آپلود تصویر از multipart/form-data استفاده کنید
- فیلد `item_type` می‌تواند "کالا" یا "خدمت" باشد
- برای کالاهای با کنترل موجودی، `track_inventory` را true قرار دهید
""",
response_model=SuccessResponse[ProductResponse],
responses={
200: {
"description": "محصول با موفقیت ایجاد شد",
"content": {
"application/json": {
"example": {
"success": True,
"message": "محصول با موفقیت ایجاد شد",
"data": {
"id": 123,
"code": "P1001",
"name": "لپ‌تاپ Asus Vivobook 15",
"item_type": "کالا",
"description": "لپ‌تاپ 15.6 اینچی با پردازنده Core i5",
"category_id": 1,
"category_name": "لپ‌تاپ",
"main_unit": "عدد",
"base_sales_price": 15000000,
"base_purchase_price": 12000000,
"track_inventory": True,
"default_warehouse_id": 1,
"is_sales_taxable": True,
"sales_tax_rate": 9,
"is_active": True,
"created_at": "2024-01-15T10:30:00Z"
}
}
}
}
},
400: {
"description": "خطا در اعتبارسنجی داده‌ها یا محدودیت ذخیره‌سازی",
"content": {
"application/json": {
"example": {
"success": False,
"error_code": "VALIDATION_ERROR",
"message": "نام محصول الزامی است"
}
}
}
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز - نیاز به مجوز products.add"
},
404: {
"description": "کسب‌وکار یافت نشد"
}
}
)
@require_business_access("business_id")
async def create_product_endpoint(
request: Request,
business_id: int,
payload: ProductCreateRequest = None,
file: UploadFile = File(None),
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("products", "add")),
) -> Dict[str, Any]:
# استفاده از helper function برای پردازش درخواست
processed_payload, processed_file, image_file_id = await process_product_request(
request=request,
business_id=business_id,
ctx=ctx,
db=db,
is_update=False,
)
# اگر payload از helper آمده، از آن استفاده می‌کنیم
if processed_payload:
payload = processed_payload
if not payload:
raise ApiError("INVALID_PAYLOAD", "داده‌های محصول ارسال نشده است", http_status=400)
if payload.opening_balance is not None:
if not has_business_permission_for_business(
ctx, db, business_id, "opening_balance", "edit"
):
raise ApiError(
"OPENING_BALANCE_PERMISSION_REQUIRED",
"برای ثبت تعداد اولیه به دسترسی ویرایش تراز افتتاحیه نیاز است",
http_status=403,
)
result = create_product_with_opening_balance(
db,
business_id,
ctx.get_user_id(),
payload,
create_product_fn=create_product,
delete_product_fn=delete_product,
)
else:
result = create_product(db, business_id, payload)
# به‌روزرسانی context_id فایل با product_id
if image_file_id and result.get("data", {}).get("id"):
from adapters.db.models.file_storage import FileStorage
file_storage = db.query(FileStorage).filter(FileStorage.id == image_file_id).first()
if file_storage:
file_storage.context_id = str(result["data"]["id"])
db.commit()
return success_response(data=format_datetime_fields(result["data"], request), request=request, message=result.get("message"))
@router.post(
"/business/{business_id}/bulk-upsert",
summary="ایجاد/ویرایش گروهی کالا (یکپارچه‌سازی)",
description=(
"بدنه: `{\"items\":[{\"client_ref?\":\"...\",\"product_id?\":null|int شناسه کالا در حسابیکس,"
'"payload\":{ ... فیلدهای ایجاد/ویرایش مانند endpoint تکی، از جمله opening_balance }}],'
'\"create_if_update_missing\":true}`. حداکثر ۱۰۰۰ آیتم. خروجی: results با status در created|updated|failed.'
),
)
@require_business_access("business_id")
async def bulk_upsert_products_integration_endpoint(
request: Request,
business_id: int,
body: Dict[str, Any] = Body(...),
db: Session = Depends(get_db),
ctx: AuthContext = Depends(get_current_user),
) -> Dict[str, Any]:
data = bulk_upsert_products_integration(db, business_id, ctx, body)
return success_response(data=data, request=request, message="BULK_PRODUCT_UPSERT_COMPLETED")
@router.post(
"/business/{business_id}/search",
summary="جستجو و فیلتر محصولات",
description="""
جستجو، فیلتر و لیست‌بندی محصولات با قابلیت‌های پیشرفته
### قابلیت‌ها:
- جستجو در چندین فیلد (نام، کد، توضیحات)
- فیلتر بر اساس دسته‌بندی، نوع محصول، وضعیت فعال/غیرفعال
- مرتب‌سازی بر اساس فیلدهای مختلف
- صفحه‌بندی نتایج
- فیلتر بر اساس دسته‌بندی (category_ids)
### نکات:
- حداکثر تعداد رکورد در هر درخواست: 1000 (پارامتر `take`)
- نتایج جستجو برای مدت کوتاهی cache می‌شوند
- برای جستجوی دقیق‌تر از فیلترها استفاده کنید
""",
response_model=SuccessResponse[ProductListResponse],
responses={
200: {
"description": "لیست محصولات با موفقیت دریافت شد",
"content": {
"application/json": {
"example": {
"success": True,
"message": "لیست محصولات دریافت شد",
"data": {
"items": [
{
"id": 1,
"code": "P1001",
"name": "لپ‌تاپ Asus Vivobook 15",
"item_type": "کالا",
"category_name": "لپ‌تاپ",
"base_sales_price": 15000000,
"is_active": True
}
],
"total_count": 100,
"has_more": True
}
}
}
}
},
400: {
"description": "خطا در اعتبارسنجی پارامترها"
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز - نیاز به مجوز products.view"
},
404: {
"description": "کسب‌وکار یافت نشد"
},
500: {
"description": "خطای داخلی سرور"
}
}
)
@require_business_access("business_id")
def search_products_endpoint(
request: Request,
business_id: int,
query_info: Annotated[QueryInfo, Depends(_get_products_search_query_info)],
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("products", "view")),
) -> Dict[str, Any]:
import logging
from app.core.responses import ApiError
from sqlalchemy.exc import SQLAlchemyError, OperationalError, TimeoutError as SQLTimeoutError
logger = logging.getLogger(__name__)
# کش نتایج جستجوی محصولات بر اساس پارامترها
cache = get_cache()
cache_key = None
category_id = None
if cache.enabled:
import json, hashlib
from decimal import Decimal
from datetime import datetime, date
from adapters.api.v1.schemas import FilterItem
# Helper function to convert objects to JSON-serializable format
def to_serializable(obj):
"""Convert Pydantic models and other non-serializable types to dict/primitive types"""
if hasattr(obj, 'model_dump'): # Pydantic v2
return obj.model_dump()
elif hasattr(obj, 'dict'): # Pydantic v1
return obj.dict()
elif isinstance(obj, (datetime, date)):
return obj.isoformat()
elif isinstance(obj, Decimal):
return float(obj)
elif isinstance(obj, dict):
return {k: to_serializable(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [to_serializable(item) for item in obj]
else:
return obj
# استخراج category_id از filters اگر موجود باشد
if query_info.filters:
for filter_item in query_info.filters:
# Convert FilterItem to dict if needed
if isinstance(filter_item, FilterItem):
filter_dict = to_serializable(filter_item)
else:
filter_dict = filter_item
if isinstance(filter_dict, dict):
field = filter_dict.get("property") or filter_dict.get("field")
if field == "category_id":
value = filter_dict.get("value")
operator = filter_dict.get("operator", "=")
if operator == "=" and value:
try:
category_id = int(value)
except (ValueError, TypeError):
pass
# اگر operator "in" است، اولین category_id را می‌گیریم
# (برای سادگی، تمام کش‌های مربوط به این category ها invalidate می‌شوند)
elif operator == "in" and isinstance(value, list) and value:
try:
category_id = int(value[0])
except (ValueError, TypeError):
pass
break
if category_id is None and getattr(query_info, "category_ids", None):
for cid in query_info.category_ids:
try:
category_id = int(cid)
break
except (ValueError, TypeError):
pass
# Convert filters to serializable format
serializable_filters = None
if query_info.filters:
serializable_filters = [to_serializable(f) for f in query_info.filters]
key_payload = {
"business_id": business_id,
"take": query_info.take,
"skip": query_info.skip,
"sort_by": query_info.sort_by,
"sort_desc": query_info.sort_desc,
"sort": to_serializable(query_info.sort) if getattr(query_info, "sort", None) else None,
"search": query_info.search,
"search_fields": to_serializable(query_info.search_fields) if getattr(query_info, "search_fields", None) else None,
"filters": serializable_filters,
"category_ids": to_serializable(query_info.category_ids) if query_info.category_ids else None,
"include_inventory": query_info.include_inventory,
"inventory_as_of_date": query_info.inventory_as_of_date,
}
key_str = json.dumps(key_payload, sort_keys=True, ensure_ascii=False)
key_hash = hashlib.sha256(key_str.encode("utf-8")).hexdigest()[:16]
cache_key = f"products_search:{business_id}:{ctx.get_user_id()}:{key_hash}"
cached = cache.get(cache_key)
if cached is not None:
# پاسخ کش‌شده ممکن است فاقد pagination باشد (کش قدیمی)؛ برای فرانت همیشه pagination لازم است
_ensure_products_pagination(cached, query_info.take, query_info.skip)
cached["items"] = [
_apply_pricing_visibility_for_user(ctx, db, business_id, item)
for item in (cached.get("items") or [])
]
return success_response(data=cached, request=request)
try:
result = list_products(db, business_id, {
"take": query_info.take,
"skip": query_info.skip,
"sort_by": query_info.sort_by,
"sort_desc": query_info.sort_desc,
"sort": [s.model_dump() for s in query_info.sort] if query_info.sort else None,
"search": query_info.search,
"search_fields": query_info.search_fields,
"filters": query_info.filters,
"category_ids": query_info.category_ids,
"include_inventory": query_info.include_inventory,
"inventory_as_of_date": query_info.inventory_as_of_date,
})
_ensure_products_pagination(result, query_info.take, query_info.skip)
result["items"] = [
_apply_pricing_visibility_for_user(ctx, db, business_id, item)
for item in (result.get("items") or [])
]
formatted = format_datetime_fields(result, request)
if cache.enabled and cache_key:
# استفاده از tag-based caching برای مدیریت بهتر invalidation
# چون موجودی و قیمت ممکن است سریع تغییر کند، TTL کوتاه
cache.set_with_products_tag(
key=cache_key,
value=formatted,
business_id=business_id,
category_id=category_id,
ttl=30
)
return success_response(data=formatted, request=request)
except (OperationalError, SQLTimeoutError) as e:
# Timeout یا خطای اتصال - rollback و بستن session
logger.error(
f"Database timeout/connection error in search_products for business_id={business_id}: {str(e)}",
exc_info=True,
extra={
"business_id": business_id,
"user_id": ctx.get_user_id() if ctx else None,
"path": request.url.path,
}
)
try:
db.rollback()
except Exception:
pass
raise ApiError(
"DATABASE_TIMEOUT",
"زمان اجرای درخواست به پایان رسید. لطفاً فیلترهای جستجو را محدودتر کنید یا بعداً تلاش کنید.",
http_status=504
)
except SQLAlchemyError as e:
logger.error(
f"Database error in search_products for business_id={business_id}",
exc_info=True,
extra={
"business_id": business_id,
"user_id": ctx.get_user_id() if ctx else None,
"path": request.url.path,
"query": {
"take": query_info.take,
"skip": query_info.skip,
"search": query_info.search,
}
}
)
try:
db.rollback()
except Exception:
pass
raise ApiError(
"DATABASE_ERROR",
"خطا در ارتباط با پایگاه داده. لطفاً بعداً تلاش کنید.",
http_status=500
)
except Exception as e:
logger.error(
f"Unexpected error in search_products for business_id={business_id}: {str(e)}",
exc_info=True,
extra={
"business_id": business_id,
"user_id": ctx.get_user_id() if ctx else None,
"path": request.url.path,
}
)
try:
db.rollback()
except Exception:
pass
raise ApiError(
"INTERNAL_SERVER_ERROR",
"خطای داخلی سرور رخ داد. لطفاً با پشتیبانی تماس بگیرید.",
http_status=500
)
@router.get(
"/business/{business_id}/recent-from-sales-invoices",
summary="کالاهای اخیر در فاکتورهای فروش",
description="""
فهرست کالاهایی که اخیراً در اسناد فروش (غیر پیش‌فاکتور) استفاده شده‌اند،
مرتب‌شده از جدیدترین فاکتور؛ برای پیشنهاد اولیه در جستجو (مثلاً فروش سریع).
""",
response_model=SuccessResponse[ProductListResponse],
)
@require_business_access("business_id")
def list_recent_from_sales_invoices_endpoint(
request: Request,
business_id: int,
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
take: int = Query(10, ge=1, le=50, description="حداکثر تعداد کالا"),
category_ids: Optional[str] = Query(
None,
description="شناسه دسته‌ها با ویرگول (مثلاً 1,2,3)؛ در صورت ارسال فقط کالاهای این دسته‌ها",
),
_: None = Depends(require_business_permission_dep("products", "view")),
) -> Dict[str, Any]:
parsed_cats: Optional[List[int]] = None
if category_ids and str(category_ids).strip():
parsed_cats = []
for part in str(category_ids).split(","):
p = part.strip()
if not p:
continue
try:
parsed_cats.append(int(p))
except ValueError:
continue
if not parsed_cats:
parsed_cats = None
result = list_recent_sales_invoice_products(
db, business_id, take=int(take), category_ids=parsed_cats
)
result["items"] = [
_apply_pricing_visibility_for_user(ctx, db, business_id, item)
for item in (result.get("items") or [])
]
formatted = format_datetime_fields(result, request)
return success_response(data=formatted, request=request)
@router.post(
"/business/{business_id}/metadata",
summary="دریافت گروهی فرادادهٔ کالاها",
)
@require_business_access("business_id")
def get_products_metadata_endpoint(
request: Request,
business_id: int,
payload: Dict[str, Any] = Body(...),
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("products", "view")),
) -> Dict[str, Any]:
raw_ids = payload.get("product_ids") or []
if not isinstance(raw_ids, list):
raise ApiError("INVALID_PRODUCT_IDS", "product_ids باید آرایه باشد", http_status=400)
product_ids: List[int] = []
for raw_id in raw_ids:
try:
product_ids.append(int(raw_id))
except (TypeError, ValueError):
raise ApiError("INVALID_PRODUCT_IDS", "شناسهٔ کالا نامعتبر است", http_status=400)
product_ids = list(dict.fromkeys(product_ids))
if len(product_ids) > 2000:
raise ApiError("TOO_MANY_PRODUCT_IDS", "حداکثر ۲۰۰۰ کالا در هر درخواست مجاز است", http_status=400)
if not product_ids:
return success_response(data={"items": []}, request=request)
products = db.query(
Product.id,
Product.code,
Product.name,
Product.description,
Product.main_unit,
Product.secondary_unit,
Product.unit_conversion_factor,
Product.base_sales_price,
Product.base_purchase_price,
Product.sales_price_fx,
Product.purchase_price_fx,
Product.price_fx_currency_id,
Product.default_warehouse_id,
Product.track_inventory,
Product.inventory_mode,
).filter(
Product.business_id == business_id,
Product.id.in_(product_ids),
).all()
attribute_rows = db.query(
ProductAttributeLink.product_id,
ProductAttributeLink.attribute_id,
).filter(ProductAttributeLink.product_id.in_([p.id for p in products])).all()
attribute_ids_by_product: Dict[int, List[int]] = {}
for product_id, attribute_id in attribute_rows:
attribute_ids_by_product.setdefault(int(product_id), []).append(int(attribute_id))
can_view_purchase_price = has_business_permission_for_business(
ctx, db, business_id, "pricing", "purchase_price_view"
)
items = []
for product in products:
item = {
"id": product.id,
"code": product.code,
"name": product.name,
"description": product.description,
"main_unit": product.main_unit,
"secondary_unit": product.secondary_unit,
"unit_conversion_factor": product.unit_conversion_factor,
"base_sales_price": product.base_sales_price,
"base_purchase_price": product.base_purchase_price,
"sales_price_fx": product.sales_price_fx,
"purchase_price_fx": product.purchase_price_fx,
"price_fx_currency_id": product.price_fx_currency_id,
"default_warehouse_id": product.default_warehouse_id,
"track_inventory": product.track_inventory,
"inventory_mode": product.inventory_mode or "bulk",
"attribute_ids": attribute_ids_by_product.get(int(product.id), []),
}
if not can_view_purchase_price:
item["base_purchase_price"] = None
items.append(item)
return success_response(data={"items": items}, request=request)
@router.get(
"/business/{business_id}/{product_id}",
summary="دریافت اطلاعات محصول",
description="""
دریافت اطلاعات کامل یک محصول یا خدمت
### اطلاعات برگشتی:
- اطلاعات پایه محصول (کد، نام، نوع، توضیحات)
- اطلاعات دسته‌بندی
- قیمت‌گذاری (فروش و خرید)
- تنظیمات موجودی و انبار
- اطلاعات مالیات
- تصویر محصول (در صورت وجود)
- ویژگی‌های محصول
- موجودی در انبارها (اختیاری)
""",
response_model=SuccessResponse,
responses={
200: {
"description": "اطلاعات محصول با موفقیت دریافت شد",
"content": {
"application/json": {
"example": {
"success": True,
"data": {
"item": {
"id": 123,
"code": "P1001",
"name": "لپ‌تاپ Asus Vivobook 15",
"item_type": "کالا",
"description": "لپ‌تاپ 15.6 اینچی با پردازنده Core i5",
"category_id": 1,
"category_name": "لپ‌تاپ",
"main_unit": "عدد",
"base_sales_price": 15000000,
"base_purchase_price": 12000000,
"track_inventory": True,
"default_warehouse_id": 1,
"default_warehouse_name": "انبار اصلی",
"total_quantity": 25,
"total_value": 300000000,
"is_sales_taxable": True,
"sales_tax_rate": 9,
"is_active": True,
"created_at": "2024-01-15T10:30:00Z",
"updated_at": "2024-01-20T14:20:00Z"
}
}
}
}
}
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز - نیاز به مجوز products.view"
},
404: {
"description": "محصول یافت نشد",
"content": {
"application/json": {
"example": {
"success": False,
"error_code": "NOT_FOUND",
"message": "Product not found"
}
}
}
}
}
)
@require_business_access("business_id")
def get_product_endpoint(
request: Request,
business_id: int,
product_id: int,
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_by_entity_dep("products", "view", Product, "product_id")),
) -> Dict[str, Any]:
item = get_product(db, product_id, business_id)
if not item:
raise ApiError("NOT_FOUND", "Product not found", http_status=404)
masked = _apply_pricing_visibility_for_user(ctx, db, business_id, item)
return success_response(data=format_datetime_fields({"item": masked}, request), request=request)
@router.get(
"/business/{business_id}/{product_id}/commercial-insights",
summary="خلاصهٔ بازرگانی کالا (حواله + تسعیر به ارز پایه)",
description="""
آمار خرید/فروش و نمودار میانگین موزون قیمت واحد به **ارز پایهٔ کسب‌وکار**.
تنها خطوط فاکتوری که مانند Kardex کنترل می‌شوند و فاکتور مربوطه **حداقل یک حوالهٔ انبار با وضعیت posted و مبدأ invoice** دارد، لحاظ می‌شوند.
نرخ تبدیل مانند شخص‌حساب: اولویت با `extra_info.fx` روی سند، سپس نرخ تاریخ سند مطابق تنظیم تسعیر کسب‌وکار.
""",
)
@require_business_access("business_id")
def get_product_commercial_insights_endpoint(
request: Request,
business_id: int,
product_id: int,
date_from: Optional[date_type] = Query(None, description="آغاز بازهٔ شمسی/میلادی یکسان با document_date اسناد"),
date_to: Optional[date_type] = Query(None, description="پایان بازه (شامل)"),
bucket: str = Query("month", description="تجمیع نمودار: day | week | month"),
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_by_entity_dep("products", "view", Product, "product_id")),
__: None = Depends(require_business_permission_dep("invoices", "view")),
) -> Dict[str, Any]:
data = get_product_commercial_insights(
db,
business_id=int(business_id),
product_id=int(product_id),
date_from=date_from,
date_to=date_to,
bucket=str(bucket or "month"),
)
return success_response(data=format_datetime_fields(data, request), request=request)
@router.post(
"/business/{business_id}/{product_id}/sync-base-price-from-fx",
summary="به‌روزرسانی قیمت پایه کالا از قیمت ارزی × نرخ روز",
tags=["products", "currency_revaluation"],
)
@require_business_access("business_id")
async def sync_product_base_price_from_fx_endpoint(
request: Request,
business_id: int,
product_id: int,
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_by_entity_dep("products", "edit", Product, "product_id")),
sync_price_list: bool = Query(False, description="همگام اختیاری با لیست قیمت پیش‌فرض"),
price_list_id: Optional[int] = Query(None, description="شناسه لیست قیمت (اختیاری؛ پیش‌فرض Quick Sales)"),
) -> Dict[str, Any]:
from app.services.product_fx_price_service import sync_product_base_from_fx
data = sync_product_base_from_fx(
db,
business_id,
product_id,
force=True,
sync_price_list=bool(sync_price_list),
price_list_id=price_list_id,
)
return success_response(data=data, request=request)
@router.post(
"/business/{business_id}/sync-base-prices-from-fx",
summary="به‌روزرسانی گروهی قیمت پایه کالاها از نرخ (auto_update)",
tags=["products", "currency_revaluation"],
)
@require_business_access("business_id")
async def sync_business_product_base_prices_from_fx_endpoint(
request: Request,
business_id: int,
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("products", "edit")),
sync_price_list: bool = Query(False, description="همگام اختیاری با لیست قیمت پیش‌فرض"),
) -> Dict[str, Any]:
from app.services.product_fx_price_service import sync_business_products_base_from_fx
data = sync_business_products_base_from_fx(
db, business_id, only_auto=True, sync_price_list=bool(sync_price_list)
)
return success_response(data=data, request=request)
@router.put(
"/business/{business_id}/{product_id}",
summary="ویرایش محصول",
description="""
ویرایش اطلاعات یک محصول یا خدمت موجود
### ویژگی‌ها:
- پشتیبانی از آپلود تصویر جدید (multipart/form-data یا JSON)
- تمام فیلدها اختیاری هستند (فقط فیلدهای ارسال شده به‌روزرسانی می‌شوند)
- تصویر قبلی در صورت آپلود تصویر جدید حذف می‌شود
### نکات مهم:
- برای آپلود تصویر از multipart/form-data استفاده کنید
- تغییر کد محصول ممکن است روی فاکتورهای موجود تأثیر بگذارد
- تغییر `track_inventory` ممکن است نیاز به بررسی موجودی داشته باشد
""",
response_model=SuccessResponse[ProductResponse],
responses={
200: {
"description": "محصول با موفقیت به‌روزرسانی شد",
"content": {
"application/json": {
"example": {
"success": True,
"message": "محصول با موفقیت به‌روزرسانی شد",
"data": {
"id": 123,
"code": "P1001",
"name": "لپ‌تاپ Asus Vivobook 15 (ویرایش شده)",
"item_type": "کالا",
"base_sales_price": 15500000,
"updated_at": "2024-01-20T14:20:00Z"
}
}
}
}
},
400: {
"description": "خطا در اعتبارسنجی داده‌ها یا محدودیت ذخیره‌سازی"
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز - نیاز به مجوز products.edit"
},
404: {
"description": "محصول یافت نشد",
"content": {
"application/json": {
"example": {
"success": False,
"error_code": "NOT_FOUND",
"message": "Product not found"
}
}
}
}
}
)
@require_business_access("business_id")
async def update_product_endpoint(
request: Request,
business_id: int,
product_id: int,
file: UploadFile | None = File(None),
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_by_entity_dep("products", "edit", Product, "product_id")),
) -> Dict[str, Any]:
# بررسی وجود محصول
from adapters.db.models.product import Product
product = db.get(Product, product_id)
if not product or product.business_id != business_id:
raise ApiError("NOT_FOUND", "Product not found", http_status=404)
old_image_file_id = product.image_file_id
processed_payload, processed_file, image_file_id = await process_product_request(
request=request,
business_id=business_id,
ctx=ctx,
db=db,
is_update=True,
product_id=product_id,
)
payload = processed_payload
# سازگاری با درخواست JSON همراه با فایل جداگانه
if not image_file_id and file and file.filename:
_validate_image_extension(file.filename)
image_file_id = await _upload_product_image(
file=file,
user_id=ctx.get_user_id(),
business_id=business_id,
db=db,
context_id=str(product_id),
developer_data={"business_id": business_id, "product_id": product_id},
)
if payload:
payload.image_file_id = image_file_id
if hasattr(payload, "model_fields_set"):
payload.model_fields_set.add("image_file_id")
elif hasattr(payload, "__fields_set__"):
payload.__fields_set__.add("image_file_id")
if not payload:
raise ApiError("INVALID_PAYLOAD", "داده‌های محصول ارسال نشده است", http_status=400)
previous_warehouse_id = product.default_warehouse_id
if payload.opening_balance is not None:
if not has_business_permission_for_business(
ctx, db, business_id, "opening_balance", "edit"
):
raise ApiError(
"OPENING_BALANCE_PERMISSION_REQUIRED",
"برای ثبت تعداد اولیه به دسترسی ویرایش تراز افتتاحیه نیاز است",
http_status=403,
)
result = update_product_with_opening_balance(
db,
business_id,
ctx.get_user_id(),
product_id,
payload,
update_product_fn=update_product,
previous_warehouse_id=previous_warehouse_id,
)
else:
result = update_product(db, product_id, business_id, payload, user_id=ctx.get_user_id())
if not result:
raise ApiError("NOT_FOUND", "Product not found", http_status=404)
# حذف عکس قبلی اگر عکس جدید آپلود شده
if image_file_id and old_image_file_id and old_image_file_id != image_file_id:
from app.services.file_storage_service import FileStorageService
from uuid import UUID
storage_service = FileStorageService(db)
try:
await storage_service.delete_file(UUID(old_image_file_id))
except Exception:
pass # اگر حذف فایل با خطا مواجه شد، ادامه می‌دهیم
return success_response(data=format_datetime_fields(result["data"], request), request=request, message=result.get("message"))
@router.delete(
"/business/{business_id}/{product_id}",
summary="حذف محصول",
description="""
حذف یک محصول یا خدمت از سیستم
### نکات مهم:
- محصولات که در فاکتورها استفاده شده‌اند ممکن است قابل حذف نباشند
- در صورت وجود وابستگی‌ها، خطا برگردانده می‌شود
- تصویر محصول نیز همراه با محصول حذف می‌شود
- این عملیات غیرقابل بازگشت است
""",
response_model=SuccessResponse[Dict[str, bool]],
responses={
200: {
"description": "محصول با موفقیت حذف شد",
"content": {
"application/json": {
"example": {
"success": True,
"message": "کالا با موفقیت حذف شد",
"data": {
"deleted": True
}
}
}
}
},
400: {
"description": "محصول قابل حذف نیست (به دلیل وابستگی‌ها)",
"content": {
"application/json": {
"example": {
"success": False,
"error_code": "HAS_DEPENDENCIES",
"message": "این محصول در فاکتورها استفاده شده و قابل حذف نیست"
}
}
}
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز - نیاز به مجوز products.delete"
},
404: {
"description": "محصول یافت نشد",
"content": {
"application/json": {
"example": {
"success": False,
"error_code": "NOT_FOUND",
"message": "کالا یافت نشد"
}
}
}
}
}
)
@require_business_access("business_id")
def delete_product_endpoint(
request: Request,
business_id: int,
product_id: int,
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_by_entity_dep("products", "delete", Product, "product_id")),
) -> Dict[str, Any]:
from fastapi import HTTPException
success, error_message = delete_product(db, product_id, business_id)
if not success:
if error_message:
raise HTTPException(status_code=400, detail=error_message)
raise HTTPException(status_code=404, detail="کالا یافت نشد")
return success_response({"deleted": True}, request, message="کالا با موفقیت حذف شد")
@router.get(
"/business/{business_id}/{product_id}/usage-check",
summary="بررسی امکان حذف کالا (اسناد مرتبط)",
description="""
بررسی می‌کند آیا کالا در فاکتور، سند حسابداری، حواله انبار، BOM یا سایر وابستگی‌ها استفاده شده است.
برای پیش‌نمایش پاک‌سازی امن (بدون حذف واقعی) مناسب است.
""",
response_model=SuccessResponse[Dict[str, Any]],
responses={
200: {"description": "نتیجه بررسی استفاده"},
404: {"description": "کالا یافت نشد"},
},
)
@require_business_access("business_id")
def check_product_usage_endpoint(
request: Request,
business_id: int,
product_id: int,
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_by_entity_dep("products", "view", Product, "product_id")),
) -> Dict[str, Any]:
obj = db.get(Product, product_id)
if not obj or int(obj.business_id) != int(business_id):
raise HTTPException(status_code=404, detail="کالا یافت نشد")
has_documents, document_types = check_product_has_related_documents(db, product_id)
return success_response(
{
"product_id": product_id,
"is_used": bool(has_documents),
"can_delete": not bool(has_documents),
"document_types": document_types or [],
"is_active": bool(getattr(obj, "is_active", True)),
"name": getattr(obj, "name", None),
"code": getattr(obj, "code", None),
},
request,
message="بررسی استفاده کالا انجام شد",
)
@router.post(
"/business/{business_id}/bulk-delete",
summary="حذف گروهی محصولات",
description="""
حذف چندین محصول به صورت همزمان بر اساس شناسه‌ها یا کدها
### ویژگی‌ها:
- حذف بر اساس شناسه‌ها (ids) یا کدها (codes) یا هر دو
- در صورت وجود وابستگی، محصول نادیده گرفته می‌شود (skipped)
- نتیجه شامل تعداد حذف شده، نادیده گرفته شده و خطاها است
### نکات:
- محصولاتی که در فاکتورها استفاده شده‌اند قابل حذف نیستند
- تصاویر محصولات حذف شده نیز حذف می‌شوند
- این عملیات غیرقابل بازگشت است
""",
response_model=SuccessResponse[Dict[str, Any]],
responses={
200: {
"description": "عملیات حذف گروهی انجام شد",
"content": {
"application/json": {
"example": {
"success": True,
"data": {
"deleted": 5,
"skipped": 2,
"errors": [
{
"product_id": 123,
"code": "P1001",
"message": "این محصول در فاکتورها استفاده شده است"
}
]
}
}
}
}
},
400: {
"description": "خطا در اعتبارسنجی داده‌ها - ids و codes نباید هر دو خالی باشند"
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز - نیاز به مجوز products.delete"
}
}
)
@require_business_access("business_id")
def bulk_delete_products_endpoint(
request: Request,
business_id: int,
body: Dict[str, Any],
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("products", "delete")),
) -> Dict[str, Any]:
from sqlalchemy import and_ as _and
from adapters.db.models.product import Product
ids = body.get("ids")
codes = body.get("codes")
deleted = 0
skipped = 0
errors = []
if not ids and not codes:
return success_response({"deleted": 0, "skipped": 0, "errors": []}, request)
# Normalize inputs
if isinstance(ids, list):
ids = [int(x) for x in ids if isinstance(x, (int, str)) and str(x).isdigit()]
else:
ids = []
if isinstance(codes, list):
codes = [str(x).strip() for x in codes if str(x).strip()]
else:
codes = []
# Delete by IDs first
if ids:
for pid in ids:
try:
product = db.get(Product, pid)
if not product or product.business_id != business_id:
skipped += 1
errors.append(f"کالا با شناسه {pid} یافت نشد")
continue
success, error_message = delete_product(db, pid, business_id)
if success:
deleted += 1
else:
skipped += 1
if error_message:
errors.append(f"کالا {product.name or product.code or pid}: {error_message}")
else:
errors.append(f"کالا {product.name or product.code or pid}: امکان حذف وجود ندارد")
except Exception as e:
skipped += 1
errors.append(f"خطا در حذف کالا با شناسه {pid}: {str(e)}")
# Delete by codes
if codes:
try:
items = db.query(Product).filter(_and(Product.business_id == business_id, Product.code.in_(codes))).all()
for obj in items:
try:
success, error_message = delete_product(db, obj.id, business_id)
if success:
deleted += 1
else:
skipped += 1
if error_message:
errors.append(f"کالا {obj.name or obj.code or obj.id}: {error_message}")
else:
errors.append(f"کالا {obj.name or obj.code or obj.id}: امکان حذف وجود ندارد")
except Exception as e:
skipped += 1
errors.append(f"خطا در حذف کالا {obj.name or obj.code or obj.id}: {str(e)}")
except Exception as e:
# In case of query issues, treat all as skipped
skipped += len(codes)
errors.append(f"خطا در جستجوی کالاها: {str(e)}")
return success_response({
"deleted": deleted,
"skipped": skipped,
"errors": errors
}, request)
@router.post(
"/business/{business_id}/export/excel",
summary="خروجی Excel لیست محصولات",
description="""
دریافت لیست محصولات به صورت فایل Excel
### قابلیت‌ها:
- فیلتر محصولات بر اساس معیارهای مختلف
- انتخاب ستون‌های دلخواه برای خروجی
- مرتب‌سازی داده‌ها
- انتخاب محصولات خاص (selected_indices)
- پشتیبانی از RTL برای زبان فارسی
### فرمت فایل:
- فرمت: `.xlsx` (Excel 2007+)
- نام فایل شامل نام کسب‌وکار و timestamp است
""",
responses={
200: {
"description": "فایل Excel با موفقیت ایجاد شد",
"content": {
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": {
"schema": {
"type": "string",
"format": "binary"
}
}
},
"headers": {
"Content-Disposition": {
"description": "نام فایل خروجی",
"schema": {
"type": "string",
"example": "attachment; filename=products_business_20240115_103000.xlsx"
}
}
}
},
400: {
"description": "خطا در اعتبارسنجی درخواست"
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز - نیاز به مجوز products.export"
}
}
)
@require_business_access("business_id")
async def export_products_excel(
request: Request,
business_id: int,
body: dict,
background: BackgroundTasks,
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("products", "export")),
):
from fastapi.responses import Response
want_async = _body_truthy(body.get("async")) or _body_truthy(body.get("async_mode"))
calendar_type = (
request.headers.get("X-Calendar-Type")
or getattr(getattr(request, "state", None), "calendar_type", None)
or "jalali"
)
accept_language = request.headers.get("Accept-Language") or "fa"
if want_async:
queued = _enqueue_products_excel_export(
business_id=business_id,
user_id=int(ctx.get_user_id()),
body=body,
calendar_type=str(calendar_type),
accept_language=str(accept_language),
background=background,
request=request,
)
if queued is not None:
return queued
data, filename, _record_count = build_products_excel_export(
db,
business_id,
body,
calendar_type=str(calendar_type),
accept_language=str(accept_language),
)
return Response(
content=data,
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
headers={
"Content-Disposition": f"attachment; filename={filename}",
"Content-Length": str(len(data)),
"Access-Control-Expose-Headers": "Content-Disposition",
},
)
@router.post(
"/business/{business_id}/price-report/export/excel",
summary="خروجی Excel گزارش قیمت کالا (ستون‌های محدود)",
)
@require_business_access("business_id")
async def export_product_price_report_excel(
request: Request,
business_id: int,
body: dict,
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("products", "export")),
):
scoped_body = dict(body or {})
scoped_body["columns_profile"] = "price_report"
return await export_products_excel(
request=request,
business_id=business_id,
body=scoped_body,
ctx=ctx,
db=db,
_=None,
)
@router.post(
"/business/{business_id}/import/template",
summary="دانلود تمپلیت ایمپورت محصولات",
description="""
دریافت فایل Excel تمپلیت برای ایمپورت محصولات
### ویژگی‌ها:
- شامل تمام ستون‌های مورد نیاز برای ایمپورت
- هدرهای فارسی و انگلیسی
- نمونه داده برای راهنمایی
- پشتیبانی از RTL برای زبان فارسی
### استفاده:
1. این endpoint را فراخوانی کنید تا تمپلیت را دریافت کنید
2. فایل را با اطلاعات محصولات پر کنید
3. فایل را از طریق endpoint `/import/excel` آپلود کنید
### فرمت فایل:
- فرمت: `.xlsx` (Excel 2007+)
""",
responses={
200: {
"description": "فایل تمپلیت با موفقیت ایجاد شد",
"content": {
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": {
"schema": {
"type": "string",
"format": "binary"
}
}
},
"headers": {
"Content-Disposition": {
"description": "نام فایل تمپلیت",
"schema": {
"type": "string",
"example": "attachment; filename=products_import_template.xlsx"
}
}
}
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز - نیاز به مجوز products.edit"
}
}
)
@require_business_access("business_id")
async def download_products_import_template(
request: Request,
business_id: int,
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("products", "edit")),
):
from fastapi.responses import Response
locale = negotiate_locale(request.headers.get("Accept-Language"))
content, filename = build_products_import_template(db, business_id, locale=locale)
return Response(
content=content,
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
headers={
"Content-Disposition": f"attachment; filename={filename}",
"Access-Control-Expose-Headers": "Content-Disposition",
},
)
@router.post(
"/business/{business_id}/import/excel",
summary="ایمپورت محصولات از فایل Excel",
description="""
آپلود و پردازش فایل Excel برای ایمپورت محصولات
### قابلیت‌ها:
- پشتیبانی از فایل Excel (.xlsx)
- پردازش به صورت dry-run (پیش‌نمایش) یا واقعی
- ایجاد یا به‌روزرسانی محصولات بر اساس کد
- پشتیبانی از دسته‌بندی، مالیات، ویژگی‌ها، انبار پیش‌فرض و تعداد اولیه (تراز افتتاحیه)
- گزارش خطاها و هشدارها
### نکات مهم:
- برای دریافت تمپلیت از endpoint `/import/template` استفاده کنید
- در حالت dry-run تغییری در داده‌ها ایجاد نمی‌شود
- محصولات با کد یکسان به‌روزرسانی می‌شوند
- خطاهای اعتبارسنجی در پاسخ برگردانده می‌شوند
""",
response_model=SuccessResponse[Dict[str, Any]],
responses={
200: {
"description": "فایل با موفقیت پردازش شد",
"content": {
"application/json": {
"example": {
"success": True,
"message": "ایمپورت با موفقیت انجام شد",
"data": {
"total_rows": 100,
"processed": 95,
"created": 80,
"updated": 15,
"skipped": 5,
"errors": [
{
"row": 10,
"code": "P-010",
"message": "نام محصول الزامی است"
}
],
"warnings": []
}
}
}
}
},
400: {
"description": "خطا در اعتبارسنجی فایل یا داده‌ها"
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز - نیاز به مجوز products.edit"
},
413: {
"description": "حجم فایل بیش از حد مجاز است"
}
}
)
@require_business_access("business_id", offload_sync=True)
def import_products_excel(
request: Request,
business_id: int,
file: UploadFile = File(...),
dry_run: str = Form(default="true"),
match_by: str = Form(default="code"),
conflict_policy: str = Form(default="upsert"),
on_missing_category: str = Form(default="error"),
on_missing_attributes: str = Form(default="error"),
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("products", "edit")),
):
import io
import logging
import re
import time
import zipfile
from decimal import Decimal
from typing import Optional
from openpyxl import load_workbook
from sqlalchemy.exc import IntegrityError
from adapters.db.models.category import BusinessCategory
from adapters.db.models.product_attribute import ProductAttribute
from adapters.db.models.tax_type import TaxType
from adapters.db.models.tax_unit import TaxUnit
from adapters.db.models.warehouse import Warehouse
logger = logging.getLogger(__name__)
def _validate_excel_signature(content: bytes) -> bool:
try:
if not content.startswith(b'PK'):
return False
with zipfile.ZipFile(io.BytesIO(content), 'r') as zf:
return any(n.startswith('xl/') for n in zf.namelist())
except Exception:
return False
try:
import_started_at = time.perf_counter()
parse_finished_at = import_started_at
validation_finished_at = import_started_at
apply_elapsed_ms = 0.0
is_dry_run = str(dry_run).lower() in ("true","1","yes","on")
logger.info(f"[IMPORT] Starting Excel import - business_id={business_id}, dry_run={is_dry_run}, match_by={match_by}, conflict_policy={conflict_policy}")
on_missing_category = str(on_missing_category or "error").strip().lower()
on_missing_attributes = str(on_missing_attributes or "error").strip().lower()
if on_missing_category not in ("error", "create"):
on_missing_category = "error"
if on_missing_attributes not in ("error", "create"):
on_missing_attributes = "error"
preview_rows: list[dict] = []
reference_summary: dict[str, Any] = {
"resolved": {
"category": 0,
"tax_type": 0,
"tax_unit": 0,
"attributes": 0,
"warehouse": 0,
"currency": 0,
},
"would_create": {
"categories": 0,
"attributes": 0,
},
"created": {
"categories": 0,
"attributes": 0,
},
"opening_balance": {
"rows_with_opening_balance": 0,
"rows_cleared": 0,
},
"policies": {
"on_missing_category": on_missing_category,
"on_missing_attributes": on_missing_attributes,
},
}
if not file.filename or not file.filename.lower().endswith('.xlsx'):
raise ApiError("INVALID_FILE", "فرمت فایل معتبر نیست. تنها xlsx پشتیبانی می‌شود", http_status=400)
content = file.file.read()
logger.info(f"[IMPORT] File received - filename={file.filename}, size={len(content)} bytes")
if len(content) > MAX_PRODUCT_IMPORT_FILE_BYTES:
raise ApiError("FILE_TOO_LARGE", "حجم فایل بیش از حد مجاز است (حداکثر ۱۵ مگابایت)", http_status=413)
if len(content) < 100 or not _validate_excel_signature(content):
raise ApiError("INVALID_FILE", "فایل Excel معتبر نیست یا خالی است", http_status=400)
try:
wb = load_workbook(filename=io.BytesIO(content), data_only=True)
except zipfile.BadZipFile:
raise ApiError("INVALID_FILE", "فایل Excel خراب است یا فرمت آن معتبر نیست", http_status=400)
ws = select_products_import_worksheet(wb)
rows = list(ws.iter_rows(values_only=True))
parse_finished_at = time.perf_counter()
logger.info(f"[IMPORT] Excel file loaded - sheet={ws.title}, total rows={len(rows)}")
if not rows:
return success_response(data={"summary": {"total": 0}}, request=request, message="EMPTY_FILE")
raw_headers = [str(h).strip() if h is not None else "" for h in rows[0]]
headers = map_headers(raw_headers)
data_rows = rows[1:]
if len(data_rows) > MAX_PRODUCT_IMPORT_DATA_ROWS:
raise ApiError(
"TOO_MANY_ROWS",
f"تعداد ردیف‌ها بیشتر از حد مجاز است (حداکثر {MAX_PRODUCT_IMPORT_DATA_ROWS})",
http_status=400,
)
mapped_keys = {h for h in headers if h in ALL_COLUMN_KEYS}
can_edit_opening_balance = has_business_permission_for_business(
ctx, db, business_id, "opening_balance", "edit"
)
can_write_inventory = ctx.has_business_permission("inventory", "write")
warehouse_rows = db.query(Warehouse).filter(Warehouse.business_id == business_id).all()
warehouse_index = WarehouseImportIndex(warehouse_rows)
if "name" not in mapped_keys:
unmapped = [raw_headers[i] for i, h in enumerate(headers) if h not in ALL_COLUMN_KEYS]
raise ApiError(
"MISSING_COLUMNS",
"ستون الزامی «نام» در شیت کالاها یافت نشد. از تمپلیت رسمی استفاده کنید."
+ (f" (ستون‌های ناشناخته: {', '.join(unmapped[:5])})" if unmapped else ""),
http_status=400,
)
logger.info(f"[IMPORT] Headers parsed: {headers}, data rows count: {len(data_rows)}")
match_by = str(match_by or "code").strip().lower()
if match_by not in ("code", "name"):
match_by = "code"
conflict_policy = str(conflict_policy or "upsert").strip().lower()
if conflict_policy not in ("insert", "update", "upsert"):
conflict_policy = "upsert"
match_column_index = headers.index(match_by) if match_by in headers else None
match_keys: list[str] = []
if match_column_index is not None:
for raw_row in data_rows:
raw_value = (
raw_row[match_column_index]
if match_column_index < len(raw_row)
else None
)
match_keys.append(
product_match_key(match_by, {match_by: raw_value})
)
existing_product_index = load_existing_product_match_index(
db,
business_id,
match_by,
match_keys,
)
currency_index = (
load_business_currency_index(db, business_id)
if "price_fx_currency_code" in mapped_keys
or "price_fx_currency_id" in mapped_keys
else None
)
def _normalize_number_text(v: object) -> str:
if v is None:
return ""
s = str(v).strip()
if s == "":
return ""
# Handle negative numbers in parentheses: (123) => -123
if s.startswith("(") and s.endswith(")"):
s = "-" + s[1:-1]
# Convert Persian/Arabic digits to English
digit_map = str.maketrans({
"۰": "0", "۱": "1", "۲": "2", "۳": "3", "۴": "4",
"۵": "5", "۶": "6", "۷": "7", "۸": "8", "۹": "9",
"٠": "0", "١": "1", "٢": "2", "٣": "3", "٤": "4",
"٥": "5", "٦": "6", "٧": "7", "٨": "8", "٩": "9",
})
s = s.translate(digit_map)
# Normalize separators:
# - Thousands separators: "," "٬" "،" spaces
# - Decimal separator: "٫" -> "."
s = s.replace("\u066b", ".") # Arabic decimal separator
for ch in [",", "\u066c", "\u060c", " ", "\u00a0", "\u202f", "\u2009", "_"]:
s = s.replace(ch, "")
s = s.strip()
return s
def _parse_decimal(v: object) -> Optional[Decimal]:
s = _normalize_number_text(v)
if s == "":
return None
try:
return Decimal(s)
except Exception:
return None
def _parse_int(v: object) -> Optional[int]:
s = _normalize_number_text(v)
if s == "":
return None
try:
return int(s.split(".")[0])
except Exception:
return None
def _normalize_item_type(v: object) -> Optional[str]:
if v is None:
return None
s = str(v).strip()
if s == "":
return None
mapping = {"product": "کالا", "service": "خدمت"}
low = s.lower()
if low in mapping:
return mapping[low]
if s in ("کالا", "خدمت"):
return s
raise ValueError("نوع باید کالا یا خدمت باشد")
def _norm_text(v: object) -> str:
if v is None:
return ""
s = str(v).strip()
s = s.replace("\u200c", " ")
s = re.sub(r"\s+", " ", s).strip()
return s
def _norm_key(v: object) -> str:
return _norm_text(v).lower()
def _split_list_text(v: object) -> list[str]:
s = _norm_text(v)
if not s:
return []
# Support comma / Persian comma / semicolon / pipe
parts = re.split(r"[,\u060c;\|]+", s)
out: list[str] = []
for p in parts:
t = _norm_text(p)
if t:
out.append(t)
return out
# Preload reference data for faster resolve (only when needed).
categories_rows: list[BusinessCategory] | None = None
categories_by_parent: dict[int | None, list[BusinessCategory]] | None = None
categories_by_id: dict[int, BusinessCategory] | None = None
attrs_rows: list[ProductAttribute] | None = None
attrs_by_title: dict[str, ProductAttribute] | None = None
attr_ids: set[int] | None = None
tax_type_by_code: dict[str, int] | None = None
tax_type_by_title: dict[str, int] | None = None
tax_unit_by_code: dict[str, int] | None = None
tax_unit_by_name: dict[str, int] | None = None
def _ensure_categories_loaded() -> list[BusinessCategory]:
nonlocal categories_rows, categories_by_parent, categories_by_id
if categories_rows is None:
categories_rows = db.query(BusinessCategory).filter(BusinessCategory.business_id == business_id).all()
categories_by_parent = {}
categories_by_id = {}
for category in categories_rows:
categories_by_parent.setdefault(category.parent_id, []).append(category)
categories_by_id[int(category.id)] = category
return categories_rows
def _ensure_attributes_loaded() -> list[ProductAttribute]:
nonlocal attrs_rows, attrs_by_title, attr_ids
if attrs_rows is None:
attrs_rows = db.query(ProductAttribute).filter(ProductAttribute.business_id == business_id).all()
attrs_by_title = {
a.title.strip().lower(): a for a in attrs_rows if a.title
}
attr_ids = {int(a.id) for a in attrs_rows}
return attrs_rows
def _ensure_tax_types_loaded() -> None:
nonlocal tax_type_by_code, tax_type_by_title
if tax_type_by_code is not None and tax_type_by_title is not None:
return
rows = db.query(TaxType).all()
tax_type_by_code = {(r.code or "").strip().lower(): r.id for r in rows if r.code}
tax_type_by_title = {(r.title or "").strip().lower(): r.id for r in rows if r.title}
def _ensure_tax_units_loaded() -> None:
nonlocal tax_unit_by_code, tax_unit_by_name
if tax_unit_by_code is not None and tax_unit_by_name is not None:
return
rows = db.query(TaxUnit).all()
tax_unit_by_code = {(r.code or "").strip().lower(): r.id for r in rows if r.code}
tax_unit_by_name = {(r.name or "").strip().lower(): r.id for r in rows if r.name}
def _get_category_titles(cat: BusinessCategory) -> list[str]:
trans = cat.title_translations or {}
vals = []
for k in ("fa", "en"):
t = (trans.get(k) or "").strip()
if t:
vals.append(t)
# include any other translations
for t in trans.values():
tt = (t or "").strip()
if tt and tt not in vals:
vals.append(tt)
return vals
def _resolve_category_by_id(category_id: Optional[int]) -> tuple[Optional[int], Optional[str]]:
if category_id is None:
return None, None
_ensure_categories_loaded()
if categories_by_id is None or int(category_id) not in categories_by_id:
return None, f"دسته‌بندی با شناسه {category_id} یافت نشد"
return category_id, None
def _resolve_category_by_name_or_path(category_value: object, category_path: object) -> tuple[Optional[int], Optional[str], list[str]]:
"""
Returns: (category_id, error, created_paths)
created_paths is for reporting (best-effort).
"""
created_paths: list[str] = []
would_create_paths: list[str] = []
# If path provided, prefer it.
path_str = _norm_text(category_path)
name_str = _norm_text(category_value)
if not path_str and not name_str:
return None, None, created_paths
# Parse path segments
path = path_str or name_str
# split on common separators
segments = [s.strip() for s in re.split(r"[>/\u203a\u00bb]+", path) if s and str(s).strip()]
segments = [_norm_text(s) for s in segments if _norm_text(s)]
if not segments:
return None, None, created_paths
cats = _ensure_categories_loaded()
by_parent = categories_by_parent if categories_by_parent is not None else {}
parent_id: int | None = None
current_id: int | None = None
for seg in segments:
seg_norm = seg.strip().lower()
candidates: list[BusinessCategory] = []
for c in by_parent.get(parent_id, []):
titles = _get_category_titles(c)
if any(t.strip().lower() == seg_norm for t in titles):
candidates.append(c)
if len(candidates) == 1:
current_id = candidates[0].id
parent_id = current_id
continue
if len(candidates) > 1:
by_id = categories_by_id or {c.id: c for c in cats}
opts = [category_full_path(c, by_id, "fa") or _get_category_titles(c)[0] for c in candidates[:5]]
return None, (
f"دسته‌بندی «{seg}» مبهم است. مسیر کامل را بنویسید، مثلاً: {opts[0]}"
), created_paths
# no match
if on_missing_category == "create" and is_dry_run:
# In dry-run, don't fail the row; just report what would be created.
# We can't know the IDs yet, so keep category_id as None.
would_create_paths.append(seg if not would_create_paths else f"{would_create_paths[-1]} > {seg}")
# Also include remaining segments (all would be created under the last known parent).
# Continue collecting for reporting.
continue
if on_missing_category == "create" and (not is_dry_run):
# Create the category under current parent
from adapters.db.repositories.category_repository import CategoryRepository
repo = CategoryRepository(db)
obj = repo.create_category(
business_id=business_id,
parent_id=parent_id,
translations={"fa": seg, "en": seg},
auto_commit=False,
)
reference_summary["created"]["categories"] += 1
created_paths.append(seg if not created_paths else f"{created_paths[-1]} > {seg}")
# update local caches
cats.append(obj)
by_parent.setdefault(parent_id, []).append(obj)
if categories_by_id is not None:
categories_by_id[int(obj.id)] = obj
current_id = obj.id
parent_id = current_id
continue
return None, f"دسته‌بندی یافت نشد: '{seg}'", created_paths
# If we collected would_create paths during dry-run, return None (unknown id) but no error.
if would_create_paths:
# Encode as a special marker in created_paths for reporting (caller will read helper key).
# We don't want to change return signature widely; keep created_paths for real creates.
return None, None, created_paths + [f"__WOULD_CREATE__:{p}" for p in would_create_paths]
return current_id, None, created_paths
def _resolve_tax_type(item: dict, row_errors: list[str]) -> None:
# If id provided, accept as-is (no FK), but validate if possible
if item.get("tax_type_id") is not None:
# basic int already parsed
_ensure_tax_types_loaded()
tid = item.get("tax_type_id")
if isinstance(tid, int) and tax_type_by_code is not None and tax_type_by_title is not None:
if tid not in set(tax_type_by_code.values()) and tid not in set(tax_type_by_title.values()):
row_errors.append(f"شناسه نوع مالیات نامعتبر است: {tid}")
return
code = _norm_text(item.get("tax_type_code"))
if code:
_ensure_tax_types_loaded()
tid = (tax_type_by_code or {}).get(code.lower())
if not tid:
row_errors.append(f"نوع مالیات با کد '{code}' یافت نشد")
else:
item["tax_type_id"] = tid
return
title = _norm_text(item.get("tax_type_title"))
if title:
_ensure_tax_types_loaded()
tid = (tax_type_by_title or {}).get(title.lower())
if not tid:
row_errors.append(f"نوع مالیات با عنوان '{title}' یافت نشد")
else:
item["tax_type_id"] = tid
def _resolve_tax_unit(item: dict, row_errors: list[str]) -> None:
if item.get("tax_unit_id") is not None:
_ensure_tax_units_loaded()
uid = item.get("tax_unit_id")
if isinstance(uid, int) and tax_unit_by_code is not None and tax_unit_by_name is not None:
if uid not in set(tax_unit_by_code.values()) and uid not in set(tax_unit_by_name.values()):
row_errors.append(f"شناسه واحد مالیاتی نامعتبر است: {uid}")
return
code = _norm_text(item.get("tax_unit_code"))
if code:
_ensure_tax_units_loaded()
uid = (tax_unit_by_code or {}).get(code.lower())
if not uid:
row_errors.append(f"واحد مالیاتی با کد '{code}' یافت نشد")
else:
item["tax_unit_id"] = uid
return
name = _norm_text(item.get("tax_unit_name"))
if name:
_ensure_tax_units_loaded()
uid = (tax_unit_by_name or {}).get(name.lower())
if not uid:
row_errors.append(f"واحد مالیاتی با نام '{name}' یافت نشد")
else:
item["tax_unit_id"] = uid
def _resolve_attributes(item: dict, row_errors: list[str]) -> None:
# If attribute_ids already parsed (list[int]), validate and keep only valid; but report missing.
if isinstance(item.get("attribute_ids"), list) and item.get("attribute_ids"):
ids = [i for i in item["attribute_ids"] if isinstance(i, int)]
if not ids:
item["attribute_ids"] = []
return
# validate against business attributes
_ensure_attributes_loaded()
existing_ids = attr_ids or set()
missing = [str(i) for i in ids if i not in existing_ids]
item["attribute_ids"] = [i for i in ids if i in existing_ids]
if missing:
row_errors.append(f"شناسه(های) ویژگی نامعتبر: {', '.join(missing)}")
# If attribute_titles provided, resolve and optionally create
titles = _split_list_text(item.get("attribute_titles"))
if not titles:
return
attrs = _ensure_attributes_loaded()
by_title = attrs_by_title if attrs_by_title is not None else {}
resolved_ids: list[int] = []
missing_titles: list[str] = []
created: list[str] = []
for t in titles:
key = t.strip().lower()
found = by_title.get(key)
if found:
resolved_ids.append(found.id)
continue
if on_missing_attributes == "create" and (not is_dry_run):
from adapters.db.repositories.product_attribute_repository import ProductAttributeRepository
repo = ProductAttributeRepository(db)
try:
obj = repo.create(
business_id=business_id,
title=t,
description=None,
data_type="text",
options=None,
auto_commit=False,
)
reference_summary["created"]["attributes"] += 1
attrs.append(obj)
by_title[obj.title.strip().lower()] = obj
if attr_ids is not None:
attr_ids.add(int(obj.id))
resolved_ids.append(obj.id)
created.append(t)
except Exception:
missing_titles.append(t)
else:
missing_titles.append(t)
if created:
# store for later summary if needed
item["_created_attribute_titles"] = created
if missing_titles:
if on_missing_attributes == "create" and is_dry_run:
# In dry-run, just report what would be created; don't fail the row.
item["_would_create_attribute_titles"] = missing_titles
else:
row_errors.append(f"ویژگی(های) یافت نشد: {', '.join(missing_titles)}")
# merge with any existing attribute_ids already present
current = item.get("attribute_ids") if isinstance(item.get("attribute_ids"), list) else []
merged = list(dict.fromkeys([*(current or []), *resolved_ids]))
item["attribute_ids"] = merged
def _extract_would_create_paths(paths: list[str] | None) -> list[str]:
if not paths:
return []
out: list[str] = []
for p in paths:
if isinstance(p, str) and p.startswith("__WOULD_CREATE__:"):
out.append(p.split(":", 1)[1])
return out
def _find_existing_product_row(data: dict) -> Optional[Product]:
found, _err = find_existing_product_in_index(
existing_product_index,
match_by,
data,
)
return found
errors: list[dict] = []
valid_items: list[dict] = []
seen_import_match_keys: dict[str, int] = {}
for idx, row in enumerate(data_rows, start=2):
item: dict[str, Any] = {}
row_errors: list[str] = []
row_warnings: list[str] = []
row_preview: dict[str, Any] = {"row": idx, "resolved": {}, "would_create": {}, "warnings": []}
for ci, key in enumerate(headers):
if not key:
continue
val = row[ci] if ci < len(row) else None
if isinstance(val, str):
val = val.strip()
item[key] = val
if not any(v not in (None, "") for v in item.values()):
continue
if is_sample_import_row(item):
row_warnings.append("ردیف نمونه نادیده گرفته شد")
row_preview["warnings"] = row_warnings
preview_rows.append(row_preview)
continue
item["_row"] = idx
item["_provided_keys"] = provided_keys_from_raw(item)
if "item_type" in item:
try:
normalized_type = _normalize_item_type(item.get("item_type"))
except ValueError as exc:
row_errors.append(str(exc))
normalized_type = None
if normalized_type is None:
item.pop("item_type", None)
else:
item["item_type"] = normalized_type
for k in DECIMAL_KEYS:
if k in item:
raw = item.get(k)
if raw in (None, ""):
item[k] = None
else:
parsed_dec = _parse_decimal(raw)
if parsed_dec is None:
row_errors.append(f"مقدار عددی نامعتبر در ستون {k}")
item[k] = parsed_dec
for k in INT_KEYS:
if k in item:
raw = item.get(k)
if raw in (None, ""):
item[k] = None
else:
parsed_int = _parse_int(raw)
if parsed_int is None:
row_errors.append(f"مقدار عددی نامعتبر در ستون {k}")
item[k] = parsed_int
for k in BOOLEAN_KEYS:
if k not in item:
continue
try:
parsed_bool = parse_bool_strict(item.get(k))
except ValueError:
row_errors.append(f"مقدار بله/خیر نامعتبر در ستون {k}")
item.pop(k, None)
continue
if parsed_bool is None:
item.pop(k, None)
else:
item[k] = parsed_bool
if "inventory_mode" in item:
try:
mode = parse_inventory_mode(item.get("inventory_mode"))
except ValueError as exc:
row_errors.append(str(exc))
item.pop("inventory_mode", None)
else:
if mode is None:
item.pop("inventory_mode", None)
else:
item["inventory_mode"] = mode
if "attribute_ids" in item and item["attribute_ids"]:
try:
parts = [p.strip() for p in str(item["attribute_ids"]).split(",") if p and p.strip()]
item["attribute_ids"] = [int(p) for p in parts if p.isdigit()]
except Exception:
item["attribute_ids"] = []
elif "attribute_ids" in item:
item.pop("attribute_ids", None)
# Resolve references: category, tax, attributes
# Category: accept category_id, or resolve from category_path/category (name), optionally create
cat_id, cat_err = _resolve_category_by_id(item.get("category_id"))
if cat_err:
# allow resolve by path/name as fallback
resolved_id, resolved_err, _created_paths = _resolve_category_by_name_or_path(item.get("category"), item.get("category_path"))
if resolved_err:
row_errors.append(cat_err + " / " + resolved_err)
else:
item["category_id"] = resolved_id
would_create = _extract_would_create_paths(_created_paths)
if would_create:
row_preview["would_create"]["categories"] = would_create
reference_summary["would_create"]["categories"] += len(would_create)
row_warnings.append("دسته‌بندی پیدا نشد و در حالت create ساخته خواهد شد")
elif resolved_id is not None:
row_preview["resolved"]["category"] = {"by": "path_or_name", "category_id": resolved_id}
reference_summary["resolved"]["category"] += 1
else:
if cat_id is not None:
row_preview["resolved"]["category"] = {"by": "id", "category_id": cat_id}
reference_summary["resolved"]["category"] += 1
# if category_id not provided but path/name provided, resolve it
if item.get("category_id") is None and (item.get("category") or item.get("category_path")):
resolved_id, resolved_err, _created_paths = _resolve_category_by_name_or_path(item.get("category"), item.get("category_path"))
if resolved_err:
row_errors.append(resolved_err)
else:
item["category_id"] = resolved_id
# detect dry-run would create marker
would_create = _extract_would_create_paths(_created_paths)
if would_create:
row_preview["would_create"]["categories"] = would_create
reference_summary["would_create"]["categories"] += len(would_create)
row_warnings.append("دسته‌بندی پیدا نشد و در حالت create ساخته خواهد شد")
elif resolved_id is not None:
row_preview["resolved"]["category"] = {"by": "path_or_name", "category_id": resolved_id}
reference_summary["resolved"]["category"] += 1
before_tax_type = item.get("tax_type_id")
_resolve_tax_type(item, row_errors)
if before_tax_type is None and item.get("tax_type_id") is not None:
row_preview["resolved"]["tax_type"] = {"tax_type_id": item.get("tax_type_id")}
reference_summary["resolved"]["tax_type"] += 1
before_tax_unit = item.get("tax_unit_id")
_resolve_tax_unit(item, row_errors)
if before_tax_unit is None and item.get("tax_unit_id") is not None:
row_preview["resolved"]["tax_unit"] = {"tax_unit_id": item.get("tax_unit_id")}
reference_summary["resolved"]["tax_unit"] += 1
before_attrs = list(item.get("attribute_ids") or []) if isinstance(item.get("attribute_ids"), list) else []
_resolve_attributes(item, row_errors)
after_attrs = list(item.get("attribute_ids") or []) if isinstance(item.get("attribute_ids"), list) else []
if len(after_attrs) > len(before_attrs):
row_preview["resolved"]["attributes"] = {"attribute_ids": after_attrs}
reference_summary["resolved"]["attributes"] += (len(after_attrs) - len(before_attrs))
if "_would_create_attribute_titles" in item:
would_titles = item.get("_would_create_attribute_titles") or []
if isinstance(would_titles, list) and would_titles:
row_preview["would_create"]["attributes"] = would_titles
reference_summary["would_create"]["attributes"] += len(would_titles)
row_warnings.append("برخی ویژگی‌ها وجود ندارند و در حالت create ساخته خواهند شد")
resolve_fx_currency(
item,
db,
business_id,
row_errors,
currency_index=currency_index,
)
if item.get("price_fx_currency_id") is not None and "price_fx_currency_code" in (item.get("_provided_keys") or set()):
row_preview["resolved"]["currency"] = {"price_fx_currency_id": item.get("price_fx_currency_id")}
reference_summary["resolved"]["currency"] += 1
existing_for_type = _find_existing_product_row(item)
effective_item_type = item.get("item_type")
if not effective_item_type and existing_for_type is not None:
existing_type = existing_for_type.item_type
effective_item_type = existing_type.value if hasattr(existing_type, "value") else str(existing_type)
if str(effective_item_type or "کالا").strip() == "خدمت":
if warehouse_columns_mapped(mapped_keys) and (
item.get("default_warehouse_id") is not None
or item.get(WAREHOUSE_CODE_KEY)
or item.get(WAREHOUSE_NAME_KEY)
):
row_warnings.append("کالاهای خدماتی انبار پیش‌فرض ندارند؛ مقدار انبار نادیده گرفته می‌شود")
item.pop("default_warehouse_id", None)
else:
wh_errors, wh_preview = resolve_import_default_warehouse(
item, mapped_keys, warehouse_index
)
if wh_errors:
row_errors.extend(wh_errors)
elif wh_preview:
row_preview["resolved"]["warehouse"] = wh_preview
reference_summary["resolved"]["warehouse"] += 1
existing_for_ob = None
if opening_balance_columns_mapped(mapped_keys):
existing_for_ob = _find_existing_product_row(item)
ob_item = dict(item)
if existing_for_ob is not None:
if "track_inventory" not in item:
ob_item["track_inventory"] = existing_for_ob.track_inventory
if "item_type" not in item:
existing_type = existing_for_ob.item_type
ob_item["item_type"] = existing_type.value if hasattr(existing_type, "value") else str(existing_type)
if "inventory_mode" not in item:
ob_item["inventory_mode"] = existing_for_ob.inventory_mode
if item.get("default_warehouse_id") is None:
ob_item["default_warehouse_id"] = existing_for_ob.default_warehouse_id
ob_input, ob_errors, ob_warnings, ob_preview = prepare_opening_balance_for_import_row(
item=ob_item,
mapped_keys=mapped_keys,
business_id=business_id,
db=db,
can_edit_opening_balance=can_edit_opening_balance,
is_update=existing_for_ob is not None,
can_write_inventory=can_write_inventory,
existing_product=existing_for_ob,
warehouse_index=warehouse_index,
validate_document_context=False,
)
if ob_item.get("default_warehouse_id") is not None:
item["default_warehouse_id"] = ob_item.get("default_warehouse_id")
row_errors.extend(ob_errors)
row_warnings.extend(ob_warnings)
if ob_preview:
row_preview["opening_balance"] = ob_preview
if ob_preview.get("action") == "clear":
reference_summary["opening_balance"]["rows_cleared"] += 1
elif ob_preview.get("action") == "upsert":
reference_summary["opening_balance"]["rows_with_opening_balance"] += 1
if ob_input is not None:
item["opening_balance"] = ob_input.model_dump()
name = item.get("name")
if not name or str(name).strip() == "":
row_errors.append("نام الزامی است")
code = item.get("code")
if code is not None:
code_str = str(code).strip()
if code_str == "" or code_str.lower() == "none":
item["code"] = None
provided = item.get("_provided_keys")
if isinstance(provided, set):
provided.discard("code")
else:
item["code"] = code_str
import_match_key = product_match_key(match_by, item)
if import_match_key and not row_errors:
previous_row = seen_import_match_keys.get(import_match_key)
if previous_row is not None:
row_errors.append(
f"کلید تطبیق در فایل تکراری است (ردیف قبلی: {previous_row})"
)
else:
seen_import_match_keys[import_match_key] = idx
_, match_err = find_existing_product_in_index(
existing_product_index,
match_by,
item,
)
if match_err:
row_errors.append(match_err)
if row_errors:
errors.append({"row": idx, "errors": row_errors})
logger.debug(f"[IMPORT] Row {idx} validation failed: {row_errors}")
if is_dry_run:
row_preview["warnings"] = row_warnings
preview_rows.append(row_preview)
continue
item.pop("_created_attribute_titles", None)
item.pop("_would_create_attribute_titles", None)
valid_items.append(item)
logger.debug(f"[IMPORT] Row {idx} validated successfully - name={item.get('name')}, code={item.get('code')}")
if is_dry_run:
row_preview["warnings"] = row_warnings
preview_rows.append(row_preview)
inserted = 0
updated = 0
skipped = 0
skipped_apply = 0
would_insert = 0
would_update = 0
would_skip_conflict = 0
warehouse_sync: dict[str, Any] = {
"receipts_created": 0,
"issues_created": 0,
"lines_created": 0,
"unchanged_lines": 0,
"posted": not is_dry_run,
"document_ids": [],
}
items_with_opening_balance = [
item for item in valid_items if item.get("opening_balance") is not None
]
if items_with_opening_balance:
try:
validate_product_opening_balance_allowed(
db,
business_id,
None,
for_update=True,
)
except ApiError as exc:
message = api_error_message(exc)
invalid_rows = {int(item.get("_row")) for item in items_with_opening_balance}
for item in items_with_opening_balance:
errors.append({"row": item.get("_row"), "errors": [message]})
valid_items = [
item
for item in valid_items
if int(item.get("_row")) not in invalid_rows
]
existing_ob_candidates: dict[int, int] = {}
for item in valid_items:
if item.get("opening_balance") is None:
continue
existing_candidate, _ = find_existing_product_in_index(
existing_product_index, match_by, item
)
if existing_candidate is not None:
existing_ob_candidates[int(item["_row"])] = int(existing_candidate.id)
if existing_ob_candidates:
from app.services.opening_balance_warehouse_sync_service import (
find_products_with_unrelated_posted_warehouse_history,
)
conflicting_product_ids = find_products_with_unrelated_posted_warehouse_history(
db,
business_id,
sorted(set(existing_ob_candidates.values())),
)
if conflicting_product_ids:
conflict_rows = {
row_id
for row_id, product_id in existing_ob_candidates.items()
if product_id in conflicting_product_ids
}
for row_id in sorted(conflict_rows):
errors.append({
"row": row_id,
"errors": [
"برای این کالا قبلاً گردش انبار ثبت شده است؛ اختلاف را با رسید/حواله یا تعدیل انبار ثبت کنید"
],
})
valid_items = [
item for item in valid_items if int(item.get("_row")) not in conflict_rows
]
if is_dry_run:
candidate_items = [
item for item in valid_items if item.get("opening_balance") is not None
]
candidate_warehouses = {
int(item["opening_balance"]["warehouse_id"])
for item in candidate_items
if item.get("opening_balance") and item["opening_balance"].get("warehouse_id")
}
warehouse_sync.update({
"candidate_warehouses": len(candidate_warehouses),
"candidate_lines": len(candidate_items),
"posted": False,
})
validation_finished_at = time.perf_counter()
logger.info(f"[IMPORT] Processing summary - total_rows={len(data_rows)}, valid_items={len(valid_items)}, errors={len(errors)}, is_dry_run={is_dry_run}")
from adapters.api.v1.schema_models.product import ProductCreateRequest, ProductUpdateRequest
from app.services.product_service import create_product, update_product
from pydantic import ValidationError
user_id = ctx.get_user_id()
for data in valid_items:
existing, _match_err = find_existing_product_in_index(
existing_product_index,
match_by,
data,
)
if existing is None:
if conflict_policy == "update":
would_skip_conflict += 1
else:
would_insert += 1
elif conflict_policy == "insert":
would_skip_conflict += 1
else:
would_update += 1
if not is_dry_run and valid_items:
apply_started_at = time.perf_counter()
logger.info(f"[IMPORT] Starting REAL import (not dry-run) for {len(valid_items)} items")
opening_balance_changes: list[dict[str, Any]] = []
try:
for data in valid_items:
row_idx = data.get("_row")
provided = data.get("_provided_keys") if isinstance(data.get("_provided_keys"), set) else provided_keys_from_raw(data)
existing, _match_err = find_existing_product_in_index(
existing_product_index,
match_by,
data,
)
if existing is None:
if conflict_policy == "update":
skipped += 1
errors.append({"row": row_idx, "errors": ["کالای منطبق یافت نشد؛ سیاست فقط به‌روزرسانی است"]})
continue
create_payload = build_create_payload(data)
try:
product_request = ProductCreateRequest(**create_payload)
except ValidationError as validation_error:
errors.append({"row": row_idx, "errors": format_pydantic_errors(validation_error)})
skipped_apply += 1
continue
result = create_product(
db,
business_id,
product_request,
defer_cache_invalidation=True,
auto_commit=False,
serialize_result=False,
prevalidated_code_uniqueness=match_by == "code",
prevalidated_attribute_ids=True,
)
ob = product_request.opening_balance
if ob is not None:
opening_balance_changes.append({
"product_id": int(result["data"]["id"]),
"product_name": result["data"].get("name") or data.get("name"),
"warehouse_id": int(ob.warehouse_id or result["data"].get("default_warehouse_id")),
"quantity": float(ob.quantity),
"cost_price": float(ob.cost_price or 0),
})
inserted += 1
elif conflict_policy == "insert":
skipped += 1
else:
update_payload = build_update_payload(data, provided)
try:
update_request = ProductUpdateRequest(**update_payload)
except ValidationError as validation_error:
errors.append({"row": row_idx, "errors": format_pydantic_errors(validation_error)})
skipped_apply += 1
continue
previous_warehouse_id = existing.default_warehouse_id
result = update_product(
db,
existing.id,
business_id,
update_request,
user_id=user_id,
defer_cache_invalidation=True,
auto_commit=False,
serialize_result=False,
prevalidated_code_uniqueness=match_by == "code",
prevalidated_attribute_ids=True,
)
ob = update_request.opening_balance
if ob is not None and result:
opening_balance_changes.append({
"product_id": int(existing.id),
"product_name": result["data"].get("name") or data.get("name"),
"warehouse_id": int(ob.warehouse_id or result["data"].get("default_warehouse_id") or previous_warehouse_id),
"previous_warehouse_id": previous_warehouse_id,
"quantity": float(ob.quantity),
"cost_price": float(ob.cost_price or 0),
})
updated += 1
if opening_balance_changes:
opening_balance_result = apply_product_opening_balance_changes(
db,
business_id,
user_id,
opening_balance_changes,
auto_commit=False,
)
from app.services.opening_balance_warehouse_sync_service import (
sync_opening_balance_warehouse_documents,
)
warehouse_sync = sync_opening_balance_warehouse_documents(
db,
business_id,
user_id,
int(opening_balance_result["id"]),
[int(change["product_id"]) for change in opening_balance_changes],
)
db.commit()
except IntegrityError as exc:
db.rollback()
raise ApiError(
"IMPORT_WRITE_CONFLICT",
"داده‌های کالا هم‌زمان تغییر کرده‌اند یا مقدار یکتای تکراری ثبت شده است؛ فایل را دوباره بررسی کنید",
http_status=409,
) from exc
except Exception:
db.rollback()
raise
if inserted or updated:
invalidate_products_cache(business_id=business_id)
invalidate_public_catalog_caches()
apply_elapsed_ms = (time.perf_counter() - apply_started_at) * 1000
else:
if is_dry_run:
logger.info("[IMPORT] DRY-RUN mode - skipping actual database operations")
else:
logger.warning("[IMPORT] No valid items to process (valid_items is empty)")
summary = {
"total": len(data_rows),
"valid": len(valid_items),
"invalid": len(errors),
"inserted": inserted,
"updated": updated,
"skipped": skipped,
"skipped_apply": skipped_apply,
"dry_run": is_dry_run,
"would_insert": would_insert,
"would_update": would_update,
"would_skip_conflict": would_skip_conflict,
}
performance = {
"parse_ms": round((parse_finished_at - import_started_at) * 1000, 2),
"validation_ms": round((validation_finished_at - parse_finished_at) * 1000, 2),
"apply_ms": round(apply_elapsed_ms, 2),
"total_ms": round((time.perf_counter() - import_started_at) * 1000, 2),
}
logger.info("[IMPORT] Final summary=%s performance=%s", summary, performance)
logger.info(f"[IMPORT] Import completed - inserted={inserted}, updated={updated}, skipped={skipped}")
return success_response(
data={
"summary": summary,
"errors": errors,
"reference_summary": reference_summary,
"warehouse_sync": warehouse_sync,
"preview": preview_rows if is_dry_run else None,
"performance": performance,
},
request=request,
message="PRODUCTS_IMPORT_RESULT",
)
except ApiError:
raise
except Exception as e:
logger.error(f"Import error: {e}", exc_info=True)
raise ApiError("IMPORT_ERROR", f"خطا در پردازش فایل: {e}", http_status=500)
@router.post(
"/business/{business_id}/export/pdf",
summary="خروجی PDF لیست محصولات",
description="""
دریافت لیست محصولات به صورت فایل PDF
### قابلیت‌ها:
- فیلتر محصولات بر اساس معیارهای مختلف
- انتخاب ستون‌های دلخواه برای خروجی
- مرتب‌سازی داده‌ها
- پشتیبانی از تقویم شمسی و میلادی (بر اساس header X-Calendar-Type)
- پشتیبانی از RTL برای زبان فارسی
### فرمت فایل:
- فرمت: `.pdf`
- نام فایل شامل نام کسب‌وکار و timestamp است
""",
responses={
200: {
"description": "فایل PDF با موفقیت ایجاد شد",
"content": {
"application/pdf": {
"schema": {
"type": "string",
"format": "binary"
}
}
},
"headers": {
"Content-Disposition": {
"description": "نام فایل خروجی",
"schema": {
"type": "string",
"example": "attachment; filename=products_business_20240115_103000.pdf"
}
}
}
},
400: {
"description": "خطا در اعتبارسنجی درخواست"
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز - نیاز به مجوز products.export"
}
}
)
@require_business_access("business_id")
async def export_products_pdf(
request: Request,
business_id: int,
body: dict,
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("products", "export")),
):
import json
import datetime
import re
from fastapi.responses import Response
from weasyprint import HTML, CSS
from weasyprint.text.fonts import FontConfiguration
# Apply selected rows filter if requested
selected_only = bool(body.get('selected_only', False))
selected_row_keys = body.get('selected_row_keys')
selected_indices = body.get('selected_indices')
has_selected_row_keys = (
selected_only
and isinstance(selected_row_keys, list)
and any(isinstance(k, dict) and k.get("id") is not None for k in selected_row_keys)
)
# خروجی کامل (یا انتخاب با id): کل نتایج فیلترشده؛ نه فقط صفحه UI.
max_export_records = 10000
if selected_only and not has_selected_row_keys:
take = max(1, min(int(body.get("take", 1000)), max_export_records))
skip = max(0, int(body.get("skip", 0)))
else:
take = max_export_records
skip = 0
include_inventory = _should_include_inventory_for_product_export(body)
query_dict = {
"take": take,
"skip": skip,
"sort_by": body.get("sort_by"),
"sort_desc": bool(body.get("sort_desc", False)),
"sort": body.get("sort") if isinstance(body.get("sort"), list) else None,
"search": body.get("search"),
"search_fields": body.get("search_fields") or body.get("searchFields"),
"filters": body.get("filters"),
"category_ids": body.get("category_ids") or body.get("categoryIds"),
"include_inventory": include_inventory,
"inventory_as_of_date": body.get("inventory_as_of_date") or body.get("inventoryAsOfDate"),
}
result = list_products(db, business_id, query_dict)
items = result.get("items", [])
items = [format_datetime_fields(item, request) for item in items]
if selected_only and isinstance(selected_row_keys, list):
try:
wanted_ids = set()
for key in selected_row_keys:
if isinstance(key, dict) and key.get("id") is not None:
wanted_ids.add(int(key.get("id")))
if wanted_ids:
filtered = []
for it in items:
try:
if int(it.get("id")) in wanted_ids:
filtered.append(it)
except Exception:
continue
items = filtered
except Exception:
pass
if selected_only and selected_indices is not None and (not isinstance(selected_row_keys, list) or not selected_row_keys or not items):
indices = None
if isinstance(selected_indices, str):
try:
indices = json.loads(selected_indices)
except (json.JSONDecodeError, TypeError):
indices = None
elif isinstance(selected_indices, list):
indices = selected_indices
if isinstance(indices, list):
items = [items[i] for i in indices if isinstance(i, int) and 0 <= i < len(items)]
report_mode = str(body.get("report_mode") or "base_plus_price_lists").strip().lower()
if report_mode not in {"base_only", "price_lists_only", "base_plus_price_lists"}:
report_mode = "base_plus_price_lists"
raw_price_list_ids = body.get("price_list_ids") or []
price_list_ids = []
if isinstance(raw_price_list_ids, list):
seen = set()
for v in raw_price_list_ids:
try:
pid = int(v)
except Exception:
continue
if pid not in seen:
seen.add(pid)
price_list_ids.append(pid)
max_items = body.get("limit")
try:
max_items = int(max_items) if max_items is not None else None
except Exception:
max_items = None
if max_items is not None:
max_items = max(1, min(max_items, 10000))
items = items[:max_items]
def _format_price_list_value(rows):
if not rows:
return ""
parts = []
for price, currency_code, tier_name, min_qty in rows:
tier_label = (tier_name or "").strip() or "base"
cur_label = (currency_code or "").strip() or "-"
qty_text = ""
try:
if min_qty is not None and float(min_qty) > 0:
qty_text = f" (>= {min_qty})"
except Exception:
qty_text = ""
parts.append(f"{tier_label}{qty_text} [{cur_label}]: {price}")
return " | ".join(parts)
dynamic_price_columns = []
if report_mode in {"price_lists_only", "base_plus_price_lists"} and price_list_ids and items:
product_ids = []
for it in items:
try:
product_ids.append(int(it.get("id")))
except Exception:
continue
if product_ids:
pl_rows = db.query(PriceList.id, PriceList.name).filter(
PriceList.business_id == business_id,
PriceList.id.in_(price_list_ids),
).all()
price_list_name_by_id = {int(pid): (name or f"#{pid}") for pid, name in pl_rows}
pi_rows = (
db.query(
PriceItem.product_id,
PriceItem.price_list_id,
PriceItem.price,
Currency.code,
PriceItem.tier_name,
PriceItem.min_qty,
)
.join(PriceList, PriceList.id == PriceItem.price_list_id)
.outerjoin(Currency, Currency.id == PriceItem.currency_id)
.filter(
PriceList.business_id == business_id,
PriceItem.product_id.in_(product_ids),
PriceItem.price_list_id.in_(price_list_ids),
)
.order_by(PriceItem.price_list_id.asc(), PriceItem.min_qty.asc(), PriceItem.tier_name.asc())
.all()
)
grouped = {}
for product_id, price_list_id, price, currency_code, tier_name, min_qty in pi_rows:
key = (int(product_id), int(price_list_id))
grouped.setdefault(key, []).append((price, currency_code, tier_name, min_qty))
for price_list_id in price_list_ids:
if price_list_id not in price_list_name_by_id:
continue
col_key = f"price_list_{price_list_id}"
col_label = f"لیست قیمت: {price_list_name_by_id[price_list_id]}"
dynamic_price_columns.append((col_key, col_label))
for it in items:
try:
pid = int(it.get("id"))
except Exception:
continue
it[col_key] = _format_price_list_value(grouped.get((pid, price_list_id), []))
columns_profile = str(body.get("columns_profile") or "").strip().lower()
force_price_report_columns = columns_profile in {"price_report", "product_price_report"}
export_columns = body.get("export_columns")
if export_columns and isinstance(export_columns, list) and not force_price_report_columns:
headers = [col.get("label") or col.get("key") for col in export_columns]
keys = [col.get("key") for col in export_columns]
else:
default_cols = [
("code", "کد"),
("name", "نام"),
("category_name", "دسته"),
]
if report_mode in {"base_only", "base_plus_price_lists"}:
default_cols.extend([
("base_sales_price", "قیمت فروش پایه"),
("base_purchase_price", "قیمت خرید پایه"),
])
if dynamic_price_columns:
default_cols.extend(dynamic_price_columns)
if not force_price_report_columns:
default_cols.extend([
("main_unit", "واحد اصلی"),
("secondary_unit", "واحد فرعی"),
("track_inventory", "کنترل موجودی"),
("created_at_formatted", "ایجاد"),
])
keys = [k for k, _ in default_cols]
headers = [v for _, v in default_cols]
# Locale and direction
locale = negotiate_locale(request.headers.get("Accept-Language"))
is_fa = (locale == 'fa')
html_lang = 'fa' if is_fa else 'en'
html_dir = 'rtl' if is_fa else 'ltr'
_enrich_product_export_items(items, is_fa=is_fa)
# Load business info for header
business_name = ""
try:
biz = db.query(Business).filter(Business.id == business_id).first()
if biz is not None:
business_name = biz.name or ""
except Exception:
business_name = ""
# Escape helper
def escape(s: Any) -> str:
try:
return str(s).replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
except Exception:
return str(s)
# Build rows
rows_html = []
for item in items:
tds = []
for key in keys:
value = item.get(key)
if value is None:
value = ""
elif isinstance(value, list):
value = ", ".join(str(v) for v in value)
tds.append(f"<td>{escape(value)}</td>")
rows_html.append(f"<tr>{''.join(tds)}</tr>")
headers_html = ''.join(f"<th>{escape(h)}</th>" for h in headers)
# Format report datetime based on X-Calendar-Type header
calendar_header = request.headers.get("X-Calendar-Type", "jalali").lower()
try:
from app.core.calendar import CalendarConverter
formatted_now = CalendarConverter.format_datetime(datetime.datetime.now(),
"jalali" if calendar_header in ["jalali", "persian", "shamsi"] else "gregorian")
now_str = formatted_now.get('formatted', formatted_now.get('date_time', ''))
except Exception:
now_str = datetime.datetime.now().strftime('%Y/%m/%d %H:%M')
title_text = "گزارش فهرست محصولات" if is_fa else "Products List Report"
label_biz = "نام کسب‌وکار" if is_fa else "Business Name"
label_date = "تاریخ گزارش" if is_fa else "Report Date"
footer_text = "تولید شده توسط Hesabix" if is_fa else "Generated by Hesabix"
page_label_left = "صفحه " if is_fa else "Page "
page_label_of = " از " if is_fa else " of "
# تلاش برای رندر با قالب سفارشی (products/list)
resolved_html = None
try:
from app.services.report_template_service import ReportTemplateService
explicit_template_id = None
try:
if body.get("template_id") is not None:
explicit_template_id = int(body.get("template_id"))
except Exception:
explicit_template_id = None
template_context = {
"title_text": title_text,
"business_name": business_name,
"generated_at": now_str,
"is_fa": is_fa,
"headers": headers,
"keys": keys,
"items": items,
"table_headers_html": headers_html,
"table_rows_html": "".join(rows_html),
}
resolved_html = ReportTemplateService.try_render_resolved(
db=db,
business_id=business_id,
module_key="products",
subtype="list",
context=template_context,
explicit_template_id=explicit_template_id,
)
except Exception:
resolved_html = None
# Inject Persian fonts (YekanBakhFaNum/Vazirmatn) for PDF rendering
fa_font_url_regular = ""
fa_font_url_bold = ""
try:
if is_fa:
from app.services.pdf.template_renderer import load_farsi_font_data_uris
fa_reg, fa_bold = load_farsi_font_data_uris()
fa_font_url_regular = fa_reg or ""
fa_font_url_bold = fa_bold or ""
except Exception:
fa_font_url_regular = ""
fa_font_url_bold = ""
font_face_css = ""
if is_fa and fa_font_url_regular:
font_face_css += f"""
@font-face {{ font-family: 'YekanBakhFaNum'; src: url('{fa_font_url_regular}') format('truetype'); font-weight: 400; font-style: normal; }}
"""
if is_fa and fa_font_url_bold:
font_face_css += f"""
@font-face {{ font-family: 'YekanBakhFaNum'; src: url('{fa_font_url_bold}') format('truetype'); font-weight: 700; font-style: normal; }}
"""
body_font_family = "YekanBakhFaNum, Vazirmatn, Tahoma, Arial, sans-serif" if is_fa else "Arial, sans-serif"
table_html = f"""
<html lang=\"{html_lang}\" dir=\"{html_dir}\">
<head>
<meta charset='utf-8'>
<style>
{font_face_css}
@page {{
size: A4 landscape;
margin: 12mm;
@bottom-{ 'left' if is_fa else 'right' } {{
content: "{page_label_left}" counter(page) "{page_label_of}" counter(pages);
font-size: 10px;
color: #666;
font-family: {body_font_family};
}}
}}
body {{
font-family: {body_font_family};
font-size: 11px;
color: #222;
}}
.header {{
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
border-bottom: 2px solid #444;
padding-bottom: 6px;
}}
.title {{
font-size: 16px;
font-weight: 700;
}}
.meta {{
font-size: 11px;
color: #555;
}}
.table-wrapper {{
width: 100%;
}}
table.report-table {{
width: 100%;
border-collapse: collapse;
table-layout: fixed;
}}
thead th {{
background: #f0f3f7;
border: 1px solid #c7cdd6;
padding: 6px 4px;
text-align: center;
font-weight: 700;
white-space: nowrap;
}}
tbody td {{
border: 1px solid #d7dde6;
padding: 5px 4px;
vertical-align: top;
overflow-wrap: anywhere;
word-break: break-word;
white-space: normal;
}}
.footer {{
position: running(footer);
font-size: 10px;
color: #666;
margin-top: 8px;
text-align: {'left' if is_fa else 'right'};
}}
</style>
</head>
<body>
<div class=\"header\">
<div>
<div class=\"title\">{title_text}</div>
<div class=\"meta\">{label_biz}: {escape(business_name)}</div>
</div>
<div class=\"meta\">{label_date}: {escape(now_str)}</div>
</div>
<div class=\"table-wrapper\">
<table class=\"report-table\">
<thead>
<tr>{headers_html}</tr>
</thead>
<tbody>
{''.join(rows_html)}
</tbody>
</table>
</div>
<div class=\"footer\">{footer_text}</div>
</body>
</html>
"""
final_html = resolved_html or table_html
font_config = FontConfiguration()
pdf_bytes = HTML(string=final_html).write_pdf(font_config=font_config)
# Build meaningful filename
biz_name = business_name
def slugify(text: str) -> str:
return re.sub(r"[^A-Za-z0-9_-]+", "_", text).strip("_")
base = "products"
if biz_name:
base += f"_{slugify(biz_name)}"
if selected_only:
base += "_selected"
filename = f"{base}_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
return Response(
content=pdf_bytes,
media_type="application/pdf",
headers={
"Content-Disposition": f"attachment; filename={filename}",
"Content-Length": str(len(pdf_bytes)),
"Access-Control-Expose-Headers": "Content-Disposition",
},
)
@router.post(
"/business/{business_id}/price-report/export/pdf",
summary="خروجی PDF گزارش قیمت کالا (ستون‌های محدود)",
)
@require_business_access("business_id")
async def export_product_price_report_pdf(
request: Request,
business_id: int,
body: dict,
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("products", "export")),
):
scoped_body = dict(body or {})
scoped_body["columns_profile"] = "price_report"
return await export_products_pdf(
request=request,
business_id=business_id,
body=scoped_body,
ctx=ctx,
db=db,
_=None,
)
@router.post(
"/business/{business_id}/bulk-price-update/preview",
summary="پیش‌نمایش تغییر قیمت‌های گروهی",
description="""
پیش‌نمایش تغییرات قیمت قبل از اعمال تغییرات واقعی
### قابلیت‌ها:
- تغییر قیمت بر اساس درصد یا مبلغ ثابت
- تغییر قیمت فروش، خرید یا هر دو
- فیلتر بر اساس دسته‌بندی، ارز، لیست قیمت و نوع محصول
- نمایش تغییرات پیش از اعمال
### نکات:
- این endpoint فقط پیش‌نمایش است و تغییری در قیمت‌ها ایجاد نمی‌کند
- برای اعمال تغییرات از endpoint `/bulk-price-update/apply` استفاده کنید
- نتایج شامل خلاصه آماری تغییرات است
""",
response_model=SuccessResponse[BulkPriceUpdatePreviewResponse],
responses={
200: {
"description": "پیش‌نمایش تغییرات با موفقیت محاسبه شد",
"content": {
"application/json": {
"example": {
"success": True,
"data": {
"total_products": 100,
"affected_products": [
{
"product_id": 1,
"product_name": "محصول A",
"product_code": "P-001",
"category_name": "دسته ۱",
"current_sales_price": 100000,
"current_purchase_price": 80000,
"new_sales_price": 110000,
"new_purchase_price": 88000,
"sales_price_change": 10000,
"purchase_price_change": 8000
}
],
"summary": {
"total_products": 100,
"affected_products": 95,
"products_with_sales_change": 95,
"products_with_purchase_change": 95,
"total_sales_change": 950000,
"total_purchase_change": 760000
}
}
}
}
}
},
400: {
"description": "خطا در اعتبارسنجی درخواست"
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز - نیاز به مجوز products.edit"
}
}
)
@require_business_access("business_id")
def preview_bulk_price_update_endpoint(
request: Request,
business_id: int,
payload: BulkPriceUpdateRequest,
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("products", "edit")),
) -> Dict[str, Any]:
result = preview_bulk_price_update(db, business_id, payload)
# Pydantic v2: prefer model_dump(); keep response JSON-friendly
return success_response(data=result.model_dump(), request=request)
@router.post(
"/business/{business_id}/bulk-price-update/apply",
summary="اعمال تغییر قیمت‌های گروهی",
description="""
اعمال تغییرات قیمت بر روی کالاهای انتخاب شده
### قابلیت‌ها:
- تغییر قیمت بر اساس درصد یا مبلغ ثابت
- تغییر قیمت فروش، خرید یا هر دو
- فیلتر بر اساس دسته‌بندی، ارز، لیست قیمت و نوع محصول
### نکات مهم:
- این عملیات تغییرات را به صورت واقعی اعمال می‌کند
- قبل از اعمال، از endpoint `/bulk-price-update/preview` برای پیش‌نمایش استفاده کنید
- تغییرات در قیمت پایه محصولات اعمال می‌شود
- این عملیات قابل بازگشت نیست (مگر با تغییر مجدد)
""",
response_model=SuccessResponse[Dict[str, Any]],
responses={
200: {
"description": "تغییرات قیمت با موفقیت اعمال شد",
"content": {
"application/json": {
"example": {
"success": True,
"message": "تغییرات قیمت با موفقیت اعمال شد",
"data": {
"total_products": 100,
"updated_products": 95,
"skipped_products": 5
}
}
}
}
},
400: {
"description": "خطا در اعتبارسنجی درخواست"
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز - نیاز به مجوز products.edit"
}
}
)
@require_business_access("business_id")
def apply_bulk_price_update_endpoint(
request: Request,
business_id: int,
payload: BulkPriceUpdateRequest,
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("products", "edit")),
) -> Dict[str, Any]:
result = apply_bulk_price_update(db, business_id, payload)
return success_response(data=result, request=request)
@router.post(
"/business/{business_id}/bulk-prices-sheet/price-items",
summary="دریافت ردیف‌های قیمت لیست برای ورق ویرایش گسترده",
response_model=SuccessResponse[Dict[str, Any]],
)
@require_business_access("business_id")
def list_bulk_product_prices_sheet_items_endpoint(
request: Request,
business_id: int,
payload: BulkProductPriceSheetItemsRequest,
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("products", "view")),
) -> Dict[str, Any]:
result = list_bulk_price_sheet_items(db, business_id, payload)
return success_response(data=format_datetime_fields(result, request), request=request)
@router.post(
"/business/{business_id}/bulk-prices-sheet/apply",
summary="اعمال دسته‌ای قیمت پایه (نمای ویرایش گسترده)",
response_model=SuccessResponse[Dict[str, Any]],
)
@require_business_access("business_id")
def apply_bulk_product_prices_sheet_endpoint(
request: Request,
business_id: int,
payload: BulkProductPriceSheetApplyRequest,
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("products", "edit")),
) -> Dict[str, Any]:
result = apply_bulk_product_price_sheet(db, business_id, payload)
return success_response(data=result, request=request)
@router.post(
"/business/{business_id}/bulk-prices-sheet/export/excel",
summary="خروجی اکسل ورق ویرایش گسترده قیمت",
response_class=Response,
)
@require_business_access("business_id")
async def export_bulk_product_prices_sheet_excel_endpoint(
request: Request,
business_id: int,
payload: BulkProductPriceSheetExcelExportRequest,
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("products", "view")),
):
locale = negotiate_locale(request.headers.get("Accept-Language"))
rtl = locale == "fa"
data, filename = export_bulk_product_price_sheet_excel(
db,
business_id,
search=payload.search,
search_fields=payload.search_fields,
price_list_ids=list(payload.price_list_ids),
rtl_sheet=rtl,
)
return Response(
content=data,
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
headers={
"Content-Disposition": f"attachment; filename={filename}",
"Access-Control-Expose-Headers": "Content-Disposition",
},
)
@router.post(
"/business/{business_id}/bulk-prices-sheet/import/excel",
summary="ایمپورت اکسل ورق ویرایش گسترده قیمت",
response_model=SuccessResponse[Dict[str, Any]],
)
@require_business_access("business_id")
async def import_bulk_product_prices_sheet_excel_endpoint(
request: Request,
business_id: int,
file: UploadFile = File(...),
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("products", "edit")),
) -> Dict[str, Any]:
if not file.filename or not str(file.filename).lower().endswith(".xlsx"):
raise ApiError(
"INVALID_FILE",
"فقط فایل‌های .xlsx پشتیبانی می‌شوند",
http_status=400,
)
content = await file.read()
result = import_bulk_product_price_sheet_from_excel(db, business_id, content)
if not result.get("ok"):
raise ApiError(
"BULK_PRICE_SHEET_IMPORT_FAILED",
result.get("message") or "ایمپورت ناموفق بود",
http_status=400,
details={"errors": result.get("errors") or []},
)
return success_response(data=result, request=request, message=result.get("message"))
@router.post(
"/business/{business_id}/bulk-default-warehouse/preview",
summary="پیش‌نمایش تغییر گروهی انبار پیش‌فرض کالاها",
description="""
پیش‌نمایش تغییر انبار پیش‌فرض برای چندین کالا به صورت همزمان
### قابلیت‌ها:
- تغییر انبار پیش‌فرض برای لیستی از کالاها
- اعمال بر روی کالاهای انبارداری، غیرانبارداری یا همه
- نمایش کالاهایی که تغییر می‌یابند و کالاهایی که نادیده گرفته می‌شوند
### نکات:
- این endpoint فقط پیش‌نمایش است و تغییری ایجاد نمی‌کند
- برای اعمال تغییرات از endpoint `/bulk-default-warehouse/apply` استفاده کنید
- کالاهای خدمتی (service) انبار پیش‌فرض ندارند
""",
response_model=SuccessResponse[BulkDefaultWarehousePreviewResponse],
responses={
200: {
"description": "پیش‌نمایش تغییرات با موفقیت محاسبه شد",
"content": {
"application/json": {
"example": {
"success": True,
"data": {
"total_requested": 50,
"found_count": 48,
"will_update_count": 45,
"forced_service_null_count": 3,
"skipped": [
{
"id": 100,
"code": "S-001",
"name": "خدمت A",
"reason": "کالاهای خدمتی انبار پیش‌فرض ندارند"
}
],
"notes": []
}
}
}
}
},
400: {
"description": "خطا در اعتبارسنجی درخواست"
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز - نیاز به مجوز products.edit"
}
}
)
@require_business_access("business_id")
def preview_bulk_default_warehouse_endpoint(
request: Request,
business_id: int,
payload: BulkDefaultWarehouseRequest,
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("products", "edit")),
) -> Dict[str, Any]:
result = preview_bulk_default_warehouse_update(db, business_id, payload)
return success_response(data=result, request=request)
@router.post(
"/business/{business_id}/bulk-default-warehouse/apply",
summary="اعمال تغییر گروهی انبار پیش‌فرض کالاها",
description="""
اعمال تغییر انبار پیش‌فرض برای چندین کالا به صورت واقعی
### قابلیت‌ها:
- تغییر انبار پیش‌فرض برای لیستی از کالاها
- اعمال بر روی کالاهای انبارداری، غیرانبارداری یا همه
- حذف انبار پیش‌فرض (با ارسال null)
### نکات مهم:
- این عملیات تغییرات را به صورت واقعی اعمال می‌کند
- قبل از اعمال، از endpoint `/bulk-default-warehouse/preview` برای پیش‌نمایش استفاده کنید
- کالاهای خدمتی (service) نادیده گرفته می‌شوند
- تغییر انبار پیش‌فرض بر روی موجودی تأثیری ندارد
""",
response_model=SuccessResponse[BulkDefaultWarehouseApplyResponse],
responses={
200: {
"description": "تغییرات با موفقیت اعمال شد",
"content": {
"application/json": {
"example": {
"success": True,
"message": "انبار پیش‌فرض با موفقیت به‌روزرسانی شد",
"data": {
"total_requested": 50,
"found_count": 48,
"updated_count": 45,
"forced_service_null_count": 3,
"skipped": [],
"notes": []
}
}
}
}
},
400: {
"description": "خطا در اعتبارسنجی درخواست"
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز - نیاز به مجوز products.edit"
}
}
)
@require_business_access("business_id")
def apply_bulk_default_warehouse_endpoint(
request: Request,
business_id: int,
payload: BulkDefaultWarehouseRequest,
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("products", "edit")),
) -> Dict[str, Any]:
result = apply_bulk_default_warehouse_update(db, business_id, ctx.get_user_id(), payload)
db.commit()
return success_response(data=result, request=request)
@router.post(
"/businesses/{business_id}/reports/item-movements",
summary="گزارش گردش کالا",
description="""
دریافت گزارش گردش کالاها در بازه زمانی مشخص
### اطلاعات گزارش:
- ورود و خروج کالاها در بازه زمانی
- مانده ابتدا و انتهای دوره
- تفکیک بر اساس انبارها
- فیلتر بر اساس محصولات، انبارها و تاریخ
### کاربردها:
- بررسی حرکت کالاها
- تحلیل ورود و خروج
- کنترل موجودی در بازه زمانی
""",
response_model=SuccessResponse[Dict[str, Any]],
responses={
200: {
"description": "گزارش با موفقیت ایجاد شد",
"content": {
"application/json": {
"example": {
"success": True,
"data": {
"items": [
{
"product_id": 1,
"product_name": "کالا A",
"warehouse_id": 1,
"warehouse_name": "انبار اصلی",
"opening_balance": 100,
"incoming": 50,
"outgoing": 30,
"closing_balance": 120
}
],
"total_count": 100,
"summary": {}
}
}
}
}
},
400: {
"description": "خطا در اعتبارسنجی پارامترها"
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز"
}
}
)
@require_business_access("business_id")
async def item_movements_report_endpoint(
request: Request,
business_id: int,
body: Dict[str, Any] = Body(default={}),
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("reports", "view")),
) -> Dict[str, Any]:
"""گزارش گردش کالا"""
# دریافت سال مالی از header یا body
fiscal_year_id = None
fy_header = request.headers.get('X-Fiscal-Year-ID')
if fy_header:
try:
fiscal_year_id = int(fy_header)
except (ValueError, TypeError):
pass
if body.get('fiscal_year_id'):
try:
fiscal_year_id = int(body['fiscal_year_id'])
except (ValueError, TypeError):
pass
if not fiscal_year_id:
from adapters.db.models.fiscal_year import FiscalYear
fiscal_year = db.query(FiscalYear).filter(
and_(
FiscalYear.business_id == business_id,
FiscalYear.is_last == True
)
).first()
if fiscal_year:
fiscal_year_id = fiscal_year.id
# استخراج پارامترها از body
date_from = body.get('date_from')
date_to = body.get('date_to')
currency_id = body.get('currency_id')
if currency_id is not None:
try:
currency_id = int(currency_id)
except (ValueError, TypeError):
currency_id = None
product_ids = body.get('product_ids')
if product_ids is not None and not isinstance(product_ids, list):
product_ids = None
warehouse_ids = body.get('warehouse_ids')
if warehouse_ids is not None and not isinstance(warehouse_ids, list):
warehouse_ids = None
category_ids = body.get('category_ids')
if category_ids is not None and not isinstance(category_ids, list):
category_ids = None
include_zero_balance = bool(body.get('include_zero_balance', False))
search = body.get('search')
# Pagination
skip = body.get('skip', 0)
take = body.get('take', 50)
try:
skip = int(skip)
take = int(take)
if take > 500:
take = 500
if take < 1:
take = 50
if skip < 0:
skip = 0
except (ValueError, TypeError):
skip = 0
take = 50
result = get_item_movements_report(
db=db,
business_id=business_id,
fiscal_year_id=fiscal_year_id,
currency_id=currency_id,
date_from=date_from,
date_to=date_to,
product_ids=product_ids,
warehouse_ids=warehouse_ids,
category_ids=category_ids,
include_zero_balance=include_zero_balance,
search=search,
skip=skip,
take=take,
)
items = result.get('items', [])
items = [format_datetime_fields(item, request) for item in items]
result['items'] = items
locale = negotiate_locale(request.headers.get("Accept-Language"))
return success_response(
data=result,
message="Item movements report retrieved successfully" if locale != 'fa' else "گزارش گردش کالا با موفقیت دریافت شد",
request=request
)
@router.post(
"/businesses/{business_id}/reports/item-movements/export/excel",
summary="خروجی Excel گزارش گردش کالا",
description="""
دریافت گزارش گردش کالا به صورت فایل Excel
### قابلیت‌ها:
- فیلتر بر اساس محصولات، انبارها و تاریخ
- انتخاب سطرهای خاص برای خروجی
- انتخاب و مرتب‌سازی ستون‌ها
- پشتیبانی از RTL برای زبان فارسی
### فرمت فایل:
- فرمت: `.xlsx` (Excel 2007+)
""",
responses={
200: {
"description": "فایل Excel با موفقیت ایجاد شد",
"content": {
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": {
"schema": {
"type": "string",
"format": "binary"
}
}
}
},
400: {
"description": "خطا در اعتبارسنجی درخواست"
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز"
}
}
)
@require_business_access("business_id")
async def export_item_movements_report_excel(
request: Request,
business_id: int,
body: Dict[str, Any] = Body(default={}),
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("reports", "export")),
):
"""خروجی Excel گزارش گردش کالا"""
from fastapi.responses import Response
from openpyxl import Workbook
from openpyxl.styles import Font, Alignment, PatternFill, Border, Side
import io
import datetime
import re
# دریافت سال مالی از header یا body
fiscal_year_id = None
fy_header = request.headers.get('X-Fiscal-Year-ID')
if fy_header:
try:
fiscal_year_id = int(fy_header)
except (ValueError, TypeError):
pass
if body.get('fiscal_year_id'):
try:
fiscal_year_id = int(body['fiscal_year_id'])
except (ValueError, TypeError):
pass
if not fiscal_year_id:
from adapters.db.models.fiscal_year import FiscalYear
fiscal_year = db.query(FiscalYear).filter(
and_(
FiscalYear.business_id == business_id,
FiscalYear.is_last == True
)
).first()
if fiscal_year:
fiscal_year_id = fiscal_year.id
# استخراج پارامترها از body
date_from = body.get('date_from')
date_to = body.get('date_to')
currency_id = body.get('currency_id')
if currency_id is not None:
try:
currency_id = int(currency_id)
except (ValueError, TypeError):
currency_id = None
product_ids = body.get('product_ids')
if product_ids is not None and not isinstance(product_ids, list):
product_ids = None
warehouse_ids = body.get('warehouse_ids')
if warehouse_ids is not None and not isinstance(warehouse_ids, list):
warehouse_ids = None
category_ids = body.get('category_ids')
if category_ids is not None and not isinstance(category_ids, list):
category_ids = None
include_zero_balance = bool(body.get('include_zero_balance', False))
search = body.get('search')
# اعمال فیلتر سطرهای انتخاب شده
selected_only = bool(body.get('selected_only', False))
selected_indices = body.get('selected_indices')
# دریافت گزارش با take بزرگ برای export
max_export_records = 10000
result = get_item_movements_report(
db=db,
business_id=business_id,
fiscal_year_id=fiscal_year_id,
currency_id=currency_id,
date_from=date_from,
date_to=date_to,
product_ids=product_ids,
warehouse_ids=warehouse_ids,
category_ids=category_ids,
include_zero_balance=include_zero_balance,
search=search,
skip=0,
take=max_export_records,
)
items = result.get('items', [])
# فیلتر سطرهای انتخاب شده
if selected_only and selected_indices is not None:
indices = None
if isinstance(selected_indices, str):
try:
import json as _json
indices = _json.loads(selected_indices)
except Exception:
indices = None
elif isinstance(selected_indices, list):
indices = selected_indices
if isinstance(indices, list):
items = [items[i] for i in indices if isinstance(i, int) and 0 <= i < len(items)]
# ستون‌های export
export_columns = body.get("export_columns")
if export_columns and isinstance(export_columns, list):
headers = [col.get("label") or col.get("key") for col in export_columns]
keys = [col.get("key") for col in export_columns]
else:
# ستون‌های پیش‌فرض
locale = negotiate_locale(request.headers.get("Accept-Language"))
is_fa = (locale == 'fa')
default_columns = [
('product_code', 'کد کالا' if is_fa else 'Product Code'),
('product_name', 'نام کالا' if is_fa else 'Product Name'),
('unit', 'واحد' if is_fa else 'Unit'),
('category_name', 'دسته‌بندی' if is_fa else 'Category'),
('opening_balance', 'مانده ابتدای دوره' if is_fa else 'Opening Balance'),
('total_in', 'ورود' if is_fa else 'Total In'),
('total_out', 'خروج' if is_fa else 'Total Out'),
('closing_balance', 'مانده انتهای دوره' if is_fa else 'Closing Balance'),
]
keys = [k for k, _ in default_columns]
headers = [h for _, h in default_columns]
# ساخت Excel
wb = Workbook()
ws = wb.active
ws.title = "Item Movements"
# Locale and RTL/LTR handling for Excel
locale = negotiate_locale(request.headers.get("Accept-Language"))
if locale == 'fa':
try:
ws.sheet_view.rightToLeft = True
except Exception:
pass
# Header style
header_font = Font(bold=True)
header_fill = PatternFill(start_color="DDDDDD", end_color="DDDDDD", fill_type="solid")
thin_border = Border(left=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'), bottom=Side(style='thin'))
ws.append(headers)
for cell in ws[1]:
cell.font = header_font
cell.fill = header_fill
cell.alignment = Alignment(horizontal="center")
cell.border = thin_border
# افزودن ردیف‌های داده
for it in items:
row = []
for k in keys:
value = it.get(k)
if value is None:
row.append('')
elif isinstance(value, (int, float)):
row.append(float(value))
else:
row.append(str(value))
ws.append(row)
for cell in ws[ws.max_row]:
cell.border = thin_border
# Align data cells based on locale
if locale == 'fa':
cell.alignment = Alignment(horizontal="right")
# Auto width columns
try:
for column in ws.columns:
max_length = 0
column_letter = column[0].column_letter
for cell in column:
try:
if cell.value is not None and len(str(cell.value)) > max_length:
max_length = len(str(cell.value))
except Exception:
pass
ws.column_dimensions[column_letter].width = min(max_length + 2, 50)
except Exception:
pass
output = io.BytesIO()
wb.save(output)
data = output.getvalue()
# Build meaningful filename
biz_name = ""
try:
b = db.query(Business).filter(Business.id == business_id).first()
if b is not None:
biz_name = b.name or ""
except Exception:
biz_name = ""
def slugify(text: str) -> str:
return re.sub(r"[^A-Za-z0-9_-]+", "_", text).strip("_")
base = "item_movements"
if biz_name:
base += f"_{slugify(biz_name)}"
if selected_only:
base += "_selected"
filename = f"{base}_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx"
return Response(
content=data,
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
headers={
"Content-Disposition": f"attachment; filename={filename}",
"Content-Length": str(len(data)),
"Access-Control-Expose-Headers": "Content-Disposition",
},
)
@router.post(
"/businesses/{business_id}/reports/item-movements/export/pdf",
summary="خروجی PDF گزارش گردش کالا",
responses={
200: {
"description": "فایل PDF",
"content": {"application/pdf": {"schema": {"type": "string", "format": "binary"}}},
}
},
)
@require_business_access("business_id")
async def export_item_movements_report_pdf(
request: Request,
business_id: int,
body: Dict[str, Any] = Body(default={}),
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("reports", "export")),
):
"""خروجی PDF گزارش گردش کالا"""
import datetime
from weasyprint import HTML, CSS
from weasyprint.text.fonts import FontConfiguration
from app.core.calendar import CalendarConverter
from app.services.pdf.template_renderer import render_template, load_farsi_font_data_uris
fiscal_year_id = None
fy_header = request.headers.get("X-Fiscal-Year-ID")
if fy_header:
try:
fiscal_year_id = int(fy_header)
except (ValueError, TypeError):
pass
if body.get("fiscal_year_id"):
try:
fiscal_year_id = int(body["fiscal_year_id"])
except (ValueError, TypeError):
pass
if not fiscal_year_id:
from adapters.db.models.fiscal_year import FiscalYear
fiscal_year = db.query(FiscalYear).filter(
and_(FiscalYear.business_id == business_id, FiscalYear.is_last == True)
).first()
if fiscal_year:
fiscal_year_id = fiscal_year.id
date_from = body.get("date_from")
date_to = body.get("date_to")
currency_id = body.get("currency_id")
if currency_id is not None:
try:
currency_id = int(currency_id)
except (ValueError, TypeError):
currency_id = None
product_ids = body.get("product_ids")
if product_ids is not None and not isinstance(product_ids, list):
product_ids = None
warehouse_ids = body.get("warehouse_ids")
if warehouse_ids is not None and not isinstance(warehouse_ids, list):
warehouse_ids = None
category_ids = body.get("category_ids")
if category_ids is not None and not isinstance(category_ids, list):
category_ids = None
include_zero_balance = bool(body.get("include_zero_balance", False))
search = body.get("search")
selected_only = bool(body.get("selected_only", False))
selected_indices = body.get("selected_indices")
max_export_records = 10000
result = get_item_movements_report(
db=db,
business_id=business_id,
fiscal_year_id=fiscal_year_id,
currency_id=currency_id,
date_from=date_from,
date_to=date_to,
product_ids=product_ids,
warehouse_ids=warehouse_ids,
category_ids=category_ids,
include_zero_balance=include_zero_balance,
search=search,
skip=0,
take=max_export_records,
)
items = result.get("items", [])
items = [format_datetime_fields(item, request) for item in items]
if selected_only and selected_indices is not None:
indices = None
if isinstance(selected_indices, str):
try:
import json as _json
indices = _json.loads(selected_indices)
except Exception:
indices = None
elif isinstance(selected_indices, list):
indices = selected_indices
if isinstance(indices, list):
items = [items[i] for i in indices if isinstance(i, int) and 0 <= i < len(items)]
calendar_type = getattr(request.state, "calendar_type", "jalali")
locale = negotiate_locale(request.headers.get("Accept-Language"))
is_fa = locale == "fa"
try:
_now = datetime.datetime.now()
_gen = CalendarConverter.format_datetime(_now, calendar_type).get("formatted") or ""
generated_at = " ".join(_gen.split(" ")[:2])
if generated_at.count(":") >= 2:
generated_at = generated_at.rsplit(":", 1)[0]
except Exception:
generated_at = datetime.datetime.now().strftime("%Y/%m/%d %H:%M")
filters_summary = []
if date_from:
filters_summary.append({"label": "از" if is_fa else "From", "value": str(date_from)})
if date_to:
filters_summary.append({"label": "تا" if is_fa else "To", "value": str(date_to)})
if search:
filters_summary.append({"label": "جستجو" if is_fa else "Search", "value": str(search)})
try:
from app.services.print_footer_settings import build_report_title_and_time_footer
footer_text = build_report_title_and_time_footer(
db,
business_id,
title="گزارش گردش کالا" if is_fa else "Item movements",
time_part=generated_at,
preparer_name=ctx.get_user_name() or None,
is_fa=is_fa,
)
except Exception:
footer_text = f"{'گزارش گردش کالا' if is_fa else 'Item movements'} • {generated_at}"
business_name = ""
try:
b = db.query(Business).filter(Business.id == business_id).first()
if b is not None:
business_name = b.name or ""
except Exception:
business_name = ""
try:
qp = request.query_params
paper_size = qp.get("paper_size")
orientation = qp.get("orientation") or "landscape"
disposition = qp.get("disposition") or "attachment"
except Exception:
paper_size = None
orientation = "landscape"
disposition = "attachment"
fa_font_url_regular = ""
fa_font_url_bold = ""
try:
if is_fa:
fa_reg, fa_bold = load_farsi_font_data_uris()
fa_font_url_regular = fa_reg or ""
fa_font_url_bold = fa_bold or ""
except Exception:
pass
explicit_template_id = None
try:
if body.get("template_id") is not None:
explicit_template_id = int(body.get("template_id"))
except Exception:
explicit_template_id = None
resolved_html = None
try:
from app.services.report_template_service import ReportTemplateService
tmpl_ctx = {
"title_text": "گزارش گردش کالا" if is_fa else "Item movements",
"business_name": business_name,
"generated_at": generated_at,
"is_fa": is_fa,
"locale": locale,
"paper_size": paper_size,
"orientation": orientation,
"fa_font_url_regular": fa_font_url_regular,
"fa_font_url_bold": fa_font_url_bold,
"footer_text": footer_text,
"filters_summary": filters_summary,
"items": items,
}
resolved_html = ReportTemplateService.try_render_resolved(
db=db,
business_id=business_id,
module_key="item_movements",
subtype="list",
context=tmpl_ctx,
explicit_template_id=explicit_template_id,
)
except Exception:
resolved_html = None
if not resolved_html:
final_html = render_template(
"pdf/item_movements/list.html",
{
"title_text": "گزارش گردش کالا" if is_fa else "Item movements",
"business_name": business_name,
"generated_at": generated_at,
"is_fa": is_fa,
"locale": locale,
"paper_size": paper_size or "A4",
"orientation": orientation or "landscape",
"fa_font_url_regular": fa_font_url_regular,
"fa_font_url_bold": fa_font_url_bold,
"footer_text": footer_text,
"filters_summary": filters_summary,
"items": items,
},
)
else:
final_html = resolved_html
page_css = f"@page {{ size: {(paper_size or 'A4')} {(orientation or 'landscape')}; margin: 1cm; }}"
font_config = FontConfiguration()
pdf_bytes = HTML(string=final_html).write_pdf(
stylesheets=[CSS(string=page_css)],
font_config=font_config,
)
fn = f"item_movements_{business_id}_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
return Response(
content=pdf_bytes,
media_type="application/pdf",
headers={
"Content-Disposition": f"{disposition}; filename={fn}",
"Content-Length": str(len(pdf_bytes)),
"Access-Control-Expose-Headers": "Content-Disposition",
},
)
@router.post(
"/businesses/{business_id}/reports/sales-by-product",
summary="گزارش فروش به تفکیک کالا",
description="""
دریافت گزارش عملکرد فروش هر کالا در بازه زمانی
### اطلاعات گزارش:
- تعداد و مقدار فروش هر کالا
- مجموع مبلغ فروش
- میانگین قیمت فروش
- تعداد فاکتورها
- فیلتر بر اساس محصولات، تاریخ و سایر معیارها
### کاربردها:
- تحلیل عملکرد فروش محصولات
- شناسایی پرفروش‌ترین کالاها
- برنامه‌ریزی فروش و موجودی
""",
response_model=SuccessResponse[Dict[str, Any]],
responses={
200: {
"description": "گزارش با موفقیت ایجاد شد",
"content": {
"application/json": {
"example": {
"success": True,
"data": {
"items": [
{
"product_id": 1,
"product_name": "کالا A",
"quantity": 100,
"total_amount": 15000000,
"average_price": 150000,
"invoice_count": 10
}
],
"total_count": 50,
"summary": {
"total_amount": 750000000,
"total_quantity": 5000
}
}
}
}
}
},
400: {
"description": "خطا در اعتبارسنجی پارامترها"
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز"
}
}
)
@require_business_access("business_id")
async def sales_by_product_report_endpoint(
request: Request,
business_id: int,
body: Dict[str, Any] = Body(default={}),
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("reports", "view")),
) -> Dict[str, Any]:
"""گزارش فروش به تفکیک کالا"""
# دریافت سال مالی از header یا body
fiscal_year_id = None
fy_header = request.headers.get('X-Fiscal-Year-ID')
if fy_header:
try:
fiscal_year_id = int(fy_header)
except (ValueError, TypeError):
pass
if body.get('fiscal_year_id'):
try:
fiscal_year_id = int(body['fiscal_year_id'])
except (ValueError, TypeError):
pass
if not fiscal_year_id:
from adapters.db.models.fiscal_year import FiscalYear
fiscal_year = db.query(FiscalYear).filter(
and_(
FiscalYear.business_id == business_id,
FiscalYear.is_last == True
)
).first()
if fiscal_year:
fiscal_year_id = fiscal_year.id
# استخراج پارامترها از body
date_from = body.get('date_from')
date_to = body.get('date_to')
currency_id = body.get('currency_id')
if currency_id is not None:
try:
currency_id = int(currency_id)
except (ValueError, TypeError):
currency_id = None
product_ids = body.get('product_ids')
if product_ids is not None and not isinstance(product_ids, list):
product_ids = None
category_ids = body.get('category_ids')
if category_ids is not None and not isinstance(category_ids, list):
category_ids = None
warehouse_ids = body.get('warehouse_ids')
if warehouse_ids is not None and not isinstance(warehouse_ids, list):
warehouse_ids = None
include_zero_sales = bool(body.get('include_zero_sales', False))
search = body.get('search')
# Pagination
skip = body.get('skip', 0)
take = body.get('take', 50)
try:
skip = int(skip)
take = int(take)
if take > 500:
take = 500
if take < 1:
take = 50
if skip < 0:
skip = 0
except (ValueError, TypeError):
skip = 0
take = 50
result = get_sales_by_product_report(
db=db,
business_id=business_id,
fiscal_year_id=fiscal_year_id,
currency_id=currency_id,
date_from=date_from,
date_to=date_to,
product_ids=product_ids,
category_ids=category_ids,
warehouse_ids=warehouse_ids,
include_zero_sales=include_zero_sales,
search=search,
skip=skip,
take=take,
)
items = result.get('items', [])
items = [format_datetime_fields(item, request) for item in items]
result['items'] = items
locale = negotiate_locale(request.headers.get("Accept-Language"))
return success_response(
data=result,
message="Sales by product report retrieved successfully" if locale != 'fa' else "گزارش فروش به تفکیک کالا با موفقیت دریافت شد",
request=request
)
@router.post(
"/businesses/{business_id}/reports/inventory-kardex",
summary="گزارش کاردکس موجودی",
description="""
دریافت گزارش کاردکس (کاردکس) موجودی کالاها
### اطلاعات گزارش:
- جزئیات تمام حرکات موجودی (ورود، خروج، انتقال)
- مانده تجمعی پس از هر حرکت
- تاریخ و زمان هر حرکت
- نوع سند و شماره سند
- فیلتر بر اساس محصولات، انبارها و تاریخ
### کاربردها:
- ردیابی دقیق حرکات موجودی
- بررسی تاریخچه موجودی
- کنترل و ممیزی موجودی
- تجزیه و تحلیل الگوهای ورود و خروج
""",
response_model=SuccessResponse[Dict[str, Any]],
responses={
200: {
"description": "گزارش با موفقیت ایجاد شد",
"content": {
"application/json": {
"example": {
"success": True,
"data": {
"items": [
{
"date": "2024-01-15",
"document_type": "فاکتور خرید",
"document_number": "INV-001",
"product_id": 1,
"product_name": "کالا A",
"warehouse_id": 1,
"warehouse_name": "انبار اصلی",
"incoming": 100,
"outgoing": 0,
"balance": 100
}
],
"total_count": 500,
"summary": {}
}
}
}
}
},
400: {
"description": "خطا در اعتبارسنجی پارامترها"
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز"
}
}
)
@require_business_access("business_id")
async def inventory_kardex_report_endpoint(
request: Request,
business_id: int,
body: Dict[str, Any] = Body(default={}),
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("reports", "view")),
) -> Dict[str, Any]:
"""گزارش کاردکس موجودی"""
# دریافت سال مالی از header یا body
fiscal_year_id = None
fy_header = request.headers.get('X-Fiscal-Year-ID')
if fy_header:
try:
fiscal_year_id = int(fy_header)
except (ValueError, TypeError):
pass
if body.get('fiscal_year_id'):
try:
fiscal_year_id = int(body['fiscal_year_id'])
except (ValueError, TypeError):
pass
if not fiscal_year_id:
from adapters.db.models.fiscal_year import FiscalYear
fiscal_year = db.query(FiscalYear).filter(
and_(
FiscalYear.business_id == business_id,
FiscalYear.is_last == True
)
).first()
if fiscal_year:
fiscal_year_id = fiscal_year.id
# استخراج پارامترها از body
date_from = body.get('date_from')
date_to = body.get('date_to')
product_ids = body.get('product_ids')
if product_ids is not None and not isinstance(product_ids, list):
product_ids = None
warehouse_ids = body.get('warehouse_ids')
if warehouse_ids is not None and not isinstance(warehouse_ids, list):
warehouse_ids = None
category_ids = body.get('category_ids')
if category_ids is not None and not isinstance(category_ids, list):
category_ids = None
search = body.get('search')
# Pagination
skip = body.get('skip', 0)
take = body.get('take', 50)
try:
skip = int(skip)
take = int(take)
if take > 500:
take = 500
if take < 1:
take = 50
if skip < 0:
skip = 0
except (ValueError, TypeError):
skip = 0
take = 50
result = get_inventory_kardex_report(
db=db,
business_id=business_id,
fiscal_year_id=fiscal_year_id,
date_from=date_from,
date_to=date_to,
product_ids=product_ids,
warehouse_ids=warehouse_ids,
category_ids=category_ids,
search=search,
skip=skip,
take=take,
)
items = result.get('items', [])
items = [format_datetime_fields(item, request) for item in items]
result['items'] = items
locale = negotiate_locale(request.headers.get("Accept-Language"))
return success_response(
data=result,
message="Inventory kardex report retrieved successfully" if locale != 'fa' else "گزارش کاردکس موجودی با موفقیت دریافت شد",
request=request
)
@router.post(
"/businesses/{business_id}/reports/inventory-stock",
summary="گزارش موجودی انبار",
description="""
دریافت گزارش موجودی فعلی محصولات در انبارها
### اطلاعات گزارش:
- موجودی فعلی هر محصول در هر انبار
- ارزش موجودی
- موجودی رزرو شده
- موجودی قابل استفاده
- فیلترهای مختلف:
- فقط کالاهای با کنترل موجودی
- موجودی منفی
- فاقد حواله (موجودی صفر)
- فیلتر بر اساس محصولات و انبارها
### کاربردها:
- کنترل موجودی فعلی
- شناسایی موجودی منفی
- مدیریت سفارشات
- ارزیابی ارزش موجودی
""",
response_model=SuccessResponse[Dict[str, Any]],
responses={
200: {
"description": "گزارش با موفقیت ایجاد شد",
"content": {
"application/json": {
"example": {
"success": True,
"data": {
"items": [
{
"product_id": 1,
"product_name": "کالا A",
"product_code": "P-001",
"warehouse_id": 1,
"warehouse_name": "انبار اصلی",
"quantity": 100,
"reserved_quantity": 20,
"available_quantity": 80,
"unit_cost": 150000,
"total_value": 15000000
}
],
"total_count": 200,
"summary": {
"total_value": 3000000000
}
}
}
}
}
},
400: {
"description": "خطا در اعتبارسنجی پارامترها"
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز"
}
}
)
@require_business_access("business_id")
async def inventory_stock_report_endpoint(
request: Request,
business_id: int,
body: Dict[str, Any] = Body(default={}),
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("reports", "view")),
) -> Dict[str, Any]:
"""گزارش موجودی انبار"""
# بررسی دسترسی
if not ctx.can_read_section("reports"):
raise ApiError("FORBIDDEN", "Missing business permission: reports.read", http_status=403)
# دریافت سال مالی از header یا body
fiscal_year_id = None
fy_header = request.headers.get('X-Fiscal-Year-ID')
if fy_header:
try:
fiscal_year_id = int(fy_header)
except (ValueError, TypeError):
pass
if body.get('fiscal_year_id'):
try:
fiscal_year_id = int(body['fiscal_year_id'])
except (ValueError, TypeError):
pass
# استخراج پارامترها از body
product_ids = body.get('product_ids')
if product_ids is not None and not isinstance(product_ids, list):
product_ids = None
warehouse_ids = body.get('warehouse_ids')
if warehouse_ids is not None and not isinstance(warehouse_ids, list):
warehouse_ids = None
category_ids = body.get('category_ids')
if category_ids is not None and not isinstance(category_ids, list):
category_ids = None
as_of_date = body.get('as_of_date')
track_inventory = body.get('track_inventory')
if track_inventory is not None:
try:
track_inventory = bool(track_inventory)
except (ValueError, TypeError):
track_inventory = None
only_negative_stock = bool(body.get('only_negative_stock', False))
only_without_movements = bool(body.get('only_without_movements', False))
include_zero = bool(body.get('include_zero', False))
search = body.get('search')
# Pagination
skip = body.get('skip', 0)
take = body.get('take', 50)
try:
skip = int(skip)
take = int(take)
if take > 500:
take = 500
if take < 1:
take = 50
if skip < 0:
skip = 0
except (ValueError, TypeError):
skip = 0
take = 50
result = get_inventory_stock_report(
db=db,
business_id=business_id,
fiscal_year_id=fiscal_year_id,
product_ids=product_ids,
warehouse_ids=warehouse_ids,
category_ids=category_ids,
as_of_date=as_of_date,
track_inventory=track_inventory,
only_negative_stock=only_negative_stock,
only_without_movements=only_without_movements,
include_zero=include_zero,
search=search,
skip=skip,
take=take,
)
items = result.get('items', [])
items = [format_datetime_fields(item, request) for item in items]
result['items'] = items
locale = negotiate_locale(request.headers.get("Accept-Language"))
return success_response(
data=result,
message="Inventory stock report retrieved successfully" if locale != 'fa' else "گزارش موجودی انبار با موفقیت دریافت شد",
request=request
)
@router.post(
"/businesses/{business_id}/reports/inventory-stock/export/excel",
summary="خروجی Excel گزارش موجودی انبار",
responses={
200: {
"description": "فایل Excel",
"content": {
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": {
"schema": {"type": "string", "format": "binary"}
}
},
}
},
)
@require_business_access("business_id")
async def export_inventory_stock_report_excel(
request: Request,
business_id: int,
body: Dict[str, Any] = Body(default={}),
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("reports", "export")),
):
"""خروجی Excel گزارش موجودی انبار"""
from fastapi.responses import Response
from openpyxl import Workbook
from openpyxl.styles import Font, Alignment, PatternFill, Border, Side
import io
import datetime
import re
if not ctx.can_read_section("reports"):
raise ApiError("FORBIDDEN", "Missing business permission: reports.read", http_status=403)
fiscal_year_id = None
fy_header = request.headers.get("X-Fiscal-Year-ID")
if fy_header:
try:
fiscal_year_id = int(fy_header)
except (ValueError, TypeError):
pass
if body.get("fiscal_year_id"):
try:
fiscal_year_id = int(body["fiscal_year_id"])
except (ValueError, TypeError):
pass
product_ids = body.get("product_ids")
if product_ids is not None and not isinstance(product_ids, list):
product_ids = None
warehouse_ids = body.get("warehouse_ids")
if warehouse_ids is not None and not isinstance(warehouse_ids, list):
warehouse_ids = None
category_ids = body.get("category_ids")
if category_ids is not None and not isinstance(category_ids, list):
category_ids = None
as_of_date = body.get("as_of_date")
track_inventory = body.get("track_inventory")
if track_inventory is not None:
try:
track_inventory = bool(track_inventory)
except (ValueError, TypeError):
track_inventory = None
only_negative_stock = bool(body.get("only_negative_stock", False))
only_without_movements = bool(body.get("only_without_movements", False))
include_zero = bool(body.get("include_zero", False))
search = body.get("search")
selected_only = bool(body.get("selected_only", False))
selected_indices = body.get("selected_indices")
selected_row_keys = body.get("selected_row_keys")
max_export_records = 10000
result = get_inventory_stock_report(
db=db,
business_id=business_id,
fiscal_year_id=fiscal_year_id,
product_ids=product_ids,
warehouse_ids=warehouse_ids,
category_ids=category_ids,
as_of_date=as_of_date,
track_inventory=track_inventory,
only_negative_stock=only_negative_stock,
only_without_movements=only_without_movements,
include_zero=include_zero,
search=search,
skip=0,
take=max_export_records,
for_export=True,
)
items = result.get("items", [])
items = [format_datetime_fields(item, request) for item in items]
if selected_only and selected_row_keys is not None and isinstance(selected_row_keys, list):
try:
wanted = [k for k in selected_row_keys if isinstance(k, dict)]
if wanted:
def _match_key(it: dict, key: dict) -> bool:
for fld in ("product_id", "warehouse_id"):
if key.get(fld) is not None and it.get(fld) is not None:
try:
if int(it.get(fld)) != int(key.get(fld)):
return False
except Exception:
return False
return True
filt = []
for it in items:
for k in wanted:
if _match_key(it, k):
filt.append(it)
break
items = filt
except Exception:
pass
if selected_only and selected_indices is not None:
indices = None
if isinstance(selected_indices, str):
try:
import json as _json
indices = _json.loads(selected_indices)
except Exception:
indices = None
elif isinstance(selected_indices, list):
indices = selected_indices
if isinstance(indices, list):
items = [items[i] for i in indices if isinstance(i, int) and 0 <= i < len(items)]
export_columns = body.get("export_columns")
if export_columns and isinstance(export_columns, list):
headers = [col.get("label") or col.get("key") for col in export_columns]
keys = [col.get("key") for col in export_columns]
else:
locale = negotiate_locale(request.headers.get("Accept-Language"))
is_fa = locale == "fa"
default_columns = [
("product_code", "کد محصول" if is_fa else "Product Code"),
("product_name", "نام محصول" if is_fa else "Product Name"),
("category_name", "دسته‌بندی" if is_fa else "Category"),
("warehouse_code", "کد انبار" if is_fa else "Warehouse Code"),
("warehouse_name", "نام انبار" if is_fa else "Warehouse Name"),
("quantity", "موجودی" if is_fa else "Quantity"),
("unit", "واحد" if is_fa else "Unit"),
("track_inventory", "کنترل موجودی" if is_fa else "Track inventory"),
]
keys = [k for k, _ in default_columns]
headers = [h for _, h in default_columns]
wb = Workbook()
ws = wb.active
ws.title = "Inventory Stock"
locale = negotiate_locale(request.headers.get("Accept-Language"))
if locale == "fa":
try:
ws.sheet_view.rightToLeft = True
except Exception:
pass
header_font = Font(bold=True)
header_fill = PatternFill(start_color="DDDDDD", end_color="DDDDDD", fill_type="solid")
thin_border = Border(
left=Side(style="thin"),
right=Side(style="thin"),
top=Side(style="thin"),
bottom=Side(style="thin"),
)
ws.append(headers)
for cell in ws[1]:
cell.font = header_font
cell.fill = header_fill
cell.alignment = Alignment(horizontal="center")
cell.border = thin_border
def _cell_val(raw_key: str, it: Dict[str, Any]):
val = it.get(raw_key)
if raw_key == "track_inventory":
if negotiate_locale(request.headers.get("Accept-Language")) == "fa":
return "بله" if val else "خیر"
return "Yes" if val else "No"
return val
for it in items:
row = []
for k in keys:
value = _cell_val(k, it)
if value is None:
row.append("")
elif isinstance(value, (int, float)):
row.append(float(value))
else:
row.append(str(value))
ws.append(row)
for cell in ws[ws.max_row]:
cell.border = thin_border
if locale == "fa":
cell.alignment = Alignment(horizontal="right")
try:
for column in ws.columns:
max_length = 0
column_letter = column[0].column_letter
for cell in column:
try:
if cell.value is not None and len(str(cell.value)) > max_length:
max_length = len(str(cell.value))
except Exception:
pass
ws.column_dimensions[column_letter].width = min(max_length + 2, 50)
except Exception:
pass
output = io.BytesIO()
wb.save(output)
data = output.getvalue()
biz_name = ""
try:
b = db.query(Business).filter(Business.id == business_id).first()
if b is not None:
biz_name = b.name or ""
except Exception:
biz_name = ""
def slugify(text: str) -> str:
return re.sub(r"[^A-Za-z0-9_-]+", "_", text).strip("_")
base = "inventory_stock"
if biz_name:
base += f"_{slugify(biz_name)}"
if selected_only:
base += "_selected"
filename = f"{base}_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx"
return Response(
content=data,
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
headers={
"Content-Disposition": f"attachment; filename={filename}",
"Content-Length": str(len(data)),
"Access-Control-Expose-Headers": "Content-Disposition",
},
)
@router.post(
"/businesses/{business_id}/reports/inventory-stock/export/pdf",
summary="خروجی PDF گزارش موجودی انبار",
responses={
200: {
"description": "فایل PDF",
"content": {"application/pdf": {"schema": {"type": "string", "format": "binary"}}},
}
},
)
@require_business_access("business_id")
async def export_inventory_stock_report_pdf(
request: Request,
business_id: int,
body: Dict[str, Any] = Body(default={}),
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("reports", "export")),
):
"""خروجی PDF گزارش موجودی انبار"""
import datetime
from weasyprint import HTML, CSS
from weasyprint.text.fonts import FontConfiguration
from app.core.calendar import CalendarConverter
from app.services.pdf.template_renderer import render_template, load_farsi_font_data_uris
if not ctx.can_read_section("reports"):
raise ApiError("FORBIDDEN", "Missing business permission: reports.read", http_status=403)
fiscal_year_id = None
fy_header = request.headers.get("X-Fiscal-Year-ID")
if fy_header:
try:
fiscal_year_id = int(fy_header)
except (ValueError, TypeError):
pass
if body.get("fiscal_year_id"):
try:
fiscal_year_id = int(body["fiscal_year_id"])
except (ValueError, TypeError):
pass
product_ids = body.get("product_ids")
if product_ids is not None and not isinstance(product_ids, list):
product_ids = None
warehouse_ids = body.get("warehouse_ids")
if warehouse_ids is not None and not isinstance(warehouse_ids, list):
warehouse_ids = None
category_ids = body.get("category_ids")
if category_ids is not None and not isinstance(category_ids, list):
category_ids = None
as_of_date = body.get("as_of_date")
track_inventory = body.get("track_inventory")
if track_inventory is not None:
try:
track_inventory = bool(track_inventory)
except (ValueError, TypeError):
track_inventory = None
only_negative_stock = bool(body.get("only_negative_stock", False))
only_without_movements = bool(body.get("only_without_movements", False))
include_zero = bool(body.get("include_zero", False))
search = body.get("search")
selected_only = bool(body.get("selected_only", False))
selected_indices = body.get("selected_indices")
selected_row_keys = body.get("selected_row_keys")
max_export_records = 10000
result = get_inventory_stock_report(
db=db,
business_id=business_id,
fiscal_year_id=fiscal_year_id,
product_ids=product_ids,
warehouse_ids=warehouse_ids,
category_ids=category_ids,
as_of_date=as_of_date,
track_inventory=track_inventory,
only_negative_stock=only_negative_stock,
only_without_movements=only_without_movements,
include_zero=include_zero,
search=search,
skip=0,
take=max_export_records,
for_export=True,
)
items = result.get("items", [])
items = [format_datetime_fields(item, request) for item in items]
if selected_only and selected_row_keys is not None and isinstance(selected_row_keys, list):
try:
wanted = [k for k in selected_row_keys if isinstance(k, dict)]
if wanted:
def _match_key(it: dict, key: dict) -> bool:
for fld in ("product_id", "warehouse_id"):
if key.get(fld) is not None and it.get(fld) is not None:
try:
if int(it.get(fld)) != int(key.get(fld)):
return False
except Exception:
return False
return True
filt = []
for it in items:
for k in wanted:
if _match_key(it, k):
filt.append(it)
break
items = filt
except Exception:
pass
if selected_only and selected_indices is not None:
indices = None
if isinstance(selected_indices, str):
try:
import json as _json
indices = _json.loads(selected_indices)
except Exception:
indices = None
elif isinstance(selected_indices, list):
indices = selected_indices
if isinstance(indices, list):
items = [items[i] for i in indices if isinstance(i, int) and 0 <= i < len(items)]
calendar_type = getattr(request.state, "calendar_type", "jalali")
locale = negotiate_locale(request.headers.get("Accept-Language"))
is_fa = locale == "fa"
try:
_now = datetime.datetime.now()
_gen = CalendarConverter.format_datetime(_now, calendar_type).get("formatted") or ""
generated_at = " ".join(_gen.split(" ")[:2])
if generated_at.count(":") >= 2:
generated_at = generated_at.rsplit(":", 1)[0]
except Exception:
generated_at = datetime.datetime.now().strftime("%Y/%m/%d %H:%M")
filters_summary = []
if as_of_date:
filters_summary.append(
{"label": "تاریخ گزارش" if is_fa else "As of date", "value": str(as_of_date)}
)
if search:
filters_summary.append({"label": "جستجو" if is_fa else "Search", "value": str(search)})
if track_inventory is True:
filters_summary.append({"label": "کنترل موجودی" if is_fa else "Tracking", "value": "فعال" if is_fa else "On"})
elif track_inventory is False:
filters_summary.append({"label": "کنترل موجودی" if is_fa else "Tracking", "value": "غیرفعال" if is_fa else "Off"})
if only_negative_stock:
filters_summary.append(
{"label": "فیلتر" if is_fa else "Filter", "value": "فقط منفی" if is_fa else "Negative only"}
)
if only_without_movements:
filters_summary.append(
{
"label": "فیلتر" if is_fa else "Filter",
"value": "بدون حرکت" if is_fa else "No movements",
}
)
if include_zero:
filters_summary.append(
{"label": "شامل صفر" if is_fa else "Include zero", "value": "بله" if is_fa else "Yes"}
)
try:
from app.services.print_footer_settings import build_report_title_and_time_footer
_t = "گزارش موجودی انبار" if is_fa else "Inventory Stock Report"
footer_text = build_report_title_and_time_footer(
db,
business_id,
title=_t,
time_part=generated_at,
preparer_name=ctx.get_user_name() or None,
is_fa=is_fa,
)
except Exception:
footer_text = f"{'گزارش موجودی انبار' if is_fa else 'Inventory Stock Report'} • {generated_at}"
business_name = ""
try:
b = db.query(Business).filter(Business.id == business_id).first()
if b is not None:
business_name = b.name or ""
except Exception:
business_name = ""
try:
qp = request.query_params
paper_size = qp.get("paper_size")
orientation = qp.get("orientation") or "landscape"
disposition = qp.get("disposition") or "attachment"
except Exception:
paper_size = None
orientation = "landscape"
disposition = "attachment"
fa_font_url_regular = ""
fa_font_url_bold = ""
try:
if is_fa:
fa_reg, fa_bold = load_farsi_font_data_uris()
fa_font_url_regular = fa_reg or ""
fa_font_url_bold = fa_bold or ""
except Exception:
pass
explicit_template_id = None
try:
if body.get("template_id") is not None:
explicit_template_id = int(body.get("template_id"))
except Exception:
explicit_template_id = None
resolved_html = None
try:
from app.services.report_template_service import ReportTemplateService
template_context = {
"title_text": "گزارش موجودی انبار" if is_fa else "Inventory Stock Report",
"business_name": business_name,
"generated_at": generated_at,
"is_fa": is_fa,
"locale": locale,
"paper_size": paper_size,
"orientation": orientation,
"fa_font_url_regular": fa_font_url_regular,
"fa_font_url_bold": fa_font_url_bold,
"footer_text": footer_text,
"filters_summary": filters_summary,
"items": items,
}
resolved_html = ReportTemplateService.try_render_resolved(
db=db,
business_id=business_id,
module_key="inventory_stock",
subtype="list",
context=template_context,
explicit_template_id=explicit_template_id,
)
except Exception:
resolved_html = None
if not resolved_html:
final_html = render_template(
"pdf/inventory_stock/list.html",
{
"title_text": "گزارش موجودی انبار" if is_fa else "Inventory Stock Report",
"business_name": business_name,
"generated_at": generated_at,
"is_fa": is_fa,
"locale": locale,
"paper_size": paper_size or "A4",
"orientation": orientation or "landscape",
"fa_font_url_regular": fa_font_url_regular,
"fa_font_url_bold": fa_font_url_bold,
"footer_text": footer_text,
"filters_summary": filters_summary,
"items": items,
},
)
else:
final_html = resolved_html
page_css = f"@page {{ size: {(paper_size or 'A4')} {(orientation or 'landscape')}; margin: 1cm; }}"
font_config = FontConfiguration()
pdf_bytes = HTML(string=final_html).write_pdf(
stylesheets=[CSS(string=page_css)],
font_config=font_config,
)
fn = f"inventory_stock_{business_id}_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
return Response(
content=pdf_bytes,
media_type="application/pdf",
headers={
"Content-Disposition": f"{disposition}; filename={fn}",
"Content-Length": str(len(pdf_bytes)),
"Access-Control-Expose-Headers": "Content-Disposition",
},
)
@router.post(
"/businesses/{business_id}/reports/inventory-kardex/export/excel",
summary="خروجی Excel گزارش کاردکس موجودی",
description="""
دریافت گزارش کاردکس موجودی به صورت فایل Excel
### قابلیت‌ها:
- فیلتر بر اساس محصولات، انبارها و تاریخ
- انتخاب سطرهای خاص برای خروجی
- انتخاب و مرتب‌سازی ستون‌ها
- نمایش تمام حرکات موجودی با مانده تجمعی
- پشتیبانی از RTL برای زبان فارسی
### فرمت فایل:
- فرمت: `.xlsx` (Excel 2007+)
""",
responses={
200: {
"description": "فایل Excel با موفقیت ایجاد شد",
"content": {
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": {
"schema": {
"type": "string",
"format": "binary"
}
}
}
},
400: {
"description": "خطا در اعتبارسنجی درخواست"
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز"
}
}
)
@require_business_access("business_id")
async def export_inventory_kardex_report_excel(
request: Request,
business_id: int,
body: Dict[str, Any] = Body(default={}),
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("reports", "export")),
):
"""خروجی Excel گزارش کاردکس موجودی"""
from fastapi.responses import Response
from openpyxl import Workbook
from openpyxl.styles import Font, Alignment, PatternFill, Border, Side
import io
import datetime
import re
# دریافت سال مالی از header یا body
fiscal_year_id = None
fy_header = request.headers.get('X-Fiscal-Year-ID')
if fy_header:
try:
fiscal_year_id = int(fy_header)
except (ValueError, TypeError):
pass
if body.get('fiscal_year_id'):
try:
fiscal_year_id = int(body['fiscal_year_id'])
except (ValueError, TypeError):
pass
if not fiscal_year_id:
from adapters.db.models.fiscal_year import FiscalYear
fiscal_year = db.query(FiscalYear).filter(
and_(
FiscalYear.business_id == business_id,
FiscalYear.is_last == True
)
).first()
if fiscal_year:
fiscal_year_id = fiscal_year.id
# استخراج پارامترها از body
date_from = body.get('date_from')
date_to = body.get('date_to')
product_ids = body.get('product_ids')
if product_ids is not None and not isinstance(product_ids, list):
product_ids = None
warehouse_ids = body.get('warehouse_ids')
if warehouse_ids is not None and not isinstance(warehouse_ids, list):
warehouse_ids = None
category_ids = body.get('category_ids')
if category_ids is not None and not isinstance(category_ids, list):
category_ids = None
search = body.get('search')
# اعمال فیلتر سطرهای انتخاب شده
selected_only = bool(body.get('selected_only', False))
selected_indices = body.get('selected_indices')
selected_row_keys = body.get('selected_row_keys')
# دریافت گزارش با take بزرگ برای export
max_export_records = 10000
result = get_inventory_kardex_report(
db=db,
business_id=business_id,
fiscal_year_id=fiscal_year_id,
date_from=date_from,
date_to=date_to,
product_ids=product_ids,
warehouse_ids=warehouse_ids,
category_ids=category_ids,
search=search,
skip=0,
take=max_export_records,
)
items = result.get('items', [])
items = [format_datetime_fields(item, request) for item in items]
# Get calendar type
calendar_type = "gregorian"
if hasattr(request.state, 'calendar_type'):
calendar_type = request.state.calendar_type
# Helper function to format date based on calendar type
def format_date_for_export(item_dict: dict, date_key: str) -> str:
"""Format date based on calendar type (date only, no time)"""
from app.core.calendar import CalendarConverter
# First check if there's a _formatted field (from format_datetime_fields)
formatted_key = f"{date_key}_formatted"
if formatted_key in item_dict:
formatted_value = item_dict.get(formatted_key)
if isinstance(formatted_value, dict):
date_only = formatted_value.get("date_only")
if date_only:
return str(date_only)
formatted = formatted_value.get("formatted", "")
if formatted:
# Extract date part only (remove time)
date_part = str(formatted).split(' ')[0].split('T')[0]
return date_part
# Get the main field value
value = item_dict.get(date_key)
if value is None:
return ""
# If it's a dict (from _formatted field), use date_only
if isinstance(value, dict):
date_only = value.get("date_only")
if date_only:
return str(date_only)
formatted = value.get("formatted", "")
if formatted:
date_part = str(formatted).split(' ')[0].split('T')[0]
return date_part
# If it's a datetime object, format it based on calendar type
if isinstance(value, datetime.datetime):
try:
formatted = CalendarConverter.format_datetime(value, calendar_type)
return formatted.get("date_only", "") or formatted.get("formatted", "").split(' ')[0]
except Exception:
pass
# If it's a date object, format it based on calendar type
if isinstance(value, datetime.date):
try:
dt_value = datetime.datetime.combine(value, datetime.datetime.min.time())
formatted = CalendarConverter.format_datetime(dt_value, calendar_type)
return formatted.get("date_only", "") or formatted.get("formatted", "").split(' ')[0]
except Exception:
pass
# If it's a string, check if it's already formatted (contains / separator for Jalali)
if isinstance(value, str):
# Check if it looks like a Jalali date (contains / and has YYYY/MM/DD format)
if '/' in value and (len(value.split('/')) == 3):
# Might be already formatted, but check if it's ISO format (YYYY-MM-DD) or Jalali (YYYY/MM/DD)
if '-' in value:
# ISO format (YYYY-MM-DD), parse and format
try:
if 'T' in value:
dt_value = datetime.datetime.fromisoformat(value.replace('Z', '+00:00'))
else:
date_value = datetime.date.fromisoformat(value)
dt_value = datetime.datetime.combine(date_value, datetime.datetime.min.time())
formatted = CalendarConverter.format_datetime(dt_value, calendar_type)
return formatted.get("date_only", "") or formatted.get("formatted", "").split(' ')[0]
except Exception:
pass
else:
# Might be Jalali format (YYYY/MM/DD), return as is but remove time if exists
if ' ' in value:
return value.split(' ')[0]
return value
else:
# Try to parse as ISO format
try:
if 'T' in value:
dt_value = datetime.datetime.fromisoformat(value.replace('Z', '+00:00'))
else:
date_value = datetime.date.fromisoformat(value)
dt_value = datetime.datetime.combine(date_value, datetime.datetime.min.time())
formatted = CalendarConverter.format_datetime(dt_value, calendar_type)
return formatted.get("date_only", "") or formatted.get("formatted", "").split(' ')[0]
except Exception:
# If parsing fails, return as is (might already be formatted)
if ' ' in value or 'T' in value:
date_part = value.split(' ')[0].split('T')[0]
return date_part
return value
# Fallback
return str(value) if value else ""
# فیلتر سطرهای انتخاب شده (ترجیحاً بر اساس کلیدهای پایدار)
if selected_only and selected_row_keys is not None and isinstance(selected_row_keys, list):
try:
wanted = []
for k in selected_row_keys:
if isinstance(k, dict):
wanted.append(k)
if wanted:
def _match_key(it: dict, key: dict) -> bool:
# Match by strongest key first
if key.get("document_id") is not None and it.get("document_id") is not None:
try:
if int(it.get("document_id")) != int(key.get("document_id")):
return False
except Exception:
return False
# Match by product/warehouse if present
for fld in ("product_id", "warehouse_id"):
if key.get(fld) is not None and it.get(fld) is not None:
try:
if int(it.get(fld)) != int(key.get(fld)):
return False
except Exception:
return False
# Match movement
if key.get("movement") is not None and it.get("movement") is not None:
if str(it.get("movement")).lower() != str(key.get("movement")).lower():
return False
# Match document_code/date if provided
if key.get("document_code") is not None and it.get("document_code") is not None:
if str(it.get("document_code")) != str(key.get("document_code")):
return False
if key.get("document_date") is not None and it.get("document_date") is not None:
# Compare date-only portion
if str(it.get("document_date")).split("T")[0].split(" ")[0] != str(key.get("document_date")).split("T")[0].split(" ")[0]:
return False
return True
filtered = []
for it in items:
for k in wanted:
if _match_key(it, k):
filtered.append(it)
break
items = filtered
except Exception:
pass
# فیلتر سطرهای انتخاب شده (سازگاری عقب‌رو با اندیس‌ها)
if selected_only and selected_indices is not None:
indices = None
if isinstance(selected_indices, str):
try:
import json as _json
indices = _json.loads(selected_indices)
except Exception:
indices = None
elif isinstance(selected_indices, list):
indices = selected_indices
if isinstance(indices, list):
items = [items[i] for i in indices if isinstance(i, int) and 0 <= i < len(items)]
# ستون‌های export
export_columns = body.get("export_columns")
if export_columns and isinstance(export_columns, list):
headers = [col.get("label") or col.get("key") for col in export_columns]
keys = [col.get("key") for col in export_columns]
else:
# ستون‌های پیش‌فرض
locale = negotiate_locale(request.headers.get("Accept-Language"))
is_fa = (locale == 'fa')
default_columns = [
('document_date', 'تاریخ' if is_fa else 'Document Date'),
('document_type_name', 'نوع سند' if is_fa else 'Document Type'),
('document_code', 'شماره سند' if is_fa else 'Document Code'),
('product_code', 'کد کالا' if is_fa else 'Product Code'),
('product_name', 'نام کالا' if is_fa else 'Product Name'),
('quantity_in', 'ورود' if is_fa else 'Quantity In'),
('quantity_out', 'خروج' if is_fa else 'Quantity Out'),
('balance', 'مانده' if is_fa else 'Balance'),
('unit_price', 'قیمت واحد' if is_fa else 'Unit Price'),
('total_amount', 'مبلغ کل' if is_fa else 'Total Amount'),
('warehouse_name', 'انبار' if is_fa else 'Warehouse'),
('description', 'توضیحات' if is_fa else 'Description'),
]
keys = [k for k, _ in default_columns]
headers = [h for _, h in default_columns]
# ساخت Excel
wb = Workbook()
ws = wb.active
ws.title = "Inventory Kardex"
# Locale and RTL/LTR handling for Excel
locale = negotiate_locale(request.headers.get("Accept-Language"))
if locale == 'fa':
try:
ws.sheet_view.rightToLeft = True
except Exception:
pass
# Header style
header_font = Font(bold=True)
header_fill = PatternFill(start_color="DDDDDD", end_color="DDDDDD", fill_type="solid")
thin_border = Border(left=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'), bottom=Side(style='thin'))
ws.append(headers)
for cell in ws[1]:
cell.font = header_font
cell.fill = header_fill
cell.alignment = Alignment(horizontal="center")
cell.border = thin_border
# افزودن ردیف‌های داده
for it in items:
row = []
for k in keys:
value = it.get(k)
if value is None:
row.append('')
elif k == 'document_date' and value:
# Format date based on calendar type
formatted_date = format_date_for_export(it, 'document_date')
row.append(formatted_date)
elif isinstance(value, (int, float)):
row.append(float(value))
else:
row.append(str(value))
ws.append(row)
for cell in ws[ws.max_row]:
cell.border = thin_border
# Align data cells based on locale
if locale == 'fa':
cell.alignment = Alignment(horizontal="right")
# Auto width columns
try:
for column in ws.columns:
max_length = 0
column_letter = column[0].column_letter
for cell in column:
try:
if cell.value is not None and len(str(cell.value)) > max_length:
max_length = len(str(cell.value))
except Exception:
pass
ws.column_dimensions[column_letter].width = min(max_length + 2, 50)
except Exception:
pass
output = io.BytesIO()
wb.save(output)
data = output.getvalue()
# Build meaningful filename
biz_name = ""
try:
from adapters.db.models.business import Business
b = db.query(Business).filter(Business.id == business_id).first()
if b is not None:
biz_name = b.name or ""
except Exception:
biz_name = ""
def slugify(text: str) -> str:
return re.sub(r"[^A-Za-z0-9_-]+", "_", text).strip("_")
base = "inventory_kardex"
if biz_name:
base += f"_{slugify(biz_name)}"
if selected_only:
base += "_selected"
filename = f"{base}_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx"
return Response(
content=data,
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
headers={
"Content-Disposition": f"attachment; filename={filename}",
"Content-Length": str(len(data)),
"Access-Control-Expose-Headers": "Content-Disposition",
},
)
@router.post(
"/businesses/{business_id}/reports/inventory-kardex/export/pdf",
summary="خروجی PDF گزارش کاردکس موجودی",
description="""
دریافت گزارش کاردکس موجودی به صورت فایل PDF
### قابلیت‌ها:
- فیلتر بر اساس محصولات، انبارها و تاریخ
- نمایش تمام حرکات موجودی با مانده تجمعی
- پشتیبانی از تقویم شمسی و میلادی (بر اساس header X-Calendar-Type)
- پشتیبانی از RTL برای زبان فارسی
- صفحه‌بندی خودکار
### فرمت فایل:
- فرمت: `.pdf`
""",
responses={
200: {
"description": "فایل PDF با موفقیت ایجاد شد",
"content": {
"application/pdf": {
"schema": {
"type": "string",
"format": "binary"
}
}
}
},
400: {
"description": "خطا در اعتبارسنجی درخواست"
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز"
}
}
)
@require_business_access("business_id")
async def export_inventory_kardex_report_pdf(
request: Request,
business_id: int,
body: Dict[str, Any] = Body(default={}),
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("reports", "export")),
):
"""خروجی PDF گزارش کاردکس موجودی"""
from fastapi.responses import Response
import datetime
from weasyprint import HTML, CSS
from weasyprint.text.fonts import FontConfiguration
from app.core.calendar import CalendarConverter
from app.services.pdf.template_renderer import render_template, load_farsi_font_data_uris
# Params
fiscal_year_id = None
fy_header = request.headers.get('X-Fiscal-Year-ID')
if fy_header:
try:
fiscal_year_id = int(fy_header)
except (ValueError, TypeError):
pass
if body.get('fiscal_year_id'):
try:
fiscal_year_id = int(body['fiscal_year_id'])
except (ValueError, TypeError):
pass
if not fiscal_year_id:
from adapters.db.models.fiscal_year import FiscalYear
fiscal_year = db.query(FiscalYear).filter(
and_(
FiscalYear.business_id == business_id,
FiscalYear.is_last == True
)
).first()
if fiscal_year:
fiscal_year_id = fiscal_year.id
date_from = body.get('date_from')
date_to = body.get('date_to')
product_ids = body.get('product_ids')
if product_ids is not None and not isinstance(product_ids, list):
product_ids = None
warehouse_ids = body.get('warehouse_ids')
if warehouse_ids is not None and not isinstance(warehouse_ids, list):
warehouse_ids = None
category_ids = body.get('category_ids')
if category_ids is not None and not isinstance(category_ids, list):
category_ids = None
search = body.get('search')
# Build a compact, human-readable filters summary for PDF (printer friendly)
filters_summary: list[dict] = []
try:
if date_from:
filters_summary.append({"label": "از" if getattr(request.state, "locale", "fa") == "fa" else "From", "value": str(date_from)})
if date_to:
filters_summary.append({"label": "تا" if getattr(request.state, "locale", "fa") == "fa" else "To", "value": str(date_to)})
if search:
filters_summary.append({"label": "جستجو" if getattr(request.state, "locale", "fa") == "fa" else "Search", "value": str(search)})
# Products / Warehouses / Categories: show names for short lists, otherwise show count
locale_for_labels = getattr(request.state, "locale", "fa")
is_fa_label = (locale_for_labels == "fa")
if product_ids:
try:
if len(product_ids) <= 3:
rows = (
db.query(Product)
.filter(Product.business_id == business_id, Product.id.in_(product_ids))
.all()
)
names = []
for p in rows:
code = getattr(p, "code", None)
name = getattr(p, "name", None)
names.append((f"{code} - {name}" if code and name else (name or code or str(p.id))))
vtxt = "، ".join(names) if is_fa_label else ", ".join(names)
else:
vtxt = f"{len(product_ids)} مورد" if is_fa_label else f"{len(product_ids)} items"
filters_summary.append({"label": "کالا" if is_fa_label else "Products", "value": vtxt})
except Exception:
filters_summary.append({"label": "کالا" if is_fa_label else "Products", "value": f"{len(product_ids)}"})
if warehouse_ids:
try:
from adapters.db.models.warehouse import Warehouse
if len(warehouse_ids) <= 3:
rows = (
db.query(Warehouse)
.filter(Warehouse.business_id == business_id, Warehouse.id.in_(warehouse_ids))
.all()
)
names = [getattr(w, "name", None) or str(getattr(w, "id", "")) for w in rows]
vtxt = "، ".join(names) if is_fa_label else ", ".join(names)
else:
vtxt = f"{len(warehouse_ids)} مورد" if is_fa_label else f"{len(warehouse_ids)} items"
filters_summary.append({"label": "انبار" if is_fa_label else "Warehouses", "value": vtxt})
except Exception:
filters_summary.append({"label": "انبار" if is_fa_label else "Warehouses", "value": f"{len(warehouse_ids)}"})
if category_ids:
try:
from adapters.db.models.support.category import Category
if len(category_ids) <= 3:
rows = db.query(Category).filter(Category.id.in_(category_ids)).all()
names = [getattr(c, "name", None) or str(getattr(c, "id", "")) for c in rows]
vtxt = "، ".join(names) if is_fa_label else ", ".join(names)
else:
vtxt = f"{len(category_ids)} مورد" if is_fa_label else f"{len(category_ids)} items"
filters_summary.append({"label": "دسته" if is_fa_label else "Categories", "value": vtxt})
except Exception:
filters_summary.append({"label": "دسته" if is_fa_label else "Categories", "value": f"{len(category_ids)}"})
except Exception:
filters_summary = []
selected_only = bool(body.get('selected_only', False))
selected_row_keys = body.get('selected_row_keys')
selected_indices = body.get('selected_indices')
max_export_records = 10000
result = get_inventory_kardex_report(
db=db,
business_id=business_id,
fiscal_year_id=fiscal_year_id,
date_from=date_from,
date_to=date_to,
product_ids=product_ids,
warehouse_ids=warehouse_ids,
category_ids=category_ids,
search=search,
skip=0,
take=max_export_records,
)
items = result.get('items', [])
items = [format_datetime_fields(item, request) for item in items]
# Determine calendar/locale
calendar_type = getattr(request.state, "calendar_type", "jalali")
locale = negotiate_locale(request.headers.get("Accept-Language"))
is_fa = (locale == "fa")
# زمان تولید گزارش بر اساس تقویم انتخابی
try:
_now = datetime.datetime.now()
_gen = CalendarConverter.format_datetime(_now, calendar_type).get("formatted") or ""
generated_at = " ".join(_gen.split(" ")[:2])
if generated_at.count(":") >= 2:
generated_at = generated_at.rsplit(":", 1)[0]
except Exception:
generated_at = datetime.datetime.now().strftime("%Y/%m/%d %H:%M")
try:
from app.services.print_footer_settings import build_report_title_and_time_footer
_t = "گزارش کاردکس موجودی" if is_fa else "Inventory Kardex Report"
_pn = ""
try:
_pn = ctx.get_user_name() or ""
except Exception:
_pn = ""
inventory_kardex_footer_text = build_report_title_and_time_footer(
db,
business_id,
title=_t,
time_part=generated_at,
preparer_name=_pn or None,
is_fa=is_fa,
)
except Exception:
inventory_kardex_footer_text = f"{'گزارش کاردکس موجودی' if is_fa else 'Inventory Kardex Report'} • {generated_at}"
# Apply selected rows filtering (prefer stable keys)
if selected_only and selected_row_keys is not None and isinstance(selected_row_keys, list):
try:
wanted = [k for k in selected_row_keys if isinstance(k, dict)]
if wanted:
def _match_key(it: dict, key: dict) -> bool:
if key.get("document_id") is not None and it.get("document_id") is not None:
try:
if int(it.get("document_id")) != int(key.get("document_id")):
return False
except Exception:
return False
for fld in ("product_id", "warehouse_id"):
if key.get(fld) is not None and it.get(fld) is not None:
try:
if int(it.get(fld)) != int(key.get(fld)):
return False
except Exception:
return False
if key.get("movement") is not None and it.get("movement") is not None:
if str(it.get("movement")).lower() != str(key.get("movement")).lower():
return False
if key.get("document_code") is not None and it.get("document_code") is not None:
if str(it.get("document_code")) != str(key.get("document_code")):
return False
if key.get("document_date") is not None and it.get("document_date") is not None:
if str(it.get("document_date")).split("T")[0].split(" ")[0] != str(key.get("document_date")).split("T")[0].split(" ")[0]:
return False
return True
filtered = []
for it in items:
for k in wanted:
if _match_key(it, k):
filtered.append(it)
break
items = filtered
except Exception:
pass
# Backward compatible selected_indices (may be mismatched if backend ordering differs)
if selected_only and (not items) and selected_indices is not None:
try:
indices = None
if isinstance(selected_indices, str):
import json as _json
indices = _json.loads(selected_indices)
elif isinstance(selected_indices, list):
indices = selected_indices
if isinstance(indices, list):
items = [result.get('items', [])[i] for i in indices if isinstance(i, int)]
except Exception:
pass
# Note: get_inventory_kardex_report now returns document_date as a date object;
# format_datetime_fields will convert it to the selected calendar string already.
# Business name
business_name = ""
try:
from adapters.db.models.business import Business
b = db.query(Business).filter(Business.id == business_id).first()
if b is not None:
business_name = b.name or ""
except Exception:
business_name = ""
# Page params via query params (optional)
try:
qp = request.query_params
paper_size = qp.get("paper_size")
orientation = qp.get("orientation") or "landscape"
disposition = qp.get("disposition") or "attachment"
except Exception:
paper_size = None
orientation = "landscape"
disposition = "attachment"
# Provide font data URIs for base.html/templates
fa_font_url_regular = ""
fa_font_url_bold = ""
try:
if is_fa:
fa_reg, fa_bold = load_farsi_font_data_uris()
fa_font_url_regular = fa_reg or ""
fa_font_url_bold = fa_bold or ""
except Exception:
fa_font_url_regular = ""
fa_font_url_bold = ""
# Template selection (custom templates supported)
resolved_html = None
explicit_template_id = None
try:
if body.get("template_id") is not None:
explicit_template_id = int(body.get("template_id"))
except Exception:
explicit_template_id = None
try:
from app.services.report_template_service import ReportTemplateService
template_context = {
"title_text": "گزارش کاردکس موجودی" if is_fa else "Inventory Kardex Report",
"business_name": business_name,
"generated_at": generated_at,
"is_fa": is_fa,
"locale": locale,
"paper_size": paper_size,
"orientation": orientation,
"fa_font_url_regular": fa_font_url_regular,
"fa_font_url_bold": fa_font_url_bold,
"footer_text": inventory_kardex_footer_text,
"filters_summary": filters_summary,
"items": items,
}
resolved_html = ReportTemplateService.try_render_resolved(
db=db,
business_id=business_id,
module_key="inventory_kardex",
subtype="list",
context=template_context,
explicit_template_id=explicit_template_id,
)
except Exception:
resolved_html = None
if not resolved_html:
final_html = render_template("pdf/inventory_kardex/list.html", {
"title_text": "گزارش کاردکس موجودی" if is_fa else "Inventory Kardex Report",
"business_name": business_name,
"generated_at": generated_at,
"is_fa": is_fa,
"locale": locale,
"paper_size": paper_size or "A4",
"orientation": orientation or "landscape",
"fa_font_url_regular": fa_font_url_regular,
"fa_font_url_bold": fa_font_url_bold,
"footer_text": inventory_kardex_footer_text,
"filters_summary": filters_summary,
"items": items,
})
else:
final_html = resolved_html
page_css = f"@page {{ size: {(paper_size or 'A4')} {(orientation or 'landscape')}; margin: 1cm; }}"
font_config = FontConfiguration()
pdf_bytes = HTML(string=final_html).write_pdf(
stylesheets=[CSS(string=page_css)],
font_config=font_config,
)
filename = f"inventory_kardex_{business_id}_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
return Response(
content=pdf_bytes,
media_type="application/pdf",
headers={
"Content-Disposition": f"{disposition}; filename={filename}",
"Content-Length": str(len(pdf_bytes)),
"Access-Control-Expose-Headers": "Content-Disposition",
},
)
@router.post(
"/businesses/{business_id}/reports/sales-by-product/export/excel",
summary="خروجی Excel گزارش فروش به تفکیک کالا",
description="""
دریافت گزارش فروش به تفکیک کالا به صورت فایل Excel
### قابلیت‌ها:
- فیلتر بر اساس محصولات و تاریخ
- انتخاب سطرهای خاص برای خروجی
- انتخاب و مرتب‌سازی ستون‌ها
- نمایش آمار فروش هر محصول
- پشتیبانی از RTL برای زبان فارسی
### فرمت فایل:
- فرمت: `.xlsx` (Excel 2007+)
""",
responses={
200: {
"description": "فایل Excel با موفقیت ایجاد شد",
"content": {
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": {
"schema": {
"type": "string",
"format": "binary"
}
}
}
},
400: {
"description": "خطا در اعتبارسنجی درخواست"
},
401: {
"description": "کاربر احراز هویت نشده است"
},
403: {
"description": "دسترسی غیرمجاز"
}
}
)
@require_business_access("business_id")
async def export_sales_by_product_report_excel(
request: Request,
business_id: int,
body: Dict[str, Any] = Body(default={}),
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("reports", "export")),
):
"""خروجی Excel گزارش فروش به تفکیک کالا"""
from fastapi.responses import Response
from openpyxl import Workbook
from openpyxl.styles import Font, Alignment, PatternFill, Border, Side
import io
import datetime
import re
# دریافت سال مالی از header یا body
fiscal_year_id = None
fy_header = request.headers.get('X-Fiscal-Year-ID')
if fy_header:
try:
fiscal_year_id = int(fy_header)
except (ValueError, TypeError):
pass
if body.get('fiscal_year_id'):
try:
fiscal_year_id = int(body['fiscal_year_id'])
except (ValueError, TypeError):
pass
if not fiscal_year_id:
from adapters.db.models.fiscal_year import FiscalYear
fiscal_year = db.query(FiscalYear).filter(
and_(
FiscalYear.business_id == business_id,
FiscalYear.is_last == True
)
).first()
if fiscal_year:
fiscal_year_id = fiscal_year.id
# استخراج پارامترها از body
date_from = body.get('date_from')
date_to = body.get('date_to')
currency_id = body.get('currency_id')
if currency_id is not None:
try:
currency_id = int(currency_id)
except (ValueError, TypeError):
currency_id = None
product_ids = body.get('product_ids')
if product_ids is not None and not isinstance(product_ids, list):
product_ids = None
category_ids = body.get('category_ids')
if category_ids is not None and not isinstance(category_ids, list):
category_ids = None
warehouse_ids = body.get('warehouse_ids')
if warehouse_ids is not None and not isinstance(warehouse_ids, list):
warehouse_ids = None
include_zero_sales = bool(body.get('include_zero_sales', False))
search = body.get('search')
# اعمال فیلتر سطرهای انتخاب شده
selected_only = bool(body.get('selected_only', False))
selected_indices = body.get('selected_indices')
# دریافت گزارش با take بزرگ برای export
max_export_records = 10000
result = get_sales_by_product_report(
db=db,
business_id=business_id,
fiscal_year_id=fiscal_year_id,
currency_id=currency_id,
date_from=date_from,
date_to=date_to,
product_ids=product_ids,
category_ids=category_ids,
warehouse_ids=warehouse_ids,
include_zero_sales=include_zero_sales,
search=search,
skip=0,
take=max_export_records,
)
items = result.get('items', [])
items = [format_datetime_fields(item, request) for item in items]
# Get calendar type
calendar_type = "gregorian"
if hasattr(request.state, 'calendar_type'):
calendar_type = request.state.calendar_type
# Helper function to format date based on calendar type
def format_date_for_export(item_dict: dict, date_key: str) -> str:
"""Format date based on calendar type (date only, no time)"""
from app.core.calendar import CalendarConverter
# First check if there's a _formatted field (from format_datetime_fields)
formatted_key = f"{date_key}_formatted"
if formatted_key in item_dict:
formatted_value = item_dict.get(formatted_key)
if isinstance(formatted_value, dict):
date_only = formatted_value.get("date_only")
if date_only:
return str(date_only)
formatted = formatted_value.get("formatted", "")
if formatted:
# Extract date part only (remove time)
date_part = str(formatted).split(' ')[0].split('T')[0]
return date_part
# Get the main field value
value = item_dict.get(date_key)
if value is None:
return ""
# If it's a dict (from _formatted field), use date_only
if isinstance(value, dict):
date_only = value.get("date_only")
if date_only:
return str(date_only)
formatted = value.get("formatted", "")
if formatted:
date_part = str(formatted).split(' ')[0].split('T')[0]
return date_part
# If it's a datetime object, format it based on calendar type
if isinstance(value, datetime.datetime):
try:
formatted = CalendarConverter.format_datetime(value, calendar_type)
return formatted.get("date_only", "") or formatted.get("formatted", "").split(' ')[0]
except Exception:
pass
# If it's a date object, format it based on calendar type
if isinstance(value, datetime.date):
try:
dt_value = datetime.datetime.combine(value, datetime.datetime.min.time())
formatted = CalendarConverter.format_datetime(dt_value, calendar_type)
return formatted.get("date_only", "") or formatted.get("formatted", "").split(' ')[0]
except Exception:
pass
# If it's a string, check if it's already formatted (contains / separator for Jalali)
if isinstance(value, str):
# Check if it looks like a Jalali date (contains / and has YYYY/MM/DD format)
if '/' in value and (len(value.split('/')) == 3):
# Might be already formatted, but check if it's ISO format (YYYY-MM-DD) or Jalali (YYYY/MM/DD)
if '-' in value:
# ISO format (YYYY-MM-DD), parse and format
try:
if 'T' in value:
dt_value = datetime.datetime.fromisoformat(value.replace('Z', '+00:00'))
else:
date_value = datetime.date.fromisoformat(value)
dt_value = datetime.datetime.combine(date_value, datetime.datetime.min.time())
formatted = CalendarConverter.format_datetime(dt_value, calendar_type)
return formatted.get("date_only", "") or formatted.get("formatted", "").split(' ')[0]
except Exception:
pass
else:
# Might be Jalali format (YYYY/MM/DD), return as is but remove time if exists
if ' ' in value:
return value.split(' ')[0]
return value
else:
# Try to parse as ISO format
try:
if 'T' in value:
dt_value = datetime.datetime.fromisoformat(value.replace('Z', '+00:00'))
else:
date_value = datetime.date.fromisoformat(value)
dt_value = datetime.datetime.combine(date_value, datetime.datetime.min.time())
formatted = CalendarConverter.format_datetime(dt_value, calendar_type)
return formatted.get("date_only", "") or formatted.get("formatted", "").split(' ')[0]
except Exception:
# If parsing fails, return as is (might already be formatted)
if ' ' in value or 'T' in value:
date_part = value.split(' ')[0].split('T')[0]
return date_part
return value
# Fallback
return str(value) if value else ""
# فیلتر سطرهای انتخاب شده
if selected_only and selected_indices is not None:
indices = None
if isinstance(selected_indices, str):
try:
import json as _json
indices = _json.loads(selected_indices)
except Exception:
indices = None
elif isinstance(selected_indices, list):
indices = selected_indices
if isinstance(indices, list):
items = [items[i] for i in indices if isinstance(i, int) and 0 <= i < len(items)]
# ستون‌های export
export_columns = body.get("export_columns")
if export_columns and isinstance(export_columns, list):
headers = [col.get("label") or col.get("key") for col in export_columns]
keys = [col.get("key") for col in export_columns]
else:
# ستون‌های پیش‌فرض
locale = negotiate_locale(request.headers.get("Accept-Language"))
is_fa = (locale == 'fa')
default_columns = [
('product_code', 'کد کالا' if is_fa else 'Product Code'),
('product_name', 'نام کالا' if is_fa else 'Product Name'),
('unit', 'واحد' if is_fa else 'Unit'),
('category_name', 'دسته‌بندی' if is_fa else 'Category'),
('total_quantity', 'تعداد فروش' if is_fa else 'Total Quantity'),
('total_amount', 'مبلغ کل فروش' if is_fa else 'Total Amount'),
('average_price', 'میانگین قیمت' if is_fa else 'Average Price'),
('last_sale_date', 'آخرین تاریخ فروش' if is_fa else 'Last Sale Date'),
]
keys = [k for k, _ in default_columns]
headers = [h for _, h in default_columns]
# ساخت Excel
wb = Workbook()
ws = wb.active
ws.title = "Sales by Product"
# Locale and RTL/LTR handling for Excel
locale = negotiate_locale(request.headers.get("Accept-Language"))
if locale == 'fa':
try:
ws.sheet_view.rightToLeft = True
except Exception:
pass
# Header style
header_font = Font(bold=True)
header_fill = PatternFill(start_color="DDDDDD", end_color="DDDDDD", fill_type="solid")
thin_border = Border(left=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'), bottom=Side(style='thin'))
ws.append(headers)
for cell in ws[1]:
cell.font = header_font
cell.fill = header_fill
cell.alignment = Alignment(horizontal="center")
cell.border = thin_border
# افزودن ردیف‌های داده
for it in items:
row = []
for k in keys:
value = it.get(k)
if value is None:
row.append('')
elif k == 'last_sale_date' and value:
# Format date based on calendar type
formatted_date = format_date_for_export(it, 'last_sale_date')
row.append(formatted_date)
elif isinstance(value, (int, float)):
row.append(float(value))
else:
row.append(str(value))
ws.append(row)
for cell in ws[ws.max_row]:
cell.border = thin_border
# Align data cells based on locale
if locale == 'fa':
cell.alignment = Alignment(horizontal="right")
# Auto width columns
try:
for column in ws.columns:
max_length = 0
column_letter = column[0].column_letter
for cell in column:
try:
if cell.value is not None and len(str(cell.value)) > max_length:
max_length = len(str(cell.value))
except Exception:
pass
ws.column_dimensions[column_letter].width = min(max_length + 2, 50)
except Exception:
pass
output = io.BytesIO()
wb.save(output)
data = output.getvalue()
# Build meaningful filename
biz_name = ""
try:
b = db.query(Business).filter(Business.id == business_id).first()
if b is not None:
biz_name = b.name or ""
except Exception:
biz_name = ""
def slugify(text: str) -> str:
return re.sub(r"[^A-Za-z0-9_-]+", "_", text).strip("_")
base = "sales_by_product"
if biz_name:
base += f"_{slugify(biz_name)}"
if selected_only:
base += "_selected"
filename = f"{base}_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx"
return Response(
content=data,
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
headers={
"Content-Disposition": f"attachment; filename={filename}",
"Content-Length": str(len(data)),
"Access-Control-Expose-Headers": "Content-Disposition",
},
)
@router.post(
"/businesses/{business_id}/reports/sales-by-product/export/pdf",
summary="خروجی PDF گزارش فروش به تفکیک کالا",
)
@require_business_access("business_id")
async def export_sales_by_product_report_pdf(
request: Request,
business_id: int,
body: Dict[str, Any] = Body(default={}),
ctx: AuthContext = Depends(get_current_user),
db: Session = Depends(get_db),
_: None = Depends(require_business_permission_dep("reports", "export")),
):
from app.services.turnover_report_export_service import (
SALES_BY_PRODUCT_COLUMNS,
turnover_pdf_response,
)
product_ids = body.get("product_ids")
if product_ids is not None and not isinstance(product_ids, list):
product_ids = None
category_ids = body.get("category_ids")
if category_ids is not None and not isinstance(category_ids, list):
category_ids = None
warehouse_ids = body.get("warehouse_ids")
if warehouse_ids is not None and not isinstance(warehouse_ids, list):
warehouse_ids = None
return turnover_pdf_response(
request,
business_id,
body,
ctx,
db,
fetch_fn=get_sales_by_product_report,
columns=SALES_BY_PRODUCT_COLUMNS,
filename_prefix="sales_by_product",
title_fa="گزارش فروش به تفکیک کالا",
title_en="Sales by Product Report",
fetch_kwargs={
"product_ids": product_ids,
"category_ids": category_ids,
"warehouse_ids": warehouse_ids,
"include_zero_sales": bool(body.get("include_zero_sales", False)),
},
)