Skip to content

Commit

Permalink
Bug fixes and Enhancements
Browse files Browse the repository at this point in the history
1. Date range bug fixed, carbon applied on dates.
2. Code cleanup done, unnecessary code removed.
3. Report column names made more meaningful
4. Required validation on date range field added.
  • Loading branch information
tahiryasin committed Mar 13, 2023
1 parent acfa673 commit df15f32
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 573 deletions.
64 changes: 35 additions & 29 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
{
"name": "tahiryasin/bagisto-reports",
"name": "tahiryasin/bagisto-reports",
"types": "bagisto-module",
"description": "Sales, Inventory, Shipping, Refunds and Inventory reports for Bagisto opensource.",
"keywords": [
"bagisto",
"bagisto-reports"
],
"homepage": "https://github.com/tahiryasin/bagisto-reports",
"license": "MIT",
"authors": [
{
"name": "Tahir Yasin",
"email": "scriptbaker@gmail.com"
}
],
"require": {},
"autoload": {
"psr-4": {
"Tahiryasin\\Reports\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Tahiryasin\\Reports\\Providers\\ReportsServiceProvider"
],
"aliases": {}
}
},
"minimum-stability": "dev"
}
"version": "1.0.1",
"homepage": "https://github.com/tahiryasin/bagisto-reports",
"license": "MIT",
"bugs": {
"url": "https://github.com/tahiryasin/bagisto-reports/issues",
"email": "scriptbaker@gmail.com"
},
"author": {
"name": "Tahir Yasin",
"url": "https://github.com/tahiryasin",
"email": "scriptbaker@gmail.com"
},
"keywords": [
"bagisto",
"bagisto-reports"
],

