diff --git a/app/Http/Controllers/Admin/InvoiceController.php b/app/Http/Controllers/Admin/InvoiceController.php new file mode 100644 index 0000000..051b095 --- /dev/null +++ b/app/Http/Controllers/Admin/InvoiceController.php @@ -0,0 +1,148 @@ + + ['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**/ +} diff --git a/app/Http/Controllers/ClientController.php b/app/Http/Controllers/ClientController.php index bfac00a..679a4ec 100644 --- a/app/Http/Controllers/ClientController.php +++ b/app/Http/Controllers/ClientController.php @@ -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; @@ -11,6 +12,7 @@ use App\Models\Contact; 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; @@ -620,4 +622,42 @@ class ClientController extends Controller { 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); + } + } } diff --git a/app/Http/Requests/InvoiceSaveRequest.php b/app/Http/Requests/InvoiceSaveRequest.php new file mode 100644 index 0000000..c42619e --- /dev/null +++ b/app/Http/Requests/InvoiceSaveRequest.php @@ -0,0 +1,31 @@ +check(); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array|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'], + ]; + } +} diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 78411fc..cb8ca30 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -6,10 +6,11 @@ use App\Events\InvoiceFailed; 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'; @@ -112,7 +113,7 @@ class Invoice extends Model $payment->status = "SUCCESS"; $payment->save(); /** @var \App\Models\Invoice $this */ - $this->status = "COMPLETED"; + $this->status = "PAID"; $this->save(); try { event(new InvoiceSucceed($this, $payment)); diff --git a/app/Models/Order.php b/app/Models/Order.php index 70b5c62..a811922 100644 --- a/app/Models/Order.php +++ b/app/Models/Order.php @@ -8,4 +8,16 @@ use Illuminate\Database\Eloquent\Model; 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); + } } diff --git a/app/Models/Ticket.php b/app/Models/Ticket.php index 64968b0..e68be4e 100644 --- a/app/Models/Ticket.php +++ b/app/Models/Ticket.php @@ -8,7 +8,7 @@ use Illuminate\Database\Eloquent\Model; class Ticket extends Model { // use HasFactory; - public static $ticket_statuses = [['PENDING','ANSWERED','CLOSED']]; + public static $ticket_statuses = ['PENDING','ANSWERED','CLOSED']; public function customer(){ diff --git a/resources/sass/panel/_common.scss b/resources/sass/panel/_common.scss index 9b46b4b..95e8cf7 100644 --- a/resources/sass/panel/_common.scss +++ b/resources/sass/panel/_common.scss @@ -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; diff --git a/resources/views/admin/invoices/invoice-form.blade.php b/resources/views/admin/invoices/invoice-form.blade.php new file mode 100644 index 0000000..b21cdaa --- /dev/null +++ b/resources/views/admin/invoices/invoice-form.blade.php @@ -0,0 +1,229 @@ +@extends('admin.templates.panel-form-template') +@section('title') + @if(isset($item)) + {{__("Edit invoice")}} [{{$item->id}}] + @else + {{__("Add new invoice")}} + @endif - +@endsection +@section('form') + +
+
+ + @include('components.err') +
+

+ + {{__("Tips")}} +

+
    +
  • + {{__("If you cancel this, You must increase credit yourself.")}} +
  • +
  • + {{__("If you change transport method you must think about think about the price diffrance")}} +
  • +
  • + {{__("If you removed order from invoice, system adding amount to customer's credit automatically")}} +
  • +
+
+ + + @if( $item->desc != null && trim($item->desc) != '') +
+

+ + {{__("Description")}} +

+

+ {{$item->desc}} +

+
+ @endif + +
+
+
+ +

+ @if(isset($item)) + {{__("Edit invoice")}} [{{$item->id}}] + @else + {{__("Add new invoice")}} + @endif +

+ +
+
+
+ + +
+
+
+
+ + status??null)}}' + :close-on-Select="true"> +
+
+
+
+ {{__("Address")}} +
+
    + @foreach($item->customer->addresses as $adr) +
  • + +
  • + @endforeach +
+
+
+
+ {{__("Address")}} +
+
    + @foreach(\App\Models\Transport::all() as $t) +
  • + +
  • + @endforeach +
+
+
+ + +
+ +
+
+
+ +
+ + + + + + + + + + @foreach($item->orders as $k => $order) + + + + + + + + + @endforeach + + + + + + + + +
+ # + + {{__("Product")}} + + {{__("Count")}} + + {{__("Quantity")}} + + {{__("Price")}} + + - +
+ {{$k + 1}} + + {{$order->product->name}} + + {{number_format($order->count)}} + + @if( ($order->quantity->meta??null) == null) + - + @else + @foreach($order->quantity->meta as $m) + + {{$m->human_value}} + + @endforeach + @endif + + {{number_format($order->price_total)}} + + + + +
+ - + + {{__("Transport")}} + {{number_format($item->transport_price)}} + + {{__("Total price")}} + {{number_format($item->total_price)}} + + {{__("Orders count")}}: ({{number_format($item->count)}}) +
+
+
+@endsection diff --git a/resources/views/admin/invoices/invoice-list.blade.php b/resources/views/admin/invoices/invoice-list.blade.php new file mode 100644 index 0000000..c09e7a5 --- /dev/null +++ b/resources/views/admin/invoices/invoice-list.blade.php @@ -0,0 +1,25 @@ +@extends('admin.templates.panel-list-template') + +@section('list-title') + + {{__("Invoices list")}} +@endsection +@section('title') + {{__("Invoices list")}} - +@endsection +@section('filter') +

+ + {{__("Status")}}: +

+ +@endsection +@section('bulk') + {{-- --}} +@endsection diff --git a/resources/views/admin/templates/panel-list-template.blade.php b/resources/views/admin/templates/panel-list-template.blade.php index 16fcf3f..9966067 100644 --- a/resources/views/admin/templates/panel-list-template.blade.php +++ b/resources/views/admin/templates/panel-list-template.blade.php @@ -178,7 +178,10 @@ {{ $item->parent?->{$cols[0]}??'-' }} @break @case('status') -
+
@break @case('user_id') diff --git a/resources/views/admin/tickets/ticket-list.blade.php b/resources/views/admin/tickets/ticket-list.blade.php index 678aef5..89181e1 100644 --- a/resources/views/admin/tickets/ticket-list.blade.php +++ b/resources/views/admin/tickets/ticket-list.blade.php @@ -8,7 +8,17 @@ {{__("Tickets list")}} - @endsection @section('filter') - {{-- Other filters --}} +

+ + {{__("Status")}}: +

+ @endsection @section('bulk') diff --git a/resources/views/components/panel-side-navbar.blade.php b/resources/views/components/panel-side-navbar.blade.php index add1b9a..ed70c63 100644 --- a/resources/views/components/panel-side-navbar.blade.php +++ b/resources/views/components/panel-side-navbar.blade.php @@ -13,6 +13,14 @@