88 $this->accountancy_codes = array();
89 foreach ($this->accountancy_codes_fields as $mode_key => $mode_info) {
90 $this->accountancy_codes[$mode_key] = array();
91 foreach ($mode_info[
'fields'] as $field_key => $field_info) {
92 $accountancy_code =
GETPOST($mode_key .
'_' . $field_key,
'aZ09');
93 if (empty($accountancy_code) || $accountancy_code ==
'-1') $accountancy_code =
'';
94 $this->accountancy_codes[$mode_key][$field_key] = $accountancy_code;
97 return $this->accountancy_codes;
109 global $langs, $hookmanager;
110 dol_syslog(__METHOD__ .
" asset_id=$asset_id, asset_model_id=$asset_model_id");
113 $this->errors = array();
114 $this->accountancy_codes = array();
117 $asset_id = $asset_id > 0 ? $asset_id : 0;
118 $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
120 $hookmanager->initHooks(array(
'assetaccountancycodesdao'));
121 $parameters = array(
'asset_id' => $asset_id,
'asset_model_id' => $asset_model_id);
122 $reshook = $hookmanager->executeHooks(
'fetchAccountancyCodes', $parameters, $this);
123 if (!empty($reshook)) {
128 if (empty($asset_id) && empty($asset_model_id)) {
129 $this->errors[] = $langs->trans(
'AssetErrorAssetOrAssetModelIDNotProvide');
137 $accountancy_codes = array();
138 foreach ($this->accountancy_codes_fields as $mode_key => $mode_info) {
139 $sql =
"SELECT " . implode(
',', array_keys($mode_info[
'fields']));
140 $sql .=
" FROM " . MAIN_DB_PREFIX . $mode_info[
'table'];
141 $sql .=
" WHERE " . ($asset_id > 0 ?
" fk_asset = " . (int) $asset_id :
" fk_asset_model = " . (int) $asset_model_id);
143 $resql = $this->db->query($sql);
145 if ($obj = $this->db->fetch_object($resql)) {
146 $accountancy_codes[$mode_key] = array();
147 foreach ($mode_info[
'fields'] as $field_key => $field_info) {
148 $accountancy_codes[$mode_key][$field_key] = $obj->$field_key;
152 $this->errors[] = $langs->trans(
'AssetErrorFetchAccountancyCodesForMode', $mode_key) .
': ' . $this->db->lasterror();
161 $this->accountancy_codes = $accountancy_codes;
177 global $langs, $hookmanager;
178 dol_syslog(__METHOD__ .
" user_id=".$user->id.
", asset_id=".$asset_id.
", asset_model_id=".$asset_model_id.
", notrigger=".$notrigger);
181 $this->errors = array();
184 $asset_id = $asset_id > 0 ? $asset_id : 0;
185 $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
187 $hookmanager->initHooks(array(
'assetaccountancycodesdao'));
188 $parameters = array(
'user' => $user,
'asset_id' => $asset_id,
'asset_model_id' => $asset_model_id);
189 $reshook = $hookmanager->executeHooks(
'updateAccountancyCodes', $parameters, $this);
190 if (!empty($reshook)) {
195 if (empty($asset_id) && empty($asset_model_id)) {
196 $this->errors[] = $langs->trans(
'AssetErrorAssetOrAssetModelIDNotProvide');
207 foreach ($this->accountancy_codes_fields as $mode_key => $mode_info) {
209 $sql =
"DELETE FROM " . MAIN_DB_PREFIX . $mode_info[
'table'];
210 $sql .=
" WHERE " . ($asset_id > 0 ?
" fk_asset = " . (int) $asset_id :
" fk_asset_model = " . (int) $asset_model_id);
211 $resql = $this->db->query($sql);
213 $this->errors[] = $langs->trans(
'AssetErrorDeleteAccountancyCodesForMode', $mode_key) .
': ' . $this->db->lasterror();
217 if (!$error && !empty($this->accountancy_codes[$mode_key])) {
219 $sql =
"INSERT INTO " . MAIN_DB_PREFIX . $mode_info[
'table'] .
"(";
220 $sql .= $asset_id > 0 ?
"fk_asset," :
"fk_asset_model,";
221 $sql .= implode(
',', array_keys($mode_info[
'fields']));
222 $sql .=
", tms, fk_user_modif";
224 $sql .= $asset_id > 0 ? $asset_id : $asset_model_id;
225 foreach ($mode_info[
'fields'] as $field_key => $field_info) {
226 $sql .=
', ' . (empty($this->accountancy_codes[$mode_key][$field_key]) ?
'NULL' :
"'" . $this->db->escape($this->accountancy_codes[$mode_key][$field_key]) .
"'");
228 $sql .=
", '" . $this->db->idate($now) .
"'";
229 $sql .=
", " . $user->id;
232 $resql = $this->db->query($sql);
234 $this->errors[] = $langs->trans(
'AssetErrorInsertAccountancyCodesForMode', $mode_key) .
': ' . $this->db->lasterror();
240 if (!$error && $asset_id > 0) {
242 require_once DOL_DOCUMENT_ROOT .
'/asset/class/asset.class.php';
243 $asset =
new Asset($this->db);
244 $result = $asset->fetch($asset_id);
245 if ($result > 0) $result = $asset->calculationDepreciation();
247 $this->errors[] = $langs->trans(
'AssetErrorCalculationDepreciationLines');
248 $this->errors[] = $asset->errorsToString();
253 if (!$error && !$notrigger) {
255 $result = $this->
call_trigger(
'ASSET_ACCOUNTANCY_CODES_MODIFY', $user);
266 $this->db->rollback();