![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/ts.corals.io/corals-api/Corals/modules/Timesheet/update-batches/ |
<?php use Corals\Modules\Timesheet\Models\Invoice; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; Schema::dropIfExists('timesheet_invoice_items'); Schema::create('timesheet_invoice_items', function (Blueprint $table) { $table->increments('id'); $table->string('type')->default('hours')->index(); $table->decimal('rate'); $table->decimal('quantity'); $table->decimal('amount'); $table->string('title'); $table->text('notes')->nullable(); $table->foreignId('invoice_id') ->constrained('timesheet_invoices') ->restrictOnDelete() ->cascadeOnUpdate(); $table->unsignedInteger('created_by')->nullable()->index(); $table->unsignedInteger('updated_by')->nullable()->index(); $table->text('properties')->nullable(); $table->timestamps(); }); Invoice::query()->eachById(function ($invoice) { $invoiceItems = []; if ($invoice->tax_total > 0.00) { if ($invoice->project_id) { $taxTitle = $invoice->project->name . ' taxes'; } else { $taxTitle = 'Sales tax'; } $invoiceItems[] = [ 'type' => 'tax', 'title' => $taxTitle, 'rate' => $invoice->tax_total, 'quantity' => 1, 'amount' => $invoice->tax_total ]; } if ($invoice->discount_total > 0.00) { $invoiceItems[] = [ 'type' => 'discount', 'title' => 'Discount', 'rate' => $invoice->discount_total, 'quantity' => 1, 'amount' => -1 * $invoice->discount_total ]; } if ($invoice->adjustment_total > 0.00) { $invoiceItems[] = [ 'type' => 'item', 'title' => 'Adjustment', 'notes' => $invoice->notes, 'rate' => $invoice->adjustment_total, 'quantity' => 1, 'amount' => $invoice->adjustment_total ]; } if ($invoice->subtotal) { if ($invoice->project_id) { $project = $invoice->project; $invoiceItems[] = [ 'type' => 'hours', 'title' => $project->name ?: 'Entries Hours', 'rate' => round($invoice->subtotal / $invoice->getProperty('total_hours')), 'quantity' => $invoice->getProperty('total_hours'), 'amount' => $invoice->subtotal ]; } else { $projectIds = $invoice->entries()->groupBy('project_id')->pluck('project_id')->toArray(); foreach ($projectIds as $projectId) { $project = \Corals\Modules\Timesheet\Models\Project::query()->find($projectId); $entriesAmount = \Corals\Modules\Timesheet\Models\Entry::query() ->where('project_id', $projectId) ->where('invoice_id', $invoice->id) ->sum('amount'); $entriesHours = \Corals\Modules\Timesheet\Models\Entry::query() ->where('project_id', $projectId) ->where('invoice_id', $invoice->id) ->selectRaw('(SUM(ifNull(timesheet_entries.evaluation_hours * 60,0)) + SUM(ifNull(timesheet_entries.evaluation_minutes,0)))/60 as total_evaluation_hours') ->groupBy('project_id') ->first(); $invoiceItems[] = [ 'type' => 'hours', 'title' => $project->name ?: 'Entries Hours', 'rate' => round($invoice->subtotal / $invoice->getProperty('total_hours')), 'quantity' => $entriesHours->total_evaluation_hours, 'amount' => $entriesAmount ]; } } } $invoice->items()->createMany($invoiceItems); $newSubtotal = $invoice->items() ->whereNotIn('type', ['discount', 'tax']) ->sum('amount'); $invoice->update([ 'subtotal' => $newSubtotal, 'notes' => null, ]); }); Schema::table('timesheet_invoices', function (Blueprint $table) { $table->dropColumn('adjustment_total'); });