Member.php 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708
  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. $this->course_months_list[] = $dt->format("m");
  285. }
  286. $course_subscription_ids = [];
  287. $this->course_price_list = [];
  288. if ($c->prices != null) {
  289. foreach (json_decode($c->prices) as $z) {
  290. $this->course_price_list[$z->course_subscription_id] = $z->price;
  291. if ($z->price > 0)
  292. $course_subscription_ids[] = $z->course_subscription_id;
  293. }
  294. }
  295. $this->course_subscriptions = \App\Models\CourseSubscription::select('*')->whereIn('id', $course_subscription_ids)->where('enabled', true)->get();
  296. /*
  297. if ($this->course_course_id > 0) {
  298. $c = \App\Models\Course::findOrFail($this->course_course_id);
  299. $this->course_price = formatPrice($c->price);
  300. $this->course_subscription_price = formatPrice($c->subscription_price);
  301. // Controllo se sono già iscritto la corso
  302. $this->course_exist = \App\Models\MemberCourse::where('course_id', $this->course_course_id)->where('member_id', $this->dataId)->count() > 0;
  303. } else {
  304. $this->course_price = 0;
  305. $this->course_subscription_price = 0;
  306. $this->course_exist = false;
  307. }*/
  308. } else {
  309. $this->course_price = 0;
  310. $this->course_subscription_price = 0;
  311. $this->course_exist = false;
  312. }
  313. }
  314. public function updatedCourseCourseType()
  315. {
  316. $this->course_level_id = '';
  317. $this->course_frequency_id = '';
  318. $this->course_course_id = null;
  319. }
  320. public function updatedCourseCourseSubscriptionId()
  321. {
  322. $this->course_price = 0;
  323. if (isset($this->course_price_list[$this->course_course_subscription_id]))
  324. $this->course_price = $this->course_price_list[$this->course_course_subscription_id];
  325. }
  326. public $documents = [];
  327. public $father_documents = [];
  328. public $mother_documents = [];
  329. public function removeDocument($idx, $type)
  330. {
  331. if ($type == 'father') {
  332. unset($this->father_document_files[$idx]);
  333. } elseif ($type == 'mother') {
  334. unset($this->mother_document_files[$idx]);
  335. } else {
  336. unset($this->document_files[$idx]);
  337. }
  338. }
  339. public function updatedDocuments()
  340. {
  341. foreach ($this->documents as $document) {
  342. $name = $document->getClientOriginalName();
  343. $document->storeAs('public', $name);
  344. $this->document_files[] = $name;
  345. }
  346. $this->documents = [];
  347. }
  348. public function updatedFatherDocuments()
  349. {
  350. foreach ($this->father_documents as $document) {
  351. $name = $document->getClientOriginalName();
  352. $document->storeAs('public', $name);
  353. $this->father_document_files[] = $name;
  354. }
  355. $this->father_documents = [];
  356. }
  357. public function updatedMotherDocuments()
  358. {
  359. foreach ($this->mother_documents as $document) {
  360. $name = $document->getClientOriginalName();
  361. $document->storeAs('public', $name);
  362. $this->mother_document_files[] = $name;
  363. }
  364. $this->mother_documents = [];
  365. }
  366. public function resetCategoryFields()
  367. {
  368. $this->category_category_id = null;
  369. }
  370. public function getCategories($records, $indentation)
  371. {
  372. foreach ($records as $record) {
  373. // $this->categories[] = array('id' => $record->id, 'name' => str_repeat(" / ", $indentation) . $record->name);
  374. $this->categories[] = array('id' => $record->id, 'name' => $record->getTree());
  375. if (count($record->childs))
  376. $this->getCategories($record->childs, $indentation + 1);
  377. }
  378. }
  379. public function getCourses($records, $indentation)
  380. {
  381. foreach ($records as $record) {
  382. // $this->categories[] = array('id' => $record->id, 'name' => str_repeat(" / ", $indentation) . $record->name);
  383. $this->courses[] = array('id' => $record->id, 'name' => $record->getTree());
  384. if (count($record->childs))
  385. $this->getCourses($record->childs, $indentation + 1);
  386. }
  387. }
  388. public function updatedBirthDate()
  389. {
  390. $this->validateOnly('birth_date');
  391. $date1 = new DateTime($this->birth_date);
  392. $date2 = new DateTime("now");
  393. $interval = $date1->diff($date2);
  394. $this->age = $interval->y . " anni";
  395. $this->under18 = $interval->y < 18;
  396. }
  397. public function mount()
  398. {
  399. if (isset($_GET["new"])) {
  400. $this->refreshAfter = 1;
  401. $this->add();
  402. }
  403. if (isset($_GET["from"])) {
  404. $this->from = $_GET["from"];
  405. }
  406. $this->cards = \App\Models\Card::select('id', 'name')->get();
  407. $this->disciplines = \App\Models\Discipline::select('id', 'name')->get();
  408. $this->categories = array();
  409. $this->getCategories(\App\Models\Category::select('id', 'name')->where('parent_id', null)->get(), 0);
  410. // $this->getCourses(\App\Models\Course::select('id', 'name')->where('parent_id', null)->get(), 0);
  411. /*$this->nations = \App\Models\Nation::select('id', 'name')->orderBy('name')->get();
  412. $this->provinces = \App\Models\Province::where('nation_id', 1)->orderBy('name')->get();
  413. $this->cities = \App\Models\City::where('province_id', 178)->orderBy('name')->orderBy('name')->get();*/
  414. $c = \App\Models\Causal::where('type', 'IN')->where('money', true)->first();
  415. if ($c)
  416. $this->causalId = $c->id;
  417. if (isset($_GET["member_detail"])) {
  418. $this->showDetailF($_GET["member_detail"]);
  419. $this->refreshAfter = 1;
  420. }
  421. /*$this->birthNations = \App\Models\Nation::select('id', 'name')->orderBy('name')->get();
  422. $this->birthProvinces = \App\Models\Province::where('nation_id', 1)->orderBy('name')->get();
  423. $this->birthCities = \App\Models\City::where('province_id', 178)->orderBy('name')->orderBy('name')->get();*/
  424. }
  425. public function updated()
  426. {
  427. if ($this->isSaving) {
  428. $zzz = [];
  429. if ($this->birth_nation_id == null)
  430. $zzz[] = 'birth_nation_id=' . $this->birth_nation_id;
  431. if ($this->isBirthItaly) {
  432. if ($this->birth_province_id == null)
  433. $zzz[] = 'birth_province_id=' . $this->birth_province_id;
  434. if ($this->birth_city_id == null)
  435. $zzz[] = 'birth_city_id=' . $this->birth_city_id;
  436. }
  437. if ($this->nation_id == null)
  438. $zzz[] = 'nation_id=' . $this->nation_id;
  439. if ($this->isItaly) {
  440. if ($this->province_id == null)
  441. $zzz[] = 'province_id=' . $this->province_id;
  442. if ($this->city_id == null)
  443. $zzz[] = 'city_id=' . $this->city_id;
  444. }
  445. $this->emit('setErrorMsg', $zzz);
  446. }
  447. // $this->emit('load-select');
  448. }
  449. public function hydrate()
  450. {
  451. if ($this->isSaving) {
  452. $zzz = [];
  453. if ($this->birth_nation_id == null)
  454. $zzz[] = 'birth_nation_id=' . $this->birth_nation_id;
  455. if ($this->isBirthItaly) {
  456. if ($this->birth_province_id == null)
  457. $zzz[] = 'birth_province_id=' . $this->birth_province_id;
  458. if ($this->birth_city_id == null)
  459. $zzz[] = 'birth_city_id=' . $this->birth_city_id;
  460. }
  461. if ($this->nation_id == null)
  462. $zzz[] = 'nation_id=' . $this->nation_id;
  463. if ($this->isItaly) {
  464. if ($this->province_id == null)
  465. $zzz[] = 'province_id=' . $this->province_id;
  466. if ($this->city_id == null)
  467. $zzz[] = 'city_id=' . $this->city_id;
  468. }
  469. $this->emit('setErrorMsg', $zzz);
  470. }
  471. $this->emit('load-select');
  472. // $this->emit('destroy-data-table');
  473. }
  474. public function checkIsItaly()
  475. {
  476. $n = \App\Models\Nation::findOrFail($this->nation_id);
  477. $this->isItaly = $n->is_italy;
  478. }
  479. public function checkIsBirthItaly()
  480. {
  481. if ($this->birth_nation_id > 0) {
  482. $n = \App\Models\Nation::findOrFail($this->birth_nation_id);
  483. $this->isBirthItaly = $n->is_italy;
  484. } else
  485. $this->isBirthItaly = false;
  486. }
  487. /*public function loadProvinces()
  488. {
  489. $n = \App\Models\Nation::findOrFail($this->nation_id);
  490. $this->isItaly = $n->is_italy;
  491. $this->provinces = \App\Models\Province::where('nation_id', $this->nation_id)->orderBy('name')->get();
  492. $this->cities = array();
  493. $this->selectId++;
  494. }
  495. public function loadCities()
  496. {
  497. $this->cities = \App\Models\City::where('province_id', $this->province_id)->orderBy('name')->orderBy('name')->get();
  498. $this->selectId++;
  499. }
  500. public function loadBirthProvinces()
  501. {
  502. $n = \App\Models\Nation::findOrFail($this->birth_nation_id);
  503. $this->isBirthItaly = $n->is_italy;
  504. $this->birthProvinces = \App\Models\Province::where('nation_id', $this->birth_nation_id)->orderBy('name')->get();
  505. $this->birthCities = array();
  506. $this->selectId++;
  507. }
  508. public function loadBirthCities()
  509. {
  510. $this->birthCities = \App\Models\City::where('province_id', $this->birth_province_id)->get();
  511. $this->selectId++;
  512. }
  513. */
  514. public function search()
  515. {
  516. if ($this->searchTxt != '') {
  517. $this->search = $this->searchTxt;
  518. $this->showReset = true;
  519. }
  520. }
  521. public function resetSearch()
  522. {
  523. $this->showReset = false;
  524. $this->searchTxt = '';
  525. $this->search = $this->searchTxt;
  526. }
  527. public function advancedSearch()
  528. {
  529. $this->advanced = true;
  530. }
  531. public function advancedSearchCancel()
  532. {
  533. $this->filterCard = [];
  534. $this->filterCategory = [];
  535. $this->filterCertNormal = 0;
  536. $this->filterCertAgonistic = 0;
  537. $this->filterCertScaduto = 0;
  538. $this->filterCertInScadenza = 0;
  539. $this->advanced = false;
  540. }
  541. public function render()
  542. {
  543. $this->course_names = [];
  544. $allC = \App\Models\Course::where('type', $this->course_course_type)->orderBy('name')->get();
  545. foreach ($allC as $c) {
  546. $cN = $c->name . " (" . $c->year . ")";
  547. if (!in_array($cN, $this->course_names))
  548. $this->course_names[] = $cN;
  549. }
  550. //$this->course_names = \App\Models\Course::orderBy('name')->groupBy('name')->pluck('name');
  551. $datas = [];
  552. // $this->emit('destroy-data-table');
  553. if (false) {
  554. if (!$this->advanced) {
  555. /*if ($this->search != '')
  556. $datas = \App\Models\Member::select('members.*') // , \DB::raw('SUM(records.id) As total'))
  557. ->where('first_name', 'LIKE', '%' . $this->search . '%')
  558. ->orWhere('last_name', 'LIKE', '%' . $this->search . '%')
  559. ->orWhere('email', 'LIKE', '%' . $this->search . '%');
  560. $this->records = \App\Models\Member::where('first_name', 'LIKE', '%' . $this->search . '%')->orWhere('last_name', 'LIKE', '%' . $this->search . '%')->orWhere('email', 'LIKE', '%' . $this->search . '%')->get();
  561. else*/
  562. $this->records = \App\Models\Member::select('id', 'first_name', 'last_name', 'phone')->get();
  563. //$datas = \App\Models\Member::select('members.*');
  564. } else {
  565. //$this->records = \App\Models\Member::where('id', '>', 0);
  566. $datas = \App\Models\Member::select('members.*')->where('id', '>', 0);
  567. if (sizeof($this->filterCard) > 0) {
  568. $card_ids = \App\Models\MemberCard::whereIn('card_id', $this->filterCard)->pluck('member_id');
  569. //$this->records->whereIn('id', $card_ids);
  570. $datas = $datas->whereIn('id', $card_ids);
  571. }
  572. if (sizeof($this->filterCategory) > 0) {
  573. $cats_ids = \App\Models\MemberCategory::whereIn('category_id', $this->filterCategory)->pluck('member_id');
  574. //$this->records->whereIn('id', $cats_ids);
  575. $datas = $datas->whereIn('id', $cats_ids);
  576. }
  577. $certs = [];
  578. if ($this->filterCertNormal > 0) {
  579. $normal = \App\Models\MemberCertificate::where('type', 'N')->pluck('member_id');
  580. //$this->records->whereIn('id', $normal);
  581. $datas = $datas->whereIn('id', $normal);;
  582. }
  583. if ($this->filterCertAgonistic > 0) {
  584. $agonistic = \App\Models\MemberCertificate::where('type', 'A')->pluck('member_id');
  585. //$this->records->whereIn('id', $agonistic);
  586. $datas = $datas->whereIn('id', $agonistic);
  587. }
  588. if ($this->filterCertScaduto > 0) {
  589. $scaduto = \App\Models\MemberCertificate::where('expire_date', '<', date("Y-m-d"))->pluck('member_id');
  590. //$this->records->whereIn('id', $scaduto);
  591. $datas = $datas->whereIn('id', $scaduto);
  592. }
  593. if ($this->filterCertInScadenza > 0) {
  594. $scaduto = \App\Models\MemberCertificate::whereBetween('expire_date', [date("Y-m-d"), date("Y-m-d", strtotime("+1 month"))])->pluck('member_id');
  595. //$this->records->whereIn('id', $scaduto);
  596. $datas = $datas->whereIn('id', $scaduto);
  597. }
  598. if (sizeof($certs) > 0) {
  599. $datas = $datas->whereIn('id', $certs);
  600. //$this->records->whereIn('id', $certs);
  601. }
  602. $this->records = $datas->get();
  603. }
  604. foreach ($this->records as $r) {
  605. $r->age = $r->getAge();
  606. $active = $r->isActive();
  607. $r->status = $active["status"];
  608. $r->date = $active["date"];
  609. $r->certificate = $r->hasCertificate()["date"];
  610. $r->state = $r->getStatus()["status"];
  611. }
  612. /*
  613. if ($this->sortAsc)
  614. $this->records = $this->records->sortBy($this->sortField);
  615. else
  616. $this->records = $this->records->sortByDesc($this->sortField);
  617. */
  618. //$datas = $datas->get(); // ->orderBy($this->sortField, $this->sortAsc ? 'ASC' : 'DESC')->paginate(10);
  619. //$this->records = $this->records->get();
  620. $this->emit('load-data-table');
  621. }
  622. $this->loadMemberCards();
  623. $this->loadMemberCourses();
  624. $this->loadMemberCategories();
  625. $this->loadMemberCertificates();
  626. $this->courses = \App\Models\Course::select('id', 'name')->where('type', $this->course_course_type)->get();
  627. // $this->course_subscriptions = \App\Models\CourseSubscription::select('*')->where('enabled', true)->get();
  628. return view('livewire.member', ['datas' => $datas]);
  629. }
  630. public function updatedCourseCourseId()
  631. {
  632. if ($this->course_course_id > 0) {
  633. $c = \App\Models\Course::findOrFail($this->course_course_id);
  634. $this->course_price = formatPrice($c->price);
  635. $this->course_subscription_price = formatPrice($c->subscription_price);
  636. $this->course_date_from = $c->date_from;
  637. $this->course_date_to = $c->date_to;
  638. // Controllo se sono già iscritto la corso
  639. $this->course_exist = \App\Models\MemberCourse::where('course_id', $this->course_course_id)->where('member_id', $this->dataId)->count() > 0;
  640. // Carico gli abbonamenti e i mesi
  641. $period = \Carbon\CarbonPeriod::create($c->date_from, '1 month', $c->date_to);
  642. $this->course_months_list = [];
  643. foreach ($period as $dt) {
  644. $this->course_months_list[] = $dt->format("m");
  645. }
  646. $course_subscription_ids = [];
  647. $this->course_price_list = [];
  648. if ($c->prices != null) {
  649. foreach (json_decode($c->prices) as $z) {
  650. $this->course_price_list[$z->course_subscription_id] = $z->price;
  651. if ($z->price > 0)
  652. $course_subscription_ids[] = $z->course_subscription_id;
  653. }
  654. }
  655. $this->course_subscriptions = \App\Models\CourseSubscription::select('*')->whereIn('id', $course_subscription_ids)->where('enabled', true)->get();
  656. } else {
  657. $this->course_price = 0;
  658. $this->course_subscription_price = 0;
  659. $this->course_exist = false;
  660. }
  661. }
  662. public function loadMemberCards()
  663. {
  664. $this->member_cards = \App\Models\MemberCard::where('member_id', $this->dataId)->get();
  665. // return view('livewire.member');
  666. }
  667. public function loadMemberCourses()
  668. {
  669. $this->member_courses = \App\Models\MemberCourse::where('member_id', $this->dataId)->get();
  670. // return view('livewire.member');
  671. }
  672. public function loadMemberCategories()
  673. {
  674. $this->member_categories = \App\Models\MemberCategory::where('member_id', $this->dataId)->get();
  675. // return view('livewire.member');
  676. }
  677. public function loadMemberCertificates()
  678. {
  679. $this->member_certificates = \App\Models\MemberCertificate::where('member_id', $this->dataId)->orderBy('expire_date', 'DESC')->get();
  680. // return view('livewire.member');
  681. }
  682. public function showDetailF($id)
  683. {
  684. if (!isset($_GET["from"]) && $this->from == '')
  685. $this->from = 'members';
  686. $this->currentMember = \App\Models\Member::findOrFail($id);
  687. $this->currentStatus = $this->currentMember->getStatus();
  688. $this->showDetail = true;
  689. }
  690. public function add()
  691. {
  692. //$this->from = 'members';
  693. $this->emit('load-select');
  694. $this->emit('hide-search');
  695. $this->showDetail = false;
  696. $this->resetFields();
  697. $this->add = true;
  698. $this->update = false;
  699. $this->emit('setEdit', true);
  700. $this->emit('setEditCorso', false);
  701. }
  702. public function store($close)
  703. {
  704. $this->isSaving = true;
  705. $this->emit('load-select');
  706. $this->emit('goToTop');
  707. $rules = [
  708. 'first_name' => 'required',
  709. 'last_name' => 'required',
  710. 'email' => 'required',
  711. 'phone' => 'required',
  712. 'birth_nation_id' => 'required',
  713. 'address' => 'required',
  714. 'zip_code' => 'required',
  715. 'nation_id' => 'required',
  716. 'birth_date' => 'before_or_equal:today',
  717. 'gender' => 'required'
  718. ];
  719. if ($this->isBirthItaly) {
  720. $rules['birth_province_id'] = 'required';
  721. $rules['birth_city_id'] = 'required';
  722. $rules['fiscal_code'] = 'required';
  723. } else {
  724. $rules['birth_place'] = 'required';
  725. }
  726. if ($this->isItaly) {
  727. $rules['province_id'] = 'required';
  728. $rules['city_id'] = 'required';
  729. }
  730. $zzz = [];
  731. if ($this->birth_nation_id == null)
  732. $zzz[] = 'birth_nation_id=' . $this->birth_nation_id;
  733. if ($this->isBirthItaly) {
  734. if ($this->birth_province_id == null)
  735. $zzz[] = 'birth_province_id=' . $this->birth_province_id;
  736. if ($this->birth_city_id == null)
  737. $zzz[] = 'birth_city_id=' . $this->birth_city_id;
  738. }
  739. if ($this->nation_id == null)
  740. $zzz[] = 'nation_id=' . $this->nation_id;
  741. if ($this->isItaly) {
  742. if ($this->province_id == null)
  743. $zzz[] = 'province_id=' . $this->province_id;
  744. if ($this->city_id == null)
  745. $zzz[] = 'city_id=' . $this->city_id;
  746. }
  747. $this->emit('setErrorMsg', $zzz);
  748. if (!empty($this->fiscal_code)) {
  749. $existingMember = \App\Models\Member::where('fiscal_code', $this->fiscal_code)->first();
  750. if ($existingMember) {
  751. $this->already_existing = true;
  752. $this->emit('focus-error-field', 'fiscal_code');
  753. return;
  754. }
  755. }
  756. if ($this->under18) {
  757. $rules['father_name'] = 'required_without:mother_name';
  758. $rules['father_email'] = 'required_without:mother_email|email';
  759. $rules['father_fiscal_code'] = 'required_without:mother_fiscal_code';
  760. $rules['mother_name'] = 'required_without:father_name';
  761. $rules['mother_email'] = 'required_without:father_email|email';
  762. $rules['mother_fiscal_code'] = 'required_without:father_fiscal_code';
  763. }
  764. try {
  765. $this->validate($rules);
  766. } catch (\Illuminate\Validation\ValidationException $e) {
  767. $this->isSaving = false;
  768. $errorFields = array_keys($e->errors());
  769. if (!empty($errorFields)) {
  770. $this->emit('focus-error-field', $errorFields[0]);
  771. }
  772. Log::error('Validation failed', ['errors' => $e->errors()]);
  773. return;
  774. }
  775. try {
  776. $name = '';
  777. if ($this->image) {
  778. $name = md5($this->image . microtime()) . '.' . $this->image->extension();
  779. $this->image->storeAs('public', $name);
  780. }
  781. $docs = implode("|", $this->document_files);
  782. $father_docs = implode("|", $this->father_document_files);
  783. $mother_docs = implode("|", $this->mother_document_files);
  784. $member = \App\Models\Member::create([
  785. 'first_name' => strtoupper($this->first_name),
  786. 'last_name' => strtoupper($this->last_name),
  787. 'status' => $this->status,
  788. 'birth_city_id' => $this->birth_city_id > 0 ? $this->birth_city_id : null,
  789. 'birth_province_id' => $this->birth_province_id > 0 ? $this->birth_province_id : null,
  790. 'birth_nation_id' => $this->birth_nation_id > 0 ? $this->birth_nation_id : null,
  791. 'birth_date' => $this->birth_date,
  792. 'birth_place' => $this->birth_place,
  793. 'father_name' => $this->father_name,
  794. 'mother_name' => $this->mother_name,
  795. 'father_email' => strtolower($this->father_email),
  796. 'mother_email' => strtolower($this->mother_email),
  797. 'father_phone' => $this->father_phone,
  798. 'mother_phone' => $this->mother_phone,
  799. 'father_fiscal_code' => $this->father_fiscal_code,
  800. 'mother_fiscal_code' => $this->mother_fiscal_code,
  801. 'father_doc_number' => $this->father_doc_number,
  802. 'father_doc_type' => $this->father_doc_type,
  803. 'mother_doc_number' => $this->mother_doc_number,
  804. 'mother_doc_type' => $this->mother_doc_type,
  805. 'document_type' => $this->document_type,
  806. 'document_number' => $this->document_number,
  807. 'document_from' => $this->document_from,
  808. 'document_expire_date' => $this->document_expire_date,
  809. 'document_files' => $docs,
  810. 'mother_document_files' => $mother_docs,
  811. 'father_document_files' => $father_docs,
  812. 'gender' => $this->gender,
  813. 'no_send_mail' => $this->no_send_mail,
  814. 'exclude_from_records' => $this->exclude_from_records,
  815. 'fiscal_code' => $this->fiscal_code,
  816. 'address' => $this->address,
  817. 'zip_code' => $this->zip_code,
  818. 'nation_id' => $this->nation_id > 0 ? $this->nation_id : null,
  819. 'province_id' => $this->province_id > 0 ? $this->province_id : null,
  820. 'city_id' => $this->city_id > 0 ? $this->city_id : null,
  821. 'phone' => $this->phone,
  822. 'phone2' => $this->phone2,
  823. 'phone3' => $this->phone3,
  824. 'email' => strtolower($this->email),
  825. 'image' => $name,
  826. 'to_complete' => false,
  827. 'enabled' => $this->enabled
  828. ]);
  829. session()->flash('success, Tesserato creato');
  830. updateMemberData($member->id);
  831. $this->resetFields();
  832. if ($close) {
  833. $this->add = false;
  834. } else {
  835. $this->edit($member->id);
  836. }
  837. $this->emit('setEdit', false);
  838. } catch (\Exception $ex) {
  839. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  840. }
  841. }
  842. public function duplicate($id)
  843. {
  844. $member = \App\Models\Member::findOrFail($id);
  845. $newMember = $member->replicate();
  846. $newMember->save();
  847. $this->edit($newMember->id);
  848. }
  849. public function edit($id)
  850. {
  851. if (!isset($_GET["from"]) && $this->from == '')
  852. $this->from = 'members';
  853. $this->showDetail = false;
  854. $this->emit('setEdit', true);
  855. $this->emit('setEditCorso', false);
  856. $this->emit('hide-search');
  857. try {
  858. $member = \App\Models\Member::findOrFail($id);
  859. if (!$member) {
  860. session()->flash('error', 'Tesserato non trovato');
  861. } else {
  862. $this->first_name = $member->first_name;
  863. $this->last_name = $member->last_name;
  864. $this->status = $member->status;
  865. $this->birth_city_id = $member->birth_city_id;
  866. $this->birth_province_id = $member->birth_province_id;
  867. $this->birth_nation_id = $member->birth_nation_id;
  868. $this->birth_date = $member->birth_date;
  869. $this->birth_place = $member->birth_place;
  870. $this->father_name = $member->father_name;
  871. $this->mother_name = $member->mother_name;
  872. $this->father_email = strtolower($member->father_email);
  873. $this->mother_email = strtolower($member->mother_email);
  874. $this->father_phone = $member->father_phone;
  875. $this->mother_phone = $member->mother_phone;
  876. $this->father_fiscal_code = $member->father_fiscal_code;
  877. $this->mother_fiscal_code = $member->mother_fiscal_code;
  878. $this->father_doc_number = $member->father_doc_number;
  879. $this->mother_doc_number = $member->mother_doc_number;
  880. $this->father_doc_type = $member->father_doc_type;
  881. $this->mother_doc_type = $member->mother_doc_type;
  882. $this->father_document_files = explode("|", $member->father_document_files);
  883. $this->mother_document_files = explode("|", $member->mother_document_files);
  884. $this->document_type = $member->document_type;
  885. $this->document_number = $member->document_number;
  886. $this->document_from = $member->document_from;
  887. $this->document_expire_date = $member->document_expire_date;
  888. $this->document_files = explode("|", $member->document_files);
  889. $this->gender = $member->gender;
  890. $this->no_send_mail = $member->no_send_mail;
  891. $this->exclude_from_records = $member->exclude_from_records;
  892. $this->fiscal_code = $member->fiscal_code;
  893. $this->address = $member->address;
  894. $this->zip_code = $member->zip_code;
  895. $this->nation_id = $member->nation_id;
  896. $this->province_id = $member->province_id;
  897. $this->city_id = $member->city_id;
  898. $this->phone = $member->phone;
  899. $this->phone2 = $member->phone2;
  900. $this->phone3 = $member->phone3;
  901. $this->email = strtolower($member->email);
  902. $this->image_old = $member->image;
  903. $this->enabled = $member->enabled;
  904. $this->dataId = $member->id;
  905. $this->active = $member->getStatus();
  906. $this->money = $member->getMoney();
  907. $this->checkIsBirthItaly();
  908. $date1 = new DateTime($this->birth_date);
  909. $date2 = new DateTime("now");
  910. $interval = $date1->diff($date2);
  911. $this->age = $interval->y . " anni";
  912. $this->under18 = $interval->y < 18;
  913. //$this->provinces = \App\Models\Province::where('nation_id', $this->nation_id)->get();
  914. //$this->cities = \App\Models\City::where('province_id', $this->province_id)->get();
  915. //$this->birthProvinces = \App\Models\Province::where('nation_id', $this->birth_nation_id)->get();
  916. //$this->birthCities = \App\Models\City::where('province_id', $this->birth_province_id)->get();
  917. $this->update = true;
  918. $this->add = false;
  919. $this->emit('setIds', $this->nation_id, $this->birth_nation_id);
  920. $this->emit('load-select');
  921. $this->emit('load-provinces', $this->nation_id, 'provinceClass');
  922. $this->emit('load-provinces', $this->birth_nation_id, 'provinceBirthClass');
  923. $this->emit('load-cities', $this->province_id, 'cityClass');
  924. $this->emit('load-cities', $this->birth_province_id, 'cityBirthClass');
  925. }
  926. } catch (\Exception $ex) {
  927. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  928. }
  929. }
  930. public function updateAAA($close)
  931. {
  932. $this->emit('goToTop');
  933. $this->isSaving = true;
  934. $this->emit('load-select');
  935. $rules = [
  936. 'first_name' => 'required',
  937. 'last_name' => 'required',
  938. 'email' => 'required',
  939. 'phone' => 'required',
  940. 'birth_nation_id' => 'required',
  941. //'birth_place' => 'required',
  942. 'address' => 'required',
  943. 'zip_code' => 'required',
  944. 'nation_id' => 'required',
  945. 'birth_date' => 'before_or_equal:today'
  946. ];
  947. if ($this->isBirthItaly) {
  948. $rules['birth_province_id'] = 'required';
  949. $rules['birth_city_id'] = 'required';
  950. } else {
  951. $rules['birth_place'] = 'required';
  952. }
  953. if ($this->isItaly) {
  954. $rules['province_id'] = 'required';
  955. $rules['city_id'] = 'required';
  956. }
  957. $zzz = [];
  958. if ($this->birth_nation_id == null)
  959. $zzz[] = 'birth_nation_id=' . $this->birth_nation_id;
  960. if ($this->isBirthItaly) {
  961. if ($this->birth_province_id == null)
  962. $zzz[] = 'birth_province_id=' . $this->birth_province_id;
  963. if ($this->birth_city_id == null)
  964. $zzz[] = 'birth_city_id=' . $this->birth_city_id;
  965. }
  966. if ($this->nation_id == null)
  967. $zzz[] = 'nation_id=' . $this->nation_id;
  968. if ($this->isItaly) {
  969. if ($this->province_id == null)
  970. $zzz[] = 'province_id=' . $this->province_id;
  971. if ($this->city_id == null)
  972. $zzz[] = 'city_id=' . $this->city_id;
  973. }
  974. $this->emit('setErrorMsg', $zzz);
  975. if (!empty($this->fiscal_code)) {
  976. $existingMember = \App\Models\Member::where('fiscal_code', $this->fiscal_code)->first();
  977. if ($existingMember) {
  978. $this->already_existing = true;
  979. $this->emit('focus-error-field', 'fiscal_code');
  980. return;
  981. }
  982. }
  983. if ($this->under18) {
  984. $rules['father_name'] = 'required_without:mother_name';
  985. $rules['father_email'] = 'required_without:mother_email|email';
  986. $rules['father_fiscal_code'] = 'required_without:mother_fiscal_code';
  987. $rules['mother_name'] = 'required_without:father_name';
  988. $rules['mother_email'] = 'required_without:father_email|email';
  989. $rules['mother_fiscal_code'] = 'required_without:father_fiscal_code';
  990. }
  991. try {
  992. $this->validate($rules);
  993. } catch (\Illuminate\Validation\ValidationException $e) {
  994. $this->isSaving = false;
  995. $errorFields = array_keys($e->errors());
  996. if (!empty($errorFields)) {
  997. $this->emit('focus-error-field', $errorFields[0]);
  998. }
  999. Log::error('Validation failed', ['errors' => $e->errors()]);
  1000. return;
  1001. }
  1002. try {
  1003. $name = '';
  1004. if ($this->image) {
  1005. $name = md5($this->image . microtime()) . '.' . $this->image->extension();
  1006. $this->image->storeAs('public', $name);
  1007. }
  1008. $docs = implode("|", $this->document_files);
  1009. $father_docs = implode("|", $this->father_document_files);
  1010. $mother_docs = implode("|", $this->mother_document_files);
  1011. \App\Models\Member::whereId($this->dataId)->update([
  1012. 'first_name' => strtoupper($this->first_name),
  1013. 'last_name' => strtoupper($this->last_name),
  1014. 'status' => $this->status,
  1015. 'birth_city_id' => $this->birth_city_id > 0 ? $this->birth_city_id : null,
  1016. 'birth_province_id' => $this->birth_province_id > 0 ? $this->birth_province_id : null,
  1017. 'birth_nation_id' => $this->birth_nation_id > 0 ? $this->birth_nation_id : null,
  1018. 'birth_date' => $this->birth_date,
  1019. 'birth_place' => $this->birth_place,
  1020. 'father_name' => $this->father_name,
  1021. 'mother_name' => $this->mother_name,
  1022. 'father_email' => strtolower($this->father_email),
  1023. 'mother_email' => strtolower($this->mother_email),
  1024. 'father_phone' => $this->father_phone,
  1025. 'mother_phone' => $this->mother_phone,
  1026. 'father_fiscal_code' => $this->father_fiscal_code,
  1027. 'mother_fiscal_code' => $this->mother_fiscal_code,
  1028. 'father_doc_number' => $this->father_doc_number,
  1029. 'father_doc_type' => $this->father_doc_type,
  1030. 'mother_doc_number' => $this->mother_doc_number,
  1031. 'mother_doc_type' => $this->mother_doc_type,
  1032. 'father_document_files' => $father_docs,
  1033. 'mother_document_files' => $mother_docs,
  1034. 'document_type' => $this->document_type,
  1035. 'document_number' => $this->document_number,
  1036. 'document_from' => $this->document_from,
  1037. 'document_expire_date' => $this->document_expire_date,
  1038. 'document_files' => $docs,
  1039. 'gender' => $this->gender,
  1040. 'no_send_mail' => $this->no_send_mail,
  1041. 'exclude_from_records' => $this->exclude_from_records,
  1042. 'fiscal_code' => $this->fiscal_code,
  1043. 'address' => $this->address,
  1044. 'zip_code' => $this->zip_code,
  1045. 'nation_id' => $this->nation_id > 0 ? $this->nation_id : null,
  1046. 'province_id' => $this->province_id > 0 ? $this->province_id : null,
  1047. 'city_id' => $this->city_id > 0 ? $this->city_id : null,
  1048. 'image' => $name != '' ? $name : $this->image_old,
  1049. 'phone' => $this->phone,
  1050. 'phone2' => $this->phone2,
  1051. 'phone3' => $this->phone3,
  1052. 'email' => strtolower($this->email),
  1053. 'to_complete' => false,
  1054. 'enabled' => $this->enabled
  1055. ]);
  1056. updateMemberData($this->dataId);
  1057. session()->flash('success', 'Tesserato aggiornato');
  1058. if ($close) {
  1059. $this->resetFields();
  1060. $this->update = false;
  1061. }
  1062. $this->emit('setEdit', false);
  1063. } catch (\Exception $ex) {
  1064. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1065. }
  1066. }
  1067. public function cancel()
  1068. {
  1069. $this->add = false;
  1070. $this->update = false;
  1071. $this->showDetail = false;
  1072. $this->resetFields();
  1073. $this->emit('setEdit', false);
  1074. //$this->emit('setEditCorso', false);
  1075. $this->emit('reload');
  1076. }
  1077. public function delete($id)
  1078. {
  1079. try {
  1080. \App\Models\Member::find($id)->delete();
  1081. session()->flash('success', "Tesserato eliminato");
  1082. $this->emit('reload');
  1083. } catch (\Exception $e) {
  1084. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  1085. }
  1086. }
  1087. public function checkUncheckAll()
  1088. {
  1089. if (!$this->checkedAll) {
  1090. $this->multipleIds = array();
  1091. } else {
  1092. foreach ($this->records as $r) {
  1093. $this->multipleIds[] = $r->id;
  1094. }
  1095. }
  1096. }
  1097. public function multipleDelete()
  1098. {
  1099. try {
  1100. foreach ($this->multipleIds as $id) {
  1101. \App\Models\Member::find($id)->delete();
  1102. }
  1103. } catch (\Exception $e) {
  1104. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  1105. }
  1106. $this->multipleAction = '';
  1107. }
  1108. // Card
  1109. public function addCard()
  1110. {
  1111. $this->resetCardFields();
  1112. $this->addCard = true;
  1113. $this->updateCard = false;
  1114. }
  1115. public function storeCard()
  1116. {
  1117. $this->validate(['card_card_id' => 'required']);
  1118. try {
  1119. // in base alla card selezionata calcolo la scadenza
  1120. $expire_date = null;
  1121. if ($this->card_date != '') {
  1122. $y = date("Y", strtotime($this->card_date));
  1123. $card = \App\Models\Card::findOrFail($this->card_card_id);
  1124. if ($card->next_day_expire > 0 && $card->next_month_expire > 0) {
  1125. $m = strlen($card->next_month_expire) == 1 ? ('0' . $card->next_month_expire) : $card->next_month_expire;
  1126. if (date("md", strtotime($this->card_date)) > ($m . $card->next_day_expire))
  1127. $y += 1;
  1128. $next_exp = date($y . "-" . $m . "-" . $card->next_day_expire);
  1129. if ($next_exp > $this->card_date) {
  1130. $expire_date = $next_exp;
  1131. } else
  1132. $expire_date = date($y . "-" . $card->next_month_expire . "-" . $card->next_day_expire, strtotime(' + 1 years'));
  1133. } else {
  1134. if ($card->one_year_expire) {
  1135. $expire_date = date("Y-m-d", strtotime($this->card_date . ' + 1 years'));
  1136. }
  1137. }
  1138. }
  1139. \App\Models\MemberCard::create([
  1140. 'member_id' => $this->dataId,
  1141. 'card_id' => $this->card_card_id,
  1142. 'number' => $this->card_number,
  1143. 'date' => $this->card_date,
  1144. 'accept_date' => $this->card_accept_date != '' ? $this->card_accept_date : $this->card_date,
  1145. 'expire_date' => $expire_date,
  1146. 'status' => $this->card_status,
  1147. 'discipline1_id' => $this->card_discipline1_id,
  1148. 'discipline2_id' => $this->card_discipline2_id,
  1149. 'discipline3_id' => $this->card_discipline3_id,
  1150. ]);
  1151. updateMemberData($this->dataId);
  1152. session()->flash('success, Tesserato creato');
  1153. $this->resetCardFields();
  1154. $this->addCard = false;
  1155. } catch (\Exception $ex) {
  1156. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1157. }
  1158. }
  1159. public function editCard($id)
  1160. {
  1161. try {
  1162. $memberCard = \App\Models\MemberCard::findOrFail($id);
  1163. if (!$memberCard) {
  1164. session()->flash('error', 'Tesserato non trovato');
  1165. } else {
  1166. $this->card_card_id = $memberCard->card_id;
  1167. $this->card_number = $memberCard->number;
  1168. $this->card_date = $memberCard->date;
  1169. $this->card_accept_date = $memberCard->accept_date;
  1170. $this->card_status = $memberCard->status;
  1171. $this->card_discipline1_id = $memberCard->discipline1_id;
  1172. $this->card_discipline2_id = $memberCard->discipline2_id;
  1173. $this->card_discipline3_id = $memberCard->discipline3_id;
  1174. $this->cardDataId = $memberCard->id;
  1175. $this->updateCard = true;
  1176. $this->addCard = false;
  1177. }
  1178. } catch (\Exception $ex) {
  1179. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1180. }
  1181. }
  1182. public function updateCard()
  1183. {
  1184. $this->validate(['card_card_id' => 'required']);
  1185. try {
  1186. \Illuminate\Support\Facades\Log::info('Starting card update', [
  1187. 'member_id' => $this->dataId,
  1188. 'card_id' => $this->card_card_id,
  1189. 'card_number' => $this->card_number
  1190. ]);
  1191. $expire_date = null;
  1192. if ($this->card_date != '') {
  1193. $card = \App\Models\Card::findOrFail($this->card_card_id);
  1194. \Illuminate\Support\Facades\Log::info('Card details', [
  1195. 'card_id' => $card->id,
  1196. 'next_day_expire' => $card->next_day_expire,
  1197. 'next_month_expire' => $card->next_month_expire,
  1198. 'one_year_expire' => $card->one_year_expire
  1199. ]);
  1200. if ($card->next_day_expire > 0 && $card->next_month_expire > 0) {
  1201. $y = date("Y", strtotime($this->card_date));
  1202. $m = strlen($card->next_month_expire) == 1 ? ('0' . $card->next_month_expire) : $card->next_month_expire;
  1203. $d = strlen($card->next_day_expire) == 1 ? ('0' . $card->next_day_expire) : $card->next_day_expire;
  1204. $next_exp = $y . "-" . $m . "-" . $d;
  1205. // Format dates for comparison
  1206. $input_date_obj = new \DateTime($this->card_date);
  1207. $next_exp_obj = new \DateTime($next_exp);
  1208. if ($next_exp_obj > $input_date_obj) {
  1209. $expire_date = $next_exp;
  1210. } else {
  1211. // Add 1 year to the expiration date if the input date is after the expiration date
  1212. $next_exp_obj->modify('+1 year');
  1213. $expire_date = $next_exp_obj->format('Y-m-d');
  1214. }
  1215. \Illuminate\Support\Facades\Log::info('Calculated expiration date (next_day_expire/next_month_expire rule)', [
  1216. 'input_date' => $this->card_date,
  1217. 'next_exp' => $next_exp,
  1218. 'expire_date' => $expire_date,
  1219. 'comparison' => ($next_exp_obj > $input_date_obj) ? 'next_exp is after input_date' : 'next_exp is before or equal to input_date'
  1220. ]);
  1221. } else {
  1222. if ($card->one_year_expire) {
  1223. $expire_date = date("Y-m-d", strtotime($this->card_date . ' + 1 years'));
  1224. \Illuminate\Support\Facades\Log::info('Calculated expiration date (one_year_expire rule)', [
  1225. 'input_date' => $this->card_date,
  1226. 'expire_date' => $expire_date
  1227. ]);
  1228. }
  1229. }
  1230. }
  1231. \Illuminate\Support\Facades\Log::info('Updating member card', [
  1232. 'card_id' => $this->cardDataId,
  1233. 'member_id' => $this->dataId,
  1234. 'card_number' => $this->card_number,
  1235. 'date' => $this->card_date,
  1236. 'expire_date' => $expire_date,
  1237. 'status' => $this->card_status
  1238. ]);
  1239. \App\Models\MemberCard::whereId($this->cardDataId)->update([
  1240. 'member_id' => $this->dataId,
  1241. 'card_id' => $this->card_card_id,
  1242. 'number' => $this->card_number,
  1243. 'date' => $this->card_date,
  1244. 'accept_date' => $this->card_accept_date != '' ? $this->card_accept_date : $this->card_date,
  1245. 'expire_date' => $expire_date,
  1246. 'status' => $this->card_status,
  1247. 'discipline1_id' => $this->card_discipline1_id,
  1248. 'discipline2_id' => $this->card_discipline2_id,
  1249. 'discipline3_id' => $this->card_discipline3_id,
  1250. ]);
  1251. updateMemberData($this->dataId);
  1252. \Illuminate\Support\Facades\Log::info('Card updated successfully', [
  1253. 'card_id' => $this->cardDataId,
  1254. 'member_id' => $this->dataId
  1255. ]);
  1256. session()->flash('success', 'Tesserato aggiornato');
  1257. $this->resetCardFields();
  1258. $this->updateCard = false;
  1259. } catch (\Exception $ex) {
  1260. \Illuminate\Support\Facades\Log::error('Error updating card', [
  1261. 'card_id' => $this->cardDataId,
  1262. 'member_id' => $this->dataId,
  1263. 'error_message' => $ex->getMessage(),
  1264. 'error_trace' => $ex->getTraceAsString()
  1265. ]);
  1266. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1267. }
  1268. }
  1269. public function cancelCard()
  1270. {
  1271. $this->addCard = false;
  1272. $this->updateCard = false;
  1273. $this->resetCardFields();
  1274. }
  1275. public function deleteCard($id)
  1276. {
  1277. try {
  1278. \App\Models\MemberCard::find($id)->delete();
  1279. session()->flash('success', "Tesserato eliminato");
  1280. } catch (\Exception $e) {
  1281. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  1282. }
  1283. }
  1284. // Courses
  1285. public function addCourse()
  1286. {
  1287. $this->resetCourseFields();
  1288. /*if ($this->under18)
  1289. {
  1290. $this->course_months[] = array("m" => 9, "status" => "");
  1291. $this->course_months[] = array("m" => 10, "status" => "");
  1292. $this->course_months[] = array("m" => 11, "status" => "");
  1293. $this->course_months[] = array("m" => 12, "status" => "");
  1294. $this->course_months[] = array("m" => 1, "status" => "");
  1295. $this->course_months[] = array("m" => 2, "status" => "");
  1296. $this->course_months[] = array("m" => 3, "status" => "");
  1297. $this->course_months[] = array("m" => 4, "status" => "");
  1298. $this->course_months[] = array("m" => 5, "status" => "");
  1299. $this->course_months[] = array("m" => 6, "status" => "");
  1300. }*/
  1301. $this->addCourse = true;
  1302. $this->updateCourse = false;
  1303. $this->emit('setEditCorso', true);
  1304. }
  1305. public function storeCourse()
  1306. {
  1307. $this->validate(['course_course_id' => 'required']);
  1308. try {
  1309. $mc = new \App\Models\MemberCourse();
  1310. $mc->member_id = $this->dataId;
  1311. $mc->course_id = $this->course_course_id;
  1312. $mc->date_from = $this->course_date_from;
  1313. $mc->date_to = $this->course_date_to;
  1314. $mc->course_subscription_id = $this->course_course_subscription_id;
  1315. $mc->status = $this->course_status;
  1316. $mc->subscribed = false;
  1317. $mc->price = currencyToDouble($this->course_price);
  1318. $mc->subscription_price = currencyToDouble($this->course_subscription_price);
  1319. $mc->notes = $this->course_note;
  1320. $mc->months = json_encode($this->course_months);
  1321. $mc->when = json_encode($this->course_when);
  1322. $mc->save();
  1323. // Se il corso ha associato una categoria iscrivo anche al gruppo
  1324. $c = \App\Models\Course::findOrFail($this->course_course_id);
  1325. if ($c) {
  1326. if ($c->category_id > 0) {
  1327. \App\Models\MemberCategory::create([
  1328. 'member_id' => $this->dataId,
  1329. 'category_id' => $c->category_id,
  1330. 'date' => \Carbon\Carbon::now()
  1331. ]);
  1332. }
  1333. }
  1334. // Creo i pagamenti in base alla tipologia
  1335. $r = \App\Models\CourseSubscription::findOrFail($this->course_course_subscription_id);
  1336. $start = $this->course_date_from;
  1337. // Creo il pagamento per l'iscrizione
  1338. $rate = new \App\Models\Rate();
  1339. $rate->member_id = $this->dataId;
  1340. $rate->member_course_id = $mc->id;
  1341. $rate->course_subscription_id = null;
  1342. $rate->price = currencyToDouble($this->course_subscription_price);
  1343. $rate->date = $start;
  1344. $rate->months = json_encode([]);
  1345. $rate->note = '';
  1346. $rate->status = 0;
  1347. $rate->is_subscription = true;
  1348. $rate->save();
  1349. $go = true;
  1350. while ($go) {
  1351. $mms = [];
  1352. $mms[] = date("n", strtotime($start));
  1353. for ($jj = 1; $jj < $r->months; $jj++) {
  1354. $mms[] = date('n', strtotime("+" . $jj . " months", strtotime($start)));
  1355. }
  1356. $rate = new \App\Models\Rate();
  1357. $rate->member_id = $this->dataId;
  1358. $rate->member_course_id = $mc->id;
  1359. $rate->course_subscription_id = $this->course_course_subscription_id;
  1360. $rate->price = currencyToDouble($this->course_price);
  1361. $rate->date = $start;
  1362. $rate->months = json_encode($mms);
  1363. $rate->note = '';
  1364. $rate->status = 0;
  1365. $rate->save();
  1366. $start = date('Y-m-d', strtotime("+" . $r->months . " months", strtotime($start)));
  1367. if ($start > $this->course_date_to) {
  1368. $go = false;
  1369. break;
  1370. }
  1371. }
  1372. session()->flash('success, Corso creato');
  1373. $this->resetCourseFields();
  1374. $this->addCourse = false;
  1375. $this->emit('setEditCorso', false);
  1376. } catch (\Exception $ex) {
  1377. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1378. }
  1379. }
  1380. public function editCourse($id)
  1381. {
  1382. try {
  1383. $memberCourse = \App\Models\MemberCourse::findOrFail($id);
  1384. if (!$memberCourse) {
  1385. session()->flash('error', 'Corso non trovato');
  1386. } else {
  1387. $this->course_course_id = $memberCourse->course_id;
  1388. // Carico i dati relativi alla struttura ad albero
  1389. $c = \App\Models\Course::findOrFail($this->course_course_id);
  1390. $this->course_name = $c->name . " (" . $c->year . ")";
  1391. $this->course_level_id = $c->course_level_id;
  1392. $this->course_course_type = $c->type;
  1393. $this->course_type_id = $c->course_type_id;
  1394. $this->course_frequency_id = $c->course_frequency_id;
  1395. $all = \App\Models\Course::where('name', 'like', '%' . $c->name . "%")->where('enabled', true)->get();
  1396. foreach ($all as $a) {
  1397. $levels_ids[] = $a->course_level_id;
  1398. }
  1399. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->whereIn('id', $levels_ids)->get();
  1400. $all = \App\Models\Course::where('name', 'like', '%' . $c->name . "%")->where('enabled', true)->where('course_level_id', $this->course_level_id)->get();
  1401. foreach ($all as $a) {
  1402. $types_ids[] = $a->course_type_id;
  1403. }
  1404. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->whereIn('id', $types_ids)->get();
  1405. $frequencies_ids = [];
  1406. $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();
  1407. foreach ($all as $a) {
  1408. $frequencies_ids[] = $a->course_frequency_id;
  1409. }
  1410. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->whereIn('id', $frequencies_ids)->get();
  1411. $this->course_when = array();
  1412. $this->course_date_from = $memberCourse->date_from;
  1413. $this->course_date_to = $memberCourse->date_to;
  1414. foreach (json_decode($memberCourse->when) as $z) {
  1415. $this->course_when[] = array("day" => $z->day, "from" => $z->from, "to" => $z->to);
  1416. }
  1417. //$this->course_when = json_decode($memberCourse->when);
  1418. $this->course_course_subscription_id = $memberCourse->course_subscription_id;
  1419. $this->course_status = $memberCourse->status;
  1420. $this->course_subscribed = $memberCourse->subscribed == 1 ? true : false;
  1421. $this->course_price = formatPrice($memberCourse->price);
  1422. $this->course_subscription_price = formatPrice($memberCourse->subscription_price);
  1423. $this->course_note = $memberCourse->notes;
  1424. foreach (json_decode($memberCourse->months) as $z) {
  1425. $this->course_months[] = array("m" => $z->m, "status" => $z->status);
  1426. }
  1427. $this->courseDataId = $memberCourse->id;
  1428. $this->updateCourse = true;
  1429. $this->addCourse = false;
  1430. $this->emit('setEditCorso', true);
  1431. }
  1432. } catch (\Exception $ex) {
  1433. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1434. }
  1435. }
  1436. public function updateCourse()
  1437. {
  1438. //$this->validate();
  1439. $this->validate(['course_course_id' => 'required']);
  1440. try {
  1441. \App\Models\MemberCourse::whereId($this->courseDataId)->update([
  1442. 'member_id' => $this->dataId,
  1443. 'course_id' => $this->course_course_id,
  1444. 'date_from' => $this->course_date_from,
  1445. 'date_to' => $this->course_date_to,
  1446. 'course_subscription_id' => $this->course_course_subscription_id,
  1447. 'status' => $this->course_status,
  1448. 'price' => currencyToDouble($this->course_price),
  1449. 'subscription_price' => currencyToDouble($this->course_subscription_price),
  1450. 'notes' => $this->course_note,
  1451. 'months' => json_encode($this->course_months),
  1452. 'when' => json_encode($this->course_when)
  1453. ]);
  1454. session()->flash('success', 'Corso aggiornato');
  1455. $this->resetCourseFields();
  1456. $this->updateCourse = false;
  1457. $this->emit('setEditCorso', false);
  1458. } catch (\Exception $ex) {
  1459. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1460. }
  1461. }
  1462. public function cancelCourse()
  1463. {
  1464. $this->addCourse = false;
  1465. $this->updateCourse = false;
  1466. $this->emit('setEditCorso', false);
  1467. $this->resetCourseFields();
  1468. }
  1469. public function deleteCourse($id)
  1470. {
  1471. try {
  1472. \App\Models\MemberCourse::find($id)->delete();
  1473. session()->flash('success', "Corso eliminato");
  1474. } catch (\Exception $e) {
  1475. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  1476. }
  1477. }
  1478. public function addRow()
  1479. {
  1480. $this->course_when[] = array('day' => array(), 'from' => '', 'to' => '');
  1481. }
  1482. public function delRow($idx)
  1483. {
  1484. unset($this->course_when[$idx]);
  1485. }
  1486. // Certificates
  1487. public function addCertificate()
  1488. {
  1489. $this->resetCertificateFields();
  1490. $this->addCertificate = true;
  1491. $this->updateCertificate = false;
  1492. }
  1493. public function storeCertificate()
  1494. {
  1495. $this->validate(['certificate_expire_date' => 'required']);
  1496. // $this->validate();
  1497. try {
  1498. $name = '';
  1499. try {
  1500. if ($this->certificate_filename) {
  1501. $name = md5($this->certificate_filename . microtime()) . '.' . $this->certificate_filename->extension();
  1502. $this->certificate_filename->storeAs('public', $name);
  1503. }
  1504. } catch (\Exception $ex) {
  1505. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1506. }
  1507. if ($this->dataId > -1) {
  1508. \App\Models\MemberCertificate::create([
  1509. 'member_id' => $this->dataId,
  1510. 'type' => $this->certificate_type,
  1511. 'filename' => $name,
  1512. 'expire_date' => $this->certificate_expire_date,
  1513. 'status' => $this->certificate_status
  1514. ]);
  1515. updateMemberData($this->dataId);
  1516. }
  1517. /*else
  1518. {
  1519. $this->certificateTmp = new \App\Models\MemberCertificate();
  1520. $this->certificateTmp->type = $this->certificate_type;
  1521. $this->certificateTmp->filename = $name;
  1522. $this->certificateTmp->expire_date = $this->certificate_expire_date;
  1523. $this->certificateTmp->status = $this->certificate_status;
  1524. $this->certificateTmp->status = $this->certificate_status;
  1525. // s $this->member_certificates[] = $certificateTmp;
  1526. }*/
  1527. session()->flash('success, Tesserato creato');
  1528. $this->resetCertificateFields();
  1529. $this->addCertificate = false;
  1530. } catch (\Exception $ex) {
  1531. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1532. }
  1533. }
  1534. public function editCertificate($id)
  1535. {
  1536. try {
  1537. $memberCertificate = \App\Models\MemberCertificate::findOrFail($id);
  1538. if (!$memberCertificate) {
  1539. session()->flash('error', 'Tesserato non trovato');
  1540. } else {
  1541. $this->certificate_type = $memberCertificate->type;
  1542. $this->certificate_filename_old = $memberCertificate->filename;
  1543. $this->certificate_expire_date = $memberCertificate->expire_date;
  1544. $this->certificate_status = $memberCertificate->status;
  1545. $this->cardCertificateId = $memberCertificate->id;
  1546. $this->updateCertificate = true;
  1547. $this->addCertificate = false;
  1548. }
  1549. } catch (\Exception $ex) {
  1550. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1551. }
  1552. }
  1553. public function updateCertificate()
  1554. {
  1555. $this->validate(['certificate_expire_date' => 'required']);
  1556. try {
  1557. $name = '';
  1558. try {
  1559. if ($this->certificate_filename) {
  1560. $name = md5($this->certificate_filename . microtime()) . '.' . $this->certificate_filename->extension();
  1561. $this->certificate_filename->storeAs('public', $name);
  1562. }
  1563. } catch (\Exception $ex) {
  1564. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1565. }
  1566. \App\Models\MemberCertificate::whereId($this->cardCertificateId)->update([
  1567. 'member_id' => $this->dataId,
  1568. 'type' => $this->certificate_type,
  1569. 'filename' => $name != '' ? $name : $this->certificate_filename_old,
  1570. 'expire_date' => $this->certificate_expire_date,
  1571. 'status' => $this->certificate_status
  1572. ]);
  1573. updateMemberData($this->dataId);
  1574. session()->flash('success', 'Tesserato aggiornato');
  1575. $this->resetCertificateFields();
  1576. $this->updateCertificate = false;
  1577. } catch (\Exception $ex) {
  1578. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1579. }
  1580. }
  1581. public function cancelCertificate()
  1582. {
  1583. $this->addCertificate = false;
  1584. $this->updateCertificate = false;
  1585. $this->resetCertificateFields();
  1586. }
  1587. public function deleteCertificate($id)
  1588. {
  1589. try {
  1590. \App\Models\MemberCertificate::find($id)->delete();
  1591. session()->flash('success', "Tesserato eliminato");
  1592. } catch (\Exception $e) {
  1593. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  1594. }
  1595. }
  1596. // Gruppi di appartenenza
  1597. public function storeCategory()
  1598. {
  1599. $this->validate(['category_category_id' => 'required']);
  1600. try {
  1601. \App\Models\MemberCategory::create([
  1602. 'member_id' => $this->dataId,
  1603. 'category_id' => $this->category_category_id,
  1604. 'date' => \Carbon\Carbon::now()
  1605. ]);
  1606. session()->flash('success, Associazione creato');
  1607. $this->resetCategoryFields();
  1608. $this->addCard = false;
  1609. } catch (\Exception $ex) {
  1610. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1611. }
  1612. }
  1613. public function hideMsg()
  1614. {
  1615. $this->groupMsg = '';
  1616. }
  1617. public function storeCategoryWithID($id)
  1618. {
  1619. $this->groupMsg = '';
  1620. try {
  1621. if (\App\Models\MemberCategory::where('member_id', $this->dataId)->where('category_id', $id)->first()) {
  1622. $this->groupMsg = '<br>Attenzione, questo corso è stato già inserito';
  1623. } else {
  1624. \App\Models\MemberCategory::create([
  1625. 'member_id' => $this->dataId,
  1626. 'category_id' => $id,
  1627. 'date' => \Carbon\Carbon::now()
  1628. ]);
  1629. session()->flash('success, Associazione creato');
  1630. $this->resetCategoryFields();
  1631. $this->addCard = false;
  1632. }
  1633. } catch (\Exception $ex) {
  1634. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1635. }
  1636. }
  1637. public function deleteCategory($id)
  1638. {
  1639. try {
  1640. \App\Models\MemberCategory::find($id)->delete();
  1641. session()->flash('success', "Associazione eliminata");
  1642. } catch (\Exception $e) {
  1643. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  1644. }
  1645. }
  1646. public function setCourse($id)
  1647. {
  1648. $this->course_course_id = $id;
  1649. }
  1650. public function getNation($nation)
  1651. {
  1652. if ($nation > 0) {
  1653. $ret = \App\Models\Nation::findOrFail($nation);
  1654. return $ret->name;
  1655. }
  1656. return "";
  1657. }
  1658. public function getProvince($province)
  1659. {
  1660. if ($province > 0) {
  1661. $ret = \App\Models\Province::findOrFail($province);
  1662. return $ret->name;
  1663. }
  1664. return "";
  1665. }
  1666. public function getCity($city)
  1667. {
  1668. if ($city > 0) {
  1669. $ret = \App\Models\City::findOrFail($city);
  1670. return $ret->name;
  1671. }
  1672. return "";
  1673. }
  1674. public function getFiscalCode()
  1675. {
  1676. $this->error_fc = false;
  1677. $cf = new codicefiscale();
  1678. $cf->setDateSeparator('-');
  1679. if ($this->first_name != '' && $this->last_name != '' && $this->birth_date != '' && $this->gender != '' && $this->birth_city_id > 0) {
  1680. $code = '';
  1681. if ($this->birth_city_id > 0) {
  1682. $code = \App\Models\City::findOrFail($this->birth_city_id)->code;
  1683. }
  1684. $codice = $cf->calcola($this->first_name, $this->last_name, $this->birth_date, $this->gender, $code);
  1685. $this->fiscal_code = $codice;
  1686. } else
  1687. $this->error_fc = true;
  1688. }
  1689. public function setMonth($m)
  1690. {
  1691. $exist = -1;
  1692. foreach ($this->course_months as $idx => $x) {
  1693. if ($x["m"] == $m) {
  1694. $exist = $idx;
  1695. break;
  1696. }
  1697. }
  1698. if ($exist > -1) {
  1699. array_splice($this->course_months, $exist, 1);
  1700. } else {
  1701. $this->course_months[] = array("m" => $m, "status" => "");
  1702. }
  1703. /*
  1704. if (in_array($m, $this->course_months))
  1705. {
  1706. $i = array_search($m, $this->course_months);
  1707. array_splice($this->course_months, $i, 1);
  1708. }
  1709. else
  1710. {
  1711. $this->course_months[] = $m;
  1712. }*/
  1713. }
  1714. public function newPayment($course)
  1715. {
  1716. $c = \App\Models\Course::findOrFail($course["course_id"]);
  1717. $price = $course["price"];
  1718. if (sizeof($this->payMonths) == 1) {
  1719. $month = $this->payMonths[0];
  1720. $records = \App\Models\Record::where('member_course_id', $this->selectedCourseMember)->where('deleted', 0)->get();
  1721. foreach ($records as $record) {
  1722. if (in_array($month, json_decode($record->months))) {
  1723. foreach ($record->rows as $row) {
  1724. if ($row->causal_id == $c->causal_id && !str_contains(strtolower($row->note), 'iscrizione')) {
  1725. $tot = sizeof(json_decode($row->when));
  1726. foreach (json_decode($row->when) as $m) {
  1727. $price -= $row->amount / $tot;
  1728. }
  1729. }
  1730. }
  1731. }
  1732. }
  1733. }
  1734. $this->emit('setEdit', false);
  1735. $this->emit('setEditCorso', false);
  1736. 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"]);
  1737. }
  1738. public function block($course)
  1739. {
  1740. $xxx = json_decode($course["months"]);
  1741. foreach ($xxx as $idx => $mm) {
  1742. if (in_array($mm->m, $this->payMonths)) {
  1743. $xxx[$idx]->status = 2;
  1744. }
  1745. }
  1746. $c = \App\Models\MemberCourse::findOrFail($course["id"]);
  1747. $c->months = json_encode($xxx);
  1748. $c->save();
  1749. $this->payMonths = array();
  1750. }
  1751. public function reactivate($course)
  1752. {
  1753. $xxx = json_decode($course["months"]);
  1754. foreach ($xxx as $idx => $mm) {
  1755. if (in_array($mm->m, $this->suspendedMonths)) {
  1756. $xxx[$idx]->status = "";
  1757. }
  1758. }
  1759. $c = \App\Models\MemberCourse::findOrFail($course["id"]);
  1760. $c->months = json_encode($xxx);
  1761. $c->save();
  1762. $this->suspendedMonths = array();
  1763. }
  1764. public function setPayMonth($m, $months, $selectedCourseMember)
  1765. {
  1766. if ($this->selectedCourseMember != $selectedCourseMember)
  1767. $this->payMonths = array();
  1768. $this->selectedCourseMember = $selectedCourseMember;
  1769. if (in_array($m, $this->payMonths)) {
  1770. $i = array_search($m, $this->payMonths);
  1771. array_splice($this->payMonths, $i, 1);
  1772. } else if (in_array($m, $this->suspendedMonths)) {
  1773. $i = array_search($m, $this->suspendedMonths);
  1774. array_splice($this->suspendedMonths, $i, 1);
  1775. } else {
  1776. foreach (json_decode($months) as $mm) {
  1777. if ($mm->m == $m) {
  1778. if ($mm->status == "")
  1779. $this->payMonths[] = $m;
  1780. if ($mm->status == "1") {
  1781. $mc = \App\Models\MemberCourse::findOrFail($selectedCourseMember);
  1782. $price = $mc->price;
  1783. $payed = 0;
  1784. $extraC = '';
  1785. $recordsPayed = \App\Models\Record::where('member_course_id', $selectedCourseMember)->where('deleted', 0)->get();
  1786. foreach ($recordsPayed as $record) {
  1787. if (in_array($m, json_decode($record->months))) {
  1788. foreach ($record->rows as $row) {
  1789. if ($row->causal_id == $mc->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione')) {
  1790. $tot = sizeof(json_decode($row->when));
  1791. $payed += $row->amount / $tot;
  1792. }
  1793. }
  1794. if ($payed < $price)
  1795. $this->payMonths[] = $m;
  1796. }
  1797. }
  1798. }
  1799. if ($mm->status == "2")
  1800. $this->suspendedMonths[] = $m;
  1801. }
  1802. }
  1803. }
  1804. if (sizeof($this->payMonths) > 0 && sizeof($this->suspendedMonths) > 0) {
  1805. $this->payMonths = array();
  1806. $this->suspendedMonths = array();
  1807. }
  1808. }
  1809. public function setCreateSubscription($subscribed, $selectedCourseMember)
  1810. {
  1811. if ($this->selectedCourseMember != $selectedCourseMember)
  1812. $this->payMonths = array();
  1813. $this->selectedCourseMember = $selectedCourseMember;
  1814. if (!$subscribed)
  1815. $this->createSubscription = !$this->createSubscription;
  1816. }
  1817. public function showHideCourse($id)
  1818. {
  1819. if (in_array($id, $this->showCourse)) {
  1820. $i = array_search($id, $this->showCourse);
  1821. array_splice($this->showCourse, $i, 1);
  1822. } else {
  1823. $this->showCourse[] = $id;
  1824. }
  1825. }
  1826. public function getMonthStatus($m, $months, $selectedCourseMember)
  1827. {
  1828. $class = "grey";
  1829. foreach (json_decode($months) as $mm) {
  1830. if ($mm->m == $m) {
  1831. if ($mm->status == "") {
  1832. if (in_array($m, $this->payMonths) && $this->selectedCourseMember == $selectedCourseMember)
  1833. $class = "blue";
  1834. else
  1835. $class = "orange";
  1836. }
  1837. if ($mm->status == "1") {
  1838. $class = "green";
  1839. }
  1840. if ($mm->status == "2") {
  1841. if (in_array($m, $this->suspendedMonths) && $this->selectedCourseMember == $selectedCourseMember)
  1842. $class = "blue";
  1843. else
  1844. $class = "yellow";
  1845. }
  1846. if ($class == 'green') {
  1847. $mc = \App\Models\MemberCourse::findOrFail($selectedCourseMember);
  1848. $price = $mc->price;
  1849. $payed = 0;
  1850. $extraC = '';
  1851. $recordsPayed = \App\Models\Record::where('member_course_id', $selectedCourseMember)->where('deleted', 0)->get();
  1852. foreach ($recordsPayed as $record) {
  1853. if (in_array($m, json_decode($record->months))) {
  1854. foreach ($record->rows as $row) {
  1855. if ($row->causal_id == $mc->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione')) {
  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. }