actions_card_common.class.php 17 KB

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