Beispiel #1
0
 public static function getPublicId()
 {
     $user = UserBranch::PublicId()->orderBy('public_id', 'DESC')->select('public_id')->first();
     $nextPublicId = $user->public_id;
     if ($nextPublicId) {
         $nextPublicId = $nextPublicId + 1;
     } else {
         $nextPublicId = 1;
     }
     return $nextPublicId;
 }
Beispiel #2
0
 /**
  * Display a listing of the resource.
  *
  * @return Response
  */
 public function index()
 {
     //
     $cuenta = Account::find(Auth::user()->account_id);
     // $branch = DB::table('branches')->where('account_id',Auth::user()->account_id)->where('id','=',Auth::user()->branch_id)->first();
     // $user->branch = $branch->name;
     // $branches =$cuenta->branches;
     if (Auth::user()->is_admin) {
         $branches = Account::find(Auth::user()->account_id)->branches;
         // $sucursales = Branch::find(Auth::user()->account_id);
         $sucursales = array();
         foreach ($branches as $branch) {
             # code...
             $sucursales[] = array('branch_id' => $branch->id, 'name' => $branch->name);
         }
         // return View::make('users.selectBranch')->with('sucursales',$sucursales);
         // return Response::json($sucursales);
     } else {
         $sucursales = UserBranch::getSucursales(Auth::user()->id);
     }
     // return $branches;
     // $categories = DB::table('categories')->where('account_id',Auth::user()->account_id)->get(array('name'));
     $categories = $cuenta->categories;
     // return $categories;
     $cats = $categories;
     $categories2 = $cuenta->categories;
     // $categories2 = DB::table('categories')->where('account_id',Auth::user()->account_id)->get();
     $products2 = $cuenta->products;
     // $products2 = DB::table('products')->where('account_id','=',Auth::user()->account_id)->get();
     $aux = array();
     foreach ($categories2 as $category) {
         foreach ($products2 as $product) {
             $pts = DB::table('products')->where('category_id', $category->id)->where('account_id', '=', Auth::user()->account_id)->get(array('id', 'product_key', 'notes', 'cost'));
             $prod = array($category->name => $pts);
         }
         $aux += $prod;
     }
     $mensaje = array('productos' => $aux, 'categorias' => $categories, 'first_name' => Auth::user()->first_name, 'last_name' => Auth::user()->last_name, 'branch' => $sucursales, 'name' => $cuenta->name, 'subdominio' => $cuenta->domain);
     return Response::json($mensaje);
 }
