/**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store()
 {
     $rulesPenerima = ['cppenerima' => 'required', 'emailpenerima' => 'email', 'notelppenerima' => 'required', 'alamatpenerima' => 'required|min:10', 'kotapenerima' => 'not_in:0'];
     $message = ['cppenerima.required' => 'Contact Person wajib diisi', 'email.email' => 'Format email penerima belum benar', 'notelppenerima.required' => 'Telp penerima wajib diisi', 'alamatpenerima.required' => 'Alamat penerima wajib diisi', 'alamatpenerima.min' => 'Alamat penerima terlalu pendek (minim 10 huruf)', 'kotapenerima.not_in' => 'Kota wajib diisi'];
     $rulesPengiriman = [];
     $rules = array_merge($rulesPenerima, $rulesPengiriman);
     $validation = Validator::make(Request::all(), $rules, $message);
     $brg = Request::get('nmbarang');
     $qty = Request::get('qty');
     $sat = Request::get('satuan');
     if ($validation->fails() || !(count($brg) > 0 && $brg[0] != '')) {
         $err = '<ul>';
         //return view('world.dashboard.order-add')->withErrors($validation->errors())->withInputs()->with('pengirim',$pengirim);
         foreach ($validation->errors()->toArray() as $key => $value) {
             foreach ($value as $k => $v) {
                 $err .= '<li>' . $v . '</li>';
             }
         }
         if (!(count($brg) > 0 && $brg[0] != '')) {
             $err .= '<li>Daftar barang belum terisi</li>';
         }
         $err .= '</ul>';
         $data['error'] = true;
         $data['message'] = $err;
         return $data;
         //json_encode($data);
     } else {
         if (Request::get('penerima') == 0) {
             //simpan data penerima
             $penerima = new konsumen();
             $penerima->nama = Request::get('nppenerima') ? Request::get('nppenerima') : '-';
             $penerima->alamat = Request::get('alamatpenerima');
             $penerima->kota = Request::get('kotapenerima');
             $penerima->notelp = Request::get('notelppenerima');
             $penerima->email = Request::get('emailpenerima');
             $penerima->cp = Request::get('cppenerima');
             $penerima->tgldaftar = Date('Y-m-d');
             $penerima->save();
         } else {
             $penerima = konsumen::find(Request::get('penerima'));
             $penerima->nama = Request::get('nppenerima') ? Request::get('nppenerima') : '-';
             $penerima->alamat = Request::get('alamatpenerima');
             $penerima->kota = Request::get('kotapenerima');
             $penerima->notelp = Request::get('notelppenerima');
             $penerima->email = Request::get('emailpenerima');
             $penerima->cp = Request::get('cppenerima');
             $penerima->tgldaftar = Date('Y-m-d');
             $penerima->save();
         }
         //Ambil id pengirim
         $pengirim = konsumen::find(Request::get('pengirim'));
         //simpan data quote
         $idquote = $this->generateId();
         $quote = new quote();
         $quote->id = $idquote;
         $quote->iduser = \Auth::user()->id;
         $quote->idkonsumen = $pengirim->idkonsumen;
         $quote->idpenerima = $penerima->idkonsumen;
         $quote->tglquote = Date('Y-m-d');
         $quote->tipe = Request::get('tipe');
         $quote->almtasal = Request::get('asalsama') ? $pengirim->alamat : Request::get('alamatasal');
         $quote->ktasal = Request::get('asalsama') ? $pengirim->kota : Request::get('kotaasal');
         $quote->tgljemput = \App\Helpers::dateToMySqlSystem(Request::get('tgljemput'));
         $quote->tglkirim = \App\Helpers::dateToMySqlSystem(Request::get('tglkirim'));
         $quote->tagihan = Request::get('tagihan');
         $quote->save();
         //simpan data pengirim dan penerima di table pengirim_penerima
         $tbl_pp = pengirim_penerima::where('pengirim', '=', $pengirim->idkonsumen)->where('penerima', '=', $penerima->idkonsumen)->get();
         if ($tbl_pp->isEmpty()) {
             $tbl_pp = new pengirim_penerima();
             $tbl_pp->pengirim = $pengirim->idkonsumen;
             $tbl_pp->penerima = $penerima->idkonsumen;
             $tbl_pp->save();
         }
         //simpan rincian item yang diorder
         foreach ($sat as $k => $v) {
             $dquote = new dquote();
             $dquote->idquote = $idquote;
             $dquote->barang = $brg[$k];
             $dquote->qty = \App\Helpers::number_parser($qty[$k], 'id_ID');
             $dquote->satuan = $sat[$k];
             $dquote->save();
         }
         $data['success'] = true;
         return json_encode($data);
     }
     //return Redirect::to('/order');
 }