first initial
This commit is contained in:
78
app/Exports/TransactionDealerExport.php
Normal file
78
app/Exports/TransactionDealerExport.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exports;
|
||||
|
||||
use App\Models\Transaction;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||
|
||||
class TransactionDealerExport implements FromCollection
|
||||
{
|
||||
|
||||
protected $dealer;
|
||||
protected $month;
|
||||
|
||||
function __construct($request) {
|
||||
$this->month = (isset($request->dealer)) ? $request->query('month') : date('m');
|
||||
$this->dealer = (isset($request->dealer)) ? $request->query('dealer') : 'all';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function collection()
|
||||
{
|
||||
$dealer_work_trx = DB::statement("SET @sql = NULL");
|
||||
$sql = "SELECT IF(work_id IS NOT NULL, GROUP_CONCAT(DISTINCT CONCAT('SUM(IF(work_id = \"', work_id,'\", qty,\"\")) AS \"',CONCAT(w.name, '|',w.id),'\"')), 's.work_id') INTO @sql FROM transactions t JOIN works w ON w.id = t.work_id WHERE month(t.date) = '". $this->month ."' and year(t.date) = '". date('Y') ."' and t.deleted_at is null";
|
||||
|
||||
if(isset($this->dealer) && $this->dealer != 'all') {
|
||||
$sql .= " and t.dealer_id = '". $this->dealer ."'";
|
||||
}
|
||||
|
||||
$dealer_work_trx = DB::statement($sql);
|
||||
|
||||
if(isset($this->dealer) && $this->dealer != 'all') {
|
||||
$dealer_work_trx = DB::statement("SET @sql = IF(@sql != 's.work_id' ,CONCAT(\"SELECT d.name as DEALER, d.id as dealer_id, \", @sql, \"FROM transactions s JOIN dealers d ON d.id = s.dealer_id WHERE month(s.date) = '". $this->month ."' and year(s.date) = '". date('Y') ."' and s.deleted_at is null and s.dealer_id = '". $this->dealer ."' GROUP BY s.dealer_id ORDER BY s.dealer_id ASC\"), CONCAT(\"SELECT d.name as DEALER \", \"FROM transactions s JOIN dealers d ON d.id = s.dealer_id WHERE month(s.date) = '". $this->month ."' and year(s.date) = '". date('Y') ."' and s.deleted_at is null and s.dealer_id = '". $this->dealer ."' GROUP BY s.`dealer_id` ORDER BY s.`dealer_id` ASC\"))");
|
||||
}else{
|
||||
$dealer_work_trx = DB::statement("SET @sql = IF(@sql != 's.work_id' ,CONCAT(\"SELECT d.name as DEALER, d.id as dealer_id, \", @sql, \"FROM transactions s JOIN dealers d ON d.id = s.dealer_id WHERE month(s.date) = '". $this->month ."' and year(s.date) = '". date('Y') ."' and s.deleted_at is null GROUP BY s.dealer_id ORDER BY s.dealer_id ASC\"), CONCAT(\"SELECT d.name as DEALER, d.id as dealer_id \", \"FROM transactions s JOIN dealers d ON d.id = s.dealer_id WHERE month(s.date) = '". $this->month ."' and year(s.date) = '". date('Y') ."' and s.deleted_at is null GROUP BY s.`dealer_id` ORDER BY s.`dealer_id` ASC\"))");
|
||||
}
|
||||
|
||||
$dealer_work_trx = DB::statement("PREPARE stmt FROM @sql");
|
||||
$dealer_work_trx = DB::select(DB::raw("EXECUTE stmt"));
|
||||
DB::statement('DEALLOCATE PREPARE stmt');
|
||||
|
||||
$work_trx = [];
|
||||
foreach($dealer_work_trx as $index => $dealer_work) {
|
||||
$dealer_work_2 = (array) $dealer_work;
|
||||
unset($dealer_work_2['dealer_id']);
|
||||
$work_names = array_keys($dealer_work_2);
|
||||
if($index == 0) {
|
||||
$work_names_e = [];
|
||||
foreach($work_names as $work) {
|
||||
$arr_work = explode('|', $work);
|
||||
$work_names_e[] = $arr_work[0];
|
||||
}
|
||||
$work_trx[] = $work_names_e;
|
||||
$work_trx[] = array_values($dealer_work_2);
|
||||
}else{
|
||||
$work_trx[] = array_values($dealer_work_2);
|
||||
}
|
||||
}
|
||||
|
||||
return collect($work_trx);
|
||||
}
|
||||
|
||||
|
||||
// public function headings(): array
|
||||
// {
|
||||
// return $this->theads;
|
||||
// }
|
||||
|
||||
// public function actions(Request $request)
|
||||
// {
|
||||
// return [
|
||||
// (new DownloadExcel)->withHeadings('Tanggal', 'Mekanik', 'SA', 'No.Polisi', 'Warranty', 'Kategori', 'Qty')
|
||||
// ];
|
||||
// }
|
||||
}
|
||||
93
app/Exports/TransactionExport.php
Normal file
93
app/Exports/TransactionExport.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exports;
|
||||
|
||||
use App\Models\Transaction;
|
||||
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||
|
||||
class TransactionExport implements FromCollection, WithHeadings
|
||||
{
|
||||
|
||||
protected $date_start;
|
||||
protected $date_end;
|
||||
protected $sa;
|
||||
protected $mechanic;
|
||||
protected $dealer;
|
||||
|
||||
function __construct($request) {
|
||||
$this->date_start = $request->query('date_start');
|
||||
$this->date_end = $request->query('date_end');
|
||||
$this->sa = $request->query('sa');
|
||||
$this->mechanic = $request->query('mechanic');
|
||||
$this->dealer = $request->query('dealer');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function collection()
|
||||
{
|
||||
$data = Transaction::leftJoin('users', 'users.id', '=', 'transactions.user_id')
|
||||
->leftJoin('users as sa', 'sa.id', '=', 'transactions.user_sa_id')
|
||||
->leftJoin('works as w', 'w.id', '=', 'transactions.work_id')
|
||||
->leftJoin('categories as cat', 'cat.id', '=', 'w.category_id')
|
||||
->leftJoin('dealers as d', 'd.id', '=', 'transactions.dealer_id')
|
||||
->select('transactions.id', 'transactions.status', 'transactions.user_id as user_id', 'transactions.user_sa_id as user_sa_id', 'users.name as username', 'sa.name as sa_name', 'cat.name as category_name', 'w.name as workname', 'transactions.qty as qty', 'transactions.date as date', 'transactions.police_number as police_number', 'transactions.warranty as warranty', 'transactions.spk as spk', 'transactions.dealer_id', 'd.name as dealer_name');
|
||||
|
||||
if(isset($this->date_start) && $this->date_start != null) {
|
||||
$data = $data->where('transactions.date', '>=', $this->date_start);
|
||||
}
|
||||
|
||||
if(isset($this->date_end) && $this->date_end != null) {
|
||||
$data = $data->where('transactions.date', '<=', $this->date_end);
|
||||
}
|
||||
|
||||
if(isset($this->sa) && $this->sa != null) {
|
||||
$data = $data->where('transactions.user_sa_id', $this->sa);
|
||||
}
|
||||
|
||||
if(isset($this->mechanic) && $this->mechanic != null) {
|
||||
$data = $data->where('transactions.user_id', $this->mechanic);
|
||||
}
|
||||
|
||||
if(isset($this->dealer) && $this->dealer != null) {
|
||||
$data = $data->where('transactions.dealer_id', $this->dealer);
|
||||
}
|
||||
|
||||
$data = $data->orderBy('date', 'DESC')->get();
|
||||
|
||||
$data2 = [];
|
||||
foreach($data as $item) {
|
||||
$data2[] = [
|
||||
"date" => date('d-m-Y', strtotime($item['date'])),
|
||||
"dealer" => $item['dealer_name'],
|
||||
"spk" => $item['spk'],
|
||||
"mechanic" => $item['username'],
|
||||
"sa" => $item['sa_name'],
|
||||
"police_number" => $item['police_number'],
|
||||
"warranty" => $item['warranty'] == 1 ? 'Ya' : 'Tidak',
|
||||
"category" => $item['category_name'],
|
||||
"work" => $item['workname'],
|
||||
"qty" => $item['qty']
|
||||
];
|
||||
}
|
||||
|
||||
$data2 = collect($data2);
|
||||
|
||||
return $data2;
|
||||
}
|
||||
|
||||
|
||||
public function headings(): array
|
||||
{
|
||||
return ["Tanggal", "Dealer", "Spk","Mekanik", "SA", "No. Polisi", "Warranty", "Kategori", "Pekerjaan", "Qty"];
|
||||
}
|
||||
|
||||
// public function actions(Request $request)
|
||||
// {
|
||||
// return [
|
||||
// (new DownloadExcel)->withHeadings('Tanggal', 'Mekanik', 'SA', 'No.Polisi', 'Warranty', 'Kategori', 'Qty')
|
||||
// ];
|
||||
// }
|
||||
}
|
||||
90
app/Exports/TransactionSaExport.php
Normal file
90
app/Exports/TransactionSaExport.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exports;
|
||||
|
||||
use App\Models\Transaction;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||
|
||||
class TransactionSaExport implements FromCollection
|
||||
{
|
||||
|
||||
protected $dealer;
|
||||
protected $month;
|
||||
protected $sa;
|
||||
|
||||
function __construct($request) {
|
||||
$this->month = (isset($request->month)) ? $request->query('month') : date('m');
|
||||
$this->dealer = (isset($request->dealer)) ? $request->query('dealer') : 'all';
|
||||
$this->sa = (isset($request->sa)) ? $request->query('sa') : 'all';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function collection()
|
||||
{
|
||||
$sa_work_trx = DB::statement("SET @sql = NULL");
|
||||
$sql = "SELECT IF(work_id IS NOT NULL, GROUP_CONCAT(DISTINCT CONCAT('SUM(IF(work_id = \"', work_id,'\", qty,\"\")) AS \"',CONCAT(w.name, '|',w.id),'\"')), 's.work_id') INTO @sql FROM transactions t JOIN works w ON w.id = t.work_id WHERE month(t.date) = '". $this->month ."' and year(t.date) = '". date('Y') ."' and t.deleted_at is null";
|
||||
|
||||
if(isset($this->dealer) && $this->dealer != 'all') {
|
||||
$sql .= " and t.dealer_id = '". $this->dealer ."'";
|
||||
}
|
||||
|
||||
if(isset($this->sa) && $this->sa != 'all') {
|
||||
$sql .= " and t.user_sa_id = '". $this->sa ."'";
|
||||
}
|
||||
|
||||
$sa_work_trx = DB::statement($sql);
|
||||
|
||||
if(isset($this->dealer) && $this->dealer != 'all') {
|
||||
if(isset($this->sa) && $this->sa != 'all') {
|
||||
$sa_work_trx = DB::statement("SET @sql = IF(@sql != 's.work_id' ,CONCAT(\"SELECT sa.name as SA, sa.id as sa_id, \", @sql, \"FROM transactions s JOIN users sa ON sa.id = s.user_sa_id WHERE month(s.date) = '". $this->month ."' and year(s.date) = '". date('Y') ."' and s.deleted_at is null and s.dealer_id = '". $this->dealer ."' and s.user_sa_id = '". $this->sa ."' GROUP BY s.user_sa_id ORDER BY s.user_sa_id ASC\"), CONCAT(\"SELECT sa.name as SA, sa.id as sa_id \", \"FROM transactions s JOIN users sa ON sa.id = s.user_sa_id WHERE month(s.date) = '". $this->month ."' and year(s.date) = '". date('Y') ."' and s.deleted_at is null and s.dealer_id = '". $this->dealer ."' and s.user_sa_id = '". $this->sa ."' GROUP BY s.`user_sa_id` ORDER BY s.`user_sa_id` ASC\"))");
|
||||
}else{
|
||||
$sa_work_trx = DB::statement("SET @sql = IF(@sql != 's.work_id' ,CONCAT(\"SELECT sa.name as SA, sa.id as sa_id, \", @sql, \"FROM transactions s JOIN users sa ON sa.id = s.user_sa_id WHERE month(s.date) = '". $this->month ."' and year(s.date) = '". date('Y') ."' and s.deleted_at is null and s.dealer_id = '". $this->dealer ."' GROUP BY s.user_sa_id ORDER BY s.user_sa_id ASC\"), CONCAT(\"SELECT sa.name as SA, sa.id as sa_id \", \"FROM transactions s JOIN users sa ON sa.id = s.user_sa_id WHERE month(s.date) = '". $this->month ."' and year(s.date) = '". date('Y') ."' and s.deleted_at is null and s.dealer_id = '". $this->dealer ."' GROUP BY s.`user_sa_id` ORDER BY s.`user_sa_id` ASC\"))");
|
||||
}
|
||||
}else{
|
||||
if(isset($this->sa) && $this->sa != 'all') {
|
||||
$sa_work_trx = DB::statement("SET @sql = IF(@sql != 's.work_id' ,CONCAT(\"SELECT sa.name as SA, sa.id as sa_id, \", @sql, \"FROM transactions s JOIN users sa ON sa.id = s.user_sa_id WHERE month(s.date) = '". $this->month ."' and year(s.date) = '". date('Y') ."' and s.deleted_at is null and s.user_sa_id = '". $this->sa ."' GROUP BY s.user_sa_id ORDER BY s.user_sa_id ASC\"), CONCAT(\"SELECT sa.name as SA, sa.id as user_sa_id \", \"FROM transactions s JOIN dealers d ON d.id = s.user_sa_id WHERE month(s.date) = '". $this->month ."' and year(s.date) = '". date('Y') ."' and s.deleted_at is null and s.user_sa_id = '". $this->sa ."' GROUP BY s.`user_sa_id` ORDER BY s.`user_sa_id` ASC\"))");
|
||||
}else{
|
||||
$sa_work_trx = DB::statement("SET @sql = IF(@sql != 's.work_id' ,CONCAT(\"SELECT sa.name as SA, sa.id as sa_id, \", @sql, \"FROM transactions s JOIN users sa ON sa.id = s.user_sa_id WHERE month(s.date) = '". $this->month ."' and year(s.date) = '". date('Y') ."' and s.deleted_at is null GROUP BY s.user_sa_id ORDER BY s.user_sa_id ASC\"), CONCAT(\"SELECT sa.name as SA, sa.id as user_sa_id \", \"FROM transactions s JOIN dealers d ON d.id = s.user_sa_id WHERE month(s.date) = '". $this->month ."' and year(s.date) = '". date('Y') ."' and s.deleted_at is null GROUP BY s.`user_sa_id` ORDER BY s.`user_sa_id` ASC\"))");
|
||||
}
|
||||
}
|
||||
|
||||
$sa_work_trx = DB::statement("PREPARE stmt FROM @sql");
|
||||
$sa_work_trx = DB::select(DB::raw("EXECUTE stmt"));
|
||||
DB::statement('DEALLOCATE PREPARE stmt');
|
||||
$work_trx = [];
|
||||
foreach($sa_work_trx as $index => $sa_work) {
|
||||
$sa_work_2 = (array) $sa_work;
|
||||
unset($sa_work_2['sa_id']);
|
||||
$work_names = array_keys($sa_work_2);
|
||||
if($index == 0) {
|
||||
$work_names_e = [];
|
||||
foreach($work_names as $work) {
|
||||
$arr_work = explode('|', $work);
|
||||
$work_names_e[] = $arr_work[0];
|
||||
}
|
||||
$work_trx[] = $work_names_e;
|
||||
$work_trx[] = array_values($sa_work_2);
|
||||
}else{
|
||||
$work_trx[] = array_values($sa_work_2);
|
||||
}
|
||||
}
|
||||
|
||||
return collect($work_trx);
|
||||
}
|
||||
|
||||
|
||||
// public function headings(): array
|
||||
// {
|
||||
// return $this->theads;
|
||||
// }
|
||||
|
||||
// public function actions(Request $request)
|
||||
// {
|
||||
// return [
|
||||
// (new DownloadExcel)->withHeadings('Tanggal', 'Mekanik', 'SA', 'No.Polisi', 'Warranty', 'Kategori', 'Qty')
|
||||
// ];
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user