Beispiel #3
0
 public function Actualizar()
 {
     if ($this->id) {
         if (empty($this->fv_error_message)) {
             $facturas = Invoice::where('branch_id', $this->id)->where('account_id', $this->account_id)->first();
             //si no tienen facturas seguir
             if (!$facturas) {
                 $usuarios = UserBranch::getUsersBranch($this->id, $this->account_id);
                 // si no tiene usuarios asignados y no tiene facturas puede hacer los cambios --esto es para de baja necesito descansar XD
                 // if(!$usuarios)
                 // {
                 $this->account_id = $this->account_id ? $this->account->id : $this->fv_account_id;
                 $this->name = $this->fv_name;
                 $this->number_branch = $this->fv_number_branch;
                 $this->address2 = $this->fv_address2;
                 $this->address1 = $this->fv_address1;
                 $this->work_phone = $this->fv_workphone;
                 $this->city = $this->fv_city;
                 $this->state = $this->fv_state;
                 $this->deadline = $this->fv_deadline;
                 $this->key_dosage = $this->fv_key_dossage;
                 $this->economic_activity = $this->fv_economic_activity;
                 $this->number_process = $this->fv_number_process;
                 $this->number_autho = $this->fv_nummber_autho;
                 $this->law = $this->fv_law;
                 $this->type_third = $this->getType_thrird();
                 $this->invoice_number_counter = 1;
                 $this->save();
                 //verificar los nuevos asignados
                 //aplicando algorimo de asignacion
                 foreach (TypeDocumentBranch::where('branch_id', $this->id)->get() as $type_document_branch) {
                     # code...
                     $type_document_branch->delete();
                 }
                 foreach ($this->fv_type_documents_branch as $type_document_nuevos) {
                     # code...
                     //TODO: acabar esta parte de la consulta me falta la asignacion  XD ...... :()
                     $existeAsignado = TypeDocumentBranch::withTrashed()->where('branch_id', $this->id)->where('type_document_id', $type_document_nuevos)->first();
                     if ($existeAsignado) {
                         $existeAsignado->restore();
                     } else {
                         $tipo = new TypeDocumentBranch();
                         $tipo->branch_id = $this->id;
                         $tipo->type_document_id = $type_document_nuevos;
                         $tipo->save();
                     }
                 }
                 $this->fv_error_message = "Registro Actualizado";
                 return true;
                 // }
                 // $this->fv_error_message = $this->fv_error_message . ' debe reasignar a los usuarios de esta sucursal <br>';
                 // return false;
             }
             //si tiene facturas hay que verificar la  fecha actual sea mayor a la fecha limite
             $this->name = $this->fv_name;
             $this->address2 = $this->fv_address2;
             $this->address1 = $this->fv_address1;
             $this->work_phone = $this->fv_workphone;
             $this->city = $this->fv_city;
             $this->state = $this->fv_state;
             $fecha_actual = new DateTime("now");
             $fecha_limite = new DateTime($this->deadline);
             if ($fecha_actual > $fecha_limite) {
                 $this->number_branch = $this->fv_number_branch;
                 //docificaciones y numero de invoicce nada masXD
                 $this->deadline = $this->fv_deadline;
                 $this->key_dosage = $this->fv_key_dossage;
                 $this->economic_activity = $this->fv_economic_activity;
                 $this->number_process = $this->fv_number_process;
                 $this->number_autho = $this->fv_nummber_autho;
                 $this->law = $this->fv_law;
                 $this->type_third = $this->getType_thrird();
                 //colocamos la sucursal en 1 de nuevo
                 $this->invoice_number_counter = 1;
             }
             $this->save();
             //modificacion
             foreach (TypeDocumentBranch::where('branch_id', $this->id) as $type_document_branch) {
                 # code...
                 $type_document_branch->delete();
             }
             foreach ($this->fv_type_documents_branch as $type_document_nuevos) {
                 # code...
                 //TODO: acabar esta parte de la consulta me falta la asignacion  XD ...... :()
                 $existeAsignado = TypeDocumentBranch::withTrashed()->where('branch_id', $this->id)->where('type_document_id', $type_document_nuevos)->first();
                 if ($existeAsignado) {
                     $existeAsignado->restore();
                 } else {
                     $tipo = new TypeDocumentBranch();
                     $tipo->branch_id = $this->id;
                     $tipo->type_document_id = $type_document_nuevos;
                     $tipo->save();
                 }
             }
             $this->fv_error_message = "Registro Actualizado";
             return true;
         }
     }
     $this->fv_error_message = $this->fv_error_message . ' Sucursal ' . ERROR_NULL . '<br>';
     return false;
 }
Beispiel #4
0
 public function indexSucursal()
 {
     if (Auth::user()->is_admin) {
         $branches = Account::find(Auth::user()->account_id)->branches;
         // $sucursales = Branch::find(Auth::user()->account_id);
         $sucursales = array();
         foreach ($branches as $branch) {
             # code...
             $sucursales[] = array('id' => $branch->id, 'text' => $branch->name);
         }
         return View::make('users.selectBranch')->with('sucursales', $sucursales);
         // return Response::json($sucursales);
     } else {
         $branches = UserBranch::getSucursales(Auth::user()->id);
         // return Response::json($branches);
         $sucursales = array();
         foreach ($branches as $branch) {
             # code...
             $sucursales[] = array('id' => $branch->branch_id, 'text' => $branch->name);
         }
     }
     // return Response::json($sucursales);
     return View::make('users.selectBranch')->with('sucursales', $sucursales);
 }
