Member.php 95 KB

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