public static function hasTypeDocument($type_documents_branch, $branch_id) { $existeAsignado = TypeDocumentBranch::where('type_document_id', $type_documents_branch)->where('branch_id', $branch_id)->first(); if ($existeAsignado) { return true; } return false; }
private function saveLote($factura) { $account = DB::table('accounts')->where('id', '=', Auth::user()->account_id)->first(); $branch = Branch::find(Session::get('branch_id')); // return $factura['nit']; $client = Client::where('account_id', '=', Auth::user()->account_id)->where('public_id', $factura['id'])->first(); //if(!$client) // return $factura['nit']; $invoice = Invoice::createNew(); $invoice->setPublicNotes($factura['nota']); $invoice->setBranch(Session::get('branch_id')); $invoice->setInvoiceDate(date("Y-m-d")); $invoice->setClient($client->id); $invoice->setEconomicActivity($branch->economic_activity); $invoice->setDiscount(0); $invoice->setClientName($client->name); $invoice->setClientNit($client->nit); $invoice->setUser(Auth::user()->id); //$date=date("Y-m-d",strtotime(Input::get('invoice_date'))); //$invoice->setInvoiceDate($date); $total_cost = 0; foreach ($factura['products'] as $producto) { //$pr = Product::where('account_id',Auth::user()->account_id)->where('product_key',$producto['product_key'])->first(); //$total_cost+= $pr->cost*$producto['qty']; $total_cost += $producto['cost']; } $invoice->importe_neto = trim($total_cost); $invoice->importe_total = trim($total_cost); //$invoice->note = trim(Input::get('nota')); $invoice->setAccountName($account->name); $invoice->setAccountNit($account->nit); $invoice->setBranchName($branch->name); $invoice->setAddress1($branch->address1); $invoice->setAddress2($branch->address2); $invoice->setPhone($branch->work_phone); $invoice->setCity($branch->city); $invoice->setState($branch->state); $invoice->setNumberAutho($branch->number_autho); $invoice->setKeyDosage($branch->key_dosage); $invoice->setTypeThird($branch->type_third); $invoice->setDeadline($branch->deadline); $invoice->setLaw($branch->law); $type_document = TypeDocument::where('account_id', Auth::user()->account_id)->first(); $invoice->invoice_number = branch::getInvoiceNumber(); $numAuth = $invoice->number_autho; $numfactura = $invoice->invoice_number; $nit = $invoice->client_nit; $fechaEmision = date("Ymd", strtotime($invoice->invoice_date)); $total = $invoice->importe_neto; $llave = $branch->key_dosage; $codigoControl = Utils::getControlCode($numfactura, $nit, $fechaEmision, $total, $numAuth, $llave); $invoice->setControlCode($codigoControl); $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 = 1; $invoice->sfc = $branch->sfc; //$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; if ($account->is_uniper) { $invoice->account_uniper = $account->uniper; } //$invoice->logo = $type_document->logo; $invoice->save(); foreach ($factura['products'] as $producto) { $product = Product::where('account_id', Auth::user()->account_id)->where('product_key', $producto['product_key'])->first(); if ($product != null) { $invoiceItem = InvoiceItem::createNew(); $invoiceItem->setInvoice($invoice->id); $invoiceItem->setProduct($product->id); $invoiceItem->setProductKey($product->product_key); $invoiceItem->setNotes($product->notes . " " . $producto['description']); $invoiceItem->setCost($producto['cost']); $invoiceItem->setQty(1); $invoiceItem->save(); } } }
public function asignarSucursal() { if (Session::has('branch_id')) { Session::forget('branch_id'); Session::forget('branch_name'); Session::forget('printer'); Session::forget('printer'); Session::forget('invoice_link'); } // Session::forget('branch_id'); Session::put('branch_id', Input::get('branch_id')); $sucursal = Branch::find(Session::get('branch_id')); Session::put('branch_name', $sucursal->name); Session::put('printer', Input::get('printer')); $documents = TypeDocumentBranch::where('branch_id', Input::get('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 || $actual_document->master_id == 3) { $id_documento = $actual_document; } } switch ($id_documento->master_id) { case 1: Session::put('invoice_link', 'factura/create'); break; case 3: Session::put('invoice_link', 'sinCreditoFiscal'); break; } // return Response::json(array('info ' =>$sucursal)); return Redirect::to('inicio'); }
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; }
function getWorkingDocuments() { $masters = MasterDocument::get(); $documents = array(); foreach ($masters as $master) { $typeDocument = TypeDocument::where("account_id", Auth::user()->account_id)->where('master_id', $master->id)->orderBy('id', 'DESC')->withTrashed()->first(); if ($typeDocument->deleted_at == null) { $documentBranch = TypeDocumentBranch::where("branch_id", Auth::user()->branch_id)->where('type_document_id', $typeDocument->id)->withTrashed()->orderBy('id', 'DESC')->first(); if ($documentBranch->deleted_at == null) { $doc = (object) ['id' => $master->id, 'name' => $master->name]; array_push($documents, $doc); } } } return $documents; }
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); }
private function saveLote2($factura, $date) { //print_r($factura); //return; $account = DB::table('accounts')->where('id', '=', Auth::user()->account_id)->first(); $branch = Branch::find(Session::get('branch_id')); $client = Client::where('account_id', '=', Auth::user()->account_id)->where('name', $factura['name'])->where('nit', $factura['nit'])->first(); if (!$client) { ///echo "creo"; $client = Client::createNew(); $client->setNit(trim($factura['nit'])); $client->setName(trim($factura['name'])); $client->setBussinesName(trim($factura['razon'])); $alt = $client->guardar(); $client->save(); } $invoice = Invoice::createNew(); $invoice->setPublicNotes(""); $invoice->setBranch(Session::get('branch_id')); $dateparser = explode("/", $date); $date = $dateparser[2] . '-' . $dateparser[1] . '-' . $dateparser[0]; $invoice->setInvoiceDate($date); $invoice->setClient($client->id); $invoice->setEconomicActivity($branch->economic_activity); $invoice->setDiscount(0); $invoice->setClientName($client->business_name); $invoice->setClientNit($client->nit); $invoice->setUser(Auth::user()->id); $total_cost = 0; /*foreach ($factura['products'] as $producto) { $total_cost+= $producto['cost']; }*/ $invoice->importe_neto = $factura['total'] * $factura['qty']; $invoice->importe_total = $factura['total'] * $factura['qty']; $invoice->debito_fiscal = $factura['total'] * $factura['qty']; //$invoice->note = trim(Input::get('nota')); $invoice->setAccountName($account->name); $invoice->setAccountNit($account->nit); $invoice->setBranchName($branch->name); $invoice->setAddress1($branch->address1); $invoice->setAddress2($branch->address2); $invoice->setPhone($branch->work_phone); $invoice->setCity($branch->city); $invoice->setState($branch->state); $invoice->setNumberAutho($branch->number_autho); $invoice->setKeyDosage($branch->key_dosage); $invoice->setTypeThird($branch->type_third); $invoice->setDeadline($branch->deadline); $invoice->setLaw($branch->law); $type_document = TypeDocument::where('account_id', Auth::user()->account_id)->firstOrFail(); $invoice->invoice_number = branch::getInvoiceNumber(); $numAuth = $invoice->number_autho; $numfactura = $invoice->invoice_number; $nit = $invoice->client_nit; $fechaEmision = date("Ymd", strtotime($invoice->invoice_date)); $total = $invoice->importe_neto; $llave = $branch->key_dosage; $totalc = number_format((double) $total, 2, '.', ''); if ($account->currency_id == 2) { $totalc = $total * $account->exchange; $totalc = number_format((double) $totalc, 2, '.', ''); } $codigoControl = Utils::getControlCode($numfactura, $nit, $fechaEmision, $totalc, $numAuth, $llave); $invoice->setControlCode($codigoControl); $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 = 1; //$invoice->sfc = $branch->sfc; if ($account->currency_id == 1) { $invoice->sfc = 1; } else { $invoice->sfc = $account->exchange; } $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; if ($account->is_uniper) { $invoice->account_uniper = $account->uniper; } //$invoice->logo = $type_document->logo; $invoice->save(); //foreach ($factura['products'] as $producto) //{ $product = Product::where('account_id', Auth::user()->account_id)->where('product_key', $factura['code'])->first(); if ($product != null) { $invoiceItem = InvoiceItem::createNew(); $invoiceItem->setInvoice($invoice->id); $invoiceItem->setProduct($product->id); $invoiceItem->setProductKey($product->product_key); $invoiceItem->setNotes($product->notes); //$invoiceItem->setCost($factura['total']); $invoiceItem->setCost($factura['total']); $invoiceItem->setQty($factura['qty']); $invoiceItem->save(); } // $this->sendInvoiceByMailLocal($client->name,$client->business_name,$invoice->id,$invoice->invoice_date,$client->nit); //} }
public function guardarFacturaG() { /* David Guardando factura con el siguiente formato: {"invoice_items":[{"id":"12","qty":"1"},{"id":"16","qty":"1"},{"id":"15","qty":"1"}],"nit":"6047054","name":"keyrus","client_id":"7"} para Golden: nota se adiciona el branch_id por que es necesario para la facturacion {"invoice_items":[{"amount":"60","id":"11"}],"name":"ROMERO","nit":"383423","client_id":"715","branch_id":"2"} */ $input = Input::all(); $branch_id = Input::get('branch_id'); // $invoice_number = Auth::user()->account->getNextInvoiceNumber(); $invoice_number = Branch::getInvoiceNumber($branch_id); $client_id = $input['client_id']; $client = Client::find($client_id); // $client= (object)array(); // $client->id = $clientF->id; // $client->name = $clientF->name; // $client->nit = $clientF->nit; // $client->public_id = $clientF->public_id; //if($input['nit']!=$client->nit || $input['name']!=$client->name){ // $client->nit = $input['nit']; // $client->name = $input['name']; // $client->save(); //} DB::table('clients')->where('id', $client->id)->update(array('nit' => $input['nit'], 'business_name' => $input['name'])); // $user_id = Auth::user()->getAuthIdentifier(); // $user = DB::table('users')->select('account_id','branch_id','public_id')->where('id',$user_id)->first(); $account = DB::table('accounts')->where('id', Auth::user()->account_id)->first(); // //$account_id = $user->account_id; // // $account = DB::table('accounts')->select('num_auto','llave_dosi','fecha_limite')->where('id',$user->account_id)->first(); // //$branch = DB::table('branches')->select('num_auto','llave_dosi','fecha_limite','address1','address2','country_id','industry_id')->where('id',$user['branch_id'])->first(); // //$branch = DB::table('branches')->select('num_auto','llave_dosi','fecha_limite','address1','address2','country_id','industry_id')->where('id','=',$user->branch_id)->first(); // // $branch = DB::table('branches')->select('number_autho','key_dosage','deadline','address1','address2','country_id','industry_id','law','activity_pri','activity_sec1','name')->where('id','=',$user->branch_id)->first(); $branch = DB::table('branches')->where('id', '=', $branch_id)->first(); // $invoice_design = DB::table('invoice_designs')->select('id') // ->where('account_id','=',$account_id) // // ->where('branch_id','=',$branch->public_id) // // ->where('user_id','=',$user->public_id) // ->first(); // return Response::json($invoice_design); $items = $input['invoice_items']; // $linea =""; $amount = 0; $subtotal = 0; $fiscal = 0; $icetotal = 0; $bonidesc = 0; $productos = array(); foreach ($items as $item) { # code... $product_id = $item['id']; $pr = DB::table('products')->where('products.id', "=", $product_id)->first(); $amount = $amount + $item['amount']; $productos = $pr; } // $fiscal = $amount -$bonidesc-$icetotal; $balance = $amount; $subtotal = $amount; // /////////////////////////hasta qui esta bien al parecer hacer prueba de que fuciona el join de los productos XD $invoice_dateCC = date("Ymd"); $invoice_date = date("Y-m-d"); $invoice_date_limitCC = date("Y-m-d", strtotime($branch->deadline)); require_once app_path() . '/includes/control_code.php'; $cod_control = codigoControl($invoice_number, $client->nit, $invoice_dateCC, $amount, $branch->number_autho, $branch->key_dosage); // $ice = DB::table('tax_rates')->select('rate')->where('name','=','ice')->first(); // // // // creando invoice $invoice = Invoice::createNew(); $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 = $branch_id; $invoice->importe_neto = $subtotal; $invoice->debito_fiscal = $subtotal; // $invoice->invoice_design_id = $invoice_design->id; //------------- hasta aqui funciona despues sale error $invoice->law = $branch->law; // $invoice->=$balance; $invoice->importe_total = number_format((double) $amount, 2, '.', ''); $invoice->debito_fiscal = number_format((double) $amount, 2, '.', ''); $invoice->balance = number_format((double) $amount, 2, '.', ''); $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 = $branch->name; $invoice->address1 = $branch->address1; $invoice->address2 = $branch->address2; $invoice->number_autho = $branch->number_autho; // $invoice->work_phone=$branch->postal_code; $invoice->city = $branch->city; $invoice->state = $branch->state; // // $invoice->industry_id=$branch->industry_id; // $invoice->country_id= $branch->country_id; $invoice->key_dosage = $branch->key_dosage; $invoice->deadline = $branch->deadline; $invoice->account_name = $account->name; $invoice->account_nit = $account->nit; $invoice->client_name = $input['name']; $invoice->client_nit = $input['nit']; $invoice->branch_name = $branch->name; $invoice->phone = $branch->work_phone; $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; //$type_document =TypeDocument::where('account_id',Auth::user()->account_id)->firstOrFail(); //$invoice->javascript=$type_document->javascript_pos;; $invoice->sfc = $branch->sfc; $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; if ($account->is_uniper) { $invoice->account_uniper = $account->uniper; } // $invoice->logo = $type_document->logo; $invoice->save(); $cliente = Client::find($invoice->client_id); $cliente->balance = $cliente->balance + $invoice->balance; $cliente->save(); //guardadndo los invoice items foreach ($items as $item) { $product_id = $item['id']; $product = DB::table('products')->where('products.id', "=", $product_id)->first(); $invoiceItem = InvoiceItem::createNew(); $invoiceItem->invoice_id = $invoice->id; $invoiceItem->product_id = $product_id; $invoiceItem->product_key = $product->product_key; $invoiceItem->notes = $product->notes; $invoiceItem->cost = $item['amount']; $invoiceItem->qty = 1; $invoiceItem->save(); } $invoiceItems = DB::table('invoice_items')->select('notes', 'cost', 'qty')->where('invoice_id', '=', $invoice->id)->get(array('notes', 'cost', 'qty')); $date = new DateTime($invoice->deadline); $dateEmision = new DateTime($invoice->invoice_date); $cuenta = array('name' => $account->name, 'nit' => $account->nit); $client->name = $input['name']; $client->business_name = $input['name']; $client->nit = $input['nit']; $factura = array('invoice_number' => $invoice->invoice_number, 'control_code' => $invoice->control_code, 'invoice_date' => $dateEmision->format('d/m/Y'), 'activity_pri' => $branch->economic_activity, 'amount' => number_format((double) $invoice->importe_total, 2, '.', ''), 'subtotal' => number_format((double) $invoice->importe_neto, 2, '.', ''), 'fiscal' => number_format((double) $invoice->debito_fiscal, 2, '.', ''), 'client' => $client, 'account' => $account, 'law' => $invoice->law, 'invoice_items' => $invoiceItems, 'address1' => str_replace('+', '°', $invoice->address1), 'address2' => $invoice->address2, 'num_auto' => $invoice->number_autho, 'fecha_limite' => $date->format('d-m-Y')); // $factura = array('invoice_number' => $invoice->invoice_number, // 'control_code'=>$invoice->control_code, // 'activity_pri' => $branch->activity_pri, // 'invoice_date'=>$dateEmision->format('d/m/Y'), // 'amount'=>number_format((float)$invoice->amount, 2, '.', ''), // 'subtotal'=>number_format((float)$invoice->subtotal, 2, '.', ''), // 'fiscal'=>number_format((float)$invoice->fiscal, 2, '.', ''), // 'client'=>$client, // // 'id'=>$invoice->id, // 'account'=>$account, // 'law' => $invoice->law, // 'invoice_items'=>$invoiceItems, // 'address1'=>str_replace('+', '°', $invoice->address1), // // 'address2'=>str_replace('+', '°', $invoice->address2), // 'address2'=>$invoice->address2, // 'num_auto'=>$invoice->number_autho, // 'fecha_limite'=>$date->format('d/m/Y') // // 'fecha_emsion'=>, // // 'ice'=>number_format((float)$ice, 2, '.', '') // ); return Response::json($factura); }
public function export() { $fecha = explode(" ", Input::get('date')); $vec = ["Ene" => '01', "Feb" => '02', "Mar" => '03', "Abr" => '04', "May" => '05', "Jun" => '06', "Jul" => '07', "Ago" => '08', "Sep" => '09', "Oct" => '10', "Nov" => '11', "Dic" => '12']; $date = $fecha[1] . "-" . $vec[substr($fecha[0], 0, 3)]; $output = fopen('php://output', 'w') or Utils::fatalError(); header('Content-Type:application/txt'); header('Content-Disposition:attachment;filename=export.txt'); $invoices = Invoice::select('client_nit', 'client_name', 'invoice_number', 'account_nit', 'invoice_date', 'importe_total', 'number_autho', 'importe_ice', 'importe_exento', 'importe_neto', 'debito_fiscal', 'invoice_status_id', 'control_code', 'discount')->where('account_id', Auth::user()->account_id)->where("invoice_number", "!=", "")->where("invoice_number", "!=", 0)->where('invoice_date', 'LIKE', $date . '%')->get(); $p = "|"; $sw = true; $num = 1; foreach ($invoices as $i) { $sw = false; if ($i->invoice_status_id == 6) { $status = "A"; } else { $status = "V"; } // $datos = $i->invoice_number.$i->client_nit.$p.$i->client_name.$p.$p.$i->account_nit.$p.$i->invoice_date.$p.$i->importe_total.$p.$i->importe_ice.$p.$i->importe_exento.$p.$i->importe_neto.$p.$i->debito_fiscal.$p.$status.$p.$i->control_code."\r\n"; $fecha = date("d/m/Y", strtotime($i->invoice_date)); $fecha = \DateTime::createFromFormat('Y-m-d', $i->invoice_date); if ($fecha == null) { $fecha = $i->invoice_date; } else { $fecha = $fecha->format('d/m/Y'); } $doc = TypeDocumentBranch::where('branch_id', $i->branch_id)->orderBy('id', 'desc')->first(); $tipo = TypeDocument::where('id', $doc->type_document_id)->first(); if ($tipo->master_id = 1 || ($tipo->master_id = 4)) { $debito = $i->importe_neto * 0.13; $debito = number_format((double) $debito, 2, '.', ''); $datos = "3" . $p . $num . $p . $fecha . $p . $i->invoice_number . $p . $i->number_autho . $p . $status . $p . $i->client_nit . $p . $i->client_name . $p . $i->importe_total . $p . $i->importe_ice . $p . $i->importe_exento . $p . "0.00" . $p . $i->importe_total . $p . $i->discount . $p . $i->importe_neto . $p . $debito . $p . $i->control_code . "\r\n"; } if ($tipo->master_id = 3) { $datos = "3" . $p . $num . $p . $fecha . $p . $i->invoice_number . $p . $i->number_autho . $p . $status . $p . $i->client_nit . $p . $i->client_name . $p . $i->importe_total . $p . $i->importe_ice . $p . $i->importe_exento . $p . $i->importe_neto . $p . $i->importe_total . $p . $i->discount . $p . "0.00" . $p . "0.00" . $p . $i->control_code . "\r\n"; } $num++; fputs($output, $datos); } if ($sw) { fputs($output, "No se encontraron ventas en este periodo: " . Input::get('date')); } fclose($output); exit; }