public function run()
 {
     require_once "customer.php";
     foreach ($customer as $c) {
         $customer = Customer::create(['customer_name' => $c['CustomerName']]);
         if ($c['Contact'] != "") {
             $contact = CustomerContact::create(['first_name' => $c['Contact'], 'mobile' => $c['MobileNumber'], 'email' => $c['Email'], 'phone' => $c['TelephoneNumber']]);
             // todo: also deal with Contact2 info
             // todo: also get all records from existing customer_contacts tables
             $customer->customer_contacts()->save($contact);
         }
     }
 }
 public function post_send_customer_quote(\Illuminate\Http\Request $request, $qr_id)
 {
     $input = Request::all();
     $qr = QuoteRequest::Find($qr_id);
     $customer = $qr->customer;
     $qris = $qr->get_quote->qris;
     if ($input['term_id'] > 0) {
         $qr->terms_id = $input['term_id'];
     } else {
         $qr->terms_id = null;
     }
     $qr->save();
     $terms = $qr->terms;
     if ($input['submit'] == 'Create PDF') {
         // Creating PDF Quote
         $html = view('quotes.pdf', compact('qr', 'customer', 'qris', 'terms'));
         $dompdf = PDF::loadHTML($html)->save('../public/quotes/' . $qr->id . '.pdf');
         return redirect('send_customer_quote/' . $qr_id)->with('message', 'PDF Quote has been successfully created');
     } else {
         // Sending email
         $user = Auth::user();
         $contact = CustomerContact::find($input['email_to']);
         $from = $user->email;
         $replyto = $from;
         $mail_to = $contact->email;
         $subject = 'Quote';
         $body = '<p>Dear ' . $customer->postal_attention . ',</p><p>Please find enclosed a quote for your order.</p>' . '<p>Kind Regards,<br>' . $user->name . ' at Franklin Direct</p>';
         $path = 'quotes/' . $qr_id . '.pdf';
         $name = basename($path);
         $EOL = "\r\n";
         $boundary = "--" . md5(uniqid(time()));
         $headers = "MIME-Version: 1.0;{$EOL}";
         $headers .= "Content-Type: multipart/mixed; boundary=\"{$boundary}\"{$EOL}";
         $headers .= "From: Franklin Direct <{$from}>";
         $multipart = "--{$boundary}{$EOL}";
         $multipart .= "Content-Type: text/html;{$EOL}";
         $multipart .= "Content-Transfer-Encoding: base64{$EOL}";
         $multipart .= $EOL;
         $multipart .= chunk_split(base64_encode($body));
         $multipart .= "{$EOL}--{$boundary}{$EOL}";
         if (file_exists($path)) {
             $fp = fopen($path, "rb");
             $file = fread($fp, filesize($path));
             fclose($fp);
             $multipart .= "Content-Type: application/octet-stream; name=\"{$name}\"{$EOL}";
             $multipart .= "Content-Transfer-Encoding: base64{$EOL}";
             $multipart .= "Content-Disposition: attachment; filename=\"{$name}\"{$EOL}";
             $multipart .= $EOL;
             $multipart .= chunk_split(base64_encode($file));
             $multipart .= "{$EOL}--{$boundary}--{$EOL}";
         }
         // Send_email
         if (!mail($mail_to, $subject, $multipart, $headers)) {
             return redirect('send_customer_quote/' . $qr_id)->with('error', 'Mail has not been sent due to some errors');
         } else {
             // Update status
             $qr->status = 3;
             $qr->save();
             return redirect('send_customer_quote/' . $qr_id)->with('message', 'Mail sent successfully to ' . $contact->first_name . ' ' . $contact->last_name . ', ' . $contact->email);
         }
     }
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update($id, Request $request)
 {
     $this->validate($request, ['customer_name' => 'required|unique:customers,customer_name,' . $id, 'web_address' => 'url']);
     // Create customer
     $customer = Customer::find($id);
     $customer->update(Input::all());
     $customer_contacts = $customer->customer_contacts;
     $contacts_to_delete = array();
     foreach ($customer_contacts as $contact) {
         array_push($contacts_to_delete, $contact->id);
     }
     foreach ($request->input('contacts') as $key => $contact) {
         // Check for deleting entries
         if (in_array($key, $contacts_to_delete)) {
             $key_to_delete = array_search($key, $contacts_to_delete);
             unset($contacts_to_delete[$key_to_delete]);
         }
         // If at least one of the fields is not null, store contact
         if (!empty($contact['first_name']) || !empty($contact['last_name']) || !empty($contact['phone']) || !empty($contact['mobile']) || !empty($contact['email'])) {
             if (strpos($key, '::') !== false) {
                 $customer->customer_contacts()->create($contact);
             } else {
                 $result = CustomerContact::find($key);
                 $result->update($contact);
             }
         } else {
             // Contacts are empty, so check for deleting
             if (strpos($key, '::') === false) {
                 $result = CustomerContact::find($key);
                 $result->delete();
             }
         }
     }
     // Delete needed contacts
     sort($contacts_to_delete);
     CustomerContact::destroy($contacts_to_delete);
     Debugbar::addMessage(Input::all(), 'input');
     $this->push_to_xero($id);
     return redirect()->route('customers.edit', compact('customer'))->with(['message' => 'Customer updated successfully!', 'action' => $customer])->withInput();
 }
 public function create($data)
 {
     return CustomerContact::create($data);
 }