actions_card_common.class.php 17 KB

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