fadlyshafa

Untitled

May 2nd, 2020
165
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6.  
  7. use App\Models\Sales;
  8.  
  9. use DB;
  10. use Datatables;
  11.  
  12. class Sales_controller extends Controller
  13. {
  14.     public function index(){
  15.         $title = 'History Transaksi POS';
  16.         $data = Sales::withCount('lines')->orderBy('created_at','desc')->get();
  17.  
  18.         $awal = date('Y-m-d',strtotime('-1 days'));
  19.         $akhir = date('Y-m-d');
  20.         $yajra = url('sales/yajra');
  21.  
  22.         return view('sales.index',compact('title','data','awal','akhir','yajra'));
  23.     }
  24.  
  25.     public function detail($id){
  26.         $dt = Sales::find($id);
  27.         $lines = \DB::select("SELECT sl.produk,sum(sl.qty) as total_qty,sum(sl.grand_total) as grand_total,sl.buy,sl.harga,mp.nama from sales_line sl join m_produk mp on mp.id=sl.produk where sl.sales='".$id."' group by sl.produk,sl.buy,sl.harga,mp.nama");
  28.         // dd($lines);
  29.         $title = "Detail Transaksi ".$dt->no_struk;
  30.  
  31.         return view('sales.detail',compact('title','dt','lines'));
  32.     }
  33.  
  34.     public function filter(Request $request){
  35.         $awal = date('Y-m-d',strtotime($request->awal));
  36.         $akhir = date('Y-m-d',strtotime($request->akhir));
  37.  
  38.         $title = "History Transaksi POS $awal s/d $akhir";
  39.         // $data = Sales::whereDate('created_at','>=',$awal)->whereDate('created_at','<=',$akhir)->orderBy('created_at','desc')->get();
  40.         $yajra = url('sales/filter/yajra/'.$awal.'/'.$akhir);
  41.         // dd($data);
  42.         return view('sales.index',compact('title','awal','akhir','yajra'));
  43.     }
  44.  
  45.     public function pelunasan(Request $request,$id){
  46.         try {
  47.             $dt = Sales::find($id);
  48.             $nominal = $request->nominal;
  49.             $deposit = $dt->nominal_deposit;
  50.             Sales::where('id',$id)->update([
  51.                 'is_lunas'=>1,
  52.                 'jumlah_bayar'=>$nominal+$deposit
  53.             ]);
  54.             \Session::flash('sukses','Pelunasan berhasil dilakukan');
  55.         } catch (\Exception $e) {
  56.             \Session::flash('gagal',$e->getMessage());
  57.         }
  58.         return redirect()->back();
  59.     }
  60.  
  61.     public function yajra(Request $request){
  62.         $users = Sales::withCount('lines')->with([
  63.             'customers'
  64.         ])->select('*')->orderBy('created_at','desc')->get();
  65.  
  66.         return Datatables::of($users)->addColumn('action',function($e){
  67.             // $edit = url('produk/'.$e->id);
  68.             $url = url('sales/'.$e->id);
  69.             $struck = url('pos/struck/'.$e->id);
  70.  
  71.             return '<a href="'.$url.'" class="btn btn-xs btn-primary">Detail</a> <a href="'.$struck.'" target="_blank" class="btn btn-xs btn-success"><i class="fa fa-print"></i></a>';
  72.         })->editColumn('jumlah_bayar',function($e){
  73.             return number_format($e->jumlah_bayar,0);
  74.         })->editColumn('diskon',function($e){
  75.             return number_format($e->diskon,0);
  76.         })->editColumn('kembalian',function($e){
  77.             return number_format($e->kembalian,0);
  78.         })->editColumn('grand_total',function($e){
  79.             return number_format($e->grand_total,0);
  80.         })->editColumn('bersih',function($e){
  81.             return number_format($e->bersih,0);
  82.         })->make();
  83.     }
  84.  
  85.     public function yajra_filter(Request $request,$awal,$akhir){
  86.         $users = Sales::withCount('lines')->with([
  87.             'customers'
  88.         ])->select('*')->whereDate('created_at','>=',$awal)->whereDate('created_at','<=',$akhir)->get();
  89.  
  90.         return Datatables::of($users)->addColumn('action',function($e){
  91.             // $edit = url('produk/'.$e->id);
  92.             $url = url('sales/'.$e->id);
  93.             $struck = url('pos/struck/'.$e->id);
  94.  
  95.             return '<a href="'.$url.'" class="btn btn-xs btn-primary">Detail</a> <a href="'.$struck.'" target="_blank" class="btn btn-xs btn-success"><i class="fa fa-print"></i></a>';
  96.         })->editColumn('jumlah_bayar',function($e){
  97.             return number_format($e->jumlah_bayar,0);
  98.         })->editColumn('diskon',function($e){
  99.             return number_format($e->diskon,0);
  100.         })->editColumn('kembalian',function($e){
  101.             return number_format($e->kembalian,0);
  102.         })->editColumn('grand_total',function($e){
  103.             return number_format($e->grand_total,0);
  104.         })->editColumn('bersih',function($e){
  105.             return number_format($e->bersih,0);
  106.         })->make();
  107.     }
  108. }
RAW Paste Data