Member.php 93 KB

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