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') {
94 $accountancy_code =
'';
96 $this->accountancy_codes[$mode_key][$field_key] = $accountancy_code;
99 return $this->accountancy_codes;
111 global $langs, $hookmanager;
112 dol_syslog(__METHOD__ .
" asset_id=$asset_id, asset_model_id=$asset_model_id");
115 $this->errors = array();
116 $this->accountancy_codes = array();
119 $asset_id = $asset_id > 0 ? $asset_id : 0;
120 $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
122 $hookmanager->initHooks(array(
'assetaccountancycodesdao'));
123 $parameters = array(
'asset_id' => $asset_id,
'asset_model_id' => $asset_model_id);
124 $reshook = $hookmanager->executeHooks(
'fetchAccountancyCodes', $parameters, $this);
125 if (!empty($reshook)) {
130 if (empty($asset_id) && empty($asset_model_id)) {
131 $this->errors[] = $langs->trans(
'AssetErrorAssetOrAssetModelIDNotProvide');
139 $accountancy_codes = array();
140 foreach ($this->accountancy_codes_fields as $mode_key => $mode_info) {
141 $sql =
"SELECT " . implode(
',', array_keys($mode_info[
'fields']));
142 $sql .=
" FROM " . MAIN_DB_PREFIX . $mode_info[
'table'];
143 $sql .=
" WHERE " . ($asset_id > 0 ?
" fk_asset = " . (int) $asset_id :
" fk_asset_model = " . (int) $asset_model_id);
145 $resql = $this->db->query($sql);
147 if ($obj = $this->db->fetch_object($resql)) {
148 $accountancy_codes[$mode_key] = array();
149 foreach ($mode_info[
'fields'] as $field_key => $field_info) {
150 $accountancy_codes[$mode_key][$field_key] = $obj->$field_key;
154 $this->errors[] = $langs->trans(
'AssetErrorFetchAccountancyCodesForMode', $mode_key) .
': ' . $this->db->lasterror();
163 $this->accountancy_codes = $accountancy_codes;
179 global $langs, $hookmanager;
180 dol_syslog(__METHOD__ .
" user_id=".$user->id.
", asset_id=".$asset_id.
", asset_model_id=".$asset_model_id.
", notrigger=".$notrigger);
183 $this->errors = array();
186 $asset_id = $asset_id > 0 ? $asset_id : 0;
187 $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
189 $hookmanager->initHooks(array(
'assetaccountancycodesdao'));
190 $parameters = array(
'user' => $user,
'asset_id' => $asset_id,
'asset_model_id' => $asset_model_id);
191 $reshook = $hookmanager->executeHooks(
'updateAccountancyCodes', $parameters, $this);
192 if (!empty($reshook)) {
197 if (empty($asset_id) && empty($asset_model_id)) {
198 $this->errors[] = $langs->trans(
'AssetErrorAssetOrAssetModelIDNotProvide');
209 foreach ($this->accountancy_codes_fields as $mode_key => $mode_info) {
211 $sql =
"DELETE FROM " . MAIN_DB_PREFIX . $mode_info[
'table'];
212 $sql .=
" WHERE " . ($asset_id > 0 ?
" fk_asset = " . (int) $asset_id :
" fk_asset_model = " . (int) $asset_model_id);
213 $resql = $this->db->query($sql);
215 $this->errors[] = $langs->trans(
'AssetErrorDeleteAccountancyCodesForMode', $mode_key) .
': ' . $this->db->lasterror();
219 if (!$error && !empty($this->accountancy_codes[$mode_key])) {
221 $sql =
"INSERT INTO " . MAIN_DB_PREFIX . $mode_info[
'table'] .
"(";
222 $sql .= $asset_id > 0 ?
"fk_asset," :
"fk_asset_model,";
223 $sql .= implode(
',', array_keys($mode_info[
'fields']));
224 $sql .=
", tms, fk_user_modif";
226 $sql .= $asset_id > 0 ? $asset_id : $asset_model_id;
227 foreach ($mode_info[
'fields'] as $field_key => $field_info) {
228 $sql .=
', ' . (empty($this->accountancy_codes[$mode_key][$field_key]) ?
'NULL' :
"'" . $this->db->escape($this->accountancy_codes[$mode_key][$field_key]) .
"'");
230 $sql .=
", '" . $this->db->idate($now) .
"'";
231 $sql .=
", " . $user->id;
234 $resql = $this->db->query($sql);
236 $this->errors[] = $langs->trans(
'AssetErrorInsertAccountancyCodesForMode', $mode_key) .
': ' . $this->db->lasterror();
242 if (!$error && $asset_id > 0) {
244 require_once DOL_DOCUMENT_ROOT .
'/asset/class/asset.class.php';
245 $asset =
new Asset($this->db);
246 $result = $asset->fetch($asset_id);
248 $result = $asset->calculationDepreciation();
251 $this->errors[] = $langs->trans(
'AssetErrorCalculationDepreciationLines');
252 $this->errors[] = $asset->errorsToString();
257 if (!$error && !$notrigger) {
259 $result = $this->
call_trigger(
'ASSET_ACCOUNTANCY_CODES_MODIFY', $user);
270 $this->db->rollback();