2021-11-07 17:48:01 +01:00
< ? php
2019-10-27 09:51:15 +01:00
/* Copyright ( C ) 2002 - 2005 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2020-07-25 02:59:49 +02:00
* Copyright ( C ) 2004 - 2020 Laurent Destailleur < eldy @ users . sourceforge . net >
2012-10-28 13:57:21 +01:00
* Copyright ( C ) 2004 Christophe Combelles < ccomb @ free . fr >
* Copyright ( C ) 2005 Marc Barilley < marc @ ocebo . fr >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2005 - 2013 Regis Houssin < regis . houssin @ inodbox . com >
2019-06-14 18:28:01 +02:00
* Copyright ( C ) 2010 - 2019 Juanjo Menent < jmenent @ 2 byte . es >
2015-12-10 16:14:37 +01:00
* Copyright ( C ) 2013 - 2015 Philippe Grand < philippe . grand @ atoo - net . com >
2016-10-02 21:39:24 +02:00
* Copyright ( C ) 2013 Florian Henry < florian . henry @ open - concept . pro >
2016-11-06 15:01:40 +01:00
* Copyright ( C ) 2014 - 2016 Marcos García < marcosgdf @ gmail . com >
2022-06-15 04:46:58 +02:00
* Copyright ( C ) 2016 - 2022 Alexandre Spangaro < aspangaro @ open - dsi . fr >
2021-02-27 09:18:03 +01:00
* Copyright ( C ) 2018 - 2021 Frédéric France < frederic . france @ netlogic . fr >
2019-09-17 09:43:15 +02:00
* Copyright ( C ) 2019 Ferran Marcet < fmarcet @ 2 byte . es >
2022-06-15 04:46:58 +02:00
* Copyright ( C ) 2022 Gauthier VERDOL < gauthier . verdol @ atm - consulting . fr >
2012-10-28 13:57:21 +01:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2012-10-28 13:57:21 +01:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2012-10-28 13:57:21 +01:00
*/
/**
2014-09-18 21:18:25 +02:00
* \file htdocs / fourn / facture / card . php
2012-10-28 13:57:21 +01:00
* \ingroup facture , fournisseur
* \brief Page for supplier invoice card ( view , edit , validate )
*/
require '../../main.inc.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/class/html.formfile.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/modules/supplier_invoice/modules_facturefournisseur.php' ;
require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php' ;
2022-02-07 14:34:27 +01:00
require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture-rec.class.php' ;
2012-10-28 13:57:21 +01:00
require_once DOL_DOCUMENT_ROOT . '/fourn/class/paiementfourn.class.php' ;
2018-02-14 10:47:10 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/class/discount.class.php' ;
2012-10-28 13:57:21 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/fourn.lib.php' ;
2013-01-23 11:28:11 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2021-02-27 09:18:03 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php' ;
2013-04-09 17:18:07 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php' ;
2019-06-24 21:18:49 +02:00
if ( ! empty ( $conf -> product -> enabled )) {
2013-01-23 11:28:11 +01:00
require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php' ;
2020-06-11 16:52:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/product.lib.php' ;
2018-10-27 18:07:05 +02:00
}
2022-06-14 17:53:17 +02:00
if ( ! empty ( $conf -> project -> enabled )) {
2013-01-23 11:28:11 +01:00
require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php' ;
2013-06-14 22:33:01 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/html.formprojet.class.php' ;
}
2012-10-28 13:57:21 +01:00
2017-02-08 12:37:38 +01:00
if ( ! empty ( $conf -> variants -> enabled )) {
require_once DOL_DOCUMENT_ROOT . '/variants/class/ProductCombination.class.php' ;
2016-07-23 16:37:21 +02:00
}
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> accounting -> enabled )) {
require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingjournal.class.php' ;
}
2016-07-23 16:37:21 +02:00
2012-10-28 13:57:21 +01:00
2020-10-25 08:56:34 +01:00
$langs -> loadLangs ( array ( 'bills' , 'compta' , 'suppliers' , 'companies' , 'products' , 'banks' , 'admin' ));
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> incoterm -> enabled )) {
$langs -> load ( 'incoterm' );
}
2012-10-28 13:57:21 +01:00
2019-11-08 10:53:31 +01:00
$id = ( GETPOST ( 'facid' , 'int' ) ? GETPOST ( 'facid' , 'int' ) : GETPOST ( 'id' , 'int' ));
$socid = GETPOST ( 'socid' , 'int' );
2019-01-27 11:55:16 +01:00
$action = GETPOST ( 'action' , 'aZ09' );
2012-10-28 13:57:21 +01:00
$confirm = GETPOST ( " confirm " );
2019-11-08 10:53:31 +01:00
$ref = GETPOST ( 'ref' , 'alpha' );
2019-01-27 11:55:16 +01:00
$cancel = GETPOST ( 'cancel' , 'alpha' );
2016-10-02 21:39:24 +02:00
$lineid = GETPOST ( 'lineid' , 'int' );
2019-11-08 10:53:31 +01:00
$projectid = GETPOST ( 'projectid' , 'int' );
2016-10-02 21:39:24 +02:00
$origin = GETPOST ( 'origin' , 'alpha' );
2019-11-08 10:53:31 +01:00
$originid = GETPOST ( 'originid' , 'int' );
2022-04-05 12:16:06 +02:00
$fac_recid = GETPOST ( 'fac_rec' , 'int' );
2021-11-16 14:09:18 +01:00
$rank = ( GETPOST ( 'rank' , 'int' ) > 0 ) ? GETPOST ( 'rank' , 'int' ) : - 1 ;
2012-10-28 13:57:21 +01:00
2017-05-05 12:33:49 +02:00
// PDF
2019-11-08 10:53:31 +01:00
$hidedetails = ( GETPOST ( 'hidedetails' , 'int' ) ? GETPOST ( 'hidedetails' , 'int' ) : ( ! empty ( $conf -> global -> MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS ) ? 1 : 0 ));
$hidedesc = ( GETPOST ( 'hidedesc' , 'int' ) ? GETPOST ( 'hidedesc' , 'int' ) : ( ! empty ( $conf -> global -> MAIN_GENERATE_DOCUMENTS_HIDE_DESC ) ? 1 : 0 ));
$hideref = ( GETPOST ( 'hideref' , 'int' ) ? GETPOST ( 'hideref' , 'int' ) : ( ! empty ( $conf -> global -> MAIN_GENERATE_DOCUMENTS_HIDE_REF ) ? 1 : 0 ));
2012-10-28 13:57:21 +01:00
2017-06-10 12:56:28 +02:00
// Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
2019-11-08 10:53:31 +01:00
$hookmanager -> initHooks ( array ( 'invoicesuppliercard' , 'globalcard' ));
2012-10-28 13:57:21 +01:00
2019-11-08 10:53:31 +01:00
$object = new FactureFournisseur ( $db );
2014-10-14 17:00:06 +02:00
$extrafields = new ExtraFields ( $db );
// fetch optionals attributes and labels
2019-10-06 14:41:52 +02:00
$extrafields -> fetch_name_optionals_label ( $object -> table_element );
2012-10-28 13:57:21 +01:00
2013-03-29 15:36:54 +01:00
// Load object
2021-02-25 23:21:30 +01:00
if ( $id > 0 || ! empty ( $ref )) {
2019-11-08 10:53:31 +01:00
$ret = $object -> fetch ( $id , $ref );
2021-02-25 23:21:30 +01:00
if ( $ret < 0 ) {
dol_print_error ( $db , $object -> error );
}
2019-11-08 10:53:31 +01:00
$ret = $object -> fetch_thirdparty ();
2021-02-25 23:21:30 +01:00
if ( $ret < 0 ) {
dol_print_error ( $db , $object -> error );
}
2013-03-29 15:36:54 +01:00
}
2018-10-29 18:19:40 +01:00
// Security check
2019-11-08 10:53:31 +01:00
$socid = '' ;
2021-02-25 23:21:30 +01:00
if ( ! empty ( $user -> socid )) {
$socid = $user -> socid ;
}
2018-10-29 18:28:19 +01:00
$isdraft = (( $object -> statut == FactureFournisseur :: STATUS_DRAFT ) ? 1 : 0 );
2018-10-30 14:47:39 +01:00
$result = restrictedArea ( $user , 'fournisseur' , $id , 'facture_fourn' , 'facture' , 'fk_soc' , 'rowid' , $isdraft );
2018-10-29 18:19:40 +01:00
2020-04-21 12:25:44 +02:00
// Common permissions
2021-04-12 07:37:34 +02:00
$usercanread = ( $user -> rights -> fournisseur -> facture -> lire || $user -> rights -> supplier_invoice -> lire );
$usercancreate = ( $user -> rights -> fournisseur -> facture -> creer || $user -> rights -> supplier_invoice -> creer );
$usercandelete = ( $user -> rights -> fournisseur -> facture -> supprimer || $user -> rights -> supplier_invoice -> supprimer );
2020-01-01 19:04:14 +01:00
2020-04-21 12:25:44 +02:00
// Advanced permissions
2020-10-31 14:32:18 +01:00
$usercanvalidate = (( empty ( $conf -> global -> MAIN_USE_ADVANCED_PERMS ) && ! empty ( $usercancreate )) || ( ! empty ( $conf -> global -> MAIN_USE_ADVANCED_PERMS ) && ! empty ( $user -> rights -> fournisseur -> supplier_invoice_advance -> validate )));
$usercansend = ( empty ( $conf -> global -> MAIN_USE_ADVANCED_PERMS ) || $user -> rights -> fournisseur -> supplier_invoice_advance -> send );
2020-04-21 12:25:44 +02:00
// Permissions for includes
2021-04-06 09:15:34 +02:00
$permissionnote = $usercancreate ; // Used by the include of actions_setnotes.inc.php
2020-10-31 14:32:18 +01:00
$permissiondellink = $usercancreate ; // Used by the include of actions_dellink.inc.php
$permissiontoedit = $usercancreate ; // Used by the include of actions_lineupdown.inc.php
2021-04-06 09:15:34 +02:00
$permissiontoadd = $usercancreate ; // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
2012-10-28 13:57:21 +01:00
2020-12-23 23:16:27 +01:00
$error = 0 ;
2012-10-28 13:57:21 +01:00
/*
* Actions
2013-09-06 13:25:45 +02:00
*/
2012-10-28 13:57:21 +01:00
2019-11-08 10:53:31 +01:00
$parameters = array ( 'socid' => $socid );
$reshook = $hookmanager -> executeHooks ( 'doActions' , $parameters , $object , $action ); // Note that $action and $object may have been modified by some hooks
2021-02-25 23:21:30 +01:00
if ( $reshook < 0 ) {
setEventMessages ( $hookmanager -> error , $hookmanager -> errors , 'errors' );
}
2014-07-22 04:24:08 +02:00
2021-02-25 23:21:30 +01:00
if ( empty ( $reshook )) {
2021-09-02 21:35:36 +02:00
$backurlforlist = DOL_URL_ROOT . '/fourn/facture/list.php' ;
if ( empty ( $backtopage ) || ( $cancel && empty ( $id ))) {
if ( empty ( $backtopage ) || ( $cancel && strpos ( $backtopage , '__ID__' ))) {
if ( empty ( $id ) && (( $action != 'add' && $action != 'create' ) || $cancel )) {
$backtopage = $backurlforlist ;
} else {
$backtopage = DOL_URL_ROOT . '/fourn/facture/card.php?id=' . (( ! empty ( $id ) && $id > 0 ) ? $id : '__ID__' );
}
}
}
2021-02-25 23:21:30 +01:00
if ( $cancel ) {
2021-09-02 21:35:36 +02:00
if ( ! empty ( $backtopageforcancel )) {
header ( " Location: " . $backtopageforcancel );
exit ;
} elseif ( ! empty ( $backtopage )) {
2017-09-17 15:38:50 +02:00
header ( " Location: " . $backtopage );
exit ;
}
2019-11-08 10:53:31 +01:00
$action = '' ;
2017-09-17 15:38:50 +02:00
}
2012-10-28 13:57:21 +01:00
2019-11-08 10:53:31 +01:00
include DOL_DOCUMENT_ROOT . '/core/actions_setnotes.inc.php' ; // Must be include, not include_once
2012-10-28 13:57:21 +01:00
2019-11-08 10:53:31 +01:00
include DOL_DOCUMENT_ROOT . '/core/actions_dellink.inc.php' ; // Must be include, not include_once
2015-07-28 14:13:16 +02:00
2019-11-08 10:53:31 +01:00
include DOL_DOCUMENT_ROOT . '/core/actions_lineupdown.inc.php' ; // Must be include, not include_once
2012-10-28 13:57:21 +01:00
2016-02-16 22:26:23 +01:00
// Link invoice to order
2021-02-25 23:21:30 +01:00
if ( GETPOST ( 'linkedOrder' ) && empty ( $cancel ) && $id > 0 ) {
2017-10-16 08:47:05 +02:00
$object -> fetch ( $id );
$object -> fetch_thirdparty ();
$result = $object -> add_object_linked ( 'order_supplier' , GETPOST ( 'linkedOrder' ));
2016-02-16 22:26:23 +01:00
}
2016-08-10 09:47:25 +02:00
2015-02-28 03:11:15 +01:00
// Action clone object
2021-02-25 23:21:30 +01:00
if ( $action == 'confirm_clone' && $confirm == 'yes' && $permissiontoadd ) {
2020-10-07 15:01:28 +02:00
$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.
2019-04-12 12:12:08 +02:00
2021-02-25 23:21:30 +01:00
if ( GETPOST ( 'newsupplierref' , 'alphanohtml' )) {
$objectutil -> ref_supplier = GETPOST ( 'newsupplierref' , 'alphanohtml' );
}
2020-10-07 15:01:28 +02:00
$objectutil -> date = dol_mktime ( 12 , 0 , 0 , GETPOST ( 'newdatemonth' , 'int' ), GETPOST ( 'newdateday' , 'int' ), GETPOST ( 'newdateyear' , 'int' ));
2019-04-12 12:12:08 +02:00
2020-10-07 15:01:28 +02:00
$result = $objectutil -> createFromClone ( $user , $id );
2021-02-25 23:21:30 +01:00
if ( $result > 0 ) {
2020-10-07 15:01:28 +02:00
header ( " Location: " . $_SERVER [ 'PHP_SELF' ] . '?id=' . $result );
exit ;
} else {
$langs -> load ( " errors " );
setEventMessages ( $objectutil -> error , $objectutil -> errors , 'errors' );
$action = '' ;
}
2021-02-25 23:21:30 +01:00
} elseif ( $action == 'confirm_valid' && $confirm == 'yes' && $usercanvalidate ) {
2019-11-08 10:53:31 +01:00
$idwarehouse = GETPOST ( 'idwarehouse' );
2015-02-28 03:11:15 +01:00
2017-10-16 08:47:05 +02:00
$object -> fetch ( $id );
$object -> fetch_thirdparty ();
2015-02-28 03:11:15 +01:00
2019-11-08 10:53:31 +01:00
$qualified_for_stock_change = 0 ;
2021-02-25 23:21:30 +01:00
if ( empty ( $conf -> global -> STOCK_SUPPORTS_SERVICES )) {
2019-11-08 10:53:31 +01:00
$qualified_for_stock_change = $object -> hasProductsOrServices ( 2 );
2020-05-21 15:05:19 +02:00
} else {
2019-11-08 10:53:31 +01:00
$qualified_for_stock_change = $object -> hasProductsOrServices ( 1 );
2017-10-16 08:47:05 +02:00
}
2015-02-28 03:11:15 +01:00
2017-10-16 08:47:05 +02:00
// Check parameters
2020-12-23 23:16:27 +01:00
if ( ! empty ( $conf -> stock -> enabled ) && ! empty ( $conf -> global -> STOCK_CALCULATE_ON_SUPPLIER_BILL ) && $qualified_for_stock_change ) {
2017-10-16 08:47:05 +02:00
$langs -> load ( " stocks " );
2021-02-25 23:21:30 +01:00
if ( ! $idwarehouse || $idwarehouse == - 1 ) {
2017-10-16 08:47:05 +02:00
$error ++ ;
2019-01-27 11:55:16 +01:00
setEventMessages ( $langs -> trans ( 'ErrorFieldRequired' , $langs -> transnoentitiesnoconv ( " Warehouse " )), null , 'errors' );
2019-11-08 10:53:31 +01:00
$action = '' ;
2017-10-16 08:47:05 +02:00
}
}
2015-02-28 03:11:15 +01:00
2020-12-23 23:16:27 +01:00
if ( ! $error ) {
2019-01-27 11:55:16 +01:00
$result = $object -> validate ( $user , '' , $idwarehouse );
2020-12-23 23:16:27 +01:00
if ( $result < 0 ) {
2019-01-27 11:55:16 +01:00
setEventMessages ( $object -> error , $object -> errors , 'errors' );
2019-11-08 10:53:31 +01:00
} else {
2017-10-16 08:47:05 +02:00
// Define output language
2021-02-25 23:21:30 +01:00
if ( empty ( $conf -> global -> MAIN_DISABLE_PDF_AUTOUPDATE )) {
2017-10-16 08:47:05 +02:00
$outputlangs = $langs ;
$newlang = '' ;
2021-02-25 23:21:30 +01:00
if ( $conf -> global -> MAIN_MULTILANGS && empty ( $newlang ) && GETPOST ( 'lang_id' , 'aZ09' )) {
$newlang = GETPOST ( 'lang_id' , 'aZ09' );
}
if ( $conf -> global -> MAIN_MULTILANGS && empty ( $newlang )) {
$newlang = $object -> thirdparty -> default_lang ;
}
2019-11-08 10:53:31 +01:00
if ( ! empty ( $newlang )) {
2017-10-16 08:47:05 +02:00
$outputlangs = new Translate ( " " , $conf );
$outputlangs -> setDefaultLang ( $newlang );
}
2020-09-10 01:49:09 +02:00
$model = $object -> model_pdf ;
2017-10-16 08:47:05 +02:00
$ret = $object -> fetch ( $id ); // Reload to get new records
2017-06-07 16:44:04 +02:00
2019-11-08 10:53:31 +01:00
$result = $object -> generateDocument ( $model , $outputlangs , $hidedetails , $hidedesc , $hideref );
2021-02-25 23:21:30 +01:00
if ( $result < 0 ) {
dol_print_error ( $db , $result );
}
2017-10-16 08:47:05 +02:00
}
}
}
2020-12-23 23:16:27 +01:00
} elseif ( $action == 'confirm_delete' && $confirm == 'yes' ) {
2017-10-16 08:47:05 +02:00
$object -> fetch ( $id );
$object -> fetch_thirdparty ();
2018-10-29 14:56:00 +01:00
2019-11-08 10:53:31 +01:00
$isErasable = $object -> is_erasable ();
2018-10-29 14:56:00 +01:00
2021-02-25 23:21:30 +01:00
if (( $usercandelete && $isErasable > 0 ) || ( $usercancreate && $isErasable == 1 )) {
2019-11-08 10:53:31 +01:00
$result = $object -> delete ( $user );
2021-02-25 23:21:30 +01:00
if ( $result > 0 ) {
2018-10-29 14:56:00 +01:00
header ( 'Location: list.php?restore_lastsearch_values=1' );
exit ;
2020-05-21 15:05:19 +02:00
} else {
2018-10-29 14:56:00 +01:00
setEventMessages ( $object -> error , $object -> errors , 'errors' );
}
2017-10-16 08:47:05 +02:00
}
2021-02-25 23:21:30 +01:00
} elseif ( $action == 'confirm_deleteline' && $confirm == 'yes' && $usercancreate ) {
// Remove a product line
2015-02-28 04:59:27 +01:00
$result = $object -> deleteline ( $lineid );
2021-02-25 23:21:30 +01:00
if ( $result > 0 ) {
2021-11-17 12:10:12 +01:00
// reorder lines
$object -> line_order ( true );
2015-02-28 04:59:27 +01:00
// Define output language
2016-10-16 21:29:12 +02:00
/* $outputlangs = $langs ;
2015-02-28 04:59:27 +01:00
$newlang = '' ;
2017-06-27 17:27:01 +02:00
if ( $conf -> global -> MAIN_MULTILANGS && empty ( $newlang ) && GETPOST ( 'lang_id' , 'aZ09' ))
$newlang = GETPOST ( 'lang_id' , 'aZ09' );
2015-02-28 04:59:27 +01:00
if ( $conf -> global -> MAIN_MULTILANGS && empty ( $newlang ))
$newlang = $object -> thirdparty -> default_lang ;
if ( ! empty ( $newlang )) {
$outputlangs = new Translate ( " " , $conf );
$outputlangs -> setDefaultLang ( $newlang );
}
if ( empty ( $conf -> global -> MAIN_DISABLE_PDF_AUTOUPDATE )) {
$ret = $object -> fetch ( $object -> id ); // Reload to get new records
2020-09-10 01:49:09 +02:00
$object -> generateDocument ( $object -> model_pdf , $outputlangs , $hidedetails , $hidedesc , $hideref );
2016-10-16 21:29:12 +02:00
} */
2015-02-28 04:59:27 +01:00
header ( 'Location: ' . $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id );
2015-02-28 03:11:15 +01:00
exit ;
2020-05-21 15:05:19 +02:00
} else {
2015-12-10 16:14:37 +01:00
setEventMessages ( $object -> error , $object -> errors , 'errors' );
2015-02-28 03:11:15 +01:00
/* Fix bug 1485 : Reset action to avoid asking again confirmation on failure */
2019-11-08 10:53:31 +01:00
$action = '' ;
2015-02-28 03:11:15 +01:00
}
2021-02-25 23:21:30 +01:00
} elseif ( $action == 'unlinkdiscount' && $usercancreate ) {
// Delete link of credit note to invoice
2018-02-14 10:47:10 +01:00
$discount = new DiscountAbsolute ( $db );
$result = $discount -> fetch ( GETPOST ( " discountid " ));
2018-02-14 18:02:40 +01:00
$discount -> unlink_invoice ();
2020-05-26 00:25:38 +02:00
} elseif ( $action == 'confirm_paid' && $confirm == 'yes' && $usercancreate ) {
2017-10-16 08:47:05 +02:00
$object -> fetch ( $id );
2021-02-09 09:55:48 +01:00
$result = $object -> setPaid ( $user );
2020-12-23 23:16:27 +01:00
if ( $result < 0 ) {
2017-10-16 08:47:05 +02:00
setEventMessages ( $object -> error , $object -> errors , 'errors' );
}
2021-04-06 09:15:34 +02:00
} elseif ( $action == 'confirm_paid_partially' && $confirm == 'yes' ) {
// Classif "paid partialy"
$object -> fetch ( $id );
$close_code = GETPOST ( " close_code " , 'restricthtml' );
$close_note = GETPOST ( " close_note " , 'restricthtml' );
if ( $close_code ) {
$result = $object -> setPaid ( $user , $close_code , $close_note );
if ( $result < 0 ) {
setEventMessages ( $object -> error , $object -> errors , 'errors' );
}
} else {
setEventMessages ( $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " Reason " )), null , 'errors' );
}
} elseif ( $action == 'confirm_canceled' && $confirm == 'yes' ) {
// Classify "abandoned"
$object -> fetch ( $id );
$close_code = GETPOST ( " close_code " , 'restricthtml' );
$close_note = GETPOST ( " close_note " , 'restricthtml' );
if ( $close_code ) {
$result = $object -> setCanceled ( $user , $close_code , $close_note );
if ( $result < 0 ) {
setEventMessages ( $object -> error , $object -> errors , 'errors' );
}
} else {
setEventMessages ( $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " Reason " )), null , 'errors' );
}
2015-02-28 03:11:15 +01:00
}
2012-10-28 13:57:21 +01:00
2015-02-28 03:11:15 +01:00
// Set supplier ref
2020-12-23 23:16:27 +01:00
if ( $action == 'setref_supplier' && $usercancreate ) {
2016-04-16 18:44:31 +02:00
$object -> ref_supplier = GETPOST ( 'ref_supplier' , 'alpha' );
2016-08-10 09:47:25 +02:00
2016-04-30 13:58:35 +02:00
if ( $object -> update ( $user ) < 0 ) {
2016-04-23 13:44:43 +02:00
setEventMessages ( $object -> error , $object -> errors , 'errors' );
2020-05-21 15:05:19 +02:00
} else {
2017-10-16 08:47:05 +02:00
// Define output language
$outputlangs = $langs ;
$newlang = '' ;
2019-10-27 09:51:15 +01:00
if ( $conf -> global -> MAIN_MULTILANGS && empty ( $newlang ) && GETPOST ( 'lang_id' , 'aZ09' )) {
2019-01-27 11:55:16 +01:00
$newlang = GETPOST ( 'lang_id' , 'aZ09' );
2019-10-27 09:51:15 +01:00
}
if ( $conf -> global -> MAIN_MULTILANGS && empty ( $newlang )) {
$newlang = $object -> thirdparty -> default_lang ;
}
2019-11-08 10:53:31 +01:00
if ( ! empty ( $newlang )) {
2019-10-27 09:51:15 +01:00
$outputlangs = new Translate ( " " , $conf );
$outputlangs -> setDefaultLang ( $newlang );
}
if ( empty ( $conf -> global -> MAIN_DISABLE_PDF_AUTOUPDATE )) {
$ret = $object -> fetch ( $object -> id ); // Reload to get new records
2020-09-10 01:49:09 +02:00
$object -> generateDocument ( $object -> model_pdf , $outputlangs , $hidedetails , $hidedesc , $hideref );
2019-10-27 09:51:15 +01:00
}
2016-09-28 18:59:44 +02:00
}
2015-02-28 03:11:15 +01:00
}
2012-10-28 13:57:21 +01:00
2015-12-10 16:14:37 +01:00
// payments conditions
2020-12-23 23:16:27 +01:00
if ( $action == 'setconditions' && $usercancreate ) {
2020-10-31 18:51:30 +01:00
$object -> fetch ( $id );
$object -> cond_reglement_code = 0 ; // To clean property
$object -> cond_reglement_id = 0 ; // To clean property
$error = 0 ;
$db -> begin ();
if ( ! $error ) {
$result = $object -> setPaymentTerms ( GETPOST ( 'cond_reglement_id' , 'int' ));
if ( $result < 0 ) {
$error ++ ;
setEventMessages ( $object -> error , $object -> errors , 'errors' );
}
}
if ( ! $error ) {
$old_date_echeance = $object -> date_echeance ;
$new_date_echeance = $object -> calculate_date_lim_reglement ();
2021-02-25 23:21:30 +01:00
if ( $new_date_echeance > $old_date_echeance ) {
$object -> date_echeance = $new_date_echeance ;
}
if ( $object -> date_echeance < $object -> date ) {
$object -> date_echeance = $object -> date ;
}
2020-10-31 18:51:30 +01:00
$result = $object -> update ( $user );
if ( $result < 0 ) {
$error ++ ;
setEventMessages ( $object -> error , $object -> errors , 'errors' );
}
}
if ( $error ) {
$db -> rollback ();
} else {
$db -> commit ();
}
2021-02-25 23:21:30 +01:00
} elseif ( $action == 'set_incoterms' && ! empty ( $conf -> incoterm -> enabled )) {
// Set incoterm
2018-04-12 21:39:12 +02:00
$result = $object -> setIncoterms ( GETPOST ( 'incoterm_id' , 'int' ), GETPOST ( 'location_incoterms' , 'alpha' ));
2021-02-25 23:21:30 +01:00
} elseif ( $action == 'setmode' && $usercancreate ) {
// payment mode
2019-01-27 11:55:16 +01:00
$result = $object -> setPaymentMethods ( GETPOST ( 'mode_reglement_id' , 'int' ));
2021-02-25 23:21:30 +01:00
} elseif ( $action == 'setmulticurrencycode' && $usercancreate ) {
// Multicurrency Code
2016-02-19 22:25:59 +01:00
$result = $object -> setMulticurrencyCode ( GETPOST ( 'multicurrency_code' , 'alpha' ));
2021-02-25 23:21:30 +01:00
} elseif ( $action == 'setmulticurrencyrate' && $usercancreate ) {
// Multicurrency rate
2021-03-02 01:17:28 +01:00
$result = $object -> setMulticurrencyRate ( price2num ( GETPOST ( 'multicurrency_tx' , 'alpha' )), GETPOST ( 'calculation_mode' , 'int' ));
2021-02-25 23:21:30 +01:00
} elseif ( $action == 'setbankaccount' && $usercancreate ) {
// bank account
2019-11-08 10:53:31 +01:00
$result = $object -> setBankAccount ( GETPOST ( 'fk_account' , 'int' ));
2015-02-28 03:11:15 +01:00
}
2013-11-17 23:25:25 +01:00
2019-07-22 06:56:55 +02:00
2021-04-12 07:37:34 +02:00
if ( $action == 'settransportmode' && ( $user -> rights -> fournisseur -> facture -> creer || $user -> rights -> supplier_invoice -> creer )) {
2021-02-25 23:21:30 +01:00
// transport mode
$result = $object -> setTransportMode ( GETPOST ( 'transport_mode_id' , 'int' ));
} elseif ( $action == 'setlabel' && $usercancreate ) {
// Set label
2017-10-16 08:47:05 +02:00
$object -> fetch ( $id );
2019-11-08 10:53:31 +01:00
$object -> label = GETPOST ( 'label' );
$result = $object -> update ( $user );
2021-02-25 23:21:30 +01:00
if ( $result < 0 ) {
dol_print_error ( $db );
}
2020-05-26 00:25:38 +02:00
} elseif ( $action == 'setdatef' && $usercancreate ) {
2021-09-23 02:10:39 +02:00
$newdate = dol_mktime ( 0 , 0 , 0 , GETPOST ( 'datefmonth' , 'int' ), GETPOST ( 'datefday' , 'int' ), GETPOST ( 'datefyear' , 'int' ), 'tzserver' );
if ( $newdate > ( dol_now ( 'tzuserrel' ) + ( empty ( $conf -> global -> INVOICE_MAX_FUTURE_DELAY ) ? 0 : $conf -> global -> INVOICE_MAX_FUTURE_DELAY ))) {
if ( empty ( $conf -> global -> INVOICE_MAX_FUTURE_DELAY )) {
2021-02-25 23:21:30 +01:00
setEventMessages ( $langs -> trans ( " WarningInvoiceDateInFuture " ), null , 'warnings' );
} else {
setEventMessages ( $langs -> trans ( " WarningInvoiceDateTooFarInFuture " ), null , 'warnings' );
}
2017-10-16 08:47:05 +02:00
}
2017-02-02 20:07:41 +01:00
2017-10-16 08:47:05 +02:00
$object -> fetch ( $id );
2017-01-27 01:46:21 +01:00
2019-11-08 10:53:31 +01:00
$object -> date = $newdate ;
$date_echence_calc = $object -> calculate_date_lim_reglement ();
2021-02-25 23:21:30 +01:00
if ( ! empty ( $object -> date_echeance ) && $object -> date_echeance < $date_echence_calc ) {
2017-10-16 08:47:05 +02:00
$object -> date_echeance = $date_echence_calc ;
}
2021-02-25 23:21:30 +01:00
if ( $object -> date_echeance && $object -> date_echeance < $object -> date ) {
2019-11-08 10:53:31 +01:00
$object -> date_echeance = $object -> date ;
2017-10-16 08:47:05 +02:00
}
2017-01-27 01:46:21 +01:00
2019-11-08 10:53:31 +01:00
$result = $object -> update ( $user );
2021-02-25 23:21:30 +01:00
if ( $result < 0 ) {
dol_print_error ( $db , $object -> error );
}
2020-05-26 00:25:38 +02:00
} elseif ( $action == 'setdate_lim_reglement' && $usercancreate ) {
2017-10-16 08:47:05 +02:00
$object -> fetch ( $id );
2021-09-23 02:10:39 +02:00
$object -> date_echeance = dol_mktime ( 12 , 0 , 0 , GETPOST ( 'date_lim_reglementmonth' , 'int' ), GETPOST ( 'date_lim_reglementday' , 'int' ), GETPOST ( 'date_lim_reglementyear' , 'int' ));
2021-02-25 23:21:30 +01:00
if ( ! empty ( $object -> date_echeance ) && $object -> date_echeance < $object -> date ) {
2019-11-08 10:53:31 +01:00
$object -> date_echeance = $object -> date ;
2017-10-16 08:47:05 +02:00
setEventMessages ( $langs -> trans ( " DatePaymentTermCantBeLowerThanObjectDate " ), null , 'warnings' );
}
2019-11-08 10:53:31 +01:00
$result = $object -> update ( $user );
2021-02-25 23:21:30 +01:00
if ( $result < 0 ) {
dol_print_error ( $db , $object -> error );
}
} elseif ( $action == " setabsolutediscount " && $usercancreate ) {
2018-02-14 10:47:10 +01:00
// We use the credit to reduce amount of invoice
2021-03-25 16:59:47 +01:00
if ( GETPOST ( " remise_id " , " int " )) {
2018-02-14 10:47:10 +01:00
$ret = $object -> fetch ( $id );
if ( $ret > 0 ) {
2021-03-25 16:59:47 +01:00
$result = $object -> insert_discount ( GETPOST ( " remise_id " , " int " ));
2018-02-14 10:47:10 +01:00
if ( $result < 0 ) {
setEventMessages ( $object -> error , $object -> errors , 'errors' );
}
} else {
dol_print_error ( $db , $object -> error );
}
}
// We use the credit to reduce remain to pay
2021-03-25 16:59:47 +01:00
if ( GETPOST ( " remise_id_for_payment " , " int " )) {
2019-11-08 10:53:31 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/class/discount.class.php' ;
2018-02-14 10:47:10 +01:00
$discount = new DiscountAbsolute ( $db );
2021-03-25 16:59:47 +01:00
$discount -> fetch ( GETPOST ( " remise_id_for_payment " , " int " ));
2018-03-08 20:49:37 +01:00
2018-02-14 10:47:10 +01:00
//var_dump($object->getRemainToPay(0));
//var_dump($discount->amount_ttc);exit;
2021-02-25 23:21:30 +01:00
if ( price2num ( $discount -> amount_ttc ) > price2num ( $object -> getRemainToPay ( 0 ))) {
2018-02-14 10:47:10 +01:00
// TODO Split the discount in 2 automatically
$error ++ ;
setEventMessages ( $langs -> trans ( " ErrorDiscountLargerThanRemainToPaySplitItBefore " ), null , 'errors' );
}
2021-02-25 23:21:30 +01:00
if ( ! $error ) {
2018-02-14 18:02:40 +01:00
$result = $discount -> link_to_invoice ( 0 , $id );
2018-02-14 10:47:10 +01:00
if ( $result < 0 ) {
setEventMessages ( $discount -> error , $discount -> errors , 'errors' );
}
}
}
2018-03-08 20:49:37 +01:00
2021-02-25 23:21:30 +01:00
if ( empty ( $conf -> global -> MAIN_DISABLE_PDF_AUTOUPDATE )) {
2018-02-14 10:47:10 +01:00
$outputlangs = $langs ;
$newlang = '' ;
2021-02-25 23:21:30 +01:00
if ( $conf -> global -> MAIN_MULTILANGS && empty ( $newlang ) && GETPOST ( 'lang_id' , 'aZ09' )) {
$newlang = GETPOST ( 'lang_id' , 'aZ09' );
}
if ( $conf -> global -> MAIN_MULTILANGS && empty ( $newlang )) {
$newlang = $object -> thirdparty -> default_lang ;
}
2019-11-08 10:53:31 +01:00
if ( ! empty ( $newlang )) {
2018-02-14 10:47:10 +01:00
$outputlangs = new Translate ( " " , $conf );
$outputlangs -> setDefaultLang ( $newlang );
}
$ret = $object -> fetch ( $id ); // Reload to get new records
2018-03-08 20:49:37 +01:00
2020-09-10 01:49:09 +02:00
$result = $object -> generateDocument ( $object -> model_pdf , $outputlangs , $hidedetails , $hidedesc , $hideref );
2021-02-25 23:21:30 +01:00
if ( $result < 0 ) {
setEventMessages ( $object -> error , $object -> errors , 'errors' );
}
2018-02-14 10:47:10 +01:00
}
2021-02-25 23:21:30 +01:00
} elseif ( $action == 'confirm_converttoreduc' && $confirm == 'yes' && $usercancreate ) {
// Convertir en reduc
2018-02-14 10:47:10 +01:00
$object -> fetch ( $id );
$object -> fetch_thirdparty ();
//$object->fetch_lines(); // Already done into fetch
2018-03-08 20:49:37 +01:00
2018-02-14 10:47:10 +01:00
// Check if there is already a discount (protection to avoid duplicate creation when resubmit post)
2019-11-08 10:53:31 +01:00
$discountcheck = new DiscountAbsolute ( $db );
$result = $discountcheck -> fetch ( 0 , 0 , $object -> id );
2018-02-14 10:47:10 +01:00
2019-11-08 10:53:31 +01:00
$canconvert = 0 ;
2021-02-25 23:21:30 +01:00
if ( $object -> type == FactureFournisseur :: TYPE_DEPOSIT && empty ( $discountcheck -> id )) {
$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)
}
if (( $object -> type == FactureFournisseur :: TYPE_CREDIT_NOTE || $object -> type == FactureFournisseur :: TYPE_STANDARD ) && $object -> paye == 0 && empty ( $discountcheck -> id )) {
$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)
}
if ( $canconvert ) {
2018-02-14 10:47:10 +01:00
$db -> begin ();
2018-03-08 20:49:37 +01:00
2018-02-14 10:47:10 +01:00
$amount_ht = $amount_tva = $amount_ttc = array ();
2020-01-01 19:04:14 +01:00
$multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array ();
2018-03-08 20:49:37 +01:00
2018-02-14 10:47:10 +01:00
// Loop on each vat rate
$i = 0 ;
2021-02-25 23:21:30 +01:00
foreach ( $object -> lines as $line ) {
2021-04-06 09:15:34 +02:00
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
$keyforvatrate = $line -> tva_tx . ( $line -> vat_src_code ? ' (' . $line -> vat_src_code . ')' : '' );
2022-02-09 09:47:48 +01:00
$amount_ht [ $keyforvatrate ] += $line -> total_ht ;
$amount_tva [ $keyforvatrate ] += $line -> total_tva ;
$amount_ttc [ $keyforvatrate ] += $line -> total_ttc ;
2021-04-06 09:15:34 +02:00
$multicurrency_amount_ht [ $keyforvatrate ] += $line -> multicurrency_total_ht ;
$multicurrency_amount_tva [ $keyforvatrate ] += $line -> multicurrency_total_tva ;
$multicurrency_amount_ttc [ $keyforvatrate ] += $line -> multicurrency_total_ttc ;
2019-11-08 10:53:31 +01:00
$i ++ ;
2018-02-14 10:47:10 +01:00
}
}
2018-03-08 20:49:37 +01:00
2020-01-01 19:04:14 +01:00
// If some payments were already done, we change the amount to pay using same prorate
2021-08-30 09:33:26 +02:00
if ( ! empty ( $conf -> global -> SUPPLIER_INVOICE_ALLOW_REUSE_OF_CREDIT_WHEN_PARTIALLY_REFUNDED ) && $object -> type == FactureFournisseur :: TYPE_CREDIT_NOTE ) {
2020-04-10 10:59:32 +02:00
$alreadypaid = $object -> getSommePaiement (); // This can be not 0 if we allow to create credit to reuse from credit notes partially refunded.
2020-01-01 19:04:14 +01:00
if ( $alreadypaid && abs ( $alreadypaid ) < abs ( $object -> total_ttc )) {
$ratio = abs (( $object -> total_ttc - $alreadypaid ) / $object -> total_ttc );
2020-04-10 10:59:32 +02:00
foreach ( $amount_ht as $vatrate => $val ) {
2020-01-01 19:04:14 +01:00
$amount_ht [ $vatrate ] = price2num ( $amount_ht [ $vatrate ] * $ratio , 'MU' );
$amount_tva [ $vatrate ] = price2num ( $amount_tva [ $vatrate ] * $ratio , 'MU' );
$amount_ttc [ $vatrate ] = price2num ( $amount_ttc [ $vatrate ] * $ratio , 'MU' );
2021-04-06 09:15:34 +02:00
$multicurrency_amount_ht [ $vatrate ] = price2num ( $multicurrency_amount_ht [ $vatrate ] * $ratio , 'MU' );
$multicurrency_amount_tva [ $vatrate ] = price2num ( $multicurrency_amount_tva [ $vatrate ] * $ratio , 'MU' );
$multicurrency_amount_ttc [ $vatrate ] = price2num ( $multicurrency_amount_ttc [ $vatrate ] * $ratio , 'MU' );
2020-01-01 19:04:14 +01:00
}
}
}
//var_dump($amount_ht);var_dump($amount_tva);var_dump($amount_ttc);exit;
2018-02-14 10:47:10 +01:00
// Insert one discount by VAT rate category
$discount = new DiscountAbsolute ( $db );
2021-02-25 23:21:30 +01:00
if ( $object -> type == FactureFournisseur :: TYPE_CREDIT_NOTE ) {
2018-02-14 10:47:10 +01:00
$discount -> description = '(CREDIT_NOTE)' ;
2021-02-25 23:21:30 +01:00
} elseif ( $object -> type == FactureFournisseur :: TYPE_DEPOSIT ) {
2018-02-14 10:47:10 +01:00
$discount -> description = '(DEPOSIT)' ;
2021-02-25 23:21:30 +01:00
} elseif ( $object -> type == FactureFournisseur :: TYPE_STANDARD || $object -> type == FactureFournisseur :: TYPE_REPLACEMENT || $object -> type == FactureFournisseur :: TYPE_SITUATION ) {
2018-02-14 10:47:10 +01:00
$discount -> description = '(EXCESS PAID)' ;
2021-02-25 23:21:30 +01:00
} else {
2018-02-14 10:47:10 +01:00
setEventMessages ( $langs -> trans ( 'CantConvertToReducAnInvoiceOfThisType' ), null , 'errors' );
}
2018-02-14 18:02:40 +01:00
$discount -> discount_type = 1 ; // Supplier discount
2018-02-14 10:47:10 +01:00
$discount -> fk_soc = $object -> socid ;
$discount -> fk_invoice_supplier_source = $object -> id ;
2018-03-08 20:49:37 +01:00
2018-02-14 10:47:10 +01:00
$error = 0 ;
2018-03-08 20:49:37 +01:00
2021-02-25 23:21:30 +01:00
if ( $object -> type == FactureFournisseur :: TYPE_STANDARD || $object -> type == FactureFournisseur :: TYPE_REPLACEMENT || $object -> type == FactureFournisseur :: TYPE_SITUATION ) {
2018-02-14 10:47:10 +01:00
// If we're on a standard invoice, we have to get excess paid to create a discount in TTC without VAT
2018-03-08 20:49:37 +01:00
2020-01-01 19:04:14 +01:00
// Total payments
2018-02-14 10:47:10 +01:00
$sql = 'SELECT SUM(pf.amount) as total_paiements' ;
2019-11-08 10:53:31 +01:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'paiementfourn_facturefourn as pf, ' . MAIN_DB_PREFIX . 'paiementfourn as p' ;
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_paiement as c ON p.fk_paiement = c.id AND c.entity IN (' . getEntity ( 'c_paiement' ) . ')' ;
2021-08-27 23:36:06 +02:00
$sql .= ' WHERE pf.fk_facturefourn = ' . (( int ) $object -> id );
2019-11-08 10:53:31 +01:00
$sql .= ' AND pf.fk_paiementfourn = p.rowid' ;
$sql .= ' AND p.entity IN (' . getEntity ( 'invoice' ) . ')' ;
2018-02-14 10:47:10 +01:00
$resql = $db -> query ( $sql );
2021-02-25 23:21:30 +01:00
if ( ! $resql ) {
dol_print_error ( $db );
}
2018-02-14 10:47:10 +01:00
$res = $db -> fetch_object ( $resql );
$total_paiements = $res -> total_paiements ;
2018-03-08 20:49:37 +01:00
2020-01-01 19:04:14 +01:00
// Total credit note and deposit
$total_creditnote_and_deposit = 0 ;
$sql = " SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc, " ;
$sql .= " re.description, re.fk_invoice_supplier_source " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " societe_remise_except as re " ;
2021-08-27 18:18:50 +02:00
$sql .= " WHERE fk_invoice_supplier = " . (( int ) $object -> id );
2020-01-01 19:04:14 +01:00
$resql = $db -> query ( $sql );
if ( ! empty ( $resql )) {
while ( $obj = $db -> fetch_object ( $resql )) {
$total_creditnote_and_deposit += $obj -> amount_ttc ;
}
2021-02-25 23:21:30 +01:00
} else {
dol_print_error ( $db );
}
2020-01-01 22:58:26 +01:00
2020-01-01 19:04:14 +01:00
$discount -> amount_ht = $discount -> amount_ttc = $total_paiements + $total_creditnote_and_deposit - $object -> total_ttc ;
2018-02-14 10:47:10 +01:00
$discount -> amount_tva = 0 ;
$discount -> tva_tx = 0 ;
2021-04-06 09:15:34 +02:00
$discount -> vat_src_code = '' ;
2018-02-14 10:47:10 +01:00
$result = $discount -> create ( $user );
2021-02-25 23:21:30 +01:00
if ( $result < 0 ) {
2018-02-14 10:47:10 +01:00
$error ++ ;
}
}
2021-02-25 23:21:30 +01:00
if ( $object -> type == FactureFournisseur :: TYPE_CREDIT_NOTE || $object -> type == FactureFournisseur :: TYPE_DEPOSIT ) {
foreach ( $amount_ht as $tva_tx => $xxx ) {
2018-02-14 10:47:10 +01:00
$discount -> amount_ht = abs ( $amount_ht [ $tva_tx ]);
$discount -> amount_tva = abs ( $amount_tva [ $tva_tx ]);
$discount -> amount_ttc = abs ( $amount_ttc [ $tva_tx ]);
2021-04-06 09:15:34 +02:00
$discount -> multicurrency_amount_ht = abs ( $multicurrency_amount_ht [ $tva_tx ]);
$discount -> multicurrency_amount_tva = abs ( $multicurrency_amount_tva [ $tva_tx ]);
$discount -> multicurrency_amount_ttc = abs ( $multicurrency_amount_ttc [ $tva_tx ]);
// Clean vat code
$reg = array ();
$vat_src_code = '' ;
if ( preg_match ( '/\((.*)\)/' , $tva_tx , $reg )) {
$vat_src_code = $reg [ 1 ];
$tva_tx = preg_replace ( '/\s*\(.*\)/' , '' , $tva_tx ); // Remove code into vatrate.
}
2018-02-14 10:47:10 +01:00
$discount -> tva_tx = abs ( $tva_tx );
2021-04-06 09:15:34 +02:00
$discount -> vat_src_code = $vat_src_code ;
2018-03-08 20:49:37 +01:00
2018-02-14 10:47:10 +01:00
$result = $discount -> create ( $user );
2021-02-25 23:21:30 +01:00
if ( $result < 0 ) {
2018-02-14 10:47:10 +01:00
$error ++ ;
break ;
}
}
}
2018-03-08 20:49:37 +01:00
2021-02-25 23:21:30 +01:00
if ( empty ( $error )) {
2019-11-08 10:53:31 +01:00
if ( $object -> type != FactureFournisseur :: TYPE_DEPOSIT ) {
2018-02-14 10:47:10 +01:00
// Classe facture
2021-02-09 09:55:48 +01:00
$result = $object -> setPaid ( $user );
2021-02-25 23:21:30 +01:00
if ( $result >= 0 ) {
2018-02-14 10:47:10 +01:00
$db -> commit ();
2020-05-21 15:05:19 +02:00
} else {
2018-02-14 10:47:10 +01:00
setEventMessages ( $object -> error , $object -> errors , 'errors' );
$db -> rollback ();
}
} else {
$db -> commit ();
}
2020-05-21 15:05:19 +02:00
} else {
2018-02-14 10:47:10 +01:00
setEventMessages ( $discount -> error , $discount -> errors , 'errors' );
$db -> rollback ();
}
}
2021-02-25 23:21:30 +01:00
} elseif ( $action == 'confirm_delete_paiement' && $confirm == 'yes' && $usercancreate ) {
// Delete payment
$object -> fetch ( $id );
if ( $object -> statut == FactureFournisseur :: STATUS_VALIDATED && $object -> paye == 0 ) {
2017-10-16 08:47:05 +02:00
$paiementfourn = new PaiementFourn ( $db );
2019-11-08 10:53:31 +01:00
$result = $paiementfourn -> fetch ( GETPOST ( 'paiement_id' ));
2017-10-16 08:47:05 +02:00
if ( $result > 0 ) {
2019-11-08 10:53:31 +01:00
$result = $paiementfourn -> delete (); // If fetch ok and found
2017-10-16 08:47:05 +02:00
header ( " Location: " . $_SERVER [ 'PHP_SELF' ] . " ?id= " . $id );
}
if ( $result < 0 ) {
setEventMessages ( $paiementfourn -> error , $paiementfourn -> errors , 'errors' );
}
}
2021-02-25 23:21:30 +01:00
} elseif ( $action == 'add' && $usercancreate ) {
2021-04-06 09:15:34 +02:00
// Insert new invoice in database
2021-02-25 23:21:30 +01:00
if ( $socid > 0 ) {
$object -> socid = GETPOST ( 'socid' , 'int' );
}
2021-09-21 11:04:59 +02:00
$selectedLines = GETPOST ( 'toselect' , 'array' );
2015-02-28 03:11:15 +01:00
2016-10-03 20:41:39 +02:00
$db -> begin ();
2015-02-28 03:11:15 +01:00
2016-10-03 20:41:39 +02:00
$error = 0 ;
2015-02-28 03:11:15 +01:00
2016-10-03 20:41:39 +02:00
// Fill array 'array_options' with data from add form
2019-10-06 14:41:52 +02:00
$ret = $extrafields -> setOptionalsFromPost ( null , $object );
2021-02-25 23:21:30 +01:00
if ( $ret < 0 ) {
$error ++ ;
}
2015-02-28 03:11:15 +01:00
2021-09-23 02:10:39 +02:00
$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
$datedue = dol_mktime ( 0 , 0 , 0 , GETPOST ( 'echmonth' , 'int' ), GETPOST ( 'echday' , 'int' ), GETPOST ( 'echyear' , 'int' ), 'tzserver' );
2022-05-17 14:55:38 +02:00
//var_dump($dateinvoice.' '.dol_print_date($dateinvoice, 'dayhour'));
//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'));
//var_dump($db->idate($dateinvoice));
//exit;
2015-02-28 03:11:15 +01:00
2016-10-03 20:41:39 +02:00
// Replacement invoice
2022-05-21 19:15:15 +02:00
if ( GETPOST ( 'type' , 'int' ) === '' ) {
setEventMessages ( $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " Type " )), null , 'errors' );
$error ++ ;
}
2021-02-25 23:21:30 +01:00
if ( GETPOST ( 'type' ) == FactureFournisseur :: TYPE_REPLACEMENT ) {
2021-01-11 15:34:25 +01:00
if ( empty ( $dateinvoice )) {
2019-01-27 11:55:16 +01:00
setEventMessages ( $langs -> trans ( 'ErrorFieldRequired' , $langs -> transnoentities ( 'DateInvoice' )), null , 'errors' );
2019-11-08 10:53:31 +01:00
$action = 'create' ;
$_GET [ 'socid' ] = $_POST [ 'socid' ];
2016-10-03 20:41:39 +02:00
$error ++ ;
2021-09-23 02:10:39 +02:00
} elseif ( $dateinvoice > ( dol_get_last_hour ( dol_now ( 'tzuserrel' )) + ( empty ( $conf -> global -> INVOICE_MAX_FUTURE_DELAY ) ? 0 : $conf -> global -> INVOICE_MAX_FUTURE_DELAY ))) {
2021-01-11 15:34:25 +01:00
$error ++ ;
setEventMessages ( $langs -> trans ( " ErrorDateIsInFuture " ), null , 'errors' );
$action = 'create' ;
2016-10-03 20:41:39 +02:00
}
2021-01-11 15:34:25 +01:00
2020-04-10 10:59:32 +02:00
if ( ! ( GETPOST ( 'fac_replacement' , 'int' ) > 0 )) {
2019-11-08 10:53:31 +01:00
$error ++ ;
2016-10-03 20:41:39 +02:00
setEventMessages ( $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " ReplaceInvoice " )), null , 'errors' );
}
2015-02-28 03:11:15 +01:00
2019-11-08 10:53:31 +01:00
if ( ! $error ) {
2016-10-03 20:41:39 +02:00
// This is a replacement invoice
2020-01-01 19:04:14 +01:00
$result = $object -> fetch ( GETPOST ( 'fac_replacement' , 'int' ));
2016-10-03 20:41:39 +02:00
$object -> fetch_thirdparty ();
2022-06-13 10:44:20 +02:00
$object -> ref = GETPOST ( 'ref' , 'alphanohtml' );
2019-11-08 10:53:31 +01:00
$object -> ref_supplier = GETPOST ( 'ref_supplier' , 'alpha' );
$object -> socid = GETPOST ( 'socid' , 'int' );
2022-06-13 10:44:20 +02:00
$object -> libelle = GETPOST ( 'label' , 'alphanohtml' );
2021-01-11 15:34:25 +01:00
$object -> date = $dateinvoice ;
2019-11-08 10:53:31 +01:00
$object -> date_echeance = $datedue ;
2020-09-18 01:29:17 +02:00
$object -> note_public = GETPOST ( 'note_public' , 'restricthtml' );
$object -> note_private = GETPOST ( 'note_private' , 'restricthtml' );
2021-09-03 18:00:28 +02:00
$object -> cond_reglement_id = GETPOST ( 'cond_reglement_id' , 'int' );
$object -> mode_reglement_id = GETPOST ( 'mode_reglement_id' , 'int' );
2016-10-03 20:41:39 +02:00
$object -> fk_account = GETPOST ( 'fk_account' , 'int' );
$object -> fk_project = ( $tmpproject > 0 ) ? $tmpproject : null ;
2019-11-08 10:53:31 +01:00
$object -> fk_incoterms = GETPOST ( 'incoterm_id' , 'int' );
2016-10-03 20:41:39 +02:00
$object -> location_incoterms = GETPOST ( 'location_incoterms' , 'alpha' );
$object -> multicurrency_code = GETPOST ( 'multicurrency_code' , 'alpha' );
2020-10-31 14:32:18 +01:00
$object -> multicurrency_tx = GETPOST ( 'originmulticurrency_tx' , 'int' );
2019-12-31 08:34:37 +01:00
$object -> transport_mode_id = GETPOST ( 'transport_mode_id' , 'int' );
2016-10-03 20:41:39 +02:00
// Proprietes particulieres a facture de remplacement
2021-09-03 18:00:28 +02:00
$object -> fk_facture_source = GETPOST ( 'fac_replacement' , 'int' );
2016-10-03 20:41:39 +02:00
$object -> type = FactureFournisseur :: TYPE_REPLACEMENT ;
$id = $object -> createFromCurrent ( $user );
if ( $id <= 0 ) {
2017-10-16 08:47:05 +02:00
$error ++ ;
2016-10-03 20:41:39 +02:00
setEventMessages ( $object -> error , $object -> errors , 'errors' );
}
}
}
2015-02-28 03:11:15 +01:00
2016-10-03 20:41:39 +02:00
// Credit note invoice
2021-02-25 23:21:30 +01:00
if ( GETPOST ( 'type' ) == FactureFournisseur :: TYPE_CREDIT_NOTE ) {
2019-01-27 11:55:16 +01:00
$sourceinvoice = GETPOST ( 'fac_avoir' , 'int' );
2021-02-25 23:21:30 +01:00
if ( ! ( $sourceinvoice > 0 ) && empty ( $conf -> global -> INVOICE_CREDIT_NOTE_STANDALONE )) {
2019-11-08 10:53:31 +01:00
$error ++ ;
2016-10-03 20:41:39 +02:00
setEventMessages ( $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " CorrectInvoice " )), null , 'errors' );
}
2021-02-25 23:21:30 +01:00
if ( GETPOST ( 'socid' , 'int' ) < 1 ) {
2019-01-27 11:55:16 +01:00
setEventMessages ( $langs -> trans ( 'ErrorFieldRequired' , $langs -> transnoentities ( 'Supplier' )), null , 'errors' );
2019-11-08 10:53:31 +01:00
$action = 'create' ;
2016-10-03 20:41:39 +02:00
$error ++ ;
}
2021-01-11 15:34:25 +01:00
if ( empty ( $dateinvoice )) {
2019-01-27 11:55:16 +01:00
setEventMessages ( $langs -> trans ( 'ErrorFieldRequired' , $langs -> transnoentities ( 'DateInvoice' )), null , 'errors' );
2019-11-08 10:53:31 +01:00
$action = 'create' ;
$_GET [ 'socid' ] = $_POST [ 'socid' ];
2016-10-03 20:41:39 +02:00
$error ++ ;
2021-09-23 02:10:39 +02:00
} elseif ( $dateinvoice > ( dol_get_last_hour ( dol_now ( 'tzuserrel' )) + ( empty ( $conf -> global -> INVOICE_MAX_FUTURE_DELAY ) ? 0 : $conf -> global -> INVOICE_MAX_FUTURE_DELAY ))) {
2021-01-11 15:34:25 +01:00
$error ++ ;
setEventMessages ( $langs -> trans ( " ErrorDateIsInFuture " ), null , 'errors' );
$action = 'create' ;
2016-10-03 20:41:39 +02:00
}
2021-01-11 15:34:25 +01:00
2021-02-25 23:21:30 +01:00
if ( ! GETPOST ( 'ref_supplier' )) {
2019-01-27 11:55:16 +01:00
setEventMessages ( $langs -> trans ( 'ErrorFieldRequired' , $langs -> transnoentities ( 'RefSupplier' )), null , 'errors' );
2019-11-08 10:53:31 +01:00
$action = 'create' ;
$_GET [ 'socid' ] = $_POST [ 'socid' ];
2016-10-03 20:41:39 +02:00
$error ++ ;
}
2015-02-28 03:11:15 +01:00
2021-02-25 23:21:30 +01:00
if ( ! $error ) {
2016-10-03 20:41:39 +02:00
$tmpproject = GETPOST ( 'projectid' , 'int' );
// Creation facture
2022-06-13 10:44:20 +02:00
$object -> ref = GETPOST ( 'ref' , 'alphanohtml' );
$object -> ref_supplier = GETPOST ( 'ref_supplier' , 'alphanohtml' );
2019-11-02 12:59:38 +01:00
$object -> socid = GETPOST ( 'socid' , 'int' );
2022-06-13 10:44:20 +02:00
$object -> libelle = GETPOST ( 'label' , 'alphanohtml' );
$object -> label = GETPOST ( 'label' , 'alphanohtml' );
2021-01-11 15:34:25 +01:00
$object -> date = $dateinvoice ;
2019-11-08 10:53:31 +01:00
$object -> date_echeance = $datedue ;
2020-09-18 01:29:17 +02:00
$object -> note_public = GETPOST ( 'note_public' , 'restricthtml' );
$object -> note_private = GETPOST ( 'note_private' , 'restricthtml' );
2016-10-03 20:41:39 +02:00
$object -> cond_reglement_id = GETPOST ( 'cond_reglement_id' );
$object -> mode_reglement_id = GETPOST ( 'mode_reglement_id' );
$object -> fk_account = GETPOST ( 'fk_account' , 'int' );
$object -> fk_project = ( $tmpproject > 0 ) ? $tmpproject : null ;
2019-11-08 10:53:31 +01:00
$object -> fk_incoterms = GETPOST ( 'incoterm_id' , 'int' );
2016-10-03 20:41:39 +02:00
$object -> location_incoterms = GETPOST ( 'location_incoterms' , 'alpha' );
$object -> multicurrency_code = GETPOST ( 'multicurrency_code' , 'alpha' );
2020-10-31 14:32:18 +01:00
$object -> multicurrency_tx = GETPOST ( 'originmulticurrency_tx' , 'int' );
2019-12-31 08:34:37 +01:00
$object -> transport_mode_id = GETPOST ( 'transport_mode_id' , 'int' );
2016-10-03 20:41:39 +02:00
// Proprietes particulieres a facture avoir
2019-11-08 10:53:31 +01:00
$object -> fk_facture_source = $sourceinvoice > 0 ? $sourceinvoice : '' ;
2016-10-03 20:41:39 +02:00
$object -> type = FactureFournisseur :: TYPE_CREDIT_NOTE ;
$id = $object -> create ( $user );
2019-11-08 10:53:31 +01:00
if ( $id <= 0 ) {
2018-02-14 10:47:10 +01:00
$error ++ ;
}
2021-02-25 23:21:30 +01:00
if ( GETPOST ( 'invoiceAvoirWithLines' , 'int' ) == 1 && $id > 0 ) {
2016-10-03 20:41:39 +02:00
$facture_source = new FactureFournisseur ( $db ); // fetch origin object
2021-02-25 23:21:30 +01:00
if ( $facture_source -> fetch ( $object -> fk_facture_source ) > 0 ) {
2016-10-03 20:41:39 +02:00
$fk_parent_line = 0 ;
2015-02-28 03:11:15 +01:00
2021-02-25 23:21:30 +01:00
foreach ( $facture_source -> lines as $line ) {
2016-10-03 20:41:39 +02:00
// Reset fk_parent_line for no child products and special product
if (( $line -> product_type != 9 && empty ( $line -> fk_parent_line )) || $line -> product_type == 9 ) {
2016-11-02 07:13:17 +01:00
$fk_parent_line = 0 ;
2016-10-03 20:41:39 +02:00
}
2016-08-10 09:47:25 +02:00
2016-11-02 07:13:17 +01:00
$line -> fk_facture_fourn = $object -> id ;
2016-10-03 20:41:39 +02:00
$line -> fk_parent_line = $fk_parent_line ;
2016-08-10 09:47:25 +02:00
2019-11-08 10:53:31 +01:00
$line -> subprice = - $line -> subprice ; // invert price for object
2016-10-03 20:41:39 +02:00
$line -> pa_ht = - $line -> pa_ht ;
2019-11-08 10:53:31 +01:00
$line -> total_ht = - $line -> total_ht ;
$line -> total_tva = - $line -> total_tva ;
$line -> total_ttc = - $line -> total_ttc ;
$line -> total_localtax1 = - $line -> total_localtax1 ;
$line -> total_localtax2 = - $line -> total_localtax2 ;
2015-02-28 03:11:15 +01:00
2016-10-03 20:41:39 +02:00
$result = $line -> insert ();
2016-08-10 09:47:25 +02:00
2016-10-03 20:41:39 +02:00
$object -> lines [] = $line ; // insert new line in current object
2015-02-28 03:11:15 +01:00
2016-10-03 20:41:39 +02:00
// Defined the new fk_parent_line
if ( $result > 0 && $line -> product_type == 9 ) {
2017-10-16 08:47:05 +02:00
$fk_parent_line = $result ;
2016-10-03 20:41:39 +02:00
}
}
2015-02-28 03:11:15 +01:00
2016-10-03 20:41:39 +02:00
$object -> update_price ( 1 );
}
}
2021-02-25 23:21:30 +01:00
if ( GETPOST ( 'invoiceAvoirWithPaymentRestAmount' , 'int' ) == 1 && $id > 0 ) {
2016-10-03 20:41:39 +02:00
$facture_source = new FactureFournisseur ( $db ); // fetch origin object if not previously defined
2021-02-25 23:21:30 +01:00
if ( $facture_source -> fetch ( $object -> fk_facture_source ) > 0 ) {
2022-05-22 17:09:08 +02:00
$totalpaid = $facture_source -> getSommePaiement ();
2016-10-03 20:41:39 +02:00
$totalcreditnotes = $facture_source -> getSumCreditNotesUsed ();
$totaldeposits = $facture_source -> getSumDepositsUsed ();
2022-05-22 17:09:08 +02:00
$remain_to_pay = abs ( $facture_source -> total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits );
2023-03-03 16:08:17 +01:00
$desc = $langs -> trans ( 'invoiceAvoirLineWithPaymentRestAmount' );
$retAddLine = $object -> addline ( $desc , $remain_to_pay , 0 , 0 , 0 , 1 , 0 , 0 , '' , '' , 0 , '' , 'TTC' );
2016-10-03 20:41:39 +02:00
2023-03-03 16:08:17 +01:00
if ( $retAddLine < 0 ) {
$error ++ ;
}
2016-10-03 20:41:39 +02:00
}
}
}
2022-04-07 19:26:07 +02:00
} elseif ( $fac_recid > 0 && ( GETPOST ( 'type' ) == FactureFournisseur :: TYPE_STANDARD || GETPOST ( 'type' ) == FactureFournisseur :: TYPE_DEPOSIT )) {
// Standard invoice or Deposit invoice, created from a Predefined template invoice
2022-02-07 14:34:27 +01:00
if ( empty ( $dateinvoice )) {
$error ++ ;
setEventMessages ( $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " Date " )), null , 'errors' );
$action = 'create' ;
} elseif ( $dateinvoice > ( dol_get_last_hour ( dol_now ( 'tzuserrel' )) + ( empty ( $conf -> global -> INVOICE_MAX_FUTURE_DELAY ) ? 0 : $conf -> global -> INVOICE_MAX_FUTURE_DELAY ))) {
$error ++ ;
setEventMessages ( $langs -> trans ( " ErrorDateIsInFuture " ), null , 'errors' );
$action = 'create' ;
}
if ( ! $error ) {
$object -> socid = GETPOST ( 'socid' , 'int' );
2022-06-13 10:44:20 +02:00
$object -> type = GETPOST ( 'type' , 'alphanohtml' );
$object -> ref = GETPOST ( 'ref' , 'alphanohtml' );
2022-02-07 14:34:27 +01:00
$object -> date = $dateinvoice ;
$object -> note_public = trim ( GETPOST ( 'note_public' , 'restricthtml' ));
$object -> note_private = trim ( GETPOST ( 'note_private' , 'restricthtml' ));
2022-06-13 10:44:20 +02:00
$object -> ref_supplier = GETPOST ( 'ref_supplier' , 'alphanohtml' );
$object -> model_pdf = GETPOST ( 'model' , 'alphanohtml' );
2022-02-07 14:34:27 +01:00
$object -> fk_project = GETPOST ( 'projectid' , 'int' );
$object -> cond_reglement_id = ( GETPOST ( 'type' ) == 3 ? 1 : GETPOST ( 'cond_reglement_id' ));
$object -> mode_reglement_id = GETPOST ( 'mode_reglement_id' , 'int' );
$object -> fk_account = GETPOST ( 'fk_account' , 'int' );
$object -> amount = price2num ( GETPOST ( 'amount' ));
$object -> remise_absolue = price2num ( GETPOST ( 'remise_absolue' ), 'MU' );
$object -> remise_percent = price2num ( GETPOST ( 'remise_percent' ), '' , 2 );
$object -> fk_incoterms = GETPOST ( 'incoterm_id' , 'int' );
$object -> location_incoterms = GETPOST ( 'location_incoterms' , 'alpha' );
$object -> multicurrency_code = GETPOST ( 'multicurrency_code' , 'alpha' );
$object -> multicurrency_tx = GETPOST ( 'originmulticurrency_tx' , 'int' );
// Source facture
2022-04-05 12:16:06 +02:00
$object -> fac_rec = $fac_recid ;
2022-02-07 14:34:27 +01:00
$fac_rec = new FactureFournisseurRec ( $db );
$fac_rec -> fetch ( $object -> fac_rec );
$fac_rec -> fetch_lines ();
$object -> lines = $fac_rec -> lines ;
$id = $object -> create ( $user ); // This include recopy of links from recurring invoice and recurring invoice lines
}
2022-04-07 19:26:07 +02:00
} elseif ( $fac_recid <= 0 && ( GETPOST ( 'type' ) == FactureFournisseur :: TYPE_STANDARD || GETPOST ( 'type' ) == FactureFournisseur :: TYPE_DEPOSIT )) {
// Standard invoice or Deposit invoice, not from a Predefined template invoice
2021-02-25 23:21:30 +01:00
if ( GETPOST ( 'socid' , 'int' ) < 1 ) {
2019-01-27 11:55:16 +01:00
setEventMessages ( $langs -> trans ( 'ErrorFieldRequired' , $langs -> transnoentities ( 'Supplier' )), null , 'errors' );
2019-11-08 10:53:31 +01:00
$action = 'create' ;
2016-10-03 20:41:39 +02:00
$error ++ ;
}
2021-01-11 15:34:25 +01:00
if ( empty ( $dateinvoice )) {
2019-01-27 11:55:16 +01:00
setEventMessages ( $langs -> trans ( 'ErrorFieldRequired' , $langs -> transnoentities ( 'DateInvoice' )), null , 'errors' );
2019-11-08 10:53:31 +01:00
$action = 'create' ;
$_GET [ 'socid' ] = $_POST [ 'socid' ];
2016-10-03 20:41:39 +02:00
$error ++ ;
2021-09-23 02:10:39 +02:00
} elseif ( $dateinvoice > ( dol_get_last_hour ( dol_now ( 'tzuserrel' )) + ( empty ( $conf -> global -> INVOICE_MAX_FUTURE_DELAY ) ? 0 : $conf -> global -> INVOICE_MAX_FUTURE_DELAY ))) {
2021-01-11 15:34:25 +01:00
$error ++ ;
setEventMessages ( $langs -> trans ( " ErrorDateIsInFuture " ), null , 'errors' );
$action = 'create' ;
2016-10-03 20:41:39 +02:00
}
2021-01-11 15:34:25 +01:00
2021-02-25 23:21:30 +01:00
if ( ! GETPOST ( 'ref_supplier' )) {
2019-01-27 11:55:16 +01:00
setEventMessages ( $langs -> trans ( 'ErrorFieldRequired' , $langs -> transnoentities ( 'RefSupplier' )), null , 'errors' );
2019-11-08 10:53:31 +01:00
$action = 'create' ;
$_GET [ 'socid' ] = $_POST [ 'socid' ];
2016-10-03 20:41:39 +02:00
$error ++ ;
}
2021-02-25 23:21:30 +01:00
if ( ! $error ) {
2016-10-03 20:41:39 +02:00
$tmpproject = GETPOST ( 'projectid' , 'int' );
2021-01-15 19:53:46 +01:00
// Creation invoice
2021-04-06 09:15:34 +02:00
$object -> socid = GETPOST ( 'socid' , 'int' );
2022-06-13 10:44:20 +02:00
$object -> type = GETPOST ( 'type' , 'alphanohtml' );
$object -> ref = GETPOST ( 'ref' , 'alphanohtml' );
$object -> ref_supplier = GETPOST ( 'ref_supplier' , 'alphanohtml' );
2021-04-06 09:15:34 +02:00
$object -> socid = GETPOST ( 'socid' , 'int' );
2022-06-13 10:44:20 +02:00
$object -> libelle = GETPOST ( 'label' , 'alphanohtml' ); // deprecated
$object -> label = GETPOST ( 'label' , 'alphanohtml' );
2021-04-06 09:15:34 +02:00
$object -> date = $dateinvoice ;
$object -> date_echeance = $datedue ;
$object -> note_public = GETPOST ( 'note_public' , 'restricthtml' );
$object -> note_private = GETPOST ( 'note_private' , 'restricthtml' );
$object -> cond_reglement_id = GETPOST ( 'cond_reglement_id' );
$object -> mode_reglement_id = GETPOST ( 'mode_reglement_id' );
$object -> fk_account = GETPOST ( 'fk_account' , 'int' );
$object -> fk_project = ( $tmpproject > 0 ) ? $tmpproject : null ;
$object -> fk_incoterms = GETPOST ( 'incoterm_id' , 'int' );
$object -> location_incoterms = GETPOST ( 'location_incoterms' , 'alpha' );
$object -> multicurrency_code = GETPOST ( 'multicurrency_code' , 'alpha' );
$object -> multicurrency_tx = GETPOST ( 'originmulticurrency_tx' , 'int' );
$object -> transport_mode_id = GETPOST ( 'transport_mode_id' );
2016-10-03 20:41:39 +02:00
// Auto calculation of date due if not filled by user
2021-02-25 23:21:30 +01:00
if ( empty ( $object -> date_echeance )) {
$object -> date_echeance = $object -> calculate_date_lim_reglement ();
}
2016-10-03 20:41:39 +02:00
2019-06-14 18:28:01 +02:00
$object -> fetch_thirdparty ();
2016-10-03 20:41:39 +02:00
// If creation from another object of another module
2021-02-25 23:21:30 +01:00
if ( ! $error && GETPOST ( 'origin' , 'alpha' ) && GETPOST ( 'originid' )) {
2016-10-03 20:41:39 +02:00
// Parse element/subelement (ex: project_task)
2020-02-25 15:14:37 +01:00
$element = $subelement = GETPOST ( 'origin' , 'alpha' );
2022-02-22 23:44:56 +01:00
/* if ( preg_match ( '/^([^_]+)_([^_]+)/i' , GETPOST ( 'origin' ), $regs ))
2016-10-03 20:41:39 +02:00
{
$element = $regs [ 1 ];
$subelement = $regs [ 2 ];
} */
// For compatibility
2019-11-08 10:53:31 +01:00
if ( $element == 'order' ) {
2016-10-03 20:41:39 +02:00
$element = $subelement = 'commande' ;
}
2019-11-08 10:53:31 +01:00
if ( $element == 'propal' ) {
2016-10-03 20:41:39 +02:00
$element = 'comm/propal' ; $subelement = 'propal' ;
}
if ( $element == 'contract' ) {
$element = $subelement = 'contrat' ;
}
if ( $element == 'order_supplier' ) {
$element = 'fourn' ; $subelement = 'fournisseur.commande' ;
}
2021-02-25 23:21:30 +01:00
if ( $element == 'project' ) {
2016-10-03 20:41:39 +02:00
$element = 'projet' ;
}
2020-02-25 15:14:37 +01:00
$object -> origin = GETPOST ( 'origin' , 'alpha' );
$object -> origin_id = GETPOST ( 'originid' , 'int' );
2016-10-03 20:41:39 +02:00
2019-01-17 19:27:26 +01:00
2018-10-18 10:26:32 +02:00
require_once DOL_DOCUMENT_ROOT . '/' . $element . '/class/' . $subelement . '.class.php' ;
$classname = ucfirst ( $subelement );
2021-02-25 23:21:30 +01:00
if ( $classname == 'Fournisseur.commande' ) {
$classname = 'CommandeFournisseur' ;
}
2018-10-18 10:26:32 +02:00
$objectsrc = new $classname ( $db );
$objectsrc -> fetch ( $originid );
$objectsrc -> fetch_thirdparty ();
2021-02-25 23:21:30 +01:00
if ( ! empty ( $object -> origin ) && ! empty ( $object -> origin_id )) {
2020-03-27 16:02:58 +01:00
$object -> linkedObjectsIds [ $object -> origin ] = $object -> origin_id ;
}
// Add also link with order if object is reception
2021-02-25 23:21:30 +01:00
if ( $object -> origin == 'reception' ) {
2018-10-18 10:26:32 +02:00
$objectsrc -> fetchObjectLinked ();
2021-02-25 23:21:30 +01:00
if ( count ( $objectsrc -> linkedObjectsIds [ 'order_supplier' ]) > 0 ) {
foreach ( $objectsrc -> linkedObjectsIds [ 'order_supplier' ] as $key => $value ) {
2020-03-27 16:02:58 +01:00
$object -> linkedObjectsIds [ 'order_supplier' ] = $value ;
2018-10-18 10:26:32 +02:00
}
}
}
2016-10-03 20:41:39 +02:00
$id = $object -> create ( $user );
// Add lines
2021-02-25 23:21:30 +01:00
if ( $id > 0 ) {
2016-10-03 20:41:39 +02:00
require_once DOL_DOCUMENT_ROOT . '/' . $element . '/class/' . $subelement . '.class.php' ;
$classname = ucfirst ( $subelement );
2021-02-25 23:21:30 +01:00
if ( $classname == 'Fournisseur.commande' ) {
$classname = 'CommandeFournisseur' ;
}
2016-10-03 20:41:39 +02:00
$srcobject = new $classname ( $db );
2019-11-08 10:53:31 +01:00
$result = $srcobject -> fetch ( GETPOST ( 'originid' , 'int' ));
2021-04-06 09:15:34 +02:00
// If deposit invoice - down payment with 1 line (fixed amount or percent)
2021-04-27 18:08:05 +02:00
$typeamount = GETPOST ( 'typedeposit' , 'alpha' );
2021-04-27 14:13:59 +02:00
if ( GETPOST ( 'type' ) == FactureFournisseur :: TYPE_DEPOSIT && in_array ( $typeamount , array ( 'amount' , 'variable' ))) {
2021-04-27 18:08:05 +02:00
$valuedeposit = price2num ( GETPOST ( 'valuedeposit' , 'alpha' ), 'MU' );
2021-04-06 09:15:34 +02:00
// Define the array $amountdeposit
$amountdeposit = array ();
if ( ! empty ( $conf -> global -> MAIN_DEPOSIT_MULTI_TVA )) {
if ( $typeamount == 'amount' ) {
$amount = $valuedeposit ;
} else {
$amount = $srcobject -> total_ttc * ( $valuedeposit / 100 );
}
$TTotalByTva = array ();
foreach ( $srcobject -> lines as & $line ) {
if ( ! empty ( $line -> special_code )) {
continue ;
}
$TTotalByTva [ $line -> tva_tx ] += $line -> total_ttc ;
}
foreach ( $TTotalByTva as $tva => & $total ) {
$coef = $total / $srcobject -> total_ttc ; // Calc coef
$am = $amount * $coef ;
$amount_ttc_diff += $am ;
$amountdeposit [ $tva ] += $am / ( 1 + $tva / 100 ); // Convert into HT for the addline
}
} else {
if ( $typeamount == 'amount' ) {
$amountdeposit [ 0 ] = $valuedeposit ;
} elseif ( $typeamount == 'variable' ) {
if ( $result > 0 ) {
$totalamount = 0 ;
$lines = $srcobject -> lines ;
$numlines = count ( $lines );
for ( $i = 0 ; $i < $numlines ; $i ++ ) {
$qualified = 1 ;
if ( empty ( $lines [ $i ] -> qty )) {
$qualified = 0 ; // We discard qty=0, it is an option
}
if ( ! empty ( $lines [ $i ] -> special_code )) {
$qualified = 0 ; // We discard special_code (frais port, ecotaxe, option, ...)
}
if ( $qualified ) {
$totalamount += $lines [ $i ] -> total_ht ; // Fixme : is it not for the customer ? Shouldn't we take total_ttc ?
$tva_tx = $lines [ $i ] -> tva_tx ;
$amountdeposit [ $tva_tx ] += ( $lines [ $i ] -> total_ht * $valuedeposit ) / 100 ;
}
}
if ( $totalamount == 0 ) {
$amountdeposit [ 0 ] = 0 ;
}
} else {
setEventMessages ( $srcobject -> error , $srcobject -> errors , 'errors' );
$error ++ ;
}
}
$amount_ttc_diff = $amountdeposit [ 0 ];
}
foreach ( $amountdeposit as $tva => $amount ) {
if ( empty ( $amount )) {
continue ;
}
$arraylist = array (
'amount' => 'FixAmount' ,
'variable' => 'VarAmount'
);
$descline = '(DEPOSIT)' ;
//$descline.= ' - '.$langs->trans($arraylist[$typeamount]);
if ( $typeamount == 'amount' ) {
$descline .= ' (' . price ( $valuedeposit , '' , $langs , 0 , - 1 , - 1 , ( ! empty ( $object -> multicurrency_code ) ? $object -> multicurrency_code : $conf -> currency )) . ')' ;
} elseif ( $typeamount == 'variable' ) {
$descline .= ' (' . $valuedeposit . '%)' ;
}
$descline .= ' - ' . $srcobject -> ref ;
$result = $object -> addline (
$descline ,
$amount , // subprice
$tva , // vat rate
0 , // localtax1_tx
0 , // localtax2_tx
1 , // quantity
( empty ( $conf -> global -> INVOICE_PRODUCTID_DEPOSIT ) ? 0 : $conf -> global -> INVOICE_PRODUCTID_DEPOSIT ), // fk_product
0 , // remise_percent
0 , // date_start
0 , // date_end
0 ,
$lines [ $i ] -> info_bits , // info_bits
'HT' ,
0 , // product_type
1 ,
0 ,
0 ,
null ,
$object -> origin ,
0 ,
'' ,
$lines [ $i ] -> special_code ,
2021-08-22 07:26:46 +02:00
0 ,
2021-04-06 09:15:34 +02:00
0
//,$langs->trans('Deposit') //Deprecated
);
}
$diff = $object -> total_ttc - $amount_ttc_diff ;
if ( ! empty ( $conf -> global -> MAIN_DEPOSIT_MULTI_TVA ) && $diff != 0 ) {
$object -> fetch_lines ();
$subprice_diff = $object -> lines [ 0 ] -> subprice - $diff / ( 1 + $object -> lines [ 0 ] -> tva_tx / 100 );
$object -> updateline ( $object -> lines [ 0 ] -> id , $object -> lines [ 0 ] -> desc , $subprice_diff , $object -> lines [ 0 ] -> qty , $object -> lines [ 0 ] -> remise_percent , $object -> lines [ 0 ] -> date_start , $object -> lines [ 0 ] -> date_end , $object -> lines [ 0 ] -> tva_tx , 0 , 0 , 'HT' , $object -> lines [ 0 ] -> info_bits , $object -> lines [ 0 ] -> product_type , 0 , 0 , 0 , $object -> lines [ 0 ] -> pa_ht , $object -> lines [ 0 ] -> label , 0 , array (), 100 );
}
2021-04-27 18:08:05 +02:00
} elseif ( $result > 0 ) {
2016-10-03 20:41:39 +02:00
$lines = $srcobject -> lines ;
2021-02-25 23:21:30 +01:00
if ( empty ( $lines ) && method_exists ( $srcobject , 'fetch_lines' )) {
2016-10-03 20:41:39 +02:00
$srcobject -> fetch_lines ();
$lines = $srcobject -> lines ;
}
2019-11-08 10:53:31 +01:00
$num = count ( $lines );
2021-02-25 23:21:30 +01:00
for ( $i = 0 ; $i < $num ; $i ++ ) { // TODO handle subprice < 0
2021-09-21 11:04:59 +02:00
if ( ! in_array ( $lines [ $i ] -> id , $selectedLines )) {
continue ; // Skip unselected lines
}
2019-11-08 10:53:31 +01:00
$desc = ( $lines [ $i ] -> desc ? $lines [ $i ] -> desc : $lines [ $i ] -> libelle );
$product_type = ( $lines [ $i ] -> product_type ? $lines [ $i ] -> product_type : 0 );
2016-10-03 20:41:39 +02:00
2018-01-02 12:06:11 +01:00
// Extrafields
2020-04-23 13:21:39 +02:00
if ( method_exists ( $lines [ $i ], 'fetch_optionals' )) {
2020-03-27 16:02:58 +01:00
$lines [ $i ] -> fetch_optionals ();
2018-01-02 12:06:11 +01:00
}
2018-01-12 00:16:55 +01:00
2016-10-03 20:41:39 +02:00
// Dates
// TODO mutualiser
2019-11-08 10:53:31 +01:00
$date_start = $lines [ $i ] -> date_debut_prevue ;
2021-02-25 23:21:30 +01:00
if ( $lines [ $i ] -> date_debut_reel ) {
$date_start = $lines [ $i ] -> date_debut_reel ;
}
if ( $lines [ $i ] -> date_start ) {
$date_start = $lines [ $i ] -> date_start ;
}
2019-11-08 10:53:31 +01:00
$date_end = $lines [ $i ] -> date_fin_prevue ;
2021-02-25 23:21:30 +01:00
if ( $lines [ $i ] -> date_fin_reel ) {
$date_end = $lines [ $i ] -> date_fin_reel ;
}
if ( $lines [ $i ] -> date_end ) {
$date_end = $lines [ $i ] -> date_end ;
}
2016-10-03 20:41:39 +02:00
2018-10-30 10:46:33 +01:00
// FIXME Missing special_code into addline and updateline methods
$object -> special_code = $lines [ $i ] -> special_code ;
2018-11-26 14:33:45 +01:00
2020-03-31 18:58:59 +02:00
// FIXME If currency different from main currency, take multicurrency price
2021-02-25 23:21:30 +01:00
if ( $object -> multicurrency_code != $conf -> currency || $object -> multicurrency_tx != 1 ) {
2020-03-31 18:58:59 +02:00
$pu = 0 ;
$pu_currency = $lines [ $i ] -> multicurrency_subprice ;
2020-05-21 15:05:19 +02:00
} else {
2020-03-31 18:58:59 +02:00
$pu = $lines [ $i ] -> subprice ;
$pu_currency = 0 ;
}
2018-11-26 14:33:45 +01:00
2016-10-03 20:41:39 +02:00
// FIXME Missing $lines[$i]->ref_supplier and $lines[$i]->label into addline and updateline methods. They are filled when coming from order for example.
$result = $object -> addline (
$desc ,
2020-03-31 18:58:59 +02:00
$pu ,
2016-10-03 20:41:39 +02:00
$lines [ $i ] -> tva_tx ,
$lines [ $i ] -> localtax1_tx ,
$lines [ $i ] -> localtax2_tx ,
$lines [ $i ] -> qty ,
$lines [ $i ] -> fk_product ,
$lines [ $i ] -> remise_percent ,
$date_start ,
$date_end ,
0 ,
$lines [ $i ] -> info_bits ,
'HT' ,
$product_type ,
$lines [ $i ] -> rang ,
0 ,
$lines [ $i ] -> array_options ,
$lines [ $i ] -> fk_unit ,
2018-10-22 14:32:50 +02:00
$lines [ $i ] -> id ,
2020-03-31 18:58:59 +02:00
$pu_currency ,
2019-03-07 14:13:38 +01:00
$lines [ $i ] -> ref_supplier ,
2019-03-06 12:10:24 +01:00
$lines [ $i ] -> special_code
2016-10-03 20:41:39 +02:00
);
2021-02-25 23:21:30 +01:00
if ( $result < 0 ) {
2016-10-03 20:41:39 +02:00
$error ++ ;
break ;
}
}
// Now reload line
$object -> fetch_lines ();
2020-05-21 15:05:19 +02:00
} else {
2016-10-03 20:41:39 +02:00
$error ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2016-10-03 20:41:39 +02:00
$error ++ ;
}
2021-02-25 23:21:30 +01:00
} elseif ( ! $error ) {
2016-10-03 20:41:39 +02:00
$id = $object -> create ( $user );
2021-02-25 23:21:30 +01:00
if ( $id < 0 ) {
2016-10-03 20:41:39 +02:00
$error ++ ;
}
}
2016-11-08 11:23:40 +01:00
}
}
2016-10-03 20:41:39 +02:00
2021-02-25 23:21:30 +01:00
if ( $error ) {
2016-11-08 11:23:40 +01:00
$langs -> load ( " errors " );
$db -> rollback ();
2016-10-03 20:41:39 +02:00
2016-11-08 11:23:40 +01:00
setEventMessages ( $object -> error , $object -> errors , 'errors' );
2019-11-08 10:53:31 +01:00
$action = 'create' ;
$_GET [ 'socid' ] = $_POST [ 'socid' ];
2020-05-21 15:05:19 +02:00
} else {
2016-11-08 11:23:40 +01:00
$db -> commit ();
2016-10-03 20:41:39 +02:00
2016-11-08 11:23:40 +01:00
if ( empty ( $conf -> global -> MAIN_DISABLE_PDF_AUTOUPDATE )) {
$outputlangs = $langs ;
2020-09-10 01:49:09 +02:00
$result = $object -> generateDocument ( $object -> model_pdf , $outputlangs , $hidedetails , $hidedesc , $hideref );
2021-02-25 23:21:30 +01:00
if ( $result < 0 ) {
2019-01-27 11:55:16 +01:00
dol_print_error ( $db , $object -> error , $object -> errors );
2016-10-03 20:41:39 +02:00
exit ;
}
}
2016-11-08 11:23:40 +01:00
header ( " Location: " . $_SERVER [ 'PHP_SELF' ] . " ?id= " . $id );
exit ;
2016-10-03 20:41:39 +02:00
}
2021-02-25 23:21:30 +01:00
} elseif ( $action == 'updateline' && $usercancreate ) {
// Edit line
2015-02-28 03:11:15 +01:00
$db -> begin ();
2021-04-26 17:43:27 +02:00
if ( ! $object -> fetch ( $id ) > 0 ) dol_print_error ( $db );
2019-10-27 09:51:15 +01:00
$object -> fetch_thirdparty ();
2015-02-28 03:11:15 +01:00
2020-10-07 15:01:28 +02:00
$tva_tx = ( GETPOST ( 'tva_tx' ) ? GETPOST ( 'tva_tx' ) : 0 );
2015-02-28 18:10:31 +01:00
2021-02-25 23:21:30 +01:00
if ( GETPOST ( 'price_ht' ) != '' || GETPOST ( 'multicurrency_subprice' ) != '' ) {
2021-03-02 23:09:55 +01:00
$up = price2num ( GETPOST ( 'price_ht' ), '' , 2 );
2019-10-27 09:51:15 +01:00
$price_base_type = 'HT' ;
2020-05-21 15:05:19 +02:00
} else {
2021-03-02 23:09:55 +01:00
$up = price2num ( GETPOST ( 'price_ttc' ), '' , 2 );
2019-10-27 09:51:15 +01:00
$price_base_type = 'TTC' ;
}
2017-10-16 08:47:05 +02:00
2021-02-25 23:21:30 +01:00
if ( GETPOST ( 'productid' ) > 0 ) {
2019-10-27 09:51:15 +01:00
$productsupplier = new ProductFournisseur ( $db );
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> global -> SUPPLIER_INVOICE_WITH_PREDEFINED_PRICES_ONLY )) {
if ( GETPOST ( 'productid' ) > 0 && $productsupplier -> get_buyprice ( 0 , price2num ( GETPOST ( 'qty' )), GETPOST ( 'productid' , 'int' ), 'restricthtml' , GETPOST ( 'socid' , 'int' )) < 0 ) {
2019-10-27 09:51:15 +01:00
setEventMessages ( $langs -> trans ( " ErrorQtyTooLowForThisSupplier " ), null , 'warnings' );
2018-04-24 13:22:34 +02:00
}
2019-10-27 09:51:15 +01:00
}
2018-04-24 13:22:34 +02:00
2019-10-27 09:51:15 +01:00
$prod = new Product ( $db );
$prod -> fetch ( GETPOST ( 'productid' ));
$label = $prod -> description ;
2021-03-25 16:59:47 +01:00
if ( trim ( GETPOST ( 'product_desc' , 'restricthtml' )) != trim ( $label )) {
$label = GETPOST ( 'product_desc' , 'restricthtml' );
2021-02-25 23:21:30 +01:00
}
2017-10-16 08:47:05 +02:00
2019-10-27 09:51:15 +01:00
$type = $prod -> type ;
2020-05-21 15:05:19 +02:00
} else {
2021-03-25 16:59:47 +01:00
$label = GETPOST ( 'product_desc' , 'restricthtml' );
$type = GETPOST ( " type " ) ? GETPOST ( " type " ) : 0 ;
2019-10-27 09:51:15 +01:00
}
2014-08-07 15:12:27 +02:00
2019-11-08 10:53:31 +01:00
$date_start = dol_mktime ( GETPOST ( 'date_starthour' ), GETPOST ( 'date_startmin' ), GETPOST ( 'date_startsec' ), GETPOST ( 'date_startmonth' ), GETPOST ( 'date_startday' ), GETPOST ( 'date_startyear' ));
$date_end = dol_mktime ( GETPOST ( 'date_endhour' ), GETPOST ( 'date_endmin' ), GETPOST ( 'date_endsec' ), GETPOST ( 'date_endmonth' ), GETPOST ( 'date_endday' ), GETPOST ( 'date_endyear' ));
2015-02-28 03:11:15 +01:00
2020-10-07 15:01:28 +02:00
// Define info_bits
$info_bits = 0 ;
2021-02-25 23:21:30 +01:00
if ( preg_match ( '/\*/' , $tva_tx )) {
2020-10-07 15:01:28 +02:00
$info_bits |= 0x01 ;
2021-02-25 23:21:30 +01:00
}
2017-10-22 14:02:24 +02:00
2020-10-07 15:01:28 +02:00
// Define vat_rate
$tva_tx = str_replace ( '*' , '' , $tva_tx );
$localtax1_tx = get_localtax ( $tva_tx , 1 , $mysoc , $object -> thirdparty );
$localtax2_tx = get_localtax ( $tva_tx , 2 , $mysoc , $object -> thirdparty );
2017-10-22 14:02:24 +02:00
2021-09-08 22:09:02 +02:00
$remise_percent = price2num ( GETPOST ( 'remise_percent' ), '' , 2 );
2022-01-10 03:24:01 +01:00
$pu_devise = price2num ( GETPOST ( 'multicurrency_subprice' ), 'MU' , 2 );
2015-02-28 03:11:15 +01:00
2019-10-27 09:51:15 +01:00
// Extrafields Lines
$extralabelsline = $extrafields -> fetch_name_optionals_label ( $object -> table_element_line );
$array_options = $extrafields -> getOptionalsFromPost ( $object -> table_element_line );
// Unset extrafield POST Data
if ( is_array ( $extralabelsline )) {
foreach ( $extralabelsline as $key => $value ) {
2019-11-08 10:53:31 +01:00
unset ( $_POST [ " options_ " . $key ]);
2017-10-16 08:47:05 +02:00
}
2019-10-27 09:51:15 +01:00
}
2014-08-07 15:12:27 +02:00
2022-01-10 03:24:01 +01:00
$result = $object -> updateline ( GETPOST ( 'lineid' , 'int' ), $label , $up , $tva_tx , $localtax1_tx , $localtax2_tx , price2num ( GETPOST ( 'qty' ), 'MS' ), GETPOST ( 'productid' , 'int' ), $price_base_type , $info_bits , $type , $remise_percent , 0 , $date_start , $date_end , $array_options , GETPOST ( 'units' ), $pu_devise , GETPOST ( 'fourn_ref' , 'alpha' ));
2021-02-25 23:21:30 +01:00
if ( $result >= 0 ) {
2020-10-07 15:01:28 +02:00
unset ( $_POST [ 'label' ]);
unset ( $_POST [ 'fourn_ref' ]);
unset ( $_POST [ 'date_starthour' ]);
2019-10-27 09:51:15 +01:00
unset ( $_POST [ 'date_startmin' ]);
unset ( $_POST [ 'date_startsec' ]);
unset ( $_POST [ 'date_startday' ]);
unset ( $_POST [ 'date_startmonth' ]);
unset ( $_POST [ 'date_startyear' ]);
unset ( $_POST [ 'date_endhour' ]);
unset ( $_POST [ 'date_endmin' ]);
unset ( $_POST [ 'date_endsec' ]);
unset ( $_POST [ 'date_endday' ]);
unset ( $_POST [ 'date_endmonth' ]);
unset ( $_POST [ 'date_endyear' ]);
2022-01-07 10:25:58 +01:00
unset ( $_POST [ 'price_ttc' ]);
unset ( $_POST [ 'price_ht' ]);
2019-10-27 09:51:15 +01:00
$db -> commit ();
2020-05-21 15:05:19 +02:00
} else {
2019-10-27 09:51:15 +01:00
$db -> rollback ();
setEventMessages ( $object -> error , $object -> errors , 'errors' );
}
2020-05-26 00:25:38 +02:00
} elseif ( $action == 'addline' && $usercancreate ) {
2022-02-10 15:03:41 +01:00
// Add a product line
2015-02-28 03:11:15 +01:00
$db -> begin ();
2019-11-08 10:53:31 +01:00
$ret = $object -> fetch ( $id );
2021-02-25 23:21:30 +01:00
if ( $ret < 0 ) {
2019-01-27 11:55:16 +01:00
dol_print_error ( $db , $object -> error );
2017-10-16 08:47:05 +02:00
exit ;
}
2019-11-08 10:53:31 +01:00
$ret = $object -> fetch_thirdparty ();
2015-02-28 03:11:15 +01:00
2017-10-16 08:47:05 +02:00
$langs -> load ( 'errors' );
2019-11-08 10:53:31 +01:00
$error = 0 ;
2015-02-28 03:11:15 +01:00
// Set if we used free entry or predefined product
2019-11-08 10:53:31 +01:00
$predef = '' ;
2021-01-02 16:18:16 +01:00
$product_desc = ( GETPOSTISSET ( 'dp_desc' ) ? GETPOST ( 'dp_desc' , 'restricthtml' ) : '' );
2019-11-08 10:53:31 +01:00
$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' ));
$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' ));
2018-08-29 17:03:18 +02:00
2016-07-23 16:37:21 +02:00
$prod_entry_mode = GETPOST ( 'prod_entry_mode' );
2021-02-25 23:21:30 +01:00
if ( $prod_entry_mode == 'free' ) {
2019-11-08 10:53:31 +01:00
$idprod = 0 ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-08 10:53:31 +01:00
$idprod = GETPOST ( 'idprod' , 'int' );
2015-02-28 03:11:15 +01:00
}
2012-10-28 13:57:21 +01:00
2022-01-10 03:24:01 +01:00
$tva_tx = ( GETPOST ( 'tva_tx' ) ? GETPOST ( 'tva_tx' ) : 0 ); // Can be '1.2' or '1.2 (CODE)'
$price_ht = price2num ( GETPOST ( 'price_ht' ), 'MU' , 2 );
$price_ht_devise = price2num ( GETPOST ( 'multicurrency_price_ht' ), 'CU' , 2 );
$price_ttc = price2num ( GETPOST ( 'price_ttc' ), 'MU' , 2 );
$price_ttc_devise = price2num ( GETPOST ( 'multicurrency_price_ttc' ), 'CU' , 2 );
2021-01-11 13:00:29 +01:00
$qty = price2num ( GETPOST ( 'qty' . $predef , 'alpha' ), 'MS' );
2022-02-10 15:03:41 +01:00
$remise_percent = ( GETPOSTISSET ( 'remise_percent' . $predef ) ? price2num ( GETPOST ( 'remise_percent' . $predef , 'alpha' ), '' , 2 ) : 0 );
if ( empty ( $remise_percent )) {
$remise_percent = 0 ;
}
2015-02-28 03:11:15 +01:00
2017-10-16 08:47:05 +02:00
// Extrafields
2019-10-06 14:41:52 +02:00
$extralabelsline = $extrafields -> fetch_name_optionals_label ( $object -> table_element_line );
$array_options = $extrafields -> getOptionalsFromPost ( $object -> table_element_line , $predef );
2017-10-16 08:47:05 +02:00
// Unset extrafield
if ( is_array ( $extralabelsline )) {
// Get extra fields
foreach ( $extralabelsline as $key => $value ) {
2019-11-08 10:53:31 +01:00
unset ( $_POST [ " options_ " . $key ]);
2017-10-16 08:47:05 +02:00
}
}
2015-12-07 03:59:03 +01:00
2021-02-25 23:21:30 +01:00
if ( $prod_entry_mode == 'free' && GETPOST ( 'price_ht' ) < 0 && $qty < 0 ) {
2017-10-16 08:47:05 +02:00
setEventMessages ( $langs -> trans ( 'ErrorBothFieldCantBeNegative' , $langs -> transnoentitiesnoconv ( 'UnitPrice' ), $langs -> transnoentitiesnoconv ( 'Qty' )), null , 'errors' );
$error ++ ;
}
2021-02-25 23:21:30 +01:00
if ( $prod_entry_mode == 'free' && ! GETPOST ( 'idprodfournprice' ) && GETPOST ( 'type' ) < 0 ) {
2017-10-16 08:47:05 +02:00
setEventMessages ( $langs -> trans ( 'ErrorFieldRequired' , $langs -> transnoentitiesnoconv ( 'Type' )), null , 'errors' );
$error ++ ;
}
2021-02-25 23:21:30 +01:00
if ( $prod_entry_mode == 'free' && GETPOST ( 'price_ht' ) === '' && GETPOST ( 'price_ttc' ) === '' && $price_ht_devise === '' ) { // Unit price can be 0 but not ''
2019-07-05 20:24:22 +02:00
setEventMessages ( $langs -> trans ( 'ErrorFieldRequired' , $langs -> transnoentitiesnoconv ( 'UnitPrice' )), null , 'errors' );
2017-10-16 08:47:05 +02:00
$error ++ ;
}
2021-02-25 23:21:30 +01:00
if ( $prod_entry_mode == 'free' && ! GETPOST ( 'dp_desc' )) {
2017-10-16 08:47:05 +02:00
setEventMessages ( $langs -> trans ( 'ErrorFieldRequired' , $langs -> transnoentitiesnoconv ( 'Description' )), null , 'errors' );
$error ++ ;
}
2022-03-26 11:22:54 +01:00
if ( ! GETPOST ( 'qty' , 'alpha' )) { // 0 is NOT allowed for invoices
2017-10-16 08:47:05 +02:00
setEventMessages ( $langs -> trans ( 'ErrorFieldRequired' , $langs -> transnoentitiesnoconv ( 'Qty' )), null , 'errors' );
$error ++ ;
}
2015-02-28 03:11:15 +01:00
2017-02-08 12:37:38 +01:00
if ( ! $error && ! empty ( $conf -> variants -> enabled ) && $prod_entry_mode != 'free' ) {
2016-07-23 16:37:21 +02:00
if ( $combinations = GETPOST ( 'combinations' , 'array' )) {
//Check if there is a product with the given combination
$prodcomb = new ProductCombination ( $db );
if ( $res = $prodcomb -> fetchByProductCombination2ValuePairs ( $idprod , $combinations )) {
$idprod = $res -> fk_product_child ;
} else {
2018-07-25 15:36:50 +02:00
setEventMessages ( $langs -> trans ( 'ErrorProductCombinationNotFound' ), null , 'errors' );
2019-11-08 10:53:31 +01:00
$error ++ ;
2016-07-23 16:37:21 +02:00
}
}
}
2021-02-25 23:21:30 +01:00
if ( $prod_entry_mode != 'free' && empty ( $error )) { // With combolist mode idprodfournprice is > 0 or -1. With autocomplete, idprodfournprice is > 0 or ''
2019-11-08 10:53:31 +01:00
$productsupplier = new ProductFournisseur ( $db );
2015-02-28 03:11:15 +01:00
2019-11-08 10:53:31 +01:00
$idprod = 0 ;
2021-02-25 23:21:30 +01:00
if ( GETPOST ( 'idprodfournprice' , 'alpha' ) == - 1 || GETPOST ( 'idprodfournprice' , 'alpha' ) == '' ) {
$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, ...)
}
2015-02-28 03:11:15 +01:00
2020-01-01 17:15:26 +01:00
$reg = array ();
2021-02-25 23:21:30 +01:00
if ( preg_match ( '/^idprod_([0-9]+)$/' , GETPOST ( 'idprodfournprice' , 'alpha' ), $reg )) {
2019-11-08 10:53:31 +01:00
$idprod = $reg [ 1 ];
$res = $productsupplier -> fetch ( $idprod ); // Load product from its id
2018-06-16 14:47:33 +02:00
// Call to init some price properties of $productsupplier
2017-08-23 15:02:38 +02:00
// So if a supplier price already exists for another thirdparty (first one found), we use it as reference price
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> global -> SUPPLIER_TAKE_FIRST_PRICE_IF_NO_PRICE_FOR_CURRENT_SUPPLIER )) {
2018-06-16 14:47:33 +02:00
$fksoctosearch = 0 ;
2019-11-08 10:53:31 +01:00
$productsupplier -> get_buyprice ( 0 , - 1 , $idprod , 'none' , $fksoctosearch ); // We force qty to -1 to be sure to find if a supplier price exist
2021-02-25 23:21:30 +01:00
if ( $productsupplier -> fourn_socid != $socid ) { // The price we found is for another supplier, so we clear supplier price
2018-06-16 14:47:33 +02:00
$productsupplier -> ref_supplier = '' ;
}
2020-05-21 15:05:19 +02:00
} else {
2018-06-16 14:47:33 +02:00
$fksoctosearch = $object -> thirdparty -> id ;
2019-11-08 10:53:31 +01:00
$productsupplier -> get_buyprice ( 0 , - 1 , $idprod , 'none' , $fksoctosearch ); // We force qty to -1 to be sure to find if a supplier price exist
2018-06-16 14:47:33 +02:00
}
2021-02-25 23:21:30 +01:00
} elseif ( GETPOST ( 'idprodfournprice' , 'alpha' ) > 0 ) {
2019-11-08 10:53:31 +01:00
$qtytosearch = $qty ; // Just to see if a price exists for the quantity. Not used to found vat.
2017-10-16 08:47:05 +02:00
//$qtytosearch=-1; // We force qty to -1 to be sure to find if a supplier price exist
2019-11-08 10:53:31 +01:00
$idprod = $productsupplier -> get_buyprice ( GETPOST ( 'idprodfournprice' , 'alpha' ), $qtytosearch );
$res = $productsupplier -> fetch ( $idprod );
2017-10-16 08:47:05 +02:00
}
2015-02-28 03:11:15 +01:00
2021-02-25 23:21:30 +01:00
if ( $idprod > 0 ) {
2017-10-16 08:47:05 +02:00
$label = $productsupplier -> label ;
2022-03-26 11:22:54 +01:00
// Define output language
if ( ! empty ( $conf -> global -> MAIN_MULTILANGS ) && ! empty ( $conf -> global -> PRODUIT_TEXTS_IN_THIRDPARTY_LANGUAGE )) {
$outputlangs = $langs ;
$newlang = '' ;
if ( empty ( $newlang ) && GETPOST ( 'lang_id' , 'aZ09' )) {
$newlang = GETPOST ( 'lang_id' , 'aZ09' );
}
if ( empty ( $newlang )) {
$newlang = $object -> thirdparty -> default_lang ;
}
if ( ! empty ( $newlang )) {
$outputlangs = new Translate ( " " , $conf );
$outputlangs -> setDefaultLang ( $newlang );
}
$desc = ( ! empty ( $productsupplier -> multilangs [ $outputlangs -> defaultlang ][ " description " ])) ? $productsupplier -> multilangs [ $outputlangs -> defaultlang ][ " description " ] : $productsupplier -> description ;
} else {
$desc = $productsupplier -> description ;
}
2018-08-06 16:26:25 +02:00
// if we use supplier description of the products
2019-11-08 10:53:31 +01:00
if ( ! empty ( $productsupplier -> desc_supplier ) && ! empty ( $conf -> global -> PRODUIT_FOURN_TEXTS )) {
2020-10-07 15:01:28 +02:00
$desc = $productsupplier -> desc_supplier ;
2021-02-25 23:21:30 +01:00
}
2018-08-29 17:03:18 +02:00
2020-10-28 16:37:41 +01:00
//If text set in desc is the same as product descpription (as now it's preloaded) whe add it only one time
2022-01-10 03:24:01 +01:00
if ( trim ( $product_desc ) == trim ( $desc ) && ! empty ( $conf -> global -> PRODUIT_AUTOFILL_DESC )) {
$product_desc = '' ;
2020-10-28 16:37:41 +01:00
}
2021-02-25 23:21:30 +01:00
if ( ! empty ( $product_desc ) && ! empty ( $conf -> global -> MAIN_NO_CONCAT_DESCRIPTION )) {
$desc = $product_desc ;
}
if ( ! empty ( $product_desc ) && trim ( $product_desc ) != trim ( $desc )) {
$desc = dol_concatdesc ( $desc , $product_desc , '' , ! empty ( $conf -> global -> MAIN_CHANGE_ORDER_CONCAT_DESCRIPTION ));
}
2015-02-28 03:11:15 +01:00
2022-01-10 03:24:01 +01:00
$ref_supplier = $productsupplier -> ref_supplier ;
// Get vat rate
if ( ! GETPOSTISSET ( 'tva_tx' )) { // If vat rate not provided from the form (the form has the priority)
$tva_tx = get_default_tva ( $object -> thirdparty , $mysoc , $productsupplier -> id , GETPOST ( 'idprodfournprice' , 'alpha' ));
$tva_npr = get_default_npr ( $object -> thirdparty , $mysoc , $productsupplier -> id , GETPOST ( 'idprodfournprice' , 'alpha' ));
}
if ( empty ( $tva_tx )) {
$tva_npr = 0 ;
}
$localtax1_tx = get_localtax ( $tva_tx , 1 , $mysoc , $object -> thirdparty , $tva_npr );
$localtax2_tx = get_localtax ( $tva_tx , 2 , $mysoc , $object -> thirdparty , $tva_npr );
2018-04-24 12:57:31 +02:00
$type = $productsupplier -> type ;
2021-01-11 13:36:53 +01:00
if ( GETPOST ( 'price_ht' ) != '' || GETPOST ( 'price_ht_devise' ) != '' ) {
2020-01-01 17:15:26 +01:00
$price_base_type = 'HT' ;
$pu = price2num ( $price_ht , 'MU' );
2022-01-10 03:24:01 +01:00
$pu_devise = price2num ( $price_ht_devise , 'CU' );
} elseif ( GETPOST ( 'price_ttc' ) != '' || GETPOST ( 'price_ttc_devise' ) != '' ) {
$price_base_type = 'TTC' ;
$pu = price2num ( $price_ttc , 'MU' );
$pu_devise = price2num ( $price_ttc_devise , 'CU' );
2020-01-01 17:15:26 +01:00
} else {
$price_base_type = ( $productsupplier -> fourn_price_base_type ? $productsupplier -> fourn_price_base_type : 'HT' );
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
$pu = $productsupplier -> fourn_pu ;
2022-01-10 03:24:01 +01:00
$pu_devise = 0 ;
2020-01-01 17:15:26 +01:00
} else {
$pu = $productsupplier -> fourn_pu ;
2022-01-10 03:24:01 +01:00
$pu_devise = $productsupplier -> fourn_multicurrency_unitprice ;
2020-01-01 17:15:26 +01:00
}
}
2015-02-28 03:11:15 +01:00
2021-02-25 23:21:30 +01:00
if ( empty ( $pu )) {
$pu = 0 ; // If pu is '' or null, we force to have a numeric value
}
2018-06-16 14:47:33 +02:00
2019-11-08 10:53:31 +01:00
$result = $object -> addline (
2017-10-16 08:47:05 +02:00
$desc ,
2018-06-16 14:47:33 +02:00
$pu ,
2017-10-16 08:47:05 +02:00
$tva_tx ,
$localtax1_tx ,
$localtax2_tx ,
$qty ,
$idprod ,
$remise_percent ,
$date_start ,
$date_end ,
0 ,
$tva_npr ,
$price_base_type ,
$type ,
2021-11-22 16:13:46 +01:00
min ( $rank , count ( $object -> lines ) + 1 ),
2017-10-16 08:47:05 +02:00
0 ,
$array_options ,
2017-11-12 16:06:08 +01:00
$productsupplier -> fk_unit ,
2018-02-23 11:06:07 +01:00
0 ,
2022-01-10 03:24:01 +01:00
$pu_devise ,
2020-10-07 15:01:28 +02:00
$ref_supplier ,
2020-01-01 17:15:26 +01:00
''
2017-10-16 08:47:05 +02:00
);
}
2021-02-25 23:21:30 +01:00
if ( $idprod == - 99 || $idprod == 0 ) {
2017-10-16 08:47:05 +02:00
// Product not selected
$error ++ ;
$langs -> load ( " errors " );
setEventMessages ( $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " ProductOrService " )), null , 'errors' );
}
2021-02-25 23:21:30 +01:00
if ( $idprod == - 1 ) {
2017-10-16 08:47:05 +02:00
// Quantity too low
$error ++ ;
$langs -> load ( " errors " );
setEventMessages ( $langs -> trans ( " ErrorQtyTooLowForThisSupplier " ), null , 'errors' );
}
2021-02-25 23:21:30 +01:00
} elseif ( empty ( $error )) { // $price_ht is already set
2015-02-28 03:11:15 +01:00
$tva_npr = ( preg_match ( '/\*/' , $tva_tx ) ? 1 : 0 );
$tva_tx = str_replace ( '*' , '' , $tva_tx );
$label = ( GETPOST ( 'product_label' ) ? GETPOST ( 'product_label' ) : '' );
$desc = $product_desc ;
$type = GETPOST ( 'type' );
2019-01-27 11:55:16 +01:00
$ref_supplier = GETPOST ( 'fourn_ref' , 'alpha' );
2015-02-28 03:11:15 +01:00
2019-11-08 10:53:31 +01:00
$fk_unit = GETPOST ( 'units' , 'alpha' );
2015-04-19 15:59:49 +02:00
2021-01-15 19:43:02 +01:00
if ( ! preg_match ( '/\((.*)\)/' , $tva_tx )) {
$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'
}
2015-02-28 03:11:15 +01:00
2017-10-16 08:47:05 +02:00
// Local Taxes
2019-11-08 10:53:31 +01:00
$localtax1_tx = get_localtax ( $tva_tx , 1 , $mysoc , $object -> thirdparty );
$localtax2_tx = get_localtax ( $tva_tx , 2 , $mysoc , $object -> thirdparty );
2015-02-28 03:11:15 +01:00
2021-01-11 13:36:53 +01:00
if ( GETPOST ( 'price_ht' ) != '' || GETPOST ( 'price_ht_devise' ) != '' ) {
FIX: excl. tax price not properly rounded
When adding a 'free line' product or service in a supplier invoice, the amount excluding tax (HT) is not properly rounded, in particular when calculated from full tax price. This leads to inconsistencies in results when using large quantities or small precision in settings. Test case :
with default settings
MAIN_MAX_DECIMALS_UNIT = 5
MAIN_MAX_DECIMALS_TOT = 2
MAIN_MAX_DECIMALS_SHOWN = 8
Add in a new invoice a free product with 13.33 TTC (full tax), quantity 1, save, then edit the line with quantity 10111
Now in another invoice, add a free product with 13.33 TTC (full tax), quantity 10111, save.
Exact same data in the two invoices, but Tax and grand total are different, and false in second case because computed before rounding.
Also removed some direct access to $_POST array.
2016-10-26 08:55:43 +02:00
$pu_ht = price2num ( $price_ht , 'MU' ); // $pu_ht must be rounded according to settings
2020-05-21 15:05:19 +02:00
} else {
FIX: excl. tax price not properly rounded
When adding a 'free line' product or service in a supplier invoice, the amount excluding tax (HT) is not properly rounded, in particular when calculated from full tax price. This leads to inconsistencies in results when using large quantities or small precision in settings. Test case :
with default settings
MAIN_MAX_DECIMALS_UNIT = 5
MAIN_MAX_DECIMALS_TOT = 2
MAIN_MAX_DECIMALS_SHOWN = 8
Add in a new invoice a free product with 13.33 TTC (full tax), quantity 1, save, then edit the line with quantity 10111
Now in another invoice, add a free product with 13.33 TTC (full tax), quantity 10111, save.
Exact same data in the two invoices, but Tax and grand total are different, and false in second case because computed before rounding.
Also removed some direct access to $_POST array.
2016-10-26 08:55:43 +02:00
$pu_ttc = price2num ( GETPOST ( 'price_ttc' ), 'MU' );
$pu_ht = price2num ( $pu_ttc / ( 1 + ( $tva_tx / 100 )), 'MU' ); // $pu_ht must be rounded according to settings
}
$price_base_type = 'HT' ;
2022-01-10 03:24:01 +01:00
$pu_devise = price2num ( $price_devise , 'CU' );
2015-04-19 15:59:49 +02:00
2022-01-10 03:24:01 +01:00
$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 );
2017-10-16 08:47:05 +02:00
}
2015-02-28 03:11:15 +01:00
2017-10-16 08:47:05 +02:00
//print "xx".$tva_tx; exit;
2021-02-25 23:21:30 +01:00
if ( ! $error && $result > 0 ) {
2017-10-16 08:47:05 +02:00
$db -> commit ();
// Define output language
2021-02-25 23:21:30 +01:00
if ( empty ( $conf -> global -> MAIN_DISABLE_PDF_AUTOUPDATE )) {
2017-10-16 08:47:05 +02:00
$outputlangs = $langs ;
$newlang = '' ;
2021-02-25 23:21:30 +01:00
if ( $conf -> global -> MAIN_MULTILANGS && empty ( $newlang ) && GETPOST ( 'lang_id' , 'aZ09' )) {
$newlang = GETPOST ( 'lang_id' , 'aZ09' );
}
if ( $conf -> global -> MAIN_MULTILANGS && empty ( $newlang )) {
$newlang = $object -> thirdparty -> default_lang ;
}
2019-11-08 10:53:31 +01:00
if ( ! empty ( $newlang )) {
2017-10-16 08:47:05 +02:00
$outputlangs = new Translate ( " " , $conf );
$outputlangs -> setDefaultLang ( $newlang );
}
2020-09-10 01:49:09 +02:00
$model = $object -> model_pdf ;
2017-10-16 08:47:05 +02:00
$ret = $object -> fetch ( $id ); // Reload to get new records
2019-11-08 10:53:31 +01:00
$result = $object -> generateDocument ( $model , $outputlangs , $hidedetails , $hidedesc , $hideref );
2021-02-25 23:21:30 +01:00
if ( $result < 0 ) {
dol_print_error ( $db , $result );
}
2017-10-16 08:47:05 +02:00
}
2015-02-28 03:11:15 +01:00
unset ( $_POST [ 'prod_entry_mode' ]);
2017-10-16 08:47:05 +02:00
unset ( $_POST [ 'qty' ]);
unset ( $_POST [ 'type' ]);
unset ( $_POST [ 'remise_percent' ]);
unset ( $_POST [ 'pu' ]);
unset ( $_POST [ 'price_ht' ]);
2016-04-12 14:10:27 +02:00
unset ( $_POST [ 'multicurrency_price_ht' ]);
2017-10-16 08:47:05 +02:00
unset ( $_POST [ 'price_ttc' ]);
2017-11-12 16:06:08 +01:00
unset ( $_POST [ 'fourn_ref' ]);
2017-10-16 08:47:05 +02:00
unset ( $_POST [ 'tva_tx' ]);
unset ( $_POST [ 'label' ]);
unset ( $localtax1_tx );
unset ( $localtax2_tx );
2015-02-28 03:11:15 +01:00
unset ( $_POST [ 'np_marginRate' ]);
unset ( $_POST [ 'np_markRate' ]);
2017-10-16 08:47:05 +02:00
unset ( $_POST [ 'dp_desc' ]);
2015-02-28 03:11:15 +01:00
unset ( $_POST [ 'idprodfournprice' ]);
2017-10-16 08:47:05 +02:00
unset ( $_POST [ 'units' ]);
unset ( $_POST [ 'date_starthour' ]);
unset ( $_POST [ 'date_startmin' ]);
unset ( $_POST [ 'date_startsec' ]);
unset ( $_POST [ 'date_startday' ]);
unset ( $_POST [ 'date_startmonth' ]);
unset ( $_POST [ 'date_startyear' ]);
unset ( $_POST [ 'date_endhour' ]);
unset ( $_POST [ 'date_endmin' ]);
unset ( $_POST [ 'date_endsec' ]);
unset ( $_POST [ 'date_endday' ]);
unset ( $_POST [ 'date_endmonth' ]);
unset ( $_POST [ 'date_endyear' ]);
2020-05-21 15:05:19 +02:00
} else {
2017-10-16 08:47:05 +02:00
$db -> rollback ();
setEventMessages ( $object -> error , $object -> errors , 'errors' );
}
$action = '' ;
2020-05-26 00:25:38 +02:00
} elseif ( $action == 'classin' && $usercancreate ) {
2017-10-16 08:47:05 +02:00
$object -> fetch ( $id );
2019-11-08 10:53:31 +01:00
$result = $object -> setProject ( $projectid );
2021-02-25 23:21:30 +01:00
} elseif ( $action == 'confirm_edit' && $confirm == 'yes' && $usercancreate ) {
// Set invoice to draft status
2017-10-16 08:47:05 +02:00
$object -> fetch ( $id );
2015-02-28 03:11:15 +01:00
2022-05-22 17:09:08 +02:00
$totalpaid = $object -> getSommePaiement ();
$resteapayer = $object -> total_ttc - $totalpaid ;
2015-02-28 03:11:15 +01:00
2019-06-29 16:29:32 +02:00
// We check that lines of invoices are exported in accountancy
2019-10-04 10:31:57 +02:00
$ventilExportCompta = $object -> getVentilExportCompta ();
2015-02-28 03:11:15 +01:00
2021-02-25 23:21:30 +01:00
if ( ! $ventilExportCompta ) {
2020-10-07 15:01:28 +02:00
// On verifie si aucun paiement n'a ete effectue
2021-02-25 23:21:30 +01:00
if ( $resteapayer == price2num ( $object -> total_ttc , 'MT' , 1 ) && $object -> statut == FactureFournisseur :: STATUS_VALIDATED ) {
2020-10-07 15:01:28 +02:00
$idwarehouse = GETPOST ( 'idwarehouse' );
$object -> fetch_thirdparty ();
$qualified_for_stock_change = 0 ;
2021-02-25 23:21:30 +01:00
if ( empty ( $conf -> global -> STOCK_SUPPORTS_SERVICES )) {
2020-10-07 15:01:28 +02:00
$qualified_for_stock_change = $object -> hasProductsOrServices ( 2 );
} else {
$qualified_for_stock_change = $object -> hasProductsOrServices ( 1 );
}
// Check parameters
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> stock -> enabled ) && ! empty ( $conf -> global -> STOCK_CALCULATE_ON_SUPPLIER_BILL ) && $qualified_for_stock_change ) {
2020-10-07 15:01:28 +02:00
$langs -> load ( " stocks " );
2021-02-25 23:21:30 +01:00
if ( ! $idwarehouse || $idwarehouse == - 1 ) {
2020-10-07 15:01:28 +02:00
$error ++ ;
setEventMessages ( $langs -> trans ( 'ErrorFieldRequired' , $langs -> transnoentitiesnoconv ( " Warehouse " )), null , 'errors' );
$action = '' ;
}
}
$object -> setDraft ( $user , $idwarehouse );
2015-02-28 03:11:15 +01:00
2019-10-04 10:31:57 +02:00
// Define output language
2021-02-25 23:21:30 +01:00
if ( empty ( $conf -> global -> MAIN_DISABLE_PDF_AUTOUPDATE )) {
2019-10-04 10:31:57 +02:00
$outputlangs = $langs ;
$newlang = '' ;
2021-02-25 23:21:30 +01:00
if ( $conf -> global -> MAIN_MULTILANGS && empty ( $newlang ) && GETPOST ( 'lang_id' , 'aZ09' )) {
$newlang = GETPOST ( 'lang_id' , 'aZ09' );
}
if ( $conf -> global -> MAIN_MULTILANGS && empty ( $newlang )) {
$newlang = $object -> thirdparty -> default_lang ;
}
2019-11-08 10:53:31 +01:00
if ( ! empty ( $newlang )) {
2019-10-04 10:31:57 +02:00
$outputlangs = new Translate ( " " , $conf );
$outputlangs -> setDefaultLang ( $newlang );
}
2020-09-10 01:49:09 +02:00
$model = $object -> model_pdf ;
2019-10-04 10:31:57 +02:00
$ret = $object -> fetch ( $id ); // Reload to get new records
2015-02-28 03:11:15 +01:00
2019-11-08 10:53:31 +01:00
$result = $object -> generateDocument ( $model , $outputlangs , $hidedetails , $hidedesc , $hideref );
2021-02-25 23:21:30 +01:00
if ( $result < 0 ) {
dol_print_error ( $db , $result );
}
2017-10-16 08:47:05 +02:00
}
2019-11-08 10:53:31 +01:00
$action = '' ;
2017-10-16 08:47:05 +02:00
}
}
2021-02-25 23:21:30 +01:00
} elseif ( $action == 'reopen' && $usercancreate ) {
// Set invoice to validated/unpaid status
2017-10-16 08:47:05 +02:00
$result = $object -> fetch ( $id );
if ( $object -> statut == FactureFournisseur :: STATUS_CLOSED
2021-02-25 23:21:30 +01:00
|| ( $object -> statut == FactureFournisseur :: STATUS_ABANDONED && $object -> close_code != 'replaced' )) {
2021-02-09 09:55:48 +01:00
$result = $object -> setUnpaid ( $user );
2021-02-25 23:21:30 +01:00
if ( $result > 0 ) {
2017-10-16 08:47:05 +02:00
header ( 'Location: ' . $_SERVER [ " PHP_SELF " ] . '?id=' . $id );
exit ;
2020-05-21 15:05:19 +02:00
} else {
2017-10-16 08:47:05 +02:00
setEventMessages ( $object -> error , $object -> errors , 'errors' );
}
}
2015-02-28 03:11:15 +01:00
}
2012-10-28 13:57:21 +01:00
2017-09-17 15:38:50 +02:00
// Actions when printing a doc from card
include DOL_DOCUMENT_ROOT . '/core/actions_printing.inc.php' ;
2016-08-10 09:47:25 +02:00
2016-06-24 16:10:52 +02:00
// Actions to send emails
2019-12-02 09:38:16 +01:00
$triggersendname = 'BILL_SUPPLIER_SENTBYMAIL' ;
2019-11-08 10:53:31 +01:00
$paramname = 'id' ;
$autocopy = 'MAIN_MAIL_AUTOCOPY_SUPPLIER_INVOICE_TO' ;
2020-11-08 14:39:20 +01:00
$trackid = 'sinv' . $object -> id ;
2016-06-24 16:10:52 +02:00
include DOL_DOCUMENT_ROOT . '/core/actions_sendmails.inc.php' ;
2016-08-10 09:47:25 +02:00
2017-05-05 12:33:49 +02:00
// Actions to build doc
$upload_dir = $conf -> fournisseur -> facture -> dir_output ;
2020-04-21 10:02:18 +02:00
$permissiontoadd = $usercancreate ;
2017-05-05 12:33:49 +02:00
include DOL_DOCUMENT_ROOT . '/core/actions_builddoc.inc.php' ;
2017-06-07 16:44:04 +02:00
2015-02-28 03:11:15 +01:00
// Make calculation according to calculationrule
2021-02-25 23:21:30 +01:00
if ( $action == 'calculate' ) {
2019-11-08 10:53:31 +01:00
$calculationrule = GETPOST ( 'calculationrule' );
2015-02-28 03:11:15 +01:00
2017-10-16 08:47:05 +02:00
$object -> fetch ( $id );
$object -> fetch_thirdparty ();
2019-11-08 10:53:31 +01:00
$result = $object -> update_price ( 0 , (( $calculationrule == 'totalofround' ) ? '0' : '1' ), 0 , $object -> thirdparty );
2021-02-25 23:21:30 +01:00
if ( $result <= 0 ) {
2019-01-27 11:55:16 +01:00
dol_print_error ( $db , $result );
2017-10-16 08:47:05 +02:00
exit ;
}
2015-02-28 03:11:15 +01:00
}
2021-02-25 23:21:30 +01:00
if ( $action == 'update_extras' ) {
2018-02-23 16:11:00 +01:00
$object -> oldcopy = dol_clone ( $object );
2015-02-28 03:11:15 +01:00
// Fill array 'array_options' with data from add form
2020-09-18 01:29:17 +02:00
$ret = $extrafields -> setOptionalsFromPost ( null , $object , GETPOST ( 'attribute' , 'restricthtml' ));
2021-02-25 23:21:30 +01:00
if ( $ret < 0 ) {
$error ++ ;
}
2013-09-06 13:25:45 +02:00
2021-02-25 23:21:30 +01:00
if ( ! $error ) {
2018-04-10 12:03:01 +02:00
// Actions on extra fields
2021-02-25 23:21:30 +01:00
if ( ! $error ) {
2019-11-08 10:53:31 +01:00
$result = $object -> insertExtraFields ( 'BILL_SUPPLIER_MODIFY' );
2021-02-25 23:21:30 +01:00
if ( $result < 0 ) {
2018-04-10 12:03:01 +02:00
$error ++ ;
2015-02-28 03:11:15 +01:00
}
}
}
2018-04-10 12:03:01 +02:00
2021-02-25 23:21:30 +01:00
if ( $error ) {
2015-02-28 03:11:15 +01:00
$action = 'edit_extras' ;
2021-02-25 23:21:30 +01:00
}
2015-02-28 03:11:15 +01:00
}
2014-10-14 17:00:06 +02:00
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> global -> MAIN_DISABLE_CONTACTS_TAB ) && $usercancreate ) {
if ( $action == 'addcontact' ) {
2015-02-28 03:11:15 +01:00
$result = $object -> fetch ( $id );
2014-10-14 17:00:06 +02:00
2021-02-25 23:21:30 +01:00
if ( $result > 0 && $id > 0 ) {
2015-02-28 03:11:15 +01:00
$contactid = ( GETPOST ( 'userid' ) ? GETPOST ( 'userid' ) : GETPOST ( 'contactid' ));
2020-11-30 15:18:58 +01:00
$typeid = ( GETPOST ( 'typecontact' ) ? GETPOST ( 'typecontact' ) : GETPOST ( 'type' ));
$result = $object -> add_contact ( $contactid , $typeid , GETPOST ( " source " , 'aZ09' ));
2015-02-28 03:11:15 +01:00
}
2014-10-14 17:00:06 +02:00
2021-02-25 23:21:30 +01:00
if ( $result >= 0 ) {
2015-02-28 03:11:15 +01:00
header ( " Location: " . $_SERVER [ 'PHP_SELF' ] . " ?id= " . $object -> id );
exit ;
2020-05-21 15:05:19 +02:00
} else {
2021-02-25 23:21:30 +01:00
if ( $object -> error == 'DB_ERROR_RECORD_ALREADY_EXISTS' ) {
2015-02-28 03:11:15 +01:00
$langs -> load ( " errors " );
2015-12-10 16:14:37 +01:00
setEventMessages ( $langs -> trans ( " ErrorThisContactIsAlreadyDefinedAsThisType " ), null , 'errors' );
2020-05-21 15:05:19 +02:00
} else {
2015-12-10 16:14:37 +01:00
setEventMessages ( $object -> error , $object -> errors , 'errors' );
2014-10-14 17:00:06 +02:00
}
}
2021-02-25 23:21:30 +01:00
} elseif ( $action == 'swapstatut' ) {
// bascule du statut d'un contact
if ( $object -> fetch ( $id )) {
2021-03-29 15:07:23 +02:00
$result = $object -> swapContactStatus ( GETPOST ( 'ligne' , 'int' ));
2020-05-21 15:05:19 +02:00
} else {
2015-02-28 03:11:15 +01:00
dol_print_error ( $db );
2012-10-28 13:57:21 +01:00
}
2021-02-25 23:21:30 +01:00
} elseif ( $action == 'deletecontact' ) {
// Efface un contact
2015-02-28 03:11:15 +01:00
$object -> fetch ( $id );
2021-03-29 15:07:23 +02:00
$result = $object -> delete_contact ( GETPOST ( " lineid " , 'int' ));
2012-10-28 13:57:21 +01:00
2021-02-25 23:21:30 +01:00
if ( $result >= 0 ) {
2015-02-28 03:11:15 +01:00
header ( " Location: " . $_SERVER [ 'PHP_SELF' ] . " ?id= " . $object -> id );
exit ;
2020-05-21 15:05:19 +02:00
} else {
2015-02-28 03:11:15 +01:00
dol_print_error ( $db );
}
2012-10-28 13:57:21 +01:00
}
}
}
/*
* View
*/
$form = new Form ( $db );
$formfile = new FormFile ( $db );
2019-11-08 10:53:31 +01:00
$bankaccountstatic = new Account ( $db );
$paymentstatic = new PaiementFourn ( $db );
2022-06-14 17:53:17 +02:00
if ( ! empty ( $conf -> project -> enabled )) {
2021-02-25 23:21:30 +01:00
$formproject = new FormProjets ( $db );
}
2016-09-26 00:10:29 +02:00
$now = dol_now ();
2012-10-28 13:57:21 +01:00
2019-11-08 10:53:31 +01:00
$title = $langs -> trans ( 'SupplierInvoice' ) . " - " . $langs -> trans ( 'Card' );
2021-05-31 10:36:43 +02:00
$help_url = 'EN:Module_Suppliers_Invoices|FR:Module_Fournisseurs_Factures|ES:Módulo_Facturas_de_proveedores|DE:Modul_Lieferantenrechnungen' ;
llxHeader ( '' , $title , $help_url );
2012-10-28 13:57:21 +01:00
// Mode creation
2022-07-11 00:05:50 +02:00
if ( $action == 'create' ) {
2014-11-06 22:16:27 +01:00
$facturestatic = new FactureFournisseur ( $db );
2015-01-25 21:32:40 +01:00
2020-04-25 13:49:44 +02:00
print load_fiche_titre ( $langs -> trans ( 'NewBill' ), '' , 'supplier_invoice' );
2012-10-28 13:57:21 +01:00
2017-10-16 08:47:05 +02:00
dol_htmloutput_events ();
2012-10-28 13:57:21 +01:00
2016-02-21 22:47:52 +01:00
$currency_code = $conf -> currency ;
2016-08-10 09:47:25 +02:00
2019-11-08 10:53:31 +01:00
$societe = '' ;
2022-04-19 23:24:08 +02:00
if ( GETPOST ( 'socid' , 'int' ) > 0 ) {
2019-11-08 10:53:31 +01:00
$societe = new Societe ( $db );
2019-01-27 11:55:16 +01:00
$societe -> fetch ( GETPOST ( 'socid' , 'int' ));
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> multicurrency -> enabled ) && ! empty ( $societe -> multicurrency_code )) {
$currency_code = $societe -> multicurrency_code ;
}
2016-10-02 21:39:24 +02:00
}
2016-08-10 09:47:25 +02:00
2020-12-23 23:16:27 +01:00
if ( ! empty ( $origin ) && ! empty ( $originid )) {
2017-10-16 08:47:05 +02:00
// Parse element/subelement (ex: project_task)
$element = $subelement = $origin ;
2012-10-28 13:57:21 +01:00
2020-12-23 23:16:27 +01:00
if ( $element == 'project' ) {
2017-10-16 08:47:05 +02:00
$projectid = $originid ;
$element = 'projet' ;
}
2019-01-17 19:27:26 +01:00
2019-10-27 09:51:15 +01:00
// For compatibility
2019-11-08 10:53:31 +01:00
if ( $element == 'order' ) {
2019-10-27 09:51:15 +01:00
$element = $subelement = 'commande' ;
}
2019-11-08 10:53:31 +01:00
if ( $element == 'propal' ) {
2019-10-27 09:51:15 +01:00
$element = 'comm/propal' ; $subelement = 'propal' ;
}
if ( $element == 'contract' ) {
$element = $subelement = 'contrat' ;
}
if ( $element == 'order_supplier' ) {
$element = 'fourn' ; $subelement = 'fournisseur.commande' ;
}
2012-10-28 13:57:21 +01:00
2019-10-27 09:51:15 +01:00
require_once DOL_DOCUMENT_ROOT . '/' . $element . '/class/' . $subelement . '.class.php' ;
$classname = ucfirst ( $subelement );
2021-02-25 23:21:30 +01:00
if ( $classname == 'Fournisseur.commande' ) {
$classname = 'CommandeFournisseur' ;
}
2019-10-27 09:51:15 +01:00
$objectsrc = new $classname ( $db );
$objectsrc -> fetch ( $originid );
$objectsrc -> fetch_thirdparty ();
2012-10-28 13:57:21 +01:00
2019-11-08 10:53:31 +01:00
$projectid = ( ! empty ( $objectsrc -> fk_project ) ? $objectsrc -> fk_project : '' );
2019-10-27 09:51:15 +01:00
//$ref_client = (!empty($objectsrc->ref_client)?$object->ref_client:'');
2019-07-22 06:56:55 +02:00
2019-10-27 09:51:15 +01:00
$soc = $objectsrc -> thirdparty ;
2019-11-08 10:53:31 +01:00
$cond_reglement_id = ( ! empty ( $objectsrc -> cond_reglement_id ) ? $objectsrc -> cond_reglement_id : ( ! empty ( $soc -> cond_reglement_supplier_id ) ? $soc -> cond_reglement_supplier_id : 0 )); // TODO maybe add default value option
$mode_reglement_id = ( ! empty ( $objectsrc -> mode_reglement_id ) ? $objectsrc -> mode_reglement_id : ( ! empty ( $soc -> mode_reglement_supplier_id ) ? $soc -> mode_reglement_supplier_id : 0 ));
$fk_account = ( ! empty ( $objectsrc -> fk_account ) ? $objectsrc -> fk_account : ( ! empty ( $soc -> fk_account ) ? $soc -> fk_account : 0 ));
$remise_percent = ( ! empty ( $objectsrc -> remise_percent ) ? $objectsrc -> remise_percent : ( ! empty ( $soc -> remise_supplier_percent ) ? $soc -> remise_supplier_percent : 0 ));
$remise_absolue = ( ! empty ( $objectsrc -> remise_absolue ) ? $objectsrc -> remise_absolue : ( ! empty ( $soc -> remise_absolue ) ? $soc -> remise_absolue : 0 ));
$dateinvoice = empty ( $conf -> global -> MAIN_AUTOFILL_DATE ) ? - 1 : '' ;
2020-10-31 14:32:18 +01:00
$transport_mode_id = ( ! empty ( $objectsrc -> transport_mode_id ) ? $objectsrc -> transport_mode_id : ( ! empty ( $soc -> transport_mode_id ) ? $soc -> transport_mode_id : 0 ));
2012-10-28 13:57:21 +01:00
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> multicurrency -> enabled )) {
if ( ! empty ( $objectsrc -> multicurrency_code )) {
$currency_code = $objectsrc -> multicurrency_code ;
}
if ( ! empty ( $conf -> global -> MULTICURRENCY_USE_ORIGIN_TX ) && ! empty ( $objectsrc -> multicurrency_tx )) {
$currency_tx = $objectsrc -> multicurrency_tx ;
}
2019-10-27 09:51:15 +01:00
}
2016-08-10 09:47:25 +02:00
2020-05-21 09:34:20 +02:00
$datetmp = dol_mktime ( 12 , 0 , 0 , GETPOST ( 'remonth' , 'int' ), GETPOST ( 'reday' , 'int' ), GETPOST ( 'reyear' , 'int' ));
2019-11-08 10:53:31 +01:00
$dateinvoice = ( $datetmp == '' ? ( empty ( $conf -> global -> MAIN_AUTOFILL_DATE ) ? - 1 : '' ) : $datetmp );
2022-02-22 23:44:56 +01:00
$datetmp = dol_mktime ( 12 , 0 , 0 , GETPOST ( 'echmonth' , 'int' ), GETPOST ( 'echday' , 'int' ), GETPOST ( 'echyear' , 'int' ));
2019-11-08 10:53:31 +01:00
$datedue = ( $datetmp == '' ? - 1 : $datetmp );
2017-10-18 17:21:30 +02:00
2019-10-27 09:51:15 +01:00
// Replicate extrafields
2020-03-27 16:02:58 +01:00
$objectsrc -> fetch_optionals ();
2019-10-27 09:51:15 +01:00
$object -> array_options = $objectsrc -> array_options ;
2020-05-21 15:05:19 +02:00
} else {
2022-05-21 18:46:41 +02:00
$cond_reglement_id = ! empty ( $societe -> cond_reglement_supplier_id ) ? $societe -> cond_reglement_supplier_id : 0 ;
$mode_reglement_id = ! empty ( $societe -> mode_reglement_supplier_id ) ? $societe -> mode_reglement_supplier_id : 0 ;
$transport_mode_id = ! empty ( $societe -> transport_mode_supplier_id ) ? $societe -> transport_mode_supplier_id : 0 ;
$fk_account = ! empty ( $societe -> fk_account ) ? $societe -> fk_account : 0 ;
2020-05-21 09:34:20 +02:00
$datetmp = dol_mktime ( 12 , 0 , 0 , GETPOST ( 'remonth' , 'int' ), GETPOST ( 'reday' , 'int' ), GETPOST ( 'reyear' , 'int' ));
2019-11-08 10:53:31 +01:00
$dateinvoice = ( $datetmp == '' ? ( empty ( $conf -> global -> MAIN_AUTOFILL_DATE ) ? - 1 : '' ) : $datetmp );
2022-02-22 23:44:56 +01:00
$datetmp = dol_mktime ( 12 , 0 , 0 , GETPOST ( 'echmonth' , 'int' ), GETPOST ( 'echday' , 'int' ), GETPOST ( 'echyear' , 'int' ));
2019-11-08 10:53:31 +01:00
$datedue = ( $datetmp == '' ? - 1 : $datetmp );
2016-08-10 09:47:25 +02:00
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> multicurrency -> enabled ) && ! empty ( $soc -> multicurrency_code )) {
$currency_code = $soc -> multicurrency_code ;
}
2017-10-16 08:47:05 +02:00
}
2020-05-22 08:00:26 +02:00
// when payment condition is empty (means not override by payment condition form a other object, like third-party), try to use default value
2020-05-23 21:07:47 +02:00
if ( empty ( $cond_reglement_id )) {
2020-05-22 08:00:26 +02:00
$cond_reglement_id = GETPOST ( " cond_reglement_id " );
}
// when payment mode is empty (means not override by payment condition form a other object, like third-party), try to use default value
2020-05-23 21:07:47 +02:00
if ( empty ( $mode_reglement_id )) {
2020-05-22 08:00:26 +02:00
$mode_reglement_id = GETPOST ( " mode_reglement_id " );
}
2020-05-22 08:08:12 +02:00
2020-10-31 14:32:18 +01:00
$note_public = $object -> getDefaultCreateValueFor ( 'note_public' , (( ! empty ( $origin ) && ! empty ( $originid ) && is_object ( $objectsrc ) && ! empty ( $conf -> global -> FACTUREFOURN_REUSE_NOTES_ON_CREATE_FROM )) ? $objectsrc -> note_public : null ));
$note_private = $object -> getDefaultCreateValueFor ( 'note_private' , (( ! empty ( $origin ) && ! empty ( $originid ) && is_object ( $objectsrc ) && ! empty ( $conf -> global -> FACTUREFOURN_REUSE_NOTES_ON_CREATE_FROM )) ? $objectsrc -> note_private : null ));
2020-07-30 21:10:06 +02:00
2017-10-16 08:47:05 +02:00
print '<form name="add" action="' . $_SERVER [ " PHP_SELF " ] . '" method="post">' ;
2019-12-18 23:12:31 +01:00
print '<input type="hidden" name="token" value="' . newToken () . '">' ;
2017-10-16 08:47:05 +02:00
print '<input type="hidden" name="action" value="add">' ;
2022-05-21 18:46:41 +02:00
if ( ! empty ( $societe -> id ) && $societe -> id > 0 ) {
2021-02-25 23:21:30 +01:00
print '<input type="hidden" name="socid" value="' . $societe -> id . '">' . " \n " ;
}
2017-10-16 08:47:05 +02:00
print '<input type="hidden" name="origin" value="' . $origin . '">' ;
print '<input type="hidden" name="originid" value="' . $originid . '">' ;
2021-02-25 23:21:30 +01:00
if ( ! empty ( $currency_tx )) {
print '<input type="hidden" name="originmulticurrency_tx" value="' . $currency_tx . '">' ;
}
2016-08-10 09:47:25 +02:00
2020-10-22 22:50:03 +02:00
print dol_get_fiche_head ();
2016-08-10 09:47:25 +02:00
2019-11-05 21:24:41 +01:00
print '<table class="border centpercent">' ;
2012-10-28 13:57:21 +01:00
2017-10-16 08:47:05 +02:00
// Ref
print '<tr><td class="titlefieldcreate">' . $langs -> trans ( 'Ref' ) . '</td><td>' . $langs -> trans ( 'Draft' ) . '</td></tr>' ;
2012-10-28 13:57:21 +01:00
2022-02-07 14:34:27 +01:00
$exampletemplateinvoice = new FactureFournisseurRec ( $db );
$invoice_predefined = new FactureFournisseurRec ( $db );
2022-04-05 12:16:06 +02:00
if ( empty ( $origin ) && empty ( $originid ) && $fac_recid > 0 ) {
$invoice_predefined -> fetch ( $fac_recid );
2022-02-07 14:34:27 +01:00
}
2017-10-16 08:47:05 +02:00
// Third party
print '<tr><td class="fieldrequired">' . $langs -> trans ( 'Supplier' ) . '</td>' ;
print '<td>' ;
2012-10-28 13:57:21 +01:00
2022-05-21 18:46:41 +02:00
if ( ! empty ( $societe -> id ) && $societe -> id > 0 && ( $fac_recid <= 0 || ! empty ( $invoice_predefined -> frequency ))) {
2018-02-14 18:02:40 +01:00
$absolute_discount = $societe -> getAvailableDiscounts ( '' , '' , 0 , 1 );
2021-08-27 15:32:53 +02:00
print $societe -> getNomUrl ( 1 , 'supplier' );
2017-10-16 08:47:05 +02:00
print '<input type="hidden" name="socid" value="' . $societe -> id . '">' ;
2020-05-21 15:05:19 +02:00
} else {
2022-09-25 17:01:44 +02:00
print img_picto ( '' , 'company' ) . $form -> select_company ( empty ( $societe -> id ) ? 0 : $societe -> id , 'socid' , '(s.fournisseur = 1 and s.status = 1)' , 'SelectThirdParty' , 0 , 0 , null , 0 , 'minwidth300 widthcentpercentminusxx maxwidth500' );
2017-10-16 08:47:05 +02:00
// reload page to retrieve supplier informations
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> global -> RELOAD_PAGE_ON_SUPPLIER_CHANGE )) {
2017-10-16 08:47:05 +02:00
print ' < script type = " text/javascript " >
2017-01-31 10:15:04 +01:00
$ ( document ) . ready ( function () {
$ ( " #socid " ) . change ( function () {
var socid = $ ( this ) . val ();
2022-02-07 14:34:27 +01:00
var fac_rec = $ ( \ ' #fac_rec\').val();
window . location . href = " '. $_SERVER["PHP_SELF"] .'?action=create&socid= " + socid + " &fac_rec= " + fac_rec ;
2017-01-31 10:15:04 +01:00
});
});
</ script > ' ;
2017-10-16 08:47:05 +02:00
}
2022-04-05 12:16:06 +02:00
if ( $fac_recid <= 0 ) {
2022-02-07 14:34:27 +01:00
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>' ;
}
2017-10-16 08:47:05 +02:00
}
print '</td></tr>' ;
2012-10-28 13:57:21 +01:00
2022-02-07 14:34:27 +01:00
// Overwrite some values if creation of invoice is from a predefined invoice
2022-04-05 12:16:06 +02:00
if ( empty ( $origin ) && empty ( $originid ) && $fac_recid > 0 ) {
$invoice_predefined -> fetch ( $fac_recid );
2022-02-07 14:34:27 +01:00
$dateinvoice = $invoice_predefined -> date_when ; // To use next gen date by default later
if ( empty ( $projectid )) {
$projectid = $invoice_predefined -> fk_project ;
}
$cond_reglement_id = $invoice_predefined -> cond_reglement_id ;
$mode_reglement_id = $invoice_predefined -> mode_reglement_id ;
$fk_account = $invoice_predefined -> fk_account ;
$note_public = $invoice_predefined -> note_public ;
$note_private = $invoice_predefined -> note_private ;
if ( ! empty ( $invoice_predefined -> multicurrency_code )) {
$currency_code = $invoice_predefined -> multicurrency_code ;
}
if ( ! empty ( $invoice_predefined -> multicurrency_tx )) {
$currency_tx = $invoice_predefined -> multicurrency_tx ;
}
$sql = 'SELECT r.rowid, r.titre as title, r.total_ttc' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'facture_fourn_rec as r' ;
$sql .= ' WHERE r.fk_soc = ' . ( int ) $invoice_predefined -> socid ;
$resql = $db -> query ( $sql );
if ( $resql ) {
$num = $db -> num_rows ( $resql );
$i = 0 ;
if ( $num > 0 ) {
print '<tr><td>' . $langs -> trans ( 'CreateFromRepeatableInvoice' ) . '</td><td>' ;
2022-04-05 12:16:06 +02:00
//print '<input type="hidden" name="fac_rec" id="fac_rec" value="'.$fac_recid.'">';
2022-02-07 14:34:27 +01:00
print '<select class="flat" id="fac_rec" name="fac_rec">' ; // We may want to change the template to use
print '<option value="0" selected></option>' ;
while ( $i < $num ) {
$objp = $db -> fetch_object ( $resql );
print '<option value="' . $objp -> rowid . '"' ;
2022-04-05 12:16:06 +02:00
if ( $fac_recid == $objp -> rowid ) {
2022-02-07 14:34:27 +01:00
print ' selected' ;
2022-04-05 12:16:06 +02:00
$exampletemplateinvoice -> fetch ( $fac_recid );
2022-02-07 14:34:27 +01:00
}
print '>' . $objp -> title . ' (' . price ( $objp -> total_ttc ) . ' ' . $langs -> trans ( " TTC " ) . ')</option>' ;
$i ++ ;
}
print '</select>' ;
// Option to reload page to retrieve customer informations. Note, this clear other input
if ( empty ( $conf -> global -> RELOAD_PAGE_ON_TEMPLATE_CHANGE_DISABLED )) {
print ' < script type = " text/javascript " >
$ ( document ) . ready ( function () {
$ ( " #fac_rec " ) . change ( function () {
console . log ( " We have changed the template invoice - Reload page " );
var fac_rec = $ ( this ) . val ();
var socid = $ ( \ ' #socid\').val();
// For template invoice change, we must reuse data of template, not input already done, so we call a GET with action=create, not a POST submit.
window . location . href = " '. $_SERVER["PHP_SELF"] .'?action=create&socid= " + socid + " &fac_rec= " + fac_rec ;
});
});
</ script > ' ;
}
print '</td></tr>' ;
}
$db -> free ( $resql );
} else {
dol_print_error ( $db );
}
}
2017-10-16 08:47:05 +02:00
// Ref supplier
2022-05-21 18:46:41 +02:00
print '<tr><td class="fieldrequired">' . $langs -> trans ( 'RefSupplier' ) . '</td><td><input name="ref_supplier" value="' . ( GETPOSTISSET ( 'ref_supplier' ) ? GETPOST ( 'ref_supplier' ) : ( ! empty ( $objectsrc -> ref_supplier ) ? $objectsrc -> ref_supplier : '' )) . '" type="text"' ;
if ( ! empty ( $societe -> id ) && $societe -> id > 0 ) {
2021-02-25 23:21:30 +01:00
print ' autofocus' ;
}
2020-10-07 15:01:28 +02:00
print '></td>' ;
2017-10-16 08:47:05 +02:00
print '</tr>' ;
2012-10-28 13:57:21 +01:00
2016-11-25 17:47:47 +01:00
print '<tr><td class="tdtop fieldrequired">' . $langs -> trans ( 'Type' ) . '</td><td>' ;
2016-10-02 21:39:24 +02:00
2019-11-08 10:53:31 +01:00
print '<div class="tagtable">' . " \n " ;
2016-10-02 21:39:24 +02:00
// Standard invoice
print '<div class="tagtr listofinvoicetype"><div class="tagtd listofinvoicetype">' ;
2022-05-21 19:15:15 +02:00
$tmp = '<input type="radio" id="radio_standard" name="type" value="0"' . ( GETPOST ( 'type' , 'int' ) ? '' : 'checked' ) . '> ' ;
2016-10-02 21:39:24 +02:00
$desc = $form -> textwithpicto ( $tmp . $langs -> trans ( " InvoiceStandardAsk " ), $langs -> transnoentities ( " InvoiceStandardDesc " ), 1 , 'help' , '' , 0 , 3 );
print $desc ;
print '</div></div>' ;
2021-04-27 12:11:28 +02:00
if ( empty ( $origin ) || ( $origin == 'order_supplier' && ! empty ( $originid ))) {
2021-04-06 09:15:34 +02:00
// Deposit - Down payment
2021-04-06 09:20:07 +02:00
if ( empty ( $conf -> global -> INVOICE_DISABLE_DEPOSIT )) {
2016-10-02 21:39:24 +02:00
print '<div class="tagtr listofinvoicetype"><div class="tagtd listofinvoicetype">' ;
$tmp = '<input type="radio" id="radio_deposit" name="type" value="3"' . ( GETPOST ( 'type' ) == 3 ? ' checked' : '' ) . '> ' ;
2021-11-29 15:09:18 +01:00
print ' < script type = " text/javascript " >
2016-10-02 21:39:24 +02:00
jQuery ( document ) . ready ( function () {
2021-04-06 09:15:34 +02:00
jQuery ( " #typestandardinvoice, #valuestandardinvoice " ) . click ( function () {
jQuery ( " #radio_standard " ) . prop ( " checked " , true );
});
jQuery ( " #typedeposit, #valuedeposit " ) . click ( function () {
jQuery ( " #radio_deposit " ) . prop ( " checked " , true );
});
jQuery ( " #typedeposit " ) . change ( function () {
console . log ( " We change type of down payment " );
2016-10-02 21:39:24 +02:00
jQuery ( " #radio_deposit " ) . prop ( " checked " , true );
2021-04-06 09:15:34 +02:00
setRadioForTypeOfInvoice ();
2016-10-02 21:39:24 +02:00
});
2021-04-06 09:15:34 +02:00
jQuery ( " #radio_standard, #radio_deposit, #radio_replacement, #radio_template " ) . change ( function () {
setRadioForTypeOfInvoice ();
});
function setRadioForTypeOfInvoice () {
console . log ( " Change radio " );
if ( jQuery ( " #radio_deposit " ) . prop ( " checked " ) && ( jQuery ( " #typedeposit " ) . val () == \ ' amount\ ' || jQuery ( " #typedeposit " ) . val () == \ ' variable\ ' )) {
jQuery ( " .checkforselect " ) . prop ( " disabled " , true );
jQuery ( " .checkforselect " ) . prop ( " checked " , false );
} else {
jQuery ( " .checkforselect " ) . prop ( " disabled " , false );
jQuery ( " .checkforselect " ) . prop ( " checked " , true );
}
};
});
</ script > ' ;
2016-10-02 21:39:24 +02:00
2021-04-06 09:15:34 +02:00
$tmp = $tmp . '<label for="radio_deposit" >' . $langs -> trans ( " InvoiceDeposit " ) . '</label>' ;
$desc = $form -> textwithpicto ( $tmp , $langs -> transnoentities ( " InvoiceDepositDesc " ), 1 , 'help' , '' , 0 , 3 );
print '<table class="nobordernopadding"><tr>' ;
print '<td>' ;
2016-10-02 21:39:24 +02:00
print $desc ;
print '</td>' ;
2021-04-27 12:11:28 +02:00
if ( $origin == 'order_supplier' ) {
2021-04-06 09:15:34 +02:00
print '<td class="nowrap" style="padding-left: 15px">' ;
$arraylist = array (
'amount' => $langs -> transnoentitiesnoconv ( 'FixAmount' , $langs -> transnoentitiesnoconv ( 'Deposit' )),
'variable' => $langs -> transnoentitiesnoconv ( 'VarAmountOneLine' , $langs -> transnoentitiesnoconv ( 'Deposit' )),
'variablealllines' => $langs -> transnoentitiesnoconv ( 'VarAmountAllLines' )
);
print $form -> selectarray ( 'typedeposit' , $arraylist , GETPOST ( 'typedeposit' , 'aZ09' ), 0 , 0 , 0 , '' , 1 );
print '</td>' ;
2016-10-02 21:39:24 +02:00
print '<td class="nowrap" style="padding-left: 5px">' ;
2021-04-06 09:15:34 +02:00
print '<span class="opacitymedium paddingleft">' . $langs -> trans ( " AmountOrPercent " ) . '</span><input type="text" id="valuedeposit" name="valuedeposit" class="width75 right" value="' . GETPOST ( 'valuedeposit' , 'int' ) . '"/>' ;
2016-10-02 21:39:24 +02:00
print '</td>' ;
}
2021-04-06 09:15:34 +02:00
print '</tr></table>' ;
2016-10-02 21:39:24 +02:00
print '</div></div>' ;
}
}
2016-11-06 15:01:40 +01:00
2017-11-23 15:22:32 +01:00
/* Not yet supported for supplier
2016-10-02 21:39:24 +02:00
if ( $societe -> id > 0 )
{
// Replacement
2016-11-06 15:01:40 +01:00
if ( empty ( $conf -> global -> INVOICE_DISABLE_REPLACEMENT ))
2016-10-02 21:39:24 +02:00
{
2019-09-17 09:43:15 +02:00
// Type invoice
$facids = $facturestatic -> list_replacable_supplier_invoices ( $societe -> id );
if ( $facids < 0 ) {
2021-06-08 12:05:45 +02:00
dol_print_error ( $db , $facturestatic -> error , $facturestatic -> errors );
2019-09-17 09:43:15 +02:00
exit ();
}
$options = " " ;
foreach ( $facids as $facparam )
{
$options .= '<option value="' . $facparam [ 'id' ] . '"' ;
2022-02-22 23:44:56 +01:00
if ( $facparam [ 'id' ] == GETPOST ( 'fac_replacement' ) {
2019-09-17 09:43:15 +02:00
$options .= ' selected' ;
2022-02-22 23:44:56 +01:00
}
2019-09-17 09:43:15 +02:00
$options .= '>' . $facparam [ 'ref' ];
$options .= ' (' . $facturestatic -> LibStatut ( 0 , $facparam [ 'status' ]) . ')' ;
$options .= '</option>' ;
}
2016-10-02 21:39:24 +02:00
print '<!-- replacement line -->' ;
print '<div class="tagtr listofinvoicetype"><div class="tagtd listofinvoicetype">' ;
$tmp = '<input type="radio" name="type" id="radio_replacement" value="1"' . ( GETPOST ( 'type' ) == 1 ? ' checked' : '' );
if ( ! $options ) $tmp .= ' disabled' ;
$tmp .= '> ' ;
2021-11-29 15:09:18 +01:00
print ' < script type = " text/javascript " >
2016-10-02 21:39:24 +02:00
jQuery ( document ) . ready ( function () {
jQuery ( " #fac_replacement " ) . change ( function () {
jQuery ( " #radio_replacement " ) . prop ( " checked " , true );
});
});
</ script > ' ;
$text = $tmp . $langs -> trans ( " InvoiceReplacementAsk " ) . ' ' ;
$text .= '<select class="flat" name="fac_replacement" id="fac_replacement"' ;
if ( ! $options )
$text .= ' disabled' ;
$text .= '>' ;
if ( $options ) {
$text .= '<option value="-1"> </option>' ;
$text .= $options ;
} else {
$text .= '<option value="-1">' . $langs -> trans ( " NoReplacableInvoice " ) . '</option>' ;
}
$text .= '</select>' ;
$desc = $form -> textwithpicto ( $text , $langs -> transnoentities ( " InvoiceReplacementDesc " ), 1 , 'help' , '' , 0 , 3 );
print $desc ;
print '</div></div>' ;
}
}
else
{
print '<div class="tagtr listofinvoicetype"><div class="tagtd listofinvoicetype">' ;
$tmp = '<input type="radio" name="type" id="radio_replacement" value="0" disabled> ' ;
$text = $tmp . $langs -> trans ( " InvoiceReplacement " ) . ' ' ;
$text .= '(' . $langs -> trans ( " YouMustCreateInvoiceFromSupplierThird " ) . ') ' ;
$desc = $form -> textwithpicto ( $text , $langs -> transnoentities ( " InvoiceReplacementDesc " ), 1 , 'help' , '' , 0 , 3 );
print $desc ;
print '</div></div>' ;
}
2016-10-04 11:14:00 +02:00
*/
2016-10-02 21:39:24 +02:00
2021-02-25 23:21:30 +01:00
if ( empty ( $origin )) {
2022-05-21 18:47:01 +02:00
if ( ! empty ( $societe -> id ) && $societe -> id > 0 ) {
2018-03-05 10:58:19 +01:00
// Credit note
2021-02-25 23:21:30 +01:00
if ( empty ( $conf -> global -> INVOICE_DISABLE_CREDIT_NOTE )) {
2019-09-17 09:43:15 +02:00
// Show link for credit note
2019-11-08 10:53:31 +01:00
$facids = $facturestatic -> list_qualified_avoir_supplier_invoices ( $societe -> id );
2021-02-25 23:21:30 +01:00
if ( $facids < 0 ) {
2021-06-08 12:05:45 +02:00
dol_print_error ( $db , $facturestatic -> error , $facturestatic -> errors );
2019-09-17 09:43:15 +02:00
exit ;
}
$optionsav = " " ;
$newinvoice_static = new FactureFournisseur ( $db );
2021-02-25 23:21:30 +01:00
foreach ( $facids as $key => $valarray ) {
2019-09-17 09:43:15 +02:00
$newinvoice_static -> id = $key ;
$newinvoice_static -> ref = $valarray [ 'ref' ];
$newinvoice_static -> statut = $valarray [ 'status' ];
$newinvoice_static -> type = $valarray [ 'type' ];
$newinvoice_static -> paye = $valarray [ 'paye' ];
2019-11-08 10:53:31 +01:00
$optionsav .= '<option value="' . $key . '"' ;
2021-02-25 23:21:30 +01:00
if ( $key == GETPOST ( 'fac_avoir' , 'int' )) {
2019-09-17 09:43:15 +02:00
$optionsav .= ' selected' ;
2021-02-25 23:21:30 +01:00
}
2019-09-17 09:43:15 +02:00
$optionsav .= '>' ;
$optionsav .= $newinvoice_static -> ref ;
2019-11-08 10:53:31 +01:00
$optionsav .= ' (' . $newinvoice_static -> getLibStatut ( 1 , $valarray [ 'paymentornot' ]) . ')' ;
2019-09-17 09:43:15 +02:00
$optionsav .= '</option>' ;
}
2017-10-16 08:47:05 +02:00
print '<div class="tagtr listofinvoicetype"><div class="tagtd listofinvoicetype">' ;
2019-11-08 10:53:31 +01:00
$tmp = '<input type="radio" id="radio_creditnote" name="type" value="2"' . ( GETPOST ( 'type' ) == 2 ? ' checked' : '' );
2021-02-25 23:21:30 +01:00
if ( ! $optionsav ) {
$tmp .= ' disabled' ;
}
2019-11-08 10:53:31 +01:00
$tmp .= '> ' ;
2018-03-05 10:58:19 +01:00
// Show credit note options only if we checked credit note
2021-11-29 15:09:18 +01:00
print ' < script type = " text/javascript " >
2018-03-05 10:58:19 +01:00
jQuery ( document ) . ready ( function () {
if ( ! jQuery ( " #radio_creditnote " ) . is ( " :checked " ))
{
jQuery ( " #credit_note_options " ) . hide ();
}
jQuery ( " #radio_creditnote " ) . click ( function () {
jQuery ( " #credit_note_options " ) . show ();
});
jQuery ( " #radio_standard, #radio_replacement, #radio_deposit " ) . click ( function () {
jQuery ( " #credit_note_options " ) . hide ();
});
});
</ script > ' ;
2019-11-08 10:53:31 +01:00
$text = $tmp . $langs -> transnoentities ( " InvoiceAvoirAsk " ) . ' ' ;
2018-03-05 10:58:19 +01:00
// $text.='<input type="text" value="">';
$text .= '<select class="flat valignmiddle" name="fac_avoir" id="fac_avoir"' ;
2021-02-25 23:21:30 +01:00
if ( ! $optionsav ) {
2018-03-05 10:58:19 +01:00
$text .= ' disabled' ;
2021-02-25 23:21:30 +01:00
}
2018-03-05 10:58:19 +01:00
$text .= '>' ;
if ( $optionsav ) {
$text .= '<option value="-1"></option>' ;
$text .= $optionsav ;
} else {
2019-11-08 10:53:31 +01:00
$text .= '<option value="-1">' . $langs -> trans ( " NoInvoiceToCorrect " ) . '</option>' ;
2018-03-05 10:58:19 +01:00
}
$text .= '</select>' ;
2017-10-16 08:47:05 +02:00
$desc = $form -> textwithpicto ( $text , $langs -> transnoentities ( " InvoiceAvoirDesc " ), 1 , 'help' , '' , 0 , 3 );
print $desc ;
2018-03-05 10:58:19 +01:00
print '<div id="credit_note_options" class="clearboth">' ;
2019-11-08 10:53:31 +01:00
print ' <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' : '' ) . ' /> ' ;
2018-05-27 15:04:12 +02:00
print '<label for="invoiceAvoirWithLines">' . $langs -> trans ( 'invoiceAvoirWithLines' ) . " </label> " ;
2019-11-08 10:53:31 +01:00
print '<br> <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' : '' ) . ' /> ' ;
2018-05-27 15:04:12 +02:00
print '<label for="invoiceAvoirWithPaymentRestAmount">' . $langs -> trans ( 'invoiceAvoirWithPaymentRestAmount' ) . " </label> " ;
2018-03-05 10:58:19 +01:00
print '</div>' ;
print '</div></div>' ;
2017-10-16 08:47:05 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2018-03-05 10:58:19 +01:00
print '<div class="tagtr listofinvoicetype"><div class="tagtd listofinvoicetype">' ;
2019-11-08 10:53:31 +01:00
$tmp = '<input type="radio" name="type" id="radio_creditnote" value="0" disabled> ' ;
$text = $tmp . $langs -> trans ( " InvoiceAvoir " ) . ' ' ;
2021-01-15 14:02:18 +01:00
$text .= '<span class="opacitymedium">(' . $langs -> trans ( " YouMustCreateInvoiceFromSupplierThird " ) . ')</span> ' ;
2018-03-05 10:58:19 +01:00
$desc = $form -> textwithpicto ( $text , $langs -> transnoentities ( " InvoiceAvoirDesc " ), 1 , 'help' , '' , 0 , 3 );
print $desc ;
2019-11-08 10:53:31 +01:00
print '</div></div>' . " \n " ;
2018-03-05 10:58:19 +01:00
}
2016-10-02 21:39:24 +02:00
}
print '</div>' ;
print '</td></tr>' ;
2022-05-21 18:46:41 +02:00
if ( ! empty ( $societe -> id ) && $societe -> id > 0 ) {
2016-10-02 21:39:24 +02:00
// Discounts for third party
2019-11-08 10:53:31 +01:00
print '<tr><td>' . $langs -> trans ( 'Discounts' ) . '</td><td>' ;
2018-02-27 16:30:39 +01:00
2018-03-02 16:48:25 +01:00
$thirdparty = $societe ;
$discount_type = 1 ;
2019-11-08 10:53:31 +01:00
$backtopage = urlencode ( $_SERVER [ " PHP_SELF " ] . '?socid=' . $societe -> id . '&action=' . $action . '&origin=' . GETPOST ( 'origin' ) . '&originid=' . GETPOST ( 'originid' ));
2018-03-02 16:48:25 +01:00
include DOL_DOCUMENT_ROOT . '/core/tpl/object_discounts.tpl.php' ;
2018-02-27 16:30:39 +01:00
2016-10-02 21:39:24 +02:00
print '</td></tr>' ;
}
2012-10-28 13:57:21 +01:00
2017-10-16 08:47:05 +02:00
// Label
print '<tr><td>' . $langs -> trans ( 'Label' ) . '</td><td><input class="minwidth200" name="label" value="' . dol_escape_htmltag ( GETPOST ( 'label' )) . '" type="text"></td></tr>' ;
2012-10-28 13:57:21 +01:00
2017-10-16 08:47:05 +02:00
// Date invoice
print '<tr><td class="fieldrequired">' . $langs -> trans ( 'DateInvoice' ) . '</td><td>' ;
2018-09-09 09:56:33 +02:00
print $form -> selectDate ( $dateinvoice , '' , '' , '' , '' , " add " , 1 , 1 );
2017-10-16 08:47:05 +02:00
print '</td></tr>' ;
2012-10-28 13:57:21 +01:00
2017-10-16 08:47:05 +02:00
// Due date
print '<tr><td>' . $langs -> trans ( 'DateMaxPayment' ) . '</td><td>' ;
2018-09-09 09:56:33 +02:00
print $form -> selectDate ( $datedue , 'ech' , '' , '' , '' , " add " , 1 , 1 );
2017-10-16 08:47:05 +02:00
print '</td></tr>' ;
2013-09-06 09:26:23 +02:00
2013-07-30 16:21:39 +02:00
// Payment term
2017-06-07 16:44:04 +02:00
print '<tr><td class="nowrap">' . $langs -> trans ( 'PaymentConditionsShort' ) . '</td><td>' ;
2019-11-08 10:53:31 +01:00
$form -> select_conditions_paiements ( GETPOSTISSET ( 'cond_reglement_id' ) ? GETPOST ( 'cond_reglement_id' , 'int' ) : $cond_reglement_id , 'cond_reglement_id' );
2013-07-30 16:21:39 +02:00
print '</td></tr>' ;
// Payment mode
2017-06-07 16:44:04 +02:00
print '<tr><td>' . $langs -> trans ( 'PaymentMode' ) . '</td><td>' ;
2021-05-19 15:11:47 +02:00
print img_picto ( '' , 'bank' , 'class="pictofixedwidth"' );
$form -> select_types_paiements ( GETPOSTISSET ( 'mode_reglement_id' ) ? GETPOST ( 'mode_reglement_id' , 'int' ) : $mode_reglement_id , 'mode_reglement_id' , 'DBIT' , 0 , 1 , 0 , 0 , 1 , 'maxwidth200 widthcentpercentminusx' );
2013-07-30 16:21:39 +02:00
print '</td></tr>' ;
2013-05-22 17:29:25 +02:00
2017-10-16 08:47:05 +02:00
// Bank Account
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> banque -> enabled )) {
2020-08-28 13:08:04 +02:00
print '<tr><td>' . $langs -> trans ( 'BankAccount' ) . '</td><td>' ;
2021-05-19 15:11:47 +02:00
print img_picto ( '' , 'bank_account' , 'class="pictofixedwidth"' ) . $form -> select_comptes (( GETPOSTISSET ( 'fk_account' ) ? GETPOST ( 'fk_account' , 'alpha' ) : $fk_account ), 'fk_account' , 0 , '' , 1 , '' , 0 , 'maxwidth200 widthcentpercentminusx' , 1 );
2016-04-01 16:29:47 +02:00
print '</td></tr>' ;
}
2014-09-07 12:22:04 +02:00
// Project
2022-06-14 17:53:17 +02:00
if ( ! empty ( $conf -> project -> enabled )) {
2014-09-07 12:22:04 +02:00
$formproject = new FormProjets ( $db );
$langs -> load ( 'projects' );
2019-11-08 10:53:31 +01:00
print '<tr><td>' . $langs -> trans ( 'Project' ) . '</td><td>' ;
2021-05-19 15:11:47 +02:00
print img_picto ( '' , 'project' , 'class="pictofixedwidth"' ) . $formproject -> select_projects (( empty ( $conf -> global -> PROJECT_CAN_ALWAYS_LINK_TO_ALL_SUPPLIERS ) ? $societe -> id : - 1 ), $projectid , 'projectid' , 0 , 0 , 1 , 1 , 0 , 0 , 0 , '' , 1 , 0 , 'maxwidth500 widthcentpercentminusxx' );
2022-05-21 18:46:41 +02:00
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>' ;
2014-09-07 12:22:04 +02:00
print '</td></tr>' ;
}
2015-02-23 09:16:14 +01:00
// Incoterms
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> incoterm -> enabled )) {
2015-02-23 09:16:14 +01:00
print '<tr>' ;
2022-05-21 18:46:41 +02:00
print '<td><label for="incoterm_id">' . $form -> textwithpicto ( $langs -> trans ( " IncotermLabel " ), ! empty ( $objectsrc -> label_incoterms ) ? $objectsrc -> label_incoterms : '' , 1 ) . '</label></td>' ;
2017-10-16 08:47:05 +02:00
print '<td colspan="3" class="maxwidthonsmartphone">' ;
2019-11-08 10:53:31 +01:00
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 : '' ));
2015-02-23 09:16:14 +01:00
print '</td></tr>' ;
}
2014-09-07 12:22:04 +02:00
2021-01-15 14:02:18 +01:00
// Multicurrency
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> multicurrency -> enabled )) {
2021-01-15 14:02:18 +01:00
print '<tr>' ;
print '<td>' . $form -> editfieldkey ( 'Currency' , 'multicurrency_code' , '' , $object , 0 ) . '</td>' ;
print '<td class="maxwidthonsmartphone">' ;
print $form -> selectMultiCurrency (( GETPOSTISSET ( 'multicurrency_code' ) ? GETPOST ( 'multicurrency_code' , 'alpha' ) : $currency_code ), 'multicurrency_code' );
print '</td></tr>' ;
}
2022-02-07 14:34:27 +01:00
// Help of substitution key
$htmltext = '' ;
2022-04-05 12:16:06 +02:00
if ( $fac_recid > 0 ) {
2022-02-07 14:34:27 +01:00
$dateexample = $newdateinvoice ? $newdateinvoice : $dateinvoice ;
if ( empty ( $dateexample )) {
$dateexample = dol_now ();
}
$substitutionarray = array (
'__TOTAL_HT__' => $langs -> trans ( " AmountHT " ) . ' (' . $langs -> trans ( " Example " ) . ': ' . price ( $exampletemplateinvoice -> total_ht ) . ')' ,
'__TOTAL_TTC__' => $langs -> trans ( " AmountTTC " ) . ' (' . $langs -> trans ( " Example " ) . ': ' . price ( $exampletemplateinvoice -> total_ttc ) . ')' ,
'__INVOICE_PREVIOUS_MONTH__' => $langs -> trans ( " PreviousMonthOfInvoice " ) . ' (' . $langs -> trans ( " Example " ) . ': ' . dol_print_date ( dol_time_plus_duree ( $dateexample , - 1 , 'm' ), '%m' ) . ')' ,
'__INVOICE_MONTH__' => $langs -> trans ( " MonthOfInvoice " ) . ' (' . $langs -> trans ( " Example " ) . ': ' . dol_print_date ( $dateexample , '%m' ) . ')' ,
'__INVOICE_NEXT_MONTH__' => $langs -> trans ( " NextMonthOfInvoice " ) . ' (' . $langs -> trans ( " Example " ) . ': ' . dol_print_date ( dol_time_plus_duree ( $dateexample , 1 , 'm' ), '%m' ) . ')' ,
'__INVOICE_PREVIOUS_MONTH_TEXT__' => $langs -> trans ( " TextPreviousMonthOfInvoice " ) . ' (' . $langs -> trans ( " Example " ) . ': ' . dol_print_date ( dol_time_plus_duree ( $dateexample , - 1 , 'm' ), '%B' ) . ')' ,
'__INVOICE_MONTH_TEXT__' => $langs -> trans ( " TextMonthOfInvoice " ) . ' (' . $langs -> trans ( " Example " ) . ': ' . dol_print_date ( $dateexample , '%B' ) . ')' ,
'__INVOICE_NEXT_MONTH_TEXT__' => $langs -> trans ( " TextNextMonthOfInvoice " ) . ' (' . $langs -> trans ( " Example " ) . ': ' . dol_print_date ( dol_time_plus_duree ( $dateexample , 1 , 'm' ), '%B' ) . ')' ,
'__INVOICE_PREVIOUS_YEAR__' => $langs -> trans ( " PreviousYearOfInvoice " ) . ' (' . $langs -> trans ( " Example " ) . ': ' . dol_print_date ( dol_time_plus_duree ( $dateexample , - 1 , 'y' ), '%Y' ) . ')' ,
'__INVOICE_YEAR__' => $langs -> trans ( " YearOfInvoice " ) . ' (' . $langs -> trans ( " Example " ) . ': ' . dol_print_date ( $dateexample , '%Y' ) . ')' ,
'__INVOICE_NEXT_YEAR__' => $langs -> trans ( " NextYearOfInvoice " ) . ' (' . $langs -> trans ( " Example " ) . ': ' . dol_print_date ( dol_time_plus_duree ( $dateexample , 1 , 'y' ), '%Y' ) . ')'
);
$htmltext = '<i>' . $langs -> trans ( " FollowingConstantsWillBeSubstituted " ) . ':<br>' ;
foreach ( $substitutionarray as $key => $val ) {
$htmltext .= $key . ' = ' . $langs -> trans ( $val ) . '<br>' ;
}
$htmltext .= '</i>' ;
}
2020-10-31 18:51:30 +01:00
// Intracomm report
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> intracommreport -> enabled )) {
2020-10-31 18:51:30 +01:00
$langs -> loadLangs ( array ( " intracommreport " ));
print '<tr><td>' . $langs -> trans ( 'IntracommReportTransportMode' ) . '</td><td>' ;
2022-02-22 23:44:56 +01:00
$form -> selectTransportMode ( GETPOSTISSET ( 'transport_mode_id' ) ? GETPOST ( 'transport_mode_id' ) : $transport_mode_id , 'transport_mode_id' );
2020-10-31 18:51:30 +01:00
print '</td></tr>' ;
}
2019-07-22 06:56:55 +02:00
2021-05-01 04:07:24 +02:00
if ( empty ( $reshook )) {
2021-10-08 15:58:08 +02:00
print $object -> showOptionals ( $extrafields , 'create' );
2021-05-01 04:07:24 +02:00
}
2014-08-06 16:46:04 +02:00
// Public note
print '<tr><td>' . $langs -> trans ( 'NotePublic' ) . '</td>' ;
2017-10-16 08:47:05 +02:00
print '<td>' ;
2020-11-30 14:54:44 +01:00
$doleditor = new DolEditor ( 'note_public' , ( GETPOSTISSET ( 'note_public' ) ? GETPOST ( 'note_public' , 'restricthtml' ) : $note_public ), '' , 80 , 'dolibarr_notes' , 'In' , 0 , false , empty ( $conf -> global -> FCKEDITOR_ENABLE_NOTE_PUBLIC ) ? 0 : 1 , ROWS_3 , '90%' );
2017-10-16 08:47:05 +02:00
print $doleditor -> Create ( 1 );
print '</td>' ;
2020-10-07 15:01:28 +02:00
// print '<td><textarea name="note" wrap="soft" cols="60" rows="'.ROWS_5.'"></textarea></td>';
2017-10-16 08:47:05 +02:00
print '</tr>' ;
2013-04-12 00:00:31 +02:00
2017-10-16 08:47:05 +02:00
// Private note
print '<tr><td>' . $langs -> trans ( 'NotePrivate' ) . '</td>' ;
print '<td>' ;
2020-11-30 14:54:44 +01:00
$doleditor = new DolEditor ( 'note_private' , ( GETPOSTISSET ( 'note_private' ) ? GETPOST ( 'note_private' , 'restricthtml' ) : $note_private ), '' , 80 , 'dolibarr_notes' , 'In' , 0 , false , empty ( $conf -> global -> FCKEDITOR_ENABLE_NOTE_PRIVATE ) ? 0 : 1 , ROWS_3 , '90%' );
2017-10-16 08:47:05 +02:00
print $doleditor -> Create ( 1 );
print '</td>' ;
// print '<td><textarea name="note" wrap="soft" cols="60" rows="'.ROWS_5.'"></textarea></td>';
print '</tr>' ;
2015-01-25 21:32:40 +01:00
2012-10-28 13:57:21 +01:00
2022-05-21 18:46:41 +02:00
if ( ! empty ( $objectsrc ) && is_object ( $objectsrc )) {
2017-10-16 08:47:05 +02:00
print " \n <!-- " . $classname . " info --> " ;
print " \n " ;
print '<input type="hidden" name="amount" value="' . $objectsrc -> total_ht . '">' . " \n " ;
print '<input type="hidden" name="total" value="' . $objectsrc -> total_ttc . '">' . " \n " ;
print '<input type="hidden" name="tva" value="' . $objectsrc -> total_tva . '">' . " \n " ;
print '<input type="hidden" name="origin" value="' . $objectsrc -> element . '">' ;
print '<input type="hidden" name="originid" value="' . $objectsrc -> id . '">' ;
2019-11-08 10:53:31 +01:00
$txt = $langs -> trans ( $classname );
if ( $classname == 'CommandeFournisseur' ) {
2017-10-16 08:47:05 +02:00
$langs -> load ( 'orders' );
2019-11-08 10:53:31 +01:00
$txt = $langs -> trans ( " SupplierOrder " );
2017-10-16 08:47:05 +02:00
}
print '<tr><td>' . $txt . '</td><td>' . $objectsrc -> getNomUrl ( 1 );
// We check if Origin document (id and type is known) has already at least one invoice attached to it
2019-01-27 11:55:16 +01:00
$objectsrc -> fetchObjectLinked ( $originid , $origin , '' , 'invoice_supplier' );
2019-10-28 10:40:07 +01:00
$invoice_supplier = $objectsrc -> linkedObjects [ 'invoice_supplier' ];
// count function need a array as argument (Note: the array must implement Countable too)
2021-02-25 23:21:30 +01:00
if ( is_array ( $invoice_supplier )) {
2019-10-28 10:40:07 +01:00
$cntinvoice = count ( $invoice_supplier );
2021-02-25 23:21:30 +01:00
if ( $cntinvoice >= 1 ) {
2019-10-28 10:40:07 +01:00
setEventMessages ( 'WarningBillExist' , null , 'warnings' );
echo ' (' . $langs -> trans ( 'LatestRelatedBill' ) . end ( $invoice_supplier ) -> getNomUrl ( 1 ) . ')' ;
}
2017-10-16 08:47:05 +02:00
}
2019-10-28 10:40:07 +01:00
2019-07-22 06:56:55 +02:00
print '</td></tr>' ;
2019-03-13 14:10:36 +01:00
print '<tr><td>' . $langs -> trans ( 'AmountHT' ) . '</td><td>' . price ( $objectsrc -> total_ht ) . '</td></tr>' ;
print '<tr><td>' . $langs -> trans ( 'AmountVAT' ) . '</td><td>' . price ( $objectsrc -> total_tva ) . " </td></tr> " ;
2021-02-25 23:21:30 +01:00
if ( $mysoc -> localtax1_assuj == " 1 " || $object -> total_localtax1 != 0 ) { //Localtax1
2019-01-27 11:55:16 +01:00
print '<tr><td>' . $langs -> transcountry ( " AmountLT1 " , $mysoc -> country_code ) . '</td><td>' . price ( $objectsrc -> total_localtax1 ) . " </td></tr> " ;
2017-10-16 08:47:05 +02:00
}
2012-10-28 13:57:21 +01:00
2021-02-25 23:21:30 +01:00
if ( $mysoc -> localtax2_assuj == " 1 " || $object -> total_localtax2 != 0 ) { //Localtax2
2019-01-27 11:55:16 +01:00
print '<tr><td>' . $langs -> transcountry ( " AmountLT2 " , $mysoc -> country_code ) . '</td><td>' . price ( $objectsrc -> total_localtax2 ) . " </td></tr> " ;
2017-10-16 08:47:05 +02:00
}
2019-03-13 14:10:36 +01:00
print '<tr><td>' . $langs -> trans ( 'AmountTTC' ) . '</td><td>' . price ( $objectsrc -> total_ttc ) . " </td></tr> " ;
2016-08-10 09:47:25 +02:00
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> multicurrency -> enabled )) {
2019-11-08 10:53:31 +01:00
print '<tr><td>' . $langs -> trans ( 'MulticurrencyAmountHT' ) . '</td><td>' . price ( $objectsrc -> multicurrency_total_ht ) . '</td></tr>' ;
print '<tr><td>' . $langs -> trans ( 'MulticurrencyAmountVAT' ) . '</td><td>' . price ( $objectsrc -> multicurrency_total_tva ) . " </td></tr> " ;
print '<tr><td>' . $langs -> trans ( 'MulticurrencyAmountTTC' ) . '</td><td>' . price ( $objectsrc -> multicurrency_total_ttc ) . " </td></tr> " ;
2016-02-19 22:25:59 +01:00
}
2017-10-16 08:47:05 +02:00
}
2012-10-28 13:57:21 +01:00
2017-10-16 08:47:05 +02:00
// Other options
2019-11-08 10:53:31 +01:00
$parameters = array ();
$reshook = $hookmanager -> executeHooks ( 'formObjectOptions' , $parameters , $object , $action ); // Note that $action and $object may have been modified by hook
2017-10-16 08:47:05 +02:00
print $hookmanager -> resPrint ;
2012-10-28 13:57:21 +01:00
2021-08-20 14:41:30 +02:00
2017-10-16 08:47:05 +02:00
print " </table> \n " ;
2012-10-28 13:57:21 +01:00
2020-10-27 18:19:31 +01:00
print dol_get_fiche_end ();
2015-05-18 22:25:51 +02:00
2021-08-20 14:41:30 +02:00
print $form -> buttonsSaveCancel ( " CreateDraft " );
2012-10-28 13:57:21 +01:00
2017-10-16 08:47:05 +02:00
// Show origin lines
2022-05-21 18:46:41 +02:00
if ( ! empty ( $objectsrc ) && is_object ( $objectsrc )) {
2017-10-16 08:47:05 +02:00
print '<br>' ;
2012-10-28 13:57:21 +01:00
2019-11-08 10:53:31 +01:00
$title = $langs -> trans ( 'ProductsAndServices' );
2017-10-16 08:47:05 +02:00
print load_fiche_titre ( $title );
2012-10-28 13:57:21 +01:00
2022-04-07 11:18:56 +02:00
print '<div class="div-table-responsive-no-min">' ;
2019-11-05 21:24:41 +01:00
print '<table class="noborder centpercent">' ;
2012-10-28 13:57:21 +01:00
2021-09-30 16:37:52 +02:00
$objectsrc -> printOriginLinesList ( '' , $selectedLines );
2012-10-28 13:57:21 +01:00
2017-10-16 08:47:05 +02:00
print '</table>' ;
2022-04-07 11:18:56 +02:00
print '</div>' ;
2017-10-16 08:47:05 +02:00
}
2021-09-21 11:04:59 +02:00
print " </form> \n " ;
2020-05-21 15:05:19 +02:00
} else {
2020-12-23 23:16:27 +01:00
if ( $id > 0 || ! empty ( $ref )) {
2021-01-16 16:06:49 +01:00
//
// View or edit mode
//
2012-10-28 13:57:21 +01:00
2019-11-08 10:53:31 +01:00
$now = dol_now ();
2012-10-28 13:57:21 +01:00
2017-10-16 08:47:05 +02:00
$productstatic = new Product ( $db );
2012-10-28 13:57:21 +01:00
2022-01-09 13:50:10 +01:00
$result = $object -> fetch ( $id , $ref );
if ( $result <= 0 ) {
$langs -> load ( " errors " );
print $langs -> trans ( " ErrorRecordNotFound " );
llxFooter ();
$db -> close ();
exit ;
2021-02-25 23:21:30 +01:00
}
2012-10-28 13:57:21 +01:00
2022-01-09 13:50:10 +01:00
$result = $object -> fetch_thirdparty ();
2021-02-25 23:21:30 +01:00
if ( $result < 0 ) {
2022-01-09 13:50:10 +01:00
dol_print_error ( $db , $object -> error , $object -> errors );
exit ;
2021-02-25 23:21:30 +01:00
}
2015-01-25 21:32:40 +01:00
2022-01-09 13:50:10 +01:00
$societe = $object -> thirdparty ;
2022-05-22 17:09:08 +02:00
$totalpaid = $object -> getSommePaiement ();
2017-12-05 12:31:57 +01:00
$totalcreditnotes = $object -> getSumCreditNotesUsed ();
$totaldeposits = $object -> getSumDepositsUsed ();
2022-05-22 17:09:08 +02:00
// print "totalpaid=".$totalpaid." totalcreditnotes=".$totalcreditnotes." totaldeposts=".$totaldeposits."
2017-12-05 12:31:57 +01:00
// selleruserrevenuestamp=".$selleruserevenustamp;
// We can also use bcadd to avoid pb with floating points
// For example print 239.2 - 229.3 - 9.9; does not return 0.
2022-05-22 17:09:08 +02:00
// $resteapayer=bcadd($object->total_ttc,$totalpaid,$conf->global->MAIN_MAX_DECIMALS_TOT);
2017-12-05 12:31:57 +01:00
// $resteapayer=bcadd($resteapayer,$totalavoir,$conf->global->MAIN_MAX_DECIMALS_TOT);
2022-05-22 17:09:08 +02:00
$resteapayer = price2num ( $object -> total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits , 'MT' );
2017-12-05 12:31:57 +01:00
2021-10-15 09:05:13 +02:00
// Multicurrency
if ( ! empty ( $conf -> multicurrency -> enabled )) {
2022-05-22 17:09:08 +02:00
$multicurrency_totalpaid = $object -> getSommePaiement ( 1 );
2021-10-15 09:05:13 +02:00
$multicurrency_totalcreditnotes = $object -> getSumCreditNotesUsed ( 1 );
$multicurrency_totaldeposits = $object -> getSumDepositsUsed ( 1 );
2022-05-22 17:09:08 +02:00
$multicurrency_resteapayer = price2num ( $object -> multicurrency_total_ttc - $multicurrency_totalpaid - $multicurrency_totalcreditnotes - $multicurrency_totaldeposits , 'MT' );
2021-12-16 12:33:45 +01:00
// Code to fix case of corrupted data
2022-10-06 20:21:31 +02:00
// 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
// 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)
if ( $resteapayer == 0 && $multicurrency_resteapayer != 0 && $object -> multicurrency_code != $conf -> currency ) {
2021-12-16 12:33:45 +01:00
$resteapayer = price2num ( $multicurrency_resteapayer / $object -> multicurrency_tx , 'MT' );
}
2021-10-15 09:05:13 +02:00
}
2021-10-15 09:19:49 +02:00
2021-02-25 23:21:30 +01:00
if ( $object -> paye ) {
2017-12-05 12:31:57 +01:00
$resteapayer = 0 ;
}
$resteapayeraffiche = $resteapayer ;
2022-10-12 11:52:33 +02:00
if ( ! empty ( $conf -> global -> FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS )) { // Never use this
2018-02-19 17:32:59 +01:00
$filterabsolutediscount = " fk_invoice_supplier_source IS NULL " ; // If we want deposit to be substracted to payments only and not to total of final invoice
$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
2018-02-14 10:47:10 +01:00
} else {
2018-02-19 17:32:59 +01:00
$filterabsolutediscount = " fk_invoice_supplier_source IS NULL OR (description LIKE '(DEPOSIT)%' AND description NOT LIKE '(EXCESS PAID)%') " ;
2018-02-20 10:08:55 +01:00
$filtercreditnote = " fk_invoice_supplier_source IS NOT NULL AND (description NOT LIKE '(DEPOSIT)%' OR description LIKE '(EXCESS PAID)%') " ;
2018-02-14 10:47:10 +01:00
}
2018-03-08 20:49:37 +01:00
2018-02-14 18:02:40 +01:00
$absolute_discount = $societe -> getAvailableDiscounts ( '' , $filterabsolutediscount , 0 , 1 );
$absolute_creditnote = $societe -> getAvailableDiscounts ( '' , $filtercreditnote , 0 , 1 );
2018-02-14 10:47:10 +01:00
$absolute_discount = price2num ( $absolute_discount , 'MT' );
$absolute_creditnote = price2num ( $absolute_creditnote , 'MT' );
2016-10-02 21:39:24 +02:00
2017-10-16 08:47:05 +02:00
/*
2021-02-25 23:21:30 +01:00
* View card
*/
2022-09-05 16:15:13 +02:00
$objectidnext = $object -> getIdReplacingInvoice ();
2022-09-07 13:55:02 +02:00
2017-10-16 08:47:05 +02:00
$head = facturefourn_prepare_head ( $object );
2019-11-08 10:53:31 +01:00
$titre = $langs -> trans ( 'SupplierInvoice' );
2013-11-17 23:25:25 +01:00
2020-12-23 23:16:27 +01:00
print dol_get_fiche_head ( $head , 'card' , $titre , - 1 , 'supplier_invoice' );
2012-10-28 13:57:21 +01:00
2018-02-14 10:47:10 +01:00
$formconfirm = '' ;
// Confirmation de la conversion de l'avoir en reduc
if ( $action == 'converttoreduc' ) {
2021-02-25 23:21:30 +01:00
if ( $object -> type == FactureFournisseur :: TYPE_STANDARD ) {
$type_fac = 'ExcessPaid' ;
} elseif ( $object -> type == FactureFournisseur :: TYPE_CREDIT_NOTE ) {
$type_fac = 'CreditNote' ;
} elseif ( $object -> type == FactureFournisseur :: TYPE_DEPOSIT ) {
$type_fac = 'Deposit' ;
}
2018-02-19 15:52:07 +01:00
$text = $langs -> trans ( 'ConfirmConvertToReducSupplier' , strtolower ( $langs -> transnoentities ( $type_fac )));
2019-06-19 14:24:48 +02:00
$text .= '<br>' . $langs -> trans ( 'ConfirmConvertToReducSupplier2' );
2019-11-08 10:53:31 +01:00
$formconfirm = $form -> formconfirm ( $_SERVER [ 'PHP_SELF' ] . '?facid=' . $object -> id , $langs -> trans ( 'ConvertToReduc' ), $text , 'confirm_converttoreduc' , '' , " yes " , 2 );
2018-02-14 10:47:10 +01:00
}
2017-10-16 08:47:05 +02:00
// Clone confirmation
2021-02-25 23:21:30 +01:00
if ( $action == 'clone' ) {
2017-10-16 08:47:05 +02:00
// Create an array for form
2019-11-08 10:53:31 +01:00
$formquestion = array (
2020-10-07 15:01:28 +02:00
array ( 'type' => 'text' , 'name' => 'newsupplierref' , 'label' => $langs -> trans ( " RefSupplier " ), 'value' => $langs -> trans ( " CopyOf " ) . ' ' . $object -> ref_supplier ),
array ( 'type' => 'date' , 'name' => 'newdate' , 'label' => $langs -> trans ( " Date " ), 'value' => dol_now ())
2017-10-16 08:47:05 +02:00
);
2019-04-12 12:12:08 +02:00
// Ask confirmation to clone
$formconfirm = $form -> formconfirm ( $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id , $langs -> trans ( 'ToClone' ), $langs -> trans ( 'ConfirmCloneInvoice' , $object -> ref ), 'confirm_clone' , $formquestion , 'yes' , 1 , 250 );
2017-10-16 08:47:05 +02:00
}
2012-10-28 13:57:21 +01:00
2020-10-26 13:22:03 +01:00
// Confirmation of validation
2021-02-25 23:21:30 +01:00
if ( $action == 'valid' ) {
2020-10-25 08:56:34 +01:00
// We check if number is temporary number
2020-10-25 23:29:51 +01:00
if ( preg_match ( '/^[\(]?PROV/i' , $object -> ref ) || empty ( $object -> ref )) {
2020-10-31 14:32:18 +01:00
// empty should not happened, but when it occurs, the test save life
2017-10-16 08:47:05 +02:00
$numref = $object -> getNextNumRef ( $societe );
2020-05-21 15:05:19 +02:00
} else {
2017-10-16 08:47:05 +02:00
$numref = $object -> ref ;
}
2013-04-12 00:00:31 +02:00
2020-10-25 23:29:51 +01:00
if ( $numref < 0 ) {
2020-10-25 08:56:34 +01:00
setEventMessages ( $object -> error , $object -> errors , 'errors' );
$action = '' ;
2020-10-25 23:29:51 +01:00
} else {
2020-10-25 08:56:34 +01:00
$text = $langs -> trans ( 'ConfirmValidateBill' , $numref );
/* if ( ! empty ( $conf -> notification -> enabled ))
2020-10-25 21:33:13 +01:00
{
require_once DOL_DOCUMENT_ROOT . '/core/class/notify.class.php' ;
$notify = new Notify ( $db );
$text .= '<br>' ;
$text .= $notify -> confirmMessage ( 'BILL_SUPPLIER_VALIDATE' , $object -> socid , $object );
} */
2020-10-25 08:56:34 +01:00
$formquestion = array ();
$qualified_for_stock_change = 0 ;
if ( empty ( $conf -> global -> STOCK_SUPPORTS_SERVICES )) {
$qualified_for_stock_change = $object -> hasProductsOrServices ( 2 );
2020-10-07 15:01:28 +02:00
} else {
2020-10-25 08:56:34 +01:00
$qualified_for_stock_change = $object -> hasProductsOrServices ( 1 );
2020-10-07 15:01:28 +02:00
}
2012-10-28 13:57:21 +01:00
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> stock -> enabled ) && ! empty ( $conf -> global -> STOCK_CALCULATE_ON_SUPPLIER_BILL ) && $qualified_for_stock_change ) {
2020-10-25 08:56:34 +01:00
$langs -> load ( " stocks " );
2020-10-25 21:33:13 +01:00
require_once DOL_DOCUMENT_ROOT . '/product/class/html.formproduct.class.php' ;
2020-10-25 08:56:34 +01:00
$formproduct = new FormProduct ( $db );
$warehouse = new Entrepot ( $db );
$warehouse_array = $warehouse -> list_array ();
if ( count ( $warehouse_array ) == 1 ) {
$label = $object -> type == FactureFournisseur :: TYPE_CREDIT_NOTE ? $langs -> trans ( " WarehouseForStockDecrease " , current ( $warehouse_array )) : $langs -> trans ( " WarehouseForStockIncrease " , current ( $warehouse_array ));
2020-10-25 21:33:13 +01:00
$value = '<input type="hidden" id="idwarehouse" name="idwarehouse" value="' . key ( $warehouse_array ) . '">' ;
2020-10-25 08:56:34 +01:00
} else {
$label = $object -> type == FactureFournisseur :: TYPE_CREDIT_NOTE ? $langs -> trans ( " SelectWarehouseForStockDecrease " ) : $langs -> trans ( " SelectWarehouseForStockIncrease " );
2020-10-25 21:33:13 +01:00
$value = $formproduct -> selectWarehouses ( GETPOST ( 'idwarehouse' ) ? GETPOST ( 'idwarehouse' ) : 'ifone' , 'idwarehouse' , '' , 1 );
2020-10-25 08:56:34 +01:00
}
$formquestion = array (
array ( 'type' => 'other' , 'name' => 'idwarehouse' , 'label' => $label , 'value' => $value )
);
}
2020-10-25 21:33:13 +01:00
$formconfirm = $form -> formconfirm ( $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id , $langs -> trans ( 'ValidateBill' ), $text , 'confirm_valid' , $formquestion , 1 , 1 );
}
2020-10-07 15:01:28 +02:00
}
2017-10-21 20:26:32 +02:00
2020-10-07 15:01:28 +02:00
// Confirmation edit (back to draft)
2021-02-25 23:21:30 +01:00
if ( $action == 'edit' ) {
2020-10-07 15:01:28 +02:00
$formquestion = array ();
2017-10-21 20:26:32 +02:00
2020-10-07 15:01:28 +02:00
$qualified_for_stock_change = 0 ;
2021-02-25 23:21:30 +01:00
if ( empty ( $conf -> global -> STOCK_SUPPORTS_SERVICES )) {
2020-10-07 15:01:28 +02:00
$qualified_for_stock_change = $object -> hasProductsOrServices ( 2 );
} else {
$qualified_for_stock_change = $object -> hasProductsOrServices ( 1 );
}
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> stock -> enabled ) && ! empty ( $conf -> global -> STOCK_CALCULATE_ON_SUPPLIER_BILL ) && $qualified_for_stock_change ) {
2020-10-07 15:01:28 +02:00
$langs -> load ( " stocks " );
require_once DOL_DOCUMENT_ROOT . '/product/class/html.formproduct.class.php' ;
$formproduct = new FormProduct ( $db );
$warehouse = new Entrepot ( $db );
$warehouse_array = $warehouse -> list_array ();
if ( count ( $warehouse_array ) == 1 ) {
$label = $object -> type == FactureFournisseur :: TYPE_CREDIT_NOTE ? $langs -> trans ( " WarehouseForStockIncrease " , current ( $warehouse_array )) : $langs -> trans ( " WarehouseForStockDecrease " , current ( $warehouse_array ));
$value = '<input type="hidden" id="idwarehouse" name="idwarehouse" value="' . key ( $warehouse_array ) . '">' ;
} else {
$label = $object -> type == FactureFournisseur :: TYPE_CREDIT_NOTE ? $langs -> trans ( " SelectWarehouseForStockIncrease " ) : $langs -> trans ( " SelectWarehouseForStockDecrease " );
$value = $formproduct -> selectWarehouses ( GETPOST ( 'idwarehouse' ) ? GETPOST ( 'idwarehouse' ) : 'ifone' , 'idwarehouse' , '' , 1 );
}
$formquestion = array (
array ( 'type' => 'other' , 'name' => 'idwarehouse' , 'label' => $label , 'value' => $value )
);
}
$formconfirm = $form -> formconfirm ( $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id , $langs -> trans ( 'UnvalidateBill' ), $langs -> trans ( 'ConfirmUnvalidateBill' , $object -> ref ), 'confirm_edit' , $formquestion , 1 , 1 );
2017-10-16 08:47:05 +02:00
}
2012-10-28 13:57:21 +01:00
2017-10-16 08:47:05 +02:00
// Confirmation set paid
2021-12-30 19:03:46 +01:00
if ( $action == 'paid' && ( $resteapayer <= 0 || ( ! empty ( $conf -> global -> SUPPLIER_INVOICE_CAN_SET_PAID_EVEN_IF_PARTIALLY_PAID ) && $resteapayer == $object -> total_ttc ))) {
2016-03-25 15:53:44 +01:00
$formconfirm = $form -> formconfirm ( $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id , $langs -> trans ( 'ClassifyPaid' ), $langs -> trans ( 'ConfirmClassifyPaidBill' , $object -> ref ), 'confirm_paid' , '' , 0 , 1 );
2017-10-16 08:47:05 +02:00
}
2012-10-28 13:57:21 +01:00
2021-12-30 19:03:46 +01:00
if ( $action == 'paid' && $resteapayer > 0 && ( empty ( $conf -> global -> SUPPLIER_INVOICE_CAN_SET_PAID_EVEN_IF_PARTIALLY_PAID ) || $resteapayer != $object -> total_ttc )) {
2021-04-06 09:15:34 +02:00
$close = array ();
// Code
$i = 0 ;
$close [ $i ][ 'code' ] = 'discount_vat' ; // escompte
$i ++ ;
$close [ $i ][ 'code' ] = 'badsupplier' ;
$i ++ ;
$close [ $i ][ 'code' ] = 'other' ;
$i ++ ;
// Help
$i = 0 ;
$close [ $i ][ 'label' ] = $langs -> trans ( " HelpEscompte " ) . '<br><br>' . $langs -> trans ( " ConfirmClassifyPaidPartiallyReasonDiscountVatDesc " );
$i ++ ;
$close [ $i ][ 'label' ] = $langs -> trans ( " ConfirmClassifyPaidPartiallyReasonBadSupplierDesc " );
$i ++ ;
$close [ $i ][ 'label' ] = $langs -> trans ( " Other " );
$i ++ ;
// Text
$i = 0 ;
$close [ $i ][ 'reason' ] = $form -> textwithpicto ( $langs -> transnoentities ( " ConfirmClassifyPaidPartiallyReasonDiscount " , $resteapayer , $langs -> trans ( " Currency " . $conf -> currency )), $close [ $i ][ 'label' ], 1 );
$i ++ ;
$close [ $i ][ 'reason' ] = $form -> textwithpicto ( $langs -> transnoentities ( " ConfirmClassifyPaidPartiallyReasonBadCustomer " , $resteapayer , $langs -> trans ( " Currency " . $conf -> currency )), $close [ $i ][ 'label' ], 1 );
$i ++ ;
$close [ $i ][ 'reason' ] = $form -> textwithpicto ( $langs -> transnoentities ( " Other " ), $close [ $i ][ 'label' ], 1 );
$i ++ ;
// arrayreasons[code]=reason
foreach ( $close as $key => $val ) {
$arrayreasons [ $close [ $key ][ 'code' ]] = $close [ $key ][ 'reason' ];
}
// Create a form table
$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' ));
// Incomplete payment. We ask if the reason is discount or other
$formconfirm = $form -> formconfirm ( $_SERVER [ " PHP_SELF " ] . '?facid=' . $object -> id , $langs -> trans ( 'ClassifyPaid' ), $langs -> trans ( 'ConfirmClassifyPaidPartially' , $object -> ref ), 'confirm_paid_partially' , $formquestion , " yes " , 1 , 310 );
}
// Confirmation of the abandoned classification
if ( $action == 'canceled' ) {
// Code
$close [ 1 ][ 'code' ] = 'badsupplier' ;
$close [ 2 ][ 'code' ] = 'abandon' ;
// Help
$close [ 1 ][ 'label' ] = $langs -> trans ( " ConfirmClassifyPaidPartiallyReasonBadSupplierDesc " );
$close [ 2 ][ 'label' ] = $langs -> trans ( " ConfirmClassifyAbandonReasonOtherDesc " );
// Text
$close [ 1 ][ 'reason' ] = $form -> textwithpicto ( $langs -> transnoentities ( " ConfirmClassifyPaidPartiallyReasonBadSupplier " , $object -> ref ), $close [ 1 ][ 'label' ], 1 );
$close [ 2 ][ 'reason' ] = $form -> textwithpicto ( $langs -> transnoentities ( " ConfirmClassifyAbandonReasonOther " ), $close [ 2 ][ 'label' ], 1 );
// arrayreasons
$arrayreasons [ $close [ 1 ][ 'code' ]] = $close [ 1 ][ 'reason' ];
$arrayreasons [ $close [ 2 ][ 'code' ]] = $close [ 2 ][ 'reason' ];
// Create a form table
$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' ));
$formconfirm = $form -> formconfirm ( $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id , $langs -> trans ( 'CancelBill' ), $langs -> trans ( 'ConfirmCancelBill' , $object -> ref ), 'confirm_canceled' , $formquestion , " yes " , 1 , 250 );
}
2017-10-16 08:47:05 +02:00
// Confirmation de la suppression de la facture fournisseur
2021-02-25 23:21:30 +01:00
if ( $action == 'delete' ) {
2016-03-25 15:53:44 +01:00
$formconfirm = $form -> formconfirm ( $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id , $langs -> trans ( 'DeleteBill' ), $langs -> trans ( 'ConfirmDeleteBill' ), 'confirm_delete' , '' , 0 , 1 );
2017-10-16 08:47:05 +02:00
}
2021-02-25 23:21:30 +01:00
if ( $action == 'deletepayment' ) {
2017-10-16 08:47:05 +02:00
$payment_id = GETPOST ( 'paiement_id' );
2016-07-01 16:59:03 +02:00
$formconfirm = $form -> formconfirm ( $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id . '&paiement_id=' . $payment_id , $langs -> trans ( 'DeletePayment' ), $langs -> trans ( 'ConfirmDeletePayment' ), 'confirm_delete_paiement' , '' , 0 , 1 );
2017-10-16 08:47:05 +02:00
}
2012-10-28 13:57:21 +01:00
2021-02-25 23:21:30 +01:00
// Confirmation to delete line
if ( $action == 'ask_deleteline' ) {
2019-11-08 10:53:31 +01:00
$formconfirm = $form -> formconfirm ( $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id . '&lineid=' . $lineid , $langs -> trans ( 'DeleteProductLine' ), $langs -> trans ( 'ConfirmDeleteProductLine' ), 'confirm_deleteline' , '' , 0 , 1 );
2015-02-28 04:59:27 +01:00
}
2021-02-25 23:21:30 +01:00
if ( ! $formconfirm ) {
2020-01-28 12:00:27 +01:00
$parameters = array ( 'formConfirm' => $formconfirm , 'lineid' => $lineid );
2015-03-18 14:31:37 +01:00
$reshook = $hookmanager -> executeHooks ( 'formConfirm' , $parameters , $object , $action ); // Note that $action and $object may have been modified by hook
2021-02-25 23:21:30 +01:00
if ( empty ( $reshook )) {
$formconfirm .= $hookmanager -> resPrint ;
} elseif ( $reshook > 0 ) {
$formconfirm = $hookmanager -> resPrint ;
}
2014-07-22 04:25:44 +02:00
}
2014-07-22 04:24:08 +02:00
// Print form confirm
2014-07-22 04:27:41 +02:00
print $formconfirm ;
2014-07-22 04:24:08 +02:00
2012-10-28 13:57:21 +01:00
2020-10-07 15:01:28 +02:00
// Supplier invoice card
$linkback = '<a href="' . DOL_URL_ROOT . '/fourn/facture/list.php?restore_lastsearch_values=1' . ( ! empty ( $socid ) ? '&socid=' . $socid : '' ) . '">' . $langs -> trans ( " BackToList " ) . '</a>' ;
2016-10-02 21:39:24 +02:00
2020-10-07 15:01:28 +02:00
$morehtmlref = '<div class="refidno">' ;
// Ref supplier
$morehtmlref .= $form -> editfieldkey ( " RefSupplier " , 'ref_supplier' , $object -> ref_supplier , $object , $usercancreate , 'string' , '' , 0 , 1 );
$morehtmlref .= $form -> editfieldval ( " RefSupplier " , 'ref_supplier' , $object -> ref_supplier , $object , $usercancreate , 'string' , '' , null , null , '' , 1 );
// Thirdparty
2021-08-27 15:32:53 +02:00
$morehtmlref .= '<br>' . $langs -> trans ( 'ThirdParty' ) . ' : ' . $object -> thirdparty -> getNomUrl ( 1 , 'supplier' );
2021-02-25 23:21:30 +01:00
if ( empty ( $conf -> global -> MAIN_DISABLE_OTHER_LINK ) && $object -> thirdparty -> id > 0 ) {
$morehtmlref .= ' (<a href="' . DOL_URL_ROOT . '/fourn/facture/list.php?socid=' . $object -> thirdparty -> id . '&search_company=' . urlencode ( $object -> thirdparty -> name ) . '">' . $langs -> trans ( " OtherBills " ) . '</a>)' ;
}
2020-10-07 15:01:28 +02:00
// Project
2022-06-14 17:53:17 +02:00
if ( ! empty ( $conf -> project -> enabled )) {
2020-10-07 15:01:28 +02:00
$langs -> load ( " projects " );
$morehtmlref .= '<br>' . $langs -> trans ( 'Project' ) . ' ' ;
2021-02-25 23:21:30 +01:00
if ( $usercancreate ) {
2020-10-07 15:01:28 +02:00
if ( $action != 'classify' ) {
2021-09-18 22:09:55 +02:00
$morehtmlref .= '<a class="editfielda" href="' . $_SERVER [ 'PHP_SELF' ] . '?action=classify&token=' . newToken () . '&id=' . $object -> id . '">' . img_edit ( $langs -> transnoentitiesnoconv ( 'SetProject' )) . '</a> : ' ;
2020-10-07 15:01:28 +02:00
}
if ( $action == 'classify' ) {
//$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1);
$morehtmlref .= '<form method="post" action="' . $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id . '">' ;
$morehtmlref .= '<input type="hidden" name="action" value="classin">' ;
$morehtmlref .= '<input type="hidden" name="token" value="' . newToken () . '">' ;
$morehtmlref .= $formproject -> select_projects (( empty ( $conf -> global -> PROJECT_CAN_ALWAYS_LINK_TO_ALL_SUPPLIERS ) ? $object -> socid : - 1 ), $object -> fk_project , 'projectid' , $maxlength , 0 , 1 , 0 , 1 , 0 , 0 , '' , 1 );
$morehtmlref .= '<input type="submit" class="button valignmiddle" value="' . $langs -> trans ( " Modify " ) . '">' ;
$morehtmlref .= '</form>' ;
} else {
$morehtmlref .= $form -> form_project ( $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id , $object -> socid , $object -> fk_project , 'none' , 0 , 0 , 0 , 1 );
}
} else {
if ( ! empty ( $object -> fk_project )) {
$proj = new Project ( $db );
$proj -> fetch ( $object -> fk_project );
2021-10-29 09:40:38 +02:00
$morehtmlref .= ' : ' . $proj -> getNomUrl ( 1 );
if ( $proj -> title ) {
$morehtmlref .= ' - ' . $proj -> title ;
}
2020-10-07 15:01:28 +02:00
} else {
$morehtmlref .= '' ;
}
}
}
$morehtmlref .= '</div>' ;
2016-10-02 21:39:24 +02:00
2022-05-22 17:09:08 +02:00
$object -> totalpaid = $totalpaid ; // To give a chance to dol_banner_tab to use already paid amount to show correct status
2016-10-02 21:39:24 +02:00
2020-10-07 15:01:28 +02:00
dol_banner_tab ( $object , 'ref' , $linkback , 1 , 'ref' , 'ref' , $morehtmlref );
2016-10-02 21:39:24 +02:00
2020-10-07 15:01:28 +02:00
print '<div class="fichecenter">' ;
print '<div class="fichehalfleft">' ;
print '<div class="underbanner clearboth"></div>' ;
2012-10-28 13:57:21 +01:00
2020-10-07 15:01:28 +02:00
print '<table class="border tableforfield" width="100%">' ;
2012-10-28 13:57:21 +01:00
2020-10-07 15:01:28 +02:00
// Type
print '<tr><td class="titlefield">' . $langs -> trans ( 'Type' ) . '</td><td>' ;
2020-11-29 15:16:53 +01:00
print '<span class="badgeneutral">' ;
2020-10-07 15:01:28 +02:00
print $object -> getLibType ();
2020-11-29 15:16:53 +01:00
print '</span>' ;
2021-02-25 23:21:30 +01:00
if ( $object -> type == FactureFournisseur :: TYPE_REPLACEMENT ) {
2020-10-07 15:01:28 +02:00
$facreplaced = new FactureFournisseur ( $db );
$facreplaced -> fetch ( $object -> fk_facture_source );
2022-06-02 11:11:28 +02:00
print ' ' . $langs -> transnoentities ( " ReplaceInvoice " , $facreplaced -> getNomUrl ( 1 ));
2020-10-07 15:01:28 +02:00
}
2021-02-25 23:21:30 +01:00
if ( $object -> type == FactureFournisseur :: TYPE_CREDIT_NOTE ) {
2020-10-07 15:01:28 +02:00
$facusing = new FactureFournisseur ( $db );
2021-11-03 14:22:08 +01:00
if ( $object -> fk_facture_source > 0 ) {
$facusing -> fetch ( $object -> fk_facture_source );
2022-06-02 11:11:28 +02:00
print ' ' . $langs -> transnoentities ( " CorrectInvoice " , $facusing -> getNomUrl ( 1 ));
2021-11-03 14:22:08 +01:00
} else {
2022-06-02 11:11:28 +02:00
print ' ' . $langs -> transnoentities ( " CorrectedInvoiceNotFound " );
2021-11-03 14:22:08 +01:00
}
2020-10-07 15:01:28 +02:00
}
2012-10-28 13:57:21 +01:00
2020-10-07 15:01:28 +02:00
$facidavoir = $object -> getListIdAvoirFromInvoice ();
2021-02-25 23:21:30 +01:00
if ( count ( $facidavoir ) > 0 ) {
2021-05-07 21:28:35 +02:00
$invoicecredits = array ();
2021-02-25 23:21:30 +01:00
foreach ( $facidavoir as $id ) {
2020-10-07 15:01:28 +02:00
$facavoir = new FactureFournisseur ( $db );
$facavoir -> fetch ( $id );
2021-05-07 21:28:35 +02:00
$invoicecredits [] = $facavoir -> getNomUrl ( 1 );
2020-10-07 15:01:28 +02:00
}
2021-05-11 19:21:38 +02:00
print ' (' . $langs -> transnoentities ( " InvoiceHasAvoir " ) . ( count ( $invoicecredits ) ? ' ' : '' ) . implode ( ',' , $invoicecredits ) . ')' ;
2020-10-07 15:01:28 +02:00
}
2022-09-05 16:15:13 +02:00
if ( isset ( $objectidnext ) && $objectidnext > 0 ) {
2020-10-07 15:01:28 +02:00
$facthatreplace = new FactureFournisseur ( $db );
2023-01-24 11:04:26 +01:00
$facthatreplace -> fetch ( $objectidnext );
2020-10-07 15:01:28 +02:00
print ' (' . $langs -> transnoentities ( " ReplacedByInvoice " , $facthatreplace -> getNomUrl ( 1 )) . ')' ;
}
if ( $object -> type == FactureFournisseur :: TYPE_CREDIT_NOTE || $object -> type == FactureFournisseur :: TYPE_DEPOSIT ) {
$discount = new DiscountAbsolute ( $db );
$result = $discount -> fetch ( 0 , 0 , $object -> id );
if ( $result > 0 ) {
2021-02-17 11:54:34 +01:00
print ' <span class="opacitymediumbycolor paddingleft">' ;
$s = $langs -> trans ( " CreditNoteConvertedIntoDiscount " , '{s1}' , '{s2}' );
$s = str_replace ( '{s1}' , $object -> getLibType ( 1 ), $s );
$s = str_replace ( '{s2}' , $discount -> getNomUrl ( 1 , 'discount' ), $s );
print $s ;
print '</span><br>' ;
2020-10-07 15:01:28 +02:00
}
}
2022-02-07 14:34:27 +01:00
if ( $object -> fk_fac_rec_source > 0 ) {
$tmptemplate = new FactureFournisseurRec ( $db );
$result = $tmptemplate -> fetch ( $object -> fk_fac_rec_source );
if ( $result > 0 ) {
print ' <span class="opacitymediumbycolor paddingleft">' ;
$link = '<a href="' . DOL_URL_ROOT . '/fourn/facture/card-rec.php?facid=' . $tmptemplate -> id . '">' . dol_escape_htmltag ( $tmptemplate -> titre ) . '</a>' ;
$s = $langs -> transnoentities ( " GeneratedFromSupplierTemplate " , $link );
print $s ;
print '</span>' ;
}
}
2020-10-07 15:01:28 +02:00
print '</td></tr>' ;
2018-02-27 16:30:39 +01:00
2020-10-07 15:01:28 +02:00
// Relative and absolute discounts
2022-06-02 12:35:38 +02:00
print '<!-- Discounts -->' . " \n " ;
print '<tr><td>' . $langs -> trans ( 'DiscountStillRemaining' );
2018-03-05 10:58:19 +01:00
print '</td><td>' ;
2018-02-27 16:30:39 +01:00
2018-03-05 10:58:19 +01:00
$thirdparty = $societe ;
$discount_type = 1 ;
2019-11-08 10:53:31 +01:00
$backtopage = urlencode ( $_SERVER [ " PHP_SELF " ] . '?facid=' . $object -> id );
2018-03-05 10:58:19 +01:00
include DOL_DOCUMENT_ROOT . '/core/tpl/object_discounts.tpl.php' ;
print '</td></tr>' ;
2018-02-27 16:30:39 +01:00
2020-10-07 15:01:28 +02:00
// Label
print '<tr>' ;
2021-07-09 13:33:37 +02:00
print '<td>' . $form -> editfieldkey ( " Label " , 'label' , $object -> label , $object , $usercancreate ) . '</td>' ;
print '<td>' . $form -> editfieldval ( " Label " , 'label' , $object -> label , $object , $usercancreate ) . '</td>' ;
2020-10-07 15:01:28 +02:00
print '</tr>' ;
2012-10-28 13:57:21 +01:00
2020-10-07 15:01:28 +02:00
$form_permission = ( $object -> statut < FactureFournisseur :: STATUS_CLOSED ) && $usercancreate && ( $object -> getSommePaiement () <= 0 );
2021-07-09 13:33:37 +02:00
$form_permission2 = ( $object -> statut < FactureFournisseur :: STATUS_CLOSED ) && $usercancreate ;
2015-04-03 05:18:47 +02:00
2020-10-07 15:01:28 +02:00
// Date
2021-07-09 13:33:37 +02:00
print '<tr><td>' ;
print $form -> editfieldkey ( " DateInvoice " , 'datef' , $object -> datep , $object , $form_permission , 'datepicker' );
print '</td><td colspan="3">' ;
2020-10-07 15:01:28 +02:00
print $form -> editfieldval ( " Date " , 'datef' , $object -> datep , $object , $form_permission , 'datepicker' );
print '</td>' ;
2012-10-28 13:57:21 +01:00
2019-03-22 09:44:30 +01:00
// Default terms of the settlement
2013-07-30 16:21:39 +02:00
$langs -> load ( 'bills' );
print '<tr><td class="nowrap">' ;
print '<table width="100%" class="nobordernopadding"><tr><td class="nowrap">' ;
print $langs -> trans ( 'PaymentConditions' );
print '<td>' ;
2021-07-09 13:33:37 +02:00
if ( $action != 'editconditions' && $form_permission ) {
2021-09-27 12:24:01 +02:00
print '<td class="right"><a class="editfielda" href="' . $_SERVER [ " PHP_SELF " ] . '?action=editconditions&token=' . newToken () . '&id=' . $object -> id . '">' . img_edit ( $langs -> trans ( 'SetConditions' ), 1 ) . '</a></td>' ;
2017-04-09 18:09:08 +02:00
}
2013-07-30 16:21:39 +02:00
print '</tr></table>' ;
2021-07-09 13:33:37 +02:00
print '</td><td>' ;
2021-02-25 23:21:30 +01:00
if ( $action == 'editconditions' ) {
2019-02-09 12:00:49 +01:00
$form -> form_conditions_reglement ( $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id , $object -> cond_reglement_id , 'cond_reglement_id' );
2020-05-21 15:05:19 +02:00
} else {
2019-02-09 12:00:49 +01:00
$form -> form_conditions_reglement ( $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id , $object -> cond_reglement_id , 'none' );
2013-07-30 16:21:39 +02:00
}
print " </td> " ;
print '</tr>' ;
2013-09-06 09:26:23 +02:00
2020-06-22 22:54:33 +02:00
// Due date
2021-07-09 13:33:37 +02:00
print '<tr><td>' ;
print $form -> editfieldkey ( " DateMaxPayment " , 'date_lim_reglement' , $object -> date_echeance , $object , $form_permission2 , 'datepicker' );
print '</td><td>' ;
print $form -> editfieldval ( " DateMaxPayment " , 'date_lim_reglement' , $object -> date_echeance , $object , $form_permission2 , 'datepicker' );
2020-06-22 22:54:33 +02:00
if ( $action != 'editdate_lim_reglement' && $object -> hasDelay ()) {
print img_warning ( $langs -> trans ( 'Late' ));
}
print '</td>' ;
2013-07-30 16:21:39 +02:00
// Mode of payment
$langs -> load ( 'bills' );
print '<tr><td class="nowrap">' ;
print '<table width="100%" class="nobordernopadding"><tr><td class="nowrap">' ;
print $langs -> trans ( 'PaymentMode' );
print '</td>' ;
2021-07-09 13:33:37 +02:00
if ( $action != 'editmode' && $form_permission2 ) {
2021-09-27 12:24:01 +02:00
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>' ;
2017-04-09 18:09:08 +02:00
}
2013-07-30 16:21:39 +02:00
print '</tr></table>' ;
2021-07-09 13:33:37 +02:00
print '</td><td>' ;
2021-02-25 23:21:30 +01:00
if ( $action == 'editmode' ) {
2018-12-29 07:54:30 +01:00
$form -> form_modes_reglement ( $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id , $object -> mode_reglement_id , 'mode_reglement_id' , 'DBIT' , 1 , 1 );
2020-05-21 15:05:19 +02:00
} else {
2018-12-29 07:54:30 +01:00
$form -> form_modes_reglement ( $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id , $object -> mode_reglement_id , 'none' );
2013-07-30 16:21:39 +02:00
}
print '</td></tr>' ;
2016-02-19 22:25:59 +01:00
// Multicurrency
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> multicurrency -> enabled )) {
2016-02-19 22:25:59 +01:00
// Multicurrency code
print '<tr>' ;
print '<td>' ;
print '<table class="nobordernopadding" width="100%"><tr><td>' ;
2018-10-27 18:07:05 +02:00
print $form -> editfieldkey ( 'Currency' , 'multicurrency_code' , '' , $object , 0 );
2016-02-19 22:25:59 +01:00
print '</td>' ;
2021-02-25 23:21:30 +01:00
if ( $action != 'editmulticurrencycode' && $object -> statut == $object :: STATUS_DRAFT ) {
2021-09-27 12:24:01 +02:00
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>' ;
2021-02-25 23:21:30 +01:00
}
2016-02-19 22:25:59 +01:00
print '</tr></table>' ;
2021-07-09 13:33:37 +02:00
print '</td><td>' ;
2016-02-19 22:25:59 +01:00
if ( $action == 'editmulticurrencycode' ) {
2019-11-08 10:53:31 +01:00
$form -> form_multicurrency_code ( $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id , $object -> multicurrency_code , 'multicurrency_code' );
2016-02-19 22:25:59 +01:00
} else {
2019-11-08 10:53:31 +01:00
$form -> form_multicurrency_code ( $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id , $object -> multicurrency_code , 'none' );
2016-02-19 22:25:59 +01:00
}
print '</td></tr>' ;
2016-08-10 09:47:25 +02:00
2016-02-19 22:25:59 +01:00
// Multicurrency rate
2021-02-25 23:21:30 +01:00
if ( $object -> multicurrency_code != $conf -> currency || $object -> multicurrency_tx != 1 ) {
2019-11-03 17:48:16 +01:00
print '<tr>' ;
print '<td>' ;
2021-07-09 13:33:37 +02:00
print '<table class="nobordernopadding centpercent"><tr><td>' ;
2019-11-03 17:48:16 +01:00
print $form -> editfieldkey ( 'CurrencyRate' , 'multicurrency_tx' , '' , $object , 0 );
print '</td>' ;
2021-02-25 23:21:30 +01:00
if ( $action != 'editmulticurrencyrate' && $object -> statut == $object :: STATUS_DRAFT && $object -> multicurrency_code && $object -> multicurrency_code != $conf -> currency ) {
2021-09-27 12:24:01 +02:00
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>' ;
2021-02-25 23:21:30 +01:00
}
2019-11-03 17:48:16 +01:00
print '</tr></table>' ;
2021-07-09 13:33:37 +02:00
print '</td><td>' ;
2019-11-03 17:48:16 +01:00
if ( $action == 'editmulticurrencyrate' || $action == 'actualizemulticurrencyrate' ) {
if ( $action == 'actualizemulticurrencyrate' ) {
list ( $object -> fk_multicurrency , $object -> multicurrency_tx ) = MultiCurrency :: getIdAndTxFromCode ( $object -> db , $object -> multicurrency_code );
}
2019-11-08 10:53:31 +01:00
$form -> form_multicurrency_rate ( $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id , $object -> multicurrency_tx , 'multicurrency_tx' , $object -> multicurrency_code );
2019-11-03 17:48:16 +01:00
} else {
2019-11-08 10:53:31 +01:00
$form -> form_multicurrency_rate ( $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id , $object -> multicurrency_tx , 'none' , $object -> multicurrency_code );
2019-11-03 17:48:16 +01:00
if ( $object -> statut == $object :: STATUS_DRAFT && $object -> multicurrency_code && $object -> multicurrency_code != $conf -> currency ) {
print '<div class="inline-block"> ' ;
print '<a href="' . $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id . '&action=actualizemulticurrencyrate">' . $langs -> trans ( " ActualizeCurrency " ) . '</a>' ;
print '</div>' ;
}
2016-12-11 02:50:21 +01:00
}
2019-11-03 17:48:16 +01:00
print '</td></tr>' ;
2016-02-19 22:25:59 +01:00
}
}
2017-10-16 08:47:05 +02:00
// Bank Account
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> banque -> enabled )) {
2020-08-28 12:53:30 +02:00
print '<tr><td class="nowrap">' ;
print '<table width="100%" class="nobordernopadding"><tr><td class="nowrap">' ;
print $langs -> trans ( 'BankAccount' );
print '<td>' ;
2021-02-25 23:21:30 +01:00
if ( $action != 'editbankaccount' && $usercancreate ) {
2021-09-27 12:24:01 +02:00
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>' ;
2021-02-25 23:21:30 +01:00
}
2020-08-28 12:53:30 +02:00
print '</tr></table>' ;
2021-07-09 13:33:37 +02:00
print '</td><td>' ;
2020-08-28 12:53:30 +02:00
if ( $action == 'editbankaccount' ) {
$form -> formSelectAccount ( $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id , $object -> fk_account , 'fk_account' , 1 );
} else {
$form -> formSelectAccount ( $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id , $object -> fk_account , 'none' );
}
print " </td> " ;
print '</tr>' ;
2017-10-16 08:47:05 +02:00
}
2014-07-09 18:44:20 +02:00
2015-02-23 09:16:14 +01:00
// Incoterms
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> incoterm -> enabled )) {
2015-02-23 09:16:14 +01:00
print '<tr><td>' ;
2017-10-16 08:47:05 +02:00
print '<table width="100%" class="nobordernopadding"><tr><td>' ;
print $langs -> trans ( 'IncotermLabel' );
2019-03-07 20:46:38 +01:00
print '<td><td class="right">' ;
2021-02-25 23:21:30 +01:00
if ( $usercancreate ) {
2021-09-27 12:24:01 +02:00
print '<a class="editfielda" href="' . DOL_URL_ROOT . '/fourn/facture/card.php?facid=' . $object -> id . '&action=editincoterm&token=' . newToken () . '">' . img_edit () . '</a>' ;
2021-02-25 23:21:30 +01:00
} else {
print ' ' ;
}
2017-10-16 08:47:05 +02:00
print '</td></tr></table>' ;
print '</td>' ;
2021-07-09 13:33:37 +02:00
print '<td>' ;
2021-02-25 23:21:30 +01:00
if ( $action != 'editincoterm' ) {
2019-06-25 13:00:02 +02:00
print $form -> textwithpicto ( $object -> display_incoterms (), $object -> label_incoterms , 1 );
2020-05-21 15:05:19 +02:00
} else {
2019-11-08 10:53:31 +01:00
print $form -> select_incoterms (( ! empty ( $object -> fk_incoterms ) ? $object -> fk_incoterms : '' ), ( ! empty ( $object -> location_incoterms ) ? $object -> location_incoterms : '' ), $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id );
2015-02-23 09:16:14 +01:00
}
2017-10-16 08:47:05 +02:00
print '</td></tr>' ;
2015-02-23 09:16:14 +01:00
}
2015-03-04 23:03:29 +01:00
2020-10-14 20:25:02 +02:00
// Intracomm report
if ( ! empty ( $conf -> intracommreport -> enabled )) {
2020-10-31 14:32:18 +01:00
$langs -> loadLangs ( array ( " intracommreport " ));
2021-06-03 21:47:10 +02:00
print '<tr><td>' ;
print '<table width="100%" class="nobordernopadding"><tr><td>' ;
2020-10-31 14:32:18 +01:00
print $langs -> trans ( 'IntracommReportTransportMode' );
print '</td>' ;
2021-04-12 07:37:34 +02:00
if ( $action != 'editmode' && ( $user -> rights -> fournisseur -> facture -> creer || $user -> rights -> supplier_invoice -> creer )) {
2021-09-27 12:24:01 +02:00
print '<td class="right"><a class="editfielda" href="' . $_SERVER [ " PHP_SELF " ] . '?action=editmode&token=' . newToken () . '&id=' . $object -> id . '">' . img_edit () . '</a></td>' ;
2020-10-31 14:32:18 +01:00
}
print '</tr></table>' ;
print '</td>' ;
print '<td>' ;
2021-02-25 23:21:30 +01:00
if ( $action == 'editmode' ) {
2020-10-31 14:32:18 +01:00
$form -> formSelectTransportMode ( $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id , $object -> transport_mode_id , 'transport_mode_id' , 1 , 1 );
2021-02-25 23:21:30 +01:00
} else {
2020-10-31 14:32:18 +01:00
$form -> formSelectTransportMode ( $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id , $object -> transport_mode_id , 'none' );
}
print '</td></tr>' ;
2020-10-14 20:25:02 +02:00
}
2019-07-22 06:56:55 +02:00
2017-10-16 08:47:05 +02:00
// Other attributes
$cols = 2 ;
2019-11-08 10:53:31 +01:00
include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_view.tpl.php' ;
2016-11-06 15:01:40 +01:00
2017-10-16 08:47:05 +02:00
print '</table>' ;
print '</div>' ;
2021-07-09 13:33:37 +02:00
2017-10-16 08:47:05 +02:00
print '<div class="fichehalfright">' ;
print '<div class="underbanner clearboth"></div>' ;
2016-10-29 07:45:24 +02:00
2019-01-17 19:31:01 +01:00
print '<table class="border tableforfield centpercent">' ;
2016-10-29 07:45:24 +02:00
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> multicurrency -> enabled ) && ( $object -> multicurrency_code != $conf -> currency )) {
2017-10-16 08:47:05 +02:00
// Multicurrency Amount HT
2019-11-08 10:53:31 +01:00
print '<tr><td class="titlefieldmiddle">' . $form -> editfieldkey ( 'MulticurrencyAmountHT' , 'multicurrency_total_ht' , '' , $object , 0 ) . '</td>' ;
2022-06-15 04:46:58 +02:00
print '<td class="nowrap right amountcard">' . price ( $object -> multicurrency_total_ht , '' , $langs , 0 , - 1 , - 1 , ( ! empty ( $object -> multicurrency_code ) ? $object -> multicurrency_code : $conf -> currency )) . '</td>' ;
2017-10-16 08:47:05 +02:00
print '</tr>' ;
// Multicurrency Amount VAT
2019-11-08 10:53:31 +01:00
print '<tr><td>' . $form -> editfieldkey ( 'MulticurrencyAmountVAT' , 'multicurrency_total_tva' , '' , $object , 0 ) . '</td>' ;
2022-06-15 04:46:58 +02:00
print '<td class="nowrap right amountcard">' . price ( $object -> multicurrency_total_tva , '' , $langs , 0 , - 1 , - 1 , ( ! empty ( $object -> multicurrency_code ) ? $object -> multicurrency_code : $conf -> currency )) . '</td>' ;
2017-10-16 08:47:05 +02:00
print '</tr>' ;
// Multicurrency Amount TTC
2021-07-09 13:33:37 +02:00
print '<tr><td>' . $form -> editfieldkey ( 'MulticurrencyAmountTTC' , 'multicurrency_total_ttc' , '' , $object , 0 ) . '</td>' ;
2022-06-15 04:46:58 +02:00
print '<td class="nowrap right amountcard">' . price ( $object -> multicurrency_total_ttc , '' , $langs , 0 , - 1 , - 1 , ( ! empty ( $object -> multicurrency_code ) ? $object -> multicurrency_code : $conf -> currency )) . '</td>' ;
2017-10-16 08:47:05 +02:00
print '</tr>' ;
}
2016-11-03 20:55:55 +01:00
2017-10-16 08:47:05 +02:00
// Amount
2022-06-15 04:46:58 +02:00
print '<tr><td class="titlefield">' . $langs -> trans ( 'AmountHT' ) . '</td>' ;
print '<td class="nowrap right amountcard">' . price ( $object -> total_ht , 1 , $langs , 0 , - 1 , - 1 , $conf -> currency ) . '</td>' ;
print '</tr>' ;
// VAT
print '<tr><td>' . $langs -> trans ( 'AmountVAT' ) . '</td>' ;
print '<td class="nowrap right amountcard">' ;
2021-02-25 23:21:30 +01:00
if ( GETPOST ( 'calculationrule' )) {
$calculationrule = GETPOST ( 'calculationrule' , 'alpha' );
} else {
$calculationrule = ( empty ( $conf -> global -> MAIN_ROUNDOFTOTAL_NOT_TOTALOFROUND ) ? 'totalofround' : 'roundoftotal' );
}
if ( $calculationrule == 'totalofround' ) {
$calculationrulenum = 1 ;
} else {
$calculationrulenum = 2 ;
}
2019-11-22 14:51:13 +01:00
// Show link for "recalculate"
2019-11-24 18:12:40 +01:00
if ( $object -> getVentilExportCompta () == 0 ) {
2022-04-11 21:22:27 +02:00
$s = '<span class="hideonsmartphone opacitymedium">' . $langs -> trans ( " ReCalculate " ) . ' </span>' ;
2019-11-22 15:04:40 +01:00
$s .= '<a href="' . $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id . '&action=calculate&calculationrule=totalofround">' . $langs -> trans ( " Mode1 " ) . '</a>' ;
$s .= ' / ' ;
$s .= '<a href="' . $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id . '&action=calculate&calculationrule=roundoftotal">' . $langs -> trans ( " Mode2 " ) . '</a>' ;
2022-03-26 09:40:34 +01:00
print '<div class="inline-block">' ;
print $form -> textwithtooltip ( $s , $langs -> trans ( " CalculationRuleDesc " , $calculationrulenum ) . '<br>' . $langs -> trans ( " CalculationRuleDescSupplier " ), 2 , 1 , img_picto ( '' , 'help' ), '' , 3 , '' , 0 , 'recalculate' );
2022-06-15 04:46:58 +02:00
print ' ' ;
2022-03-26 09:40:34 +01:00
print '</div>' ;
2019-11-21 14:58:20 +01:00
}
2022-06-15 04:46:58 +02:00
print price ( $object -> total_tva , 1 , $langs , 0 , - 1 , - 1 , $conf -> currency );
2022-03-26 09:40:34 +01:00
print '</td></tr>' ;
2017-10-16 08:47:05 +02:00
// Amount Local Taxes
//TODO: Place into a function to control showing by country or study better option
2021-02-25 23:21:30 +01:00
if ( $societe -> localtax1_assuj == " 1 " ) { //Localtax1
2019-01-27 11:55:16 +01:00
print '<tr><td>' . $langs -> transcountry ( " AmountLT1 " , $societe -> country_code ) . '</td>' ;
2022-06-15 04:46:58 +02:00
print '<td class="nowrap right amountcard">' . price ( $object -> total_localtax1 , 1 , $langs , 0 , - 1 , - 1 , $conf -> currency ) . '</td>' ;
2017-10-16 08:47:05 +02:00
print '</tr>' ;
}
2021-02-25 23:21:30 +01:00
if ( $societe -> localtax2_assuj == " 1 " ) { //Localtax2
2019-01-27 11:55:16 +01:00
print '<tr><td>' . $langs -> transcountry ( " AmountLT2 " , $societe -> country_code ) . '</td>' ;
2022-06-15 04:46:58 +02:00
print '<td class="nowrap right amountcard">' . price ( $object -> total_localtax2 , 1 , $langs , 0 , - 1 , - 1 , $conf -> currency ) . '</td>' ;
2017-10-16 08:47:05 +02:00
print '</tr>' ;
}
2022-06-15 04:46:58 +02:00
print '<tr><td>' . $langs -> trans ( 'AmountTTC' ) . '</td>' ;
print '<td colspan="3" class="nowrap right amountcard">' . price ( $object -> total_ttc , 1 , $langs , 0 , - 1 , - 1 , $conf -> currency ) . '</td>' ;
print '</tr>' ;
2016-10-29 07:45:24 +02:00
2017-10-16 08:47:05 +02:00
print '</table>' ;
2016-11-06 15:01:40 +01:00
2021-07-09 13:33:37 +02:00
// List of payments
2017-02-02 20:07:41 +01:00
2022-05-22 17:09:08 +02:00
$totalpaid = 0 ;
2017-02-02 20:07:41 +01:00
2016-10-29 07:45:24 +02:00
$sign = 1 ;
2021-02-25 23:21:30 +01:00
if ( $object -> type == FactureFournisseur :: TYPE_CREDIT_NOTE ) {
$sign = - 1 ;
}
2016-10-29 07:45:24 +02:00
2019-11-08 10:53:31 +01:00
$nbrows = 9 ; $nbcols = 3 ;
2022-06-14 17:53:17 +02:00
if ( ! empty ( $conf -> project -> enabled )) {
2021-02-25 23:21:30 +01:00
$nbrows ++ ;
}
if ( ! empty ( $conf -> banque -> enabled )) {
$nbrows ++ ; $nbcols ++ ;
}
if ( ! empty ( $conf -> incoterm -> enabled )) {
$nbrows ++ ;
}
if ( ! empty ( $conf -> multicurrency -> enabled )) {
$nbrows += 5 ;
}
2016-10-29 07:45:24 +02:00
2017-10-16 08:47:05 +02:00
// Local taxes
2021-02-25 23:21:30 +01:00
if ( $societe -> localtax1_assuj == " 1 " ) {
$nbrows ++ ;
}
if ( $societe -> localtax2_assuj == " 1 " ) {
$nbrows ++ ;
}
2017-10-16 08:47:05 +02:00
2020-09-14 01:03:51 +02:00
$sql = 'SELECT p.datep as dp, p.ref, p.num_paiement as num_payment, p.rowid, p.fk_bank,' ;
2022-09-05 16:15:13 +02:00
$sql .= ' c.id as paiement_type, c.code as payment_code,' ;
2019-11-08 10:53:31 +01:00
$sql .= ' pf.amount,' ;
$sql .= ' ba.rowid as baid, ba.ref as baref, ba.label, ba.number as banumber, ba.account_number, ba.fk_accountancy_journal' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'paiementfourn as p' ;
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'bank as b ON p.fk_bank = b.rowid' ;
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'bank_account as ba ON b.fk_account = ba.rowid' ;
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_paiement as c ON p.fk_paiement = c.id' ;
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'paiementfourn_facturefourn as pf ON pf.fk_paiementfourn = p.rowid' ;
2021-08-27 23:36:06 +02:00
$sql .= ' WHERE pf.fk_facturefourn = ' . (( int ) $object -> id );
2019-11-08 10:53:31 +01:00
$sql .= ' ORDER BY p.datep, p.tms' ;
2017-10-16 08:47:05 +02:00
$result = $db -> query ( $sql );
2021-02-25 23:21:30 +01:00
if ( $result ) {
2017-10-16 08:47:05 +02:00
$num = $db -> num_rows ( $result );
$i = 0 ;
2018-01-12 00:16:55 +01:00
print '<div class="div-table-responsive-no-min">' ;
2017-10-16 08:47:05 +02:00
print '<table class="noborder paymenttable" width="100%">' ;
print '<tr class="liste_titre">' ;
2019-11-08 10:53:31 +01:00
print '<td class="liste_titre">' . ( $object -> type == FactureFournisseur :: TYPE_CREDIT_NOTE ? $langs -> trans ( " PaymentsBack " ) : $langs -> trans ( 'Payments' )) . '</td>' ;
2017-10-16 08:47:05 +02:00
print '<td>' . $langs -> trans ( 'Date' ) . '</td>' ;
print '<td>' . $langs -> trans ( 'Type' ) . '</td>' ;
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> banque -> enabled )) {
print '<td class="right">' . $langs -> trans ( 'BankAccount' ) . '</td>' ;
}
2019-03-07 20:46:38 +01:00
print '<td class="right">' . $langs -> trans ( 'Amount' ) . '</td>' ;
2017-10-16 08:47:05 +02:00
print '<td width="18"> </td>' ;
print '</tr>' ;
2021-02-25 23:21:30 +01:00
if ( $num > 0 ) {
while ( $i < $num ) {
2017-10-16 08:47:05 +02:00
$objp = $db -> fetch_object ( $result );
2019-11-08 10:53:31 +01:00
$paymentstatic -> id = $objp -> rowid ;
$paymentstatic -> datepaye = $db -> jdate ( $objp -> dp );
$paymentstatic -> ref = ( $objp -> ref ? $objp -> ref : $objp -> rowid );
2020-09-14 01:03:51 +02:00
$paymentstatic -> num_payment = $objp -> num_payment ;
2022-09-07 13:55:02 +02:00
2022-09-05 16:15:13 +02:00
$paymentstatic -> paiementcode = $objp -> payment_code ;
2022-09-07 13:55:02 +02:00
$paymentstatic -> type_code = $objp -> payment_code ;
$paymentstatic -> type_label = $objp -> payment_type ;
2017-11-23 15:22:32 +01:00
print '<tr class="oddeven">' ;
print '<td>' ;
2017-10-16 08:47:05 +02:00
print $paymentstatic -> getNomUrl ( 1 );
print '</td>' ;
2019-11-08 10:53:31 +01:00
print '<td>' . dol_print_date ( $db -> jdate ( $objp -> dp ), 'day' ) . '</td>' ;
2017-10-16 08:47:05 +02:00
print '<td>' ;
2020-09-14 01:03:51 +02:00
print $form -> form_modes_reglement ( null , $objp -> paiement_type , 'none' ) . ' ' . $objp -> num_payment ;
2017-10-16 08:47:05 +02:00
print '</td>' ;
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> banque -> enabled )) {
2019-11-08 10:53:31 +01:00
$bankaccountstatic -> id = $objp -> baid ;
$bankaccountstatic -> ref = $objp -> baref ;
$bankaccountstatic -> label = $objp -> baref ;
2017-05-15 05:11:00 +02:00
$bankaccountstatic -> number = $objp -> banumber ;
2019-11-08 10:53:31 +01:00
if ( ! empty ( $conf -> accounting -> enabled )) {
2017-05-15 05:11:00 +02:00
$bankaccountstatic -> account_number = $objp -> account_number ;
$accountingjournal = new AccountingJournal ( $db );
$accountingjournal -> fetch ( $objp -> fk_accountancy_journal );
2019-01-27 11:55:16 +01:00
$bankaccountstatic -> accountancy_journal = $accountingjournal -> getNomUrl ( 0 , 1 , 1 , '' , 1 );
2017-05-15 05:11:00 +02:00
}
2019-03-07 20:46:38 +01:00
print '<td class="right">' ;
2021-02-25 23:21:30 +01:00
if ( $objp -> baid > 0 ) {
print $bankaccountstatic -> getNomUrl ( 1 , 'transactions' );
}
2017-10-16 08:47:05 +02:00
print '</td>' ;
}
2019-11-08 10:53:31 +01:00
print '<td class="right">' . price ( $sign * $objp -> amount ) . '</td>' ;
2019-03-07 20:46:38 +01:00
print '<td class="center">' ;
2021-02-25 23:21:30 +01:00
if ( $object -> statut == FactureFournisseur :: STATUS_VALIDATED && $object -> paye == 0 && $user -> socid == 0 ) {
2020-10-01 10:50:54 +02:00
print '<a href="' . $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id . '&action=deletepayment&token=' . newToken () . '&paiement_id=' . $objp -> rowid . '">' ;
2017-10-16 08:47:05 +02:00
print img_delete ();
print '</a>' ;
}
print '</td>' ;
print '</tr>' ;
2022-05-22 17:09:08 +02:00
$totalpaid += $objp -> amount ;
2017-10-16 08:47:05 +02:00
$i ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-05-03 11:25:53 +02:00
print '<tr class="oddeven"><td colspan="' . $nbcols . '"><span class="opacitymedium">' . $langs -> trans ( " None " ) . '</span></td><td></td><td></td></tr>' ;
2017-10-16 08:47:05 +02:00
}
2016-11-02 07:13:17 +01:00
/*
2021-02-25 23:21:30 +01:00
if ( $object -> paye == 0 )
{
2022-05-22 17:09:08 +02:00
print '<tr><td colspan="' . $nbcols . '" class="right">' . $langs -> trans ( 'AlreadyPaid' ) . ' :</td><td class="right">' . price ( $totalpaid ) . '</td><td></td></tr>' ;
2021-02-25 23:21:30 +01:00
print '<tr><td colspan="' . $nbcols . '" class="right">' . $langs -> trans ( " Billed " ) . ' :</td><td class="right">' . price ( $object -> total_ttc ) . '</td><td></td></tr>' ;
2016-11-06 15:01:40 +01:00
2022-05-22 17:09:08 +02:00
$resteapayer = $object -> total_ttc - $totalpaid ;
2016-11-06 15:01:40 +01:00
2021-02-25 23:21:30 +01:00
print '<tr><td colspan="' . $nbcols . '" class="right">' . $langs -> trans ( 'RemainderToPay' ) . ' :</td>' ;
print '<td class="right' . ( $resteapayer ? ' amountremaintopay' : '' ) . '">' . price ( $resteapayer ) . '</td><td></td></tr>' ;
}
2016-11-02 07:13:17 +01:00
*/
2017-10-16 08:47:05 +02:00
$db -> free ( $result );
2020-05-21 15:05:19 +02:00
} else {
2017-10-16 08:47:05 +02:00
dol_print_error ( $db );
}
2016-09-26 00:10:29 +02:00
2021-02-25 23:21:30 +01:00
if ( $object -> type != FactureFournisseur :: TYPE_CREDIT_NOTE ) {
2016-11-02 07:13:17 +01:00
// Total already paid
2019-11-08 10:53:31 +01:00
print '<tr><td colspan="' . $nbcols . '" class="right">' ;
2020-11-29 15:16:53 +01:00
print '<span class="opacitymedium">' ;
2021-02-25 23:21:30 +01:00
if ( $object -> type != FactureFournisseur :: TYPE_DEPOSIT ) {
2016-11-02 07:13:17 +01:00
print $langs -> trans ( 'AlreadyPaidNoCreditNotesNoDeposits' );
2021-02-25 23:21:30 +01:00
} else {
print $langs -> trans ( 'AlreadyPaid' );
}
2020-11-29 15:16:53 +01:00
print '</span>' ;
2022-05-22 17:09:08 +02:00
print '</td><td class="right"' . (( $totalpaid > 0 ) ? ' class="amountalreadypaid"' : '' ) . '>' . price ( $totalpaid ) . '</td><td> </td></tr>' ;
2016-11-02 07:13:17 +01:00
2022-05-22 17:09:08 +02:00
//$resteapayer = $object->total_ttc - $totalpaid;
2016-11-02 07:13:17 +01:00
$resteapayeraffiche = $resteapayer ;
2018-02-14 10:47:10 +01:00
2016-11-02 07:13:17 +01:00
$cssforamountpaymentcomplete = 'amountpaymentcomplete' ;
// Loop on each credit note or deposit amount applied
$creditnoteamount = 0 ;
$depositamount = 0 ;
2018-02-14 10:47:10 +01:00
2016-11-02 07:13:17 +01:00
$sql = " SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc, " ;
2018-03-05 10:58:19 +01:00
$sql .= " re.description, re.fk_invoice_supplier_source " ;
2019-11-08 10:53:31 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " societe_remise_except as re " ;
2021-08-27 18:18:50 +02:00
$sql .= " WHERE fk_invoice_supplier = " . (( int ) $object -> id );
2016-11-02 07:13:17 +01:00
$resql = $db -> query ( $sql );
if ( $resql ) {
$num = $db -> num_rows ( $resql );
$i = 0 ;
$invoice = new FactureFournisseur ( $db );
while ( $i < $num ) {
$obj = $db -> fetch_object ( $resql );
2018-03-05 10:58:19 +01:00
$invoice -> fetch ( $obj -> fk_invoice_supplier_source );
2019-11-08 10:53:31 +01:00
print '<tr><td colspan="' . $nbcols . '" class="right">' ;
2021-02-25 23:21:30 +01:00
if ( $invoice -> type == FactureFournisseur :: TYPE_CREDIT_NOTE ) {
2019-11-08 10:53:31 +01:00
print $langs -> trans ( " CreditNote " ) . ' ' ;
2021-02-25 23:21:30 +01:00
}
if ( $invoice -> type == FactureFournisseur :: TYPE_DEPOSIT ) {
2019-11-08 10:53:31 +01:00
print $langs -> trans ( " Deposit " ) . ' ' ;
2021-02-25 23:21:30 +01:00
}
2016-11-02 07:13:17 +01:00
print $invoice -> getNomUrl ( 0 );
print ' :</td>' ;
2019-11-08 10:53:31 +01:00
print '<td class="right">' . price ( $obj -> amount_ttc ) . '</td>' ;
2019-03-07 20:46:38 +01:00
print '<td class="right">' ;
2019-11-08 10:53:31 +01:00
print '<a href="' . $_SERVER [ " PHP_SELF " ] . '?facid=' . $object -> id . '&action=unlinkdiscount&discountid=' . $obj -> rowid . '">' . img_delete () . '</a>' ;
2016-11-02 07:13:17 +01:00
print '</td></tr>' ;
2019-11-08 10:53:31 +01:00
$i ++ ;
2021-02-25 23:21:30 +01:00
if ( $invoice -> type == FactureFournisseur :: TYPE_CREDIT_NOTE ) {
2016-11-02 07:13:17 +01:00
$creditnoteamount += $obj -> amount_ttc ;
2021-02-25 23:21:30 +01:00
}
if ( $invoice -> type == FactureFournisseur :: TYPE_DEPOSIT ) {
2016-11-02 07:13:17 +01:00
$depositamount += $obj -> amount_ttc ;
2021-02-25 23:21:30 +01:00
}
2016-11-02 07:13:17 +01:00
}
} else {
dol_print_error ( $db );
}
// Paye partiellement 'escompte'
if (( $object -> statut == FactureFournisseur :: STATUS_CLOSED || $object -> statut == FactureFournisseur :: STATUS_ABANDONED ) && $object -> close_code == 'discount_vat' ) {
2019-11-08 10:53:31 +01:00
print '<tr><td colspan="' . $nbcols . '" class="right nowrap">' ;
2020-11-29 15:16:53 +01:00
print '<span class="opacitymedium">' ;
print $form -> textwithpicto ( $langs -> trans ( " Discount " ), $langs -> trans ( " HelpEscompte " ), - 1 );
print '</span>' ;
2022-05-22 17:09:08 +02:00
print '</td><td class="right">' . price ( $object -> total_ttc - $creditnoteamount - $depositamount - $totalpaid ) . '</td><td> </td></tr>' ;
2016-11-02 07:13:17 +01:00
$resteapayeraffiche = 0 ;
2018-06-23 14:39:24 +02:00
$cssforamountpaymentcomplete = 'amountpaymentneutral' ;
2016-11-02 07:13:17 +01:00
}
// Paye partiellement ou Abandon 'badsupplier'
if (( $object -> statut == FactureFournisseur :: STATUS_CLOSED || $object -> statut == FactureFournisseur :: STATUS_ABANDONED ) && $object -> close_code == 'badsupplier' ) {
2019-11-08 10:53:31 +01:00
print '<tr><td colspan="' . $nbcols . '" class="right nowrap">' ;
2020-11-29 15:16:53 +01:00
print '<span class="opacitymedium">' ;
print $form -> textwithpicto ( $langs -> trans ( " Abandoned " ), $langs -> trans ( " HelpAbandonBadCustomer " ), - 1 );
print '</span>' ;
2022-05-22 17:09:08 +02:00
print '</td><td class="right">' . price ( $object -> total_ttc - $creditnoteamount - $depositamount - $totalpaid ) . '</td><td> </td></tr>' ;
2016-11-02 07:13:17 +01:00
// $resteapayeraffiche=0;
2018-06-23 14:39:24 +02:00
$cssforamountpaymentcomplete = 'amountpaymentneutral' ;
2016-11-02 07:13:17 +01:00
}
// Paye partiellement ou Abandon 'product_returned'
if (( $object -> statut == FactureFournisseur :: STATUS_CLOSED || $object -> statut == FactureFournisseur :: STATUS_ABANDONED ) && $object -> close_code == 'product_returned' ) {
2019-11-08 10:53:31 +01:00
print '<tr><td colspan="' . $nbcols . '" class="right nowrap">' ;
2020-11-29 15:16:53 +01:00
print '<span class="opacitymedium">' ;
print $form -> textwithpicto ( $langs -> trans ( " ProductReturned " ), $langs -> trans ( " HelpAbandonProductReturned " ), - 1 );
print '</span>' ;
2022-05-22 17:09:08 +02:00
print '</td><td class="right">' . price ( $object -> total_ttc - $creditnoteamount - $depositamount - $totalpaid ) . '</td><td> </td></tr>' ;
2016-11-02 07:13:17 +01:00
$resteapayeraffiche = 0 ;
2018-06-23 14:39:24 +02:00
$cssforamountpaymentcomplete = 'amountpaymentneutral' ;
2016-11-02 07:13:17 +01:00
}
// Paye partiellement ou Abandon 'abandon'
if (( $object -> statut == FactureFournisseur :: STATUS_CLOSED || $object -> statut == FactureFournisseur :: STATUS_ABANDONED ) && $object -> close_code == 'abandon' ) {
2019-11-08 10:53:31 +01:00
print '<tr><td colspan="' . $nbcols . '" class="right nowrap">' ;
2016-11-02 07:13:17 +01:00
$text = $langs -> trans ( " HelpAbandonOther " );
2021-02-25 23:21:30 +01:00
if ( $object -> close_note ) {
2019-11-08 10:53:31 +01:00
$text .= '<br><br><b>' . $langs -> trans ( " Reason " ) . '</b>:' . $object -> close_note ;
2021-02-25 23:21:30 +01:00
}
2020-11-29 15:16:53 +01:00
print '<span class="opacitymedium">' ;
print $form -> textwithpicto ( $langs -> trans ( " Abandoned " ), $text , - 1 );
print '</span>' ;
2022-05-22 17:09:08 +02:00
print '</td><td class="right">' . price ( $object -> total_ttc - $creditnoteamount - $depositamount - $totalpaid ) . '</td><td> </td></tr>' ;
2016-11-02 07:13:17 +01:00
$resteapayeraffiche = 0 ;
2018-06-23 14:39:24 +02:00
$cssforamountpaymentcomplete = 'amountpaymentneutral' ;
2016-11-02 07:13:17 +01:00
}
// Billed
2020-11-29 15:16:53 +01:00
print '<tr><td colspan="' . $nbcols . '" class="right">' ;
print '<span class="opacitymedium">' ;
print $langs -> trans ( " Billed " );
print '</span>' ;
print '</td><td class="right">' . price ( $object -> total_ttc ) . '</td><td> </td></tr>' ;
2016-11-02 07:13:17 +01:00
// Remainder to pay
2019-11-08 10:53:31 +01:00
print '<tr><td colspan="' . $nbcols . '" class="right">' ;
2020-11-29 15:16:53 +01:00
print '<span class="opacitymedium">' ;
2022-04-19 23:24:08 +02:00
print $langs -> trans ( 'RemainderToPay' );
if ( $resteapayeraffiche < 0 ) {
print ' (' . $langs -> trans ( 'NegativeIfExcessPaid' ) . ')' ;
2021-02-25 23:21:30 +01:00
}
2020-11-29 15:16:53 +01:00
print '</span>' ;
print '</td>' ;
2021-08-28 16:52:06 +02:00
print '<td class="right' . ( $resteapayeraffiche ? ' amountremaintopay' : ( ' ' . $cssforamountpaymentcomplete )) . '">' . price ( $resteapayeraffiche ) . '</td><td> </td></tr>' ;
2021-08-16 03:45:33 +02:00
2021-08-16 03:44:09 +02:00
// Remainder to pay Multicurrency
2021-08-16 03:45:33 +02:00
if ( $object -> multicurrency_code != $conf -> currency || $object -> multicurrency_tx != 1 ) {
print '<tr><td colspan="' . $nbcols . '" class="right">' ;
print '<span class="opacitymedium">' ;
2022-04-19 23:24:08 +02:00
print $langs -> trans ( 'RemainderToPayMulticurrency' );
if ( $resteapayeraffiche < 0 ) {
print ' (' . $langs -> trans ( 'NegativeIfExcessPaid' ) . ')' ;
2021-08-26 03:40:44 +02:00
}
2021-08-16 03:45:33 +02:00
print '</span>' ;
print '</td>' ;
2022-11-26 00:13:15 +01:00
print '<td class="right' . ( $resteapayeraffiche ? ' amountremaintopay' : ( ' ' . $cssforamountpaymentcomplete )) . '">' . ( ! empty ( $object -> multicurrency_code ) ? $object -> multicurrency_code : $conf -> currency ) . ' ' . price ( price2num ( $multicurrency_resteapayer , 'MT' )) . '</td><td> </td></tr>' ;
2021-08-16 03:45:33 +02:00
}
2020-05-21 15:05:19 +02:00
} else // Credit note
2016-11-02 07:13:17 +01:00
{
2019-11-08 10:53:31 +01:00
$cssforamountpaymentcomplete = 'amountpaymentneutral' ;
2017-12-05 12:31:57 +01:00
2016-11-02 07:13:17 +01:00
// Total already paid back
2019-11-08 10:53:31 +01:00
print '<tr><td colspan="' . $nbcols . '" class="right">' ;
2016-11-02 07:13:17 +01:00
print $langs -> trans ( 'AlreadyPaidBack' );
2022-05-22 17:09:08 +02:00
print ' :</td><td class="right">' . price ( $sign * $totalpaid ) . '</td><td> </td></tr>' ;
2016-11-02 07:13:17 +01:00
// Billed
2019-11-08 10:53:31 +01:00
print '<tr><td colspan="' . $nbcols . '" class="right">' . $langs -> trans ( " Billed " ) . ' :</td><td class="right">' . price ( $sign * $object -> total_ttc ) . '</td><td> </td></tr>' ;
2016-11-02 07:13:17 +01:00
// Remainder to pay back
2019-11-08 10:53:31 +01:00
print '<tr><td colspan="' . $nbcols . '" class="right">' ;
2020-11-29 15:16:53 +01:00
print '<span class="opacitymedium">' ;
2022-04-19 23:24:08 +02:00
print $langs -> trans ( 'RemainderToPayBack' );
if ( $resteapayeraffiche > 0 ) {
print ' (' . $langs -> trans ( 'NegativeIfExcessRefunded' ) . ')' ;
2021-02-25 23:21:30 +01:00
}
2020-11-29 15:16:53 +01:00
print '</td>' ;
print '</span>' ;
2021-08-28 16:52:06 +02:00
print '<td class="right' . ( $resteapayeraffiche ? ' amountremaintopay' : ( ' ' . $cssforamountpaymentcomplete )) . '">' . price ( $sign * $resteapayeraffiche ) . '</td><td> </td></tr>' ;
2021-08-28 16:54:16 +02:00
2021-08-26 03:40:44 +02:00
// Remainder to pay back Multicurrency
if ( $object -> multicurrency_code != $conf -> currency || $object -> multicurrency_tx != 1 ) {
print '<tr><td colspan="' . $nbcols . '" class="right">' ;
print '<span class="opacitymedium">' ;
2022-04-19 23:24:08 +02:00
print $langs -> trans ( 'RemainderToPayBackMulticurrency' );
if ( $resteapayeraffiche > 0 ) {
print ' (' . $langs -> trans ( 'NegativeIfExcessRefunded' ) . ')' ;
2021-08-26 03:40:44 +02:00
}
print '</span>' ;
print '</td>' ;
2021-08-28 16:52:06 +02:00
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> </td></tr>' ;
2021-08-26 03:40:44 +02:00
}
2021-08-28 16:54:16 +02:00
2016-11-02 07:13:17 +01:00
// Sold credit note
2019-03-07 20:46:38 +01:00
// print '<tr><td colspan="'.$nbcols.'" class="right">'.$langs->trans('TotalTTC').' :</td>';
// print '<td class="right" style="border: 1px solid;" bgcolor="#f0f0f0"><b>'.price($sign *
2016-11-02 07:13:17 +01:00
// $object->total_ttc).'</b></td><td> </td></tr>';
}
print '</table>' ;
2018-01-12 00:16:55 +01:00
print '</div>' ;
2016-11-02 07:13:17 +01:00
2017-10-16 08:47:05 +02:00
print '</div>' ;
print '</div>' ;
2016-11-06 15:01:40 +01:00
2017-10-16 08:47:05 +02:00
print '<div class="clearboth"></div><br>' ;
2012-10-28 13:57:21 +01:00
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> global -> MAIN_DISABLE_CONTACTS_TAB )) {
2017-10-16 08:47:05 +02:00
$blocname = 'contacts' ;
$title = $langs -> trans ( 'ContactsAddresses' );
include DOL_DOCUMENT_ROOT . '/core/tpl/bloc_showhide.tpl.php' ;
}
2012-10-28 13:57:21 +01:00
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> global -> MAIN_DISABLE_NOTES_TAB )) {
2019-11-08 10:53:31 +01:00
$colwidth = 20 ;
2017-10-16 08:47:05 +02:00
$blocname = 'notes' ;
$title = $langs -> trans ( 'Notes' );
include DOL_DOCUMENT_ROOT . '/core/tpl/bloc_showhide.tpl.php' ;
}
2012-10-28 13:57:21 +01:00
2017-10-16 08:47:05 +02:00
/*
2021-02-25 23:21:30 +01:00
* Lines
*/
2021-07-04 21:14:46 +02:00
print '<form name="addproduct" id="addproduct" action="' . $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id . (( $action != 'editline' ) ? '' : '#line_' . GETPOST ( 'lineid' , 'int' )) . '" method="POST">' ;
2019-12-18 23:12:31 +01:00
print '<input type="hidden" name="token" value="' . newToken () . '">' ;
2019-11-08 10:53:31 +01:00
print '<input type="hidden" name="action" value="' . (( $action != 'editline' ) ? 'addline' : 'updateline' ) . '">' ;
2016-10-29 07:45:24 +02:00
print '<input type="hidden" name="mode" value="">' ;
2021-07-04 21:14:46 +02:00
print '<input type="hidden" name="page_y" value="">' ;
2016-10-29 07:45:24 +02:00
print '<input type="hidden" name="id" value="' . $object -> id . '">' ;
print '<input type="hidden" name="socid" value="' . $societe -> id . '">' ;
2013-11-17 23:25:25 +01:00
2019-11-08 10:53:31 +01:00
if ( ! empty ( $conf -> use_javascript_ajax ) && $object -> statut == FactureFournisseur :: STATUS_DRAFT ) {
include DOL_DOCUMENT_ROOT . '/core/tpl/ajaxrow.tpl.php' ;
2015-02-27 18:05:12 +01:00
}
2017-10-16 08:47:05 +02:00
print '<div class="div-table-responsive-no-min">' ;
2022-03-26 11:22:54 +01:00
print '<table id="tablelines" class="noborder noshadow centpercent">' ;
2013-11-17 23:25:25 +01:00
2016-10-29 07:45:24 +02:00
global $forceall , $senderissupplier , $dateSelector , $inputalsopricewithtax ;
2019-11-08 10:53:31 +01:00
$forceall = 1 ; $dateSelector = 0 ; $inputalsopricewithtax = 1 ;
$senderissupplier = 2 ; // $senderissupplier=2 is same than 1 but disable test on minimum qty and disable autofill qty with minimum.
2018-04-24 12:57:31 +02:00
//if (! empty($conf->global->SUPPLIER_INVOICE_WITH_NOPRICEDEFINED)) $senderissupplier=2;
2021-02-25 23:21:30 +01:00
if ( ! empty ( $conf -> global -> SUPPLIER_INVOICE_WITH_PREDEFINED_PRICES_ONLY )) {
$senderissupplier = 1 ;
}
2016-08-10 09:47:25 +02:00
2015-02-27 18:05:12 +01:00
// Show object lines
2021-01-16 16:06:49 +01:00
if ( ! empty ( $object -> lines )) {
2015-11-04 18:21:59 +01:00
$ret = $object -> printObjectLines ( $action , $societe , $mysoc , $lineid , 1 );
2021-01-16 16:06:49 +01:00
}
2015-02-27 18:05:12 +01:00
2019-11-08 10:53:31 +01:00
$num = count ( $object -> lines );
2015-02-27 18:05:12 +01:00
2014-05-06 18:31:39 +02:00
// Form to add new line
2021-02-25 23:21:30 +01:00
if ( $object -> statut == FactureFournisseur :: STATUS_DRAFT && $usercancreate ) {
if ( $action != 'editline' ) {
2015-02-28 04:59:27 +01:00
// Add free products/services
2014-05-06 18:31:39 +02:00
2015-02-28 04:59:27 +01:00
$parameters = array ();
$reshook = $hookmanager -> executeHooks ( 'formAddObjectLine' , $parameters , $object , $action ); // Note that $action and $object may have been modified by hook
2021-04-29 15:32:15 +02:00
if ( $reshook < 0 ) setEventMessages ( $hookmanager -> error , $hookmanager -> errors , 'errors' );
if ( empty ( $reshook ))
$object -> formAddObjectLine ( 1 , $societe , $mysoc );
2014-05-06 18:31:39 +02:00
}
2017-10-16 08:47:05 +02:00
}
2012-10-28 13:57:21 +01:00
2017-10-16 08:47:05 +02:00
print '</table>' ;
print '</div>' ;
print '</form>' ;
2013-11-17 23:25:25 +01:00
2020-10-27 18:19:31 +01:00
print dol_get_fiche_end ();
2013-11-17 23:25:25 +01:00
2012-10-28 13:57:21 +01:00
2021-02-25 23:21:30 +01:00
if ( $action != 'presend' ) {
2017-10-16 08:47:05 +02:00
/*
2021-02-25 23:21:30 +01:00
* Buttons actions
*/
2012-10-28 13:57:21 +01:00
2017-10-16 08:47:05 +02:00
print '<div class="tabsAction">' ;
2012-10-28 13:57:21 +01:00
2015-07-31 11:06:25 +02:00
$parameters = array ();
$reshook = $hookmanager -> executeHooks ( 'addMoreActionsButtons' , $parameters , $object , $action ); // Note that $action and $object may have been
2017-10-16 08:47:05 +02:00
// modified by hook
2021-02-25 23:21:30 +01:00
if ( empty ( $reshook )) {
2020-10-07 15:01:28 +02:00
// Modify a validated invoice with no payments
2021-02-25 23:21:30 +01:00
if ( $object -> statut == FactureFournisseur :: STATUS_VALIDATED && $action != 'confirm_edit' && $object -> getSommePaiement () == 0 && $usercancreate ) {
2019-10-04 10:31:57 +02:00
// We check if lines of invoice are not already transfered into accountancy
2019-11-08 10:53:31 +01:00
$ventilExportCompta = $object -> getVentilExportCompta (); // Should be 0 since the sum of payments are zero. But we keep the protection.
2019-10-04 10:31:57 +02:00
2021-02-25 23:21:30 +01:00
if ( $ventilExportCompta == 0 ) {
2021-09-27 12:24:01 +02:00
print '<a class="butAction' . ( $conf -> use_javascript_ajax ? ' reposition' : '' ) . '" href="' . $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id . '&action=edit&token=' . newToken () . '">' . $langs -> trans ( 'Modify' ) . '</a>' ;
2020-05-21 15:05:19 +02:00
} else {
2021-04-06 09:15:34 +02:00
print '<span class="butActionRefused classfortooltip" title="' . $langs -> trans ( " DisabledBecauseDispatchedInBookkeeping " ) . '">' . $langs -> trans ( 'Modify' ) . '</span>' ;
2019-10-04 10:31:57 +02:00
}
2015-07-31 11:06:25 +02:00
}
2016-08-10 09:47:25 +02:00
2019-02-15 11:26:29 +01:00
$discount = new DiscountAbsolute ( $db );
$result = $discount -> fetch ( 0 , 0 , $object -> id );
2021-02-25 23:21:30 +01:00
// Reopen a standard paid invoice
2019-02-15 11:26:29 +01:00
if (( $object -> type == FactureFournisseur :: TYPE_STANDARD || $object -> type == FactureFournisseur :: TYPE_REPLACEMENT
2021-04-06 09:15:34 +02:00
|| ( $object -> type == FactureFournisseur :: TYPE_CREDIT_NOTE && empty ( $discount -> id ))
|| ( $object -> type == FactureFournisseur :: TYPE_DEPOSIT && empty ( $discount -> id )))
2021-02-25 23:21:30 +01:00
&& ( $object -> statut == FactureFournisseur :: STATUS_CLOSED || $object -> statut == FactureFournisseur :: STATUS_ABANDONED )) { // A paid invoice (partially or completely)
2022-09-05 16:15:13 +02:00
if ( ! $objectidnext && $object -> close_code != 'replaced' && $usercancreate ) { // Not replaced by another invoice
2021-09-18 19:34:46 +02:00
print '<a class="butAction' . ( $conf -> use_javascript_ajax ? ' reposition' : '' ) . '" href="' . $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id . '&action=reopen&token=' . newToken () . '">' . $langs -> trans ( 'ReOpen' ) . '</a>' ;
2020-05-21 15:05:19 +02:00
} else {
2020-04-21 10:02:18 +02:00
if ( $usercancreate ) {
2021-04-06 09:15:34 +02:00
print '<span class="butActionRefused classfortooltip" title="' . $langs -> trans ( " DisabledBecauseReplacedInvoice " ) . '">' . $langs -> trans ( 'ReOpen' ) . '</span>' ;
2017-10-16 08:47:05 +02:00
} elseif ( empty ( $conf -> global -> MAIN_BUTTON_HIDE_UNAUTHORIZED )) {
2021-04-06 09:15:34 +02:00
print '<span class="butActionRefused classfortooltip">' . $langs -> trans ( 'ReOpen' ) . '</span>' ;
}
}
}
// Validate
if ( $action != 'confirm_edit' && $object -> statut == FactureFournisseur :: STATUS_DRAFT ) {
if ( count ( $object -> lines )) {
if ( $usercanvalidate ) {
print '<a class="butAction" href="' . $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id . '&action=valid"' ;
print '>' . $langs -> trans ( 'Validate' ) . '</a>' ;
} else {
print '<a class="butActionRefused classfortooltip" href="#" title="' . dol_escape_htmltag ( $langs -> trans ( " NotAllowed " )) . '"' ;
print '>' . $langs -> trans ( 'Validate' ) . '</a>' ;
2017-10-16 08:47:05 +02:00
}
}
}
2016-08-10 09:47:25 +02:00
2017-10-16 08:47:05 +02:00
// Send by mail
2020-04-10 01:37:04 +02:00
if ( empty ( $user -> socid )) {
2021-02-25 23:21:30 +01:00
if (( $object -> statut == FactureFournisseur :: STATUS_VALIDATED || $object -> statut == FactureFournisseur :: STATUS_CLOSED )) {
if ( $usercansend ) {
2021-04-06 09:15:34 +02:00
print '<a class="butAction" href="' . $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id . '&action=presend&mode=init#formmailbeforetitle">' . $langs -> trans ( 'SendMail' ) . '</a>' ;
2021-02-25 23:21:30 +01:00
} else {
2021-04-06 09:15:34 +02:00
print '<span class="butActionRefused classfortooltip">' . $langs -> trans ( 'SendMail' ) . '</span>' ;
2021-02-25 23:21:30 +01:00
}
2017-10-16 08:47:05 +02:00
}
}
2016-08-10 09:47:25 +02:00
2021-04-06 09:15:34 +02:00
// Create payment
if ( $object -> type != FactureFournisseur :: TYPE_CREDIT_NOTE && $object -> statut == FactureFournisseur :: STATUS_VALIDATED && $object -> paye == 0 ) {
print '<a class="butAction' . ( $conf -> use_javascript_ajax ? ' reposition' : '' ) . '" href="' . DOL_URL_ROOT . '/fourn/facture/paiement.php?facid=' . $object -> id . '&action=create' . ( $object -> fk_account > 0 ? '&accountid=' . $object -> fk_account : '' ) . '">' . $langs -> trans ( 'DoPayment' ) . '</a>' ; // must use facid because id is for payment id not invoice
2017-10-16 08:47:05 +02:00
}
2016-08-10 09:47:25 +02:00
2016-11-10 06:51:15 +01:00
// Reverse back money or convert to reduction
2018-03-05 10:58:19 +01:00
if ( $object -> type == FactureFournisseur :: TYPE_CREDIT_NOTE || $object -> type == FactureFournisseur :: TYPE_DEPOSIT || $object -> type == FactureFournisseur :: TYPE_STANDARD ) {
2016-11-10 06:51:15 +01:00
// For credit note only
2021-02-25 23:21:30 +01:00
if ( $object -> type == FactureFournisseur :: TYPE_CREDIT_NOTE && $object -> statut == 1 && $object -> paye == 0 ) {
if ( $resteapayer == 0 ) {
2021-04-06 09:15:34 +02:00
print '<span class="butActionRefused classfortooltip" title="' . $langs -> trans ( " DisabledBecauseRemainderToPayIsZero " ) . '">' . $langs -> trans ( 'DoPaymentBack' ) . '</span>' ;
2020-05-21 15:05:19 +02:00
} else {
2021-04-06 09:15:34 +02:00
print '<a class="butAction" href="' . DOL_URL_ROOT . '/fourn/facture/paiement.php?facid=' . $object -> id . '&action=create&accountid=' . $object -> fk_account . '">' . $langs -> trans ( 'DoPaymentBack' ) . '</a>' ;
2016-11-10 06:51:15 +01:00
}
}
2018-02-14 10:47:10 +01:00
// For standard invoice with excess paid
2022-05-22 17:09:08 +02:00
if ( $object -> type == FactureFournisseur :: TYPE_STANDARD && empty ( $object -> paye ) && ( $object -> total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits ) < 0 && $usercancreate && empty ( $discount -> id )) {
2021-04-06 09:15:34 +02:00
print '<a class="butAction' . ( $conf -> use_javascript_ajax ? ' reposition' : '' ) . '" href="' . $_SERVER [ " PHP_SELF " ] . '?facid=' . $object -> id . '&action=converttoreduc">' . $langs -> trans ( 'ConvertExcessPaidToReduc' ) . '</a>' ;
2018-02-14 10:47:10 +01:00
}
2016-11-10 06:51:15 +01:00
// For credit note
2020-04-21 10:02:18 +02:00
if ( $object -> type == FactureFournisseur :: TYPE_CREDIT_NOTE && $object -> statut == 1 && $object -> paye == 0 && $usercancreate
2020-04-10 10:59:32 +02:00
&& ( ! empty ( $conf -> global -> SUPPLIER_INVOICE_ALLOW_REUSE_OF_CREDIT_WHEN_PARTIALLY_REFUNDED ) || $object -> getSommePaiement () == 0 )
2020-01-01 19:04:14 +01:00
) {
2021-04-06 09:15:34 +02:00
print '<a class="butAction' . ( $conf -> use_javascript_ajax ? ' reposition' : '' ) . '" href="' . $_SERVER [ " PHP_SELF " ] . '?facid=' . $object -> id . '&action=converttoreduc" title="' . dol_escape_htmltag ( $langs -> trans ( " ConfirmConvertToReducSupplier2 " )) . '">' . $langs -> trans ( 'ConvertToReduc' ) . '</a>' ;
2016-11-10 06:51:15 +01:00
}
// For deposit invoice
2021-04-06 09:15:34 +02:00
if ( $object -> type == FactureFournisseur :: TYPE_DEPOSIT && $usercancreate && $object -> statut > 0 && empty ( $discount -> id )) {
print '<a class="butAction' . ( $conf -> use_javascript_ajax ? ' reposition' : '' ) . '" href="' . $_SERVER [ " PHP_SELF " ] . '?facid=' . $object -> id . '&action=converttoreduc">' . $langs -> trans ( 'ConvertToReduc' ) . '</a>' ;
2016-11-10 06:51:15 +01:00
}
}
2021-04-06 09:15:34 +02:00
// Classify paid
2021-11-02 16:43:09 +01:00
if ( $object -> statut == FactureFournisseur :: STATUS_VALIDATED && $object -> paye == 0 && (
2021-12-30 19:03:46 +01:00
( $object -> type != FactureFournisseur :: TYPE_CREDIT_NOTE && $object -> type != FactureFournisseur :: TYPE_DEPOSIT && ( $resteapayer <= 0 || ( ! empty ( $conf -> global -> SUPPLIER_INVOICE_CAN_SET_PAID_EVEN_IF_PARTIALLY_PAID ) && $object -> total_ttc == $resteapayer ))) ||
2021-11-02 16:43:09 +01:00
( $object -> type == FactureFournisseur :: TYPE_CREDIT_NOTE && $resteapayer >= 0 ) ||
2021-12-30 19:03:46 +01:00
( $object -> type == FactureFournisseur :: TYPE_DEPOSIT && $object -> total_ttc > 0 && ( $resteapayer == 0 || ( ! empty ( $conf -> global -> SUPPLIER_INVOICE_CAN_SET_PAID_EVEN_IF_PARTIALLY_PAID ) && $object -> total_ttc == $resteapayer )))
2021-11-02 16:43:09 +01:00
)
2021-04-06 09:15:34 +02:00
) {
print '<a class="butAction' . ( $conf -> use_javascript_ajax ? ' reposition' : '' ) . '" href="' . $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id . '&action=paid">' . $langs -> trans ( 'ClassifyPaid' ) . '</a>' ;
}
// Classify 'closed not completely paid' (possible if validated and not yet filed paid)
2021-12-30 19:03:46 +01:00
if ( $object -> statut == FactureFournisseur :: STATUS_VALIDATED && $object -> paye == 0 && $resteapayer > 0 && ( empty ( $conf -> global -> SUPPLIER_INVOICE_CAN_SET_PAID_EVEN_IF_PARTIALLY_PAID ) || $object -> total_ttc != $resteapayer )) {
2022-05-22 17:09:08 +02:00
if ( $totalpaid > 0 || $totalcreditnotes > 0 ) {
2021-04-06 09:15:34 +02:00
// If one payment or one credit note was linked to this invoice
print '<a class="butAction' . ( $conf -> use_javascript_ajax ? ' reposition' : '' ) . '" href="' . $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id . '&action=paid">' . $langs -> trans ( 'ClassifyPaidPartially' ) . '</a>' ;
} else {
if ( empty ( $conf -> global -> INVOICE_CAN_NEVER_BE_CANCELED )) {
print '<a class="butAction' . ( $conf -> use_javascript_ajax ? ' reposition' : '' ) . '" href="' . $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id . '&action=canceled">' . $langs -> trans ( 'ClassifyCanceled' ) . '</a>' ;
2020-10-07 15:01:28 +02:00
}
}
}
2016-08-10 09:47:25 +02:00
2016-03-04 10:38:33 +01:00
// Create event
2017-12-07 00:28:43 +01:00
/* if ( $conf -> agenda -> enabled && ! 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.
2016-03-04 10:38:33 +01:00
{
print '<div class="inline-block divButAction"><a class="butAction" href="' . DOL_URL_ROOT . '/comm/action/card.php?action=create&origin=' . $object -> element . '&originid=' . $object -> id . '&socid=' . $object -> socid . '">' . $langs -> trans ( " AddAction " ) . '</a></div>' ;
2017-12-07 00:28:43 +01:00
} */
2016-08-10 09:47:25 +02:00
2016-10-02 21:39:24 +02:00
// Create a credit note
2021-02-25 23:21:30 +01:00
if (( $object -> type == FactureFournisseur :: TYPE_STANDARD || $object -> type == FactureFournisseur :: TYPE_DEPOSIT ) && $object -> statut > 0 && $usercancreate ) {
if ( ! $objectidnext ) {
2021-04-06 09:15:34 +02:00
print '<a class="butAction" href="' . $_SERVER [ 'PHP_SELF' ] . '?socid=' . $object -> socid . '&fac_avoir=' . $object -> id . '&action=create&type=2' . ( $object -> fk_project > 0 ? '&projectid=' . $object -> fk_project : '' ) . '">' . $langs -> trans ( " CreateCreditNote " ) . '</a>' ;
2016-10-02 21:39:24 +02:00
}
}
2021-04-06 09:15:34 +02:00
// Clone
if ( $action != 'edit' && $usercancreate ) {
print '<a class="butAction" href="' . $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id . '&action=clone&socid=' . $object -> socid . '">' . $langs -> trans ( 'ToClone' ) . '</a>' ;
}
2022-02-07 14:34:27 +01:00
// Clone as predefined / Create template
if (( $object -> type == FactureFournisseur :: TYPE_STANDARD || $object -> type == FactureFournisseur :: TYPE_DEPOSIT ) && $object -> statut == 0 && $usercancreate ) {
if ( ! $objectidnext && count ( $object -> lines ) > 0 ) {
print '<a class="butAction" href="' . DOL_URL_ROOT . '/fourn/facture/card-rec.php?facid=' . $object -> id . '&action=create">' . $langs -> trans ( " ChangeIntoRepeatableInvoice " ) . '</a>' ;
}
}
2020-10-07 15:01:28 +02:00
// Delete
2019-11-08 10:53:31 +01:00
$isErasable = $object -> is_erasable ();
2021-02-25 23:21:30 +01:00
if ( $action != 'confirm_edit' && ( $user -> rights -> fournisseur -> facture -> supprimer || ( $usercancreate && $isErasable == 1 ))) { // isErasable = 1 means draft with temporary ref (draft can always be deleted with no need of permissions)
2020-10-07 15:01:28 +02:00
//var_dump($isErasable);
if ( $isErasable == - 4 ) {
2021-04-06 09:15:34 +02:00
print '<a class="butActionRefused classfortooltip" href="#" title="' . $langs -> trans ( " DisabledBecausePayments " ) . '">' . $langs -> trans ( 'Delete' ) . '</a>' ;
2020-10-07 15:01:28 +02:00
} elseif ( $isErasable == - 3 ) { // Should never happen with supplier invoice
2021-04-06 09:15:34 +02:00
print '<a class="butActionRefused classfortooltip" href="#" title="' . $langs -> trans ( " DisabledBecauseNotLastSituationInvoice " ) . '">' . $langs -> trans ( 'Delete' ) . '</a>' ;
2020-10-07 15:01:28 +02:00
} elseif ( $isErasable == - 2 ) { // Should never happen with supplier invoice
2021-04-06 09:15:34 +02:00
print '<a class="butActionRefused classfortooltip" href="#" title="' . $langs -> trans ( " DisabledBecauseNotLastInvoice " ) . '">' . $langs -> trans ( 'Delete' ) . '</a>' ;
2020-10-07 15:01:28 +02:00
} elseif ( $isErasable == - 1 ) {
2021-04-06 09:15:34 +02:00
print '<a class="butActionRefused classfortooltip" href="#" title="' . $langs -> trans ( " DisabledBecauseDispatchedInBookkeeping " ) . '">' . $langs -> trans ( 'Delete' ) . '</a>' ;
2021-02-25 23:21:30 +01:00
} elseif ( $isErasable <= 0 ) { // Any other cases
2021-04-06 09:15:34 +02:00
print '<a class="butActionRefused classfortooltip" href="#" title="' . $langs -> trans ( " DisabledBecauseNotErasable " ) . '">' . $langs -> trans ( 'Delete' ) . '</a>' ;
2020-10-07 15:01:28 +02:00
} else {
2021-09-18 22:04:41 +02:00
print '<a class="butActionDelete' . ( $conf -> use_javascript_ajax ? ' reposition' : '' ) . '" href="' . $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id . '&action=delete&token=' . newToken () . '">' . $langs -> trans ( 'Delete' ) . '</a>' ;
2020-10-07 15:01:28 +02:00
}
}
print '</div>' ;
2021-02-25 23:21:30 +01:00
if ( $action != 'confirm_edit' ) {
2015-07-31 11:06:25 +02:00
print '<div class="fichecenter"><div class="fichehalfleft">' ;
2016-08-10 09:47:25 +02:00
2016-08-28 02:40:20 +02:00
/*
2021-03-16 04:04:33 +01:00
* Generated documents
2021-02-25 23:21:30 +01:00
*/
2019-11-08 10:53:31 +01:00
$ref = dol_sanitizeFileName ( $object -> ref );
2017-10-16 08:47:05 +02:00
$subdir = get_exdir ( $object -> id , 2 , 0 , 0 , $object , 'invoice_supplier' ) . $ref ;
$filedir = $conf -> fournisseur -> facture -> dir_output . '/' . $subdir ;
2019-11-08 10:53:31 +01:00
$urlsource = $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id ;
2020-04-21 10:02:18 +02:00
$genallowed = $usercanread ;
$delallowed = $usercancreate ;
2020-09-10 01:49:09 +02:00
$modelpdf = ( ! empty ( $object -> model_pdf ) ? $object -> model_pdf : ( empty ( $conf -> global -> INVOICE_SUPPLIER_ADDON_PDF ) ? '' : $conf -> global -> INVOICE_SUPPLIER_ADDON_PDF ));
2017-10-16 08:47:05 +02:00
2019-01-27 11:55:16 +01:00
print $formfile -> showdocuments ( 'facture_fournisseur' , $subdir , $filedir , $urlsource , $genallowed , $delallowed , $modelpdf , 1 , 0 , 0 , 40 , 0 , '' , '' , '' , $societe -> default_lang );
2019-11-08 10:53:31 +01:00
$somethingshown = $formfile -> numoffiles ;
2017-10-16 08:47:05 +02:00
// Show links to link elements
$linktoelem = $form -> showLinkToObjectBlock ( $object , null , array ( 'invoice_supplier' ));
$somethingshown = $form -> showLinkedObjectBlock ( $object , $linktoelem );
2021-10-24 00:37:37 +02:00
print '</div><div class="fichehalfright">' ;
2017-10-16 08:47:05 +02:00
// List of actions on element
include_once DOL_DOCUMENT_ROOT . '/core/class/html.formactions.class.php' ;
2019-11-08 10:53:31 +01:00
$formactions = new FormActions ( $db );
$somethingshown = $formactions -> showactions ( $object , 'invoice_supplier' , $socid , 1 , 'listaction' . ( $genallowed ? 'largetitle' : '' ));
2016-08-10 09:47:25 +02:00
2021-10-24 00:37:37 +02:00
print '</div></div>' ;
2017-10-16 08:47:05 +02:00
}
2013-01-06 13:33:09 +01:00
}
2017-10-16 08:47:05 +02:00
}
2012-10-28 13:57:21 +01:00
2017-10-16 08:47:05 +02:00
// Select mail models is same action as presend
if ( GETPOST ( 'modelselected' )) {
$action = 'presend' ;
}
2012-10-28 13:57:21 +01:00
2017-10-16 08:47:05 +02:00
// Presend form
2019-11-08 10:53:31 +01:00
$modelmail = 'invoice_supplier_send' ;
$defaulttopic = 'SendBillRef' ;
2017-10-16 08:47:05 +02:00
$diroutput = $conf -> fournisseur -> facture -> dir_output ;
2019-11-08 10:53:31 +01:00
$autocopy = 'MAIN_MAIL_AUTOCOPY_SUPPLIER_INVOICE_TO' ;
2020-11-08 14:39:20 +01:00
$trackid = 'sinv' . $object -> id ;
2012-10-28 13:57:21 +01:00
2017-10-16 08:47:05 +02:00
include DOL_DOCUMENT_ROOT . '/core/tpl/card_presend.tpl.php' ;
}
2012-10-28 13:57:21 +01:00
}
2019-02-21 09:34:05 +01:00
2012-10-28 13:57:21 +01:00
// End of page
llxFooter ();
$db -> close ();