89 $this->accountancy_codes = array();
90 foreach ($this->accountancy_codes_fields as $mode_key => $mode_info) {
91 $this->accountancy_codes[$mode_key] = array();
92 foreach ($mode_info[
'fields'] as $field_key => $field_info) {
93 $accountancy_code =
GETPOST($mode_key .
'_' . $field_key,
'aZ09');
94 if (empty($accountancy_code) || $accountancy_code ==
'-1') {
95 $accountancy_code =
'';
97 $this->accountancy_codes[$mode_key][$field_key] = $accountancy_code;
100 return $this->accountancy_codes;
112 global $langs, $hookmanager;
113 dol_syslog(__METHOD__ .
" asset_id=$asset_id, asset_model_id=$asset_model_id");
116 $this->errors = array();
117 $this->accountancy_codes = array();
120 $asset_id = $asset_id > 0 ? $asset_id : 0;
121 $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
123 $hookmanager->initHooks(array(
'assetaccountancycodesdao'));
124 $parameters = array(
'asset_id' => $asset_id,
'asset_model_id' => $asset_model_id);
125 $reshook = $hookmanager->executeHooks(
'fetchAccountancyCodes', $parameters, $this);
126 if (!empty($reshook)) {
131 if (empty($asset_id) && empty($asset_model_id)) {
132 $this->errors[] = $langs->trans(
'AssetErrorAssetOrAssetModelIDNotProvide');
140 $accountancy_codes = array();
141 foreach ($this->accountancy_codes_fields as $mode_key => $mode_info) {
142 $sql =
"SELECT " . implode(
',', array_keys($mode_info[
'fields']));
143 $sql .=
" FROM " . MAIN_DB_PREFIX . $mode_info[
'table'];
144 $sql .=
" WHERE " . ($asset_id > 0 ?
" fk_asset = " . (int) $asset_id :
" fk_asset_model = " . (int) $asset_model_id);
146 $resql = $this->db->query($sql);
148 if ($obj = $this->db->fetch_object($resql)) {
149 $accountancy_codes[$mode_key] = array();
150 foreach ($mode_info[
'fields'] as $field_key => $field_info) {
151 $accountancy_codes[$mode_key][$field_key] = $obj->$field_key;
155 $this->errors[] = $langs->trans(
'AssetErrorFetchAccountancyCodesForMode', $mode_key) .
': ' . $this->db->lasterror();
164 $this->accountancy_codes = $accountancy_codes;
180 global $langs, $hookmanager;
181 dol_syslog(__METHOD__ .
" user_id=".$user->id.
", asset_id=".$asset_id.
", asset_model_id=".$asset_model_id.
", notrigger=".$notrigger);
184 $this->errors = array();
187 $asset_id = $asset_id > 0 ? $asset_id : 0;
188 $asset_model_id = $asset_model_id > 0 ? $asset_model_id : 0;
190 $hookmanager->initHooks(array(
'assetaccountancycodesdao'));
191 $parameters = array(
'user' => $user,
'asset_id' => $asset_id,
'asset_model_id' => $asset_model_id);
192 $reshook = $hookmanager->executeHooks(
'updateAccountancyCodes', $parameters, $this);
193 if (!empty($reshook)) {
198 if (empty($asset_id) && empty($asset_model_id)) {
199 $this->errors[] = $langs->trans(
'AssetErrorAssetOrAssetModelIDNotProvide');
210 foreach ($this->accountancy_codes_fields as $mode_key => $mode_info) {
212 $sql =
"DELETE FROM " . MAIN_DB_PREFIX . $mode_info[
'table'];
213 $sql .=
" WHERE " . ($asset_id > 0 ?
" fk_asset = " . (int) $asset_id :
" fk_asset_model = " . (int) $asset_model_id);
214 $resql = $this->db->query($sql);
216 $this->errors[] = $langs->trans(
'AssetErrorDeleteAccountancyCodesForMode', $mode_key) .
': ' . $this->db->lasterror();
220 if (!$error && !empty($this->accountancy_codes[$mode_key])) {
222 $sql =
"INSERT INTO " . MAIN_DB_PREFIX . $mode_info[
'table'] .
"(";
223 $sql .= $asset_id > 0 ?
"fk_asset," :
"fk_asset_model,";
224 $sql .= implode(
',', array_keys($mode_info[
'fields']));
225 $sql .=
", tms, fk_user_modif";
227 $sql .= $asset_id > 0 ? $asset_id : $asset_model_id;
228 foreach ($mode_info[
'fields'] as $field_key => $field_info) {
229 $sql .=
', ' . (empty($this->accountancy_codes[$mode_key][$field_key]) ?
'NULL' :
"'" . $this->db->escape($this->accountancy_codes[$mode_key][$field_key]) .
"'");
231 $sql .=
", '" . $this->db->idate($now) .
"'";
232 $sql .=
", " . $user->id;
235 $resql = $this->db->query($sql);
237 $this->errors[] = $langs->trans(
'AssetErrorInsertAccountancyCodesForMode', $mode_key) .
': ' . $this->db->lasterror();
243 if (!$error && $asset_id > 0) {
245 require_once DOL_DOCUMENT_ROOT .
'/asset/class/asset.class.php';
246 $asset =
new Asset($this->db);
247 $result = $asset->fetch($asset_id);
249 $result = $asset->calculationDepreciation();
252 $this->errors[] = $langs->trans(
'AssetErrorCalculationDepreciationLines');
253 $this->errors[] = $asset->errorsToString();
258 if (!$error && !$notrigger) {
260 $result = $this->
call_trigger(
'ASSET_ACCOUNTANCY_CODES_MODIFY', $user);
271 $this->db->rollback();