adding export scripts for ea, RT#19067
[freeside.git] / bin / svc_phone.export
diff --git a/bin/svc_phone.export b/bin/svc_phone.export
new file mode 100755 (executable)
index 0000000..aa0eb20
--- /dev/null
@@ -0,0 +1,55 @@
+#!/usr/bin/perl
+
+use strict;
+use Text::CSV_XS;
+use FS::UID qw(adminsuidsetup);
+use FS::Record qw(qsearch);
+use FS::svc_phone;
+
+my @fields = (
+  { 'header'   => 'pkgnum',
+    'callback' => sub { shift->cust_svc->pkgnum; },
+  },
+  { 'header'   => 'svcpart',
+    'callback' => sub { shift->cust_svc->svcpart; },
+  },
+  { 'header'   => 'Service',
+    'callback' => sub { shift->cust_svc->part_svc->svc; },
+  },
+  qw(
+    phonenum
+    pin
+    sip_password
+    phone_name
+  )
+);
+
+my $user = shift or die &usage;
+adminsuidsetup $user;
+
+my $agentnum = shift or die &usage;
+
+my $csv = new Text::CSV_XS;
+
+$csv->combine( map { ref($_) ? $_->{'header'} : $_ } @fields ) or die;
+print $csv->string."\n";
+
+my @svc_phone = qsearch({
+  'table'     => 'svc_phone',
+  'addl_from' => 'LEFT JOIN cust_svc USING (svcnum)
+                  LEFT JOIN cust_pkg USING (pkgnum)
+                  LEFT JOIN cust_main USING ( custnum )',
+  'hashref'   => {},
+  'extra_sql' => "WHERE cust_main.agentnum = $agentnum",
+});
+
+foreach my $svc_phone ( @svc_phone ) {
+  $csv->combine( map { ref($_) ? &{$_->{'callback'}}($svc_phone)
+                               : $svc_phone->$_()
+                     } 
+                   @fields
+               ) or die;
+  print $csv->string."\n";
+}
+
+1;