Member.php 88 KB

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