Completed
Branch master (547415)
by Laurent
01:53
created

generateMonthlyBilling.php (2 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
define("EXPENSE_REPORT_GENERATOR_ACTION_GENERATE", "generate");
6
7
/**
8
 * When a user changes dates (year / Month)
9
 */
10
define("EXPENSE_REPORT_GENERATOR_ACTION_CHANGE_DATES", "refresh");
11
12
/**
13
 * \file    generateExpenseNote.php
14
 * \ingroup flightlog
15
 * \brief   Generate expense notes for a quartil
16
 *
17
 */
18
19
// Load Dolibarr environment
20
if (false === (@include '../main.inc.php')) {  // From htdocs directory
21
    require '../../documents/custom/main.inc.php'; // From "custom" directory
22
}
23
24
dol_include_once('/flightlog/command/CommandHandler.php');
25
dol_include_once('/flightlog/command/CommandInterface.php');
26
dol_include_once('/flightlog/command/CreateMonthBillCommand.php');
27
dol_include_once('/flightlog/command/CreateMonthBillCommandHandler.php');
28
dol_include_once('/compta/facture/class/facture.class.php');
29
dol_include_once('/adherents/class/adherent.class.php');
30
dol_include_once("/flightlog/lib/flightLog.lib.php");
31
dol_include_once("/flightlog/class/bbctypes.class.php");
32
dol_include_once("/product/class/product.class.php");
33
dol_include_once('/core/modules/facture/modules_facture.php');
34
dol_include_once('/flightlog/query/MonthlyBillableQuery.php');
35
dol_include_once('/flightlog/query/MonthlyBillableQueryHandler.php');
36
37
global $db, $langs, $user, $conf;
38
39
//variables
40
$currentYear = date('Y');
41
$currentMonth = date('m');
42
43
// Load translation files required by the page
44
$langs->load("mymodule@mymodule");
45
$langs->load("trips");
46
$langs->load("bills");
47
48
// Get parameters
49
$id = GETPOST('id', 'int');
50
$action = GETPOST('action', 'alpha');
51
$year = GETPOST('year', 'int', 3) ?: $currentYear;
52
$month = GETPOST('month', 'int', 3) ?: $currentMonth - 1;
53
54
//post parameters
55
$publicNote = GETPOST('public_note', 'alpha', 2);
56
$privateNote = GETPOST('private_note', 'alpha', 2);
57
$type = GETPOST("type", "int", 3);
58
$conditionReglement = GETPOST("cond_reglement_id", "int", 3);
59
$modeReglement = GETPOST("mode_reglement_id", "int", 3);
60
$documentModel = GETPOST("model", "alpha", 3);
61
62
63
//Query
64
$queryHandler = new MonthlyBillableQueryHandler($db, $conf->global);
65
$query = new MonthlyBillableQuery($month, $year);
66
$queryResult = $queryHandler->__invoke($query);
67
68
$handler = new CreateMonthBillCommandHandler($db, $conf->global, $user, $langs);
69
70
//pdf
71
$hidedetails = (GETPOST('hidedetails', 'int') ? GETPOST('hidedetails',
72
    'int') : (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0));
73
$hidedesc = (GETPOST('hidedesc', 'int') ? GETPOST('hidedesc',
74
    'int') : (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0));
75
$hideref = (GETPOST('hideref', 'int') ? GETPOST('hideref',
76
    'int') : (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0));
77
78
//service
79
80
// Access control
81 View Code Duplication
if (!$conf->facture->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...
82
    accessforbidden();
83
}
84
85
// Default action
86
if (empty($action)) {
87
    $action = EXPENSE_REPORT_GENERATOR_ACTION_SELECT;
88
}
89
90
llxHeader('', $langs->trans('Generate billing'), '');
91
print load_fiche_titre("Générer factures pilote");
92
print '<div class="bbc-style">';
93
94
/*
95
 * ACTIONS
96
 *
97
 * Put here all code to do according to value of "action" parameter
98
 */
99 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...
100
    try {
101
102
        $command = new CreateMonthBillCommand($type, $modeReglement, $conditionReglement, $documentModel, $publicNote,
103
            $privateNote, $year, $month);
104
        $handler->handle($command);
105
        dol_htmloutput_mesg('Génération : OK');
106
    } catch (Exception $e) {
107
        dol_syslog($e->getMessage(), LOG_ERR);
108
        dol_htmloutput_mesg('Erreur pendant la génération.', '', 'error');
109
    }
110
111
}
112
113
/*
114
 * VIEW
115
 *
116
 * Put here all code to build page
117
 */
118
119
$form = new Form($db);
120
121
?>
122
    <section class="section">
123
        <h2 class="section-title"><?php echo $langs->trans('Période'); ?></h2>
124
125
        <form class="form-inline flight-form" method="POST">
126
            <section class="form-section">
127
                <input type="hidden" name="action" value="<?php echo EXPENSE_REPORT_GENERATOR_ACTION_CHANGE_DATES ?>">
128
129
                <!-- Year -->
130
                <div class="form-group">
131
                    <label><?php echo $langs->trans('Année') ?></label>
132
                    <select name="year">
133
                        <?php for ($selectYearOption = $currentYear; $selectYearOption >= $currentYear - 6; $selectYearOption--): ?>
134
                            <option value="<?php echo $selectYearOption; ?>" <?php echo $selectYearOption == $year ? 'selected' : '' ?>><?php echo $selectYearOption; ?></option>
