Completed
Push — master ( 7cff09...c9a8e2 )
by Laurent
01:30
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
        $object->add_contact($flight->fk_pilot, 'BBC_PILOT', 'internal');
187
        $object->add_contact($flight->fk_receiver, 'BBC_RECEIVER', 'internal');
188
        $object->add_contact($flight->fk_organisateur, 'BBC_ORGANISATOR', 'internal');
189
190
        $ret = $object->fetch($id);
191
        $result = $object->generateDocument($documentModel, $langs, $hidedetails, $hidedesc, $hideref);
192
193
        // Validate
194
        $object->fetch($id);
195
        $object->validate($user);
196
197
        // Generate document
198
        $object->fetch($id);
199
        $result = $object->generateDocument($documentModel, $langs, $hidedetails, $hidedesc, $hideref);
200
201
        if ($result > 0) {
202
            $flight->is_facture = true;
203
            $flight->update($user);
204
205
            Header("Location: card.php?id=" . $flight->getId());
206
        } else {
207
            dol_htmloutput_errors("Facture non créée");
208
        }
209
    }
210
}
211
212
/*
213
 * VIEW
214
 *
215
 * Put here all code to build page
216
 */
217
llxHeader('', $langs->trans('Generate billing'), '');
218
print load_fiche_titre("Créer facture");
219
220
221
$form = new Form($db);
222
223
if (!$flightProduct) {
224
    dol_htmloutput_mesg("Le produit -vol- n'est pas configuré", '', 'warning');
225
}
226
227
if ($puFlight > $flightProduct->price_ttc) {
228
    dol_htmloutput_mesg("Le prix unitaire encodé pour ce vol est suppérieur au prix unitaire du produit", '',
229
        'warning');
230
}
231
232
if ($pilot->id != $receiver->id || $pilot->id != $organisator->id) {
233
    dol_htmloutput_mesg("L'organisateur / la personne ayant reçu l'argent n'est pas le pilote.", '',
234
        'warning');
235
}
236
237
?>
238
239
    <table class="border centpercent">
240
241
        <tr>
242
            <td class="fieldrequired"><?php echo $langs->trans("FieldidBBC_vols") ?> </td>
243
            <td> <?php echo $flight->idBBC_vols ?> </td>
244
        </tr>
245
        <tr>
246
            <td class="fieldrequired"><?php echo $langs->trans("Fielddate") ?> </td>
247
            <td> <?php echo dol_print_date($flight->date) ?> </td>
248
        </tr>
249
        <tr>
250
            <td class="fieldrequired"><?php echo $langs->trans("FieldBBC_ballons_idBBC_ballons") ?> </td>
251
            <td> <?php echo $balloon->immat ?> </td>
252
        </tr>
253
254
        <tr>
255
            <td class="fieldrequired"><?php echo $langs->trans("Fieldfk_pilot") ?> </td>
256
            <td> <?php echo $pilot->getNomUrl() ?> </td>
257
        </tr>
258
        <tr>
259
            <td class="fieldrequired"><?php echo $langs->trans("Fieldfk_organisateur") ?> </td>
260
            <td> <?php echo $organisator->getNomUrl() ?> </td>
261
        </tr>
262
        <tr>
263
            <td class="fieldrequired"><?php echo $langs->trans("Fieldfk_receiver") ?> </td>
264
            <td> <?php echo $receiver->getNomUrl() ?> </td>
265
        </tr>
266
267
        <tr>
268
            <td class="fieldrequired"><?php echo $langs->trans("FieldnbrPax") ?> </td>
269
            <td> <?php echo $flight->nbrPax ?> </td>
270
        </tr>
271
272
        <tr>
273
            <td class="fieldrequired"><?php echo $langs->trans("Fieldis_facture") ?> </td>
274
            <td> <?php echo $flight->getLibStatut(5) ?> </td>
275
        </tr>
276
277
        <tr>
278
            <td class="fieldrequired">Prix standard</td>
