Member.php 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723
  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 = date('Y-m-d', strtotime('+1 year'));
  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)->orderBy('name')->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. 'address' => 'required',
  942. 'zip_code' => 'required',
  943. 'nation_id' => 'required',
  944. 'birth_date' => 'before_or_equal:today'
  945. ];
  946. if ($this->isBirthItaly) {
  947. $rules['birth_province_id'] = 'required';
  948. $rules['birth_city_id'] = 'required';
  949. $rules['fiscal_code'] = 'required';
  950. } else {
  951. $rules['birth_place'] = 'required';
  952. }
  953. Log::info('isItaly', ['isItaly' => $this->isItaly]);
  954. $this->isItaly = $this->checkIsItaly();
  955. if ($this->isItaly) {
  956. $rules['province_id'] = 'required';
  957. $rules['city_id'] = 'required';
  958. }
  959. $zzz = [];
  960. if ($this->birth_nation_id == null)
  961. $zzz[] = 'birth_nation_id=' . $this->birth_nation_id;
  962. if ($this->isBirthItaly) {
  963. if ($this->birth_province_id == null)
  964. $zzz[] = 'birth_province_id=' . $this->birth_province_id;
  965. if ($this->birth_city_id == null)
  966. $zzz[] = 'birth_city_id=' . $this->birth_city_id;
  967. }
  968. if ($this->nation_id == null)
  969. $zzz[] = 'nation_id=' . $this->nation_id;
  970. if ($this->isItaly) {
  971. if ($this->province_id == null)
  972. $zzz[] = 'province_id=' . $this->province_id;
  973. if ($this->city_id == null)
  974. $zzz[] = 'city_id=' . $this->city_id;
  975. }
  976. $this->emit('setErrorMsg', $zzz);
  977. if ($this->under18) {
  978. $rules['father_name'] = 'required_without:mother_name';
  979. $rules['father_email'] = 'required_without:mother_email|email';
  980. $rules['father_fiscal_code'] = 'required_without:mother_fiscal_code';
  981. $rules['mother_name'] = 'required_without:father_name';
  982. $rules['mother_email'] = 'required_without:father_email|email';
  983. $rules['mother_fiscal_code'] = 'required_without:father_fiscal_code';
  984. }
  985. try {
  986. $this->validate($rules);
  987. } catch (\Illuminate\Validation\ValidationException $e) {
  988. $this->isSaving = false;
  989. $errorFields = array_keys($e->errors());
  990. if (!empty($errorFields)) {
  991. $this->emit('focus-error-field', $errorFields[0]);
  992. }
  993. Log::error('Validation failed', ['errors' => $e->errors()]);
  994. return;
  995. }
  996. try {
  997. $name = '';
  998. if ($this->image) {
  999. $name = md5($this->image . microtime()) . '.' . $this->image->extension();
  1000. $this->image->storeAs('public', $name);
  1001. }
  1002. $docs = implode("|", $this->document_files);
  1003. $father_docs = implode("|", $this->father_document_files);
  1004. $mother_docs = implode("|", $this->mother_document_files);
  1005. \App\Models\Member::whereId($this->dataId)->update([
  1006. 'first_name' => strtoupper($this->first_name),
  1007. 'last_name' => strtoupper($this->last_name),
  1008. 'status' => $this->status,
  1009. 'birth_city_id' => $this->birth_city_id > 0 ? $this->birth_city_id : null,
  1010. 'birth_province_id' => $this->birth_province_id > 0 ? $this->birth_province_id : null,
  1011. 'birth_nation_id' => $this->birth_nation_id > 0 ? $this->birth_nation_id : null,
  1012. 'birth_date' => $this->birth_date,
  1013. 'birth_place' => $this->birth_place,
  1014. 'father_name' => $this->father_name,
  1015. 'mother_name' => $this->mother_name,
  1016. 'father_email' => strtolower($this->father_email),
  1017. 'mother_email' => strtolower($this->mother_email),
  1018. 'father_phone' => $this->father_phone,
  1019. 'mother_phone' => $this->mother_phone,
  1020. 'father_fiscal_code' => $this->father_fiscal_code,
  1021. 'mother_fiscal_code' => $this->mother_fiscal_code,
  1022. 'father_doc_number' => $this->father_doc_number,
  1023. 'father_doc_type' => $this->father_doc_type,
  1024. 'mother_doc_number' => $this->mother_doc_number,
  1025. 'mother_doc_type' => $this->mother_doc_type,
  1026. 'father_document_files' => $father_docs,
  1027. 'mother_document_files' => $mother_docs,
  1028. 'document_type' => $this->document_type,
  1029. 'document_number' => $this->document_number,
  1030. 'document_from' => $this->document_from,
  1031. 'document_expire_date' => $this->document_expire_date,
  1032. 'document_files' => $docs,
  1033. 'gender' => $this->gender,
  1034. 'no_send_mail' => $this->no_send_mail,
  1035. 'exclude_from_records' => $this->exclude_from_records,
  1036. 'fiscal_code' => $this->fiscal_code,
  1037. 'address' => $this->address,
  1038. 'zip_code' => $this->zip_code,
  1039. 'nation_id' => $this->nation_id > 0 ? $this->nation_id : null,
  1040. 'province_id' => $this->province_id > 0 ? $this->province_id : null,
  1041. 'city_id' => $this->city_id > 0 ? $this->city_id : null,
  1042. 'image' => $name != '' ? $name : $this->image_old,
  1043. 'phone' => $this->phone,
  1044. 'phone2' => $this->phone2,
  1045. 'phone3' => $this->phone3,
  1046. 'email' => strtolower($this->email),
  1047. 'to_complete' => false,
  1048. 'enabled' => $this->enabled
  1049. ]);
  1050. updateMemberData($this->dataId);
  1051. session()->flash('success', 'Tesserato aggiornato');
  1052. if ($close) {
  1053. $this->resetFields();
  1054. $this->update = false;
  1055. }
  1056. $this->emit('setEdit', false);
  1057. } catch (\Exception $ex) {
  1058. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1059. }
  1060. }
  1061. public function cancel()
  1062. {
  1063. $this->add = false;
  1064. $this->update = false;
  1065. $this->showDetail = false;
  1066. $this->resetFields();
  1067. $this->emit('setEdit', false);
  1068. //$this->emit('setEditCorso', false);
  1069. $this->emit('reload');
  1070. }
  1071. public function delete($id)
  1072. {
  1073. try {
  1074. \App\Models\Member::find($id)->delete();
  1075. session()->flash('success', "Tesserato eliminato");
  1076. $this->emit('reload');
  1077. } catch (\Exception $e) {
  1078. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  1079. }
  1080. }
  1081. public function checkUncheckAll()
  1082. {
  1083. if (!$this->checkedAll) {
  1084. $this->multipleIds = array();
  1085. } else {
  1086. foreach ($this->records as $r) {
  1087. $this->multipleIds[] = $r->id;
  1088. }
  1089. }
  1090. }
  1091. public function multipleDelete()
  1092. {
  1093. try {
  1094. foreach ($this->multipleIds as $id) {
  1095. \App\Models\Member::find($id)->delete();
  1096. }
  1097. } catch (\Exception $e) {
  1098. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  1099. }
  1100. $this->multipleAction = '';
  1101. }
  1102. // Card
  1103. public function addCard()
  1104. {
  1105. $this->resetCardFields();
  1106. $this->addCard = true;
  1107. $this->updateCard = false;
  1108. }
  1109. public function storeCard()
  1110. {
  1111. $this->validate(['card_card_id' => 'required']);
  1112. try {
  1113. // in base alla card selezionata calcolo la scadenza
  1114. $expire_date = null;
  1115. if ($this->card_date != '') {
  1116. $y = date("Y", strtotime($this->card_date));
  1117. $card = \App\Models\Card::findOrFail($this->card_card_id);
  1118. if ($card->next_day_expire > 0 && $card->next_month_expire > 0) {
  1119. $m = strlen($card->next_month_expire) == 1 ? ('0' . $card->next_month_expire) : $card->next_month_expire;
  1120. if (date("md", strtotime($this->card_date)) > ($m . $card->next_day_expire))
  1121. $y += 1;
  1122. $next_exp = date($y . "-" . $m . "-" . $card->next_day_expire);
  1123. if ($next_exp > $this->card_date) {
  1124. $expire_date = $next_exp;
  1125. } else
  1126. $expire_date = date($y . "-" . $card->next_month_expire . "-" . $card->next_day_expire, strtotime(' + 1 years'));
  1127. } else {
  1128. if ($card->one_year_expire) {
  1129. $expire_date = date("Y-m-d", strtotime($this->card_date . ' + 1 years'));
  1130. }
  1131. }
  1132. }
  1133. \App\Models\MemberCard::create([
  1134. 'member_id' => $this->dataId,
  1135. 'card_id' => $this->card_card_id,
  1136. 'number' => $this->card_number,
  1137. 'date' => $this->card_date,
  1138. 'accept_date' => $this->card_accept_date != '' ? $this->card_accept_date : $this->card_date,
  1139. 'expire_date' => $expire_date,
  1140. 'status' => $this->card_status,
  1141. 'discipline1_id' => $this->card_discipline1_id,
  1142. 'discipline2_id' => $this->card_discipline2_id,
  1143. 'discipline3_id' => $this->card_discipline3_id,
  1144. ]);
  1145. updateMemberData($this->dataId);
  1146. session()->flash('success, Tesserato creato');
  1147. $this->resetCardFields();
  1148. $this->addCard = false;
  1149. } catch (\Exception $ex) {
  1150. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1151. }
  1152. }
  1153. public function editCard($id)
  1154. {
  1155. try {
  1156. $memberCard = \App\Models\MemberCard::findOrFail($id);
  1157. if (!$memberCard) {
  1158. session()->flash('error', 'Tesserato non trovato');
  1159. } else {
  1160. $this->card_card_id = $memberCard->card_id;
  1161. $this->card_number = $memberCard->number;
  1162. $this->card_date = $memberCard->date;
  1163. $this->card_accept_date = $memberCard->accept_date;
  1164. $this->card_status = $memberCard->status;
  1165. $this->card_discipline1_id = $memberCard->discipline1_id;
  1166. $this->card_discipline2_id = $memberCard->discipline2_id;
  1167. $this->card_discipline3_id = $memberCard->discipline3_id;
  1168. $this->cardDataId = $memberCard->id;
  1169. $this->updateCard = true;
  1170. $this->addCard = false;
  1171. }
  1172. } catch (\Exception $ex) {
  1173. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1174. }
  1175. }
  1176. public function updateCard()
  1177. {
  1178. $this->validate(['card_card_id' => 'required']);
  1179. try {
  1180. \Illuminate\Support\Facades\Log::info('Starting card update', [
  1181. 'member_id' => $this->dataId,
  1182. 'card_id' => $this->card_card_id,
  1183. 'card_number' => $this->card_number
  1184. ]);
  1185. $expire_date = null;
  1186. if ($this->card_date != '') {
  1187. $card = \App\Models\Card::findOrFail($this->card_card_id);
  1188. \Illuminate\Support\Facades\Log::info('Card details', [
  1189. 'card_id' => $card->id,
  1190. 'next_day_expire' => $card->next_day_expire,
  1191. 'next_month_expire' => $card->next_month_expire,
  1192. 'one_year_expire' => $card->one_year_expire
  1193. ]);
  1194. if ($card->next_day_expire > 0 && $card->next_month_expire > 0) {
  1195. $y = date("Y", strtotime($this->card_date));
  1196. $m = strlen($card->next_month_expire) == 1 ? ('0' . $card->next_month_expire) : $card->next_month_expire;
  1197. $d = strlen($card->next_day_expire) == 1 ? ('0' . $card->next_day_expire) : $card->next_day_expire;
  1198. $next_exp = $y . "-" . $m . "-" . $d;
  1199. // Format dates for comparison
  1200. $input_date_obj = new \DateTime($this->card_date);
  1201. $next_exp_obj = new \DateTime($next_exp);
  1202. if ($next_exp_obj > $input_date_obj) {
  1203. $expire_date = $next_exp;
  1204. } else {
  1205. // Add 1 year to the expiration date if the input date is after the expiration date
  1206. $next_exp_obj->modify('+1 year');
  1207. $expire_date = $next_exp_obj->format('Y-m-d');
  1208. }
  1209. \Illuminate\Support\Facades\Log::info('Calculated expiration date (next_day_expire/next_month_expire rule)', [
  1210. 'input_date' => $this->card_date,
  1211. 'next_exp' => $next_exp,
  1212. 'expire_date' => $expire_date,
  1213. 'comparison' => ($next_exp_obj > $input_date_obj) ? 'next_exp is after input_date' : 'next_exp is before or equal to input_date'
  1214. ]);
  1215. } else {
  1216. if ($card->one_year_expire) {
  1217. $expire_date = date("Y-m-d", strtotime($this->card_date . ' + 1 years'));
  1218. \Illuminate\Support\Facades\Log::info('Calculated expiration date (one_year_expire rule)', [
  1219. 'input_date' => $this->card_date,
  1220. 'expire_date' => $expire_date
  1221. ]);
  1222. }
  1223. }
  1224. }
  1225. \Illuminate\Support\Facades\Log::info('Updating member card', [
  1226. 'card_id' => $this->cardDataId,
  1227. 'member_id' => $this->dataId,
  1228. 'card_number' => $this->card_number,
  1229. 'date' => $this->card_date,
  1230. 'expire_date' => $expire_date,
  1231. 'status' => $this->card_status
  1232. ]);
  1233. \App\Models\MemberCard::whereId($this->cardDataId)->update([
  1234. 'member_id' => $this->dataId,
  1235. 'card_id' => $this->card_card_id,
  1236. 'number' => $this->card_number,
  1237. 'date' => $this->card_date,
  1238. 'accept_date' => $this->card_accept_date != '' ? $this->card_accept_date : $this->card_date,
  1239. 'expire_date' => $expire_date,
  1240. 'status' => $this->card_status,
  1241. 'discipline1_id' => $this->card_discipline1_id,
  1242. 'discipline2_id' => $this->card_discipline2_id,
  1243. 'discipline3_id' => $this->card_discipline3_id,
  1244. ]);
  1245. updateMemberData($this->dataId);
  1246. \Illuminate\Support\Facades\Log::info('Card updated successfully', [
  1247. 'card_id' => $this->cardDataId,
  1248. 'member_id' => $this->dataId
  1249. ]);
  1250. session()->flash('success', 'Tesserato aggiornato');
  1251. $this->resetCardFields();
  1252. $this->updateCard = false;
  1253. } catch (\Exception $ex) {
  1254. \Illuminate\Support\Facades\Log::error('Error updating card', [
  1255. 'card_id' => $this->cardDataId,
  1256. 'member_id' => $this->dataId,
  1257. 'error_message' => $ex->getMessage(),
  1258. 'error_trace' => $ex->getTraceAsString()
  1259. ]);
  1260. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1261. }
  1262. }
  1263. public function cancelCard()
  1264. {
  1265. $this->addCard = false;
  1266. $this->updateCard = false;
  1267. $this->resetCardFields();
  1268. }
  1269. public function deleteCard($id)
  1270. {
  1271. try {
  1272. \App\Models\MemberCard::find($id)->delete();
  1273. session()->flash('success', "Tesserato eliminato");
  1274. } catch (\Exception $e) {
  1275. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  1276. }
  1277. }
  1278. // Courses
  1279. public function addCourse()
  1280. {
  1281. $this->resetCourseFields();
  1282. /*if ($this->under18)
  1283. {
  1284. $this->course_months[] = array("m" => 9, "status" => "");
  1285. $this->course_months[] = array("m" => 10, "status" => "");
  1286. $this->course_months[] = array("m" => 11, "status" => "");
  1287. $this->course_months[] = array("m" => 12, "status" => "");
  1288. $this->course_months[] = array("m" => 1, "status" => "");
  1289. $this->course_months[] = array("m" => 2, "status" => "");
  1290. $this->course_months[] = array("m" => 3, "status" => "");
  1291. $this->course_months[] = array("m" => 4, "status" => "");
  1292. $this->course_months[] = array("m" => 5, "status" => "");
  1293. $this->course_months[] = array("m" => 6, "status" => "");
  1294. }*/
  1295. $this->addCourse = true;
  1296. $this->updateCourse = false;
  1297. $this->emit('setEditCorso', true);
  1298. }
  1299. public function storeCourse()
  1300. {
  1301. $this->validate(['course_course_id' => 'required']);
  1302. try {
  1303. $mc = new \App\Models\MemberCourse();
  1304. $mc->member_id = $this->dataId;
  1305. $mc->course_id = $this->course_course_id;
  1306. $mc->date_from = $this->course_date_from;
  1307. $mc->date_to = $this->course_date_to;
  1308. $mc->course_subscription_id = $this->course_course_subscription_id;
  1309. $mc->status = $this->course_status;
  1310. $mc->subscribed = false;
  1311. $mc->price = currencyToDouble($this->course_price);
  1312. $mc->subscription_price = currencyToDouble($this->course_subscription_price);
  1313. $mc->notes = $this->course_note;
  1314. $mc->months = json_encode($this->course_months);
  1315. $mc->when = json_encode($this->course_when);
  1316. $mc->save();
  1317. // Se il corso ha associato una categoria iscrivo anche al gruppo
  1318. $c = \App\Models\Course::findOrFail($this->course_course_id);
  1319. if ($c) {
  1320. if ($c->category_id > 0) {
  1321. \App\Models\MemberCategory::create([
  1322. 'member_id' => $this->dataId,
  1323. 'category_id' => $c->category_id,
  1324. 'date' => \Carbon\Carbon::now()
  1325. ]);
  1326. }
  1327. }
  1328. // Creo i pagamenti in base alla tipologia
  1329. $r = \App\Models\CourseSubscription::findOrFail($this->course_course_subscription_id);
  1330. $start = $this->course_date_from;
  1331. // Creo il pagamento per l'iscrizione
  1332. $rate = new \App\Models\Rate();
  1333. $rate->member_id = $this->dataId;
  1334. $rate->member_course_id = $mc->id;
  1335. $rate->course_subscription_id = null;
  1336. $rate->price = currencyToDouble($this->course_subscription_price);
  1337. $rate->date = $start;
  1338. $rate->months = json_encode([]);
  1339. $rate->note = '';
  1340. $rate->status = 0;
  1341. $rate->is_subscription = true;
  1342. $rate->save();
  1343. // Se la data inizio pagamenti è "inizio corso" allora prendo quella come start date
  1344. /*if ($r->when_start == 'COURSE')
  1345. $start = $c->date_from;
  1346. //
  1347. $month_day = $r->month_day;
  1348. if ($month_day > 0)
  1349. {
  1350. if ($month_day >= 29)
  1351. $start = date("Y-m-t", strtotime($start));
  1352. else
  1353. $start = date('Y-m-d', strtotime('+' . ($month_day - 1) . ' days', strtotime('first day of ' . ((int)date('j', strtotime($start)) < $month_day ? 'this' : 'next' ) . ' month', strtotime($start))));
  1354. }
  1355. if ($r->months > 1)
  1356. $start = date('Y-m-' . ($month_day >= 29 ? 't' : 'd'), strtotime("+" . ($r->months - 1) . " months", strtotime($start)));
  1357. */
  1358. $go = true;
  1359. while ($go) {
  1360. $mms = [];
  1361. $mms[] = date("n", strtotime($start));
  1362. for ($jj = 1; $jj < $r->months; $jj++) {
  1363. if (date('Ymd', strtotime("+" . $jj . " months", strtotime($start))) < date("Ymd", strtotime($this->course_date_to)))
  1364. {
  1365. $mms[] = date('n', strtotime("+" . $jj . " months", strtotime($start)));
  1366. }
  1367. else
  1368. {
  1369. break;
  1370. }
  1371. }
  1372. $rate = new \App\Models\Rate();
  1373. $rate->member_id = $this->dataId;
  1374. $rate->member_course_id = $mc->id;
  1375. $rate->course_subscription_id = $this->course_course_subscription_id;
  1376. $rate->price = currencyToDouble($this->course_price);
  1377. $rate->date = $start;
  1378. $rate->months = json_encode($mms);
  1379. $rate->note = '';
  1380. $rate->status = 0;
  1381. $rate->save();
  1382. $start = date('Y-m-d', strtotime("+" . $r->months . " months", strtotime($start)));
  1383. if ($start > $this->course_date_to) {
  1384. $go = false;
  1385. break;
  1386. }
  1387. }
  1388. session()->flash('success, Corso creato');
  1389. $this->resetCourseFields();
  1390. $this->addCourse = false;
  1391. $this->emit('setEditCorso', false);
  1392. } catch (\Exception $ex) {
  1393. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1394. }
  1395. }
  1396. public function editCourse($id)
  1397. {
  1398. try {
  1399. $memberCourse = \App\Models\MemberCourse::findOrFail($id);
  1400. if (!$memberCourse) {
  1401. session()->flash('error', 'Corso non trovato');
  1402. } else {
  1403. $this->course_course_id = $memberCourse->course_id;
  1404. // Carico i dati relativi alla struttura ad albero
  1405. $c = \App\Models\Course::findOrFail($this->course_course_id);
  1406. $this->course_name = $c->name . " (" . $c->year . ")";
  1407. $this->course_level_id = $c->course_level_id;
  1408. $this->course_course_type = $c->type;
  1409. $this->course_type_id = $c->course_type_id;
  1410. $this->course_frequency_id = $c->course_frequency_id;
  1411. $all = \App\Models\Course::where('name', 'like', '%' . $c->name . "%")->where('enabled', true)->get();
  1412. foreach ($all as $a) {
  1413. $levels_ids[] = $a->course_level_id;
  1414. }
  1415. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->whereIn('id', $levels_ids)->get();
  1416. $all = \App\Models\Course::where('name', 'like', '%' . $c->name . "%")->where('enabled', true)->where('course_level_id', $this->course_level_id)->get();
  1417. foreach ($all as $a) {
  1418. $types_ids[] = $a->course_type_id;
  1419. }
  1420. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->whereIn('id', $types_ids)->get();
  1421. $frequencies_ids = [];
  1422. $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();
  1423. foreach ($all as $a) {
  1424. $frequencies_ids[] = $a->course_frequency_id;
  1425. }
  1426. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->whereIn('id', $frequencies_ids)->get();
  1427. $this->course_when = array();
  1428. $this->course_date_from = $memberCourse->date_from;
  1429. $this->course_date_to = $memberCourse->date_to;
  1430. foreach (json_decode($memberCourse->when) as $z) {
  1431. $this->course_when[] = array("day" => $z->day, "from" => $z->from, "to" => $z->to);
  1432. }
  1433. //$this->course_when = json_decode($memberCourse->when);
  1434. $this->course_course_subscription_id = $memberCourse->course_subscription_id;
  1435. $this->course_status = $memberCourse->status;
  1436. $this->course_subscribed = $memberCourse->subscribed == 1 ? true : false;
  1437. $this->course_price = formatPrice($memberCourse->price);
  1438. $this->course_subscription_price = formatPrice($memberCourse->subscription_price);
  1439. $this->course_note = $memberCourse->notes;
  1440. foreach (json_decode($memberCourse->months) as $z) {
  1441. $this->course_months[] = array("m" => $z->m, "status" => $z->status);
  1442. }
  1443. $this->courseDataId = $memberCourse->id;
  1444. $this->updateCourse = true;
  1445. $this->addCourse = false;
  1446. $this->emit('setEditCorso', true);
  1447. }
  1448. } catch (\Exception $ex) {
  1449. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1450. }
  1451. }
  1452. public function updateCourse()
  1453. {
  1454. //$this->validate();
  1455. $this->validate(['course_course_id' => 'required']);
  1456. try {
  1457. \App\Models\MemberCourse::whereId($this->courseDataId)->update([
  1458. 'member_id' => $this->dataId,
  1459. 'course_id' => $this->course_course_id,
  1460. 'date_from' => $this->course_date_from,
  1461. 'date_to' => $this->course_date_to,
  1462. 'course_subscription_id' => $this->course_course_subscription_id,
  1463. 'status' => $this->course_status,
  1464. 'price' => currencyToDouble($this->course_price),
  1465. 'subscription_price' => currencyToDouble($this->course_subscription_price),
  1466. 'notes' => $this->course_note,
  1467. 'months' => json_encode($this->course_months),
  1468. 'when' => json_encode($this->course_when)
  1469. ]);
  1470. session()->flash('success', 'Corso aggiornato');
  1471. $this->resetCourseFields();
  1472. $this->updateCourse = false;
  1473. $this->emit('setEditCorso', false);
  1474. } catch (\Exception $ex) {
  1475. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1476. }
  1477. }
  1478. public function cancelCourse()
  1479. {
  1480. $this->addCourse = false;
  1481. $this->updateCourse = false;
  1482. $this->emit('setEditCorso', false);
  1483. $this->resetCourseFields();
  1484. }
  1485. public function deleteCourse($id)
  1486. {
  1487. try {
  1488. \App\Models\MemberCourse::find($id)->delete();
  1489. session()->flash('success', "Corso eliminato");
  1490. } catch (\Exception $e) {
  1491. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  1492. }
  1493. }
  1494. public function addRow()
  1495. {
  1496. $this->course_when[] = array('day' => array(), 'from' => '', 'to' => '');
  1497. }
  1498. public function delRow($idx)
  1499. {
  1500. unset($this->course_when[$idx]);
  1501. }
  1502. // Certificates
  1503. public function addCertificate()
  1504. {
  1505. $this->resetCertificateFields();
  1506. $this->addCertificate = true;
  1507. $this->updateCertificate = false;
  1508. }
  1509. public function storeCertificate()
  1510. {
  1511. $this->validate(['certificate_expire_date' => 'required']);
  1512. // $this->validate();
  1513. try {
  1514. $name = '';
  1515. try {
  1516. if ($this->certificate_filename) {
  1517. $name = md5($this->certificate_filename . microtime()) . '.' . $this->certificate_filename->extension();
  1518. $this->certificate_filename->storeAs('public', $name);
  1519. }
  1520. } catch (\Exception $ex) {
  1521. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1522. }
  1523. if ($this->dataId > -1) {
  1524. \App\Models\MemberCertificate::create([
  1525. 'member_id' => $this->dataId,
  1526. 'type' => $this->certificate_type,
  1527. 'filename' => $name,
  1528. 'expire_date' => $this->certificate_expire_date,
  1529. 'status' => $this->certificate_status
  1530. ]);
  1531. updateMemberData($this->dataId);
  1532. }
  1533. /*else
  1534. {
  1535. $this->certificateTmp = new \App\Models\MemberCertificate();
  1536. $this->certificateTmp->type = $this->certificate_type;
  1537. $this->certificateTmp->filename = $name;
  1538. $this->certificateTmp->expire_date = $this->certificate_expire_date;
  1539. $this->certificateTmp->status = $this->certificate_status;
  1540. $this->certificateTmp->status = $this->certificate_status;
  1541. // s $this->member_certificates[] = $certificateTmp;
  1542. }*/
  1543. session()->flash('success, Tesserato creato');
  1544. $this->resetCertificateFields();
  1545. $this->addCertificate = false;
  1546. } catch (\Exception $ex) {
  1547. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1548. }
  1549. }
  1550. public function editCertificate($id)
  1551. {
  1552. try {
  1553. $memberCertificate = \App\Models\MemberCertificate::findOrFail($id);
  1554. if (!$memberCertificate) {
  1555. session()->flash('error', 'Tesserato non trovato');
  1556. } else {
  1557. $this->certificate_type = $memberCertificate->type;
  1558. $this->certificate_filename_old = $memberCertificate->filename;
  1559. $this->certificate_expire_date = $memberCertificate->expire_date;
  1560. $this->certificate_status = $memberCertificate->status;
  1561. $this->cardCertificateId = $memberCertificate->id;
  1562. $this->updateCertificate = true;
  1563. $this->addCertificate = false;
  1564. }
  1565. } catch (\Exception $ex) {
  1566. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1567. }
  1568. }
  1569. public function updateCertificate()
  1570. {
  1571. $this->validate(['certificate_expire_date' => 'required']);
  1572. try {
  1573. $name = '';
  1574. try {
  1575. if ($this->certificate_filename) {
  1576. $name = md5($this->certificate_filename . microtime()) . '.' . $this->certificate_filename->extension();
  1577. $this->certificate_filename->storeAs('public', $name);
  1578. }
  1579. } catch (\Exception $ex) {
  1580. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1581. }
  1582. \App\Models\MemberCertificate::whereId($this->cardCertificateId)->update([
  1583. 'member_id' => $this->dataId,
  1584. 'type' => $this->certificate_type,
  1585. 'filename' => $name != '' ? $name : $this->certificate_filename_old,
  1586. 'expire_date' => $this->certificate_expire_date,
  1587. 'status' => $this->certificate_status
  1588. ]);
  1589. updateMemberData($this->dataId);
  1590. session()->flash('success', 'Tesserato aggiornato');
  1591. $this->resetCertificateFields();
  1592. $this->updateCertificate = false;
  1593. } catch (\Exception $ex) {
  1594. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1595. }
  1596. }
  1597. public function cancelCertificate()
  1598. {
  1599. $this->addCertificate = false;
  1600. $this->updateCertificate = false;
  1601. $this->resetCertificateFields();
  1602. }
  1603. public function deleteCertificate($id)
  1604. {
  1605. try {
  1606. \App\Models\MemberCertificate::find($id)->delete();
  1607. session()->flash('success', "Tesserato eliminato");
  1608. } catch (\Exception $e) {
  1609. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  1610. }
  1611. }
  1612. // Gruppi di appartenenza
  1613. public function storeCategory()
  1614. {
  1615. $this->validate(['category_category_id' => 'required']);
  1616. try {
  1617. \App\Models\MemberCategory::create([
  1618. 'member_id' => $this->dataId,
  1619. 'category_id' => $this->category_category_id,
  1620. 'date' => \Carbon\Carbon::now()
  1621. ]);
  1622. session()->flash('success, Associazione creato');
  1623. $this->resetCategoryFields();
  1624. $this->addCard = false;
  1625. } catch (\Exception $ex) {
  1626. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1627. }
  1628. }
  1629. public function hideMsg()
  1630. {
  1631. $this->groupMsg = '';
  1632. }
  1633. public function storeCategoryWithID($id)
  1634. {
  1635. $this->groupMsg = '';
  1636. try {
  1637. if (\App\Models\MemberCategory::where('member_id', $this->dataId)->where('category_id', $id)->first()) {
  1638. $this->groupMsg = '<br>Attenzione, questo corso è stato già inserito';
  1639. } else {
  1640. \App\Models\MemberCategory::create([
  1641. 'member_id' => $this->dataId,
  1642. 'category_id' => $id,
  1643. 'date' => \Carbon\Carbon::now()
  1644. ]);
  1645. session()->flash('success, Associazione creato');
  1646. $this->resetCategoryFields();
  1647. $this->addCard = false;
  1648. }
  1649. } catch (\Exception $ex) {
  1650. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1651. }
  1652. }
  1653. public function deleteCategory($id)
  1654. {
  1655. try {
  1656. \App\Models\MemberCategory::find($id)->delete();
  1657. session()->flash('success', "Associazione eliminata");
  1658. } catch (\Exception $e) {
  1659. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  1660. }
  1661. }
  1662. public function setCourse($id)
  1663. {
  1664. $this->course_course_id = $id;
  1665. }
  1666. public function getNation($nation)
  1667. {
  1668. if ($nation > 0) {
  1669. $ret = \App\Models\Nation::findOrFail($nation);
  1670. return $ret->name;
  1671. }
  1672. return "";
  1673. }
  1674. public function getProvince($province)
  1675. {
  1676. if ($province > 0) {
  1677. $ret = \App\Models\Province::findOrFail($province);
  1678. return $ret->name;
  1679. }
  1680. return "";
  1681. }
  1682. public function getCity($city)
  1683. {
  1684. if ($city > 0) {
  1685. $ret = \App\Models\City::findOrFail($city);
  1686. return $ret->name;
  1687. }
  1688. return "";
  1689. }
  1690. public function getFiscalCode()
  1691. {
  1692. $this->error_fc = false;
  1693. $cf = new codicefiscale();
  1694. $cf->setDateSeparator('-');
  1695. if ($this->first_name != '' && $this->last_name != '' && $this->birth_date != '' && $this->gender != '' && $this->birth_city_id > 0) {
  1696. $code = '';
  1697. if ($this->birth_city_id > 0) {
  1698. $code = \App\Models\City::findOrFail($this->birth_city_id)->code;
  1699. }
  1700. $codice = $cf->calcola($this->first_name, $this->last_name, $this->birth_date, $this->gender, $code);
  1701. $this->fiscal_code = $codice;
  1702. } else
  1703. $this->error_fc = true;
  1704. }
  1705. public function setMonth($m)
  1706. {
  1707. $exist = -1;
  1708. foreach ($this->course_months as $idx => $x) {
  1709. if ($x["m"] == $m) {
  1710. $exist = $idx;
  1711. break;
  1712. }
  1713. }
  1714. if ($exist > -1) {
  1715. array_splice($this->course_months, $exist, 1);
  1716. } else {
  1717. $this->course_months[] = array("m" => $m, "status" => "");
  1718. }
  1719. /*
  1720. if (in_array($m, $this->course_months))
  1721. {
  1722. $i = array_search($m, $this->course_months);
  1723. array_splice($this->course_months, $i, 1);
  1724. }
  1725. else
  1726. {
  1727. $this->course_months[] = $m;
  1728. }*/
  1729. }
  1730. public function newPayment($course)
  1731. {
  1732. $c = \App\Models\Course::findOrFail($course["course_id"]);
  1733. $price = $course["price"];
  1734. if (sizeof($this->payMonths) == 1) {
  1735. $month = $this->payMonths[0];
  1736. $records = \App\Models\Record::where('member_course_id', $this->selectedCourseMember)->where('deleted', 0)->get();
  1737. foreach ($records as $record) {
  1738. if (in_array($month, json_decode($record->months))) {
  1739. foreach ($record->rows as $row) {
  1740. if ($row->causal_id == $c->causal_id && !str_contains(strtolower($row->note), 'iscrizione')) {
  1741. $tot = sizeof(json_decode($row->when));
  1742. foreach (json_decode($row->when) as $m) {
  1743. $price -= $row->amount / $tot;
  1744. }
  1745. }
  1746. }
  1747. }
  1748. }
  1749. }
  1750. $this->emit('setEdit', false);
  1751. $this->emit('setEditCorso', false);
  1752. 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"]);
  1753. }
  1754. public function block($course)
  1755. {
  1756. $xxx = json_decode($course["months"]);
  1757. foreach ($xxx as $idx => $mm) {
  1758. if (in_array($mm->m, $this->payMonths)) {
  1759. $xxx[$idx]->status = 2;
  1760. }
  1761. }
  1762. $c = \App\Models\MemberCourse::findOrFail($course["id"]);
  1763. $c->months = json_encode($xxx);
  1764. $c->save();
  1765. $this->payMonths = array();
  1766. }
  1767. public function reactivate($course)
  1768. {
  1769. $xxx = json_decode($course["months"]);
  1770. foreach ($xxx as $idx => $mm) {
  1771. if (in_array($mm->m, $this->suspendedMonths)) {
  1772. $xxx[$idx]->status = "";
  1773. }
  1774. }
  1775. $c = \App\Models\MemberCourse::findOrFail($course["id"]);
  1776. $c->months = json_encode($xxx);
  1777. $c->save();
  1778. $this->suspendedMonths = array();
  1779. }
  1780. public function setPayMonth($m, $months, $selectedCourseMember)
  1781. {
  1782. if ($this->selectedCourseMember != $selectedCourseMember)
  1783. $this->payMonths = array();
  1784. $this->selectedCourseMember = $selectedCourseMember;
  1785. if (in_array($m, $this->payMonths)) {
  1786. $i = array_search($m, $this->payMonths);
  1787. array_splice($this->payMonths, $i, 1);
  1788. } else if (in_array($m, $this->suspendedMonths)) {
  1789. $i = array_search($m, $this->suspendedMonths);
  1790. array_splice($this->suspendedMonths, $i, 1);
  1791. } else {
  1792. foreach (json_decode($months) as $mm) {
  1793. if ($mm->m == $m) {
  1794. if ($mm->status == "")
  1795. $this->payMonths[] = $m;
  1796. if ($mm->status == "1") {
  1797. $mc = \App\Models\MemberCourse::findOrFail($selectedCourseMember);
  1798. $price = $mc->price;
  1799. $payed = 0;
  1800. $extraC = '';
  1801. $recordsPayed = \App\Models\Record::where('member_course_id', $selectedCourseMember)->where('deleted', 0)->get();
  1802. foreach ($recordsPayed as $record) {
  1803. if (in_array($m, json_decode($record->months))) {
  1804. foreach ($record->rows as $row) {
  1805. if ($row->causal_id == $mc->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione')) {
  1806. $tot = sizeof(json_decode($row->when));
  1807. $payed += $row->amount / $tot;
  1808. }
  1809. }
  1810. if ($payed < $price)
  1811. $this->payMonths[] = $m;
  1812. }
  1813. }
  1814. }
  1815. if ($mm->status == "2")
  1816. $this->suspendedMonths[] = $m;
  1817. }
  1818. }
  1819. }
  1820. if (sizeof($this->payMonths) > 0 && sizeof($this->suspendedMonths) > 0) {
  1821. $this->payMonths = array();
  1822. $this->suspendedMonths = array();
  1823. }
  1824. }
  1825. public function setCreateSubscription($subscribed, $selectedCourseMember)
  1826. {
  1827. if ($this->selectedCourseMember != $selectedCourseMember)
  1828. $this->payMonths = array();
  1829. $this->selectedCourseMember = $selectedCourseMember;
  1830. if (!$subscribed)
  1831. $this->createSubscription = !$this->createSubscription;
  1832. }
  1833. public function showHideCourse($id)
  1834. {
  1835. if (in_array($id, $this->showCourse)) {
  1836. $i = array_search($id, $this->showCourse);
  1837. array_splice($this->showCourse, $i, 1);
  1838. } else {
  1839. $this->showCourse[] = $id;
  1840. }
  1841. }
  1842. public function getMonthStatus($m, $months, $selectedCourseMember)
  1843. {
  1844. $class = "grey";
  1845. foreach (json_decode($months) as $mm) {
  1846. if ($mm->m == $m) {
  1847. if ($mm->status == "") {
  1848. if (in_array($m, $this->payMonths) && $this->selectedCourseMember == $selectedCourseMember)
  1849. $class = "blue";
  1850. else
  1851. $class = "orange";
  1852. }
  1853. if ($mm->status == "1") {
  1854. $class = "green";
  1855. }
  1856. if ($mm->status == "2") {
  1857. if (in_array($m, $this->suspendedMonths) && $this->selectedCourseMember == $selectedCourseMember)
  1858. $class = "blue";
  1859. else
  1860. $class = "yellow";
  1861. }
  1862. if ($class == 'green') {
  1863. $mc = \App\Models\MemberCourse::findOrFail($selectedCourseMember);
  1864. $price = $mc->price;
  1865. $payed = 0;
  1866. $extraC = '';
  1867. $recordsPayed = \App\Models\Record::where('member_course_id', $selectedCourseMember)->where('deleted', 0)->get();
  1868. foreach ($recordsPayed as $record) {
  1869. if (in_array($m, json_decode($record->months))) {
  1870. foreach ($record->rows as $row) {
  1871. if ($row->causal_id == $mc->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione')) {
  1872. $tot = sizeof(json_decode($row->when));
  1873. $payed += $row->amount / $tot;
  1874. }
  1875. }
  1876. }
  1877. }
  1878. if ($payed < $price)
  1879. $class = 'orange half';
  1880. //$class .= $extraC;
  1881. }
  1882. }
  1883. }
  1884. return $class;
  1885. }
  1886. public function getSubscriptionStatus($subscribed, $selectedCourseMember)
  1887. {
  1888. $class = "grey";
  1889. if ($this->createSubscription && $this->selectedCourseMember == $selectedCourseMember)
  1890. $class = "blue";
  1891. else
  1892. $class = "orange";
  1893. if ($subscribed)
  1894. $class = "green";
  1895. return $class;
  1896. }
  1897. public function checkMonth($m)
  1898. {
  1899. $ret = false;
  1900. foreach ($this->course_months as $idx => $x) {
  1901. if ($x["m"] == $m) {
  1902. $ret = true;
  1903. break;
  1904. }
  1905. }
  1906. return $ret;
  1907. }
  1908. public function setDay($idx, $d)
  1909. {
  1910. if (in_array($d, $this->course_when[$idx]["day"])) {
  1911. $i = array_search($d, $this->course_when[$idx]["day"]);
  1912. array_splice($this->course_when[$idx]["day"], $i, 1);
  1913. } else {
  1914. $this->course_when[$idx]["day"][] = $d;
  1915. }
  1916. }
  1917. public function updateBorsellino($importoBorsellino, $s)
  1918. {
  1919. if ($importoBorsellino > 0) {
  1920. $imp = $importoBorsellino;
  1921. if ($s == "-")
  1922. $imp = $imp * -1;
  1923. $money = new \App\Models\Money();
  1924. $money->member_id = $this->currentMember->id;
  1925. $money->record_id = null;
  1926. $money->amount = $imp;
  1927. $money->date = date("Y-m-d");
  1928. $money->note = 'Forzato ' . $imp;
  1929. $money->save();
  1930. }
  1931. $this->emit('saved');
  1932. }
  1933. public function getMonth($m)
  1934. {
  1935. $ret = '';
  1936. switch ($m) {
  1937. case 1:
  1938. $ret = 'Gennaio';
  1939. break;
  1940. case 2:
  1941. $ret = 'Febbraio';
  1942. break;
  1943. case 3:
  1944. $ret = 'Marzo';
  1945. break;
  1946. case 4:
  1947. $ret = 'Aprile';
  1948. break;
  1949. case 5:
  1950. $ret = 'Maggio';
  1951. break;
  1952. case 6:
  1953. $ret = 'Giugno';
  1954. break;
  1955. case 7:
  1956. $ret = 'Luglio';
  1957. break;
  1958. case 8:
  1959. $ret = 'Agosto';
  1960. break;
  1961. case 9:
  1962. $ret = 'Settembre';
  1963. break;
  1964. case 10:
  1965. $ret = 'Ottobre';
  1966. break;
  1967. case 11:
  1968. $ret = 'Novembre';
  1969. break;
  1970. case 12:
  1971. $ret = 'Dicembre';
  1972. break;
  1973. default:
  1974. $ret = '';
  1975. break;
  1976. }
  1977. return $ret;
  1978. }
  1979. }
  1980. class codicefiscale
  1981. {
  1982. /**
  1983. * Array delle consonanti
  1984. */
  1985. protected $_consonanti = array(
  1986. 'B',
  1987. 'C',
  1988. 'D',
  1989. 'F',
  1990. 'G',
  1991. 'H',
  1992. 'J',
  1993. 'K',
  1994. 'L',
  1995. 'M',
  1996. 'N',
  1997. 'P',
  1998. 'Q',
  1999. 'R',
  2000. 'S',
  2001. 'T',
  2002. 'V',
  2003. 'W',
  2004. 'X',
  2005. 'Y',
  2006. 'Z'
  2007. );
  2008. /**
  2009. * Array delle vocali
  2010. */
  2011. protected $_vocali = array(
  2012. 'A',
  2013. 'E',
  2014. 'I',
  2015. 'O',
  2016. 'U'
  2017. );
  2018. /**
  2019. * Array per il calcolo della lettera del mese
  2020. * Al numero del mese corrisponde una lettera
  2021. */
  2022. protected $_mesi = array(
  2023. 1 => 'A',
  2024. 2 => 'B',
  2025. 3 => 'C',
  2026. 4 => 'D',
  2027. 5 => 'E',
  2028. 6 => 'H',
  2029. 7 => 'L',
  2030. 8 => 'M',
  2031. 9 => 'P',
  2032. 10 => 'R',
  2033. 11 => 'S',
  2034. 12 => 'T'
  2035. );
  2036. protected $_pari = array(
  2037. '0' => 0,
  2038. '1' => 1,
  2039. '2' => 2,
  2040. '3' => 3,
  2041. '4' => 4,
  2042. '5' => 5,
  2043. '6' => 6,
  2044. '7' => 7,
  2045. '8' => 8,
  2046. '9' => 9,
  2047. 'A' => 0,
  2048. 'B' => 1,
  2049. 'C' => 2,
  2050. 'D' => 3,
  2051. 'E' => 4,
  2052. 'F' => 5,
  2053. 'G' => 6,
  2054. 'H' => 7,
  2055. 'I' => 8,
  2056. 'J' => 9,
  2057. 'K' => 10,
  2058. 'L' => 11,
  2059. 'M' => 12,
  2060. 'N' => 13,
  2061. 'O' => 14,
  2062. 'P' => 15,
  2063. 'Q' => 16,
  2064. 'R' => 17,
  2065. 'S' => 18,
  2066. 'T' => 19,
  2067. 'U' => 20,
  2068. 'V' => 21,
  2069. 'W' => 22,
  2070. 'X' => 23,
  2071. 'Y' => 24,
  2072. 'Z' => 25
  2073. );
  2074. protected $_dispari = array(
  2075. '0' => 1,
  2076. '1' => 0,
  2077. '2' => 5,
  2078. '3' => 7,
  2079. '4' => 9,
  2080. '5' => 13,
  2081. '6' => 15,
  2082. '7' => 17,
  2083. '8' => 19,
  2084. '9' => 21,
  2085. 'A' => 1,
  2086. 'B' => 0,
  2087. 'C' => 5,
  2088. 'D' => 7,
  2089. 'E' => 9,
  2090. 'F' => 13,
  2091. 'G' => 15,
  2092. 'H' => 17,
  2093. 'I' => 19,
  2094. 'J' => 21,
  2095. 'K' => 2,
  2096. 'L' => 4,
  2097. 'M' => 18,
  2098. 'N' => 20,
  2099. 'O' => 11,
  2100. 'P' => 3,
  2101. 'Q' => 6,
  2102. 'R' => 8,
  2103. 'S' => 12,
  2104. 'T' => 14,
  2105. 'U' => 16,
  2106. 'V' => 10,
  2107. 'W' => 22,
  2108. 'X' => 25,
  2109. 'Y' => 24,
  2110. 'Z' => 23
  2111. );
  2112. protected $_controllo = array(
  2113. '0' => 'A',
  2114. '1' => 'B',
  2115. '2' => 'C',
  2116. '3' => 'D',
  2117. '4' => 'E',
  2118. '5' => 'F',
  2119. '6' => 'G',
  2120. '7' => 'H',
  2121. '8' => 'I',
  2122. '9' => 'J',
  2123. '10' => 'K',
  2124. '11' => 'L',
  2125. '12' => 'M',
  2126. '13' => 'N',
  2127. '14' => 'O',
  2128. '15' => 'P',
  2129. '16' => 'Q',
  2130. '17' => 'R',
  2131. '18' => 'S',
  2132. '19' => 'T',
  2133. '20' => 'U',
  2134. '21' => 'V',
  2135. '22' => 'W',
  2136. '23' => 'X',
  2137. '24' => 'Y',
  2138. '25' => 'Z'
  2139. );
  2140. /**
  2141. * Stringa di errore
  2142. */
  2143. protected $_error = null;
  2144. /**
  2145. * Separatore per la data di nascita
  2146. */
  2147. protected $_dateSeparator = '/';
  2148. /**
  2149. * Percorso del file del database SQLite
  2150. * dei codici catastali
  2151. */
  2152. protected $_dbCatastali = null;
  2153. /**
  2154. * Trasforma la stringa passata in un array di lettere
  2155. * e lo incrocia con un ulteriore array
  2156. */
  2157. protected function _getLettere($string, array $haystack)
  2158. {
  2159. $letters = array();
  2160. foreach (str_split($string) as $needle) {
  2161. if (in_array($needle, $haystack)) {
  2162. $letters[] = $needle;
  2163. }
  2164. }
  2165. return $letters;
  2166. }
  2167. /**
  2168. * Ritorna un array con le vocali di una data stringa
  2169. */
  2170. protected function _getVocali($string)
  2171. {
  2172. return $this->_getLettere($string, $this->_vocali);
  2173. }
  2174. /**
  2175. * Ritorna un array con le consonanti di una data stringa
  2176. */
  2177. protected function _getConsonanti($string)
  2178. {
  2179. return $this->_getLettere($string, $this->_consonanti);
  2180. }
  2181. /**
  2182. * Pulisce la stringa filtrando tutti i caratteri che
  2183. * non sono lettere. Lo switch $toupper se impostato a TRUE
  2184. * converte la stringa risultante in MAIUSCOLO.
  2185. */
  2186. protected function _sanitize($string, $toupper = true)
  2187. {
  2188. $result = preg_replace('/[^A-Za-z]*/', '', $string);
  2189. return ($toupper) ? strtoupper($result) : $result;
  2190. }
  2191. /**
  2192. * Se la stringa passata a funzione e' costituita
  2193. * da meno di 3 caratteri, rimpiazza le lettere
  2194. * mancanti con la lettera X.
  2195. */
  2196. protected function _addMissingX($string)
  2197. {
  2198. $code = $string;
  2199. while (strlen($code) < 3) {
  2200. $code .= 'X';
  2201. }
  2202. return $code;
  2203. }
  2204. /**
  2205. * Ottiene il codice identificativo del nome
  2206. */
  2207. protected function _calcolaNome($string)
  2208. {
  2209. $nome = $this->_sanitize($string);
  2210. $code = '';
  2211. // Se il nome inserito e' piu' corto di 3 lettere
  2212. // si aggiungono tante X quanti sono i caratteri
  2213. // mancanti.
  2214. if (strlen($nome) < 3) {
  2215. return $this->_addMissingX($nome);
  2216. }
  2217. $nome_cons = $this->_getConsonanti($nome);
  2218. // Se le consonanti contenute nel nome sono minori
  2219. // o uguali a 3 vengono considerate nell'ordine in cui
  2220. // compaiono.
  2221. if (count($nome_cons) <= 3) {
  2222. $code = implode('', $nome_cons);
  2223. } else {
  2224. // Se invece abbiamo almeno 4 consonanti, prendiamo
  2225. // la prima, la terza e la quarta.
  2226. for ($i = 0; $i < 4; $i++) {
  2227. if ($i == 1) continue;
  2228. if (!empty($nome_cons[$i])) {
  2229. $code .= $nome_cons[$i];
  2230. }
  2231. }
  2232. }
  2233. // Se compaiono meno di 3 consonanti nel nome, si
  2234. // utilizzano le vocali, nell'ordine in cui compaiono
  2235. // nel nome.
  2236. if (strlen($code) < 3) {
  2237. $nome_voc = $this->_getVocali($nome);
  2238. while (strlen($code) < 3) {
  2239. $code .= array_shift($nome_voc);
  2240. }
  2241. }
  2242. return $code;
  2243. }
  2244. protected function _calcolaCognome($string)
  2245. {
  2246. $cognome = $this->_sanitize($string);
  2247. $code = '';
  2248. // Se il cognome inserito e' piu' corto di 3 lettere
  2249. // si aggiungono tante X quanti sono i caratteri
  2250. // mancanti.
  2251. if (strlen($cognome) < 3) {
  2252. return $this->_addMissingX($cognome);
  2253. }
  2254. $cognome_cons = $this->_getConsonanti($cognome);
  2255. // Per il calcolo del cognome si prendono le prime
  2256. // 3 consonanti.
  2257. for ($i = 0; $i < 3; $i++) {
  2258. if (array_key_exists($i, $cognome_cons)) {
  2259. $code .= $cognome_cons[$i];
  2260. }
  2261. }
  2262. // Se le consonanti non bastano, vengono prese
  2263. // le vocali nell'ordine in cui compaiono.
  2264. if (strlen($code) < 3) {
  2265. $cognome_voc = $this->_getVocali($cognome);
  2266. while (strlen($code) < 3) {
  2267. $code .= array_shift($cognome_voc);
  2268. }
  2269. }
  2270. return $code;
  2271. }
  2272. /**
  2273. * Imposta il separatore di data ( default: / )
  2274. */
  2275. public function setDateSeparator($char)
  2276. {
  2277. $this->_dateSeparator = $char;
  2278. return $this;
  2279. }
  2280. /**
  2281. * Ritorna la parte di codice fiscale corrispondente
  2282. * alla data di nascita del soggetto (Forma: AAMGG)
  2283. */
  2284. protected function _calcolaDataNascita($data, $sesso)
  2285. {
  2286. $dn = explode($this->_dateSeparator, $data);
  2287. $giorno = (int) @$dn[2];
  2288. $mese = (int) @$dn[1];
  2289. $anno = (int) @$dn[0];
  2290. // Le ultime due cifre dell'anno di nascita
  2291. $aa = substr($anno, -2);
  2292. // La lettera corrispondente al mese di nascita
  2293. $mm = $this->_mesi[$mese];
  2294. // Il giorno viene calcolato a seconda del sesso
  2295. // del soggetto di cui si calcola il codice:
  2296. // se e' Maschio si mette il giorno reale, se e'
  2297. // Femmina viene aggiungo 40 a questo numero.
  2298. $gg = (strtoupper($sesso) == 'M') ? $giorno : ($giorno + 40);
  2299. // Bug #1: Thanks to Luca
  2300. if (strlen($gg) < 2) $gg = '0' . $gg;
  2301. return $aa . $mm . $gg;
  2302. }
  2303. /**
  2304. * Ritorna la cifra di controllo sulla base dei
  2305. * 15 caratteri del codice fiscale calcolati.
  2306. */
  2307. protected function _calcolaCifraControllo($codice)
  2308. {
  2309. $code = str_split($codice);
  2310. $sum = 0;
  2311. for ($i = 1; $i <= count($code); $i++) {
  2312. $cifra = $code[$i - 1];
  2313. $sum += ($i % 2) ? $this->_dispari[$cifra] : $this->_pari[$cifra];
  2314. }
  2315. $sum %= 26;
  2316. return $this->_controllo[$sum];
  2317. }
  2318. /**
  2319. * Imposta il messaggio di errore
  2320. */
  2321. protected function _setError($string)
  2322. {
  2323. $this->_error = $string;
  2324. }
  2325. /**
  2326. * Verifica la presenza di un errore.
  2327. * Ritorna TRUE se presente, FALSE altrimenti.
  2328. */
  2329. public function hasError()
  2330. {
  2331. return !is_null($this->_error);
  2332. }
  2333. /**
  2334. * Ritorna la stringa di errore
  2335. */
  2336. public function getError()
  2337. {
  2338. return $this->_error;
  2339. }
  2340. /**
  2341. * Ritorna il codice fiscale utilizzando i parametri
  2342. * passati a funzione. Se si verifica
  2343. */
  2344. public function calcola($nome, $cognome, $data, $sesso, $comune)
  2345. {
  2346. $codice = $this->_calcolaCognome($cognome) .
  2347. $this->_calcolaNome($nome) .
  2348. $this->_calcolaDataNascita($data, $sesso) .
  2349. $comune;
  2350. if ($this->hasError()) {
  2351. return false;
  2352. }
  2353. $codice .= $this->_calcolaCifraControllo($codice);
  2354. if (strlen($codice) != 16) {
  2355. //$this->_setError(self::ERR_GENERIC);
  2356. return 'ERROR';
  2357. }
  2358. return $codice;
  2359. }
  2360. }