Skip to content

Commit d39ee3e

Browse files
committed
Merge new features (see description)
Display prices with currency everywhere, improve Cart and Payment features
2 parents 9ce284d + d6b159d commit d39ee3e

File tree

67 files changed

+33770
-7996
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+33770
-7996
lines changed

.env.example

+3
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,6 @@ MAILING_SYSTEM=log
6565
MAILERLITE_API_KEY=
6666
ACTIVE_STUDENTS_MAILING_LIST_ID=
6767
INACTIVE_STUDENTS_MAILING_LIST_ID=
68+
69+
CURRENCY_SYMBOL=
70+
CURRENCY_POSITION=after

app/Console/Commands/importData.php

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Models\Course;
6+
use App\Models\Level;
7+
use App\Models\Period;
8+
use App\Models\Rhythm;
9+
use App\Models\Student;
10+
use App\Models\User;
11+
use App\Models\Year;
12+
use Carbon\Carbon;
13+
use Illuminate\Console\Command;
14+
use Illuminate\Support\Facades\Hash;
15+
use Illuminate\Support\Str;
16+
use League\Csv\Reader;
17+
18+
class importData extends Command
19+
{
20+
protected $signature = 'academico:import';
21+
22+
protected $description = 'Command description';
23+
24+
private function stripAccents($str) {
25+
return str_replace(' ', '', strtr(utf8_decode($str), utf8_decode('àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ'), 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY'));
26+
}
27+
28+
public function handle()
29+
{
30+
31+
$reader = Reader::createFromPath(\Storage::path('enrollments.csv'), 'r');
32+
$reader->setHeaderOffset(0);
33+
$records = $reader->getRecords();
34+
35+
foreach ($records as $offset => $record) {
36+
// if the course already exists, retrieve it
37+
$period = Period::firstWhere('name', $record['year']);
38+
39+
// Retrieve or create the student
40+
$email = $record['email'] !== "" ? $record['email'] : $this->stripAccents($record['firstname']) . "." . $this->stripAccents($record['name']) . "@academico.afsantiago.es";
41+
42+
if (User::where('email', $email)->count() > 0) {
43+
$user = User::where('email', $email)->first();
44+
if (($user->firstname !== trim($record['firstname']) || $user->lastname !== trim($record['name'])))
45+
{
46+
$email = $this->stripAccents($record['firstname']) . "." . $this->stripAccents($record['name']) . "@academico.afsantiago.es";
47+
}
48+
}
49+
50+
$user = User::firstOrCreate([
51+
'firstname' => trim($record['firstname']),
52+
'lastname' => trim($record['name']),
53+
'locale' => 'es',
54+
'email' => $email,
55+
], [
56+
'password' => Hash::make(Str::random(12)),
57+
]);
58+
59+
$student = Student::firstOrCreate(
60+
['id' => $user->id],
61+
[
62+
'idnumber' => $record['idnumber'] !== "" ? $record['idnumber'] : null,
63+
'address' => $record['address'] !== "" ? $record['address'] : null,
64+
'city' => $record['city'] !== "" ? $record['city'] : null,
65+
'birthdate' => $record['birthdate'] !== "" ? Carbon::createFromFormat("d/m/Y", $record['birthdate'])->toDateString() : null,
66+
'zip_code' => $record['zip_code'] !== "" ? $record['zip_code'] : null,
67+
'iban' => $record['IBAN'] !== "" ? $record['IBAN'] : null,
68+
'bic' => $record['bic_code'] !== "" ? $record['bic_code'] : null,
69+
]
70+
);
71+
72+
// add phone number
73+
if ($record['phone'] !== "" && $student->phone()->where('phone_number', $record['phone'])->count() == 0) {
74+
$student->phone()->create(['phone_number' => $record['phone']]);
75+
}
76+
77+
if ($period) {
78+
$course = Course::where('period_id', $period->id)->where('name', $record['course_name']);
79+
if ($course->exists()) {
80+
$course = $course->first();
81+
} // otherwise try and build a course matching the info we have
82+
else {
83+
// if the level name is contained in the course name, assign it.
84+
foreach (Level::all() as $level) {
85+
if (Str::contains($record['course_name'], $level->name)) {
86+
$levelID = $level->id;
87+
break;
88+
}
89+
}
90+
91+
// if the rhythm name is contained in the course name, assign it.
92+
foreach (Rhythm::all() as $rhythm) {
93+
if (Str::contains($record['course_name'], $rhythm->name)) {
94+
$rhythmID = $rhythm->id;
95+
break;
96+
}
97+
}
98+
99+
$course = Course::firstOrCreate([
100+
'campus_id' => 1,
101+
'rhythm_id' => $rhythmID ?? null,
102+
'level_id' => $levelID ?? null,
103+
'name' => $record['course_name'],
104+
'period_id' => $period->id,
105+
'start_date' => $period->start,
106+
'end_date' => $period->end,
107+
]);
108+
109+
unset($rhythmID);
110+
unset($levelID);
111+
}
112+
$student->enroll($course);
113+
}
114+
115+
}
116+
117+
118+
/*$reader = Reader::createFromPath(\Storage::path('years.csv'), 'r');
119+
$records = $reader->getRecords();
120+
121+
foreach ($records as $offset => $record) {
122+
Year::firstOrCreate([
123+
'name' => $record[0],
124+
]);
125+
}
126+
127+
foreach (Year::all() as $year) {
128+
Period::create([
129+
'name' => $year->name,
130+
'year_id' => $year->id,
131+
'start' => \Carbon\Carbon::parse('01/01/' . $year->name)->format('Y-m-d'),
132+
'end' => \Carbon\Carbon::parse('12/31/' . $year->name)->format('Y-m-d'),
133+
]);
134+
}*/
135+
}
136+
}

app/Http/Controllers/Admin/CourseCrudController.php

+47-6
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,40 @@ function () {
246246
CRUD::addClause('parent');
247247
}
248248
);
249+
250+
$this->crud->addFilter([
251+
'type' => 'date_range',
252+
'name' => 'start_date',
253+
'label' => __('Start')
254+
],
255+
false,
256+
function ($value) { // if the filter is active, apply these constraints
257+
$dates = json_decode($value);
258+
$this->crud->addClause('where', 'start_date', '>=', $dates->from);
259+
$this->crud->addClause('where', 'start_date', '<=', $dates->to . ' 23:59:59');
260+
});
261+
262+
$this->crud->addFilter([
263+
'type' => 'date_range',
264+
'name' => 'end_date',
265+
'label' => __('End')
266+
],
267+
false,
268+
function ($value) { // if the filter is active, apply these constraints
269+
$dates = json_decode($value);
270+
$this->crud->addClause('where', 'end_date', '>=', $dates->from);
271+
$this->crud->addClause('where', 'end_date', '<=', $dates->to . ' 23:59:59');
272+
});
249273
}
250274

