/**
  * Execute the job.
  *
  * @return void
  */
 public function handle()
 {
     //Log::info("In Handle ".$this->tanggal);
     $tanggal = $this->tanggal;
     $reports = Order::ReportByDate($tanggal);
     $reports = collect($reports)->groupBy('_type_bayar');
     $totalCash = isset($reports['tunai']) ? collect($reports['tunai'])->sum('jumlah') : 0;
     $mergeDebitCcard = [];
     $mergeDebitCcard = isset($reports['debit']) ? array_merge($mergeDebitCcard, $reports['debit']->toArray()) : array_merge($mergeDebitCcard, []);
     $mergeDebitCcard = isset($reports['credit_card']) ? array_merge($mergeDebitCcard, $reports['credit_card']->toArray()) : array_merge($mergeDebitCcard, []);
     $bayarBank = collect($mergeDebitCcard)->groupBy('_bank_id');
     $accountSaldo = AccountSaldo::join('accounts', 'account_saldos.account_id', '=', 'accounts.id')->leftJoin('banks', 'account_saldos.relation_id', '=', 'banks.id')->where('tanggal', $tanggal)->where('account_id', 2)->select(['account_saldos.*', DB::raw('IFNULL(account_saldos.relation_id, "cash")_relation_id')])->get()->groupBy("_relation_id");
     $totals = [];
     if ($totalCash > 0) {
         $totals['cash'] = $totalCash;
     }
     foreach ($bayarBank as $key => $val) {
         $totals[$key] = $val->sum('jumlah') - $val->sum('pajak_pembayaran');
     }
     $actions = [];
     $totalKeys = array_keys($totals);
     foreach ($totalKeys as $bank_id) {
         $nominal = $totals[$bank_id];
         if (!isset($accountSaldo[$bank_id])) {
             // create new
             $inputs = ['tanggal' => $tanggal, 'account_id' => 2, 'nominal' => $nominal];
             if ($bank_id != 'cash') {
                 $inputs += ['type' => 'kredit', 'relation_id' => $bank_id];
             } else {
                 $inputs += ['type' => 'debet'];
             }
             AccountSaldo::create($inputs);
             //$actions[$bank_id] = $inputs;
         } else {
             // update value
             $row = $accountSaldo[$bank_id][0];
             $row = AccountSaldo::find($row['id']);
             $row->update(['nominal' => $nominal]);
             //$actions[$bank_id]  = $row;
         }
     }
     //return $actions;
     //Log::info('Queue End @'.Carbon::now('Asia/Jakarta'));
 }
 /**
  * Execute the job.
  *
  * @return void
  */
 public function handle()
 {
     //Log::info("Handle Purchase Count Job ".$this->tanggal);
     $tanggal = $this->tanggal;
     $pembelian = PembelianBayar::where("tanggal", $tanggal)->select([DB::raw("SUM(nominal)total")])->groupBy("tanggal")->first();
     if ($pembelian) {
         $total = $pembelian->total;
         if ($total >= 1) {
             $account = AccountSaldo::where('tanggal', $tanggal)->where('account_id', 1)->first();
             if (!$account) {
                 //create new
                 AccountSaldo::create(['tanggal' => $tanggal, 'account_id' => 1, 'type' => 'kredit', 'nominal' => $total]);
             } else {
                 // update value
                 if ($account->nominal != $total) {
                     AccountSaldo::find($account->id)->update(['nominal' => $total]);
                 }
             }
         }
     }
     //Log::info("Handle Purchase Count Job ".$pembelian->total);
 }
 public function saveEditInputManual(Request $request, $id)
 {
     $validator = Validator::make($request->all(), ['tanggal' => 'required|date', 'nominal' => 'required'], ['tanggal.required' => 'Tanggal tidak boleh kosong.', 'tanggal.date' => 'Input harus tanggal.', 'nominal.required' => 'Nominal tidak boleh kosong.']);
     if ($validator->fails()) {
         return redirect()->back()->withErrors($validator)->withInput();
     }
     $account_id = $request->get('account_id');
     $account = Account::find($account_id);
     $input = $request->all() + ['type' => $account->type];
     if (AccountSaldo::find($id)->update($input)) {
         return redirect('/account/saldo?tanggal=' . $request->get('tanggal'))->with('succcess', 'Sukses ubah saldo.');
     }
     return redirect()->back()->withErrors(['failed' => 'Gagal ubah saldo.']);
 }