pos-gis/app/Models/orderModel.php
2024-10-08 13:24:47 +07:00

126 lines
3.7 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 orderModel extends Model
{
protected $primaryKey = 'order_id';
protected $table = 'orders';
public $timestamps = false;
public function initData($request,$route)
{
// INIT DB
$data['title'] = 'Order';
$data['actButton'] = [];
$data['tableHead'] =
array(
["Company","all","merchant_nm"],
["Time","all","crtdt"],
["Staff","all","user_nm"],
["Order Type","all","order_type_nm"],
["Pay Date","all","paydt"],
["Order Code","all","order_code"],
["Billing Type","all","bill_type_nm"],
["Customer Alias","all","customer_alias"],
["Payment Status","all","payment_status_nm"]
);
$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("orders")
->join("users", function($join){
$join->on("users.user_id", "=", "orders.user_id");
})
->leftJoin("merchant", function($join){
$join->on("merchant.mid", "=", "users.mid");
})
->leftJoin("outlet", function($join){
$join->on("outlet.outlet_id", "=", "orders.outlet_id");
})
->join("reff_order_type", function($join){
$join->on("reff_order_type.order_type_id", "=", "orders.order_type_id");
})
->join("reff_bill_type", function($join){
$join->on("reff_bill_type.bill_type_id", "=", "orders.bill_type_id");
})
->join("reff_payment_status", function($join){
$join->on("reff_payment_status.payment_status_id", "=", "orders.payment_status_id");
})
->select("orders.*", "reff_bill_type.*", "merchant_nm", "reff_payment_status.payment_status_nm", "users.user_nm", "orders.outlet_id", "reff_order_type.*")
->where("orders.crtdt", ">=", $startDate)
->where("orders.crtdt", "<=", $endDate)
->when(request('merchant_filter') != null, function ($query) use ($request) {
if ($request->merchant_filter != 'all') {
$query->where('users.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 H:i:s',strtotime($data->crtdt));
});
$dt->editColumn('paydt',function($data) {
return date('d-m-Y H:i:s',strtotime($data->paydt));
});
return $dt;
}
}