Guest User

Untitled

a guest
Jan 22nd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Enable\Hooks;
  4.  
  5. use Enable\Utils\AutoNumberWithPrefixAndSuffix;
  6. use Enable\Utils\FieldHelper;
  7.  
  8. /**
  9. * Account Hook class
  10. *
  11. * @author Daniel Cherrington <dcherrington@enable.services>
  12. */
  13. class AccountHooks
  14. {
  15. /**
  16. * Generate agent number
  17. *
  18. * @param Account $account Account bean
  19. *
  20. * @return void
  21. */
  22. public function setAutoNumber(\Account $account)
  23. {
  24. if ($account->account_type == 'Partner') {
  25.  
  26. //only run if the number is empty or the type, agent type or country has just changed
  27. if (empty($account->agent_number_c)
  28. || FieldHelper::hasChangedTo($account, "account_type", "Partner")
  29. || FieldHelper::hasChanged($account, "agent_type_c")
  30. || FieldHelper::hasChanged($account, "account_country_c")
  31. ) {
  32.  
  33. if ($account->account_country_c == 'DK') {
  34. //use the full 3 char doppdown key i.e 800 for prefix
  35. //autonumber length = 4
  36. //example: 800****
  37. $autoNumberer = new \Enable\Utils\AutoNumberWithPrefix(false, 7, 'Accounts', 'agent');
  38. $prefix = $account->agent_type_c;
  39. $account->agent_number_c = $autoNumberer->generate($prefix);
  40. }
  41.  
  42. if ($account->account_country_c == 'SE') {
  43. //use the first number of the key i.e 8 for prefix
  44. //autonumber length = 7
  45. //example: 8*******
  46. $autoNumberer = new \Enable\Utils\AutoNumberWithPrefix(false, 8, 'Accounts', 'agent');
  47. $prefix = substr($account->agent_type_c, 0, 1);
  48. $account->agent_number_c = $autoNumberer->generate($prefix);
  49. }
  50. }
  51. }
  52.  
  53. if ($account->account_type == 'Customer') {
  54.  
  55. if (empty($account->customer_number_c)
  56. || FieldHelper::hasChangedTo($account, "account_type", "Customer")
  57. ) {
  58. //no prefix required
  59. //autonumber length = 7
  60. //example: *******000; first record = 0000001000
  61. //this field is a varchar because integers cant start with a 0
  62. $autoNumberer = new \Enable\Utils\AutoNumberWithSuffix(true, 10, 'Accounts', 'customer');
  63. $suffix = '000';
  64. $account->customer_number_c = $autoNumberer->generate($suffix);
  65. }
  66. }
  67. }
  68. }
Add Comment
Please, Sign In to add comment