RecordIN.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Livewire\WithPagination;
  5. use Barryvdh\DomPDF\Facade\Pdf;
  6. use Illuminate\Support\Facades\Storage;
  7. use Illuminate\Support\Facades\Mail;
  8. use Illuminate\Contracts\Validation\Validator;
  9. class RecordIN extends Component
  10. {
  11. use WithPagination;
  12. protected $paginationTheme = 'bootstrap';
  13. protected $listeners = ['setCausal' => 'setCausal','refreshMembers' => 'refreshMembers'];
  14. public $sortField ='date';
  15. public $sortAsc = false;
  16. public $typeIN = "IN";
  17. public $fromPage = '';
  18. public $isDuplicate = false;
  19. public $multiP = false;
  20. public $from, $to;
  21. public $multiMonthTo = 0;
  22. public $multiYearTo = 0;
  23. public $multiMonthFrom = 0;
  24. public $multiYearFrom = 0;
  25. public $createSubscription = 0;
  26. public $corrispettivo_causal_id = 0;
  27. public function sortBy($field)
  28. {
  29. if($this->sortField === $field)
  30. {
  31. $this->sortAsc = ! $this->sortAsc;
  32. } else {
  33. $this->sortAsc = true;
  34. }
  35. $this->sortField = $field;
  36. }
  37. public $courseId = 0;
  38. public $months = array();
  39. public $records, $dataId, $member_id, $supplier_id,
  40. $causal_id,
  41. $payment_method_id,
  42. $destination_id,
  43. $date,
  44. $month,
  45. $year,
  46. $type,
  47. $deleted,
  48. $financial_movement,
  49. $amount,
  50. $vat,
  51. $virtual,
  52. $note,
  53. $parent,
  54. $corrispettivo_fiscale,
  55. $commercial, $update = false, $add = false;
  56. public $corrispettivo = [];
  57. public $corrispettivo_destination = [];
  58. public $currentReceip;
  59. public $filterMember = 0, $filterPaymentMethod = 0, $filterCausals = 0, $filterFrom = '', $filterTo = '', $filterCommercial = 0;
  60. public $hasFilter = false;
  61. public $total = 0;
  62. public $selectedFilter = '';
  63. public $multipleIds = [];
  64. public $multipleAction = '';
  65. public $first = true;
  66. public $selectId = 0;
  67. public $refreshAfter = 0;
  68. public $canSave = true;
  69. public $newMemberFirstName = '';
  70. public $newMemberLastName = '';
  71. public $newMemberEmail = '';
  72. public $newMemberToComplete = false;
  73. public $newMemberFiscalCode = '';
  74. public $newMemberFiscalCodeExist = false;
  75. public $newMemberExists = false;
  76. public $causals = array();
  77. public $payments = array();
  78. public $banks = array();
  79. public $members = array();
  80. public $vats = array();
  81. public $rows = array();
  82. protected $rules = [
  83. 'member_id' => 'required',
  84. 'payment_method_id' => 'required',
  85. 'rows.*.causal_id' => 'required',
  86. 'rows.*.amount' => 'required'
  87. ];
  88. protected $messages = [
  89. 'member_id.required' => 'La persona è obbligatorio',
  90. 'payment_method_id.required' => 'Il metodo di pagamento è obbligatorio',
  91. 'rows.*.causal_id.required' => 'La causale è obbligatoria',
  92. 'rows.*.amount.required' => 'L\'importo è obbligatorio',
  93. ];
  94. public function updatedMemberId() {
  95. if ($this->member_id > 0)
  96. {
  97. $member = \App\Models\Member::findOrFail($this->member_id);
  98. if (!$member->isAdult())
  99. {
  100. if($member->father_name != '')
  101. $this->parent = $member->father_name . " (C.F. " . $member->father_fiscal_code . ")";
  102. else if($member->mother_name != '')
  103. $this->parent = $member->mother_name . " (C.F. " . $member->mother_fiscal_code . ")";
  104. else
  105. $this->parent = '';
  106. }
  107. $this->virtual = $member->getMoney();
  108. $this->newMemberFirstName = '';
  109. $this->newMemberLastName = '';
  110. $this->newMemberEmail = '';
  111. $this->newMemberToComplete = false;
  112. $this->newMemberFiscalCode = '';
  113. $this->newMemberFiscalCodeExist = false;
  114. }
  115. $this->emit('refresh');
  116. }
  117. public function updatedCausalId() {
  118. //$this->emit('refresh');
  119. }
  120. public function updatedDate() {
  121. //$this->emit('refresh');
  122. }
  123. public function hydrate()
  124. {
  125. $this->emit('load-select');
  126. }
  127. /*public function updated() {
  128. $this->emit('refresh');
  129. }*/
  130. public function updatedPaymentMethodId() {
  131. //$this->emit('refresh');
  132. if ($this->payment_method_id) {
  133. $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
  134. $this->destination_id = $payment_method->destination_id ?? null;
  135. }
  136. $this->canSave = $this->checkCanSave();
  137. }
  138. public function updatedCorrispettivoFiscale() {
  139. if ($this->corrispettivo_fiscale) {
  140. foreach ($this->payments as $payment) {
  141. if ($payment->corrispettivo_fiscale) {
  142. $this->corrispettivo_destination[$payment->id] = $payment->destination_id;
  143. }
  144. }
  145. }
  146. }
  147. public function updatedAmount() {
  148. // $this->emit('refresh');
  149. $this->canSave = $this->checkCanSave();
  150. }
  151. public function updatedCommercial($value)
  152. {
  153. $this->emitSelf('refreshMembers');
  154. }
  155. public function checkCanSave()
  156. {
  157. $ret = true;
  158. if ($this->payment_method_id != null)
  159. {
  160. $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
  161. if ($payment_method->money)
  162. {
  163. $from = str_replace(".", "", str_replace(",", "", number_format($this->virtual, 2)));
  164. $this->from = $this->virtual;
  165. $from = $this->from;
  166. $to = str_replace(".", "", str_replace(",", "", $this->currencyToDouble($this->amount)));
  167. $this->to = $this->amount;
  168. $to = $this->to;
  169. $ret = $from >= $to;
  170. }
  171. }
  172. return $ret;
  173. }
  174. public function setAmount()
  175. {
  176. $tot = 0;
  177. foreach($this->rows as $r)
  178. {
  179. $tot += $this->currencyToDouble($r["amount"]);
  180. }
  181. $this->amount = $tot;
  182. $this->canSave = $this->checkCanSave();
  183. }
  184. public function refreshMembers()
  185. {
  186. if ($this->add || $this->update)
  187. {
  188. if($this->commercial){
  189. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])
  190. ->where(function($query) {
  191. $query->where('is_archived', false)
  192. ->orWhereNull('is_archived');
  193. })
  194. ->where(function($query) {
  195. $query->where('is_deleted', false)
  196. ->orWhereNull('is_deleted');
  197. })->orderBy('last_name')->orderBy('first_name')->get();
  198. } else {
  199. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->where('current_status', 2)->orWhere('current_status', 1)
  200. ->where(function($query) {
  201. $query->where('is_archived', false)
  202. ->orWhereNull('is_archived');
  203. })
  204. ->where(function($query) {
  205. $query->where('is_deleted', false)
  206. ->orWhereNull('is_deleted');
  207. })->orderBy('last_name')->orderBy('first_name')->get();
  208. }
  209. }
  210. else
  211. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])
  212. ->where(function($query) {
  213. $query->where('is_archived', false)
  214. ->orWhereNull('is_archived');
  215. })
  216. ->where(function($query) {
  217. $query->where('is_deleted', false)
  218. ->orWhereNull('is_deleted');
  219. })->orderBy('last_name')->orderBy('first_name')->get();
  220. }
  221. public function resetFields(){
  222. $this->member_id = null;
  223. $this->supplier_id = null;
  224. $this->payment_method_id = null;
  225. $this->destination_id = null;
  226. $this->commercial = 0;
  227. $this->corrispettivo_fiscale = false;
  228. $this->date = date("Y-m-d");
  229. $this->type = 'IN';
  230. $this->deleted = false;
  231. $this->financial_movement = false;
  232. $this->newMemberFirstName = '';
  233. $this->newMemberLastName = '';
  234. $this->newMemberEmail = '';
  235. $this->newMemberToComplete = false;
  236. $this->newMemberFiscalCode = '';
  237. $this->newMemberFiscalCodeExist = false;
  238. $this->newMemberExists = false;
  239. $this->currentReceip = null;
  240. $this->parent = '';
  241. $this->courseId = 0;
  242. $this->months = array();
  243. $this->rows = array();
  244. $this->rows[] = array('causal_id' => isset($_GET["causalId"]) ? $_GET["causalId"] : null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'vat_id' => null, 'note' => '', 'commercial' => 0);
  245. $this->corrispettivo = [];
  246. $this->corrispettivo_destination = [];
  247. $this->emit('load-data-table');
  248. }
  249. public function getMemberProperty()
  250. {
  251. $ret = null;
  252. if ($this->member_id > 0)
  253. {
  254. $ret = \App\Models\Member::findOrFail($this->member_id);
  255. }
  256. return $ret;
  257. }
  258. public function getCausalProperty()
  259. {
  260. $ret = null;
  261. if ($this->causal_id > 0)
  262. {
  263. $ret = \App\Models\Causal::findOrFail($this->causal_id);
  264. }
  265. return $ret;
  266. }
  267. public function getCausal($causal)
  268. {
  269. $ret = '';
  270. if ($causal > 0)
  271. {
  272. $ret = \App\Models\Causal::findOrFail($causal)->getTree();
  273. }
  274. return $ret;
  275. }
  276. public function getPaymentMethod($payment)
  277. {
  278. $ret = '';
  279. if ($payment > 0)
  280. {
  281. $ret = \App\Models\PaymentMethod::findOrFail($payment)->name;
  282. }
  283. return $ret;
  284. }
  285. public function getBankName($bank_id)
  286. {
  287. $ret = '';
  288. if ($bank_id > 0)
  289. {
  290. $ret = \App\Models\Bank::findOrFail($bank_id)->name;
  291. }
  292. return $ret;
  293. }
  294. function buildTree($records, $parentId = 0) {
  295. $this->causals = array();
  296. foreach ($records as $record)
  297. {
  298. if ($record->parent_id == $parentId)
  299. {
  300. $children = $this->buildTree($record, $record->id);
  301. if ($children) {
  302. $record->children = $children;
  303. }
  304. $this->causals[] = $record;
  305. }
  306. }
  307. return $this->causals;
  308. }
  309. public function getCausale($records, $indentation)
  310. {
  311. foreach($records as $record)
  312. {
  313. $this->causals[] = array('id' => $record->id, 'name' => $record->getTree(), 'text' => $record->getTree(), 'level' => $indentation);
  314. if(count($record->childs))
  315. $this->getCausale($record->childs, $indentation + 1);
  316. }
  317. }
  318. public function mount()
  319. {
  320. if (isset($_GET["from"]))
  321. {
  322. $this->fromPage = $_GET["from"];
  323. }
  324. $this->causals = array();
  325. $this->multiMonthFrom = date("n");
  326. $this->multiYearFrom = date("Y");
  327. $this->multiMonthTo = date("n");
  328. $this->multiYearTo = date("Y");
  329. $fisc = \App\Models\Causal::where('corrispettivo_fiscale', true)->first();
  330. if ($fisc)
  331. $this->corrispettivo_causal_id = $fisc->id;
  332. $this->getCausale(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'IN')->get(), 0);
  333. //$this->buildTree(\App\Models\Causal::all(), null);
  334. $this->refreshMembers();
  335. $this->payments = \App\Models\PaymentMethod::select('id', 'name')->where('enabled', true)->whereIn('type', array('ALL', 'IN'))->orderBy('name')->get();
  336. $this->banks = \App\Models\Bank::select('id', 'name')->where('enabled', true)->whereIn('visibility', array('ALL', 'IN'))->orderBy('name')->get();
  337. $this->vats = \App\Models\Vat::select('id', 'name', 'value')->orderBy('value')->get();
  338. if ($this->first)
  339. {
  340. if (isset($_GET["new"]))
  341. {
  342. $this->refreshAfter = 1;
  343. $this->add();
  344. }
  345. if (isset($_GET["memberId"]))
  346. {
  347. $this->refreshAfter = 1;
  348. $this->member_id = $_GET["memberId"];
  349. $member = \App\Models\Member::findOrFail($this->member_id);
  350. if (!$member->isAdult())
  351. {
  352. if($member->father_name != '')
  353. $this->parent = $member->father_name . " (C.F. " . $member->father_fiscal_code . ")";
  354. else if($member->mother_name != '')
  355. $this->parent = $member->mother_name . " (C.F. " . $member->mother_fiscal_code . ")";
  356. else
  357. $this->parent = '';
  358. }
  359. }
  360. if (isset($_GET["causalId"]))
  361. {
  362. $this->refreshAfter = 1;
  363. $this->causal_id = $_GET["causalId"];
  364. }
  365. if (isset($_GET["id"]))
  366. {
  367. $this->refreshAfter = 1;
  368. $this->edit($_GET["id"]);
  369. }
  370. $count = 0;
  371. if (isset($_GET["courseId"]))
  372. {
  373. $this->courseId = $_GET["courseId"];
  374. $mc = \App\Models\MemberCourse::findOrFail($this->courseId);
  375. $course = \App\Models\Course::findOrFail($mc->course_id);
  376. }
  377. if (isset($_GET["months"]))
  378. {
  379. $price = $_GET["price"] / 100 * 100;
  380. $this->refreshAfter = 1;
  381. $desc = "Pagamento";
  382. $months = explode("|", $_GET["months"]);
  383. $this->months = $months;
  384. $this->rows[0]["amount"] = formatPrice($price * sizeof($months));
  385. foreach($months as $idx => $m)
  386. {
  387. $this->rows[0]["when"][$idx]["month"] = $m;
  388. $currentYear = date('Y');
  389. $currentMonth = date('n');
  390. if ($currentMonth > env('FISCAL_YEAR_MONTH_TO', 1) && $m > env('FISCAL_YEAR_MONTH_TO', 1)) { // current 9-12 && $m 9-12
  391. $currentYear = $currentYear;
  392. } elseif ($currentMonth > env('FISCAL_YEAR_MONTH_TO', 1) && $m < env('FISCAL_YEAR_MONTH_FROM', 1)) { // current 9-12 && $m 1-8
  393. $currentYear += 1;
  394. } elseif ($currentMonth < env('FISCAL_YEAR_MONTH_FROM', 1) && $m > env('FISCAL_YEAR_MONTH_TO', 1)) { // current 1-8 && $m 9-12
  395. $currentYear -= 1;
  396. } elseif ($currentMonth < env('FISCAL_YEAR_MONTH_FROM', 1) && $m < env('FISCAL_YEAR_MONTH_FROM', 1)) { // current 1-8 && $m 1-8
  397. $currentYear = $currentYear;
  398. }
  399. $this->rows[0]["when"][$idx]["year"] = $currentYear;
  400. if ($idx > 0)
  401. {
  402. if ($this->rows[0]["when"][$idx - 1]["year"] != $this->rows[0]["when"][$idx]["year"])
  403. $desc .= " " . $this->rows[0]["when"][$idx - 1]["year"] . " ";
  404. }
  405. $desc .= " " . $this->getMonth($m);// . " " . $this->rows[0]["when"][$idx]["year"];
  406. }
  407. $desc .= " " . $this->rows[0]["when"][$idx]["year"];
  408. $this->rows[0]["note"] = $desc;
  409. $count += 1;
  410. }
  411. if (isset($_GET["createSubscription"]) && $_GET["createSubscription"] == 1)
  412. {
  413. $this->createSubscription = 1;
  414. $this->courseId = $_GET["courseId"];
  415. $price = $_GET["subscription_price"] / 100 * 100;
  416. $this->refreshAfter = 1;
  417. if ($count == 1)
  418. $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'vat_id' => null, 'note' => '', 'commercial' => 0);
  419. $this->rows[$count]["causal_id"] = null;
  420. $this->rows[$count]["amount"] = formatPrice($price);
  421. $this->rows[$count]["note"] = "Pagamento iscrizione " . $course->name;
  422. if (isset($_GET["subCausalId"]))
  423. {
  424. $this->refreshAfter = 1;
  425. $this->rows[$count]["causal_id"] = $_GET["subCausalId"];
  426. }
  427. }
  428. }
  429. $this->first = false;
  430. $this->newMemberExists = false;
  431. }
  432. public function search()
  433. {
  434. $this->hasFilter = true;
  435. }
  436. public function disableSearch()
  437. {
  438. $this->filterMember = 0;
  439. $this->filterPaymentMethod = 0;
  440. $this->filterCausals = 0;
  441. $this->filterTo = '';
  442. $this->filterFrom = '';
  443. $this->filterCommercial = 0;
  444. $this->hasFilter = false;
  445. $this->total = 0;
  446. }
  447. public function render()
  448. {
  449. $datas = [];
  450. return view('livewire.records_in', ['datas' => $datas]);
  451. }
  452. public function executeMultipleAction(){
  453. if ($this->multipleAction == 'delete')
  454. $this->multipleDelete();
  455. }
  456. public function add()
  457. {
  458. $this->emit('load-select');
  459. //if ($this->hasFilter)
  460. $this->emit('hide-search');
  461. $this->resetFields();
  462. $this->dataId = 0;
  463. $this->add = true;
  464. $this->update = false;
  465. $this->emit('setEdit', true);
  466. }
  467. public function storeCorrispettivo()
  468. {
  469. $this->emit('refresh');
  470. $rules = [
  471. 'member_id' => 'required'
  472. ];
  473. $this->validate($rules);
  474. foreach($this->payments as $p)
  475. {
  476. if ($p->corrispettivo_fiscale)
  477. {
  478. $price = isset($this->corrispettivo[$p->id]) ? $this->currencyToDouble($this->corrispettivo[$p->id]) : 0;
  479. $destination_id = isset($this->corrispettivo_destination[$p->id]) ? $this->corrispettivo_destination[$p->id] : null;
  480. if ($price > 0)
  481. {
  482. $record = \App\Models\Record::create([
  483. 'member_id' => $this->member_id,
  484. 'supplier_id' => null,
  485. 'payment_method_id' => $p->id,
  486. 'destination_id' => $destination_id,
  487. 'commercial' => $this->commercial,
  488. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  489. 'date' => $this->date,
  490. 'type' => $this->type,
  491. 'amount' => $price,
  492. 'financial_movement' => $this->financial_movement,
  493. 'deleted' => $this->deleted
  494. ]);
  495. \App\Models\RecordRow::create([
  496. 'record_id' => $record->id,
  497. 'causal_id' => $this->corrispettivo_causal_id,
  498. 'note' => '',
  499. 'amount' => $price,
  500. 'vat_id' => null,
  501. 'commercial' => $this->commercial,
  502. 'when' => json_encode([])
  503. ]);
  504. }
  505. }
  506. }
  507. $this->resetFields();
  508. $this->add = false;
  509. $this->isDuplicate = false;
  510. $this->emit('setEdit', false);
  511. }
  512. public function store($generate)
  513. {
  514. $this->emit('refresh');
  515. $rules = [
  516. 'payment_method_id' => 'required',
  517. 'rows.*.causal_id' => 'required',
  518. 'rows.*.amount' => 'required'
  519. ];
  520. /*$f = false;
  521. foreach($this->rows as $row)
  522. {
  523. if ($row["commercial"]) $f = true;
  524. }
  525. if ($f)
  526. $rules["member_id"] = 'required';*/
  527. if(!$this->commercial)
  528. $rules["member_id"] = 'required';
  529. //dd($this->getErrorBag());
  530. /*$validator = Validator::make($request->all(), $rules);
  531. if ($validator->fails())
  532. {
  533. $this->emit('goToAnchor');
  534. }*/
  535. //if (!$this->validate($rules))
  536. //$this->emit('goToAnchor');
  537. $this->validate($rules);
  538. //dd("£ASDSDFS");
  539. try {
  540. $record = \App\Models\Record::create([
  541. 'member_id' => $this->member_id,
  542. 'supplier_id' => $this->supplier_id,
  543. 'payment_method_id' => $this->payment_method_id,
  544. 'destination_id' => $this->destination_id,
  545. 'commercial' => $this->commercial,
  546. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  547. 'date' => $this->date,
  548. 'type' => $this->type,
  549. 'financial_movement' => $this->financial_movement,
  550. 'deleted' => $this->deleted
  551. ]);
  552. $this->dataId = $record->id;
  553. $no_receipt_causal = false;
  554. // Inserisco le righe
  555. $tot = 0;
  556. $vat = 0;
  557. foreach($this->rows as $row)
  558. {
  559. foreach($row["when"] as $x => $y)
  560. {
  561. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  562. }
  563. \App\Models\RecordRow::create([
  564. 'record_id' => $this->dataId,
  565. 'causal_id' => $row["causal_id"],
  566. 'note' => $row["note"],
  567. 'amount' => $this->currencyToDouble($row["amount"]),
  568. 'vat_id' => $row["vat_id"],
  569. 'commercial' => $this->commercial,
  570. 'when' => json_encode($row["when"])
  571. ]);
  572. $tot += $this->currencyToDouble($row["amount"]);
  573. $vat += getVatValue($this->currencyToDouble($row["amount"]), $row["vat_id"]);
  574. $cau = \App\Models\Causal::findOrFail($row["causal_id"]);
  575. if ($cau->no_receipt)
  576. $no_receipt_causal = true;
  577. // Se la causale ricarica a borsellino, se il totale supera y euro, inserisco x euro gratis
  578. if ($cau->money)
  579. {
  580. if ($this->currencyToDouble($row["amount"]) >= env('MIN_MONEY', 100))
  581. {
  582. $money = new \App\Models\Money();
  583. $money->member_id = $this->member_id;
  584. $money->record_id = $this->dataId;
  585. $money->amount = env('EXTRA_MONEY', 10);
  586. $money->date = date("Y-m-d");
  587. $money->note = env('EXTRA_MONEY', 10) . ' in più per aver caricato ' . $this->currencyToDouble($row["amount"]);
  588. $money->save();
  589. }
  590. }
  591. }
  592. $record->amount = $tot;
  593. $record->save();
  594. // se sto pagando un corso, aggiorno lo status dei mesi
  595. if ($this->courseId > 0)
  596. {
  597. $record->months = json_encode($this->months);
  598. $record->member_course_id = $this->courseId;
  599. $record->save();
  600. $c = \App\Models\MemberCourse::findOrFail($this->courseId);
  601. $xxx = json_decode($c->months);
  602. foreach($xxx as $idx => $mm)
  603. {
  604. if (in_array($mm->m, $this->months))
  605. {
  606. $xxx[$idx]->status = 1;
  607. }
  608. }
  609. $c->months = json_encode($xxx);
  610. if ($this->createSubscription == 1)
  611. $c->subscribed = true;
  612. $c->save();
  613. }
  614. if (!$this->financial_movement)
  615. {
  616. $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
  617. if (!$payment_method->money)
  618. {
  619. if ($generate && !$no_receipt_causal)
  620. $this->createReceipt();
  621. }
  622. }
  623. session()->flash('success','Movimento creato');
  624. $this->resetFields();
  625. $this->add = false;
  626. $this->isDuplicate = false;
  627. $this->emit('setEdit', false);
  628. } catch (\Exception $ex) {
  629. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  630. }
  631. }
  632. public function duplicate($id){
  633. $record = \App\Models\Record::findOrFail($id);
  634. $newRecord = $record->replicate();
  635. $newRecord->save();
  636. $rows = \App\Models\RecordRow::where('record_id', $id)->get();
  637. foreach($rows as $r)
  638. {
  639. $newRow = $r->replicate();
  640. $newRow->record_id = $newRecord->id;
  641. $newRow->save();
  642. }
  643. $this->edit($newRecord->id);
  644. $this->isDuplicate = true;
  645. }
  646. public function edit($id){
  647. if (!isset($_GET["from"]) && $this->fromPage == '')
  648. $this->fromPage = 'in';
  649. $this->emit('setEdit', true);
  650. //if ($this->hasFilter)
  651. $this->emit('hide-search');
  652. $this->emit('load-select');
  653. try {
  654. $record = \App\Models\Record::findOrFail($id);
  655. if( !$record) {
  656. session()->flash('error','Movimento non trovato');
  657. } else {
  658. $this->member_id = $record->member_id;
  659. $this->supplier_id = $record->supplier_id;
  660. $this->payment_method_id = $record->payment_method_id;
  661. $this->destination_id = $record->destination_id;
  662. $this->amount = $record->amount;
  663. $this->commercial = $record->commercial;
  664. $this->corrispettivo_fiscale = $record->corrispettivo_fiscale;
  665. $this->date = date("Y-m-d", strtotime($record->date));
  666. $this->type = $record->type;
  667. $this->deleted = $record->deleted;
  668. $this->financial_movement = $record->financial_movement;
  669. $this->dataId = $record->id;
  670. $this->update = true;
  671. $this->add = false;
  672. $this->rows = \App\Models\RecordRow::where('record_id', $this->dataId)->select('causal_id', 'note', 'commercial', 'when', 'amount', 'vat_id')->get()->toArray();
  673. foreach($this->rows as $i => $r)
  674. {
  675. $this->rows[$i]['amount'] = formatPrice($this->rows[$i]['amount']);
  676. $this->rows[$i]['vat_id'] = $this->rows[$i]['vat_id'];
  677. $this->rows[$i]['when'] = json_decode($this->rows[$i]['when']);
  678. }
  679. $exist = \App\Models\Receipt::where('record_id', $id)->orderBy('id', 'DESC')->first();
  680. if ($exist != null)
  681. {
  682. $this->currentReceip = $exist;
  683. $this->parent = $this->currentReceip->parent;
  684. }
  685. }
  686. } catch (\Exception $ex) {
  687. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  688. }
  689. }
  690. public function update($generate = false)
  691. {
  692. $this->emit('refresh');
  693. $rules = [
  694. 'payment_method_id' => 'required',
  695. 'rows.*.causal_id' => 'required',
  696. 'rows.*.amount' => 'required'
  697. ];
  698. if(!$this->commercial)
  699. $rules["member_id"] = 'required';
  700. $this->validate($rules);
  701. try {
  702. \App\Models\Record::whereId($this->dataId)->update([
  703. 'member_id' => $this->member_id,
  704. 'supplier_id' => $this->supplier_id,
  705. 'payment_method_id' => $this->payment_method_id,
  706. 'destination_id' => $this->destination_id,
  707. 'commercial' => $this->commercial,
  708. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  709. 'date' => date("Y-m-d", strtotime($this->date)),
  710. 'type' => $this->type,
  711. 'financial_movement' => $this->financial_movement,
  712. 'deleted' => $this->deleted,
  713. ]);
  714. $no_receipt_causal = false;
  715. $tot = 0;
  716. $vat = 0;
  717. // Elimino le righe
  718. \App\Models\RecordRow::where('record_id', $this->dataId)->delete();
  719. // Inserisco le righe
  720. foreach($this->rows as $row)
  721. {
  722. foreach($row["when"] as $x => $y)
  723. {
  724. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  725. }
  726. \App\Models\RecordRow::create([
  727. 'record_id' => $this->dataId,
  728. 'causal_id' => $row["causal_id"],
  729. 'note' => $row["note"],
  730. 'vat_id' => $row["vat_id"],
  731. 'amount' => $this->currencyToDouble($row["amount"]),
  732. 'commercial' => $this->commercial,
  733. 'when' => json_encode($row["when"])
  734. ]);
  735. $tot += $this->currencyToDouble($row["amount"]);
  736. $vat += getVatValue($this->currencyToDouble($row["amount"]), $row["vat_id"]);
  737. $cau = \App\Models\Causal::findOrFail($row["causal_id"]);
  738. if ($cau->no_receipt)
  739. $no_receipt_causal = true;
  740. }
  741. $rec = \App\Models\Record::findOrFail($this->dataId);
  742. $rec->amount = $tot;
  743. $rec->save();
  744. if (!$this->financial_movement)
  745. {
  746. $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
  747. if (!$payment_method->money)
  748. {
  749. if ($generate && !$no_receipt_causal)
  750. $this->createReceipt();
  751. }
  752. }
  753. session()->flash('success','Movimento aggiornato');
  754. $this->resetFields();
  755. $this->update = false;
  756. $this->isDuplicate = false;
  757. $this->emit('setEdit', false);
  758. } catch (\Exception $ex) {
  759. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  760. }
  761. }
  762. public function rigenerate()
  763. {
  764. $this->emit('refresh');
  765. try {
  766. $record = \App\Models\Record::findOrFail($this->dataId);
  767. $newRecord = $record->replicate();
  768. $newRecord->date = date("Y-m-d");
  769. $newRecord->save();
  770. $recordRows = \App\Models\RecordRow::where('record_id', $this->dataId)->get();
  771. // Inserisco le righe
  772. foreach($recordRows as $row)
  773. {
  774. $newRow = $row->replicate();
  775. $newRow->record_id = $newRecord->id;
  776. $newRow->save();
  777. }
  778. $this->dataId = $newRecord->id;
  779. $this->createReceipt();
  780. session()->flash('success','Movimento aggiornato');
  781. $this->resetFields();
  782. $this->update = false;
  783. $this->isDuplicate = false;
  784. $this->emit('setEdit', false);
  785. } catch (\Exception $ex) {
  786. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  787. }
  788. }
  789. public function cancel()
  790. {
  791. // Se arrivo da duplica elimino
  792. if ($this->isDuplicate)
  793. {
  794. try{
  795. \App\Models\Record::find($this->dataId)->delete();
  796. session()->flash('success',"Movimento eliminato");
  797. }catch(\Exception $e){
  798. session()->flash('error','Errore (' . $e->getMessage() . ')');
  799. }
  800. }
  801. $this->isDuplicate = false;
  802. $this->add = false;
  803. $this->update = false;
  804. $this->resetFields();
  805. $this->emit('setEdit', false);
  806. return redirect()->to('/in');
  807. }
  808. public function delete($id)
  809. {
  810. try{
  811. $r = \App\Models\Record::find($id);
  812. // Annullo la ricevuta
  813. $receipt = \App\Models\Receipt::where('record_id', $r->id)->first();
  814. if($receipt)
  815. {
  816. $receipt->status = 99;
  817. sendReceiptDeleteEmail($receipt);
  818. $receipt->save();
  819. }
  820. if ($r->member_course_id > 0)
  821. {
  822. $months = json_decode($r->months);
  823. $c = \App\Models\MemberCourse::findOrFail($r->member_course_id);
  824. $xxx = json_decode($c->months);
  825. foreach($xxx as $idx => $mm)
  826. {
  827. if (in_array($mm->m, $months))
  828. {
  829. $xxx[$idx]->status = "";
  830. }
  831. }
  832. $c->months = json_encode($xxx);
  833. $c->save();
  834. }
  835. $r->deleted = true;
  836. $r->save();
  837. session()->flash('success','Movimento aggiornato');
  838. $this->resetFields();
  839. $this->update = false;
  840. $this->isDuplicate = false;
  841. $this->emit('setEdit', false);
  842. $this->emit('reload');
  843. //$this->emit('load-data-table');
  844. }catch(\Exception $ex){
  845. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  846. }
  847. }
  848. public function multipleDelete()
  849. {
  850. try{
  851. foreach($this->multipleIds as $id)
  852. {
  853. \App\Models\Record::find($id)->delete();
  854. }
  855. }catch(\Exception $e){
  856. session()->flash('error','Errore (' . $e->getMessage() . ')');
  857. }
  858. $this->multipleAction = '';
  859. }
  860. public function createMember()
  861. {
  862. $this->newMemberFiscalCodeExist = false;
  863. $this->validate([
  864. // 'newMemberFiscalCode'=>'required|max:16',
  865. 'newMemberFirstName'=>'required',
  866. 'newMemberLastName'=>'required',
  867. //'newMemberEmail'=>'required',
  868. ]);
  869. // Check fiscal code exist
  870. $exist = false;
  871. if ($this->newMemberFiscalCode != '')
  872. {
  873. $check = \App\Models\Member::where('fiscal_code', $this->newMemberFiscalCode)
  874. ->where(function($query) {
  875. $query->where('is_deleted', false)
  876. ->orWhereNull('is_deleted');
  877. })->get();
  878. $exist = $check->count() > 0;
  879. }
  880. if (!$exist)
  881. {
  882. $member = \App\Models\Member::create([
  883. 'first_name' => strtoupper($this->newMemberFirstName),
  884. 'last_name' => strtoupper($this->newMemberLastName),
  885. 'email' => strtoupper($this->newMemberEmail),
  886. 'to_complete' => $this->newMemberToComplete,
  887. 'fiscal_code' => $this->newMemberFiscalCode,
  888. 'status' => true
  889. ]);
  890. $this->member_id = $member->id;
  891. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])
  892. ->where(function($query) {
  893. $query->where('is_archived', false)
  894. ->orWhereNull('is_archived');
  895. })
  896. ->where(function($query) {
  897. $query->where('is_deleted', false)
  898. ->orWhereNull('is_deleted');
  899. })->get();
  900. // $this->emit('reloadMembers');
  901. $this->emit('refresh');
  902. $this->newMemberFirstName = '';
  903. $this->newMemberLastName = '';
  904. $this->newMemberEmail = '';
  905. $this->newMemberToComplete = false;
  906. $this->newMemberFiscalCode = '';
  907. $this->emit('saved');
  908. //$this->validate();
  909. $this->selectId++;
  910. }
  911. else
  912. {
  913. $this->newMemberFiscalCodeExist = true;
  914. }
  915. }
  916. public function createReceipt()
  917. {
  918. // Ulteriore controllo commerciale/non commerciale
  919. if (!$this->commercial)
  920. {
  921. $create = false;
  922. $receipt = \App\Models\Receipt::where('record_id', $this->dataId)->orderBy('id', 'DESC')->first();
  923. if ($receipt != null)
  924. {
  925. // Controllo lo stato, se 99 ne genero una nuova
  926. if ($receipt->status == 99)
  927. {
  928. $create = true;
  929. }
  930. }
  931. else
  932. {
  933. $create = true;
  934. }
  935. if ($create)
  936. {
  937. $number = 1;
  938. $exist = \App\Models\Receipt::where('year', date("Y"))->orderBy('number', 'DESC')->first();
  939. if ($exist != null)
  940. $number = $exist->number + 1;
  941. $receipt = \App\Models\Receipt::create([
  942. 'record_id' => $this->dataId,
  943. 'member_id' => $this->member_id,
  944. 'supplier_id' => $this->supplier_id,
  945. 'payment_method_id' => $this->payment_method_id,
  946. 'number' => $number,
  947. 'date' => $this->date,
  948. 'year' => date("Y"),
  949. 'type' => $this->type,
  950. 'parent' => $this->parent,
  951. 'status' => 1,
  952. ]);
  953. foreach($this->rows as $row)
  954. {
  955. \App\Models\ReceiptRow::create([
  956. 'receip_id' => $receipt->id,
  957. 'causal_id' => $row["causal_id"],
  958. 'note' => $row["note"],
  959. 'vat_id' => $row["vat_id"],
  960. 'amount' => $this->currencyToDouble($row["amount"]),
  961. 'commercial' => $row["commercial"],
  962. 'when' => json_encode($row["when"])
  963. ]);
  964. }
  965. $this->currentReceip = $receipt;
  966. sendReceiptEmail($receipt);
  967. /*
  968. // Invio mail
  969. $pdf = PDF::loadView('receipt', array('receipt' => $receipt));
  970. $pdfName = "ricevuta_" . $receipt->number . "_" . $receipt->year . ".pdf";
  971. Storage::put('public/pdf/' . $pdfName, $pdf->output());
  972. $email = \App\Models\Member::findOrFail($receipt->member_id)->email;
  973. if ($email != '')
  974. {
  975. Mail::to($email)->send(new \App\Mail\ReceipEmail([
  976. 'pdf' => 'public/pdf/' . $pdfName,
  977. 'number' => $receipt->number . "/" . $receipt->year
  978. ]));
  979. }
  980. */
  981. session()->flash('receipt',"Ricevuta " . $number . "/" . date("Y") . " creata correttamente");
  982. // Apro la ricevuta
  983. $this->emit('showReceipt', $this->currentReceip->id);
  984. }
  985. }
  986. }
  987. public function removeReceipt()
  988. {
  989. $receipt = \App\Models\Receipt::findOrFail($this->currentReceip->id);
  990. $receipt->status = 99;
  991. $receipt->save();
  992. // cancellazione record associato
  993. try{
  994. $record = \App\Models\Record::find($receipt->record_id);
  995. if ($record->member_course_id > 0)
  996. {
  997. $months = json_decode($record->months);
  998. $c = \App\Models\MemberCourse::findOrFail($record->member_course_id);
  999. $xxx = json_decode($c->months);
  1000. foreach($xxx as $idx => $mm)
  1001. {
  1002. if (in_array($mm->m, $months))
  1003. {
  1004. $xxx[$idx]->status = "";
  1005. }
  1006. }
  1007. $c->months = json_encode($xxx);
  1008. $c->save();
  1009. }
  1010. $record->deleted = true;
  1011. $record->save();
  1012. $this->emit('success');
  1013. } catch(\Exception $ex) {
  1014. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1015. }
  1016. sendReceiptDeleteEmail($receipt);
  1017. $this->currentReceip = $receipt;
  1018. }
  1019. function currencyToDouble($val)
  1020. {
  1021. $x = str_replace("€", "", $val);
  1022. $x = str_replace(".", "", $x);
  1023. $x = str_replace(",", ".", $x);
  1024. return floatval(trim($x));
  1025. }
  1026. public function addRow()
  1027. {
  1028. $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'vat_id' => null, 'note' => '', 'commercial' => 0);
  1029. $this->emit('load-select');
  1030. }
  1031. public function delRow($idx)
  1032. {
  1033. unset($this->rows[$idx]);
  1034. // $this->emit('load-select');
  1035. }
  1036. public function addPeriod($idx)
  1037. {
  1038. $x = sizeof($this->rows[$idx]['when']) - 1;
  1039. $newDate = \Carbon\Carbon::create($this->rows[$idx]['when'][$x]["year"] . "-" . $this->rows[$idx]['when'][$x]["month"] . '-01')->addMonth();
  1040. $this->rows[$idx]['when'][] = array('month' => $newDate->format("n"), 'year' => $newDate->format("Y"), 'period' => '');
  1041. }
  1042. public function delPeriod($idx, $xxx)
  1043. {
  1044. array_splice($this->rows[$idx]['when'], $xxx, $xxx);
  1045. //unset($this->rows[$idx]['when'][$xxx]);
  1046. // $this->emit('load-select');
  1047. }
  1048. public function getTotal()
  1049. {
  1050. $total = 0.00;
  1051. foreach($this->rows as $r)
  1052. {
  1053. if ($r["amount"] != null && $r["amount"] != "")
  1054. {
  1055. $total += $this->currencyToDouble($r["amount"]);
  1056. if ($r["vat_id"] > 0)
  1057. $total += getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
  1058. }
  1059. }
  1060. return formatPrice($total);
  1061. // $this->emit('load-select');
  1062. }
  1063. public function getTotalCorrispettivo()
  1064. {
  1065. $total = 0.00;
  1066. foreach($this->corrispettivo as $r)
  1067. {
  1068. $total += $this->currencyToDouble($r);
  1069. }
  1070. return formatPrice($total);
  1071. // $this->emit('load-select');
  1072. }
  1073. public function getPrice()
  1074. {
  1075. $total = 0.00;
  1076. foreach($this->rows as $r)
  1077. {
  1078. if ($r["amount"] != null && $r["amount"] != "")
  1079. $total += $this->currencyToDouble($r["amount"]);
  1080. }
  1081. return formatPrice($total);
  1082. // $this->emit('load-select');
  1083. }
  1084. public function getVat()
  1085. {
  1086. $total = 0.00;
  1087. foreach($this->rows as $r)
  1088. {
  1089. if ($r["amount"] != null && $r["amount"] != "" && $r["vat_id"] > 0)
  1090. $total += getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
  1091. }
  1092. return formatPrice($total);
  1093. // $this->emit('load-select');
  1094. }
  1095. public function getVats()
  1096. {
  1097. $vats = array();
  1098. foreach($this->rows as $r)
  1099. {
  1100. if ($r["amount"] != null && $r["amount"] != "" && $r["vat_id"] > 0)
  1101. {
  1102. $vat = getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
  1103. $vatName = "";
  1104. foreach($this->vats as $v)
  1105. {
  1106. if ($v->id == $r["vat_id"])
  1107. $vatName = $v->name;
  1108. }
  1109. if (isset($vats[$vatName]))
  1110. $vats[$vatName] += $vat;
  1111. else
  1112. $vats[$vatName] = $vat;
  1113. }
  1114. }
  1115. return $vats;
  1116. // $this->emit('load-select');
  1117. }
  1118. public function setCausal($id, $idx)
  1119. {
  1120. $this->rows[$idx]["causal_id"] = $id;
  1121. }
  1122. public function printReceipt()
  1123. {
  1124. //$pdf = PDF::loadView('pdf/receipt', array('datas' => $datas, 'from' => $x, 'to' => $y, 'who' => '', 'matricola' => $matricola));
  1125. $pdf = PDF::loadView('receipt', array('receipt' => $this->currentReceip));//->output();
  1126. $pdfName = "Ricevuta_" . $this->currentReceip->member->last_name . "_" . $this->currentReceip->number . "_" . $this->currentReceip->year . ".pdf";
  1127. return $pdf->stream();
  1128. /*return response()->streamDownload(
  1129. fn () => print($pdf),
  1130. "ricevuta_" . $this->currentReceip->number . "_" . $this->currentReceip->year . ".pdf"
  1131. );*/
  1132. /*return response()->streamDownload(function () {
  1133. echo $pdf->stream();
  1134. }, 'test.pdf');*/
  1135. }
  1136. public function multiPeriod()
  1137. {
  1138. $this->multiP = true;
  1139. }
  1140. public function multiPeriodCreate($idx)
  1141. {
  1142. $period = \Carbon\CarbonPeriod::create($this->multiYearFrom . '-' . $this->multiMonthFrom . '-01', '1 month', $this->multiYearTo . '-' . $this->multiMonthTo . '-01');
  1143. $this->rows[$idx]['when'] = [];
  1144. foreach ($period as $dt) {
  1145. if (!in_array(array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => ''), $this->rows[$idx]['when']))
  1146. $this->rows[$idx]['when'][] = array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => '');
  1147. }
  1148. $this->multiP = false;
  1149. }
  1150. public function multiPeriodCancel()
  1151. {
  1152. $this->multiP = false;
  1153. }
  1154. public function getMonth($m)
  1155. {
  1156. $ret = '';
  1157. switch ($m) {
  1158. case 1:
  1159. $ret = 'Gennaio';
  1160. break;
  1161. case 2:
  1162. $ret = 'Febbraio';
  1163. break;
  1164. case 3:
  1165. $ret = 'Marzo';
  1166. break;
  1167. case 4:
  1168. $ret = 'Aprile';
  1169. break;
  1170. case 5:
  1171. $ret = 'Maggio';
  1172. break;
  1173. case 6:
  1174. $ret = 'Giugno';
  1175. break;
  1176. case 7:
  1177. $ret = 'Luglio';
  1178. break;
  1179. case 8:
  1180. $ret = 'Agosto';
  1181. break;
  1182. case 9:
  1183. $ret = 'Settembre';
  1184. break;
  1185. case 10:
  1186. $ret = 'Ottobre';
  1187. break;
  1188. case 11:
  1189. $ret = 'Novembre';
  1190. break;
  1191. case 12:
  1192. $ret = 'Dicembre';
  1193. break;
  1194. default:
  1195. $ret = '';
  1196. break;
  1197. }
  1198. return $ret;
  1199. }
  1200. public function setYear($idx, $xxx)
  1201. {
  1202. $currentYear = date('Y');
  1203. $currentMonth = date('n');
  1204. $m = $this->rows[$idx]["when"][$xxx]["month"];
  1205. if ($currentMonth > env('FISCAL_YEAR_MONTH_TO', 1) && $m > env('FISCAL_YEAR_MONTH_TO', 1)) { // current 9-12 && $m 9-12
  1206. $currentYear = $currentYear;
  1207. } elseif ($currentMonth > env('FISCAL_YEAR_MONTH_TO', 1) && $m < env('FISCAL_YEAR_MONTH_FROM', 1)) { // current 9-12 && $m 1-8
  1208. $currentYear += 1;
  1209. } elseif ($currentMonth < env('FISCAL_YEAR_MONTH_FROM', 1) && $m > env('FISCAL_YEAR_MONTH_TO', 1)) { // current 1-8 && $m 9-12
  1210. $currentYear -= 1;
  1211. } elseif ($currentMonth < env('FISCAL_YEAR_MONTH_FROM', 1) && $m < env('FISCAL_YEAR_MONTH_FROM', 1)) { // current 1-8 && $m 1-8
  1212. $currentYear = $currentYear;
  1213. }
  1214. $this->rows[$idx]["when"][$xxx]["year"] = $currentYear;
  1215. }
  1216. public function updatedNewMemberFirstName()
  1217. {
  1218. $this->checkUserExists();
  1219. }
  1220. public function updatedNewMemberLastName()
  1221. {
  1222. $this->checkUserExists();
  1223. }
  1224. private function checkUserExists(): void
  1225. {
  1226. $first = trim((string) $this->newMemberFirstName);
  1227. $last = trim((string) $this->newMemberLastName);
  1228. if ($first === '' || $last === '') {
  1229. $this->newMemberExists = false;
  1230. return;
  1231. }
  1232. $first = mb_strtolower($first);
  1233. $last = mb_strtolower($last);
  1234. $this->newMemberExists = \App\Models\Member::query()
  1235. ->where(function ($q) {
  1236. $q->where('is_deleted', false)
  1237. ->orWhereNull('is_deleted');
  1238. })
  1239. ->whereRaw('LOWER(TRIM(first_name)) = ?', [$first])
  1240. ->whereRaw('LOWER(TRIM(last_name)) = ?', [$last])
  1241. ->exists();
  1242. }
  1243. }