| Server IP : 14.225.204.176 / Your IP : 216.73.216.252 Web Server : nginx/1.24.0 System : Linux nodejs-ybgk 6.8.0-40-generic #40-Ubuntu SMP PREEMPT_DYNAMIC Fri Jul 5 10:34:03 UTC 2024 x86_64 User : root ( 0) PHP Version : 8.1.34 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/html/php/thanhphuong/app/Http/Controllers/ |
Upload File : |
<?php
namespace App\Http\Controllers;
use App\Currency;
use App\Product;
use App\ProductDiscount;
use App\Unit;
use App\Warehouse;
use App\Discount;
use Auth;
use Carbon\Carbon;
use DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Keygen\Keygen;
use Spatie\Permission\Models\Role;
class DiscountController extends Controller
{
public function index()
{
$role = Role::find(Auth::user()->role_id);
if ($role->hasPermissionTo('discounts-index')) {
$lims_discount_list = Discount::orderBy('id', 'desc')->get();
$permissions = Role::findByName($role->name)->permissions;
foreach ($permissions as $permission) {
$all_permission[] = $permission->name;
}
if (empty($all_permission)) {
$all_permission[] = 'dummy text';
}
return view('discount.index', compact('lims_discount_list', 'all_permission'));
} else {
return redirect()->back()->with('not_permitted', 'Sorry! You are not allowed to access this module');
}
}
public function discountData(Request $request)
{
$columns = array(
1 => 'discount_date',
2 => 'discount_no',
3 => 'name',
4 => 'currency',
5 => 'discount_percent',
6 => 'amount',
7 => 'discount_amount'
);
$start = $request->input('start');
$query = Discount::where('discounts.is_active', true)->withCount('products');
$dir = $request->input('order.0.dir');
$orderKey = $columns[$request->input('order.0.column')] ?? '';
$order = '';
switch ($orderKey) {
case 'discount_amount':
case 'amount':
break;
default:
$order = !empty($orderKey) ? 'discounts.' . $orderKey : '';
break;
}
if (!empty($request->input('search.value'))) {
$search = $request->input('search.value');
$query->where('discounts.name', 'LIKE', "%{$search}%")
->orWhere('discounts.discount_no', 'LIKE', "%{$search}%")
->orWhere('discounts.discount_date', 'LIKE', "%{$search}%");
}
$totalData = $totalFiltered = $query->count();
if ($request->input('length') != -1) {
$query->limit($request->input('length'));
}
$discounts = (!empty($order) ? $query->offset($start)->orderBy($order, $dir) : $query->offset($start))
->get();
if ($orderKey == 'amount' || $orderKey == 'discount_amount') {
$discounts = $dir == 'asc' ? $discounts->sortBy($orderKey) : $discounts->sortByDesc($orderKey);
}
$data = [];
if (!empty($discounts)) {
foreach ($discounts as $key => $discount) {
$products = $discount->products;
$nestedData['id'] = $discount->id;
$nestedData['key'] = $key;
$nestedData['discount_date'] = $discount->discount_date;
$nestedData['code'] = $discount->discount_no;
$nestedData['name'] = $discount->name;
$nestedData['product'] = $discount->products_count;
$nestedData['currency'] = $discount->currency->code;
$nestedData['amount'] = number_format($discount->amount) . $discount->currency->code;
$nestedData['discount_total'] = number_format($products->sum('pivot.discount_total')) . $discount->currency->code;
$nestedData['options'] = '<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' . trans("file.action") . '
<span class="caret"></span>
</button>
<ul class="dropdown-menu edit-options dropdown-menu-right dropdown-default" user="menu">';
if (in_array("discounts-edit", $request['all_permission'])) {
$nestedData['options'] .= '<li>
<a href="' . route('discounts.edit',
$discount->id) . '" class="btn btn-link"><i class="fa fa-edit"></i> ' . trans('file.edit') . '</a>
</li>';
}
if (in_array("discounts-delete", $request['all_permission'])) {
$nestedData['options'] .= \Form::open([
"route" => ["discounts.destroy", $discount->id],
"method" => "POST"
]) . '
<li>
<button type="submit" class="btn btn-link" onclick="return confirmDelete()"><i class="fa fa-trash"></i> ' . trans("file.delete") . '</button>
</li>' . \Form::close() . '
</ul>
</div>';
}
$data[] = $nestedData;
}
}
$json_data = array(
"draw" => intval($request->input('draw')),
"recordsTotal" => intval($totalData),
"recordsFiltered" => intval($totalFiltered),
"data" => $data
);
echo json_encode($json_data);
}
public function create(Request $request)
{
$lims_product_list = Product::select('code', 'unit_id', DB::raw("CONCAT(code, ' (', name, ')') as name"))->where([ ['is_active', true], ['type', 'standard'] ])->get();
$lims_currency_list = Currency::all();
return view('discount.create',
compact(
'lims_product_list',
'lims_currency_list'
)
);
}
public function store(Request $request)
{
$discountId = $request->input('id');
// Edit
if (!empty($discountId)) {
DB::transaction(function() use ($request, $discountId) {
$discount = Discount::findOrFail($discountId);
// Update Discount Info
$discount->update($request->only('id', 'name', 'currency_id', 'discount_date'));
// Update Product Discount
$productDiscounts = [];
foreach ($request->input("product") as $id => $data) {
$productDiscounts[] = [
'product_id' => $id,
'discount_id' => $discountId,
'discount_price' => $data['discount_price'],
'discount_qty' => $data['discount_qty'],
'discount_percent' => $data['discount_percent'],
'discount_total' => $data['discount_total'],
'contract_number' => $data['contract_number'],
'invoice_number' => $data['invoice_number'],
'invoice_date' => $data['invoice_date'],
'tr_number' => $data['tr_number'],
'updated_at' => Carbon::now()
];
}
// Remove old data
$discount->products()->detach();
// Insert new data
ProductDiscount::insert($productDiscounts);
});
return back()->with('success', 'Discount updated successfully');
} else {
// Create
$success = false;
DB::transaction(function () use ($request, &$success) {
// Create Discount
$discount = Discount::create([
'discount_no' => Keygen::numeric(7)->generate(),
'name' => $request->input('name'),
'currency_id' => $request->input('currency_id'),
'discount_date' => $request->input('discount_date'),
"is_active" => true
]);
// Create Product Discount
$productDiscounts = [];
$now = Carbon::now();
foreach ($request->input("product") as $id => $data) {
$productDiscounts[] = [
'product_id' => $id,
'discount_id' => $discount->id,
'discount_price' => $data['discount_price'],
'discount_qty' => $data['discount_qty'],
'discount_percent' => $data['discount_percent'],
'discount_total' => $data['discount_total'],
'contract_number' => $data['contract_number'],
'invoice_number' => $data['invoice_number'],
'invoice_date' => $data['invoice_date'],
'tr_number' => $data['tr_number'],
'created_at' => $now,
'updated_at' => $now
];
}
$success = ProductDiscount::insert($productDiscounts);
});
if ($success) {
return Redirect::route('discounts.index')->with('success', 'Discount created successfully');
}
return Redirect::route('discounts.create')->with('error', 'Cannot create discount');
}
}
public function edit($id)
{
$role = Role::firstOrCreate(['id' => Auth::user()->role_id]);
if ($role->hasPermissionTo('discounts-edit')) {
$lims_currency_list = Currency::all();
$lims_product_list = Product::select('code', 'unit_id', DB::raw("CONCAT(code, ' (', name, ')') as name"))->where([ ['is_active', true], ['type', 'standard'] ])->get();
$lims_discount_data = Discount::where([
['id', $id],
['is_active', true]
])->firstOrFail();
return view('discount.edit',
compact(
'lims_product_list',
'lims_discount_data',
'lims_currency_list'
)
);
} else {
return redirect()->back()->with('not_permitted', 'Sorry! You are not allowed to access this module');
}
}
public function destroy($id)
{
$role = Role::firstOrCreate(['id' => Auth::user()->role_id]);
if (!$role->hasPermissionTo('discounts-delete')) {
return redirect()->back()->with('not_permitted', 'Sorry! You are not allowed to access this module');
}
Discount::findOrFail($id)->update(['is_active' => false]);
return back()->with('success', 'Discount deleted successfully');
}
public function destroyBySelection(Request $request)
{
$role = Role::firstOrCreate(['id' => Auth::user()->role_id]);
if (!$role->hasPermissionTo('discounts-delete')) {
Session::flash('not_permitted', 'Sorry! You are not allowed to access this module');
return response()->json(['error' => 'Sorry! You are not allowed to access this module'], 500);
}
Discount::whereIn('id', $request->input('ids'))->update(['is_active' => false]);
Session::flash('success', 'Discount deleted successfully');
return response()->json(['success' => 'Discount deleted successfully']);
}
}