279
            <td> <?php echo $flightProduct->price_ttc . " " . $langs->getCurrencySymbol($conf->currency) ?> </td>
280
        </tr>
281
        <tr>
282
            <td class="fieldrequired"><?php echo $langs->trans("Fieldcost") ?> </td>
283
            <td> <?php echo $flight->cost . " " . $langs->getCurrencySymbol($conf->currency) ?> </td>
284
        </tr>
285
        <tr>
286
            <td class="fieldrequired"><?php echo $langs->trans("UnitPrice") ?> </td>
287
            <td> <?php echo $puFlight . " " . $langs->getCurrencySymbol($conf->currency) ?> </td>
288
        </tr>
289
    </table>
290
291
    <br/>
292
    <br/>
293
294
    <form method="POST">
295
296
        <!-- action -->
297
        <input type="hidden" name="action" value="<?= EXPENSE_REPORT_GENERATOR_ACTION_GENERATE ?>">
298
        <input type="hidden" name="id" value="<?= $id ?>">
299
300
        <!-- Billing type -->
301
        <label><?= $langs->trans("Type de facture"); ?></label><br/>
302
        <input type="radio" id="radio_standard" name="type" value="0" checked="checked"/>
303
        <?= $form->textwithpicto($langs->trans("InvoiceStandardAsk"), $langs->transnoentities("InvoiceStandardDesc"), 1,
304
            'help', '', 0, 3) ?>
305
        <br/>
306
        <br/>
307
308
        <!-- Payment mode -->
309
        <label><?= $langs->trans("Mode de payement"); ?></label><br/>
310
        <?php $form->select_types_paiements($customer->mode_reglement_id, 'mode_reglement_id', 'CRDT'); ?>
311
        <br/>
312
        <br/>
313
314
        <!-- Payment condition -->
315
        <label><?= $langs->trans("Condition de payement"); ?></label><br/>
316
        <?php $form->select_conditions_paiements($customer->cond_reglement_id, 'cond_reglement_id'); ?>
317
        <br/>
318
        <br/>
319
320
        <!-- bank account -->
321
        <label><?= $langs->trans("Compte en banque"); ?></label><br/>
322
        <?php $form->select_comptes($customer->fk_account, 'fk_account', 0, '', 1); ?>
323
        <br/>
324
        <br/>
325
326
        <!-- Public note -->
327
        <label><?= $langs->trans("Note publique"); ?></label><br/>
328
        <textarea name="public_note" wrap="soft" class="quatrevingtpercent" rows="2">
329
        Vol (identifiant : <?php echo $flight->getId(); ?>) de <?php echo $flight->lieuD; ?>
330
            à <?php echo $flight->lieuA; ?> avec <?php echo $pilot->getFullName($langs); ?>
331
        </textarea>
332
        <br/>
333
        <br/>
334
335
        <!-- Private note -->
336
        <label><?= $langs->trans("Note privée"); ?></label><br/>
337
        <textarea name="private_note" wrap="soft" class="quatrevingtpercent" rows="2">
338
        </textarea>
339
        <br/>
340
341
        <!-- model document -->
342
        <label><?= $langs->trans("Model de document "); ?></label><br/>
343
        <?php $liste = ModelePDFFactures::liste_modeles($db); ?>
344
        <?= $form->selectarray('model', $liste, $conf->global->FACTURE_ADDON_PDF); ?>
345
        <br/>
346
        <br/>
347
348
        <?php if (!$flightProduct) : ?>
349
            <a class="butActionRefused" href="#">Générer</a>
350
        <?php else: ?>
351
            <button class="butAction" type="submit">Générer</button>
352
        <?php endif; ?>
353
354
        <a class="butAction" href="<?php echo DOL_URL_ROOT . '/flightlog/card.php?id=' . $flight->id; ?>">Retour au
355
            vol</a>
356
357
    </form>
358
359
<?php
360
llxFooter();