Completed
Push — feature/api ( de88d6...6cf568 )
by Laurent
02:01
created

generateExpenseNote.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * When a user generates the expense report for all pilots
4
 */
5
6
define("EXPENSE_REPORT_GENERATOR_ACTION_GENERATE", "generate");
7
8
/**
9
 * When a user has to select year and quartil
10
 */
11
define("EXPENSE_REPORT_GENERATOR_ACTION_SELECT", "select");
12
13
/**
14
 * \file    generateExpenseNote.php
15
 * \ingroup flightlog
16
 * \brief   Generate expense notes for a quartil
17
 *
18
 */
19
20
// Load Dolibarr environment
21
if (false === (@include '../main.inc.php')) {  // From htdocs directory
22
    require '../../documents/custom/main.inc.php'; // From "custom" directory
23
}
24
25
global $db, $langs, $user, $conf;
26
27
dol_include_once('/expensereport/class/expensereport.class.php');
28
dol_include_once("/flightlog/flightlog.inc.php");
29
30
use flightlog\command\CreateExpenseNoteCommand;
31
use flightlog\command\CreateExpenseNoteCommandHandler;
32
use flightlog\exceptions\PeriodNotFinishedException;
33
use flightlog\query\GetPilotsWithMissionsQuery;
34
use flightlog\query\GetPilotsWithMissionsQueryHandler;
35
36
// Load translation files required by the page
37
$langs->load("mymodule@mymodule");
38
$langs->load("trips");
39
$langs->load("bills");
40
$langs->load("mails");
41
42
// Get parameters
43
$id = GETPOST('id', 'int');
44
$action = GETPOST('action', 'alpha');
45
$year = GETPOST('year', 'int', 3);
46
$quarter = GETPOST('quarter', 'int');
47
$userValidatorId = GETPOST('fk_user_validator', 'int');
48
$userValidatorId = GETPOST('fk_user_validator', 'int');
49
$privateNote = GETPOST('private_note');
50
$publicNote = GETPOST('public_note');
51
52
$currentYear = date('Y');
53
$currentQuarter = floor((date('n') - 1) / 3) + 1;
54
55
$tauxRemb = isset($conf->global->BBC_FLIGHT_LOG_TAUX_REMB_KM) ? $conf->global->BBC_FLIGHT_LOG_TAUX_REMB_KM : 0;
56
$unitPriceMission = $conf->global->BBC_FLIGHT_LOG_UNIT_PRICE_MISSION;
57
58
$flightYears = getFlightYears();
59
$object = new ExpenseReport($db);
60
$vatrate = "0.000";
61
62
$commandHandler = new CreateExpenseNoteCommandHandler($db, $conf, $user, $langs, new \flightlog\query\GetPilotsWithMissionsQueryHandler($db), new \flightlog\query\FlightForQuarterAndPilotQueryHandler($db));
63
64
// Access control
65 View Code Duplication
if (!$conf->expensereport->enabled || !$user->rights->flightlog->vol->status || !$user->rights->flightlog->vol->financialGenerateDocuments) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    accessforbidden();
67
}
68
69
// Default action
70
if (empty($action)) {
71
    $action = EXPENSE_REPORT_GENERATOR_ACTION_SELECT;
72
}
73
74
llxHeader('', $langs->trans('Generate expense report'), '');
75
print load_fiche_titre("Générer note de frais");
76
77
/*
78
 * ACTIONS
79
 *
80
 * Put here all code to do according to value of "action" parameter
81
 */
82
83 View Code Duplication
if ($action == EXPENSE_REPORT_GENERATOR_ACTION_GENERATE) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
85
    try {
86
        $command = new CreateExpenseNoteCommand($year, $quarter, $userValidatorId, $privateNote, $publicNote);
87
        $commandHandler->__invoke($command);
88
    } catch(PeriodNotFinishedException $e){
89
        dol_htmloutput_errors("Le quadri n'est pas encore fini !");
90
    } catch(\Exception $e){
91
        dol_syslog($e->getMessage(), LOG_ERR);
92
        dol_htmloutput_errors('Error : ' . $e->getMessage());
93
    }
94
}
95
96
/*
97
 * VIEW
98
 *
99
 * Put here all code to build page
100
 */
101
102
103
$form = new Form($db);
104
105
$tabLinks = [];
106 View Code Duplication
foreach($flightYears as $currentFlightYear){
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
    $tabLinks[] = [
108
        DOL_URL_ROOT."/flightlog/generateExpenseNote.php?year=".$currentFlightYear,
109
        $currentFlightYear,
110
        "tab_".$currentFlightYear
111
    ];
112
}
113
114
dol_fiche_head($tabLinks, "tab_".$year);
115
116
?>
117
    <form method="POST">
118
119
        <!-- action -->
120
        <input type="hidden" name="action" value="<?= EXPENSE_REPORT_GENERATOR_ACTION_GENERATE ?>">
121
122
        <?php
123
            $queryHandler = new GetPilotsWithMissionsQueryHandler($db);
124
            $query = new GetPilotsWithMissionsQuery($year);
125
126
            printBbcKilometersByQuartil($queryHandler->__invoke($query), $tauxRemb, $unitPriceMission);
127
        ?>
128
129
        <!-- Quarter -->
130
        <label for="field_quarter">Q : </label>
131
        <br/>
132
        <select name="quarter" id="field_quarter">
133
            <option value="1" <?= ($year == $currentYear && $currentQuarter <= 1) ? 'disabled="disabled"' : "" ?>>1</option>
134
            <option value="2" <?= ($year == $currentYear && $currentQuarter <= 2) ? 'disabled="disabled"' : "" ?>>2</option>
135
            <option value="3" <?= ($year == $currentYear && $currentQuarter <= 3) ? 'disabled="disabled"': "" ?>>3</option>
136
            <option value="4" <?= ($year == $currentYear && $currentQuarter <= 4) ? 'disabled="disabled"' : "" ?>>4</option>
137
        </select>
138
        <br/>
139
140
        <!-- Validator -->
141
        <label><?= $langs->trans("Validateur de la note de frais")?></label><br/>
142
        <?php
143
            $include_users = $object->fetch_users_approver_expensereport();
144
            print $form->select_dolusers($user->id,"fk_user_validator",1,"",0,$include_users);
145
        ?>
146
        <br/>
147
148
        <!-- Public note -->
149
        <label><?= $langs->trans("Note publique (commune à toutes les notes de frais)"); ?></label><br/>
150
        <textarea name="public_note" wrap="soft" class="quatrevingtpercent" rows="2">
151
            Les frais pilotes regroupent tous les frais qu'à le pilote pour organiser son vol (Champagne, Téléphone, Diplômes, ...).
152
        </textarea>
153
        <br/>
154
155
        <!-- Private note -->
156
        <label><?= $langs->trans("Note privée (commune à toutes les notes de frais)"); ?></label><br/>
157
        <textarea name="private_note" wrap="soft" class="quatrevingtpercent" rows="2"></textarea>
158
        <br/>
159
160
        <button class="butAction" type="submit">Générer</button>
161
    </form>
162
163
<?php
164
llxFooter();