Member.php 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698
  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. $go = true;
  1343. while ($go) {
  1344. $mms = [];
  1345. $mms[] = date("n", strtotime($start));
  1346. for ($jj = 1; $jj < $r->months; $jj++) {
  1347. $mms[] = date('n', strtotime("+" . $jj . " months", strtotime($start)));
  1348. }
  1349. $rate = new \App\Models\Rate();
  1350. $rate->member_id = $this->dataId;
  1351. $rate->member_course_id = $mc->id;
  1352. $rate->course_subscription_id = $this->course_course_subscription_id;
  1353. $rate->price = currencyToDouble($this->course_price);
  1354. $rate->date = $start;
  1355. $rate->months = json_encode($mms);
  1356. $rate->note = '';
  1357. $rate->status = 0;
  1358. $rate->save();
  1359. $start = date('Y-m-d', strtotime("+" . $r->months . " months", strtotime($start)));
  1360. if ($start > $this->course_date_to) {
  1361. $go = false;
  1362. break;
  1363. }
  1364. }
  1365. session()->flash('success, Corso creato');
  1366. $this->resetCourseFields();
  1367. $this->addCourse = false;
  1368. $this->emit('setEditCorso', false);
  1369. } catch (\Exception $ex) {
  1370. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1371. }
  1372. }
  1373. public function editCourse($id)
  1374. {
  1375. try {
  1376. $memberCourse = \App\Models\MemberCourse::findOrFail($id);
  1377. if (!$memberCourse) {
  1378. session()->flash('error', 'Corso non trovato');
  1379. } else {
  1380. $this->course_course_id = $memberCourse->course_id;
  1381. // Carico i dati relativi alla struttura ad albero
  1382. $c = \App\Models\Course::findOrFail($this->course_course_id);
  1383. $this->course_name = $c->name . " (" . $c->year . ")";
  1384. $this->course_level_id = $c->course_level_id;
  1385. $this->course_course_type = $c->type;
  1386. $this->course_type_id = $c->course_type_id;
  1387. $this->course_frequency_id = $c->course_frequency_id;
  1388. $all = \App\Models\Course::where('name', 'like', '%' . $c->name . "%")->where('enabled', true)->get();
  1389. foreach ($all as $a) {
  1390. $levels_ids[] = $a->course_level_id;
  1391. }
  1392. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->whereIn('id', $levels_ids)->get();
  1393. $all = \App\Models\Course::where('name', 'like', '%' . $c->name . "%")->where('enabled', true)->where('course_level_id', $this->course_level_id)->get();
  1394. foreach ($all as $a) {
  1395. $types_ids[] = $a->course_type_id;
  1396. }
  1397. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->whereIn('id', $types_ids)->get();
  1398. $frequencies_ids = [];
  1399. $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();
  1400. foreach ($all as $a) {
  1401. $frequencies_ids[] = $a->course_frequency_id;
  1402. }
  1403. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->whereIn('id', $frequencies_ids)->get();
  1404. $this->course_when = array();
  1405. $this->course_date_from = $memberCourse->date_from;
  1406. $this->course_date_to = $memberCourse->date_to;
  1407. foreach (json_decode($memberCourse->when) as $z) {
  1408. $this->course_when[] = array("day" => $z->day, "from" => $z->from, "to" => $z->to);
  1409. }
  1410. //$this->course_when = json_decode($memberCourse->when);
  1411. $this->course_course_subscription_id = $memberCourse->course_subscription_id;
  1412. $this->course_status = $memberCourse->status;
  1413. $this->course_subscribed = $memberCourse->subscribed == 1 ? true : false;
  1414. $this->course_price = formatPrice($memberCourse->price);
  1415. $this->course_subscription_price = formatPrice($memberCourse->subscription_price);
  1416. $this->course_note = $memberCourse->notes;
  1417. foreach (json_decode($memberCourse->months) as $z) {
  1418. $this->course_months[] = array("m" => $z->m, "status" => $z->status);
  1419. }
  1420. $this->courseDataId = $memberCourse->id;
  1421. $this->updateCourse = true;
  1422. $this->addCourse = false;
  1423. $this->emit('setEditCorso', true);
  1424. }
  1425. } catch (\Exception $ex) {
  1426. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1427. }
  1428. }
  1429. public function updateCourse()
  1430. {
  1431. //$this->validate();
  1432. $this->validate(['course_course_id' => 'required']);
  1433. try {
  1434. \App\Models\MemberCourse::whereId($this->courseDataId)->update([
  1435. 'member_id' => $this->dataId,
  1436. 'course_id' => $this->course_course_id,
  1437. 'date_from' => $this->course_date_from,
  1438. 'date_to' => $this->course_date_to,
  1439. 'course_subscription_id' => $this->course_course_subscription_id,
  1440. 'status' => $this->course_status,
  1441. 'price' => currencyToDouble($this->course_price),
  1442. 'subscription_price' => currencyToDouble($this->course_subscription_price),
  1443. 'notes' => $this->course_note,
  1444. 'months' => json_encode($this->course_months),
  1445. 'when' => json_encode($this->course_when)
  1446. ]);
  1447. session()->flash('success', 'Corso aggiornato');
  1448. $this->resetCourseFields();
  1449. $this->updateCourse = false;
  1450. $this->emit('setEditCorso', false);
  1451. } catch (\Exception $ex) {
  1452. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1453. }
  1454. }
  1455. public function cancelCourse()
  1456. {
  1457. $this->addCourse = false;
  1458. $this->updateCourse = false;
  1459. $this->emit('setEditCorso', false);
  1460. $this->resetCourseFields();
  1461. }
  1462. public function deleteCourse($id)
  1463. {
  1464. try {
  1465. \App\Models\MemberCourse::find($id)->delete();
  1466. session()->flash('success', "Corso eliminato");
  1467. } catch (\Exception $e) {
  1468. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  1469. }
  1470. }
  1471. public function addRow()
  1472. {
  1473. $this->course_when[] = array('day' => array(), 'from' => '', 'to' => '');
  1474. }
  1475. public function delRow($idx)
  1476. {
  1477. unset($this->course_when[$idx]);
  1478. }
  1479. // Certificates
  1480. public function addCertificate()
  1481. {
  1482. $this->resetCertificateFields();
  1483. $this->addCertificate = true;
  1484. $this->updateCertificate = false;
  1485. }
  1486. public function storeCertificate()
  1487. {
  1488. $this->validate(['certificate_expire_date' => 'required']);
  1489. // $this->validate();
  1490. try {
  1491. $name = '';
  1492. try {
  1493. if ($this->certificate_filename) {
  1494. $name = md5($this->certificate_filename . microtime()) . '.' . $this->certificate_filename->extension();
  1495. $this->certificate_filename->storeAs('public', $name);
  1496. }
  1497. } catch (\Exception $ex) {
  1498. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1499. }
  1500. if ($this->dataId > -1) {
  1501. \App\Models\MemberCertificate::create([
  1502. 'member_id' => $this->dataId,
  1503. 'type' => $this->certificate_type,
  1504. 'filename' => $name,
  1505. 'expire_date' => $this->certificate_expire_date,
  1506. 'status' => $this->certificate_status
  1507. ]);
  1508. updateMemberData($this->dataId);
  1509. }
  1510. /*else
  1511. {
  1512. $this->certificateTmp = new \App\Models\MemberCertificate();
  1513. $this->certificateTmp->type = $this->certificate_type;
  1514. $this->certificateTmp->filename = $name;
  1515. $this->certificateTmp->expire_date = $this->certificate_expire_date;
  1516. $this->certificateTmp->status = $this->certificate_status;
  1517. $this->certificateTmp->status = $this->certificate_status;
  1518. // s $this->member_certificates[] = $certificateTmp;
  1519. }*/
  1520. session()->flash('success, Tesserato creato');
  1521. $this->resetCertificateFields();
  1522. $this->addCertificate = false;
  1523. } catch (\Exception $ex) {
  1524. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1525. }
  1526. }
  1527. public function editCertificate($id)
  1528. {
  1529. try {
  1530. $memberCertificate = \App\Models\MemberCertificate::findOrFail($id);
  1531. if (!$memberCertificate) {
  1532. session()->flash('error', 'Tesserato non trovato');
  1533. } else {
  1534. $this->certificate_type = $memberCertificate->type;
  1535. $this->certificate_filename_old = $memberCertificate->filename;
  1536. $this->certificate_expire_date = $memberCertificate->expire_date;
  1537. $this->certificate_status = $memberCertificate->status;
  1538. $this->cardCertificateId = $memberCertificate->id;
  1539. $this->updateCertificate = true;
  1540. $this->addCertificate = false;
  1541. }
  1542. } catch (\Exception $ex) {
  1543. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1544. }
  1545. }
  1546. public function updateCertificate()
  1547. {
  1548. $this->validate(['certificate_expire_date' => 'required']);
  1549. try {
  1550. $name = '';
  1551. try {
  1552. if ($this->certificate_filename) {
  1553. $name = md5($this->certificate_filename . microtime()) . '.' . $this->certificate_filename->extension();
  1554. $this->certificate_filename->storeAs('public', $name);
  1555. }
  1556. } catch (\Exception $ex) {
  1557. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1558. }
  1559. \App\Models\MemberCertificate::whereId($this->cardCertificateId)->update([
  1560. 'member_id' => $this->dataId,
  1561. 'type' => $this->certificate_type,
  1562. 'filename' => $name != '' ? $name : $this->certificate_filename_old,
  1563. 'expire_date' => $this->certificate_expire_date,
  1564. 'status' => $this->certificate_status
  1565. ]);
  1566. updateMemberData($this->dataId);
  1567. session()->flash('success', 'Tesserato aggiornato');
  1568. $this->resetCertificateFields();
  1569. $this->updateCertificate = false;
  1570. } catch (\Exception $ex) {
  1571. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1572. }
  1573. }
  1574. public function cancelCertificate()
  1575. {
  1576. $this->addCertificate = false;
  1577. $this->updateCertificate = false;
  1578. $this->resetCertificateFields();
  1579. }
  1580. public function deleteCertificate($id)
  1581. {
  1582. try {
  1583. \App\Models\MemberCertificate::find($id)->delete();
  1584. session()->flash('success', "Tesserato eliminato");
  1585. } catch (\Exception $e) {
  1586. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  1587. }
  1588. }
  1589. // Gruppi di appartenenza
  1590. public function storeCategory()
  1591. {
  1592. $this->validate(['category_category_id' => 'required']);
  1593. try {
  1594. \App\Models\MemberCategory::create([
  1595. 'member_id' => $this->dataId,
  1596. 'category_id' => $this->category_category_id,
  1597. 'date' => \Carbon\Carbon::now()
  1598. ]);
  1599. session()->flash('success, Associazione creato');
  1600. $this->resetCategoryFields();
  1601. $this->addCard = false;
  1602. } catch (\Exception $ex) {
  1603. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1604. }
  1605. }
  1606. public function hideMsg()
  1607. {
  1608. $this->groupMsg = '';
  1609. }
  1610. public function storeCategoryWithID($id)
  1611. {
  1612. $this->groupMsg = '';
  1613. try {
  1614. if (\App\Models\MemberCategory::where('member_id', $this->dataId)->where('category_id', $id)->first()) {
  1615. $this->groupMsg = '<br>Attenzione, questo corso è stato già inserito';
  1616. } else {
  1617. \App\Models\MemberCategory::create([
  1618. 'member_id' => $this->dataId,
  1619. 'category_id' => $id,
  1620. 'date' => \Carbon\Carbon::now()
  1621. ]);
  1622. session()->flash('success, Associazione creato');
  1623. $this->resetCategoryFields();
  1624. $this->addCard = false;
  1625. }
  1626. } catch (\Exception $ex) {
  1627. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  1628. }
  1629. }
  1630. public function deleteCategory($id)
  1631. {
  1632. try {
  1633. \App\Models\MemberCategory::find($id)->delete();
  1634. session()->flash('success', "Associazione eliminata");
  1635. } catch (\Exception $e) {
  1636. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  1637. }
  1638. }
  1639. public function setCourse($id)
  1640. {
  1641. $this->course_course_id = $id;
  1642. }
  1643. public function getNation($nation)
  1644. {
  1645. if ($nation > 0) {
  1646. $ret = \App\Models\Nation::findOrFail($nation);
  1647. return $ret->name;
  1648. }
  1649. return "";
  1650. }
  1651. public function getProvince($province)
  1652. {
  1653. if ($province > 0) {
  1654. $ret = \App\Models\Province::findOrFail($province);
  1655. return $ret->name;
  1656. }
  1657. return "";
  1658. }
  1659. public function getCity($city)
  1660. {
  1661. if ($city > 0) {
  1662. $ret = \App\Models\City::findOrFail($city);
  1663. return $ret->name;
  1664. }
  1665. return "";
  1666. }
  1667. public function getFiscalCode()
  1668. {
  1669. $this->error_fc = false;
  1670. $cf = new codicefiscale();
  1671. $cf->setDateSeparator('-');
  1672. if ($this->first_name != '' && $this->last_name != '' && $this->birth_date != '' && $this->gender != '' && $this->birth_city_id > 0) {
  1673. $code = '';
  1674. if ($this->birth_city_id > 0) {
  1675. $code = \App\Models\City::findOrFail($this->birth_city_id)->code;
  1676. }
  1677. $codice = $cf->calcola($this->first_name, $this->last_name, $this->birth_date, $this->gender, $code);
  1678. $this->fiscal_code = $codice;
  1679. } else
  1680. $this->error_fc = true;
  1681. }
  1682. public function setMonth($m)
  1683. {
  1684. $exist = -1;
  1685. foreach ($this->course_months as $idx => $x) {
  1686. if ($x["m"] == $m) {
  1687. $exist = $idx;
  1688. break;
  1689. }
  1690. }
  1691. if ($exist > -1) {
  1692. array_splice($this->course_months, $exist, 1);
  1693. } else {
  1694. $this->course_months[] = array("m" => $m, "status" => "");
  1695. }
  1696. /*
  1697. if (in_array($m, $this->course_months))
  1698. {
  1699. $i = array_search($m, $this->course_months);
  1700. array_splice($this->course_months, $i, 1);
  1701. }
  1702. else
  1703. {
  1704. $this->course_months[] = $m;
  1705. }*/
  1706. }
  1707. public function newPayment($course)
  1708. {
  1709. $c = \App\Models\Course::findOrFail($course["course_id"]);
  1710. $price = $course["price"];
  1711. if (sizeof($this->payMonths) == 1) {
  1712. $month = $this->payMonths[0];
  1713. $records = \App\Models\Record::where('member_course_id', $this->selectedCourseMember)->where('deleted', 0)->get();
  1714. foreach ($records as $record) {
  1715. if (in_array($month, json_decode($record->months))) {
  1716. foreach ($record->rows as $row) {
  1717. if ($row->causal_id == $c->causal_id && !str_contains(strtolower($row->note), 'iscrizione')) {
  1718. $tot = sizeof(json_decode($row->when));
  1719. foreach (json_decode($row->when) as $m) {
  1720. $price -= $row->amount / $tot;
  1721. }
  1722. }
  1723. }
  1724. }
  1725. }
  1726. }
  1727. $this->emit('setEdit', false);
  1728. $this->emit('setEditCorso', false);
  1729. 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"]);
  1730. }
  1731. public function block($course)
  1732. {
  1733. $xxx = json_decode($course["months"]);
  1734. foreach ($xxx as $idx => $mm) {
  1735. if (in_array($mm->m, $this->payMonths)) {
  1736. $xxx[$idx]->status = 2;
  1737. }
  1738. }
  1739. $c = \App\Models\MemberCourse::findOrFail($course["id"]);
  1740. $c->months = json_encode($xxx);
  1741. $c->save();
  1742. $this->payMonths = array();
  1743. }
  1744. public function reactivate($course)
  1745. {
  1746. $xxx = json_decode($course["months"]);
  1747. foreach ($xxx as $idx => $mm) {
  1748. if (in_array($mm->m, $this->suspendedMonths)) {
  1749. $xxx[$idx]->status = "";
  1750. }
  1751. }
  1752. $c = \App\Models\MemberCourse::findOrFail($course["id"]);
  1753. $c->months = json_encode($xxx);
  1754. $c->save();
  1755. $this->suspendedMonths = array();
  1756. }
  1757. public function setPayMonth($m, $months, $selectedCourseMember)
  1758. {
  1759. if ($this->selectedCourseMember != $selectedCourseMember)
  1760. $this->payMonths = array();
  1761. $this->selectedCourseMember = $selectedCourseMember;
  1762. if (in_array($m, $this->payMonths)) {
  1763. $i = array_search($m, $this->payMonths);
  1764. array_splice($this->payMonths, $i, 1);
  1765. } else if (in_array($m, $this->suspendedMonths)) {
  1766. $i = array_search($m, $this->suspendedMonths);
  1767. array_splice($this->suspendedMonths, $i, 1);
  1768. } else {
  1769. foreach (json_decode($months) as $mm) {
  1770. if ($mm->m == $m) {
  1771. if ($mm->status == "")
  1772. $this->payMonths[] = $m;
  1773. if ($mm->status == "1") {
  1774. $mc = \App\Models\MemberCourse::findOrFail($selectedCourseMember);
  1775. $price = $mc->price;
  1776. $payed = 0;
  1777. $extraC = '';
  1778. $recordsPayed = \App\Models\Record::where('member_course_id', $selectedCourseMember)->where('deleted', 0)->get();
  1779. foreach ($recordsPayed as $record) {
  1780. if (in_array($m, json_decode($record->months))) {
  1781. foreach ($record->rows as $row) {
  1782. if ($row->causal_id == $mc->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione')) {
  1783. $tot = sizeof(json_decode($row->when));
  1784. $payed += $row->amount / $tot;
  1785. }
  1786. }
  1787. if ($payed < $price)
  1788. $this->payMonths[] = $m;
  1789. }
  1790. }
  1791. }
  1792. if ($mm->status == "2")
  1793. $this->suspendedMonths[] = $m;
  1794. }
  1795. }
  1796. }
  1797. if (sizeof($this->payMonths) > 0 && sizeof($this->suspendedMonths) > 0) {
  1798. $this->payMonths = array();
  1799. $this->suspendedMonths = array();
  1800. }
  1801. }
  1802. public function setCreateSubscription($subscribed, $selectedCourseMember)
  1803. {
  1804. if ($this->selectedCourseMember != $selectedCourseMember)
  1805. $this->payMonths = array();
  1806. $this->selectedCourseMember = $selectedCourseMember;
  1807. if (!$subscribed)
  1808. $this->createSubscription = !$this->createSubscription;
  1809. }
  1810. public function showHideCourse($id)
  1811. {
  1812. if (in_array($id, $this->showCourse)) {
  1813. $i = array_search($id, $this->showCourse);
  1814. array_splice($this->showCourse, $i, 1);
  1815. } else {
  1816. $this->showCourse[] = $id;
  1817. }
  1818. }
  1819. public function getMonthStatus($m, $months, $selectedCourseMember)
  1820. {
  1821. $class = "grey";
  1822. foreach (json_decode($months) as $mm) {
  1823. if ($mm->m == $m) {
  1824. if ($mm->status == "") {
  1825. if (in_array($m, $this->payMonths) && $this->selectedCourseMember == $selectedCourseMember)
  1826. $class = "blue";
  1827. else
  1828. $class = "orange";
  1829. }
  1830. if ($mm->status == "1") {
  1831. $class = "green";
  1832. }
  1833. if ($mm->status == "2") {
  1834. if (in_array($m, $this->suspendedMonths) && $this->selectedCourseMember == $selectedCourseMember)
  1835. $class = "blue";
  1836. else
  1837. $class = "yellow";
  1838. }
  1839. if ($class == 'green') {
  1840. $mc = \App\Models\MemberCourse::findOrFail($selectedCourseMember);
  1841. $price = $mc->price;
  1842. $payed = 0;
  1843. $extraC = '';
  1844. $recordsPayed = \App\Models\Record::where('member_course_id', $selectedCourseMember)->where('deleted', 0)->get();
  1845. foreach ($recordsPayed as $record) {
  1846. if (in_array($m, json_decode($record->months))) {
  1847. foreach ($record->rows as $row) {
  1848. if ($row->causal_id == $mc->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione')) {
  1849. $tot = sizeof(json_decode($row->when));
  1850. $payed += $row->amount / $tot;
  1851. }
  1852. }
  1853. }
  1854. }
  1855. if ($payed < $price)
  1856. $class = 'orange half';
  1857. //$class .= $extraC;
  1858. }
  1859. }
  1860. }
  1861. return $class;
  1862. }
  1863. public function getSubscriptionStatus($subscribed, $selectedCourseMember)
  1864. {
  1865. $class = "grey";
  1866. if ($this->createSubscription && $this->selectedCourseMember == $selectedCourseMember)
  1867. $class = "blue";
  1868. else
  1869. $class = "orange";
  1870. if ($subscribed)
  1871. $class = "green";
  1872. return $class;
  1873. }
  1874. public function checkMonth($m)
  1875. {
  1876. $ret = false;
  1877. foreach ($this->course_months as $idx => $x) {
  1878. if ($x["m"] == $m) {
  1879. $ret = true;
  1880. break;
  1881. }
  1882. }
  1883. return $ret;
  1884. }
  1885. public function setDay($idx, $d)
  1886. {
  1887. if (in_array($d, $this->course_when[$idx]["day"])) {
  1888. $i = array_search($d, $this->course_when[$idx]["day"]);
  1889. array_splice($this->course_when[$idx]["day"], $i, 1);
  1890. } else {
  1891. $this->course_when[$idx]["day"][] = $d;
  1892. }
  1893. }
  1894. public function updateBorsellino($importoBorsellino, $s)
  1895. {
  1896. if ($importoBorsellino > 0) {
  1897. $imp = $importoBorsellino;
  1898. if ($s == "-")
  1899. $imp = $imp * -1;
  1900. $money = new \App\Models\Money();
  1901. $money->member_id = $this->currentMember->id;
  1902. $money->record_id = null;
  1903. $money->amount = $imp;
  1904. $money->date = date("Y-m-d");
  1905. $money->note = 'Forzato ' . $imp;
  1906. $money->save();
  1907. }
  1908. $this->emit('saved');
  1909. }
  1910. public function getMonth($m)
  1911. {
  1912. $ret = '';
  1913. switch ($m) {
  1914. case 1:
  1915. $ret = 'Gennaio';
  1916. break;
  1917. case 2:
  1918. $ret = 'Febbraio';
  1919. break;
  1920. case 3:
  1921. $ret = 'Marzo';
  1922. break;
  1923. case 4:
  1924. $ret = 'Aprile';
  1925. break;
  1926. case 5:
  1927. $ret = 'Maggio';
  1928. break;
  1929. case 6:
  1930. $ret = 'Giugno';
  1931. break;
  1932. case 7:
  1933. $ret = 'Luglio';
  1934. break;
  1935. case 8:
  1936. $ret = 'Agosto';
  1937. break;
  1938. case 9:
  1939. $ret = 'Settembre';
  1940. break;
  1941. case 10:
  1942. $ret = 'Ottobre';
  1943. break;
  1944. case 11:
  1945. $ret = 'Novembre';
  1946. break;
  1947. case 12:
  1948. $ret = 'Dicembre';
  1949. break;
  1950. default:
  1951. $ret = '';
  1952. break;
  1953. }
  1954. return $ret;
  1955. }
  1956. }
  1957. class codicefiscale
  1958. {
  1959. /**
  1960. * Array delle consonanti
  1961. */
  1962. protected $_consonanti = array(
  1963. 'B',
  1964. 'C',
  1965. 'D',
  1966. 'F',
  1967. 'G',
  1968. 'H',
  1969. 'J',
  1970. 'K',
  1971. 'L',
  1972. 'M',
  1973. 'N',
  1974. 'P',
  1975. 'Q',
  1976. 'R',
  1977. 'S',
  1978. 'T',
  1979. 'V',
  1980. 'W',
  1981. 'X',
  1982. 'Y',
  1983. 'Z'
  1984. );
  1985. /**
  1986. * Array delle vocali
  1987. */
  1988. protected $_vocali = array(
  1989. 'A',
  1990. 'E',
  1991. 'I',
  1992. 'O',
  1993. 'U'
  1994. );
  1995. /**
  1996. * Array per il calcolo della lettera del mese
  1997. * Al numero del mese corrisponde una lettera
  1998. */
  1999. protected $_mesi = array(
  2000. 1 => 'A',
  2001. 2 => 'B',
  2002. 3 => 'C',
  2003. 4 => 'D',
  2004. 5 => 'E',
  2005. 6 => 'H',
  2006. 7 => 'L',
  2007. 8 => 'M',
  2008. 9 => 'P',
  2009. 10 => 'R',
  2010. 11 => 'S',
  2011. 12 => 'T'
  2012. );
  2013. protected $_pari = array(
  2014. '0' => 0,
  2015. '1' => 1,
  2016. '2' => 2,
  2017. '3' => 3,
  2018. '4' => 4,
  2019. '5' => 5,
  2020. '6' => 6,
  2021. '7' => 7,
  2022. '8' => 8,
  2023. '9' => 9,
  2024. 'A' => 0,
  2025. 'B' => 1,
  2026. 'C' => 2,
  2027. 'D' => 3,
  2028. 'E' => 4,
  2029. 'F' => 5,
  2030. 'G' => 6,
  2031. 'H' => 7,
  2032. 'I' => 8,
  2033. 'J' => 9,
  2034. 'K' => 10,
  2035. 'L' => 11,
  2036. 'M' => 12,
  2037. 'N' => 13,
  2038. 'O' => 14,
  2039. 'P' => 15,
  2040. 'Q' => 16,
  2041. 'R' => 17,
  2042. 'S' => 18,
  2043. 'T' => 19,
  2044. 'U' => 20,
  2045. 'V' => 21,
  2046. 'W' => 22,
  2047. 'X' => 23,
  2048. 'Y' => 24,
  2049. 'Z' => 25
  2050. );
  2051. protected $_dispari = array(
  2052. '0' => 1,
  2053. '1' => 0,
  2054. '2' => 5,
  2055. '3' => 7,
  2056. '4' => 9,
  2057. '5' => 13,
  2058. '6' => 15,
  2059. '7' => 17,
  2060. '8' => 19,
  2061. '9' => 21,
  2062. 'A' => 1,
  2063. 'B' => 0,
  2064. 'C' => 5,
  2065. 'D' => 7,
  2066. 'E' => 9,
  2067. 'F' => 13,
  2068. 'G' => 15,
  2069. 'H' => 17,
  2070. 'I' => 19,
  2071. 'J' => 21,
  2072. 'K' => 2,
  2073. 'L' => 4,
  2074. 'M' => 18,
  2075. 'N' => 20,
  2076. 'O' => 11,
  2077. 'P' => 3,
  2078. 'Q' => 6,
  2079. 'R' => 8,
  2080. 'S' => 12,
  2081. 'T' => 14,
  2082. 'U' => 16,
  2083. 'V' => 10,
  2084. 'W' => 22,
  2085. 'X' => 25,
  2086. 'Y' => 24,
  2087. 'Z' => 23
  2088. );
  2089. protected $_controllo = array(
  2090. '0' => 'A',
  2091. '1' => 'B',
  2092. '2' => 'C',
  2093. '3' => 'D',
  2094. '4' => 'E',
  2095. '5' => 'F',
  2096. '6' => 'G',
  2097. '7' => 'H',
  2098. '8' => 'I',
  2099. '9' => 'J',
  2100. '10' => 'K',
  2101. '11' => 'L',
  2102. '12' => 'M',
  2103. '13' => 'N',
  2104. '14' => 'O',
  2105. '15' => 'P',
  2106. '16' => 'Q',
  2107. '17' => 'R',
  2108. '18' => 'S',
  2109. '19' => 'T',
  2110. '20' => 'U',
  2111. '21' => 'V',
  2112. '22' => 'W',
  2113. '23' => 'X',
  2114. '24' => 'Y',
  2115. '25' => 'Z'
  2116. );
  2117. /**
  2118. * Stringa di errore
  2119. */
  2120. protected $_error = null;
  2121. /**
  2122. * Separatore per la data di nascita
  2123. */
  2124. protected $_dateSeparator = '/';
  2125. /**
  2126. * Percorso del file del database SQLite
  2127. * dei codici catastali
  2128. */
  2129. protected $_dbCatastali = null;
  2130. /**
  2131. * Trasforma la stringa passata in un array di lettere
  2132. * e lo incrocia con un ulteriore array
  2133. */
  2134. protected function _getLettere($string, array $haystack)
  2135. {
  2136. $letters = array();
  2137. foreach (str_split($string) as $needle) {
  2138. if (in_array($needle, $haystack)) {
  2139. $letters[] = $needle;
  2140. }
  2141. }
  2142. return $letters;
  2143. }
  2144. /**
  2145. * Ritorna un array con le vocali di una data stringa
  2146. */
  2147. protected function _getVocali($string)
  2148. {
  2149. return $this->_getLettere($string, $this->_vocali);
  2150. }
  2151. /**
  2152. * Ritorna un array con le consonanti di una data stringa
  2153. */
  2154. protected function _getConsonanti($string)
  2155. {
  2156. return $this->_getLettere($string, $this->_consonanti);
  2157. }
  2158. /**
  2159. * Pulisce la stringa filtrando tutti i caratteri che
  2160. * non sono lettere. Lo switch $toupper se impostato a TRUE
  2161. * converte la stringa risultante in MAIUSCOLO.
  2162. */
  2163. protected function _sanitize($string, $toupper = true)
  2164. {
  2165. $result = preg_replace('/[^A-Za-z]*/', '', $string);
  2166. return ($toupper) ? strtoupper($result) : $result;
  2167. }
  2168. /**
  2169. * Se la stringa passata a funzione e' costituita
  2170. * da meno di 3 caratteri, rimpiazza le lettere
  2171. * mancanti con la lettera X.
  2172. */
  2173. protected function _addMissingX($string)
  2174. {
  2175. $code = $string;
  2176. while (strlen($code) < 3) {
  2177. $code .= 'X';
  2178. }
  2179. return $code;
  2180. }
  2181. /**
  2182. * Ottiene il codice identificativo del nome
  2183. */
  2184. protected function _calcolaNome($string)
  2185. {
  2186. $nome = $this->_sanitize($string);
  2187. $code = '';
  2188. // Se il nome inserito e' piu' corto di 3 lettere
  2189. // si aggiungono tante X quanti sono i caratteri
  2190. // mancanti.
  2191. if (strlen($nome) < 3) {
  2192. return $this->_addMissingX($nome);
  2193. }
  2194. $nome_cons = $this->_getConsonanti($nome);
  2195. // Se le consonanti contenute nel nome sono minori
  2196. // o uguali a 3 vengono considerate nell'ordine in cui
  2197. // compaiono.
  2198. if (count($nome_cons) <= 3) {
  2199. $code = implode('', $nome_cons);
  2200. } else {
  2201. // Se invece abbiamo almeno 4 consonanti, prendiamo
  2202. // la prima, la terza e la quarta.
  2203. for ($i = 0; $i < 4; $i++) {
  2204. if ($i == 1) continue;
  2205. if (!empty($nome_cons[$i])) {
  2206. $code .= $nome_cons[$i];
  2207. }
  2208. }
  2209. }
  2210. // Se compaiono meno di 3 consonanti nel nome, si
  2211. // utilizzano le vocali, nell'ordine in cui compaiono
  2212. // nel nome.
  2213. if (strlen($code) < 3) {
  2214. $nome_voc = $this->_getVocali($nome);
  2215. while (strlen($code) < 3) {
  2216. $code .= array_shift($nome_voc);
  2217. }
  2218. }
  2219. return $code;
  2220. }
  2221. protected function _calcolaCognome($string)
  2222. {
  2223. $cognome = $this->_sanitize($string);
  2224. $code = '';
  2225. // Se il cognome inserito e' piu' corto di 3 lettere
  2226. // si aggiungono tante X quanti sono i caratteri
  2227. // mancanti.
  2228. if (strlen($cognome) < 3) {
  2229. return $this->_addMissingX($cognome);
  2230. }
  2231. $cognome_cons = $this->_getConsonanti($cognome);
  2232. // Per il calcolo del cognome si prendono le prime
  2233. // 3 consonanti.
  2234. for ($i = 0; $i < 3; $i++) {
  2235. if (array_key_exists($i, $cognome_cons)) {
  2236. $code .= $cognome_cons[$i];
  2237. }
  2238. }
  2239. // Se le consonanti non bastano, vengono prese
  2240. // le vocali nell'ordine in cui compaiono.
  2241. if (strlen($code) < 3) {
  2242. $cognome_voc = $this->_getVocali($cognome);
  2243. while (strlen($code) < 3) {
  2244. $code .= array_shift($cognome_voc);
  2245. }
  2246. }
  2247. return $code;
  2248. }
  2249. /**
  2250. * Imposta il separatore di data ( default: / )
  2251. */
  2252. public function setDateSeparator($char)
  2253. {
  2254. $this->_dateSeparator = $char;
  2255. return $this;
  2256. }
  2257. /**
  2258. * Ritorna la parte di codice fiscale corrispondente
  2259. * alla data di nascita del soggetto (Forma: AAMGG)
  2260. */
  2261. protected function _calcolaDataNascita($data, $sesso)
  2262. {
  2263. $dn = explode($this->_dateSeparator, $data);
  2264. $giorno = (int) @$dn[2];
  2265. $mese = (int) @$dn[1];
  2266. $anno = (int) @$dn[0];
  2267. // Le ultime due cifre dell'anno di nascita
  2268. $aa = substr($anno, -2);
  2269. // La lettera corrispondente al mese di nascita
  2270. $mm = $this->_mesi[$mese];
  2271. // Il giorno viene calcolato a seconda del sesso
  2272. // del soggetto di cui si calcola il codice:
  2273. // se e' Maschio si mette il giorno reale, se e'
  2274. // Femmina viene aggiungo 40 a questo numero.
  2275. $gg = (strtoupper($sesso) == 'M') ? $giorno : ($giorno + 40);
  2276. // Bug #1: Thanks to Luca
  2277. if (strlen($gg) < 2) $gg = '0' . $gg;
  2278. return $aa . $mm . $gg;
  2279. }
  2280. /**
  2281. * Ritorna la cifra di controllo sulla base dei
  2282. * 15 caratteri del codice fiscale calcolati.
  2283. */
  2284. protected function _calcolaCifraControllo($codice)
  2285. {
  2286. $code = str_split($codice);
  2287. $sum = 0;
  2288. for ($i = 1; $i <= count($code); $i++) {
  2289. $cifra = $code[$i - 1];
  2290. $sum += ($i % 2) ? $this->_dispari[$cifra] : $this->_pari[$cifra];
  2291. }
  2292. $sum %= 26;
  2293. return $this->_controllo[$sum];
  2294. }
  2295. /**
  2296. * Imposta il messaggio di errore
  2297. */
  2298. protected function _setError($string)
  2299. {
  2300. $this->_error = $string;
  2301. }
  2302. /**
  2303. * Verifica la presenza di un errore.
  2304. * Ritorna TRUE se presente, FALSE altrimenti.
  2305. */
  2306. public function hasError()
  2307. {
  2308. return !is_null($this->_error);
  2309. }
  2310. /**
  2311. * Ritorna la stringa di errore
  2312. */
  2313. public function getError()
  2314. {
  2315. return $this->_error;
  2316. }
  2317. /**
  2318. * Ritorna il codice fiscale utilizzando i parametri
  2319. * passati a funzione. Se si verifica
  2320. */
  2321. public function calcola($nome, $cognome, $data, $sesso, $comune)
  2322. {
  2323. $codice = $this->_calcolaCognome($cognome) .
  2324. $this->_calcolaNome($nome) .
  2325. $this->_calcolaDataNascita($data, $sesso) .
  2326. $comune;
  2327. if ($this->hasError()) {
  2328. return false;
  2329. }
  2330. $codice .= $this->_calcolaCifraControllo($codice);
  2331. if (strlen($codice) != 16) {
  2332. //$this->_setError(self::ERR_GENERIC);
  2333. return 'ERROR';
  2334. }
  2335. return $codice;
  2336. }
  2337. }