def _incomplete_price(self, usage_type, start, end, warehouse=None, team=None): """ Calculate if for every day in report there is price defined for usage type :param list usage_type: usage type :param datetime start: Start date of the report :param datetime end: End date of the report :returns str: 'No price' or 'Incomplete price' or None (if all is ok) """ total_days = (end - start).days + 1 # total report days ut_days = 0 usage_prices = usage_type.usageprice_set.filter( end__gte=start, start__lte=end, ) if usage_type.by_warehouse and warehouse: usage_prices = usage_prices.filter(warehouse=warehouse) if usage_type.by_team and team: usage_prices = usage_prices.filter(team=team) usage_prices = usage_prices.values('start', 'end').distinct() intervals = [(v['start'], v['end']) for v in usage_prices] sum_of_intervals = utils.sum_of_intervals(intervals) ut_days = sum( map(lambda k: (min(end, k[1]) - max(start, k[0])).days + 1, sum_of_intervals)) if ut_days == 0: return _('No price') if ut_days != total_days: return _('Incomplete price')
def test_sum_of_dates_intervals(self): intervals = [ (date(2013, 10, 10), date(2013, 10, 15)), (date(2013, 10, 10), date(2013, 10, 15)), (date(2013, 10, 12), date(2013, 10, 14)), (date(2013, 10, 14), date(2013, 10, 19)), (date(2013, 10, 21), date(2013, 10, 28)), ] result = utils.sum_of_intervals(intervals) self.assertEquals( [ (date(2013, 10, 10), date(2013, 10, 19)), (date(2013, 10, 21), date(2013, 10, 28)), ], result )
def _incomplete_price( self, usage_type, start, end, warehouse=None, team=None ): """ Calculate if for every day in report there is price defined for usage type :param list usage_type: usage type :param datetime start: Start date of the report :param datetime end: End date of the report :returns str: 'No price' or 'Incomplete price' or None (if all is ok) """ total_days = (end - start).days + 1 # total report days ut_days = 0 usage_prices = usage_type.usageprice_set.filter( end__gte=start, start__lte=end, ) if usage_type.by_warehouse and warehouse: usage_prices = usage_prices.filter(warehouse=warehouse) if usage_type.by_team and team: usage_prices = usage_prices.filter(team=team) usage_prices = usage_prices.values('start', 'end').distinct() intervals = [(v['start'], v['end']) for v in usage_prices] sum_of_intervals = utils.sum_of_intervals(intervals) ut_days = sum(map( lambda k: (min(end, k[1]) - max(start, k[0])).days + 1, sum_of_intervals )) if ut_days == 0: return _('No price') if ut_days != total_days: return _('Incomplete price')
def test_sum_of_intervals(self): intervals = [(1, 5), (4, 10), (7, 13), (15, 20), (21, 30)] result = utils.sum_of_intervals(intervals) self.assertEquals([(1, 13), (15, 20), (21, 30)], result)