137 lines
4.2 KiB
PHP
137 lines
4.2 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Models;
|
||
|
|
||
|
use Datatables, DB;
|
||
|
use Exception;
|
||
|
use Illuminate\Database\Eloquent\Model;
|
||
|
use Illuminate\Http\Request;
|
||
|
use Yajra\DataTables\DataTables as DT;
|
||
|
use Illuminate\Support\Facades\Http;
|
||
|
use App\Http\Controllers\Controller;
|
||
|
use Auth;
|
||
|
|
||
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||
|
|
||
|
class grossModel extends Model
|
||
|
{
|
||
|
protected $primaryKey = 'outlet_id';
|
||
|
protected $table = 'outlet';
|
||
|
public $timestamps = false;
|
||
|
|
||
|
|
||
|
public function initData($request,$route)
|
||
|
{
|
||
|
// INIT DB
|
||
|
$data['title'] = 'Gross Profit and Sales';
|
||
|
$data['actButton'] = [];
|
||
|
$data['tableHead'] =
|
||
|
array(
|
||
|
["Tanggal","all","crtdt"],
|
||
|
["Merchant","all","merchant_nm"],
|
||
|
["Outlet","all","outlet_nm"],
|
||
|
["Gross Sales","all","gross_sales"],
|
||
|
["Gross Profit","all","gross_profit"],
|
||
|
["Trx","all","count_trx"]
|
||
|
);
|
||
|
|
||
|
|
||
|
$data['db'] = $this->table;
|
||
|
$data['db_key'] = $this->primaryKey;
|
||
|
$data['route'] = $route;
|
||
|
|
||
|
$cGlobal = new Controller();
|
||
|
|
||
|
if ($request->startDate) {
|
||
|
|
||
|
$startDate = date('Y-m-d',strtotime($request->startDate)).' 00:00:00';
|
||
|
$endDate = date('Y-m-d',strtotime($request->endDate)).' 24:00:00';
|
||
|
}else{
|
||
|
$startDate = date('Y-m-d').' 00:00:00';
|
||
|
$endDate = date('Y-m-d').' 24:00:00';
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
$dtable = DB::table("payment")
|
||
|
->leftJoin("merchant", function($join){
|
||
|
$join->on("payment.mid", "=", "merchant.mid");
|
||
|
})
|
||
|
->leftJoin("outlet", function($join){
|
||
|
$join->on("outlet.outlet_id", "=", "payment.outlet_id");
|
||
|
})
|
||
|
->leftJoin("bill", function($join){
|
||
|
$join->on("bill.bill_id", "=", "payment.bill_id");
|
||
|
})
|
||
|
->leftJoin("orders_bill", function($join){
|
||
|
$join->on("orders_bill.bill_id", "=", "bill.bill_id");
|
||
|
})
|
||
|
->leftJoin("orders", function($join){
|
||
|
$join->on("orders.order_id", "=", "orders_bill.order_id");
|
||
|
})
|
||
|
->select("merchant.mid", "merchant.merchant_nm", "outlet.outlet_id", "outlet.outlet_nm",
|
||
|
DB::raw("payment.crtdt::date"), DB::raw("sum (payment.total_amount) as total_amount"),
|
||
|
DB::raw("sum (bill.tax_amount) as tax", "sum (bill.charge_amount) as charge"),
|
||
|
DB::raw("sum (bill.bill_amount) as gross_sales"),
|
||
|
DB::raw("sum (orders.total_amount_base) as cogs"),
|
||
|
DB::raw("sum (bill.bill_amount) - sum (distinct orders.total_amount_base) as gross_profit"),
|
||
|
DB::raw("count (payment.payment_id) as count_trx"),
|
||
|
DB::raw("count (orders.order_id) as count_order"))
|
||
|
->where("payment.payment_status_id", "=", 1)
|
||
|
->where("bill.crtdt", ">=", $startDate)
|
||
|
->where("bill.crtdt", "<=", $endDate)
|
||
|
->groupBy("merchant.mid","merchant.merchant_nm","outlet.outlet_id","outlet.outlet_nm",DB::raw("payment.crtdt::date"))
|
||
|
->orderByDesc(DB::raw("payment.crtdt::date"))
|
||
|
->when(request('merchant_filter') != null, function ($query) use ($request) {
|
||
|
if ($request->merchant_filter != 'all') {
|
||
|
$query->where('payment.mid', $request->merchant_filter);
|
||
|
}
|
||
|
})
|
||
|
->when(Auth::guard('admin')->user()->role == 3, function ($query) use ($request) {
|
||
|
$query->where('merchant.city_id', Auth::guard('admin')->user()->id_kota);
|
||
|
})
|
||
|
->get();
|
||
|
|
||
|
|
||
|
|
||
|
// LIST DATA TABLE
|
||
|
$data['data_table'] = $dtable;
|
||
|
|
||
|
// FORM FIELD FOR STORE
|
||
|
$data['set_field'] = [
|
||
|
|
||
|
];
|
||
|
|
||
|
foreach($data['tableHead'] as $v){
|
||
|
$arrHead[] = $v[2];
|
||
|
}
|
||
|
$data['head'] = implode(",",$arrHead);
|
||
|
|
||
|
return $data;
|
||
|
}
|
||
|
|
||
|
public function getDT($data,$init)
|
||
|
{
|
||
|
$dt = DT::of($data);
|
||
|
|
||
|
$dt->editColumn('crtdt',function($data) {
|
||
|
return date('d-m-Y',strtotime($data->crtdt));
|
||
|
});
|
||
|
|
||
|
$dt->editColumn('gross_sales',function($data) {
|
||
|
return number_format($data->gross_sales,0,",",".");
|
||
|
});
|
||
|
|
||
|
$dt->editColumn('gross_profit',function($data) {
|
||
|
return number_format($data->gross_profit,0,",",".");
|
||
|
});
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
return $dt;
|
||
|
}
|
||
|
|
||
|
}
|