Files
AutoMarket/backend/routes/finance.py

48 lines
1.8 KiB
Python
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.
from flask import Blueprint, request, jsonify
from ..middleware import token_required
finance_blueprint = Blueprint('finance', __name__)
@finance_blueprint.route('/finance/calculate-pnl', methods=['POST'])
@token_required
def calculate_pnl(current_user_id):
"""
Расчет чистой прибыли и маржинальности.
"""
data = request.json
# Входные данные
sale_price = float(data.get('sale_price', 0)) # Цена продажи
purchase_price = float(data.get('purchase_price', 0)) # Себестоимость (закупка)
logistics = float(data.get('logistics', 0)) # Логистика
commission_pct = float(data.get('commission_pct', 0)) # Комиссия маркетплейса %
tax_pct = float(data.get('tax_pct', 6)) # Налог % (УСН)
adv_per_unit = float(data.get('adv_per_unit', 0)) # Реклама на 1 шт
other_costs = float(data.get('other_costs', 0)) # Прочие расходы (упаковка и т.д.)
# Расчеты
marketplace_commission = sale_price * (commission_pct / 100)
taxes = sale_price * (tax_pct / 100)
total_costs = (
purchase_price +
logistics +
marketplace_commission +
taxes +
adv_per_unit +
other_costs
)
net_profit = sale_price - total_costs
margin = (net_profit / sale_price * 100) if sale_price > 0 else 0
roi = (net_profit / purchase_price * 100) if purchase_price > 0 else 0
return jsonify({
"net_profit": round(net_profit, 2),
"margin": round(margin, 2),
"roi": round(roi, 2),
"total_costs": round(total_costs, 2),
"tax_amount": round(taxes, 2),
"commission_amount": round(marketplace_commission, 2)
})