RecordIN.php 48 KB

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