Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Enable\Hooks;
- use Enable\Utils\AutoNumberWithPrefixAndSuffix;
- use Enable\Utils\FieldHelper;
- /**
- * Account Hook class
- *
- * @author Daniel Cherrington <dcherrington@enable.services>
- */
- class AccountHooks
- {
- /**
- * Generate agent number
- *
- * @param Account $account Account bean
- *
- * @return void
- */
- public function setAutoNumber(\Account $account)
- {
- if ($account->account_type == 'Partner') {
- //only run if the number is empty or the type, agent type or country has just changed
- if (empty($account->agent_number_c)
- || FieldHelper::hasChangedTo($account, "account_type", "Partner")
- || FieldHelper::hasChanged($account, "agent_type_c")
- || FieldHelper::hasChanged($account, "account_country_c")
- ) {
- if ($account->account_country_c == 'DK') {
- //use the full 3 char doppdown key i.e 800 for prefix
- //autonumber length = 4
- //example: 800****
- $autoNumberer = new \Enable\Utils\AutoNumberWithPrefix(false, 7, 'Accounts', 'agent');
- $prefix = $account->agent_type_c;
- $account->agent_number_c = $autoNumberer->generate($prefix);
- }
- if ($account->account_country_c == 'SE') {
- //use the first number of the key i.e 8 for prefix
- //autonumber length = 7
- //example: 8*******
- $autoNumberer = new \Enable\Utils\AutoNumberWithPrefix(false, 8, 'Accounts', 'agent');
- $prefix = substr($account->agent_type_c, 0, 1);
- $account->agent_number_c = $autoNumberer->generate($prefix);
- }
- }
- }
- if ($account->account_type == 'Customer') {
- if (empty($account->customer_number_c)
- || FieldHelper::hasChangedTo($account, "account_type", "Customer")
- ) {
- //no prefix required
- //autonumber length = 7
- //example: *******000; first record = 0000001000
- //this field is a varchar because integers cant start with a 0
- $autoNumberer = new \Enable\Utils\AutoNumberWithSuffix(true, 10, 'Accounts', 'customer');
- $suffix = '000';
- $account->customer_number_c = $autoNumberer->generate($suffix);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment