Fixed Assets: Added sum-of-the-year digits method, fixed declining balance method...
[fa-stable.git] / fixed_assets / includes / fixed_assets_db.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12
13 function process_fixed_asset_depreciation($stock_id, $gl_rows, $refline, $memo_)
14 {
15   global $Refs;
16
17   $row = get_item($stock_id);
18   $old_cost = $row['material_cost'];
19
20   begin_transaction();
21
22   $date_ = Today();
23   if (!is_date_in_fiscalyear($date_))
24     $date_ = end_fiscalyear();
25
26   $stock_gl_code = get_stock_gl_code($stock_id);
27
28   $cart = new items_cart(ST_JOURNAL);
29   $cart->tran_date = $cart->doc_date = $cart->event_date = $date_;
30   $cart->tran_date = end_fiscalyear();
31   $cart->reference = $Refs->get_next(ST_JOURNAL, $refline, $cart->tran_date, $date_);
32
33   $value_of_change = 0;
34   foreach ($gl_rows as $row) {
35     $value_of_change += round($row['value'], 2);
36
37     $cart->add_gl_item($stock_gl_code["adjustment_account"],
38         $stock_gl_code["dimension_id"], $stock_gl_code["dimension2_id"], -$row['value'],
39         '', null, null, $row['date']);
40     $cart->add_gl_item($stock_gl_code["cogs_account"], 0, 0, $row['value'],
41         '', null, null, $row['date']);
42   }
43
44   $new_cost = $old_cost - $value_of_change;
45
46   if (empty($memo_))
47     $cart->memo_ = sprintf(_("Fixed asset has been deprecated by the value of %s"),
48         number_format2($value_of_change, 2));
49   else
50     $cart->memo_ = $memo_;
51
52   $trans_no = write_journal_entries($cart);
53
54   $sql = "UPDATE ".TB_PREF."stock_master SET 
55     depreciation_date='".date2sql($cart->tran_date)."',
56     material_cost=".db_escape($new_cost).",
57     last_cost=".db_escape($old_cost)."
58     WHERE stock_id=".db_escape($stock_id);
59   db_query($sql,"The depreciation start date could not be updated");
60
61   $update_no = -1;
62   add_audit_trail(ST_JOURNAL, $update_no, $date_);
63   commit_transaction();
64
65   return $trans_no;
66 }
67
68 //--------------------------------------------------------------------------------------------------
69
70 function get_fixed_asset_move($stock_id, $tran_type) {
71
72   $sql = "SELECT * FROM ".TB_PREF."stock_moves 
73             WHERE stock_id=".db_escape($stock_id)."
74                 AND type=".db_escape($tran_type);
75
76   $result = db_query($sql, "cannot retrieve fixed asset move");
77
78   if (db_num_rows($result) == 0)
79     return false;
80
81   $row = db_fetch_assoc($result);
82   return $row;
83 }
84
85 function get_fixed_asset_disposal($stock_id) {
86
87   $sql = "SELECT * FROM ".TB_PREF."stock_moves 
88         WHERE stock_id=".db_escape($stock_id)."
89                 AND (type=".ST_INVADJUST." OR type=".ST_CUSTDELIVERY.")";
90
91   $result = db_query($sql, "cannot retrieve fixed asset move");
92
93   if (db_num_rows($result) == 0)
94     return false;
95
96   $row = db_fetch_assoc($result);
97   return $row;
98 }
99
100 function get_fixed_asset_purchase($stock_id) {
101
102   $sql = "SELECT * 
103         FROM ".TB_PREF."stock_master m, "
104                 .TB_PREF."supp_invoice_items i, "
105                 .TB_PREF."supp_trans t
106     WHERE m.stock_id=".db_escape($stock_id)."
107             AND i.supp_trans_no=t.trans_no
108         AND m.stock_id=i.stock_id";
109
110   $result = db_query($sql, "cannot retrieve fixed asset move");
111
112   if (db_num_rows($result) == 0)
113     return false;
114
115   $row = db_fetch_assoc($result);
116   return $row;
117 }
118
119 //--------------------------------------------------------------------------------------------------
120
121 function get_initial_price($stock_id) {
122   $row = get_fixed_asset_move($stock_id, ST_SUPPRECEIVE);
123   return $row['price'];
124 }
125
126 //--------------------------------------------------------------------------------------------------
127
128 function get_fixed_asset_class($id)
129 {
130         $sql="SELECT * FROM ".TB_PREF."stock_fa_class WHERE fa_class_id=".db_escape($id);
131
132         $result = db_query($sql,"a fixed asset class could not be retrieved");
133
134         return db_fetch($result);
135 }
136
137 //--------------------------------------------------------------------------------------------------
138
139 function get_sql_for_fixed_assets($show_inactive = false)
140 {
141   $sql = "SELECT s.stock_id, c.description, s.units, s.description as name,
142     s.depreciation_rate, s.depreciation_method, s.inactive, rcv.tran_date as purchase_date, rcv.trans_no as purchase_no,
143     adj.tran_date as disposal_date, adj.type as disposal_type, adj.trans_no as disposal_no, s.material_cost, s.last_cost, s.depreciation_factor
144     FROM ".TB_PREF."stock_master s"
145         ." LEFT JOIN ".TB_PREF."stock_moves rcv ON rcv.stock_id=s.stock_id AND rcv.type=".ST_SUPPRECEIVE
146         ." LEFT JOIN ".TB_PREF."stock_moves adj ON adj.stock_id=s.stock_id AND adj.type IN(".ST_INVADJUST.",".ST_CUSTDELIVERY.")"
147         ." LEFT JOIN ".TB_PREF."stock_fa_class c ON s.fa_class_id=c.fa_class_id"
148         ." WHERE s.mb_flag='F'";
149
150   if (!$show_inactive)
151     $sql .= " AND !s.inactive AND !ISNULL(rcv.tran_date) AND ISNULL(adj.tran_date)";
152
153         return $sql;
154 }
155
156 //--------------------------------------------------------------------------------------------------
157
158 //function get_sql_for_fa_journal_inquiry($stock_id)
159 //{
160 //
161 //      $sql = "SELECT  IF(ISNULL(a.gl_seq),0,a.gl_seq) as gl_seq,
162 //              gl.tran_date,
163 //              gl.type as trans_type,
164 //              gl.type_no as trans_no,
165 //              IFNULL(max(supp.supp_name), max(cust.name)) as name,
166 //              refs.reference,
167 //              SUM(IF(gl.amount>0, gl.amount,0)) as amount,
168 //              com.memo_,
169 //              IF(ISNULL(u.user_id),'',u.user_id) as user_id";
170 //
171 //      $sql.= " FROM ".TB_PREF."gl_trans as gl
172 //               LEFT JOIN ".TB_PREF."audit_trail as a ON
173 //                      (gl.type=a.type AND gl.type_no=a.trans_no)
174 //               LEFT JOIN ".TB_PREF."comments as com ON
175 //                      (gl.type=com.type AND gl.type_no=com.id)
176 //               LEFT JOIN ".TB_PREF."refs as refs ON
177 //                      (gl.type=refs.type AND gl.type_no=refs.id)
178 //               LEFT JOIN ".TB_PREF."users as u ON
179 //                      a.user=u.id
180 //               LEFT JOIN ".TB_PREF."debtor_trans dt ON dt.type=gl.type AND gl.type_no=dt.trans_no
181 //               LEFT JOIN ".TB_PREF."debtors_master cust ON gl.person_type_id=2 AND gl.person_id=cust.debtor_no
182 //               LEFT JOIN ".TB_PREF."supp_trans st ON st.type=gl.type AND gl.type_no=st.trans_no
183 //               LEFT JOIN ".TB_PREF."suppliers supp ON gl.person_type_id=3 AND gl.person_id=supp.supplier_id
184 //               WHERE gl.amount!=0
185 //     AND NOT ISNULL(a.gl_seq)";
186 //
187 ////    if (!$alsoclosed) {
188 ////            $sql .= " AND gl_seq=0";
189 ////    }
190 ////    else
191 ////            $sql .= " AND NOT ISNULL(a.gl_seq)";
192 //
193 //      $sql .= " GROUP BY tran_date, gl_seq, trans_type, trans_no";
194 //
195 //      return $sql;
196 //}
197
198 //--------------------------------------------------------------------------------------------------