Added memo/comments print in remittance and receipt. Optimized memo/prints in other...
[fa-stable.git] / reporting / rep210.php
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
13 $page_security = $_POST['PARAM_0'] == $_POST['PARAM_1'] ?
14         'SA_SUPPTRANSVIEW' : 'SA_SUPPBULKREP';
15 // ----------------------------------------------------------------
16 // $ Revision:  2.0 $
17 // Creator:     Joe Hunt
18 // date_:       2005-05-19
19 // Title:       Purchase Remittance
20 // ----------------------------------------------------------------
21 $path_to_root="..";
22
23 include_once($path_to_root . "/includes/session.inc");
24 include_once($path_to_root . "/includes/date_functions.inc");
25 include_once($path_to_root . "/includes/data_checks.inc");
26 include_once($path_to_root . "/includes/db/crm_contacts_db.inc");
27
28 //----------------------------------------------------------------------------------------------------
29
30 print_remittances();
31
32 //----------------------------------------------------------------------------------------------------
33 function get_remittance($type, $trans_no)
34 {
35         $sql = "SELECT ".TB_PREF."supp_trans.*, 
36                 (".TB_PREF."supp_trans.ov_amount+".TB_PREF."supp_trans.ov_gst+".TB_PREF."supp_trans.ov_discount) AS Total, 
37                 ".TB_PREF."suppliers.supp_name,  ".TB_PREF."suppliers.supp_account_no, 
38                 ".TB_PREF."suppliers.curr_code, ".TB_PREF."suppliers.payment_terms, ".TB_PREF."suppliers.gst_no AS tax_id, 
39                 ".TB_PREF."suppliers.address
40                 FROM ".TB_PREF."supp_trans, ".TB_PREF."suppliers
41                 WHERE ".TB_PREF."supp_trans.supplier_id = ".TB_PREF."suppliers.supplier_id
42                 AND ".TB_PREF."supp_trans.type = ".db_escape($type)."
43                 AND ".TB_PREF."supp_trans.trans_no = ".db_escape($trans_no);
44         $result = db_query($sql, "The remittance cannot be retrieved");
45         if (db_num_rows($result) == 0)
46                 return false;
47     return db_fetch($result);
48 }
49
50 function get_allocations_for_remittance($supplier_id, $type, $trans_no)
51 {
52         $sql = get_alloc_supp_sql("amt, supp_reference, trans.alloc", "trans.trans_no = alloc.trans_no_to
53                 AND trans.type = alloc.trans_type_to
54                 AND alloc.trans_no_from=".db_escape($trans_no)."
55                 AND alloc.trans_type_from=".db_escape($type)."
56                 AND trans.supplier_id=".db_escape($supplier_id),
57                 TB_PREF."supp_allocations as alloc");
58         $sql .= " ORDER BY trans_no";
59         return db_query($sql, "Cannot retreive alloc to transactions");
60 }
61
62 function print_remittances()
63 {
64         global $path_to_root, $systypes_array;
65
66         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
67
68         $from = $_POST['PARAM_0'];
69         $to = $_POST['PARAM_1'];
70         $currency = $_POST['PARAM_2'];
71         $email = $_POST['PARAM_3'];
72         $comments = $_POST['PARAM_4'];
73
74         if (!$from || !$to) return;
75
76         $dec = user_price_dec();
77
78         $fno = explode("-", $from);
79         $tno = explode("-", $to);
80         $from = min($fno[0], $tno[0]);
81         $to = max($fno[0], $tno[0]);
82
83         $cols = array(4, 85, 150, 225, 275, 360, 450, 515);
84
85         // $headers in doctext.inc
86         $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right');
87
88         $params = array('comments' => $comments);
89
90         $cur = get_company_Pref('curr_default');
91
92         if ($email == 0)
93         {
94                 $rep = new FrontReport(_('REMITTANCE'), "RemittanceBulk", user_pagesize());
95                 $rep->SetHeaderType('Header2');
96                 $rep->currency = $cur;
97                 $rep->Font();
98                 $rep->Info($params, $cols, null, $aligns);
99         }
100
101         for ($i = $from; $i <= $to; $i++)
102         {
103                 if ($fno[0] == $tno[0])
104                         $types = array($fno[1]);
105                 else
106                         $types = array(ST_BANKPAYMENT, ST_SUPPAYMENT, ST_SUPPCREDIT);
107                 foreach ($types as $j)
108                 {
109                         $myrow = get_remittance($j, $i);
110                         if (!$myrow)
111                                 continue;                       
112                         $res = get_bank_trans($j, $i);
113                         $baccount = db_fetch($res);
114                         $params['bankaccount'] = $baccount['bank_act'];
115
116                         if ($email == 1)
117                         {
118                                 $rep = new FrontReport("", "", user_pagesize());
119                                 $rep->SetHeaderType('Header2');
120                                 $rep->currency = $cur;
121                                 $rep->Font();
122                                 $rep->title = _('REMITTANCE');
123                                 $rep->filename = "Remittance" . $i . ".pdf";
124                                 $rep->Info($params, $cols, null, $aligns);
125                         }
126                         else
127                                 $rep->title = _('REMITTANCE');
128                         $contacts = get_supplier_contacts($myrow['supplier_id'], 'invoice');
129                         $rep->SetCommonData($myrow, null, $myrow, $baccount, ST_SUPPAYMENT, $contacts);
130                         $rep->NewPage();
131                         $result = get_allocations_for_remittance($myrow['supplier_id'], $myrow['type'], $myrow['trans_no']);
132
133                         $doctype = ST_SUPPAYMENT;
134
135                         $total_allocated = 0;
136                         $rep->TextCol(0, 4,     _("As advance / full / part / payment towards:"), -2);
137                         $rep->NewLine(2);
138                         
139                         while ($myrow2=db_fetch($result))
140                         {
141                                 $rep->TextCol(0, 1,     $systypes_array[$myrow2['type']], -2);
142                                 $rep->TextCol(1, 2,     $myrow2['supp_reference'], -2);
143                                 $rep->TextCol(2, 3,     sql2date($myrow2['tran_date']), -2);
144                                 $rep->TextCol(3, 4,     sql2date($myrow2['due_date']), -2);
145                                 $rep->AmountCol(4, 5, $myrow2['Total'], $dec, -2);
146                                 $rep->AmountCol(5, 6, $myrow2['Total'] - $myrow2['alloc'], $dec, -2);
147                                 $rep->AmountCol(6, 7, $myrow2['amt'], $dec, -2);
148
149                                 $total_allocated += $myrow2['amt'];
150                                 $rep->NewLine(1);
151                                 if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight))
152                                         $rep->NewPage();
153                         }
154
155                         $memo = get_comments_string($j, $i);
156                         if ($memo != "")
157                         {
158                                 $rep->NewLine();
159                                 $rep->TextColLines(1, 5, $memo, -2);
160                         }
161                         $rep->row = $rep->bottomMargin + (15 * $rep->lineHeight);
162
163                         $rep->TextCol(3, 6, _("Total Allocated"), -2);
164                         $rep->AmountCol(6, 7, $total_allocated, $dec, -2);
165                         $rep->NewLine();
166                         $rep->TextCol(3, 6, _("Left to Allocate"), -2);
167                         $myrow['Total'] *= -1;
168                         $rep->AmountCol(6, 7, $myrow['Total'] - $total_allocated, $dec, -2);
169                         $rep->NewLine();
170                         $rep->Font('bold');
171                         $rep->TextCol(3, 6, _("TOTAL REMITTANCE"), - 2);
172                         $rep->AmountCol(6, 7, $myrow['Total'], $dec, -2);
173                         $words = price_in_words($myrow['Total'], ST_SUPPAYMENT);
174                         if ($words != "")
175                         {
176                                 $rep->NewLine(2);
177                                 $rep->TextCol(1, 7, $myrow['curr_code'] . ": " . $words, - 2);
178                         }       
179                         $rep->Font();
180                         if ($email == 1)
181                         {
182                                 $myrow['DebtorName'] = $myrow['supp_name'];
183                                 $rep->End($email);
184                         }
185                 }       
186         }
187         if ($email == 0)
188                 $rep->End();
189 }
190
191 ?>