Skip to content

Commit

Permalink
added invoice controller
Browse files Browse the repository at this point in the history
added order relations
fixed ticket bug
  • Loading branch information
A1Gard committed Sep 3, 2024
1 parent e9402ca commit 63b96c1
Show file tree
Hide file tree
Showing 13 changed files with 537 additions and 16 deletions.
148 changes: 148 additions & 0 deletions app/Http/Controllers/Admin/InvoiceController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Controllers\XController;
use App\Http\Requests\InvoiceSaveRequest;
use App\Models\Access;
use App\Models\Credit;
use App\Models\Customer;
use App\Models\Invoice;
use App\Models\Order;
use Illuminate\Http\Request;
use App\Helper;
use function App\Helpers\hasCreateRoute;

class InvoiceController extends XController
{

// protected $_MODEL_ = Invoice::class;
// protected $SAVE_REQUEST = InvoiceSaveRequest::class;

protected $cols = ['hash', 'customer_id', 'count', 'total_price', 'status'];
protected $extra_cols = ['id'];

protected $searchable = ['desc'];

protected $listView = 'admin.invoices.invoice-list';
protected $formView = 'admin.invoices.invoice-form';


protected $buttons = [
'edit' =>
['title' => "Edit", 'class' => 'btn-outline-primary', 'icon' => 'ri-edit-2-line'],
'show' =>
['title' => "Detail", 'class' => 'btn-outline-light', 'icon' => 'ri-eye-line'],
'destroy' =>
['title' => "Remove", 'class' => 'btn-outline-danger delete-confirm', 'icon' => 'ri-close-line'],
];


public function __construct()
{
parent::__construct(Invoice::class, InvoiceSaveRequest::class);
}

/**
* @param $invoice Invoice
* @param $request InvoiceSaveRequest
* @return Invoice
*/
public function save($invoice, $request)
{

$invoice->transport_id = $request->input('transport_id', null);
$invoice->address_id = $request->input('address_id', null);
$invoice->tracking_code = $request->tracking_code;
$invoice->status = $request->status;
$invoice->save();
return $invoice;

}


/**
* Show the form for creating a new resource.
*/
public function create()
{
//
return view($this->formView);
}

/**
* Show the form for editing the specified resource.
*/
public function edit(Invoice $item)
{
//
return view($this->formView, compact('item'));
}

public function bulk(Request $request)
{

// dd($request->all());
$data = explode('.', $request->input('action'));
$action = $data[0];
$ids = $request->input('id');
switch ($action) {
case 'delete':
$msg = __(':COUNT items deleted successfully', ['COUNT' => count($ids)]);
$this->_MODEL_::destroy($ids);
break;
/**restore*/
case 'restore':
$msg = __(':COUNT items restored successfully', ['COUNT' => count($ids)]);
foreach ($ids as $id) {
$this->_MODEL_::withTrashed()->find($id)->restore();
}
break;
/*restore**/
default:
$msg = __('Unknown bulk action : :ACTION', ["ACTION" => $action]);
}

return $this->do_bulk($msg, $action, $ids);
}

public function destroy(Invoice $item)
{
return parent::delete($item);
}


public function update(Request $request, Invoice $item)
{
return $this->bringUp($request, $item);
}

/**restore*/
public function restore($item)
{
return parent::restoreing(Invoice::withTrashed()->where('id', $item)->first());
}

public function removeOrder(Order $order)
{

$customer = Customer::whereId($order->invoice->customer_id)->first();
if ($order->price_total > 0) {
$diff = $order->price_total;
$customer->credit += $diff;
$customer->save();
$cr = new Credit();
$cr->customer_id = $customer->id;
$cr->amount = $diff;
$cr->data = json_encode([
'user_id' => auth()->user()->id,
'message' => __("Increase by Admin removed:") . ' ' . $order->product->name . __("Invoice") . ' : ' . $order->invoice->hash,
]);
$cr->save();
$order->delete();
}
return redirect()->back()->with('message', __('Order removed successfully'));
}
/*restore**/
}
40 changes: 40 additions & 0 deletions app/Http/Controllers/ClientController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Contracts\Payment;
use App\Http\Requests\ContactSubmitRequest;
use App\Models\Attachment;
use App\Models\Category;
Expand All @@ -11,6 +12,7 @@
use App\Models\Customer;
use App\Models\Gallery;
use App\Models\Group;
use App\Models\Invoice;
use App\Models\Post;
use App\Models\Product;
use App\Models\Quantity;
Expand Down Expand Up @@ -620,4 +622,42 @@ public function langIndex()
{
return $this->welcome();
}


