def test_cost_summary_calculator(self):
        ExpenseFactory(travel=self.travel,
                       currency=self.currency_huf,
                       amount=None)
        ExpenseFactory(travel=self.travel, currency=None, amount=600)
        ExpenseFactory(travel=self.travel,
                       currency=self.currency_usd,
                       amount=50)

        calculator = CostSummaryCalculator(self.travel)
        # Should not raise TypeError
        calculator.get_cost_summary()
 def test_get_expenses_amount_none(self):
     """Ignore expenses where amount is None"""
     ExpenseFactory(travel=self.travel,
                    type=self.user_et_1,
                    currency=self.currency_usd,
                    amount=None)
     calc = CostSummaryCalculator(self.travel)
     self.assertEqual(calc.get_expenses(), {})
 def test_get_expenses(self):
     """Check that dictionary with expense type vendor number as key is
     returned
     """
     expense_1 = ExpenseFactory(travel=self.travel,
                                type=self.user_et_1,
                                currency=self.currency_usd,
                                amount=100)
     expense_2 = ExpenseFactory(travel=self.travel,
                                type=self.user_et_1,
                                currency=self.currency_usd,
                                amount=150)
     expense_3 = ExpenseFactory(travel=self.travel,
                                currency=self.currency_usd,
                                amount=200)
     calc = CostSummaryCalculator(self.travel)
     self.assertEqual(
         calc.get_expenses(), {
             self.user_et_1.vendor_number: [expense_1, expense_2],
             expense_3.type.vendor_number: [expense_3],
         })
 def test_init(self):
     currency_usd = PublicsCurrencyFactory(code='USD')
     travel = TravelFactory(currency=currency_usd)
     expense_type = PublicsTravelExpenseTypeFactory(
         title='Train cost',
         vendor_number=TravelExpenseType.USER_VENDOR_NUMBER_PLACEHOLDER)
     expense = ExpenseFactory(travel=travel,
                              type=expense_type,
                              currency=currency_usd,
                              amount=100)
     dto = ExpenseDTO("vendor_number", expense)
     self.assertEqual(dto.vendor_number, "vendor_number")
     self.assertEqual(dto.label, expense_type.title)
     self.assertEqual(dto.currency, currency_usd)
     self.assertEqual(dto.amount, 100)
    def test_cost_calculation_expense_delta(self):
        """If preserved expense set, ensure correct delta updated"""
        self.travel.preserved_expenses_local = Decimal("150")
        self.travel.preserved_expenses_usd = Decimal("275")
        itinerary = ItineraryItemFactory(travel=self.travel,
                                         arrival_date=datetime(2017,
                                                               1,
                                                               1,
                                                               1,
                                                               0,
                                                               tzinfo=UTC),
                                         departure_date=datetime(2017,
                                                                 1,
                                                                 2,
                                                                 4,
                                                                 0,
                                                                 tzinfo=UTC),
                                         dsa_region=self.budapest)

        ItineraryItemFactory(travel=self.travel,
                             arrival_date=datetime(2017,
                                                   1,
                                                   3,
                                                   1,
                                                   0,
                                                   tzinfo=UTC),
                             departure_date=datetime(2017,
                                                     1,
                                                     4,
                                                     4,
                                                     0,
                                                     tzinfo=UTC),
                             dsa_region=self.budapest)
        expense_huf = ExpenseFactory(travel=self.travel,
                                     type=self.user_et_1,
                                     currency=self.currency_huf,
                                     amount=100)
        expense_usd = ExpenseFactory(travel=self.travel,
                                     type=self.user_et_2,
                                     currency=self.currency_usd,
                                     amount=200)

        daily_amt = self.budapest.dsa_amount_local
        last_day_amount = daily_amt * (1 - DSACalculator.LAST_DAY_DEDUCTION)
        total = daily_amt * 3 + last_day_amount
        expense_total = expense_huf.amount + expense_usd.amount

        calculator = CostSummaryCalculator(self.travel)
        cost_summary = calculator.get_cost_summary()
        self.assertEqual(cost_summary["dsa"],
                         [{
                             "start_date": date(2017, 1, 1),
                             "end_date": date(2017, 1, 4),
                             "dsa_region": itinerary.dsa_region.pk,
                             "dsa_region_name": itinerary.dsa_region.label,
                             "night_count": 3,
                             "daily_rate": daily_amt,
                             "paid_to_traveler": total,
                             "total_amount": total,
                             "deduction": Decimal("0.0000"),
                         }])
        self.assertCountEqual(cost_summary["expenses_total"], [
            {
                "currency": self.currency_huf,
                "amount": expense_huf.amount
            },
            {
                "currency": self.currency_usd,
                "amount": expense_usd.amount
            },
        ])
        self.assertEqual(cost_summary["preserved_expenses"], Decimal("150"))
        self.assertEqual(cost_summary["expenses_delta_local"], Decimal("50"))
        self.assertEqual(cost_summary["expenses_delta_usd"], Decimal("275"))
        self.assertEqual(cost_summary["dsa_total"], total)
        self.assertEqual(cost_summary["paid_to_traveler"],
                         total + expense_total)
        self.assertEqual(cost_summary["traveler_dsa"], total)
    def test_cost_calculation_parking_money(self):
        """If expense mapping has empty key, allocate to parking money"""
        itinerary = ItineraryItemFactory(travel=self.travel,
                                         arrival_date=datetime(2017,
                                                               1,
                                                               1,
                                                               1,
                                                               0,
                                                               tzinfo=UTC),
                                         departure_date=datetime(2017,
                                                                 1,
                                                                 2,
                                                                 4,
                                                                 0,
                                                                 tzinfo=UTC),
                                         dsa_region=self.budapest)

        ItineraryItemFactory(travel=self.travel,
                             arrival_date=datetime(2017,
                                                   1,
                                                   3,
                                                   1,
                                                   0,
                                                   tzinfo=UTC),
                             departure_date=datetime(2017,
                                                     1,
                                                     4,
                                                     4,
                                                     0,
                                                     tzinfo=UTC),
                             dsa_region=self.budapest)
        parking_money_type = PublicsTravelExpenseTypeFactory(
            title='Parking money',
            vendor_number="",
        )
        ExpenseFactory(travel=self.travel,
                       type=parking_money_type,
                       currency=self.currency_usd,
                       amount=100)

        daily_amt = self.budapest.dsa_amount_local
        last_day_amount = daily_amt * (1 - DSACalculator.LAST_DAY_DEDUCTION)
        total = daily_amt * 3 + last_day_amount

        calculator = CostSummaryCalculator(self.travel)
        cost_summary = calculator.get_cost_summary()
        self.assertEqual(cost_summary["dsa"],
                         [{
                             "start_date": date(2017, 1, 1),
                             "end_date": date(2017, 1, 4),
                             "dsa_region": itinerary.dsa_region.pk,
                             "dsa_region_name": itinerary.dsa_region.label,
                             "night_count": 3,
                             "daily_rate": daily_amt,
                             "paid_to_traveler": total,
                             "total_amount": total,
                             "deduction": Decimal("0.0000"),
                         }])
        self.assertEqual(cost_summary["expenses_total"],
                         [{
                             "currency": self.currency_usd,
                             "amount": Decimal("100.0")
                         }])
        self.assertEqual(cost_summary["preserved_expenses"], Decimal("150"))
        self.assertEqual(cost_summary["dsa_total"], total)
        self.assertEqual(cost_summary["paid_to_traveler"], total)
        self.assertEqual(cost_summary["traveler_dsa"], total)
    def test_calculations(self):
        ItineraryItemFactory(travel=self.travel,
                             departure_date=datetime(2017,
                                                     1,
                                                     1,
                                                     1,
                                                     0,
                                                     tzinfo=UTC),
                             arrival_date=datetime(2017,
                                                   1,
                                                   1,
                                                   2,
                                                   0,
                                                   tzinfo=UTC),
                             dsa_region=self.budapest)

        ItineraryItemFactory(travel=self.travel,
                             departure_date=datetime(2017,
                                                     1,
                                                     1,
                                                     10,
                                                     0,
                                                     tzinfo=UTC),
                             arrival_date=datetime(2017,
                                                   1,
                                                   1,
                                                   11,
                                                   0,
                                                   tzinfo=UTC),
                             dsa_region=self.copenhagen)

        ItineraryItemFactory(travel=self.travel,
                             departure_date=datetime(2017,
                                                     1,
                                                     1,
                                                     22,
                                                     0,
                                                     tzinfo=UTC),
                             arrival_date=datetime(2017,
                                                   1,
                                                   1,
                                                   23,
                                                   0,
                                                   tzinfo=UTC),
                             dsa_region=self.dusseldorf)

        ItineraryItemFactory(travel=self.travel,
                             departure_date=datetime(2017,
                                                     1,
                                                     3,
                                                     10,
                                                     0,
                                                     tzinfo=UTC),
                             arrival_date=datetime(2017,
                                                   1,
                                                   3,
                                                   13,
                                                   0,
                                                   tzinfo=UTC),
                             dsa_region=self.amsterdam)

        ExpenseFactory(travel=self.travel,
                       type=self.user_et_1,
                       currency=self.currency_huf,
                       amount=100)
        ExpenseFactory(travel=self.travel,
                       type=self.user_et_2,
                       currency=self.currency_huf,
                       amount=200)

        calculator = CostSummaryCalculator(self.travel)
        cost_summary = calculator.get_cost_summary()
        cost_summary.pop('expenses')

        self.assertEqual(
            cost_summary, {
                'deductions_total':
                Decimal('0.0000'),
                'dsa': [{
                    'daily_rate': Decimal('200.0000'),
                    'deduction': Decimal('0.00000'),
                    'dsa_region': self.dusseldorf.id,
                    'dsa_region_name': 'Germany - Duesseldorf',
                    'end_date': date(2017, 1, 3),
                    'night_count': 2,
                    'paid_to_traveler': Decimal('640.00000'),
                    'start_date': date(2017, 1, 1),
                    'total_amount': Decimal('640.00000')
                }],
                'dsa_total':
                Decimal('640.00000'),
                'expenses_delta':
                Decimal('0'),
                'expenses_delta_local':
                Decimal('0'),
                'expenses_delta_usd':
                Decimal('0'),
                'expenses_total': [{
                    'amount': Decimal('300.0000'),
                    'currency': self.currency_huf
                }],
                'paid_to_traveler':
                Decimal('940.0000'),
                'preserved_expenses':
                None,
                'traveler_dsa':
                Decimal('640.0000')
            })
