save() public method

public save ( $data, $client = null )
 /**
  * @param $data
  * @param null $client
  * @return mixed|null
  */
 public function save($data, $client = null)
 {
     if (Auth::user()->account->isNinjaAccount() && isset($data['plan'])) {
         $this->ninjaRepo->updatePlanDetails($data['public_id'], $data);
     }
     return $this->clientRepo->save($data, $client);
 }
 /**
  * @param array $data
  * @param Invoice|null $invoice
  * @return \App\Models\Invoice|Invoice|mixed
  */
 public function save(array $data, Invoice $invoice = null)
 {
     if (isset($data['client'])) {
         $canSaveClient = false;
         $canViewClient = false;
         $clientPublicId = array_get($data, 'client.public_id') ?: array_get($data, 'client.id');
         if (empty($clientPublicId) || $clientPublicId == '-1') {
             $canSaveClient = Auth::user()->can('create', ENTITY_CLIENT);
         } else {
             $client = Client::scope($clientPublicId)->first();
             $canSaveClient = Auth::user()->can('edit', $client);
             $canViewClient = Auth::user()->can('view', $client);
         }
         if ($canSaveClient) {
             $client = $this->clientRepo->save($data['client']);
         }
         if ($canSaveClient || $canViewClient) {
             $data['client_id'] = $client->id;
         }
     }
     $invoice = $this->invoiceRepo->save($data, $invoice);
     $client = $invoice->client;
     $client->load('contacts');
     $sendInvoiceIds = [];
     foreach ($client->contacts as $contact) {
         if ($contact->send_invoice) {
             $sendInvoiceIds[] = $contact->id;
         }
     }
     // if no contacts are selected auto-select the first to enusre there's an invitation
     if (!count($sendInvoiceIds)) {
         $sendInvoiceIds[] = $client->contacts[0]->id;
     }
     foreach ($client->contacts as $contact) {
         $invitation = Invitation::scope()->whereContactId($contact->id)->whereInvoiceId($invoice->id)->first();
         if (in_array($contact->id, $sendInvoiceIds) && !$invitation) {
             $invitation = Invitation::createNew();
             $invitation->invoice_id = $invoice->id;
             $invitation->contact_id = $contact->id;
             $invitation->invitation_key = str_random(RANDOM_KEY_LENGTH);
             $invitation->save();
         } elseif (!in_array($contact->id, $sendInvoiceIds) && $invitation) {
             $invitation->delete();
         }
     }
     return $invoice;
 }
 /**
  * @param $file
  * @return array
  * @throws Exception
  */
 public function importJSON($file)
 {
     $this->init();
     $file = file_get_contents($file);
     $json = json_decode($file, true);
     $json = $this->removeIdFields($json);
     $this->checkClientCount(count($json['clients']));
     foreach ($json['clients'] as $jsonClient) {
         if (EntityModel::validate($jsonClient, ENTITY_CLIENT) === true) {
             $client = $this->clientRepo->save($jsonClient);
             $this->addSuccess($client);
         } else {
             $this->addFailure(ENTITY_CLIENT, $jsonClient);
             continue;
         }
         foreach ($jsonClient['invoices'] as $jsonInvoice) {
             $jsonInvoice['client_id'] = $client->id;
             if (EntityModel::validate($jsonInvoice, ENTITY_INVOICE) === true) {
                 $invoice = $this->invoiceRepo->save($jsonInvoice);
                 $this->addSuccess($invoice);
             } else {
                 $this->addFailure(ENTITY_INVOICE, $jsonInvoice);
                 continue;
             }
             foreach ($jsonInvoice['payments'] as $jsonPayment) {
                 $jsonPayment['client_id'] = $jsonPayment['client'] = $client->id;
                 // TODO: change to client_id once views are updated
                 $jsonPayment['invoice_id'] = $jsonPayment['invoice'] = $invoice->id;
                 // TODO: change to invoice_id once views are updated
                 if (EntityModel::validate($jsonPayment, ENTITY_PAYMENT) === true) {
                     $payment = $this->paymentRepo->save($jsonPayment);
                     $this->addSuccess($payment);
                 } else {
                     $this->addFailure(ENTITY_PAYMENT, $jsonPayment);
                     continue;
                 }
             }
         }
     }
     return $this->results;
 }
 public function handleBuyNow(ClientRepository $clientRepo, InvoiceService $invoiceService, $gatewayTypeAlias = false)
 {
     if (Crawler::isCrawler()) {
         return redirect()->to(NINJA_WEB_URL, 301);
     }
     $account = Account::whereAccountKey(Input::get('account_key'))->first();
     $redirectUrl = Input::get('redirect_url', URL::previous());
     if (!$account || !$account->enable_buy_now_buttons || !$account->hasFeature(FEATURE_BUY_NOW_BUTTONS)) {
         return redirect()->to("{$redirectUrl}/?error=invalid account");
     }
     Auth::onceUsingId($account->users[0]->id);
     $product = Product::scope(Input::get('product_id'))->first();
     if (!$product) {
         return redirect()->to("{$redirectUrl}/?error=invalid product");
     }
     $rules = ['first_name' => 'string|max:100', 'last_name' => 'string|max:100', 'email' => 'email|string|max:100'];
     $validator = Validator::make(Input::all(), $rules);
     if ($validator->fails()) {
         return redirect()->to("{$redirectUrl}/?error=" . $validator->errors()->first());
     }
     $data = ['currency_id' => $account->currency_id, 'contact' => Input::all()];
     $client = $clientRepo->save($data);
     $data = ['client_id' => $client->id, 'tax_rate1' => $account->default_tax_rate ? $account->default_tax_rate->rate : 0, 'tax_name1' => $account->default_tax_rate ? $account->default_tax_rate->name : '', 'invoice_items' => [['product_key' => $product->product_key, 'notes' => $product->notes, 'cost' => $product->cost, 'qty' => 1, 'tax_rate1' => $product->default_tax_rate ? $product->default_tax_rate->rate : 0, 'tax_name1' => $product->default_tax_rate ? $product->default_tax_rate->name : '']]];
     $invoice = $invoiceService->save($data);
     $invitation = $invoice->invitations[0];
     $link = $invitation->getLink();
     if ($gatewayTypeAlias) {
         return redirect()->to($invitation->getLink('payment') . "/{$gatewayTypeAlias}");
     } else {
         return redirect()->to($invitation->getLink());
     }
 }
 private function importFile()
 {
     $data = Session::get('data');
     Session::forget('data');
     $map = Input::get('map');
     $count = 0;
     $hasHeaders = Input::get('header_checkbox');
     $countries = Cache::get('countries');
     $countryMap = [];
     foreach ($countries as $country) {
         $countryMap[strtolower($country->name)] = $country->id;
     }
     foreach ($data as $row) {
         if ($hasHeaders) {
             $hasHeaders = false;
             continue;
         }
         $data = ['contacts' => [[]]];
         foreach ($row as $index => $value) {
             $field = $map[$index];
             if (!($value = trim($value))) {
                 continue;
             }
             if ($field == Client::$fieldName) {
                 $data['name'] = $value;
             } elseif ($field == Client::$fieldPhone) {
                 $data['work_phone'] = $value;
             } elseif ($field == Client::$fieldAddress1) {
                 $data['address1'] = $value;
             } elseif ($field == Client::$fieldAddress2) {
                 $data['address2'] = $value;
             } elseif ($field == Client::$fieldCity) {
                 $data['city'] = $value;
             } elseif ($field == Client::$fieldState) {
                 $data['state'] = $value;
             } elseif ($field == Client::$fieldPostalCode) {
                 $data['postal_code'] = $value;
             } elseif ($field == Client::$fieldCountry) {
                 $value = strtolower($value);
                 $data['country_id'] = isset($countryMap[$value]) ? $countryMap[$value] : null;
             } elseif ($field == Client::$fieldNotes) {
                 $data['private_notes'] = $value;
             } elseif ($field == Contact::$fieldFirstName) {
                 $data['contacts'][0]['first_name'] = $value;
             } elseif ($field == Contact::$fieldLastName) {
                 $data['contacts'][0]['last_name'] = $value;
             } elseif ($field == Contact::$fieldPhone) {
                 $data['contacts'][0]['phone'] = $value;
             } elseif ($field == Contact::$fieldEmail) {
                 $data['contacts'][0]['email'] = strtolower($value);
             }
         }
         $rules = ['contacts' => 'valid_contacts'];
         $validator = Validator::make($data, $rules);
         if ($validator->fails()) {
             continue;
         }
         $clientRepository = new ClientRepository();
         $clientRepository->save($data);
         $count++;
     }
     $message = Utils::pluralize('created_client', $count);
     Session::flash('message', $message);
     return Redirect::to('clients');
 }