RecordOUT.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class RecordOUT extends Component
  5. {
  6. protected $listeners = ['setCausal' => 'setCausal','refreshMembers' => 'refreshMembers'];
  7. public $sortField ='date';
  8. public $sortAsc = false;
  9. public $typeOUT = "OUT";
  10. public $fromPage = '';
  11. public $multiP = false;
  12. public $multiMonthTo = 0;
  13. public $multiYearTo = 0;
  14. public $multiMonthFrom = 0;
  15. public $multiYearFrom = 0;
  16. public function sortBy($field)
  17. {
  18. if($this->sortField === $field)
  19. {
  20. $this->sortAsc = ! $this->sortAsc;
  21. } else {
  22. $this->sortAsc = true;
  23. }
  24. $this->sortField = $field;
  25. }
  26. public $records, $dataId, $member_id, $supplier_id,
  27. $causal_id,
  28. $payment_method_id,
  29. $origin_id,
  30. $date,
  31. $month,
  32. $year,
  33. $type,
  34. $amount,
  35. $note,
  36. $commercial, $update = false, $add = false;
  37. public $filterSupplier = 0, $filterPaymentMethod = 0, $filterCausals = [], $filterFrom = '', $filterTo = '', $filterCommercial = 0;
  38. public $hasFilter = false;
  39. public $total = 0;
  40. public $selectedFilter = '';
  41. public $multipleIds = [];
  42. public $multipleAction = '';
  43. public $selectId = 0;
  44. public $parent = null;
  45. public $newMemberFirstName = '';
  46. public $newMemberLastName = '';
  47. public $newMemberEmail = '';
  48. public $newMemberToComplete = false;
  49. public $newMemberFiscalCode = '';
  50. public $newMemberFiscalCodeExist = false;
  51. public $newMemberExists = false;
  52. public $causals = array();
  53. public $payments = array();
  54. public $banks = array();
  55. public $members = array();
  56. public $suppliers = array();
  57. public $rows = array();
  58. protected $rules = [
  59. 'member_id' => ['nullable', 'required_without:supplier_id'],
  60. 'supplier_id' => ['nullable', 'required_without:member_id'],
  61. 'payment_method_id' => 'required',
  62. 'rows.*.causal_id' => 'required',
  63. 'rows.*.amount' => 'required'
  64. //'causal_id' => 'required',
  65. //'amount' => 'required'
  66. ];
  67. protected $messages = [
  68. 'member_id.required_without' => 'Devi compilare almeno uno tra membro e fornitore',
  69. 'supplier_id.required_without' => 'Devi compilare almeno uno tra membro e fornitore',
  70. 'payment_method_id.required' => 'Il metodo di pagamento è obbligatorio',
  71. 'rows.*.amount.required' => 'L\'importo è obbligatorio',
  72. 'rows.*.causal_id.required' => 'La causale è obbligatoria'
  73. ];
  74. public function getSupplierProperty()
  75. {
  76. $ret = null;
  77. if ($this->supplier_id > 0)
  78. {
  79. $ret = \App\Models\Supplier::findOrFail($this->supplier_id);
  80. }
  81. return $ret;
  82. }
  83. public function getCausalProperty()
  84. {
  85. $ret = null;
  86. if ($this->causal_id > 0)
  87. {
  88. $ret = \App\Models\Causal::findOrFail($this->causal_id);
  89. }
  90. return $ret;
  91. }
  92. public function resetFields(){
  93. $this->member_id = null;
  94. $this->supplier_id = null;
  95. //$this->causal_id = null;
  96. $this->payment_method_id = null;
  97. $this->origin_id = null;
  98. $this->date = date("Y-m-d");
  99. //$this->month = date("n");
  100. //$this->year = date("Y");
  101. $this->type = 'OUT';
  102. //$this->note = '';
  103. //$this->amount = null;
  104. $this->newMemberFiscalCodeExist = false;
  105. $this->parent = '';
  106. $this->newMemberFirstName = '';
  107. $this->newMemberLastName = '';
  108. $this->newMemberEmail = '';
  109. $this->newMemberToComplete = false;
  110. $this->newMemberFiscalCode = '';
  111. $this->newMemberFiscalCodeExist = false;
  112. $this->newMemberExists = false;
  113. $this->commercial = 1;
  114. $this->rows = array();
  115. $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'note' => '', 'commercial' => 0);
  116. $this->emit('load-data-table');
  117. }
  118. public function getCausale($records, $indentation)
  119. {
  120. foreach($records as $record)
  121. {
  122. $this->causals[] = array('id' => $record->id, 'name' => $record->getTree());
  123. if(count($record->childs))
  124. $this->getCausale($record->childs, $indentation + 1);
  125. }
  126. }
  127. public function hydrate()
  128. {
  129. $this->emit('load-select');
  130. }
  131. public function mount()
  132. {
  133. if (isset($_GET["from"]))
  134. {
  135. $this->fromPage = $_GET["from"];
  136. }
  137. if(\Auth::user()->level != env('LEVEL_ADMIN', 0))
  138. return redirect()->to('/dashboard');
  139. $this->multiMonthFrom = date("n");
  140. $this->multiYearFrom = date("Y");
  141. $this->multiMonthTo = date("n");
  142. $this->multiYearTo = date("Y");
  143. if (isset($_GET["new"]))
  144. $this->add();
  145. $this->causals = array();
  146. $this->getCausale(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'OUT')->orderBy('name')->get(), 0);
  147. $this->refreshMembers();
  148. $this->suppliers = \App\Models\Supplier::select('name','id')->orderBy('name')->get();
  149. $this->payments = \App\Models\PaymentMethod::select('id', 'name')->whereIn('type', array('ALL', 'OUT'))->where('enabled', true)->orderBy('name')->get();
  150. $this->banks = \App\Models\Bank::select('id', 'name')->where('enabled', true)->whereIn('visibility', array('ALL', 'OUT'))->orderBy('name')->get();
  151. }
  152. public function refreshMembers()
  153. {
  154. if ($this->add || $this->update)
  155. {
  156. if($this->commercial){
  157. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])
  158. ->where(function($query) {
  159. $query->where('is_archived', false)
  160. ->orWhereNull('is_archived');
  161. })
  162. ->where(function($query) {
  163. $query->where('is_deleted', false)
  164. ->orWhereNull('is_deleted');
  165. })->orderBy('last_name')->orderBy('first_name')->get();
  166. } else {
  167. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->where('current_status', 2)->orWhere('current_status', 1)
  168. ->where(function($query) {
  169. $query->where('is_archived', false)
  170. ->orWhereNull('is_archived');
  171. })
  172. ->where(function($query) {
  173. $query->where('is_deleted', false)
  174. ->orWhereNull('is_deleted');
  175. })->orderBy('last_name')->orderBy('first_name')->get();
  176. }
  177. }
  178. else
  179. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])
  180. ->where(function($query) {
  181. $query->where('is_archived', false)
  182. ->orWhereNull('is_archived');
  183. })
  184. ->where(function($query) {
  185. $query->where('is_deleted', false)
  186. ->orWhereNull('is_deleted');
  187. })->orderBy('last_name')->orderBy('first_name')->get();
  188. }
  189. public function updatedPaymentMethodId() {
  190. if ($this->payment_method_id) {
  191. $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
  192. $this->origin_id = $payment_method->origin_id ?? null;
  193. }
  194. }
  195. public function updatedCommercial($value)
  196. {
  197. $this->emitSelf('refreshMembers');
  198. }
  199. public function getCausal($causal)
  200. {
  201. $ret = '';
  202. if ($causal > 0)
  203. {
  204. $ret = \App\Models\Causal::findOrFail($causal)->getTree();
  205. }
  206. return $ret;
  207. }
  208. public function search()
  209. {
  210. $this->hasFilter = true;
  211. }
  212. public function disableSearch()
  213. {
  214. $this->filterSupplier = 0;
  215. $this->filterPaymentMethod = 0;
  216. $this->filterCausals = [];
  217. $this->filterTo = '';
  218. $this->filterFrom = '';
  219. $this->filterCommercial = 0;
  220. $this->hasFilter = false;
  221. $this->hasFilter = false;
  222. }
  223. public function render()
  224. {
  225. $datas = [];
  226. if (false)
  227. {
  228. if ($this->hasFilter)
  229. {
  230. $datas = \App\Models\Record::where('type', 'OUT')->with('supplier', 'payment_method');
  231. /*if ($this->filterCommercial > 0)
  232. {
  233. $datas = $datas->where('commercial', $this->filterCommercial == 1 ? true : false);
  234. }*/
  235. if ($this->filterSupplier > 0)
  236. {
  237. $datas = $datas->where('supplier_id', $this->filterSupplier);
  238. }
  239. if ($this->filterPaymentMethod > 0)
  240. {
  241. $datas = $datas->where('payment_method_id', $this->filterPaymentMethod);
  242. }
  243. /*if (sizeof($this->filterCausals) > 0)
  244. {
  245. $datas = $datas->whereIn('causal_id', $this->filterCausals);
  246. }*/
  247. if ($this->filterFrom != '')
  248. {
  249. $datas = $datas->where('date', '>=', $this->filterFrom);
  250. }
  251. if ($this->filterTo != '')
  252. {
  253. $datas = $datas->where('date', '<=', $this->filterTo);
  254. }
  255. //$this->records = $datas->orderBy('date', 'DESC')->get();
  256. $this->records = $datas->get();
  257. //$this->total = $this->records->sum('amount');
  258. $this->total = 0;
  259. foreach($this->records as $r)
  260. {
  261. foreach($r->rows as $rr)
  262. {
  263. $this->total += $rr->amount;
  264. }
  265. }
  266. }
  267. else
  268. {
  269. if ($this->selectedFilter == '')
  270. {
  271. $this->records = \App\Models\Record::where('type', 'OUT')->with('supplier', 'payment_method')->limit(20)->orderBy('date', 'DESC')->orderBy('id', 'DESC')->get();
  272. }
  273. else
  274. {
  275. if ($this->selectedFilter == 0)
  276. {
  277. $fromDate = date("Y-m-d");
  278. $toDate = date("Y-m-d");
  279. }
  280. if ($this->selectedFilter == 1)
  281. {
  282. $fromDate = date("Y-m-01");
  283. $toDate = date("Y-m-t");
  284. }
  285. if ($this->selectedFilter == 2)
  286. {
  287. $fromDate = date("Y-01-01");
  288. $toDate = date("Y-12-31");
  289. }
  290. if ($this->selectedFilter == 3)
  291. {
  292. $fromDate = date("2000-01-01");
  293. $toDate = date("Y-12-31");
  294. }
  295. $this->records = \App\Models\Record::where('type', 'OUT')->whereBetween('date', [$fromDate, $toDate])->with('supplier', 'payment_method')->get();
  296. }
  297. }
  298. foreach($this->records as $r)
  299. {
  300. $r->total = $r->getTotal();
  301. $r->supplier = $r->supplier ? $r->supplier->name : '';
  302. $r->payment = $r->payment_method ? $r->payment_method->name : '';
  303. }
  304. }
  305. /*if ($this->sortField != '')
  306. {
  307. if ($this->sortAsc)
  308. $this->records = $this->records->sortBy($this->sortField);
  309. else
  310. $this->records = $this->records->sortByDesc($this->sortField);
  311. }
  312. else
  313. $this->records = $this->records->sortByDesc('id');*/
  314. return view('livewire.records_out');
  315. }
  316. public function executeMultipleAction(){
  317. if ($this->multipleAction == 'delete')
  318. $this->multipleDelete();
  319. }
  320. public function add()
  321. {
  322. $this->emit('load-select');
  323. //if ($this->hasFilter)
  324. $this->emit('hide-search');
  325. $this->resetFields();
  326. $this->add = true;
  327. $this->update = false;
  328. $this->emit('setEdit', true);
  329. }
  330. public function store()
  331. {
  332. $this->emit('refresh');
  333. $this->validate();
  334. try {
  335. $record = \App\Models\Record::create([
  336. 'member_id' => $this->member_id,
  337. 'supplier_id' => $this->supplier_id,
  338. //'causal_id' => $this->causal_id,
  339. 'payment_method_id' => $this->payment_method_id,
  340. 'origin_id' => $this->origin_id,
  341. 'date' => $this->date,
  342. //'month' => $this->month,
  343. //'year' => $this->year,
  344. //'note' => $this->note,
  345. 'type' => $this->type,
  346. //'amount' => $this->currencyToDouble($this->amount),
  347. 'commercial' => $this->commercial,
  348. ]);
  349. $this->dataId = $record->id;
  350. $tot = 0;
  351. foreach($this->rows as $row)
  352. {
  353. foreach($row["when"] as $x => $y)
  354. {
  355. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  356. }
  357. \App\Models\RecordRow::create([
  358. 'record_id' => $this->dataId,
  359. 'causal_id' => $row["causal_id"],
  360. 'note' => $row["note"],
  361. 'amount' => $this->currencyToDouble($row["amount"]),
  362. 'commercial' => $row["commercial"],
  363. 'when' => json_encode($row["when"])
  364. ]);
  365. $tot += $this->currencyToDouble($row["amount"]);
  366. }
  367. $record->amount = $tot;
  368. $record->save();
  369. session()->flash('success','Movimento creato');
  370. $this->resetFields();
  371. $this->add = false;
  372. $this->emit('setEdit', false);
  373. } catch (\Exception $ex) {
  374. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  375. }
  376. }
  377. public function edit($id){
  378. $this->resetFields();
  379. if (!isset($_GET["from"]) && $this->fromPage == '')
  380. $this->fromPage = 'out';
  381. $this->emit('setEdit', true);
  382. $this->emit('load-select');
  383. //if ($this->hasFilter)
  384. $this->emit('hide-search');
  385. try {
  386. $record = \App\Models\Record::findOrFail($id);
  387. if( !$record) {
  388. session()->flash('error','Movimento non trovato');
  389. } else {
  390. $this->member_id = $record->member_id;
  391. $this->parent = $record->member->parent;
  392. $this->supplier_id = $record->supplier_id;
  393. //$this->causal_id = $record->causal_id;
  394. $this->payment_method_id = $record->payment_method_id;
  395. $this->origin_id = $record->origin_id;
  396. $this->date = date("Y-m-d", strtotime($record->date));
  397. //$this->month = $record->month;
  398. //$this->year = $record->year;
  399. //$this->note = $record->note;
  400. $this->type = $record->type;
  401. //$this->amount = formatPrice($record->amount);
  402. $this->commercial = $record->commercial;
  403. $this->dataId = $record->id;
  404. $this->update = true;
  405. $this->add = false;
  406. $this->rows = \App\Models\RecordRow::where('record_id', $this->dataId)->select('causal_id', 'note', 'commercial', 'when', 'amount')->get()->toArray();
  407. foreach($this->rows as $i => $r)
  408. {
  409. $this->rows[$i]['amount'] = formatPrice($this->rows[$i]['amount']);
  410. $this->rows[$i]['when'] = json_decode($this->rows[$i]['when']);
  411. }
  412. }
  413. } catch (\Exception $ex) {
  414. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  415. }
  416. }
  417. public function update()
  418. {
  419. $this->emit('refresh');
  420. $this->validate();
  421. try {
  422. \App\Models\Record::whereId($this->dataId)->update([
  423. 'member_id' => $this->member_id,
  424. 'supplier_id' => $this->supplier_id,
  425. //'causal_id' => $this->causal_id,
  426. 'payment_method_id' => $this->payment_method_id,
  427. 'origin_id' => $this->origin_id,
  428. 'date' => $this->date,
  429. //'month' => $this->month,
  430. //'year' => $this->year,
  431. //'note' => $this->note,
  432. 'type' => $this->type,
  433. //'amount' => $this->currencyToDouble($this->amount),
  434. 'commercial' => $this->commercial,
  435. ]);
  436. $tot = 0;
  437. // Elimino le righe
  438. \App\Models\RecordRow::where('record_id', $this->dataId)->delete();
  439. // Inserisco le righe
  440. foreach($this->rows as $row)
  441. {
  442. foreach($row["when"] as $x => $y)
  443. {
  444. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  445. }
  446. \App\Models\RecordRow::create([
  447. 'record_id' => $this->dataId,
  448. 'causal_id' => $row["causal_id"],
  449. 'note' => $row["note"],
  450. 'amount' => $this->currencyToDouble($row["amount"]),
  451. 'commercial' => $row["commercial"],
  452. 'when' => json_encode($row["when"])
  453. ]);
  454. $tot += $this->currencyToDouble($row["amount"]);
  455. }
  456. $rec = \App\Models\Record::findOrFail($this->dataId);
  457. $rec->amount = $tot;
  458. $rec->save();
  459. session()->flash('success','Movimento aggiornato');
  460. $this->resetFields();
  461. $this->update = false;
  462. $this->emit('setEdit', false);
  463. } catch (\Exception $ex) {
  464. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  465. }
  466. }
  467. public function cancel()
  468. {
  469. $this->add = false;
  470. $this->update = false;
  471. $this->emit('setEdit', false);
  472. $this->resetFields();
  473. }
  474. public function delete($id)
  475. {
  476. try{
  477. \App\Models\Record::find($id)->delete();
  478. session()->flash('success',"Movimento eliminato");
  479. }catch(\Exception $e){
  480. session()->flash('error','Errore (' . $e->getMessage() . ')');
  481. }
  482. }
  483. public function multipleDelete()
  484. {
  485. try{
  486. foreach($this->multipleIds as $id)
  487. {
  488. \App\Models\Record::find($id)->delete();
  489. }
  490. }catch(\Exception $e){
  491. session()->flash('error','Errore (' . $e->getMessage() . ')');
  492. }
  493. $this->multipleAction = '';
  494. }
  495. /*
  496. public function createReceipt()
  497. {
  498. $receipt = \App\Models\Receipt::where('record_id', $this->dataId)->first();
  499. if ($receipt != null)
  500. {
  501. $receipt->update([
  502. 'member_id' => $this->member_id,
  503. 'supplier_id' => $this->supplier_id,
  504. 'causal_id' => $this->causal_id,
  505. 'payment_method_id' => $this->payment_method_id,
  506. 'date' => date("Y-m-d", strtotime($this->date)),
  507. 'month' => $this->month,
  508. 'year' => $this->year,
  509. 'type' => $this->type,
  510. 'amount' => $this->currencyToDouble($this->amount),
  511. 'commercial' => $this->commercial,
  512. 'status' => 1,
  513. ]);
  514. }
  515. else
  516. {
  517. $number = 1;
  518. $exist = \App\Models\Receipt::where('year', $this->year)->orderBy('number', 'DESC')->first();
  519. if ($exist != null)
  520. $number = $exist->number + 1;
  521. $receipt = \App\Models\Receipt::create([
  522. 'record_id' => $this->dataId,
  523. 'member_id' => $this->member_id,
  524. 'supplier_id' => $this->supplier_id,
  525. 'causal_id' => $this->causal_id,
  526. 'payment_method_id' => $this->payment_method_id,
  527. 'number' => $number,
  528. 'date' => $this->date,
  529. 'month' => $this->month,
  530. 'year' => $this->year,
  531. 'type' => $this->type,
  532. 'amount' => $this->currencyToDouble($this->amount),
  533. 'commercial' => $this->commercial,
  534. 'status' => 1,
  535. ]);
  536. }
  537. }
  538. public function removeReceipt()
  539. {
  540. $receipt = \App\Models\Receipt::findOrFail($this->currentReceip->id);
  541. $receipt->status = 99;
  542. $receipt->save();
  543. $this->currentReceip = $receipt;
  544. }
  545. */
  546. function currencyToDouble($val)
  547. {
  548. $x = str_replace("€", "", $val);
  549. $x = str_replace(".", "", $x);
  550. $x = str_replace(",", ".", $x);
  551. return floatval(trim($x));
  552. }
  553. public function addRow()
  554. {
  555. $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'note' => '', 'commercial' => 0);
  556. $this->emit('load-select');
  557. }
  558. public function delRow($idx)
  559. {
  560. unset($this->rows[$idx]);
  561. // $this->emit('load-select');
  562. }
  563. public function addPeriod($idx)
  564. {
  565. $x = sizeof($this->rows[$idx]['when']) - 1;
  566. $newDate = \Carbon\Carbon::create($this->rows[$idx]['when'][$x]["year"] . "-" . $this->rows[$idx]['when'][$x]["month"] . '-01')->addMonth();
  567. $this->rows[$idx]['when'][] = array('month' => $newDate->format("n"), 'year' => $newDate->format("Y"), 'period' => '');
  568. //$this->rows[$idx]['when'][] = array('month' => date("n"), 'year' => date("Y"), 'period' => '');
  569. }
  570. public function delPeriod($idx, $xxx)
  571. {
  572. array_splice($this->rows[$idx]['when'], $xxx, 1);
  573. // $this->emit('load-select');
  574. }
  575. public function getTotal()
  576. {
  577. $total = 0.00;
  578. foreach($this->rows as $r)
  579. {
  580. $total += $this->currencyToDouble($r["amount"] != null ? $r["amount"] : 0);
  581. }
  582. return formatPrice($total);
  583. // $this->emit('load-select');
  584. }
  585. public function setCausal($id, $idx)
  586. {
  587. $this->rows[$idx]["causal_id"] = $id;
  588. }
  589. public function multiPeriod()
  590. {
  591. $this->multiP = true;
  592. }
  593. public function multiPeriodCreate($idx)
  594. {
  595. $period = \Carbon\CarbonPeriod::create($this->multiYearFrom . '-' . $this->multiMonthFrom . '-01', '1 month', $this->multiYearTo . '-' . $this->multiMonthTo . '-01');
  596. $this->rows[$idx]['when'] = [];
  597. foreach ($period as $dt) {
  598. if (!in_array(array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => ''), $this->rows[$idx]['when']))
  599. $this->rows[$idx]['when'][] = array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => '');
  600. }
  601. $this->multiP = false;
  602. }
  603. public function multiPeriodCancel()
  604. {
  605. $this->multiP = false;
  606. }
  607. public function createMember()
  608. {
  609. $this->newMemberFiscalCodeExist = false;
  610. $this->validate([
  611. // 'newMemberFiscalCode'=>'required|max:16',
  612. 'newMemberFirstName'=>'required',
  613. 'newMemberLastName'=>'required',
  614. //'newMemberEmail'=>'required',
  615. ]);
  616. // Check fiscal code exist
  617. $exist = false;
  618. if ($this->newMemberFiscalCode != '')
  619. {
  620. $check = \App\Models\Member::where('fiscal_code', $this->newMemberFiscalCode)
  621. ->where(function($query) {
  622. $query->where('is_deleted', false)
  623. ->orWhereNull('is_deleted');
  624. })->get();
  625. $exist = $check->count() > 0;
  626. }
  627. if (!$exist)
  628. {
  629. $member = \App\Models\Member::create([
  630. 'first_name' => strtoupper($this->newMemberFirstName),
  631. 'last_name' => strtoupper($this->newMemberLastName),
  632. 'email' => strtoupper($this->newMemberEmail),
  633. 'to_complete' => $this->newMemberToComplete,
  634. 'fiscal_code' => $this->newMemberFiscalCode,
  635. 'status' => true
  636. ]);
  637. $this->member_id = $member->id;
  638. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])
  639. ->where(function($query) {
  640. $query->where('is_archived', false)
  641. ->orWhereNull('is_archived');
  642. })
  643. ->where(function($query) {
  644. $query->where('is_deleted', false)
  645. ->orWhereNull('is_deleted');
  646. })->get();
  647. // $this->emit('reloadMembers');
  648. $this->emit('refresh');
  649. $this->newMemberFirstName = '';
  650. $this->newMemberLastName = '';
  651. $this->newMemberEmail = '';
  652. $this->newMemberToComplete = false;
  653. $this->newMemberFiscalCode = '';
  654. $this->emit('saved');
  655. //$this->validate();
  656. $this->selectId++;
  657. }
  658. else
  659. {
  660. $this->newMemberFiscalCodeExist = true;
  661. }
  662. }
  663. public function updatedSupplierId() {
  664. if (!$this->supplier_id) {
  665. $this->supplier_id = null;
  666. }
  667. }
  668. public function updatedMemberId() {
  669. if ($this->member_id > 0)
  670. {
  671. $member = \App\Models\Member::findOrFail($this->member_id);
  672. if (!$member->isAdult())
  673. {
  674. if($member->father_name != '')
  675. $this->parent = $member->father_name . " (C.F. " . $member->father_fiscal_code . ")";
  676. else if($member->mother_name != '')
  677. $this->parent = $member->mother_name . " (C.F. " . $member->mother_fiscal_code . ")";
  678. else
  679. $this->parent = '';
  680. }
  681. $this->newMemberFirstName = '';
  682. $this->newMemberLastName = '';
  683. $this->newMemberEmail = '';
  684. $this->newMemberToComplete = false;
  685. $this->newMemberFiscalCode = '';
  686. $this->newMemberFiscalCodeExist = false;
  687. }
  688. $this->emit('refresh');
  689. }
  690. }