Exemple #8
0
    def test_finance_export(self):
        currency_usd = PublicsCurrencyFactory(code="USD")
        travel = TravelFactory(traveler=self.traveler,
                               supervisor=self.unicef_staff,
                               start_date=datetime(2016, 11, 20, tzinfo=UTC),
                               end_date=datetime(2016, 12, 5, tzinfo=UTC),
                               mode_of_travel=[ModeOfTravel.PLANE, ModeOfTravel.CAR, ModeOfTravel.RAIL])
        travel.expenses.all().delete()
        ExpenseFactory(travel=travel, amount=Decimal('500'), currency=currency_usd)

        travel_2 = TravelFactory(traveler=self.traveler,
                                 supervisor=self.unicef_staff,
                                 start_date=datetime(2016, 11, 20, tzinfo=UTC),
                                 end_date=datetime(2016, 12, 5, tzinfo=UTC),
                                 mode_of_travel=None)
        travel_2.expenses.all().delete()
        ExpenseFactory(travel=travel_2, amount=Decimal('200'), currency=currency_usd)
        ExpenseFactory(travel=travel_2, amount=Decimal('100'), currency=None)

        with self.assertNumQueries(27):
            response = self.forced_auth_req('get', reverse('t2f:travels:list:finance_export'),
                                            user=self.unicef_staff)
        export_csv = csv.reader(StringIO(response.content.decode('utf-8')))
        rows = [r for r in export_csv]

        self.assertEqual(len(rows), 3)

        # check header
        self.assertEqual(rows[0],
                         ['reference_number',
                          'traveler',
                          'office',
                          'section',
                          'status',
                          'supervisor',
                          'start_date',
                          'end_date',
                          'purpose_of_travel',
                          'mode_of_travel',
                          'international_travel',
                          'require_ta',
                          'dsa_total',
                          'expense_total',
                          'deductions_total'])

        self.assertEqual(rows[1],
                         ['{}/1'.format(datetime.now().year),
                          'John Doe',
                          'An Office',
                          travel.sector.name,
                          'planned',
                          'Jakab Gipsz',
                          '20-Nov-2016',
                          '05-Dec-2016',
                          travel.purpose,
                          'Plane, Car, Rail',
                          'No',
                          'Yes',
                          '0.00',
                          '500 USD',
                          '0.00'])

        self.assertEqual(rows[2],
                         ['{}/2'.format(datetime.now().year),
                          'John Doe',
                          'An Office',
                          travel_2.sector.name,
                          'planned',
                          'Jakab Gipsz',
                          '20-Nov-2016',
                          '05-Dec-2016',
                          travel_2.purpose,
                          '',
                          'No',
                          'Yes',
                          '0.00',
                          '200 USD',
                          '0.00'])