1<?php
2/* Copyright (C) 2018		ATM Consulting		<support@atm-consulting.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Needs the following variables defined:
18 * $object					Proposal, order, invoice (including supplier versions)
19 * $thirdparty				Thirdparty of object
20 * $absolute_discount		Amount of fixed discounts available
21 * $absolute_creditnote		Amount of credit notes available
22 * $discount_type			0 => Customer discounts, 1 => Supplier discounts
23 * $cannotApplyDiscount		Set it to prevent form to apply discount
24 * $backtopage				URL to come back to from discount modification pages
25 */
26
27print '<!-- BEGIN object_discounts.tpl.php -->'."\n";
28
29$objclassname = get_class($object);
30$isInvoice = in_array($object->element, array('facture', 'invoice', 'facture_fourn', 'invoice_supplier'));
31$isNewObject = empty($object->id) && empty($object->rowid);
32
33// Relative and absolute discounts
34$addrelativediscount = '<a href="'.DOL_URL_ROOT.'/comm/remise.php?id='.$thirdparty->id.'&backtopage='.$backtopage.'">'.$langs->trans("EditRelativeDiscount").'</a>';
35$addabsolutediscount = '<a href="'.DOL_URL_ROOT.'/comm/remx.php?id='.$thirdparty->id.'&backtopage='.$backtopage.'">'.$langs->trans("EditGlobalDiscounts").'</a>';
36$viewabsolutediscount = '<a href="'.DOL_URL_ROOT.'/comm/remx.php?id='.$thirdparty->id.'&backtopage='.$backtopage.'">'.$langs->trans("ViewAvailableGlobalDiscounts").'</a>';
37
38$fixedDiscount = $thirdparty->remise_percent;
39if (!empty($discount_type)) {
40	$fixedDiscount = $thirdparty->remise_supplier_percent;
41}
42
43if ($fixedDiscount > 0) {
44	$translationKey = (!empty($discount_type)) ? 'HasRelativeDiscountFromSupplier' : 'CompanyHasRelativeDiscount';
45	print $langs->trans($translationKey, $fixedDiscount).'.';
46} else {
47	$translationKey = (!empty($discount_type)) ? 'HasNoRelativeDiscountFromSupplier' : 'CompanyHasNoRelativeDiscount';
48	print '<span class="opacitymedium">'.$langs->trans($translationKey).'.</span>';
49}
50if ($isNewObject) {
51	print ' ('.$addrelativediscount.')';
52}
53
54// Is there is commercial discount or down payment available ?
55if ($absolute_discount > 0) {
56	if (!empty($cannotApplyDiscount) || !$isInvoice || $isNewObject || $object->statut > $objclassname::STATUS_DRAFT || $object->type == $objclassname::TYPE_CREDIT_NOTE || $object->type == $objclassname::TYPE_DEPOSIT) {
57		$translationKey = !empty($discount_type) ? 'HasAbsoluteDiscountFromSupplier' : 'CompanyHasAbsoluteDiscount';
58		$text = $langs->trans($translationKey, price($absolute_discount), $langs->transnoentities("Currency".$conf->currency)).'.';
59
60		if ($isInvoice && !$isNewObject && $object->statut > $objclassname::STATUS_DRAFT && $object->type != $objclassname::TYPE_CREDIT_NOTE && $object->type != $objclassname::TYPE_DEPOSIT) {
61			$text = $form->textwithpicto($text, $langs->trans('AbsoluteDiscountUse'));
62		}
63
64		if ($isNewObject) {
65			$text .= ' ('.$addabsolutediscount.')';
66		}
67
68		if ($isNewObject) {
69			print '<br>'.$text;
70		} else {
71			print '<div class="inline-block clearboth">'.$text.'</div>';
72		}
73	} else {
74		// Discount available of type fixed amount (not credit note)
75		$more = '('.$addabsolutediscount.')';
76		$form->form_remise_dispo($_SERVER["PHP_SELF"].'?facid='.$object->id, GETPOST('discountid'), 'remise_id', $thirdparty->id, $absolute_discount, $filterabsolutediscount, $resteapayer, $more, 0, $discount_type);
77	}
78}
79
80// Is there credit notes availables ?
81if ($absolute_creditnote > 0) {
82	// If validated, we show link "add credit note to payment"
83	if ($cannotApplyDiscount || !$isInvoice || $isNewObject || $object->statut != $objclassname::STATUS_VALIDATED || $object->type == $objclassname::TYPE_CREDIT_NOTE) {
84		$translationKey = !empty($discount_type) ? 'HasCreditNoteFromSupplier' : 'CompanyHasCreditNote';
85		$text = $langs->trans($translationKey, price($absolute_creditnote), $langs->transnoentities("Currency".$conf->currency)).'.';
86
87		if ($isInvoice && !$isNewObject && $object->statut == $objclassname::STATUS_DRAFT && $object->type != $objclassname::TYPE_DEPOSIT) {
88			$text = $form->textwithpicto($text, $langs->trans('CreditNoteDepositUse'));
89		}
90
91		if ($absolute_discount <= 0 || $isNewObject) {
92			$text .= ' ('.$addabsolutediscount.')';
93		}
94
95		if ($isNewObject) {
96			print '<br>'.$text;
97		} else {
98			print '<div class="inline-block clearboth">'.$text.'</div>';
99		}
100	} else {  // We can add a credit note on a down payment or standard invoice or situation invoice
101		// There is credit notes discounts available
102		$more = $isInvoice && !$isNewObject ? ' ('.$viewabsolutediscount.')' : '';
103		$form->form_remise_dispo($_SERVER["PHP_SELF"].'?facid='.$object->id, 0, 'remise_id_for_payment', $thirdparty->id, $absolute_creditnote, $filtercreditnote, 0, $more, 0, $discount_type); // We allow credit note even if amount is higher
104	}
105}
106
107if ($absolute_discount <= 0 && $absolute_creditnote <= 0) {
108	$translationKey = !empty($discount_type) ? 'HasNoAbsoluteDiscountFromSupplier' : 'CompanyHasNoAbsoluteDiscount';
109	print '<br><span class="opacitymedium">'.$langs->trans($translationKey).'.</span>';
110
111	if ($isInvoice && $object->statut == $objclassname::STATUS_DRAFT && $object->type != $objclassname::TYPE_CREDIT_NOTE && $object->type != $objclassname::TYPE_DEPOSIT) {
112		print ' ('.$addabsolutediscount.')';
113	}
114}
115
116print '<!-- END template -->';
117