public function pay($hash){

$invoice = Invoice::where('hash', $hash)->first();
// dd($invoice->created_at->timestamp , (time() - 3600));

if (!in_array($invoice->status, ['PENDING', 'CANCELED', 'FAILED'] ) || $invoice->created_at->timestamp < (time() - 3600) ){
return redirect()->back()->withErrors(__('This payment method is not available.'));
}
$activeGateway = config('xshop.payment.active_gateway');
/** @var Payment $gateway */
$gateway = app($activeGateway . '-gateway');
logger()->info('pay controller', ["active_gateway" => $activeGateway, "invoice" => $invoice->toArray(),]);

if ($invoice->isCompleted()) {
return redirect()->back()->with('message', __('Invoice payed.'));
}

$callbackUrl = route('pay.check', ['invoice_hash' => $invoice->hash, 'gateway' => $gateway->getName()]);
$payment = null;
try {
$response = $gateway->request((($invoice->total_price - $invoice->credit_price) * config('app.currency.factor')), $callbackUrl);
$payment = $invoice->storePaymentRequest($response['order_id'], (($invoice->total_price - $invoice->credit_price) * config('app.currency.factor')), $response['token'] ?? null, null, $gateway->getName());
session(["payment_id" => $payment->id]);
\Session::save();

return $gateway->goToBank();
} catch (\Throwable $exception) {
$invoice->status = 'FAILED';
$invoice->save();
\Log::error("Payment REQUEST exception: " . $exception->getMessage());
\Log::warning($exception->getTraceAsString());
$result = false;
$message = __('error in payment. contact admin.');
return redirect()->back()->withErrors($message);
}
}
}
31 changes: 31 additions & 0 deletions app/Http/Requests/InvoiceSaveRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class InvoiceSaveRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return auth()->check();
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'transport_id' => ['nullable', 'integer', 'exists:transports,id'],
'address_id' => ['nullable', 'integer', 'exists:addresses,id'],
'tracking_code' => ['nullable', 'string'],
'status' => ['required', 'string'],
];
}
}
5 changes: 3 additions & 2 deletions app/Models/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
use App\Events\InvoiceSucceed;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Invoice extends Model
{
use HasFactory;
use HasFactory,SoftDeletes;

const PENDING = 'PENDING';
const PROCESSING = 'PROCESSING';
Expand Down Expand Up @@ -112,7 +113,7 @@ public function storeSuccessPayment($paymentId, $referenceId, $cardNumber = null
$payment->status = "SUCCESS";
$payment->save();
/** @var \App\Models\Invoice $this */
$this->status = "COMPLETED";
$this->status = "PAID";
$this->save();
try {
event(new InvoiceSucceed($this, $payment));
Expand Down
12 changes: 12 additions & 0 deletions app/Models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,16 @@
class Order extends Model
{
use HasFactory;

public function product(){
return $this->belongsTo(Product::class);
}

public function quantity(){
return $this->belongsTo(Quantity::class);
}

public function invoice(){
return $this->belongsTo(Invoice::class);
}
}
2 changes: 1 addition & 1 deletion app/Models/Ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Ticket extends Model
{
// use HasFactory;
public static $ticket_statuses = [['PENDING','ANSWERED','CLOSED']];
public static $ticket_statuses = ['PENDING','ANSWERED','CLOSED'];


public function customer(){
Expand Down
11 changes: 9 additions & 2 deletions resources/sass/panel/_common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,23 @@ a.btn,a.action-btn,a.circle-btn{
margin: auto;
border-radius: 3px;
}
.status-0,.status-CLOSED {
.status-0,.status-CLOSED,.status-FAILED,.status-PROCESSING {
background: red;
}
.status-1,.status-ANSWERED{
.status-1,.status-ANSWERED,.status-COMPLETED{
background: lime;
}
.status-PENDING{
background: gold;
}

.status-CANCELED{
background: orange;
}

.status-PAID{
background: white;
}

.image-x64{
height: 64px;
Expand Down
Loading

0 comments on commit 63b96c1

Please sign in to comment.