RT#37165: Print document when account is created [added svcnum to job]
[freeside.git] / FS / FS / part_export / print_template.pm
1 package FS::part_export::print_template;
2
3 use strict;
4
5 use base qw( FS::part_export );
6
7 use FS::Record qw(qsearchs);
8 use FS::Misc;
9 use FS::queue;
10
11 =pod
12
13 =head1 NAME
14
15 FS::part_export::print_template
16
17 =head1 SYNOPSIS
18
19 Print a document of a template.
20
21 =head1 DESCRIPTION
22
23 See the L<Text::Template> documentation and the billing documentation for details on the template substitution language.
24
25 Currently does not support printing during replace.
26
27 =cut
28
29 use vars qw( %info );
30
31 tie my %options, 'Tie::IxHash',
32   'phase'           => { label => 'Print during',
33                          type  => 'select',
34                          options => [qw(insert delete suspend unsuspend)] },
35   'template_text'   => { label => 'Template text',
36                          type => 'textarea' },
37 ;
38
39 %info = (
40                        #unfortunately, FS::part_svc->svc_tables fails at this point, not sure why
41   'svc'             => [ map { 'svc_'.$_ } qw(
42                            acct domain cert forward mailinglist www broadband cable dsl 
43                            conferencing video dish hardware phone pbx circuit port alarm external)
44                        ],
45   'desc'            => 'Print document during service change, for all services',
46   'options'         => \%options,
47   'no_machine'      => 1,
48   'notes'           => <<'EOF',
49 Will use the print command configured by the lpr setting.
50 See the <a href="http://search.cpan.org/dist/Text-Template/lib/Text/Template.pm">Text::Template</a> documentation and the billing documentation for details on the template substitution language.
51 Fields from the customer and service records are available for substitution, as well as the following fields:
52
53 <ul>
54 <li>$payby - a friendler represenation of the field</li>
55 <li>$payinfo - the masked payment information</li>
56 <li>$expdate - the time at which the payment method expires (a UNIX timestamp)</li>
57 <li>$returnaddress - the invoice return address for this customer's agent</li>
58 <li>$logo_file - the image stored in the logo.eps setting
59 </ul>
60 EOF
61 );
62
63 =head1 Hook Methods
64
65 Each of these simply invoke this module's L<print_template> method,
66 passing the appropriate phase.
67
68 =cut
69
70 =head2 _export_insert
71
72 Hook that is called when service is initially provisioned.
73 To avoid confusion, don't use for anything else.
74
75 =cut
76
77 sub _export_insert {
78   my $self = shift;
79   return $self->print_template('insert',@_);
80 }
81
82 =head2 _export_delete
83
84 Hook that is called when service is unprovisioned.
85 To avoid confusion, don't use for anything else.
86
87 =cut
88
89 sub _export_delete {
90   my $self = shift;
91   return $self->print_template('delete',@_);
92 }
93
94 =head2 _export_replace
95
96 Hook that is called when provisioned service is edited.
97 To avoid confusion, don't use for anything else.
98
99 Currently not supported for this export.
100
101 =cut
102
103 sub _export_replace {
104   return '';
105 }
106
107 =head2 _export_suspend
108
109 Hook that is called when service is suspended.
110 To avoid confusion, don't use for anything else.
111
112 =cut
113
114 sub _export_suspend {
115   my $self = shift;
116   return $self->print_template('suspend',@_);
117 }
118
119 =head2 _export_unsuspend
120
121 Hook that is called when service is unsuspended.
122 To avoid confusion, don't use for anything else.
123
124 =cut
125
126 sub _export_unsuspend {
127   my $self = shift;
128   return $self->print_template('unsuspend',@_);
129 }
130
131 =head1 Core Methods
132
133 =head2 print_template
134
135 Accepts $phase and $svc_x.
136 If phase matches the configured option, starts a L</process_print_template>
137 job in the queue.
138
139 =cut
140
141 sub print_template {
142   my ($self, $phase, $svc_x) = @_;
143   if ($self->option('phase') eq $phase) {
144     my $queue = new FS::queue {
145       'svcnum' => $svc_x->svcnum,
146       'job'    => 'FS::part_export::print_template::process_print_template',
147     };
148     my $error = $queue->insert(
149       'svcnum'        => $svc_x->svcnum,
150       'table'         => $svc_x->table,
151       'template_text' => $self->option('template_text'),
152     );
153     return "can't start print job: $error" if $error;
154   }
155   return '';
156 }
157
158 =head2 process_print_template
159
160 For use as an FS::queue job.  Requires opts svcnum, table and template_text.
161 Constructs page from template and sends to printer.
162
163 =cut
164
165 sub process_print_template {
166   my %opt = @_;
167
168   my $svc_x = qsearchs($opt{'table'}, { 'svcnum' => $opt{'svcnum'} } )
169     or die "invalid " . $opt{'table'} . " svcnum " . $opt{'svcnum'};
170   my $cust_main = $svc_x->cust_svc->cust_pkg->cust_main
171     or die "could not find customer for service";
172
173   my $ps = $cust_main->print_ps(undef,
174     'template_text' => $opt{'template_text'},
175     'extra_fields' => {
176       map { $_ => $svc_x->$_ } $svc_x->fields,
177     },
178   );
179   my $error = FS::Misc::do_print(
180     [ $ps ],
181     'agentnum' => $cust_main->agentnum,
182   );
183   die $error if $error;
184 }
185
186 =head1 SEE ALSO
187
188 L<FS::part_export>
189
190 =head1 AUTHOR
191
192 Jonathan Prykop 
193 jonathan@freeside.biz
194
195 =cut
196
197 1;
198
199