<?php
<?php
/**
* @author Tony Kudrys
* Customer
* This controller is setup to seupt new customer in the DB2 database "AR.MAST"/"AR.EMAIL"
*/
class Customer extends CI_Controller{
/**
* function index - This is the default funtion for this controller.
* It will load the form data and prepopulate the forms based on the provided URL variables
*
* @param string $comp - Company URL Variable
* @param string $Loc - Location URL Variable
* @param string $custNum - Customer# URL Variable
*/
public function index($comp=FALSE,$Loc=FALSE,$custNum=FALSE)
{
//load new customer model
$this->load->model('Customer/NewCustomerFields');
//get the list of fields to pass to the view
$customerfields = $this->NewCustomerFields->get_customer_fields_array();
//load locations model
$this->load->model('Locations/Locations_model');
//get the form default values based on the provided company and location info (if any is provided)
$data['LocDefaults']=$this->Locations_model->get_Locations_defaults($comp, $Loc);
//check if the location provided is valid
if(count($data['LocDefaults'])<1)
{
//*create view for error data
//location provided is not valid. echo error.
echo "no such company/location [company $comp, location $Loc]";
die();
}
//if both company and customer fields are provided pull the customer info to populate fields
if($comp != FALSE and $custNum != FALSE)
{
//load customer data model
$this->load->model('Customer/Customer_model');
//get the customer data
$customer = $this->Customer_model->get_Customer($comp, $custNum);
//if there is no customer returned echo error
if(count($customer)<1)
{
//*create view for error data
//no customer found echo error
echo "no such customer $custNum in company $comp";
die();
}
//assign the first customer found to the customer variable
$customer=$customer[0];
//cycle through customer data fields to get values
foreach ($customer as $key=>$value)
{
$customer->$key = trim($value);
}
//seprate the city and state from the cityst value
$citystarr = explode(',', $customer->CITYST);
$customer->CITY=trim($citystarr[0]);
$customer->STATE=trim($citystarr[1]);
//set the customer data to be passed the the view
$data['form_data'] = $customer;
}
//set the customer fields to be shown by the view
$data['customerfields'] = $customerfields;
//$data['CO3']=$comp;
//$data['SLMN']="5";
//$data['STATE']='OR';
//set the view to load in the main template view
$data['main_content'] = 'customer/NewCustomer';
//set the title for the main template view
$data['Page_Title'] = "New Customer Setup ".$data['LocDefaults'][0]->CompanyName." ".$data['LocDefaults'][0]->LocationName;
//load the main template view
$this->load->view('includes/template', $data);
}
/**
* function submit - controller function called when forms are submited.
*/
public function submit()
{
// get submited fields
$Form_Field_Array = array();
//validate form
$this->load->library('form_validation');
$this->form_validation->set_rules("NAME","Name","required");
$this->form_validation->set_rules("ADR1","Address","required");
$this->form_validation->set_rules("CITY","City","required");
$this->form_validation->set_rules("STATE","State","required|alpha");
$this->form_validation->set_rules("ZIPCOD","Zip Code","required|numeric");
$this->form_validation->set_rules("PRCLVL","Customer Type","required|numeric");
$this->form_validation->set_rules("SLM","Salesman#","required|numeric");
$this->form_validation->set_rules('EMAIL', 'Email', 'valid_email');
$this->form_validation->set_rules('ACVCE', 'Cell Area Code', 'numeric');
$this->form_validation->set_rules('PHVCE', 'Cell Phone#', 'numeric');
$this->form_validation->set_rules('ACOTH', 'Buisness Area Code', 'numeric');
$this->form_validation->set_rules('PHOTH', 'Buisness Phone#', 'numeric');
$this->form_validation->set_rules('ACFAX', 'Fax Area Code', 'numeric');
$this->form_validation->set_rules('PHFAX', 'Fax Other', 'numeric');
if ($this->form_validation->run() == FALSE)
{
echo "<div id=\"veh_err\">".validation_errors()."</div>";
die();
}
//assign form fields to array
$Form_Field_Array['CO'] = $this->input->post('CO3'); //company
$Form_Field_Array['CUST'] = $this->input->post('CUST'); //cust#
$Form_Field_Array['NAME'] = $this->input->post('NAME'); //cust or buisness name
$Form_Field_Array['CONTCT'] = $this->input->post('CONTCT'); //contact name
$Form_Field_Array['ALPHA'] = $this->input->post('ALPHA'); //search name
$Form_Field_Array['ADR1'] = $this->input->post('ADR1'); //address 1
$Form_Field_Array['ADR2'] = $this->input->post('ADR2'); //address 2
$Form_Field_Array['CITY'] = $this->input->post('CITY'); //city
$Form_Field_Array['STATE'] = $this->input->post('STATE'); //state
$Form_Field_Array['ZIPCOD'] = $this->input->post('ZIPCOD'); //zip code
$Form_Field_Array['EMAIL'] = $this->input->post('EMAIL'); //email address
$Form_Field_Array['ACVCE'] = $this->input->post('ACVCE'); //area code
$Form_Field_Array['PHVCE'] = $this->input->post('PHVCE'); //phone #
$Form_Field_Array['ACOTH'] = $this->input->post('ACOTH'); //alt area code
$Form_Field_Array['PHOTH'] = $this->input->post('PHOTH'); //alt phone #
$Form_Field_Array['ACFAX'] = $this->input->post('ACFAX'); //fax area code
$Form_Field_Array['PHFAX'] = $this->input->post('PHFAX'); //fax number
$Form_Field_Array['PRCLVL'] = $this->input->post('PRCLVL'); //customer price level
$Form_Field_Array['SLM'] = $this->input->post('SLM'); //salesman
$Form_Field_Array['TYPE5'] = $this->input->post('TYPE5'); //customer type
$Form_Field_Array['COMM01'] = $this->input->post('COMM01'); //comments
$Form_Field_Array = array_map('strtoupper', $Form_Field_Array); //set all data to upper case
//validate address via UPS api
$this->load->model('UPS/UPS_model');
$ADVA = $this->UPS_model->ajaxUPSAV($Form_Field_Array['CITY'], $Form_Field_Array['STATE'], $Form_Field_Array['ZIPCOD']);
//check if address is valid
if($ADVA != 1)
{
//*create view for error data
//address is not valid. echo error string.
echo "<div id=\"veh_err\">".$ADVA."</div>";
die();
}
//check if customer exists
$this->load->model('Customer/Customer_model');
if($Form_Field_Array['CUST'] != '' and $this->Customer_model->does_customer_exist($Form_Field_Array['CO'], $Form_Field_Array['CUST']))
{
//if exits update customer
$this->Customer_model->update_customer($Form_Field_Array);
echo "Customer ".$Form_Field_Array['CUST']." has been updated successfully";
}
else
{
//if does not exist insert new customer
$custNum = $this->Customer_model->insert_new_customer($Form_Field_Array);
echo "Customer $custNum has been created successfully";
echo '<script>$("#CUST").val("'.$custNum.'")</script>';
}
}
}