RecordIN.php 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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. if ($m > date("M"))
  389. $this->rows[0]["when"][$idx]["year"] = $m < env('FISCAL_YEAR_MONTH_FROM', 1) ? (date("Y") + 1) : date("Y");
  390. else
  391. $this->rows[0]["when"][$idx]["year"] = $m > env('FISCAL_YEAR_MONTH_TO', 1) ? (date("Y")) : date("Y") + 1;
  392. if ($idx > 0)
  393. {
  394. if ($this->rows[0]["when"][$idx - 1]["year"] != $this->rows[0]["when"][$idx]["year"])
  395. $desc .= " " . $this->rows[0]["when"][$idx - 1]["year"] . " ";
  396. }
  397. $desc .= " " . $this->getMonth($m);// . " " . $this->rows[0]["when"][$idx]["year"];
  398. }
  399. $desc .= " " . $this->rows[0]["when"][$idx]["year"];
  400. $this->rows[0]["note"] = $desc;
  401. $count += 1;
  402. }
  403. if (isset($_GET["createSubscription"]) && $_GET["createSubscription"] == 1)
  404. {
  405. $this->createSubscription = 1;
  406. $this->courseId = $_GET["courseId"];
  407. $price = $_GET["subscription_price"] / 100 * 100;
  408. $this->refreshAfter = 1;
  409. if ($count == 1)
  410. $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'vat_id' => null, 'note' => '', 'commercial' => 0);
  411. $this->rows[$count]["causal_id"] = null;
  412. $this->rows[$count]["amount"] = formatPrice($price);
  413. $this->rows[$count]["note"] = "Pagamento iscrizione " . $course->name;
  414. if (isset($_GET["subCausalId"]))
  415. {
  416. $this->refreshAfter = 1;
  417. $this->rows[$count]["causal_id"] = $_GET["subCausalId"];
  418. }
  419. }
  420. }
  421. $this->first = false;
  422. $this->newMemberExists = false;
  423. }
  424. public function search()
  425. {
  426. $this->hasFilter = true;
  427. }
  428. public function disableSearch()
  429. {
  430. $this->filterMember = 0;
  431. $this->filterPaymentMethod = 0;
  432. $this->filterCausals = 0;
  433. $this->filterTo = '';
  434. $this->filterFrom = '';
  435. $this->filterCommercial = 0;
  436. $this->hasFilter = false;
  437. $this->total = 0;
  438. }
  439. public function render()
  440. {
  441. $datas = [];
  442. return view('livewire.records_in', ['datas' => $datas]);
  443. }
  444. public function executeMultipleAction(){
  445. if ($this->multipleAction == 'delete')
  446. $this->multipleDelete();
  447. }
  448. public function add()
  449. {
  450. $this->emit('load-select');
  451. //if ($this->hasFilter)
  452. $this->emit('hide-search');
  453. $this->resetFields();
  454. $this->dataId = 0;
  455. $this->add = true;
  456. $this->update = false;
  457. $this->emit('setEdit', true);
  458. }
  459. public function storeCorrispettivo()
  460. {
  461. $this->emit('refresh');
  462. $rules = [
  463. 'member_id' => 'required'
  464. ];
  465. $this->validate($rules);
  466. foreach($this->payments as $p)
  467. {
  468. if ($p->corrispettivo_fiscale)
  469. {
  470. $price = isset($this->corrispettivo[$p->id]) ? $this->currencyToDouble($this->corrispettivo[$p->id]) : 0;
  471. $destination_id = isset($this->corrispettivo_destination[$p->id]) ? $this->corrispettivo_destination[$p->id] : null;
  472. if ($price > 0)
  473. {
  474. $record = \App\Models\Record::create([
  475. 'member_id' => $this->member_id,
  476. 'supplier_id' => null,
  477. 'payment_method_id' => $p->id,
  478. 'destination_id' => $destination_id,
  479. 'commercial' => $this->commercial,
  480. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  481. 'date' => $this->date,
  482. 'type' => $this->type,
  483. 'amount' => $price,
  484. 'financial_movement' => $this->financial_movement,
  485. 'deleted' => $this->deleted
  486. ]);
  487. \App\Models\RecordRow::create([
  488. 'record_id' => $record->id,
  489. 'causal_id' => $this->corrispettivo_causal_id,
  490. 'note' => '',
  491. 'amount' => $price,
  492. 'vat_id' => null,
  493. 'commercial' => $this->commercial,
  494. 'when' => json_encode([])
  495. ]);
  496. }
  497. }
  498. }
  499. $this->resetFields();
  500. $this->add = false;
  501. $this->isDuplicate = false;
  502. $this->emit('setEdit', false);
  503. }
  504. public function store($generate)
  505. {
  506. $this->emit('refresh');
  507. $rules = [
  508. 'payment_method_id' => 'required',
  509. 'rows.*.causal_id' => 'required',
  510. 'rows.*.amount' => 'required'
  511. ];
  512. /*$f = false;
  513. foreach($this->rows as $row)
  514. {
  515. if ($row["commercial"]) $f = true;
  516. }
  517. if ($f)
  518. $rules["member_id"] = 'required';*/
  519. if(!$this->commercial)
  520. $rules["member_id"] = 'required';
  521. //dd($this->getErrorBag());
  522. /*$validator = Validator::make($request->all(), $rules);
  523. if ($validator->fails())
  524. {
  525. $this->emit('goToAnchor');
  526. }*/
  527. //if (!$this->validate($rules))
  528. //$this->emit('goToAnchor');
  529. $this->validate($rules);
  530. //dd("£ASDSDFS");
  531. try {
  532. $record = \App\Models\Record::create([
  533. 'member_id' => $this->member_id,
  534. 'supplier_id' => $this->supplier_id,
  535. 'payment_method_id' => $this->payment_method_id,
  536. 'destination_id' => $this->destination_id,
  537. 'commercial' => $this->commercial,
  538. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  539. 'date' => $this->date,
  540. 'type' => $this->type,
  541. 'financial_movement' => $this->financial_movement,
  542. 'deleted' => $this->deleted
  543. ]);
  544. $this->dataId = $record->id;
  545. $no_receipt_causal = false;
  546. // Inserisco le righe
  547. $tot = 0;
  548. $vat = 0;
  549. foreach($this->rows as $row)
  550. {
  551. foreach($row["when"] as $x => $y)
  552. {
  553. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  554. }
  555. \App\Models\RecordRow::create([
  556. 'record_id' => $this->dataId,
  557. 'causal_id' => $row["causal_id"],
  558. 'note' => $row["note"],
  559. 'amount' => $this->currencyToDouble($row["amount"]),
  560. 'vat_id' => $row["vat_id"],
  561. 'commercial' => $this->commercial,
  562. 'when' => json_encode($row["when"])
  563. ]);
  564. $tot += $this->currencyToDouble($row["amount"]);
  565. $vat += getVatValue($this->currencyToDouble($row["amount"]), $row["vat_id"]);
  566. $cau = \App\Models\Causal::findOrFail($row["causal_id"]);
  567. if ($cau->no_receipt)
  568. $no_receipt_causal = true;
  569. // Se la causale ricarica a borsellino, se il totale supera y euro, inserisco x euro gratis
  570. if ($cau->money)
  571. {
  572. if ($this->currencyToDouble($row["amount"]) >= env('MIN_MONEY', 100))
  573. {
  574. $money = new \App\Models\Money();
  575. $money->member_id = $this->member_id;
  576. $money->record_id = $this->dataId;
  577. $money->amount = env('EXTRA_MONEY', 10);
  578. $money->date = date("Y-m-d");
  579. $money->note = env('EXTRA_MONEY', 10) . ' in più per aver caricato ' . $this->currencyToDouble($row["amount"]);
  580. $money->save();
  581. }
  582. }
  583. }
  584. $record->amount = $tot;
  585. $record->save();
  586. // se sto pagando un corso, aggiorno lo status dei mesi
  587. if ($this->courseId > 0)
  588. {
  589. $record->months = json_encode($this->months);
  590. $record->member_course_id = $this->courseId;
  591. $record->save();
  592. $c = \App\Models\MemberCourse::findOrFail($this->courseId);
  593. $xxx = json_decode($c->months);
  594. foreach($xxx as $idx => $mm)
  595. {
  596. if (in_array($mm->m, $this->months))
  597. {
  598. $xxx[$idx]->status = 1;
  599. }
  600. }
  601. $c->months = json_encode($xxx);
  602. if ($this->createSubscription == 1)
  603. $c->subscribed = true;
  604. $c->save();
  605. }
  606. if (!$this->financial_movement)
  607. {
  608. $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
  609. if (!$payment_method->money)
  610. {
  611. if ($generate && !$no_receipt_causal)
  612. $this->createReceipt();
  613. }
  614. }
  615. session()->flash('success','Movimento creato');
  616. $this->resetFields();
  617. $this->add = false;
  618. $this->isDuplicate = false;
  619. $this->emit('setEdit', false);
  620. } catch (\Exception $ex) {
  621. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  622. }
  623. }
  624. public function duplicate($id){
  625. $record = \App\Models\Record::findOrFail($id);
  626. $newRecord = $record->replicate();
  627. $newRecord->save();
  628. $rows = \App\Models\RecordRow::where('record_id', $id)->get();
  629. foreach($rows as $r)
  630. {
  631. $newRow = $r->replicate();
  632. $newRow->record_id = $newRecord->id;
  633. $newRow->save();
  634. }
  635. $this->edit($newRecord->id);
  636. $this->isDuplicate = true;
  637. }
  638. public function edit($id){
  639. if (!isset($_GET["from"]) && $this->fromPage == '')
  640. $this->fromPage = 'in';
  641. $this->emit('setEdit', true);
  642. //if ($this->hasFilter)
  643. $this->emit('hide-search');
  644. $this->emit('load-select');
  645. try {
  646. $record = \App\Models\Record::findOrFail($id);
  647. if( !$record) {
  648. session()->flash('error','Movimento non trovato');
  649. } else {
  650. $this->member_id = $record->member_id;
  651. $this->supplier_id = $record->supplier_id;
  652. $this->payment_method_id = $record->payment_method_id;
  653. $this->destination_id = $record->destination_id;
  654. $this->amount = $record->amount;
  655. $this->commercial = $record->commercial;
  656. $this->corrispettivo_fiscale = $record->corrispettivo_fiscale;
  657. $this->date = date("Y-m-d", strtotime($record->date));
  658. $this->type = $record->type;
  659. $this->deleted = $record->deleted;
  660. $this->financial_movement = $record->financial_movement;
  661. $this->dataId = $record->id;
  662. $this->update = true;
  663. $this->add = false;
  664. $this->rows = \App\Models\RecordRow::where('record_id', $this->dataId)->select('causal_id', 'note', 'commercial', 'when', 'amount', 'vat_id')->get()->toArray();
  665. foreach($this->rows as $i => $r)
  666. {
  667. $this->rows[$i]['amount'] = formatPrice($this->rows[$i]['amount']);
  668. $this->rows[$i]['vat_id'] = $this->rows[$i]['vat_id'];
  669. $this->rows[$i]['when'] = json_decode($this->rows[$i]['when']);
  670. }
  671. $exist = \App\Models\Receipt::where('record_id', $id)->orderBy('id', 'DESC')->first();
  672. if ($exist != null)
  673. {
  674. $this->currentReceip = $exist;
  675. $this->parent = $this->currentReceip->parent;
  676. }
  677. }
  678. } catch (\Exception $ex) {
  679. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  680. }
  681. }
  682. public function update($generate = false)
  683. {
  684. $this->emit('refresh');
  685. $rules = [
  686. 'payment_method_id' => 'required',
  687. 'rows.*.causal_id' => 'required',
  688. 'rows.*.amount' => 'required'
  689. ];
  690. if(!$this->commercial)
  691. $rules["member_id"] = 'required';
  692. $this->validate($rules);
  693. try {
  694. \App\Models\Record::whereId($this->dataId)->update([
  695. 'member_id' => $this->member_id,
  696. 'supplier_id' => $this->supplier_id,
  697. 'payment_method_id' => $this->payment_method_id,
  698. 'destination_id' => $this->destination_id,
  699. 'commercial' => $this->commercial,
  700. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  701. 'date' => date("Y-m-d", strtotime($this->date)),
  702. 'type' => $this->type,
  703. 'financial_movement' => $this->financial_movement,
  704. 'deleted' => $this->deleted,
  705. ]);
  706. $no_receipt_causal = false;
  707. $tot = 0;
  708. $vat = 0;
  709. // Elimino le righe
  710. \App\Models\RecordRow::where('record_id', $this->dataId)->delete();
  711. // Inserisco le righe
  712. foreach($this->rows as $row)
  713. {
  714. foreach($row["when"] as $x => $y)
  715. {
  716. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  717. }
  718. \App\Models\RecordRow::create([
  719. 'record_id' => $this->dataId,
  720. 'causal_id' => $row["causal_id"],
  721. 'note' => $row["note"],
  722. 'vat_id' => $row["vat_id"],
  723. 'amount' => $this->currencyToDouble($row["amount"]),
  724. 'commercial' => $this->commercial,
  725. 'when' => json_encode($row["when"])
  726. ]);
  727. $tot += $this->currencyToDouble($row["amount"]);
  728. $vat += getVatValue($this->currencyToDouble($row["amount"]), $row["vat_id"]);
  729. $cau = \App\Models\Causal::findOrFail($row["causal_id"]);
  730. if ($cau->no_receipt)
  731. $no_receipt_causal = true;
  732. }
  733. $rec = \App\Models\Record::findOrFail($this->dataId);
  734. $rec->amount = $tot;
  735. $rec->save();
  736. if (!$this->financial_movement)
  737. {
  738. $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
  739. if (!$payment_method->money)
  740. {
  741. if ($generate && !$no_receipt_causal)
  742. $this->createReceipt();
  743. }
  744. }
  745. session()->flash('success','Movimento aggiornato');
  746. $this->resetFields();
  747. $this->update = false;
  748. $this->isDuplicate = false;
  749. $this->emit('setEdit', false);
  750. } catch (\Exception $ex) {
  751. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  752. }
  753. }
  754. public function rigenerate()
  755. {
  756. $this->emit('refresh');
  757. try {
  758. $record = \App\Models\Record::findOrFail($this->dataId);
  759. $newRecord = $record->replicate();
  760. $newRecord->date = date("Y-m-d");
  761. $newRecord->save();
  762. $recordRows = \App\Models\RecordRow::where('record_id', $this->dataId)->get();
  763. // Inserisco le righe
  764. foreach($recordRows as $row)
  765. {
  766. $newRow = $row->replicate();
  767. $newRow->record_id = $newRecord->id;
  768. $newRow->save();
  769. }
  770. $this->dataId = $newRecord->id;
  771. $this->createReceipt();
  772. session()->flash('success','Movimento aggiornato');
  773. $this->resetFields();
  774. $this->update = false;
  775. $this->isDuplicate = false;
  776. $this->emit('setEdit', false);
  777. } catch (\Exception $ex) {
  778. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  779. }
  780. }
  781. public function cancel()
  782. {
  783. // Se arrivo da duplica elimino
  784. if ($this->isDuplicate)
  785. {
  786. try{
  787. \App\Models\Record::find($this->dataId)->delete();
  788. session()->flash('success',"Movimento eliminato");
  789. }catch(\Exception $e){
  790. session()->flash('error','Errore (' . $e->getMessage() . ')');
  791. }
  792. }
  793. $this->isDuplicate = false;
  794. $this->add = false;
  795. $this->update = false;
  796. $this->resetFields();
  797. $this->emit('setEdit', false);
  798. return redirect()->to('/in');
  799. }
  800. public function delete($id)
  801. {
  802. try{
  803. $r = \App\Models\Record::find($id);
  804. // Annullo la ricevuta
  805. $receipt = \App\Models\Receipt::where('record_id', $r->id)->first();
  806. if($receipt)
  807. {
  808. $receipt->status = 99;
  809. sendReceiptDeleteEmail($receipt);
  810. $receipt->save();
  811. }
  812. if ($r->member_course_id > 0)
  813. {
  814. $months = json_decode($r->months);
  815. $c = \App\Models\MemberCourse::findOrFail($r->member_course_id);
  816. $xxx = json_decode($c->months);
  817. foreach($xxx as $idx => $mm)
  818. {
  819. if (in_array($mm->m, $months))
  820. {
  821. $xxx[$idx]->status = "";
  822. }
  823. }
  824. $c->months = json_encode($xxx);
  825. $c->save();
  826. }
  827. $r->deleted = true;
  828. $r->save();
  829. session()->flash('success','Movimento aggiornato');
  830. $this->resetFields();
  831. $this->update = false;
  832. $this->isDuplicate = false;
  833. $this->emit('setEdit', false);
  834. $this->emit('reload');
  835. //$this->emit('load-data-table');
  836. }catch(\Exception $ex){
  837. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  838. }
  839. }
  840. public function multipleDelete()
  841. {
  842. try{
  843. foreach($this->multipleIds as $id)
  844. {
  845. \App\Models\Record::find($id)->delete();
  846. }
  847. }catch(\Exception $e){
  848. session()->flash('error','Errore (' . $e->getMessage() . ')');
  849. }
  850. $this->multipleAction = '';
  851. }
  852. public function createMember()
  853. {
  854. $this->newMemberFiscalCodeExist = false;
  855. $this->validate([
  856. // 'newMemberFiscalCode'=>'required|max:16',
  857. 'newMemberFirstName'=>'required',
  858. 'newMemberLastName'=>'required',
  859. //'newMemberEmail'=>'required',
  860. ]);
  861. // Check fiscal code exist
  862. $exist = false;
  863. if ($this->newMemberFiscalCode != '')
  864. {
  865. $check = \App\Models\Member::where('fiscal_code', $this->newMemberFiscalCode)
  866. ->where(function($query) {
  867. $query->where('is_deleted', false)
  868. ->orWhereNull('is_deleted');
  869. })->get();
  870. $exist = $check->count() > 0;
  871. }
  872. if (!$exist)
  873. {
  874. $member = \App\Models\Member::create([
  875. 'first_name' => strtoupper($this->newMemberFirstName),
  876. 'last_name' => strtoupper($this->newMemberLastName),
  877. 'email' => strtoupper($this->newMemberEmail),
  878. 'to_complete' => $this->newMemberToComplete,
  879. 'fiscal_code' => $this->newMemberFiscalCode,
  880. 'status' => true
  881. ]);
  882. $this->member_id = $member->id;
  883. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])
  884. ->where(function($query) {
  885. $query->where('is_archived', false)
  886. ->orWhereNull('is_archived');
  887. })
  888. ->where(function($query) {
  889. $query->where('is_deleted', false)
  890. ->orWhereNull('is_deleted');
  891. })->get();
  892. // $this->emit('reloadMembers');
  893. $this->emit('refresh');
  894. $this->newMemberFirstName = '';
  895. $this->newMemberLastName = '';
  896. $this->newMemberEmail = '';
  897. $this->newMemberToComplete = false;
  898. $this->newMemberFiscalCode = '';
  899. $this->emit('saved');
  900. //$this->validate();
  901. $this->selectId++;
  902. }
  903. else
  904. {
  905. $this->newMemberFiscalCodeExist = true;
  906. }
  907. }
  908. public function createReceipt()
  909. {
  910. // Ulteriore controllo commerciale/non commerciale
  911. if (!$this->commercial)
  912. {
  913. $create = false;
  914. $receipt = \App\Models\Receipt::where('record_id', $this->dataId)->orderBy('id', 'DESC')->first();
  915. if ($receipt != null)
  916. {
  917. // Controllo lo stato, se 99 ne genero una nuova
  918. if ($receipt->status == 99)
  919. {
  920. $create = true;
  921. }
  922. }
  923. else
  924. {
  925. $create = true;
  926. }
  927. if ($create)
  928. {
  929. $number = 1;
  930. $exist = \App\Models\Receipt::where('year', date("Y"))->orderBy('number', 'DESC')->first();
  931. if ($exist != null)
  932. $number = $exist->number + 1;
  933. $receipt = \App\Models\Receipt::create([
  934. 'record_id' => $this->dataId,
  935. 'member_id' => $this->member_id,
  936. 'supplier_id' => $this->supplier_id,
  937. 'payment_method_id' => $this->payment_method_id,
  938. 'number' => $number,
  939. 'date' => $this->date,
  940. 'year' => date("Y"),
  941. 'type' => $this->type,
  942. 'parent' => $this->parent,
  943. 'status' => 1,
  944. ]);
  945. foreach($this->rows as $row)
  946. {
  947. \App\Models\ReceiptRow::create([
  948. 'receip_id' => $receipt->id,
  949. 'causal_id' => $row["causal_id"],
  950. 'note' => $row["note"],
  951. 'vat_id' => $row["vat_id"],
  952. 'amount' => $this->currencyToDouble($row["amount"]),
  953. 'commercial' => $row["commercial"],
  954. 'when' => json_encode($row["when"])
  955. ]);
  956. }
  957. $this->currentReceip = $receipt;
  958. sendReceiptEmail($receipt);
  959. /*
  960. // Invio mail
  961. $pdf = PDF::loadView('receipt', array('receipt' => $receipt));
  962. $pdfName = "ricevuta_" . $receipt->number . "_" . $receipt->year . ".pdf";
  963. Storage::put('public/pdf/' . $pdfName, $pdf->output());
  964. $email = \App\Models\Member::findOrFail($receipt->member_id)->email;
  965. if ($email != '')
  966. {
  967. Mail::to($email)->send(new \App\Mail\ReceipEmail([
  968. 'pdf' => 'public/pdf/' . $pdfName,
  969. 'number' => $receipt->number . "/" . $receipt->year
  970. ]));
  971. }
  972. */
  973. session()->flash('receipt',"Ricevuta " . $number . "/" . date("Y") . " creata correttamente");
  974. // Apro la ricevuta
  975. $this->emit('showReceipt', $this->currentReceip->id);
  976. }
  977. }
  978. }
  979. public function removeReceipt()
  980. {
  981. $receipt = \App\Models\Receipt::findOrFail($this->currentReceip->id);
  982. $receipt->status = 99;
  983. $receipt->save();
  984. // cancellazione record associato
  985. try{
  986. $record = \App\Models\Record::find($receipt->record_id);
  987. if ($record->member_course_id > 0)
  988. {
  989. $months = json_decode($record->months);
  990. $c = \App\Models\MemberCourse::findOrFail($record->member_course_id);
  991. $xxx = json_decode($c->months);
  992. foreach($xxx as $idx => $mm)
  993. {
  994. if (in_array($mm->m, $months))
  995. {
  996. $xxx[$idx]->status = "";
  997. }
  998. }
  999. $c->months = json_encode($xxx);
  1000. $c->save();
  1001. }
  1002. $record->deleted = true;
  1003. $record->save();
  1004. $this->emit('success');
  1005. } catch(\Exception $ex) {
  1006. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1007. }
  1008. sendReceiptDeleteEmail($receipt);
  1009. $this->currentReceip = $receipt;
  1010. }
  1011. function currencyToDouble($val)
  1012. {
  1013. $x = str_replace("€", "", $val);
  1014. $x = str_replace(".", "", $x);
  1015. $x = str_replace(",", ".", $x);
  1016. return floatval(trim($x));
  1017. }
  1018. public function addRow()
  1019. {
  1020. $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'vat_id' => null, 'note' => '', 'commercial' => 0);
  1021. $this->emit('load-select');
  1022. }
  1023. public function delRow($idx)
  1024. {
  1025. unset($this->rows[$idx]);
  1026. // $this->emit('load-select');
  1027. }
  1028. public function addPeriod($idx)
  1029. {
  1030. $x = sizeof($this->rows[$idx]['when']) - 1;
  1031. $newDate = \Carbon\Carbon::create($this->rows[$idx]['when'][$x]["year"] . "-" . $this->rows[$idx]['when'][$x]["month"] . '-01')->addMonth();
  1032. $this->rows[$idx]['when'][] = array('month' => $newDate->format("n"), 'year' => $newDate->format("Y"), 'period' => '');
  1033. }
  1034. public function delPeriod($idx, $xxx)
  1035. {
  1036. array_splice($this->rows[$idx]['when'], $xxx, $xxx);
  1037. //unset($this->rows[$idx]['when'][$xxx]);
  1038. // $this->emit('load-select');
  1039. }
  1040. public function getTotal()
  1041. {
  1042. $total = 0.00;
  1043. foreach($this->rows as $r)
  1044. {
  1045. if ($r["amount"] != null && $r["amount"] != "")
  1046. {
  1047. $total += $this->currencyToDouble($r["amount"]);
  1048. if ($r["vat_id"] > 0)
  1049. $total += getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
  1050. }
  1051. }
  1052. return formatPrice($total);
  1053. // $this->emit('load-select');
  1054. }
  1055. public function getTotalCorrispettivo()
  1056. {
  1057. $total = 0.00;
  1058. foreach($this->corrispettivo as $r)
  1059. {
  1060. $total += $this->currencyToDouble($r);
  1061. }
  1062. return formatPrice($total);
  1063. // $this->emit('load-select');
  1064. }
  1065. public function getPrice()
  1066. {
  1067. $total = 0.00;
  1068. foreach($this->rows as $r)
  1069. {
  1070. if ($r["amount"] != null && $r["amount"] != "")
  1071. $total += $this->currencyToDouble($r["amount"]);
  1072. }
  1073. return formatPrice($total);
  1074. // $this->emit('load-select');
  1075. }
  1076. public function getVat()
  1077. {
  1078. $total = 0.00;
  1079. foreach($this->rows as $r)
  1080. {
  1081. if ($r["amount"] != null && $r["amount"] != "" && $r["vat_id"] > 0)
  1082. $total += getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
  1083. }
  1084. return formatPrice($total);
  1085. // $this->emit('load-select');
  1086. }
  1087. public function getVats()
  1088. {
  1089. $vats = array();
  1090. foreach($this->rows as $r)
  1091. {
  1092. if ($r["amount"] != null && $r["amount"] != "" && $r["vat_id"] > 0)
  1093. {
  1094. $vat = getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
  1095. $vatName = "";
  1096. foreach($this->vats as $v)
  1097. {
  1098. if ($v->id == $r["vat_id"])
  1099. $vatName = $v->name;
  1100. }
  1101. if (isset($vats[$vatName]))
  1102. $vats[$vatName] += $vat;
  1103. else
  1104. $vats[$vatName] = $vat;
  1105. }
  1106. }
  1107. return $vats;
  1108. // $this->emit('load-select');
  1109. }
  1110. public function setCausal($id, $idx)
  1111. {
  1112. $this->rows[$idx]["causal_id"] = $id;
  1113. }
  1114. public function printReceipt()
  1115. {
  1116. //$pdf = PDF::loadView('pdf/receipt', array('datas' => $datas, 'from' => $x, 'to' => $y, 'who' => '', 'matricola' => $matricola));
  1117. $pdf = PDF::loadView('receipt', array('receipt' => $this->currentReceip));//->output();
  1118. $pdfName = "Ricevuta_" . $this->currentReceip->member->last_name . "_" . $this->currentReceip->number . "_" . $this->currentReceip->year . ".pdf";
  1119. return $pdf->stream();
  1120. /*return response()->streamDownload(
  1121. fn () => print($pdf),
  1122. "ricevuta_" . $this->currentReceip->number . "_" . $this->currentReceip->year . ".pdf"
  1123. );*/
  1124. /*return response()->streamDownload(function () {
  1125. echo $pdf->stream();
  1126. }, 'test.pdf');*/
  1127. }
  1128. public function multiPeriod()
  1129. {
  1130. $this->multiP = true;
  1131. }
  1132. public function multiPeriodCreate($idx)
  1133. {
  1134. $period = \Carbon\CarbonPeriod::create($this->multiYearFrom . '-' . $this->multiMonthFrom . '-01', '1 month', $this->multiYearTo . '-' . $this->multiMonthTo . '-01');
  1135. $this->rows[$idx]['when'] = [];
  1136. foreach ($period as $dt) {
  1137. if (!in_array(array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => ''), $this->rows[$idx]['when']))
  1138. $this->rows[$idx]['when'][] = array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => '');
  1139. }
  1140. $this->multiP = false;
  1141. }
  1142. public function multiPeriodCancel()
  1143. {
  1144. $this->multiP = false;
  1145. }
  1146. public function getMonth($m)
  1147. {
  1148. $ret = '';
  1149. switch ($m) {
  1150. case 1:
  1151. $ret = 'Gennaio';
  1152. break;
  1153. case 2:
  1154. $ret = 'Febbraio';
  1155. break;
  1156. case 3:
  1157. $ret = 'Marzo';
  1158. break;
  1159. case 4:
  1160. $ret = 'Aprile';
  1161. break;
  1162. case 5:
  1163. $ret = 'Maggio';
  1164. break;
  1165. case 6:
  1166. $ret = 'Giugno';
  1167. break;
  1168. case 7:
  1169. $ret = 'Luglio';
  1170. break;
  1171. case 8:
  1172. $ret = 'Agosto';
  1173. break;
  1174. case 9:
  1175. $ret = 'Settembre';
  1176. break;
  1177. case 10:
  1178. $ret = 'Ottobre';
  1179. break;
  1180. case 11:
  1181. $ret = 'Novembre';
  1182. break;
  1183. case 12:
  1184. $ret = 'Dicembre';
  1185. break;
  1186. default:
  1187. $ret = '';
  1188. break;
  1189. }
  1190. return $ret;
  1191. }
  1192. public function setYear($idx, $xxx)
  1193. {
  1194. $m = $this->rows[$idx]["when"][$xxx]["month"];
  1195. $this->rows[$idx]["when"][$xxx]["year"] = $m < env('FISCAL_YEAR_MONTH_FROM', 1) ? (date("Y") + 1) : date("Y");
  1196. }
  1197. public function updatedNewMemberFirstName()
  1198. {
  1199. $this->checkUserExists();
  1200. }
  1201. public function updatedNewMemberLastName()
  1202. {
  1203. $this->checkUserExists();
  1204. }
  1205. private function checkUserExists(): void
  1206. {
  1207. $first = trim((string) $this->newMemberFirstName);
  1208. $last = trim((string) $this->newMemberLastName);
  1209. if ($first === '' || $last === '') {
  1210. $this->newMemberExists = false;
  1211. return;
  1212. }
  1213. $first = mb_strtolower($first);
  1214. $last = mb_strtolower($last);
  1215. $this->newMemberExists = \App\Models\Member::query()
  1216. ->where(function ($q) {
  1217. $q->where('is_deleted', false)
  1218. ->orWhereNull('is_deleted');
  1219. })
  1220. ->whereRaw('LOWER(TRIM(first_name)) = ?', [$first])
  1221. ->whereRaw('LOWER(TRIM(last_name)) = ?', [$last])
  1222. ->exists();
  1223. }
  1224. }