/** * Execute the job. * * @return void */ public function handle() { MutasiStokProduk::where('tanggal', $this->tanggal)->delete(); $bahans = \App\Produk::MutasiStok($this->tanggal); $inserts = []; foreach ($bahans as $bahan) { array_push($inserts, ['produk_id' => $bahan['id'], 'before' => $bahan['before'], 'pembelian' => $bahan['pembelian'], 'penjualan' => $bahan['penjualan'], 'adjustment_increase' => $bahan['adjustment_increase'], 'adjustment_reduction' => $bahan['adjustment_reduction'], 'sisa' => $bahan['sisa'], 'tanggal' => $this->tanggal]); } MutasiStokProduk::insert($inserts); }
protected function _stokPeriode(Request $request) { $tanggal = $request->get('tanggal') ? $request->get('tanggal') : date('Y-m-d'); $to_tanggal = $request->get('to_tanggal') ? $request->get('to_tanggal') : $tanggal; $produks = \App\MutasiStokProduk::with('produk')->select(['id', 'produk_id', 'before', DB::raw("SUM(pembelian)pembelian"), DB::raw("SUM(pembelian)pembelian"), DB::raw("SUM(adjustment_increase)adjustment_increase"), DB::raw("SUM(adjustment_reduction)adjustment_reduction"), DB::raw("(((((`before`)+SUM(pembelian))-SUM(penjualan))+SUM(adjustment_increase))-SUM(adjustment_reduction))sisa")])->whereBetween('tanggal', [$tanggal, $to_tanggal])->groupBy('produk_id')->get(); return ['tanggal' => Carbon::createFromFormat('Y-m-d', $tanggal), 'to_tanggal' => Carbon::createFromFormat('Y-m-d', $to_tanggal), 'produks' => $produks]; }