Record.php 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use DateInterval;
  5. use DatePeriod;
  6. use DateTime;
  7. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  8. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  9. use Illuminate\Support\Facades\Storage;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Facades\Mail;
  12. use App\Mail\ExportNotification;
  13. use App\Jobs\ExportPrimaNota;
  14. class Record extends Component
  15. {
  16. public $records, $dataId, $totals;
  17. public $in;
  18. public $out;
  19. public $payments = [];
  20. public $origins = [];
  21. public $destinations = [];
  22. public $fromDate;
  23. public $toDate;
  24. public $appliedFromDate;
  25. public $appliedToDate;
  26. public $exportFromDate;
  27. public $exportToDate;
  28. public $isExporting = false;
  29. public $selectedPeriod = 'OGGI';
  30. public $filterCausals = null;
  31. public $filterPaymentMethods = null;
  32. public $filterOrigins = null;
  33. public $filterDestinations = null;
  34. public $filterMember = null;
  35. public $isFiltering = false;
  36. public array $recordDatas = [];
  37. public array $labels = [];
  38. public array $causals = [];
  39. public $members = array();
  40. public $sendViaEmail = false;
  41. public $exportEmailAddress = '';
  42. public $exportEmailSubject = 'Prima Nota - Export';
  43. private $causalAmounts = [];
  44. public $selectedMonth;
  45. public $showMonthPicker = false;
  46. public $selectedDay;
  47. public $showDayPicker = false;
  48. public $selectedYear;
  49. protected $rules = [
  50. 'exportEmailAddress' => 'required_if:sendViaEmail,true|email',
  51. 'exportEmailSubject' => 'required_if:sendViaEmail,true|string|max:255',
  52. ];
  53. public $total_in = 0;
  54. public $total_out = 0;
  55. protected $messages = [
  56. 'exportEmailAddress.required_if' => 'L\'indirizzo email è obbligatorio quando si sceglie di inviare via email.',
  57. 'exportEmailAddress.email' => 'Inserisci un indirizzo email valido.',
  58. 'exportEmailSubject.required_if' => 'L\'oggetto dell\'email è obbligatorio.',
  59. 'exportEmailSubject.max' => 'L\'oggetto dell\'email non può superare i 255 caratteri.',
  60. ];
  61. public function hydrate()
  62. {
  63. $this->emit('load-select');
  64. }
  65. public function mount()
  66. {
  67. $this->fromDate = date("Y-m-d");
  68. $this->toDate = date("Y-m-d");
  69. $this->appliedFromDate = date("Y-m-d");
  70. $this->appliedToDate = date("Y-m-d");
  71. $this->exportFromDate = date("Y-m-d");
  72. $this->exportToDate = date("Y-m-d");
  73. $this->exportEmailSubject = 'Prima Nota - Export del ' . date('d/m/Y');
  74. $this->getCausals(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->get(), 0);
  75. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])
  76. ->where(function($query) {
  77. $query->where('is_archived', false)
  78. ->orWhereNull('is_archived');
  79. })
  80. ->where(function($query) {
  81. $query->where('is_deleted', false)
  82. ->orWhereNull('is_deleted');
  83. })->orderBy('last_name')->orderBy('first_name')->get();
  84. $this->payments = \App\Models\PaymentMethod::select('id', 'name', 'type')->where('enabled', true)->where('money', false)->get();
  85. $this->origins = \App\Models\Bank::select('id', 'name')->where('enabled', true)->whereIn('visibility', ['OUT', 'ALL'])->get();
  86. $this->destinations = \App\Models\Bank::select('id', 'name')->where('enabled', true)->whereIn('visibility', ['IN', 'ALL'])->get();
  87. $this->selectedMonth = date('Y-m');
  88. $this->selectedDay = date('Y-m-d');
  89. $this->selectedYear = date('Y');
  90. }
  91. private function generateExportDataAndTotals($fromDate, $toDate)
  92. {
  93. Log::info('generateExportDataAndTotals: Start (combined method)', [
  94. 'from_date' => $fromDate,
  95. 'to_date' => $toDate,
  96. 'memory_before' => memory_get_usage(true)
  97. ]);
  98. $exportRecords = array();
  99. $exportTotals = array();
  100. Log::info('generateExportDataAndTotals: Getting excluded members');
  101. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  102. Log::info('generateExportDataAndTotals: Excluded members retrieved', ['count' => count($exclude_from_records)]);
  103. Log::info('generateExportDataAndTotals: Building main query');
  104. $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
  105. ->select(
  106. 'records.*',
  107. 'records_rows.id as row_id',
  108. 'records_rows.record_id',
  109. 'records_rows.causal_id',
  110. 'records_rows.amount',
  111. 'records_rows.note',
  112. 'records_rows.when',
  113. 'records_rows.vat_id',
  114. 'records_rows.created_at as row_created_at',
  115. 'records_rows.updated_at as row_updated_at'
  116. )
  117. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  118. //->where('records.deleted', false)
  119. ->where(function ($query) {
  120. $query->where('records.deleted', false)->orWhereNull('records.deleted');
  121. })
  122. ->whereBetween('date', [$fromDate, $toDate])
  123. ->where(function ($query) {
  124. $query->where('type', 'OUT')
  125. ->orWhere(function ($query) {
  126. $query->where('records.corrispettivo_fiscale', true)
  127. ->orWhere('records.commercial', false);
  128. });
  129. })
  130. ->where(function ($query) use ($exclude_from_records) {
  131. $query->where('type', 'OUT')
  132. ->orWhere(function ($subquery) use ($exclude_from_records) {
  133. $subquery->whereNotIn('member_id', $exclude_from_records);
  134. });
  135. });
  136. Log::info('generateExportDataAndTotals: Applying causal filters');
  137. if ($this->filterCausals != null && sizeof($this->filterCausals) > 0) {
  138. $causals = array();
  139. foreach ($this->filterCausals as $z) {
  140. $causals[] = $z;
  141. $childs = \App\Models\Causal::where('parent_id', $z)->get();
  142. foreach ($childs as $c) {
  143. $causals[] = $c->id;
  144. $childsX = \App\Models\Causal::where('parent_id', $c->id)->get();
  145. foreach ($childsX as $cX) {
  146. $causals[] = $cX->id;
  147. }
  148. }
  149. }
  150. $datas->whereIn('causal_id', $causals);
  151. Log::info('generateExportDataAndTotals: Causal filters applied', ['causal_count' => count($causals)]);
  152. }
  153. if ($this->filterPaymentMethods != null && sizeof($this->filterPaymentMethods) > 0) {
  154. $datas->whereIn('payment_method_id', $this->filterPaymentMethods);
  155. Log::info('generateExportDataAndTotals: Payment method filters applied', ['payment_method_count' => count($this->filterPaymentMethods)]);
  156. }
  157. if ($this->filterOrigins != null && sizeof($this->filterOrigins) > 0) {
  158. $datas->whereIn('origin_id', $this->filterOrigins);
  159. Log::info('generateExportDataAndTotals: Origin filters applied', ['origins_count' => count($this->filterOrigins)]);
  160. }
  161. if ($this->filterDestinations != null && sizeof($this->filterDestinations) > 0) {
  162. $datas->whereIn('destination_id', $this->filterDestinations);
  163. Log::info('generateExportDataAndTotals: Destination filters applied', ['destinations_count' => count($this->filterDestinations)]);
  164. }
  165. if ($this->filterMember != null && $this->filterMember > 0) {
  166. $datas->where('member_id', $this->filterMember);
  167. Log::info('generateExportDataAndTotals: Member filter applied', ['member_id' => $this->filterMember]);
  168. }
  169. Log::info('generateExportDataAndTotals: Executing query');
  170. $queryStart = microtime(true);
  171. $datas = $datas->orderBy('date', 'ASC')
  172. ->orderBy('records.created_at', 'ASC')
  173. ->orderBy('records_rows.id', 'ASC')
  174. ->get();
  175. $queryTime = microtime(true) - $queryStart;
  176. Log::info('generateExportDataAndTotals: Query executed', [
  177. 'record_count' => $datas->count(),
  178. 'query_time' => $queryTime,
  179. 'memory_after_query' => memory_get_usage(true)
  180. ]);
  181. $groupedData = [];
  182. $causalsCount = [];
  183. $processedCount = 0;
  184. // Initialize totals array
  185. foreach ($this->payments as $p) {
  186. $exportTotals[$p->name] = ["IN" => 0, "OUT" => 0];
  187. }
  188. Log::info('generateExportDataAndTotals: Starting combined data processing loop');
  189. $loopStart = microtime(true);
  190. foreach ($datas as $idx => $data) {
  191. if ($processedCount % 100 == 0) {
  192. Log::info('generateExportDataAndTotals: Processing progress', [
  193. 'processed' => $processedCount,
  194. 'total' => $datas->count(),
  195. 'memory_current' => memory_get_usage(true),
  196. 'memory_peak' => memory_get_peak_usage(true)
  197. ]);
  198. }
  199. try {
  200. $causalCheck = \App\Models\Causal::findOrFail($data->causal_id);
  201. $paymentCheck = $data->payment_method->money;
  202. if (!$paymentCheck && ($causalCheck->no_first == null || !$causalCheck->no_first)) {
  203. if (!$data->deleted) {
  204. $amount = $data->amount;
  205. $amount += getVatValue($amount, $data->vat_id);
  206. } else {
  207. $amount = $data->amount;
  208. }
  209. // CALCULATE TOTALS HERE (in the same loop)
  210. /*if (!$data->deleted) {
  211. $exportTotals[$data->payment_method->name][$data->type] += $amount;
  212. }*/
  213. $isCommercial = ($data->commercial == 1 || $data->commercial === '1' || $data->commercial === true);
  214. $typeLabel = $isCommercial ? 'Commerciale' : 'Non Commerciale';
  215. $nominativo = '';
  216. if ($data->type == "IN") {
  217. if ($data->member) {
  218. $nominativo = $data->member->last_name . " " . $data->member->first_name;
  219. }
  220. } else {
  221. if ($data->supplier) {
  222. $nominativo = $data->supplier->name;
  223. }
  224. }
  225. $groupKey = $data->date . '|' . $typeLabel . '|' . $data->payment_method->name . '|' . $data->type . '|' . $nominativo;
  226. if (!isset($groupedData[$groupKey])) {
  227. $groupedData[$groupKey] = [
  228. 'date' => $data->date,
  229. 'type_label' => $typeLabel,
  230. 'payment_method' => $data->payment_method->name,
  231. 'transaction_type' => $data->type,
  232. 'nominativo' => $nominativo,
  233. 'amount' => 0,
  234. 'deleted' => false,
  235. 'causals' => [],
  236. 'notes' => []
  237. ];
  238. $causalsCount[$groupKey] = [];
  239. }
  240. $groupedData[$groupKey]['amount'] += $amount;
  241. $causalsCount[$groupKey][$causalCheck->getTree()] = true;
  242. if (!empty($data->note)) {
  243. $groupedData[$groupKey]['notes'][] = $data->note;
  244. }
  245. if ($data->deleted) {
  246. $groupedData[$groupKey]['deleted'] = true;
  247. }
  248. }
  249. $processedCount++;
  250. } catch (\Exception $e) {
  251. Log::error('generateExportDataAndTotals: Error processing individual record', [
  252. 'record_id' => $data->id ?? 'unknown',
  253. 'error' => $e->getMessage(),
  254. 'processed_so_far' => $processedCount
  255. ]);
  256. throw $e;
  257. }
  258. }
  259. $loopTime = microtime(true) - $loopStart;
  260. Log::info('generateExportDataAndTotals: Combined processing loop completed', [
  261. 'total_processed' => $processedCount,
  262. 'grouped_records' => count($groupedData),
  263. 'loop_time' => $loopTime,
  264. 'memory_after_loop' => memory_get_usage(true)
  265. ]);
  266. Log::info('generateExportDataAndTotals: Building final export records');
  267. $finalStart = microtime(true);
  268. $tot = 0;
  269. $count = 0;
  270. foreach ($groupedData as $groupKey => $group) {
  271. $causalsInGroup = array_keys($causalsCount[$groupKey]);
  272. $causalDisplay = $group['type_label'];
  273. if (count($causalsInGroup) > 1) {
  274. $detailDisplay = 'Varie|' . implode('|', $causalsInGroup);
  275. } else {
  276. $detailDisplay = $causalsInGroup[0];
  277. }
  278. $recordKey = $group['date'] . "§" . $causalDisplay . "§" . $group['nominativo'] . "§" . $detailDisplay . "§" . ($group['deleted'] ? 'DELETED' : '') . "§";
  279. if (!isset($exportRecords[$recordKey][$group['payment_method']][$group['transaction_type']])) {
  280. $exportRecords[$recordKey][$group['payment_method']][$group['transaction_type']] = 0;
  281. }
  282. $exportRecords[$recordKey][$group['payment_method']][$group['transaction_type']] += $group['amount'];
  283. if (!$group['deleted'])
  284. $exportTotals[$group['payment_method']][$group['transaction_type']] += $group['amount'];
  285. }
  286. $finalTime = microtime(true) - $finalStart;
  287. Log::info('generateExportDataAndTotals: Final processing completed', [
  288. 'final_export_records' => count($exportRecords),
  289. 'final_export_totals' => count($exportTotals),
  290. 'final_time' => $finalTime,
  291. 'total_time' => microtime(true) - $loopStart + $queryTime,
  292. 'memory_final' => memory_get_usage(true),
  293. 'memory_peak' => memory_get_peak_usage(true)
  294. ]);
  295. // Cleanup
  296. unset($datas, $groupedData, $causalsCount);
  297. gc_collect_cycles();
  298. Log::info('generateExportDataAndTotals: Completed with cleanup', [
  299. 'memory_after_cleanup' => memory_get_usage(true)
  300. ]);
  301. return ['records' => $exportRecords, 'totals' => $exportTotals];
  302. }
  303. private function generateExportTotals($fromDate, $toDate)
  304. {
  305. $exportTotals = array();
  306. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  307. $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
  308. ->select(
  309. 'records.*',
  310. 'records_rows.id as row_id',
  311. 'records_rows.record_id',
  312. 'records_rows.causal_id',
  313. 'records_rows.amount',
  314. 'records_rows.note',
  315. 'records_rows.when',
  316. 'records_rows.vat_id',
  317. )
  318. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  319. //->where('records.deleted', false)
  320. ->where(function ($query) {
  321. $query->where('records.deleted', false)->orWhereNull('records.deleted');
  322. })
  323. ->whereBetween('date', [$fromDate, $toDate])
  324. ->where(function ($query) {
  325. $query->where('type', 'OUT')
  326. ->orWhere(function ($query) {
  327. $query->where('records.corrispettivo_fiscale', true)
  328. ->orWhere('records.commercial', false);
  329. });
  330. })
  331. ->where(function ($query) use ($exclude_from_records) {
  332. $query->where('type', 'OUT')
  333. ->orWhere(function ($subquery) use ($exclude_from_records) {
  334. $subquery->whereNotIn('member_id', $exclude_from_records);
  335. });
  336. });
  337. if ($this->filterCausals != null && sizeof($this->filterCausals) > 0) {
  338. $causals = array();
  339. foreach ($this->filterCausals as $z) {
  340. $causals[] = $z;
  341. $childs = \App\Models\Causal::where('parent_id', $z)->get();
  342. foreach ($childs as $c) {
  343. $causals[] = $c->id;
  344. $childsX = \App\Models\Causal::where('parent_id', $c->id)->get();
  345. foreach ($childsX as $cX) {
  346. $causals[] = $cX->id;
  347. }
  348. }
  349. }
  350. $datas->whereIn('causal_id', $causals);
  351. }
  352. if ($this->filterPaymentMethods != null && sizeof($this->filterPaymentMethods) > 0) {
  353. $datas->whereIn('payment_method_id', $this->filterPaymentMethods);
  354. }
  355. if ($this->filterOrigins != null && sizeof($this->filterOrigins) > 0) {
  356. $datas->whereIn('origin_id', $this->filterOrigins);
  357. }
  358. if ($this->filterDestinations != null && sizeof($this->filterDestinations) > 0) {
  359. $datas->whereIn('destination_id', $this->filterDestinations);
  360. }
  361. if ($this->filterMember != null && $this->filterMember > 0) {
  362. $datas->where('member_id', $this->filterMember);
  363. }
  364. $datas = $datas->orderBy('date', 'ASC')
  365. ->orderBy('records.created_at', 'ASC')
  366. ->orderBy('records_rows.id', 'ASC')
  367. ->get();
  368. foreach ($datas as $data) {
  369. $causalCheck = \App\Models\Causal::findOrFail($data->causal_id);
  370. $paymentCheck = $data->payment_method->money;
  371. if (!$paymentCheck && ($causalCheck->no_first == null || !$causalCheck->no_first)) {
  372. if (!$data->deleted) {
  373. $amount = $data->amount;
  374. $amount += getVatValue($amount, $data->vat_id);
  375. } else {
  376. $amount = $data->amount;
  377. }
  378. if (!isset($exportTotals[$data->payment_method->name])) {
  379. $exportTotals[$data->payment_method->name]["IN"] = 0;
  380. $exportTotals[$data->payment_method->name]["OUT"] = 0;
  381. }
  382. if (!$data->deleted)
  383. $exportTotals[$data->payment_method->name][$data->type] += $amount;
  384. }
  385. }
  386. return $exportTotals;
  387. }
  388. public function resetFilters()
  389. {
  390. $this->selectedPeriod = 'OGGI';
  391. $this->selectedMonth = date('Y-m');
  392. $this->selectedDay = date('Y-m-d');
  393. $this->selectedYear = date('Y');
  394. $this->showMonthPicker = false;
  395. $this->showDayPicker = false;
  396. $this->filterCausals = [];
  397. $this->filterPaymentMethods = [];
  398. $this->filterOrigins = [];
  399. $this->filterDestinations = [];
  400. $this->filterMember = null;
  401. $today = date("Y-m-d");
  402. $this->fromDate = $today;
  403. $this->toDate = $today;
  404. $this->appliedFromDate = $today;
  405. $this->appliedToDate = $today;
  406. $this->emit('filters-reset');
  407. }
  408. public function applyFilters()
  409. {
  410. $this->isFiltering = true;
  411. $this->setPeriodDates();
  412. $this->appliedFromDate = $this->fromDate;
  413. $this->appliedToDate = $this->toDate;
  414. $this->render();
  415. $this->isFiltering = false;
  416. $this->emit('filters-applied');
  417. }
  418. private function setPeriodDates()
  419. {
  420. $today = now();
  421. switch ($this->selectedPeriod) {
  422. case 'OGGI':
  423. $this->fromDate = $today->format('Y-m-d');
  424. $this->toDate = $today->format('Y-m-d');
  425. break;
  426. case 'IERI':
  427. $yesterday = $today->copy()->subDay();
  428. $this->fromDate = $yesterday->format('Y-m-d');
  429. $this->toDate = $yesterday->format('Y-m-d');
  430. break;
  431. case 'MESE CORRENTE':
  432. $this->fromDate = $today->copy()->startOfMonth()->format('Y-m-d');
  433. $this->toDate = $today->copy()->endOfMonth()->format('Y-m-d');
  434. break;
  435. case 'MESE PRECEDENTE':
  436. $lastMonth = $today->copy()->subMonth();
  437. $this->fromDate = $lastMonth->startOfMonth()->format('Y-m-d');
  438. $this->toDate = $lastMonth->endOfMonth()->format('Y-m-d');
  439. break;
  440. case 'MESE_PERSONALIZZATO':
  441. if (!empty($this->selectedMonth)) {
  442. $firstDay = date('Y-m-01', strtotime($this->selectedMonth . '-01'));
  443. $lastDay = date('Y-m-t', strtotime($this->selectedMonth . '-01'));
  444. $this->fromDate = $firstDay;
  445. $this->toDate = $lastDay;
  446. }
  447. break;
  448. case 'GIORNO_PERSONALIZZATO':
  449. if (!empty($this->selectedDay)) {
  450. $this->fromDate = $this->selectedDay;
  451. $this->toDate = $this->selectedDay;
  452. }
  453. break;
  454. case 'ULTIMO TRIMESTRE':
  455. $this->fromDate = $today->copy()->subMonths(3)->format('Y-m-d');
  456. $this->toDate = $today->format('Y-m-d');
  457. break;
  458. case 'ULTIMO QUADRIMESTRE':
  459. $this->fromDate = $today->copy()->subMonths(4)->format('Y-m-d');
  460. $this->toDate = $today->format('Y-m-d');
  461. break;
  462. }
  463. }
  464. public function getCausals($records, $indentation)
  465. {
  466. foreach ($records as $record) {
  467. $this->causals[] = array('id' => $record->id, 'name' => $record->getTree(), 'text' => $record->getTree(), 'level' => $indentation);
  468. if (count($record->childs))
  469. $this->getCausals($record->childs, $indentation + 1);
  470. }
  471. }
  472. public function getMonth($m)
  473. {
  474. $ret = '';
  475. switch ($m) {
  476. case 1:
  477. $ret = 'Gennaio';
  478. break;
  479. case 2:
  480. $ret = 'Febbraio';
  481. break;
  482. case 3:
  483. $ret = 'Marzo';
  484. break;
  485. case 4:
  486. $ret = 'Aprile';
  487. break;
  488. case 5:
  489. $ret = 'Maggio';
  490. break;
  491. case 6:
  492. $ret = 'Giugno';
  493. break;
  494. case 7:
  495. $ret = 'Luglio';
  496. break;
  497. case 8:
  498. $ret = 'Agosto';
  499. break;
  500. case 9:
  501. $ret = 'Settembre';
  502. break;
  503. case 10:
  504. $ret = 'Ottobre';
  505. break;
  506. case 11:
  507. $ret = 'Novembre';
  508. break;
  509. case 12:
  510. $ret = 'Dicembre';
  511. break;
  512. default:
  513. $ret = '';
  514. break;
  515. }
  516. return $ret;
  517. }
  518. public function loadData($from, $to)
  519. {
  520. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  521. $f = $from != '' ? $from : $this->appliedFromDate;
  522. $t = $to != '' ? $to : $this->appliedToDate;
  523. $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
  524. ->select(
  525. 'records.*',
  526. 'records_rows.id as row_id',
  527. 'records_rows.record_id',
  528. 'records_rows.causal_id',
  529. 'records_rows.amount',
  530. 'records_rows.note',
  531. 'records_rows.when',
  532. 'records_rows.vat_id',
  533. //'causals.name as causal_name',
  534. 'd.name as destination',
  535. 'o.name as origin',
  536. )
  537. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  538. ->join('causals', 'records_rows.causal_id', '=', 'causals.id')
  539. ->leftJoin('banks as d', 'records.destination_id', '=', 'd.id')
  540. ->leftJoin('banks as o', 'records.origin_id', '=', 'o.id')
  541. //->where('records.deleted', false)
  542. /*->where(function ($query) {
  543. $query->where('records.deleted', false)->orWhereNull('records.deleted');
  544. })*/
  545. ->whereBetween('date', [$f . " 00:00:00", $t . " 23:59:59"])
  546. ->where(function ($query) {
  547. $query->where('records.type', 'OUT')
  548. ->orWhere(function ($query) {
  549. $query->where('records.corrispettivo_fiscale', true)
  550. ->orWhere('records.commercial', false);
  551. });
  552. })
  553. ->where(function ($query) use ($exclude_from_records) {
  554. $query->where('records.type', 'OUT')
  555. ->orWhere(function ($subquery) use ($exclude_from_records) {
  556. $subquery->whereNotIn('member_id', $exclude_from_records);
  557. });
  558. });
  559. $financial_movements = \App\Models\FinancialMovement::select(
  560. 'financial_movements.*',
  561. 'causals.name as causal_name',
  562. 'd.name as destination',
  563. 'o.name as origin',
  564. )
  565. ->join('causals', 'financial_movements.causal_id', '=', 'causals.id')
  566. ->leftJoin('banks as d', 'financial_movements.destination_id', '=', 'd.id')
  567. ->leftJoin('banks as o', 'financial_movements.origin_id', '=', 'o.id')
  568. ->whereBetween('date', [$f . " 00:00:00", $t . " 23:59:59"]);
  569. if ($this->filterCausals != null && sizeof($this->filterCausals) > 0) {
  570. $causals = array();
  571. foreach ($this->filterCausals as $z) {
  572. $causals[] = $z;
  573. $childs = \App\Models\Causal::where('parent_id', $z)->get();
  574. foreach ($childs as $c) {
  575. $causals[] = $c->id;
  576. $childsX = \App\Models\Causal::where('parent_id', $c->id)->get();
  577. foreach ($childsX as $cX) {
  578. $causals[] = $cX->id;
  579. }
  580. }
  581. }
  582. $datas->whereIn('causal_id', $causals);
  583. $financial_movements->whereIn('financial_movements.causal_id', $causals);
  584. }
  585. if ($this->filterPaymentMethods != null && sizeof($this->filterPaymentMethods) > 0) {
  586. $datas->whereIn('payment_method_id', $this->filterPaymentMethods);
  587. $financial_movements->whereRaw('0 = 1');
  588. }
  589. if ($this->filterOrigins != null && sizeof($this->filterOrigins) > 0) {
  590. $datas->whereIn('origin_id', $this->filterOrigins);
  591. $financial_movements->whereIn('financial_movements.origin_id', $this->filterOrigins);
  592. }
  593. if ($this->filterDestinations != null && sizeof($this->filterDestinations) > 0) {
  594. $datas->whereIn('destination_id', $this->filterDestinations);
  595. $financial_movements->whereIn('financial_movements.destination_id', $this->filterDestinations);
  596. }
  597. if ($this->filterMember != null && $this->filterMember > 0) {
  598. $datas->where('member_id', $this->filterMember);
  599. $financial_movements->whereRaw('0 = 1');
  600. }
  601. $datas = $datas->orderBy('date', 'ASC')
  602. ->orderBy('records.created_at', 'ASC')
  603. ->orderBy('records_rows.id', 'ASC')
  604. ->get();
  605. $financial_movements = $financial_movements->orderBy('date', 'ASC')
  606. ->orderBy('financial_movements.created_at', 'ASC')
  607. ->get();
  608. $ret = array();
  609. $this->total_in = 0;
  610. $this->total_out = 0;
  611. foreach($datas as $data)
  612. {
  613. $causal = \App\Models\Causal::findOrFail($data->causal_id);
  614. $paymentCheck = $data->payment_method->money;
  615. if (!$paymentCheck && ($causal->no_first == null || !$causal->no_first))
  616. {
  617. $data->causal_name = $causal->getTree();
  618. $ret[] = $data;
  619. if ($data->type == 'IN')
  620. $this->total_in += $data->amount;
  621. if ($data->type == 'OUT')
  622. $this->total_out += $data->amount;
  623. }
  624. }
  625. foreach ($financial_movements as $m) {
  626. // USCITA
  627. $ret[] = (object) [
  628. 'id' => 'fm_out_' . $m->id,
  629. 'date' => $m->date,
  630. 'type' => 'MOVE_OUT',
  631. 'commercial' => false,
  632. 'causal_name' => $m->causal_name,
  633. 'amount' => $m->amount,
  634. 'origin' => $m->origin,
  635. 'destination' => null,
  636. 'deleted' => $m->deleted,
  637. ];
  638. $this->total_out += $m->amount;
  639. // ENTRATA
  640. $ret[] = (object) [
  641. 'id' => 'fm_in_' . $m->id,
  642. 'date' => $m->date,
  643. 'type' => 'MOVE_IN',
  644. 'commercial' => false,
  645. 'causal_name' => $m->causal_name,
  646. 'amount' => $m->amount,
  647. 'origin' => null,
  648. 'destination' => $m->destination,
  649. 'deleted' => $m->deleted,
  650. ];
  651. $this->total_in += $m->amount;
  652. }
  653. usort($ret, function ($a, $b) {
  654. $da = $a->date ? strtotime((string)$a->date) : 0;
  655. $db = $b->date ? strtotime((string)$b->date) : 0;
  656. if ($da !== $db) return $da <=> $db;
  657. $ca = isset($a->created_at) && $a->created_at ? strtotime((string)$a->created_at) : 0;
  658. $cb = isset($b->created_at) && $b->created_at ? strtotime((string)$b->created_at) : 0;
  659. if ($ca !== $cb) return $ca <=> $cb;
  660. return ((int)($a->id ?? 0)) <=> ((int)($b->id ?? 0));
  661. });
  662. if ($this->total_in > 0 || $this->total_out > 0)
  663. {
  664. $ret[] = (object) array("date" => "", "total_in" => $this->total_in, "total_out" => $this->total_out);
  665. }
  666. return $ret;
  667. }
  668. public function render()
  669. {
  670. $month = 0;
  671. $year = 0;
  672. $this->records = array();
  673. $this->totals = array();
  674. $this->causalAmounts = array();
  675. $this->records = $this->loadData('', '');
  676. /*
  677. foreach ($datas as $idx => $data) {
  678. $causalCheck = \App\Models\Causal::findOrFail($data->causal_id);
  679. $paymentCheck = $data->payment_method->money;
  680. if (!$paymentCheck && ($causalCheck->no_first == null || !$causalCheck->no_first)) {
  681. if (!$data->deleted) {
  682. $amount = $data->amount;
  683. if ($data->vat_id > 0)
  684. $amount += getVatValue($amount, $data->vat_id);
  685. } else {
  686. $amount = $data->amount;
  687. }
  688. $isCommercial = ($data->commercial == 1 || $data->commercial === '1' || $data->commercial === true);
  689. $typeLabel = $isCommercial ? 'Commerciale' : 'Non Commerciale';
  690. $nominativo = '';
  691. if ($data->type == "IN") {
  692. if ($data->member) {
  693. $nominativo = $data->member->last_name . " " . $data->member->first_name;
  694. }
  695. if ($data->payment_method_id == 7)
  696. $values[] = array('id' => $data->record_id, 'amount' => $amount);
  697. } else {
  698. if ($data->supplier) {
  699. $nominativo = $data->supplier->name;
  700. }
  701. }
  702. $groupKey = $data->date . '|' . $typeLabel . '|' . $data->payment_method->name . '|' . $data->type . '|' . $nominativo;
  703. if (!isset($groupedData[$groupKey])) {
  704. $groupedData[$groupKey] = [
  705. 'date' => $data->date,
  706. 'type_label' => $typeLabel,
  707. 'payment_method' => $data->payment_method->name,
  708. 'transaction_type' => $data->type,
  709. 'nominativo' => $nominativo,
  710. 'amount' => 0,
  711. 'deleted' => false,
  712. 'causals' => [],
  713. 'notes' => []
  714. ];
  715. $causalsCount[$groupKey] = [];
  716. $causalsAmounts[$groupKey] = []; // Initialize causal amounts for this group
  717. $nominativi[$groupKey] = $nominativo;
  718. }
  719. $groupedData[$groupKey]['amount'] += $amount;
  720. $causalsCount[$groupKey][$causalCheck->getTree()] = true;
  721. $causalName = $causalCheck->getTree();
  722. if (!isset($causalsAmounts[$groupKey][$causalName])) {
  723. $causalsAmounts[$groupKey][$causalName] = 0;
  724. }
  725. $causalsAmounts[$groupKey][$causalName] += $amount;
  726. if (!empty($data->note)) {
  727. $groupedData[$groupKey]['notes'][] = $data->note;
  728. }
  729. if ($data->deleted) {
  730. $groupedData[$groupKey]['deleted'] = true;
  731. }
  732. }
  733. }
  734. Log::info('values', [$values]);
  735. foreach ($groupedData as $groupKey => $group) {
  736. $causalsInGroup = array_keys($causalsCount[$groupKey]);
  737. $causalDisplay = $group['type_label'];
  738. if (count($causalsInGroup) > 1) {
  739. $causalAmountsForJs = [];
  740. foreach ($causalsInGroup as $causalName) {
  741. $causalAmountsForJs[] = $causalName . ':::' . formatPrice($causalsAmounts[$groupKey][$causalName]);
  742. }
  743. $detailDisplay = 'Varie|' . implode('|', $causalAmountsForJs);
  744. } else {
  745. $detailDisplay = $causalsInGroup[0];
  746. }
  747. $recordKey = $group['date'] . "§" . $causalDisplay . "§" . $group['nominativo'] . "§" . $detailDisplay . "§" . ($group['deleted'] ? 'DELETED' : '') . "§";
  748. if (!isset($this->records[$recordKey][$group['payment_method']][$group['transaction_type']])) {
  749. $this->records[$recordKey][$group['payment_method']][$group['transaction_type']] = 0;
  750. }
  751. $this->records[$recordKey][$group['payment_method']][$group['transaction_type']] += $group['amount'];
  752. if (!isset($this->totals[$group['payment_method']])) {
  753. $this->totals[$group['payment_method']]["IN"] = 0;
  754. $this->totals[$group['payment_method']]["OUT"] = 0;
  755. }
  756. if (!$group['deleted'])
  757. $this->totals[$group['payment_method']][$group['transaction_type']] += $group['amount'];
  758. }
  759. */
  760. return view('livewire.records');
  761. }
  762. private function getLabels($fromDate, $toDate)
  763. {
  764. $begin = new DateTime($fromDate);
  765. $end = new DateTime($toDate);
  766. $interval = DateInterval::createFromDateString('1 day');
  767. $date_range = new DatePeriod($begin, $interval, $end);
  768. foreach ($date_range as $date) {
  769. $labels[] = $date->format('d/M');
  770. }
  771. return $labels;
  772. }
  773. private function getRecordData($type, $fromDate, $toDate)
  774. {
  775. $data = [];
  776. $begin = new DateTime($fromDate);
  777. $end = new DateTime($toDate);
  778. $interval = DateInterval::createFromDateString('1 day');
  779. $date_range = new DatePeriod($begin, $interval, $end);
  780. foreach ($date_range as $date) {
  781. if ($type == 'IN') {
  782. $found = false;
  783. foreach ($this->in as $in) {
  784. if (date("Y-m-d", strtotime($in->date)) == $date->format('Y-m-d')) {
  785. $data[] = number_format($in->total, 0, "", "");
  786. $found = true;
  787. }
  788. }
  789. if (!$found)
  790. $data[] = 0;
  791. }
  792. if ($type == 'OUT') {
  793. $found = false;
  794. foreach ($this->out as $out) {
  795. if (date("Y-m-d", strtotime($out->date)) == $date->format('Y-m-d')) {
  796. $data[] = number_format($out->total, 0, "", "");
  797. $found = true;
  798. }
  799. }
  800. if (!$found)
  801. $data[] = 0;
  802. }
  803. }
  804. return $data;
  805. }
  806. public function openExportModal()
  807. {
  808. $this->exportFromDate = $this->appliedFromDate;
  809. $this->exportToDate = $this->appliedToDate;
  810. // Reset email options
  811. $this->sendViaEmail = false;
  812. $this->exportEmailAddress = $this->getPreferredEmail();
  813. $this->updateEmailSubject();
  814. $this->emit('show-export-modal');
  815. }
  816. public function exportWithDateRange()
  817. {
  818. Log::info('=== EXPORT START ===', [
  819. 'user_id' => auth()->id(),
  820. 'from_date' => $this->exportFromDate,
  821. 'to_date' => $this->exportToDate,
  822. 'send_via_email' => $this->sendViaEmail,
  823. 'memory_usage' => memory_get_usage(true),
  824. 'memory_peak' => memory_get_peak_usage(true),
  825. 'time_limit' => ini_get('max_execution_time')
  826. ]);
  827. $this->isExporting = true;
  828. $this->emit('$refresh');
  829. usleep(100000);
  830. if ($this->sendViaEmail) {
  831. Log::info('Export: Validating email fields');
  832. $this->validate([
  833. 'exportEmailAddress' => 'required|email',
  834. 'exportEmailSubject' => 'required|string|max:255',
  835. ]);
  836. Log::info('Export: Email validation passed');
  837. }
  838. $this->isExporting = true;
  839. try {
  840. Log::info('Export: Starting COMBINED data generation phase (NO SEPARATE CALLS)');
  841. $startTime = microtime(true);
  842. // *** THIS IS THE KEY CHANGE - USE ONLY THE COMBINED METHOD ***
  843. $result = $this->generateExportDataAndTotals($this->exportFromDate, $this->exportToDate);
  844. $exportRecords = $result['records'];
  845. $exportTotals = $result['totals'];
  846. Log::info('TOTALS', [$exportTotals]);
  847. $dataGenTime = microtime(true) - $startTime;
  848. Log::info('Export: COMBINED data generation completed (NO SEPARATE TOTALS CALL)', [
  849. 'records_count' => count($exportRecords),
  850. 'totals_count' => count($exportTotals),
  851. 'generation_time' => $dataGenTime,
  852. 'memory_usage' => memory_get_usage(true),
  853. 'memory_peak' => memory_get_peak_usage(true)
  854. ]);
  855. if ($this->sendViaEmail) {
  856. Log::info('Export: Dispatching to background job');
  857. //$this->dispatchExportJob($exportRecords, $exportTotals);
  858. Log::info('Export: Job dispatched successfully');
  859. } else {
  860. Log::info('Export: Starting direct download export');
  861. $exportStartTime = microtime(true);
  862. $result = $this->exportWithData($exportRecords, $exportTotals);
  863. $exportTime = microtime(true) - $exportStartTime;
  864. Log::info('Export: Direct export completed', [
  865. 'export_time' => $exportTime,
  866. 'total_time' => microtime(true) - $startTime,
  867. 'memory_usage' => memory_get_usage(true),
  868. 'memory_peak' => memory_get_peak_usage(true)
  869. ]);
  870. return $result;
  871. }
  872. } catch (\Illuminate\Validation\ValidationException $e) {
  873. Log::error('Export: Validation error', [
  874. 'error' => $e->getMessage(),
  875. 'errors' => $e->errors()
  876. ]);
  877. $this->isExporting = false;
  878. throw $e;
  879. } catch (\Exception $e) {
  880. Log::error('Export: General error', [
  881. 'error' => $e->getMessage(),
  882. 'trace' => $e->getTraceAsString(),
  883. 'memory_usage' => memory_get_usage(true),
  884. 'memory_peak' => memory_get_peak_usage(true),
  885. 'execution_time' => microtime(true) - ($startTime ?? 0)
  886. ]);
  887. $this->isExporting = false;
  888. if ($this->sendViaEmail) {
  889. $this->emit('export-email-error', 'Errore durante l\'invio dell\'email: ' . $e->getMessage());
  890. } else {
  891. session()->flash('error', 'Errore durante l\'export: ' . $e->getMessage());
  892. }
  893. } finally {
  894. Log::info('Export: Cleanup phase');
  895. $this->isExporting = false;
  896. $this->emit('export-complete');
  897. $this->emit('hide-export-modal');
  898. Log::info('=== EXPORT END ===');
  899. }
  900. }
  901. public function exportData()
  902. {
  903. $this->isExporting = true;
  904. $this->emit('$refresh');
  905. usleep(100000);
  906. if ($this->sendViaEmail) {
  907. Log::info('Export: Validating email fields');
  908. $this->validate([
  909. 'exportEmailAddress' => 'required|email',
  910. 'exportEmailSubject' => 'required|string|max:255',
  911. ]);
  912. Log::info('Export: Email validation passed');
  913. }
  914. $this->isExporting = true;
  915. try {
  916. Log::info('Export: Starting COMBINED data generation phase (NO SEPARATE CALLS)');
  917. $startTime = microtime(true);
  918. $records = $this->loadData($this->exportFromDate, $this->exportToDate);
  919. $dataGenTime = microtime(true) - $startTime;
  920. if ($this->sendViaEmail) {
  921. Log::info('Export: Dispatching to background job');
  922. // Invio mail
  923. $this->dispatchExportJob($records);
  924. Log::info('Export: Job dispatched successfully');
  925. } else {
  926. Log::info('Export: Starting direct download export');
  927. $exportStartTime = microtime(true);
  928. // Creo excel e scarico
  929. $result = $this->exportExcel($records);
  930. $exportTime = microtime(true) - $exportStartTime;
  931. return $result;
  932. }
  933. } catch (\Illuminate\Validation\ValidationException $e) {
  934. Log::error('Export: Validation error', [
  935. 'error' => $e->getMessage(),
  936. 'errors' => $e->errors()
  937. ]);
  938. $this->isExporting = false;
  939. throw $e;
  940. } catch (\Exception $e) {
  941. Log::error('Export: General error', [
  942. 'error' => $e->getMessage(),
  943. 'trace' => $e->getTraceAsString(),
  944. 'memory_usage' => memory_get_usage(true),
  945. 'memory_peak' => memory_get_peak_usage(true),
  946. 'execution_time' => microtime(true) - ($startTime ?? 0)
  947. ]);
  948. $this->isExporting = false;
  949. if ($this->sendViaEmail) {
  950. $this->emit('export-email-error', 'Errore durante l\'invio dell\'email: ' . $e->getMessage());
  951. } else {
  952. session()->flash('error', 'Errore durante l\'export: ' . $e->getMessage());
  953. }
  954. } finally {
  955. Log::info('Export: Cleanup phase');
  956. $this->isExporting = false;
  957. $this->emit('export-complete');
  958. $this->emit('hide-export-modal');
  959. Log::info('=== EXPORT END ===');
  960. }
  961. }
  962. private function getEstimatedRecordCount($fromDate, $toDate)
  963. {
  964. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  965. $query = \App\Models\Record::join('records_rows', 'records.id', '=', 'records_rows.record_id')
  966. ->whereBetween('date', [$fromDate, $toDate])
  967. ->where(function ($query) {
  968. $query->where('type', 'OUT')
  969. ->orWhere(function ($query) {
  970. $query->where('records.corrispettivo_fiscale', true)
  971. ->orWhere('records.commercial', false);
  972. });
  973. })
  974. ->where(function ($query) use ($exclude_from_records) {
  975. $query->where('type', 'OUT')
  976. ->orWhere(function ($subquery) use ($exclude_from_records) {
  977. $subquery->whereNotIn('member_id', $exclude_from_records);
  978. });
  979. });
  980. if ($this->filterCausals != null && sizeof($this->filterCausals) > 0) {
  981. $causals = array();
  982. foreach ($this->filterCausals as $z) {
  983. $causals[] = $z;
  984. $childs = \App\Models\Causal::where('parent_id', $z)->get();
  985. foreach ($childs as $c) {
  986. $causals[] = $c->id;
  987. $childsX = \App\Models\Causal::where('parent_id', $c->id)->get();
  988. foreach ($childsX as $cX) {
  989. $causals[] = $cX->id;
  990. }
  991. }
  992. }
  993. $query->whereIn('causal_id', $causals);
  994. }
  995. if ($this->filterPaymentMethods != null && sizeof($this->filterPaymentMethods) > 0) {
  996. $query->whereIn('payment_method_id', $this->filterPaymentMethods);
  997. }
  998. if ($this->filterOrigins != null && sizeof($this->filterOrigins) > 0) {
  999. $query->whereIn('origin_id', $this->filterOrigins);
  1000. }
  1001. if ($this->filterDestinations != null && sizeof($this->filterDestinations) > 0) {
  1002. $query->whereIn('destination_id', $this->filterDestinations);
  1003. }
  1004. if ($this->filterMember != null && $this->filterMember > 0) {
  1005. $query->where('member_id', $this->filterMember);
  1006. }
  1007. return $query->count();
  1008. }
  1009. private function getMemberName($memberId)
  1010. {
  1011. $member = \App\Models\Member::find($memberId);
  1012. return $member ? $member->last_name . ' ' . $member->first_name : 'Sconosciuto';
  1013. }
  1014. private function getCausalsNames($causalIds)
  1015. {
  1016. if (!is_array($causalIds)) {
  1017. return null;
  1018. }
  1019. $causals = \App\Models\Causal::whereIn('id', $causalIds)->pluck('name')->toArray();
  1020. return implode(', ', $causals);
  1021. }
  1022. private function getPaymentMethodsNames($paymentMethodIds)
  1023. {
  1024. if (!is_array($paymentMethodIds)) {
  1025. return null;
  1026. }
  1027. $payment_methods = \App\Models\PaymentMethod::whereIn('id', $paymentMethodIds)->pluck('name')->toArray();
  1028. return implode(', ', $payment_methods);
  1029. }
  1030. private function getOriginsNames($originIds)
  1031. {
  1032. if (!is_array($originIds)) {
  1033. return null;
  1034. }
  1035. $origins = \App\Models\Bank::whereIn('id', $originIds)->pluck('name')->toArray();
  1036. return implode(', ', $origins);
  1037. }
  1038. private function getDestinationsNames($destinationIds)
  1039. {
  1040. if (!is_array($destinationIds)) {
  1041. return null;
  1042. }
  1043. $destinations = \App\Models\Bank::whereIn('id', $destinationIds)->pluck('name')->toArray();
  1044. return implode(', ', $destinations);
  1045. }
  1046. public function updatedExportFromDate()
  1047. {
  1048. $this->updateEmailSubject();
  1049. }
  1050. public function updatedExportToDate()
  1051. {
  1052. $this->updateEmailSubject();
  1053. }
  1054. public function updatedSendViaEmail($value)
  1055. {
  1056. if ($value && empty($this->exportEmailAddress)) {
  1057. $this->exportEmailAddress = $this->getPreferredEmail();
  1058. }
  1059. }
  1060. public function resetEmailForm()
  1061. {
  1062. $this->sendViaEmail = false;
  1063. $this->exportEmailAddress = $this->getPreferredEmail();
  1064. $this->updateEmailSubject();
  1065. }
  1066. private function updateEmailSubject()
  1067. {
  1068. if (!empty($this->exportFromDate) && !empty($this->exportToDate)) {
  1069. $fromFormatted = date('d/m/Y', strtotime($this->exportFromDate));
  1070. $toFormatted = date('d/m/Y', strtotime($this->exportToDate));
  1071. if ($this->exportFromDate === $this->exportToDate) {
  1072. $this->exportEmailSubject = "Prima Nota - Export del {$fromFormatted}";
  1073. } else {
  1074. $this->exportEmailSubject = "Prima Nota - Export dal {$fromFormatted} al {$toFormatted}";
  1075. }
  1076. }
  1077. }
  1078. /**
  1079. * Dispatch export job to queue
  1080. */
  1081. //private function dispatchExportJob($exportRecords, $exportTotals)
  1082. private function dispatchExportJob($records)
  1083. {
  1084. try {
  1085. // Prepare filter descriptions for the job
  1086. $filterDescriptions = [
  1087. 'member' => $this->filterMember ? $this->getMemberName($this->filterMember) : null,
  1088. 'causals' => $this->filterCausals ? $this->getCausalsNames($this->filterCausals) : null,
  1089. 'payment_methods' => $this->filterPaymentMethods ? $this->getPaymentMethodsNames($this->filterPaymentMethods) : null,
  1090. 'origins' => $this->filterOrigins ? $this->getOriginsNames($this->filterOrigins) : null,
  1091. 'destinations' => $this->filterDestinations ? $this->getDestinationsNames($this->filterDestinations) : null,
  1092. ];
  1093. $paymentsArray = $this->payments->map(function ($payment) {
  1094. return [
  1095. 'id' => $payment->id,
  1096. 'name' => $payment->name,
  1097. 'type' => $payment->type
  1098. ];
  1099. })->toArray();
  1100. // Dispatch job to background queue
  1101. ExportPrimaNota::dispatch(
  1102. /*$exportRecords,
  1103. $exportTotals,*/
  1104. $records,
  1105. $this->exportEmailAddress,
  1106. $this->exportEmailSubject,
  1107. [
  1108. 'from' => date('d/m/Y', strtotime($this->exportFromDate)),
  1109. 'to' => date('d/m/Y', strtotime($this->exportToDate))
  1110. ],
  1111. auth()->id(),
  1112. $paymentsArray,
  1113. $filterDescriptions
  1114. );
  1115. $this->emit('export-email-queued');
  1116. session()->flash('success', 'Export in corso! Riceverai l\'email a breve alla casella: ' . $this->exportEmailAddress);
  1117. Log::info('Export job dispatched', [
  1118. 'user_id' => auth()->id(),
  1119. 'email' => $this->exportEmailAddress,
  1120. 'date_range' => [$this->exportFromDate, $this->exportToDate],
  1121. 'total_records' => count($this->records)
  1122. ]);
  1123. } catch (\Exception $e) {
  1124. Log::error('Failed to dispatch export job', [
  1125. 'user_id' => auth()->id(),
  1126. 'email' => $this->exportEmailAddress,
  1127. 'error' => $e->getMessage()
  1128. ]);
  1129. throw new \Exception('Errore nell\'avvio dell\'export: ' . $e->getMessage());
  1130. }
  1131. }
  1132. function export()
  1133. {
  1134. $result = $this->generateExportDataAndTotals($this->exportFromDate, $this->exportToDate);
  1135. $exportRecords = $result['records'];
  1136. $exportTotals = $result['totals'];
  1137. return $this->exportWithData($exportRecords, $exportTotals);
  1138. }
  1139. private function exportWithData($exportRecords, $exportTotals)
  1140. {
  1141. Log::info('exportWithData: Starting Excel generation', [
  1142. 'records_count' => count($exportRecords),
  1143. 'totals_count' => count($exportTotals),
  1144. 'memory_before' => memory_get_usage(true)
  1145. ]);
  1146. $startTime = microtime(true);
  1147. Log::info('exportWithData: Setting memory and GC');
  1148. ini_set('memory_limit', '512M');
  1149. gc_enable();
  1150. Log::info('exportWithData: Preparing column letters');
  1151. $letters = array('F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA');
  1152. Log::info('exportWithData: Creating spreadsheet object');
  1153. $spreadsheet = new Spreadsheet();
  1154. $activeWorksheet = $spreadsheet->getActiveSheet();
  1155. Log::info('exportWithData: Setting basic headers');
  1156. $activeWorksheet->setCellValue('A1', "Data");
  1157. $activeWorksheet->setCellValue('B1', "Tipologia");
  1158. $activeWorksheet->setCellValue('C1', "Causale");
  1159. $activeWorksheet->setCellValue('D1', "Nominativo");
  1160. $activeWorksheet->setCellValue('E1', "Stato");
  1161. $activeWorksheet->getStyle('A1:Q1')->getFill()
  1162. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  1163. ->getStartColor()->setARGB('FF0C6197');
  1164. $activeWorksheet->getStyle('A1:Q1')->getFont()->getColor()->setARGB('FFFFFFFF');
  1165. Log::info('exportWithData: Setting payment method headers');
  1166. $idx = 0;
  1167. foreach ($this->payments as $p) {
  1168. if ($idx >= count($letters)) {
  1169. Log::warning('exportWithData: Reached letter limit', ['payment_index' => $idx]);
  1170. break;
  1171. }
  1172. Log::debug('exportWithData: Setting payment header', [
  1173. 'payment_name' => $p->name,
  1174. 'column_index' => $idx,
  1175. 'column_letter' => $letters[$idx]
  1176. ]);
  1177. $activeWorksheet->setCellValue($letters[$idx] . '1', $p->name);
  1178. $idx++;
  1179. if ($idx >= count($letters)) {
  1180. break;
  1181. }
  1182. $activeWorksheet->mergeCells($letters[$idx] . '1:' . $letters[$idx] . '1');
  1183. $idx++;
  1184. }
  1185. Log::info('exportWithData: Setting sub-headers');
  1186. $idx = 0;
  1187. $activeWorksheet->setCellValue('A2', "");
  1188. $activeWorksheet->setCellValue('B2', "");
  1189. $activeWorksheet->setCellValue('C2', "");
  1190. $activeWorksheet->setCellValue('D2', "");
  1191. $activeWorksheet->setCellValue('E2', "");
  1192. foreach ($this->payments as $p) {
  1193. if ($p->type == 'ALL') {
  1194. if ($idx >= count($letters)) {
  1195. break;
  1196. }
  1197. $activeWorksheet->setCellValue($letters[$idx] . '2', "Entrate");
  1198. $idx++;
  1199. $activeWorksheet->setCellValue($letters[$idx] . '2', "Uscite");
  1200. $idx++;
  1201. } elseif ($p->type == 'IN') {
  1202. if ($idx >= count($letters)) {
  1203. break;
  1204. }
  1205. $activeWorksheet->setCellValue($letters[$idx] . '2', "Entrate");
  1206. $idx++;
  1207. $activeWorksheet->setCellValue($letters[$idx] . '2', "");
  1208. $idx++;
  1209. } elseif ($p->type == 'OUT') {
  1210. if ($idx >= count($letters)) {
  1211. break;
  1212. }
  1213. $activeWorksheet->setCellValue($letters[$idx] . '2', "");
  1214. $idx++;
  1215. $activeWorksheet->setCellValue($letters[$idx] . '2', "Uscite");
  1216. $idx++;
  1217. }
  1218. }
  1219. Log::info('exportWithData: Applying header styles');
  1220. $activeWorksheet->getStyle('A1:Q1')->getFont()->setBold(true);
  1221. $activeWorksheet->getStyle('A2:Q2')->getFont()->setBold(true);
  1222. Log::info('exportWithData: Starting data row processing');
  1223. $count = 3;
  1224. $batchSize = 1000;
  1225. $recordsProcessed = 0;
  1226. $totalRecords = count($exportRecords);
  1227. Log::info('exportWithData: Processing records in batches', [
  1228. 'total_records' => $totalRecords,
  1229. 'batch_size' => $batchSize
  1230. ]);
  1231. $recordsArray = array_chunk($exportRecords, $batchSize, true);
  1232. Log::info('exportWithData: Created batches', ['batch_count' => count($recordsArray)]);
  1233. foreach ($recordsArray as $batchIndex => $recordsBatch) {
  1234. Log::info('exportWithData: Processing batch', [
  1235. 'batch_index' => $batchIndex,
  1236. 'batch_size' => count($recordsBatch),
  1237. 'memory_current' => memory_get_usage(true),
  1238. 'time_elapsed' => microtime(true) - $startTime
  1239. ]);
  1240. foreach ($recordsBatch as $causal => $record) {
  1241. if ($recordsProcessed % 250 == 0) {
  1242. Log::info('exportWithData: Record processing progress', [
  1243. 'processed' => $recordsProcessed,
  1244. 'total' => $totalRecords,
  1245. 'current_row' => $count,
  1246. 'memory_usage' => memory_get_usage(true),
  1247. 'time_elapsed' => microtime(true) - $startTime
  1248. ]);
  1249. }
  1250. try {
  1251. $check = $causal;
  1252. $parts = explode("§", $check);
  1253. $d = $parts[0] ?? '';
  1254. $c = $parts[1] ?? '';
  1255. $j = $parts[2] ?? '';
  1256. $det = $parts[3] ?? '';
  1257. $deleted = $parts[4] ?? '';
  1258. $detailParts = explode('|', $det);
  1259. $exportDetail = count($detailParts) > 1 ? implode(', ', array_slice($detailParts, 1)) : $det;
  1260. Log::debug('exportWithData: Setting row cells', ['row' => $count]);
  1261. $activeWorksheet->setCellValue('A' . $count, !empty($d) ? date("d/m/Y", strtotime($d)) : '');
  1262. $activeWorksheet->setCellValue('B' . $count, $c);
  1263. $activeWorksheet->setCellValue('C' . $count, $exportDetail);
  1264. $activeWorksheet->setCellValue('D' . $count, $j);
  1265. $stato = ($deleted === 'DELETED') ? 'ANNULLATA' : '';
  1266. $activeWorksheet->setCellValue('E' . $count, $stato);
  1267. if ($stato === 'ANNULLATA') {
  1268. $activeWorksheet->getStyle('E' . $count)->getFont()->getColor()->setARGB('FFFF0000');
  1269. }
  1270. $idx = 0;
  1271. foreach ($this->payments as $p) {
  1272. if ($idx >= count($letters) - 1) {
  1273. break;
  1274. }
  1275. if (isset($record[$p->name])) {
  1276. $inValue = isset($record[$p->name]["IN"]) ? formatPrice($record[$p->name]["IN"]) : "";
  1277. $outValue = isset($record[$p->name]["OUT"]) ? formatPrice($record[$p->name]["OUT"]) : "";
  1278. $activeWorksheet->setCellValue($letters[$idx] . $count, $inValue);
  1279. $idx++;
  1280. $activeWorksheet->setCellValue($letters[$idx] . $count, $outValue);
  1281. $idx++;
  1282. } else {
  1283. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  1284. $idx++;
  1285. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  1286. $idx++;
  1287. }
  1288. }
  1289. $count++;
  1290. $recordsProcessed++;
  1291. if ($recordsProcessed % 500 === 0) {
  1292. Log::debug('exportWithData: Garbage collection');
  1293. gc_collect_cycles();
  1294. }
  1295. } catch (\Exception $e) {
  1296. Log::error('exportWithData: Error processing record row', [
  1297. 'row' => $count,
  1298. 'causal' => $causal,
  1299. 'error' => $e->getMessage(),
  1300. 'processed_so_far' => $recordsProcessed
  1301. ]);
  1302. throw $e;
  1303. }
  1304. }
  1305. Log::info('exportWithData: Batch completed', [
  1306. 'batch_index' => $batchIndex,
  1307. 'records_in_batch' => count($recordsBatch),
  1308. 'total_processed' => $recordsProcessed
  1309. ]);
  1310. unset($recordsBatch);
  1311. gc_collect_cycles();
  1312. }
  1313. Log::info('exportWithData: Adding totals row');
  1314. $count++;
  1315. $idx = 0;
  1316. $activeWorksheet->setCellValue('A' . $count, 'Totale');
  1317. $activeWorksheet->setCellValue('B' . $count, '');
  1318. $activeWorksheet->setCellValue('C' . $count, '');
  1319. $activeWorksheet->setCellValue('D' . $count, '');
  1320. $activeWorksheet->setCellValue('E' . $count, '');
  1321. foreach ($this->payments as $p) {
  1322. if ($idx >= count($letters) - 1) {
  1323. break;
  1324. }
  1325. if (isset($exportTotals[$p->name])) {
  1326. if ($p->type == 'ALL') {
  1327. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($exportTotals[$p->name]["IN"] ?? 0));
  1328. $idx++;
  1329. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($exportTotals[$p->name]["OUT"] ?? 0));
  1330. $idx++;
  1331. } elseif ($p->type == 'IN') {
  1332. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($exportTotals[$p->name]["IN"] ?? 0));
  1333. $idx++;
  1334. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  1335. $idx++;
  1336. } elseif ($p->type == 'OUT') {
  1337. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  1338. $idx++;
  1339. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($exportTotals[$p->name]["OUT"] ?? 0));
  1340. $idx++;
  1341. }
  1342. } else {
  1343. if ($p->type == 'ALL') {
  1344. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  1345. $idx++;
  1346. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  1347. $idx++;
  1348. } elseif ($p->type == 'IN') {
  1349. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  1350. $idx++;
  1351. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  1352. $idx++;
  1353. } elseif ($p->type == 'OUT') {
  1354. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  1355. $idx++;
  1356. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  1357. $idx++;
  1358. }
  1359. }
  1360. }
  1361. Log::info('exportWithData: Applying final styles');
  1362. $activeWorksheet->getStyle('A' . $count . ':Q' . $count)->getFont()->setBold(true);
  1363. Log::info('exportWithData: Setting column dimensions');
  1364. $activeWorksheet->getColumnDimension('A')->setWidth(20);
  1365. $activeWorksheet->getColumnDimension('B')->setWidth(40);
  1366. $activeWorksheet->getColumnDimension('C')->setWidth(40);
  1367. $activeWorksheet->getColumnDimension('D')->setWidth(40);
  1368. $activeWorksheet->getColumnDimension('E')->setWidth(20);
  1369. foreach ($letters as $l) {
  1370. $activeWorksheet->getColumnDimension($l)->setWidth(20);
  1371. }
  1372. $filename = 'prima_nota_' . date("YmdHis") . '.xlsx';
  1373. Log::info('exportWithData: Preparing to save file', [
  1374. 'filename' => $filename,
  1375. 'total_processing_time' => microtime(true) - $startTime,
  1376. 'memory_before_save' => memory_get_usage(true)
  1377. ]);
  1378. try {
  1379. $currentClient = session('currentClient', 'default');
  1380. $tempPath = sys_get_temp_dir() . '/' . $filename;
  1381. Log::info('exportWithData: Creating Excel writer');
  1382. $writer = new Xlsx($spreadsheet);
  1383. Log::info('exportWithData: Saving to temp path', ['temp_path' => $tempPath]);
  1384. $writerStart = microtime(true);
  1385. $writer->save($tempPath);
  1386. $writerTime = microtime(true) - $writerStart;
  1387. Log::info('exportWithData: File saved to temp', [
  1388. 'writer_time' => $writerTime,
  1389. 'file_size' => file_exists($tempPath) ? filesize($tempPath) : 'unknown',
  1390. 'memory_after_save' => memory_get_usage(true)
  1391. ]);
  1392. unset($spreadsheet, $activeWorksheet, $writer);
  1393. gc_collect_cycles();
  1394. Log::info('exportWithData: Uploading to S3');
  1395. $disk = Storage::disk('s3');
  1396. $s3Path = $currentClient . '/prima_nota/' . $filename;
  1397. $primaNotaFolderPath = $currentClient . '/prima_nota/.gitkeep';
  1398. if (!$disk->exists($primaNotaFolderPath)) {
  1399. $disk->put($primaNotaFolderPath, '');
  1400. Log::info("Created prima_nota folder for client: {$currentClient}");
  1401. }
  1402. $uploadStart = microtime(true);
  1403. $fileContent = file_get_contents($tempPath);
  1404. $uploaded = $disk->put($s3Path, $fileContent, 'private');
  1405. $uploadTime = microtime(true) - $uploadStart;
  1406. if (!$uploaded) {
  1407. throw new \Exception('Failed to upload file to Wasabi S3');
  1408. }
  1409. Log::info("Export completed successfully", [
  1410. 'client' => $currentClient,
  1411. 'path' => $s3Path,
  1412. 'file_size' => filesize($tempPath),
  1413. 'records_processed' => $recordsProcessed,
  1414. 'upload_time' => $uploadTime,
  1415. 'total_time' => microtime(true) - $startTime,
  1416. 'memory_peak' => memory_get_peak_usage(true)
  1417. ]);
  1418. if (file_exists($tempPath)) {
  1419. unlink($tempPath);
  1420. }
  1421. $downloadUrl = $disk->temporaryUrl($s3Path, now()->addHour());
  1422. return redirect($downloadUrl);
  1423. } catch (\Exception $e) {
  1424. Log::error('Export S3 error - falling back to local', [
  1425. 'error' => $e->getMessage(),
  1426. 'trace' => $e->getTraceAsString(),
  1427. 'client' => session('currentClient', 'unknown'),
  1428. 'filename' => $filename,
  1429. 'records_processed' => $recordsProcessed ?? 0,
  1430. 'time_elapsed' => microtime(true) - $startTime
  1431. ]);
  1432. // Fallback logic remains the same...
  1433. $currentClient = session('currentClient', 'default');
  1434. $clientFolder = storage_path('app/prima_nota/' . $currentClient);
  1435. if (!is_dir($clientFolder)) {
  1436. mkdir($clientFolder, 0755, true);
  1437. Log::info("Created local client prima_nota folder: {$clientFolder}");
  1438. }
  1439. $localPath = $clientFolder . '/' . $filename;
  1440. if (isset($tempPath) && file_exists($tempPath)) {
  1441. rename($tempPath, $localPath);
  1442. } else {
  1443. $writer = new Xlsx($spreadsheet);
  1444. $writer->save($localPath);
  1445. unset($spreadsheet, $activeWorksheet, $writer);
  1446. }
  1447. gc_collect_cycles();
  1448. Log::warning("Export saved locally due to S3 error", [
  1449. 'client' => $currentClient,
  1450. 'local_path' => $localPath,
  1451. 'error' => $e->getMessage()
  1452. ]);
  1453. session()->flash('warning', 'File salvato localmente a causa di un errore del cloud storage.');
  1454. return response()->download($localPath)->deleteFileAfterSend();
  1455. }
  1456. }
  1457. private function getPreferredEmail()
  1458. {
  1459. $email = auth()->user()->email ?? null;
  1460. if (empty($email)) {
  1461. $email = session('user_email', null);
  1462. }
  1463. if (empty($email)) {
  1464. $member = \App\Models\Member::where('user_id', auth()->id())->first();
  1465. $email = $member ? $member->email : null;
  1466. }
  1467. if (empty($email)) {
  1468. $email = config('mail.default_recipient', '');
  1469. }
  1470. return $email;
  1471. }
  1472. public function updatedSelectedPeriod($value) {
  1473. $this->setPeriodDates();
  1474. $this->applyFilters();
  1475. }
  1476. public function updatedSelectedDay($value)
  1477. {
  1478. if (!empty($value)) {
  1479. $this->selectedPeriod = 'GIORNO_PERSONALIZZATO';
  1480. $this->fromDate = $value;
  1481. $this->toDate = $value;
  1482. $this->applyFilters();
  1483. }
  1484. }
  1485. public function selectDay($day)
  1486. {
  1487. $this->selectedDay = $day;
  1488. $this->showDayPicker = false;
  1489. $this->updatedSelectedDay($day);
  1490. }
  1491. public function toggleDayPicker()
  1492. {
  1493. $this->showDayPicker = !$this->showDayPicker;
  1494. }
  1495. public function selectToday()
  1496. {
  1497. $today = date('Y-m-d');
  1498. $this->selectDay($today);
  1499. }
  1500. public function selectYesterday()
  1501. {
  1502. $yesterday = date('Y-m-d', strtotime('-1 day'));
  1503. $this->selectDay($yesterday);
  1504. }
  1505. // Updated month methods to work with both custom month and day
  1506. public function updatedSelectedMonth($value)
  1507. {
  1508. if (!empty($value)) {
  1509. $this->selectedPeriod = 'MESE_PERSONALIZZATO';
  1510. $firstDay = date('Y-m-01', strtotime($value . '-01'));
  1511. $lastDay = date('Y-m-t', strtotime($value . '-01'));
  1512. $this->fromDate = $firstDay;
  1513. $this->toDate = $lastDay;
  1514. $this->applyFilters();
  1515. }
  1516. }
  1517. public function selectMonth($month)
  1518. {
  1519. $this->selectedMonth = $month;
  1520. $this->showMonthPicker = false;
  1521. $this->updatedSelectedMonth($month);
  1522. }
  1523. public function toggleMonthPicker()
  1524. {
  1525. $this->showMonthPicker = !$this->showMonthPicker;
  1526. }
  1527. private function exportExcel($records)
  1528. {
  1529. $startTime = microtime(true);
  1530. $spreadsheet = new Spreadsheet();
  1531. $activeWorksheet = $spreadsheet->getActiveSheet();
  1532. $activeWorksheet->setCellValue('A1', "Data");
  1533. $activeWorksheet->setCellValue('B1', "Tipologia");
  1534. $activeWorksheet->setCellValue('C1', "Causale");
  1535. $activeWorksheet->setCellValue('D1', "Nominativo");
  1536. $activeWorksheet->setCellValue('E1', "Stato");
  1537. $activeWorksheet->setCellValue('F1', "Entrata");
  1538. $activeWorksheet->setCellValue('G1', "Uscita");
  1539. $activeWorksheet->setCellValue('H1', "Origine");
  1540. $activeWorksheet->setCellValue('I1', "Destinazione");
  1541. $activeWorksheet->setCellValue('J1', "Metodo di pagamento");
  1542. $activeWorksheet->getStyle('A1:J1')->getFill()
  1543. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  1544. ->getStartColor()->setARGB('FF0C6197');
  1545. $activeWorksheet->getStyle('A1:J1')->getFont()->getColor()->setARGB('FFFFFFFF');
  1546. $idx = 2;
  1547. foreach ($records as $record) {
  1548. if ($record->date != '') {
  1549. $isMoveIn = $record->type === 'MOVE_IN';
  1550. $isMoveOut = $record->type === 'MOVE_OUT';
  1551. $isIn = $record->type === 'IN';
  1552. $isOut = $record->type === 'OUT';
  1553. $activeWorksheet->setCellValue('A' . $idx, date("d/m/Y", strtotime($record->date)));
  1554. // Tipologia
  1555. $activeWorksheet->setCellValue(
  1556. 'B' . $idx,
  1557. ($isMoveIn || $isMoveOut) ? '' : ($record->commercial ? 'Commerciale' : 'Non commerciale')
  1558. );
  1559. // Causale
  1560. $activeWorksheet->setCellValue('C' . $idx, $record->causal_name);
  1561. // Nominativo
  1562. if ($isIn && isset($record->member)) {
  1563. $nominativo = $record->member->first_name . ' ' . $record->member->last_name;
  1564. } elseif ($isOut && isset($record->supplier)) {
  1565. $nominativo = $record->supplier->name;
  1566. } else {
  1567. $nominativo = '';
  1568. }
  1569. $activeWorksheet->setCellValue('D' . $idx, $nominativo);
  1570. // Stato
  1571. $activeWorksheet->setCellValue('E' . $idx, $record->deleted ? 'Annullata' : '');
  1572. // Entrata
  1573. $activeWorksheet->setCellValue(
  1574. 'F' . $idx,
  1575. ($isIn || $isMoveIn) ? formatPrice($record->amount) : ''
  1576. );
  1577. // Uscita
  1578. $activeWorksheet->setCellValue(
  1579. 'G' . $idx,
  1580. ($isOut || $isMoveOut) ? formatPrice($record->amount) : ''
  1581. );
  1582. // Origine
  1583. $activeWorksheet->setCellValue(
  1584. 'H' . $idx,
  1585. ($isOut || $isMoveOut) ? ($record->origin ?? '') : ''
  1586. );
  1587. // Destinazione
  1588. $activeWorksheet->setCellValue(
  1589. 'I' . $idx,
  1590. ($isIn || $isMoveIn) ? ($record->destination ?? '') : ''
  1591. );
  1592. // Metodo di pagamento
  1593. $activeWorksheet->setCellValue(
  1594. 'J' . $idx,
  1595. ($isMoveIn || $isMoveOut) ? '' : ($record->payment_method->name ?? '')
  1596. );
  1597. } else {
  1598. // RIGA TOTALI
  1599. $activeWorksheet->setCellValue('A' . $idx, "Totali");
  1600. $activeWorksheet->setCellValue('F' . $idx, formatPrice($record->total_in));
  1601. $activeWorksheet->setCellValue('G' . $idx, formatPrice($record->total_out));
  1602. }
  1603. $idx++;
  1604. }
  1605. $activeWorksheet->getColumnDimension('A')->setWidth(10);
  1606. $activeWorksheet->getColumnDimension('B')->setWidth(30);
  1607. $activeWorksheet->getColumnDimension('C')->setWidth(30);
  1608. $activeWorksheet->getColumnDimension('D')->setWidth(30);
  1609. $activeWorksheet->getColumnDimension('E')->setWidth(10);
  1610. $activeWorksheet->getColumnDimension('F')->setWidth(10);
  1611. $activeWorksheet->getColumnDimension('G')->setWidth(10);
  1612. $activeWorksheet->getColumnDimension('H')->setWidth(20);
  1613. $activeWorksheet->getColumnDimension('I')->setWidth(20);
  1614. $activeWorksheet->getColumnDimension('J')->setWidth(30);
  1615. $activeWorksheet->getStyle('A' . ($idx - 1) . ':J' . ($idx - 1))->getFont()->setBold(true);
  1616. $filename = 'prima_nota_' . date("YmdHis") . '.xlsx';
  1617. Log::info('exportWithData: Preparing to save file', [
  1618. 'filename' => $filename,
  1619. //'total_processing_time' => microtime(true) - $startTime,
  1620. 'memory_before_save' => memory_get_usage(true)
  1621. ]);
  1622. try {
  1623. $currentClient = session('currentClient', 'default');
  1624. $tempPath = sys_get_temp_dir() . '/' . $filename;
  1625. $writer = new Xlsx($spreadsheet);
  1626. $writer->save($tempPath);
  1627. unset($spreadsheet, $activeWorksheet, $writer);
  1628. gc_collect_cycles();
  1629. Log::info('exportWithData: Uploading to S3');
  1630. $disk = Storage::disk('s3');
  1631. $s3Path = $currentClient . '/prima_nota/' . $filename;
  1632. $primaNotaFolderPath = $currentClient . '/prima_nota/.gitkeep';
  1633. if (!$disk->exists($primaNotaFolderPath)) {
  1634. $disk->put($primaNotaFolderPath, '');
  1635. }
  1636. $uploadStart = microtime(true);
  1637. $fileContent = file_get_contents($tempPath);
  1638. $uploaded = $disk->put($s3Path, $fileContent, 'private');
  1639. $uploadTime = microtime(true) - $uploadStart;
  1640. if (!$uploaded) {
  1641. throw new \Exception('Failed to upload file to Wasabi S3');
  1642. }
  1643. Log::info("Export completed successfully", [
  1644. 'client' => $currentClient,
  1645. 'path' => $s3Path,
  1646. 'file_size' => filesize($tempPath),
  1647. 'records_processed' => $recordsProcessed,
  1648. 'upload_time' => $uploadTime,
  1649. 'total_time' => microtime(true) - $startTime,
  1650. 'memory_peak' => memory_get_peak_usage(true)
  1651. ]);
  1652. if (file_exists($tempPath)) {
  1653. unlink($tempPath);
  1654. }
  1655. $downloadUrl = $disk->temporaryUrl($s3Path, now()->addHour());
  1656. return redirect($downloadUrl);
  1657. } catch (\Exception $e) {
  1658. Log::error('Export S3 error - falling back to local', [
  1659. 'error' => $e->getMessage(),
  1660. 'trace' => $e->getTraceAsString(),
  1661. 'client' => session('currentClient', 'unknown'),
  1662. 'filename' => $filename,
  1663. 'records_processed' => $recordsProcessed ?? 0,
  1664. 'time_elapsed' => microtime(true) - $startTime
  1665. ]);
  1666. // Fallback logic remains the same...
  1667. $currentClient = session('currentClient', 'default');
  1668. $clientFolder = storage_path('app/prima_nota/' . $currentClient);
  1669. if (!is_dir($clientFolder)) {
  1670. mkdir($clientFolder, 0755, true);
  1671. Log::info("Created local client prima_nota folder: {$clientFolder}");
  1672. }
  1673. $localPath = $clientFolder . '/' . $filename;
  1674. if (isset($tempPath) && file_exists($tempPath)) {
  1675. rename($tempPath, $localPath);
  1676. } else {
  1677. $writer = new Xlsx($spreadsheet);
  1678. $writer->save($localPath);
  1679. unset($spreadsheet, $activeWorksheet, $writer);
  1680. }
  1681. gc_collect_cycles();
  1682. Log::warning("Export saved locally due to S3 error", [
  1683. 'client' => $currentClient,
  1684. 'local_path' => $localPath,
  1685. 'error' => $e->getMessage()
  1686. ]);
  1687. session()->flash('warning', 'File salvato localmente a causa di un errore del cloud storage.');
  1688. return response()->download($localPath)->deleteFileAfterSend();
  1689. }
  1690. }
  1691. }