Completed
Branch feature/create_monthly_billing (30fb74)
by Laurent
02:14
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
$t = 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
93
/*
94
 * ACTIONS
95
 *
96
 * Put here all code to do according to value of "action" parameter
97
 */
98 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...
99
    try{
100
101
        $command = new CreateMonthBillCommand($type, $modeReglement, $conditionReglement, $documentModel, $publicNote, $privateNote,$year, $month);
102
        $handler->handle($command);
103
        dol_htmloutput_mesg('Génération : OK');
104
    }catch(Exception $e){
105
        dol_syslog($e->getMessage(), LOG_ERR);
106
        dol_htmloutput_mesg('Erreur pendant la génération.', '', 'error');
107
    }
108
109
}
110
111
/*
112
 * VIEW
113
 *
114
 * Put here all code to build page
115
 */
116
117
$form = new Form($db);
118
119
?>
120
    <form method="POST">
121
        <input type="hidden" name="action" value="<?php echo EXPENSE_REPORT_GENERATOR_ACTION_CHANGE_DATES ?>">
122
123
        <!-- Year -->
124
        <label><?php echo $langs->trans('Année') ?></label>
125
        <select name="year">
126
            <?php for ($selectYearOption = $currentYear; $selectYearOption >= $currentYear - 6; $selectYearOption--): ?>
127
                <option value="<?php echo $selectYearOption; ?>" <?php echo $selectYearOption == $year? 'selected' : '' ?>><?php echo $selectYearOption; ?></option>
128
            <?php endfor; ?>
129
        </select>
130
131
        <!-- Year -->
132
        <label><?php echo $langs->trans('Mois') ?></label>
133
        <select name="month">
134
            <?php for ($selectMonthOption = 1; $selectMonthOption <= 12; $selectMonthOption++): ?>
135
                <option value="<?php echo $selectMonthOption; ?>" <?php echo $selectMonthOption == $month? 'selected' : '' ?>><?php echo $selectMonthOption; ?></option>
136
            <?php endfor; ?>
137
        </select>
138
139
        <button class="butAction" type="submit"><?php echo $langs->trans('refresh'); ?></button>
140
    </form>
141
    <div>
142
        <p>
143
            <?php echo $langs->trans("Comprends les vols non facturés pour le mois demandé."); ?>
144
        </p>
145
    </div>
146
    <form action="#" method="POST">
147
148
        <!-- action -->
149
        <input type="hidden" name="action" value="<?php echo EXPENSE_REPORT_GENERATOR_ACTION_GENERATE ?>">
150
        <input type="hidden" name="month" value="<?php echo $month ?>">
151
        <input type="hidden" name="year" value="<?php echo $year ?>">
152
153
        <table class="border">
154
            <thead>
155
            <tr>
156
                <td><?php echo $langs->trans('pilote'); ?></td>
157
                <td><?php echo $langs->trans('Nombre de vols'); ?></td>
158
                <td><?php echo $langs->trans('Montant'); ?></td>
159
                <td><?php echo $langs->trans('Moyenne / passager'); ?></td>
160
            </tr>
161
            </thead>
162
163
            <tbody>
164
            <?php if ($queryResult->isEmpty()): ?>
165
                <tr>
166
                    <td colspan="4"><?php echo $langs->trans('Nous n\'avons pas trouvé de vol pour la période demandée.'); ?></td>
167
                </tr>
168
            <?php endif; ?>
169
            <?php foreach ($queryResult->getFlights() as $monthlyFlightBill): ?>
170
                <tr>
171
                    <td><?php echo $monthlyFlightBill->getReceiver(); ?></td>
172
                    <td><?php echo $monthlyFlightBill->getFlightsCount(); ?></td>
173
                    <td><?php echo price($monthlyFlightBill->getTotal(), 0, '', 1, 1); ?></td>
174
                    <td><?php echo price($monthlyFlightBill->getAverageByPax(), 0, '', 1, 1); ?></td>
175
                </tr>
176
            <?php endforeach; ?>
177
            </tbody>
178
        </table>
179
180
181
        <!-- Billing type -->
182
        <label><?= $langs->trans("Type de facture"); ?></label><br/>
183
        <input type="radio" id="radio_standard" name="type" value="0" checked="checked"/>
184
        <?= $form->textwithpicto($langs->trans("InvoiceStandardAsk"), $langs->transnoentities("InvoiceStandardDesc"), 1,
185
            'help', '', 0, 3) ?>
186
        <br/>
187
        <br/>
188
189
        <!-- Payment mode -->
190
        <label><?= $langs->trans("Mode de payement"); ?></label><br/>
191
        <?php $form->select_types_paiements(0, 'mode_reglement_id', 'CRDT'); ?>
192
        <br/>
193
        <br/>
194
195
        <!-- Payment condition -->
196
        <label><?= $langs->trans("Condition de payement"); ?></label><br/>
197
        <?php $form->select_conditions_paiements(0, 'cond_reglement_id'); ?>
198
        <br/>
199
        <br/>
200
201
        <!-- Public note -->
202
        <label><?= $langs->trans("Note publique (commune à toutes les factures)"); ?></label><br/>
203
        <textarea name="public_note" wrap="soft" class="quatrevingtpercent" rows="2">
204
        </textarea>
205
        <br/>
206
        <br/>
207
208
        <!-- Private note -->
209
        <label><?= $langs->trans("Note privée (commune à toutes les factures)"); ?></label><br/>
210
        <textarea name="private_note" wrap="soft" class="quatrevingtpercent" rows="2">
211
        </textarea>
212
        <br/>
213
214
        <!-- model document -->
215
        <label><?= $langs->trans("Model de document "); ?></label><br/>
216
        <?php $liste = ModelePDFFactures::liste_modeles($db); ?>
217
        <?= $form->selectarray('model', $liste, $conf->global->FACTURE_ADDON_PDF); ?>
218
        <br/>
219
        <br/>
220
221
        <?php if ($queryResult->isEmpty() || $year > $currentYear || ($year == $currentYear && $month >= $currentMonth)) : ?>
222
            <p class="warning">
223
                <?php echo $langs->trans('La periode demandée n\'est pas cloturée.') ?>
224
            </p>
225
            <a class="butActionRefused" href="#">Générer</a>
226
        <?php else: ?>
227
            <button class="butAction" type="submit">Générer</button>
228
        <?php endif; ?>
229
230
    </form>
231
232
<?php
233
llxFooter();