342 lines
No EOL
14 KiB
PHP
342 lines
No EOL
14 KiB
PHP
<?php
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
use Auth;
|
|
use Request;
|
|
use App\User;
|
|
use Carbon\Carbon;
|
|
use App\Exports\xExport;
|
|
use App\Services\HTMLHelper;
|
|
use App\Models\ShoppingOrder;
|
|
use App\Exports\UserTeamExport;
|
|
use App\Models\ShoppingOrderItem;
|
|
use App\Http\Controllers\Controller;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use App\Services\BusinessPlan\ExportBot;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
class ProductsSalesController extends Controller
|
|
{
|
|
|
|
public function __construct()
|
|
{
|
|
$this->middleware('admin');
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
|
|
$this->setFilterVars();
|
|
$data = [
|
|
'filter_months' => HTMLHelper::getTransMonths(),
|
|
'filter_years' => HTMLHelper::getYearRange(2022),
|
|
];
|
|
return view('admin.payment.salesvolume', $data);
|
|
}
|
|
|
|
|
|
|
|
public function download(){
|
|
|
|
/*
|
|
EXCEL EXPORT function */
|
|
|
|
if(Request::get('action') === "exportfull_paid"){
|
|
return $this->exportFullList(1);
|
|
}
|
|
if(Request::get('action') === "exportfull_unpaid"){
|
|
return $this->exportFullList(0);
|
|
}
|
|
|
|
if(Request::get('action') === "exportfull_paid_invoice"){
|
|
return $this->exportFullListInvoice();
|
|
}
|
|
|
|
|
|
|
|
if(Request::get('action') === "export"){
|
|
$objects = $this->initSearch(false);
|
|
$columns = [];
|
|
$filename = "mivita-absatzmengen-".session('product_sales_vol_filter_month').'_'.session('product_sales_vol_filter_year')."-export";
|
|
$headers = array(
|
|
'#',
|
|
'Produkt',
|
|
'Artikelnummer',
|
|
'Menge',
|
|
|
|
);
|
|
if($objects){
|
|
foreach ($objects as $key => $obj){
|
|
$columns[] = array(
|
|
'id' => $key,
|
|
'name' => $obj['name'],
|
|
'number' => $obj['number'],
|
|
'value' => $obj['value'],
|
|
);
|
|
}
|
|
}
|
|
return Excel::download(new UserTeamExport($columns, $headers), $filename.'.xls');
|
|
}
|
|
}
|
|
|
|
|
|
private function setFilterVars(){
|
|
|
|
if(!session('product_sales_vol_filter_month')){
|
|
session(['product_sales_vol_filter_month' => intval(date('m'))]);
|
|
}
|
|
if(!session('product_sales_vol_filter_year')){
|
|
session(['product_sales_vol_filter_year' => intval(date('Y'))]);
|
|
}
|
|
|
|
if(Request::get('product_sales_vol_filter_month')){
|
|
session(['product_sales_vol_filter_month' => Request::get('product_sales_vol_filter_month')]);
|
|
}
|
|
if(Request::get('product_sales_vol_filter_year')){
|
|
session(['product_sales_vol_filter_year' => Request::get('product_sales_vol_filter_year')]);
|
|
}
|
|
}
|
|
|
|
public function exportFullList($paid = 1){
|
|
|
|
$date_start = Carbon::parse('01.'.session('product_sales_vol_filter_month').'.'.session('product_sales_vol_filter_year'))->format('Y-m-d H:i:s');
|
|
$date_end = Carbon::parse('01.'.session('product_sales_vol_filter_month').'.'.session('product_sales_vol_filter_year'))->endOfMonth()->format('Y-m-d H:i:s');
|
|
$ShoppingOrders = ShoppingOrder::where('paid', $paid)->where('mode', 'live')->whereBetween('created_at', [$date_start, $date_end])->get();
|
|
|
|
$txActions = ['prev' => 'keine Zahlung', 'appointed' => 'offen', 'failed' => 'abbruch', 'paid' => 'bezahlt'];
|
|
$headers = array('ID', 'Zahlung', 'Datum', 'EMail', 'ProduktID', 'ProduktNummer', 'ProduktName', 'Anzahl', 'Notiz', 'Gesamt');
|
|
$objects = [];
|
|
$columns = [];
|
|
$hasSOID = [];
|
|
foreach($ShoppingOrders as $ShoppingOrder){
|
|
foreach($ShoppingOrder->shopping_order_items as $shopping_order_item){
|
|
$value = "";
|
|
if($shopping_order_item->product){
|
|
if(isset($objects[$shopping_order_item->product->id])){
|
|
$value = intval($objects[$shopping_order_item->product->id]['value'] + $shopping_order_item->qty);
|
|
$objects[$shopping_order_item->product->id]['value'] = $value;
|
|
}else{
|
|
$objects[$shopping_order_item->product->id] = [
|
|
'name' => $shopping_order_item->product->name,
|
|
'number' => $shopping_order_item->product->number,
|
|
'value' => $shopping_order_item->qty
|
|
];
|
|
$value = $shopping_order_item->qty;
|
|
|
|
}
|
|
|
|
}
|
|
$object = [];
|
|
if(in_array($ShoppingOrder->id, $hasSOID)){
|
|
$object['ID'] = '';
|
|
$object['EMail'] = '';
|
|
$object['Zahlung'] = '';
|
|
$object['Datum'] = '';
|
|
}else{
|
|
$object['ID'] = $ShoppingOrder->id;
|
|
$object['EMail'] = $ShoppingOrder->shopping_user ? $ShoppingOrder->shopping_user->billing_email : 'n/a';
|
|
$object['Zahlung'] = isset($txActions[$ShoppingOrder->txaction]) ? $txActions[$ShoppingOrder->txaction] : $ShoppingOrder->txaction;
|
|
$object['Datum'] = $ShoppingOrder->created_at->format('d.m.Y');
|
|
}
|
|
$object['ProduktID'] = $shopping_order_item->product_id;
|
|
$object['ProduktNummer'] = $shopping_order_item->product ? $shopping_order_item->product->number : "n/a";
|
|
$object['ProduktName'] = $shopping_order_item->product ? $shopping_order_item->product->name : "n/a";
|
|
$object['Anzahl'] = $shopping_order_item->qty;
|
|
$object['Notiz'] = ($shopping_order_item->comp ? 'Compensation '.$shopping_order_item->comp : '') . ($shopping_order_item->shopping_collect_order_id ? 'Sammelbestellung '.$shopping_order_item->shopping_collect_order_id : '');
|
|
$object['Gesamt'] = $value;
|
|
$columns[] = $object;
|
|
|
|
$hasSOID[] = $ShoppingOrder->id;
|
|
}
|
|
}
|
|
if($paid){
|
|
$filename = "mivita-absatzmengen-full-paid-".session('product_sales_vol_filter_month').'_'.session('product_sales_vol_filter_year')."-export";
|
|
}else{
|
|
$filename = "mivita-absatzmengen-full-unpaid-".session('product_sales_vol_filter_month').'_'.session('product_sales_vol_filter_year')."-export";
|
|
}
|
|
|
|
return Excel::download(new xExport($columns, $headers), $filename.'.xls');
|
|
|
|
/* CSV EXPORT function
|
|
$headers = array(
|
|
"Content-type" => "text/csv",
|
|
"Content-Disposition" => "attachment; filename=$fileName",
|
|
"Pragma" => "no-cache",
|
|
"Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
|
|
"Expires" => "0"
|
|
);
|
|
|
|
$header = array('ID', 'Zahlung', 'Datum', 'EMail', 'ProduktID', 'ProduktNummer', 'ProduktName', 'Anzahl', 'Notiz', 'Gesamt');
|
|
|
|
$callback = function() use($columns, $header) {
|
|
|
|
$file = fopen('php://output', 'w');
|
|
fputcsv($file, $header);
|
|
$row = [];
|
|
|
|
foreach ($columns as $row) {
|
|
fputcsv($file, $row);
|
|
|
|
}
|
|
|
|
fclose($file);
|
|
};
|
|
|
|
return response()->stream($callback, 200, $headers);
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
private function initSearch($returnColl = true)
|
|
{
|
|
$this->setFilterVars();
|
|
|
|
$date_start = Carbon::parse('01.'.session('product_sales_vol_filter_month').'.'.session('product_sales_vol_filter_year'))->format('Y-m-d H:i:s');
|
|
$date_end = Carbon::parse('01.'.session('product_sales_vol_filter_month').'.'.session('product_sales_vol_filter_year'))->endOfMonth()->format('Y-m-d H:i:s');
|
|
$ShoppingOrders = ShoppingOrder::where('paid', 1)->where('mode', 'live')->whereBetween('created_at', [$date_start, $date_end])->get();
|
|
|
|
$objects = [];
|
|
foreach($ShoppingOrders as $ShoppingOrder){
|
|
foreach($ShoppingOrder->shopping_order_items as $shopping_order_item){
|
|
if($shopping_order_item->product){
|
|
if(isset($objects[$shopping_order_item->product->id])){
|
|
$value = intval($objects[$shopping_order_item->product->id]['value'] + $shopping_order_item->qty);
|
|
$objects[$shopping_order_item->product->id]['value'] = $value;
|
|
}else{
|
|
$objects[$shopping_order_item->product->id] = [
|
|
'name' => $shopping_order_item->product->name,
|
|
'number' => $shopping_order_item->product->number,
|
|
'value' => $shopping_order_item->qty
|
|
];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if($returnColl){
|
|
$collection = collect();
|
|
|
|
foreach($objects as $key => $obj){
|
|
$collection->push([
|
|
'id' => $key,
|
|
'name' => $obj['name'],
|
|
'number' => $obj['number'],
|
|
'value' => $obj['value'],
|
|
]);
|
|
}
|
|
return $collection;
|
|
}
|
|
return $objects;
|
|
}
|
|
|
|
|
|
public function datatable(){
|
|
|
|
$collection = $this->initSearch(true);
|
|
|
|
$collect = collect([
|
|
['id' => 1, 'name' => 'John', 'number'=>92012, 'value'=>123],
|
|
['id' => 2, 'name' => 'Jane', 'number'=>92012, 'value'=>123],
|
|
['id' => 3, 'name' => 'James', 'number'=>92012, 'value'=>123],
|
|
]);
|
|
|
|
return \DataTables::of($collection)->toJson();
|
|
|
|
|
|
|
|
}
|
|
|
|
/*private function export_vp(){
|
|
|
|
$query = User::with('account')->select('users.*')->where('users.deleted_at', '=', null)->where('users.admin', "<", 4)->get();
|
|
$fileName = "GS-VP-export-".date("d-m-Y").".csv";
|
|
$headers = array(
|
|
"Content-type" => "text/csv",
|
|
"Content-Disposition" => "attachment; filename=$fileName",
|
|
"Pragma" => "no-cache",
|
|
"Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
|
|
"Expires" => "0"
|
|
);
|
|
|
|
$columns = array('ID', 'Email', 'Firma', 'Anrede', 'Vorname', 'Nachname', 'Mitglied', 'Bis');
|
|
|
|
$callback = function() use($query, $columns) {
|
|
|
|
$file = fopen('php://output', 'w');
|
|
fputcsv($file, $columns);
|
|
$row = [];
|
|
|
|
foreach ($query as $val) {
|
|
$row['ID'] = $val->id;
|
|
$row['Email'] = $val->email;
|
|
$row['Firma'] = $val->account->company;
|
|
$row['Anrede'] = $val->account->salutation == 'mr' ? 'Herr' : 'Frau' ;
|
|
$row['Vorname'] = $val->account->first_name;
|
|
$row['Nachname'] = $val->account->last_name;
|
|
$row['Mitglied'] = $val->payment_account ? ($val->isActiveAccount() ? 'JA' : 'Abgelaufen') : "Nein";
|
|
$row['Bis'] = $val->payment_account ? $val->getPaymentAccountDateFormat(false) : "-";
|
|
fputcsv($file, $row);
|
|
|
|
}
|
|
|
|
fclose($file);
|
|
};
|
|
|
|
return response()->stream($callback, 200, $headers);
|
|
|
|
//dd("ok");
|
|
}*/
|
|
|
|
/*private function testCheckFunction(){
|
|
|
|
//$date_start = Carbon::parse('01.'.session('product_sales_vol_filter_month').'.'.session('product_sales_vol_filter_year'))->format('Y-m-d');
|
|
//$date_end = Carbon::parse('01.'.session('product_sales_vol_filter_month').'.'.session('product_sales_vol_filter_year'))->endOfMonth()->format('Y-m-d');
|
|
|
|
$date_start = Carbon::parse('01.01.2024')->format('Y-m-d H:i:s');
|
|
$date_end = Carbon::parse('01.01.2024')->endOfMonth()->format('Y-m-d H:i:s');
|
|
dump($date_start);
|
|
dump($date_end);
|
|
|
|
$ShoppingOrders = ShoppingOrder::where('mode', 'live')->whereBetween('created_at', [$date_start, $date_end])->get();
|
|
|
|
$objects = [];
|
|
$counter = 0;
|
|
foreach($ShoppingOrders as $ShoppingOrder){
|
|
foreach($ShoppingOrder->shopping_order_items as $shopping_order_item){
|
|
|
|
if($shopping_order_item->product){
|
|
if($shopping_order_item->product->id === 122){
|
|
//dump($shopping_order_item->qty);
|
|
//$counter += $shopping_order_item->qty;
|
|
if(isset($objects[$shopping_order_item->product->id])){
|
|
$value = intval($objects[$shopping_order_item->product->id]['value'] + $shopping_order_item->qty);
|
|
$objects[$shopping_order_item->product->id]['value'] = $value;
|
|
}else{
|
|
$objects[$shopping_order_item->product->id] = [
|
|
'name' => $shopping_order_item->product->name,
|
|
'number' => $shopping_order_item->product->number,
|
|
'value' => $shopping_order_item->qty
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
$ShoppingOrderItems = ShoppingOrderItem::whereProductId(122)->whereBetween('created_at', [$date_start, $date_end])->get();
|
|
$counter = 0;
|
|
foreach($ShoppingOrderItems as $ShoppingOrderItem){
|
|
$counter += $ShoppingOrderItem->qty;
|
|
dump($ShoppingOrderItem->id);
|
|
}
|
|
// dump($objects);
|
|
dump($counter);
|
|
dd("OKAY");
|
|
}*/
|
|
|
|
|
|
|
|
} |