o
    jEi                     @   sh   d Z ddlZddlZddlZejejejeddZ	dd Z
dd Zd	d
 Zdd Zdd ZdS )z
This module contains functions for calculating business-related metrics
such as costs, profit, and margins based on deal data and user settings.
    Nz..zsettings.jsonc               
   C   s   t t} z(ttd}t|}| dt  |W  d   W S 1 s&w   Y  W dS  ttj	fyV } z| 
dt d| d ddd	d
ddW  Y d}~S d}~ww )z=Loads the business cost settings from the settings.json file.rz"Successfully loaded settings from NzCould not load or parse z: z. Returning default values.g      @g       @   F
   )prep_fee_per_bookestimated_shipping_per_bookestimated_tax_per_book
tax_exemptdefault_markup)logging	getLogger__name__openSETTINGS_FILEjsonloadinfoFileNotFoundErrorJSONDecodeErrorerror)loggerfsettingse r   </var/www/agentarbitrage/keepa_deals/business_calculations.pyload_settings   s"   

(r   c                  G   s*   | D ]}t |ttfr|dk r dS qdS )zKHelper function to check if all arguments are valid numbers (int or float).r   FT)
isinstanceintfloat)argsargr   r   r   _is_valid_numeric   s
   r!   c                 C   s  t t}tdd | ||fD s!|d|  d| d|  dS t|}d}|rC|d }	||	 }|d	|d
d|	d
d|d
 n	|d| d || }
|d|d
d|d
d|
d
 |dd}|dd}|dd}|dd}|d| d| d| d|  d}|s| dkr| |d  }|s|nd}| | | |
 | }|d| d
d|d
d|d
d|
d
d |d
d|d
 |S )!z
    Calculates the all-in cost for acquiring and selling a book.
    The Referral Fee is now correctly calculated based on the 'List at' price.
    c                 s   s    | ]}t |V  qd S )N)r!   ).0valr   r   r   	<genexpr>.   s    z(calculate_all_in_cost.<locals>.<genexpr>z=Skipping All-in Cost: Invalid base numeric inputs. now_price=z
, fba_fee=z, ref_fee_pct=-g              Y@zReferral Fee: List At Price (.2fz) * z = zCReferral Fee: Could not be calculated as List At Price is invalid (z).zTotal AMZ Fees: Referral Fee (z) + FBA Fee (z) = r   r   r   r   Fr   zCost settings: Prep=$z, Tax=z
%, Exempt=z, Est. Ship=$z"All-in Cost Breakdown: Now Price (z	) + Tax (z
) + Prep (z) + AMZ Fees (z) + Added Ship ()r
   r   r   allr   r!   debugget)	now_pricelist_at_pricefba_feereferral_fee_percentr   shipping_included_flagr   is_list_at_price_validreferral_fee_amountreferral_fee_decimaltotal_amz_feesprep_feetax_percentis_tax_exemptestimated_shipping
tax_amountshipping_cost_to_addall_in_costr   r   r   calculate_all_in_cost&   sN   
$""	r;   c                 C   s   t t}t| |s|d|  d|  dddS | | }| dkr(||  d nd}|d| dd	|dd
|dd|dd	 ||dS )z
    Calculates the potential profit and profit margin.
    Returns a dictionary with 'profit' and 'margin', or '-' if inputs are invalid.
    zESkipping Profit/Margin calculation due to invalid inputs: peak_price=z, all_in_cost=r%   )profitmarginr   d   zProfit/Margin: (Peak r'   z - Cost z) = Profit z	, Margin %)r
   r   r   r!   r)   )
peak_pricer:   r   r<   r=   r   r   r   calculate_profit_and_margin\   s   


,
rA   c              	   C   s   t t}t| s|d|   dS |dd}|dkr(|d| d d}d	|d
  }|dkr?|d| d|  dS | | }|d| dd| d|dd |S )z
    Calculates the minimum listing price required to achieve the user's default markup.
    This value is intended for use as a floor price in repricing software.
    Formula: All-in Cost / (1 - Default Markup %)
    zISkipping Min Listing Price calculation due to invalid input: all_in_cost=r%   r	   r   r>   zDefault markup is z9%, which is >= 100%. This is not allowed. Capping at 99%.c      r&   r   z,Cannot calculate min list price with markup z%, as denominator is z,Min Listing Price Calculation: All-in Cost (r'   z) / (1 - Markup z%) = Min Price ())	r
   r   r   r!   r)   r*   warningr   r   )r:   r   r   markup_percentdenominator	min_pricer   r   r   calculate_min_listing_pricek   s.   
rI   )__doc__r
   r   ospathjoindirnameabspath__file__r   r   r!   r;   rA   rI   r   r   r   r   <module>   s     6