1<?php
2/* Copyright (C) 2012 Regis Houssin  <regis.houssin@inodbox.com>
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
18/**
19 *       \file       htdocs/core/class/commonincoterm.class.php
20 *       \ingroup    core
21 *       \brief      File of the superclass of object classes that support incoterm (customer and supplier)
22 */
23
24
25/**
26 *      Superclass for incoterm classes
27 */
28trait CommonIncoterm
29{
30	/**
31	 * @var int		ID incoterm.
32	 * @see setIncoterms()
33	 */
34	public $fk_incoterms;
35
36	/**
37	 * @var string	Label of incoterm. Used for tooltip.
38	 * @see SetIncoterms()
39	 */
40	public $label_incoterms;
41
42	/**
43	 * @var string	Location of incoterm.
44	 * @see display_incoterms()
45	 */
46	public $location_incoterms;
47
48
49	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
50	/**
51	 *    Return incoterms informations
52	 *    TODO Use a cache for label get
53	 *
54	 *    @return	string	incoterms info
55	 */
56	public function display_incoterms()
57	{
58		// phpcs:enable
59		$out = '';
60
61		$this->label_incoterms = '';
62		if (!empty($this->fk_incoterms))
63		{
64			$sql = 'SELECT code FROM '.MAIN_DB_PREFIX.'c_incoterms WHERE rowid = '.(int) $this->fk_incoterms;
65			$result = $this->db->query($sql);
66			if ($result)
67			{
68				$res = $this->db->fetch_object($result);
69				$out .= $res->code;
70			}
71		}
72
73		$out .= (($out && $this->location_incoterms) ? ' - ' : '').$this->location_incoterms;
74
75		return $out;
76	}
77
78	/**
79	 *    Return incoterms informations for pdf display
80	 *
81	 *    @return	string		incoterms info
82	 */
83	public function getIncotermsForPDF()
84	{
85		$sql = 'SELECT code FROM '.MAIN_DB_PREFIX.'c_incoterms WHERE rowid = '.(int) $this->fk_incoterms;
86		$resql = $this->db->query($sql);
87		if ($resql)
88		{
89			$num = $this->db->num_rows($resql);
90			if ($num > 0)
91			{
92				$res = $this->db->fetch_object($resql);
93				return 'Incoterm : '.$res->code.' - '.$this->location_incoterms;
94			} else {
95				return '';
96			}
97		} else {
98			$this->errors[] = $this->db->lasterror();
99			return false;
100		}
101	}
102
103	/**
104	 *    Define incoterms values of current object
105	 *
106	 *    @param	int		$id_incoterm     Id of incoterm to set or '' to remove
107	 * 	  @param 	string  $location		 location of incoterm
108	 *    @return	int     				<0 if KO, >0 if OK
109	 */
110	public function setIncoterms($id_incoterm, $location)
111	{
112		if ($this->id && $this->table_element)
113		{
114			$sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
115			$sql .= " SET fk_incoterms = ".($id_incoterm > 0 ? $id_incoterm : "null");
116			$sql .= ", location_incoterms = ".($id_incoterm > 0 ? "'".$this->db->escape($location)."'" : "null");
117			$sql .= " WHERE rowid = ".$this->id;
118			dol_syslog(get_class($this).'::setIncoterms', LOG_DEBUG);
119			$resql = $this->db->query($sql);
120			if ($resql)
121			{
122				$this->fk_incoterms = $id_incoterm;
123				$this->location_incoterms = $location;
124
125				$sql = 'SELECT libelle as label_incotermsFROM '.MAIN_DB_PREFIX.'c_incoterms WHERE rowid = '.(int) $this->fk_incoterms;
126				$res = $this->db->query($sql);
127				if ($res)
128				{
129					$obj = $this->db->fetch_object($res);
130					$this->label_incoterms = $obj->label_incoterms;
131				}
132				return 1;
133			} else {
134				$this->errors[] = $this->db->lasterror();
135				return -1;
136			}
137		} else {
138			return -1;
139		}
140	}
141}
142