Guest User

Untitled

a guest
Jan 20th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. diff --cc appserver/export/UPM.pm
  2. index 6d25185,16b603f..0000000
  3. --- a/appserver/export/UPM.pm
  4. diff --cc appserver/export/UPM.pm
  5. index 6d25185,16b603f..0000000
  6. --- a/appserver/export/UPM.pm
  7. +++ b/appserver/export/UPM.pm
  8. @@@ -7379,162 -7214,65 +7392,224 @@@ sub SetNameReviewStatus
  9. return rpc('QB::UpdateUserPaymentMethod', $upm_recno, {'Name Review Status' => $status});
  10. }
  11.  
  12. ++<<<<<<< HEAD
  13. +=item BillingAgreementCanceled($upm_recno)
  14. +
  15. +Clone specified UPM (reset Billing Agreement, Verification Completed date columns).
  16. +Keep cloned UPM primary/backup if it was such. If specified UPM is primary and
  17. +backup UPM is ACTIVE then set it as new primary UPM.
  18. +
  19. +If cloned UPM becomes primary then queue OQS job to suspend user in 24 hours
  20. +if required (See B<SuspendCompaniesIfNonBillablePrimaryUPM>). Send email
  21. +notification about possible suspension.
  22. +
  23. +B<Parameters>
  24. +
  25. +$upm_recno
  26. +
  27. + UPM recno.
  28. +
  29. +B<Return value>
  30. +
  31. + None. Exception is thrown on error.
  32. +
  33. +=cut
  34. +
  35. +sub BillingAgreementCanceled {
  36. + my ($upm_recno) = @_;
  37. +
  38. + my $t_begin = Time::HiRes::time;
  39. +
  40. + # to keep BPA history
  41. + my ($ace_recno, $history_comment, $metadata);
  42. +
  43. + try {
  44. +
  45. + # get all non system UPM fields of type 'value'
  46. + my @fields = map { $_->{'Name'} } grep (
  47. + $_->{'Class'} eq 'value' && !$_->{'IsSystem'},
  48. + @{rpc('QB::GetFieldProperties', 'UserPaymentMethods')}
  49. + );
  50. + my $upm = rpc('QB::GetUserPaymentMethod', $upm_recno, \@fields);
  51. + $ace_recno = $upm->{'Related AccountingEntity'};
  52. +
  53. + _error("UPM $upm_recno is deleted") if $upm->{'ValidUntil'};
  54. + _error("UPM $upm_recno is of unexpected type $upm->{'Related PaymentMethod'}")
  55. + unless $upm->{'Related PaymentMethod'} eq 'PAYPAL_DEPOSIT';
  56. + _error("Billing agreement is not set for UPM $upm_recno")
  57. + unless $upm->{'Billing Agreement ID'};
  58. +
  59. + my $ba = rpc('BillingAgreement::Get', $upm->{'Billing Agreement ID'});
  60. + _error("Billing agreement $ba->{rec_id} is not canceled ($ba->{status})")
  61. + unless $ba->{status} eq 'CANCELED';
  62. +
  63. + # prevent some concurrent actions on the same ACE
  64. + write_log("set lock lock_ace_$ace_recno");
  65. + execute_critical_section {
  66. + my $upms = GetUPMsByAccountingEntity($ace_recno);
  67. + $metadata = {primary_upm => $upms->{primary}, backup_upm => $upms->{backup}};
  68. +
  69. + execute_transactional {
  70. + # Clone UPM, set Date Entered to now.
  71. + # Don't clone billing agreement column.
  72. + delete $upm->{'Billing Agreement ID'};
  73. + $upm->{'Date Entered'} = any_to_msec(my_time());
  74. + my $new_recno = rpc('QB::AddUserPaymentMethod', $upm);
  75. + $metadata->{cloned_upm} = $new_recno;
  76. + $history_comment = "Clone of UPM #$upm_recno is #$new_recno";
  77. + write_log($history_comment);
  78. +
  79. + if (defined $upms->{primary} && $upms->{primary} == $upm_recno) {
  80. + # BillingAgreement is canceled on PrimaryUPM
  81. + if ($upms->{backup} && $upms->{backup_rec}{upm_status} eq 'ACTIVE') {
  82. + $history_comment = "Set BackupUPM $upms->{backup} as new PrimaryUPM";
  83. + try {
  84. + SetPrimary($upms->{backup});
  85. + $metadata->{new_primary_upm} = $upms->{backup};
  86. + # notify user that primary UPM has been changed due to BA cancellation
  87. + qrpc('orpc-notifications-queue',
  88. + 'NotifyUser::BillingAgreementCanceled',
  89. + [$upm_recno, $upms->{backup}]
  90. + );
  91. + } catch Error with {
  92. + my $err = shift;
  93. + $history_comment .= ": $err->{-text}";
  94. + $metadata->{set_backup_as_primary_err} = $history_comment;
  95. + };
  96. + write_log($history_comment);
  97. + }
  98. +
  99. + # check if backup UPM has been set as primary
  100. + if (!$metadata->{new_primary_upm}) {
  101. + $history_comment = "Set cloned UPM $new_recno as new PrimaryUPM";
  102. + try {
  103. + SetPrimary($new_recno, 1); # '1' means to allow primary UPM without BA
  104. + $metadata->{new_primary_upm} = $new_recno;
  105. + } catch Error with {
  106. + my $err = shift;
  107. + $history_comment .= ": $err->{-text}";
  108. + };
  109. + write_log($history_comment);
  110. +
  111. + # notify user about upcoming suspension due to BA cancellation
  112. + qrpc('orpc-notifications-queue', 'NotifyUser::BillingAgreementCanceled', [$upm_recno]);
  113. + }
  114. +
  115. + # schedule job to suspend AE if primary UPM will stay unbillable,
  116. + # note that we schedule this job even if backup UPM became primary
  117. + $metadata->{job_to_suspend} = qrpc(
  118. + 'orpc-bpa-emails-misc-queue',
  119. + 'UPM::SuspendCompaniesIfNonBillablePrimaryUPM',
  120. + [$ace_recno, 'no'],
  121. + 'Billing Agreement canceled',
  122. + $ace_recno, # unique key
  123. + 5, # priority
  124. + time + 86400, # visible in 24h,
  125. + undef, undef, # out_of_transaction, batch
  126. + 'yes', # do not raise error if same AE job is already in the queue
  127. + );
  128. + write_log("Suspend OQS job created $metadata->{job_to_suspend}");
  129. +
  130. + } elsif (defined $upms->{backup} && $upms->{backup} == $upm_recno) {
  131. + # BillingAgreement is canceled on BackupUPM (should)
  132. + $history_comment = "Set cloned UPM $new_recno as new BackupUPM";
  133. + try {
  134. + SetBackup($new_recno);
  135. + $metadata->{new_backup_upm} = $new_recno;
  136. + } catch Error with {
  137. + my $err = shift;
  138. + $history_comment .= ": $err->{-text}";
  139. + };
  140. + write_log($history_comment);
  141. +
  142. + } else {
  143. + $history_comment = "Billing agreement is canceled on non-primary UPM $upm_recno, ace=$ace_recno, cloned_upm=$new_recno";
  144. + write_log($history_comment);
  145. + }
  146. +
  147. + # mark original UPM as deleted
  148. + rpc('QB::UpdateUserPaymentMethod', $upm_recno, {'ValidUntil' => any_to_msec(my_time())});
  149. + };
  150. + } "lock_ace_$ace_recno"; # execute_critical_section
  151. + write_log("released lock lock_ace_$ace_recno");
  152. +
  153. + } catch Error with {
  154. + my $e = shift;
  155. + $history_comment = bpa_history_error_comment($e);
  156. + $e->throw();
  157. +
  158. + } finally {
  159. + insert_to_bpa_history({
  160. + 'Start Time' => $t_begin,
  161. + 'Related AccountingEntity' => $ace_recno,
  162. + 'Related UserPaymentMethod' => $upm_recno,
  163. + 'Comment' => $history_comment,
  164. + 'Metadata' => $metadata,
  165. + });
  166. + };
  167. +
  168. + return undef;
  169. ++=======
  170. + =item TmpB39561FindInvalidCcUpms()
  171. +
  172. + Find non-deleted CC UPMs that cannot pass current
  173. + validation rules.
  174. +
  175. + B<Parameters>
  176. +
  177. + None.
  178. +
  179. + B<Return value>
  180. +
  181. + CSV:
  182. +
  183. + "upm", "field", "value", "error"
  184. + ...
  185. +
  186. + =cut
  187. +
  188. + sub TmpB39561FindInvalidCcUpms {
  189. + my ($csv, $cnt);
  190. +
  191. + # use slave
  192. + local $CTX{RPC_ODB_SLAVE} = 1;
  193. +
  194. + # fetch non-deleted CC UPMs
  195. + my @upms = osql_select(q|
  196. + SELECT "Record ID#", "CC First Name", "CC Last Name", "CC Address 1",
  197. + "CC Address 2", "CC City", "CC Country", "CC State", "CC Postal Code",
  198. + "CC Phone", "CC Email", "CC FirstSixDigits", "CC LastFourDigits",
  199. + "CC Expiration Date"
  200. + FROM "UserPaymentMethods"
  201. + WHERE "ValidUntil" IS NULL
  202. + AND "Related PaymentMethod" = 'CC'
  203. + ORDER BY "Record ID#"
  204. + |);
  205. +
  206. + write_log(scalar(@upms).' to go');
  207. +
  208. + # header
  209. + $csv = qq|"upm","field","value","error"\n|;
  210. +
  211. + # check all UPMS
  212. + foreach my $upm (@upms) {
  213. +
  214. + # timestamp to MDY
  215. + $upm->{'CC Expiration Date'} = any_to_mdy($upm->{'CC Expiration Date'});
  216. +
  217. + # check fields
  218. + my $err = _check_cc_fields($upm, 0);
  219. +
  220. + # add errors to output
  221. + foreach my $fld (keys %$err) {
  222. + $csv .= join(',', map {qq|"$_"|} ($upm->{'Record ID#'}, $fld, $upm->{$fld}, $err->{$fld}))."\n";
  223. + }
  224. +
  225. + write_log("$cnt UPMs have been processed") unless ++$cnt % 1000;
  226. + }
  227. +
  228. + return $csv;
  229. ++>>>>>>> origin/master
  230. }
  231.  
  232. sub _name_breakdown {
Add Comment
Please, Sign In to add comment