Inheritance: extends BaseRepository
Ejemplo n.º 1
0
 private function initMaps()
 {
     $this->init();
     $this->maps = ['client' => [], 'invoice' => [], 'invoice_client' => [], 'product' => [], 'countries' => [], 'countries2' => [], 'currencies' => [], 'client_ids' => [], 'invoice_ids' => [], 'vendors' => [], 'expense_categories' => []];
     $clients = $this->clientRepo->all();
     foreach ($clients as $client) {
         $this->addClientToMaps($client);
     }
     $invoices = $this->invoiceRepo->all();
     foreach ($invoices as $invoice) {
         $this->addInvoiceToMaps($invoice);
     }
     $products = $this->productRepo->all();
     foreach ($products as $product) {
         $this->addProductToMaps($product);
     }
     $countries = Cache::get('countries');
     foreach ($countries as $country) {
         $this->maps['countries'][strtolower($country->name)] = $country->id;
         $this->maps['countries2'][strtolower($country->iso_3166_2)] = $country->id;
     }
     $currencies = Cache::get('currencies');
     foreach ($currencies as $currency) {
         $this->maps['currencies'][strtolower($currency->code)] = $currency->id;
     }
     $vendors = $this->vendorRepo->all();
     foreach ($vendors as $vendor) {
         $this->addVendorToMaps($vendor);
     }
     $expenseCaegories = $this->expenseCategoryRepo->all();
     foreach ($expenseCaegories as $category) {
         $this->addExpenseCategoryToMaps($category);
     }
 }
Ejemplo n.º 2
0
 /**
  * @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;
 }
 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());
     }
 }
Ejemplo n.º 4
0
 /**
  * @param $search
  * @param $userId
  * @return \Illuminate\Http\JsonResponse
  */
 public function getDatatable($search, $userId)
 {
     $datatable = new ClientDatatable();
     $query = $this->clientRepo->find($search, $userId);
     return $this->datatableService->createDatatable($datatable, $query);
 }
Ejemplo n.º 5
0
 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');
 }