Member.php 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Livewire\Attributes\Url;
  5. use Livewire\WithFileUploads;
  6. use Livewire\WithPagination;
  7. use DateTime;
  8. class Member extends Component
  9. {
  10. use WithPagination;
  11. protected $paginationTheme = 'bootstrap';
  12. protected $listeners = ['storeCategoryWithID' => 'storeCategoryWithID', 'setCourse' => 'setCourse', 'hideMsg' => 'hideMsg'];
  13. use WithFileUploads;
  14. public $sortField ='id';
  15. public $sortAsc = false;
  16. public $groupMsg = '';
  17. public $type = 'dati';
  18. public $from = '';
  19. public $selectedCourseMember = 0;
  20. protected $messages = [
  21. 'birth_nation_id.required' => 'birth_nation_id',
  22. 'birth_province_id.required' => 'birth_province_id',
  23. 'birth_city_id.required' => 'birth_city_id',
  24. 'nation_id.required' => 'nation_id',
  25. 'province_id.required' => 'province_id',
  26. 'city_id.required' => 'city_id',
  27. 'gender' => 'gender'
  28. ];
  29. public function change($type)
  30. {
  31. $this->type = $type;
  32. }
  33. public function sortBy($field)
  34. {
  35. if($this->sortField === $field)
  36. {
  37. $this->sortAsc = ! $this->sortAsc;
  38. } else {
  39. $this->sortAsc = true;
  40. }
  41. $this->sortField = $field;
  42. }
  43. public $records, $first_name, $last_name, $image, $image_old, $status, $birth_place, $birth_city_id, $birth_province_id, $birth_nation_id, $birth_date, $gender, $no_send_mail, $exclude_from_records, $fiscal_code, $address, $zip_code, $nation_id, $province_id, $city_id, $phone, $phone2, $phone3, $email, $enabled, $dataId, $update = false, $add = false;
  44. public $isSaving = false;
  45. public $importoBorsellino, $father_name, $mother_name, $father_email, $mother_email, $father_phone, $mother_phone, $father_fiscal_code, $mother_fiscal_code, $father_doc_number, $father_doc_type, $mother_doc_number, $mother_doc_type;
  46. public $document_type, $document_number, $document_from, $document_expire_date, $document_files, $document_files_old;
  47. public $selectId = 0;
  48. public $causalId = 0;
  49. public $error_fc = false;
  50. public $under18 = false;
  51. public $money = 0;
  52. public $refreshAfter = 0;
  53. public $checkedAll = false;
  54. public $multipleIds = [];
  55. public $multipleAction = '';
  56. public $isItaly = true;
  57. public $isBirthItaly = true;
  58. public $showDetail = false;
  59. public $currentMember;
  60. public $currentStatus;
  61. public $age = '';
  62. public $disciplines = array();
  63. public $cards = array();
  64. public $categories = array();
  65. public $courses = array();
  66. public $course_names = array();
  67. public $course_levels = array();
  68. public $course_types = array();
  69. public $course_frequencies = array();
  70. public $course_subscriptions = array();
  71. public $createSubscription = false;
  72. public $payMonths = array();
  73. public $suspendedMonths = array();
  74. public $showCourse = array();
  75. public $searchTxt;
  76. public $search;
  77. public $showReset = false;
  78. public $advanced = false;
  79. public $birthNations = array();
  80. public $birthProvinces = array();
  81. public $birthCities = array();
  82. public $active;
  83. // Card data
  84. public $member_cards = array(), $card_card_id, $card_number, $card_date, $card_accept_date, $card_status, $card_discipline1_id, $card_discipline2_id, $card_discipline3_id, $addCard, $updateCard, $cardDataId;
  85. // Categories data
  86. public $member_categories = array(), $category_category_id;
  87. // Courses data
  88. public $member_courses = array(), $course_course_id, $course_date_from, $course_date_to, $course_when = array(), $addCourse, $updateCourse, $courseDataId, $course_course_subscription_id, $course_status, $course_note, $course_price, $course_subscription_price, $course_subscribed, $course_months = array(), $course_exist;
  89. public $course_name, $course_level_id, $course_type_id, $course_frequency_id;
  90. // Certificates data
  91. public $member_certificates = array(), $certificate_type, $certificate_filename_old, $certificate_filename, $certificate_expire_date, $certificate_status, $addCertificate, $updateCertificate, $certificateDataId;
  92. public $filterCard = [];
  93. public $filterCategory = [];
  94. public $filterCertNormal = 0;
  95. public $filterCertAgonistic = 0;
  96. public $filterCertScaduto = 0;
  97. public $filterCertInScadenza = 0;
  98. protected $rules = [
  99. 'first_name' => 'required',
  100. 'last_name' => 'required',
  101. 'email' => 'required',
  102. 'phone' => 'required',
  103. 'birth_date' => 'before_or_equal:today'
  104. ];
  105. public function resetFields(){
  106. $this->dataId = -1;
  107. $this->first_name = '';
  108. $this->last_name = '';
  109. $this->status = '';
  110. $this->birth_city_id = null;
  111. $this->birth_province_id = null;
  112. $this->birth_nation_id = null;
  113. $this->birth_date = null;
  114. $this->birth_place = '';
  115. $this->importoBorsellino = 0;
  116. $this->father_name = '';
  117. $this->mother_name = '';
  118. $this->father_email = '';
  119. $this->mother_email = '';
  120. $this->father_phone = '';
  121. $this->mother_phone = '';
  122. $this->father_fiscal_code = '';
  123. $this->mother_fiscal_code = '';
  124. $this->father_doc_number = '';
  125. $this->mother_doc_number = '';
  126. $this->father_doc_type = '';
  127. $this->mother_doc_type = '';
  128. $this->gender = null;
  129. $this->no_send_mail = false;
  130. $this->exclude_from_records = false;
  131. $this->fiscal_code = '';
  132. $this->address = '';
  133. $this->zip_code = '';
  134. $this->document_type = '';
  135. $this->document_number = '';
  136. $this->document_from = '';
  137. $this->document_expire_date = null;
  138. $this->document_files = array();
  139. //$this->nation_id = null;
  140. //$this->province_id = null;
  141. $this->nation_id = null;
  142. $this->province_id = null;
  143. $this->city_id = null;
  144. $this->phone = '';
  145. $this->phone2 = '';
  146. $this->phone3 = '';
  147. $this->email = '';
  148. $this->enabled = true;
  149. $this->under18 = false;
  150. $this->money = 0;
  151. $this->image = null;
  152. $this->age = '';
  153. $this->error_fc = false;
  154. $this->isSaving = false;
  155. $this->emit('load-data-table');
  156. }
  157. public function resetCardFields(){
  158. $this->card_card_id = null;
  159. $this->card_number = '';
  160. $this->card_date = null;
  161. $this->card_accept_date = null;
  162. $this->card_status = 0;
  163. $this->card_discipline1_id = null;
  164. $this->card_discipline2_id = null;
  165. $this->card_discipline3_id = null;
  166. }
  167. public function resetCertificateFields(){
  168. $this->certificate_type = 'N';
  169. $this->certificate_filename = '';
  170. $this->certificate_filename_old = '';
  171. $this->certificate_expire_date = null;
  172. $this->certificate_status = 0;
  173. }
  174. public function resetCourseFields(){
  175. $this->course_course_id = null;
  176. $this->course_when = array();
  177. $this->course_when[] = array('day' => array(), 'from' => '', 'to' => '');
  178. $this->course_date_from = null;
  179. $this->course_date_to = null;
  180. $this->course_course_subscription_id = null;
  181. $this->course_status = 0;
  182. $this->course_price = 0;
  183. $this->course_subscription_price = 0;
  184. $this->course_exist = false;
  185. $this->course_subscribed = false;
  186. $this->course_months = array();
  187. $this->course_note = '';
  188. $this->course_name = '';
  189. $this->course_level_id = '';
  190. $this->course_type_id = '';
  191. $this->course_frequency_id = '';
  192. }
  193. public function executeMultipleAction(){
  194. if ($this->multipleAction == 'delete')
  195. $this->multipleDelete();
  196. }
  197. public function updatedImage()
  198. {
  199. $this->validate([
  200. 'image' => 'image|max:1024',
  201. ]);
  202. $this->image_old = '';
  203. }
  204. public function updatedCourseName()
  205. {
  206. $this->course_course_id = null;
  207. $this->course_level_id = '';
  208. $this->course_type_id = '';
  209. $this->course_frequency_id = '';
  210. $levels_ids = [];
  211. if ($this->course_name != '')
  212. {
  213. list($n, $y) = explode("(", $this->course_name);
  214. $y = trim(str_replace(")", "", $y));
  215. $all = \App\Models\Course::where('name', 'like', '%' . trim($n) . "%")->where('year', $y)->where('enabled', true)->get();
  216. foreach($all as $a)
  217. {
  218. $levels_ids[] = $a->course_level_id;
  219. }
  220. }
  221. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->whereIn('id', $levels_ids)->get();
  222. $this->course_types = [];
  223. $this->course_frequencies = [];
  224. }
  225. public function updatedCourseLevelId()
  226. {
  227. $this->course_course_id = null;
  228. $this->course_type_id = '';
  229. $this->course_frequency_id = '';
  230. $types_ids = [];
  231. if ($this->course_level_id != '')
  232. {
  233. list($n, $y) = explode("(", $this->course_name);
  234. $y = trim(str_replace(")", "", $y));
  235. $all = \App\Models\Course::where('name', 'like', '%' . trim($n) . "%")->where('year', $y)->where('enabled', true)->where('course_level_id', $this->course_level_id)->get();
  236. foreach($all as $a)
  237. {
  238. $types_ids[] = $a->course_type_id;
  239. }
  240. }
  241. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->whereIn('id', $types_ids)->get();
  242. $this->course_frequencies = [];
  243. }
  244. public function updatedCourseTypeId()
  245. {
  246. $this->course_course_id = null;
  247. $this->course_frequency_id = '';
  248. $frequencies_ids = [];
  249. if ($this->course_type_id != '')
  250. {
  251. list($n, $y) = explode("(", $this->course_name);
  252. $y = trim(str_replace(")", "", $y));
  253. $all = \App\Models\Course::where('name', 'like', '%' . trim($n) . "%")->where('year', $y)->where('enabled', true)->where('course_level_id', $this->course_level_id)->where('course_type_id', $this->course_type_id)->get();
  254. foreach($all as $a)
  255. {
  256. $frequencies_ids[] = $a->course_frequency_id;
  257. }
  258. }
  259. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->whereIn('id', $frequencies_ids)->get();
  260. }
  261. public function updatedCourseFrequencyId()
  262. {
  263. $this->course_course_id = null;
  264. if ($this->course_frequency_id != '')
  265. {
  266. list($n, $y) = explode("(", $this->course_name);
  267. $y = trim(str_replace(")", "", $y));
  268. $this->course_course_id = \App\Models\Course::where('name', 'like', '%' . trim($n) . "%")->where('year', $y)->where('course_level_id', $this->course_level_id)->where('course_type_id', $this->course_type_id)->where('course_frequency_id', $this->course_frequency_id)->first()->id;
  269. if ($this->course_course_id > 0)
  270. {
  271. $c = \App\Models\Course::findOrFail($this->course_course_id);
  272. $this->course_price = formatPrice($c->price);
  273. $this->course_subscription_price = formatPrice($c->subscription_price);
  274. // Controllo se sono già iscritto la corso
  275. $this->course_exist = \App\Models\MemberCourse::where('course_id', $this->course_course_id)->where('member_id', $this->dataId)->count() > 0;
  276. }
  277. else
  278. {
  279. $this->course_price = 0;
  280. $this->course_subscription_price = 0;
  281. $this->course_exist = false;
  282. }
  283. }
  284. else
  285. {
  286. $this->course_price = 0;
  287. $this->course_subscription_price = 0;
  288. $this->course_exist = false;
  289. }
  290. }
  291. public $documents = [];
  292. public function removeDocument($idx)
  293. {
  294. unset($this->document_files[$idx]);
  295. }
  296. public function updatedDocuments()
  297. {
  298. foreach ($this->documents as $document)
  299. {
  300. $name = $document->getClientOriginalName(); // . '.'.$allegato->extension();
  301. $document->storeAs('public', $name);
  302. $this->document_files[] = $name;
  303. }
  304. $this->documents = [];
  305. }
  306. public function resetCategoryFields(){
  307. $this->category_category_id = null;
  308. }
  309. public function getCategories($records, $indentation)
  310. {
  311. foreach($records as $record)
  312. {
  313. // $this->categories[] = array('id' => $record->id, 'name' => str_repeat(" / ", $indentation) . $record->name);
  314. $this->categories[] = array('id' => $record->id, 'name' => $record->getTree());
  315. if(count($record->childs))
  316. $this->getCategories($record->childs, $indentation + 1);
  317. }
  318. }
  319. public function getCourses($records, $indentation)
  320. {
  321. foreach($records as $record)
  322. {
  323. // $this->categories[] = array('id' => $record->id, 'name' => str_repeat(" / ", $indentation) . $record->name);
  324. $this->courses[] = array('id' => $record->id, 'name' => $record->getTree());
  325. if(count($record->childs))
  326. $this->getCourses($record->childs, $indentation + 1);
  327. }
  328. }
  329. public function updatedBirthDate()
  330. {
  331. $this->validateOnly('birth_date');
  332. $date1 = new DateTime($this->birth_date);
  333. $date2 = new DateTime("now");
  334. $interval = $date1->diff($date2);
  335. $this->age = $interval->y . " anni";
  336. $this->under18 = $interval->y < 18;
  337. }
  338. public function mount()
  339. {
  340. if (isset($_GET["new"]))
  341. {
  342. $this->refreshAfter = 1;
  343. $this->add();
  344. }
  345. if (isset($_GET["from"]))
  346. {
  347. $this->from = $_GET["from"];
  348. }
  349. $this->cards = \App\Models\Card::select('id', 'name')->get();
  350. $this->disciplines = \App\Models\Discipline::select('id', 'name')->get();
  351. $this->categories = array();
  352. $this->getCategories(\App\Models\Category::select('id', 'name')->where('parent_id', null)->get(), 0);
  353. $this->courses = \App\Models\Course::select('id', 'name')->where('enabled', null)->get();
  354. $this->course_subscriptions = \App\Models\CourseSubscription::select('*')->where('enabled', true)->get();
  355. // $this->getCourses(\App\Models\Course::select('id', 'name')->where('parent_id', null)->get(), 0);
  356. /*$this->nations = \App\Models\Nation::select('id', 'name')->orderBy('name')->get();
  357. $this->provinces = \App\Models\Province::where('nation_id', 1)->orderBy('name')->get();
  358. $this->cities = \App\Models\City::where('province_id', 178)->orderBy('name')->orderBy('name')->get();*/
  359. $c = \App\Models\Causal::where('type', 'IN')->where('money', true)->first();
  360. if ($c)
  361. $this->causalId = $c->id;
  362. if (isset($_GET["member_detail"]))
  363. {
  364. $this->showDetailF($_GET["member_detail"]);
  365. $this->refreshAfter = 1;
  366. }
  367. /*$this->birthNations = \App\Models\Nation::select('id', 'name')->orderBy('name')->get();
  368. $this->birthProvinces = \App\Models\Province::where('nation_id', 1)->orderBy('name')->get();
  369. $this->birthCities = \App\Models\City::where('province_id', 178)->orderBy('name')->orderBy('name')->get();*/
  370. }
  371. public function updated()
  372. {
  373. if ($this->isSaving)
  374. {
  375. $zzz = [];
  376. if ($this->birth_nation_id == null)
  377. $zzz[] = 'birth_nation_id='.$this->birth_nation_id;
  378. if ($this->isBirthItaly)
  379. {
  380. if ($this->birth_province_id == null)
  381. $zzz[] = 'birth_province_id='.$this->birth_province_id;
  382. if ($this->birth_city_id == null)
  383. $zzz[] = 'birth_city_id='.$this->birth_city_id;
  384. }
  385. if ($this->nation_id == null)
  386. $zzz[] = 'nation_id='.$this->nation_id;
  387. if ($this->isItaly)
  388. {
  389. if ($this->province_id == null)
  390. $zzz[] = 'province_id='.$this->province_id;
  391. if ($this->city_id == null)
  392. $zzz[] = 'city_id='.$this->city_id;
  393. }
  394. $this->emit('setErrorMsg', $zzz);
  395. }
  396. // $this->emit('load-select');
  397. }
  398. public function hydrate()
  399. {
  400. if ($this->isSaving)
  401. {
  402. $zzz = [];
  403. if ($this->birth_nation_id == null)
  404. $zzz[] = 'birth_nation_id='.$this->birth_nation_id;
  405. if ($this->isBirthItaly)
  406. {
  407. if ($this->birth_province_id == null)
  408. $zzz[] = 'birth_province_id='.$this->birth_province_id;
  409. if ($this->birth_city_id == null)
  410. $zzz[] = 'birth_city_id='.$this->birth_city_id;
  411. }
  412. if ($this->nation_id == null)
  413. $zzz[] = 'nation_id='.$this->nation_id;
  414. if ($this->isItaly)
  415. {
  416. if ($this->province_id == null)
  417. $zzz[] = 'province_id='.$this->province_id;
  418. if ($this->city_id == null)
  419. $zzz[] = 'city_id='.$this->city_id;
  420. }
  421. $this->emit('setErrorMsg', $zzz);
  422. }
  423. $this->emit('load-select');
  424. // $this->emit('destroy-data-table');
  425. }
  426. public function checkIsItaly()
  427. {
  428. $n = \App\Models\Nation::findOrFail($this->nation_id);
  429. $this->isItaly = $n->is_italy;
  430. }
  431. public function checkIsBirthItaly()
  432. {
  433. if ($this->birth_nation_id > 0)
  434. {
  435. $n = \App\Models\Nation::findOrFail($this->birth_nation_id);
  436. $this->isBirthItaly = $n->is_italy;
  437. }
  438. else
  439. $this->isBirthItaly = false;
  440. }
  441. /*public function loadProvinces()
  442. {
  443. $n = \App\Models\Nation::findOrFail($this->nation_id);
  444. $this->isItaly = $n->is_italy;
  445. $this->provinces = \App\Models\Province::where('nation_id', $this->nation_id)->orderBy('name')->get();
  446. $this->cities = array();
  447. $this->selectId++;
  448. }
  449. public function loadCities()
  450. {
  451. $this->cities = \App\Models\City::where('province_id', $this->province_id)->orderBy('name')->orderBy('name')->get();
  452. $this->selectId++;
  453. }
  454. public function loadBirthProvinces()
  455. {
  456. $n = \App\Models\Nation::findOrFail($this->birth_nation_id);
  457. $this->isBirthItaly = $n->is_italy;
  458. $this->birthProvinces = \App\Models\Province::where('nation_id', $this->birth_nation_id)->orderBy('name')->get();
  459. $this->birthCities = array();
  460. $this->selectId++;
  461. }
  462. public function loadBirthCities()
  463. {
  464. $this->birthCities = \App\Models\City::where('province_id', $this->birth_province_id)->get();
  465. $this->selectId++;
  466. }
  467. */
  468. public function search()
  469. {
  470. if ($this->searchTxt != '')
  471. {
  472. $this->search = $this->searchTxt;
  473. $this->showReset = true;
  474. }
  475. }
  476. public function resetSearch()
  477. {
  478. $this->showReset = false;
  479. $this->searchTxt = '';
  480. $this->search = $this->searchTxt;
  481. }
  482. public function advancedSearch()
  483. {
  484. $this->advanced = true;
  485. }
  486. public function advancedSearchCancel()
  487. {
  488. $this->filterCard = [];
  489. $this->filterCategory = [];
  490. $this->filterCertNormal = 0;
  491. $this->filterCertAgonistic = 0;
  492. $this->filterCertScaduto = 0;
  493. $this->filterCertInScadenza = 0;
  494. $this->advanced = false;
  495. }
  496. public function render()
  497. {
  498. $this->course_names = [];
  499. $allC = \App\Models\Course::orderBy('name')->get();
  500. foreach($allC as $c)
  501. {
  502. $cN = $c->name . " (" . $c->year . ")";
  503. if (!in_array($cN, $this->course_names))
  504. $this->course_names[] = $cN;
  505. }
  506. //$this->course_names = \App\Models\Course::orderBy('name')->groupBy('name')->pluck('name');
  507. $datas = [];
  508. // $this->emit('destroy-data-table');
  509. if (false)
  510. {
  511. if (!$this->advanced)
  512. {
  513. /*if ($this->search != '')
  514. $datas = \App\Models\Member::select('members.*') // , \DB::raw('SUM(records.id) As total'))
  515. ->where('first_name', 'LIKE', '%' . $this->search . '%')
  516. ->orWhere('last_name', 'LIKE', '%' . $this->search . '%')
  517. ->orWhere('email', 'LIKE', '%' . $this->search . '%');
  518. $this->records = \App\Models\Member::where('first_name', 'LIKE', '%' . $this->search . '%')->orWhere('last_name', 'LIKE', '%' . $this->search . '%')->orWhere('email', 'LIKE', '%' . $this->search . '%')->get();
  519. else*/
  520. $this->records = \App\Models\Member::select('id', 'first_name', 'last_name', 'phone')->get();
  521. //$datas = \App\Models\Member::select('members.*');
  522. }
  523. else
  524. {
  525. //$this->records = \App\Models\Member::where('id', '>', 0);
  526. $datas = \App\Models\Member::select('members.*')->where('id', '>', 0);
  527. if (sizeof($this->filterCard) > 0)
  528. {
  529. $card_ids = \App\Models\MemberCard::whereIn('card_id', $this->filterCard)->pluck('member_id');
  530. //$this->records->whereIn('id', $card_ids);
  531. $datas = $datas->whereIn('id', $card_ids);
  532. }
  533. if (sizeof($this->filterCategory) > 0)
  534. {
  535. $cats_ids = \App\Models\MemberCategory::whereIn('category_id', $this->filterCategory)->pluck('member_id');
  536. //$this->records->whereIn('id', $cats_ids);
  537. $datas = $datas->whereIn('id', $cats_ids);
  538. }
  539. $certs = [];
  540. if ($this->filterCertNormal > 0)
  541. {
  542. $normal = \App\Models\MemberCertificate::where('type', 'N')->pluck('member_id');
  543. //$this->records->whereIn('id', $normal);
  544. $datas = $datas->whereIn('id', $normal);;
  545. }
  546. if ($this->filterCertAgonistic > 0)
  547. {
  548. $agonistic = \App\Models\MemberCertificate::where('type', 'A')->pluck('member_id');
  549. //$this->records->whereIn('id', $agonistic);
  550. $datas = $datas->whereIn('id', $agonistic);
  551. }
  552. if ($this->filterCertScaduto > 0)
  553. {
  554. $scaduto = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))->pluck('member_id');
  555. //$this->records->whereIn('id', $scaduto);
  556. $datas = $datas->whereIn('id', $scaduto);
  557. }
  558. if ($this->filterCertInScadenza > 0)
  559. {
  560. $scaduto = \App\Models\MemberCertificate::whereBetween('expire_date', [date("Y-m-d"), date("Y-m-d", strtotime("+1 month"))])->pluck('member_id');
  561. //$this->records->whereIn('id', $scaduto);
  562. $datas = $datas->whereIn('id', $scaduto);
  563. }
  564. if (sizeof($certs) > 0)
  565. {
  566. $datas = $datas->whereIn('id', $certs);
  567. //$this->records->whereIn('id', $certs);
  568. }
  569. $this->records = $datas->get();
  570. }
  571. foreach($this->records as $r)
  572. {
  573. $r->age = $r->getAge();
  574. $active = $r->isActive();
  575. $r->status = $active["status"];
  576. $r->date = $active["date"];
  577. $r->certificate = $r->hasCertificate()["date"];
  578. $r->state = $r->getStatus()["status"];
  579. }
  580. /*
  581. if ($this->sortAsc)
  582. $this->records = $this->records->sortBy($this->sortField);
  583. else
  584. $this->records = $this->records->sortByDesc($this->sortField);
  585. */
  586. //$datas = $datas->get(); // ->orderBy($this->sortField, $this->sortAsc ? 'ASC' : 'DESC')->paginate(10);
  587. //$this->records = $this->records->get();
  588. $this->emit('load-data-table');
  589. }
  590. $this->loadMemberCards();
  591. $this->loadMemberCourses();
  592. $this->loadMemberCategories();
  593. $this->loadMemberCertificates();
  594. return view('livewire.member', ['datas' => $datas]);
  595. }
  596. public function updatedCourseCourseId()
  597. {
  598. if ($this->course_course_id > 0)
  599. {
  600. $c = \App\Models\Course::findOrFail($this->course_course_id);
  601. $this->course_price = formatPrice($c->price);
  602. $this->course_subscription_price = formatPrice($c->subscription_price);
  603. // Controllo se sono già iscritto la corso
  604. $this->course_exist = \App\Models\MemberCourse::where('course_id', $this->course_course_id)->where('member_id', $this->dataId)->count() > 0;
  605. }
  606. else
  607. {
  608. $this->course_price = 0;
  609. $this->course_subscription_price = 0;
  610. $this->course_exist = false;
  611. }
  612. }
  613. public function loadMemberCards()
  614. {
  615. $this->member_cards = \App\Models\MemberCard::where('member_id', $this->dataId)->get();
  616. // return view('livewire.member');
  617. }
  618. public function loadMemberCourses()
  619. {
  620. $this->member_courses = \App\Models\MemberCourse::where('member_id', $this->dataId)->get();
  621. // return view('livewire.member');
  622. }
  623. public function loadMemberCategories()
  624. {
  625. $this->member_categories = \App\Models\MemberCategory::where('member_id', $this->dataId)->get();
  626. // return view('livewire.member');
  627. }
  628. public function loadMemberCertificates()
  629. {
  630. $this->member_certificates = \App\Models\MemberCertificate::where('member_id', $this->dataId)->orderBy('expire_date', 'DESC')->get();
  631. // return view('livewire.member');
  632. }
  633. public function showDetailF($id)
  634. {
  635. if (!isset($_GET["from"]) && $this->from == '')
  636. $this->from = 'members';
  637. $this->currentMember = \App\Models\Member::findOrFail($id);
  638. $this->currentStatus = $this->currentMember->getStatus();
  639. $this->showDetail = true;
  640. }
  641. public function add()
  642. {
  643. //$this->from = 'members';
  644. $this->emit('load-select');
  645. $this->emit('hide-search');
  646. $this->showDetail = false;
  647. $this->resetFields();
  648. $this->add = true;
  649. $this->update = false;
  650. $this->emit('setEdit', true);
  651. $this->emit('setEditCorso', false);
  652. }
  653. public function store($close)
  654. {
  655. $this->isSaving = true;
  656. $this->emit('load-select');
  657. $this->emit('goToTop');
  658. $rules = [
  659. 'first_name' => 'required',
  660. 'last_name' => 'required',
  661. 'email' => 'required',
  662. 'phone' => 'required',
  663. 'birth_nation_id' => 'required',
  664. //'birth_place' => 'required',
  665. 'address' => 'required',
  666. 'zip_code' => 'required',
  667. 'nation_id' => 'required',
  668. 'fiscal_code' => 'required',
  669. 'birth_date' => 'before_or_equal:today',
  670. 'gender' => 'required'
  671. ];
  672. if ($this->isBirthItaly)
  673. {
  674. $rules['birth_province_id'] = 'required';
  675. $rules['birth_city_id'] = 'required';
  676. }
  677. else
  678. {
  679. $rules['birth_place'] = 'required';
  680. }
  681. if ($this->isItaly)
  682. {
  683. $rules['province_id'] = 'required';
  684. $rules['city_id'] = 'required';
  685. }
  686. $zzz = [];
  687. if ($this->birth_nation_id == null)
  688. $zzz[] = 'birth_nation_id='.$this->birth_nation_id;
  689. if ($this->isBirthItaly)
  690. {
  691. if ($this->birth_province_id == null)
  692. $zzz[] = 'birth_province_id='.$this->birth_province_id;
  693. if ($this->birth_city_id == null)
  694. $zzz[] = 'birth_city_id='.$this->birth_city_id;
  695. }
  696. if ($this->nation_id == null)
  697. $zzz[] = 'nation_id='.$this->nation_id;
  698. if ($this->isItaly)
  699. {
  700. if ($this->province_id == null)
  701. $zzz[] = 'province_id='.$this->province_id;
  702. if ($this->city_id == null)
  703. $zzz[] = 'city_id='.$this->city_id;
  704. }
  705. $this->emit('setErrorMsg', $zzz);
  706. $this->validate($rules);
  707. //$this->emit('setErrors');
  708. try {
  709. $name = '';
  710. if ($this->image)
  711. {
  712. $name = md5($this->image . microtime()).'.'.$this->image->extension();
  713. $this->image->storeAs('public', $name);
  714. }
  715. $docs = implode("|", $this->document_files);
  716. $member = \App\Models\Member::create([
  717. 'first_name' => strtoupper($this->first_name),
  718. 'last_name' => strtoupper($this->last_name),
  719. 'status' => $this->status,
  720. 'birth_city_id' => $this->birth_city_id > 0 ? $this->birth_city_id : null,
  721. 'birth_province_id' => $this->birth_province_id > 0 ? $this->birth_province_id : null,
  722. 'birth_nation_id' => $this->birth_nation_id > 0 ? $this->birth_nation_id : null,
  723. 'birth_date' => $this->birth_date,
  724. 'birth_place' => $this->birth_place,
  725. 'father_name' => $this->father_name,
  726. 'mother_name' => $this->mother_name,
  727. 'father_email' => strtolower($this->father_email),
  728. 'mother_email' => strtolower($this->mother_email),
  729. 'father_phone' => $this->father_phone,
  730. 'mother_phone' => $this->mother_phone,
  731. 'father_fiscal_code' => $this->father_fiscal_code,
  732. 'mother_fiscal_code' => $this->mother_fiscal_code,
  733. 'father_doc_number' => $this->father_doc_number,
  734. 'father_doc_type' => $this->father_doc_type,
  735. 'mother_doc_number' => $this->mother_doc_number,
  736. 'mother_doc_type' => $this->mother_doc_type,
  737. 'document_type' => $this->document_type,
  738. 'document_number' => $this->document_number,
  739. 'document_from' => $this->document_from,
  740. 'document_expire_date' => $this->document_expire_date,
  741. 'document_files' => $docs,
  742. 'gender' => $this->gender,
  743. 'no_send_mail' => $this->no_send_mail,
  744. 'exclude_from_records' => $this->exclude_from_records,
  745. 'fiscal_code' => $this->fiscal_code,
  746. 'address' => $this->address,
  747. 'zip_code' => $this->zip_code,
  748. 'nation_id' => $this->nation_id > 0 ? $this->nation_id : null,
  749. 'province_id' => $this->province_id > 0 ? $this->province_id : null,
  750. 'city_id' => $this->city_id > 0 ? $this->city_id : null,
  751. 'phone' => $this->phone,
  752. 'phone2' => $this->phone2,
  753. 'phone3' => $this->phone3,
  754. 'email' => strtolower($this->email),
  755. 'image' => $name,
  756. 'enabled' => $this->enabled
  757. ]);
  758. session()->flash('success, Tesserato creato');
  759. updateMemberData($member->id);
  760. $this->resetFields();
  761. if ($close)
  762. {
  763. $this->add = false;
  764. }
  765. else
  766. {
  767. $this->edit($member->id);
  768. }
  769. $this->emit('setEdit', false);
  770. } catch (\Exception $ex) {
  771. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  772. }
  773. }
  774. public function duplicate($id){
  775. $member = \App\Models\Member::findOrFail($id);
  776. $newMember = $member->replicate();
  777. $newMember->save();
  778. $this->edit($newMember->id);
  779. }
  780. public function edit($id){
  781. if (!isset($_GET["from"]) && $this->from == '')
  782. $this->from = 'members';
  783. $this->showDetail = false;
  784. $this->emit('setEdit', true);
  785. $this->emit('setEditCorso', false);
  786. $this->emit('hide-search');
  787. try {
  788. $member = \App\Models\Member::findOrFail($id);
  789. if( !$member) {
  790. session()->flash('error','Tesserato non trovato');
  791. } else {
  792. $this->first_name = $member->first_name;
  793. $this->last_name = $member->last_name;
  794. $this->status = $member->status;
  795. $this->birth_city_id = $member->birth_city_id;
  796. $this->birth_province_id = $member->birth_province_id;
  797. $this->birth_nation_id = $member->birth_nation_id;
  798. $this->birth_date = $member->birth_date;
  799. $this->birth_place = $member->birth_place;
  800. $this->father_name = $member->father_name;
  801. $this->mother_name = $member->mother_name;
  802. $this->father_email = strtolower($member->father_email);
  803. $this->mother_email = strtolower($member->mother_email);
  804. $this->father_phone = $member->father_phone;
  805. $this->mother_phone = $member->mother_phone;
  806. $this->father_fiscal_code = $member->father_fiscal_code;
  807. $this->mother_fiscal_code = $member->mother_fiscal_code;
  808. $this->father_doc_number = $member->father_doc_number;
  809. $this->mother_doc_number = $member->mother_doc_number;
  810. $this->father_doc_type = $member->father_doc_type;
  811. $this->mother_doc_type = $member->mother_doc_type;
  812. $this->document_type = $member->document_type;
  813. $this->document_number = $member->document_number;
  814. $this->document_from = $member->document_from;
  815. $this->document_expire_date = $member->document_expire_date;
  816. $this->document_files = explode("|", $member->document_files);
  817. $this->gender = $member->gender;
  818. $this->no_send_mail = $member->no_send_mail;
  819. $this->exclude_from_records = $member->exclude_from_records;
  820. $this->fiscal_code = $member->fiscal_code;
  821. $this->address = $member->address;
  822. $this->zip_code = $member->zip_code;
  823. $this->nation_id = $member->nation_id;
  824. $this->province_id = $member->province_id;
  825. $this->city_id = $member->city_id;
  826. $this->phone = $member->phone;
  827. $this->phone2 = $member->phone2;
  828. $this->phone3 = $member->phone3;
  829. $this->email = strtolower($member->email);
  830. $this->image_old = $member->image;
  831. $this->enabled = $member->enabled;
  832. $this->dataId = $member->id;
  833. $this->active = $member->getStatus();
  834. $this->money = $member->getMoney();
  835. $this->checkIsBirthItaly();
  836. $date1 = new DateTime($this->birth_date);
  837. $date2 = new DateTime("now");
  838. $interval = $date1->diff($date2);
  839. $this->age = $interval->y . " anni";
  840. $this->under18 = $interval->y < 18;
  841. //$this->provinces = \App\Models\Province::where('nation_id', $this->nation_id)->get();
  842. //$this->cities = \App\Models\City::where('province_id', $this->province_id)->get();
  843. //$this->birthProvinces = \App\Models\Province::where('nation_id', $this->birth_nation_id)->get();
  844. //$this->birthCities = \App\Models\City::where('province_id', $this->birth_province_id)->get();
  845. $this->update = true;
  846. $this->add = false;
  847. $this->emit('setIds', $this->nation_id, $this->birth_nation_id);
  848. $this->emit('load-select');
  849. $this->emit('load-provinces', $this->nation_id, 'provinceClass');
  850. $this->emit('load-provinces', $this->birth_nation_id, 'provinceBirthClass');
  851. $this->emit('load-cities', $this->province_id, 'cityClass');
  852. $this->emit('load-cities', $this->birth_province_id, 'cityBirthClass');
  853. }
  854. } catch (\Exception $ex) {
  855. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  856. }
  857. }
  858. public function updateAAA($close)
  859. {
  860. $this->emit('goToTop');
  861. $this->isSaving = true;
  862. $this->emit('load-select');
  863. $rules = [
  864. 'first_name' => 'required',
  865. 'last_name' => 'required',
  866. 'email' => 'required',
  867. 'phone' => 'required',
  868. 'birth_nation_id' => 'required',
  869. //'birth_place' => 'required',
  870. 'address' => 'required',
  871. 'zip_code' => 'required',
  872. 'nation_id' => 'required',
  873. 'fiscal_code' => 'required',
  874. 'birth_date' => 'before_or_equal:today'
  875. ];
  876. if ($this->isBirthItaly)
  877. {
  878. $rules['birth_province_id'] = 'required';
  879. $rules['birth_city_id'] = 'required';
  880. }
  881. else
  882. {
  883. $rules['birth_place'] = 'required';
  884. }
  885. if ($this->isItaly)
  886. {
  887. $rules['province_id'] = 'required';
  888. $rules['city_id'] = 'required';
  889. }
  890. $zzz = [];
  891. if ($this->birth_nation_id == null)
  892. $zzz[] = 'birth_nation_id='.$this->birth_nation_id;
  893. if ($this->isBirthItaly)
  894. {
  895. if ($this->birth_province_id == null)
  896. $zzz[] = 'birth_province_id='.$this->birth_province_id;
  897. if ($this->birth_city_id == null)
  898. $zzz[] = 'birth_city_id='.$this->birth_city_id;
  899. }
  900. if ($this->nation_id == null)
  901. $zzz[] = 'nation_id='.$this->nation_id;
  902. if ($this->isItaly)
  903. {
  904. if ($this->province_id == null)
  905. $zzz[] = 'province_id='.$this->province_id;
  906. if ($this->city_id == null)
  907. $zzz[] = 'city_id='.$this->city_id;
  908. }
  909. $this->emit('setErrorMsg', $zzz);
  910. $this->validate($rules);
  911. try {
  912. $name = '';
  913. if ($this->image)
  914. {
  915. $name = md5($this->image . microtime()).'.'.$this->image->extension();
  916. $this->image->storeAs('public', $name);
  917. }
  918. $docs = implode("|", $this->document_files);
  919. \App\Models\Member::whereId($this->dataId)->update([
  920. 'first_name' => strtoupper($this->first_name),
  921. 'last_name' => strtoupper($this->last_name),
  922. 'status' => $this->status,
  923. 'birth_city_id' => $this->birth_city_id > 0 ? $this->birth_city_id : null,
  924. 'birth_province_id' => $this->birth_province_id > 0 ? $this->birth_province_id : null,
  925. 'birth_nation_id' => $this->birth_nation_id > 0 ? $this->birth_nation_id : null,
  926. 'birth_date' => $this->birth_date,
  927. 'birth_place' => $this->birth_place,
  928. 'father_name' => $this->father_name,
  929. 'mother_name' => $this->mother_name,
  930. 'father_email' => strtolower($this->father_email),
  931. 'mother_email' => strtolower($this->mother_email),
  932. 'father_phone' => $this->father_phone,
  933. 'mother_phone' => $this->mother_phone,
  934. 'father_fiscal_code' => $this->father_fiscal_code,
  935. 'mother_fiscal_code' => $this->mother_fiscal_code,
  936. 'father_doc_number' => $this->father_doc_number,
  937. 'father_doc_type' => $this->father_doc_type,
  938. 'mother_doc_number' => $this->mother_doc_number,
  939. 'mother_doc_type' => $this->mother_doc_type,
  940. 'document_type' => $this->document_type,
  941. 'document_number' => $this->document_number,
  942. 'document_from' => $this->document_from,
  943. 'document_expire_date' => $this->document_expire_date,
  944. 'document_files' => $docs,
  945. 'gender' => $this->gender,
  946. 'no_send_mail' => $this->no_send_mail,
  947. 'exclude_from_records' => $this->exclude_from_records,
  948. 'fiscal_code' => $this->fiscal_code,
  949. 'address' => $this->address,
  950. 'zip_code' => $this->zip_code,
  951. 'nation_id' => $this->nation_id > 0 ? $this->nation_id : null,
  952. 'province_id' => $this->province_id > 0 ? $this->province_id : null,
  953. 'city_id' => $this->city_id > 0 ? $this->city_id : null,
  954. 'image' => $name != '' ? $name : $this->image_old,
  955. 'phone' => $this->phone,
  956. 'phone2' => $this->phone2,
  957. 'phone3' => $this->phone3,
  958. 'email' => strtolower($this->email),
  959. 'enabled' => $this->enabled
  960. ]);
  961. updateMemberData($this->dataId);
  962. session()->flash('success','Tesserato aggiornato');
  963. if ($close)
  964. {
  965. $this->resetFields();
  966. $this->update = false;
  967. }
  968. $this->emit('setEdit', false);
  969. } catch (\Exception $ex) {
  970. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  971. }
  972. }
  973. public function cancel()
  974. {
  975. $this->add = false;
  976. $this->update = false;
  977. $this->showDetail = false;
  978. $this->resetFields();
  979. $this->emit('setEdit', false);
  980. //$this->emit('setEditCorso', false);
  981. $this->emit('reload');
  982. }
  983. public function delete($id)
  984. {
  985. try{
  986. \App\Models\Member::find($id)->delete();
  987. session()->flash('success',"Tesserato eliminato");
  988. $this->emit('reload');
  989. }catch(\Exception $e){
  990. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  991. }
  992. }
  993. public function checkUncheckAll()
  994. {
  995. if (!$this->checkedAll)
  996. {
  997. $this->multipleIds = array();
  998. }
  999. else
  1000. {
  1001. foreach($this->records as $r)
  1002. {
  1003. $this->multipleIds[] = $r->id;
  1004. }
  1005. }
  1006. }
  1007. public function multipleDelete()
  1008. {
  1009. try{
  1010. foreach($this->multipleIds as $id)
  1011. {
  1012. \App\Models\Member::find($id)->delete();
  1013. }
  1014. }catch(\Exception $e){
  1015. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1016. }
  1017. $this->multipleAction = '';
  1018. }
  1019. // Card
  1020. public function addCard()
  1021. {
  1022. $this->resetCardFields();
  1023. $this->addCard = true;
  1024. $this->updateCard = false;
  1025. }
  1026. public function storeCard()
  1027. {
  1028. $this->validate(['card_card_id' => 'required']);
  1029. try {
  1030. // in base alla card selezionata calcolo la scadenza
  1031. $expire_date = null;
  1032. if ($this->card_date != '')
  1033. {
  1034. $y = date("Y", strtotime($this->card_date));
  1035. $card = \App\Models\Card::findOrFail($this->card_card_id);
  1036. if ($card->next_day_expire > 0 && $card->next_month_expire > 0)
  1037. {
  1038. $m = strlen($card->next_month_expire) == 1 ? ('0' . $card->next_month_expire) : $card->next_month_expire;
  1039. if (date("md", strtotime($this->card_date)) > ($m . $card->next_day_expire))
  1040. $y += 1;
  1041. $next_exp = date($y . "-" . $m . "-" . $card->next_day_expire);
  1042. if ($next_exp > $this->card_date)
  1043. {
  1044. $expire_date = $next_exp;
  1045. }
  1046. else
  1047. $expire_date = date($y . "-" . $card->next_month_expire . "-" . $card->next_day_expire, strtotime(' + 1 years'));
  1048. }
  1049. else
  1050. {
  1051. if ($card->one_year_expire)
  1052. {
  1053. $expire_date = date("Y-m-d", strtotime($this->card_date . ' + 1 years'));
  1054. }
  1055. }
  1056. }
  1057. \App\Models\MemberCard::create([
  1058. 'member_id' => $this->dataId,
  1059. 'card_id' => $this->card_card_id,
  1060. 'number' => $this->card_number,
  1061. 'date' => $this->card_date,
  1062. 'accept_date' => $this->card_accept_date != '' ? $this->card_accept_date : $this->card_date,
  1063. 'expire_date' => $expire_date,
  1064. 'status' => $this->card_status,
  1065. 'discipline1_id' => $this->card_discipline1_id,
  1066. 'discipline2_id' => $this->card_discipline2_id,
  1067. 'discipline3_id' => $this->card_discipline3_id,
  1068. ]);
  1069. updateMemberData($this->dataId);
  1070. session()->flash('success, Tesserato creato');
  1071. $this->resetCardFields();
  1072. $this->addCard = false;
  1073. } catch (\Exception $ex) {
  1074. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1075. }
  1076. }
  1077. public function editCard($id){
  1078. try {
  1079. $memberCard = \App\Models\MemberCard::findOrFail($id);
  1080. if( !$memberCard) {
  1081. session()->flash('error','Tesserato non trovato');
  1082. } else {
  1083. $this->card_card_id = $memberCard->card_id;
  1084. $this->card_number = $memberCard->number;
  1085. $this->card_date = $memberCard->date;
  1086. $this->card_accept_date = $memberCard->accept_date;
  1087. $this->card_status = $memberCard->status;
  1088. $this->card_discipline1_id = $memberCard->discipline1_id;
  1089. $this->card_discipline2_id = $memberCard->discipline2_id;
  1090. $this->card_discipline3_id = $memberCard->discipline3_id;
  1091. $this->cardDataId = $memberCard->id;
  1092. $this->updateCard = true;
  1093. $this->addCard = false;
  1094. }
  1095. } catch (\Exception $ex) {
  1096. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1097. }
  1098. }
  1099. public function updateCard()
  1100. {
  1101. //$this->validate();
  1102. $this->validate(['card_card_id' => 'required']);
  1103. try {
  1104. // in base alla card selezionata calcolo la scadenza
  1105. $expire_date = null;
  1106. if ($this->card_date != '')
  1107. {
  1108. $card = \App\Models\Card::findOrFail($this->card_card_id);
  1109. if ($card->next_day_expire > 0 && $card->next_month_expire > 0)
  1110. {
  1111. $y = date("Y", strtotime($this->card_date));
  1112. $m = strlen($card->next_month_expire) == 1 ? ('0' . $card->next_month_expire) : $card->next_month_expire;
  1113. $next_exp = date($y . "-" . $m . "-" . $card->next_day_expire);
  1114. if ($next_exp > $this->card_date)
  1115. {
  1116. $expire_date = $next_exp;
  1117. }
  1118. else
  1119. $expire_date = date($y . "-" . $card->next_month_expire . "-" . $card->next_day_expire, strtotime(' + 1 years'));
  1120. }
  1121. else
  1122. {
  1123. if ($card->one_year_expire)
  1124. {
  1125. $expire_date = date("Y-m-d", strtotime($this->card_date . ' + 1 years'));
  1126. }
  1127. }
  1128. }
  1129. \App\Models\MemberCard::whereId($this->cardDataId)->update([
  1130. 'member_id' => $this->dataId,
  1131. 'card_id' => $this->card_card_id,
  1132. 'number' => $this->card_number,
  1133. 'date' => $this->card_date,
  1134. 'accept_date' => $this->card_accept_date != '' ? $this->card_accept_date : $this->card_date,
  1135. 'expire_date' => $expire_date,
  1136. 'status' => $this->card_status,
  1137. 'discipline1_id' => $this->card_discipline1_id,
  1138. 'discipline2_id' => $this->card_discipline2_id,
  1139. 'discipline3_id' => $this->card_discipline3_id,
  1140. ]);
  1141. updateMemberData($this->dataId);
  1142. session()->flash('success','Tesserato aggiornato');
  1143. $this->resetCardFields();
  1144. $this->updateCard = false;
  1145. } catch (\Exception $ex) {
  1146. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1147. }
  1148. }
  1149. public function cancelCard()
  1150. {
  1151. $this->addCard = false;
  1152. $this->updateCard = false;
  1153. $this->resetCardFields();
  1154. }
  1155. public function deleteCard($id)
  1156. {
  1157. try{
  1158. \App\Models\MemberCard::find($id)->delete();
  1159. session()->flash('success',"Tesserato eliminato");
  1160. }catch(\Exception $e){
  1161. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1162. }
  1163. }
  1164. // Courses
  1165. public function addCourse()
  1166. {
  1167. $this->resetCourseFields();
  1168. /*if ($this->under18)
  1169. {
  1170. $this->course_months[] = array("m" => 9, "status" => "");
  1171. $this->course_months[] = array("m" => 10, "status" => "");
  1172. $this->course_months[] = array("m" => 11, "status" => "");
  1173. $this->course_months[] = array("m" => 12, "status" => "");
  1174. $this->course_months[] = array("m" => 1, "status" => "");
  1175. $this->course_months[] = array("m" => 2, "status" => "");
  1176. $this->course_months[] = array("m" => 3, "status" => "");
  1177. $this->course_months[] = array("m" => 4, "status" => "");
  1178. $this->course_months[] = array("m" => 5, "status" => "");
  1179. $this->course_months[] = array("m" => 6, "status" => "");
  1180. }*/
  1181. $this->addCourse = true;
  1182. $this->updateCourse = false;
  1183. $this->emit('setEditCorso', true);
  1184. }
  1185. public function storeCourse()
  1186. {
  1187. $this->validate(['course_course_id' => 'required']);
  1188. try {
  1189. \App\Models\MemberCourse::create([
  1190. 'member_id' => $this->dataId,
  1191. 'course_id' => $this->course_course_id,
  1192. 'date_from' => $this->course_date_from,
  1193. 'date_to' => $this->course_date_to,
  1194. 'course_subscription_id' => $this->course_course_subscription_id,
  1195. 'status' => $this->course_status,
  1196. 'subscribed' => false, // default non iscritto
  1197. 'price' => currencyToDouble($this->course_price),
  1198. 'subscription_price' => currencyToDouble($this->course_subscription_price),
  1199. 'notes' => $this->course_note,
  1200. 'months' => json_encode($this->course_months),
  1201. 'when' => json_encode($this->course_when)
  1202. ]);
  1203. // Se il corso ha associato una categoria iscrivo anche al gruppo
  1204. $c = \App\Models\Course::findOrFail($this->course_course_id);
  1205. if ($c)
  1206. {
  1207. if ($c->category_id > 0)
  1208. {
  1209. \App\Models\MemberCategory::create([
  1210. 'member_id' => $this->dataId,
  1211. 'category_id' => $c->category_id,
  1212. 'date' => \Carbon\Carbon::now()
  1213. ]);
  1214. }
  1215. }
  1216. session()->flash('success, Corso creato');
  1217. $this->resetCourseFields();
  1218. $this->addCourse = false;
  1219. $this->emit('setEditCorso', false);
  1220. } catch (\Exception $ex) {
  1221. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1222. }
  1223. }
  1224. public function editCourse($id){
  1225. try {
  1226. $memberCourse = \App\Models\MemberCourse::findOrFail($id);
  1227. if( !$memberCourse) {
  1228. session()->flash('error','Corso non trovato');
  1229. } else {
  1230. $this->course_course_id = $memberCourse->course_id;
  1231. // Carico i dati relativi alla struttura ad albero
  1232. $c = \App\Models\Course::findOrFail($this->course_course_id);
  1233. $this->course_name = $c->name . " (" . $c->year . ")";
  1234. $this->course_level_id = $c->course_level_id;
  1235. $this->course_type_id = $c->course_type_id;
  1236. $this->course_frequency_id = $c->course_frequency_id;
  1237. $all = \App\Models\Course::where('name', 'like', '%' . $c->name . "%")->where('enabled', true)->get();
  1238. foreach($all as $a)
  1239. {
  1240. $levels_ids[] = $a->course_level_id;
  1241. }
  1242. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->whereIn('id', $levels_ids)->get();
  1243. $all = \App\Models\Course::where('name', 'like', '%' . $c->name . "%")->where('enabled', true)->where('course_level_id', $this->course_level_id)->get();
  1244. foreach($all as $a)
  1245. {
  1246. $types_ids[] = $a->course_type_id;
  1247. }
  1248. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->whereIn('id', $types_ids)->get();
  1249. $frequencies_ids = [];
  1250. $all = \App\Models\Course::where('name', 'like', '%' . $c->name . "%")->where('enabled', true)->where('course_level_id', $this->course_level_id)->where('course_type_id', $this->course_type_id)->get();
  1251. foreach($all as $a)
  1252. {
  1253. $frequencies_ids[] = $a->course_frequency_id;
  1254. }
  1255. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->whereIn('id', $frequencies_ids)->get();
  1256. $this->course_when = array();
  1257. $this->course_date_from = $memberCourse->date_from;
  1258. $this->course_date_to = $memberCourse->date_to;
  1259. foreach(json_decode($memberCourse->when) as $z)
  1260. {
  1261. $this->course_when[] = array("day" => $z->day, "from" => $z->from, "to" => $z->to);
  1262. }
  1263. //$this->course_when = json_decode($memberCourse->when);
  1264. $this->course_course_subscription_id = $memberCourse->course_subscription_id;
  1265. $this->course_status = $memberCourse->status;
  1266. $this->course_subscribed = $memberCourse->subscribed == 1 ? true : false;
  1267. $this->course_price = formatPrice($memberCourse->price);
  1268. $this->course_subscription_price = formatPrice($memberCourse->subscription_price);
  1269. $this->course_note = $memberCourse->notes;
  1270. foreach(json_decode($memberCourse->months) as $z)
  1271. {
  1272. $this->course_months[] = array("m" => $z->m, "status" => $z->status);
  1273. }
  1274. $this->courseDataId = $memberCourse->id;
  1275. $this->updateCourse = true;
  1276. $this->addCourse = false;
  1277. $this->emit('setEditCorso', true);
  1278. }
  1279. } catch (\Exception $ex) {
  1280. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1281. }
  1282. }
  1283. public function updateCourse()
  1284. {
  1285. //$this->validate();
  1286. $this->validate(['course_course_id' => 'required']);
  1287. try {
  1288. \App\Models\MemberCourse::whereId($this->courseDataId)->update([
  1289. 'member_id' => $this->dataId,
  1290. 'course_id' => $this->course_course_id,
  1291. 'date_from' => $this->course_date_from,
  1292. 'date_to' => $this->course_date_to,
  1293. 'course_subscription_id' => $this->course_course_subscription_id,
  1294. 'status' => $this->course_status,
  1295. 'price' => currencyToDouble($this->course_price),
  1296. 'subscription_price' => currencyToDouble($this->course_subscription_price),
  1297. 'notes' => $this->course_note,
  1298. 'months' => json_encode($this->course_months),
  1299. 'when' => json_encode($this->course_when)
  1300. ]);
  1301. session()->flash('success','Corso aggiornato');
  1302. $this->resetCourseFields();
  1303. $this->updateCourse = false;
  1304. $this->emit('setEditCorso', false);
  1305. } catch (\Exception $ex) {
  1306. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1307. }
  1308. }
  1309. public function cancelCourse()
  1310. {
  1311. $this->addCourse = false;
  1312. $this->updateCourse = false;
  1313. $this->emit('setEditCorso', false);
  1314. $this->resetCourseFields();
  1315. }
  1316. public function deleteCourse($id)
  1317. {
  1318. try{
  1319. \App\Models\MemberCourse::find($id)->delete();
  1320. session()->flash('success',"Corso eliminato");
  1321. }catch(\Exception $e){
  1322. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1323. }
  1324. }
  1325. public function addRow()
  1326. {
  1327. $this->course_when[] = array('day' => array(), 'from' => '', 'to' => '');
  1328. }
  1329. public function delRow($idx)
  1330. {
  1331. unset($this->course_when[$idx]);
  1332. }
  1333. // Certificates
  1334. public function addCertificate()
  1335. {
  1336. $this->resetCertificateFields();
  1337. $this->addCertificate = true;
  1338. $this->updateCertificate = false;
  1339. }
  1340. public function storeCertificate()
  1341. {
  1342. $this->validate(['certificate_expire_date' => 'required']);
  1343. // $this->validate();
  1344. try {
  1345. $name = '';
  1346. try{
  1347. if ($this->certificate_filename)
  1348. {
  1349. $name = md5($this->certificate_filename . microtime()).'.'.$this->certificate_filename->extension();
  1350. $this->certificate_filename->storeAs('public', $name);
  1351. }
  1352. } catch (\Exception $ex) {
  1353. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1354. }
  1355. if ($this->dataId > -1)
  1356. {
  1357. \App\Models\MemberCertificate::create([
  1358. 'member_id' => $this->dataId,
  1359. 'type' => $this->certificate_type,
  1360. 'filename' => $name,
  1361. 'expire_date' => $this->certificate_expire_date,
  1362. 'status' => $this->certificate_status
  1363. ]);
  1364. updateMemberData($this->dataId);
  1365. }
  1366. /*else
  1367. {
  1368. $this->certificateTmp = new \App\Models\MemberCertificate();
  1369. $this->certificateTmp->type = $this->certificate_type;
  1370. $this->certificateTmp->filename = $name;
  1371. $this->certificateTmp->expire_date = $this->certificate_expire_date;
  1372. $this->certificateTmp->status = $this->certificate_status;
  1373. $this->certificateTmp->status = $this->certificate_status;
  1374. // s $this->member_certificates[] = $certificateTmp;
  1375. }*/
  1376. session()->flash('success, Tesserato creato');
  1377. $this->resetCertificateFields();
  1378. $this->addCertificate = false;
  1379. } catch (\Exception $ex) {
  1380. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1381. }
  1382. }
  1383. public function editCertificate($id){
  1384. try {
  1385. $memberCertificate = \App\Models\MemberCertificate::findOrFail($id);
  1386. if( !$memberCertificate) {
  1387. session()->flash('error','Tesserato non trovato');
  1388. } else {
  1389. $this->certificate_type = $memberCertificate->type;
  1390. $this->certificate_filename_old = $memberCertificate->filename;
  1391. $this->certificate_expire_date = $memberCertificate->expire_date;
  1392. $this->certificate_status = $memberCertificate->status;
  1393. $this->cardCertificateId = $memberCertificate->id;
  1394. $this->updateCertificate = true;
  1395. $this->addCertificate = false;
  1396. }
  1397. } catch (\Exception $ex) {
  1398. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1399. }
  1400. }
  1401. public function updateCertificate()
  1402. {
  1403. $this->validate(['certificate_expire_date' => 'required']);
  1404. try {
  1405. $name = '';
  1406. try{
  1407. if ($this->certificate_filename)
  1408. {
  1409. $name = md5($this->certificate_filename . microtime()).'.'.$this->certificate_filename->extension();
  1410. $this->certificate_filename->storeAs('public', $name);
  1411. }
  1412. } catch (\Exception $ex) {
  1413. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1414. }
  1415. \App\Models\MemberCertificate::whereId($this->cardCertificateId)->update([
  1416. 'member_id' => $this->dataId,
  1417. 'type' => $this->certificate_type,
  1418. 'filename' => $name != '' ? $name : $this->certificate_filename_old,
  1419. 'expire_date' => $this->certificate_expire_date,
  1420. 'status' => $this->certificate_status
  1421. ]);
  1422. updateMemberData($this->dataId);
  1423. session()->flash('success','Tesserato aggiornato');
  1424. $this->resetCertificateFields();
  1425. $this->updateCertificate = false;
  1426. } catch (\Exception $ex) {
  1427. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1428. }
  1429. }
  1430. public function cancelCertificate()
  1431. {
  1432. $this->addCertificate = false;
  1433. $this->updateCertificate = false;
  1434. $this->resetCertificateFields();
  1435. }
  1436. public function deleteCertificate($id)
  1437. {
  1438. try{
  1439. \App\Models\MemberCertificate::find($id)->delete();
  1440. session()->flash('success',"Tesserato eliminato");
  1441. }catch(\Exception $e){
  1442. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1443. }
  1444. }
  1445. // Gruppi di appartenenza
  1446. public function storeCategory()
  1447. {
  1448. $this->validate(['category_category_id' => 'required']);
  1449. try {
  1450. \App\Models\MemberCategory::create([
  1451. 'member_id' => $this->dataId,
  1452. 'category_id' => $this->category_category_id,
  1453. 'date' => \Carbon\Carbon::now()
  1454. ]);
  1455. session()->flash('success, Associazione creato');
  1456. $this->resetCategoryFields();
  1457. $this->addCard = false;
  1458. } catch (\Exception $ex) {
  1459. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1460. }
  1461. }
  1462. public function hideMsg()
  1463. {
  1464. $this->groupMsg = '';
  1465. }
  1466. public function storeCategoryWithID($id)
  1467. {
  1468. $this->groupMsg = '';
  1469. try {
  1470. if (\App\Models\MemberCategory::where('member_id', $this->dataId)->where('category_id', $id)->first())
  1471. {
  1472. $this->groupMsg = '<br>Attenzione, questo corso è stato già inserito';
  1473. }
  1474. else
  1475. {
  1476. \App\Models\MemberCategory::create([
  1477. 'member_id' => $this->dataId,
  1478. 'category_id' => $id,
  1479. 'date' => \Carbon\Carbon::now()
  1480. ]);
  1481. session()->flash('success, Associazione creato');
  1482. $this->resetCategoryFields();
  1483. $this->addCard = false;
  1484. }
  1485. } catch (\Exception $ex) {
  1486. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1487. }
  1488. }
  1489. public function deleteCategory($id)
  1490. {
  1491. try{
  1492. \App\Models\MemberCategory::find($id)->delete();
  1493. session()->flash('success',"Associazione eliminata");
  1494. }catch(\Exception $e){
  1495. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1496. }
  1497. }
  1498. public function setCourse($id)
  1499. {
  1500. $this->course_course_id = $id;
  1501. }
  1502. public function getNation($nation)
  1503. {
  1504. if ($nation > 0)
  1505. {
  1506. $ret = \App\Models\Nation::findOrFail($nation);
  1507. return $ret->name;
  1508. }
  1509. return "";
  1510. }
  1511. public function getProvince($province)
  1512. {
  1513. if ($province > 0)
  1514. {
  1515. $ret = \App\Models\Province::findOrFail($province);
  1516. return $ret->name;
  1517. }
  1518. return "";
  1519. }
  1520. public function getCity($city)
  1521. {
  1522. if ($city > 0)
  1523. {
  1524. $ret = \App\Models\City::findOrFail($city);
  1525. return $ret->name;
  1526. }
  1527. return "";
  1528. }
  1529. public function getFiscalCode()
  1530. {
  1531. $this->error_fc = false;
  1532. $cf = new codicefiscale();
  1533. $cf->setDateSeparator('-');
  1534. if ($this->first_name != '' && $this->last_name != '' && $this->birth_date != '' && $this->gender != '' && $this->birth_city_id > 0)
  1535. {
  1536. $code = '';
  1537. if ($this->birth_city_id > 0)
  1538. {
  1539. $code = \App\Models\City::findOrFail($this->birth_city_id)->code;
  1540. }
  1541. $codice = $cf->calcola($this->first_name, $this->last_name, $this->birth_date, $this->gender, $code);
  1542. $this->fiscal_code = $codice;
  1543. }
  1544. else
  1545. $this->error_fc = true;
  1546. }
  1547. public function setMonth($m)
  1548. {
  1549. $exist = -1;
  1550. foreach($this->course_months as $idx => $x)
  1551. {
  1552. if ($x["m"] == $m)
  1553. {
  1554. $exist = $idx;
  1555. break;
  1556. }
  1557. }
  1558. if ($exist > -1)
  1559. {
  1560. array_splice($this->course_months, $exist, 1);
  1561. }
  1562. else
  1563. {
  1564. $this->course_months[] = array("m" => $m, "status" => "");
  1565. }
  1566. /*
  1567. if (in_array($m, $this->course_months))
  1568. {
  1569. $i = array_search($m, $this->course_months);
  1570. array_splice($this->course_months, $i, 1);
  1571. }
  1572. else
  1573. {
  1574. $this->course_months[] = $m;
  1575. }*/
  1576. }
  1577. public function newPayment($course)
  1578. {
  1579. $c = \App\Models\Course::findOrFail($course["course_id"]);
  1580. $this->emit('setEdit', false);
  1581. $this->emit('setEditCorso', false);
  1582. return redirect()->to('/in?new=1&memberId=' . $this->dataId . '&causalId=' . $c->causal_id . '&subCausalId=' . $c->sub_causal_id . '&createSubscription=' . $this->createSubscription . (sizeof($this->payMonths) > 0 ? '&months=' . implode("|", $this->payMonths) : "") . '&price=' . $course["price"] . '&subscription_price=' . $course["subscription_price"] . "&courseId=" . $course["id"]);
  1583. }
  1584. public function block($course)
  1585. {
  1586. $xxx = json_decode($course["months"]);
  1587. foreach($xxx as $idx => $mm)
  1588. {
  1589. if (in_array($mm->m, $this->payMonths))
  1590. {
  1591. $xxx[$idx]->status = 2;
  1592. }
  1593. }
  1594. $c = \App\Models\MemberCourse::findOrFail($course["id"]);
  1595. $c->months = json_encode($xxx);
  1596. $c->save();
  1597. $this->payMonths = array();
  1598. }
  1599. public function reactivate($course)
  1600. {
  1601. $xxx = json_decode($course["months"]);
  1602. foreach($xxx as $idx => $mm)
  1603. {
  1604. if (in_array($mm->m, $this->suspendedMonths))
  1605. {
  1606. $xxx[$idx]->status = "";
  1607. }
  1608. }
  1609. $c = \App\Models\MemberCourse::findOrFail($course["id"]);
  1610. $c->months = json_encode($xxx);
  1611. $c->save();
  1612. $this->suspendedMonths = array();
  1613. }
  1614. public function setPayMonth($m, $months, $selectedCourseMember)
  1615. {
  1616. if ($this->selectedCourseMember != $selectedCourseMember)
  1617. $this->payMonths = array();
  1618. $this->selectedCourseMember = $selectedCourseMember;
  1619. if (in_array($m, $this->payMonths))
  1620. {
  1621. $i = array_search($m, $this->payMonths);
  1622. array_splice($this->payMonths, $i, 1);
  1623. }
  1624. else if (in_array($m, $this->suspendedMonths))
  1625. {
  1626. $i = array_search($m, $this->suspendedMonths);
  1627. array_splice($this->suspendedMonths, $i, 1);
  1628. }
  1629. else
  1630. {
  1631. foreach(json_decode($months) as $mm)
  1632. {
  1633. if ($mm->m == $m)
  1634. {
  1635. if ($mm->status == "")
  1636. $this->payMonths[] = $m;
  1637. if ($mm->status == "2")
  1638. $this->suspendedMonths[] = $m;
  1639. }
  1640. }
  1641. }
  1642. if (sizeof($this->payMonths) > 0 && sizeof($this->suspendedMonths) > 0)
  1643. {
  1644. $this->payMonths = array();
  1645. $this->suspendedMonths = array();
  1646. }
  1647. }
  1648. public function setCreateSubscription($subscribed, $selectedCourseMember)
  1649. {
  1650. if ($this->selectedCourseMember != $selectedCourseMember)
  1651. $this->payMonths = array();
  1652. $this->selectedCourseMember = $selectedCourseMember;
  1653. if (!$subscribed)
  1654. $this->createSubscription = !$this->createSubscription;
  1655. }
  1656. public function showHideCourse($id)
  1657. {
  1658. if (in_array($id, $this->showCourse))
  1659. {
  1660. $i = array_search($id, $this->showCourse);
  1661. array_splice($this->showCourse, $i, 1);
  1662. }
  1663. else
  1664. {
  1665. $this->showCourse[] = $id;
  1666. }
  1667. }
  1668. public function getMonthStatus($m, $months, $selectedCourseMember)
  1669. {
  1670. $class = "grey";
  1671. foreach(json_decode($months) as $mm)
  1672. {
  1673. if ($mm->m == $m)
  1674. {
  1675. if ($mm->status == "")
  1676. {
  1677. if (in_array($m, $this->payMonths) && $this->selectedCourseMember == $selectedCourseMember)
  1678. $class = "blue";
  1679. else
  1680. $class = "orange";
  1681. }
  1682. if ($mm->status == "1")
  1683. {
  1684. $class = "green";
  1685. }
  1686. if ($mm->status == "2")
  1687. {
  1688. if (in_array($m, $this->suspendedMonths) && $this->selectedCourseMember == $selectedCourseMember)
  1689. $class = "blue";
  1690. else
  1691. $class = "yellow";
  1692. }
  1693. }
  1694. }
  1695. return $class;
  1696. }
  1697. public function getSubscriptionStatus($subscribed, $selectedCourseMember)
  1698. {
  1699. $class = "grey";
  1700. if ($this->createSubscription && $this->selectedCourseMember == $selectedCourseMember)
  1701. $class = "blue";
  1702. else
  1703. $class = "orange";
  1704. if ($subscribed)
  1705. $class = "green";
  1706. return $class;
  1707. }
  1708. public function checkMonth($m)
  1709. {
  1710. $ret = false;
  1711. foreach($this->course_months as $idx => $x)
  1712. {
  1713. if ($x["m"] == $m)
  1714. {
  1715. $ret = true;
  1716. break;
  1717. }
  1718. }
  1719. return $ret;
  1720. }
  1721. public function setDay($idx, $d)
  1722. {
  1723. if (in_array($d, $this->course_when[$idx]["day"]))
  1724. {
  1725. $i = array_search($d, $this->course_when[$idx]["day"]);
  1726. array_splice($this->course_when[$idx]["day"], $i, 1);
  1727. }
  1728. else
  1729. {
  1730. $this->course_when[$idx]["day"][] = $d;
  1731. }
  1732. }
  1733. public function updateBorsellino($importoBorsellino, $s)
  1734. {
  1735. if ($importoBorsellino > 0)
  1736. {
  1737. $imp = $importoBorsellino;
  1738. if ($s == "-")
  1739. $imp = $imp * -1;
  1740. $money = new \App\Models\Money();
  1741. $money->member_id = $this->currentMember->id;
  1742. $money->record_id = null;
  1743. $money->amount = $imp;
  1744. $money->date = date("Y-m-d");
  1745. $money->note = 'Forzato ' . $imp;
  1746. $money->save();
  1747. }
  1748. $this->emit('saved');
  1749. }
  1750. public function getMonth($m)
  1751. {
  1752. $ret = '';
  1753. switch ($m) {
  1754. case 1:
  1755. $ret = 'Gennaio';
  1756. break;
  1757. case 2:
  1758. $ret = 'Febbraio';
  1759. break;
  1760. case 3:
  1761. $ret = 'Marzo';
  1762. break;
  1763. case 4:
  1764. $ret = 'Aprile';
  1765. break;
  1766. case 5:
  1767. $ret = 'Maggio';
  1768. break;
  1769. case 6:
  1770. $ret = 'Giugno';
  1771. break;
  1772. case 7:
  1773. $ret = 'Luglio';
  1774. break;
  1775. case 8:
  1776. $ret = 'Agosto';
  1777. break;
  1778. case 9:
  1779. $ret = 'Settembre';
  1780. break;
  1781. case 10:
  1782. $ret = 'Ottobre';
  1783. break;
  1784. case 11:
  1785. $ret = 'Novembre';
  1786. break;
  1787. case 12:
  1788. $ret = 'Dicembre';
  1789. break;
  1790. default:
  1791. $ret = '';
  1792. break;
  1793. }
  1794. return $ret;
  1795. }
  1796. }
  1797. class codicefiscale {
  1798. /**
  1799. * Array delle consonanti
  1800. */
  1801. protected $_consonanti = array(
  1802. 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K',
  1803. 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T',
  1804. 'V', 'W', 'X', 'Y', 'Z'
  1805. );
  1806. /**
  1807. * Array delle vocali
  1808. */
  1809. protected $_vocali = array(
  1810. 'A', 'E', 'I', 'O', 'U'
  1811. );
  1812. /**
  1813. * Array per il calcolo della lettera del mese
  1814. * Al numero del mese corrisponde una lettera
  1815. */
  1816. protected $_mesi = array(
  1817. 1 => 'A', 2 => 'B', 3 => 'C', 4 => 'D', 5 => 'E',
  1818. 6 => 'H', 7 => 'L', 8 => 'M', 9 => 'P', 10 => 'R',
  1819. 11 => 'S', 12 => 'T'
  1820. );
  1821. protected $_pari = array(
  1822. '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4,
  1823. '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9,
  1824. 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4,
  1825. 'F' => 5, 'G' => 6, 'H' => 7, 'I' => 8, 'J' => 9,
  1826. 'K' => 10, 'L' => 11, 'M' => 12, 'N' => 13, 'O' => 14,
  1827. 'P' => 15, 'Q' => 16, 'R' => 17, 'S' => 18, 'T' => 19,
  1828. 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23, 'Y' => 24,
  1829. 'Z' => 25
  1830. );
  1831. protected $_dispari = array(
  1832. '0' => 1, '1' => 0, '2' => 5, '3' => 7, '4' => 9,
  1833. '5' => 13, '6' => 15, '7' => 17, '8' => 19, '9' => 21,
  1834. 'A' => 1, 'B' => 0, 'C' => 5, 'D' => 7, 'E' => 9,
  1835. 'F' => 13, 'G' => 15, 'H' => 17, 'I' => 19, 'J' => 21,
  1836. 'K' => 2, 'L' => 4, 'M' => 18, 'N' => 20, 'O' => 11,
  1837. 'P' => 3, 'Q' => 6, 'R' => 8, 'S' => 12, 'T' => 14,
  1838. 'U' => 16, 'V' => 10, 'W' => 22, 'X' => 25, 'Y' => 24,
  1839. 'Z' => 23
  1840. );
  1841. protected $_controllo = array(
  1842. '0' => 'A', '1' => 'B', '2' => 'C', '3' => 'D',
  1843. '4' => 'E', '5' => 'F', '6' => 'G', '7' => 'H',
  1844. '8' => 'I', '9' => 'J', '10' => 'K', '11' => 'L',
  1845. '12' => 'M', '13' => 'N', '14' => 'O', '15' => 'P',
  1846. '16' => 'Q', '17' => 'R', '18' => 'S', '19' => 'T',
  1847. '20' => 'U', '21' => 'V', '22' => 'W', '23' => 'X',
  1848. '24' => 'Y', '25' => 'Z'
  1849. );
  1850. /**
  1851. * Stringa di errore
  1852. */
  1853. protected $_error = null;
  1854. /**
  1855. * Separatore per la data di nascita
  1856. */
  1857. protected $_dateSeparator = '/';
  1858. /**
  1859. * Percorso del file del database SQLite
  1860. * dei codici catastali
  1861. */
  1862. protected $_dbCatastali = null;
  1863. /**
  1864. * Trasforma la stringa passata in un array di lettere
  1865. * e lo incrocia con un ulteriore array
  1866. */
  1867. protected function _getLettere($string, array $haystack) {
  1868. $letters = array();
  1869. foreach(str_split($string) as $needle) {
  1870. if (in_array($needle, $haystack)) {
  1871. $letters[] = $needle;
  1872. }
  1873. }
  1874. return $letters;
  1875. }
  1876. /**
  1877. * Ritorna un array con le vocali di una data stringa
  1878. */
  1879. protected function _getVocali($string) {
  1880. return $this->_getLettere($string, $this->_vocali);
  1881. }
  1882. /**
  1883. * Ritorna un array con le consonanti di una data stringa
  1884. */
  1885. protected function _getConsonanti($string) {
  1886. return $this->_getLettere($string, $this->_consonanti);
  1887. }
  1888. /**
  1889. * Pulisce la stringa filtrando tutti i caratteri che
  1890. * non sono lettere. Lo switch $toupper se impostato a TRUE
  1891. * converte la stringa risultante in MAIUSCOLO.
  1892. */
  1893. protected function _sanitize($string, $toupper = true) {
  1894. $result = preg_replace('/[^A-Za-z]*/', '', $string);
  1895. return ($toupper) ? strtoupper($result) : $result;
  1896. }
  1897. /**
  1898. * Se la stringa passata a funzione e' costituita
  1899. * da meno di 3 caratteri, rimpiazza le lettere
  1900. * mancanti con la lettera X.
  1901. */
  1902. protected function _addMissingX($string) {
  1903. $code = $string;
  1904. while(strlen($code) < 3) {
  1905. $code .= 'X';
  1906. }
  1907. return $code;
  1908. }
  1909. /**
  1910. * Ottiene il codice identificativo del nome
  1911. */
  1912. protected function _calcolaNome($string) {
  1913. $nome = $this->_sanitize($string);
  1914. $code = '';
  1915. // Se il nome inserito e' piu' corto di 3 lettere
  1916. // si aggiungono tante X quanti sono i caratteri
  1917. // mancanti.
  1918. if (strlen($nome) < 3) {
  1919. return $this->_addMissingX($nome);
  1920. }
  1921. $nome_cons = $this->_getConsonanti($nome);
  1922. // Se le consonanti contenute nel nome sono minori
  1923. // o uguali a 3 vengono considerate nell'ordine in cui
  1924. // compaiono.
  1925. if (count($nome_cons) <= 3) {
  1926. $code = implode('', $nome_cons);
  1927. } else {
  1928. // Se invece abbiamo almeno 4 consonanti, prendiamo
  1929. // la prima, la terza e la quarta.
  1930. for($i=0; $i<4; $i++) {
  1931. if ($i == 1) continue;
  1932. if (!empty($nome_cons[$i])) {
  1933. $code .= $nome_cons[$i];
  1934. }
  1935. }
  1936. }
  1937. // Se compaiono meno di 3 consonanti nel nome, si
  1938. // utilizzano le vocali, nell'ordine in cui compaiono
  1939. // nel nome.
  1940. if (strlen($code) < 3) {
  1941. $nome_voc = $this->_getVocali($nome);
  1942. while (strlen($code) < 3) {
  1943. $code .= array_shift($nome_voc);
  1944. }
  1945. }
  1946. return $code;
  1947. }
  1948. protected function _calcolaCognome($string) {
  1949. $cognome = $this->_sanitize($string);
  1950. $code = '';
  1951. // Se il cognome inserito e' piu' corto di 3 lettere
  1952. // si aggiungono tante X quanti sono i caratteri
  1953. // mancanti.
  1954. if (strlen($cognome) < 3) {
  1955. return $this->_addMissingX($cognome);
  1956. }
  1957. $cognome_cons = $this->_getConsonanti($cognome);
  1958. // Per il calcolo del cognome si prendono le prime
  1959. // 3 consonanti.
  1960. for ($i=0; $i<3; $i++) {
  1961. if (array_key_exists($i, $cognome_cons)) {
  1962. $code .= $cognome_cons[$i];
  1963. }
  1964. }
  1965. // Se le consonanti non bastano, vengono prese
  1966. // le vocali nell'ordine in cui compaiono.
  1967. if (strlen($code) < 3) {
  1968. $cognome_voc = $this->_getVocali($cognome);
  1969. while (strlen($code) < 3) {
  1970. $code .= array_shift($cognome_voc);
  1971. }
  1972. }
  1973. return $code;
  1974. }
  1975. /**
  1976. * Imposta il separatore di data ( default: / )
  1977. */
  1978. public function setDateSeparator($char) {
  1979. $this->_dateSeparator = $char;
  1980. return $this;
  1981. }
  1982. /**
  1983. * Ritorna la parte di codice fiscale corrispondente
  1984. * alla data di nascita del soggetto (Forma: AAMGG)
  1985. */
  1986. protected function _calcolaDataNascita($data, $sesso) {
  1987. $dn = explode($this->_dateSeparator, $data);
  1988. $giorno = (int) @$dn[2];
  1989. $mese = (int) @$dn[1];
  1990. $anno = (int) @$dn[0];
  1991. // Le ultime due cifre dell'anno di nascita
  1992. $aa = substr($anno, -2);
  1993. // La lettera corrispondente al mese di nascita
  1994. $mm = $this->_mesi[$mese];
  1995. // Il giorno viene calcolato a seconda del sesso
  1996. // del soggetto di cui si calcola il codice:
  1997. // se e' Maschio si mette il giorno reale, se e'
  1998. // Femmina viene aggiungo 40 a questo numero.
  1999. $gg = (strtoupper($sesso) == 'M') ? $giorno : ($giorno + 40);
  2000. // Bug #1: Thanks to Luca
  2001. if (strlen($gg) < 2) $gg = '0' . $gg;
  2002. return $aa . $mm . $gg;
  2003. }
  2004. /**
  2005. * Ritorna la cifra di controllo sulla base dei
  2006. * 15 caratteri del codice fiscale calcolati.
  2007. */
  2008. protected function _calcolaCifraControllo($codice) {
  2009. $code = str_split($codice);
  2010. $sum = 0;
  2011. for($i=1; $i <= count($code); $i++) {
  2012. $cifra = $code[$i-1];
  2013. $sum += ($i % 2) ? $this->_dispari[$cifra] : $this->_pari[$cifra];
  2014. }
  2015. $sum %= 26;
  2016. return $this->_controllo[$sum];
  2017. }
  2018. /**
  2019. * Imposta il messaggio di errore
  2020. */
  2021. protected function _setError($string) {
  2022. $this->_error = $string;
  2023. }
  2024. /**
  2025. * Verifica la presenza di un errore.
  2026. * Ritorna TRUE se presente, FALSE altrimenti.
  2027. */
  2028. public function hasError() {
  2029. return !is_null($this->_error);
  2030. }
  2031. /**
  2032. * Ritorna la stringa di errore
  2033. */
  2034. public function getError() {
  2035. return $this->_error;
  2036. }
  2037. /**
  2038. * Ritorna il codice fiscale utilizzando i parametri
  2039. * passati a funzione. Se si verifica
  2040. */
  2041. public function calcola($nome, $cognome, $data, $sesso, $comune) {
  2042. $codice = $this->_calcolaCognome($cognome) .
  2043. $this->_calcolaNome($nome) .
  2044. $this->_calcolaDataNascita($data, $sesso) .
  2045. $comune;
  2046. if ($this->hasError()) {
  2047. return false;
  2048. }
  2049. $codice .= $this->_calcolaCifraControllo($codice);
  2050. if (strlen($codice) != 16) {
  2051. //$this->_setError(self::ERR_GENERIC);
  2052. return 'ERROR';
  2053. }
  2054. return $codice;
  2055. }
  2056. }