Member.php 95 KB

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