public function edit($publicId) { $credit = Credit::scope($publicId)->firstOrFail(); $credit->credit_date = Utils::fromSqlDate($credit->credit_date); $data = array('client' => null, 'credit' => $credit, 'method' => 'PUT', 'url' => 'credits/' . $publicId, 'title' => 'Edit Credit', 'clients' => Client::scope()->with('contacts')->orderBy('name')->get()); return View::make('credit.edit', $data); }
/** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function bulk() { $public_id = Input::get('public_id'); $credit = Credit::scope($public_id)->first(); if ($credit->balance < $credit->amount) { $message = "El Crédito ya fue utilizado en pagos."; Session::flash('error', $message); return Redirect::to('clientes'); } else { $credit->delete(); $message = "Crédito eliminado con éxito"; Session::flash('message', $message); return Redirect::to('creditos'); } }
private function save() { $rules = array('client' => 'required', 'invoice' => 'required', 'amount' => 'required|positive'); if (Input::get('invoice')) { $invoice = Invoice::scope(Input::get('invoice'))->firstOrFail(); $rules['amount'] .= '|less_than:' . $invoice->balance; } if (Input::get('payment_type_id') == PAYMENT_TYPE_CREDIT) { $rules['payment_type_id'] = 'has_credit:' . Input::get('client') . ',' . Input::get('amount'); } $messages = array('required' => 'El campo es Requerido', 'positive' => 'El Monto debe ser mayor a cero', 'less_than' => 'El Monto debe ser menor o igual a ' . $invoice->balance, 'has_credit' => 'El Cliente no tiene crédito suficiente'); $validator = \Validator::make(Input::all(), $rules, $messages); if ($validator->fails()) { $url = 'pagos/create'; return Redirect::to($url)->withErrors($validator)->withInput(); } else { $payment = Payment::createNew(); $paymentTypeId = Input::get('payment_type_id') ? Input::get('payment_type_id') : null; $clientId = Client::getPrivateId(Input::get('client')); $amount = floatval(Input::get('amount')); if ($paymentTypeId == PAYMENT_TYPE_CREDIT) { $credits = Credit::scope()->where('client_id', '=', $clientId)->where('balance', '>', 0)->orderBy('created_at')->get(); $applied = 0; foreach ($credits as $credit) { $applied += $credit->apply($amount); if ($applied >= $amount) { break; } } } $payment->client_id = $clientId; $payment->invoice_id = Invoice::getPrivateId(Input::get('invoice')); $payment->payment_type_id = $paymentTypeId; $payment->payment_date = date("Y-m-d", strtotime(Input::get('payment_date'))); $payment->amount = $amount; $payment->transaction_reference = trim(Input::get('transaction_reference')); $payment->save(); Session::flash('message', 'Pago creado con éxito'); return Redirect::to('clientes/' . Input::get('client')); } }
/** * Display the specified resource. * * @param int $id * @return Response */ public function show($publicId) { $client = Client::scope($publicId)->withTrashed()->with('contacts')->first(); if ($client) { //$client = Client::scope($publicId)->with('contacts')->firstOrFail(); $getTotalCredit = Credit::scope()->where('client_id', '=', $client->id)->whereNull('deleted_at')->where('balance', '>', 0)->sum('balance'); $invoices = Invoice::join('invoice_statuses', 'invoice_statuses.id', '=', 'invoices.invoice_status_id')->where('invoices.account_id', Auth::user()->account_id)->where('invoices.client_id', $client->id)->where('invoices.branch_id', Session::get('branch_id'))->select('invoices.invoice_number', 'invoices.invoice_date', 'invoices.importe_total', 'invoices.balance', 'invoices.due_date', 'invoice_statuses.name', 'invoices.public_id')->get(); $pagos = Payment::join('invoices', 'invoices.id', '=', 'payments.invoice_id')->join('payment_types', 'payment_types.id', '=', 'payments.payment_type_id')->where('payments.account_id', Auth::user()->account_id)->where('payments.client_id', $client->id)->select('invoices.invoice_number', 'payments.transaction_reference', 'payment_types.name', 'payments.amount', 'payments.payment_date')->get(); $creditos = Credit::where('account_id', '=', Auth::user()->account_id)->where('client_id', '=', $client->getId())->get(); $data = array('title' => 'Ver Cliente', 'client' => $client, 'invoices' => $invoices, 'pagos' => $pagos, 'credit' => $getTotalCredit, 'creditos' => $creditos); // return Response::json($data); return View::make('clientes.show', $data); } Session::flash('error', 'No existe el usuario'); return Redirect::to('clientes'); }
private function save($publicId = null) { $action = Input::get('action'); $entityType = Input::get('entityType'); if ($action == 'archive' || $action == 'delete' || $action == 'mark') { return InvoiceController::bulk($entityType); } $input = json_decode(Input::get('data')); //echo "this is the result"; $invoice = $input->invoice; //print_r($invoice);exit(); $branch = Branch::where('account_id', '=', Auth::user()->account_id)->where('id', Auth::user()->branch_id)->first(); $today = new DateTime('now'); $today = $today->format('Y-m-d'); $datelimit = DateTime::createFromFormat('Y-m-d', $branch->deadline); $datelimit = $datelimit->format('Y-m-d'); $first = explode("-", $datelimit); $second = explode("-", $today); $first_day = $first[2]; $first_month = $first[1]; $first_year = $first[0]; $second_day = $second[2]; $second_month = $second[1]; $second_year = $second[0]; $a = gregoriantojd($first_month, $first_day, $first_year); $b = gregoriantojd($second_month, $second_day, $second_year); $errorS = "Expiró la Fecha Límite de " . $branch->name; if ($a - $b < 0) { Session::flash('error', $errorS); return Redirect::to("{$entityType}s/create")->withInput(); } else { $last_invoice = Invoice::where('account_id', '=', Auth::user()->account_id)->first(); if ($last_invoice) { $yesterday = $last_invoice->invoice_date; $today = date("Y-m-d", strtotime($invoice->invoice_date)); $errorD = "La fecha de la factura es incorrecta"; $yesterday = new DateTime($yesterday); $today = new DateTime($today); if ($yesterday > $today) { Session::flash('error', $errorD); return Redirect::to("{$entityType}s/create")->withInput(); } } if (false && ($errors = $this->invoiceRepo->getErrors($invoice))) { Session::flash('error', trans('texts.invoice_error')); return Redirect::to("{$entityType}s/create")->withInput()->withErrors($errors); } else { //$this->taxRateRepo->save($input->tax_rates); $clientData = (array) $invoice->client; $clientData['branch'] = $branch->id; $client = $this->saveClient($invoice->client->public_id, $clientData); $invoiceData = (array) $invoice; $invoiceData['branch_id'] = $branch->id; $invoiceData['client_id'] = $client->id; $invoiceData['client_nit'] = $client->nit; $invoiceData['client_name'] = $client->name; $invoiceData['action'] = $action; //$invoice = $this->invoiceRepo->save($publicId, $invoiceData, $entityType); $account = Auth::user()->account; $client->load('contacts'); $sendInvoiceIds = []; foreach ($client->contacts as $contact) { if ($contact->send_invoice || count($client->contacts) == 1) { $sendInvoiceIds[] = $contact->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(); } else if (!in_array($contact->id, $sendInvoiceIds) && $invitation) { $invitation->delete(); } }*/ $message = trans($publicId ? "texts.updated_{$entityType}" : "texts.created_{$entityType}"); if ($input->invoice->client->public_id == '-1') { $message = $message . ' ' . trans('texts.and_created_client'); $url = URL::to('clients/' . $client->public_id); Utils::trackViewed($client->getDisplayName(), ENTITY_CLIENT, $url); } if ($action == 'email') { $aux = 0; foreach ($client->contacts as $contact) { if ($contact->email) { $aux = 1; } } if ($aux == 0) { $errorMessage = trans('El cliente no tiene Correo Electrónico.'); Session::flash('error', $errorMessage); } else { if (Auth::user()->confirmed && !Auth::user()->isDemo()) { $message = trans("texts.emailed_{$entityType}"); $this->mailer->sendInvoice($invoice); Session::flash('message', $message); } else { $errorMessage = trans(Auth::user()->registered ? 'texts.confirmation_required' : 'texts.registration_required'); Session::flash('error', $errorMessage); Session::flash('message', $message); } } } else { if ($action == 'savepay') { $payment = Payment::createNew(); $payment->client_id = $client->id; $payment->invoice_id = $invoice->id; $payment->payment_type_id = 1; $payment->payment_date = $invoice->invoice_date; $payment->amount = $invoice->amount; $payment->save(); $message = trans("texts.savepay_{$entityType}"); Session::flash('message', $message); } else { if ($action == 'savepaycredit') { $payment = Payment::createNew(); $credits = Credit::scope()->where('client_id', '=', $client->id)->where('balance', '>', 0)->orderBy('created_at')->get(); $applied = 0; foreach ($credits as $credit) { $applied += $credit->apply($invoice->amount); if ($applied >= $invoice->amount) { break; } } $payment->client_id = $client->id; $payment->invoice_id = $invoice->id; $payment->payment_type_id = 2; $payment->payment_date = $invoice->invoice_date; $payment->amount = $invoice->amount; $payment->save(); $message = trans("texts.savepay_{$entityType}"); Session::flash('message', $message); } else { Session::flash('message', $message); } } } //$url = "factura/" . $invoice->public_id . '/show'; $url = "factura/1"; return Redirect::to($url); } } }
private function save($publicId = null) { $action = Input::get('action'); $entityType = Input::get('entityType'); if ($action == 'archive' || $action == 'delete' || $action == 'mark') { return InvoiceController::bulk($entityType); } $input = json_decode(Input::get('data')); $invoice = $input->invoice; if (Utils::isAdmin()) { $branch_id = $input->invoice->branch_id; $branch = Branch::where('account_id', '=', Auth::user()->account_id)->where('public_id', $branch_id)->first(); // $branch = DB::table('branches')->where('id',$branch_id)->first(); } else { $branch = Auth::user()->branch; $branch_id = $branch->id; $branch = DB::table('branches')->where('id', $branch_id)->first(); } $today = new DateTime('now'); $today = $today->format('Y-m-d'); $datelimit = DateTime::createFromFormat('Y-m-d', $branch->deadline); $datelimit = $datelimit->format('Y-m-d'); $valoresPrimera = explode("-", $datelimit); $valoresSegunda = explode("-", $today); $diaPrimera = $valoresPrimera[2]; $mesPrimera = $valoresPrimera[1]; $anyoPrimera = $valoresPrimera[0]; $diaSegunda = $valoresSegunda[2]; $mesSegunda = $valoresSegunda[1]; $anyoSegunda = $valoresSegunda[0]; $a = gregoriantojd($mesPrimera, $diaPrimera, $anyoPrimera); $b = gregoriantojd($mesSegunda, $diaSegunda, $anyoSegunda); $errorS = "Expiró la fecha límite de " . $branch->name; if ($a - $b < 0) { Session::flash('error', $errorS); return Redirect::to("{$entityType}s/create")->withInput(); } else { $last_invoice = Invoice::where('account_id', '=', Auth::user()->account_id)->first(); if ($last_invoice) { $yesterday = $last_invoice->invoice_date; $today = date("Y-m-d", strtotime($invoice->invoice_date)); $errorD = "La fecha de la factura es incorrecta"; $yesterday = new DateTime($yesterday); $today = new DateTime($today); if ($yesterday > $today) { Session::flash('error', $errorD); return Redirect::to("{$entityType}s/create")->withInput(); } } if ($errors = $this->invoiceRepo->getErrors($invoice)) { Session::flash('error', trans('texts.invoice_error')); return Redirect::to("{$entityType}s/create")->withInput()->withErrors($errors); } else { $this->taxRateRepo->save($input->tax_rates); $clientData = (array) $invoice->client; $clientData['branch'] = $branch->id; $client = $this->clientRepo->save($invoice->client->public_id, $clientData); $invoiceData = (array) $invoice; $invoiceData['branch_id'] = $branch->id; $invoiceData['client_id'] = $client->id; $invoiceData['client_nit'] = $client->nit; $invoiceData['client_name'] = $client->name; $invoiceData['action'] = $action; $invoice = $this->invoiceRepo->save($publicId, $invoiceData, $entityType); $account = Auth::user()->account; // if ($account->invoice_taxes != $input->invoice_taxes // || $account->invoice_item_taxes != $input->invoice_item_taxes // || $account->invoice_design_id != $input->invoice->invoice_design_id) // { // $account->invoice_taxes = $input->invoice_taxes; // $account->invoice_item_taxes = $input->invoice_item_taxes; // $account->invoice_design_id = $input->invoice->invoice_design_id; // $account->save(); // } $client->load('contacts'); $sendInvoiceIds = []; foreach ($client->contacts as $contact) { if ($contact->send_invoice || count($client->contacts) == 1) { $sendInvoiceIds[] = $contact->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(); } else { if (!in_array($contact->id, $sendInvoiceIds) && $invitation) { $invitation->delete(); } } } $invoice_date = date("d/m/Y", strtotime($invoice->invoice_date)); require_once app_path() . '/includes/BarcodeQR.php'; // $ice = $invoice->amount-$invoice->fiscal; $desc = $invoice->subtotal - $invoice->amount; $subtotal = number_format($invoice->subtotal, 2, '.', ''); $amount = number_format($invoice->amount, 2, '.', ''); $fiscal = number_format($invoice->fiscal, 2, '.', ''); // $icef = number_format($ice, 2, '.', ''); $descf = number_format($desc, 2, '.', ''); // if($icef=="0.00"){ // $icef = 0; // } if ($descf == "0.00") { $descf = 0; } $icef = 0; $qr = new BarcodeQR(); $datosqr = $invoice->account_nit . '|' . $invoice->invoice_number . '|' . $invoice->number_autho . '|' . $invoice_date . '|' . $subtotal . '|' . $amount . '|' . $invoice->control_code . '|' . $invoice->client_nit . '|' . $icef . '|0|0|' . $descf; $qr->text($datosqr); $qr->draw(150, 'qr/' . $account->account_key . '_' . $branch->name . '_' . $invoice->invoice_number . '.png'); $input_file = 'qr/' . $account->account_key . '_' . $branch->name . '_' . $invoice->invoice_number . '.png'; $output_file = 'qr/' . $account->account_key . '_' . $branch->name . '_' . $invoice->invoice_number . '.jpg'; $inputqr = imagecreatefrompng($input_file); list($width, $height) = getimagesize($input_file); $output = imagecreatetruecolor($width, $height); $white = imagecolorallocate($output, 255, 255, 255); imagefilledrectangle($output, 0, 0, $width, $height, $white); imagecopy($output, $inputqr, 0, 0, 0, 0, $width, $height); imagejpeg($output, $output_file); $invoice->qr = HTML::image_data('qr/' . $account->account_key . '_' . $branch->name . '_' . $invoice->invoice_number . '.jpg'); $invoice->save(); $message = trans($publicId ? "texts.updated_{$entityType}" : "texts.created_{$entityType}"); if ($input->invoice->client->public_id == '-1') { $message = $message . ' ' . trans('texts.and_created_client'); $url = URL::to('clients/' . $client->public_id); Utils::trackViewed($client->getDisplayName(), ENTITY_CLIENT, $url); } if ($action == 'clone') { return $this->cloneInvoice($publicId); } else { if ($action == 'convert') { return $this->convertQuote($publicId); } else { if ($action == 'email') { $aux = 0; foreach ($client->contacts as $contact) { if ($contact->email) { $aux = 1; } } if ($aux == 0) { $errorMessage = trans('El cliente no tiene Correo Electrónico.'); Session::flash('error', $errorMessage); } else { if (Auth::user()->confirmed && !Auth::user()->isDemo()) { $message = trans("texts.emailed_{$entityType}"); $this->mailer->sendInvoice($invoice); Session::flash('message', $message); } else { $errorMessage = trans(Auth::user()->registered ? 'texts.confirmation_required' : 'texts.registration_required'); Session::flash('error', $errorMessage); Session::flash('message', $message); } } } else { if ($action == 'savepay') { $payment = Payment::createNew(); $payment->client_id = $client->id; $payment->invoice_id = $invoice->id; $payment->payment_type_id = 1; $payment->payment_date = $invoice->invoice_date; $payment->amount = $invoice->amount; $payment->save(); $message = trans("texts.savepay_{$entityType}"); Session::flash('message', $message); } else { if ($action == 'savepaycredit') { $payment = Payment::createNew(); $credits = Credit::scope()->where('client_id', '=', $client->id)->where('balance', '>', 0)->orderBy('created_at')->get(); $applied = 0; foreach ($credits as $credit) { $applied += $credit->apply($invoice->amount); if ($applied >= $invoice->amount) { break; } } $payment->client_id = $client->id; $payment->invoice_id = $invoice->id; $payment->payment_type_id = 2; $payment->payment_date = $invoice->invoice_date; $payment->amount = $invoice->amount; $payment->save(); $message = trans("texts.savepay_{$entityType}"); Session::flash('message', $message); } else { Session::flash('message', $message); } } } } } $url = "{$entityType}s/" . $invoice->public_id . '/edit'; return Redirect::to($url); } } }
/** * Store a newly created resource in storage. * * @return Response */ public function store() { //return 0; // return Response::json(Input::all()); // $rules = array( // 'client' => 'required', // 'invoice' => 'required', // 'amount' => 'required' // ); // if (Input::get('invoice')) { // $invoice = Invoice::scope(Input::get('invoice'))->firstOrFail(); // $rules['amount'] .= '|less_than:' . $invoice->balance; // } // if (Input::get('payment_type_id') == PAYMENT_TYPE_CREDIT) // { // $rules['payment_type_id'] = 'has_credit:' . Input::get('client') . ',' . Input::get('amount'); // } // $messages = array( // 'required' => 'El campo es Requerido', // 'positive' => 'El Monto debe ser mayor a cero', // 'less_than' => 'El Monto debe ser menor o igual a ' . $invoice->balance, // 'has_credit' => 'El Cliente no tiene crédito suficiente' // ); // $validator = \Validator::make(Input::all(), $rules, $messages); // if ($validator->fails()) // { // $url = 'pagos/create'; // return Redirect::to($url) // ->withErrors($validator) // ->withInput(); // } // else // { $payment = Payment::createNew(); $paymentTypeId = Input::get('payment_type_id'); $clientId = Input::get('client'); $amount = floatval(Input::get('amount')); // if ($paymentTypeId == PAYMENT_TYPE_CREDIT) // { // $credits = Credit::scope()->where('client_id', '=', $clientId) // ->where('balance', '>', 0)->orderBy('created_at')->get(); // $applied = 0; // foreach ($credits as $credit) // { // $applied += $credit->apply($amount); // if ($applied >= $amount) // { // break; // } // } // } // $payment->client_id = $clientId; // $payment->invoice_id =Input::get('invoice'); // $payment->payment_type_id = $paymentTypeId; // $payment->user_id = Auth::user()->id; // $payment->payment_date = date("Y-m-d",strtotime(Input::get('payment_date'))); // $payment->amount = $amount; // $payment->transaction_reference = trim(Input::get('transaction_reference')); $payment->setClientId($clientId); $payment->setInvoiceId(Input::get('invoice')); $payment->setPaymentTypeId($paymentTypeId); $payment->setUserId(Auth::user()->id); $dateparser = explode("/", Input::get('payment_date')); $date = $dateparser[2] . '-' . $dateparser[1] . '-' . $dateparser[0]; $payment->setPaymentDate($date); $payment->setAmount($amount); $payment->setTransactionReference(trim(Input::get('transaction_reference'))); $error = $payment->guardar(); if ($error) { Session::flash('error', $error); return Redirect::to('pagos/create'); } $payment->save(); $cliente = Client::find($payment->client_id); $cliente->balance = $cliente->balance - $payment->amount; $cliente->paid_to_date = $cliente->paid_to_date + $payment->amount; $cliente->save(); $invoice = Invoice::find($payment->invoice_id); $invoice->balance = $invoice->balance - $payment->amount; $invoice->save(); if ($paymentTypeId == PAYMENT_TYPE_CREDIT) { $credits = Credit::scope()->where('client_id', '=', $clientId)->where('balance', '>', 0)->orderBy('created_at')->get(); $applied = 0; foreach ($credits as $credit) { $applied += $credit->apply($amount); if ($applied >= $amount) { break; } } } $paymentName = PaymentType::where('id', '=', $paymentTypeId)->first(); if ($invoice->balance == 0) { // $invoice->invoice_status_id = INVOICE_STATUS_PAID; Utils::addNote($invoice->id, '<b>' . $invoice->getClientName() . ': </b>Totalmente pagada; se pagó:<b>' . $payment->amount . '</b>Bs, con <b>' . $paymentName->name . '</b>', INVOICE_STATUS_PAID); } else { // $invoice->invoice_status_id = INVOICE_STATUS_PARTIAL; Utils::addNote($invoice->id, '<b>' . $invoice->getClientName() . ': </b>Parcialmente pagado; se pagó:<b>' . $payment->amount . '</b> Bs, con <b>' . $paymentName->name . '</b>', INVOICE_STATUS_PARTIAL); } Session::flash('message', 'Pago creado con éxito'); $client = Client::where('id', '=', Input::get('client'))->first(); return Redirect::to('clientes/' . $client->public_id); // } }
public function borrar() { $getTotalCredit = Credit::scope()->where('client_id', '=', $this->id)->whereNull('deleted_at')->where('balance', '>', 0)->sum('balance'); if ($this->balance == 0) { if ($getTotalCredit == 0) { $this->error_message = 'Cliente ' . $this->name . ' con nit ' . $this->nit . ' eliminado con éxito'; $this->delete(); return true; } else { $this->error_message = ERROR_CREDITO; return false; } } else { $this->error_message = ERROR_BALANCE_CLIENTE; return false; } }
private function export() { $output = fopen('php://output', 'w') or Utils::fatalError(); header('Content-Type:application/csv'); header('Content-Disposition:attachment;filename=export.csv'); $clients = Client::scope()->get(); AccountController::exportData($output, $clients->toArray()); $contacts = Contact::scope()->get(); AccountController::exportData($output, $contacts->toArray()); $invoices = Invoice::scope()->get(); AccountController::exportData($output, $invoices->toArray()); $invoiceItems = InvoiceItem::scope()->get(); AccountController::exportData($output, $invoiceItems->toArray()); $payments = Payment::scope()->get(); AccountController::exportData($output, $payments->toArray()); $credits = Credit::scope()->get(); AccountController::exportData($output, $credits->toArray()); fclose($output); exit; }