RecordIN.php 46 KB

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