actions_card_service.class.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /* Copyright (C) 2010-2018 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. * \file htdocs/product/canvas/service/actions_card_service.class.php
  19. * \ingroup service
  20. * \brief File with class of actions for canvas service
  21. */
  22. include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  23. /**
  24. * Class with controller methods for service canvas
  25. */
  26. class ActionsCardService
  27. {
  28. /**
  29. * @var DoliDB Database handler.
  30. */
  31. public $db;
  32. public $dirmodule;
  33. public $module;
  34. public $label;
  35. public $price_base_type;
  36. public $accountancy_code_sell;
  37. public $accountancy_code_buy;
  38. public $targetmodule;
  39. public $canvas;
  40. public $card;
  41. public $name;
  42. public $definition;
  43. public $fieldListName;
  44. public $next_prev_filter;
  45. //! Object container
  46. public $object;
  47. //! Template container
  48. public $tpl = array();
  49. // List of fiels for action=list
  50. public $field_list = array();
  51. public $id;
  52. public $ref;
  53. public $description;
  54. public $note;
  55. public $price;
  56. public $price_min;
  57. /**
  58. * @var string Error code (or message)
  59. */
  60. public $error = '';
  61. /**
  62. * @var string[] Error codes (or messages)
  63. */
  64. public $errors = array();
  65. /**
  66. * Constructor
  67. *
  68. * @param DoliDB $db Database handler
  69. * @param string $dirmodule Name of directory of module
  70. * @param string $targetmodule Name of directory where canvas is stored
  71. * @param string $canvas Name of canvas
  72. * @param string $card Name of tab (sub-canvas)
  73. */
  74. public function __construct($db, $dirmodule, $targetmodule, $canvas, $card)
  75. {
  76. $this->db = $db;
  77. $this->dirmodule = $dirmodule;
  78. $this->targetmodule = $targetmodule;
  79. $this->canvas = $canvas;
  80. $this->card = $card;
  81. $this->module = "service";
  82. $this->name = "service";
  83. $this->definition = "Services canvas";
  84. $this->fieldListName = "product_service";
  85. $this->next_prev_filter = "canvas='service'";
  86. }
  87. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  88. /**
  89. * Assign custom values for canvas (for example into this->tpl to be used by templates)
  90. *
  91. * @param string $action Type of action
  92. * @param integer $id Id of object
  93. * @param string $ref Ref of object
  94. * @return void
  95. */
  96. public function assign_values(&$action, $id = 0, $ref = '')
  97. {
  98. // phpcs:enable
  99. global $conf, $langs, $user, $mysoc, $canvas;
  100. global $form;
  101. $tmpobject = new Product($this->db);
  102. if (!empty($id) || !empty($ref)) {
  103. $tmpobject->fetch($id, $ref);
  104. }
  105. $this->object = $tmpobject;
  106. foreach ($this->object as $key => $value) {
  107. $this->tpl[$key] = $value;
  108. }
  109. $this->tpl['error'] = get_htmloutput_errors($this->object->error, $this->object->errors);
  110. // canvas
  111. $this->tpl['canvas'] = $this->canvas;
  112. // id
  113. $this->tpl['id'] = $this->id;
  114. // Ref
  115. $this->tpl['ref'] = $this->ref;
  116. // Label
  117. $this->tpl['label'] = $this->label;
  118. // Description
  119. $this->tpl['description'] = nl2br($this->description);
  120. // Statut
  121. $this->tpl['status'] = $this->object->getLibStatut(2);
  122. // Note
  123. $this->tpl['note'] = nl2br($this->note);
  124. if ($action == 'create') {
  125. // Price
  126. $this->tpl['price'] = $this->price;
  127. $this->tpl['price_min'] = $this->price_min;
  128. $this->tpl['price_base_type'] = $form->selectPriceBaseType($this->price_base_type, "price_base_type");
  129. // VAT
  130. $this->tpl['tva_tx'] = $form->load_tva("tva_tx", -1, $mysoc, '');
  131. }
  132. if ($action == 'view') {
  133. $head = product_prepare_head($this->object);
  134. $this->tpl['showrefnav'] = $form->showrefnav($this->object, 'ref', '', 1, 'ref');
  135. $titre = $langs->trans("CardProduct".$this->object->type);
  136. $picto = ($this->object->type == Product::TYPE_SERVICE ? 'service' : 'product');
  137. $this->tpl['showhead'] = dol_get_fiche_head($head, 'card', $titre, 0, $picto);
  138. $this->tpl['showend'] = dol_get_fiche_end();
  139. // Accountancy buy code
  140. $this->tpl['accountancyBuyCodeKey'] = $form->editfieldkey("ProductAccountancyBuyCode", 'productaccountancycodesell', $this->accountancy_code_sell, $this, $user->rights->produit->creer);
  141. $this->tpl['accountancyBuyCodeVal'] = $form->editfieldval("ProductAccountancyBuyCode", 'productaccountancycodesell', $this->accountancy_code_sell, $this, $user->rights->produit->creer);
  142. // Accountancy sell code
  143. $this->tpl['accountancySellCodeKey'] = $form->editfieldkey("ProductAccountancySellCode", 'productaccountancycodebuy', $this->accountancy_code_buy, $this, $user->rights->produit->creer);
  144. $this->tpl['accountancySellCodeVal'] = $form->editfieldval("ProductAccountancySellCode", 'productaccountancycodebuy', $this->accountancy_code_buy, $this, $user->rights->produit->creer);
  145. }
  146. $this->tpl['finished'] = $this->object->finished;
  147. $this->tpl['ref'] = $this->object->ref;
  148. $this->tpl['label'] = $this->object->label;
  149. $this->tpl['id'] = $this->object->id;
  150. $this->tpl['type'] = $this->object->type;
  151. $this->tpl['note'] = $this->object->note_private;
  152. $this->tpl['seuil_stock_alerte'] = $this->object->seuil_stock_alerte;
  153. // Duration
  154. $this->tpl['duration_value'] = $this->object->duration_value;
  155. if ($action == 'create') {
  156. // Title
  157. $this->tpl['title'] = $langs->trans("NewService");
  158. }
  159. if ($action == 'edit') {
  160. $this->tpl['title'] = $langs->trans('Modify').' '.$langs->trans('Service').' : '.$this->object->ref;
  161. }
  162. if ($action == 'create' || $action == 'edit') {
  163. // Status
  164. $statutarray = array('1' => $langs->trans("OnSell"), '0' => $langs->trans("NotOnSell"));
  165. $this->tpl['status'] = $form->selectarray('statut', $statutarray, $this->object->status);
  166. $statutarray = array('1' => $langs->trans("ProductStatusOnBuy"), '0' => $langs->trans("ProductStatusNotOnBuy"));
  167. $this->tpl['status_buy'] = $form->selectarray('statut_buy', $statutarray, $this->object->status_buy);
  168. $this->tpl['description'] = $this->description;
  169. $this->tpl['note'] = $this->note;
  170. // Duration unit
  171. // TODO creer fonction
  172. $duration_unit = '<input name="duration_unit" type="radio" value="h"'.($this->object->duration_unit == 'h' ? ' checked' : '').'>'.$langs->trans("Hour");
  173. $duration_unit .= '&nbsp; ';
  174. $duration_unit .= '<input name="duration_unit" type="radio" value="d"'.($this->object->duration_unit == 'd' ? ' checked' : '').'>'.$langs->trans("Day");
  175. $duration_unit .= '&nbsp; ';
  176. $duration_unit .= '<input name="duration_unit" type="radio" value="w"'.($this->object->duration_unit == 'w' ? ' checked' : '').'>'.$langs->trans("Week");
  177. $duration_unit .= '&nbsp; ';
  178. $duration_unit .= '<input name="duration_unit" type="radio" value="m"'.($this->object->duration_unit == 'm' ? ' checked' : '').'>'.$langs->trans("Month");
  179. $duration_unit .= '&nbsp; ';
  180. $duration_unit .= '<input name="duration_unit" type="radio" value="y"'.($this->object->duration_unit == 'y' ? ' checked' : '').'>'.$langs->trans("Year");
  181. $this->tpl['duration_unit'] = $duration_unit;
  182. }
  183. if ($action == 'view') {
  184. // Photo
  185. $this->tpl['nblines'] = 4;
  186. if ($this->object->is_photo_available($conf->service->multidir_output[$this->object->entity])) {
  187. $this->tpl['photos'] = $this->object->show_photos('product', $conf->service->multidir_output[$this->object->entity], 1, 1, 0, 0, 0, 80);
  188. }
  189. // Duration
  190. $dur = array();
  191. if ($this->object->duration_value > 1) {
  192. $dur = array("h"=>$langs->trans("Hours"), "d"=>$langs->trans("Days"), "w"=>$langs->trans("Weeks"), "m"=>$langs->trans("Months"), "y"=>$langs->trans("Years"));
  193. } elseif ($this->object->duration_value > 0) {
  194. $dur = array("h"=>$langs->trans("Hour"), "d"=>$langs->trans("Day"), "w"=>$langs->trans("Week"), "m"=>$langs->trans("Month"), "y"=>$langs->trans("Year"));
  195. }
  196. $this->tpl['duration_unit'] = $langs->trans($dur[$this->object->duration_unit]);
  197. $this->tpl['fiche_end'] = dol_get_fiche_end();
  198. }
  199. }
  200. /**
  201. * Fetch field list
  202. *
  203. * @return void
  204. */
  205. private function getFieldListCanvas()
  206. {
  207. global $conf, $langs;
  208. $this->field_list = array();
  209. $sql = "SELECT rowid, name, alias, title, align, sort, search, visible, enabled, rang";
  210. $sql .= " FROM ".MAIN_DB_PREFIX."c_field_list";
  211. $sql .= " WHERE element = '".$this->db->escape($this->fieldListName)."'";
  212. $sql .= " AND entity = ".$conf->entity;
  213. $sql .= " ORDER BY rang ASC";
  214. $resql = $this->db->query($sql);
  215. if ($resql) {
  216. $num = $this->db->num_rows($resql);
  217. $i = 0;
  218. while ($i < $num) {
  219. $fieldlist = array();
  220. $obj = $this->db->fetch_object($resql);
  221. $fieldlist["id"] = $obj->rowid;
  222. $fieldlist["name"] = $obj->name;
  223. $fieldlist["alias"] = $obj->alias;
  224. $fieldlist["title"] = $langs->trans($obj->title);
  225. $fieldlist["align"] = $obj->align;
  226. $fieldlist["sort"] = $obj->sort;
  227. $fieldlist["search"] = $obj->search;
  228. $fieldlist["visible"] = $obj->visible;
  229. $fieldlist["enabled"] = verifCond($obj->enabled);
  230. $fieldlist["order"] = $obj->rang;
  231. array_push($this->field_list, $fieldlist);
  232. $i++;
  233. }
  234. $this->db->free($resql);
  235. } else {
  236. dol_print_error($this->db, $sql);
  237. }
  238. }
  239. }