def prices_show(request, connection_id): connection = Connection.load(connection_id) plan = plan_from_connection(connection) outcome = calculate(plan, Discount.objects.all()) stats = dict() operators = dict() razem = 0 odl = 0 for name, oferta, typ, args, znizka, cena, km in outcome: if typ not in stats: stats[typ] = (0, 0) stats[typ] = tuple(a + b for a, b in zip(stats[typ], (cena, km))) if oferta.operator not in operators: operators[oferta.operator] = (0, 0) operators[oferta.operator] = tuple((a + b) for a, b in zip(operators[oferta.operator], (cena, km))) razem += cena odl += km return dict(out=outcome, stats=stats, razem=razem, plan=plan, date=None, operators=operators, odl=odl)
def get_price(connection, discounts, typ = "normal"): clc = calculate(plan_from_connection(connection), discounts) if typ == 'normal': return "{:.2f}".format(calc_price(clc)) elif typ == 'full': price = 0.0 discounts = set() classes = set() distance = 0.0 for name, oferta, typ, args, znizka, cena, km in clc: if znizka is not None: discounts.add(znizka) if name == 'base': distance += km classes.add(oferta.klass) price += cena description = '{}{}{}'.format('{:.2f}km, '.format(distance), ('{}, '.format(' oraz '.join('zniżka {} ({}%)'.format(x.description, x.discount) for x in discounts)) if discounts else 'bilet normalny, '), '{} klasa'.format(' oraz '.join(x.name for x in classes))) return '{:.2f}zł ({})'.format(price, description)