1<?php
2/* Copyright (C) 2010-2012	Regis Houssin		<regis.houssin@inodbox.com>
3 * Copyright (C) 2011-2012	Laurent Destailleur	<eldy@users.sourceforge.net>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19/**
20 *	\file       htdocs/societe/canvas/actions_card_common.class.php
21 *	\ingroup    thirdparty
22 *	\brief      Fichier de la classe Thirdparty card controller (common)
23 */
24
25/**
26 *	Classe permettant la gestion des tiers par defaut
27 */
28abstract class ActionsCardCommon
29{
30	/**
31	 * @var DoliDB Database handler.
32	 */
33	public $db;
34
35	public $dirmodule;
36	public $targetmodule;
37	public $canvas;
38	public $card;
39
40	//! Template container
41	public $tpl = array();
42	//! Object container
43	public $object;
44
45	/**
46	 * @var string Error code (or message)
47	 */
48	public $error = '';
49
50
51	/**
52	 * @var string[] Error codes (or messages)
53	 */
54	public $errors = array();
55
56
57	/**
58	 *  Get object from id or ref and save it into this->object
59	 *
60	 *  @param		int		$id			Object id
61	 *  @param		string	$ref		Object ref
62	 *  @return		object				Object loaded
63	 */
64	protected function getObject($id, $ref = '')
65	{
66		//$ret = $this->getInstanceDao();
67
68		$object = new Societe($this->db);
69		if (!empty($id) || !empty($ref)) {
70			$object->fetch($id, $ref);
71		}
72		$this->object = $object;
73	}
74
75	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
76	/**
77	 *    Assign custom values for canvas (for example into this->tpl to be used by templates)
78	 *
79	 *    @param	string	$action    Type of action
80	 *    @param	integer	$id			Id of object
81	 *    @param	string	$ref		Ref of object
82	 *    @return	void
83	 */
84	public function assign_values(&$action, $id = 0, $ref = '')
85	{
86		// phpcs:enable
87		global $conf, $langs, $db, $user, $mysoc, $canvas;
88		global $form, $formadmin, $formcompany;
89
90		if ($action == 'add' || $action == 'update') {
91			$this->assign_post($action);
92		}
93
94		if ($_GET["type"] == 'f') {
95			$this->object->fournisseur = 1;
96		}
97		if ($_GET["type"] == 'c') {
98			$this->object->client = 1;
99		}
100		if ($_GET["type"] == 'p') {
101			$this->object->client = 2;
102		}
103		if ($_GET["type"] == 'cp') {
104			$this->object->client = 3;
105		}
106		if ($_REQUEST["private"] == 1) {
107			$this->object->particulier = 1;
108		}
109
110		foreach ($this->object as $key => $value) {
111			$this->tpl[$key] = $value;
112		}
113
114		$this->tpl['error'] = get_htmloutput_errors($this->object->error, $this->object->errors);
115		if (is_array($GLOBALS['errors'])) {
116			$this->tpl['error'] = get_htmloutput_mesg('', $GLOBALS['errors'], 'error');
117		}
118
119		if ($action == 'create') {
120			if ($conf->use_javascript_ajax) {
121				$this->tpl['ajax_selecttype'] = "\n".'<script type="text/javascript" language="javascript">
122				$(document).ready(function () {
123		              $("#radiocompany").click(function() {
124                            document.formsoc.action.value="create";
125                            document.formsoc.canvas.value="company";
126                            document.formsoc.private.value=0;
127                            document.formsoc.submit();
128		              });
129		               $("#radioprivate").click(function() {
130                            document.formsoc.action.value="create";
131                            document.formsoc.canvas.value="individual";
132                            document.formsoc.private.value=1;
133                            document.formsoc.submit();
134                      });
135		          });
136                </script>'."\n";
137			}
138		}
139
140		if ($action == 'create' || $action == 'edit') {
141			if ($conf->use_javascript_ajax) {
142				$this->tpl['ajax_selectcountry'] = "\n".'<script type="text/javascript" language="javascript">
143				$(document).ready(function () {
144						$("#selectcountry_id").change(function() {
145							document.formsoc.action.value="'.$action.'";
146							document.formsoc.canvas.value="'.$canvas.'";
147							document.formsoc.submit();
148						});
149					})
150				</script>'."\n";
151			}
152
153			// Load object modCodeClient
154			$module = (!empty($conf->global->SOCIETE_CODECLIENT_ADDON) ? $conf->global->SOCIETE_CODECLIENT_ADDON : 'mod_codeclient_leopard');
155			if (substr($module, 0, 15) == 'mod_codeclient_' && substr($module, -3) == 'php') {
156				$module = substr($module, 0, dol_strlen($module) - 4);
157			}
158			$dirsociete = array_merge(array('/core/modules/societe/'), $conf->modules_parts['societe']);
159			foreach ($dirsociete as $dirroot) {
160				$res = dol_include_once($dirroot.$module.'.php');
161				if ($res) {
162					break;
163				}
164			}
165			$modCodeClient = new $module($db);
166			$this->tpl['auto_customercode'] = $modCodeClient->code_auto;
167			// We verified if the tag prefix is used
168			if ($modCodeClient->code_auto) {
169				$this->tpl['prefix_customercode'] = $modCodeClient->verif_prefixIsUsed();
170			}
171
172			// TODO create a function
173			$this->tpl['select_customertype'] = Form::selectarray('client', array(
174				0 => $langs->trans('NorProspectNorCustomer'),
175				1 => $langs->trans('Customer'),
176				2 => $langs->trans('Prospect'),
177				3 => $langs->trans('ProspectCustomer')
178			), $this->object->client);
179
180			// Customer
181			$this->tpl['customercode'] = $this->object->code_client;
182			if ((!$this->object->code_client || $this->object->code_client == -1) && $modCodeClient->code_auto) {
183				$this->tpl['customercode'] = $modCodeClient->getNextValue($this->object, 0);
184			}
185			$this->tpl['ismodifiable_customercode'] = $this->object->codeclient_modifiable();
186			$s = $modCodeClient->getToolTip($langs, $this->object, 0);
187			$this->tpl['help_customercode'] = $form->textwithpicto('', $s, 1);
188
189			if ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || !empty($conf->supplier_order->enabled) || !empty($conf->supplier_invoice->enabled)) {
190				$this->tpl['supplier_enabled'] = 1;
191
192				// Load object modCodeFournisseur
193				$module = $conf->global->SOCIETE_CODECLIENT_ADDON;
194				if (substr($module, 0, 15) == 'mod_codeclient_' && substr($module, -3) == 'php') {
195					$module = substr($module, 0, dol_strlen($module) - 4);
196				}
197				$dirsociete = array_merge(array('/core/modules/societe/'), $conf->modules_parts['societe']);
198				foreach ($dirsociete as $dirroot) {
199					$res = dol_include_once($dirroot.$module.'.php');
200					if ($res) {
201						break;
202					}
203				}
204				$modCodeFournisseur = new $module;
205				$this->tpl['auto_suppliercode'] = $modCodeFournisseur->code_auto;
206				// We verified if the tag prefix is used
207				if ($modCodeFournisseur->code_auto) {
208					$this->tpl['prefix_suppliercode'] = $modCodeFournisseur->verif_prefixIsUsed();
209				}
210
211				// Supplier
212				$this->tpl['yn_supplier'] = $form->selectyesno("fournisseur", $this->object->fournisseur, 1);
213				$this->tpl['suppliercode'] = $this->object->code_fournisseur;
214				if ((!$this->object->code_fournisseur || $this->object->code_fournisseur == -1) && $modCodeFournisseur->code_auto) {
215					$this->tpl['suppliercode'] = $modCodeFournisseur->getNextValue($this->object, 1);
216				}
217				$this->tpl['ismodifiable_suppliercode'] = $this->object->codefournisseur_modifiable();
218				$s = $modCodeFournisseur->getToolTip($langs, $this->object, 1);
219				$this->tpl['help_suppliercode'] = $form->textwithpicto('', $s, 1);
220
221				$this->object->LoadSupplierCateg();
222				$this->tpl['suppliercategory'] = $this->object->SupplierCategories;
223			}
224
225			// Zip
226			$this->tpl['select_zip'] = $formcompany->select_ziptown($this->object->zip, 'zipcode', array('town', 'selectcountry_id', 'state_id'), 6);
227
228			// Town
229			$this->tpl['select_town'] = $formcompany->select_ziptown($this->object->town, 'town', array('zipcode', 'selectcountry_id', 'state_id'));
230
231			// Country
232			$this->object->country_id = ($this->object->country_id ? $this->object->country_id : $mysoc->country_id);
233			$this->object->country_code = ($this->object->country_code ? $this->object->country_code : $mysoc->country_code);
234			$this->tpl['select_country'] = $form->select_country($this->object->country_id, 'country_id');
235			$countrynotdefined = $langs->trans("ErrorSetACountryFirst").' ('.$langs->trans("SeeAbove").')';
236
237			if ($user->admin) {
238				$this->tpl['info_admin'] = info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
239			}
240
241			// State
242			if ($this->object->country_id) {
243				$this->tpl['select_state'] = $formcompany->select_state($this->object->state_id, $this->object->country_code);
244			} else {
245				$this->tpl['select_state'] = $countrynotdefined;
246			}
247
248			// Language
249			if (!empty($conf->global->MAIN_MULTILANGS)) {
250				$this->tpl['select_lang'] = $formadmin->select_language(($this->object->default_lang ? $this->object->default_lang : $conf->global->MAIN_LANG_DEFAULT), 'default_lang', 0, 0, 1);
251			}
252
253			// VAT
254			$this->tpl['yn_assujtva'] = $form->selectyesno('assujtva_value', $this->tpl['tva_assuj'], 1); // Assujeti par defaut en creation
255
256			// Select users
257			$this->tpl['select_users'] = $form->select_dolusers($this->object->commercial_id, 'commercial_id', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'maxwidth300');
258
259			// Local Tax
260			// TODO mettre dans une classe propre au pays
261			if ($mysoc->country_code == 'ES') {
262				$this->tpl['localtax'] = '';
263
264				if ($mysoc->localtax1_assuj == "1" && $mysoc->localtax2_assuj == "1") {
265					$this->tpl['localtax'] .= '<tr><td>'.$langs->trans("LocalTax1IsUsedES").'</td><td>';
266					$this->tpl['localtax'] .= $form->selectyesno('localtax1assuj_value', $this->object->localtax1_assuj, 1);
267					$this->tpl['localtax'] .= '</td><td>'.$langs->trans("LocalTax2IsUsedES").'</td><td>';
268					$this->tpl['localtax'] .= $form->selectyesno('localtax2assuj_value', $this->object->localtax1_assuj, 1);
269					$this->tpl['localtax'] .= '</td></tr>';
270				} elseif ($mysoc->localtax1_assuj == "1") {
271					$this->tpl['localtax'] .= '<tr><td>'.$langs->trans("LocalTax1IsUsedES").'</td><td colspan="3">';
272					$this->tpl['localtax'] .= $form->selectyesno('localtax1assuj_value', $this->object->localtax1_assuj, 1);
273					$this->tpl['localtax'] .= '</td><tr>';
274				} elseif ($mysoc->localtax2_assuj == "1") {
275					$this->tpl['localtax'] .= '<tr><td>'.$langs->trans("LocalTax2IsUsedES").'</td><td colspan="3">';
276					$this->tpl['localtax'] .= $form->selectyesno('localtax2assuj_value', $this->object->localtax1_assuj, 1);
277					$this->tpl['localtax'] .= '</td><tr>';
278				}
279			}
280		} else {
281			$head = societe_prepare_head($this->object);
282
283			$this->tpl['showhead'] = dol_get_fiche_head($head, 'card', '', 0, 'company');
284			$this->tpl['showend'] = dol_get_fiche_end();
285
286			$this->tpl['showrefnav'] = $form->showrefnav($this->object, 'socid', '', ($user->socid ? 0 : 1), 'rowid', 'nom');
287
288			$this->tpl['checkcustomercode'] = $this->object->check_codeclient();
289			$this->tpl['checksuppliercode'] = $this->object->check_codefournisseur();
290			$this->tpl['address'] = dol_nl2br($this->object->address);
291
292			$img = picto_from_langcode($this->object->country_code);
293			if ($this->object->isInEEC()) {
294				$this->tpl['country'] = $form->textwithpicto(($img ? $img.' ' : '').$this->object->country, $langs->trans("CountryIsInEEC"), 1, 0);
295			}
296			$this->tpl['country'] = ($img ? $img.' ' : '').$this->object->country;
297
298			$this->tpl['phone'] 	= dol_print_phone($this->object->phone, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
299			$this->tpl['fax'] 		= dol_print_phone($this->object->fax, $this->object->country_code, 0, $this->object->id, 'AC_FAX');
300			$this->tpl['email'] 	= dol_print_email($this->object->email, 0, $this->object->id, 'AC_EMAIL');
301			$this->tpl['url'] 		= dol_print_url($this->object->url);
302
303			$this->tpl['tva_assuj'] = yn($this->object->tva_assuj);
304
305			// Third party type
306			$arr = $formcompany->typent_array(1);
307			$this->tpl['typent'] = $arr[$this->object->typent_code];
308
309			if (!empty($conf->global->MAIN_MULTILANGS)) {
310				require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
311				//$s=picto_from_langcode($this->default_lang);
312				//print ($s?$s.' ':'');
313				$langs->load("languages");
314				$this->tpl['default_lang'] = ($this->default_lang ? $langs->trans('Language_'.$this->object->default_lang) : '');
315			}
316
317			$this->tpl['image_edit'] = img_edit();
318
319			$this->tpl['display_rib'] = $this->object->display_rib();
320
321			// Sales representatives
322			$this->tpl['sales_representatives'] = '';
323			$listsalesrepresentatives = $this->object->getSalesRepresentatives($user);
324			$nbofsalesrepresentative = count($listsalesrepresentatives);
325			if ($nbofsalesrepresentative > 3) {   // We print only number
326				$this->tpl['sales_representatives'] .= $nbofsalesrepresentative;
327			} elseif ($nbofsalesrepresentative > 0) {
328				$userstatic = new User($this->db);
329				$i = 0;
330				foreach ($listsalesrepresentatives as $val) {
331					$userstatic->id = $val['id'];
332					$userstatic->lastname = $val['name'];
333					$userstatic->firstname = $val['firstname'];
334					$this->tpl['sales_representatives'] .= $userstatic->getNomUrl(1);
335					$i++;
336					if ($i < $nbofsalesrepresentative) {
337						$this->tpl['sales_representatives'] .= ', ';
338					}
339				}
340			} else {
341				$this->tpl['sales_representatives'] .= $langs->trans("NoSalesRepresentativeAffected");
342			}
343
344			// Linked member
345			if (!empty($conf->adherent->enabled)) {
346				$langs->load("members");
347				$adh = new Adherent($this->db);
348				$result = $adh->fetch('', '', $this->object->id);
349				if ($result > 0) {
350					$adh->ref = $adh->getFullName($langs);
351					$this->tpl['linked_member'] = $adh->getNomUrl(1);
352				} else {
353					$this->tpl['linked_member'] = $langs->trans("ThirdpartyNotLinkedToMember");
354				}
355			}
356
357			// Local Tax
358			// TODO mettre dans une classe propre au pays
359			if ($mysoc->country_code == 'ES') {
360				$this->tpl['localtax'] = '';
361
362				if ($mysoc->localtax1_assuj == "1" && $mysoc->localtax2_assuj == "1") {
363					$this->tpl['localtax'] .= '<tr><td>'.$langs->trans("LocalTax1IsUsedES").'</td>';
364					$this->tpl['localtax'] .= '<td>'.yn($this->object->localtax1_assuj).'</td>';
365					$this->tpl['localtax'] .= '<td>'.$langs->trans("LocalTax2IsUsedES").'</td>';
366					$this->tpl['localtax'] .= '<td>'.yn($this->object->localtax2_assuj).'</td></tr>';
367				} elseif ($mysoc->localtax1_assuj == "1") {
368					$this->tpl['localtax'] .= '<tr><td>'.$langs->trans("LocalTax1IsUsedES").'</td>';
369					$this->tpl['localtax'] .= '<td colspan="3">'.yn($this->object->localtax1_assuj).'</td></tr>';
370				} elseif ($mysoc->localtax2_assuj == "1") {
371					$this->tpl['localtax'] .= '<tr><td>'.$langs->trans("LocalTax2IsUsedES").'</td>';
372					$this->tpl['localtax'] .= '<td colspan="3">'.yn($this->object->localtax2_assuj).'</td></tr>';
373				}
374			}
375		}
376	}
377
378	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
379	/**
380	 *  Assign POST values into object
381	 *
382	 *	@param		string		$action		Action string
383	 *  @return		string					HTML output
384	 */
385	private function assign_post($action)
386	{
387		// phpcs:enable
388		global $langs, $mysoc;
389
390		$this->object->id = GETPOST("socid");
391		$this->object->name = GETPOST("nom");
392		$this->object->prefix_comm			= GETPOST("prefix_comm");
393		$this->object->client = GETPOST("client");
394		$this->object->code_client			= GETPOST("code_client");
395		$this->object->fournisseur			= GETPOST("fournisseur");
396		$this->object->code_fournisseur = GETPOST("code_fournisseur");
397		$this->object->address = GETPOST("adresse");
398		$this->object->zip = GETPOST("zipcode");
399		$this->object->town					= GETPOST("town");
400		$this->object->country_id = GETPOST("country_id") ? GETPOST("country_id") : $mysoc->country_id;
401		$this->object->state_id = GETPOST("state_id");
402		$this->object->phone				= GETPOST("tel");
403		$this->object->fax					= GETPOST("fax");
404		$this->object->email				= GETPOST("email", 'alphawithlgt');
405		$this->object->url					= GETPOST("url");
406		$this->object->capital				= GETPOST("capital");
407		$this->object->idprof1				= GETPOST("idprof1");
408		$this->object->idprof2				= GETPOST("idprof2");
409		$this->object->idprof3				= GETPOST("idprof3");
410		$this->object->idprof4				= GETPOST("idprof4");
411		$this->object->typent_id = GETPOST("typent_id");
412		$this->object->effectif_id = GETPOST("effectif_id");
413		$this->object->barcode				= GETPOST("barcode");
414		$this->object->forme_juridique_code = GETPOST("forme_juridique_code");
415		$this->object->default_lang			= GETPOST("default_lang");
416		$this->object->commercial_id		= GETPOST("commercial_id");
417
418		$this->object->tva_assuj = GETPOST("assujtva_value") ? GETPOST("assujtva_value") : 1;
419		$this->object->tva_intra = GETPOST("tva_intra");
420
421		//Local Taxes
422		$this->object->localtax1_assuj		= GETPOST("localtax1assuj_value");
423		$this->object->localtax2_assuj		= GETPOST("localtax2assuj_value");
424
425		// We set country_id, and country_code label of the chosen country
426		if ($this->object->country_id) {
427			$tmparray = getCountry($this->object->country_id, 'all', $this->db, $langs, 0);
428			$this->object->country_code = $tmparray['code'];
429			$this->object->country_label = $tmparray['label'];
430		}
431	}
432}
433