Member.php 94 KB

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