public function save($publicId, $data, $notify = true) { if (!$publicId || $publicId == "-1") { $client = Client::createNew(); $client->currency_id = 1; $contact = Contact::createNew(); $contact->is_primary = true; $contact->send_invoice = true; } else { $client = Client::scope($publicId)->with('contacts')->firstOrFail(); $contact = $client->contacts()->where('is_primary', '=', true)->firstOrFail(); } if (isset($data['name'])) { $client->name = trim($data['name']); } if (isset($data['work_phone'])) { $client->work_phone = trim($data['work_phone']); } if (isset($data['custom_value1'])) { $client->custom_value1 = trim($data['custom_value1']); } if (isset($data['custom_value2'])) { $client->custom_value2 = trim($data['custom_value2']); } if (isset($data['address1'])) { $client->address1 = trim($data['address1']); } if (isset($data['address2'])) { $client->address2 = trim($data['address2']); } if (isset($data['city'])) { $client->city = trim($data['city']); } if (isset($data['state'])) { $client->state = trim($data['state']); } if (isset($data['postal_code'])) { $client->postal_code = trim($data['postal_code']); } if (isset($data['country_id'])) { $client->country_id = $data['country_id'] ? $data['country_id'] : null; } if (isset($data['private_notes'])) { $client->private_notes = trim($data['private_notes']); } if (isset($data['size_id'])) { $client->size_id = $data['size_id'] ? $data['size_id'] : null; } if (isset($data['industry_id'])) { $client->industry_id = $data['industry_id'] ? $data['industry_id'] : null; } if (isset($data['currency_id'])) { $client->currency_id = $data['currency_id'] ? $data['currency_id'] : 1; } if (isset($data['payment_terms'])) { $client->payment_terms = $data['payment_terms']; } if (isset($data['website'])) { $client->website = trim($data['website']); } $client->save(); $isPrimary = true; $contactIds = []; if (isset($data['contact'])) { $info = $data['contact']; if (isset($info['email'])) { $contact->email = trim(strtolower($info['email'])); } if (isset($info['first_name'])) { $contact->first_name = trim($info['first_name']); } if (isset($info['last_name'])) { $contact->last_name = trim($info['last_name']); } if (isset($info['phone'])) { $contact->phone = trim($info['phone']); } $contact->is_primary = true; $contact->send_invoice = true; $client->contacts()->save($contact); } else { foreach ($data['contacts'] as $record) { $record = (array) $record; if ($publicId != "-1" && isset($record['public_id']) && $record['public_id']) { $contact = Contact::scope($record['public_id'])->firstOrFail(); } else { $contact = Contact::createNew(); } if (isset($record['email'])) { $contact->email = trim(strtolower($record['email'])); } if (isset($record['first_name'])) { $contact->first_name = trim($record['first_name']); } if (isset($record['last_name'])) { $contact->last_name = trim($record['last_name']); } if (isset($record['phone'])) { $contact->phone = trim($record['phone']); } $contact->is_primary = $isPrimary; $contact->send_invoice = isset($record['send_invoice']) ? $record['send_invoice'] : true; $isPrimary = false; $client->contacts()->save($contact); $contactIds[] = $contact->public_id; } foreach ($client->contacts as $contact) { if (!in_array($contact->public_id, $contactIds)) { $contact->delete(); } } } $client->save(); if (!$publicId || $publicId == "-1") { \Activity::createClient($client, $notify); } return $client; }
private function importFile() { $data = Session::get('data'); Session::forget('data'); $map = Input::get('map'); $count = 0; $hasHeaders = Input::get('header_checkbox'); $countries = Country::remember(DEFAULT_QUERY_CACHE)->get(); $countryMap = []; foreach ($countries as $country) { $countryMap[strtolower($country->name)] = $country->id; } foreach ($data as $row) { if ($hasHeaders) { $hasHeaders = false; continue; } $client = Client::createNew(); $contact = Contact::createNew(); $contact->is_primary = true; $contact->send_invoice = true; $count++; foreach ($row as $index => $value) { $field = $map[$index]; $value = trim($value); if ($field == Client::$fieldVat_number && !$client->vat_number) { $client->vat_number = $value; } else { if ($field == Client::$fieldName && !$client->name) { $client->name = $value; } else { if ($field == Client::$fieldNit && !$client->nit) { $client->nit = $value; } else { if ($field == Client::$fieldPhone && !$client->work_phone) { $client->work_phone = $value; } else { if ($field == Client::$fieldAddress1 && !$client->address1) { $client->address1 = $value; } else { if ($field == Client::$fieldAddress2 && !$client->address2) { $client->address2 = $value; } else { if ($field == Client::$fieldNotes && !$client->private_notes) { $client->private_notes = $value; } else { if ($field == Contact::$fieldFirstName && !$contact->first_name) { $contact->first_name = $value; } else { if ($field == Contact::$fieldLastName && !$contact->last_name) { $contact->last_name = $value; } else { if ($field == Contact::$fieldPhone && !$contact->phone) { $contact->phone = $value; } else { if ($field == Contact::$fieldEmail && !$contact->email) { $contact->email = strtolower($value); } } } } } } } } } } } } $client->save(); $client->contacts()->save($contact); $client->save(); Activity::createClient($client, false); } $message = Utils::pluralize('created_client', $count); Session::flash('message', $message); return Redirect::to('clients'); }
private function save($publicId = null, $isPos = null) { $rules = array('nit' => 'required'); // $clientId = $publicId ? Client::getPrivateId($publicId) : null; // $rules = ['nit' => 'unique:clients,nit,' . $clientId . ',id,account_id,' . Auth::user()->account_id]; $validator = Validator::make(Input::all(), $rules); if ($validator->fails()) { if ($isPos) { $datos = array('resultado' => 1, 'mensaje' => 'Nit ya ha sido registrado.'); return Response::json($datos); } else { $url = $publicId ? 'clients/' . $publicId . '/edit' : 'clients/create'; return Redirect::to($url)->withErrors($validator)->withInput(Input::except('password')); } } else { if ($publicId) { $client = Client::scope($publicId)->firstOrFail(); } else { $client = Client::createNew(); } $client->nit = trim(Input::get('nit')); $client->name = trim(Input::get('name')); $client->business_name = trim(Input::get('business_name')); $client->work_phone = trim(Input::get('work_phone')); if ($isPos) { $client->business_name = trim(Input::get('name')); } $client->custom_value1 = trim(Input::get('custom_value1')); $client->custom_value2 = trim(Input::get('custom_value2')); $client->custom_value3 = trim(Input::get('custom_value3')); $client->custom_value4 = trim(Input::get('custom_value4')); $client->custom_value5 = trim(Input::get('custom_value5')); $client->custom_value6 = trim(Input::get('custom_value6')); $client->custom_value7 = trim(Input::get('custom_value7')); $client->custom_value8 = trim(Input::get('custom_value8')); $client->custom_value9 = trim(Input::get('custom_value9')); $client->custom_value10 = trim(Input::get('custom_value10')); $client->custom_value11 = trim(Input::get('custom_value11')); $client->custom_value12 = trim(Input::get('custom_value12')); $client->address1 = trim(Input::get('address1')); $client->address2 = trim(Input::get('address2')); $client->city = trim(Input::get('city')); $client->state = trim(Input::get('state')); $client->private_notes = trim(Input::get('private_notes')); $client->save(); if (!$isPos) { $data = json_decode(Input::get('data')); $contactIds = []; $isPrimary = true; foreach ($data->contacts as $contact) { if (isset($contact->public_id) && $contact->public_id) { $record = Contact::scope($contact->public_id)->firstOrFail(); } else { $record = Contact::createNew(); } $record->email = trim(strtolower($contact->email)); $record->first_name = trim($contact->first_name); $record->last_name = trim($contact->last_name); $record->phone = trim($contact->phone); $record->is_primary = $isPrimary; $isPrimary = false; $client->contacts()->save($record); $contactIds[] = $record->public_id; } foreach ($client->contacts as $contact) { if (!in_array($contact->public_id, $contactIds)) { $contact->delete(); } } if ($publicId) { Activity::editClient($client); Session::flash('message', trans('texts.updated_client')); } else { Activity::createClient($client); Session::flash('message', trans('texts.created_client')); } return Redirect::to('clients/' . $client->public_id); } else { $record = Contact::createNew(); $record->is_primary = true; $client->contacts()->save($record); $clientPOS = array('id' => $client->id, 'public_id' => $client->public_id, 'name' => $client->name, 'nit' => $client->nit, 'business_name' => $client->business_name); $datos = array('resultado' => 0, 'cliente' => $clientPOS); return Response::json($datos); } } }
private function save($publicId = null) { $rules = array('email' => 'required'); $validator = Validator::make(Input::all(), $rules); if ($validator->fails()) { $url = $publicId ? 'clients/' . $publicId . '/edit' : 'clients/create'; return Redirect::to($url)->withErrors($validator)->withInput(Input::except('password')); } else { if ($publicId) { $client = Client::scope($publicId)->firstOrFail(); } else { $client = Client::createNew(); } $client->name = trim(Input::get('name')); $client->id_number = trim(Input::get('id_number')); $client->vat_number = trim(Input::get('vat_number')); $client->work_phone = trim(Input::get('work_phone')); $client->custom_value1 = trim(Input::get('custom_value1')); $client->custom_value2 = trim(Input::get('custom_value2')); $client->address1 = trim(Input::get('address1')); $client->address2 = trim(Input::get('address2')); $client->city = trim(Input::get('city')); $client->state = trim(Input::get('state')); $client->postal_code = trim(Input::get('postal_code')); $client->country_id = Input::get('country_id') ?: null; $client->private_notes = trim(Input::get('private_notes')); $client->size_id = Input::get('size_id') ?: null; $client->industry_id = Input::get('industry_id') ?: null; $client->currency_id = Input::get('currency_id') ?: null; $client->payment_terms = Input::get('payment_terms') ?: 0; $client->website = trim(Input::get('website')); $client->save(); $data = json_decode(Input::get('data')); $contactIds = []; $isPrimary = true; foreach ($data->contacts as $contact) { if (isset($contact->public_id) && $contact->public_id) { $record = Contact::scope($contact->public_id)->firstOrFail(); } else { $record = Contact::createNew(); } $record->email = trim(strtolower($contact->email)); $record->first_name = trim($contact->first_name); $record->last_name = trim($contact->last_name); $record->phone = trim($contact->phone); $record->is_primary = $isPrimary; $isPrimary = false; $client->contacts()->save($record); $contactIds[] = $record->public_id; } foreach ($client->contacts as $contact) { if (!in_array($contact->public_id, $contactIds)) { $contact->delete(); } } if ($publicId) { Session::flash('message', trans('texts.updated_client')); } else { Activity::createClient($client); Session::flash('message', trans('texts.created_client')); } return Redirect::to('clients/' . $client->public_id); } }
private function save($publicId = null) { $rules['nit'] = 'required'; $validator = Validator::make(Input::all(), $rules); if ($publicId) { $client = Client::scope($publicId)->firstOrFail(); } else { $client = Client::createNew(); if ($validator->fails()) { $url = $publicId ? 'clients/' . $publicId . '/edit' : 'clients/create'; return Redirect::to($url)->withErrors($validator)->withInput(Input::except('password')); } } $client->nit = trim(Input::get('nit')); $client->name = trim(Input::get('name')); $client->work_phone = trim(Input::get('work_phone')); $frecuency = ""; if (Input::get('custom_value1_0')) { $frecuency .= "1,"; } if (Input::get('custom_value1_1')) { $frecuency .= "2,"; } if (Input::get('custom_value1_2')) { $frecuency .= "3,"; } if (Input::get('custom_value1_3')) { $frecuency .= "4,"; } if (Input::get('custom_value1_4')) { $frecuency .= "5,"; } if (Input::get('custom_value1_5')) { $frecuency .= "6,"; } if (Input::get('custom_value1_6')) { $frecuency .= "7,"; } $client->custom_value1 = $frecuency; $client->address1 = trim(Input::get('address1')); $client->address2 = trim(Input::get('address2')); $client->city = trim(Input::get('city')); $client->state = trim(Input::get('state')); $client->postal_code = trim(Input::get('postal_code')); $client->country_id = Input::get('country_id') ? Input::get('country_id') : null; $client->private_notes = trim(Input::get('private_notes')); $client->business_type_id = Input::get('business_type_id') ? Input::get('business_type_id') : null; $client->group_id = Input::get('group_id') ? Input::get('group_id') : null; $client->zone_id = Input::get('zone_id') ? Input::get('zone_id') : null; $client->currency_id = Input::get('currency_id') ? Input::get('currency_id') : 1; $client->website = trim(Input::get('website')); $client->save(); $data = json_decode(Input::get('data')); $contactIds = []; $isPrimary = true; foreach ($data->contacts as $contact) { if (isset($contact->public_id) && $contact->public_id) { $record = Contact::scope($contact->public_id)->firstOrFail(); } else { $record = Contact::createNew(); } $record->email = trim(strtolower($contact->email)); $record->first_name = trim($contact->first_name); $record->last_name = trim($contact->last_name); $record->phone = trim($contact->phone); $record->is_primary = $isPrimary; $isPrimary = false; $client->contacts()->save($record); $contactIds[] = $record->public_id; } foreach ($client->contacts as $contact) { if (!in_array($contact->public_id, $contactIds)) { $contact->delete(); } } if ($publicId) { Session::flash('message', trans('texts.updated_client')); } else { Activity::createClient($client); Session::flash('message', trans('texts.created_client')); } return Redirect::to('clients/' . $client->public_id); }