Completed
Push — master ( 78fac9...861463 )
by Laurent
01:54
created

facture.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
define("EXPENSE_REPORT_GENERATOR_ACTION_GENERATE", "generate");
6
7
/**
8
 * When a user has to select year and quartil
9
 */
10
define("EXPENSE_REPORT_GENERATOR_ACTION_CREATE", "select");
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('/compta/facture/class/facture.class.php');
25
dol_include_once('/adherents/class/adherent.class.php');
26
dol_include_once("/flightlog/lib/flightLog.lib.php");
27
dol_include_once("/flightlog/class/bbctypes.class.php");
28
dol_include_once("/flightlog/class/bbcvols.class.php");
29
dol_include_once("/product/class/product.class.php");
30
dol_include_once('/core/modules/facture/modules_facture.php');
31
dol_include_once('/fourn/class/fournisseur.class.php');
32
33
global $db, $langs, $user, $conf;
34
35
// Load translation files required by the page
36
$langs->load("trips");
37
$langs->load("bills");
38
$langs->load("mymodule@flightlog");
39
$langs->load("other");
40
41
// Get parameters
42
$id = GETPOST('id', 'int', 3);
43
$action = GETPOST('action', 'alpha');
44
$year = GETPOST('year', 'int', 3);
45
46
//post parameters
47
$additionalBonus = GETPOST('additional_bonus', 'array', 2);
48
$pilotIds = GETPOST('pilot', 'array', 2);
49
$amouts = GETPOST('amout', 'array', 2);
50
$amoutDiscounts = GETPOST('amoutDiscount', 'array', 2);
51
$publicNote = GETPOST('public_note', 'alpha', 2);
52
$privateNote = GETPOST('private_note', 'alpha', 2);
53
$type = GETPOST("type", "int", 3);
54
$conditionReglement = GETPOST("cond_reglement_id", "int", 3);
55
$modeReglement = GETPOST("mode_reglement_id", "int", 3);
56
$bankAccount = GETPOST("fk_account", "int", 3);
57
$documentModel = GETPOST("model", "alpha", 3);
58
59
//variables
60
$flightProduct = new Product($db);
61
$flightProduct->fetch($conf->global->BBC_FLIGHT_TYPE_CUSTOMER);
62
63
$flight = new Bbcvols($db);
64
$flight->fetch($id);
65
$puFlight = $flight->cost / $flight->nbrPax;
66
67
$organisator = new User($db);
68
$organisator->fetch($flight->fk_organisateur);
69
70
$receiver = new User($db);
71
$receiver->fetch($flight->fk_receiver);
72
73
$pilot = new User($db);
74
$pilot->fetch($flight->fk_pilot);
75
76
$adherent = new Adherent($db);
77
$adherent->fetch($pilot->fk_member);
78
79
$customer = new Fournisseur($db);
80
$customer->fetch($conf->global->BBC_FLIGHT_DEFAULT_CUSTOMER ?: $adherent->fk_soc);
81
82
//Query
83
84
//pdf
85
$hidedetails = (GETPOST('hidedetails', 'int') ? GETPOST('hidedetails',
86
    'int') : (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0));
87
$hidedesc = (GETPOST('hidedesc', 'int') ? GETPOST('hidedesc',
88
    'int') : (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0));
89
$hideref = (GETPOST('hideref', 'int') ? GETPOST('hideref',
90
    'int') : (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0));
91
92
$object = new Facture($db);
93
$vatrate = "0.000";
94
95
// Access control
96 View Code Duplication
if (!$conf->facture->enabled || !$user->rights->flightlog->vol->financial || !$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...
97
    accessforbidden();
98
}
99
100
// Default action
101
if (empty($action)) {
102
    $action = EXPENSE_REPORT_GENERATOR_ACTION_CREATE;
103
}
104
105
/*
106
 * ACTIONS
107
 *
108
 * Put here all code to do according to value of "action" parameter
109
 */
