dolibarr 19.0.4
card.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2002-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2020 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2004 Christophe Combelles <ccomb@free.fr>
5 * Copyright (C) 2005 Marc Barilley <marc@ocebo.fr>
6 * Copyright (C) 2005-2013 Regis Houssin <regis.houssin@inodbox.com>
7 * Copyright (C) 2010-2023 Juanjo Menent <jmenent@simnandez.es>
8 * Copyright (C) 2013-2022 Philippe Grand <philippe.grand@atoo-net.com>
9 * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
10 * Copyright (C) 2014-2016 Marcos García <marcosgdf@gmail.com>
11 * Copyright (C) 2016-2023 Alexandre Spangaro <aspangaro@open-dsi.fr>
12 * Copyright (C) 2018-2023 Frédéric France <frederic.france@netlogic.fr>
13 * Copyright (C) 2019 Ferran Marcet <fmarcet@2byte.es>
14 * Copyright (C) 2022 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
15 * Copyright (C) 2023 Nick Fragoulis
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 3 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <https://www.gnu.org/licenses/>.
29 */
30
37// Load Dolibarr environment
38require '../../main.inc.php';
39require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
40require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
41require_once DOL_DOCUMENT_ROOT.'/core/modules/supplier_invoice/modules_facturefournisseur.php';
42require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
43require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture-rec.class.php';
44require_once DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php';
45require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
46require_once DOL_DOCUMENT_ROOT.'/core/lib/fourn.lib.php';
47require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
48require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
49require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
50if (isModEnabled("product")) {
51 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
52 require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
53}
54if (isModEnabled('project')) {
55 require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
56 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
57}
58
59if (isModEnabled('variants')) {
60 require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination.class.php';
61}
62if (isModEnabled('accounting')) {
63 require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingjournal.class.php';
64}
65
66
67$langs->loadLangs(array('bills', 'compta', 'suppliers', 'companies', 'products', 'banks', 'admin'));
68if (isModEnabled('incoterm')) {
69 $langs->load('incoterm');
70}
71
72$id = (GETPOST('facid', 'int') ? GETPOST('facid', 'int') : GETPOST('id', 'int'));
73
74$action = GETPOST('action', 'aZ09');
75$confirm = GETPOST("confirm");
76$ref = GETPOST('ref', 'alpha');
77$cancel = GETPOST('cancel', 'alpha');
78$backtopage = GETPOST('backtopage', 'alpha');
79$backtopageforcancel = '';
80
81$lineid = GETPOST('lineid', 'int');
82$projectid = GETPOST('projectid', 'int');
83$origin = GETPOST('origin', 'alpha');
84$originid = GETPOST('originid', 'int');
85$fac_recid = GETPOST('fac_rec', 'int');
86$rank = (GETPOST('rank', 'int') > 0) ? GETPOST('rank', 'int') : -1;
87
88// PDF
89$hidedetails = (GETPOST('hidedetails', 'int') ? GETPOST('hidedetails', 'int') : (getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS') ? 1 : 0));
90$hidedesc = (GETPOST('hidedesc', 'int') ? GETPOST('hidedesc', 'int') : (getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_DESC') ? 1 : 0));
91$hideref = (GETPOST('hideref', 'int') ? GETPOST('hideref', 'int') : (getDolGlobalString('MAIN_GENERATE_DOCUMENTS_HIDE_REF') ? 1 : 0));
92
93// Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
94$hookmanager->initHooks(array('invoicesuppliercard', 'globalcard'));
95
96$object = new FactureFournisseur($db);
97$extrafields = new ExtraFields($db);
98
99// fetch optionals attributes and labels
100$extrafields->fetch_name_optionals_label($object->table_element);
101
102// Load object
103if ($id > 0 || !empty($ref)) {
104 $ret = $object->fetch($id, $ref);
105 if ($ret < 0) {
106 dol_print_error($db, $object->error);
107 }
108 $ret = $object->fetch_thirdparty();
109 if ($ret < 0) {
110 dol_print_error($db, $object->error);
111 }
112}
113
114// Security check
115$socid = GETPOST('socid', 'int');
116if (!empty($user->socid)) {
117 $socid = $user->socid;
118}
119
120$isdraft = (($object->statut == FactureFournisseur::STATUS_DRAFT) ? 1 : 0);
121$result = restrictedArea($user, 'fournisseur', $id, 'facture_fourn', 'facture', 'fk_soc', 'rowid', $isdraft);
122
123// Common permissions
124$usercanread = ($user->hasRight("fournisseur", "facture", "lire") || $user->hasRight("supplier_invoice", "lire"));
125$usercancreate = ($user->hasRight("fournisseur", "facture", "creer") || $user->hasRight("supplier_invoice", "creer"));
126$usercandelete = ($user->hasRight("fournisseur", "facture", "supprimer") || $user->hasRight("supplier_invoice", "supprimer"));
127$usercancreatecontract = $user->hasRight("contrat", "creer");
128
129// Advanced permissions
130$usercanvalidate = ((!getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && !empty($usercancreate)) || (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight("fournisseur", "supplier_invoice_advance", "validate")));
131$usercansend = (!getDolGlobalString('MAIN_USE_ADVANCED_PERMS') || $user->hasRight("fournisseur", "supplier_invoice_advance", "send"));
132
133// Permissions for includes
134$permissionnote = $usercancreate; // Used by the include of actions_setnotes.inc.php
135$permissiondellink = $usercancreate; // Used by the include of actions_dellink.inc.php
136$permissiontoedit = $usercancreate; // Used by the include of actions_lineupdown.inc.php
137$permissiontoadd = $usercancreate; // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
138
139$error = 0;
140
141
142/*
143 * Actions
144 */
145
146$parameters = array('socid'=>$socid);
147$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
148if ($reshook < 0) {
149 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
150}
151
152if (empty($reshook)) {
153 $backurlforlist = DOL_URL_ROOT.'/fourn/facture/list.php';
154
155 if (empty($backtopage) || ($cancel && empty($id))) {
156 if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
157 if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) {
158 $backtopage = $backurlforlist;
159 } else {
160 $backtopage = DOL_URL_ROOT.'/fourn/facture/card.php?id='.((!empty($id) && $id > 0) ? $id : '__ID__');
161 }
162 }
163 }
164
165 if ($cancel) {
166 if (!empty($backtopageforcancel)) {
167 header("Location: ".$backtopageforcancel);
168 exit;
169 } elseif (!empty($backtopage)) {
170 header("Location: ".$backtopage);
171 exit;
172 }
173 $action = '';
174 }
175
176 include DOL_DOCUMENT_ROOT.'/core/actions_setnotes.inc.php'; // Must be include, not include_once
177
178 include DOL_DOCUMENT_ROOT.'/core/actions_dellink.inc.php'; // Must be include, not include_once
179
180 include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php'; // Must be include, not include_once
181
182 // Link invoice to order
183 if (GETPOST('linkedOrder') && empty($cancel) && $id > 0) {
184 $object->fetch($id);
185 $object->fetch_thirdparty();
186 $result = $object->add_object_linked('order_supplier', GETPOST('linkedOrder'));
187 }
188
189 // Action clone object
190 if ($action == 'confirm_clone' && $confirm == 'yes' && $permissiontoadd) {
191 $objectutil = dol_clone($object, 1); // To avoid to denaturate loaded object when setting some properties for clone. We use native clone to keep this->db valid.
192
193 if (GETPOST('newsupplierref', 'alphanohtml')) {
194 $objectutil->ref_supplier = GETPOST('newsupplierref', 'alphanohtml');
195 }
196 $objectutil->date = dol_mktime(12, 0, 0, GETPOST('newdatemonth', 'int'), GETPOST('newdateday', 'int'), GETPOST('newdateyear', 'int'));
197
198 $result = $objectutil->createFromClone($user, $id);
199 if ($result > 0) {
200 header("Location: ".$_SERVER['PHP_SELF'].'?id='.$result);
201 exit;
202 } else {
203 $langs->load("errors");
204 setEventMessages($objectutil->error, $objectutil->errors, 'errors');
205 $action = '';
206 }
207 } elseif ($action == 'confirm_valid' && $confirm == 'yes' && $usercanvalidate) {
208 $idwarehouse = GETPOST('idwarehouse');
209
210 $object->fetch($id);
211 $object->fetch_thirdparty();
212
213 $qualified_for_stock_change = 0;
214 if (!getDolGlobalString('STOCK_SUPPORTS_SERVICES')) {
215 $qualified_for_stock_change = $object->hasProductsOrServices(2);
216 } else {
217 $qualified_for_stock_change = $object->hasProductsOrServices(1);
218 }
219
220 // Check parameters
221 if (isModEnabled('stock') && getDolGlobalString('STOCK_CALCULATE_ON_SUPPLIER_BILL') && $qualified_for_stock_change) {
222 $langs->load("stocks");
223 if (!$idwarehouse || $idwarehouse == -1) {
224 $error++;
225 setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Warehouse")), null, 'errors');
226 $action = '';
227 }
228 }
229
230 if (!$error) {
231 $db->begin();
232
233 $result = $object->validate($user, '', $idwarehouse);
234 if ($result < 0) {
235 $db->rollback();
236
237 setEventMessages($object->error, $object->errors, 'errors');
238 } else {
239 $db->commit();
240
241 // Define output language
242 if (!getDolGlobalString('MAIN_DISABLE_PDF_AUTOUPDATE')) {
243 $outputlangs = $langs;
244 $newlang = '';
245 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
246 $newlang = GETPOST('lang_id', 'aZ09');
247 }
248 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
249 $newlang = $object->thirdparty->default_lang;
250 }
251 if (!empty($newlang)) {
252 $outputlangs = new Translate("", $conf);
253 $outputlangs->setDefaultLang($newlang);
254 }
255 $model = $object->model_pdf;
256 $ret = $object->fetch($id); // Reload to get new records
257
258 $result = $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
259 if ($result < 0) {
260 dol_print_error($db, $result);
261 }
262 }
263 }
264 }
265 } elseif ($action == 'confirm_delete' && $confirm == 'yes') {
266 $object->fetch($id);
267 $object->fetch_thirdparty();
268
269 $isErasable = $object->is_erasable();
270
271 if (($usercandelete && $isErasable > 0) || ($usercancreate && $isErasable == 1)) {
272 $revertstock = GETPOST('revertstock');
273
274 if ($revertstock) {
275 $idwarehouse = GETPOST('idwarehouse');
276
277 $qualified_for_stock_change = 0;
278 if (!getDolGlobalString('STOCK_SUPPORTS_SERVICES')) {
279 $qualified_for_stock_change = $object->hasProductsOrServices(2);
280 } else {
281 $qualified_for_stock_change = $object->hasProductsOrServices(1);
282 }
283
284 // Check parameters
285 if (isModEnabled('stock') && getDolGlobalString('STOCK_CALCULATE_ON_SUPPLIER_BILL') && $qualified_for_stock_change) {
286 $langs->load("stocks");
287 if (!$idwarehouse || $idwarehouse == -1) {
288 $error++;
289 setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Warehouse")), null, 'errors');
290 $action = 'delete';
291 } else {
292 $result = $object->setDraft($user, $idwarehouse);
293 if ($result < 0) {
294 $error++;
295 }
296 }
297 }
298 }
299
300 if (!$error) {
301 $result = $object->delete($user);
302 if ($result > 0) {
303 header('Location: list.php?restore_lastsearch_values=1');
304 exit;
305 } else {
306 setEventMessages($object->error, $object->errors, 'errors');
307 }
308 }
309 }
310 } elseif ($action == 'confirm_deleteline' && $confirm == 'yes' && $usercancreate) {
311 // Remove a product line
312 $result = $object->deleteline($lineid);
313 if ($result > 0) {
314 // reorder lines
315 $object->line_order(true);
316 // Define output language
317 /*$outputlangs = $langs;
318 $newlang = '';
319 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id','aZ09'))
320 $newlang = GETPOST('lang_id','aZ09');
321 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang))
322 $newlang = $object->thirdparty->default_lang;
323 if (!empty($newlang)) {
324 $outputlangs = new Translate("", $conf);
325 $outputlangs->setDefaultLang($newlang);
326 }
327 if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
328 $ret = $object->fetch($object->id); // Reload to get new records
329 $object->generateDocument($object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
330 }*/
331
332 header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id);
333 exit;
334 } else {
335 setEventMessages($object->error, $object->errors, 'errors');
336 /* Fix bug 1485 : Reset action to avoid asking again confirmation on failure */
337 $action = '';
338 }
339 } elseif ($action == 'unlinkdiscount' && $usercancreate) {
340 // Delete link of credit note to invoice
341 $discount = new DiscountAbsolute($db);
342 $result = $discount->fetch(GETPOSTINT("discountid"));
343 $discount->unlink_invoice();
344 } elseif ($action == 'confirm_paid' && $confirm == 'yes' && $usercancreate) {
345 $object->fetch($id);
346 $result = $object->setPaid($user);
347 if ($result < 0) {
348 setEventMessages($object->error, $object->errors, 'errors');
349 }
350 } elseif ($action == 'confirm_paid_partially' && $confirm == 'yes') {
351 // Classif "paid partialy"
352 $object->fetch($id);
353 $close_code = GETPOST("close_code", 'restricthtml');
354 $close_note = GETPOST("close_note", 'restricthtml');
355 if ($close_code) {
356 $result = $object->setPaid($user, $close_code, $close_note);
357 if ($result < 0) {
358 setEventMessages($object->error, $object->errors, 'errors');
359 }
360 } else {
361 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Reason")), null, 'errors');
362 }
363 } elseif ($action == 'confirm_canceled' && $confirm == 'yes') {
364 // Classify "abandoned"
365 $object->fetch($id);
366 $close_code = GETPOST("close_code", 'restricthtml');
367 $close_note = GETPOST("close_note", 'restricthtml');
368 if ($close_code) {
369 $result = $object->setCanceled($user, $close_code, $close_note);
370 if ($result < 0) {
371 setEventMessages($object->error, $object->errors, 'errors');
372 }
373 } else {
374 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Reason")), null, 'errors');
375 }
376 }
377
378 // Set supplier ref
379 if ($action == 'setref_supplier' && $usercancreate) {
380 $object->ref_supplier = GETPOST('ref_supplier', 'alpha');
381
382 if ($object->update($user) < 0) {
383 setEventMessages($object->error, $object->errors, 'errors');
384 } else {
385 // Define output language
386 $outputlangs = $langs;
387 $newlang = '';
388 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
389 $newlang = GETPOST('lang_id', 'aZ09');
390 }
391 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
392 $newlang = $object->thirdparty->default_lang;
393 }
394 if (!empty($newlang)) {
395 $outputlangs = new Translate("", $conf);
396 $outputlangs->setDefaultLang($newlang);
397 }
398 if (!getDolGlobalString('MAIN_DISABLE_PDF_AUTOUPDATE')) {
399 $ret = $object->fetch($object->id); // Reload to get new records
400 $object->generateDocument($object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
401 }
402 }
403 }
404
405 // payments conditions
406 if ($action == 'setconditions' && $usercancreate) {
407 $object->fetch($id);
408 $object->cond_reglement_code = 0; // To clean property
409 $object->cond_reglement_id = 0; // To clean property
410
411 $error = 0;
412
413 $db->begin();
414
415 if (!$error) {
416 $result = $object->setPaymentTerms(GETPOST('cond_reglement_id', 'int'));
417 if ($result < 0) {
418 $error++;
419 setEventMessages($object->error, $object->errors, 'errors');
420 }
421 }
422
423 if (!$error) {
424 $new_date_echeance = $object->calculate_date_lim_reglement();
425 if ($new_date_echeance) {
426 $object->date_echeance = $new_date_echeance;
427 }
428 if ($object->date_echeance < $object->date) {
429 $object->date_echeance = $object->date;
430 }
431 $result = $object->update($user);
432 if ($result < 0) {
433 $error++;
434 setEventMessages($object->error, $object->errors, 'errors');
435 }
436 }
437
438 if ($error) {
439 $db->rollback();
440 } else {
441 $db->commit();
442 }
443 } elseif ($action == 'set_incoterms' && isModEnabled('incoterm')) {
444 // Set incoterm
445 $result = $object->setIncoterms(GETPOST('incoterm_id', 'int'), GETPOST('location_incoterms', 'alpha'));
446 } elseif ($action == 'setmode' && $usercancreate) {
447 // payment mode
448 $result = $object->setPaymentMethods(GETPOST('mode_reglement_id', 'int'));
449 } elseif ($action == 'setmulticurrencycode' && $usercancreate) {
450 // Multicurrency Code
451 $result = $object->setMulticurrencyCode(GETPOST('multicurrency_code', 'alpha'));
452 } elseif ($action == 'setmulticurrencyrate' && $usercancreate) {
453 // Multicurrency rate
454 $result = $object->setMulticurrencyRate(price2num(GETPOST('multicurrency_tx', 'alpha')), GETPOST('calculation_mode', 'int'));
455 } elseif ($action == 'setbankaccount' && $usercancreate) {
456 // bank account
457 $result = $object->setBankAccount(GETPOST('fk_account', 'int'));
458 } elseif ($action == 'setvatreversecharge' && $usercancreate) {
459 // vat reverse charge
460 $vatreversecharge = GETPOST('vat_reverse_charge') == 'on' ? 1 : 0;
461 $result = $object->setVATReverseCharge($vatreversecharge);
462 }
463
464 if ($action == 'settransportmode' && ($user->hasRight("fournisseur", "facture", "creer") || $user->hasRight("supplier_invoice", "creer"))) {
465 // transport mode
466 $result = $object->setTransportMode(GETPOST('transport_mode_id', 'int'));
467 } elseif ($action == 'setlabel' && $usercancreate) {
468 // Set label
469 $object->fetch($id);
470 $object->label = GETPOST('label');
471 $result = $object->update($user);
472 if ($result < 0) {
473 dol_print_error($db);
474 }
475 } elseif ($action == 'setdatef' && $usercancreate) {
476 $newdate = dol_mktime(0, 0, 0, GETPOST('datefmonth', 'int'), GETPOST('datefday', 'int'), GETPOST('datefyear', 'int'), 'tzserver');
477 if ($newdate > (dol_now('tzuserrel') + (!getDolGlobalString('INVOICE_MAX_FUTURE_DELAY') ? 0 : $conf->global->INVOICE_MAX_FUTURE_DELAY))) {
478 if (!getDolGlobalString('INVOICE_MAX_FUTURE_DELAY')) {
479 setEventMessages($langs->trans("WarningInvoiceDateInFuture"), null, 'warnings');
480 } else {
481 setEventMessages($langs->trans("WarningInvoiceDateTooFarInFuture"), null, 'warnings');
482 }
483 }
484
485 $object->fetch($id);
486
487 $object->date = $newdate;
488 $date_echence_calc = $object->calculate_date_lim_reglement();
489 if (!empty($object->date_echeance)) {
490 $object->date_echeance = $date_echence_calc;
491 }
492 if ($object->date_echeance && $object->date_echeance < $object->date) {
493 $object->date_echeance = $object->date;
494 }
495
496 $result = $object->update($user);
497 if ($result < 0) {
498 dol_print_error($db, $object->error);
499 }
500 } elseif ($action == 'setdate_lim_reglement' && $usercancreate) {
501 $object->fetch($id);
502 $object->date_echeance = dol_mktime(12, 0, 0, GETPOST('date_lim_reglementmonth', 'int'), GETPOST('date_lim_reglementday', 'int'), GETPOST('date_lim_reglementyear', 'int'));
503 if (!empty($object->date_echeance) && $object->date_echeance < $object->date) {
504 $object->date_echeance = $object->date;
505 setEventMessages($langs->trans("DatePaymentTermCantBeLowerThanObjectDate"), null, 'warnings');
506 }
507 $result = $object->update($user);
508 if ($result < 0) {
509 dol_print_error($db, $object->error);
510 }
511 } elseif ($action == "setabsolutediscount" && $usercancreate) {
512 $db->begin();
513 // We use the credit to reduce amount of invoice
514 if (GETPOST("remise_id", "int")) {
515 $ret = $object->fetch($id);
516 if ($ret > 0) {
517 $result = $object->insert_discount(GETPOST("remise_id", "int"));
518 if ($result < 0) {
519 setEventMessages($object->error, $object->errors, 'errors');
520 }
521 } else {
522 dol_print_error($db, $object->error);
523 }
524 }
525 // We use the credit to reduce remain to pay
526 if (GETPOST("remise_id_for_payment", "int")) {
527 require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php';
528 $discount = new DiscountAbsolute($db);
529 $discount->fetch(GETPOST("remise_id_for_payment", "int"));
530
531 //var_dump($object->getRemainToPay(0));
532 //var_dump($discount->amount_ttc);exit;
533 $remaintopay = $object->getRemainToPay(0);
534 if (price2num($discount->amount_ttc) > price2num($remaintopay)) {
535 // TODO Split the discount in 2 automatically
536 $error++;
537 setEventMessages($langs->trans("ErrorDiscountLargerThanRemainToPaySplitItBefore"), null, 'errors');
538 }
539
540 if (!$error) {
541 $result = $discount->link_to_invoice(0, $id);
542 if ($result < 0) {
543 $error++;
544 setEventMessages($discount->error, $discount->errors, 'errors');
545 }
546 }
547 if (!$error) {
548 $newremaintopay = $object->getRemainToPay(0);
549 if ($newremaintopay == 0) {
550 $object->setPaid($user);
551 }
552 }
553 }
554 if (!$error) {
555 $db->commit();
556 } else {
557 $db->rollback();
558 }
559 if (empty($error) && !getDolGlobalString('MAIN_DISABLE_PDF_AUTOUPDATE')) {
560 $outputlangs = $langs;
561 $newlang = '';
562 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
563 $newlang = GETPOST('lang_id', 'aZ09');
564 }
565 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
566 $newlang = $object->thirdparty->default_lang;
567 }
568 if (!empty($newlang)) {
569 $outputlangs = new Translate("", $conf);
570 $outputlangs->setDefaultLang($newlang);
571 }
572 $ret = $object->fetch($id); // Reload to get new records
573
574 $result = $object->generateDocument($object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
575 if ($result < 0) {
576 setEventMessages($object->error, $object->errors, 'errors');
577 }
578 }
579 } elseif ($action == 'confirm_converttoreduc' && $confirm == 'yes' && $usercancreate) {
580 // Convertir en reduc
581 $object->fetch($id);
582 $object->fetch_thirdparty();
583 //$object->fetch_lines(); // Already done into fetch
584
585 // Check if there is already a discount (protection to avoid duplicate creation when resubmit post)
586 $discountcheck = new DiscountAbsolute($db);
587 $result = $discountcheck->fetch(0, 0, $object->id);
588
589 $canconvert = 0;
590 if ($object->type == FactureFournisseur::TYPE_DEPOSIT && empty($discountcheck->id)) {
591 $canconvert = 1; // we can convert deposit into discount if deposit is paid (completely, partially or not at all) and not already converted (see real condition into condition used to show button converttoreduc)
592 }
593 if (($object->type == FactureFournisseur::TYPE_CREDIT_NOTE || $object->type == FactureFournisseur::TYPE_STANDARD) && $object->paye == 0 && empty($discountcheck->id)) {
594 $canconvert = 1; // we can convert credit note into discount if credit note is not refunded completely and not already converted and amount of payment is 0 (see also the real condition used as the condition to show button converttoreduc)
595 }
596 if ($canconvert) {
597 $db->begin();
598
599 $amount_ht = $amount_tva = $amount_ttc = array();
600 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
601
602 // Loop on each vat rate
603 $i = 0;
604 foreach ($object->lines as $line) {
605 if ($line->product_type < 9 && $line->total_ht != 0) { // Remove lines with product_type greater than or equal to 9 and no need to create discount if amount is null
606 $keyforvatrate = $line->tva_tx.($line->vat_src_code ? ' ('.$line->vat_src_code.')' : '');
607
608 $amount_ht[$keyforvatrate] += $line->total_ht;
609 $amount_tva[$keyforvatrate] += $line->total_tva;
610 $amount_ttc[$keyforvatrate] += $line->total_ttc;
611 $multicurrency_amount_ht[$keyforvatrate] += $line->multicurrency_total_ht;
612 $multicurrency_amount_tva[$keyforvatrate] += $line->multicurrency_total_tva;
613 $multicurrency_amount_ttc[$keyforvatrate] += $line->multicurrency_total_ttc;
614 $i++;
615 }
616 }
617
618 // If some payments were already done, we change the amount to pay using same prorate
619 if (getDolGlobalString('SUPPLIER_INVOICE_ALLOW_REUSE_OF_CREDIT_WHEN_PARTIALLY_REFUNDED') && $object->type == FactureFournisseur::TYPE_CREDIT_NOTE) {
620 $alreadypaid = $object->getSommePaiement(); // This can be not 0 if we allow to create credit to reuse from credit notes partially refunded.
621 if ($alreadypaid && abs($alreadypaid) < abs($object->total_ttc)) {
622 $ratio = abs(($object->total_ttc - $alreadypaid) / $object->total_ttc);
623 foreach ($amount_ht as $vatrate => $val) {
624 $amount_ht[$vatrate] = price2num($amount_ht[$vatrate] * $ratio, 'MU');
625 $amount_tva[$vatrate] = price2num($amount_tva[$vatrate] * $ratio, 'MU');
626 $amount_ttc[$vatrate] = price2num($amount_ttc[$vatrate] * $ratio, 'MU');
627 $multicurrency_amount_ht[$vatrate] = price2num($multicurrency_amount_ht[$vatrate] * $ratio, 'MU');
628 $multicurrency_amount_tva[$vatrate] = price2num($multicurrency_amount_tva[$vatrate] * $ratio, 'MU');
629 $multicurrency_amount_ttc[$vatrate] = price2num($multicurrency_amount_ttc[$vatrate] * $ratio, 'MU');
630 }
631 }
632 }
633 //var_dump($amount_ht);var_dump($amount_tva);var_dump($amount_ttc);exit;
634
635 // Insert one discount by VAT rate category
636 $discount = new DiscountAbsolute($db);
637 if ($object->type == FactureFournisseur::TYPE_CREDIT_NOTE) {
638 $discount->description = '(CREDIT_NOTE)';
639 } elseif ($object->type == FactureFournisseur::TYPE_DEPOSIT) {
640 $discount->description = '(DEPOSIT)';
641 } elseif ($object->type == FactureFournisseur::TYPE_STANDARD || $object->type == FactureFournisseur::TYPE_REPLACEMENT || $object->type == FactureFournisseur::TYPE_SITUATION) {
642 $discount->description = '(EXCESS PAID)';
643 } else {
644 setEventMessages($langs->trans('CantConvertToReducAnInvoiceOfThisType'), null, 'errors');
645 }
646 $discount->discount_type = 1; // Supplier discount
647 $discount->fk_soc = $object->socid;
648 $discount->fk_invoice_supplier_source = $object->id;
649
650 $error = 0;
651
652 if ($object->type == FactureFournisseur::TYPE_STANDARD || $object->type == FactureFournisseur::TYPE_REPLACEMENT || $object->type == FactureFournisseur::TYPE_SITUATION) {
653 // If we're on a standard invoice, we have to get excess paid to create a discount in TTC without VAT
654
655 // Total payments
656 $sql = 'SELECT SUM(pf.amount) as total_paiements';
657 $sql .= ' FROM '.MAIN_DB_PREFIX.'paiementfourn_facturefourn as pf, '.MAIN_DB_PREFIX.'paiementfourn as p';
658 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as c ON p.fk_paiement = c.id AND c.entity IN ('.getEntity('c_paiement').')';
659 $sql .= ' WHERE pf.fk_facturefourn = '.((int) $object->id);
660 $sql .= ' AND pf.fk_paiementfourn = p.rowid';
661 $sql .= ' AND p.entity IN ('.getEntity('invoice').')';
662
663 $resql = $db->query($sql);
664 if (!$resql) {
665 dol_print_error($db);
666 }
667
668 $res = $db->fetch_object($resql);
669 $total_paiements = $res->total_paiements;
670
671 // Total credit note and deposit
672 $total_creditnote_and_deposit = 0;
673 $sql = "SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
674 $sql .= " re.description, re.fk_invoice_supplier_source";
675 $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as re";
676 $sql .= " WHERE fk_invoice_supplier = ".((int) $object->id);
677 $resql = $db->query($sql);
678 if (!empty($resql)) {
679 while ($obj = $db->fetch_object($resql)) {
680 $total_creditnote_and_deposit += $obj->amount_ttc;
681 }
682 } else {
683 dol_print_error($db);
684 }
685
686 $discount->amount_ht = $discount->amount_ttc = $total_paiements + $total_creditnote_and_deposit - $object->total_ttc;
687 $discount->amount_tva = 0;
688 $discount->tva_tx = 0;
689 $discount->vat_src_code = '';
690
691 $result = $discount->create($user);
692 if ($result < 0) {
693 $error++;
694 }
695 }
696 if ($object->type == FactureFournisseur::TYPE_CREDIT_NOTE || $object->type == FactureFournisseur::TYPE_DEPOSIT) {
697 foreach ($amount_ht as $tva_tx => $xxx) {
698 $discount->amount_ht = abs($amount_ht[$tva_tx]);
699 $discount->amount_tva = abs($amount_tva[$tva_tx]);
700 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
701 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
702 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
703 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
704
705 // Clean vat code
706 $reg = array();
707 $vat_src_code = '';
708 if (preg_match('/\‍((.*)\‍)/', $tva_tx, $reg)) {
709 $vat_src_code = $reg[1];
710 $tva_tx = preg_replace('/\s*\‍(.*\‍)/', '', $tva_tx); // Remove code into vatrate.
711 }
712
713 $discount->tva_tx = abs($tva_tx);
714 $discount->vat_src_code = $vat_src_code;
715
716 $result = $discount->create($user);
717 if ($result < 0) {
718 $error++;
719 break;
720 }
721 }
722 }
723
724 if (empty($error)) {
725 if ($object->type != FactureFournisseur::TYPE_DEPOSIT) {
726 // Classe facture
727 $result = $object->setPaid($user);
728 if ($result >= 0) {
729 $db->commit();
730 } else {
731 setEventMessages($object->error, $object->errors, 'errors');
732 $db->rollback();
733 }
734 } else {
735 $db->commit();
736 }
737 } else {
738 setEventMessages($discount->error, $discount->errors, 'errors');
739 $db->rollback();
740 }
741 }
742 } elseif ($action == 'confirm_delete_paiement' && $confirm == 'yes' && $usercancreate) {
743 // Delete payment
744 $object->fetch($id);
745 if ($object->statut == FactureFournisseur::STATUS_VALIDATED && $object->paye == 0) {
746 $paiementfourn = new PaiementFourn($db);
747 $result = $paiementfourn->fetch(GETPOST('paiement_id'));
748 if ($result > 0) {
749 $result = $paiementfourn->delete();
750 if ($result > 0) {
751 header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
752 exit;
753 }
754 }
755 if ($result < 0) {
756 setEventMessages($paiementfourn->error, $paiementfourn->errors, 'errors');
757 }
758 }
759 } elseif ($action == 'add' && $usercancreate) {
760 // Insert new invoice in database
761 if ($socid > 0) {
762 $object->socid = GETPOST('socid', 'int');
763 }
764 $selectedLines = GETPOST('toselect', 'array');
765
766 $db->begin();
767
768 $error = 0;
769
770 // Fill array 'array_options' with data from add form
771 $ret = $extrafields->setOptionalsFromPost(null, $object);
772 if ($ret < 0) {
773 $error++;
774 }
775
776 $dateinvoice = dol_mktime(0, 0, 0, GETPOST('remonth', 'int'), GETPOST('reday', 'int'), GETPOST('reyear', 'int'), 'tzserver'); // If we enter the 02 january, we need to save the 02 january for server
777 $datedue = dol_mktime(0, 0, 0, GETPOST('echmonth', 'int'), GETPOST('echday', 'int'), GETPOST('echyear', 'int'), 'tzserver');
778 //var_dump($dateinvoice.' '.dol_print_date($dateinvoice, 'dayhour'));
779 //var_dump(dol_now('tzuserrel').' '.dol_get_last_hour(dol_now('tzuserrel')).' '.dol_print_date(dol_now('tzuserrel'),'dayhour').' '.dol_print_date(dol_get_last_hour(dol_now('tzuserrel')), 'dayhour'));
780 //var_dump($db->idate($dateinvoice));
781 //exit;
782
783 // Replacement invoice
784 if (GETPOST('type', 'int') === '') {
785 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type")), null, 'errors');
786 $error++;
787 }
788
790 if (empty($dateinvoice)) {
791 setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentities('DateInvoice')), null, 'errors');
792 $action = 'create';
793 $_GET['socid'] = $_POST['socid'];
794 $error++;
795 } elseif ($dateinvoice > (dol_get_last_hour(dol_now('tzuserrel')) + (!getDolGlobalString('INVOICE_MAX_FUTURE_DELAY') ? 0 : $conf->global->INVOICE_MAX_FUTURE_DELAY))) {
796 $error++;
797 setEventMessages($langs->trans("ErrorDateIsInFuture"), null, 'errors');
798 $action = 'create';
799 }
800
801 if (!(GETPOST('fac_replacement', 'int') > 0)) {
802 $error++;
803 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ReplaceInvoice")), null, 'errors');
804 }
805
806 if (!$error) {
807 // This is a replacement invoice
808 $result = $object->fetch(GETPOST('fac_replacement', 'int'));
809 $object->fetch_thirdparty();
810
811 $object->ref = GETPOST('ref', 'alphanohtml');
812 $object->ref_supplier = GETPOST('ref_supplier', 'alpha');
813 $object->socid = GETPOST('socid', 'int');
814 $object->libelle = GETPOST('label', 'alphanohtml'); // deprecated
815 $object->label = GETPOST('label', 'alphanohtml');
816 $object->date = $dateinvoice;
817 $object->date_echeance = $datedue;
818 $object->note_public = GETPOST('note_public', 'restricthtml');
819 $object->note_private = GETPOST('note_private', 'restricthtml');
820 $object->cond_reglement_id = GETPOST('cond_reglement_id', 'int');
821 $object->mode_reglement_id = GETPOST('mode_reglement_id', 'int');
822 $object->fk_account = GETPOST('fk_account', 'int');
823 $object->vat_reverse_charge = GETPOST('vat_reverse_charge') == 'on' ? 1 : 0;
824 $object->fk_project = ($tmpproject > 0) ? $tmpproject : null;
825 $object->fk_incoterms = GETPOST('incoterm_id', 'int');
826 $object->location_incoterms = GETPOST('location_incoterms', 'alpha');
827 $object->multicurrency_code = GETPOST('multicurrency_code', 'alpha');
828 $object->multicurrency_tx = GETPOST('originmulticurrency_tx', 'int');
829 $object->transport_mode_id = GETPOST('transport_mode_id', 'int');
830
831 // Proprietes particulieres a facture de remplacement
832 $object->fk_facture_source = GETPOST('fac_replacement', 'int');
834
835 $id = $object->createFromCurrent($user);
836 if ($id <= 0) {
837 $error++;
838 setEventMessages($object->error, $object->errors, 'errors');
839 }
840 }
841 }
842
843 // Credit note invoice
845 $sourceinvoice = GETPOST('fac_avoir', 'int');
846 if (!($sourceinvoice > 0) && !getDolGlobalString('INVOICE_CREDIT_NOTE_STANDALONE')) {
847 $error++;
848 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("CorrectInvoice")), null, 'errors');
849 }
850 if (GETPOST('socid', 'int') < 1) {
851 setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentities('Supplier')), null, 'errors');
852 $action = 'create';
853 $error++;
854 }
855
856 if (empty($dateinvoice)) {
857 setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentities('DateInvoice')), null, 'errors');
858 $action = 'create';
859 $_GET['socid'] = $_POST['socid'];
860 $error++;
861 } elseif ($dateinvoice > (dol_get_last_hour(dol_now('tzuserrel')) + (!getDolGlobalString('INVOICE_MAX_FUTURE_DELAY') ? 0 : $conf->global->INVOICE_MAX_FUTURE_DELAY))) {
862 $error++;
863 setEventMessages($langs->trans("ErrorDateIsInFuture"), null, 'errors');
864 $action = 'create';
865 }
866
867 if (!GETPOST('ref_supplier')) {
868 setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentities('RefSupplierBill')), null, 'errors');
869 $action = 'create';
870 $_GET['socid'] = $_POST['socid'];
871 $error++;
872 }
873
874 if (getDolGlobalInt('INVOICE_SUBTYPE_ENABLED') && empty(GETPOST("subtype"))) {
875 $error++;
876 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("InvoiceSubtype")), null, 'errors');
877 $action = 'create';
878 }
879
880 if (!$error) {
881 $tmpproject = GETPOST('projectid', 'int');
882
883 // Creation facture
884 $object->ref = GETPOST('ref', 'alphanohtml');
885 $object->ref_supplier = GETPOST('ref_supplier', 'alphanohtml');
886 $object->subtype = GETPOST('subtype', 'alphanohtml');
887 $object->socid = GETPOST('socid', 'int');
888 $object->libelle = GETPOST('label', 'alphanohtml');
889 $object->label = GETPOST('label', 'alphanohtml');
890 $object->date = $dateinvoice;
891 $object->date_echeance = $datedue;
892 $object->note_public = GETPOST('note_public', 'restricthtml');
893 $object->note_private = GETPOST('note_private', 'restricthtml');
894 $object->cond_reglement_id = GETPOST('cond_reglement_id');
895 $object->mode_reglement_id = GETPOST('mode_reglement_id');
896 $object->fk_account = GETPOST('fk_account', 'int');
897 $object->vat_reverse_charge = GETPOST('vat_reverse_charge') == 'on' ? 1 : 0;
898 $object->fk_project = ($tmpproject > 0) ? $tmpproject : null;
899 $object->fk_incoterms = GETPOST('incoterm_id', 'int');
900 $object->location_incoterms = GETPOST('location_incoterms', 'alpha');
901 $object->multicurrency_code = GETPOST('multicurrency_code', 'alpha');
902 $object->multicurrency_tx = GETPOST('originmulticurrency_tx', 'int');
903 $object->transport_mode_id = GETPOST('transport_mode_id', 'int');
904
905 // Proprietes particulieres a facture avoir
906 $object->fk_facture_source = $sourceinvoice > 0 ? $sourceinvoice : '';
908
909 $id = $object->create($user);
910
911 if ($id <= 0) {
912 $error++;
913 }
914
915 if (GETPOST('invoiceAvoirWithLines', 'int') == 1 && $id > 0) {
916 $facture_source = new FactureFournisseur($db); // fetch origin object
917 if ($facture_source->fetch($object->fk_facture_source) > 0) {
918 $fk_parent_line = 0;
919
920 foreach ($facture_source->lines as $line) {
921 // Reset fk_parent_line for no child products and special product
922 if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
923 $fk_parent_line = 0;
924 }
925
926 $line->fk_facture_fourn = $object->id;
927 $line->fk_parent_line = $fk_parent_line;
928
929 $line->subprice = -$line->subprice; // invert price for object
930 $line->pa_ht = -$line->pa_ht;
931 $line->total_ht = -$line->total_ht;
932 $line->total_tva = -$line->total_tva;
933 $line->total_ttc = -$line->total_ttc;
934 $line->total_localtax1 = -$line->total_localtax1;
935 $line->total_localtax2 = -$line->total_localtax2;
936
937 $result = $line->insert();
938
939 $object->lines[] = $line; // insert new line in current object
940
941 // Defined the new fk_parent_line
942 if ($result > 0 && $line->product_type == 9) {
943 $fk_parent_line = $result;
944 }
945 }
946
947 $object->update_price(1);
948 }
949 }
950
951 if (GETPOST('invoiceAvoirWithPaymentRestAmount', 'int') == 1 && $id > 0) {
952 $facture_source = new FactureFournisseur($db); // fetch origin object if not previously defined
953 if ($facture_source->fetch($object->fk_facture_source) > 0) {
954 $totalpaid = $facture_source->getSommePaiement();
955 $totalcreditnotes = $facture_source->getSumCreditNotesUsed();
956 $totaldeposits = $facture_source->getSumDepositsUsed();
957 $remain_to_pay = abs($facture_source->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits);
958 $desc = $langs->trans('invoiceAvoirLineWithPaymentRestAmount');
959 $retAddLine = $object->addline($desc, $remain_to_pay, 0, 0, 0, 1, 0, 0, '', '', 0, '', 'TTC');
960
961 if ($retAddLine < 0) {
962 $error++;
963 }
964 }
965 }
966 }
967 } elseif ($fac_recid > 0 && (GETPOST('type') == FactureFournisseur::TYPE_STANDARD || GETPOST('type') == FactureFournisseur::TYPE_DEPOSIT)) {
968 // Standard invoice or Deposit invoice, created from a Predefined template invoice
969 if (empty($dateinvoice)) {
970 $error++;
971 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Date")), null, 'errors');
972 $action = 'create';
973 } elseif ($dateinvoice > (dol_get_last_hour(dol_now('tzuserrel')) + (!getDolGlobalString('INVOICE_MAX_FUTURE_DELAY') ? 0 : $conf->global->INVOICE_MAX_FUTURE_DELAY))) {
974 $error++;
975 setEventMessages($langs->trans("ErrorDateIsInFuture"), null, 'errors');
976 $action = 'create';
977 }
978
979 if (getDolGlobalInt('INVOICE_SUBTYPE_ENABLED') && empty(GETPOST("subtype"))) {
980 $error++;
981 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("InvoiceSubtype")), null, 'errors');
982 $action = 'create';
983 }
984
985 if (!$error) {
986 $object->socid = GETPOST('socid', 'int');
987 $object->type = GETPOST('type', 'alphanohtml');
988 $object->subtype = GETPOST('subtype', 'alphanohtml');
989 $object->ref = GETPOST('ref', 'alphanohtml');
990 $object->date = $dateinvoice;
991 $object->note_public = trim(GETPOST('note_public', 'restricthtml'));
992 $object->note_private = trim(GETPOST('note_private', 'restricthtml'));
993 $object->ref_supplier = GETPOST('ref_supplier', 'alphanohtml');
994 $object->model_pdf = GETPOST('model', 'alphanohtml');
995 $object->fk_project = GETPOST('projectid', 'int');
996 $object->cond_reglement_id = (GETPOST('type') == 3 ? 1 : GETPOST('cond_reglement_id'));
997 $object->mode_reglement_id = GETPOST('mode_reglement_id', 'int');
998 $object->fk_account = GETPOST('fk_account', 'int');
999 $object->amount = price2num(GETPOST('amount'));
1000 //$object->remise_absolue = price2num(GETPOST('remise_absolue'), 'MU');
1001 //$object->remise_percent = price2num(GETPOST('remise_percent'), '', 2);
1002 $object->fk_incoterms = GETPOST('incoterm_id', 'int');
1003 $object->location_incoterms = GETPOST('location_incoterms', 'alpha');
1004 $object->multicurrency_code = GETPOST('multicurrency_code', 'alpha');
1005 $object->multicurrency_tx = GETPOST('originmulticurrency_tx', 'int');
1006
1007 // Source facture
1008 $object->fac_rec = $fac_recid;
1009 $fac_rec = new FactureFournisseurRec($db);
1010 $fac_rec->fetch($object->fac_rec);
1011 $fac_rec->fetch_lines();
1012 $object->lines = $fac_rec->lines;
1013
1014 $id = $object->create($user); // This include recopy of links from recurring invoice and recurring invoice lines
1015 }
1016 } elseif ($fac_recid <= 0 && (GETPOST('type') == FactureFournisseur::TYPE_STANDARD || GETPOST('type') == FactureFournisseur::TYPE_DEPOSIT)) {
1017 // Standard invoice or Deposit invoice, not from a Predefined template invoice
1018 if (GETPOST('socid', 'int') < 1) {
1019 setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentities('Supplier')), null, 'errors');
1020 $action = 'create';
1021 $error++;
1022 }
1023
1024 if (empty($dateinvoice)) {
1025 setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentities('DateInvoice')), null, 'errors');
1026 $action = 'create';
1027 $_GET['socid'] = $_POST['socid'];
1028 $error++;
1029 } elseif ($dateinvoice > (dol_get_last_hour(dol_now('tzuserrel')) + (!getDolGlobalString('INVOICE_MAX_FUTURE_DELAY') ? 0 : $conf->global->INVOICE_MAX_FUTURE_DELAY))) {
1030 $error++;
1031 setEventMessages($langs->trans("ErrorDateIsInFuture"), null, 'errors');
1032 $action = 'create';
1033 }
1034
1035 if (!GETPOST('ref_supplier')) {
1036 setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentities('RefSupplierBill')), null, 'errors');
1037 $action = 'create';
1038 $_GET['socid'] = $_POST['socid'];
1039 $error++;
1040 }
1041
1042 if (getDolGlobalInt('INVOICE_SUBTYPE_ENABLED') && empty(GETPOST("subtype"))) {
1043 $error++;
1044 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("InvoiceSubtype")), null, 'errors');
1045 $action = 'create';
1046 }
1047
1048 if (!$error) {
1049 $tmpproject = GETPOST('projectid', 'int');
1050
1051 // Creation invoice
1052 $object->socid = GETPOST('socid', 'int');
1053 $object->type = GETPOST('type', 'alphanohtml');
1054 $object->subtype = GETPOST('subtype', 'alphanohtml');
1055 $object->ref = GETPOST('ref', 'alphanohtml');
1056 $object->ref_supplier = GETPOST('ref_supplier', 'alphanohtml');
1057 $object->socid = GETPOST('socid', 'int');
1058 $object->libelle = GETPOST('label', 'alphanohtml'); // deprecated
1059 $object->label = GETPOST('label', 'alphanohtml');
1060 $object->date = $dateinvoice;
1061 $object->date_echeance = $datedue;
1062 $object->note_public = GETPOST('note_public', 'restricthtml');
1063 $object->note_private = GETPOST('note_private', 'restricthtml');
1064 $object->cond_reglement_id = GETPOST('cond_reglement_id');
1065 $object->mode_reglement_id = GETPOST('mode_reglement_id');
1066 $object->fk_account = GETPOST('fk_account', 'int');
1067 $object->vat_reverse_charge = GETPOST('vat_reverse_charge') == 'on' ? 1 : 0;
1068 $object->fk_project = ($tmpproject > 0) ? $tmpproject : null;
1069 $object->fk_incoterms = GETPOST('incoterm_id', 'int');
1070 $object->location_incoterms = GETPOST('location_incoterms', 'alpha');
1071 $object->multicurrency_code = GETPOST('multicurrency_code', 'alpha');
1072 $object->multicurrency_tx = GETPOST('originmulticurrency_tx', 'int');
1073 $object->transport_mode_id = GETPOST('transport_mode_id');
1074
1075 // Auto calculation of date due if not filled by user
1076 if (empty($object->date_echeance)) {
1077 $object->date_echeance = $object->calculate_date_lim_reglement();
1078 }
1079
1080 $object->fetch_thirdparty();
1081
1082 // If creation from another object of another module
1083 if (!$error && GETPOST('origin', 'alpha') && GETPOST('originid')) {
1084 // Parse element/subelement (ex: project_task)
1085 $element = $subelement = GETPOST('origin', 'alpha');
1086 /*if (preg_match('/^([^_]+)_([^_]+)/i', GETPOST('origin'),$regs))
1087 {
1088 $element = $regs[1];
1089 $subelement = $regs[2];
1090 }*/
1091
1092 // For compatibility
1093 if ($element == 'order') {
1094 $element = $subelement = 'commande';
1095 }
1096 if ($element == 'propal') {
1097 $element = 'comm/propal';
1098 $subelement = 'propal';
1099 }
1100 if ($element == 'contract') {
1101 $element = $subelement = 'contrat';
1102 }
1103 if ($element == 'order_supplier') {
1104 $element = 'fourn';
1105 $subelement = 'fournisseur.commande';
1106 }
1107 if ($element == 'project') {
1108 $element = 'projet';
1109 }
1110 $object->origin = GETPOST('origin', 'alpha');
1111 $object->origin_id = GETPOST('originid', 'int');
1112
1113
1114 require_once DOL_DOCUMENT_ROOT.'/'.$element.'/class/'.$subelement.'.class.php';
1115 $classname = ucfirst($subelement);
1116 if ($classname == 'Fournisseur.commande') {
1117 $classname = 'CommandeFournisseur';
1118 }
1119 $objectsrc = new $classname($db);
1120 $objectsrc->fetch($originid);
1121 $objectsrc->fetch_thirdparty();
1122
1123 if (!empty($object->origin) && !empty($object->origin_id)) {
1124 $object->linkedObjectsIds[$object->origin] = $object->origin_id;
1125 }
1126
1127 // Add also link with order if object is reception
1128 if ($object->origin == 'reception') {
1129 $objectsrc->fetchObjectLinked();
1130
1131 if (count($objectsrc->linkedObjectsIds['order_supplier']) > 0) {
1132 foreach ($objectsrc->linkedObjectsIds['order_supplier'] as $key => $value) {
1133 $object->linkedObjectsIds['order_supplier'] = $value;
1134 }
1135 }
1136 }
1137
1138 $id = $object->create($user);
1139
1140 // Add lines
1141 if ($id > 0) {
1142 require_once DOL_DOCUMENT_ROOT.'/'.$element.'/class/'.$subelement.'.class.php';
1143 $classname = ucfirst($subelement);
1144 if ($classname == 'Fournisseur.commande') {
1145 $classname = 'CommandeFournisseur';
1146 }
1147 $srcobject = new $classname($db);
1148
1149 $result = $srcobject->fetch(GETPOST('originid', 'int'));
1150
1151 // If deposit invoice - down payment with 1 line (fixed amount or percent)
1152 $typeamount = GETPOST('typedeposit', 'alpha');
1153 if (GETPOST('type') == FactureFournisseur::TYPE_DEPOSIT && in_array($typeamount, array('amount', 'variable'))) {
1154 $valuedeposit = price2num(GETPOST('valuedeposit', 'alpha'), 'MU');
1155
1156 // Define the array $amountdeposit
1157 $amountdeposit = array();
1158 if (getDolGlobalString('MAIN_DEPOSIT_MULTI_TVA')) {
1159 if ($typeamount == 'amount') {
1160 $amount = $valuedeposit;
1161 } else {
1162 $amount = $srcobject->total_ttc * ($valuedeposit / 100);
1163 }
1164
1165 $TTotalByTva = array();
1166 foreach ($srcobject->lines as &$line) {
1167 if (!empty($line->special_code)) {
1168 continue;
1169 }
1170 $TTotalByTva[$line->tva_tx] += $line->total_ttc;
1171 }
1172
1173 foreach ($TTotalByTva as $tva => &$total) {
1174 $coef = $total / $srcobject->total_ttc; // Calc coef
1175 $am = $amount * $coef;
1176 $amount_ttc_diff += $am;
1177 $amountdeposit[$tva] += $am / (1 + $tva / 100); // Convert into HT for the addline
1178 }
1179 } else {
1180 if ($typeamount == 'amount') {
1181 $amountdeposit[0] = $valuedeposit;
1182 } elseif ($typeamount == 'variable') {
1183 if ($result > 0) {
1184 $totalamount = 0;
1185 $lines = $srcobject->lines;
1186 $numlines = count($lines);
1187 for ($i = 0; $i < $numlines; $i++) {
1188 $qualified = 1;
1189 if (empty($lines[$i]->qty)) {
1190 $qualified = 0; // We discard qty=0, it is an option
1191 }
1192 if (!empty($lines[$i]->special_code)) {
1193 $qualified = 0; // We discard special_code (frais port, ecotaxe, option, ...)
1194 }
1195 if ($qualified) {
1196 $totalamount += $lines[$i]->total_ht; // Fixme : is it not for the customer ? Shouldn't we take total_ttc ?
1197 $tva_tx = $lines[$i]->tva_tx;
1198 $amountdeposit[$tva_tx] += ($lines[$i]->total_ht * $valuedeposit) / 100;
1199 }
1200 }
1201
1202 if ($totalamount == 0) {
1203 $amountdeposit[0] = 0;
1204 }
1205 } else {
1206 setEventMessages($srcobject->error, $srcobject->errors, 'errors');
1207 $error++;
1208 }
1209 }
1210
1211 $amount_ttc_diff = $amountdeposit[0];
1212 }
1213
1214 foreach ($amountdeposit as $tva => $amount) {
1215 if (empty($amount)) {
1216 continue;
1217 }
1218
1219 $arraylist = array(
1220 'amount' => 'FixAmount',
1221 'variable' => 'VarAmount'
1222 );
1223 $descline = '(DEPOSIT)';
1224 //$descline.= ' - '.$langs->trans($arraylist[$typeamount]);
1225 if ($typeamount == 'amount') {
1226 $descline .= ' ('.price($valuedeposit, '', $langs, 0, - 1, - 1, (!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency)).')';
1227 } elseif ($typeamount == 'variable') {
1228 $descline .= ' ('.$valuedeposit.'%)';
1229 }
1230
1231 $descline .= ' - '.$srcobject->ref;
1232 $result = $object->addline(
1233 $descline,
1234 $amount, // subprice
1235 $tva, // vat rate
1236 0, // localtax1_tx
1237 0, // localtax2_tx
1238 1, // quantity
1239 (!getDolGlobalString('INVOICE_PRODUCTID_DEPOSIT') ? 0 : $conf->global->INVOICE_PRODUCTID_DEPOSIT), // fk_product
1240 0, // remise_percent
1241 0, // date_start
1242 0, // date_end
1243 0,
1244 $lines[$i]->info_bits, // info_bits
1245 'HT',
1246 0, // product_type
1247 1,
1248 0,
1249 0,
1250 null,
1251 $object->origin,
1252 0,
1253 '',
1254 $lines[$i]->special_code,
1255 0,
1256 0
1257 //,$langs->trans('Deposit') //Deprecated
1258 );
1259 }
1260
1261 $diff = $object->total_ttc - $amount_ttc_diff;
1262
1263 if (getDolGlobalString('MAIN_DEPOSIT_MULTI_TVA') && $diff != 0) {
1264 $object->fetch_lines();
1265 $subprice_diff = $object->lines[0]->subprice - $diff / (1 + $object->lines[0]->tva_tx / 100);
1266 $object->updateline(
1267 $object->lines[0]->id,
1268 $object->lines[0]->desc,
1269 $subprice_diff,
1270 $object->lines[0]->tva_tx,
1271 $object->lines[0]->localtax1_tx,
1272 $object->lines[0]->localtax2_tx,
1273 $object->lines[0]->qty,
1274 $object->lines[0]->fk_product,
1275 'HT',
1276 $object->lines[0]->info_bits,
1277 $object->lines[0]->product_type,
1278 $object->lines[0]->remise_percent,
1279 0,
1280 $object->lines[0]->date_start,
1281 $object->lines[0]->date_end,
1282 0,
1283 0,
1284 0,
1285 '',
1286 100
1287 );
1288 }
1289 } elseif ($result > 0) {
1290 $lines = $srcobject->lines;
1291 if (empty($lines) && method_exists($srcobject, 'fetch_lines')) {
1292 $srcobject->fetch_lines();
1293 $lines = $srcobject->lines;
1294 }
1295
1296 $num = count($lines);
1297 for ($i = 0; $i < $num; $i++) { // TODO handle subprice < 0
1298 if (!in_array($lines[$i]->id, $selectedLines)) {
1299 continue; // Skip unselected lines
1300 }
1301
1302 $desc = ($lines[$i]->desc ? $lines[$i]->desc : $lines[$i]->libelle);
1303 $product_type = ($lines[$i]->product_type ? $lines[$i]->product_type : 0);
1304
1305 // Extrafields
1306 if (method_exists($lines[$i], 'fetch_optionals')) {
1307 $lines[$i]->fetch_optionals();
1308 }
1309
1310 // Dates
1311 // TODO mutualiser
1312 $date_start = $lines[$i]->date_debut_prevue;
1313 if ($lines[$i]->date_debut_reel) {
1314 $date_start = $lines[$i]->date_debut_reel;
1315 }
1316 if ($lines[$i]->date_start) {
1317 $date_start = $lines[$i]->date_start;
1318 }
1319 $date_end = $lines[$i]->date_fin_prevue;
1320 if ($lines[$i]->date_fin_reel) {
1321 $date_end = $lines[$i]->date_fin_reel;
1322 }
1323 if ($lines[$i]->date_end) {
1324 $date_end = $lines[$i]->date_end;
1325 }
1326
1327 $tva_tx = $lines[$i]->tva_tx;
1328 if (!empty($lines[$i]->vat_src_code) && !preg_match('/\‍(/', $tva_tx)) {
1329 $tva_tx .= ' ('.$lines[$i]->vat_src_code.')';
1330 }
1331
1332 // FIXME Missing special_code into addline and updateline methods
1333 $object->special_code = $lines[$i]->special_code;
1334
1335 // FIXME If currency different from main currency, take multicurrency price
1336 if ($object->multicurrency_code != $conf->currency || $object->multicurrency_tx != 1) {
1337 $pu = 0;
1338 $pu_currency = $lines[$i]->multicurrency_subprice;
1339 } else {
1340 $pu = $lines[$i]->subprice;
1341 $pu_currency = 0;
1342 }
1343
1344 // FIXME Missing $lines[$i]->ref_supplier and $lines[$i]->label into addline and updateline methods. They are filled when coming from order for example.
1345 $result = $object->addline(
1346 $desc,
1347 $pu,
1348 $tva_tx,
1349 $lines[$i]->localtax1_tx,
1350 $lines[$i]->localtax2_tx,
1351 $lines[$i]->qty,
1352 $lines[$i]->fk_product,
1353 $lines[$i]->remise_percent,
1354 $date_start,
1355 $date_end,
1356 0,
1357 $lines[$i]->info_bits,
1358 'HT',
1359 $product_type,
1360 $lines[$i]->rang,
1361 0,
1362 $lines[$i]->array_options,
1363 $lines[$i]->fk_unit,
1364 $lines[$i]->id,
1365 $pu_currency,
1366 $lines[$i]->ref_supplier,
1367 $lines[$i]->special_code
1368 );
1369
1370 if ($result < 0) {
1371 $error++;
1372 break;
1373 }
1374 }
1375
1376 // Now reload line
1377 $object->fetch_lines();
1378 } else {
1379 $error++;
1380 }
1381 } else {
1382 $error++;
1383 }
1384 } elseif (!$error) {
1385 $id = $object->create($user);
1386 if ($id < 0) {
1387 $error++;
1388 }
1389 }
1390 }
1391 }
1392
1393 if ($error) {
1394 $langs->load("errors");
1395 $db->rollback();
1396
1397 setEventMessages($object->error, $object->errors, 'errors');
1398 $action = 'create';
1399 $_GET['socid'] = $_POST['socid'];
1400 } else {
1401 $db->commit();
1402
1403 if (!getDolGlobalString('MAIN_DISABLE_PDF_AUTOUPDATE')) {
1404 $outputlangs = $langs;
1405 $result = $object->generateDocument($object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
1406 if ($result < 0) {
1407 dol_print_error($db, $object->error, $object->errors);
1408 exit;
1409 }
1410 }
1411
1412 header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
1413 exit;
1414 }
1415 } elseif ($action == 'updateline' && $usercancreate) {
1416 // Edit line
1417 $db->begin();
1418
1419 if (! $object->fetch($id) > 0) {
1420 dol_print_error($db);
1421 }
1422 $object->fetch_thirdparty();
1423
1424 $tva_tx = (GETPOST('tva_tx') ? GETPOST('tva_tx') : 0);
1425 $tva_tx = str_replace('*', '', $tva_tx);
1426
1427 if (GETPOST('price_ht') != '' || GETPOST('multicurrency_subprice') != '') {
1428 $up = price2num(GETPOST('price_ht'), '', 2);
1429 $price_base_type = 'HT';
1430 } else {
1431 $up = price2num(GETPOST('price_ttc'), '', 2);
1432 $price_base_type = 'TTC';
1433 }
1434
1435 if (GETPOST('productid') > 0) {
1436 $productsupplier = new ProductFournisseur($db);
1437 if (getDolGlobalString('SUPPLIER_INVOICE_WITH_PREDEFINED_PRICES_ONLY')) {
1438 if (GETPOST('productid') > 0 && $productsupplier->get_buyprice(0, price2num(GETPOST('qty')), GETPOST('productid', 'int'), 'restricthtml', GETPOST('socid', 'int')) < 0) {
1439 setEventMessages($langs->trans("ErrorQtyTooLowForThisSupplier"), null, 'warnings');
1440 }
1441 }
1442
1443 $prod = new Product($db);
1444 $prod->fetch(GETPOST('productid'));
1445 $label = $prod->description;
1446 if (trim(GETPOST('product_desc', 'restricthtml')) != trim($label)) {
1447 $label = GETPOST('product_desc', 'restricthtml');
1448 }
1449
1450 $type = $prod->type;
1451 } else {
1452 $label = GETPOST('product_desc', 'restricthtml');
1453 $type = GETPOST("type") ? GETPOST("type") : 0;
1454 }
1455
1456 $date_start = dol_mktime(GETPOST('date_starthour'), GETPOST('date_startmin'), GETPOST('date_startsec'), GETPOST('date_startmonth'), GETPOST('date_startday'), GETPOST('date_startyear'));
1457 $date_end = dol_mktime(GETPOST('date_endhour'), GETPOST('date_endmin'), GETPOST('date_endsec'), GETPOST('date_endmonth'), GETPOST('date_endday'), GETPOST('date_endyear'));
1458
1459 // Define info_bits
1460 $info_bits = 0;
1461 if (preg_match('/\*/', $tva_tx)) {
1462 $info_bits |= 0x01;
1463 }
1464
1465 // Define vat_rate
1466 $tva_tx = str_replace('*', '', $tva_tx);
1467 $localtax1_tx = get_localtax($tva_tx, 1, $mysoc, $object->thirdparty);
1468 $localtax2_tx = get_localtax($tva_tx, 2, $mysoc, $object->thirdparty);
1469
1470 $remise_percent = price2num(GETPOST('remise_percent'), '', 2);
1471 $pu_devise = price2num(GETPOST('multicurrency_subprice'), 'MU', 2);
1472
1473 // Extrafields Lines
1474 $extralabelsline = $extrafields->fetch_name_optionals_label($object->table_element_line);
1475 $array_options = $extrafields->getOptionalsFromPost($object->table_element_line);
1476 // Unset extrafield POST Data
1477 if (is_array($extralabelsline)) {
1478 foreach ($extralabelsline as $key => $value) {
1479 unset($_POST["options_".$key]);
1480 }
1481 }
1482
1483 $result = $object->updateline(
1484 GETPOST('lineid', 'int'),
1485 $label,
1486 $up,
1487 $tva_tx,
1488 $localtax1_tx,
1489 $localtax2_tx,
1490 price2num(GETPOST('qty'), 'MS'),
1491 GETPOST('productid', 'int'),
1492 $price_base_type,
1493 $info_bits,
1494 $type,
1495 $remise_percent,
1496 0,
1497 $date_start,
1498 $date_end,
1499 $array_options,
1500 GETPOST('units', 'alpha'),
1501 $pu_devise,
1502 GETPOST('fourn_ref', 'alpha')
1503 );
1504 if ($result >= 0) {
1505 unset($_POST['label']);
1506 unset($_POST['fourn_ref']);
1507 unset($_POST['date_starthour']);
1508 unset($_POST['date_startmin']);
1509 unset($_POST['date_startsec']);
1510 unset($_POST['date_startday']);
1511 unset($_POST['date_startmonth']);
1512 unset($_POST['date_startyear']);
1513 unset($_POST['date_endhour']);
1514 unset($_POST['date_endmin']);
1515 unset($_POST['date_endsec']);
1516 unset($_POST['date_endday']);
1517 unset($_POST['date_endmonth']);
1518 unset($_POST['date_endyear']);
1519 unset($_POST['price_ttc']);
1520 unset($_POST['price_ht']);
1521
1522 $db->commit();
1523 } else {
1524 $db->rollback();
1525 setEventMessages($object->error, $object->errors, 'errors');
1526 }
1527 } elseif ($action == 'addline' && GETPOST('submitforalllines', 'aZ09') && GETPOST('vatforalllines', 'alpha') != '' && $usercancreate) {
1528 // Define vat_rate
1529 $vat_rate = (GETPOST('vatforalllines') ? GETPOST('vatforalllines') : 0);
1530 $vat_rate = str_replace('*', '', $vat_rate);
1531 $localtax1_rate = get_localtax($vat_rate, 1, $object->thirdparty, $mysoc);
1532 $localtax2_rate = get_localtax($vat_rate, 2, $object->thirdparty, $mysoc);
1533 foreach ($object->lines as $line) {
1534 $result = $object->updateline($line->id, $line->desc, $line->subprice, $vat_rate, $localtax1_rate, $localtax2_rate, $line->qty, $line->fk_product, 'HT', $line->info_bits, $line->product_type, $line->remise_percent, 0, $line->date_start, $line->date_end, $line->array_options, $line->fk_unit, $line->multicurrency_subprice, $line->ref_supplier, $line->rang);
1535 }
1536 } elseif ($action == 'addline' && $usercancreate) {
1537 // Add a product line
1538 $db->begin();
1539
1540 $ret = $object->fetch($id);
1541 if ($ret < 0) {
1542 dol_print_error($db, $object->error);
1543 exit;
1544 }
1545 $ret = $object->fetch_thirdparty();
1546
1547 $langs->load('errors');
1548 $error = 0;
1549
1550 // Set if we used free entry or predefined product
1551 $predef = '';
1552 $product_desc = (GETPOSTISSET('dp_desc') ? GETPOST('dp_desc', 'restricthtml') : '');
1553 $date_start = dol_mktime(GETPOST('date_start'.$predef.'hour'), GETPOST('date_start'.$predef.'min'), GETPOST('date_start'.$predef.'sec'), GETPOST('date_start'.$predef.'month'), GETPOST('date_start'.$predef.'day'), GETPOST('date_start'.$predef.'year'));
1554 $date_end = dol_mktime(GETPOST('date_end'.$predef.'hour'), GETPOST('date_end'.$predef.'min'), GETPOST('date_end'.$predef.'sec'), GETPOST('date_end'.$predef.'month'), GETPOST('date_end'.$predef.'day'), GETPOST('date_end'.$predef.'year'));
1555
1556 $prod_entry_mode = GETPOST('prod_entry_mode');
1557 if ($prod_entry_mode == 'free') {
1558 $idprod = 0;
1559 } else {
1560 $idprod = GETPOST('idprod', 'int');
1561 }
1562
1563 $tva_tx = (GETPOST('tva_tx') ? GETPOST('tva_tx') : 0); // Can be '1.2' or '1.2 (CODE)'
1564
1565 $price_ht = price2num(GETPOST('price_ht'), 'MU', 2);
1566 $price_ht_devise = price2num(GETPOST('multicurrency_price_ht'), 'CU', 2);
1567 $price_ttc = price2num(GETPOST('price_ttc'), 'MU', 2);
1568 $price_ttc_devise = price2num(GETPOST('multicurrency_price_ttc'), 'CU', 2);
1569 $qty = price2num(GETPOST('qty'.$predef, 'alpha'), 'MS');
1570
1571 $remise_percent = (GETPOSTISSET('remise_percent'.$predef) ? price2num(GETPOST('remise_percent'.$predef, 'alpha'), '', 2) : 0);
1572 if (empty($remise_percent)) {
1573 $remise_percent = 0;
1574 }
1575
1576 // Extrafields
1577 $extralabelsline = $extrafields->fetch_name_optionals_label($object->table_element_line);
1578 $array_options = $extrafields->getOptionalsFromPost($object->table_element_line, $predef);
1579 // Unset extrafield
1580 if (is_array($extralabelsline)) {
1581 // Get extra fields
1582 foreach ($extralabelsline as $key => $value) {
1583 unset($_POST["options_".$key]);
1584 }
1585 }
1586
1587 if ($prod_entry_mode == 'free' && GETPOST('price_ht') < 0 && $qty < 0) {
1588 setEventMessages($langs->trans('ErrorBothFieldCantBeNegative', $langs->transnoentitiesnoconv('UnitPrice'), $langs->transnoentitiesnoconv('Qty')), null, 'errors');
1589 $error++;
1590 }
1591 if ($prod_entry_mode == 'free' && !GETPOST('idprodfournprice') && GETPOST('type') < 0) {
1592 setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Type')), null, 'errors');
1593 $error++;
1594 }
1595 if ($prod_entry_mode == 'free' && GETPOST('price_ht') === '' && GETPOST('price_ttc') === '' && $price_ht_devise === '') { // Unit price can be 0 but not ''
1596 setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('UnitPrice')), null, 'errors');
1597 $error++;
1598 }
1599 if ($prod_entry_mode == 'free' && !GETPOST('dp_desc')) {
1600 setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Description')), null, 'errors');
1601 $error++;
1602 }
1603 if (!GETPOST('qty', 'alpha')) { // 0 is NOT allowed for invoices
1604 setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Qty')), null, 'errors');
1605 $error++;
1606 }
1607
1608 if (!$error && isModEnabled('variants') && $prod_entry_mode != 'free') {
1609 if ($combinations = GETPOST('combinations', 'array')) {
1610 //Check if there is a product with the given combination
1611 $prodcomb = new ProductCombination($db);
1612
1613 if ($res = $prodcomb->fetchByProductCombination2ValuePairs($idprod, $combinations)) {
1614 $idprod = $res->fk_product_child;
1615 } else {
1616 setEventMessages($langs->trans('ErrorProductCombinationNotFound'), null, 'errors');
1617 $error++;
1618 }
1619 }
1620 }
1621
1622 if ($prod_entry_mode != 'free' && empty($error)) { // With combolist mode idprodfournprice is > 0 or -1. With autocomplete, idprodfournprice is > 0 or ''
1623 $productsupplier = new ProductFournisseur($db);
1624
1625 $idprod = 0;
1626 if (GETPOST('idprodfournprice', 'alpha') == -1 || GETPOST('idprodfournprice', 'alpha') == '') {
1627 $idprod = -99; // Same behaviour than with combolist. When not select idprodfournprice is now -99 (to avoid conflict with next action that may return -1, -2, ...)
1628 }
1629
1630 $reg = array();
1631 if (preg_match('/^idprod_([0-9]+)$/', GETPOST('idprodfournprice', 'alpha'), $reg)) {
1632 $idprod = $reg[1];
1633 $res = $productsupplier->fetch($idprod); // Load product from its id
1634 // Call to init some price properties of $productsupplier
1635 // So if a supplier price already exists for another thirdparty (first one found), we use it as reference price
1636 if (getDolGlobalString('SUPPLIER_TAKE_FIRST_PRICE_IF_NO_PRICE_FOR_CURRENT_SUPPLIER')) {
1637 $fksoctosearch = 0;
1638 $productsupplier->get_buyprice(0, -1, $idprod, 'none', $fksoctosearch); // We force qty to -1 to be sure to find if a supplier price exist
1639 if ($productsupplier->fourn_socid != $socid) { // The price we found is for another supplier, so we clear supplier price
1640 $productsupplier->ref_supplier = '';
1641 }
1642 } else {
1643 $fksoctosearch = $object->thirdparty->id;
1644 $productsupplier->get_buyprice(0, -1, $idprod, 'none', $fksoctosearch); // We force qty to -1 to be sure to find if a supplier price exist
1645 }
1646 } elseif (GETPOST('idprodfournprice', 'alpha') > 0) {
1647 $qtytosearch = $qty; // Just to see if a price exists for the quantity. Not used to found vat.
1648 //$qtytosearch=-1; // We force qty to -1 to be sure to find if a supplier price exist
1649 $idprod = $productsupplier->get_buyprice(GETPOST('idprodfournprice', 'alpha'), $qtytosearch);
1650 $res = $productsupplier->fetch($idprod);
1651 }
1652
1653 if ($idprod > 0) {
1654 $label = $productsupplier->label;
1655 // Define output language
1656 if (getDolGlobalInt('MAIN_MULTILANGS') && getDolGlobalString('PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE')) {
1657 $outputlangs = $langs;
1658 $newlang = '';
1659 if (empty($newlang) && GETPOST('lang_id', 'aZ09')) {
1660 $newlang = GETPOST('lang_id', 'aZ09');
1661 }
1662 if (empty($newlang)) {
1663 $newlang = $object->thirdparty->default_lang;
1664 }
1665 if (!empty($newlang)) {
1666 $outputlangs = new Translate("", $conf);
1667 $outputlangs->setDefaultLang($newlang);
1668 }
1669 $desc = (!empty($productsupplier->multilangs[$outputlangs->defaultlang]["description"])) ? $productsupplier->multilangs[$outputlangs->defaultlang]["description"] : $productsupplier->description;
1670 } else {
1671 $desc = $productsupplier->description;
1672 }
1673 // if we use supplier description of the products
1674 if (!empty($productsupplier->desc_supplier) && getDolGlobalString('PRODUIT_FOURN_TEXTS')) {
1675 $desc = $productsupplier->desc_supplier;
1676 }
1677
1678 //If text set in desc is the same as product descpription (as now it's preloaded) whe add it only one time
1679 if (trim($product_desc) == trim($desc) && getDolGlobalString('PRODUIT_AUTOFILL_DESC')) {
1680 $product_desc = '';
1681 }
1682 if (!empty($product_desc) && getDolGlobalString('MAIN_NO_CONCAT_DESCRIPTION')) {
1683 $desc = $product_desc;
1684 }
1685 if (!empty($product_desc) && trim($product_desc) != trim($desc)) {
1686 $desc = dol_concatdesc($desc, $product_desc, '', getDolGlobalString('MAIN_CHANGE_ORDER_CONCAT_DESCRIPTION'));
1687 }
1688
1689 $ref_supplier = $productsupplier->ref_supplier;
1690
1691 // Get vat rate
1692 if (!GETPOSTISSET('tva_tx')) { // If vat rate not provided from the form (the form has the priority)
1693 $tva_tx = get_default_tva($object->thirdparty, $mysoc, $productsupplier->id, GETPOST('idprodfournprice', 'alpha'));
1694 $tva_npr = get_default_npr($object->thirdparty, $mysoc, $productsupplier->id, GETPOST('idprodfournprice', 'alpha'));
1695 }
1696 if (empty($tva_tx) || empty($tva_npr)) {
1697 $tva_npr = 0;
1698 }
1699 $localtax1_tx = get_localtax($tva_tx, 1, $mysoc, $object->thirdparty, $tva_npr);
1700 $localtax2_tx = get_localtax($tva_tx, 2, $mysoc, $object->thirdparty, $tva_npr);
1701
1702 $type = $productsupplier->type;
1703 if (GETPOST('price_ht') != '' || GETPOST('multicurrency_price_ht') != '') {
1704 $price_base_type = 'HT';
1705 $pu = price2num($price_ht, 'MU');
1706 $pu_devise = price2num($price_ht_devise, 'CU');
1707 } elseif (GETPOST('price_ttc') != '' || GETPOST('multicurrency_price_ttc') != '') {
1708 $price_base_type = 'TTC';
1709 $pu = price2num($price_ttc, 'MU');
1710 $pu_devise = price2num($price_ttc_devise, 'CU');
1711 } else {
1712 $price_base_type = ($productsupplier->fourn_price_base_type ? $productsupplier->fourn_price_base_type : 'HT');
1713 if (empty($object->multicurrency_code) || ($productsupplier->fourn_multicurrency_code != $object->multicurrency_code)) { // If object is in a different currency and price not in this currency
1714 $pu = $productsupplier->fourn_pu;
1715 $pu_devise = 0;
1716 } else {
1717 $pu = $productsupplier->fourn_pu;
1718 $pu_devise = $productsupplier->fourn_multicurrency_unitprice;
1719 }
1720 }
1721
1722 $ref_supplier = $productsupplier->ref_supplier;
1723
1724 if (empty($pu)) {
1725 $pu = 0; // If pu is '' or null, we force to have a numeric value
1726 }
1727
1728 $result = $object->addline(
1729 $desc,
1730 $pu,
1731 $tva_tx,
1732 $localtax1_tx,
1733 $localtax2_tx,
1734 $qty,
1735 $idprod,
1736 $remise_percent,
1737 $date_start,
1738 $date_end,
1739 0,
1740 $tva_npr,
1741 $price_base_type,
1742 $type,
1743 min($rank, count($object->lines) + 1),
1744 0,
1745 $array_options,
1746 $productsupplier->fk_unit,
1747 0,
1748 $pu_devise,
1749 GETPOST('fourn_ref', 'alpha'),
1750 ''
1751 );
1752 }
1753 if ($idprod == -99 || $idprod == 0) {
1754 // Product not selected
1755 $error++;
1756 $langs->load("errors");
1757 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ProductOrService")), null, 'errors');
1758 }
1759 if ($idprod == -1) {
1760 // Quantity too low
1761 $error++;
1762 $langs->load("errors");
1763 setEventMessages($langs->trans("ErrorQtyTooLowForThisSupplier"), null, 'errors');
1764 }
1765 } elseif (empty($error)) { // $price_ht is already set
1766 $tva_npr = (preg_match('/\*/', $tva_tx) ? 1 : 0);
1767 $tva_tx = str_replace('*', '', $tva_tx);
1768 $label = (GETPOST('product_label') ? GETPOST('product_label') : '');
1769 $desc = $product_desc;
1770 $type = GETPOST('type');
1771 $ref_supplier = GETPOST('fourn_ref', 'alpha');
1772
1773 $fk_unit = GETPOST('units', 'alpha');
1774
1775 if (!preg_match('/\‍((.*)\‍)/', $tva_tx)) {
1776 $tva_tx = price2num($tva_tx); // $txtva can have format '5,1' or '5.1' or '5.1(XXX)', we must clean only if '5,1'
1777 }
1778
1779 // Local Taxes
1780 $localtax1_tx = get_localtax($tva_tx, 1, $mysoc, $object->thirdparty);
1781 $localtax2_tx = get_localtax($tva_tx, 2, $mysoc, $object->thirdparty);
1782
1783 if (GETPOST('price_ht') != '' || GETPOST('multicurrency_price_ht') != '') {
1784 $pu_ht = price2num($price_ht, 'MU'); // $pu_ht must be rounded according to settings
1785 } else {
1786 $pu_ttc = price2num(GETPOST('price_ttc'), 'MU');
1787 $pu_ht = price2num($pu_ttc / (1 + ($tva_tx / 100)), 'MU'); // $pu_ht must be rounded according to settings
1788 }
1789 $price_base_type = 'HT';
1790 $pu_devise = price2num($price_ht_devise, 'CU');
1791
1792 $result = $object->addline($product_desc, $pu_ht, $tva_tx, $localtax1_tx, $localtax2_tx, $qty, 0, $remise_percent, $date_start, $date_end, 0, $tva_npr, $price_base_type, $type, -1, 0, $array_options, $fk_unit, 0, $pu_devise, $ref_supplier);
1793 }
1794
1795 //print "xx".$tva_tx; exit;
1796 if (!$error && $result > 0) {
1797 $db->commit();
1798
1799 // Define output language
1800 if (!getDolGlobalString('MAIN_DISABLE_PDF_AUTOUPDATE')) {
1801 $outputlangs = $langs;
1802 $newlang = '';
1803 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
1804 $newlang = GETPOST('lang_id', 'aZ09');
1805 }
1806 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
1807 $newlang = $object->thirdparty->default_lang;
1808 }
1809 if (!empty($newlang)) {
1810 $outputlangs = new Translate("", $conf);
1811 $outputlangs->setDefaultLang($newlang);
1812 }
1813 $model = $object->model_pdf;
1814 $ret = $object->fetch($id); // Reload to get new records
1815
1816 $result = $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
1817 if ($result < 0) {
1818 dol_print_error($db, $result);
1819 }
1820 }
1821
1822 unset($_POST ['prod_entry_mode']);
1823
1824 unset($_POST['qty']);
1825 unset($_POST['type']);
1826 unset($_POST['remise_percent']);
1827 unset($_POST['pu']);
1828 unset($_POST['price_ht']);
1829 unset($_POST['multicurrency_price_ht']);
1830 unset($_POST['price_ttc']);
1831 unset($_POST['fourn_ref']);
1832 unset($_POST['tva_tx']);
1833 unset($_POST['label']);
1834 unset($localtax1_tx);
1835 unset($localtax2_tx);
1836 unset($_POST['np_marginRate']);
1837 unset($_POST['np_markRate']);
1838 unset($_POST['dp_desc']);
1839 unset($_POST['idprodfournprice']);
1840 unset($_POST['units']);
1841
1842 unset($_POST['date_starthour']);
1843 unset($_POST['date_startmin']);
1844 unset($_POST['date_startsec']);
1845 unset($_POST['date_startday']);
1846 unset($_POST['date_startmonth']);
1847 unset($_POST['date_startyear']);
1848 unset($_POST['date_endhour']);
1849 unset($_POST['date_endmin']);
1850 unset($_POST['date_endsec']);
1851 unset($_POST['date_endday']);
1852 unset($_POST['date_endmonth']);
1853 unset($_POST['date_endyear']);
1854 } else {
1855 $db->rollback();
1856 setEventMessages($object->error, $object->errors, 'errors');
1857 }
1858
1859 $action = '';
1860 } elseif ($action == 'classin' && $usercancreate) {
1861 $object->fetch($id);
1862 $result = $object->setProject($projectid);
1863 } elseif ($action == 'confirm_edit' && $confirm == 'yes' && $usercancreate) {
1864 // Set invoice to draft status
1865 $object->fetch($id);
1866
1867 $totalpaid = $object->getSommePaiement();
1868 $resteapayer = $object->total_ttc - $totalpaid;
1869
1870 // We check that lines of invoices are exported in accountancy
1871 $ventilExportCompta = $object->getVentilExportCompta();
1872
1873 if (!$ventilExportCompta) {
1874 // On verifie si aucun paiement n'a ete effectue
1875 if ($resteapayer == price2num($object->total_ttc, 'MT', 1) && $object->statut == FactureFournisseur::STATUS_VALIDATED) {
1876 $idwarehouse = GETPOST('idwarehouse');
1877
1878 $object->fetch_thirdparty();
1879
1880 $qualified_for_stock_change = 0;
1881 if (!getDolGlobalString('STOCK_SUPPORTS_SERVICES')) {
1882 $qualified_for_stock_change = $object->hasProductsOrServices(2);
1883 } else {
1884 $qualified_for_stock_change = $object->hasProductsOrServices(1);
1885 }
1886
1887 // Check parameters
1888 if (isModEnabled('stock') && getDolGlobalString('STOCK_CALCULATE_ON_SUPPLIER_BILL') && $qualified_for_stock_change) {
1889 $langs->load("stocks");
1890 if (!$idwarehouse || $idwarehouse == -1) {
1891 $error++;
1892 setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Warehouse")), null, 'errors');
1893 $action = '';
1894 }
1895 }
1896
1897 $object->setDraft($user, $idwarehouse);
1898
1899 // Define output language
1900 if (!getDolGlobalString('MAIN_DISABLE_PDF_AUTOUPDATE')) {
1901 $outputlangs = $langs;
1902 $newlang = '';
1903 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
1904 $newlang = GETPOST('lang_id', 'aZ09');
1905 }
1906 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
1907 $newlang = $object->thirdparty->default_lang;
1908 }
1909 if (!empty($newlang)) {
1910 $outputlangs = new Translate("", $conf);
1911 $outputlangs->setDefaultLang($newlang);
1912 }
1913 $model = $object->model_pdf;
1914 $ret = $object->fetch($id); // Reload to get new records
1915
1916 $result = $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
1917 if ($result < 0) {
1918 dol_print_error($db, $result);
1919 }
1920 }
1921
1922 $action = '';
1923 }
1924 }
1925 } elseif ($action == 'reopen' && $usercancreate) {
1926 // Set invoice to validated/unpaid status
1927 $result = $object->fetch($id);
1928 if ($object->statut == FactureFournisseur::STATUS_CLOSED
1929 || ($object->statut == FactureFournisseur::STATUS_ABANDONED && $object->close_code != 'replaced')) {
1930 $result = $object->setUnpaid($user);
1931 if ($result > 0) {
1932 header('Location: '.$_SERVER["PHP_SELF"].'?id='.$id);
1933 exit;
1934 } else {
1935 setEventMessages($object->error, $object->errors, 'errors');
1936 }
1937 }
1938 }
1939
1940 // Actions when printing a doc from card
1941 include DOL_DOCUMENT_ROOT.'/core/actions_printing.inc.php';
1942
1943 // Actions to send emails
1944 $triggersendname = 'BILL_SUPPLIER_SENTBYMAIL';
1945 $paramname = 'id';
1946 $autocopy = 'MAIN_MAIL_AUTOCOPY_SUPPLIER_INVOICE_TO';
1947 $trackid = 'sinv'.$object->id;
1948 include DOL_DOCUMENT_ROOT.'/core/actions_sendmails.inc.php';
1949
1950 // Actions to build doc
1951 $upload_dir = $conf->fournisseur->facture->dir_output;
1952 $permissiontoadd = $usercancreate;
1953 include DOL_DOCUMENT_ROOT.'/core/actions_builddoc.inc.php';
1954
1955 // Make calculation according to calculationrule
1956 if ($action == 'calculate') {
1957 $calculationrule = GETPOST('calculationrule');
1958
1959 $object->fetch($id);
1960 $object->fetch_thirdparty();
1961 $result = $object->update_price(0, (($calculationrule == 'totalofround') ? '0' : '1'), 0, $object->thirdparty);
1962 if ($result <= 0) {
1963 dol_print_error($db, $result);
1964 exit;
1965 }
1966 }
1967 if ($action == 'update_extras') {
1968 $object->oldcopy = dol_clone($object, 2);
1969
1970 // Fill array 'array_options' with data from add form
1971 $ret = $extrafields->setOptionalsFromPost(null, $object, GETPOST('attribute', 'restricthtml'));
1972 if ($ret < 0) {
1973 $error++;
1974 }
1975
1976 if (!$error) {
1977 // Actions on extra fields
1978 if (!$error) {
1979 $result = $object->insertExtraFields('BILL_SUPPLIER_MODIFY');
1980 if ($result < 0) {
1981 $error++;
1982 }
1983 }
1984 }
1985
1986 if ($error) {
1987 $action = 'edit_extras';
1988 }
1989 }
1990
1991 if (getDolGlobalString('MAIN_DISABLE_CONTACTS_TAB') && $usercancreate) {
1992 if ($action == 'addcontact') {
1993 $result = $object->fetch($id);
1994
1995 if ($result > 0 && $id > 0) {
1996 $contactid = (GETPOST('userid') ? GETPOST('userid') : GETPOST('contactid'));
1997 $typeid = (GETPOST('typecontact') ? GETPOST('typecontact') : GETPOST('type'));
1998 $result = $object->add_contact($contactid, $typeid, GETPOST("source", 'aZ09'));
1999 }
2000
2001 if ($result >= 0) {
2002 header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
2003 exit;
2004 } else {
2005 if ($object->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
2006 $langs->load("errors");
2007 setEventMessages($langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType"), null, 'errors');
2008 } else {
2009 setEventMessages($object->error, $object->errors, 'errors');
2010 }
2011 }
2012 } elseif ($action == 'swapstatut') {
2013 // bascule du statut d'un contact
2014 if ($object->fetch($id)) {
2015 $result = $object->swapContactStatus(GETPOST('ligne', 'int'));
2016 } else {
2017 dol_print_error($db);
2018 }
2019 } elseif ($action == 'deletecontact') {
2020 // Efface un contact
2021 $object->fetch($id);
2022 $result = $object->delete_contact(GETPOST("lineid", 'int'));
2023
2024 if ($result >= 0) {
2025 header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
2026 exit;
2027 } else {
2028 dol_print_error($db);
2029 }
2030 }
2031 }
2032}
2033
2034
2035/*
2036 * View
2037 */
2038
2039$form = new Form($db);
2040$formfile = new FormFile($db);
2041$bankaccountstatic = new Account($db);
2042$paymentstatic = new PaiementFourn($db);
2043if (isModEnabled('project')) {
2044 $formproject = new FormProjets($db);
2045}
2046
2047$now = dol_now();
2048
2049$title = $object->ref." - ".$langs->trans('Card');
2050if ($action == 'create') {
2051 $title = $langs->trans("NewSupplierInvoice");
2052}
2053$help_url = 'EN:Module_Suppliers_Invoices|FR:Module_Fournisseurs_Factures|ES:Módulo_Facturas_de_proveedores|DE:Modul_Lieferantenrechnungen';
2054llxHeader('', $title, $help_url);
2055
2056// Mode creation
2057if ($action == 'create') {
2058 $facturestatic = new FactureFournisseur($db);
2059
2060 print load_fiche_titre($langs->trans('NewSupplierInvoice'), '', 'supplier_invoice');
2061
2063
2064 $currency_code = $conf->currency;
2065
2066 $societe = '';
2067 if (GETPOST('socid', 'int') > 0) {
2068 $societe = new Societe($db);
2069 $societe->fetch(GETPOST('socid', 'int'));
2070 if (isModEnabled("multicurrency") && !empty($societe->multicurrency_code)) {
2071 $currency_code = $societe->multicurrency_code;
2072 }
2073 }
2074
2075 if (!empty($origin) && !empty($originid)) {
2076 // Parse element/subelement (ex: project_task)
2077 $element = $subelement = $origin;
2078
2079 if ($element == 'project') {
2080 $projectid = $originid;
2081 $element = 'projet';
2082 }
2083
2084 // For compatibility
2085 if ($element == 'order') {
2086 $element = $subelement = 'commande';
2087 }
2088 if ($element == 'propal') {
2089 $element = 'comm/propal';
2090 $subelement = 'propal';
2091 }
2092 if ($element == 'contract') {
2093 $element = $subelement = 'contrat';
2094 }
2095 if ($element == 'order_supplier') {
2096 $element = 'fourn';
2097 $subelement = 'fournisseur.commande';
2098 }
2099
2100 require_once DOL_DOCUMENT_ROOT.'/'.$element.'/class/'.$subelement.'.class.php';
2101 $classname = ucfirst($subelement);
2102 if ($classname == 'Fournisseur.commande') {
2103 $classname = 'CommandeFournisseur';
2104 }
2105 $objectsrc = new $classname($db);
2106 $objectsrc->fetch($originid);
2107 $objectsrc->fetch_thirdparty();
2108
2109 $projectid = (!empty($objectsrc->fk_project) ? $objectsrc->fk_project : '');
2110 //$ref_client = (!empty($objectsrc->ref_client)?$object->ref_client:'');
2111 $soc = $objectsrc->thirdparty;
2112
2113 $cond_reglement_id = 0;
2114 $mode_reglement_id = 0;
2115 $fk_account = 0;
2116 //$remise_percent = 0;
2117 //$remise_absolue = 0;
2118 $transport_mode_id = 0;
2119
2120 // set from object source
2121 if (!empty($objectsrc->cond_reglement_id)) {
2122 $cond_reglement_id = $objectsrc->cond_reglement_id;
2123 }
2124 if (!empty($objectsrc->mode_reglement_id)) {
2125 $mode_reglement_id = $objectsrc->mode_reglement_id;
2126 }
2127 if (!empty($objectsrc->fk_account)) {
2128 $fk_account = $objectsrc->fk_account;
2129 }
2130 if (!empty($objectsrc->transport_mode_id)) {
2131 $transport_mode_id = $objectsrc->transport_mode_id;
2132 }
2133
2134 if (empty($cond_reglement_id)
2135 || empty($mode_reglement_id)
2136 || empty($fk_account)
2137 || empty($transport_mode_id)
2138 ) {
2139 if ($origin == 'reception') {
2140 // try to get from source of reception (supplier order)
2141 if (!isset($objectsrc->supplier_order)) {
2142 $objectsrc->fetch_origin();
2143 }
2144
2145 if (!empty($objectsrc->commandeFournisseur)) {
2146 $supplierOrder = $objectsrc->commandeFournisseur;
2147 if (empty($cond_reglement_id) && !empty($supplierOrder->cond_reglement_id)) {
2148 $cond_reglement_id = $supplierOrder->cond_reglement_id;
2149 }
2150 if (empty($mode_reglement_id) && !empty($supplierOrder->mode_reglement_id)) {
2151 $mode_reglement_id = $supplierOrder->mode_reglement_id;
2152 }
2153 if (empty($fk_account) && !empty($supplierOrder->fk_account)) {
2154 $fk_account = $supplierOrder->fk_account;
2155 }
2156 if (empty($transport_mode_id) && !empty($supplierOrder->transport_mode_id)) {
2157 $transport_mode_id = $supplierOrder->transport_mode_id;
2158 }
2159 }
2160 }
2161
2162 // try to get from third-party of source object
2163 if (!empty($soc)) {
2164 if (empty($cond_reglement_id) && !empty($soc->cond_reglement_supplier_id)) {
2165 $cond_reglement_id = $soc->cond_reglement_supplier_id;
2166 }
2167 if (empty($mode_reglement_id) && !empty($soc->mode_reglement_supplier_id)) {
2168 $mode_reglement_id = $soc->mode_reglement_supplier_id;
2169 }
2170 if (empty($fk_account) && !empty($soc->fk_account)) {
2171 $fk_account = $soc->fk_account;
2172 }
2173 if (empty($transport_mode_id) && !empty($soc->transport_mode_id)) {
2174 $transport_mode_id = $soc->transport_mode_id;
2175 }
2176 }
2177 }
2178
2179 if (isModEnabled("multicurrency")) {
2180 if (!empty($objectsrc->multicurrency_code)) {
2181 $currency_code = $objectsrc->multicurrency_code;
2182 }
2183 if (getDolGlobalString('MULTICURRENCY_USE_ORIGIN_TX') && !empty($objectsrc->multicurrency_tx)) {
2184 $currency_tx = $objectsrc->multicurrency_tx;
2185 }
2186 }
2187
2188 $datetmp = dol_mktime(12, 0, 0, GETPOST('remonth', 'int'), GETPOST('reday', 'int'), GETPOST('reyear', 'int'));
2189 $dateinvoice = ($datetmp == '' ? (!getDolGlobalString('MAIN_AUTOFILL_DATE') ? -1 : '') : $datetmp);
2190 $datetmp = dol_mktime(12, 0, 0, GETPOST('echmonth', 'int'), GETPOST('echday', 'int'), GETPOST('echyear', 'int'));
2191 $datedue = ($datetmp == '' ? -1 : $datetmp);
2192
2193 // Replicate extrafields
2194 $objectsrc->fetch_optionals();
2195 $object->array_options = $objectsrc->array_options;
2196 } else {
2197 $cond_reglement_id = !empty($societe->cond_reglement_supplier_id) ? $societe->cond_reglement_supplier_id : 0;
2198 $mode_reglement_id = !empty($societe->mode_reglement_supplier_id) ? $societe->mode_reglement_supplier_id : 0;
2199 $vat_reverse_charge = (empty($societe) ? '' : $societe->vat_reverse_charge);
2200 $transport_mode_id = !empty($societe->transport_mode_supplier_id) ? $societe->transport_mode_supplier_id : 0;
2201 $fk_account = !empty($societe->fk_account) ? $societe->fk_account : 0;
2202 $datetmp = dol_mktime(12, 0, 0, GETPOST('remonth', 'int'), GETPOST('reday', 'int'), GETPOST('reyear', 'int'));
2203 $dateinvoice = ($datetmp == '' ? (getDolGlobalInt('MAIN_AUTOFILL_DATE') ? '' : -1) : $datetmp);
2204 $datetmp = dol_mktime(12, 0, 0, GETPOST('echmonth', 'int'), GETPOST('echday', 'int'), GETPOST('echyear', 'int'));
2205 $datedue = ($datetmp == '' ? -1 : $datetmp);
2206
2207 if (isModEnabled("multicurrency") && !empty($soc->multicurrency_code)) {
2208 $currency_code = $soc->multicurrency_code;
2209 }
2210 }
2211
2212 // when payment condition is empty (means not override by payment condition form a other object, like third-party), try to use default value
2213 if (empty($cond_reglement_id)) {
2214 $cond_reglement_id = GETPOST("cond_reglement_id");
2215 }
2216
2217 // when payment mode is empty (means not override by payment condition form a other object, like third-party), try to use default value
2218 if (empty($mode_reglement_id)) {
2219 $mode_reglement_id = GETPOST("mode_reglement_id");
2220 }
2221
2222 // If form was posted (but error returned), we must reuse the value posted in priority (standard Dolibarr behaviour)
2223 if (!GETPOST('changecompany')) {
2224 if (GETPOSTISSET('cond_reglement_id')) {
2225 $cond_reglement_id = GETPOST('cond_reglement_id', 'int');
2226 }
2227 if (GETPOSTISSET('mode_reglement_id')) {
2228 $mode_reglement_id = GETPOST('mode_reglement_id', 'int');
2229 }
2230 if (GETPOSTISSET('cond_reglement_id')) {
2231 $fk_account = GETPOST('fk_account', 'int');
2232 }
2233 }
2234
2235 $note_public = $object->getDefaultCreateValueFor('note_public', ((!empty($origin) && !empty($originid) && is_object($objectsrc) && getDolGlobalString('FACTUREFOURN_REUSE_NOTES_ON_CREATE_FROM')) ? $objectsrc->note_public : null));
2236 $note_private = $object->getDefaultCreateValueFor('note_private', ((!empty($origin) && !empty($originid) && is_object($objectsrc) && getDolGlobalString('FACTUREFOURN_REUSE_NOTES_ON_CREATE_FROM')) ? $objectsrc->note_private : null));
2237
2238 if ($origin == 'contrat') {
2239 $langs->load("admin");
2240 $text = $langs->trans("ToCreateARecurringInvoice");
2241 $text .= ' '.$langs->trans("ToCreateARecurringInvoiceGene", $langs->transnoentitiesnoconv("MenuFinancial"), $langs->transnoentitiesnoconv("SupplierBills"), $langs->transnoentitiesnoconv("ListOfTemplates"));
2242 if (!getDolGlobalString('INVOICE_DISABLE_AUTOMATIC_RECURRING_INVOICE')) {
2243 $text .= ' '.$langs->trans("ToCreateARecurringInvoiceGeneAuto", $langs->transnoentitiesnoconv('Module2300Name'));
2244 }
2245 print info_admin($text, 0, 0, 0, 'opacitymedium').'<br>';
2246 }
2247
2248 print '<form name="add" action="'.$_SERVER["PHP_SELF"].'" method="post">';
2249 print '<input type="hidden" name="token" value="'.newToken().'">';
2250 print '<input type="hidden" name="action" value="add">';
2251 print '<input type="hidden" name="changecompany" value="0">'; // will be set to 1 by javascript so we know post is done after a company change
2252
2253 if (!empty($societe->id) && $societe->id > 0) {
2254 print '<input type="hidden" name="socid" value="'.$societe->id.'">'."\n";
2255 }
2256 print '<input type="hidden" name="origin" value="'.$origin.'">';
2257 print '<input type="hidden" name="originid" value="'.$originid.'">';
2258 if (!empty($currency_tx)) {
2259 print '<input type="hidden" name="originmulticurrency_tx" value="'.$currency_tx.'">';
2260 }
2261 print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
2262
2263 print dol_get_fiche_head();
2264
2265 // Call Hook tabContentCreateSupplierInvoice
2266 $parameters = array();
2267 // Note that $action and $object may be modified by hook
2268 $reshook = $hookmanager->executeHooks('tabContentCreateSupplierInvoice', $parameters, $object, $action);
2269 if (empty($reshook)) {
2270 print '<table class="border centpercent">';
2271
2272 // Ref
2273 print '<tr><td class="titlefieldcreate">'.$langs->trans('Ref').'</td><td>'.$langs->trans('Draft').'</td></tr>';
2274
2275 $exampletemplateinvoice = new FactureFournisseurRec($db);
2276 $invoice_predefined = new FactureFournisseurRec($db);
2277 if (empty($origin) && empty($originid) && $fac_recid > 0) {
2278 $invoice_predefined->fetch($fac_recid);
2279 }
2280
2281 // Third party
2282 print '<tr><td class="fieldrequired">'.$langs->trans('Supplier').'</td>';
2283 print '<td>';
2284
2285 if (!empty($societe->id) && $societe->id > 0 && ($fac_recid <= 0 || !empty($invoice_predefined->frequency))) {
2286 $absolute_discount = $societe->getAvailableDiscounts('', '', 0, 1);
2287 print $societe->getNomUrl(1, 'supplier');
2288 print '<input type="hidden" name="socid" value="'.$societe->id.'">';
2289 } else {
2290 $filter = '((s.fournisseur:=:1) AND (s.status:=:1))';
2291 print img_picto('', 'company', 'class="pictofixedwidth"').$form->select_company(empty($societe->id) ? 0 : $societe->id, 'socid', $filter, 'SelectThirdParty', 1, 0, null, 0, 'minwidth175 widthcentpercentminusxx maxwidth500');
2292 // reload page to retrieve supplier informations
2293 if (!getDolGlobalString('RELOAD_PAGE_ON_SUPPLIER_CHANGE_DISABLED')) {
2294 print '<script type="text/javascript">
2295 $(document).ready(function() {
2296 $("#socid").change(function() {
2297 console.log("We have changed the company - Reload page");
2298 // reload page
2299 $("input[name=action]").val("create");
2300 $("input[name=changecompany]").val("1");
2301 $("form[name=add]").submit();
2302 });
2303 });
2304 </script>';
2305 }
2306 if ($fac_recid <= 0) {
2307 print ' <a href="'.DOL_URL_ROOT.'/societe/card.php?action=create&client=0&fournisseur=1&backtopage='.urlencode($_SERVER["PHP_SELF"].'?action=create').'"><span class="fa fa-plus-circle valignmiddle paddingleft" title="'.$langs->trans("AddThirdParty").'"></span></a>';
2308 }
2309 }
2310 print '</td></tr>';
2311
2312 // Overwrite some values if creation of invoice is from a predefined invoice
2313 if (empty($origin) && empty($originid) && $fac_recid > 0) {
2314 $invoice_predefined->fetch($fac_recid);
2315
2316 $dateinvoice = $invoice_predefined->date_when; // To use next gen date by default later
2317 if (empty($projectid)) {
2318 $projectid = $invoice_predefined->fk_project;
2319 }
2320 $cond_reglement_id = $invoice_predefined->cond_reglement_id;
2321 $mode_reglement_id = $invoice_predefined->mode_reglement_id;
2322 $fk_account = $invoice_predefined->fk_account;
2323 $note_public = $invoice_predefined->note_public;
2324 $note_private = $invoice_predefined->note_private;
2325
2326 if (!empty($invoice_predefined->multicurrency_code)) {
2327 $currency_code = $invoice_predefined->multicurrency_code;
2328 }
2329 if (!empty($invoice_predefined->multicurrency_tx)) {
2330 $currency_tx = $invoice_predefined->multicurrency_tx;
2331 }
2332
2333 $sql = 'SELECT r.rowid, r.titre as title, r.total_ttc';
2334 $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_fourn_rec as r';
2335 $sql .= ' WHERE r.fk_soc = '. (int) $invoice_predefined->socid;
2336
2337 $resql = $db->query($sql);
2338 if ($resql) {
2339 $num = $db->num_rows($resql);
2340 $i = 0;
2341
2342 if ($num > 0) {
2343 print '<tr><td>'.$langs->trans('CreateFromRepeatableInvoice').'</td><td>';
2344 //print '<input type="hidden" name="fac_rec" id="fac_rec" value="'.$fac_recid.'">';
2345 print '<select class="flat" id="fac_rec" name="fac_rec">'; // We may want to change the template to use
2346 print '<option value="0" selected></option>';
2347 while ($i < $num) {
2348 $objp = $db->fetch_object($resql);
2349 print '<option value="'.$objp->rowid.'"';
2350 if ($fac_recid == $objp->rowid) {
2351 print ' selected';
2352 $exampletemplateinvoice->fetch($fac_recid);
2353 }
2354 print '>'.$objp->title.' ('.price($objp->total_ttc).' '.$langs->trans("TTC").')</option>';
2355 $i++;
2356 }
2357 print '</select>';
2358 // Option to reload page to retrieve customer informations. Note, this clear other input
2359 if (!getDolGlobalString('RELOAD_PAGE_ON_TEMPLATE_CHANGE_DISABLED')) {
2360 print '<script type="text/javascript">
2361 $(document).ready(function() {
2362 $("#fac_rec").change(function() {
2363 console.log("We have changed the template invoice - Reload page");
2364 // reload page
2365 $("input[name=action]").val("create");
2366 $("form[name=add]").submit();
2367 });
2368 });
2369 </script>';
2370 }
2371 print '</td></tr>';
2372 }
2373 $db->free($resql);
2374 } else {
2375 dol_print_error($db);
2376 }
2377 }
2378
2379 // Ref supplier
2380 print '<tr><td class="fieldrequired">'.$langs->trans('RefSupplierBill').'</td><td><input name="ref_supplier" value="'.(GETPOSTISSET('ref_supplier') ? GETPOST('ref_supplier') : (!empty($objectsrc->ref_supplier) ? $objectsrc->ref_supplier : '')).'" type="text"';
2381 if (!empty($societe->id) && $societe->id > 0) {
2382 print ' autofocus';
2383 }
2384 print '></td>';
2385 print '</tr>';
2386
2387 print '<tr><td class="tdtop fieldrequired">'.$langs->trans('Type').'</td><td>';
2388
2389 print '<div class="tagtable">'."\n";
2390
2391 // Standard invoice
2392 print '<div class="tagtr listofinvoicetype"><div class="tagtd listofinvoicetype">';
2393 $tmp = '<input type="radio" id="radio_standard" name="type" value="0"'.(GETPOST('type', 'int') ? '' : 'checked').'> ';
2394 $desc = $form->textwithpicto($tmp.'<label for="radio_standard">'.$langs->trans("InvoiceStandardAsk").'</label>', $langs->transnoentities("InvoiceStandardDesc"), 1, 'help', '', 0, 3);
2395 print $desc;
2396 print '</div></div>';
2397
2398 if (empty($origin) || ($origin == 'order_supplier' && !empty($originid))) {
2399 // Deposit - Down payment
2400 if (!getDolGlobalString('INVOICE_DISABLE_DEPOSIT')) {
2401 print '<div class="tagtr listofinvoicetype"><div class="tagtd listofinvoicetype">';
2402 $tmp='<input type="radio" id="radio_deposit" name="type" value="3"' . (GETPOST('type') == 3 ? ' checked' : '') . '> ';
2403 print '<script type="text/javascript">
2404 jQuery(document).ready(function() {
2405 jQuery("#typestandardinvoice, #valuestandardinvoice").click(function() {
2406 jQuery("#radio_standard").prop("checked", true);
2407 });
2408 jQuery("#typedeposit, #valuedeposit").click(function() {
2409 jQuery("#radio_deposit").prop("checked", true);
2410 });
2411 jQuery("#typedeposit").change(function() {
2412 console.log("We change type of down payment");
2413 jQuery("#radio_deposit").prop("checked", true);
2414 setRadioForTypeOfInvoice();
2415 });
2416 jQuery("#radio_standard, #radio_deposit, #radio_replacement, #radio_template").change(function() {
2417 setRadioForTypeOfInvoice();
2418 });
2419 function setRadioForTypeOfInvoice() {
2420 console.log("Change radio");
2421 if (jQuery("#radio_deposit").prop("checked") && (jQuery("#typedeposit").val() == \'amount\' || jQuery("#typedeposit").val() == \'variable\')) {
2422 jQuery(".checkforselect").prop("disabled", true);
2423 jQuery(".checkforselect").prop("checked", false);
2424 } else {
2425 jQuery(".checkforselect").prop("disabled", false);
2426 jQuery(".checkforselect").prop("checked", true);
2427 }
2428 }
2429 });
2430 </script>';
2431
2432 $tmp = $tmp.'<label for="radio_deposit" >'.$langs->trans("InvoiceDeposit").'</label>';
2433 $desc = $form->textwithpicto($tmp, $langs->transnoentities("InvoiceDepositDesc"), 1, 'help', '', 0, 3);
2434 print '<table class="nobordernopadding"><tr>';
2435 print '<td>';
2436 print $desc;
2437 print '</td>';
2438 if ($origin == 'order_supplier') {
2439 print '<td class="nowrap" style="padding-left: 15px">';
2440 $arraylist = array(
2441 'amount' => $langs->transnoentitiesnoconv('FixAmount', $langs->transnoentitiesnoconv('Deposit')),
2442 'variable' => $langs->transnoentitiesnoconv('VarAmountOneLine', $langs->transnoentitiesnoconv('Deposit')),
2443 'variablealllines' => $langs->transnoentitiesnoconv('VarAmountAllLines')
2444 );
2445 print $form->selectarray('typedeposit', $arraylist, GETPOST('typedeposit', 'aZ09'), 0, 0, 0, '', 1);
2446 print '</td>';
2447 print '<td class="nowrap" style="padding-left: 5px">';
2448 print '<span class="opacitymedium paddingleft">'.$langs->trans("AmountOrPercent").'</span><input type="text" id="valuedeposit" name="valuedeposit" class="width75 right" value="' . GETPOST('valuedeposit', 'int') . '"/>';
2449 print '</td>';
2450 }
2451 print '</tr></table>';
2452
2453 print '</div></div>';
2454 }
2455 }
2456
2457 /* Not yet supported for supplier
2458 if ($societe->id > 0)
2459 {
2460 // Replacement
2461 if (empty($conf->global->INVOICE_DISABLE_REPLACEMENT))
2462 {
2463 // Type invoice
2464 $facids = $facturestatic->list_replacable_supplier_invoices($societe->id);
2465 if ($facids < 0) {
2466 dol_print_error($db, $facturestatic->error, $facturestatic->errors);
2467 exit();
2468 }
2469 $options = "";
2470 foreach ($facids as $facparam)
2471 {
2472 $options .= '<option value="' . $facparam ['id'] . '"';
2473 if ($facparam ['id'] == GETPOST('fac_replacement') {
2474 $options .= ' selected';
2475 }
2476 $options .= '>' . $facparam ['ref'];
2477 $options .= ' (' . $facturestatic->LibStatut(0, $facparam ['status']) . ')';
2478 $options .= '</option>';
2479 }
2480
2481 print '<!-- replacement line -->';
2482 print '<div class="tagtr listofinvoicetype"><div class="tagtd listofinvoicetype">';
2483 $tmp='<input type="radio" name="type" id="radio_replacement" value="1"' . (GETPOST('type') == 1 ? ' checked' : '');
2484 if (! $options) $tmp.=' disabled';
2485 $tmp.='> ';
2486 print '<script type="text/javascript">
2487 jQuery(document).ready(function() {
2488 jQuery("#fac_replacement").change(function() {
2489 jQuery("#radio_replacement").prop("checked", true);
2490 });
2491 });
2492 </script>';
2493 $text = $tmp.$langs->trans("InvoiceReplacementAsk") . ' ';
2494 $text .= '<select class="flat" name="fac_replacement" id="fac_replacement"';
2495 if (! $options)
2496 $text .= ' disabled';
2497 $text .= '>';
2498 if ($options) {
2499 $text .= '<option value="-1">&nbsp;</option>';
2500 $text .= $options;
2501 } else {
2502 $text .= '<option value="-1">' . $langs->trans("NoReplacableInvoice") . '</option>';
2503 }
2504 $text .= '</select>';
2505 $desc = $form->textwithpicto($text, $langs->transnoentities("InvoiceReplacementDesc"), 1, 'help', '', 0, 3);
2506 print $desc;
2507 print '</div></div>';
2508 }
2509 }
2510 else
2511 {
2512 print '<div class="tagtr listofinvoicetype"><div class="tagtd listofinvoicetype">';
2513 $tmp='<input type="radio" name="type" id="radio_replacement" value="0" disabled> ';
2514 $text = $tmp.$langs->trans("InvoiceReplacement") . ' ';
2515 $text.= '('.$langs->trans("YouMustCreateInvoiceFromSupplierThird").') ';
2516 $desc = $form->textwithpicto($text, $langs->transnoentities("InvoiceReplacementDesc"), 1, 'help', '', 0, 3);
2517 print $desc;
2518 print '</div></div>';
2519 }
2520 */
2521
2522 if (empty($origin)) {
2523 if (!empty($societe->id) && $societe->id > 0) {
2524 // Credit note
2525 if (!getDolGlobalString('INVOICE_DISABLE_CREDIT_NOTE')) {
2526 // Show link for credit note
2527 $facids = $facturestatic->list_qualified_avoir_supplier_invoices($societe->id);
2528 if ($facids < 0) {
2529 dol_print_error($db, $facturestatic->error, $facturestatic->errors);
2530 exit;
2531 }
2532 $optionsav = "";
2533 $newinvoice_static = new FactureFournisseur($db);
2534 foreach ($facids as $key => $valarray) {
2535 $newinvoice_static->id = $key;
2536 $newinvoice_static->ref = $valarray ['ref'];
2537 $newinvoice_static->statut = $valarray ['status'];
2538 $newinvoice_static->type = $valarray ['type'];
2539 $newinvoice_static->paye = $valarray ['paye'];
2540
2541 $optionsav .= '<option value="'.$key.'"';
2542 if ($key == GETPOST('fac_avoir', 'int')) {
2543 $optionsav .= ' selected';
2544 }
2545 $optionsav .= '>';
2546 $optionsav .= $newinvoice_static->ref;
2547 $optionsav .= ' ('.$newinvoice_static->getLibStatut(1, $valarray ['paymentornot']).')';
2548 $optionsav .= '</option>';
2549 }
2550
2551 print '<div class="tagtr listofinvoicetype"><div class="tagtd listofinvoicetype">';
2552 $tmp = '<input type="radio" id="radio_creditnote" name="type" value="2"'.(GETPOST('type') == 2 ? ' checked' : '');
2553 if (!$optionsav && !getDolGlobalString('INVOICE_CREDIT_NOTE_STANDALONE')) {
2554 $tmp .= ' disabled';
2555 }
2556 $tmp .= '> ';
2557 // Show credit note options only if we checked credit note
2558 print '<script type="text/javascript">
2559 jQuery(document).ready(function() {
2560 if (! jQuery("#radio_creditnote").is(":checked"))
2561 {
2562 jQuery("#credit_note_options").hide();
2563 }
2564 jQuery("#radio_creditnote").click(function() {
2565 jQuery("#credit_note_options").show();
2566 });
2567 jQuery("#radio_standard, #radio_replacement, #radio_deposit").click(function() {
2568 jQuery("#credit_note_options").hide();
2569 });
2570 });
2571 </script>';
2572 $text = $tmp.'<label for="radio_creditnote">'.$langs->transnoentities("InvoiceAvoirAsk").'</label> ';
2573 // $text.='<input type="text" value="">';
2574 $text .= '<select class="flat valignmiddle" name="fac_avoir" id="fac_avoir"';
2575 if (!$optionsav) {
2576 $text .= ' disabled';
2577 }
2578 $text .= '>';
2579 if ($optionsav) {
2580 $text .= '<option value="-1"></option>';
2581 $text .= $optionsav;
2582 } else {
2583 $text .= '<option value="-1">'.$langs->trans("NoInvoiceToCorrect").'</option>';
2584 }
2585 $text .= '</select>';
2586 $desc = $form->textwithpicto($text, $langs->transnoentities("InvoiceAvoirDesc"), 1, 'help', '', 0, 3);
2587 print $desc;
2588
2589 print '<div id="credit_note_options" class="clearboth">';
2590 print '&nbsp;&nbsp;&nbsp; <input type="checkbox" name="invoiceAvoirWithLines" id="invoiceAvoirWithLines" value="1" onclick="if($(this).is(\':checked\') ) { $(\'#radio_creditnote\').prop(\'checked\', true); $(\'#invoiceAvoirWithPaymentRestAmount\').removeAttr(\'checked\'); }" '.(GETPOST('invoiceAvoirWithLines', 'int') > 0 ? 'checked' : '').' /> ';
2591 print '<label for="invoiceAvoirWithLines">'.$langs->trans('invoiceAvoirWithLines')."</label>";
2592 print '<br>&nbsp;&nbsp;&nbsp; <input type="checkbox" name="invoiceAvoirWithPaymentRestAmount" id="invoiceAvoirWithPaymentRestAmount" value="1" onclick="if($(this).is(\':checked\') ) { $(\'#radio_creditnote\').prop(\'checked\', true); $(\'#invoiceAvoirWithLines\').removeAttr(\'checked\'); }" '.(GETPOST('invoiceAvoirWithPaymentRestAmount', 'int') > 0 ? 'checked' : '').' /> ';
2593 print '<label for="invoiceAvoirWithPaymentRestAmount">'.$langs->trans('invoiceAvoirWithPaymentRestAmount')."</label>";
2594 print '</div>';
2595
2596 print '</div></div>';
2597 }
2598 } else {
2599 print '<div class="tagtr listofinvoicetype"><div class="tagtd listofinvoicetype">';
2600 if (!getDolGlobalString('INVOICE_CREDIT_NOTE_STANDALONE')) {
2601 $tmp = '<input type="radio" name="type" id="radio_creditnote" value="0" disabled> ';
2602 } else {
2603 $tmp='<input type="radio" name="type" id="radio_creditnote" value="2"> ';
2604 }
2605 $text = $tmp.$langs->trans("InvoiceAvoir").' ';
2606 $text .= '<span class="opacitymedium">('.$langs->trans("YouMustCreateInvoiceFromSupplierThird").')</span> ';
2607 $desc = $form->textwithpicto($text, $langs->transnoentities("InvoiceAvoirDesc"), 1, 'help', '', 0, 3);
2608 print $desc;
2609 print '</div></div>'."\n";
2610 }
2611 }
2612
2613 print '</div>';
2614
2615 print '</td></tr>';
2616
2617
2618 // Invoice Subtype
2619 if (getDolGlobalInt('INVOICE_SUBTYPE_ENABLED')) {
2620 print '<tr><td class="fieldrequired">'.$langs->trans('InvoiceSubtype').'</td><td colspan="2">';
2621 print $form->getSelectInvoiceSubtype(GETPOST('subtype'), 'subtype', 1, 0, '');
2622 print '</td></tr>';
2623 }
2624
2625 if (!empty($societe->id) && $societe->id > 0) {
2626 // Discounts for third party
2627 print '<tr><td>'.$langs->trans('Discounts').'</td><td>';
2628
2629 $thirdparty = $societe;
2630 $discount_type = 1;
2631 $backtopage = urlencode($_SERVER["PHP_SELF"].'?socid='.$societe->id.'&action='.$action.'&origin='.GETPOST('origin').'&originid='.GETPOST('originid'));
2632 include DOL_DOCUMENT_ROOT.'/core/tpl/object_discounts.tpl.php';
2633
2634 print '</td></tr>';
2635 }
2636
2637 // Label
2638 print '<tr><td>'.$langs->trans('Label').'</td><td><input class="minwidth200" name="label" value="'.dol_escape_htmltag(GETPOST('label')).'" type="text"></td></tr>';
2639
2640 // Date invoice
2641 print '<tr><td class="fieldrequired">'.$langs->trans('DateInvoice').'</td><td>';
2642 print img_picto('', 'action', 'class="pictofixedwidth"');
2643 print $form->selectDate($dateinvoice, '', '', '', '', "add", 1, 1);
2644 print '</td></tr>';
2645
2646 // Payment term
2647 print '<tr><td class="nowrap">'.$langs->trans('PaymentConditionsShort').'</td><td>';
2648 print img_picto('', 'payment', 'class="pictofixedwidth"');
2649 print $form->getSelectConditionsPaiements($cond_reglement_id, 'cond_reglement_id', -1, 1);
2650
2651 print '</td></tr>';
2652
2653 // Due date
2654 print '<tr><td>'.$langs->trans('DateMaxPayment').'</td><td>';
2655 print img_picto('', 'action', 'class="pictofixedwidth"');
2656 print $form->selectDate($datedue, 'ech', '', '', '', "add", 1, 1);
2657 print '</td></tr>';
2658
2659 // Payment mode
2660 print '<tr><td>'.$langs->trans('PaymentMode').'</td><td>';
2661 print img_picto('', 'bank', 'class="pictofixedwidth"');
2662 $form->select_types_paiements($mode_reglement_id, 'mode_reglement_id', 'DBIT', 0, 1, 0, 0, 1, 'maxwidth200 widthcentpercentminusx');
2663 print '</td></tr>';
2664
2665 // Bank Account
2666 if (isModEnabled("banque")) {
2667 print '<tr><td>'.$langs->trans('BankAccount').'</td><td>';
2668 // when bank account is empty (means not override by payment mode form a other object, like third-party), try to use default value
2669 print img_picto('', 'bank_account', 'class="pictofixedwidth"').$form->select_comptes($fk_account, 'fk_account', 0, '', 1, '', 0, 'maxwidth200 widthcentpercentminusx', 1);
2670 print '</td></tr>';
2671 }
2672
2673 // Project
2674 if (isModEnabled('project')) {
2675 $formproject = new FormProjets($db);
2676
2677 $langs->load('projects');
2678 print '<tr><td>'.$langs->trans('Project').'</td><td>';
2679 print img_picto('', 'project', 'class="pictofixedwidth"').$formproject->select_projects((!getDolGlobalString('PROJECT_CAN_ALWAYS_LINK_TO_ALL_SUPPLIERS') ? $societe->id : -1), $projectid, 'projectid', 0, 0, 1, 1, 0, 0, 0, '', 1, 0, 'maxwidth500 widthcentpercentminusxx');
2680 print ' <a href="'.DOL_URL_ROOT.'/projet/card.php?socid='.(!empty($soc->id) ? $soc->id : 0).'&action=create&status=1&backtopage='.urlencode($_SERVER["PHP_SELF"].'?action=create&socid='.(!empty($soc->id) ? $soc->id : 0).($fac_recid > 0 ? '&fac_rec='.$fac_recid : '')).'"><span class="fa fa-plus-circle valignmiddle" title="'.$langs->trans("AddProject").'"></span></a>';
2681 print '</td></tr>';
2682 }
2683
2684 // Incoterms
2685 if (isModEnabled('incoterm')) {
2686 print '<tr>';
2687 print '<td><label for="incoterm_id">'.$form->textwithpicto($langs->trans("IncotermLabel"), !empty($objectsrc->label_incoterms) ? $objectsrc->label_incoterms : '', 1).'</label></td>';
2688 print '<td colspan="3" class="maxwidthonsmartphone">';
2689 print img_picto('', 'incoterm', 'class="pictofixedwidth"');
2690 print $form->select_incoterms(GETPOSTISSET('incoterm_id') ? GETPOST('incoterm_id', 'alphanohtml') : (!empty($objectsrc->fk_incoterms) ? $objectsrc->fk_incoterms : ''), GETPOSTISSET('location_incoterms') ? GETPOST('location_incoterms', 'alphanohtml') : (!empty($objectsrc->location_incoterms) ? $objectsrc->location_incoterms : ''));
2691 print '</td></tr>';
2692 }
2693
2694 // Vat reverse-charge by default
2695 if (getDolGlobalString('ACCOUNTING_FORCE_ENABLE_VAT_REVERSE_CHARGE')) {
2696 require_once DOL_DOCUMENT_ROOT . '/core/lib/company.lib.php';
2697 print '<tr><td>' . $langs->trans('VATReverseCharge') . '</td><td>';
2698 // Try to propose to use VAT reverse charge even if the VAT reverse charge is not activated in the supplier card, if this corresponds to the context of use, the activation is proposed
2699 if (GETPOSTISSET('vat_reverse_charge')) { // Check if form was submitted previously
2700 $vat_reverse_charge = (GETPOST('vat_reverse_charge', 'alpha') == 'on' || GETPOST('vat_reverse_charge', 'alpha') == '1') ? 1 : 0;
2701 } elseif ($vat_reverse_charge == 1 || $societe->vat_reverse_charge == 1 || ($societe->country_code != 'FR' && isInEEC($societe) && !empty($societe->tva_intra))) {
2702 $vat_reverse_charge = 1;
2703 } else {
2704 $vat_reverse_charge = 0;
2705 }
2706
2707 print '<input type="checkbox" name="vat_reverse_charge"'. (!empty($vat_reverse_charge) ? ' checked ' : '') . '>';
2708 print '</td></tr>';
2709 }
2710
2711 // Multicurrency
2712 if (isModEnabled("multicurrency")) {
2713 print '<tr>';
2714 print '<td>'.$form->editfieldkey('Currency', 'multicurrency_code', '', $object, 0).'</td>';
2715 print '<td class="maxwidthonsmartphone">';
2716 print img_picto('', 'currency', 'class="pictofixedwidth"');
2717 $used_currency_code = $currency_code;
2718 if (!GETPOST('changecompany')) {
2719 $used_currency_code = GETPOSTISSET('multicurrency_code') ? GETPOST('multicurrency_code', 'alpha') : $currency_code;
2720 }
2721 print $form->selectMultiCurrency($used_currency_code, 'multicurrency_code');
2722 print '</td></tr>';
2723 }
2724
2725 // Help of substitution key
2726 $htmltext = '';
2727 if ($fac_recid > 0) {
2728 $dateexample = $dateinvoice;
2729 if (empty($dateexample)) {
2730 $dateexample = dol_now();
2731 }
2732 $substitutionarray = array(
2733 '__TOTAL_HT__' => $langs->trans("AmountHT").' ('.$langs->trans("Example").': '.price($exampletemplateinvoice->total_ht).')',
2734 '__TOTAL_TTC__' => $langs->trans("AmountTTC").' ('.$langs->trans("Example").': '.price($exampletemplateinvoice->total_ttc).')',
2735 '__INVOICE_PREVIOUS_MONTH__' => $langs->trans("PreviousMonthOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree($dateexample, -1, 'm'), '%m').')',
2736 '__INVOICE_MONTH__' => $langs->trans("MonthOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date($dateexample, '%m').')',
2737 '__INVOICE_NEXT_MONTH__' => $langs->trans("NextMonthOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree($dateexample, 1, 'm'), '%m').')',
2738 '__INVOICE_PREVIOUS_MONTH_TEXT__' => $langs->trans("TextPreviousMonthOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree($dateexample, -1, 'm'), '%B').')',
2739 '__INVOICE_MONTH_TEXT__' => $langs->trans("TextMonthOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date($dateexample, '%B').')',
2740 '__INVOICE_NEXT_MONTH_TEXT__' => $langs->trans("TextNextMonthOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree($dateexample, 1, 'm'), '%B').')',
2741 '__INVOICE_PREVIOUS_YEAR__' => $langs->trans("PreviousYearOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree($dateexample, -1, 'y'), '%Y').')',
2742 '__INVOICE_YEAR__' => $langs->trans("YearOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date($dateexample, '%Y').')',
2743 '__INVOICE_NEXT_YEAR__' => $langs->trans("NextYearOfInvoice").' ('.$langs->trans("Example").': '.dol_print_date(dol_time_plus_duree($dateexample, 1, 'y'), '%Y').')'
2744 );
2745
2746 $htmltext = '<i>'.$langs->trans("FollowingConstantsWillBeSubstituted").':<br>';
2747 foreach ($substitutionarray as $key => $val) {
2748 $htmltext .= $key.' = '.$langs->trans($val).'<br>';
2749 }
2750 $htmltext .= '</i>';
2751 }
2752
2753 // Intracomm report
2754 if (isModEnabled('intracommreport')) {
2755 $langs->loadLangs(array("intracommreport"));
2756 print '<!-- If module intracomm on -->'."\n";
2757 print '<tr><td>'.$langs->trans('IntracommReportTransportMode').'</td><td>';
2758 $form->selectTransportMode(GETPOSTISSET('transport_mode_id') ? GETPOST('transport_mode_id') : $transport_mode_id, 'transport_mode_id');
2759 print '</td></tr>';
2760 }
2761
2762 if (empty($reshook)) {
2763 print $object->showOptionals($extrafields, 'create');
2764 }
2765
2766 // Public note
2767 print '<tr><td>'.$langs->trans('NotePublic').'</td>';
2768 print '<td>';
2769 $doleditor = new DolEditor('note_public', (GETPOSTISSET('note_public') ? GETPOST('note_public', 'restricthtml') : $note_public), '', 80, 'dolibarr_notes', 'In', 0, false, !getDolGlobalString('FCKEDITOR_ENABLE_NOTE_PUBLIC') ? 0 : 1, ROWS_3, '90%');
2770 print $doleditor->Create(1);
2771 print '</td>';
2772 // print '<td><textarea name="note" wrap="soft" cols="60" rows="'.ROWS_5.'"></textarea></td>';
2773 print '</tr>';
2774
2775 // Private note
2776 print '<tr><td>'.$langs->trans('NotePrivate').'</td>';
2777 print '<td>';
2778 $doleditor = new DolEditor('note_private', (GETPOSTISSET('note_private') ? GETPOST('note_private', 'restricthtml') : $note_private), '', 80, 'dolibarr_notes', 'In', 0, false, !getDolGlobalString('FCKEDITOR_ENABLE_NOTE_PRIVATE') ? 0 : 1, ROWS_3, '90%');
2779 print $doleditor->Create(1);
2780 print '</td>';
2781 // print '<td><textarea name="note" wrap="soft" cols="60" rows="'.ROWS_5.'"></textarea></td>';
2782 print '</tr>';
2783
2784
2785 if (!empty($objectsrc) && is_object($objectsrc)) {
2786 print "\n<!-- ".$classname." info -->";
2787 print "\n";
2788 print '<input type="hidden" name="amount" value="'.$objectsrc->total_ht.'">'."\n";
2789 print '<input type="hidden" name="total" value="'.$objectsrc->total_ttc.'">'."\n";
2790 print '<input type="hidden" name="tva" value="'.$objectsrc->total_tva.'">'."\n";
2791 print '<input type="hidden" name="origin" value="'.$objectsrc->element.'">';
2792 print '<input type="hidden" name="originid" value="'.$objectsrc->id.'">';
2793
2794 $txt = $langs->trans($classname);
2795 if ($classname == 'CommandeFournisseur') {
2796 $langs->load('orders');
2797 $txt = $langs->trans("SupplierOrder");
2798 }
2799 print '<tr><td>'.$txt.'</td><td>'.$objectsrc->getNomUrl(1);
2800 // We check if Origin document (id and type is known) has already at least one invoice attached to it
2801 $objectsrc->fetchObjectLinked($originid, $origin, '', 'invoice_supplier');
2802
2803 $invoice_supplier = $objectsrc->linkedObjects['invoice_supplier'];
2804
2805 // count function need a array as argument (Note: the array must implement Countable too)
2806 if (is_array($invoice_supplier)) {
2807 $cntinvoice = count($invoice_supplier);
2808
2809 if ($cntinvoice >= 1) {
2810 setEventMessages('WarningBillExist', null, 'warnings');
2811 echo ' ('.$langs->trans('LatestRelatedBill').end($invoice_supplier)->getNomUrl(1).')';
2812 }
2813 }
2814
2815 print '</td></tr>';
2816 print '<tr><td>'.$langs->trans('AmountHT').'</td><td>'.price($objectsrc->total_ht).'</td></tr>';
2817 print '<tr><td>'.$langs->trans('AmountVAT').'</td><td>'.price($objectsrc->total_tva)."</td></tr>";
2818 if ($mysoc->localtax1_assuj == "1" || $object->total_localtax1 != 0) { //Localtax1
2819 print '<tr><td>'.$langs->transcountry("AmountLT1", $mysoc->country_code).'</td><td>'.price($objectsrc->total_localtax1)."</td></tr>";
2820 }
2821
2822 if ($mysoc->localtax2_assuj == "1" || $object->total_localtax2 != 0) { //Localtax2
2823 print '<tr><td>'.$langs->transcountry("AmountLT2", $mysoc->country_code).'</td><td>'.price($objectsrc->total_localtax2)."</td></tr>";
2824 }
2825 print '<tr><td>'.$langs->trans('AmountTTC').'</td><td>'.price($objectsrc->total_ttc)."</td></tr>";
2826
2827 if (isModEnabled("multicurrency")) {
2828 print '<tr><td>'.$langs->trans('MulticurrencyAmountHT').'</td><td>'.price($objectsrc->multicurrency_total_ht).'</td></tr>';
2829 print '<tr><td>'.$langs->trans('MulticurrencyAmountVAT').'</td><td>'.price($objectsrc->multicurrency_total_tva)."</td></tr>";
2830 print '<tr><td>'.$langs->trans('MulticurrencyAmountTTC').'</td><td>'.price($objectsrc->multicurrency_total_ttc)."</td></tr>";
2831 }
2832 }
2833
2834 // Other options
2835 $parameters = array();
2836 $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
2837 print $hookmanager->resPrint;
2838
2839
2840 print "</table>\n";
2841 }
2842
2843 print dol_get_fiche_end();
2844
2845 print $form->buttonsSaveCancel("CreateDraft");
2846
2847 // Show origin lines
2848 if (!empty($objectsrc) && is_object($objectsrc)) {
2849 print '<br>';
2850
2851 $title = $langs->trans('ProductsAndServices');
2852 print load_fiche_titre($title);
2853
2854 print '<div class="div-table-responsive-no-min">';
2855 print '<table class="noborder centpercent">';
2856
2857 $objectsrc->printOriginLinesList('', $selectedLines);
2858
2859 print '</table>';
2860 print '</div>';
2861 }
2862
2863 print "</form>\n";
2864} else {
2865 if ($id > 0 || !empty($ref)) {
2866 //
2867 // View or edit mode
2868 //
2869 $now = dol_now();
2870
2871 $productstatic = new Product($db);
2872
2873 $result = $object->fetch($id, $ref);
2874 if ($result <= 0) {
2875 $langs->load("errors");
2876 print $langs->trans("ErrorRecordNotFound");
2877 llxFooter();
2878 $db->close();
2879 exit;
2880 }
2881
2882 $result = $object->fetch_thirdparty();
2883 if ($result < 0) {
2884 dol_print_error($db, $object->error, $object->errors);
2885 exit;
2886 }
2887
2888 $societe = $object->thirdparty;
2889
2890 $totalpaid = $object->getSommePaiement();
2891 $totalcreditnotes = $object->getSumCreditNotesUsed();
2892 $totaldeposits = $object->getSumDepositsUsed();
2893 // print "totalpaid=".$totalpaid." totalcreditnotes=".$totalcreditnotes." totaldeposts=".$totaldeposits."
2894 // selleruserrevenuestamp=".$selleruserevenustamp;
2895
2896 // We can also use bcadd to avoid pb with floating points
2897 // For example print 239.2 - 229.3 - 9.9; does not return 0.
2898 // $resteapayer=bcadd($object->total_ttc,$totalpaid,$conf->global->MAIN_MAX_DECIMALS_TOT);
2899 // $resteapayer=bcadd($resteapayer,$totalavoir,$conf->global->MAIN_MAX_DECIMALS_TOT);
2900 $resteapayer = price2num($object->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits, 'MT');
2901
2902 // Multicurrency
2903 $multicurrency_resteapayer = 0;
2904 if (isModEnabled("multicurrency")) {
2905 $multicurrency_totalpaid = $object->getSommePaiement(1);
2906 $multicurrency_totalcreditnotes = $object->getSumCreditNotesUsed(1);
2907 $multicurrency_totaldeposits = $object->getSumDepositsUsed(1);
2908 $multicurrency_resteapayer = price2num($object->multicurrency_total_ttc - $multicurrency_totalpaid - $multicurrency_totalcreditnotes - $multicurrency_totaldeposits, 'MT');
2909 // Code to fix case of corrupted data
2910 // TODO We should not need this. Also data comes from not reliable value of $object->multicurrency_total_ttc that may be wrong if it was
2911 // calculated by summing lines that were in a currency for some of them and into another for others (lines from discount/down payment into another currency for example)
2912 if ($resteapayer == 0 && $multicurrency_resteapayer != 0 && $object->multicurrency_code != $conf->currency) {
2913 $resteapayer = price2num($multicurrency_resteapayer / $object->multicurrency_tx, 'MT');
2914 }
2915 }
2916
2917 if ($object->paye) {
2918 $resteapayer = 0;
2919 }
2920 $resteapayeraffiche = $resteapayer;
2921
2922 if (getDolGlobalString('FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS')) { // Never use this
2923 $filterabsolutediscount = "fk_invoice_supplier_source IS NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
2924 $filtercreditnote = "fk_invoice_supplier_source IS NOT NULL"; // If we want deposit to be substracted to payments only and not to total of final invoice
2925 } else {
2926 $filterabsolutediscount = "fk_invoice_supplier_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS PAID)%')";
2927 $filtercreditnote = "fk_invoice_supplier_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS PAID)%')";
2928 }
2929
2930 $absolute_discount = $societe->getAvailableDiscounts('', $filterabsolutediscount, 0, 1);
2931 $absolute_creditnote = $societe->getAvailableDiscounts('', $filtercreditnote, 0, 1);
2932 $absolute_discount = price2num($absolute_discount, 'MT');
2933 $absolute_creditnote = price2num($absolute_creditnote, 'MT');
2934
2935 /*
2936 * View card
2937 */
2938 $objectidnext = $object->getIdReplacingInvoice();
2939
2940 $head = facturefourn_prepare_head($object);
2941 $titre = $langs->trans('SupplierInvoice');
2942
2943 print dol_get_fiche_head($head, 'card', $titre, -1, 'supplier_invoice', 0, '', '', 0, '', 1);
2944
2945 $formconfirm = '';
2946
2947 // Confirmation de la conversion de l'avoir en reduc
2948 if ($action == 'converttoreduc') {
2949 $type_fac = '';
2950 if ($object->type == FactureFournisseur::TYPE_STANDARD) {
2951 $type_fac = 'ExcessPaid';
2952 } elseif ($object->type == FactureFournisseur::TYPE_CREDIT_NOTE) {
2953 $type_fac = 'CreditNote';
2954 } elseif ($object->type == FactureFournisseur::TYPE_DEPOSIT) {
2955 $type_fac = 'Deposit';
2956 }
2957 $text = $langs->trans('ConfirmConvertToReducSupplier', strtolower($langs->transnoentities($type_fac)));
2958 $text .= '<br>'.$langs->trans('ConfirmConvertToReducSupplier2');
2959 $formconfirm = $form->formconfirm($_SERVER['PHP_SELF'].'?facid='.$object->id, $langs->trans('ConvertToReduc'), $text, 'confirm_converttoreduc', '', "yes", 2);
2960 }
2961
2962 // Clone confirmation
2963 if ($action == 'clone') {
2964 // Create an array for form
2965 $formquestion = array(
2966 array('type' => 'text', 'name' => 'newsupplierref', 'label' => $langs->trans("RefSupplierBill"), 'value' => $langs->trans("CopyOf").' '.$object->ref_supplier),
2967 array('type' => 'date', 'name' => 'newdate', 'label' => $langs->trans("Date"), 'value' => dol_now())
2968 );
2969 // Ask confirmation to clone
2970 $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ToClone'), $langs->trans('ConfirmCloneInvoice', $object->ref), 'confirm_clone', $formquestion, 'yes', 1, 250);
2971 }
2972
2973 // Confirmation of validation
2974 if ($action == 'valid') {
2975 // We check if number is temporary number
2976 if (preg_match('/^[\‍(]?PROV/i', $object->ref) || empty($object->ref)) {
2977 // empty should not happened, but when it occurs, the test save life
2978 $numref = $object->getNextNumRef($societe);
2979 } else {
2980 $numref = $object->ref;
2981 }
2982
2983 if ($numref < 0) {
2984 setEventMessages($object->error, $object->errors, 'errors');
2985 $action = '';
2986 } else {
2987 $text = $langs->trans('ConfirmValidateBill', $numref);
2988 /*if (isModEnabled('notification'))
2989 {
2990 require_once DOL_DOCUMENT_ROOT .'/core/class/notify.class.php';
2991 $notify=new Notify($db);
2992 $text.='<br>';
2993 $text.=$notify->confirmMessage('BILL_SUPPLIER_VALIDATE',$object->socid, $object);
2994 }*/
2995 $formquestion = array();
2996
2997 $qualified_for_stock_change = 0;
2998 if (!getDolGlobalString('STOCK_SUPPORTS_SERVICES')) {
2999 $qualified_for_stock_change = $object->hasProductsOrServices(2);
3000 } else {
3001 $qualified_for_stock_change = $object->hasProductsOrServices(1);
3002 }
3003
3004 if (isModEnabled('stock') && getDolGlobalString('STOCK_CALCULATE_ON_SUPPLIER_BILL') && $qualified_for_stock_change) {
3005 $langs->load("stocks");
3006 require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
3007 $formproduct = new FormProduct($db);
3008 $warehouse = new Entrepot($db);
3009 $warehouse_array = $warehouse->list_array();
3010 if (count($warehouse_array) == 1) {
3011 $label = $object->type == FactureFournisseur::TYPE_CREDIT_NOTE ? $langs->trans("WarehouseForStockDecrease", current($warehouse_array)) : $langs->trans("WarehouseForStockIncrease", current($warehouse_array));
3012 $value = '<input type="hidden" id="idwarehouse" name="idwarehouse" value="'.key($warehouse_array).'">';
3013 } else {
3014 $label = $object->type == FactureFournisseur::TYPE_CREDIT_NOTE ? $langs->trans("SelectWarehouseForStockDecrease") : $langs->trans("SelectWarehouseForStockIncrease");
3015 $value = $formproduct->selectWarehouses(GETPOST('idwarehouse') ? GETPOST('idwarehouse') : 'ifone', 'idwarehouse', '', 1);
3016 }
3017 $formquestion = array(
3018 array('type' => 'other', 'name' => 'idwarehouse', 'label' => $label, 'value' => $value)
3019 );
3020 }
3021
3022 $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ValidateBill'), $text, 'confirm_valid', $formquestion, 1, 1);
3023 }
3024 }
3025
3026 // Confirmation edit (back to draft)
3027 if ($action == 'edit') {
3028 $formquestion = array();
3029
3030 $qualified_for_stock_change = 0;
3031 if (!getDolGlobalString('STOCK_SUPPORTS_SERVICES')) {
3032 $qualified_for_stock_change = $object->hasProductsOrServices(2);
3033 } else {
3034 $qualified_for_stock_change = $object->hasProductsOrServices(1);
3035 }
3036 if (isModEnabled('stock') && getDolGlobalString('STOCK_CALCULATE_ON_SUPPLIER_BILL') && $qualified_for_stock_change) {
3037 $langs->load("stocks");
3038 require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
3039 $formproduct = new FormProduct($db);
3040 $warehouse = new Entrepot($db);
3041 $warehouse_array = $warehouse->list_array();
3042 if (count($warehouse_array) == 1) {
3043 $label = $object->type == FactureFournisseur::TYPE_CREDIT_NOTE ? $langs->trans("WarehouseForStockIncrease", current($warehouse_array)) : $langs->trans("WarehouseForStockDecrease", current($warehouse_array));
3044 $value = '<input type="hidden" id="idwarehouse" name="idwarehouse" value="'.key($warehouse_array).'">';
3045 } else {
3046 $label = $object->type == FactureFournisseur::TYPE_CREDIT_NOTE ? $langs->trans("SelectWarehouseForStockIncrease") : $langs->trans("SelectWarehouseForStockDecrease");
3047 $value = $formproduct->selectWarehouses(GETPOST('idwarehouse') ? GETPOST('idwarehouse') : 'ifone', 'idwarehouse', '', 1);
3048 }
3049 $formquestion = array(
3050 array('type' => 'other', 'name' => 'idwarehouse', 'label' => $label, 'value' => $value)
3051 );
3052 }
3053 $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('UnvalidateBill'), $langs->trans('ConfirmUnvalidateBill', $object->ref), 'confirm_edit', $formquestion, 1, 1);
3054 }
3055
3056 // Confirmation set paid
3057 if ($action == 'paid' && ($resteapayer <= 0 || (getDolGlobalString('SUPPLIER_INVOICE_CAN_SET_PAID_EVEN_IF_PARTIALLY_PAID') && $resteapayer == $object->total_ttc))) {
3058 $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ClassifyPaid'), $langs->trans('ConfirmClassifyPaidBill', $object->ref), 'confirm_paid', '', 0, 1);
3059 }
3060
3061 if ($action == 'paid' && $resteapayer > 0 && (!getDolGlobalString('SUPPLIER_INVOICE_CAN_SET_PAID_EVEN_IF_PARTIALLY_PAID') || $resteapayer != $object->total_ttc)) {
3062 $close = array();
3063 // Code
3064 $i = 0;
3065 $close[$i]['code'] = 'discount_vat'; // escompte
3066 $i++;
3067 $close[$i]['code'] = 'badsupplier';
3068 $i++;
3069 $close[$i]['code'] = 'other';
3070 $i++;
3071 // Help
3072 $i = 0;
3073 $close[$i]['label'] = $langs->trans("HelpEscompte").'<br><br>'.$langs->trans("ConfirmClassifyPaidPartiallyReasonDiscountVatDesc");
3074 $i++;
3075 $close[$i]['label'] = $langs->trans("ConfirmClassifyPaidPartiallyReasonBadSupplierDesc");
3076 $i++;
3077 $close[$i]['label'] = $langs->trans("Other");
3078 $i++;
3079 // Text
3080 $i = 0;
3081 $close[$i]['reason'] = $form->textwithpicto($langs->transnoentities("ConfirmClassifyPaidPartiallyReasonDiscount", $resteapayer, $langs->trans("Currency".$conf->currency)), $close[$i]['label'], 1);
3082 $i++;
3083 $close[$i]['reason'] = $form->textwithpicto($langs->transnoentities("ConfirmClassifyPaidPartiallyReasonBadCustomer", $resteapayer, $langs->trans("Currency".$conf->currency)), $close[$i]['label'], 1);
3084 $i++;
3085 $close[$i]['reason'] = $form->textwithpicto($langs->transnoentities("Other"), $close[$i]['label'], 1);
3086 $i++;
3087 // arrayreasons[code]=reason
3088 foreach ($close as $key => $val) {
3089 $arrayreasons[$close[$key]['code']] = $close[$key]['reason'];
3090 }
3091
3092 // Create a form table
3093 $formquestion = array('text' => $langs->trans("ConfirmClassifyPaidPartiallyQuestion"), array('type' => 'radio', 'name' => 'close_code', 'label' => $langs->trans("Reason"), 'values' => $arrayreasons), array('type' => 'text', 'name' => 'close_note', 'label' => $langs->trans("Comment"), 'value' => '', 'morecss' => 'minwidth300'));
3094 // Incomplete payment. We ask if the reason is discount or other
3095 $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?facid='.$object->id, $langs->trans('ClassifyPaid'), $langs->trans('ConfirmClassifyPaidPartially', $object->ref), 'confirm_paid_partially', $formquestion, "yes", 1, 310);
3096 }
3097
3098 // Confirmation of the abandoned classification
3099 if ($action == 'canceled') {
3100 // Code
3101 $close[1]['code'] = 'badsupplier';
3102 $close[2]['code'] = 'abandon';
3103 // Help
3104 $close[1]['label'] = $langs->trans("ConfirmClassifyPaidPartiallyReasonBadSupplierDesc");
3105 $close[2]['label'] = $langs->trans("ConfirmClassifyAbandonReasonOtherDesc");
3106 // Text
3107 $close[1]['reason'] = $form->textwithpicto($langs->transnoentities("ConfirmClassifyPaidPartiallyReasonBadSupplier", $object->ref), $close[1]['label'], 1);
3108 $close[2]['reason'] = $form->textwithpicto($langs->transnoentities("ConfirmClassifyAbandonReasonOther"), $close[2]['label'], 1);
3109 // arrayreasons
3110 $arrayreasons[$close[1]['code']] = $close[1]['reason'];
3111 $arrayreasons[$close[2]['code']] = $close[2]['reason'];
3112
3113 // Create a form table
3114 $formquestion = array('text' => $langs->trans("ConfirmCancelBillQuestion"), array('type' => 'radio', 'name' => 'close_code', 'label' => $langs->trans("Reason"), 'values' => $arrayreasons), array('type' => 'text', 'name' => 'close_note', 'label' => $langs->trans("Comment"), 'value' => '', 'morecss' => 'minwidth300'));
3115
3116 $formconfirm = $form->formconfirm($_SERVER['PHP_SELF'].'?id='.$object->id, $langs->trans('CancelBill'), $langs->trans('ConfirmCancelBill', $object->ref), 'confirm_canceled', $formquestion, "yes", 1, 250);
3117 }
3118
3119 // Confirmation de la suppression de la facture fournisseur
3120 if ($action == 'delete') {
3121 $formquestion = array();
3122
3123 $qualified_for_stock_change = 0;
3124 if (!getDolGlobalString('STOCK_SUPPORTS_SERVICES')) {
3125 $qualified_for_stock_change = $object->hasProductsOrServices(2);
3126 } else {
3127 $qualified_for_stock_change = $object->hasProductsOrServices(1);
3128 }
3129
3130 if (isModEnabled('stock') && getDolGlobalString('STOCK_CALCULATE_ON_SUPPLIER_BILL') && $qualified_for_stock_change) {
3131 $langs->load("stocks");
3132 require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
3133 $formproduct = new FormProduct($db);
3134 $warehouse = new Entrepot($db);
3135 $warehouse_array = $warehouse->list_array();
3136
3137 $selectwarehouse = '<span class="questionrevertstock hidden">';
3138 if (count($warehouse_array) == 1) {
3139 $label = $object->type == FactureFournisseur::TYPE_CREDIT_NOTE ? $langs->trans("WarehouseForStockIncrease", current($warehouse_array)) : $langs->trans("WarehouseForStockDecrease", current($warehouse_array));
3140 $selectwarehouse .= '<input type="hidden" id="idwarehouse" name="idwarehouse" value="'.key($warehouse_array).'">';
3141 } else {
3142 $label = $object->type == FactureFournisseur::TYPE_CREDIT_NOTE ? $langs->trans("SelectWarehouseForStockIncrease") : $langs->trans("SelectWarehouseForStockDecrease");
3143 $selectwarehouse .= $formproduct->selectWarehouses(GETPOST('idwarehouse') ? GETPOST('idwarehouse') : 'ifone', 'idwarehouse', '', 1);
3144 }
3145 $selectwarehouse .= '</span>';
3146
3147 $selectyesno = array(0 => $langs->trans('No'), 1 => $langs->trans('Yes'));
3148
3149 print '<script type="text/javascript">
3150 $(document).ready(function() {
3151 $("#revertstock").change(function() {
3152 if(this.value > 0) {
3153 $(".questionrevertstock").removeClass("hidden");
3154 } else {
3155 $(".questionrevertstock").addClass("hidden");
3156 }
3157 });
3158 });
3159 </script>';
3160
3161 $formquestion = array(
3162 array('type' => 'select', 'name' => 'revertstock', 'label' => $langs->trans("RevertProductsToStock"), 'select_show_empty' => 0, 'values' => $selectyesno),
3163 array('type' => 'other', 'name' => 'idwarehouse', 'label' => $label, 'value' => $selectwarehouse, 'tdclass' => 'questionrevertstock hidden')
3164 );
3165 }
3166
3167 $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteBill'), $langs->trans('ConfirmDeleteBill'), 'confirm_delete', $formquestion, 1, 1);
3168 }
3169 if ($action == 'deletepayment') {
3170 $payment_id = GETPOST('paiement_id');
3171 $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&paiement_id='.$payment_id, $langs->trans('DeletePayment'), $langs->trans('ConfirmDeletePayment'), 'confirm_delete_paiement', '', 0, 1);
3172 }
3173
3174 // Confirmation to delete line
3175 if ($action == 'ask_deleteline') {
3176 $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&lineid='.$lineid, $langs->trans('DeleteProductLine'), $langs->trans('ConfirmDeleteProductLine'), 'confirm_deleteline', '', 0, 1);
3177 }
3178
3179 $parameters = array('formConfirm' => $formconfirm, 'lineid'=>$lineid);
3180 $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
3181 if (empty($reshook)) {
3182 $formconfirm .= $hookmanager->resPrint;
3183 } elseif ($reshook > 0) {
3184 $formconfirm = $hookmanager->resPrint;
3185 }
3186
3187 // Print form confirm
3188 print $formconfirm;
3189
3190
3191 // Supplier invoice card
3192 $linkback = '<a href="'.DOL_URL_ROOT.'/fourn/facture/list.php?restore_lastsearch_values=1'.(!empty($socid) ? '&socid='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
3193
3194 $morehtmlref = '<div class="refidno">';
3195 // Ref supplier
3196 $morehtmlref .= $form->editfieldkey("RefSupplierBill", 'ref_supplier', $object->ref_supplier, $object, $usercancreate, 'string', '', 0, 1);
3197 $morehtmlref .= $form->editfieldval("RefSupplierBill", 'ref_supplier', $object->ref_supplier, $object, $usercancreate, 'string', '', null, null, '', 1);
3198 // Thirdparty
3199 $morehtmlref .= '<br>'.$object->thirdparty->getNomUrl(1, 'supplier');
3200 if (!getDolGlobalString('MAIN_DISABLE_OTHER_LINK') && $object->thirdparty->id > 0) {
3201 $morehtmlref .= ' <div class="inline-block valignmiddle">(<a class="valignmiddle" href="'.DOL_URL_ROOT.'/fourn/facture/list.php?socid='.((int) $object->thirdparty->id).'&search_company='.urlencode($object->thirdparty->name).'">'.$langs->trans("OtherBills").'</a>)</div>';
3202 }
3203 // Project
3204 if (isModEnabled('project')) {
3205 $langs->load("projects");
3206 $morehtmlref .= '<br>';
3207 if ($permissiontoadd) {
3208 $morehtmlref .= img_picto($langs->trans("Project"), 'project', 'class="pictofixedwidth"');
3209 if ($action != 'classify') {
3210 $morehtmlref .= '<a class="editfielda" href="'.$_SERVER['PHP_SELF'].'?action=classify&token='.newToken().'&id='.((int) $object->id).'">'.img_edit($langs->transnoentitiesnoconv('SetProject')).'</a> ';
3211 }
3212 $morehtmlref .= $form->form_project($_SERVER['PHP_SELF'].'?id='.$object->id, (!getDolGlobalString('PROJECT_CAN_ALWAYS_LINK_TO_ALL_SUPPLIERS') ? $object->socid : -1), $object->fk_project, ($action == 'classify' ? 'projectid' : 'none'), 0, 0, 0, 1, '', 'maxwidth300');
3213 } else {
3214 if (!empty($object->fk_project)) {
3215 $proj = new Project($db);
3216 $proj->fetch($object->fk_project);
3217 $morehtmlref .= $proj->getNomUrl(1);
3218 if ($proj->title) {
3219 $morehtmlref .= '<span class="opacitymedium"> - '.dol_escape_htmltag($proj->title).'</span>';
3220 }
3221 }
3222 }
3223 }
3224 $morehtmlref .= '</div>';
3225
3226 $object->totalpaid = $totalpaid; // To give a chance to dol_banner_tab to use already paid amount to show correct status
3227
3228 dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref);
3229
3230 // Call Hook tabContentViewSupplierInvoice
3231 $parameters = array();
3232 // Note that $action and $object may be modified by hook
3233 $reshook = $hookmanager->executeHooks('tabContentViewSupplierInvoice', $parameters, $object, $action);
3234 if (empty($reshook)) {
3235 print '<div class="fichecenter">';
3236 print '<div class="fichehalfleft">';
3237 print '<div class="underbanner clearboth"></div>';
3238
3239 print '<table class="border tableforfield centpercent">';
3240
3241 // Type
3242 print '<tr><td class="titlefield">'.$langs->trans('Type').'</td><td>';
3243 print '<span class="badgeneutral">';
3244 print $object->getLibType();
3245 print '</span>';
3246 if ($object->subtype > 0) {
3247 print ' '.$object->getSubtypeLabel('facture_fourn');
3248 }
3249 if ($object->type == FactureFournisseur::TYPE_REPLACEMENT) {
3250 $facreplaced = new FactureFournisseur($db);
3251 $facreplaced->fetch($object->fk_facture_source);
3252 print ' <span class="opacitymediumbycolor paddingleft">'.$langs->transnoentities("ReplaceInvoice", $facreplaced->getNomUrl(1)).'</span>';
3253 }
3254 if ($object->type == FactureFournisseur::TYPE_CREDIT_NOTE) {
3255 if ($object->fk_facture_source > 0) {
3256 $facusing = new FactureFournisseur($db);
3257 $facusing->fetch($object->fk_facture_source);
3258 print ' <span class="opacitymediumbycolor paddingleft">'.$langs->transnoentities("CorrectInvoice", $facusing->getNomUrl(1)).'</span>';
3259 } else {
3260 $langs->load("errors");
3261 print ' <span class="opacitymediumbycolor paddingleft">'.$langs->transnoentities("WarningCorrectedInvoiceNotFound").'</span>';
3262 }
3263 }
3264
3265 $facidavoir = $object->getListIdAvoirFromInvoice();
3266 if (count($facidavoir) > 0) {
3267 $invoicecredits = array();
3268 foreach ($facidavoir as $id) {
3269 $facavoir = new FactureFournisseur($db);
3270 $facavoir->fetch($id);
3271 $invoicecredits[] = $facavoir->getNomUrl(1);
3272 }
3273 print ' <span class="opacitymediumbycolor paddingleft">'.$langs->transnoentities("InvoiceHasAvoir") . (count($invoicecredits) ? ' ' : '') . implode(',', $invoicecredits);
3274 print '</span>';
3275 }
3276 if (isset($objectidnext) && $objectidnext > 0) {
3277 $facthatreplace = new FactureFournisseur($db);
3278
3279 $facthatreplace->fetch($objectidnext);
3280 print ' <span class="opacitymediumbycolor paddingleft">'.str_replace('{s1}', $facthatreplace->getNomUrl(1), $langs->transnoentities("ReplacedByInvoice", '{s1}')).'</span>';
3281 }
3282 if ($object->type == FactureFournisseur::TYPE_CREDIT_NOTE || $object->type == FactureFournisseur::TYPE_DEPOSIT) {
3283 $discount = new DiscountAbsolute($db);
3284 $result = $discount->fetch(0, 0, $object->id);
3285 if ($result > 0) {
3286 print ' <span class="opacitymediumbycolor paddingleft">';
3287 $s = $langs->trans("CreditNoteConvertedIntoDiscount", '{s1}', '{s2}');
3288 $s = str_replace('{s1}', $object->getLibType(1), $s);
3289 $s = str_replace('{s2}', $discount->getNomUrl(1, 'discount'), $s);
3290 print $s;
3291 print '</span><br>';
3292 }
3293 }
3294
3295 if ($object->fk_fac_rec_source > 0) {
3296 $tmptemplate = new FactureFournisseurRec($db);
3297 $result = $tmptemplate->fetch($object->fk_fac_rec_source);
3298 if ($result > 0) {
3299 print ' <span class="opacitymediumbycolor paddingleft">';
3300 $link = '<a href="'.DOL_URL_ROOT.'/fourn/facture/card-rec.php?facid='.$tmptemplate->id.'">'.dol_escape_htmltag($tmptemplate->title).'</a>';
3301 $s = $langs->transnoentities("GeneratedFromSupplierTemplate", $link);
3302
3303 print $s;
3304 print '</span>';
3305 }
3306 }
3307 print '</td></tr>';
3308
3309
3310 // Relative and absolute discounts
3311 print '<!-- Discounts -->'."\n";
3312 print '<tr><td>'.$langs->trans('DiscountStillRemaining');
3313 print '</td><td>';
3314
3315 $thirdparty = $societe;
3316 $discount_type = 1;
3317 include DOL_DOCUMENT_ROOT.'/core/tpl/object_discounts.tpl.php';
3318
3319 print '</td></tr>';
3320
3321 // Label
3322 print '<tr>';
3323 print '<td>'.$form->editfieldkey("Label", 'label', $object->label, $object, $usercancreate).'</td>';
3324 print '<td>'.$form->editfieldval("Label", 'label', $object->label, $object, $usercancreate).'</td>';
3325 print '</tr>';
3326
3327 //$form_permission = ($object->statut < FactureFournisseur::STATUS_CLOSED) && $usercancreate && ($object->getSommePaiement() <= 0);
3328 $form_permission = ($object->statut < FactureFournisseur::STATUS_CLOSED) && $usercancreate;
3329
3330 // Date
3331 print '<tr><td>';
3332 print $form->editfieldkey("DateInvoice", 'datef', $object->date, $object, $form_permission, 'datepicker');
3333 print '</td><td colspan="3">';
3334 print $form->editfieldval("Date", 'datef', $object->date, $object, $form_permission, 'datepicker');
3335 print '</td>';
3336
3337 // Default terms of the settlement
3338 $langs->load('bills');
3339 print '<tr><td class="nowrap">';
3340 print '<table class="nobordernopadding centpercent"><tr><td class="nowrap">';
3341 print $langs->trans('PaymentConditions');
3342 print '<td>';
3343 if ($action != 'editconditions' && $form_permission) {
3344 print '<td class="right"><a class="editfielda reposition" href="'.$_SERVER["PHP_SELF"].'?action=editconditions&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->trans('SetConditions'), 1).'</a></td>';
3345 }
3346 print '</tr></table>';
3347 print '</td><td>';
3348 if ($action == 'editconditions') {
3349 $form->form_conditions_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->cond_reglement_id, 'cond_reglement_id');
3350 } else {
3351 $form->form_conditions_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->cond_reglement_id, 'none');
3352 }
3353 print "</td>";
3354 print '</tr>';
3355
3356 // Due date
3357 print '<tr><td>';
3358 print $form->editfieldkey("DateMaxPayment", 'date_lim_reglement', $object->date_echeance, $object, $form_permission, 'datepicker');
3359 print '</td><td>';
3360 print $form->editfieldval("DateMaxPayment", 'date_lim_reglement', $object->date_echeance, $object, $form_permission, 'datepicker');
3361 if ($action != 'editdate_lim_reglement' && $object->hasDelay()) {
3362 print img_warning($langs->trans('Late'));
3363 }
3364 print '</td>';
3365
3366 // Mode of payment
3367 $langs->load('bills');
3368 print '<tr><td class="nowrap">';
3369 print '<table class="nobordernopadding centpercent"><tr><td class="nowrap">';
3370 print $langs->trans('PaymentMode');
3371 print '</td>';
3372 if ($action != 'editmode' && $form_permission) {
3373 print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editmode&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->trans('SetMode'), 1).'</a></td>';
3374 }
3375 print '</tr></table>';
3376 print '</td><td>';
3377 if ($action == 'editmode') {
3378 $form->form_modes_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->mode_reglement_id, 'mode_reglement_id', 'DBIT', 1, 1);
3379 } else {
3380 $form->form_modes_reglement($_SERVER['PHP_SELF'].'?id='.$object->id, $object->mode_reglement_id, 'none');
3381 }
3382 print '</td></tr>';
3383
3384 // Multicurrency
3385 if (isModEnabled("multicurrency")) {
3386 // Multicurrency code
3387 print '<tr>';
3388 print '<td>';
3389 print '<table class="nobordernopadding" width="100%"><tr><td>';
3390 print $form->editfieldkey('Currency', 'multicurrency_code', '', $object, 0);
3391 print '</td>';
3392 if ($action != 'editmulticurrencycode' && $object->statut == $object::STATUS_DRAFT) {
3393 print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editmulticurrencycode&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->transnoentitiesnoconv('SetMultiCurrencyCode'), 1).'</a></td>';
3394 }
3395 print '</tr></table>';
3396 print '</td><td>';
3397 if ($action == 'editmulticurrencycode') {
3398 $form->form_multicurrency_code($_SERVER['PHP_SELF'].'?id='.$object->id, $object->multicurrency_code, 'multicurrency_code');
3399 } else {
3400 $form->form_multicurrency_code($_SERVER['PHP_SELF'].'?id='.$object->id, $object->multicurrency_code, 'none');
3401 }
3402 print '</td></tr>';
3403
3404 // Multicurrency rate
3405 if ($object->multicurrency_code != $conf->currency || $object->multicurrency_tx != 1) {
3406 print '<tr>';
3407 print '<td>';
3408 print '<table class="nobordernopadding centpercent"><tr><td>';
3409 print $form->editfieldkey('CurrencyRate', 'multicurrency_tx', '', $object, 0);
3410 print '</td>';
3411 if ($action != 'editmulticurrencyrate' && $object->statut == $object::STATUS_DRAFT && $object->multicurrency_code && $object->multicurrency_code != $conf->currency) {
3412 print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editmulticurrencyrate&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->transnoentitiesnoconv('SetMultiCurrencyCode'), 1).'</a></td>';
3413 }
3414 print '</tr></table>';
3415 print '</td><td>';
3416 if ($action == 'editmulticurrencyrate' || $action == 'actualizemulticurrencyrate') {
3417 if ($action == 'actualizemulticurrencyrate') {
3418 list($object->fk_multicurrency, $object->multicurrency_tx) = MultiCurrency::getIdAndTxFromCode($object->db, $object->multicurrency_code);
3419 }
3420 $form->form_multicurrency_rate($_SERVER['PHP_SELF'].'?id='.$object->id, $object->multicurrency_tx, 'multicurrency_tx', $object->multicurrency_code);
3421 } else {
3422 $form->form_multicurrency_rate($_SERVER['PHP_SELF'].'?id='.$object->id, $object->multicurrency_tx, 'none', $object->multicurrency_code);
3423 if ($object->statut == $object::STATUS_DRAFT && $object->multicurrency_code && $object->multicurrency_code != $conf->currency) {
3424 print '<div class="inline-block"> &nbsp; &nbsp; &nbsp; &nbsp; ';
3425 print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=actualizemulticurrencyrate">'.$langs->trans("ActualizeCurrency").'</a>';
3426 print '</div>';
3427 }
3428 }
3429 print '</td></tr>';
3430 }
3431 }
3432
3433 // Bank Account
3434 if (isModEnabled("banque")) {
3435 print '<tr><td class="nowrap">';
3436 print '<table width="100%" class="nobordernopadding"><tr><td class="nowrap">';
3437 print $langs->trans('BankAccount');
3438 print '<td>';
3439 if ($action != 'editbankaccount' && $usercancreate) {
3440 print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editbankaccount&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->trans('SetBankAccount'), 1).'</a></td>';
3441 }
3442 print '</tr></table>';
3443 print '</td><td>';
3444 if ($action == 'editbankaccount') {
3445 $form->formSelectAccount($_SERVER['PHP_SELF'].'?id='.$object->id, $object->fk_account, 'fk_account', 1);
3446 } else {
3447 $form->formSelectAccount($_SERVER['PHP_SELF'].'?id='.$object->id, $object->fk_account, 'none');
3448 }
3449 print "</td>";
3450 print '</tr>';
3451 }
3452
3453 // Vat reverse-charge by default
3454 if (getDolGlobalString('ACCOUNTING_FORCE_ENABLE_VAT_REVERSE_CHARGE')) {
3455 print '<tr><td class="nowrap">';
3456 print '<table width="100%" class="nobordernopadding"><tr><td class="nowrap">';
3457 print $langs->trans('VATReverseCharge');
3458 print '<td>';
3459 if ($action != 'editvatreversecharge' && $usercancreate) {
3460 print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=editvatreversecharge&amp;id='.$object->id.'">'.img_edit($langs->trans('SetVATReverseCharge'), 1).'</a></td>';
3461 }
3462 print '</tr></table>';
3463 print '</td><td>';
3464 if ($action == 'editvatreversecharge') {
3465 print '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
3466 print '<input type="hidden" name="action" value="setvatreversecharge">';
3467 print '<input type="hidden" name="token" value="'.newToken().'">';
3468
3469 print '<input type="checkbox" name="vat_reverse_charge"' . ($object->vat_reverse_charge == '1' ? ' checked ' : '') . '>';
3470
3471 print '<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
3472 print '</form>';
3473 } else {
3474 print '<input type="checkbox" name="vat_reverse_charge"'. ($object->vat_reverse_charge == '1' ? ' checked ' : '') . ' disabled>';
3475 }
3476 print '</td></tr>';
3477 }
3478
3479 // Incoterms
3480 if (isModEnabled('incoterm')) {
3481 print '<tr><td>';
3482 print '<table width="100%" class="nobordernopadding"><tr><td>';
3483 print $langs->trans('IncotermLabel');
3484 print '<td><td class="right">';
3485 if ($usercancreate) {
3486 print '<a class="editfielda" href="'.DOL_URL_ROOT.'/fourn/facture/card.php?facid='.$object->id.'&action=editincoterm&token='.newToken().'">'.img_edit().'</a>';
3487 } else {
3488 print '&nbsp;';
3489 }
3490 print '</td></tr></table>';
3491 print '</td>';
3492 print '<td>';
3493 if ($action != 'editincoterm') {
3494 print $form->textwithpicto($object->display_incoterms(), $object->label_incoterms, 1);
3495 } else {
3496 print $form->select_incoterms((!empty($object->fk_incoterms) ? $object->fk_incoterms : ''), (!empty($object->location_incoterms) ? $object->location_incoterms : ''), $_SERVER['PHP_SELF'].'?id='.$object->id);
3497 }
3498 print '</td></tr>';
3499 }
3500
3501 // Intracomm report
3502 if (isModEnabled('intracommreport')) {
3503 $langs->loadLangs(array("intracommreport"));
3504 print '<!-- If module intracomm on -->'."\n";
3505 print '<tr><td>';
3506 print '<table class="nobordernopadding centpercent"><tr><td>';
3507 print $langs->trans('IntracommReportTransportMode');
3508 print '</td>';
3509 if ($action != 'edittransportmode' && ($user->hasRight("fournisseur", "facture", "creer") || $user->hasRight("supplier_invoice", "creer"))) {
3510 print '<td class="right"><a class="editfielda" href="'.$_SERVER["PHP_SELF"].'?action=edittransportmode&token='.newToken().'&id='.$object->id.'">'.img_edit().'</a></td>';
3511 }
3512 print '</tr></table>';
3513 print '</td>';
3514 print '<td>';
3515 if ($action == 'edittransportmode') {
3516 $form->formSelectTransportMode($_SERVER['PHP_SELF'].'?id='.$object->id, $object->transport_mode_id, 'transport_mode_id', 1, 1);
3517 } else {
3518 $form->formSelectTransportMode($_SERVER['PHP_SELF'].'?id='.$object->id, $object->transport_mode_id, 'none');
3519 }
3520 print '</td></tr>';
3521 }
3522
3523 // Other attributes
3524 $cols = 2;
3525 include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
3526
3527 print '</table>';
3528 print '</div>';
3529
3530 print '<div class="fichehalfright">';
3531 print '<div class="underbanner clearboth"></div>';
3532
3533 print '<table class="border tableforfield centpercent">';
3534
3535 print '<tr>';
3536 print '<td class="titlefieldmiddle">' . $langs->trans('AmountHT') . '</td>';
3537 print '<td class="nowrap amountcard right">' . price($object->total_ht, '', $langs, 0, -1, -1, $conf->currency) . '</td>';
3538 if (isModEnabled("multicurrency") && ($object->multicurrency_code && $object->multicurrency_code != $conf->currency)) {
3539 print '<td class="nowrap amountcard right">' . price($object->multicurrency_total_ht, '', $langs, 0, -1, -1, $object->multicurrency_code) . '</td>';
3540 }
3541 print '</tr>';
3542
3543 print '<tr>';
3544 print '<td>' . $langs->trans('AmountVAT') . '</td>';
3545 print '<td class="nowrap amountcard right">';
3546 if (GETPOST('calculationrule')) {
3547 $calculationrule = GETPOST('calculationrule', 'alpha');
3548 } else {
3549 $calculationrule = (!getDolGlobalString('MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND') ? 'totalofround' : 'roundoftotal');
3550 }
3551 if ($calculationrule == 'totalofround') {
3552 $calculationrulenum = 1;
3553 } else {
3554 $calculationrulenum = 2;
3555 }
3556 // Show link for "recalculate"
3557 if ($object->getVentilExportCompta() == 0) {
3558 $s = '<span class="hideonsmartphone opacitymedium">' . $langs->trans("ReCalculate") . ' </span>';
3559 $s .= '<a href="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&action=calculate&calculationrule=totalofround">' . $langs->trans("Mode1") . '</a>';
3560 $s .= ' / ';
3561 $s .= '<a href="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&action=calculate&calculationrule=roundoftotal">' . $langs->trans("Mode2") . '</a>';
3562 print '<div class="inline-block">';
3563 print $form->textwithtooltip($s, $langs->trans("CalculationRuleDesc", $calculationrulenum) . '<br>' . $langs->trans("CalculationRuleDescSupplier"), 2, 1, img_picto('', 'help'), '', 3, '', 0, 'recalculate');
3564 print '&nbsp; &nbsp; &nbsp; &nbsp;';
3565 print '</div>';
3566 }
3567 print price($object->total_tva, 1, $langs, 0, -1, -1, $conf->currency);
3568 print '</td>';
3569 if (isModEnabled("multicurrency") && ($object->multicurrency_code && $object->multicurrency_code != $conf->currency)) {
3570 print '<td class="nowrap amountcard right">' . price($object->multicurrency_total_tva, '', $langs, 0, -1, -1, $object->multicurrency_code) . '</td>';
3571 }
3572 print '</tr>';
3573
3574 if ($societe->localtax1_assuj == "1") { //Localtax1
3575 print '<tr>';
3576 print '<td>' . $langs->transcountry("AmountLT1", $societe->country_code) . '</td>';
3577 print '<td class="nowrap amountcard right">' . price($object->total_localtax1, 1, $langs, 0, -1, -1, $conf->currency) . '</td>';
3578 print '</tr>';
3579 }
3580 if ($societe->localtax2_assuj == "1") { //Localtax2
3581 print '<tr>';
3582 print '<td>' . $langs->transcountry("AmountLT2", $societe->country_code) . '</td>';
3583 print '<td class="nowrap amountcard right">' . price($object->total_localtax2, 1, $langs, 0, -1, -1, $conf->currency) . '</td>';
3584 print '</tr>';
3585 }
3586
3587 print '<tr>';
3588 print '<td>' . $langs->trans('AmountTTC') . '</td>';
3589 print '<td class="nowrap amountcard right">' . price($object->total_ttc, '', $langs, 0, -1, -1, $conf->currency) . '</td>';
3590 if (isModEnabled("multicurrency") && ($object->multicurrency_code && $object->multicurrency_code != $conf->currency)) {
3591 print '<td class="nowrap amountcard right">' . price($object->multicurrency_total_ttc, '', $langs, 0, -1, -1, $object->multicurrency_code) . '</td>';
3592 }
3593 print '</tr>';
3594
3595 print '</table>';
3596
3597
3598 // List of payments
3599
3600 $totalpaid = 0;
3601
3602 $sign = 1;
3603 if ($object->type == FactureFournisseur::TYPE_CREDIT_NOTE) {
3604 $sign = - 1;
3605 }
3606
3607 $nbrows = 9;
3608 $nbcols = 3;
3609 if (isModEnabled('project')) {
3610 $nbrows++;
3611 }
3612 if (isModEnabled("banque")) {
3613 $nbrows++;
3614 $nbcols++;
3615 }
3616 if (isModEnabled('incoterm')) {
3617 $nbrows++;
3618 }
3619 if (isModEnabled("multicurrency")) {
3620 $nbrows += 5;
3621 }
3622
3623 // Local taxes
3624 if ($societe->localtax1_assuj == "1") {
3625 $nbrows++;
3626 }
3627 if ($societe->localtax2_assuj == "1") {
3628 $nbrows++;
3629 }
3630
3631 $sql = 'SELECT p.datep as dp, p.ref, p.num_paiement as num_payment, p.rowid, p.fk_bank,';
3632 $sql .= ' c.id as payment_type, c.code as payment_code,';
3633 $sql .= ' pf.amount,';
3634 $sql .= ' ba.rowid as baid, ba.ref as baref, ba.label, ba.number as banumber, ba.account_number, ba.fk_accountancy_journal';
3635 $sql .= ' FROM '.MAIN_DB_PREFIX.'paiementfourn as p';
3636 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON p.fk_bank = b.rowid';
3637 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank_account as ba ON b.fk_account = ba.rowid';
3638 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as c ON p.fk_paiement = c.id';
3639 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'paiementfourn_facturefourn as pf ON pf.fk_paiementfourn = p.rowid';
3640 $sql .= ' WHERE pf.fk_facturefourn = '.((int) $object->id);
3641 $sql .= ' ORDER BY p.datep, p.tms';
3642
3643 $result = $db->query($sql);
3644 if ($result) {
3645 $num = $db->num_rows($result);
3646 $i = 0;
3647
3648 print '<div class="div-table-responsive-no-min">';
3649 print '<table class="noborder paymenttable centpercent">';
3650 print '<tr class="liste_titre">';
3651 print '<td class="liste_titre">'.($object->type == FactureFournisseur::TYPE_CREDIT_NOTE ? $langs->trans("PaymentsBack") : $langs->trans('Payments')).'</td>';
3652 print '<td>'.$langs->trans('Date').'</td>';
3653 print '<td>'.$langs->trans('Type').'</td>';
3654 if (isModEnabled("banque")) {
3655 print '<td class="right">'.$langs->trans('BankAccount').'</td>';
3656 }
3657 print '<td class="right">'.$langs->trans('Amount').'</td>';
3658 print '<td width="18">&nbsp;</td>';
3659 print '</tr>';
3660
3661 if ($num > 0) {
3662 while ($i < $num) {
3663 $objp = $db->fetch_object($result);
3664
3665 $paymentstatic->id = $objp->rowid;
3666 $paymentstatic->datepaye = $db->jdate($objp->dp);
3667 $paymentstatic->ref = ($objp->ref ? $objp->ref : $objp->rowid);
3668 $paymentstatic->num_payment = $objp->num_payment;
3669
3670 $paymentstatic->paiementcode = $objp->payment_code;
3671 $paymentstatic->type_code = $objp->payment_code;
3672 $paymentstatic->type_label = $objp->payment_type;
3673
3674 print '<tr class="oddeven">';
3675 print '<td class="nowraponall">';
3676 print $paymentstatic->getNomUrl(1);
3677 print '</td>';
3678 print '<td>'.dol_print_date($db->jdate($objp->dp), 'day').'</td>';
3679 $s = $form->form_modes_reglement(null, $objp->payment_type, 'none', '', 1, 0, '', 1).' '.$objp->num_payment;
3680 print '<td class="tdoverflowmax125" title="'.dol_escape_htmltag($s).'">';
3681 print $s;
3682 print '</td>';
3683 if (isModEnabled("banque")) {
3684 $bankaccountstatic->id = $objp->baid;
3685 $bankaccountstatic->ref = $objp->baref;
3686 $bankaccountstatic->label = $objp->baref;
3687 $bankaccountstatic->number = $objp->banumber;
3688
3689 if (isModEnabled('accounting')) {
3690 $bankaccountstatic->account_number = $objp->account_number;
3691
3692 $accountingjournal = new AccountingJournal($db);
3693 $accountingjournal->fetch($objp->fk_accountancy_journal);
3694 $bankaccountstatic->accountancy_journal = $accountingjournal->getNomUrl(0, 1, 1, '', 1);
3695 }
3696
3697 print '<td class="right">';
3698 if ($objp->baid > 0) {
3699 print $bankaccountstatic->getNomUrl(1, 'transactions');
3700 }
3701 print '</td>';
3702 }
3703 print '<td class="right">'.price($sign * $objp->amount).'</td>';
3704 print '<td class="center">';
3705 if ($object->statut == FactureFournisseur::STATUS_VALIDATED && $object->paye == 0 && $user->socid == 0) {
3706 print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=deletepayment&token='.newToken().'&paiement_id='.$objp->rowid.'">';
3707 print img_delete();
3708 print '</a>';
3709 }
3710 print '</td>';
3711 print '</tr>';
3712 $totalpaid += $objp->amount;
3713 $i++;
3714 }
3715 } else {
3716 print '<tr class="oddeven"><td colspan="'.$nbcols.'"><span class="opacitymedium">'.$langs->trans("None").'</span></td><td></td><td></td></tr>';
3717 }
3718
3719 /*
3720 if ($object->paye == 0)
3721 {
3722 print '<tr><td colspan="'.$nbcols.'" class="right">'.$langs->trans('AlreadyPaid').' :</td><td class="right">'.price($totalpaid).'</td><td></td></tr>';
3723 print '<tr><td colspan="'.$nbcols.'" class="right">'.$langs->trans("Billed").' :</td><td class="right">'.price($object->total_ttc).'</td><td></td></tr>';
3724
3725 $resteapayer = $object->total_ttc - $totalpaid;
3726
3727 print '<tr><td colspan="'.$nbcols.'" class="right">'.$langs->trans('RemainderToPay').' :</td>';
3728 print '<td class="right'.($resteapayer?' amountremaintopay':'').'">'.price($resteapayer).'</td><td></td></tr>';
3729 }
3730 */
3731
3732 $db->free($result);
3733 } else {
3734 dol_print_error($db);
3735 }
3736
3737 if ($object->type != FactureFournisseur::TYPE_CREDIT_NOTE) {
3738 // Total already paid
3739 print '<tr><td colspan="'.$nbcols.'" class="right">';
3740 print '<span class="opacitymedium">';
3741 if ($object->type != FactureFournisseur::TYPE_DEPOSIT) {
3742 print $langs->trans('AlreadyPaidNoCreditNotesNoDeposits');
3743 } else {
3744 print $langs->trans('AlreadyPaid');
3745 }
3746 print '</span>';
3747 print '</td><td class="right"'.(($totalpaid > 0) ? ' class="amountalreadypaid"' : '').'>'.price($totalpaid).'</td><td>&nbsp;</td></tr>';
3748
3749 //$resteapayer = $object->total_ttc - $totalpaid;
3750 $resteapayeraffiche = $resteapayer;
3751
3752 $cssforamountpaymentcomplete = 'amountpaymentcomplete';
3753
3754 // Loop on each credit note or deposit amount applied
3755 $creditnoteamount = 0;
3756 $depositamount = 0;
3757
3758 $sql = "SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
3759 $sql .= " re.description, re.fk_invoice_supplier_source";
3760 $sql .= " FROM ".MAIN_DB_PREFIX."societe_remise_except as re";
3761 $sql .= " WHERE fk_invoice_supplier = ".((int) $object->id);
3762 $resql = $db->query($sql);
3763 if ($resql) {
3764 $num = $db->num_rows($resql);
3765 $i = 0;
3766 $invoice = new FactureFournisseur($db);
3767 while ($i < $num) {
3768 $obj = $db->fetch_object($resql);
3769 $invoice->fetch($obj->fk_invoice_supplier_source);
3770 print '<tr><td colspan="'.$nbcols.'" class="right">';
3771 if ($invoice->type == FactureFournisseur::TYPE_CREDIT_NOTE) {
3772 print $langs->trans("CreditNote").' ';
3773 }
3774 if ($invoice->type == FactureFournisseur::TYPE_DEPOSIT) {
3775 print $langs->trans("Deposit").' ';
3776 }
3777 print $invoice->getNomUrl(0);
3778 print ' :</td>';
3779 print '<td class="right">'.price($obj->amount_ttc).'</td>';
3780 print '<td class="right">';
3781 print '<a href="'.$_SERVER["PHP_SELF"].'?facid='.$object->id.'&action=unlinkdiscount&discountid='.$obj->rowid.'">';
3782 print img_picto($langs->transnoentitiesnoconv("RemoveDiscount"), 'unlink');
3783 print '</a>';
3784 print '</td></tr>';
3785 $i++;
3786 if ($invoice->type == FactureFournisseur::TYPE_CREDIT_NOTE) {
3787 $creditnoteamount += $obj->amount_ttc;
3788 }
3789 if ($invoice->type == FactureFournisseur::TYPE_DEPOSIT) {
3790 $depositamount += $obj->amount_ttc;
3791 }
3792 }
3793 } else {
3794 dol_print_error($db);
3795 }
3796
3797 // Paye partiellement 'escompte'
3798 if (($object->statut == FactureFournisseur::STATUS_CLOSED || $object->statut == FactureFournisseur::STATUS_ABANDONED) && $object->close_code == 'discount_vat') {
3799 print '<tr><td colspan="'.$nbcols.'" class="right nowrap">';
3800 print '<span class="opacitymedium">';
3801 print $form->textwithpicto($langs->trans("Discount"), $langs->trans("HelpEscompte"), - 1);
3802 print '</span>';
3803 print '</td><td class="right">'.price($object->total_ttc - $creditnoteamount - $depositamount - $totalpaid).'</td><td>&nbsp;</td></tr>';
3804 $resteapayeraffiche = 0;
3805 $cssforamountpaymentcomplete = 'amountpaymentneutral';
3806 }
3807 // Paye partiellement ou Abandon 'badsupplier'
3808 if (($object->statut == FactureFournisseur::STATUS_CLOSED || $object->statut == FactureFournisseur::STATUS_ABANDONED) && $object->close_code == 'badsupplier') {
3809 print '<tr><td colspan="'.$nbcols.'" class="right nowrap">';
3810 print '<span class="opacitymedium">';
3811 print $form->textwithpicto($langs->trans("Abandoned"), $langs->trans("HelpAbandonBadCustomer"), - 1);
3812 print '</span>';
3813 print '</td><td class="right">'.price($object->total_ttc - $creditnoteamount - $depositamount - $totalpaid).'</td><td>&nbsp;</td></tr>';
3814 // $resteapayeraffiche=0;
3815 $cssforamountpaymentcomplete = 'amountpaymentneutral';
3816 }
3817 // Paye partiellement ou Abandon 'product_returned'
3818 if (($object->statut == FactureFournisseur::STATUS_CLOSED || $object->statut == FactureFournisseur::STATUS_ABANDONED) && $object->close_code == 'product_returned') {
3819 print '<tr><td colspan="'.$nbcols.'" class="right nowrap">';
3820 print '<span class="opacitymedium">';
3821 print $form->textwithpicto($langs->trans("ProductReturned"), $langs->trans("HelpAbandonProductReturned"), - 1);
3822 print '</span>';
3823 print '</td><td class="right">'.price($object->total_ttc - $creditnoteamount - $depositamount - $totalpaid).'</td><td>&nbsp;</td></tr>';
3824 $resteapayeraffiche = 0;
3825 $cssforamountpaymentcomplete = 'amountpaymentneutral';
3826 }
3827 // Paye partiellement ou Abandon 'abandon'
3828 if (($object->statut == FactureFournisseur::STATUS_CLOSED || $object->statut == FactureFournisseur::STATUS_ABANDONED) && $object->close_code == 'abandon') {
3829 print '<tr><td colspan="'.$nbcols.'" class="right nowrap">';
3830 $text = $langs->trans("HelpAbandonOther");
3831 if ($object->close_note) {
3832 $text .= '<br><br><b>'.$langs->trans("Reason").'</b>:'.$object->close_note;
3833 }
3834 print '<span class="opacitymedium">';
3835 print $form->textwithpicto($langs->trans("Abandoned"), $text, - 1);
3836 print '</span>';
3837 print '</td><td class="right">'.price($object->total_ttc - $creditnoteamount - $depositamount - $totalpaid).'</td><td>&nbsp;</td></tr>';
3838 $resteapayeraffiche = 0;
3839 $cssforamountpaymentcomplete = 'amountpaymentneutral';
3840 }
3841
3842 // Billed
3843 print '<tr><td colspan="'.$nbcols.'" class="right">';
3844 print '<span class="opacitymedium">';
3845 print $langs->trans("Billed");
3846 print '</span>';
3847 print '</td><td class="right">'.price($object->total_ttc).'</td><td>&nbsp;</td></tr>';
3848
3849 // Remainder to pay
3850 print '<tr><td colspan="'.$nbcols.'" class="right">';
3851 print '<span class="opacitymedium">';
3852 print $langs->trans('RemainderToPay');
3853 if ($resteapayeraffiche < 0) {
3854 print ' ('.$langs->trans('NegativeIfExcessPaid').')';
3855 }
3856 print '</span>';
3857 print '</td>';
3858 print '<td class="right'.($resteapayeraffiche ? ' amountremaintopay' : (' '.$cssforamountpaymentcomplete)).'">'.price($resteapayeraffiche).'</td><td>&nbsp;</td></tr>';
3859
3860 // Remainder to pay Multicurrency
3861 if (isModEnabled('multicurrency') && $object->multicurrency_code != $conf->currency || $object->multicurrency_tx != 1) {
3862 print '<tr><td colspan="'.$nbcols.'" class="right">';
3863 print '<span class="opacitymedium">';
3864 print $langs->trans('RemainderToPayMulticurrency');
3865 if ($resteapayeraffiche < 0) {
3866 print ' ('.$langs->trans('NegativeIfExcessPaid').')';
3867 }
3868 print '</span>';
3869 print '</td>';
3870 print '<td class="right'.($resteapayeraffiche ? ' amountremaintopay' : (' '.$cssforamountpaymentcomplete)).'">'.(!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency).' '.price(price2num($multicurrency_resteapayer, 'MT')).'</td><td>&nbsp;</td></tr>';
3871 }
3872 } else { // Credit note
3873 $cssforamountpaymentcomplete = 'amountpaymentneutral';
3874
3875 // Total already paid back
3876 print '<tr><td colspan="'.$nbcols.'" class="right">';
3877 print $langs->trans('AlreadyPaidBack');
3878 print ' :</td><td class="right">'.price($sign * $totalpaid).'</td><td>&nbsp;</td></tr>';
3879
3880 // Billed
3881 print '<tr><td colspan="'.$nbcols.'" class="right">'.$langs->trans("Billed").' :</td><td class="right">'.price($sign * $object->total_ttc).'</td><td>&nbsp;</td></tr>';
3882
3883 // Remainder to pay back
3884 print '<tr><td colspan="'.$nbcols.'" class="right">';
3885 print '<span class="opacitymedium">';
3886 print $langs->trans('RemainderToPayBack');
3887 if ($resteapayeraffiche > 0) {
3888 print ' ('.$langs->trans('NegativeIfExcessRefunded').')';
3889 }
3890 print '</td>';
3891 print '</span>';
3892 print '<td class="right'.($resteapayeraffiche ? ' amountremaintopay' : (' '.$cssforamountpaymentcomplete)).'">'.price($sign * $resteapayeraffiche).'</td><td>&nbsp;</td></tr>';
3893
3894 // Remainder to pay back Multicurrency
3895 if (isModEnabled('multicurrency') && $object->multicurrency_code != $conf->currency || $object->multicurrency_tx != 1) {
3896 print '<tr><td colspan="'.$nbcols.'" class="right">';
3897 print '<span class="opacitymedium">';
3898 print $langs->trans('RemainderToPayBackMulticurrency');
3899 if ($resteapayeraffiche> 0) {
3900 print ' ('.$langs->trans('NegativeIfExcessRefunded').')';
3901 }
3902 print '</span>';
3903 print '</td>';
3904 print '<td class="right'.($resteapayeraffiche ? ' amountremaintopay' : (' '.$cssforamountpaymentcomplete)).'">'.(!empty($object->multicurrency_code) ? $object->multicurrency_code : $conf->currency).' '.price(price2num($sign * $object->multicurrency_tx * $resteapayeraffiche, 'MT')).'</td><td>&nbsp;</td></tr>';
3905 }
3906
3907 // Sold credit note
3908 // print '<tr><td colspan="'.$nbcols.'" class="right">'.$langs->trans('TotalTTC').' :</td>';
3909 // print '<td class="right" style="border: 1px solid;" bgcolor="#f0f0f0"><b>'.price($sign *
3910 // $object->total_ttc).'</b></td><td>&nbsp;</td></tr>';
3911 }
3912
3913 print '</table>';
3914 print '</div>';
3915
3916 print '</div>';
3917 print '</div>';
3918
3919 print '<div class="clearboth"></div><br>';
3920
3921 if (getDolGlobalString('MAIN_DISABLE_CONTACTS_TAB')) {
3922 $blocname = 'contacts';
3923 $title = $langs->trans('ContactsAddresses');
3924 include DOL_DOCUMENT_ROOT.'/core/tpl/bloc_showhide.tpl.php';
3925 }
3926
3927 if (getDolGlobalString('MAIN_DISABLE_NOTES_TAB')) {
3928 $colwidth = 20;
3929 $blocname = 'notes';
3930 $title = $langs->trans('Notes');
3931 include DOL_DOCUMENT_ROOT.'/core/tpl/bloc_showhide.tpl.php';
3932 }
3933
3934
3935 /*
3936 * Lines
3937 */
3938 print '<form name="addproduct" id="addproduct" action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
3939 print '<input type="hidden" name="token" value="'.newToken().'">';
3940 print '<input type="hidden" name="action" value="'.(($action != 'editline') ? 'addline' : 'updateline').'">';
3941 print '<input type="hidden" name="mode" value="">';
3942 print '<input type="hidden" name="page_y" value="">';
3943 print '<input type="hidden" name="id" value="'.$object->id.'">';
3944 print '<input type="hidden" name="socid" value="'.$societe->id.'">';
3945 print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
3946
3947 if (!empty($conf->use_javascript_ajax) && $object->statut == FactureFournisseur::STATUS_DRAFT) {
3948 include DOL_DOCUMENT_ROOT.'/core/tpl/ajaxrow.tpl.php';
3949 }
3950
3951 print '<div class="div-table-responsive-no-min">';
3952 print '<table id="tablelines" class="noborder noshadow centpercent">';
3953
3954 global $forceall, $senderissupplier, $dateSelector, $inputalsopricewithtax;
3955 $forceall = 1;
3956 $dateSelector = 0;
3957 $inputalsopricewithtax = 1;
3958 $senderissupplier = 2; // $senderissupplier=2 is same than 1 but disable test on minimum qty and disable autofill qty with minimum.
3959 //if (!empty($conf->global->SUPPLIER_INVOICE_WITH_NOPRICEDEFINED)) $senderissupplier=2;
3960 if (getDolGlobalString('SUPPLIER_INVOICE_WITH_PREDEFINED_PRICES_ONLY')) {
3961 $senderissupplier = 1;
3962 }
3963
3964 // Show object lines (result may vary according to hidden option MAIN_NO_INPUT_PRICE_WITH_TAX)
3965 if (!empty($object->lines)) {
3966 $object->printObjectLines($action, $societe, $mysoc, $lineid, 1);
3967 }
3968
3969 $num = count($object->lines);
3970
3971 // Form to add new line
3972 if ($object->statut == FactureFournisseur::STATUS_DRAFT && $usercancreate) {
3973 if ($action != 'editline') {
3974 // Add free products/services
3975
3976 $parameters = array();
3977 $reshook = $hookmanager->executeHooks('formAddObjectLine', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
3978 if ($reshook < 0) {
3979 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
3980 }
3981 if (empty($reshook)) {
3982 $object->formAddObjectLine(1, $societe, $mysoc);
3983 }
3984 }
3985 }
3986
3987 print '</table>';
3988 print '</div>';
3989 print '</form>';
3990 }
3991
3992 print dol_get_fiche_end();
3993
3994
3995 if ($action != 'presend') {
3996 /*
3997 * Buttons actions
3998 */
3999
4000 print '<div class="tabsAction">';
4001
4002 $parameters = array();
4003 $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been
4004 // modified by hook
4005 if (empty($reshook)) {
4006 // Modify a validated invoice with no payments
4007 if ($object->statut == FactureFournisseur::STATUS_VALIDATED && $action != 'confirm_edit' && $object->getSommePaiement() == 0 && $usercancreate) {
4008 // We check if lines of invoice are not already transfered into accountancy
4009 $ventilExportCompta = $object->getVentilExportCompta(); // Should be 0 since the sum of payments are zero. But we keep the protection.
4010
4011 if ($ventilExportCompta == 0) {
4012 print '<a class="butAction'.($conf->use_javascript_ajax ? ' reposition' : '').'" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=edit&token='.newToken().'">'.$langs->trans('Modify').'</a>';
4013 } else {
4014 print '<span class="butActionRefused classfortooltip" title="'.$langs->trans("DisabledBecauseDispatchedInBookkeeping").'">'.$langs->trans('Modify').'</span>';
4015 }
4016 }
4017
4018 $discount = new DiscountAbsolute($db);
4019 $result = $discount->fetch(0, 0, $object->id);
4020
4021 // Reopen a standard paid invoice
4022 if (($object->type == FactureFournisseur::TYPE_STANDARD || $object->type == FactureFournisseur::TYPE_REPLACEMENT
4023 || ($object->type == FactureFournisseur::TYPE_CREDIT_NOTE && empty($discount->id))
4024 || ($object->type == FactureFournisseur::TYPE_DEPOSIT && empty($discount->id)))
4025 && ($object->statut == FactureFournisseur::STATUS_CLOSED || $object->statut == FactureFournisseur::STATUS_ABANDONED)) { // A paid invoice (partially or completely)
4026 if (!$objectidnext && $object->close_code != 'replaced' && $usercancreate) { // Not replaced by another invoice
4027 print '<a class="butAction'.($conf->use_javascript_ajax ? ' reposition' : '').'" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=reopen&token='.newToken().'">'.$langs->trans('ReOpen').'</a>';
4028 } else {
4029 if ($usercancreate) {
4030 print '<span class="butActionRefused classfortooltip" title="'.$langs->trans("DisabledBecauseReplacedInvoice").'">'.$langs->trans('ReOpen').'</span>';
4031 } elseif (!getDolGlobalString('MAIN_BUTTON_HIDE_UNAUTHORIZED')) {
4032 print '<span class="butActionRefused classfortooltip">'.$langs->trans('ReOpen').'</span>';
4033 }
4034 }
4035 }
4036
4037 // Validate
4038 if ($action != 'confirm_edit' && $object->statut == FactureFournisseur::STATUS_DRAFT) {
4039 if (count($object->lines)) {
4040 if ($usercanvalidate) {
4041 print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=valid"';
4042 print '>'.$langs->trans('Validate').'</a>';
4043 } else {
4044 print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->trans("NotAllowed")).'"';
4045 print '>'.$langs->trans('Validate').'</a>';
4046 }
4047 }
4048 }
4049
4050 // Send by mail
4051 if (empty($user->socid)) {
4052 if (($object->statut == FactureFournisseur::STATUS_VALIDATED || $object->statut == FactureFournisseur::STATUS_CLOSED)) {
4053 if ($usercansend) {
4054 print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=presend&mode=init#formmailbeforetitle">'.$langs->trans('SendMail').'</a>';
4055 } else {
4056 print '<span class="butActionRefused classfortooltip">'.$langs->trans('SendMail').'</span>';
4057 }
4058 }
4059 }
4060
4061 // Create payment
4062 if ($object->type != FactureFournisseur::TYPE_CREDIT_NOTE && $object->statut == FactureFournisseur::STATUS_VALIDATED && $object->paye == 0) {
4063 print '<a class="butAction'.($conf->use_javascript_ajax ? ' reposition' : '').'" href="'.DOL_URL_ROOT.'/fourn/facture/paiement.php?facid='.$object->id.'&amp;action=create'.($object->fk_account > 0 ? '&amp;accountid='.$object->fk_account : '').'">'.$langs->trans('DoPayment').'</a>'; // must use facid because id is for payment id not invoice
4064 }
4065
4066 // Reverse back money or convert to reduction
4067 if ($object->type == FactureFournisseur::TYPE_CREDIT_NOTE || $object->type == FactureFournisseur::TYPE_DEPOSIT || $object->type == FactureFournisseur::TYPE_STANDARD) {
4068 // For credit note only
4069 if ($object->type == FactureFournisseur::TYPE_CREDIT_NOTE && $object->statut == 1 && $object->paye == 0) {
4070 if ($resteapayer == 0) {
4071 print '<span class="butActionRefused classfortooltip" title="'.$langs->trans("DisabledBecauseRemainderToPayIsZero").'">'.$langs->trans('DoPaymentBack').'</span>';
4072 } else {
4073 print '<a class="butAction" href="'.DOL_URL_ROOT.'/fourn/facture/paiement.php?facid='.$object->id.'&amp;action=create&amp;accountid='.$object->fk_account.'">'.$langs->trans('DoPaymentBack').'</a>';
4074 }
4075 }
4076
4077 // For standard invoice with excess paid
4078 if ($object->type == FactureFournisseur::TYPE_STANDARD && empty($object->paye) && ($object->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits) < 0 && $usercancreate && empty($discount->id)) {
4079 print '<a class="butAction'.($conf->use_javascript_ajax ? ' reposition' : '').'" href="'.$_SERVER["PHP_SELF"].'?facid='.$object->id.'&amp;action=converttoreduc">'.$langs->trans('ConvertExcessPaidToReduc').'</a>';
4080 }
4081 // For credit note
4082 if ($object->type == FactureFournisseur::TYPE_CREDIT_NOTE && $object->statut == 1 && $object->paye == 0 && $usercancreate
4083 && (getDolGlobalString('SUPPLIER_INVOICE_ALLOW_REUSE_OF_CREDIT_WHEN_PARTIALLY_REFUNDED') || $object->getSommePaiement() == 0)
4084 ) {
4085 print '<a class="butAction'.($conf->use_javascript_ajax ? ' reposition' : '').'" href="'.$_SERVER["PHP_SELF"].'?facid='.$object->id.'&amp;action=converttoreduc" title="'.dol_escape_htmltag($langs->trans("ConfirmConvertToReducSupplier2")).'">'.$langs->trans('ConvertToReduc').'</a>';
4086 }
4087 // For deposit invoice
4088 if ($object->type == FactureFournisseur::TYPE_DEPOSIT && $usercancreate && $object->statut > 0 && empty($discount->id)) {
4089 print '<a class="butAction'.($conf->use_javascript_ajax ? ' reposition' : '').'" href="'.$_SERVER["PHP_SELF"].'?facid='.$object->id.'&amp;action=converttoreduc">'.$langs->trans('ConvertToReduc').'</a>';
4090 }
4091 }
4092
4093 // Classify paid
4094 if ($object->statut == FactureFournisseur::STATUS_VALIDATED && $object->paye == 0 && (
4095 ($object->type != FactureFournisseur::TYPE_CREDIT_NOTE && $object->type != FactureFournisseur::TYPE_DEPOSIT && ($resteapayer <= 0 || (getDolGlobalString('SUPPLIER_INVOICE_CAN_SET_PAID_EVEN_IF_PARTIALLY_PAID') && $object->total_ttc == $resteapayer))) ||
4096 ($object->type == FactureFournisseur::TYPE_CREDIT_NOTE && $resteapayer >= 0) ||
4097 ($object->type == FactureFournisseur::TYPE_DEPOSIT && $object->total_ttc > 0 && ($resteapayer == 0 || (getDolGlobalString('SUPPLIER_INVOICE_CAN_SET_PAID_EVEN_IF_PARTIALLY_PAID') && $object->total_ttc == $resteapayer)))
4098 )
4099 ) {
4100 print '<a class="butAction'.($conf->use_javascript_ajax ? ' reposition' : '').'" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=paid">'.$langs->trans('ClassifyPaid').'</a>';
4101 }
4102
4103 // Classify 'closed not completely paid' (possible if validated and not yet filed paid)
4104 if ($object->statut == FactureFournisseur::STATUS_VALIDATED && $object->paye == 0 && $resteapayer > 0 && (!getDolGlobalString('SUPPLIER_INVOICE_CAN_SET_PAID_EVEN_IF_PARTIALLY_PAID') || $object->total_ttc != $resteapayer)) {
4105 if ($totalpaid > 0 || $totalcreditnotes > 0) {
4106 // If one payment or one credit note was linked to this invoice
4107 print '<a class="butAction'.($conf->use_javascript_ajax ? ' reposition' : '').'" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;action=paid">'.$langs->trans('ClassifyPaidPartially').'</a>';
4108 } else {
4109 if (!getDolGlobalString('INVOICE_CAN_NEVER_BE_CANCELED')) {
4110 print '<a class="butAction'.($conf->use_javascript_ajax ? ' reposition' : '').'" href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;action=canceled">'.$langs->trans('ClassifyCanceled').'</a>';
4111 }
4112 }
4113 }
4114
4115 // Create event
4116 /*if (isModEnabled('agenda') && !empty($conf->global->MAIN_ADD_EVENT_ON_ELEMENT_CARD)) // Add hidden condition because this is not a "workflow" action so should appears somewhere else on page.
4117 {
4118 print '<div class="inline-block divButAction"><a class="butAction" href="' . DOL_URL_ROOT . '/comm/action/card.php?action=create&amp;origin=' . $object->element . '&amp;originid=' . $object->id . '&amp;socid=' . $object->socid . '">' . $langs->trans("AddAction") . '</a></div>';
4119 }*/
4120
4121 // Create a credit note
4122 if (($object->type == FactureFournisseur::TYPE_STANDARD || $object->type == FactureFournisseur::TYPE_DEPOSIT) && $object->statut > 0 && $usercancreate) {
4123 if (!$objectidnext) {
4124 print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?socid='.$object->socid.'&amp;fac_avoir='.$object->id.'&amp;action=create&amp;type=2'.($object->fk_project > 0 ? '&amp;projectid='.$object->fk_project : '').'">'.$langs->trans("CreateCreditNote").'</a>';
4125 }
4126 }
4127
4128 // Clone
4129 if ($action != 'edit' && $usercancreate) {
4130 print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=clone&amp;socid='.$object->socid.'">'.$langs->trans('ToClone').'</a>';
4131 }
4132
4133 // Clone as predefined / Create template
4134 if (($object->type == FactureFournisseur::TYPE_STANDARD || $object->type == FactureFournisseur::TYPE_DEPOSIT) && $object->statut == 0 && $usercancreate) {
4135 if (!$objectidnext && count($object->lines) > 0) {
4136 print '<a class="butAction" href="'.DOL_URL_ROOT.'/fourn/facture/card-rec.php?facid='.$object->id.'&amp;action=create">'.$langs->trans("ChangeIntoRepeatableInvoice").'</a>';
4137 }
4138 }
4139
4140 // Delete
4141 $isErasable = $object->is_erasable();
4142 if ($action != 'confirm_edit' && ($usercandelete || ($usercancreate && $isErasable == 1))) { // isErasable = 1 means draft with temporary ref (draft can always be deleted with no need of permissions)
4143 $enableDelete = false;
4144 $htmltooltip = '';
4145 $params = (empty($conf->use_javascript_ajax) ? array() : array('attr' => array('class' => 'reposition')));
4146 //var_dump($isErasable); var_dump($params);
4147 if ($isErasable == -4) {
4148 $htmltooltip = $langs->trans("DisabledBecausePayments");
4149 } elseif ($isErasable == -3) { // Should never happen with supplier invoice
4150 $htmltooltip = $langs->trans("DisabledBecauseNotLastSituationInvoice");
4151 } elseif ($isErasable == -2) { // Should never happen with supplier invoice
4152 $htmltooltip = $langs->trans("DisabledBecauseNotLastInvoice");
4153 } elseif ($isErasable == -1) {
4154 $htmltooltip = $langs->trans("DisabledBecauseDispatchedInBookkeeping");
4155 } elseif ($isErasable <= 0) { // Any other cases
4156 $htmltooltip = $langs->trans("DisabledBecauseNotErasable");
4157 } else {
4158 $enableDelete = true;
4159 $htmltooltip = '';
4160 }
4161 print dolGetButtonAction($htmltooltip, $langs->trans("Delete"), 'delete', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete&token='.newToken(), $object->id, $enableDelete, $params);
4162 }
4163 print '</div>';
4164
4165 if ($action != 'confirm_edit') {
4166 print '<div class="fichecenter"><div class="fichehalfleft">';
4167
4168 /*
4169 * Generated documents
4170 */
4171 $ref = dol_sanitizeFileName($object->ref);
4172 $subdir = get_exdir($object->id, 2, 0, 0, $object, 'invoice_supplier').$ref;
4173 $filedir = $conf->fournisseur->facture->dir_output.'/'.$subdir;
4174 $urlsource = $_SERVER['PHP_SELF'].'?id='.$object->id;
4175 $genallowed = $usercanread;
4176 $delallowed = $usercancreate;
4177 $modelpdf = (!empty($object->model_pdf) ? $object->model_pdf : (!getDolGlobalString('INVOICE_SUPPLIER_ADDON_PDF') ? '' : $conf->global->INVOICE_SUPPLIER_ADDON_PDF));
4178
4179 print $formfile->showdocuments('facture_fournisseur', $subdir, $filedir, $urlsource, $genallowed, $delallowed, $modelpdf, 1, 0, 0, 40, 0, '', '', '', $societe->default_lang);
4180 $somethingshown = $formfile->numoffiles;
4181
4182 // Show links to link elements
4183 $linktoelem = $form->showLinkToObjectBlock($object, null, array('invoice_supplier'));
4184 $somethingshown = $form->showLinkedObjectBlock($object, $linktoelem);
4185
4186 print '</div><div class="fichehalfright">';
4187
4188 // List of actions on element
4189 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formactions.class.php';
4190 $formactions = new FormActions($db);
4191 $somethingshown = $formactions->showactions($object, 'invoice_supplier', $socid, 1, 'listaction'.($genallowed ? 'largetitle' : ''));
4192
4193 print '</div></div>';
4194 }
4195 }
4196 }
4197
4198 // Select mail models is same action as presend
4199 if (GETPOST('modelselected')) {
4200 $action = 'presend';
4201 }
4202
4203 // Presend form
4204 $modelmail = 'invoice_supplier_send';
4205 $defaulttopic = 'SendBillRef';
4206 $diroutput = $conf->fournisseur->facture->dir_output;
4207 $autocopy = 'MAIN_MAIL_AUTOCOPY_SUPPLIER_INVOICE_TO';
4208 $trackid = 'sinv'.$object->id;
4209
4210 include DOL_DOCUMENT_ROOT.'/core/tpl/card_presend.tpl.php';
4211 }
4212}
4213
4214
4215// End of page
4216llxFooter();
4217$db->close();
if(preg_match('/set_([a-z0-9_\-]+)/i', $action, $reg)) if(preg_match('/del_([a-z0-9_\-]+)/i', $action, $reg)) if($action=='set') elseif( $action=='specimen') elseif($action=='setmodel') elseif( $action=='del') elseif($action=='setdoc') $formactions
View.
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
Definition wrapper.php:55
llxFooter()
Empty footer.
Definition wrapper.php:69
Class to manage bank accounts.
Class to manage accounting journals.
const TYPE_SITUATION
Situation invoice.
Class to manage absolute discounts.
Class to manage a WYSIWYG editor.
Class to manage warehouses.
Class to manage standard extra fields.
Class to manage suppliers invoices.
const TYPE_DEPOSIT
Deposit invoice.
const TYPE_CREDIT_NOTE
Credit note invoice.
const TYPE_REPLACEMENT
Replacement invoice.
const STATUS_VALIDATED
Validated (need to be paid)
const TYPE_STANDARD
Standard invoice.
const STATUS_ABANDONED
Classified abandoned and no payment done.
const STATUS_CLOSED
Classified paid.
Class to manage invoice templates.
Class to manage building of HTML components.
Class to offer components to list and upload files.
Class to manage generation of HTML components Only common components must be here.
Class with static methods for building HTML components related to products Only components common to ...
Class to manage building of HTML components.
static getIdAndTxFromCode($dbs, $code, $date_document='')
Get id and rate of currency from code.
Class to manage payments for supplier invoices.
Class ProductCombination Used to represent a product combination.
Class to manage predefined suppliers products.
Class to manage products or services.
Class to manage projects.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage translations.
isInEEC($object)
Return if a country of an object is inside the EEC (European Economic Community)
dol_get_last_hour($date, $gm='tzserver')
Return GMT time for last hour of a given GMT date (it replaces hours, min and second part to 23:59:59...
Definition date.lib.php:640
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition date.lib.php:125
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
facturefourn_prepare_head(FactureFournisseur $object)
Prepare array with list of tabs.
Definition fourn.lib.php:36
dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='rowid', $fieldref='ref', $morehtmlref='', $moreparam='', $nodbprefix=0, $morehtmlleft='', $morehtmlstatus='', $onlybanner=0, $morehtmlright='')
Show tab footer of a card.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
img_delete($titlealt='default', $other='class="pictodelete"', $morecss='')
Show delete logo.
GETPOSTINT($paramname, $method=0)
Return value of a param into GET or POST supervariable.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_get_fiche_end($notab=0)
Return tab footer of a card.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_clone($object, $native=0)
Create a clone of instance of object (new instance with same value for each properties) With native =...
dolGetButtonAction($label, $text='', $actionType='default', $url='', $id='', $userRight=1, $params=array())
Function dolGetButtonAction.
get_default_npr(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
Function that returns whether VAT must be recoverable collected VAT (e.g.: VAT NPR in France)
dol_concatdesc($text1, $text2, $forxml=false, $invert=false)
Concat 2 descriptions with a new line between them (second operand after first one with appropriate n...
dol_htmloutput_events($disabledoutputofmessages=0)
Print formated messages to output (Used to show messages on html output).
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='')
Show information for admin users or standard users.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
get_localtax($vatrate, $local, $thirdparty_buyer="", $thirdparty_seller="", $vatnpr=0)
Return localtax rate for a particular vat, when selling a product with vat $vatrate,...
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
img_edit($titlealt='default', $float=0, $other='')
Show logo editer/modifier fiche.
get_default_tva(Societe $thirdparty_seller, Societe $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
Function that return vat rate of a product line (according to seller, buyer and product vat rate) VAT...
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.