no need for fix for #74270 in 5.x, FK enforcement was strict in 4.x
[freeside.git] / FS / FS / cust_refund.pm
index e3fc910..12ab0d6 100644 (file)
@@ -82,6 +82,10 @@ Payment Type (See L<FS::payinfo_Mixin> for valid payby values)
 
 Payment Information (See L<FS::payinfo_Mixin> for data format)
 
+=item paycardtype
+
+Detected credit card type, if appropriate; autodetected.
+
 =item paymask
 
 Masked payinfo (See L<FS::payinfo_Mixin> for how this works)
@@ -143,16 +147,23 @@ sub insert {
   local $FS::UID::AutoCommit = 0;
   my $dbh = dbh;
 
-  unless ($self->reasonnum) {
-    my $result = $self->reason( $self->getfield('reason'),
-                                exists($options{ 'reason_type' })
-                                  ? ('reason_type' => $options{ 'reason_type' })
-                                  : (),
-                              );
-    unless($result) {
+  if (!$self->reasonnum) {
+    my $reason_text = $self->get('reason')
+      or return "reason text or existing reason required";
+    my $reason_type = $options{'reason_type'}
+      or return "reason type required";
+
+    local $@;
+    my $reason = FS::reason->new_or_existing(
+      reason => $reason_text,
+      type   => $reason_type,
+      class  => 'F',
+    );
+    if ($@) {
       $dbh->rollback if $oldAutoCommit;
-      return "failed to set reason for $me"; #: ". $dbh->errstr;
+      return "failed to set refund reason: $@";
     }
+    $self->set('reasonnum', $reason->reasonnum);
   }
 
   $self->setfield('reason', '');
@@ -278,7 +289,8 @@ otherwise returns false.
 
 sub replace {
   my $self = shift;
-  return "Can't modify closed refund" if $self->closed =~ /^Y/i;
+  return "Can't modify closed refund" 
+    if $self->closed =~ /^Y/i && !$FS::payinfo_Mixin::allow_closed_replace;
   $self->SUPER::replace(@_);
 }
 
@@ -303,6 +315,7 @@ sub check {
     || $self->ut_numbern('_date')
     || $self->ut_textn('paybatch')
     || $self->ut_enum('closed', [ '', 'Y' ])
+    || $self->ut_foreign_keyn('source_paynum', 'cust_pay', 'paynum')
   ;
   return $error if $error;
 
@@ -370,6 +383,59 @@ sub unapplied {
   sprintf("%.2f", $amount );
 }
 
+=item send_receipt HASHREF | OPTION => VALUE ...
+
+Sends a payment receipt for this payment.
+
+refund_receipt_msgnum must be configured.
+
+Available options:
+
+=over 4
+
+=item cust_main
+
+Customer (FS::cust_main) object (for efficiency).
+
+=cut
+
+=back
+
+=cut
+
+sub send_receipt {
+  my $self = shift;
+  my $opt = ref($_[0]) ? shift : { @_ };
+
+  my $cust_main = $opt->{'cust_main'} || $self->cust_main;
+
+  my $conf = new FS::Conf;
+  
+  my $msgnum = $conf->config('refund_receipt_msgnum', $cust_main->agentnum);
+  return "No refund_receipt_msgnum configured" unless $msgnum;
+
+  my $msg_template = qsearchs('msg_template',{ msgnum => $msgnum});
+  return "Could not load template"
+    unless $msg_template;
+
+  my $cust_msg = $msg_template->prepare(
+    'cust_main'     => $cust_main,
+    'object'        => $self,
+    'msgtype'       => 'receipt',
+  );
+  return 'Error preparing message' unless $cust_msg;
+  my $error = $cust_msg->insert;
+  return $error if $error;
+
+  my $queue = new FS::queue {
+    'job'     => 'FS::cust_msg::process_send',
+    'custnum' => $cust_main->custnum,
+  };
+  $error = $queue->insert( $cust_msg->custmsgnum );
+
+  return $error;
+}
+
 =back
 
 =head1 CLASS METHODS
@@ -411,6 +477,9 @@ sub _upgrade_data {  # class method
   my ($class, %opts) = @_;
   $class->_upgrade_reasonnum(%opts);
   $class->_upgrade_otaker(%opts);
+
+  local $ignore_empty_reasonnum = 1;
+  $class->upgrade_set_cardtype;
 }
 
 =back