Completed
Push — master ( 5594ea...e43a77 )
by Laurent
03:34 queued 01:39
created

generateExpenseNote.php (1 issue)

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
use flightlog\command\CreateExpenseNoteCommand;
7
use flightlog\command\CreateExpenseNoteCommandHandler;
8
use flightlog\exceptions\PeriodNotFinishedException;
9
use flightlog\query\GetPilotsWithMissionsQuery;
10
use flightlog\query\GetPilotsWithMissionsQueryHandler;
11
12
define("EXPENSE_REPORT_GENERATOR_ACTION_GENERATE", "generate");
13
14
/**
15
 * When a user has to select year and quartil
16
 */
17
define("EXPENSE_REPORT_GENERATOR_ACTION_SELECT", "select");
18
19
/**
20
 * \file    generateExpenseNote.php
21
 * \ingroup flightlog
22
 * \brief   Generate expense notes for a quartil
23
 *
24
 */
25
26
// Load Dolibarr environment
27
if (false === (@include '../main.inc.php')) {  // From htdocs directory
28
    require '../../documents/custom/main.inc.php'; // From "custom" directory
29
}
30
31
dol_include_once('/expensereport/class/expensereport.class.php');
32
dol_include_once("/flightlog/lib/flightLog.lib.php");
33
dol_include_once("/flightlog/flightLog.inc.php");
34
35
global $db, $langs, $user, $conf;
36
37
// Load translation files required by the page
38
$langs->load("mymodule@mymodule");
39
$langs->load("trips");
40
$langs->load("bills");
41
$langs->load("mails");
42
43
// Get parameters
44
$id = GETPOST('id', 'int');
45
$action = GETPOST('action', 'alpha');
46
$year = GETPOST('year', 'int', 3);
47
$quarter = GETPOST('quarter', 'int');
48
$userValidatorId = GETPOST('fk_user_validator', 'int');
49
$userValidatorId = GETPOST('fk_user_validator', 'int');
50
$privateNote = GETPOST('private_note');
51
$publicNote = GETPOST('public_note');
52
53
$currentYear = date('Y');
54
$currentQuarter = floor((date('n') - 1) / 3) + 1;
55
56
$tauxRemb = isset($conf->global->BBC_FLIGHT_LOG_TAUX_REMB_KM) ? $conf->global->BBC_FLIGHT_LOG_TAUX_REMB_KM : 0;
57
$unitPriceMission = $conf->global->BBC_FLIGHT_LOG_UNIT_PRICE_MISSION;
58
59
$flightYears = getFlightYears();
60
61
$object = new ExpenseReport($db);
62
$vatrate = "0.000";
63
64
$commandHandler = new CreateExpenseNoteCommandHandler($db, $conf, $user, $langs, new \flightlog\query\GetPilotsWithMissionsQueryHandler($db), new \flightlog\query\FlightForQuarterAndPilotQueryHandler($db));
65
66
// Access control
67 View Code Duplication
if (!$conf->expensereport->enabled || !$user->rights->flightlog->vol->status || !$user->rights->flightlog->vol->financialGenerateDocuments) {
68
    accessforbidden();
69
}
70
71
// Default action
72
if (empty($action)) {
73
    $action = EXPENSE_REPORT_GENERATOR_ACTION_SELECT;
74
}
75
76
llxHeader('', $langs->trans('Generate expense report'), '');
77
print load_fiche_titre("Générer note de frais");
78
79
/*
80
 * ACTIONS
81
 *
82
 * Put here all code to do according to value of "action" parameter
83
 */
84
85 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...
86
87
    try {
88
        $command = new CreateExpenseNoteCommand($year, $quarter, $userValidatorId, $privateNote, $publicNote);
89
        $commandHandler->__invoke($command);
90
    } catch(PeriodNotFinishedException $e){
91
        dol_htmloutput_errors("Le quadri n'est pas encore fini !");
92
    } catch(\Exception $e){
93
        dol_syslog($e->getMessage(), LOG_ERR);
94
        dol_htmloutput_errors('Error : ' . $e->getMessage());
95
    }
96
}
97
98
/*
99
 * VIEW
100
 *
101
 * Put here all code to build page
102
 */
103
104
105
$form = new Form($db);
106
107
$tabLinks = [];
108 View Code Duplication
foreach($flightYears as $currentFlightYear){
109
    $tabLinks[] = [
110
        DOL_URL_ROOT."/flightlog/generateExpenseNote.php?year=".$currentFlightYear,
111
        $currentFlightYear,
112
        "tab_".$currentFlightYear
113
    ];
114
}
115
116
dol_fiche_head($tabLinks, "tab_".$year);
117
118
?>
119
    <form method="POST">
120
121
        <!-- action -->
122
        <input type="hidden" name="action" value="<?= EXPENSE_REPORT_GENERATOR_ACTION_GENERATE ?>">
123
124
        <?php
125
            $queryHandler = new GetPilotsWithMissionsQueryHandler($db);
126
            $query = new GetPilotsWithMissionsQuery($year);
127
128
            printBbcKilometersByQuartil($queryHandler->__invoke($query), $tauxRemb, $unitPriceMission);
129
        ?>
130
131
        <!-- Quarter -->
132
        <label for="field_quarter">Q : </label>
133
        <br/>
134
        <select name="quarter" id="field_quarter">
135
            <option value="1" <?= ($year == $currentYear && $currentQuarter <= 1) ? 'disabled="disabled"' : "" ?>>1</option>
136
            <option value="2" <?= ($year == $currentYear && $currentQuarter <= 2) ? 'disabled="disabled"' : "" ?>>2</option>
137
            <option value="3" <?= ($year == $currentYear && $currentQuarter <= 3) ? 'disabled="disabled"': "" ?>>3</option>
138
            <option value="4" <?= ($year == $currentYear && $currentQuarter <= 4) ? 'disabled="disabled"' : "" ?>>4</option>
139
        </select>
140
        <br/>
141
142
        <!-- Validator -->
143
        <label><?= $langs->trans("Validateur de la note de frais")?></label><br/>
144
        <?php
145
            $include_users = $object->fetch_users_approver_expensereport();
146
            print $form->select_dolusers($user->id,"fk_user_validator",1,"",0,$include_users);
147
        ?>
148
        <br/>
149
150
        <!-- Public note -->
151
        <label><?= $langs->trans("Note publique (commune à toutes les notes de frais)"); ?></label><br/>
152
        <textarea name="public_note" wrap="soft" class="quatrevingtpercent" rows="2">
153
            Les frais pilotes regroupent tous les frais qu'à le pilote pour organiser son vol (Champagne, Téléphone, Diplômes, ...).
154
        </textarea>
155
        <br/>
156
157
        <!-- Private note -->
158
        <label><?= $langs->trans("Note privée (commune à toutes les notes de frais)"); ?></label><br/>
159
        <textarea name="private_note" wrap="soft" class="quatrevingtpercent" rows="2"></textarea>
160
        <br/>
161
162
        <button class="butAction" type="submit">Générer</button>
163
    </form>
164
165
<?php
166
llxFooter();