Member.php 88 KB

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