72cc3390427cd2ef1f003830a099391572f6d6c0
[fa-stable.git] / reporting / includes / pdf_report.inc
1 <?php
2 /* $Revision$ */
3 $page_security = 8;
4 include_once($path_to_root . "reporting/includes/class.pdf.inc");
5 include_once($path_to_root . "admin/db/company_db.inc");
6
7 class FrontReport extends Cpdf
8 {
9         var $size;
10         var $company;
11         var $user;
12         var $host;
13         var $fiscal_year;
14         var $title;
15         var $filename;
16         var $pageWidth;
17         var $pageHeight;
18         var $topMargin;
19         var $bottomMargin;
20         var $leftMargin;
21         var $rightMargin;
22         var $endLine;
23         var $lineHeight;
24         var $rtl;
25
26         var $cols;
27         var $params;
28         var $headers;
29         var $aligns;
30         var $headers2;
31         var $aligns2;
32         var $cols2;
33         var $companyCol;
34         var $titleCol;
35         var $pageNumber;
36         var $fontSize;
37         var $currency;
38
39         function FrontReport($title, $filename, $size = 'A4', $fontsize = 9)
40         {
41                 switch ($size) 
42                 {
43
44                   case 'A4':
45                   case 'a4':
46                           $this->pageWidth=595;
47                           $this->pageHeight=842;
48                           $this->topMargin=40;
49                           $this->bottomMargin=30;
50                           $this->leftMargin=40;
51                           $this->rightMargin=30;
52                           break;
53                   case 'A4_Landscape':
54                           $this->pageWidth=842;
55                           $this->pageHeight=595;
56                           $this->topMargin=30;
57                           $this->bottomMargin=30;
58                           $this->leftMargin=40;
59                           $this->rightMargin=30;
60                           break;
61                    case 'A3':
62                           $this->pageWidth=842;
63                           $this->pageHeight=1190;
64                           $this->topMargin=50;
65                           $this->bottomMargin=50;
66                           $this->leftMargin=50;
67                           $this->rightMargin=40;
68                           break;
69                    case 'A3_landscape':
70                           $this->pageWidth=1190;
71                           $this->pageHeight=842;
72                           $this->topMargin=50;
73                           $this->bottomMargin=50;
74                           $this->leftMargin=50;
75                           $this->rightMargin=40;
76                           break;
77                    case 'letter':
78                    case 'Letter':
79                           $this->pageWidth=612;
80                           $this->pageHeight=792;
81                           $this->topMargin=30;
82                           $this->bottomMargin=30;
83                           $this->leftMargin=30;
84                           $this->rightMargin=25;
85                           break;
86                    case 'letter_landscape':
87                           $this->pageWidth=792;
88                           $this->pageHeight=612;
89                           $this->topMargin=30;
90                           $this->bottomMargin=30;
91                           $this->leftMargin=30;
92                           $this->rightMargin=25;
93                           break;
94                    case 'legal':
95                           $this->pageWidth=612;
96                           $this->pageHeight=1008;
97                           $this->topMargin=50;
98                           $this->bottomMargin=40;
99                           $this->leftMargin=30;
100                           $this->rightMargin=25;
101                           break;
102                    case 'legal_landscape':
103                           $this->pageWidth=1008;
104                           $this->pageHeight=612;
105                           $this->topMargin=50;
106                           $this->bottomMargin=40;
107                           $this->leftMargin=30;
108                           $this->rightMargin=25;
109                           break;
110                 }
111                 $this->size = array(0, 0, $this->pageWidth, $this->pageHeight);
112                 $this->title = $title;
113                 $this->filename = $filename;
114                 $this->pageNumber = 0;
115                 $this->endLine = $this->pageWidth - $this->rightMargin;
116                 $this->companyCol = $this->endLine - 150;
117                 $this->titleCol = $this->leftMargin + 100;
118                 $this->lineHeight = 12;
119                 $this->fontSize = $fontsize;
120                 $this->currency = '';
121                 $this->rtl = ($_SESSION['language']->dir == 'rtl');
122                 // for quick testing
123                 //$this->rtl = true;
124                 $this->Cpdf($this->size);
125         }
126
127         function Font($style = 'normal')
128         {
129                 global $path_to_root;
130                 $own = false;
131                 $locale = $path_to_root . "lang/" . $_SESSION['language']->code . "/locale.inc";
132                 if (file_exists($locale))
133                 {
134                         $fontinclude = true;
135                         include($locale);
136                 }       
137                 //if ($this->rtl) // this is good for presentation only
138                 if (!$own)
139                 {
140                         if (user_price_dec() == 3) // only for presentation of farsi
141                         {
142                                 $this->rtl = true;
143                                 $path = $path_to_root . 'reporting/fonts/farsi_1.afm';
144                         }               
145                         elseif ($style == 'italic')
146                                 $path = $path_to_root . 'reporting/fonts/Helvetica-Oblique.afm';
147                         elseif ($style == 'bold')
148                                 $path = $path_to_root . 'reporting/fonts/Helvetica-Bold.afm';
149                         else // even take misspelled styles
150                                 $path = $path_to_root . 'reporting/fonts/Helvetica.afm';
151                 }               
152                 $this->selectFont($path, 'WinAnsiEncoding');
153         }
154
155         function Info($params, $cols, $headers, $aligns,
156                 $cols2 = null, $headers2 = null, $aligns2 = null)
157         {
158                 global $app_title, $version, $power_by, $power_url, $path_to_root,      $db_connections;
159
160                 $this->addinfo('Title', $this->title);
161                 $this->addinfo('Subject', $this->title);
162                 $this->addinfo('Author', $app_title . ' ' . $version);
163                 $this->addinfo('Creator',$power_by . ' - ' . $power_url);
164                 $year = get_current_fiscalyear();
165                 if ($year['closed'] == 0)
166                         $how = _("Active");
167                 else
168                         $how = _("Closed");
169                 $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . "  " . "(" . $how . ")";
170                 $this->company = get_company_prefs();
171                 $this->user = $_SESSION["wa_current_user"]->name;
172                 //$this->host = $db_connections[$_SESSION["wa_current_user"]->company]["host"];
173                 $this->host = $_SERVER['SERVER_NAME'];
174                 $this->params = $params;
175                 $this->cols = $cols;
176                 for ($i = 0; $i < count($this->cols); $i++)
177                         $this->cols[$i] += $this->leftMargin;
178                 $this->headers = $headers;
179                 $this->aligns = $aligns;
180                 $this->cols2 = $cols2;
181                 if ($this->cols2 != null)
182                 {
183                         for ($i = 0; $i < count($this->cols2); $i++)
184                                 $this->cols2[$i] += $this->leftMargin;
185                 }
186                 $this->headers2 = $headers2;
187                 $this->aligns2 = $aligns2;
188         }
189
190         function Header()
191         {
192                 $this->pageNumber++;
193                 if ($this->pageNumber > 1)
194                         $this->newPage();
195                 $this->row = $this->pageHeight - $this->topMargin;
196
197                 $this->SetDrawColor(128, 128, 128);
198                 $this->Line($this->row + 5, 1);
199
200                 $this->NewLine();
201
202                 $this->fontSize += 4;
203                 $this->Font('bold');
204                 $this->Text($this->leftMargin, $this->title, $this->companyCol);
205                 $this->Font();
206                 $this->fontSize -= 4;
207                 $this->Text($this->companyCol, $this->company['coy_name']);
208                 $this->row -= ($this->lineHeight + 4);
209
210                 $str = _("Print Out Date") . ':';
211                 $this->Text($this->leftMargin, $str, $this->titleCol);
212                 $str = Today() . '   ' . Now();
213                 $this->Text($this->titleCol, $str, $this->companyCol);
214                 $this->Text($this->companyCol, $this->host);
215
216                 $this->NewLine();
217                 $str = _("Fiscal Year") . ':';
218                 $this->Text($this->leftMargin, $str, $this->titleCol);
219                 $str = $this->fiscal_year;
220                 $this->Text($this->titleCol, $str, $this->companyCol);
221                 $this->Text($this->companyCol, $this->user);
222                 for ($i = 1; $i < count($this->params); $i++)
223                 {
224                         if ($this->params[$i]['from'] != '')
225                         {
226                                 $this->NewLine();
227                                 $str = $this->params[$i]['text'] . ':';
228                                 $this->Text($this->leftMargin, $str, $this->titleCol);
229                                 $str = $this->params[$i]['from'];
230                                 if ($this->params[$i]['to'] != '')
231                                         $str .= " - " . $this->params[$i]['to'];
232                                 $this->Text($this->titleCol, $str, $this->companyCol);
233                         }
234                 }       
235                 if ($this->params[0] != '') // Comments
236                 {
237                         $this->NewLine();
238                         $str = _("Comments") . ':';
239                         $this->Text($this->leftMargin, $str, $this->titleCol);
240                         $this->Font('bold');
241                         $this->Text($this->titleCol, $this->params[0], $this->endLine - 35);
242                         $this->Font();
243                 }
244                 $str = _("Page") . ' ' . $this->pageNumber;
245                 $this->Text($this->endLine - 35, $str);
246                 $this->Line($this->row - 5, 1);
247
248                 $this->row -= ($this->lineHeight + 6);
249                 $this->Font('italic');
250                 if ($this->headers2 != null)
251                 {
252                         $count = count($this->headers2);
253                         for ($i = 0; $i < $count; $i++)
254                                 $this->TextCol2($i, $i + 1,     $this->headers2[$i]);
255                         $this->NewLine();
256                 }
257                 $count = count($this->headers);
258                 for ($i = 0; $i < $count; $i++)
259                         $this->TextCol($i, $i + 1, $this->headers[$i]);
260                 $this->Font();
261                 $this->Line($this->row - 5, 1);
262
263                 $this->NewLine(2);
264         }
265
266         function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
267         {
268                 global $path_to_root, $print_as_quote;
269                 
270                 $this->pageNumber++;
271                 if ($this->pageNumber > 1)
272                         $this->newPage();
273                 $header2type = true;
274                 if ($this->currency != $myrow['curr_code'])
275                 {
276                         include($path_to_root . "reporting/includes/doctext2.inc");                     
277                 }       
278                 else
279                 {
280                         include($path_to_root . "reporting/includes/doctext.inc");                      
281                 }       
282                 $this->row = $this->pageHeight - $this->topMargin;
283                 
284                 $upper = $this->row - 2 * $this->lineHeight;
285                 $lower = $this->bottomMargin + 6 * $this->lineHeight;
286                 $iline1 = $upper - 4 * $this->lineHeight;
287                 $iline2 = $iline1 - 8 * $this->lineHeight;
288                 $iline3 = $iline2 - 8 * $this->lineHeight;
289                 $iline4 = $iline3 - 2 * $this->lineHeight;
290                 $iline5 = $lower + 5 * $this->lineHeight;
291                 $icol = $this->pageWidth / 2;
292                 $ccol = $this->cols[0] + 4;
293                 $ccol2 = $icol / 2;
294                 $mcol = $icol + 8;
295                 $mcol2 = $this->pageWidth - $ccol2;
296
297                 $this->SetDrawColor(128, 128, 128);
298                 $this->LineTo($this->pageWidth - $this->rightMargin, $upper ,$this->leftMargin, $upper);
299                 $this->LineTo($this->leftMargin, $upper ,$this->leftMargin, $lower);
300                 $this->LineTo($this->pageWidth - $this->rightMargin, $lower ,$this->leftMargin, $lower);
301                 $this->LineTo($this->pageWidth - $this->rightMargin, $lower ,$this->pageWidth - $this->rightMargin, $upper);
302                 $this->Line($iline1);
303                 $this->Line($iline2);
304                 $this->Line($iline3);
305                 $this->Line($iline4);
306                 $this->Line($iline5);
307                 $this->LineTo($icol, $upper ,$icol, $iline1);
308                 $this->NewLine();
309
310                 $this->fontSize += 4;
311                 $this->Font('bold');
312                 $this->Text($mcol, $this->title);
313                 $this->Font();
314                 $this->fontSize -= 4;
315                 if ($this->pageNumber > 1 && !strstr($this->filename, "Bulk"))
316                         $this->Text($this->endLine - 35, _("Page") . ' ' . $this->pageNumber);
317                 $this->fontSize -= 4;
318                 $this->row = $upper - 5;
319                 
320                 $this->Text($mcol, $doc_Invoice_no, $mcol + 90);
321                 $this->Text($mcol + 90, $doc_Cust_no, $mcol + 180);
322                 $this->Text($mcol + 180, $doc_Date);
323                 $this->fontSize += 4;
324
325                 $this->row = $upper - 2 * $this->lineHeight - 2;
326                 if ($this->company['coy_logo'] != '')
327                 {
328                         $logo = $path_to_root . "themes/default/images/" . $this->company['coy_logo'];
329                         $this->AddImage($logo, $ccol, $iline1 + 5, 220, 40);
330                 }
331                 else
332                 {
333                         $this->fontSize += 4;
334                         $this->Font('bold');
335                         $this->Text($ccol, $this->company['coy_name'], $icol);
336                         $this->Font();
337                         $this->fontSize -= 4;
338                 }
339                 if ($doctype == 8) // PO
340                         $this->Text($mcol, $myrow['order_no'], $mcol + 90);
341                 else if ($doctype == 9) // SO
342                         $this->Text($mcol, $myrow['order_no'] ." ".$myrow['customer_ref'], $mcol + 90);
343                 else // INV/CRE/STA     
344                         $this->Text($mcol, $myrow['reference'], $mcol + 90);
345                 $this->Text($mcol + 90, $myrow['debtor_no'], $mcol + 180);
346                 if ($doctype == 8 || $doctype == 9)
347                         $this->Text($mcol + 180, sql2date($myrow['ord_date']));
348                 else    
349                         $this->Text($mcol + 180, sql2date($myrow['tran_date']));
350
351                 $this->fontSize -= 4;
352                 $this->row = $iline1 - 5;
353                 $this->Text($ccol, $doc_Charge_To, $icol);
354                 $this->Text($mcol, $doc_Delivered_To);
355                 $this->fontSize += 4;
356                 
357                 $this->NewLine(2);
358                 $temp = $this->row;
359                 if ($doctype == 9)
360                 {
361                         $this->Text($ccol, $myrow['name'], $icol);
362                         //$adr = ?;
363                 }       
364                 else
365                 {
366                         if ($doctype == 8)
367                                 $this->Text($ccol, $myrow['supp_name'], $icol);
368                         else    
369                                 $this->Text($ccol, $myrow['DebtorName'], $icol);
370                         $adr = explode("\n", $myrow['address']);
371                 }       
372                 for ($i = 0; $i < count($adr); $i++)
373                 {       
374                         $this->NewLine();
375                         $this->Text($ccol, $adr[$i], $icol);
376                 }
377                 if ($sales_order != NULL)
378                 {
379                         $this->row = $temp;
380                         if ($doctype == 8)
381                                 $this->Text($mcol, $this->company['coy_name']);
382                         else    
383                                 $this->Text($mcol, $sales_order['deliver_to']);
384                         $adr = explode("\n", $sales_order['delivery_address']);
385                         for ($i = 0; $i < count($adr); $i++)
386                         {       
387                                 $this->NewLine();
388                                 $this->Text($mcol, $adr[$i]);
389                         }
390                 }
391                 $this->row = $iline2 - 2 * $this->lineHeight;
392                 $this->Text($ccol, $doc_Shipping_Company . ":", $ccol2);
393                 if ($doctype != 8)
394                         $this->Text($ccol2, $myrow['shipper_name'], $mcol);
395                 $this->Text($mcol, $doc_Due_Date . ":", $mcol2);
396                 if ($doctype == 9)
397                         $this->Text($mcol2, sql2date($myrow['delivery_date']));
398                 else if ($doctype != 8) 
399                         $this->Text($mcol2, sql2date($myrow['due_date']));
400                 if ($branch != null)
401                 {
402                         $this->NewLine();
403                         $this->Text($ccol, $doc_Your_Ref . ":", $ccol2);
404                         $this->Text($ccol2, $branch['contact_name'], $mcol);
405                         $this->Text($mcol, $doc_Our_Ref . ":", $mcol2);
406
407                         $id = $branch['salesman'];
408                         $sql = "SELECT salesman_name  FROM ".TB_PREF."salesman WHERE salesman_code='$id'";
409                         $result = db_query($sql,"could not get sales person");
410                         $row = db_fetch($result);
411
412                         $this->Text($mcol2, $row['salesman_name']);
413                 }       
414                 $this->NewLine();
415                 $this->Text($ccol, $doc_Your_VAT_no . ":", $ccol2);
416                 if ($doctype != 8)
417                         $this->Text($ccol2, $myrow['tax_id'], $mcol);
418                 $this->Text($mcol, $doc_Our_VAT_no . ":", $mcol2);
419                 $this->Text($mcol2, $this->company['gst_no']);
420                 $this->NewLine();
421                 $this->Text($ccol, $doc_Payment_Terms . ":", $ccol2);
422
423                 $id = $myrow['payment_terms'];
424                 $sql = "SELECT terms FROM ".TB_PREF."payment_terms WHERE terms_indicator='$id'";
425                 $result = db_query($sql,"could not get paymentterms");
426                 $row = db_fetch($result);
427
428                 $this->Text($ccol2, $row["terms"], $mcol);
429                 $this->Text($mcol, $doc_Our_Order_No . ":", $mcol2);
430                 $this->Text($mcol2, $myrow['order_']);
431                 
432                 $locale = $path_to_root . "lang/" . $_SESSION['language']->code . "/locale.inc";
433                 if (file_exists($locale))
434                 {
435                         $header2include = true;
436                         include($locale);
437                 }       
438                 $this->row = $iline3 - $this->lineHeight - 2;
439                 $this->Font('bold');
440                 $count = count($this->headers);
441                 for ($i = 0; $i < $count; $i++)
442                         $this->TextCol($i, $i + 1, $this->headers[$i], -2);
443                 $this->Font();
444                 $temp = $this->row - 2 * $this->lineHeight;
445                 $this->row = $iline5 - $this->lineHeight - 6;
446                 $this->Text($ccol, $doc_Please_Quote . " - " . $myrow['curr_code']);
447                 if ($this->params['comments'] != '')
448                 {
449                         $this->NewLine(2);
450                         $this->Font('bold');
451                         $this->Text($ccol, $this->params['comments']);
452                         $this->Font();
453                 }
454                 
455                 $this->row = $lower - 5;
456                 $this->fontSize -= 4;
457                 
458                 $this->Text($ccol, $doc_Address, $ccol2 + 40);
459                 $this->Text($ccol2 + 30, $doc_Phone_Fax_Email, $mcol);
460                 $this->Text($mcol , $doc_Bank, $mcol2);
461                 $this->Text($mcol2, $doc_Bank_Account);
462                 $this->fontSize += 4;
463                 $this->NewLine();
464                 $adrline = $this->row; 
465                 
466                 $adr = explode("\n", $this->company['postal_address']);
467                 for ($i = 0; $i < count($adr); $i++)
468                 {       
469                         $this->NewLine();
470                         $this->Text($ccol, $adr[$i], $ccol2 + 40);
471                 }
472                 $this->row = $adrline; 
473                 $this->Text($ccol2 + 30, $this->company['phone'], $mcol);
474                 $this->NewLine();
475                 $this->Text($ccol2 + 30, $this->company['fax'], $mcol);
476                 $this->NewLine();
477                 $this->Text($ccol2 + 30, $this->company['email'], $mcol);
478                 // fetch this later
479                 $this->row = $adrline; 
480                 $this->Text($mcol, $bankaccount['bank_name'], $mcol2);
481                 $adr = explode("\n", $bankaccount['bank_address']);
482                 for ($i = 0; $i < count($adr); $i++)
483                 {       
484                         $this->NewLine();
485                         $this->Text($mcol, $adr[$i], $mcol2);
486                 }
487                 
488                 $this->row = $adrline; 
489                 $this->Text($mcol2, $bankaccount['bank_account_name']);
490                 $this->NewLine();
491                 $this->Text($mcol2, $bankaccount['bank_account_number']);
492                 $this->row = $temp;
493         }
494
495         function AddImage($logo, $x, $y, $w, $h)
496         {
497                 if ($this->rtl)
498                         $x = $this->pageWidth - $x - $w;
499                 if (strpos($logo, ".png") || strpos($logo, ".PNG"))
500                         $this->addPngFromFile($logo, $x, $y, $w, $h);
501                 else
502                         $this->addJpegFromFile($logo, $x, $y, $w, $h);
503         }       
504
505         function SetDrawColor($r, $g, $b)
506         {
507                 $this->setStrokeColor($r / 255, $g / 255, $b / 255);
508         }
509         
510         function SetTextColor($r, $g, $b)
511         {
512                 $this->setColor($r / 255, $g / 255, $b / 255);
513         }
514
515         function Text($c, $txt, $n=0, $corr=0, $r=0)
516         {
517                 if ($n == 0)
518                         $n = $this->pageWidth - $this->rightMargin;
519
520                 return $this->TextWrap($c, $this->row - $r, $n - $c + $corr, $txt, 'left'); 
521         }
522         
523         function TextWrap($xpos, $ypos, $len, $str, $align = 'left') 
524         {
525                 if ($this->rtl)
526                 {
527                         $str = strrev($str);
528                         $xpos = $this->pageWidth - $xpos - $len;
529                         if ($align == 'left')
530                                 $align = 'right';
531                         elseif ($align == 'right')
532                                 $align = 'left';
533                 }       
534                 return $this->addTextWrap($xpos, $ypos, $len, $this->fontSize, $str, $align);
535         }
536
537         function TextCol($c, $n, $txt, $corr=0, $r=0)
538         {
539                 return $this->TextWrap($this->cols[$c], $this->row - $r, $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
540         }
541         
542         function TextCol2($c, $n, $txt, $corr=0, $r=0)
543         {
544                 return $this->TextWrap($this->cols2[$c], $this->row - $r, $this->cols2[$n] - $this->cols2[$c] + $corr, $txt, $this->aligns2[$c]);
545         }
546         
547         function TextColLines($c, $n, $txt, $corr=0)
548         {
549                 $str = Explode("\n", $txt);
550                 for ($i = 0; $i < count($str); $i++)
551                 {
552                         $l = $str[$i];
553                         do 
554                         {
555                                 $l = $this->TextCol($c, $n, $l, $corr); 
556                                 $this->NewLine();
557                         }
558                         while ($l != '');
559                 }       
560         }
561
562         function LineTo($from, $row, $to, $row2) 
563         {
564                 Cpdf::line($from, $row, $to, $row2);
565         }
566
567         function Line($row, $height = 0) 
568         {
569                 $this->setLineStyle($height + 1);
570                 Cpdf::line($this->pageWidth - $this->rightMargin, $row ,$this->leftMargin, $row);
571         }
572
573         function NewLine($l=1, $np=0)
574         {
575                 $this->row -= ($l * $this->lineHeight);
576                 if ($np > 0 &&  $this->row < $this->bottomMargin + ($np * $this->lineHeight))
577                         $this->Header();
578         }               
579
580         function End($email=0, $subject=null, $myrow=null, $doctype = 0)
581         {
582                 global $go_debug, $path_to_root;
583
584                 // this is no good in IE so has been replaced, see down under
585                 // but good for debugging purposes in IE
586                 //session_write_close();
587                 
588                 if ($go_debug == 1) 
589                 {
590                         $buf = $this->output(1);
591                         $len = strlen($buf);
592                         $pdfcode = $buf;
593                         $pdfcode = str_replace("\n", "\n<br>", htmlspecialchars($pdfcode));
594                         echo '<html><body>';
595                         echo trim($pdfcode);
596                         echo '</body></html>';
597                         //header("Content-Length: $len");
598                         //header("Content-Disposition: inline; filename=" . $this->filename);
599                         //header('Expires: 0');
600                         //header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
601                         //header('Pragma: public');
602
603                         //$this->pdf->stream();
604                 } 
605                 else 
606                 {
607                         $buf = $this->output();
608                         $len = strlen($buf);
609                         $dir = './pdf_files';
610                         //save the file
611                         if (!file_exists($dir))
612                         {
613                                 mkdir ($dir,0777);
614                         }
615                         $fname = $dir . '/' . $this->filename;
616                         $fp = fopen($fname,'w');
617                         fwrite($fp,$buf);
618                         fclose($fp);
619                         if ($email == 1)
620                         {
621                                 $emailtype = true;
622                                 if ($this->currency != $myrow['curr_code'])
623                                 {
624                                         include("doctext2.inc");                        
625                                 }       
626                                 else
627                                 {
628                                         include("doctext.inc");                 
629                                 }       
630                                 require_once($path_to_root . "reporting/includes/class.mail.inc");
631                         $mail = new email($this->company['coy_name'], $this->company['email']);
632                         $from = $this->company['coy_name'] . " <" . $this->company['email'] . ">";
633                         $to = $myrow['DebtorName'] . " <" . $myrow['email'] . ">";
634                         $msg = $doc_Dear_Sirs . ",\n\n" . $doc_AttachedFile . " " . $subject . 
635                                 "\n\n" . $doc_Kindest_regards . "\n\n";
636                         $sender = $this->user . "\n" . $this->company['coy_name'];
637                         $mail->to($to);
638                         $mail->subject($subject);
639                         $mail->text($msg . $sender);
640                         $mail->attachment($fname);
641                         $ret = $mail->send();
642                         if (1 == 1) // just for fun and for debugging purposes!!
643                         {
644                                 $from = str_replace("<", "(", $from);
645                                 $from = str_replace(">", ")", $from);
646                                 $to = str_replace("<", "(", $to);
647                                 $to = str_replace(">", ")", $to);
648                                 $msg2 = "<br>From: " . $from;
649                                 $msg2 .= "<br>To: " . $to;
650                                 $msg2 .= "<br>Subject: " . $subject;
651                                 $msg2 .= "<br>Msg: " . nl2br($msg);
652                                 $msg2 .= nl2br($sender) . "<br>";
653                                 $msg2 .= "<br>Filepath: " . $fname . "<br>Filename: " . $this->filename . "<br>";
654                         }
655                         if ($ret)
656                                 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("sent to") . " ";
657                         else
658                                 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("NOT sent to") . " ";
659                         $msg2 .= $str . $myrow['DebtorName'] . " - " . $myrow['email'];
660                         echo "<html><body>";
661                         echo $msg2;
662                         echo "</body></html>";
663                         }
664                         else
665                         {
666                                 echo '<html>
667                                                 <head>
668                                                   <SCRIPT LANGUAGE="JavaScript"><!--
669                                               function go_now () { window.location.href = "'.$fname.'"; }
670                                               //--></SCRIPT>
671                                           </head>
672                                           <body onLoad="go_now()"; >
673                                             <a href="'.$fname.'">click here</a> if you are not re-directed.
674                                           </body>
675                                     </html>';
676                         }           
677                         // also have a look through the directory, and remove the files that are older than a week
678                         // rather want to save 'em
679                         /*if ($d = @opendir($dir)) {
680                                 while (($file = readdir($d)) !== false) {
681                                         // then check to see if this one is too old
682                                         $ftime = filemtime($dir.'/'.$file);
683                                         if (time()-$ftime > 3600*24*7){
684                                                 unlink($dir.'/'.$file);
685                                         }
686                                 }
687                                 closedir($d);
688                         }*/
689                 }
690         }
691 }
692
693 ?>