I'm trying to add a new field (programmatically) in the (admin->customer->tab general) customer section of customers called customer number. Everything works as expected but I don't want to get double numbers in the database since the user can enter his own number. Every time a new user gets created from the admin the code looks for the last (highest) customer number and adds +1 to it.
I cannot find a solution for this case:
If the customer already exists (edit mode) it should show a not editable value (readonly)
if registering a new customer the last (highest) customer number should be shown (editable). If the user enters a number that is lower than the highest customer number an error should show up and the highest customer number + 1 should show.
This is the code I have so far In the controller (getForm):
if (isset($this->error['customer_number'])) {
$data['error_customer_number'] = $this->error['customer_number'];
} else {
$data['error_customer_number'] = '';
}
$data['latest_customer'] = $this->model_sale_customer->getLatestCustomerNumber();
if (isset($this->request->post['customer_number'])) {
$data['customer_number'] = $this->request->post['customer_number'];
} elseif (!empty($customer_info)) {
$data['customer_number'] = $customer_info['customer_number'];
} else {
$data['customer_number'] = '0';
}
In the validation (validateForm):
$data['latest_customer'] = $this->model_sale_customer->getLatestCustomerNumber();
if (isset($this->request->post['customer_number']) && ($this->request->post['customer_number'] <= $data['latest_customer'])) {
$this->error['customer_number'] = $this->language->get('error_customer_number');
}
In my customer_form.tpl I have:
<div class="form-group">
<label class="col-sm-2 control-label" for="input-customer-number"><span data-toggle="tooltip" title="<?php echo $help_customer_number; ?>"> <?php echo $entry_customer_number; ?></span></label>
<div class="col-sm-10">
<?php if (!empty($customer_number) || $customer_number != 0){ ?>
<input type="text" name="customer_number" value="<?php echo $customer_number; ?>" placeholder="<?php echo $entry_customer_number; ?>" id="input-customer-number" class="form-control" />
<?php }else{ ?>
<input type="text" name="customer_number" value="<?php echo $latest_customer + 1; ?>" placeholder="<?php echo $entry_customer_number; ?>" id="input-customer-number" class="form-control" />
<?php if ($error_customer_number) { ?>
<div class="text-danger"><?php echo $error_customer_number; ?></div>
<?php } ?>
<?php } ?>
</div>
</div>
This obviously doesn't work because when editing an existing customer the customer number is always lower than the highest customer number in the system (and thus gives an error). Hope some one can help me out with this! Thanks in advance!