110
111
if ($action == EXPENSE_REPORT_GENERATOR_ACTION_GENERATE) {
112
    if (empty($documentModel) || $conditionReglement == 0 || empty($conditionReglement) || $modeReglement == 0 || empty($modeReglement) || !$flight) {
113
        dol_htmloutput_errors("Erreur de configuration !");
114
    } else {
115
        $object = new Facture($db);
116
        $object->fetch_thirdparty();
117
118
        $object->socid = $customer->id;
119
        $object->type = $type;
120
        $object->number = "provisoire";
121
        $object->date = $flight->date;
122
        $object->date_pointoftax = "";
123
        $object->note_public = $publicNote;
124
        $object->note_private = $privateNote;
125
        $object->ref_client = "";
126
        $object->ref_int = "";
127
        $object->modelpdf = $documentModel;
128
        $object->cond_reglement_id = $conditionReglement;
129
        $object->mode_reglement_id = $modeReglement;
130
        $object->fk_account = $bankAccount;
131
132
        $id = $object->create($user);
133
134
        if ($id <= 0) {
135
            setEventMessages($object->error, $object->errors, 'errors');
136
        }
137
138
        $localtax1_tx = get_localtax(0, 1, $object->thirdparty);
139
        $localtax2_tx = get_localtax(0, 2, $object->thirdparty);
140
141
        $pu_ht = price2num($flightProduct->price, 'MU');
142
        $pu_ttc = price2num($flightProduct->price_ttc, 'MU');
143
        $pu_ht_devise = price2num($flightProduct->price, 'MU');
144
145
146
        $discount = ($flightProduct->price_ttc - $puFlight) * 100 / $flightProduct->price_ttc;
147
148
        $result = $object->addline(
149
            $flightProduct->description,
150
            $pu_ht,
151
            $flight->nbrPax,
152
            0,
153
            $localtax1_tx,
154
            $localtax2_tx,
155
            $t3->service->id,
156
            $discount,
157
            $flight->date,
158
            $flight->date,
159
            0,
160
            0,
161
            '',
162
            'TTC',
163
            $pu_ttc,
164
            1,
165
            -1,
166
            0,
167
            '',
168
            0,
169
            0,
170
            '',
171
            '',
172
            $flightProduct->label,
173
            [],
174
            100,
175
            '',
176
            0,
177
            0
178
        );
179
180
        $object->add_object_linked('flightlog_bbcvols', $flight->getId());
181
182
        $ret = $object->fetch($id);
183
        $result = $object->generateDocument($documentModel, $langs, $hidedetails, $hidedesc, $hideref);
184
185
        // Validate
186
        $object->fetch($id);
187
        $object->validate($user);
188
189
        // Generate document
190
        $object->fetch($id);
191
        $result = $object->generateDocument($documentModel, $langs, $hidedetails, $hidedesc, $hideref);
192
193
        if ($result > 0) {
194
            dol_htmloutput_mesg("Facture créées");
195
196
            $flight->is_facture = true;
197
            $flight->update($user);
198
199
            Header("Location: card.php?id=" . $flight->getId());
200
            return;
201
        } else {
202
            dol_htmloutput_errors("Facture non créée");
203
        }
204
    }
205
}
206
207
/*
208
 * VIEW
209
 *
210
 * Put here all code to build page
211
 */
212
llxHeader('', $langs->trans('Generate billing'), '');
213
print load_fiche_titre("Créer facture");
214
215
216
$form = new Form($db);
217
218
if (!$flightProduct) {
219
    dol_htmloutput_mesg("Le produit -vol- n'est pas configuré", '', 'warning');
220
}
221
222
if ($puFlight > $flightProduct->total_ttc) {
223
    dol_htmloutput_mesg("Le prix unitaire encodé pour ce vol est suppérieur au prix unitaire du produit", '',
224
        'warning');
225
}
226
227
if ($pilot->id != $receiver->id || $pilot->id != $organisator->id) {
228
    dol_htmloutput_mesg("L'organisateur / la personne ayant reçu l'argent n'est pas le pilote.", '',
229
        'warning');
230
}
231
232
?>
233
234
    <table class="border centpercent">
235
236
        <tr>
237
            <td class="fieldrequired"><?php echo $langs->trans("FieldidBBC_vols") ?> </td>
238
            <td> <?php echo $flight->idBBC_vols ?> </td>
239
        </tr>
240
        <tr>
241
            <td class="fieldrequired"><?php echo $langs->trans("Fielddate") ?> </td>
242
            <td> <?php echo dol_print_date($flight->date) ?> </td>
243
        </tr>
244
245
        <tr>
246
            <td class="fieldrequired"><?php echo $langs->trans("Fieldfk_pilot") ?> </td>
247
            <td> <?php echo $pilot->getNomUrl() ?> </td>
248
        </tr>
249
        <tr>
250
            <td class="fieldrequired"><?php echo $langs->trans("Fieldfk_organisateur") ?> </td>
251
            <td> <?php echo $organisator->getNomUrl() ?> </td>
252
        </tr>
253
        <tr>
254
            <td class="fieldrequired"><?php echo $langs->trans("Fieldfk_receiver") ?> </td>
