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