251275
protected function setupCreateOperation()
252276
{
277+
if (config('app.currency_position' === 'before')) {
278+
$currency = array('prefix' => config('app.currency_symbol'));
279+
} else {
280+
$currency = array('suffix' => config('app.currency_symbol'));
281+
}
282+
253283
CRUD::addFields([
254284
[
255285
// RYTHM
@@ -279,11 +309,12 @@ protected function setupCreateOperation()
279309
'tab' => __('Course info'),
280310
],
281311

282-
[
312+
array_merge([
283313
'name' => 'price', // The db column name
284314
'label' => __('Price'), // Table column heading
285315
'tab' => __('Course info'),
286-
],
316+
'type' => 'number'
317+
], $currency),
287318

288319
[
289320
'name' => 'volume', // The db column name
@@ -332,10 +363,13 @@ protected function setupCreateOperation()
332363
'allows_null' => true,
333364
'wrapper' => ['class' => 'form-group col-md-4'],
334365
],
335-
[
366+
367+
array_merge([
336368
'name' => 'price', // The db column name
337369
'label' => __('Price'), // Table column heading
338-
],
370+
'type' => 'number'
371+
], $currency),
372+
339373
[
340374
'name' => 'volume', // The db column name
341375
'label' => __('Presential volume'), // Table column heading
@@ -554,6 +588,12 @@ protected function setupCreateOperation()
554588

555589
protected function setupUpdateOperation()
556590
{
591+
if (config('app.currency_position' === 'before')) {
592+
$currency = array('prefix' => config('app.currency_symbol'));
593+
} else {
594+
$currency = array('suffix' => config('app.currency_symbol'));
595+
}
596+
557597
if ($this->crud->getCurrentEntry()->children->count() > 0) {
558598
CRUD::addField([ // view
559599
'name' => 'custom-ajax-button',
@@ -602,11 +642,12 @@ protected function setupUpdateOperation()
602642
'tab' => __('Course info'),
603643
],
604644

605-
[
645+
array_merge([
606646
'name' => 'price', // The db column name
607647
'label' => __('Price'), // Table column heading
608648
'tab' => __('Course info'),
609-
],
649+
'type' => 'number'
650+
], $currency),
610651

611652
[
612653
'name' => 'volume', // The db column name

app/Http/Controllers/Admin/EnrollmentCrudController.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,10 @@ public function show($enrollment)
203203

204204
$enrollment->load('invoice')->load('invoice.payments');
205205

206+
$writeaccess = $enrollment->status_id !== 2 && backpack_user()->can('enrollments.edit');
207+
206208
// then load the page
207-
return view('enrollments.show', compact('enrollment', 'products', 'comments', 'scholarships', 'availablePaymentMethods'));
209+
return view('enrollments.show', compact('enrollment', 'products', 'comments', 'scholarships', 'availablePaymentMethods', 'writeaccess'));
208210
}
209211

210212
public function destroy($enrollment)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Admin;
4+
5+
use App\Http\Requests\PaymentRequest;
6+
use App\Models\Payment;
7+
use App\Models\Paymentmethod;
8+
use App\Models\Scholarship;
9+
use Backpack\CRUD\app\Http\Controllers\CrudController;
10+
use Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
11+
use Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;
12+
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
13+
use Illuminate\Support\Carbon;
14+
15+
/**
16+
* Class PaymentCrudController
17+
* @package App\Http\Controllers\Admin
18+
* @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud
19+
*/
20+
class PaymentCrudController extends CrudController
21+
{
22+
use ListOperation;
23+
use ShowOperation { show as traitShow; }
24+
25+
public function __construct()
26+
{
27+
parent::__construct();
28+
$this->middleware('permission:enrollments.edit');
29+
}
30+
31+
/**
32+
* Configure the CrudPanel object. Apply settings to all operations.
33+
*
34+
*/
35+
public function setup()
36+
{
37+
CRUD::setModel(\App\Models\Payment::class);
38+
CRUD::setRoute(config('backpack.base.route_prefix') . '/payment');
39+
CRUD::setEntityNameStrings('payment', 'payments');
40+
41+
$this->crud->enableExportButtons();
42+
43+
$this->crud->addButtonFromView('top', 'createInvoice', 'createInvoice', 'start');
44+
}
45+
46+
/**
47+
* Define what happens when the List operation is loaded.
48+
*/
49+
protected function setupListOperation()
50+
{
51+
$this->crud->addFilter([
52+
'type' => 'date',
53+
'name' => 'date',
54+
'label' => __('Due Date'),
55+
],
56+
false,
57+
function ($value) { // if the filter is active, apply these constraints
58+
$this->crud->addClause('where', 'date', '>=', Carbon::parse($value)->firstOfMonth());
59+
$this->crud->addClause('where', 'date', '<=', Carbon::parse($value)->lastOfMonth());
60+
});
61+
62+
CRUD::column('month');
63+
64+
if (config('app.currency_position' === 'before')) {
65+
$currency = array('prefix' => config('app.currency_symbol'));
66+
} else {
67+
$currency = array('suffix' => config('app.currency_symbol'));
68+
}
69+
70+
CRUD::addColumn([
71+
'name' => 'enrollment_name',
72+
'type' => 'attribute',
73+
'label' => __('Enrollment'),
74+
]);
75+
76+
CRUD::addColumn(array_merge([
77+
'name' => 'value',
78+
'label' => __('Value'),
79+
'type' => 'number'], $currency));
80+
81+
CRUD::addColumn([
82+
'name' => 'display_status',
83+
'type' => 'attribute',
84+
'label' => __('Status'),
85+
]);
86+
CRUD::addColumn([
87+
'name' => 'iban',
88+
'type' => 'attribute',
89+
'label' => 'IBAN',
90+
]);
91+
92+
CRUD::addColumn([
93+
'name' => 'bic',
94+
'type' => 'attribute',
95+
'label' => 'BIC',
96+
]);
97+
}
98+
99+
public function show($id)
100+
{
101+
$payment = Payment::findOrFail($id);
102+
103+
if (! backpack_user()->can('enrollments.edit')) {
104+
abort(403);
105+
}
106+
107+
if (! $payment->invoice || ! $payment->invoice->enrollment)
108+
{
109+
abort(404, 'No enrollment found for this payment');
110+
}
111+
112+
return view('enrollments.show', [
113+
'enrollment' => $payment->invoice->enrollment->load('invoice')->load('invoice.payments'),
114+
'products' => $payment->invoice()->with('invoiceDetails')->get(),
115+
'comments' => $payment->invoice->enrollment->comments,
116+
'scholarships' => Scholarship::all(),
117+
'availablePaymentMethods' => Paymentmethod::all(),
118+
'writeaccess' => $payment->invoice->enrollment->status_id !== 2 && backpack_user()->can('enrollments.edit'),
119+
]);
120+
}
121+
}

app/Http/Controllers/Admin/PaymentmethodCrudController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function setup()
2828
*/
2929
CRUD::setModel(Paymentmethod::class);
3030
CRUD::setRoute(config('backpack.base.route_prefix').'/paymentmethod');
31-
CRUD::setEntityNameStrings(__('payment method'), __('payment methods'));
31+
CRUD::setEntityNameStrings(__('Payment method'), __('Payment methods'));
3232
}
3333

3434
protected function setupListOperation()

0 commit comments

Comments
 (0)