8efc0036dbdd0d13451545894da9200601ce363a
[fa-stable.git] / admin / db / voiding_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 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 ST_JOURNAL : // it's a journal entry
26                         if (!exists_gl_trans($type, $type_no))
27                                 return false;
28                         void_journal_trans($type, $type_no);
29                         break;
30
31                 case ST_BANKPAYMENT : // it's a payment
32                 case ST_BANKDEPOSIT : // it's a deposit
33                 case ST_BANKTRANSFER : // it's a transfer
34                         if (!exists_bank_trans($type, $type_no))
35                                 return false;
36                         void_bank_trans($type, $type_no);
37                         break;
38
39                 case ST_SALESINVOICE : // it's a customer invoice
40                 case ST_CUSTCREDIT : // it's a customer credit note
41                 case ST_CUSTPAYMENT : // it's a customer payment
42                 case ST_CUSTDELIVERY : // it's a customer dispatch
43                         if (!exists_customer_trans($type, $type_no))
44                                 return false;
45                         if ($type == 13)        // added 04 Oct 2008 by Joe Hunt. If delivery note has a not voided invoice, then NO.
46                         {
47                                 $delivery = get_customer_trans($type_no, $type);
48                                 if ($delivery['trans_link'] != 0)
49                                 {
50                                         if (get_voided_entry(10, $delivery['trans_link']) === false)
51                                                 return false;
52                                 }
53                         }       
54                         post_void_customer_trans($type, $type_no);
55                         break;
56
57                 case ST_LOCTRANSFER : // it's a stock transfer
58                         if (get_stock_transfer_items($type_no) == null)
59                                 return false;
60                         void_stock_transfer($type_no);
61                         break;
62
63                 case ST_INVADJUST : // it's a stock adjustment
64                         if (get_stock_adjustment_items($type_no) == null)
65                                 return false;
66                         void_stock_adjustment($type_no);
67                         break;
68
69                 case ST_PURCHORDER : // it's a PO
70                 case ST_SUPPRECEIVE : // it's a GRN
71                         return false;
72                 case ST_SUPPINVOICE : // it's a suppler invoice
73                 case ST_SUPPCREDIT : // it's a supplier credit note
74                 case ST_SUPPAYMENT : // it's a supplier payment
75                         if (!exists_supp_trans($type, $type_no))
76                                 return false;
77                         if (!post_void_supp_trans($type, $type_no))
78                                 return false;
79                         break;
80
81                 case ST_WORKORDER : // it's a work order
82                         if (!get_work_order($type_no, true))
83                                 return false;
84                         void_work_order($type_no);
85                         break;
86
87                 case ST_MANUISSUE : // it's a work order issue
88                         if (!exists_work_order_issue($type_no))
89                                 return false;
90                         void_work_order_issue($type_no);
91                         break;
92
93                 case ST_MANURECEIVE : // it's a work order production
94                         if (!exists_work_order_produce($type_no))
95                                 return false;
96                         void_work_order_produce($type_no);
97                         break;
98
99                 case ST_SALESORDER: // it's a sales order
100                 case ST_SALESQUOTE: // it's a sales quotation
101                         return false;
102
103                 case ST_COSTUPDATE : // it's a stock cost update
104                         return false;
105                         break;
106         }
107
108         // only add an entry if it's actually been voided
109         add_audit_trail($type, $type_no, $date_, _("Voided.")."\n".$memo_);
110         add_voided_entry($type, $type_no, $date_, $memo_);
111         return true;
112 }
113
114 //--------------------------------------------------------------------------------------------------
115
116 function get_voided_entry($type, $type_no)
117 {
118         $sql = "SELECT * FROM ".TB_PREF."voided WHERE type=$type AND id=$type_no";
119
120         $result = db_query($sql, "could not query voided transaction table");
121
122         return db_fetch($result);
123 }
124
125 //--------------------------------------------------------------------------------------------------
126
127 function add_voided_entry($type, $type_no, $date_, $memo_)
128 {
129         $date = date2sql($date_);
130         $sql = "INSERT INTO ".TB_PREF."voided (type, id, date_, memo_)
131                 VALUES ($type, $type_no, ".db_escape($date).", ".db_escape($memo_).")";
132
133         db_query($sql, "could not add voided transaction entry");
134 }
135
136 //--------------------------------------------------------------------------------------------------
137
138 ?>