Inserted Copyright Notice and fixed graphic items
[fa-stable.git] / admin / db / voiding_db.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-3.0.html>.
11 ***********************************************************************/
12 include_once($path_to_root . "/sales/includes/sales_db.inc");
13 include_once($path_to_root . "/purchasing/includes/purchasing_db.inc");
14 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
15 include_once($path_to_root . "/manufacturing/includes/manufacturing_db.inc");
16
17 function void_transaction($type, $type_no, $date_, $memo_)
18 {
19         $void_entry = get_voided_entry($type, $type_no);
20
21         if ($void_entry != null)
22                 return false;
23
24         switch ($type) {
25                 case 0 : // it's a journal entry
26                         if (!exists_gl_trans($type, $type_no))
27                                 return false;
28                         void_gl_trans($type, $type_no);
29                         if (exists_bank_trans($type, $type_no))
30                                 void_bank_trans($type, $type_no);
31                         break;
32
33                 case 1 : // it's a payment
34                 case 2 : // it's a deposit
35                 case 4 : // it's a transfer
36                         if (!exists_bank_trans($type, $type_no))
37                                 return false;
38                         void_bank_trans($type, $type_no);
39                         break;
40
41                 case 10 : // it's a customer invoice
42                 case 11 : // it's a customer credit note
43                 case 12 : // it's a customer payment
44                 case 13 : // it's a customer dispatch
45                         if (!exists_customer_trans($type, $type_no))
46                                 return false;
47                         if ($type == 13)        // added 04 Oct 2008 by Joe Hunt. If delivery note has a not voided invoice, then NO.
48                         {
49                                 $delivery = get_customer_trans($type_no, $type);
50                                 if ($delivery['trans_link'] != 0)
51                                 {
52                                         $inv = get_customer_trans($delivery['trans_link'], 10);
53                                         if ($inv['ov_amount'] != 0 || $inv['ov_discount'] != 0 || $inv['ov_gst'] != 0 || 
54                                                 $inv['ov_freight'] != 0 || $inv['ov_freight_tax'] != 0 || $inv['alloc'] != 0)
55                                                 return false;
56                                 }
57                         }       
58                         post_void_customer_trans($type, $type_no);
59                         break;
60
61                 case systypes::location_transfer() : // it's a stock transfer
62                         if (get_stock_transfer_items($type_no) == null)
63                                 return false;
64                         void_stock_transfer($type_no);
65                         break;
66
67                 case systypes::inventory_adjustment() : // it's a stock adjustment
68                         if (get_stock_adjustment_items($type_no) == null)
69                                 return false;
70                         void_stock_adjustment($type_no);
71                         break;
72
73                 case 25 : // it's a GRN
74                         return false;
75                 case 20 : // it's a suppler invoice
76                 case 21 : // it's a supplier credit note
77                 case 22 : // it's a supplier payment
78                         if (!exists_supp_trans($type, $type_no))
79                                 return false;
80                         if (!post_void_supp_trans($type, $type_no))
81                                 return false;
82                         break;
83
84                 case systypes::work_order() : // it's a work order
85                         if (!get_work_order($type_no, true))
86                                 return false;
87                         void_work_order($type_no);
88                         break;
89
90                 case 28 : // it's a work order issue
91                         if (!exists_work_order_issue($type_no))
92                                 return false;
93                         void_work_order_issue($type_no);
94                         break;
95
96                 case 29 : // it's a work order production
97                         if (!exists_work_order_produce($type_no))
98                                 return false;
99                         void_work_order_produce($type_no);
100                         break;
101
102                 case systypes::cost_update() : // it's a stock cost update
103                         return false;
104                         break;
105         }
106
107         // only add an entry if it's actually been voided
108         add_voided_entry($type, $type_no, $date_, $memo_);
109
110         return true;
111 }
112
113 //--------------------------------------------------------------------------------------------------
114
115 function get_voided_entry($type, $type_no)
116 {
117         $sql = "SELECT * FROM ".TB_PREF."voided WHERE type=$type AND id=$type_no";
118
119         $result = db_query($sql, "could not query voided transaction table");
120
121         return db_fetch($result);
122 }
123
124 //--------------------------------------------------------------------------------------------------
125
126 function add_voided_entry($type, $type_no, $date_, $memo_)
127 {
128         $date = date2sql($date_);
129         $sql = "INSERT INTO ".TB_PREF."voided (type, id, date_, memo_)
130                 VALUES ($type, $type_no, ".db_escape($date).", ".db_escape($memo_).")";
131
132         db_query($sql, "could not add voided transaction entry");
133 }
134
135 //--------------------------------------------------------------------------------------------------
136
137 ?>