Member.php 78 KB

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