public static function getProgressiveCode($group_id) { $max = Customers::maximum(array('column' => 'codice_fatturatario', 'conditions' => "customers_groups_id = {$group_id}")); $customer_code = str_pad((int) substr($max, -4) + 1, 4, 0, STR_PAD_LEFT); $group_code = CustomersGroups::findFirstById($group_id)->codice_gruppo; return $group_code . $customer_code; }
public function updateCustomerAction() { if ($this->request->isPost()) { if ($this->request->isAjax()) { $data = $this->request->getPost(); $id = $data['data']['id']; $newValue = $data['value']; $field = $data['columnName']; $customer = Customers::findFirstById($id); $response = array(); if ($customer) { if ($field == 'gruppo') { $field = 'customers_groups_id'; $group = CustomersGroups::findFirstById($newValue); if ($group) { $customer->codice_fatturatario = $group->codice_gruppo . substr($customer->codice_fatturatario, -4); } } if ($field == 'stato') { $field = 'customers_state_id'; } if ($field == 'contabile') { $field = 'bookkeeper_id'; } $customer->{$field} = $newValue; } try { if ($customer->save() !== false) { $response['error'] = 0; $response['message'] = _('The customer has been updated'); } else { $response['error'] = 1; foreach ($customer->getMessages() as $message) { $response['message'] .= $message . '<br>'; } } } catch (\Exception $e) { $response['error'] = 2; $response['message'] = $e->getMessage(); } if ($field == 'customers_groups_id') { $response['newValue'] = $customer->gruppo->nome; } elseif ($field == 'customers_state_id') { $response['newValue'] = $customer->stato->stato; } elseif ($field == 'bookkeeper_id') { $response['newValue'] = $customer->contabile->details->name . ' ' . $customer->contabile->details->surname; } else { $response['newValue'] = $customer->{$field}; } return $this->sendAjax($response); } } }