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