public function fire()
 {
     $this->info(date('Y-m-d') . ' Running 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) . ' recurring invoice(s) found');
     foreach ($invoices as $recurInvoice) {
         if ($recurInvoice->client->deleted_at) {
             continue;
         }
         date_default_timezone_set($recurInvoice->account->getTimezone());
         $this->info('Processing Invoice ' . $recurInvoice->id . ' - Should send ' . ($recurInvoice->shouldSendToday() ? 'YES' : 'NO'));
         if (!$recurInvoice->shouldSendToday()) {
             continue;
         }
         $invoice = Invoice::createNew($recurInvoice);
         $invoice->client_id = $recurInvoice->client_id;
         $invoice->recurring_invoice_id = $recurInvoice->id;
         $invoice->invoice_number = 'R' . $recurInvoice->account->getNextInvoiceNumber();
         $invoice->amount = $recurInvoice->amount;
         $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;
         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');
 }
Exemplo n.º 2
0
 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();
         }
     }
 }
Exemplo n.º 3
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);
 }
Exemplo n.º 4
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');
 }
Exemplo n.º 5
0
 public function guardarFacturaOffline()
 {
     /* David 
         	 Guardando  factura con el siguiente formato:
         	
     			//Formato Offline
     			//nuevo formato para la cascada XD
     			{"invoice_items":[{"qty":"2","id":"2","boni":"1","desc":"3"}],"client_id":"1","nit":"6047054","name":"torrez","public_id":"1","invoice_date":"14-10-2014","invoice_number":"0001","cod_control"}
     
         	*/
     $input = Input::all();
     // $invoice_number = Auth::user()->account->getNextInvoiceNumber();
     $invoice_number = (int) Auth::user()->branch->getNextInvoiceNumber();
     $numero = (int) $input['invoice_number'];
     // $numero =(int)  $input['invoice_number'];
     if ($invoice_number != $numero) {
         return Response::json(array('resultado' => '1', 'invoice_number' => $invoice_number));
     }
     $client_id = $input['client_id'];
     // $client = DB::table('clients')->select('id','nit','name','public_id')->where('id',$input['client_id'])->first();
     $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'];
     // $linea ="";
     $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 = $amount - $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, $input['nit'], $invoice_dateCC, $amount, $branch->number_autho, $branch->key_dosage);
     $cod_control = $input['cod_control'];
     $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 = $input['nit'];
     $invoice->name = $input['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|' . $input['invoice_number'] . '|' . $invoice->number_autho . '|' . $invoice_date . '|' . $amount . '|' . $fiscal . '|' . $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 . '_' . $input['invoice_number'] . '.jpg');
     $invoice->save();
     $fecha = $input['fecha'];
     $f = date("Y-m-d", strtotime($fecha));
     DB::table('invoices')->where('id', $invoice->id)->update(array('branch' => $branch->name, 'invoice_date' => $f));
     //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((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, '.', '')
     // 				);
     // $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('resultado ' => "0");
     //colocar una excepcion en caso de error
     // return Response::json($datos);
     return Response::json($datos);
 }
Exemplo n.º 6
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);
 }
Exemplo n.º 7
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');
 }
Exemplo n.º 8
0
 public function fire()
 {
     $this->info(date('Y-m-d') . ' Running CreateRandomData...');
     $user = User::first();
     if (!$user) {
         $this->error("Error: please create user account by logging in");
         return;
     }
     $productNames = ['Arkansas', 'New York', 'Arizona', 'California', 'Colorado', 'Alabama', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'Alaska', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'];
     $clientNames = ['IBM', 'Nestle', 'Mitsubishi UFJ Financial', 'Vodafone', 'Eni', 'Procter & Gamble', 'Johnson & Johnson', 'American International Group', 'Banco Santander', 'BHP Billiton', 'Pfizer', 'Itaú Unibanco Holding', 'Ford Motor', 'BMW Group', 'Commonwealth Bank', 'EDF', 'Statoil', 'Google', 'Siemens', 'Novartis', 'Royal Bank of Canada', 'Sumitomo Mitsui Financial', 'Comcast', 'Sberbank', 'Goldman Sachs Group', 'Westpac Banking Group', 'Nippon Telegraph & Tel', 'Ping An Insurance Group', 'Banco Bradesco', 'Anheuser-Busch InBev', 'Bank of Communications', 'China Life Insurance', 'General Motors', 'Telefónica', 'MetLife', 'Honda Motor', 'Enel', 'BASF', 'Softbank', 'National Australia Bank', 'ANZ', 'ConocoPhillips', 'TD Bank Group', 'Intel', 'UBS', 'Hewlett-Packard', 'Coca-Cola', 'Cisco Systems', 'UnitedHealth Group', 'Boeing', 'Zurich Insurance Group', 'Hyundai Motor', 'Sanofi', 'Credit Agricole', 'United Technologies', 'Roche Holding', 'Munich Re', 'PepsiCo', 'Oracle', 'Bank of Nova Scotia'];
     foreach ($productNames as $i => $value) {
         $product = Product::createNew($user);
         $product->id = $i + 1;
         $product->product_key = $value;
         $product->save();
     }
     foreach ($clientNames as $i => $value) {
         $client = Client::createNew($user);
         $client->name = $value;
         $client->save();
         $contact = Contact::createNew($user);
         $contact->email = "*****@*****.**";
         $contact->is_primary = 1;
         $client->contacts()->save($contact);
         $numInvoices = rand(1, 25);
         if ($numInvoices == 4 || $numInvoices == 10 || $numInvoices == 25) {
             // leave these
         } else {
             if ($numInvoices % 3 == 0) {
                 $numInvoices = 1;
             } else {
                 if ($numInvoices > 10) {
                     $numInvoices = $numInvoices % 2;
                 }
             }
         }
         $paidUp = rand(0, 1) == 1;
         for ($j = 1; $j <= $numInvoices; $j++) {
             $price = rand(10, 1000);
             if ($price < 900) {
                 $price = rand(10, 150);
             }
             $invoice = Invoice::createNew($user);
             $invoice->invoice_number = $user->account->getNextInvoiceNumber();
             $invoice->amount = $invoice->balance = $price;
             $invoice->created_at = date('Y-m-d', strtotime(date("Y-m-d") . ' - ' . rand(1, 100) . ' days'));
             $client->invoices()->save($invoice);
             $productId = rand(0, 40);
             if ($productId > 20) {
                 $productId = $productId % 2 + rand(0, 2);
             }
             $invoiceItem = InvoiceItem::createNew($user);
             $invoiceItem->product_id = $productId + 1;
             $invoiceItem->product_key = $productNames[$invoiceItem->product_id];
             $invoiceItem->cost = $invoice->amount;
             $invoiceItem->qty = 1;
             $invoice->invoice_items()->save($invoiceItem);
             if ($paidUp || rand(0, 2) > 1) {
                 $payment = Payment::createNew($user);
                 $payment->invoice_id = $invoice->id;
                 $payment->amount = $invoice->amount;
                 $client->payments()->save($payment);
             }
         }
     }
 }
Exemplo n.º 9
0
 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);
     //}
 }
Exemplo n.º 10
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);
 }