Remove test material from source.
[fa-stable.git] / gl / includes / db / gl_journal.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 function add_journal($type, $trans_no, $amount, $tran_date, $currency, $reference, $source_ref='', $rate=1,
13          $event_date='', $doc_date='')
14 {
15         $sql = "INSERT INTO ".TB_PREF."journal(
16            `type`,`trans_no`, `amount`, `currency`, `rate`, `reference`, `source_ref`, `tran_date`,`event_date`,`doc_date`)
17           VALUES("
18           .db_escape($type).","
19           .db_escape($trans_no).","
20           .db_escape($amount).","
21           .db_escape($currency).","
22           .db_escape($rate).","
23           .db_escape($reference).","
24           .db_escape($source_ref).","
25           ."'".date2sql($tran_date)."','"
26           . ($event_date === '' ? '0000-00-00' : date2sql($event_date))."','"
27           . ($doc_date === '' ? '0000-00-00' : date2sql($doc_date))."')";
28
29         return db_query($sql, 'cannot add journal entry');
30 }
31
32 function update_journal($type, $trans_no, $amount, $tran_date, $currency, $reference, $source_ref='', $rate=1,
33          $event_date='', $doc_date='')
34 {
35         $sql = "UPDATE ".TB_PREF."journal SET "
36           ."`amount`=".db_escape($amount).","
37           ."`currency`=".db_escape($currency).","
38           ."`rate`=".db_escape($rate).","
39           ."`reference`=".db_escape($reference).","
40           ."`source_ref`=".db_escape($source_ref).","
41           ."`tran_date`='".date2sql($tran_date)."',"
42           ."`event_date`='".($event_date === '' ? '0000-00-00' : date2sql($event_date))."',"
43           ."`doc_date`='".($doc_date === '' ? '0000-00-00' : date2sql($doc_date))."'
44            WHERE `type`=".db_escape($type)." AND " ."`trans_no`=".db_escape($trans_no);
45
46         return db_query($sql, 'cannot update journal entry');
47 }
48
49 function delete_journal($type, $trans_no)
50 {
51         $sql = "DELETE FROM ".TB_PREF."journal 
52            WHERE `type`=".db_escape($type)." AND " ."`trans_no`=".db_escape($trans_no);
53
54         return db_query($sql, 'cannot delete journal entry');
55 }
56
57 function get_journal($type, $trans_no)
58 {
59         $sql = "SELECT * FROM ".TB_PREF."journal 
60            WHERE `type`=".db_escape($type)." AND " ."`trans_no`=".db_escape($trans_no);
61
62         $myrow= db_query($sql, 'cannot retrieve journal entry');
63         return db_fetch($myrow);
64 }
65
66 //
67 // Write headers in debtor_trans for allocation purposes.
68 // Amount in customer currency
69 //
70 function write_cust_journal($trans_type, $trans_no, $branch_id, $date, $ref, $amount, $rate)
71 {
72         // we have to select any branch_id for given AR_act - in fact w
73         $sql = "INSERT INTO ".TB_PREF."debtor_trans (trans_no, type, debtor_no, branch_code, tran_date, reference, ov_amount, rate)
74         VALUES (".db_escape($trans_no).", ".db_escape($trans_type).", 
75                         (SELECT debtor_no FROM ".TB_PREF."cust_branch WHERE branch_code=".db_escape($branch_id). ") ,"
76                         .db_escape($branch_id).",'".date2sql($date)."', ".db_escape($ref).","
77                         .db_escape($amount).", ".db_escape($rate).")";
78         return db_query($sql, 'cannot write cust_journal');
79 }
80
81 //
82 // Write headers in supp_trans for allocation purposes.
83 // Amount in supplier currency
84 //
85 function write_supp_journal($trans_type, $trans_no, $supp_id, $date, $ref, $amount, $rate, $src_ref)
86 {
87         $sql = "INSERT INTO ".TB_PREF."supp_trans (trans_no, type, supplier_id, tran_date, reference, ov_amount, rate, supp_reference)
88                 VALUES (".db_escape($trans_no).", ".db_escape($trans_type).", ".db_escape($supp_id).", '".date2sql($date)."',"
89                 .db_escape($ref).", ".db_escape($amount).",".db_escape($rate).",".db_escape($src_ref).")";
90
91         return db_query($sql, 'cannot write supp_journal');
92 }
93
94 //--------------------------------------------------------------------------------
95 // Write/update journal entries.
96 //
97 function write_journal_entries(&$cart)
98 {
99         global $Refs;
100
101         $date_ = $cart->tran_date;
102         $ref   = $cart->reference;
103         $memo_ = $cart->memo_;
104         $trans_type = $cart->trans_type;
105         $new = $cart->order_id == 0;
106         $old_no = $cart->order_id;
107
108         begin_transaction();
109         hook_db_prewrite($cart, ST_JOURNAL);
110         if (!$new)
111         {
112                 $msg = void_transaction($trans_type, $cart->order_id, Today(), _("Document reentered."));
113                 if ($msg)
114                 {
115                         display_error($msg);
116                         return false;
117                 }
118         }
119
120         $trans_id = $cart->order_id = get_next_trans_no($trans_type);
121         if ($old_no)
122         {
123                 $aid = 0;
124                 $aid = has_attachment($trans_type, $old_no);
125                 if ($aid != 0)
126                 {
127                         $row = get_attachment($aid);
128                         update_attachment($aid, $row['type_no'], $trans_id, $row['description'],
129                                 $row['filename'], $row['unique_name'], $row['filesize'], $row['filetype']);
130                 }
131
132         }       
133         $total = $cart->gl_items_total_debit();
134
135         $cart->write_gl();
136
137         add_journal($trans_type, $trans_id, $total,     $date_ , $cart->currency, $ref,
138                 $cart->source_ref, $cart->rate, $cart->event_date, $cart->doc_date);
139
140         $Refs->save($trans_type, $trans_id, $ref);
141         if ($new) {
142                 add_comments($trans_type, $trans_id, $date_, $memo_);
143         } else {
144                 update_comments($trans_type, $trans_id, null, $memo_);
145         }
146
147         add_audit_trail($trans_type, $trans_id, $date_);
148         hook_db_postwrite($cart, ST_JOURNAL);
149         commit_transaction();
150
151         return $trans_id;
152 }
153
154 //----------------------------------------------------------------------------------------
155
156 function void_journal_trans($type, $type_no, $use_transaction=true)
157 {
158         if ($use_transaction)
159                 begin_transaction();
160
161         $sql = "UPDATE ".TB_PREF."journal SET amount=0
162                 WHERE type=".db_escape($type)." AND trans_no=".db_escape($type_no);
163
164         db_query($sql, "could not void journal transaction for type=$type and trans_no=$type_no");
165
166         void_bank_trans($type, $type_no, true);
167
168         if ($use_transaction)
169                 commit_transaction();
170 }
171