public function save($data, $vendor = null)
 {
     $publicId = isset($data['public_id']) ? $data['public_id'] : false;
     if ($vendor) {
         // do nothing
     } elseif (!$publicId || $publicId == '-1') {
         $vendor = Vendor::createNew();
     } else {
         $vendor = Vendor::scope($publicId)->with('vendor_contacts')->firstOrFail();
         if (Utils::isNinjaDev()) {
             \Log::warning('Entity not set in vendor repo save');
         }
     }
     if ($vendor->is_deleted) {
         return $vendor;
     }
     $vendor->fill($data);
     $vendor->save();
     $first = true;
     $vendorcontacts = isset($data['vendor_contact']) ? [$data['vendor_contact']] : $data['vendor_contacts'];
     $vendorcontactIds = [];
     foreach ($vendorcontacts as $vendorcontact) {
         $vendorcontact = $vendor->addVendorContact($vendorcontact, $first);
         $vendorcontactIds[] = $vendorcontact->public_id;
         $first = false;
     }
     if (!$vendor->wasRecentlyCreated) {
         foreach ($vendor->vendor_contacts as $contact) {
             if (!in_array($contact->public_id, $vendorcontactIds)) {
                 $contact->delete();
             }
         }
     }
     return $vendor;
 }
 /**
  * @SWG\Post(
  *   path="/vendors",
  *   tags={"vendor"},
  *   summary="Create a vendor",
  *   @SWG\Parameter(
  *     in="body",
  *     name="body",
  *     @SWG\Schema(ref="#/definitions/Vendor")
  *   ),
  *   @SWG\Response(
  *     response=200,
  *     description="New vendor",
  *      @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Vendor"))
  *   ),
  *   @SWG\Response(
  *     response="default",
  *     description="an ""unexpected"" error"
  *   )
  * )
  */
 public function store(CreateVendorRequest $request)
 {
     $vendor = $this->vendorRepo->save($request->input());
     $vendor = Vendor::scope($vendor->public_id)->with('country', 'vendorcontacts', 'industry', 'size', 'currency')->first();
     $transformer = new VendorTransformer(Auth::user()->account, Input::get('serializer'));
     $data = $this->createItem($vendor, $transformer, ENTITY_VENDOR);
     return $this->response($data);
 }
 /**
  * Show the form for creating a new resource.
  *
  * @return Response
  */
 public function create(VendorRequest $request)
 {
     if (Vendor::scope()->count() > Auth::user()->getMaxNumVendors()) {
         return View::make('error', ['hideHeader' => true, 'error' => "Sorry, you've exceeded the limit of " . Auth::user()->getMaxNumVendors() . ' vendors']);
     }
     $data = ['vendor' => null, 'method' => 'POST', 'url' => 'vendors', 'title' => trans('texts.new_vendor')];
     $data = array_merge($data, self::getViewModel());
     return View::make('vendors.edit', $data);
 }
 public function save($data)
 {
     $publicId = isset($data['public_id']) ? $data['public_id'] : false;
     if (!$publicId || $publicId == '-1') {
         $vendor = Vendor::createNew();
     } else {
         $vendor = Vendor::scope($publicId)->with('vendorcontacts')->firstOrFail();
     }
     $vendor->fill($data);
     $vendor->save();
     $first = true;
     $vendorcontacts = isset($data['vendorcontact']) ? [$data['vendorcontact']] : $data['vendorcontacts'];
     foreach ($vendorcontacts as $vendorcontact) {
         $vendorcontact = $vendor->addVendorContact($vendorcontact, $first);
         $first = false;
     }
     return $vendor;
 }
 public function edit(ExpenseRequest $request)
 {
     $expense = $request->entity();
     $expense->expense_date = Utils::fromSqlDate($expense->expense_date);
     $actions = [];
     if ($expense->invoice) {
         $actions[] = ['url' => URL::to("invoices/{$expense->invoice->public_id}/edit"), 'label' => trans("texts.view_invoice")];
     } else {
         $actions[] = ['url' => 'javascript:submitAction("invoice")', 'label' => trans("texts.invoice_expense")];
     }
     $actions[] = \DropdownButton::DIVIDER;
     if (!$expense->trashed()) {
         $actions[] = ['url' => 'javascript:submitAction("archive")', 'label' => trans('texts.archive_expense')];
         $actions[] = ['url' => 'javascript:onDeleteClick()', 'label' => trans('texts.delete_expense')];
     } else {
         $actions[] = ['url' => 'javascript:submitAction("restore")', 'label' => trans('texts.restore_expense')];
     }
     $data = array('vendor' => null, 'expense' => $expense, 'method' => 'PUT', 'url' => 'expenses/' . $expense->public_id, 'title' => 'Edit Expense', 'actions' => $actions, 'vendors' => Vendor::scope()->with('vendor_contacts')->orderBy('name')->get(), 'vendorPublicId' => $expense->vendor ? $expense->vendor->public_id : null, 'clients' => Client::scope()->with('contacts')->orderBy('name')->get(), 'clientPublicId' => $expense->client ? $expense->client->public_id : null);
     $data = array_merge($data, self::getViewModel());
     return View::make('expenses.edit', $data);
 }
 public function save($data, $vendor = null)
 {
     $publicId = isset($data['public_id']) ? $data['public_id'] : false;
     if ($vendor) {
         // do nothing
     } elseif (!$publicId || $publicId == '-1') {
         $vendor = Vendor::createNew();
     } else {
         $vendor = Vendor::scope($publicId)->with('vendor_contacts')->firstOrFail();
         \Log::warning('Entity not set in vendor repo save');
     }
     $vendor->fill($data);
     $vendor->save();
     $first = true;
     $vendorcontacts = isset($data['vendor_contact']) ? [$data['vendor_contact']] : $data['vendor_contacts'];
     foreach ($vendorcontacts as $vendorcontact) {
         $vendorcontact = $vendor->addVendorContact($vendorcontact, $first);
         $first = false;
     }
     return $vendor;
 }
 private function getData($request)
 {
     $account = Auth::user()->account;
     $data = ['account' => $account, 'title' => 'Invoice Ninja v' . NINJA_VERSION . ' - ' . $account->formatDateTime($account->getDateTime()), 'multiUser' => $account->users->count() > 1];
     if ($request->input(ENTITY_CLIENT)) {
         $data['clients'] = Client::scope()->with('user', 'contacts', 'country')->withArchived()->get();
         $data['contacts'] = Contact::scope()->with('user', 'client.contacts')->withTrashed()->get();
         $data['credits'] = Credit::scope()->with('user', 'client.contacts')->get();
     }
     if ($request->input(ENTITY_TASK)) {
         $data['tasks'] = Task::scope()->with('user', 'client.contacts')->withArchived()->get();
     }
     if ($request->input(ENTITY_INVOICE)) {
         $data['invoices'] = Invoice::scope()->with('user', 'client.contacts', 'invoice_status')->withArchived()->where('is_quote', '=', false)->where('is_recurring', '=', false)->get();
         $data['quotes'] = Invoice::scope()->with('user', 'client.contacts', 'invoice_status')->withArchived()->where('is_quote', '=', true)->where('is_recurring', '=', false)->get();
         $data['recurringInvoices'] = Invoice::scope()->with('user', 'client.contacts', 'invoice_status', 'frequency')->withArchived()->where('is_quote', '=', false)->where('is_recurring', '=', true)->get();
     }
     if ($request->input(ENTITY_PAYMENT)) {
         $data['payments'] = Payment::scope()->withArchived()->with('user', 'client.contacts', 'payment_type', 'invoice', 'account_gateway.gateway')->get();
     }
     if ($request->input(ENTITY_VENDOR)) {
         $data['clients'] = Vendor::scope()->with('user', 'vendorcontacts', 'country')->withArchived()->get();
         $data['vendor_contacts'] = VendorContact::scope()->with('user', 'vendor.contacts')->withTrashed()->get();
         /*
         $data['expenses'] = Credit::scope()
             ->with('user', 'client.contacts')
             ->get();
         */
     }
     return $data;
 }
 private function createVendorMap()
 {
     $vendorMap = [];
     $vendors = Vendor::scope()->withTrashed()->get(['id', 'name', 'transaction_name']);
     foreach ($vendors as $vendor) {
         $vendorMap[strtolower($vendor->name)] = $vendor;
         $vendorMap[strtolower($vendor->transaction_name)] = $vendor;
     }
     return $vendorMap;
 }
 public function importExpenses($bankId, $input)
 {
     $countVendors = 0;
     $countExpenses = 0;
     // create a vendor map
     $vendorMap = [];
     $vendors = Vendor::scope()->withTrashed()->get(['id', 'name', 'transaction_name']);
     foreach ($vendors as $vendor) {
         $vendorMap[strtolower($vendor->name)] = $vendor;
         $vendorMap[strtolower($vendor->transaction_name)] = $vendor;
     }
     foreach ($input as $transaction) {
         $vendorName = $transaction['vendor'];
         $key = strtolower($vendorName);
         $info = $transaction['info'];
         // find vendor otherwise create it
         if (isset($vendorMap[$key])) {
             $vendor = $vendorMap[$key];
         } else {
             $field = $this->determineInfoField($info);
             $vendor = $this->vendorRepo->save([$field => $info, 'name' => $vendorName, 'transaction_name' => $transaction['vendor_orig'], 'vendorcontact' => []]);
             $vendorMap[$key] = $vendor;
             $vendorMap[$transaction['vendor_orig']] = $vendor;
             $countVendors++;
         }
         // create the expense record
         $this->expenseRepo->save(['vendor_id' => $vendor->id, 'amount' => $transaction['amount'], 'public_notes' => $transaction['memo'], 'expense_date' => $transaction['date'], 'transaction_id' => $transaction['id'], 'bank_id' => $bankId, 'should_be_invoiced' => true]);
         $countExpenses++;
     }
     return trans('texts.imported_expenses', ['count_vendors' => $countVendors, 'count_expenses' => $countExpenses]);
 }
 public function edit($publicId)
 {
     $expense = Expense::scope($publicId)->firstOrFail();
     $expense->expense_date = Utils::fromSqlDate($expense->expense_date);
     $actions = [];
     if ($expense->invoice) {
         $actions[] = ['url' => URL::to("invoices/{$expense->invoice->public_id}/edit"), 'label' => trans("texts.view_invoice")];
     } else {
         $actions[] = ['url' => 'javascript:submitAction("invoice")', 'label' => trans("texts.invoice_expense")];
         /*
         // check for any open invoices
         $invoices = $task->client_id ? $this->invoiceRepo->findOpenInvoices($task->client_id) : [];
         
         foreach ($invoices as $invoice) {
             $actions[] = ['url' => 'javascript:submitAction("add_to_invoice", '.$invoice->public_id.')', 'label' => trans("texts.add_to_invoice", ["invoice" => $invoice->invoice_number])];
         }
         */
     }
     $actions[] = \DropdownButton::DIVIDER;
     if (!$expense->trashed()) {
         $actions[] = ['url' => 'javascript:submitAction("archive")', 'label' => trans('texts.archive_expense')];
         $actions[] = ['url' => 'javascript:onDeleteClick()', 'label' => trans('texts.delete_expense')];
     } else {
         $actions[] = ['url' => 'javascript:submitAction("restore")', 'label' => trans('texts.restore_expense')];
     }
     $data = array('vendor' => null, 'expense' => $expense, 'method' => 'PUT', 'url' => 'expenses/' . $publicId, 'title' => 'Edit Expense', 'actions' => $actions, 'vendors' => Vendor::scope()->with('vendorcontacts')->orderBy('name')->get(), 'vendorPublicId' => $expense->vendor ? $expense->vendor->public_id : null, 'clients' => Client::scope()->with('contacts')->orderBy('name')->get(), 'clientPublicId' => $expense->client ? $expense->client->public_id : null);
     $data = array_merge($data, self::getViewModel());
     if (Auth::user()->account->isNinjaAccount()) {
         if ($account = Account::whereId($client->public_id)->first()) {
             $data['proPlanPaid'] = $account['pro_plan_paid'];
         }
     }
     return View::make('expenses.edit', $data);
 }
 /**
  * @param $request
  *
  * @return array
  */
 private function getData($request)
 {
     $account = Auth::user()->account;
     $data = ['account' => $account, 'title' => 'Invoice Ninja v' . NINJA_VERSION . ' - ' . $account->formatDateTime($account->getDateTime()), 'multiUser' => $account->users->count() > 1];
     if ($request->input('include') === 'all' || $request->input('clients')) {
         $data['clients'] = Client::scope()->with('user', 'contacts', 'country')->withArchived()->get();
     }
     if ($request->input('include') === 'all' || $request->input('contacts')) {
         $data['contacts'] = Contact::scope()->with('user', 'client.contacts')->withTrashed()->get();
     }
     if ($request->input('include') === 'all' || $request->input('credits')) {
         $data['credits'] = Credit::scope()->with('user', 'client.contacts')->get();
     }
     if ($request->input('include') === 'all' || $request->input('tasks')) {
         $data['tasks'] = Task::scope()->with('user', 'client.contacts')->withArchived()->get();
     }
     if ($request->input('include') === 'all' || $request->input('invoices')) {
         $data['invoices'] = Invoice::scope()->invoiceType(INVOICE_TYPE_STANDARD)->with('user', 'client.contacts', 'invoice_status')->withArchived()->where('is_recurring', '=', false)->get();
     }
     if ($request->input('include') === 'all' || $request->input('quotes')) {
         $data['quotes'] = Invoice::scope()->invoiceType(INVOICE_TYPE_QUOTE)->with('user', 'client.contacts', 'invoice_status')->withArchived()->where('is_recurring', '=', false)->get();
     }
     if ($request->input('include') === 'all' || $request->input('recurring')) {
         $data['recurringInvoices'] = Invoice::scope()->invoiceType(INVOICE_TYPE_STANDARD)->with('user', 'client.contacts', 'invoice_status', 'frequency')->withArchived()->where('is_recurring', '=', true)->get();
     }
     if ($request->input('include') === 'all' || $request->input('payments')) {
         $data['payments'] = Payment::scope()->withArchived()->with('user', 'client.contacts', 'payment_type', 'invoice', 'account_gateway.gateway')->get();
     }
     if ($request->input('include') === 'all' || $request->input('vendors')) {
         $data['vendors'] = Vendor::scope()->with('user', 'vendor_contacts', 'country')->withArchived()->get();
     }
     if ($request->input('include') === 'all' || $request->input('vendor_contacts')) {
         $data['vendor_contacts'] = VendorContact::scope()->with('user', 'vendor.vendor_contacts')->withTrashed()->get();
     }
     return $data;
 }
 /**
  * Show the form for editing the specified resource.
  *
  * @param  int      $id
  * @return Response
  */
 public function edit($publicId)
 {
     $vendor = Vendor::scope($publicId)->with('vendorcontacts')->firstOrFail();
     if (!$this->checkEditPermission($vendor, $response)) {
         return $response;
     }
     $data = ['vendor' => $vendor, 'method' => 'PUT', 'url' => 'vendors/' . $publicId, 'title' => trans('texts.edit_vendor')];
     $data = array_merge($data, self::getViewModel());
     if (Auth::user()->account->isNinjaAccount()) {
         if ($account = Account::whereId($vendor->public_id)->first()) {
             $data['proPlanPaid'] = $account['pro_plan_paid'];
         }
     }
     return View::make('vendors.edit', $data);
 }
 /**
  * @SWG\Post(
  *   path="/vendors",
  *   tags={"vendor"},
  *   summary="Create a vendor",
  *   @SWG\Parameter(
  *     in="body",
  *     name="body",
  *     @SWG\Schema(ref="#/definitions/Vendor")
  *   ),
  *   @SWG\Response(
  *     response=200,
  *     description="New vendor",
  *      @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Vendor"))
  *   ),
  *   @SWG\Response(
  *     response="default",
  *     description="an ""unexpected"" error"
  *   )
  * )
  */
 public function store(CreateVendorRequest $request)
 {
     $vendor = $this->vendorRepo->save($request->input());
     $vendor = Vendor::scope($vendor->public_id)->with('country', 'vendor_contacts', 'industry', 'size', 'currency')->first();
     return $this->itemResponse($vendor);
 }
 public function edit(ExpenseRequest $request)
 {
     $expense = $request->entity();
     $expense->expense_date = Utils::fromSqlDate($expense->expense_date);
     $actions = [];
     if ($expense->invoice) {
         $actions[] = ['url' => URL::to("invoices/{$expense->invoice->public_id}/edit"), 'label' => trans('texts.view_invoice')];
     } else {
         $actions[] = ['url' => 'javascript:submitAction("invoice")', 'label' => trans('texts.invoice_expense')];
         // check for any open invoices
         $invoices = $expense->client_id ? $this->invoiceRepo->findOpenInvoices($expense->client_id, ENTITY_EXPENSE) : [];
         foreach ($invoices as $invoice) {
             $actions[] = ['url' => 'javascript:submitAction("add_to_invoice", ' . $invoice->public_id . ')', 'label' => trans('texts.add_to_invoice', ['invoice' => $invoice->invoice_number])];
         }
     }
     $actions[] = \DropdownButton::DIVIDER;
     if (!$expense->trashed()) {
         $actions[] = ['url' => 'javascript:submitAction("archive")', 'label' => trans('texts.archive_expense')];
         $actions[] = ['url' => 'javascript:onDeleteClick()', 'label' => trans('texts.delete_expense')];
     } else {
         $actions[] = ['url' => 'javascript:submitAction("restore")', 'label' => trans('texts.restore_expense')];
     }
     $data = ['vendor' => null, 'expense' => $expense, 'method' => 'PUT', 'url' => 'expenses/' . $expense->public_id, 'title' => 'Edit Expense', 'actions' => $actions, 'vendors' => Vendor::scope()->with('vendor_contacts')->orderBy('name')->get(), 'vendorPublicId' => $expense->vendor ? $expense->vendor->public_id : null, 'clients' => Client::scope()->with('contacts')->orderBy('name')->get(), 'clientPublicId' => $expense->client ? $expense->client->public_id : null, 'categoryPublicId' => $expense->expense_category ? $expense->expense_category->public_id : null];
     $data = array_merge($data, self::getViewModel());
     return View::make('expenses.edit', $data);
 }