Beispiel #5
0
 public function guardarFactura()
 {
     $input = json_decode(json_encode(Input::all()), FALSE);
     $numero = (int) $input->invoice_number;
     $user_branch = UserBranch::where('user_id', Auth::user()->id)->first();
     $invoice_number = $numero;
     $client_id = $input->client_id;
     // return Response::json($client_id);
     $user = DB::table('users')->select('account_id', 'price_type_id')->where('id', Auth::user()->id)->first();
     $branch = DB::table('branches')->where('id', '=', $user_branch->branch_id)->first();
     $items = $input->invoice_items;
     $amount = 0;
     $subtotal = 0;
     $fiscal = 0;
     $icetotal = 0;
     $bonidesc = 0;
     foreach ($items as $item) {
         # code...
         $product_id = $item->id;
         $pr = DB::table('products')->join('prices', "product_id", "=", 'products.id')->select('products.id', 'products.notes', 'prices.cost', 'products.ice', 'products.units', 'products.cc')->where('prices.price_type_id', '=', $user->price_type_id)->where('products.account_id', '=', $user->account_id)->where('products.id', "=", $product_id)->first();
         $qty = (int) $item->qty;
         $cost = $pr->cost / $pr->units;
         $st = $cost * $qty;
         $subtotal = $subtotal + $st;
         //$bd= ($item['boni']*$cost) + $item['desc'];
         $bd = $item->boni * $cost + $item->desc;
         $bonidesc = $bonidesc + $bd;
         $amount = $amount + $st - $bd;
         $ice = DB::table('tax_rates')->select('rate')->where('name', '=', 'ICE')->first();
         if ($pr->ice == 1) {
             //caluclo ice bruto
             $iceBruto = $qty * ($pr->cc / 1000) * $ice->rate;
             //$iceNeto = (((int)$item['boni']) *($pr->cc/1000)*$ice->rate);
             $iceNeto = (int) $item->boni * ($pr->cc / 1000) * $ice->rate;
             $icetotal = $icetotal + ($iceBruto - $iceNeto);
             // $fiscal = $fiscal + ($amount - ($item['qty'] *($pr->cc/1000)*$ice->rate) );
         }
     }
     $fiscal = $subtotal - $bonidesc - $icetotal;
     $balance = $amount;
     /////////////////////////hasta qui esta bien al parecer hacer prueba de que fuciona el join de los productos XD
     $invoice_date = date("Y-m-d", strtotime($input->fecha));
     $invoice_date_limitCC = date("Y/m/d", strtotime($branch->deadline));
     $ice = DB::table('tax_rates')->select('rate')->where('name', '=', 'ice')->first();
     //
     // creando invoice
     $cuenta = Account::find(1);
     $invoice = Invoice::createNew();
     $invoice->invoice_status_id = 6;
     $invoice->invoice_number = $invoice_number;
     $invoice->client_id = $client_id;
     $invoice->user_id = Auth::user()->id;
     $invoice->account_id = Auth::user()->account_id;
     $invoice->branch_id = $user_branch->branch_id;
     $invoice->importe_neto = number_format((double) $amount, 2, '.', '');
     $invoice->importe_total = $subtotal;
     $invoice->debito_fiscal = $fiscal;
     $invoice->law = $branch->law;
     $invoice->balance = $balance;
     // $invoice->control_code=$cod_control;
     $invoice->start_date = $invoice_date;
     $invoice->invoice_date = $invoice_date;
     $invoice->economic_activity = $branch->economic_activity;
     // $invoice->activity_sec1=$branch->activity_sec1;
     // $invoice->invoice
     $invoice->end_date = $invoice_date_limitCC;
     //datos de la empresa atra vez de una consulta XD
     /*****************error generado al intentar guardar **/
     $invoice->branch_name = $branch->name;
     $invoice->address1 = $branch->address1;
     $invoice->address2 = $branch->address2;
     $invoice->number_autho = $branch->number_autho;
     $invoice->phone = $branch->work_phone;
     $invoice->city = $branch->city;
     $invoice->state = $branch->state;
     $invoice->account_name = $cuenta->name;
     $invoice->account_nit = $cuenta->nit;
     // $invoice->industry_id=$branch->industry_id;
     $invoice->qr = $invoice->account_nit . '|' . $invoice->invoice_number . '|' . $invoice->number_autho . '|' . $invoice->invoice_date . '|' . $invoice->importe_neto . '|' . $invoice->importe_total . '|' . $invoice->client_nit . '|' . $invoice->importe_ice . '|0|0|' . $invoice->descuento_total;
     // $invoice->country_id= $branch->country_id;
     $invoice->key_dosage = $branch->key_dosage;
     $invoice->deadline = $branch->deadline;
     $invoice->importe_ice = $icetotal;
     // $invoice->ice = $ice->rate;
     //cliente
     //$invoice->client_nit=$input['nit'];
     $invoice->client_nit = $input->nit;
     ///$invoice->client_name =$input['name'];
     $invoice->client_name = $input->name;
     $documents = TypeDocumentBranch::where('branch_id', $invoice->branch_id)->orderBy('id', 'ASC')->get();
     foreach ($documents as $document) {
         $actual_document = TypeDocument::where('id', $document->type_document_id)->first();
         if ($actual_document->master_id == 1) {
             $id_documento = $actual_document->id;
         }
     }
     $invoice->setJavascript($id_documento);
     $invoice->logo = 0;
     //$invoice->save();
     $account = Auth::user()->account;
     $numAuth = $invoice->number_autho;
     $numfactura = $invoice->invoice_number;
     $nit = $invoice->client_nit;
     $fechaEmision = date("Ymd", strtotime($invoice->invoice_date));
     $total = $invoice->importe_total;
     $llave = $branch->key_dosage;
     $codigoControl = Utils::getControlCode($numfactura, $nit, $fechaEmision, $total, $numAuth, $llave);
     $invoice->control_code = $codigoControl;
     $invoice->save();
     // return Response::json($invoice);
     //guardadndo los invoice items
     foreach ($items as $item) {
         // $product = DB::table('products')->select('notes')->where('id',$product_id)->first();
         //$product_id = $item['id'];
         $product_id = $item->id;
         $product = DB::table('products')->join('prices', "product_id", "=", 'products.id')->select('products.id', 'products.notes', 'prices.cost', 'products.ice', 'products.units', 'products.cc', 'products.product_key')->where('prices.price_type_id', '=', $user->price_type_id)->where('products.account_id', '=', $user->account_id)->where('products.id', "=", $product_id)->first();
         // $pr = DB::table('products')->select('cost')->where('id',$product_id)->first();
         $cost = $product->cost / $product->units;
         //$line_total= ((int)$item['qty'])*$cost;
         $line_total = (int) $item->qty * $cost;
         $invoiceItem = InvoiceItem::createNew();
         // $invoiceItem->invoice_id = $invoice->id;
         $invoiceItem->invoice_id = $invoice->id;
         $invoiceItem->product_id = $product_id;
         $invoiceItem->product_key = $product->product_key;
         $invoiceItem->notes = $product->notes;
         $invoiceItem->cost = $cost;
         /*$invoiceItem->boni = (int)$item['boni'];
           $invoiceItem->discount =$item['desc'];
           $invoiceItem->qty = (int)$item['qty'];*/
         $invoiceItem->boni = (int) $item->boni;
         $invoiceItem->discount = $item->desc;
         $invoiceItem->qty = (int) $item->qty;
         // $invoiceItem->line_total=$line_total;
         // $invoiceItem->tax_rate = 0;
         $invoiceItem->save();
     }
     return Response::json($invoice);
 }