255
            <td> <?php echo $receiver->getNomUrl() ?> </td>
256
        </tr>
257
258
        <tr>
259
            <td class="fieldrequired"><?php echo $langs->trans("FieldnbrPax") ?> </td>
260
            <td> <?php echo $flight->nbrPax ?> </td>
261
        </tr>
262
263
        <tr>
264
            <td class="fieldrequired"><?php echo $langs->trans("Fieldis_facture") ?> </td>
265
            <td> <?php echo $flight->getLibStatut(5) ?> </td>
266
        </tr>
267
268
        <tr>
269
            <td class="fieldrequired">Prix standard</td>
270
            <td> <?php echo $flightProduct->total_ttc . " " . $langs->getCurrencySymbol($conf->currency) ?> </td>
271
        </tr>
272
        <tr>
273
            <td class="fieldrequired"><?php echo $langs->trans("Fieldcost") ?> </td>
274
            <td> <?php echo $flight->cost . " " . $langs->getCurrencySymbol($conf->currency) ?> </td>
275
        </tr>
276
        <tr>
277
            <td class="fieldrequired"><?php echo $langs->trans("UnitPrice") ?> </td>
278
            <td> <?php echo $puFlight . " " . $langs->getCurrencySymbol($conf->currency) ?> </td>
279
        </tr>
280
    </table>
281
282
    <br/>
283
    <br/>
284
285
    <form method="POST">
286
287
        <!-- action -->
288
        <input type="hidden" name="action" value="<?= EXPENSE_REPORT_GENERATOR_ACTION_GENERATE ?>">
289
        <input type="hidden" name="id" value="<?= $id ?>">
290
291
        <!-- Billing type -->
292
        <label><?= $langs->trans("Type de facture"); ?></label><br/>
293
        <input type="radio" id="radio_standard" name="type" value="0" checked="checked"/>
294
        <?= $form->textwithpicto($langs->trans("InvoiceStandardAsk"), $langs->transnoentities("InvoiceStandardDesc"), 1,
295
            'help', '', 0, 3) ?>
296
        <br/>
297
        <br/>
298
299
        <!-- Payment mode -->
300
        <label><?= $langs->trans("Mode de payement"); ?></label><br/>
301
        <?php $form->select_types_paiements($customer->mode_reglement_id, 'mode_reglement_id', 'CRDT'); ?>
302
        <br/>
303
        <br/>
304
305
        <!-- Payment condition -->
306
        <label><?= $langs->trans("Condition de payement"); ?></label><br/>
307
        <?php $form->select_conditions_paiements($customer->cond_reglement_id, 'cond_reglement_id'); ?>
308
        <br/>
309
        <br/>
310
311
        <!-- bank account -->
312
        <label><?= $langs->trans("Compte en banque"); ?></label><br/>
313
        <?php $form->select_comptes($customer->fk_account, 'fk_account', 0, '', 1); ?>
314
        <br/>
315
        <br/>
316
317
        <!-- Public note -->
318
        <label><?= $langs->trans("Note publique"); ?></label><br/>
319
        <textarea name="public_note" wrap="soft" class="quatrevingtpercent" rows="2">
320
        Vol (identifiant : <?php echo $flight->getId(); ?>) de <?php echo $flight->lieuD; ?>
321
            à <?php echo $flight->lieuA; ?> avec <?php echo $pilot->getFullName($langs); ?>
322
        </textarea>
323
        <br/>
324
        <br/>
325
326
        <!-- Private note -->
327
        <label><?= $langs->trans("Note privée"); ?></label><br/>
328
        <textarea name="private_note" wrap="soft" class="quatrevingtpercent" rows="2">
329
        </textarea>
330
        <br/>
331
332
        <!-- model document -->
333
        <label><?= $langs->trans("Model de document "); ?></label><br/>
334
        <?php $liste = ModelePDFFactures::liste_modeles($db); ?>
335
        <?= $form->selectarray('model', $liste, $conf->global->FACTURE_ADDON_PDF); ?>
336
        <br/>
337
        <br/>
338
339
        <?php if (!$flightProduct) : ?>
340
            <a class="butActionRefused" href="#">Générer</a>
341
        <?php else: ?>
342
            <button class="butAction" type="submit">Générer</button>
343
        <?php endif; ?>
344
345
        <a class="butAction" href="<?php echo DOL_URL_ROOT . '/flightlog/card.php?id=' . $flight->id; ?>">Retour au
346
            vol</a>
347
348
    </form>
349
350
<?php
351
llxFooter();