b3bba8b6e4082a592cf7f13de3de390a3cae8bc6
[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         global $Refs;
20         $void_entry = get_voided_entry($type, $type_no);
21
22         if ($void_entry != null)
23                 return _('This transaction was already voided before.');
24
25         switch ($type) {
26                 case ST_JOURNAL : // it's a journal entry
27                         if (!exists_gl_trans($type, $type_no))
28                                 return _('Selected transaction does not exists.');
29                         if ($wo = check_wo_costing($type, $type_no))
30                                 return sprintf(_('This transaction cannot be voided because it is part of Work Order %s costs.'), $wo);
31                         void_journal_trans($type, $type_no);
32                         break;
33
34                 case ST_BANKDEPOSIT : // it's a deposit
35                 case ST_BANKTRANSFER : // it's a transfer
36                         if (!check_void_bank_trans($type, $type_no))
37                                 return _('This transaction cannot be voided because the operation would decrease account balance below allowed limit in some point of account history.');
38                 case ST_BANKPAYMENT : // it's a payment
39                         if (!exists_bank_trans($type, $type_no))
40                                 return _('Selected transaction does not exists.');
41                         void_bank_trans($type, $type_no);
42                         break;
43
44                 case ST_CUSTPAYMENT : // it's a customer payment
45                         if (!check_void_bank_trans($type, $type_no))
46                                 return _('This transaction cannot be voided because the operation would decrease account balance below allowed limit in some point of account history.');
47                 case ST_SALESINVOICE : // it's a customer invoice
48                         if (is_cust_invoice_credited($type_no) && $type == ST_SALESINVOICE)
49                                 return _('This invoice cannot be voided because it was already credited.');
50                 case ST_CUSTCREDIT : // it's a customer credit note
51                 case ST_CUSTDELIVERY : // it's a customer dispatch
52                         if (!exists_customer_trans($type, $type_no))
53                                 return _('Selected transaction does not exists.');
54                         if ($type == ST_CUSTDELIVERY)   // added 04 Oct 2008 by Joe Hunt. If delivery note has a not voided invoice, then NO.
55                         {
56                                 $vers = get_customer_trans_version($type, $type_no);
57                                 if ($vers[$type_no] == 1) {
58                                         $childs = get_sales_child_lines($type, $type_no, false); // 2011-03-17 This had been changed. Joe
59                                         if ($childs && db_num_rows($childs))
60                                                 return _('This delivery cannot be voided because it was already invoiced.');
61                                 }
62                         }
63                         post_void_customer_trans($type, $type_no);
64                         break;
65
66                 case ST_LOCTRANSFER : // it's a stock transfer
67                         if (get_stock_transfer_items($type_no) == null)
68                                 return _('Selected transaction does not exists.');
69                         void_stock_transfer($type_no);
70                         break;
71
72                 case ST_INVADJUST : // it's a stock adjustment
73                         if (get_stock_adjustment_items($type_no) == null)
74                                 return _('Selected transaction does not exists.');
75                         void_stock_adjustment($type_no);
76                         break;
77
78                 case ST_PURCHORDER : // it's a PO
79                                 return _('This transaction type cannot be voided.');
80                         break;
81
82                 case ST_SUPPRECEIVE : // it's a GRN
83                         if (exists_grn_on_invoices($type_no))
84                                 return _('This GRN cannot be voided because it was already invoiced.');
85                         if (!post_void_supp_trans($type, $type_no))
86                                 return _('Error encountered when voiding transaction.');
87                         break;
88
89                 case ST_SUPPINVOICE : // it's a suppler invoice
90                 case ST_SUPPCREDIT : // it's a supplier credit note
91                 case ST_SUPPAYMENT : // it's a supplier payment
92                         if (!exists_supp_trans($type, $type_no))
93                                 return _('Selected transaction does not exists.');
94                         if (!post_void_supp_trans($type, $type_no))
95                                 return _('Error encountered when voiding transaction.');
96                         break;
97
98                 case ST_WORKORDER : // it's a work order
99                         if (!get_work_order($type_no, true))
100                                 return _('Selected transaction does not exists.');
101                         void_work_order($type_no);
102                         break;
103
104                 case ST_MANUISSUE : // it's a work order issue
105                         if (!exists_work_order_issue($type_no))
106                                 return _('Selected transaction does not exists.');
107                         if ($wo = check_void_wo_issue($type_no))
108                                 return sprintf(_('Selected document cannot be voided because related Work Order %s is already closed.'), $wo);
109                         void_work_order_issue($type_no);
110                         break;
111
112                 case ST_MANURECEIVE : // it's a work order production
113                         if (!exists_work_order_produce($type_no))
114                                 return _('Selected transaction does not exists.');
115                         if ($wo = check_void_wo_production($type_no))
116                                 return sprintf(_('Selected document cannot be voided because related Work Order %s is already closed.'), $wo);
117                         void_work_order_produce($type_no);
118                         break;
119
120                 case ST_SALESORDER: // it's a sales order
121                 case ST_SALESQUOTE: // it's a sales quotation
122                         return _('This transaction type cannot be voided.');
123
124                 case ST_COSTUPDATE : // it's a stock cost update
125                         return _('This transaction type cannot be voided.');
126                         break;
127         }
128
129         // only add an entry if it's actually been voided
130         add_audit_trail($type, $type_no, $date_, _("Voided.")."\n".$memo_);
131         $Refs->restore_last($type, $type_no);
132         add_voided_entry($type, $type_no, $date_, $memo_);
133         return false;
134 }
135
136 //--------------------------------------------------------------------------------------------------
137
138 function get_voided_entry($type, $type_no)
139 {
140         $sql = "SELECT * FROM ".TB_PREF."voided WHERE type=".db_escape($type)
141                 ." AND id=".db_escape($type_no);
142
143         $result = db_query($sql, "could not query voided transaction table");
144
145         return db_fetch($result);
146 }
147
148 //--------------------------------------------------------------------------------------------------
149
150 function add_voided_entry($type, $type_no, $date_, $memo_)
151 {
152         $date = date2sql($date_);
153         $sql = "INSERT INTO ".TB_PREF."voided (type, id, date_, memo_)
154                 VALUES (".db_escape($type).", ".db_escape($type_no).", "
155                 .db_escape($date).", ".db_escape($memo_).")";
156
157         db_query($sql, "could not add voided transaction entry");
158 }
159
160 //--------------------------------------------------------------------------------------------------
161