"""Synthetic teaching arithmetic, INR amounts; no investment recommendations."""
import json,math
from fractions import Fraction

def nonnegative(v,name):
 if type(v) is not int or v<0:raise ValueError(name+' requires nonnegative integer INR')
 return v

def break_even(fixed,price,variable,units):
 for name,value in [('fixed',fixed),('price',price),('variable',variable),('units',units)]:nonnegative(value,name)
 contribution=price-variable
 if contribution<=0:raise ValueError('positive contribution required')
 exact=Fraction(fixed,contribution);whole=math.ceil(exact)
 return {'contribution_inr':contribution,'break_even_exact':str(exact),'whole_units_required':whole,'profit_inr':units*contribution-fixed,'margin_of_safety_units':units-whole}

def ratios(current_assets,inventory,prepaid,current_liabilities,revenue,profit):
 for name,value in [('assets',current_assets),('inventory',inventory),('prepaid',prepaid),('liabilities',current_liabilities),('revenue',revenue)]:nonnegative(value,name)
 if type(profit) is not int:raise ValueError('profit must be integer INR; may be negative')
 if inventory+prepaid>current_assets:raise ValueError('excluded assets exceed current assets')
 return {'current_ratio':str(Fraction(current_assets,current_liabilities)) if current_liabilities else None,'quick_ratio':str(Fraction(current_assets-inventory-prepaid,current_liabilities)) if current_liabilities else None,'profit_margin':str(Fraction(profit,revenue)) if revenue else None}

def dashboard(periods):
 seen=set();totals={'revenue':0,'cost':0,'orders':0,'on_time':0}
 for row in periods:
  if set(row)!={'period','revenue','cost','orders','on_time'}:raise ValueError('exact dashboard columns required')
  if not isinstance(row['period'],str) or not row['period'].strip() or row['period'] in seen:raise ValueError('unique nonblank periods required')
  seen.add(row['period'])
  for k in totals:nonnegative(row[k],k);totals[k]+=row[k]
  if row['on_time']>row['orders']:raise ValueError('on-time count exceeds eligible orders')
 return dict(totals,profit=totals['revenue']-totals['cost'],on_time_rate=str(Fraction(totals['on_time'],totals['orders'])) if totals['orders'] else None)

def main():
 data={'break_even':break_even(12000,500,300,80),'ratios':ratios(120000,40000,10000,60000,200000,20000),'dashboard':dashboard([{'period':'A','revenue':100000,'cost':80000,'orders':10,'on_time':9},{'period':'B','revenue':50000,'cost':45000,'orders':90,'on_time':45}])}
 print(json.dumps(data,indent=2))
if __name__=='__main__':main()
