Member.php 80 KB

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