135
                        <?php endfor; ?>
136
                    </select>
137
                </div>
138
139
                <!-- Year -->
140
                <div class="form-group">
141
                    <label><?php echo $langs->trans('Mois') ?></label>
142
                    <select name="month">
143
                        <?php for ($selectMonthOption = 1; $selectMonthOption <= 12; $selectMonthOption++): ?>
144
                            <option value="<?php echo $selectMonthOption; ?>" <?php echo $selectMonthOption == $month ? 'selected' : '' ?>><?php echo $selectMonthOption; ?></option>
145
                        <?php endfor; ?>
146
                    </select>
147
                </div>
148
            </section>
149
150
            <button class="butAction" type="submit"><?php echo $langs->trans('refresh'); ?></button>
151
        </form>
152
    </section>
153
154
    <section class="section">
155
        <h2 class="section-title"><?php echo $langs->trans('Generation des factures') ?></h2>
156
157
        <div>
158
            <p>
159
                <?php echo $langs->trans("Comprends les vols non facturés pour le mois demandé."); ?>
160
            </p>
161
        </div>
162
163
        <form action="#" method="POST">
164
165
            <!-- action -->
166
            <input type="hidden" name="action" value="<?php echo EXPENSE_REPORT_GENERATOR_ACTION_GENERATE ?>">
167
            <input type="hidden" name="month" value="<?php echo $month ?>">
168
            <input type="hidden" name="year" value="<?php echo $year ?>">
169
170
            <table class="border _width50">
171
                <thead>
172
                <tr>
173
                    <td><?php echo $langs->trans('pilote'); ?></td>
174
                    <td><?php echo $langs->trans('Nombre de vols'); ?></td>
175
                    <td><?php echo $langs->trans('Montant'); ?></td>
176
                    <td><?php echo $langs->trans('Moyenne / passager'); ?></td>
177
                </tr>
178
                </thead>
179
180
                <tbody>
181
                <?php if ($queryResult->isEmpty()): ?>
182
                    <tr>
183
                        <td colspan="4" class="_alignCenter _info"><?php echo $langs->trans('Nous n\'avons pas trouvé de vol pour la période demandée.'); ?></td>
184
                    </tr>
185
                <?php endif; ?>
186
                <?php foreach ($queryResult->getFlights() as $monthlyFlightBill): ?>
187
                    <tr>
188
                        <td><?php echo $monthlyFlightBill->getReceiver(); ?></td>
189
                        <td><?php echo $monthlyFlightBill->getFlightsCount(); ?></td>
190
                        <td class="_alignRight"><?php echo price($monthlyFlightBill->getTotal(), 0, '', 1, 1); ?>€</td>
191
                        <td class="_alignRight"><?php echo price($monthlyFlightBill->getAverageByPax(), 0, '', 1, 1); ?>€</td>
192
                    </tr>
193
                <?php endforeach; ?>
194
                </tbody>
195
            </table>
196
197
198
            <!-- Billing type -->
199
            <label><?= $langs->trans("Type de facture"); ?></label><br/>
200
            <input type="radio" id="radio_standard" name="type" value="0" checked="checked"/>
201
            <?= $form->textwithpicto($langs->trans("InvoiceStandardAsk"),
202
                $langs->transnoentities("InvoiceStandardDesc"), 1,
203
                'help', '', 0, 3) ?>
204
            <br/>
205
            <br/>
206
207
            <!-- Payment mode -->
208
            <label><?= $langs->trans("Mode de payement"); ?></label><br/>
209
            <?php $form->select_types_paiements(0, 'mode_reglement_id', 'CRDT'); ?>
210
            <br/>
211
            <br/>
212
213
            <!-- Payment condition -->
214
            <label><?= $langs->trans("Condition de payement"); ?></label><br/>
215
            <?php $form->select_conditions_paiements(0, 'cond_reglement_id'); ?>
216
            <br/>
217
            <br/>
218
219
            <!-- Public note -->
220
            <label><?= $langs->trans("Note publique (commune à toutes les factures)"); ?></label><br/>
221
            <textarea name="public_note" wrap="soft" class="quatrevingtpercent" rows="2"></textarea>
222
            <br/>
223
            <br/>
224
225
            <!-- Private note -->
226
            <label><?= $langs->trans("Note privée (commune à toutes les factures)"); ?></label><br/>
227
            <textarea name="private_note" wrap="soft" class="quatrevingtpercent" rows="2"></textarea>
228
            <br/>
229
230
            <!-- model document -->
231
            <label><?= $langs->trans("Model de document "); ?></label><br/>
232
            <?php $liste = ModelePDFFactures::liste_modeles($db); ?>
233
            <?= $form->selectarray('model', $liste, $conf->global->FACTURE_ADDON_PDF); ?>
234
            <br/>
235
            <br/>
236
237
            <?php if ($queryResult->isEmpty() || $year > $currentYear || ($year == $currentYear && $month >= $currentMonth)) : ?>
238
                <p class="warning">
239
                    <?php echo $langs->trans('La periode demandée n\'est pas cloturée.') ?>
240
                </p>
241
                <a class="butActionRefused" href="#">Générer</a>
242
            <?php else: ?>
243
                <button class="butAction" type="submit">Générer</button>
244
            <?php endif; ?>
245
246
        </form>
247
    </section>
248
    </div>
249
    <!-- end bbc style -->
250
<?php
251
llxFooter();