Member.php 78 KB

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