Member.php 79 KB

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