0

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!

Ali Zia
  • 3,825
  • 5
  • 29
  • 77
Danny
  • 83
  • 1
  • 10
  • You will have to use multiple conditions in order to achieve this. I can help you with this if you want. – Ali Zia Jan 07 '16 at 04:31
  • Hi Ali thanks for helping me out. Can you show me some sample conditions I need to use? – Danny Jan 07 '16 at 11:08
  • You can declare a variable and get the `id` through `request`. If there is some `id` in `url`, you can disable the `checks` in `validation`. Something like that. For further details you can add me on skype. My id is `syed_ali_zia` – Ali Zia Jan 07 '16 at 11:26

1 Answers1

0

For other who are looking into this I solved my problem myself. Ali's comment made me think. I added a hidden input field so I could check against that. Is it an existing customer or not (has number or not). I only have to check for new customers cause that is an inputfield (existing is readonly and thus not editable). The check is to see if this is a new customer (value 0) - is the posted value < The highest customer number. That's it!

This is the code I have so In the controller (getForm):

if (isset($this->error['customer_number'])) {
$data['error_customer_number'] = $this->error['customer_number'];
} else {
$data['error_customer_number'] = '';
}

if (isset($this->request->post['customer_number']) && (($this->request->post['customer_number'] < $data['latest_customer']) && $this->request->post['existing_customer'] == '0')) {
        $this->request->post['customer_number'] = '0';
        $data['customer_number'] = '0';
    } elseif (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';
    }

    if (isset($this->request->post['existing_customer'])) {
        $data['existing_customer'] = $this->request->post['existing_customer'];
    } else {
        $data['existing_customer'] = '0';
    }

In the validation (validateForm):

$data['latest_customer'] = $this->model_customer_customer->getLatestCustomerNumber();

    if (isset($this->request->post['customer_number']) && (($this->request->post['customer_number'] < $data['latest_customer']) && ($this->request->post['existing_customer'] == '0' || $data['existing_customer'] = '0'))) {
        $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" readonly />
                  <input type="hidden" name="existing_customer" value="1" id="input-existing-customer" 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" />
                    <input type="hidden" name="existing_customer" value="0" id="input-existing-customer" class="form-control" />
                    <?php } ?>
                    <?php if ($error_customer_number) { ?>
                  <div class="text-danger"><?php echo $error_customer_number; ?></div>
                  <?php  } ?>
                </div>
              </div>
Danny
  • 83
  • 1
  • 10