ActionsCardService   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 263
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 137
dl 0
loc 263
rs 10
c 0
b 0
f 0
wmc 24

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFieldListCanvas() 0 40 3
F assign_values() 0 130 20
A __construct() 0 13 1
1
<?php
2
3
/* Copyright (C) 2010-2018  Regis Houssin               <[email protected]>
4
 * Copyright (C) 2024       Frédéric France             <[email protected]>
5
 * Copyright (C) 2024       Rafael San José             <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
use Dolibarr\Code\Product\Classes\Product;
22
use Dolibarr\Lib\ViewMain;
23
24
/**
25
 *  \file       htdocs/product/canvas/service/actions_card_service.class.php
26
 *  \ingroup    service
27
 *  \brief      File with class of actions for canvas service
28
 */
29
30
31
/**
32
 *  Class with controller methods for service canvas
33
 */
34
class ActionsCardService
35
{
36
    /**
37
     * @var DoliDB Database handler.
38
     */
39
    public $db;
40
41
    public $dirmodule;
42
    public $module;
43
    public $label;
44
    public $price_base_type;
45
    public $accountancy_code_sell;
46
    public $accountancy_code_buy;
47
    public $targetmodule;
48
    public $canvas;
49
    public $card;
50
51
    public $name;
52
    public $definition;
53
    public $fieldListName;
54
    public $next_prev_filter;
55
56
    //! Object container
57
    public $object;
58
59
    //! Template container
60
    public $tpl = array();
61
62
    // List of fields for action=list
63
    public $field_list = array();
64
65
    public $id;
66
    public $ref;
67
    public $description;
68
    public $note;
69
    public $price;
70
    public $price_min;
71
72
    /**
73
     * @var string Error code (or message)
74
     */
75
    public $error = '';
76
77
    /**
78
     * @var string[] Error codes (or messages)
79
     */
80
    public $errors = array();
81
82
83
    /**
84
     *    Constructor
85
     *
86
     * @param DoliDB $db Database handler
87
     * @param string $dirmodule Name of directory of module
88
     * @param string $targetmodule Name of directory where canvas is stored
89
     * @param string $canvas Name of canvas
90
     * @param string $card Name of tab (sub-canvas)
91
     */
92
    public function __construct($db, $dirmodule, $targetmodule, $canvas, $card)
93
    {
94
        $this->db = $db;
95
        $this->dirmodule = $dirmodule;
96
        $this->targetmodule = $targetmodule;
97
        $this->canvas = $canvas;
98
        $this->card = $card;
99
100
        $this->module = "service";
101
        $this->name = "service";
102
        $this->definition = "Services canvas";
103
        $this->fieldListName = "product_service";
104
        $this->next_prev_filter = "canvas='service'";
105
    }
106
107
108
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
109
110
    /**
111
     *    Assign custom values for canvas (for example into this->tpl to be used by templates)
112
     *
113
     * @param string $action Type of action
114
     * @param integer $id Id of object
115
     * @param string $ref Ref of object
116
     * @return   void
117
     */
118
    public function assign_values(&$action, $id = 0, $ref = '')
119
    {
120
        // phpcs:enable
121
        global $conf, $langs, $user, $mysoc, $canvas;
122
        global $form;
123
124
        $tmpobject = new Product($this->db);
125
        if (!empty($id) || !empty($ref)) {
126
            $tmpobject->fetch($id, $ref);
127
        }
128
        $this->object = $tmpobject;
129
130
        foreach ($this->object as $key => $value) {
131
            $this->tpl[$key] = $value;
132
        }
133
134
        $this->tpl['error'] = get_htmloutput_errors($this->object->error, $this->object->errors);
135
136
        // canvas
137
        $this->tpl['canvas'] = $this->canvas;
138
139
        // id
140
        $this->tpl['id'] = $this->id;
141
142
        // Ref
143
        $this->tpl['ref'] = $this->ref;
144
145
        // Label
146
        $this->tpl['label'] = $this->label;
147
148
        // Description
149
        $this->tpl['description'] = nl2br($this->description);
150
151
        // Statut
152
        $this->tpl['status'] = $this->object->getLibStatut(2);
153
154
        // Note
155
        $this->tpl['note'] = nl2br($this->note);
156
157
        if ($action == 'create') {
158
            // Price
159
            $this->tpl['price'] = $this->price;
160
            $this->tpl['price_min'] = $this->price_min;
161
            $this->tpl['price_base_type'] = $form->selectPriceBaseType($this->price_base_type, "price_base_type");
162
163
            // VAT
164
            $this->tpl['tva_tx'] = $form->load_tva("tva_tx", -1, $mysoc, '');
165
        }
166
167
        if ($action == 'view') {
168
            $head = product_prepare_head($this->object);
169
170
            $this->tpl['showrefnav'] = $form->showrefnav($this->object, 'ref', '', 1, 'ref');
171
172
            $titre = $langs->trans("CardProduct" . $this->object->type);
173
            $picto = ($this->object->type == Product::TYPE_SERVICE ? 'service' : 'product');
174
            $this->tpl['showhead'] = dol_get_fiche_head($head, 'card', $titre, 0, $picto);
175
            $this->tpl['showend'] = dol_get_fiche_end();
176
177
            // Accountancy buy code
178
            $this->tpl['accountancyBuyCodeKey'] = $form->editfieldkey("ProductAccountancyBuyCode", 'productaccountancycodesell', $this->accountancy_code_sell, $this, $user->rights->produit->creer);
179
            $this->tpl['accountancyBuyCodeVal'] = $form->editfieldval("ProductAccountancyBuyCode", 'productaccountancycodesell', $this->accountancy_code_sell, $this, $user->rights->produit->creer);
180
181
            // Accountancy sell code
182
            $this->tpl['accountancySellCodeKey'] = $form->editfieldkey("ProductAccountancySellCode", 'productaccountancycodebuy', $this->accountancy_code_buy, $this, $user->rights->produit->creer);
183
            $this->tpl['accountancySellCodeVal'] = $form->editfieldval("ProductAccountancySellCode", 'productaccountancycodebuy', $this->accountancy_code_buy, $this, $user->rights->produit->creer);
184
        }
185
186
        $this->tpl['finished'] = $this->object->finished;
187
        $this->tpl['ref'] = $this->object->ref;
188
        $this->tpl['label'] = $this->object->label;
189
        $this->tpl['id'] = $this->object->id;
190
        $this->tpl['type'] = $this->object->type;
191
        $this->tpl['note'] = $this->object->note_private;
192
        $this->tpl['seuil_stock_alerte'] = $this->object->seuil_stock_alerte;
193
194
        // Duration
195
        $this->tpl['duration_value'] = $this->object->duration_value;
196
197
        if ($action == 'create') {
198
            // Title
199
            $this->tpl['title'] = $langs->trans("NewService");
200
        }
201
202
        if ($action == 'edit') {
203
            $this->tpl['title'] = $langs->trans('Modify') . ' ' . $langs->trans('Service') . ' : ' . $this->object->ref;
204
        }
205
206
        if ($action == 'create' || $action == 'edit') {
207
            // Status
208
            $statutarray = array('1' => $langs->trans("OnSell"), '0' => $langs->trans("NotOnSell"));
209
            $this->tpl['status'] = $form->selectarray('statut', $statutarray, $this->object->status);
210
211
            $statutarray = array('1' => $langs->trans("ProductStatusOnBuy"), '0' => $langs->trans("ProductStatusNotOnBuy"));
212
            $this->tpl['status_buy'] = $form->selectarray('statut_buy', $statutarray, $this->object->status_buy);
213
214
            $this->tpl['description'] = $this->description;
215
            $this->tpl['note'] = $this->note;
216
217
            // Duration unit
218
            // TODO creer fonction
219
            $duration_unit = '<input name="duration_unit" type="radio" value="h"' . ($this->object->duration_unit == 'h' ? ' checked' : '') . '>' . $langs->trans("Hour");
220
            $duration_unit .= '&nbsp; ';
221
            $duration_unit .= '<input name="duration_unit" type="radio" value="d"' . ($this->object->duration_unit == 'd' ? ' checked' : '') . '>' . $langs->trans("Day");
222
            $duration_unit .= '&nbsp; ';
223
            $duration_unit .= '<input name="duration_unit" type="radio" value="w"' . ($this->object->duration_unit == 'w' ? ' checked' : '') . '>' . $langs->trans("Week");
224
            $duration_unit .= '&nbsp; ';
225
            $duration_unit .= '<input name="duration_unit" type="radio" value="m"' . ($this->object->duration_unit == 'm' ? ' checked' : '') . '>' . $langs->trans("Month");
226
            $duration_unit .= '&nbsp; ';
227
            $duration_unit .= '<input name="duration_unit" type="radio" value="y"' . ($this->object->duration_unit == 'y' ? ' checked' : '') . '>' . $langs->trans("Year");
228
            $this->tpl['duration_unit'] = $duration_unit;
229
        }
230
231
        if ($action == 'view') {
232
            // Photo
233
            $this->tpl['nblines'] = 4;
234
            if ($this->object->is_photo_available($conf->service->multidir_output[$this->object->entity])) {
235
                $this->tpl['photos'] = $this->object->show_photos('product', $conf->service->multidir_output[$this->object->entity], 1, 1, 0, 0, 0, 80);
236
            }
237
238
            // Duration
239
            $dur = array();
240
            if ($this->object->duration_value > 1) {
241
                $dur = array("h" => $langs->trans("Hours"), "d" => $langs->trans("Days"), "w" => $langs->trans("Weeks"), "m" => $langs->trans("Months"), "y" => $langs->trans("Years"));
242
            } elseif ($this->object->duration_value > 0) {
243
                $dur = array("h" => $langs->trans("Hour"), "d" => $langs->trans("Day"), "w" => $langs->trans("Week"), "m" => $langs->trans("Month"), "y" => $langs->trans("Year"));
244
            }
245
            $this->tpl['duration_unit'] = $langs->trans($dur[$this->object->duration_unit]);
246
247
            $this->tpl['fiche_end'] = dol_get_fiche_end();
248
        }
249
    }
250
251
252
    /**
253
     *  Fetch field list
254
     *
255
     * @return void
256
     */
257
    private function getFieldListCanvas() // @phpstan-ignore-line
0 ignored issues
show
Unused Code introduced by
The method getFieldListCanvas() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
258
    {
259
        global $conf, $langs;
260
261
        $this->field_list = array();
262
263
        $sql = "SELECT rowid, name, alias, title, align, sort, search, visible, enabled, rang";
264
        $sql .= " FROM " . MAIN_DB_PREFIX . "c_field_list";
265
        $sql .= " WHERE element = '" . $this->db->escape($this->fieldListName) . "'";
266
        $sql .= " AND entity = " . $conf->entity;
267
        $sql .= " ORDER BY rang ASC";
268
269
        $resql = $this->db->query($sql);
270
        if ($resql) {
271
            $num = $this->db->num_rows($resql);
272
273
            $i = 0;
274
            while ($i < $num) {
275
                $fieldlist = array();
276
277
                $obj = $this->db->fetch_object($resql);
278
279
                $fieldlist["id"] = $obj->rowid;
280
                $fieldlist["name"] = $obj->name;
281
                $fieldlist["alias"] = $obj->alias;
282
                $fieldlist["title"] = $langs->trans($obj->title);
283
                $fieldlist["align"] = $obj->align;
284
                $fieldlist["sort"] = $obj->sort;
285
                $fieldlist["search"] = $obj->search;
286
                $fieldlist["visible"] = $obj->visible;
287
                $fieldlist["enabled"] = verifCond($obj->enabled);
288
                $fieldlist["order"] = $obj->rang;
289
290
                array_push($this->field_list, $fieldlist);
291
292
                $i++;
293
            }
294
            $this->db->free($resql);
295
        } else {
296
            dol_print_error($this->db, $sql);
297
        }
298
    }
299
}
300