Member.php 104 KB

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