"require": {},
"autoload": {
"psr-4": {
"Tahiryasin\\Reports\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Tahiryasin\\Reports\\Providers\\ReportsServiceProvider"
],
"aliases": {}
}
},
"minimum-stability": "dev"
}
21 changes: 14 additions & 7 deletions src/Tahiryasin/Reports/composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
{
"name": "tahiryasin/bagisto-reports",
"license": "MIT",
"authors": [
{
"name": "Tahir Yasin",
"email": "scriptbaker@gmail.com"
}
],
"types": "bagisto-module",
"description": "Sales, Inventory, Shipping, Refunds and Inventory reports for Bagisto opensource.",
"version": "1.0.1",
"homepage": "https://github.com/tahiryasin/bagisto-reports",
"license": "MIT",
"bugs": {
"url": "https://github.com/tahiryasin/bagisto-reports/issues",
"email": "scriptbaker@gmail.com"
},
"author": {
"name": "Tahir Yasin",
"url": "https://github.com/tahiryasin",
"email": "scriptbaker@gmail.com"
},
"require": {},
"autoload": {
"psr-4": {
Expand Down
107 changes: 16 additions & 91 deletions src/Tahiryasin/Reports/src/DataGrids/InventoryReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tahiryasin\Reports\DataGrids;

use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Webkul\Core\Models\Channel;
use Webkul\Core\Models\Locale;
Expand All @@ -23,12 +24,6 @@ class InventoryReport extends DataGrid
*/
protected $index = 'product_id';

/**
* If paginated then value of pagination.
*
* @var int
*/
protected $itemsPerPage = 10;

/**
* Locale.
Expand Down Expand Up @@ -78,32 +73,37 @@ public function __construct()
*/
public function prepareQueryBuilder()
{
$report_from = request()->post('report_from');
$report_to = request()->post('report_to');

if($report_from)
$report_from = Carbon::createFromFormat('Y-m-d', $report_to)->startOfDay();
if($report_to)
$report_to = Carbon::createFromFormat('Y-m-d', $report_to)->endOfDay();

$whereInChannels = [$this->channel];
$whereInLocales = [$this->locale];

/* query builder */
$queryBuilder = DB::table('product_flat')

->leftJoin('products', 'product_flat.product_id', '=', 'products.id')
->leftJoin('product_inventories', 'product_flat.product_id', '=', 'product_inventories.product_id')
->select(
'product_flat.product_id',
'products.sku as product_sku',
'product_flat.name as product_name',
// 'product_flat.status',
DB::raw('SUM(' . DB::getTablePrefix() . 'product_inventories.qty) as quantity')
DB::raw('CASE WHEN product_flat.name IS NOT NULL THEN product_flat.name ELSE "NULL" END as product_name'),
DB::raw('CASE WHEN product_inventories.qty IS NOT NULL THEN SUM(' . DB::getTablePrefix() . 'product_inventories.qty) ELSE 0 END as quantity')
);

if($report_from && $report_to)
$queryBuilder->whereBetween('product_flat.created_at', [$report_from, $report_to]);

$queryBuilder->groupBy('product_flat.product_id', 'product_flat.locale', 'product_flat.channel');

$queryBuilder->whereIn('product_flat.locale', $whereInLocales);
$queryBuilder->whereIn('product_flat.channel', $whereInChannels);

$this->addFilter('product_id', 'product_flat.product_id');
$this->addFilter('product_name', 'product_flat.name');
$this->addFilter('product_sku', 'products.sku');
$this->addFilter('product_number', 'product_flat.product_number');
// $this->addFilter('status', 'product_flat.status');
$this->addFilter('product_type', 'products.type');
$queryBuilder->where('product_flat.status', 1);

$this->setQueryBuilder($queryBuilder);
}
Expand All @@ -116,84 +116,9 @@ public function prepareQueryBuilder()
public function addColumns()
{

$this->addColumn([
'index' => 'product_sku',
'label' => trans('admin::app.datagrid.sku'),
'type' => 'string',
'searchable' => true,
'sortable' => true,
'filterable' => true,
]);



$this->addColumn([
'index' => 'product_name',
'label' => trans('admin::app.datagrid.name'),
'type' => 'string',
'searchable' => true,
'sortable' => true,
'filterable' => true,
'closure' => function ($row) {
if (! empty($row->url_key)) {
return "<a href='" . route('shop.productOrCategory.index', $row->url_key) . "' target='_blank'>" . $row->product_name . "</a>";
}

return $row->product_name;
},
]);

$this->addColumn([
'index' => 'quantity',
'label' => trans('admin::app.datagrid.qty'),
'type' => 'number',
'sortable' => true,
'searchable' => false,
'filterable' => false,
'closure' => function ($row) {
if (is_null($row->quantity)) {
return 0;
} else {
return $this->renderQuantityView($row);
}
},
]);
}

/**
* Prepare actions.
*
* @return void
*/
public function prepareActions()
{

}

/**
* Prepare mass actions.
*
* @return void
*/
public function prepareMassActions()
{

}

/**
* Render quantity view.
*
* @param object $row
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
*/
private function renderQuantityView($row)
{
$product = app(\Webkul\Product\Repositories\ProductRepository::class)->find($row->product_id);

$inventorySources = app(\Webkul\Inventory\Repositories\InventorySourceRepository::class)->findWhere(['status' => 1]);

$totalQuantity = $row->quantity;

return view('admin::catalog.products.datagrid.quantity', compact('product', 'inventorySources', 'totalQuantity'))->render();
}
}
79 changes: 8 additions & 71 deletions src/Tahiryasin/Reports/src/DataGrids/InvoiceReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tahiryasin\Reports\DataGrids;

use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Webkul\Ui\DataGrid\DataGrid;

Expand All @@ -12,7 +13,7 @@ class InvoiceReport extends DataGrid
*
* @var string
*/
protected $index = 'id';
protected $index = 'invoices.id';

/**
* Sort order.
Expand All @@ -32,17 +33,15 @@ public function prepareQueryBuilder()

$from = request()->post('report_from');
$to = request()->post('report_to');
$report_from = Carbon::createFromFormat('Y-m-d', $from)->startOfDay();
$report_to = Carbon::createFromFormat('Y-m-d', $to)->endOfDay();

$queryBuilder = DB::table('invoices')
->whereBetween('invoices.created_at', [$from, $to])
->whereBetween('invoices.created_at', [$report_from, $report_to])
->leftJoin('orders as ors', 'invoices.order_id', '=', 'ors.id')
->select('invoices.id as id', 'ors.increment_id as order_id', 'invoices.state as state', 'invoices.base_grand_total as base_grand_total', 'invoices.created_at as created_at')
->selectRaw("CASE WHEN {$dbPrefix}invoices.increment_id IS NOT NULL THEN {$dbPrefix}invoices.increment_id ELSE {$dbPrefix}invoices.id END AS increment_id");

$this->addFilter('increment_id', 'invoices.increment_id');
$this->addFilter('order_id', 'ors.increment_id');
$this->addFilter('base_grand_total', 'invoices.base_grand_total');
$this->addFilter('created_at', 'invoices.created_at');
->selectRaw("CASE WHEN {$dbPrefix}invoices.increment_id IS NOT NULL THEN {$dbPrefix}invoices.increment_id
ELSE {$dbPrefix}invoices.id END AS invoice_id,
invoices.state as state,invoices.total_qty as invoiced_qty, invoices.base_grand_total as invoice_total, invoices.created_at as invoice_date");

$this->setQueryBuilder($queryBuilder);
}
Expand All @@ -54,70 +53,8 @@ public function prepareQueryBuilder()
*/
public function addColumns()
{
$this->addColumn([
'index' => 'increment_id',
'label' => trans('admin::app.datagrid.id'),
'type' => 'string',
'searchable' => false,
'sortable' => true,
'filterable' => true,
]);

$this->addColumn([
'index' => 'order_id',
'label' => trans('admin::app.datagrid.order-id'),
'type' => 'string',
'searchable' => true,
'sortable' => true,
'filterable' => true,
]);

$this->addColumn([
'index' => 'created_at',
'label' => trans('admin::app.datagrid.invoice-date'),
'type' => 'datetime',
'searchable' => true,
'sortable' => true,
'filterable' => true,
]);

$this->addColumn([
'index' => 'base_grand_total',
'label' => trans('admin::app.datagrid.grand-total'),
'type' => 'price',
'searchable' => true,
'sortable' => true,
'filterable' => true,
]);

$this->addColumn([
'index' => 'state',
'label' => trans('admin::app.datagrid.status'),
'type' => 'string',
'sortable' => true,
'searchable' => true,
'filterable' => true,
'closure' => function ($value) {
if ($value->state == 'paid') {
return '<span class="badge badge-md badge-success">' . trans('admin::app.sales.invoices.status-paid') . '</span>';
} elseif ($value->state == 'pending' || $value->state == 'pending_payment') {
return '<span class="badge badge-md badge-warning">' . trans('admin::app.sales.invoices.status-pending') . '</span>';
} elseif ($value->state == 'overdue') {
return '<span class="badge badge-md badge-info">' . trans('admin::app.sales.invoices.status-overdue') . '</span>';
} else {
return $value->state;
}
},
]);
}

/**
* Prepare actions.
*
* @return void
*/
public function prepareActions()
{

}
}
Loading

0 comments on commit df15f32

Please sign in to comment.