예제 #1
0
 private function importFilei()
 {
     $data = Session::get('data');
     $map = Input::get('map');
     $count = 0;
     $hasHeaders = true;
     foreach ($data as $row) {
         if ($hasHeaders) {
             $hasHeaders = false;
             continue;
         }
         foreach ($row as $index => $value) {
             $field = $map[$index];
             $value = trim($value);
             if ($field == Invoice::$fieldCodClient) {
                 $clients = Client::scope()->get();
                 $flag = 1;
                 foreach ($clients as $client) {
                     $cod_client = intval($value);
                     if ($client->public_id == $cod_client) {
                         $flag = 1;
                     }
                 }
                 if ($flag == 0) {
                     $message = 'cliente no encontrado ' . $value . 'Favor revisar el archivo importado ';
                     Session::flash('message', $message);
                     return Redirect::to('company/import_export');
                 }
             } else {
                 if ($field == Invoice::$fieldProduct) {
                     if ($value == '') {
                         $message = 'Concepto vacío. Favor revisar el archivo importado ';
                         Session::flash('message', $message);
                         return Redirect::to('company/import_export');
                     }
                 } else {
                     if ($field == Invoice::$fieldAmount) {
                         if ($value == '') {
                             $message = 'Monto vacío. Favor revisar el archivo importado ';
                             Session::flash('message', $message);
                             return Redirect::to('company/import_export');
                         }
                     }
                 }
             }
         }
     }
     $branch_id = Input::get('branch_id');
     $clients = Client::scope()->get();
     $clientMap = [];
     $data = Session::get('data');
     Session::forget('data');
     $hasHeaders = true;
     foreach ($clients as $client) {
         $i = 0;
         $clientMap[$client->public_id][$i] = $client->id;
         $clientMap[$client->public_id][$i + 1] = $client->nit;
         $clientMap[$client->public_id][$i + 2] = $client->name;
     }
     foreach ($data as $row) {
         if ($hasHeaders) {
             $hasHeaders = false;
             continue;
         }
         $invoice = Invoice::createNew();
         $invoiceItem = InvoiceItem::createNew();
         $count++;
         $branch = \DB::table('branches')->where('id', $branch_id)->first();
         $invoice->branch_id = $branch_id;
         $invoiceNumber = $branch->invoice_number_counter;
         $invoice->invoice_number = $invoiceNumber;
         $today = new DateTime('now');
         $invoice->invoice_date = $today->format('Y-m-d');
         $invoiceDesign = \DB::table('invoice_designs')->where('account_id', \Auth::user()->account_id)->orderBy('public_id', 'desc')->first();
         $invoice->invoice_design_id = $invoiceDesign->id;
         $account = \Auth::user()->account;
         $invoice->account_name = $account->name;
         $invoice->account_nit = $account->nit;
         $invoice->branch_name = $branch->name;
         $invoice->address1 = $branch->address1;
         $invoice->address2 = $branch->address2;
         $invoice->phone = $branch->postal_code;
         $invoice->city = $branch->city;
         $invoice->state = $branch->state;
         $invoice->number_autho = $branch->number_autho;
         $invoice->deadline = $branch->deadline;
         $invoice->key_dosage = $branch->key_dosage;
         $invoice->activity_pri = $branch->activity_pri;
         $invoice->activity_sec1 = $branch->activity_sec1;
         $invoice->law = $branch->law;
         foreach ($row as $index => $value) {
             $field = $map[$index];
             $value = trim($value);
             if ($field == Invoice::$fieldCodClient) {
                 $cod_client = intval($value);
                 $invoice->client_id = $clientMap[$cod_client][0];
                 $invoice->client_nit = $clientMap[$cod_client][1];
                 $invoice->client_name = $clientMap[$cod_client][2];
             } else {
                 if ($field == Invoice::$fieldProduct) {
                     $notes = $value;
                 } else {
                     if ($field == Invoice::$fieldAmount) {
                         $invoiceItem->notes = wordwrap($notes, 70, "\n");
                         $str = str_replace(".", "", $value);
                         $amount = str_replace(",", ".", $str);
                         $invoiceItem->cost = $amount;
                         $invoiceItem->qty = 1;
                         $invoiceItem->tax_rate = 0;
                         $invoice->subtotal = $invoiceItem->cost;
                         $invoice->amount = $invoiceItem->cost;
                         $invoice->balance = $invoiceItem->cost;
                     }
                 }
             }
         }
         $invoice_dateCC = date("Ymd", strtotime($invoice->invoice_date));
         $invoice_date_limitCC = date("d/m/Y", strtotime($branch->deadline));
         require_once app_path() . '/includes/control_code.php';
         $cod_control = codigoControl($invoice->invoice_number, $invoice->client_nit, $invoice_dateCC, $invoice->amount, $branch->number_autho, $branch->key_dosage);
         $invoice->control_code = $cod_control;
         $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();
         $invoice->invoice_items()->save($invoiceItem);
         // Activity::createInvoice($invoice, false);
     }
     $message = Utils::pluralize('created_invoicei', $count);
     Session::flash('message', $message);
     return Redirect::to('invoices');
 }
