|
@@ -9,32 +9,48 @@ use DateTime;
|
|
|
|
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
|
-
|
|
|
|
|
|
|
+use Illuminate\Support\Facades\Storage;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
+use Illuminate\Support\Facades\Mail;
|
|
|
|
|
+use App\Mail\ExportNotification;
|
|
|
|
|
+use App\Jobs\ExportPrimaNota;
|
|
|
|
|
|
|
|
class Record extends Component
|
|
class Record extends Component
|
|
|
{
|
|
{
|
|
|
public $records, $dataId, $totals;
|
|
public $records, $dataId, $totals;
|
|
|
-
|
|
|
|
|
public $in;
|
|
public $in;
|
|
|
public $out;
|
|
public $out;
|
|
|
-
|
|
|
|
|
public $payments = [];
|
|
public $payments = [];
|
|
|
-
|
|
|
|
|
public $fromDate;
|
|
public $fromDate;
|
|
|
public $toDate;
|
|
public $toDate;
|
|
|
-
|
|
|
|
|
|
|
+ public $appliedFromDate;
|
|
|
|
|
+ public $appliedToDate;
|
|
|
|
|
+ public $exportFromDate;
|
|
|
|
|
+ public $exportToDate;
|
|
|
|
|
+ public $isExporting = false;
|
|
|
|
|
+ public $selectedPeriod = 'OGGI';
|
|
|
public $filterCausals = null;
|
|
public $filterCausals = null;
|
|
|
public $filterMember = null;
|
|
public $filterMember = null;
|
|
|
-
|
|
|
|
|
|
|
+ public $isFiltering = false;
|
|
|
public array $recordDatas = [];
|
|
public array $recordDatas = [];
|
|
|
public array $labels = [];
|
|
public array $labels = [];
|
|
|
-
|
|
|
|
|
public array $causals = [];
|
|
public array $causals = [];
|
|
|
public $members = array();
|
|
public $members = array();
|
|
|
- public $filterSupplier = null;
|
|
|
|
|
-
|
|
|
|
|
- public $suppliers = array();
|
|
|
|
|
|
|
+ public $sendViaEmail = false;
|
|
|
|
|
+ public $exportEmailAddress = '';
|
|
|
|
|
+ public $exportEmailSubject = 'Prima Nota - Export';
|
|
|
|
|
+
|
|
|
|
|
+ protected $rules = [
|
|
|
|
|
+ 'exportEmailAddress' => 'required_if:sendViaEmail,true|email',
|
|
|
|
|
+ 'exportEmailSubject' => 'required_if:sendViaEmail,true|string|max:255',
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ protected $messages = [
|
|
|
|
|
+ 'exportEmailAddress.required_if' => 'L\'indirizzo email è obbligatorio quando si sceglie di inviare via email.',
|
|
|
|
|
+ 'exportEmailAddress.email' => 'Inserisci un indirizzo email valido.',
|
|
|
|
|
+ 'exportEmailSubject.required_if' => 'L\'oggetto dell\'email è obbligatorio.',
|
|
|
|
|
+ 'exportEmailSubject.max' => 'L\'oggetto dell\'email non può superare i 255 caratteri.',
|
|
|
|
|
+ ];
|
|
|
|
|
|
|
|
public function hydrate()
|
|
public function hydrate()
|
|
|
{
|
|
{
|
|
@@ -43,19 +59,325 @@ class Record extends Component
|
|
|
|
|
|
|
|
public function mount()
|
|
public function mount()
|
|
|
{
|
|
{
|
|
|
-
|
|
|
|
|
$this->fromDate = date("Y-m-d");
|
|
$this->fromDate = date("Y-m-d");
|
|
|
$this->toDate = date("Y-m-d");
|
|
$this->toDate = date("Y-m-d");
|
|
|
|
|
|
|
|
|
|
+ $this->appliedFromDate = date("Y-m-d");
|
|
|
|
|
+ $this->appliedToDate = date("Y-m-d");
|
|
|
|
|
+
|
|
|
|
|
+ $this->exportFromDate = date("Y-m-d");
|
|
|
|
|
+ $this->exportToDate = date("Y-m-d");
|
|
|
|
|
+
|
|
|
|
|
+ $this->exportEmailSubject = 'Prima Nota - Export del ' . date('d/m/Y');
|
|
|
|
|
+
|
|
|
|
|
|
|
|
$this->getCausals(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->get(), 0);
|
|
$this->getCausals(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->get(), 0);
|
|
|
- $this->suppliers = \App\Models\Supplier::select(['id', 'name'])->orderBy('name')->get();
|
|
|
|
|
- Log::info($this->suppliers);
|
|
|
|
|
|
|
+
|
|
|
$this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->orderBy('last_name')->orderBy('first_name')->get();
|
|
$this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->orderBy('last_name')->orderBy('first_name')->get();
|
|
|
|
|
|
|
|
$this->payments = \App\Models\PaymentMethod::select('id', 'name', 'type')->where('enabled', true)->where('money', false)->get();
|
|
$this->payments = \App\Models\PaymentMethod::select('id', 'name', 'type')->where('enabled', true)->where('money', false)->get();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+ private function generateExportData($fromDate, $toDate)
|
|
|
|
|
+ {
|
|
|
|
|
+ $exportRecords = array();
|
|
|
|
|
+ $exportTotals = array();
|
|
|
|
|
+
|
|
|
|
|
+ $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
|
|
|
|
|
+
|
|
|
|
|
+ $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
|
|
|
|
|
+ ->select(
|
|
|
|
|
+ 'records.*',
|
|
|
|
|
+ 'records_rows.id as row_id',
|
|
|
|
|
+ 'records_rows.record_id',
|
|
|
|
|
+ 'records_rows.causal_id',
|
|
|
|
|
+ 'records_rows.amount',
|
|
|
|
|
+ 'records_rows.note',
|
|
|
|
|
+ 'records_rows.when',
|
|
|
|
|
+ 'records_rows.vat_id',
|
|
|
|
|
+ 'records_rows.created_at as row_created_at',
|
|
|
|
|
+ 'records_rows.updated_at as row_updated_at'
|
|
|
|
|
+ )
|
|
|
|
|
+ ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
|
|
|
|
|
+ ->whereBetween('date', [$fromDate, $toDate])
|
|
|
|
|
+ ->where(function ($query) {
|
|
|
|
|
+ $query->where('type', 'OUT')
|
|
|
|
|
+ ->orWhere(function ($query) {
|
|
|
|
|
+ $query->where('records.corrispettivo_fiscale', true)
|
|
|
|
|
+ ->orWhere('records.commercial', false);
|
|
|
|
|
+ });
|
|
|
|
|
+ })
|
|
|
|
|
+ ->where(function ($query) use ($exclude_from_records) {
|
|
|
|
|
+ $query->where('type', 'OUT')
|
|
|
|
|
+ ->orWhere(function ($subquery) use ($exclude_from_records) {
|
|
|
|
|
+ $subquery->whereNotIn('member_id', $exclude_from_records);
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ if ($this->filterCausals != null && sizeof($this->filterCausals) > 0) {
|
|
|
|
|
+ $causals = array();
|
|
|
|
|
+ foreach ($this->filterCausals as $z) {
|
|
|
|
|
+ $causals[] = $z;
|
|
|
|
|
+ $childs = \App\Models\Causal::where('parent_id', $z)->get();
|
|
|
|
|
+ foreach ($childs as $c) {
|
|
|
|
|
+ $causals[] = $c->id;
|
|
|
|
|
+ $childsX = \App\Models\Causal::where('parent_id', $c->id)->get();
|
|
|
|
|
+ foreach ($childsX as $cX) {
|
|
|
|
|
+ $causals[] = $cX->id;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ $datas->whereIn('causal_id', $causals);
|
|
|
|
|
+ }
|
|
|
|
|
+ if ($this->filterMember != null && $this->filterMember > 0) {
|
|
|
|
|
+ $datas->where('member_id', $this->filterMember);
|
|
|
|
|
+ }
|
|
|
|
|
+ $datas = $datas->orderBy('date', 'ASC')
|
|
|
|
|
+ ->orderBy('records.created_at', 'ASC')
|
|
|
|
|
+ ->orderBy('records_rows.id', 'ASC')
|
|
|
|
|
+ ->get();
|
|
|
|
|
+
|
|
|
|
|
+ $groupedData = [];
|
|
|
|
|
+ $causalsCount = [];
|
|
|
|
|
+
|
|
|
|
|
+ foreach ($datas as $idx => $data) {
|
|
|
|
|
+ $causalCheck = \App\Models\Causal::findOrFail($data->causal_id);
|
|
|
|
|
+ $paymentCheck = $data->payment_method->money;
|
|
|
|
|
+
|
|
|
|
|
+ if (!$paymentCheck && ($causalCheck->no_first == null || !$causalCheck->no_first)) {
|
|
|
|
|
+ if (!$data->deleted) {
|
|
|
|
|
+ $amount = $data->amount;
|
|
|
|
|
+ $amount += getVatValue($amount, $data->vat_id);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $amount = $data->amount;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $isCommercial = ($data->commercial == 1 || $data->commercial === '1' || $data->commercial === true);
|
|
|
|
|
+ $typeLabel = $isCommercial ? 'Commerciale' : 'Non Commerciale';
|
|
|
|
|
+
|
|
|
|
|
+ $nominativo = '';
|
|
|
|
|
+ if ($data->type == "IN") {
|
|
|
|
|
+ if ($data->member) {
|
|
|
|
|
+ $nominativo = $data->member->last_name . " " . $data->member->first_name;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ if ($data->supplier) {
|
|
|
|
|
+ $nominativo = $data->supplier->name;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $groupKey = $data->date . '|' . $typeLabel . '|' . $data->payment_method->name . '|' . $data->type . '|' . $nominativo;
|
|
|
|
|
+
|
|
|
|
|
+ if (!isset($groupedData[$groupKey])) {
|
|
|
|
|
+ $groupedData[$groupKey] = [
|
|
|
|
|
+ 'date' => $data->date,
|
|
|
|
|
+ 'type_label' => $typeLabel,
|
|
|
|
|
+ 'payment_method' => $data->payment_method->name,
|
|
|
|
|
+ 'transaction_type' => $data->type,
|
|
|
|
|
+ 'nominativo' => $nominativo,
|
|
|
|
|
+ 'amount' => 0,
|
|
|
|
|
+ 'deleted' => false,
|
|
|
|
|
+ 'causals' => [],
|
|
|
|
|
+ 'notes' => []
|
|
|
|
|
+ ];
|
|
|
|
|
+ $causalsCount[$groupKey] = [];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $groupedData[$groupKey]['amount'] += $amount;
|
|
|
|
|
+ $causalsCount[$groupKey][$causalCheck->getTree()] = true;
|
|
|
|
|
+
|
|
|
|
|
+ if (!empty($data->note)) {
|
|
|
|
|
+ $groupedData[$groupKey]['notes'][] = $data->note;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($data->deleted) {
|
|
|
|
|
+ $groupedData[$groupKey]['deleted'] = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ foreach ($groupedData as $groupKey => $group) {
|
|
|
|
|
+ $causalsInGroup = array_keys($causalsCount[$groupKey]);
|
|
|
|
|
+
|
|
|
|
|
+ $causalDisplay = $group['type_label'];
|
|
|
|
|
+
|
|
|
|
|
+ if (count($causalsInGroup) > 1) {
|
|
|
|
|
+ $detailDisplay = 'Varie|' . implode('|', $causalsInGroup);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $detailDisplay = $causalsInGroup[0];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $recordKey = $group['date'] . "§" . $causalDisplay . "§" . $group['nominativo'] . "§" . $detailDisplay . "§" . ($group['deleted'] ? 'DELETED' : '') . "§";
|
|
|
|
|
+
|
|
|
|
|
+ if (!isset($exportRecords[$recordKey][$group['payment_method']][$group['transaction_type']])) {
|
|
|
|
|
+ $exportRecords[$recordKey][$group['payment_method']][$group['transaction_type']] = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $exportRecords[$recordKey][$group['payment_method']][$group['transaction_type']] += $group['amount'];
|
|
|
|
|
+
|
|
|
|
|
+ if (!isset($exportTotals[$group['payment_method']])) {
|
|
|
|
|
+ $exportTotals[$group['payment_method']]["IN"] = 0;
|
|
|
|
|
+ $exportTotals[$group['payment_method']]["OUT"] = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!$group['deleted'])
|
|
|
|
|
+ $exportTotals[$group['payment_method']][$group['transaction_type']] += $group['amount'];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return $exportRecords;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function generateExportTotals($fromDate, $toDate)
|
|
|
|
|
+ {
|
|
|
|
|
+ $exportTotals = array();
|
|
|
|
|
+
|
|
|
|
|
+ $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
|
|
|
|
|
+
|
|
|
|
|
+ $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
|
|
|
|
|
+ ->select(
|
|
|
|
|
+ 'records.*',
|
|
|
|
|
+ 'records_rows.id as row_id',
|
|
|
|
|
+ 'records_rows.record_id',
|
|
|
|
|
+ 'records_rows.causal_id',
|
|
|
|
|
+ 'records_rows.amount',
|
|
|
|
|
+ 'records_rows.note',
|
|
|
|
|
+ 'records_rows.when',
|
|
|
|
|
+ 'records_rows.vat_id',
|
|
|
|
|
+ )
|
|
|
|
|
+ ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
|
|
|
|
|
+ ->whereBetween('date', [$fromDate, $toDate])
|
|
|
|
|
+ ->where(function ($query) {
|
|
|
|
|
+ $query->where('type', 'OUT')
|
|
|
|
|
+ ->orWhere(function ($query) {
|
|
|
|
|
+ $query->where('records.corrispettivo_fiscale', true)
|
|
|
|
|
+ ->orWhere('records.commercial', false);
|
|
|
|
|
+ });
|
|
|
|
|
+ })
|
|
|
|
|
+ ->where(function ($query) use ($exclude_from_records) {
|
|
|
|
|
+ $query->where('type', 'OUT')
|
|
|
|
|
+ ->orWhere(function ($subquery) use ($exclude_from_records) {
|
|
|
|
|
+ $subquery->whereNotIn('member_id', $exclude_from_records);
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ if ($this->filterCausals != null && sizeof($this->filterCausals) > 0) {
|
|
|
|
|
+ $causals = array();
|
|
|
|
|
+ foreach ($this->filterCausals as $z) {
|
|
|
|
|
+ $causals[] = $z;
|
|
|
|
|
+ $childs = \App\Models\Causal::where('parent_id', $z)->get();
|
|
|
|
|
+ foreach ($childs as $c) {
|
|
|
|
|
+ $causals[] = $c->id;
|
|
|
|
|
+ $childsX = \App\Models\Causal::where('parent_id', $c->id)->get();
|
|
|
|
|
+ foreach ($childsX as $cX) {
|
|
|
|
|
+ $causals[] = $cX->id;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ $datas->whereIn('causal_id', $causals);
|
|
|
|
|
+ }
|
|
|
|
|
+ if ($this->filterMember != null && $this->filterMember > 0) {
|
|
|
|
|
+ $datas->where('member_id', $this->filterMember);
|
|
|
|
|
+ }
|
|
|
|
|
+ $datas = $datas->orderBy('date', 'ASC')
|
|
|
|
|
+ ->orderBy('records.created_at', 'ASC')
|
|
|
|
|
+ ->orderBy('records_rows.id', 'ASC')
|
|
|
|
|
+ ->get();
|
|
|
|
|
+
|
|
|
|
|
+ foreach ($datas as $data) {
|
|
|
|
|
+ $causalCheck = \App\Models\Causal::findOrFail($data->causal_id);
|
|
|
|
|
+ $paymentCheck = $data->payment_method->money;
|
|
|
|
|
+
|
|
|
|
|
+ if (!$paymentCheck && ($causalCheck->no_first == null || !$causalCheck->no_first)) {
|
|
|
|
|
+ if (!$data->deleted) {
|
|
|
|
|
+ $amount = $data->amount;
|
|
|
|
|
+ $amount += getVatValue($amount, $data->vat_id);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $amount = $data->amount;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!isset($exportTotals[$data->payment_method->name])) {
|
|
|
|
|
+ $exportTotals[$data->payment_method->name]["IN"] = 0;
|
|
|
|
|
+ $exportTotals[$data->payment_method->name]["OUT"] = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!$data->deleted)
|
|
|
|
|
+ $exportTotals[$data->payment_method->name][$data->type] += $amount;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return $exportTotals;
|
|
|
|
|
+ }
|
|
|
|
|
+ public function resetFilters()
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->selectedPeriod = 'OGGI';
|
|
|
|
|
+ $this->filterCausals = [];
|
|
|
|
|
+ $this->filterMember = null;
|
|
|
|
|
+
|
|
|
|
|
+ $today = date("Y-m-d");
|
|
|
|
|
+ $this->fromDate = $today;
|
|
|
|
|
+ $this->toDate = $today;
|
|
|
|
|
+ $this->appliedFromDate = $today;
|
|
|
|
|
+ $this->appliedToDate = $today;
|
|
|
|
|
+
|
|
|
|
|
+ $this->emit('filters-reset');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function applyFilters()
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->isFiltering = true;
|
|
|
|
|
+
|
|
|
|
|
+ $this->setPeriodDates();
|
|
|
|
|
+
|
|
|
|
|
+ $this->appliedFromDate = $this->fromDate;
|
|
|
|
|
+ $this->appliedToDate = $this->toDate;
|
|
|
|
|
+
|
|
|
|
|
+ $this->render();
|
|
|
|
|
+
|
|
|
|
|
+ $this->isFiltering = false;
|
|
|
|
|
+
|
|
|
|
|
+ $this->emit('filters-applied');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function setPeriodDates()
|
|
|
|
|
+ {
|
|
|
|
|
+ $today = now();
|
|
|
|
|
+
|
|
|
|
|
+ switch ($this->selectedPeriod) {
|
|
|
|
|
+ case 'OGGI':
|
|
|
|
|
+ $this->fromDate = $today->format('Y-m-d');
|
|
|
|
|
+ $this->toDate = $today->format('Y-m-d');
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case 'IERI':
|
|
|
|
|
+ $yesterday = $today->copy()->subDay();
|
|
|
|
|
+ $this->fromDate = $yesterday->format('Y-m-d');
|
|
|
|
|
+ $this->toDate = $yesterday->format('Y-m-d');
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case 'MESE CORRENTE':
|
|
|
|
|
+ $this->fromDate = $today->copy()->startOfMonth()->format('Y-m-d');
|
|
|
|
|
+ $this->toDate = $today->copy()->endOfMonth()->format('Y-m-d');
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case 'MESE PRECEDENTE':
|
|
|
|
|
+ $lastMonth = $today->copy()->subMonth();
|
|
|
|
|
+ $this->fromDate = $lastMonth->startOfMonth()->format('Y-m-d');
|
|
|
|
|
+ $this->toDate = $lastMonth->endOfMonth()->format('Y-m-d');
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case 'ULTIMO TRIMESTRE':
|
|
|
|
|
+ $this->fromDate = $today->copy()->subMonths(3)->format('Y-m-d');
|
|
|
|
|
+ $this->toDate = $today->format('Y-m-d');
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case 'ULTIMO QUADRIMESTRE':
|
|
|
|
|
+ $this->fromDate = $today->copy()->subMonths(4)->format('Y-m-d');
|
|
|
|
|
+ $this->toDate = $today->format('Y-m-d');
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public function getCausals($records, $indentation)
|
|
public function getCausals($records, $indentation)
|
|
|
{
|
|
{
|
|
|
foreach ($records as $record) {
|
|
foreach ($records as $record) {
|
|
@@ -112,9 +434,9 @@ class Record extends Component
|
|
|
return $ret;
|
|
return $ret;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
public function render()
|
|
public function render()
|
|
|
{
|
|
{
|
|
|
-
|
|
|
|
|
$month = 0;
|
|
$month = 0;
|
|
|
$year = 0;
|
|
$year = 0;
|
|
|
|
|
|
|
@@ -123,23 +445,19 @@ class Record extends Component
|
|
|
|
|
|
|
|
$exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
|
|
$exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
|
|
|
|
|
|
|
|
- /*
|
|
|
|
|
- $fromDate = '';
|
|
|
|
|
- $toDate = '';
|
|
|
|
|
-
|
|
|
|
|
- if ($this->selectedFilterFromDay != '' && $this->selectedFilterFromMonth != '' && $this->selectedFilterFromYear != '')
|
|
|
|
|
- {
|
|
|
|
|
- $fromDate = date($this->selectedFilterFromYear . "-" . $this->selectedFilterFromMonth . "-" . $this->selectedFilterFromDay);
|
|
|
|
|
- }
|
|
|
|
|
- if ($this->selectedFilterToDay != '' && $this->selectedFilterToMonth != '' && $this->selectedFilterToYear != '')
|
|
|
|
|
- {
|
|
|
|
|
- $toDate = date($this->selectedFilterToYear . "-" . $this->selectedFilterToMonth . "-" . $this->selectedFilterToDay . " 23:59:59");
|
|
|
|
|
- }
|
|
|
|
|
- */
|
|
|
|
|
$datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
|
|
$datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
|
|
|
|
|
+ ->select(
|
|
|
|
|
+ 'records.*',
|
|
|
|
|
+ 'records_rows.id as row_id',
|
|
|
|
|
+ 'records_rows.record_id',
|
|
|
|
|
+ 'records_rows.causal_id',
|
|
|
|
|
+ 'records_rows.amount',
|
|
|
|
|
+ 'records_rows.note',
|
|
|
|
|
+ 'records_rows.when',
|
|
|
|
|
+ 'records_rows.vat_id',
|
|
|
|
|
+ )
|
|
|
->join('records_rows', 'records.id', '=', 'records_rows.record_id')
|
|
->join('records_rows', 'records.id', '=', 'records_rows.record_id')
|
|
|
- //->where('records_rows.when', 'like', '%' . date("m-Y") . '%')
|
|
|
|
|
- ->whereBetween('date', [$this->fromDate, $this->toDate])
|
|
|
|
|
|
|
+ ->whereBetween('date', [$this->appliedFromDate, $this->appliedToDate])
|
|
|
->where(function ($query) {
|
|
->where(function ($query) {
|
|
|
$query->where('type', 'OUT')
|
|
$query->where('type', 'OUT')
|
|
|
->orWhere(function ($query) {
|
|
->orWhere(function ($query) {
|
|
@@ -147,10 +465,13 @@ class Record extends Component
|
|
|
->orWhere('records.commercial', false);
|
|
->orWhere('records.commercial', false);
|
|
|
});
|
|
});
|
|
|
})
|
|
})
|
|
|
- /*->where(function ($query) {
|
|
|
|
|
- $query->where('deleted', false)->orWhere('deleted', null);
|
|
|
|
|
- })*/
|
|
|
|
|
- ->whereNotIn('member_id', $exclude_from_records);
|
|
|
|
|
|
|
+ ->where(function ($query) use ($exclude_from_records) {
|
|
|
|
|
+ $query->where('type', 'OUT')
|
|
|
|
|
+ ->orWhere(function ($subquery) use ($exclude_from_records) {
|
|
|
|
|
+ $subquery->whereNotIn('member_id', $exclude_from_records);
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
if ($this->filterCausals != null && sizeof($this->filterCausals) > 0) {
|
|
if ($this->filterCausals != null && sizeof($this->filterCausals) > 0) {
|
|
|
$causals = array();
|
|
$causals = array();
|
|
|
foreach ($this->filterCausals as $z) {
|
|
foreach ($this->filterCausals as $z) {
|
|
@@ -169,16 +490,18 @@ class Record extends Component
|
|
|
if ($this->filterMember != null && $this->filterMember > 0) {
|
|
if ($this->filterMember != null && $this->filterMember > 0) {
|
|
|
$datas->where('member_id', $this->filterMember);
|
|
$datas->where('member_id', $this->filterMember);
|
|
|
}
|
|
}
|
|
|
- if ($this->filterSupplier != null && $this->filterSupplier > 0) {
|
|
|
|
|
- $datas->where('supplier_id', $this->filterSupplier);
|
|
|
|
|
- }
|
|
|
|
|
- $datas = $datas->orderBy('date', 'ASC')->orderBy('records.created_at', 'ASC')
|
|
|
|
|
|
|
+ $datas = $datas->orderBy('date', 'ASC')
|
|
|
|
|
+ ->orderBy('records.created_at', 'ASC')
|
|
|
|
|
+ ->orderBy('records_rows.id', 'ASC')
|
|
|
->get();
|
|
->get();
|
|
|
|
|
|
|
|
|
|
+ $groupedData = [];
|
|
|
|
|
+ $causalsCount = [];
|
|
|
|
|
+ $nominativi = [];
|
|
|
|
|
+
|
|
|
foreach ($datas as $idx => $data) {
|
|
foreach ($datas as $idx => $data) {
|
|
|
|
|
|
|
|
$causalCheck = \App\Models\Causal::findOrFail($data->causal_id);
|
|
$causalCheck = \App\Models\Causal::findOrFail($data->causal_id);
|
|
|
-
|
|
|
|
|
$paymentCheck = $data->payment_method->money;
|
|
$paymentCheck = $data->payment_method->money;
|
|
|
|
|
|
|
|
if (!$paymentCheck && ($causalCheck->no_first == null || !$causalCheck->no_first)) {
|
|
if (!$paymentCheck && ($causalCheck->no_first == null || !$causalCheck->no_first)) {
|
|
@@ -189,42 +512,76 @@ class Record extends Component
|
|
|
} else {
|
|
} else {
|
|
|
$amount = $data->amount;
|
|
$amount = $data->amount;
|
|
|
}
|
|
}
|
|
|
- /*$when = sizeof(json_decode($data->when));
|
|
|
|
|
-
|
|
|
|
|
- if ($when > 1)
|
|
|
|
|
- {
|
|
|
|
|
- $amount = $amount / $when;
|
|
|
|
|
- }*/
|
|
|
|
|
-
|
|
|
|
|
- $prefix = '';
|
|
|
|
|
- if (!$data->commercial)
|
|
|
|
|
- $prefix = $idx . "$";
|
|
|
|
|
|
|
|
|
|
|
|
+ $isCommercial = ($data->commercial == 1 || $data->commercial === '1' || $data->commercial === true);
|
|
|
|
|
+ $typeLabel = $isCommercial ? 'Commerciale' : 'Non Commerciale';
|
|
|
|
|
|
|
|
- // aggiungere il nome * * *
|
|
|
|
|
- //$causal = $prefix . $data->date . "§" . $causalCheck->getTree();
|
|
|
|
|
- $causal = $prefix . $data->date . "§" . $causalCheck->getTree() . "§" . ($data->type == "IN" ? ($data->member ? ($data->member->last_name . " " . $data->member->first_name) : "") : $data->supplier->name ?? "") . "§" . $data->note . "§" . ($data->deleted ? 'DELETED' : '');
|
|
|
|
|
-
|
|
|
|
|
- if (isset($this->records[$causal])) {
|
|
|
|
|
- if (isset($this->records[$causal][$data->payment_method->name])) {
|
|
|
|
|
- if ($data->commercial) {
|
|
|
|
|
- if ($data->deleted && $this->records[$causal][$data->payment_method->name][$data->type])
|
|
|
|
|
- $amount += $this->records[$causal][$data->payment_method->name][$data->type];
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ $nominativo = '';
|
|
|
|
|
+ if ($data->type == "IN") {
|
|
|
|
|
+ if ($data->member) {
|
|
|
|
|
+ $nominativo = $data->member->last_name . " " . $data->member->first_name;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ if ($data->supplier) {
|
|
|
|
|
+ $nominativo = $data->supplier->name;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (!isset($this->totals[$data->payment_method->name])) {
|
|
|
|
|
- $this->totals[$data->payment_method->name]["IN"] = 0;
|
|
|
|
|
- $this->totals[$data->payment_method->name]["OUT"] = 0;
|
|
|
|
|
|
|
+ $groupKey = $data->date . '|' . $typeLabel . '|' . $data->payment_method->name . '|' . $data->type . '|' . $nominativo;
|
|
|
|
|
+
|
|
|
|
|
+ if (!isset($groupedData[$groupKey])) {
|
|
|
|
|
+ $groupedData[$groupKey] = [
|
|
|
|
|
+ 'date' => $data->date,
|
|
|
|
|
+ 'type_label' => $typeLabel,
|
|
|
|
|
+ 'payment_method' => $data->payment_method->name,
|
|
|
|
|
+ 'transaction_type' => $data->type,
|
|
|
|
|
+ 'nominativo' => $nominativo,
|
|
|
|
|
+ 'amount' => 0,
|
|
|
|
|
+ 'deleted' => false,
|
|
|
|
|
+ 'causals' => [],
|
|
|
|
|
+ 'notes' => []
|
|
|
|
|
+ ];
|
|
|
|
|
+ $causalsCount[$groupKey] = [];
|
|
|
|
|
+ $nominativi[$groupKey] = $nominativo;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $groupedData[$groupKey]['amount'] += $amount;
|
|
|
|
|
+ $causalsCount[$groupKey][$causalCheck->getTree()] = true;
|
|
|
|
|
+ if (!empty($data->note)) {
|
|
|
|
|
+ $groupedData[$groupKey]['notes'][] = $data->note;
|
|
|
}
|
|
}
|
|
|
|
|
+ if ($data->deleted) {
|
|
|
|
|
+ $groupedData[$groupKey]['deleted'] = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- $this->records[$causal][$data->payment_method->name][$data->type] = $amount;
|
|
|
|
|
|
|
+ foreach ($groupedData as $groupKey => $group) {
|
|
|
|
|
+ $causalsInGroup = array_keys($causalsCount[$groupKey]);
|
|
|
|
|
|
|
|
- if (!$data->deleted)
|
|
|
|
|
- $this->totals[$data->payment_method->name][$data->type] += $amount; // $data->amount;//$this->records[$causal][$data->payment_method->name][$data->type];
|
|
|
|
|
|
|
+ $causalDisplay = $group['type_label'];
|
|
|
|
|
|
|
|
|
|
+ if (count($causalsInGroup) > 1) {
|
|
|
|
|
+ $detailDisplay = 'Varie|' . implode('|', $causalsInGroup);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $detailDisplay = $causalsInGroup[0];
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ $recordKey = $group['date'] . "§" . $causalDisplay . "§" . $group['nominativo'] . "§" . $detailDisplay . "§" . ($group['deleted'] ? 'DELETED' : '') . "§";
|
|
|
|
|
+
|
|
|
|
|
+ if (!isset($this->records[$recordKey][$group['payment_method']][$group['transaction_type']])) {
|
|
|
|
|
+ $this->records[$recordKey][$group['payment_method']][$group['transaction_type']] = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $this->records[$recordKey][$group['payment_method']][$group['transaction_type']] += $group['amount'];
|
|
|
|
|
+
|
|
|
|
|
+ if (!isset($this->totals[$group['payment_method']])) {
|
|
|
|
|
+ $this->totals[$group['payment_method']]["IN"] = 0;
|
|
|
|
|
+ $this->totals[$group['payment_method']]["OUT"] = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!$group['deleted'])
|
|
|
|
|
+ $this->totals[$group['payment_method']][$group['transaction_type']] += $group['amount'];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return view('livewire.records');
|
|
return view('livewire.records');
|
|
@@ -232,7 +589,6 @@ class Record extends Component
|
|
|
|
|
|
|
|
private function getLabels($fromDate, $toDate)
|
|
private function getLabels($fromDate, $toDate)
|
|
|
{
|
|
{
|
|
|
-
|
|
|
|
|
$begin = new DateTime($fromDate);
|
|
$begin = new DateTime($fromDate);
|
|
|
$end = new DateTime($toDate);
|
|
$end = new DateTime($toDate);
|
|
|
|
|
|
|
@@ -282,10 +638,188 @@ class Record extends Component
|
|
|
return $data;
|
|
return $data;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function export()
|
|
|
|
|
|
|
+ public function openExportModal()
|
|
|
{
|
|
{
|
|
|
- ini_set('memory_limit', '512M');
|
|
|
|
|
|
|
+ $this->exportFromDate = $this->appliedFromDate;
|
|
|
|
|
+ $this->exportToDate = $this->appliedToDate;
|
|
|
|
|
|
|
|
|
|
+ // Reset email options
|
|
|
|
|
+ $this->sendViaEmail = false;
|
|
|
|
|
+ $this->exportEmailAddress = $this->getPreferredEmail();
|
|
|
|
|
+ $this->updateEmailSubject();
|
|
|
|
|
+
|
|
|
|
|
+ $this->emit('show-export-modal');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function exportWithDateRange()
|
|
|
|
|
+ {
|
|
|
|
|
+
|
|
|
|
|
+ $this->isExporting = true;
|
|
|
|
|
+ $this->emit('$refresh'); // This forces Livewire to re-render
|
|
|
|
|
+
|
|
|
|
|
+ // Add a small delay to allow the view to update
|
|
|
|
|
+ usleep(100000);
|
|
|
|
|
+ if ($this->sendViaEmail) {
|
|
|
|
|
+ $this->validate([
|
|
|
|
|
+ 'exportEmailAddress' => 'required|email',
|
|
|
|
|
+ 'exportEmailSubject' => 'required|string|max:255',
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $this->isExporting = true;
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ $exportRecords = $this->generateExportData($this->exportFromDate, $this->exportToDate);
|
|
|
|
|
+ $exportTotals = $this->generateExportTotals($this->exportFromDate, $this->exportToDate);
|
|
|
|
|
+
|
|
|
|
|
+ if ($this->sendViaEmail) {
|
|
|
|
|
+ // Dispatch job to background queue
|
|
|
|
|
+ $this->dispatchExportJob($exportRecords, $exportTotals);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // Direct download (synchronous)
|
|
|
|
|
+ return $this->exportWithData($exportRecords, $exportTotals);
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (\Illuminate\Validation\ValidationException $e) {
|
|
|
|
|
+ $this->isExporting = false;
|
|
|
|
|
+ throw $e;
|
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
|
+ $this->isExporting = false;
|
|
|
|
|
+ Log::error('Export error: ' . $e->getMessage());
|
|
|
|
|
+
|
|
|
|
|
+ if ($this->sendViaEmail) {
|
|
|
|
|
+ $this->emit('export-email-error', 'Errore durante l\'invio dell\'email: ' . $e->getMessage());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ session()->flash('error', 'Errore durante l\'export: ' . $e->getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ $this->isExporting = false;
|
|
|
|
|
+ $this->emit('export-complete');
|
|
|
|
|
+ $this->emit('hide-export-modal');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function getMemberName($memberId)
|
|
|
|
|
+ {
|
|
|
|
|
+ $member = \App\Models\Member::find($memberId);
|
|
|
|
|
+ return $member ? $member->last_name . ' ' . $member->first_name : 'Sconosciuto';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function getCausalsNames($causalIds)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (!is_array($causalIds)) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $causals = \App\Models\Causal::whereIn('id', $causalIds)->pluck('name')->toArray();
|
|
|
|
|
+ return implode(', ', $causals);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function updatedExportFromDate()
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->updateEmailSubject();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function updatedExportToDate()
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->updateEmailSubject();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function updatedSendViaEmail($value)
|
|
|
|
|
+ {
|
|
|
|
|
+ if ($value && empty($this->exportEmailAddress)) {
|
|
|
|
|
+ $this->exportEmailAddress = $this->getPreferredEmail();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function resetEmailForm()
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->sendViaEmail = false;
|
|
|
|
|
+ $this->exportEmailAddress = $this->getPreferredEmail();
|
|
|
|
|
+ $this->updateEmailSubject();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function updateEmailSubject()
|
|
|
|
|
+ {
|
|
|
|
|
+ if (!empty($this->exportFromDate) && !empty($this->exportToDate)) {
|
|
|
|
|
+ $fromFormatted = date('d/m/Y', strtotime($this->exportFromDate));
|
|
|
|
|
+ $toFormatted = date('d/m/Y', strtotime($this->exportToDate));
|
|
|
|
|
+
|
|
|
|
|
+ if ($this->exportFromDate === $this->exportToDate) {
|
|
|
|
|
+ $this->exportEmailSubject = "Prima Nota - Export del {$fromFormatted}";
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $this->exportEmailSubject = "Prima Nota - Export dal {$fromFormatted} al {$toFormatted}";
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Dispatch export job to queue
|
|
|
|
|
+ */
|
|
|
|
|
+ private function dispatchExportJob($exportRecords, $exportTotals)
|
|
|
|
|
+ {
|
|
|
|
|
+ try {
|
|
|
|
|
+ // Prepare filter descriptions for the job
|
|
|
|
|
+ $filterDescriptions = [
|
|
|
|
|
+ 'member' => $this->filterMember ? $this->getMemberName($this->filterMember) : null,
|
|
|
|
|
+ 'causals' => $this->filterCausals ? $this->getCausalsNames($this->filterCausals) : null,
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ $paymentsArray = $this->payments->map(function ($payment) {
|
|
|
|
|
+ return [
|
|
|
|
|
+ 'id' => $payment->id,
|
|
|
|
|
+ 'name' => $payment->name,
|
|
|
|
|
+ 'type' => $payment->type
|
|
|
|
|
+ ];
|
|
|
|
|
+ })->toArray();
|
|
|
|
|
+
|
|
|
|
|
+ // Dispatch job to background queue
|
|
|
|
|
+ ExportPrimaNota::dispatch(
|
|
|
|
|
+ $exportRecords,
|
|
|
|
|
+ $exportTotals,
|
|
|
|
|
+ $this->exportEmailAddress,
|
|
|
|
|
+ $this->exportEmailSubject,
|
|
|
|
|
+ [
|
|
|
|
|
+ 'from' => date('d/m/Y', strtotime($this->exportFromDate)),
|
|
|
|
|
+ 'to' => date('d/m/Y', strtotime($this->exportToDate))
|
|
|
|
|
+ ],
|
|
|
|
|
+ auth()->id(),
|
|
|
|
|
+ $paymentsArray,
|
|
|
|
|
+ $filterDescriptions
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ $this->emit('export-email-queued');
|
|
|
|
|
+ session()->flash('success', 'Export in corso! Riceverai l\'email a breve alla casella: ' . $this->exportEmailAddress);
|
|
|
|
|
+
|
|
|
|
|
+ Log::info('Export job dispatched', [
|
|
|
|
|
+ 'user_id' => auth()->id(),
|
|
|
|
|
+ 'email' => $this->exportEmailAddress,
|
|
|
|
|
+ 'date_range' => [$this->exportFromDate, $this->exportToDate],
|
|
|
|
|
+ 'total_records' => count($exportRecords)
|
|
|
|
|
+ ]);
|
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
|
+ Log::error('Failed to dispatch export job', [
|
|
|
|
|
+ 'user_id' => auth()->id(),
|
|
|
|
|
+ 'email' => $this->exportEmailAddress,
|
|
|
|
|
+ 'error' => $e->getMessage()
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ throw new \Exception('Errore nell\'avvio dell\'export: ' . $e->getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ function export()
|
|
|
|
|
+ {
|
|
|
|
|
+ $exportRecords = $this->generateExportData($this->appliedFromDate, $this->appliedToDate);
|
|
|
|
|
+ $exportTotals = $this->generateExportTotals($this->appliedFromDate, $this->appliedToDate);
|
|
|
|
|
+
|
|
|
|
|
+ return $this->exportWithData($exportRecords, $exportTotals);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function exportWithData($exportRecords, $exportTotals)
|
|
|
|
|
+ {
|
|
|
|
|
+ ini_set('memory_limit', '512M');
|
|
|
gc_enable();
|
|
gc_enable();
|
|
|
|
|
|
|
|
$letters = array('F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA');
|
|
$letters = array('F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA');
|
|
@@ -355,24 +889,25 @@ class Record extends Component
|
|
|
$batchSize = 1000;
|
|
$batchSize = 1000;
|
|
|
$recordsProcessed = 0;
|
|
$recordsProcessed = 0;
|
|
|
|
|
|
|
|
- $totalRecords = count($this->records);
|
|
|
|
|
- $recordsArray = array_chunk($this->records, $batchSize, true);
|
|
|
|
|
|
|
+ $totalRecords = count($exportRecords);
|
|
|
|
|
+ $recordsArray = array_chunk($exportRecords, $batchSize, true);
|
|
|
|
|
|
|
|
foreach ($recordsArray as $recordsBatch) {
|
|
foreach ($recordsArray as $recordsBatch) {
|
|
|
foreach ($recordsBatch as $causal => $record) {
|
|
foreach ($recordsBatch as $causal => $record) {
|
|
|
- $check = strpos($causal, "$") ? explode("$", $causal)[1] : $causal;
|
|
|
|
|
-
|
|
|
|
|
|
|
+ $check = $causal;
|
|
|
$parts = explode("§", $check);
|
|
$parts = explode("§", $check);
|
|
|
$d = $parts[0] ?? '';
|
|
$d = $parts[0] ?? '';
|
|
|
$c = $parts[1] ?? '';
|
|
$c = $parts[1] ?? '';
|
|
|
$j = $parts[2] ?? '';
|
|
$j = $parts[2] ?? '';
|
|
|
- $k = $parts[3] ?? '';
|
|
|
|
|
|
|
+ $det = $parts[3] ?? '';
|
|
|
$deleted = $parts[4] ?? '';
|
|
$deleted = $parts[4] ?? '';
|
|
|
- $numeroLinea = $parts[5] ?? '';
|
|
|
|
|
|
|
+
|
|
|
|
|
+ $detailParts = explode('|', $det);
|
|
|
|
|
+ $exportDetail = count($detailParts) > 1 ? implode(', ', array_slice($detailParts, 1)) : $det;
|
|
|
|
|
|
|
|
$activeWorksheet->setCellValue('A' . $count, !empty($d) ? date("d/m/Y", strtotime($d)) : '');
|
|
$activeWorksheet->setCellValue('A' . $count, !empty($d) ? date("d/m/Y", strtotime($d)) : '');
|
|
|
$activeWorksheet->setCellValue('B' . $count, $c);
|
|
$activeWorksheet->setCellValue('B' . $count, $c);
|
|
|
- $activeWorksheet->setCellValue('C' . $count, $k);
|
|
|
|
|
|
|
+ $activeWorksheet->setCellValue('C' . $count, $exportDetail);
|
|
|
$activeWorksheet->setCellValue('D' . $count, $j);
|
|
$activeWorksheet->setCellValue('D' . $count, $j);
|
|
|
|
|
|
|
|
$stato = ($deleted === 'DELETED') ? 'ANNULLATA' : '';
|
|
$stato = ($deleted === 'DELETED') ? 'ANNULLATA' : '';
|
|
@@ -430,21 +965,21 @@ class Record extends Component
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (isset($this->totals[$p->name])) {
|
|
|
|
|
|
|
+ if (isset($exportTotals[$p->name])) {
|
|
|
if ($p->type == 'ALL') {
|
|
if ($p->type == 'ALL') {
|
|
|
- $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["IN"] ?? 0));
|
|
|
|
|
|
|
+ $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($exportTotals[$p->name]["IN"] ?? 0));
|
|
|
$idx++;
|
|
$idx++;
|
|
|
- $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["OUT"] ?? 0));
|
|
|
|
|
|
|
+ $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($exportTotals[$p->name]["OUT"] ?? 0));
|
|
|
$idx++;
|
|
$idx++;
|
|
|
} elseif ($p->type == 'IN') {
|
|
} elseif ($p->type == 'IN') {
|
|
|
- $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["IN"] ?? 0));
|
|
|
|
|
|
|
+ $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($exportTotals[$p->name]["IN"] ?? 0));
|
|
|
$idx++;
|
|
$idx++;
|
|
|
$activeWorksheet->setCellValue($letters[$idx] . $count, "");
|
|
$activeWorksheet->setCellValue($letters[$idx] . $count, "");
|
|
|
$idx++;
|
|
$idx++;
|
|
|
} elseif ($p->type == 'OUT') {
|
|
} elseif ($p->type == 'OUT') {
|
|
|
$activeWorksheet->setCellValue($letters[$idx] . $count, "");
|
|
$activeWorksheet->setCellValue($letters[$idx] . $count, "");
|
|
|
$idx++;
|
|
$idx++;
|
|
|
- $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["OUT"] ?? 0));
|
|
|
|
|
|
|
+ $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($exportTotals[$p->name]["OUT"] ?? 0));
|
|
|
$idx++;
|
|
$idx++;
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
@@ -478,13 +1013,108 @@ class Record extends Component
|
|
|
$activeWorksheet->getColumnDimension($l)->setWidth(20);
|
|
$activeWorksheet->getColumnDimension($l)->setWidth(20);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $writer = new Xlsx($spreadsheet);
|
|
|
|
|
- $path = storage_path('prima_nota_' . date("YmdHis") . '.xlsx');
|
|
|
|
|
- $writer->save($path);
|
|
|
|
|
|
|
+ $filename = 'prima_nota_' . date("YmdHis") . '.xlsx';
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ $currentClient = session('currentClient', 'default');
|
|
|
|
|
+
|
|
|
|
|
+ $tempPath = sys_get_temp_dir() . '/' . $filename;
|
|
|
|
|
+
|
|
|
|
|
+ $writer = new Xlsx($spreadsheet);
|
|
|
|
|
+ $writer->save($tempPath);
|
|
|
|
|
+
|
|
|
|
|
+ unset($spreadsheet, $activeWorksheet, $writer);
|
|
|
|
|
+ gc_collect_cycles();
|
|
|
|
|
+
|
|
|
|
|
+ $disk = Storage::disk('s3');
|
|
|
|
|
+
|
|
|
|
|
+ $s3Path = $currentClient . '/prima_nota/' . $filename;
|
|
|
|
|
+
|
|
|
|
|
+ $primaNotaFolderPath = $currentClient . '/prima_nota/.gitkeep';
|
|
|
|
|
+ if (!$disk->exists($primaNotaFolderPath)) {
|
|
|
|
|
+ $disk->put($primaNotaFolderPath, '');
|
|
|
|
|
+ Log::info("Created prima_nota folder for client: {$currentClient}");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $fileContent = file_get_contents($tempPath);
|
|
|
|
|
+ $uploaded = $disk->put($s3Path, $fileContent, 'private');
|
|
|
|
|
+
|
|
|
|
|
+ if (!$uploaded) {
|
|
|
|
|
+ throw new \Exception('Failed to upload file to Wasabi S3');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Log::info("Prima Nota exported to Wasabi", [
|
|
|
|
|
+ 'client' => $currentClient,
|
|
|
|
|
+ 'path' => $s3Path,
|
|
|
|
|
+ 'size' => filesize($tempPath),
|
|
|
|
|
+ 'records_processed' => $recordsProcessed
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ if (file_exists($tempPath)) {
|
|
|
|
|
+ unlink($tempPath);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $downloadUrl = $disk->temporaryUrl($s3Path, now()->addHour());
|
|
|
|
|
+ return redirect($downloadUrl);
|
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
|
+ Log::error('Error exporting Prima Nota to Wasabi S3', [
|
|
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
|
|
+ 'client' => session('currentClient', 'unknown'),
|
|
|
|
|
+ 'filename' => $filename,
|
|
|
|
|
+ 'records_processed' => $recordsProcessed ?? 0
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ $currentClient = session('currentClient', 'default');
|
|
|
|
|
+ $clientFolder = storage_path('app/prima_nota/' . $currentClient);
|
|
|
|
|
+
|
|
|
|
|
+ if (!is_dir($clientFolder)) {
|
|
|
|
|
+ mkdir($clientFolder, 0755, true);
|
|
|
|
|
+ Log::info("Created local client prima_nota folder: {$clientFolder}");
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- unset($spreadsheet, $activeWorksheet, $writer);
|
|
|
|
|
- gc_collect_cycles();
|
|
|
|
|
|
|
+ $localPath = $clientFolder . '/' . $filename;
|
|
|
|
|
+
|
|
|
|
|
+ if (isset($tempPath) && file_exists($tempPath)) {
|
|
|
|
|
+ rename($tempPath, $localPath);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $writer = new Xlsx($spreadsheet);
|
|
|
|
|
+ $writer->save($localPath);
|
|
|
|
|
+ unset($spreadsheet, $activeWorksheet, $writer);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ gc_collect_cycles();
|
|
|
|
|
+
|
|
|
|
|
+ Log::warning("Prima Nota saved locally due to S3 error", [
|
|
|
|
|
+ 'client' => $currentClient,
|
|
|
|
|
+ 'local_path' => $localPath,
|
|
|
|
|
+ 'error' => $e->getMessage()
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ session()->flash('warning', 'File salvato localmente a causa di un errore del cloud storage.');
|
|
|
|
|
+
|
|
|
|
|
+ return response()->download($localPath)->deleteFileAfterSend();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function getPreferredEmail()
|
|
|
|
|
+ {
|
|
|
|
|
+ // Try multiple sources in order of preference
|
|
|
|
|
+ $email = auth()->user()->email ?? null;
|
|
|
|
|
+
|
|
|
|
|
+ if (empty($email)) {
|
|
|
|
|
+ $email = session('user_email', null);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (empty($email)) {
|
|
|
|
|
+ $member = \App\Models\Member::where('user_id', auth()->id())->first();
|
|
|
|
|
+ $email = $member ? $member->email : null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (empty($email)) {
|
|
|
|
|
+ // Get from user input or company default
|
|
|
|
|
+ $email = config('mail.default_recipient', '');
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- return response()->download($path)->deleteFileAfterSend();
|
|
|
|
|
|
|
+ return $email;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|