예제 #2
0
 public function guardarFactura()
 {
     /* David 
         	 Guardando  factura con el siguiente formato:
         	
     			{"invoice_items":[{"qty":"2","id":"2"}],"client_id":"1"}
     			//nuevo formato para la cascada XD
     			{"invoice_items":[{"qty":"2","id":"2","boni":"1","desc":"3"}],"client_id":"1"}
     			//para version generica
     
     			{"invoice_items":[{"qty":"6","id":"11"}],"name":"Sin Nombre","nit":"0","client_id":"19"}
     
         	*/
     $input = Input::all();
     // $invoice_number = Auth::user()->account->getNextInvoiceNumber();
     $invoice_number = Auth::user()->branch->getNextInvoiceNumber();
     $client_id = $input['client_id'];
     $clientF = Client::scope($client_id)->firstOrFail();
     $client = (object) array();
     $client->id = $clientF->id;
     $client->name = $clientF->name;
     $client->nit = $clientF->nit;
     $client->public_id = $clientF->public_id;
     DB::table('clients')->where('id', $client->id)->update(array('nit' => $input['nit'], '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', $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', '=', $user->branch_id)->first();
     $invoice_design = DB::table('invoice_designs')->select('id')->where('account_id', '=', $user->account_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();
         // $pr->xd ='hola';
         //me quede aqui me llego sueñito XD
         $amount = $amount + $pr->cost * $item['qty'];
         // $pr->qty = 1;
         $productos = $pr;
         // $pr = DB::table('products')->select('cost')->where('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'];
         // $bonidesc= $bonidesc +$bd;
         // $amount = $amount +$st-$bd;
         //   			// $fiscal = $fiscal +$amount;
     }
     //   	$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 = $user_id;
     $invoice->account_id = $user->account_id;
     $invoice->branch_id = $user->branch_id;
     $invoice->amount = number_format((double) $amount, 2, '.', '');
     $invoice->invoice_design_id = $invoice_design->id;
     //------------- hasta aqui funciona despues sale error
     $invoice->law = $branch->law;
     $invoice->balance = $balance;
     $invoice->subtotal = $subtotal;
     $invoice->control_code = $cod_control;
     $invoice->start_date = $invoice_date;
     $invoice->invoice_date = $invoice_date;
     $invoice->activity_pri = $branch->activity_pri;
     $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->custom_value1 =$icetotal;
     //     $invoice->ice = $ice->rate;
     //     //cliente
     //     $invoice->nit=$client->nit;
     //     $invoice->name =$client->name;
     //adicionales de la nueva plataforma
     $invoice->account_name = $account->name;
     $invoice->account_nit = $account->nit;
     $invoice->client_name = $input['name'];
     $invoice->client_nit = $input['nit'];
     $invoice->phone = $branch->postal_code;
     $invoice->save();
     //     $account = Auth::user()->account;
     // 	$ice = $invoice->amount-$invoice->fiscal;
     // 	$desc = $invoice->subtotal-$invoice->amount;
     // 	$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;
     // 	}
     require_once app_path() . '/includes/BarcodeQR.php';
     $icef = 0;
     $descf = 0;
     $qr = new BarcodeQR();
     $datosqr = $invoice->account_nit . '|' . $invoice->invoice_number . '|' . $invoice->number_autho . '|' . $invoice_date . '|' . $invoice->amount . '|' . $invoice->amount . '|' . $invoice->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();
     DB::table('invoices')->where('id', $invoice->id)->update(array('branch_name' => $branch->name));
     //error verificar
     // $invoice = DB::table('invoices')->select('id')->where('invoice_number',$invoice_number)->first();
     //guardadndo los invoice items
     foreach ($items as $item) {
         // $product = DB::table('products')->select('notes')->where('id',$product_id)->first();
         $product_id = $item['id'];
         $product = DB::table('products')->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;
         $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 = $product->cost;
         $invoiceItem->qty = $item['qty'];
         // $invoiceItem->line_total=$line_total;
         $invoiceItem->tax_rate = 0;
         $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);
     // $ice = $invoice->amount-$invoice->fiscal;
     // $factura  = array('invoice_number' => $invoice->invoice_number,
     //   					'control_code'=>$invoice->control_code,
     //   					'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, '.', '')
     //   					);
     $client->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->activity_pri, 'amount' => number_format((double) $invoice->amount, 2, '.', ''), 'subtotal' => number_format((double) $invoice->balance, 2, '.', ''), 'fiscal' => number_format((double) $invoice->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'));
     // $invoic = Invoice::scope($invoice_number)->withTrashed()->with('client.contacts', 'client.country', 'invoice_items')->firstOrFail();
     // $d  = Input::all();
     //en caso de problemas irracionales me refiero a que se jodio
     // $input = Input::all();
     // $client_id = $input['client_id'];
     // $client = DB::table('clients')->select('id','nit','name')->where('id',$input['client_id'])->first();
     return Response::json($factura);
 }
예제 #3
0
 public function save($publicId, $data, $entityType)
 {
     if ($publicId) {
         $invoice = Invoice::scope($publicId)->firstOrFail();
     } else {
         $invoice = Invoice::createNew();
         if ($entityType == ENTITY_QUOTE) {
             $invoice->is_quote = true;
         }
         $invoice->client_id = $data['client_id'];
         $invoice->discount = Utils::parseFloat($data['discount']);
         $discount_item = Utils::parseFloat($data['discount_item']);
         $invoiceNumber = \Auth::user()->branch->getNextInvoiceNumber();
         if (!$invoice->is_recurring) {
             $invoice->invoice_number = $invoiceNumber;
         } else {
             $invoice->invoice_number = " ";
         }
         $invoice->is_recurring = $data['is_recurring'] && !Utils::isDemo() ? true : false;
         $invoice->invoice_date = Utils::toSqlDate($data['invoice_date']);
         if ($invoice->is_recurring) {
             $invoice->frequency_id = $data['frequency_id'] ? $data['frequency_id'] : 0;
             $invoice->start_date = Utils::toSqlDate($data['start_date']);
             $invoice->end_date = Utils::toSqlDate($data['end_date']);
             $invoice->due_date = null;
         } else {
             if ($data['action'] == 'save') {
                 $date = date("Y-m-d", strtotime(date("Y-m-d") . " +1 month"));
                 $invoice->due_date = $date;
             } else {
                 $invoice->due_date = Utils::toSqlDate($data['due_date']);
             }
             $invoice->frequency_id = 0;
             $invoice->start_date = null;
             $invoice->end_date = null;
         }
         $invoice->terms = trim($data['terms']);
         $invoice->public_notes = trim($data['public_notes']);
         $invoice->po_number = trim($data['po_number']);
         $invoiceDesign = \DB::table('invoice_designs')->where('account_id', \Auth::user()->account_id)->orderBy('public_id', 'desc')->first();
         $invoice->invoice_design_id = $invoiceDesign->id;
         if (isset($data['tax_name']) && isset($data['tax_rate']) && Utils::parseFloat($data['tax_rate']) > 0) {
             $invoice->tax_rate = Utils::parseFloat($data['tax_rate']);
             $invoice->tax_name = trim($data['tax_name']);
         } else {
             $invoice->tax_rate = 0;
             $invoice->tax_name = '';
         }
         $total = 0;
         foreach ($data['invoice_items'] as $item) {
             if (!$item->cost && !$item->product_key && !$item->notes) {
                 continue;
             }
             $invoiceItemCost = Utils::parseFloat($item->cost);
             $invoiceItemQty = Utils::parseFloat($item->qty);
             $invoiceItemTaxRate = 0;
             if (isset($item->tax_rate) && Utils::parseFloat($item->tax_rate) > 0) {
                 $invoiceItemTaxRate = Utils::parseFloat($item->tax_rate);
             }
             $lineTotal = $invoiceItemCost * $invoiceItemQty;
             $total += round($lineTotal + $lineTotal * $invoiceItemTaxRate / 100, 2);
         }
         $invoice->subtotal = $total;
         if ($invoice->discount > 0) {
             $total *= (100 - $invoice->discount) / 100;
         }
         if ($discount_item > 0) {
             $total -= $discount_item;
         }
         $invoice->custom_value1 = round($data['custom_value1'], 2);
         $invoice->custom_value2 = round($data['custom_value2'], 2);
         $invoice->custom_taxes1 = $data['custom_taxes1'] ? true : false;
         $invoice->custom_taxes2 = $data['custom_taxes2'] ? true : false;
         // custom fields charged taxes
         if ($invoice->custom_value1 && $invoice->custom_taxes1) {
             $total += $invoice->custom_value1;
         }
         if ($invoice->custom_value2 && $invoice->custom_taxes2) {
             $total += $invoice->custom_value2;
         }
         $total += $total * $invoice->tax_rate / 100;
         $total = round($total, 2);
         // custom fields not charged taxes
         if ($invoice->custom_value1) {
             $total += $invoice->custom_value1;
         }
         if ($invoice->custom_value2 && !$invoice->custom_taxes2) {
             $total += $invoice->custom_value2;
         }
         if ($publicId) {
             $invoice->balance = $total - ($invoice->amount - $invoice->balance);
         } else {
             $invoice->balance = $total;
         }
         $invoice->amount = $total;
         $account = \Auth::user()->account;
         $branch = \Auth::user()->branch;
         $invoice->account_name = $account->name;
         $invoice->account_nit = $account->nit;
         $invoice->branch_name = $branch->name;
         $invoice->address1 = $branch->address1;
         $invoice->address2 = $branch->address2;
         $invoice->phone = $branch->postal_code;
         $invoice->city = $branch->city;
         $invoice->state = $branch->state;
         $invoice->number_autho = $branch->number_autho;
         $invoice->deadline = $branch->deadline;
         $invoice->key_dosage = $branch->key_dosage;
         $invoice->client_nit = $data['client_nit'];
         $invoice->client_name = $data['client_name'];
         $invoice->activity_pri = $branch->activity_pri;
         $invoice->activity_sec1 = $branch->activity_sec1;
         $invoice->law = $branch->law;
         $invoice->aux1 = $branch->aux2;
         $invoice->third = $branch->third;
         $invoice_dateCC = date("Ymd", strtotime($invoice->invoice_date));
         $invoice_date_limitCC = date("d/m/Y", strtotime($branch->deadline));
         require_once app_path() . '/includes/control_code.php';
         $cod_control = codigoControl($invoice->invoice_number, $invoice->client_nit, $invoice_dateCC, $invoice->amount, $branch->number_autho, $branch->key_dosage);
         $invoice->control_code = $cod_control;
         $invoice->save();
         $invoice->invoice_items()->forceDelete();
         foreach ($data['invoice_items'] as $item) {
             if (!$item->cost && !$item->product_key && !$item->notes) {
                 continue;
             }
             if ($item->product_key) {
                 $product = Product::findProductByKey(trim($item->product_key));
                 if (!$product) {
                     $product = Product::createNew();
                     $product->product_key = strtoupper(trim($item->product_key));
                 }
                 if (\Auth::user()->account->update_products) {
                     $categories = \DB::table('categories')->where('account_id', \Auth::user()->account_id)->orderBy('public_id', 'desc')->first();
                     $product->category_id = $categories->id;
                     $product->notes = $item->notes;
                     $product->cost = $item->cost;
                     //$product->qty = $item->qty;
                 }
                 $product->save();
             }
             $invoiceItem = InvoiceItem::createNew();
             $invoiceItem->product_id = isset($product) ? $product->id : null;
             $invoiceItem->product_key = trim($invoice->is_recurring ? $item->product_key : Utils::processVariables($item->product_key));
             $invoiceItem->notes = trim($invoice->is_recurring ? $item->notes : Utils::processVariables($item->notes));
             $invoiceItem->cost = Utils::parseFloat($item->cost);
             $invoiceItem->qty = Utils::parseFloat($item->qty);
             $invoiceItem->tax_rate = 0;
             if (isset($item->tax_rate) && Utils::parseFloat($item->tax_rate) > 0) {
                 $invoiceItem->tax_rate = Utils::parseFloat($item->tax_rate);
                 $invoiceItem->tax_name = trim($item->tax_name);
             }
             $invoice->invoice_items()->save($invoiceItem);
         }
         if ($data['set_default_terms']) {
             $account = \Auth::user()->account;
             $account->invoice_terms = $invoice->terms;
             $account->save();
         }
     }
     return $invoice;
 }
예제 #4
0
 public function guardarFactura()
 {
     /* David 
         	 Guardando  factura con el siguiente formato:
         	
     			{"invoice_items":[{"qty":"2","id":"2"}],"client_id":"1"}
     			//nuevo formato para la cascada XD
     			{"invoice_items":[{"qty":"2","id":"2","boni":"1","desc":"3"}],"client_id":"1"}
     
         	*/
     $input = Input::all();
     // $invoice_number = Auth::user()->account->getNextInvoiceNumber();
     $invoice_number = Auth::user()->branch->getNextInvoiceNumber();
     $client_id = $input['client_id'];
     $client = DB::table('clients')->select('id', 'nit', 'name', 'public_id')->where('id', $input['client_id'])->first();
     //update for response change client  nit and name for the new entry
     //$swap = false ;
     if (strcmp($input['nit'], $client->nit) xor strcmp($input['name'], $client->name)) {
         DB::table('clients')->where('id', $input['client_id'])->update(array('nit' => $input['nit'], 'name' => $input['name']));
         //if change nit an name  update client
         $client = DB::table('clients')->select('id', 'nit', 'name', 'public_id')->where('id', $input['client_id'])->first();
         //		$swap = true;
     }
     $user_id = Auth::user()->getAuthIdentifier();
     $user = DB::table('users')->select('account_id', 'branch_id', 'price_type_id')->where('id', $user_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', '=', $user->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();
         // $pr = DB::table('products')->select('cost')->where('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'];
         $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;
             $icetotal = $icetotal + ($iceBruto - $iceNeto);
             // $fiscal = $fiscal + ($amount - ($item['qty'] *($pr->cc/1000)*$ice->rate) );
         }
         // $fiscal = $fiscal +$amount;
     }
     //$fiscal = $amount -$bonidesc-$icetotal;
     $fiscal = $subtotal - $bonidesc - $icetotal;
     $balance = $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 = $user_id;
     $invoice->account_id = $user->account_id;
     $invoice->branch_id = $user->branch_id;
     $invoice->amount = number_format((double) $amount, 2, '.', '');
     $invoice->subtotal = $subtotal;
     $invoice->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->activity_pri = $branch->activity_pri;
     $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->custom_value1 = $icetotal;
     $invoice->ice = $ice->rate;
     //cliente
     $invoice->nit = $client->nit;
     $invoice->name = $client->name;
     $invoice->save();
     $account = Auth::user()->account;
     require_once app_path() . '/includes/BarcodeQR.php';
     $ice = $invoice->amount - $invoice->fiscal;
     $desc = $invoice->subtotal - $invoice->amount;
     $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;
     }
     $qr = new BarcodeQR();
     $datosqr = '1006909025|' . $invoice->invoice_number . '|' . $invoice->number_autho . '|' . $invoice_date . '|' . $amount . '|' . $fiscal . '|' . $invoice->control_code . '|' . $invoice->nit . '|' . $icef . '|0|0|' . $descf;
     $qr->text($datosqr);
     $qr->draw(150, 'qr/' . $account->account_key . '_' . $invoice->invoice_number . '.png');
     $input_file = 'qr/' . $account->account_key . '_' . $invoice->invoice_number . '.png';
     $output_file = 'qr/' . $account->account_key . '_' . $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 . '_' . $invoice->invoice_number . '.jpg');
     $invoice->save();
     DB::table('invoices')->where('id', $invoice->id)->update(array('branch' => $branch->name));
     //error verificar
     // $invoice = DB::table('invoices')->select('id')->where('invoice_number',$invoice_number)->first();
     //guardadndo los invoice items
     foreach ($items as $item) {
         // $product = DB::table('products')->select('notes')->where('id',$product_id)->first();
         $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;
         $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 = $cost;
         $invoiceItem->boni = (int) $item['boni'];
         $invoiceItem->desc = $item['desc'];
         $invoiceItem->qty = (int) $item['qty'];
         $invoiceItem->line_total = $line_total;
         $invoiceItem->tax_rate = 0;
         $invoiceItem->save();
     }
     $invoiceItems = DB::table('invoice_items')->select('notes', 'cost', 'qty', 'boni', 'desc')->where('invoice_id', '=', $invoice->id)->get(array('notes', 'cost', 'qty', 'boni', 'desc'));
     $date = new DateTime($invoice->deadline);
     $dateEmision = new DateTime($invoice->invoice_date);
     $account = array('name' => $branch->name, 'nit' => '1006909025');
     $ice = $invoice->amount - $invoice->fiscal;
     $factura = array('invoice_number' => $invoice->invoice_number, 'control_code' => $invoice->control_code, 'invoice_date' => $dateEmision->format('d-m-Y'), 'amount' => number_format((double) $invoice->amount, 2, '.', ''), 'subtotal' => number_format((double) $invoice->subtotal, 2, '.', ''), 'fiscal' => number_format((double) $invoice->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'), 'ice' => number_format((double) $ice, 2, '.', ''));
     // $invoic = Invoice::scope($invoice_number)->withTrashed()->with('client.contacts', 'client.country', 'invoice_items')->firstOrFail();
     // $d  = Input::all();
     //en caso de problemas irracionales me refiero a que se jodio
     // $input = Input::all();
     // $client_id = $input['client_id'];
     // $client = DB::table('clients')->select('id','nit','name')->where('id',$input['client_id'])->first();
     // $datos = array('hola ' => $input);
     // return Response::json($datos);
     return Response::json($factura);
 }
예제 #5
0
 public function fire()
 {
     $this->info(date('Y-m-d') . ' Cargando SendRecurringInvoices...');
     $today = new DateTime();
     $invoices = Invoice::with('account.timezone', 'invoice_items', 'client')->whereRaw('is_deleted IS FALSE AND deleted_at IS NULL AND is_recurring IS TRUE AND start_date <= ? AND (end_date IS NULL OR end_date >= ?)', array($today, $today))->get();
     $this->info(count($invoices) . ' facturas recurrentes encontradas');
     foreach ($invoices as $recurInvoice) {
         if ($recurInvoice->client->deleted_at) {
             continue;
         }
         if (!$recurInvoice->user->confirmed) {
             continue;
         }
         date_default_timezone_set($recurInvoice->account->getTimezone());
         $this->info('Procesando la factura con el id ' . $recurInvoice->id . ($recurInvoice->shouldSendToday() ? ' si se enviará' : ' no se enviará'));
         // if (!$recurInvoice->shouldSendToday())
         // {
         // 	continue;
         // }
         $branch_id = $recurInvoice->branch_id;
         $branch = DB::table('branches')->where('id', $branch_id)->first();
         $account_id = $recurInvoice->account_id;
         $account = DB::table('accounts')->where('id', $account_id)->first();
         if (strtotime($branch->deadline) < strtotime('now')) {
             continue;
         }
         $invoice = Invoice::createNew($recurInvoice);
         $invoice->client_id = $recurInvoice->client_id;
         $invoice->recurring_invoice_id = $recurInvoice->id;
         $invoice->branch_id = $recurInvoice->branch_id;
         $invoiceNumber = $branch->invoice_number_counter;
         $invoice->invoice_number = $branch->invoice_number_counter;
         $invoice->amount = $recurInvoice->amount;
         $invoice->subtotal = $recurInvoice->subtotal;
         $invoice->balance = $recurInvoice->amount;
         $invoice->invoice_date = date_create()->format('Y-m-d');
         $invoice->discount = $recurInvoice->discount;
         $invoice->po_number = $recurInvoice->po_number;
         $invoice->public_notes = $recurInvoice->public_notes;
         $invoice->terms = $recurInvoice->terms;
         // $invoice->tax_name = $recurInvoice->tax_name;
         // $invoice->tax_rate = $recurInvoice->tax_rate;
         $invoice->invoice_design_id = $recurInvoice->invoice_design_id;
         $invoice->account_name = $recurInvoice->account_name;
         $invoice->account_nit = $recurInvoice->account_nit;
         $invoice->branch_name = $branch->name;
         $invoice->address1 = $branch->address1;
         $invoice->address2 = $branch->address2;
         $invoice->phone = $branch->postal_code;
         $invoice->city = $branch->city;
         $invoice->state = $branch->state;
         $invoice->number_autho = $branch->number_autho;
         $invoice->deadline = $branch->deadline;
         $invoice->key_dosage = $branch->key_dosage;
         $invoice->client_nit = $recurInvoice->client_nit;
         $invoice->client_name = $recurInvoice->client_name;
         $invoice->activity_pri = $branch->activity_pri;
         $invoice->activity_sec1 = $branch->activity_sec1;
         $invoice->law = $branch->law;
         $invoice_dateCC = date("Ymd", strtotime($invoice->invoice_date));
         $invoice_date_limitCC = date("d/m/Y", strtotime($branch->deadline));
         require_once app_path() . '/includes/control_code.php';
         $cod_control = codigoControl($invoice->invoice_number, $invoice->client_nit, $invoice_dateCC, $invoice->amount, $branch->number_autho, $branch->key_dosage);
         $invoice->control_code = $cod_control;
         $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, 'public/qr/' . $account->account_key . '_' . $branch->name . '_' . $invoice->invoice_number . '.png');
         $input_file = 'public/qr/' . $account->account_key . '_' . $branch->name . '_' . $invoice->invoice_number . '.png';
         $output_file = 'public/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');
         if ($invoice->client->payment_terms) {
             $invoice->due_date = date_create()->modify($invoice->client->payment_terms . ' day')->format('Y-m-d');
         }
         $invoice->save();
         foreach ($recurInvoice->invoice_items as $recurItem) {
             $item = InvoiceItem::createNew($recurItem);
             $item->product_id = $recurItem->product_id;
             $item->qty = $recurItem->qty;
             $item->cost = $recurItem->cost;
             $item->notes = Utils::processVariables($recurItem->notes);
             $item->product_key = Utils::processVariables($recurItem->product_key);
             $item->tax_name = $recurItem->tax_name;
             $item->tax_rate = $recurItem->tax_rate;
             $invoice->invoice_items()->save($item);
         }
         foreach ($recurInvoice->invitations as $recurInvitation) {
             $invitation = Invitation::createNew($recurInvitation);
             $invitation->contact_id = $recurInvitation->contact_id;
             $invitation->invitation_key = str_random(RANDOM_KEY_LENGTH);
             $invoice->invitations()->save($invitation);
         }
         $this->mailer->sendInvoice($invoice);
         $recurInvoice->last_sent_date = Carbon::now()->toDateTimeString();
         $recurInvoice->save();
     }
     $this->info('Done');
 }
예제 #6
0
 public static function getControlCode($invoice_number, $nit, $fecha, $total, $number_autho, $key_dosage)
 {
     require_once app_path() . '/includes/control_code.php';
     $codigo_de_control = codigoControl($invoice_number, $nit, $fecha, $total, $number_autho, $key_dosage);
     return $codigo_de_control;
 }
예제 #7
0
 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);
 }