RecordIN.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Livewire\WithPagination;
  5. use Barryvdh\DomPDF\Facade\Pdf;
  6. use Illuminate\Support\Facades\Storage;
  7. use Illuminate\Support\Facades\Mail;
  8. use Illuminate\Contracts\Validation\Validator;
  9. class RecordIN extends Component
  10. {
  11. use WithPagination;
  12. protected $paginationTheme = 'bootstrap';
  13. protected $listeners = ['setCausal' => 'setCausal','refreshMembers' => 'refreshMembers'];
  14. public $sortField ='date';
  15. public $sortAsc = false;
  16. public $typeIN = "IN";
  17. public $fromPage = '';
  18. public $isDuplicate = false;
  19. public $multiP = false;
  20. public $from, $to;
  21. public $multiMonthTo = 0;
  22. public $multiYearTo = 0;
  23. public $multiMonthFrom = 0;
  24. public $multiYearFrom = 0;
  25. public $createSubscription = 0;
  26. public $corrispettivo_causal_id = 0;
  27. public function sortBy($field)
  28. {
  29. if($this->sortField === $field)
  30. {
  31. $this->sortAsc = ! $this->sortAsc;
  32. } else {
  33. $this->sortAsc = true;
  34. }
  35. $this->sortField = $field;
  36. }
  37. public $courseId = 0;
  38. public $months = array();
  39. public $records, $dataId, $member_id, $supplier_id,
  40. $causal_id,
  41. $payment_method_id,
  42. $destination_id,
  43. $date,
  44. $month,
  45. $year,
  46. $type,
  47. $deleted,
  48. $financial_movement,
  49. $amount,
  50. $vat,
  51. $virtual,
  52. $note,
  53. $parent,
  54. $corrispettivo_fiscale,
  55. $commercial, $update = false, $add = false;
  56. public $corrispettivo = [];
  57. public $corrispettivo_destination = [];
  58. public $currentReceip;
  59. public $filterMember = 0, $filterPaymentMethod = 0, $filterCausals = 0, $filterFrom = '', $filterTo = '', $filterCommercial = 0;
  60. public $hasFilter = false;
  61. public $total = 0;
  62. public $selectedFilter = '';
  63. public $multipleIds = [];
  64. public $multipleAction = '';
  65. public $first = true;
  66. public $selectId = 0;
  67. public $refreshAfter = 0;
  68. public $canSave = true;
  69. public $newMemberFirstName = '';
  70. public $newMemberLastName = '';
  71. public $newMemberEmail = '';
  72. public $newMemberToComplete = false;
  73. public $newMemberFiscalCode = '';
  74. public $newMemberFiscalCodeExist = false;
  75. public $newMemberExists = false;
  76. public $causals = array();
  77. public $payments = array();
  78. public $banks = array();
  79. public $members = array();
  80. public $vats = array();
  81. public $rows = array();
  82. protected $rules = [
  83. 'member_id' => 'required',
  84. 'payment_method_id' => 'required',
  85. 'rows.*.causal_id' => 'required',
  86. 'rows.*.amount' => 'required'
  87. ];
  88. protected $messages = [
  89. 'member_id.required' => 'La persona è obbligatorio',
  90. 'payment_method_id.required' => 'Il metodo di pagamento è obbligatorio',
  91. 'rows.*.causal_id.required' => 'La causale è obbligatoria',
  92. 'rows.*.amount.required' => 'L\'importo è obbligatorio',
  93. ];
  94. public function updatedMemberId() {
  95. if ($this->member_id > 0)
  96. {
  97. $member = \App\Models\Member::findOrFail($this->member_id);
  98. if (!$member->isAdult())
  99. {
  100. if($member->father_name != '')
  101. $this->parent = $member->father_name . " (C.F. " . $member->father_fiscal_code . ")";
  102. else if($member->mother_name != '')
  103. $this->parent = $member->mother_name . " (C.F. " . $member->mother_fiscal_code . ")";
  104. else
  105. $this->parent = '';
  106. }
  107. $this->virtual = $member->getMoney();
  108. $this->newMemberFirstName = '';
  109. $this->newMemberLastName = '';
  110. $this->newMemberEmail = '';
  111. $this->newMemberToComplete = false;
  112. $this->newMemberFiscalCode = '';
  113. $this->newMemberFiscalCodeExist = false;
  114. }
  115. $this->emit('refresh');
  116. }
  117. public function updatedCausalId() {
  118. //$this->emit('refresh');
  119. }
  120. public function updatedDate() {
  121. //$this->emit('refresh');
  122. }
  123. public function hydrate()
  124. {
  125. $this->emit('load-select');
  126. }
  127. /*public function updated() {
  128. $this->emit('refresh');
  129. }*/
  130. public function updatedPaymentMethodId() {
  131. //$this->emit('refresh');
  132. if ($this->payment_method_id) {
  133. $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
  134. $this->destination_id = $payment_method->destination_id ?? null;
  135. }
  136. $this->canSave = $this->checkCanSave();
  137. }
  138. public function updatedCorrispettivoFiscale() {
  139. if ($this->corrispettivo_fiscale) {
  140. foreach ($this->payments as $payment) {
  141. if ($payment->corrispettivo_fiscale) {
  142. $this->corrispettivo_destination[$payment->id] = $payment->destination_id;
  143. }
  144. }
  145. }
  146. }
  147. public function updatedAmount() {
  148. // $this->emit('refresh');
  149. $this->canSave = $this->checkCanSave();
  150. }
  151. public function updatedCommercial($value)
  152. {
  153. $this->emitSelf('refreshMembers');
  154. }
  155. public function checkCanSave()
  156. {
  157. $ret = true;
  158. if ($this->payment_method_id != null)
  159. {
  160. $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
  161. if ($payment_method->money)
  162. {
  163. $from = str_replace(".", "", str_replace(",", "", number_format($this->virtual, 2)));
  164. $this->from = $this->virtual;
  165. $from = $this->from;
  166. $to = str_replace(".", "", str_replace(",", "", $this->currencyToDouble($this->amount)));
  167. $this->to = $this->amount;
  168. $to = $this->to;
  169. $ret = $from >= $to;
  170. }
  171. }
  172. return $ret;
  173. }
  174. public function setAmount()
  175. {
  176. $tot = 0;
  177. foreach($this->rows as $r)
  178. {
  179. $tot += $this->currencyToDouble($r["amount"]);
  180. }
  181. $this->amount = $tot;
  182. $this->canSave = $this->checkCanSave();
  183. }
  184. public function refreshMembers()
  185. {
  186. if ($this->add || $this->update)
  187. {
  188. if($this->commercial){
  189. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])
  190. ->where(function($query) {
  191. $query->where('is_archived', false)
  192. ->orWhereNull('is_archived');
  193. })
  194. ->where(function($query) {
  195. $query->where('is_deleted', false)
  196. ->orWhereNull('is_deleted');
  197. })->orderBy('last_name')->orderBy('first_name')->get();
  198. } else {
  199. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->where('current_status', 2)->orWhere('current_status', 1)
  200. ->where(function($query) {
  201. $query->where('is_archived', false)
  202. ->orWhereNull('is_archived');
  203. })
  204. ->where(function($query) {
  205. $query->where('is_deleted', false)
  206. ->orWhereNull('is_deleted');
  207. })->orderBy('last_name')->orderBy('first_name')->get();
  208. }
  209. }
  210. else
  211. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])
  212. ->where(function($query) {
  213. $query->where('is_archived', false)
  214. ->orWhereNull('is_archived');
  215. })
  216. ->where(function($query) {
  217. $query->where('is_deleted', false)
  218. ->orWhereNull('is_deleted');
  219. })->orderBy('last_name')->orderBy('first_name')->get();
  220. }
  221. public function resetFields(){
  222. $this->member_id = null;
  223. $this->supplier_id = null;
  224. $this->payment_method_id = null;
  225. $this->destination_id = null;
  226. $this->commercial = 0;
  227. $this->corrispettivo_fiscale = false;
  228. $this->date = date("Y-m-d");
  229. $this->type = 'IN';
  230. $this->deleted = false;
  231. $this->financial_movement = false;
  232. $this->newMemberFirstName = '';
  233. $this->newMemberLastName = '';
  234. $this->newMemberEmail = '';
  235. $this->newMemberToComplete = false;
  236. $this->newMemberFiscalCode = '';
  237. $this->newMemberFiscalCodeExist = false;
  238. $this->newMemberExists = false;
  239. $this->currentReceip = null;
  240. $this->parent = '';
  241. $this->courseId = 0;
  242. $this->months = array();
  243. $this->rows = array();
  244. $this->rows[] = array('causal_id' => isset($_GET["causalId"]) ? $_GET["causalId"] : null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'vat_id' => null, 'note' => '', 'commercial' => 0);
  245. $this->corrispettivo = [];
  246. $this->corrispettivo_destination = [];
  247. $this->emit('load-data-table');
  248. }
  249. public function getMemberProperty()
  250. {
  251. $ret = null;
  252. if ($this->member_id > 0)
  253. {
  254. $ret = \App\Models\Member::findOrFail($this->member_id);
  255. }
  256. return $ret;
  257. }
  258. public function getCausalProperty()
  259. {
  260. $ret = null;
  261. if ($this->causal_id > 0)
  262. {
  263. $ret = \App\Models\Causal::findOrFail($this->causal_id);
  264. }
  265. return $ret;
  266. }
  267. public function getCausal($causal)
  268. {
  269. $ret = '';
  270. if ($causal > 0)
  271. {
  272. $ret = \App\Models\Causal::findOrFail($causal)->getTree();
  273. }
  274. return $ret;
  275. }
  276. public function getPaymentMethod($payment)
  277. {
  278. $ret = '';
  279. if ($payment > 0)
  280. {
  281. $ret = \App\Models\PaymentMethod::findOrFail($payment)->name;
  282. }
  283. return $ret;
  284. }
  285. public function getBankName($bank_id)
  286. {
  287. $ret = '';
  288. if ($bank_id > 0)
  289. {
  290. $ret = \App\Models\Bank::findOrFail($bank_id)->name;
  291. }
  292. return $ret;
  293. }
  294. function buildTree($records, $parentId = 0) {
  295. $this->causals = array();
  296. foreach ($records as $record)
  297. {
  298. if ($record->parent_id == $parentId)
  299. {
  300. $children = $this->buildTree($record, $record->id);
  301. if ($children) {
  302. $record->children = $children;
  303. }
  304. $this->causals[] = $record;
  305. }
  306. }
  307. return $this->causals;
  308. }
  309. public function getCausale($records, $indentation)
  310. {
  311. foreach($records as $record)
  312. {
  313. $this->causals[] = array('id' => $record->id, 'name' => $record->getTree(), 'text' => $record->getTree(), 'level' => $indentation);
  314. if(count($record->childs))
  315. $this->getCausale($record->childs, $indentation + 1);
  316. }
  317. }
  318. public function mount()
  319. {
  320. if (isset($_GET["from"]))
  321. {
  322. $this->fromPage = $_GET["from"];
  323. }
  324. $this->causals = array();
  325. $this->multiMonthFrom = date("n");
  326. $this->multiYearFrom = date("Y");
  327. $this->multiMonthTo = date("n");
  328. $this->multiYearTo = date("Y");
  329. $fisc = \App\Models\Causal::where('corrispettivo_fiscale', true)->first();
  330. if ($fisc)
  331. $this->corrispettivo_causal_id = $fisc->id;
  332. $this->getCausale(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'IN')->get(), 0);
  333. //$this->buildTree(\App\Models\Causal::all(), null);
  334. $this->refreshMembers();
  335. $this->payments = \App\Models\PaymentMethod::select('id', 'name')->where('enabled', true)->whereIn('type', array('ALL', 'IN'))->orderBy('name')->get();
  336. $this->banks = \App\Models\Bank::select('id', 'name')->where('enabled', true)->whereIn('visibility', array('ALL', 'IN'))->orderBy('name')->get();
  337. $this->vats = \App\Models\Vat::select('id', 'name', 'value')->orderBy('value')->get();
  338. if ($this->first)
  339. {
  340. if (isset($_GET["new"]))
  341. {
  342. $this->refreshAfter = 1;
  343. $this->add();
  344. }
  345. if (isset($_GET["memberId"]))
  346. {
  347. $this->refreshAfter = 1;
  348. $this->member_id = $_GET["memberId"];
  349. $member = \App\Models\Member::findOrFail($this->member_id);
  350. if (!$member->isAdult())
  351. {
  352. if($member->father_name != '')
  353. $this->parent = $member->father_name . " (C.F. " . $member->father_fiscal_code . ")";
  354. else if($member->mother_name != '')
  355. $this->parent = $member->mother_name . " (C.F. " . $member->mother_fiscal_code . ")";
  356. else
  357. $this->parent = '';
  358. }
  359. }
  360. if (isset($_GET["causalId"]))
  361. {
  362. $this->refreshAfter = 1;
  363. $this->causal_id = $_GET["causalId"];
  364. }
  365. if (isset($_GET["id"]))
  366. {
  367. $this->refreshAfter = 1;
  368. $this->edit($_GET["id"]);
  369. }
  370. $count = 0;
  371. if (isset($_GET["courseId"]))
  372. {
  373. $this->courseId = $_GET["courseId"];
  374. $mc = \App\Models\MemberCourse::findOrFail($this->courseId);
  375. $course = \App\Models\Course::findOrFail($mc->course_id);
  376. }
  377. if (isset($_GET["months"]))
  378. {
  379. $price = $_GET["price"] / 100 * 100;
  380. $this->refreshAfter = 1;
  381. $desc = "Pagamento";
  382. $months = explode("|", $_GET["months"]);
  383. $this->months = $months;
  384. $this->rows[0]["amount"] = formatPrice($price * sizeof($months));
  385. foreach($months as $idx => $m)
  386. {
  387. $this->rows[0]["when"][$idx]["month"] = $m;
  388. $currentYear = date('Y');
  389. $currentMonth = date('n');
  390. if ($currentMonth > env('FISCAL_YEAR_MONTH_TO', 1) && $m > env('FISCAL_YEAR_MONTH_TO', 1)) { // current 9-12 && $m 9-12
  391. $currentYear = $currentYear;
  392. } elseif ($currentMonth > env('FISCAL_YEAR_MONTH_TO', 1) && $m < env('FISCAL_YEAR_MONTH_FROM', 1)) { // current 9-12 && $m 1-8
  393. $currentYear += 1;
  394. } elseif ($currentMonth < env('FISCAL_YEAR_MONTH_FROM', 1) && $m > env('FISCAL_YEAR_MONTH_TO', 1)) { // current 1-8 && $m 9-12
  395. $currentYear -= 1;
  396. } elseif ($currentMonth < env('FISCAL_YEAR_MONTH_FROM', 1) && $m < env('FISCAL_YEAR_MONTH_FROM', 1)) { // current 1-8 && $m 1-8
  397. $currentYear = $currentYear;
  398. }
  399. $this->rows[0]["when"][$idx]["year"] = $currentYear;
  400. // $currentYear = date('Y');
  401. // $currentMonth = date('n');
  402. // // L'anno fiscale "corrente" (anno di fine) in base ad oggi
  403. // // Se siamo da Sep(9) a Dec(12), l'FY finisce l'anno prossimo.
  404. // $currentFY = ($currentMonth >= env('FISCAL_YEAR_MONTH_FROM', 1)) ? ($currentYear + 1) : $currentYear;
  405. // // Se il mese richiesto è Sep-Dec, appartiene all'FY "successivo" rispetto a Jan-Aug.
  406. // // Rispetto all'FY corrente, i mesi Sep-Dec sono nel "blocco" che può essere
  407. // // dell'anno fiscale corrente o del prossimo, a seconda di dove siamo oggi.
  408. // $requestedIsSepToDec = ($m >= env('FISCAL_YEAR_MONTH_FROM', 1));
  409. // // Se oggi siamo Jan-Aug (FY = anno corrente) e chiedi Sep-Dec,
  410. // // allora stai chiedendo l'inizio del prossimo FY.
  411. // if ($currentMonth <= env('FISCAL_YEAR_MONTH_TO', 1) && $requestedIsSepToDec) {
  412. // $this->rows[0]["when"][$idx]["year"] = $currentFY + 1;
  413. // }
  414. // else {
  415. // $this->rows[0]["when"][$idx]["year"] = $currentFY;
  416. // }
  417. // if ((date("n") + 7) > env('FISCAL_YEAR_MONTH_TO', 1)) { // mese attuale nel primo periodo fiscale [9-12]
  418. // $this->rows[0]["when"][$idx]["year"] = $m < env('FISCAL_YEAR_MONTH_TO', 1) ? (date("Y") + 1) : date("Y");
  419. // } else { // mese attuale nel secondo periodo fiscale [1-8]
  420. // $this->rows[0]["when"][$idx]["year"] = $m < env('FISCAL_YEAR_MONTH_TO', 1) ? date("Y") : (date("Y") - 1);
  421. // }
  422. if ($idx > 0)
  423. {
  424. if ($this->rows[0]["when"][$idx - 1]["year"] != $this->rows[0]["when"][$idx]["year"])
  425. $desc .= " " . $this->rows[0]["when"][$idx - 1]["year"] . " ";
  426. }
  427. $desc .= " " . $this->getMonth($m);// . " " . $this->rows[0]["when"][$idx]["year"];
  428. }
  429. $desc .= " " . $this->rows[0]["when"][$idx]["year"];
  430. $this->rows[0]["note"] = $desc;
  431. $count += 1;
  432. }
  433. if (isset($_GET["createSubscription"]) && $_GET["createSubscription"] == 1)
  434. {
  435. $this->createSubscription = 1;
  436. $this->courseId = $_GET["courseId"];
  437. $price = $_GET["subscription_price"] / 100 * 100;
  438. $this->refreshAfter = 1;
  439. if ($count == 1)
  440. $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'vat_id' => null, 'note' => '', 'commercial' => 0);
  441. $this->rows[$count]["causal_id"] = null;
  442. $this->rows[$count]["amount"] = formatPrice($price);
  443. $this->rows[$count]["note"] = "Pagamento iscrizione " . $course->name;
  444. if (isset($_GET["subCausalId"]))
  445. {
  446. $this->refreshAfter = 1;
  447. $this->rows[$count]["causal_id"] = $_GET["subCausalId"];
  448. }
  449. }
  450. }
  451. $this->first = false;
  452. $this->newMemberExists = false;
  453. }
  454. public function search()
  455. {
  456. $this->hasFilter = true;
  457. }
  458. public function disableSearch()
  459. {
  460. $this->filterMember = 0;
  461. $this->filterPaymentMethod = 0;
  462. $this->filterCausals = 0;
  463. $this->filterTo = '';
  464. $this->filterFrom = '';
  465. $this->filterCommercial = 0;
  466. $this->hasFilter = false;
  467. $this->total = 0;
  468. }
  469. public function render()
  470. {
  471. $datas = [];
  472. return view('livewire.records_in', ['datas' => $datas]);
  473. }
  474. public function executeMultipleAction(){
  475. if ($this->multipleAction == 'delete')
  476. $this->multipleDelete();
  477. }
  478. public function add()
  479. {
  480. $this->emit('load-select');
  481. //if ($this->hasFilter)
  482. $this->emit('hide-search');
  483. $this->resetFields();
  484. $this->dataId = 0;
  485. $this->add = true;
  486. $this->update = false;
  487. $this->emit('setEdit', true);
  488. }
  489. public function storeCorrispettivo()
  490. {
  491. $this->emit('refresh');
  492. $rules = [
  493. 'member_id' => 'required'
  494. ];
  495. $this->validate($rules);
  496. foreach($this->payments as $p)
  497. {
  498. if ($p->corrispettivo_fiscale)
  499. {
  500. $price = isset($this->corrispettivo[$p->id]) ? $this->currencyToDouble($this->corrispettivo[$p->id]) : 0;
  501. $destination_id = isset($this->corrispettivo_destination[$p->id]) ? $this->corrispettivo_destination[$p->id] : null;
  502. if ($price > 0)
  503. {
  504. $record = \App\Models\Record::create([
  505. 'member_id' => $this->member_id,
  506. 'supplier_id' => null,
  507. 'payment_method_id' => $p->id,
  508. 'destination_id' => $destination_id,
  509. 'commercial' => $this->commercial,
  510. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  511. 'date' => $this->date,
  512. 'type' => $this->type,
  513. 'amount' => $price,
  514. 'financial_movement' => $this->financial_movement,
  515. 'deleted' => $this->deleted
  516. ]);
  517. \App\Models\RecordRow::create([
  518. 'record_id' => $record->id,
  519. 'causal_id' => $this->corrispettivo_causal_id,
  520. 'note' => '',
  521. 'amount' => $price,
  522. 'vat_id' => null,
  523. 'commercial' => $this->commercial,
  524. 'when' => json_encode([])
  525. ]);
  526. }
  527. }
  528. }
  529. $this->resetFields();
  530. $this->add = false;
  531. $this->isDuplicate = false;
  532. $this->emit('setEdit', false);
  533. }
  534. public function store($generate)
  535. {
  536. $this->emit('refresh');
  537. $rules = [
  538. 'payment_method_id' => 'required',
  539. 'rows.*.causal_id' => 'required',
  540. 'rows.*.amount' => 'required'
  541. ];
  542. /*$f = false;
  543. foreach($this->rows as $row)
  544. {
  545. if ($row["commercial"]) $f = true;
  546. }
  547. if ($f)
  548. $rules["member_id"] = 'required';*/
  549. if(!$this->commercial)
  550. $rules["member_id"] = 'required';
  551. //dd($this->getErrorBag());
  552. /*$validator = Validator::make($request->all(), $rules);
  553. if ($validator->fails())
  554. {
  555. $this->emit('goToAnchor');
  556. }*/
  557. //if (!$this->validate($rules))
  558. //$this->emit('goToAnchor');
  559. $this->validate($rules);
  560. //dd("£ASDSDFS");
  561. try {
  562. $record = \App\Models\Record::create([
  563. 'member_id' => $this->member_id,
  564. 'supplier_id' => $this->supplier_id,
  565. 'payment_method_id' => $this->payment_method_id,
  566. 'destination_id' => $this->destination_id,
  567. 'commercial' => $this->commercial,
  568. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  569. 'date' => $this->date,
  570. 'type' => $this->type,
  571. 'financial_movement' => $this->financial_movement,
  572. 'deleted' => $this->deleted
  573. ]);
  574. $this->dataId = $record->id;
  575. $no_receipt_causal = false;
  576. // Inserisco le righe
  577. $tot = 0;
  578. $vat = 0;
  579. foreach($this->rows as $row)
  580. {
  581. foreach($row["when"] as $x => $y)
  582. {
  583. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  584. }
  585. \App\Models\RecordRow::create([
  586. 'record_id' => $this->dataId,
  587. 'causal_id' => $row["causal_id"],
  588. 'note' => $row["note"],
  589. 'amount' => $this->currencyToDouble($row["amount"]),
  590. 'vat_id' => $row["vat_id"],
  591. 'commercial' => $this->commercial,
  592. 'when' => json_encode($row["when"])
  593. ]);
  594. $tot += $this->currencyToDouble($row["amount"]);
  595. $vat += getVatValue($this->currencyToDouble($row["amount"]), $row["vat_id"]);
  596. $cau = \App\Models\Causal::findOrFail($row["causal_id"]);
  597. if ($cau->no_receipt)
  598. $no_receipt_causal = true;
  599. // Se la causale ricarica a borsellino, se il totale supera y euro, inserisco x euro gratis
  600. if ($cau->money)
  601. {
  602. if ($this->currencyToDouble($row["amount"]) >= env('MIN_MONEY', 100))
  603. {
  604. $money = new \App\Models\Money();
  605. $money->member_id = $this->member_id;
  606. $money->record_id = $this->dataId;
  607. $money->amount = env('EXTRA_MONEY', 10);
  608. $money->date = date("Y-m-d");
  609. $money->note = env('EXTRA_MONEY', 10) . ' in più per aver caricato ' . $this->currencyToDouble($row["amount"]);
  610. $money->save();
  611. }
  612. }
  613. }
  614. $record->amount = $tot;
  615. $record->save();
  616. // se sto pagando un corso, aggiorno lo status dei mesi
  617. if ($this->courseId > 0)
  618. {
  619. $record->months = json_encode($this->months);
  620. $record->member_course_id = $this->courseId;
  621. $record->save();
  622. $c = \App\Models\MemberCourse::findOrFail($this->courseId);
  623. $xxx = json_decode($c->months);
  624. foreach($xxx as $idx => $mm)
  625. {
  626. if (in_array($mm->m, $this->months))
  627. {
  628. $xxx[$idx]->status = 1;
  629. }
  630. }
  631. $c->months = json_encode($xxx);
  632. if ($this->createSubscription == 1)
  633. $c->subscribed = true;
  634. $c->save();
  635. }
  636. if (!$this->financial_movement)
  637. {
  638. $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
  639. if (!$payment_method->money)
  640. {
  641. if ($generate && !$no_receipt_causal)
  642. $this->createReceipt();
  643. }
  644. }
  645. session()->flash('success','Movimento creato');
  646. $this->resetFields();
  647. $this->add = false;
  648. $this->isDuplicate = false;
  649. $this->emit('setEdit', false);
  650. } catch (\Exception $ex) {
  651. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  652. }
  653. }
  654. public function duplicate($id){
  655. $record = \App\Models\Record::findOrFail($id);
  656. $newRecord = $record->replicate();
  657. $newRecord->save();
  658. $rows = \App\Models\RecordRow::where('record_id', $id)->get();
  659. foreach($rows as $r)
  660. {
  661. $newRow = $r->replicate();
  662. $newRow->record_id = $newRecord->id;
  663. $newRow->save();
  664. }
  665. $this->edit($newRecord->id);
  666. $this->isDuplicate = true;
  667. }
  668. public function edit($id){
  669. if (!isset($_GET["from"]) && $this->fromPage == '')
  670. $this->fromPage = 'in';
  671. $this->emit('setEdit', true);
  672. //if ($this->hasFilter)
  673. $this->emit('hide-search');
  674. $this->emit('load-select');
  675. try {
  676. $record = \App\Models\Record::findOrFail($id);
  677. if( !$record) {
  678. session()->flash('error','Movimento non trovato');
  679. } else {
  680. $this->member_id = $record->member_id;
  681. $this->supplier_id = $record->supplier_id;
  682. $this->payment_method_id = $record->payment_method_id;
  683. $this->destination_id = $record->destination_id;
  684. $this->amount = $record->amount;
  685. $this->commercial = $record->commercial;
  686. $this->corrispettivo_fiscale = $record->corrispettivo_fiscale;
  687. $this->date = date("Y-m-d", strtotime($record->date));
  688. $this->type = $record->type;
  689. $this->deleted = $record->deleted;
  690. $this->financial_movement = $record->financial_movement;
  691. $this->dataId = $record->id;
  692. $this->update = true;
  693. $this->add = false;
  694. $this->rows = \App\Models\RecordRow::where('record_id', $this->dataId)->select('causal_id', 'note', 'commercial', 'when', 'amount', 'vat_id')->get()->toArray();
  695. foreach($this->rows as $i => $r)
  696. {
  697. $this->rows[$i]['amount'] = formatPrice($this->rows[$i]['amount']);
  698. $this->rows[$i]['vat_id'] = $this->rows[$i]['vat_id'];
  699. $this->rows[$i]['when'] = json_decode($this->rows[$i]['when']);
  700. }
  701. $exist = \App\Models\Receipt::where('record_id', $id)->orderBy('id', 'DESC')->first();
  702. if ($exist != null)
  703. {
  704. $this->currentReceip = $exist;
  705. $this->parent = $this->currentReceip->parent;
  706. }
  707. }
  708. } catch (\Exception $ex) {
  709. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  710. }
  711. }
  712. public function update($generate = false)
  713. {
  714. $this->emit('refresh');
  715. $rules = [
  716. 'payment_method_id' => 'required',
  717. 'rows.*.causal_id' => 'required',
  718. 'rows.*.amount' => 'required'
  719. ];
  720. if(!$this->commercial)
  721. $rules["member_id"] = 'required';
  722. $this->validate($rules);
  723. try {
  724. \App\Models\Record::whereId($this->dataId)->update([
  725. 'member_id' => $this->member_id,
  726. 'supplier_id' => $this->supplier_id,
  727. 'payment_method_id' => $this->payment_method_id,
  728. 'destination_id' => $this->destination_id,
  729. 'commercial' => $this->commercial,
  730. 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
  731. 'date' => date("Y-m-d", strtotime($this->date)),
  732. 'type' => $this->type,
  733. 'financial_movement' => $this->financial_movement,
  734. 'deleted' => $this->deleted,
  735. ]);
  736. $no_receipt_causal = false;
  737. $tot = 0;
  738. $vat = 0;
  739. // Elimino le righe
  740. \App\Models\RecordRow::where('record_id', $this->dataId)->delete();
  741. // Inserisco le righe
  742. foreach($this->rows as $row)
  743. {
  744. foreach($row["when"] as $x => $y)
  745. {
  746. $row["when"][$x]['period'] = $row["when"][$x]['month'] . "-" . $row["when"][$x]['year'];
  747. }
  748. \App\Models\RecordRow::create([
  749. 'record_id' => $this->dataId,
  750. 'causal_id' => $row["causal_id"],
  751. 'note' => $row["note"],
  752. 'vat_id' => $row["vat_id"],
  753. 'amount' => $this->currencyToDouble($row["amount"]),
  754. 'commercial' => $this->commercial,
  755. 'when' => json_encode($row["when"])
  756. ]);
  757. $tot += $this->currencyToDouble($row["amount"]);
  758. $vat += getVatValue($this->currencyToDouble($row["amount"]), $row["vat_id"]);
  759. $cau = \App\Models\Causal::findOrFail($row["causal_id"]);
  760. if ($cau->no_receipt)
  761. $no_receipt_causal = true;
  762. }
  763. $rec = \App\Models\Record::findOrFail($this->dataId);
  764. $rec->amount = $tot;
  765. $rec->save();
  766. if (!$this->financial_movement)
  767. {
  768. $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
  769. if (!$payment_method->money)
  770. {
  771. if ($generate && !$no_receipt_causal)
  772. $this->createReceipt();
  773. }
  774. }
  775. session()->flash('success','Movimento aggiornato');
  776. $this->resetFields();
  777. $this->update = false;
  778. $this->isDuplicate = false;
  779. $this->emit('setEdit', false);
  780. } catch (\Exception $ex) {
  781. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  782. }
  783. }
  784. public function rigenerate()
  785. {
  786. $this->emit('refresh');
  787. try {
  788. $record = \App\Models\Record::findOrFail($this->dataId);
  789. $newRecord = $record->replicate();
  790. $newRecord->date = date("Y-m-d");
  791. $newRecord->save();
  792. $recordRows = \App\Models\RecordRow::where('record_id', $this->dataId)->get();
  793. // Inserisco le righe
  794. foreach($recordRows as $row)
  795. {
  796. $newRow = $row->replicate();
  797. $newRow->record_id = $newRecord->id;
  798. $newRow->save();
  799. }
  800. $this->dataId = $newRecord->id;
  801. $this->createReceipt();
  802. session()->flash('success','Movimento aggiornato');
  803. $this->resetFields();
  804. $this->update = false;
  805. $this->isDuplicate = false;
  806. $this->emit('setEdit', false);
  807. } catch (\Exception $ex) {
  808. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  809. }
  810. }
  811. public function cancel()
  812. {
  813. // Se arrivo da duplica elimino
  814. if ($this->isDuplicate)
  815. {
  816. try{
  817. \App\Models\Record::find($this->dataId)->delete();
  818. session()->flash('success',"Movimento eliminato");
  819. }catch(\Exception $e){
  820. session()->flash('error','Errore (' . $e->getMessage() . ')');
  821. }
  822. }
  823. $this->isDuplicate = false;
  824. $this->add = false;
  825. $this->update = false;
  826. $this->resetFields();
  827. $this->emit('setEdit', false);
  828. return redirect()->to('/in');
  829. }
  830. public function delete($id)
  831. {
  832. try{
  833. $r = \App\Models\Record::find($id);
  834. // Annullo la ricevuta
  835. $receipt = \App\Models\Receipt::where('record_id', $r->id)->first();
  836. if($receipt)
  837. {
  838. $receipt->status = 99;
  839. sendReceiptDeleteEmail($receipt);
  840. $receipt->save();
  841. }
  842. if ($r->member_course_id > 0)
  843. {
  844. $months = json_decode($r->months);
  845. $c = \App\Models\MemberCourse::findOrFail($r->member_course_id);
  846. $xxx = json_decode($c->months);
  847. foreach($xxx as $idx => $mm)
  848. {
  849. if (in_array($mm->m, $months))
  850. {
  851. $xxx[$idx]->status = "";
  852. }
  853. }
  854. $c->months = json_encode($xxx);
  855. $c->save();
  856. }
  857. $r->deleted = true;
  858. $r->save();
  859. session()->flash('success','Movimento aggiornato');
  860. $this->resetFields();
  861. $this->update = false;
  862. $this->isDuplicate = false;
  863. $this->emit('setEdit', false);
  864. $this->emit('reload');
  865. //$this->emit('load-data-table');
  866. }catch(\Exception $ex){
  867. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  868. }
  869. }
  870. public function multipleDelete()
  871. {
  872. try{
  873. foreach($this->multipleIds as $id)
  874. {
  875. \App\Models\Record::find($id)->delete();
  876. }
  877. }catch(\Exception $e){
  878. session()->flash('error','Errore (' . $e->getMessage() . ')');
  879. }
  880. $this->multipleAction = '';
  881. }
  882. public function createMember()
  883. {
  884. $this->newMemberFiscalCodeExist = false;
  885. $this->validate([
  886. // 'newMemberFiscalCode'=>'required|max:16',
  887. 'newMemberFirstName'=>'required',
  888. 'newMemberLastName'=>'required',
  889. //'newMemberEmail'=>'required',
  890. ]);
  891. // Check fiscal code exist
  892. $exist = false;
  893. if ($this->newMemberFiscalCode != '')
  894. {
  895. $check = \App\Models\Member::where('fiscal_code', $this->newMemberFiscalCode)
  896. ->where(function($query) {
  897. $query->where('is_deleted', false)
  898. ->orWhereNull('is_deleted');
  899. })->get();
  900. $exist = $check->count() > 0;
  901. }
  902. if (!$exist)
  903. {
  904. $member = \App\Models\Member::create([
  905. 'first_name' => strtoupper($this->newMemberFirstName),
  906. 'last_name' => strtoupper($this->newMemberLastName),
  907. 'email' => strtoupper($this->newMemberEmail),
  908. 'to_complete' => $this->newMemberToComplete,
  909. 'fiscal_code' => $this->newMemberFiscalCode,
  910. 'status' => true
  911. ]);
  912. $this->member_id = $member->id;
  913. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])
  914. ->where(function($query) {
  915. $query->where('is_archived', false)
  916. ->orWhereNull('is_archived');
  917. })
  918. ->where(function($query) {
  919. $query->where('is_deleted', false)
  920. ->orWhereNull('is_deleted');
  921. })->get();
  922. // $this->emit('reloadMembers');
  923. $this->emit('refresh');
  924. $this->newMemberFirstName = '';
  925. $this->newMemberLastName = '';
  926. $this->newMemberEmail = '';
  927. $this->newMemberToComplete = false;
  928. $this->newMemberFiscalCode = '';
  929. $this->emit('saved');
  930. //$this->validate();
  931. $this->selectId++;
  932. }
  933. else
  934. {
  935. $this->newMemberFiscalCodeExist = true;
  936. }
  937. }
  938. public function createReceipt()
  939. {
  940. // Ulteriore controllo commerciale/non commerciale
  941. if (!$this->commercial)
  942. {
  943. $create = false;
  944. $receipt = \App\Models\Receipt::where('record_id', $this->dataId)->orderBy('id', 'DESC')->first();
  945. if ($receipt != null)
  946. {
  947. // Controllo lo stato, se 99 ne genero una nuova
  948. if ($receipt->status == 99)
  949. {
  950. $create = true;
  951. }
  952. }
  953. else
  954. {
  955. $create = true;
  956. }
  957. if ($create)
  958. {
  959. $number = 1;
  960. $exist = \App\Models\Receipt::where('year', date("Y"))->orderBy('number', 'DESC')->first();
  961. if ($exist != null)
  962. $number = $exist->number + 1;
  963. $receipt = \App\Models\Receipt::create([
  964. 'record_id' => $this->dataId,
  965. 'member_id' => $this->member_id,
  966. 'supplier_id' => $this->supplier_id,
  967. 'payment_method_id' => $this->payment_method_id,
  968. 'number' => $number,
  969. 'date' => $this->date,
  970. 'year' => date("Y"),
  971. 'type' => $this->type,
  972. 'parent' => $this->parent,
  973. 'status' => 1,
  974. ]);
  975. foreach($this->rows as $row)
  976. {
  977. \App\Models\ReceiptRow::create([
  978. 'receip_id' => $receipt->id,
  979. 'causal_id' => $row["causal_id"],
  980. 'note' => $row["note"],
  981. 'vat_id' => $row["vat_id"],
  982. 'amount' => $this->currencyToDouble($row["amount"]),
  983. 'commercial' => $row["commercial"],
  984. 'when' => json_encode($row["when"])
  985. ]);
  986. }
  987. $this->currentReceip = $receipt;
  988. sendReceiptEmail($receipt);
  989. /*
  990. // Invio mail
  991. $pdf = PDF::loadView('receipt', array('receipt' => $receipt));
  992. $pdfName = "ricevuta_" . $receipt->number . "_" . $receipt->year . ".pdf";
  993. Storage::put('public/pdf/' . $pdfName, $pdf->output());
  994. $email = \App\Models\Member::findOrFail($receipt->member_id)->email;
  995. if ($email != '')
  996. {
  997. Mail::to($email)->send(new \App\Mail\ReceipEmail([
  998. 'pdf' => 'public/pdf/' . $pdfName,
  999. 'number' => $receipt->number . "/" . $receipt->year
  1000. ]));
  1001. }
  1002. */
  1003. session()->flash('receipt',"Ricevuta " . $number . "/" . date("Y") . " creata correttamente");
  1004. // Apro la ricevuta
  1005. $this->emit('showReceipt', $this->currentReceip->id);
  1006. }
  1007. }
  1008. }
  1009. public function removeReceipt()
  1010. {
  1011. $receipt = \App\Models\Receipt::findOrFail($this->currentReceip->id);
  1012. $receipt->status = 99;
  1013. $receipt->save();
  1014. // cancellazione record associato
  1015. try{
  1016. $record = \App\Models\Record::find($receipt->record_id);
  1017. if ($record->member_course_id > 0)
  1018. {
  1019. $months = json_decode($record->months);
  1020. $c = \App\Models\MemberCourse::findOrFail($record->member_course_id);
  1021. $xxx = json_decode($c->months);
  1022. foreach($xxx as $idx => $mm)
  1023. {
  1024. if (in_array($mm->m, $months))
  1025. {
  1026. $xxx[$idx]->status = "";
  1027. }
  1028. }
  1029. $c->months = json_encode($xxx);
  1030. $c->save();
  1031. }
  1032. $record->deleted = true;
  1033. $record->save();
  1034. $this->emit('success');
  1035. } catch(\Exception $ex) {
  1036. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  1037. }
  1038. sendReceiptDeleteEmail($receipt);
  1039. $this->currentReceip = $receipt;
  1040. }
  1041. function currencyToDouble($val)
  1042. {
  1043. $x = str_replace("€", "", $val);
  1044. $x = str_replace(".", "", $x);
  1045. $x = str_replace(",", ".", $x);
  1046. return floatval(trim($x));
  1047. }
  1048. public function addRow()
  1049. {
  1050. $this->rows[] = array('causal_id' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')), 'amount' => null, 'vat_id' => null, 'note' => '', 'commercial' => 0);
  1051. $this->emit('load-select');
  1052. }
  1053. public function delRow($idx)
  1054. {
  1055. unset($this->rows[$idx]);
  1056. // $this->emit('load-select');
  1057. }
  1058. public function addPeriod($idx)
  1059. {
  1060. $x = sizeof($this->rows[$idx]['when']) - 1;
  1061. $newDate = \Carbon\Carbon::create($this->rows[$idx]['when'][$x]["year"] . "-" . $this->rows[$idx]['when'][$x]["month"] . '-01')->addMonth();
  1062. $this->rows[$idx]['when'][] = array('month' => $newDate->format("n"), 'year' => $newDate->format("Y"), 'period' => '');
  1063. }
  1064. public function delPeriod($idx, $xxx)
  1065. {
  1066. array_splice($this->rows[$idx]['when'], $xxx, $xxx);
  1067. //unset($this->rows[$idx]['when'][$xxx]);
  1068. // $this->emit('load-select');
  1069. }
  1070. public function getTotal()
  1071. {
  1072. $total = 0.00;
  1073. foreach($this->rows as $r)
  1074. {
  1075. if ($r["amount"] != null && $r["amount"] != "")
  1076. {
  1077. $total += $this->currencyToDouble($r["amount"]);
  1078. if ($r["vat_id"] > 0)
  1079. $total += getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
  1080. }
  1081. }
  1082. return formatPrice($total);
  1083. // $this->emit('load-select');
  1084. }
  1085. public function getTotalCorrispettivo()
  1086. {
  1087. $total = 0.00;
  1088. foreach($this->corrispettivo as $r)
  1089. {
  1090. $total += $this->currencyToDouble($r);
  1091. }
  1092. return formatPrice($total);
  1093. // $this->emit('load-select');
  1094. }
  1095. public function getPrice()
  1096. {
  1097. $total = 0.00;
  1098. foreach($this->rows as $r)
  1099. {
  1100. if ($r["amount"] != null && $r["amount"] != "")
  1101. $total += $this->currencyToDouble($r["amount"]);
  1102. }
  1103. return formatPrice($total);
  1104. // $this->emit('load-select');
  1105. }
  1106. public function getVat()
  1107. {
  1108. $total = 0.00;
  1109. foreach($this->rows as $r)
  1110. {
  1111. if ($r["amount"] != null && $r["amount"] != "" && $r["vat_id"] > 0)
  1112. $total += getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
  1113. }
  1114. return formatPrice($total);
  1115. // $this->emit('load-select');
  1116. }
  1117. public function getVats()
  1118. {
  1119. $vats = array();
  1120. foreach($this->rows as $r)
  1121. {
  1122. if ($r["amount"] != null && $r["amount"] != "" && $r["vat_id"] > 0)
  1123. {
  1124. $vat = getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
  1125. $vatName = "";
  1126. foreach($this->vats as $v)
  1127. {
  1128. if ($v->id == $r["vat_id"])
  1129. $vatName = $v->name;
  1130. }
  1131. if (isset($vats[$vatName]))
  1132. $vats[$vatName] += $vat;
  1133. else
  1134. $vats[$vatName] = $vat;
  1135. }
  1136. }
  1137. return $vats;
  1138. // $this->emit('load-select');
  1139. }
  1140. public function setCausal($id, $idx)
  1141. {
  1142. $this->rows[$idx]["causal_id"] = $id;
  1143. }
  1144. public function printReceipt()
  1145. {
  1146. //$pdf = PDF::loadView('pdf/receipt', array('datas' => $datas, 'from' => $x, 'to' => $y, 'who' => '', 'matricola' => $matricola));
  1147. $pdf = PDF::loadView('receipt', array('receipt' => $this->currentReceip));//->output();
  1148. $pdfName = "Ricevuta_" . $this->currentReceip->member->last_name . "_" . $this->currentReceip->number . "_" . $this->currentReceip->year . ".pdf";
  1149. return $pdf->stream();
  1150. /*return response()->streamDownload(
  1151. fn () => print($pdf),
  1152. "ricevuta_" . $this->currentReceip->number . "_" . $this->currentReceip->year . ".pdf"
  1153. );*/
  1154. /*return response()->streamDownload(function () {
  1155. echo $pdf->stream();
  1156. }, 'test.pdf');*/
  1157. }
  1158. public function multiPeriod()
  1159. {
  1160. $this->multiP = true;
  1161. }
  1162. public function multiPeriodCreate($idx)
  1163. {
  1164. $period = \Carbon\CarbonPeriod::create($this->multiYearFrom . '-' . $this->multiMonthFrom . '-01', '1 month', $this->multiYearTo . '-' . $this->multiMonthTo . '-01');
  1165. $this->rows[$idx]['when'] = [];
  1166. foreach ($period as $dt) {
  1167. if (!in_array(array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => ''), $this->rows[$idx]['when']))
  1168. $this->rows[$idx]['when'][] = array('month' => $dt->format("n"), 'year' => $dt->format("Y"), 'period' => '');
  1169. }
  1170. $this->multiP = false;
  1171. }
  1172. public function multiPeriodCancel()
  1173. {
  1174. $this->multiP = false;
  1175. }
  1176. public function getMonth($m)
  1177. {
  1178. $ret = '';
  1179. switch ($m) {
  1180. case 1:
  1181. $ret = 'Gennaio';
  1182. break;
  1183. case 2:
  1184. $ret = 'Febbraio';
  1185. break;
  1186. case 3:
  1187. $ret = 'Marzo';
  1188. break;
  1189. case 4:
  1190. $ret = 'Aprile';
  1191. break;
  1192. case 5:
  1193. $ret = 'Maggio';
  1194. break;
  1195. case 6:
  1196. $ret = 'Giugno';
  1197. break;
  1198. case 7:
  1199. $ret = 'Luglio';
  1200. break;
  1201. case 8:
  1202. $ret = 'Agosto';
  1203. break;
  1204. case 9:
  1205. $ret = 'Settembre';
  1206. break;
  1207. case 10:
  1208. $ret = 'Ottobre';
  1209. break;
  1210. case 11:
  1211. $ret = 'Novembre';
  1212. break;
  1213. case 12:
  1214. $ret = 'Dicembre';
  1215. break;
  1216. default:
  1217. $ret = '';
  1218. break;
  1219. }
  1220. return $ret;
  1221. }
  1222. public function setYear($idx, $xxx)
  1223. {
  1224. $currentYear = date('Y');
  1225. $currentMonth = date('n');
  1226. $m = $this->rows[$idx]["when"][$xxx]["month"];
  1227. if ($currentMonth > env('FISCAL_YEAR_MONTH_TO', 1) && $m > env('FISCAL_YEAR_MONTH_TO', 1)) { // current 9-12 && $m 9-12
  1228. $currentYear = $currentYear;
  1229. } elseif ($currentMonth > env('FISCAL_YEAR_MONTH_TO', 1) && $m < env('FISCAL_YEAR_MONTH_FROM', 1)) { // current 9-12 && $m 1-8
  1230. $currentYear += 1;
  1231. } elseif ($currentMonth < env('FISCAL_YEAR_MONTH_FROM', 1) && $m > env('FISCAL_YEAR_MONTH_TO', 1)) { // current 1-8 && $m 9-12
  1232. $currentYear -= 1;
  1233. } elseif ($currentMonth < env('FISCAL_YEAR_MONTH_FROM', 1) && $m < env('FISCAL_YEAR_MONTH_FROM', 1)) { // current 1-8 && $m 1-8
  1234. $currentYear = $currentYear;
  1235. }
  1236. $this->rows[$idx]["when"][$xxx]["year"] = $currentYear;
  1237. }
  1238. public function updatedNewMemberFirstName()
  1239. {
  1240. $this->checkUserExists();
  1241. }
  1242. public function updatedNewMemberLastName()
  1243. {
  1244. $this->checkUserExists();
  1245. }
  1246. private function checkUserExists(): void
  1247. {
  1248. $first = trim((string) $this->newMemberFirstName);
  1249. $last = trim((string) $this->newMemberLastName);
  1250. if ($first === '' || $last === '') {
  1251. $this->newMemberExists = false;
  1252. return;
  1253. }
  1254. $first = mb_strtolower($first);
  1255. $last = mb_strtolower($last);
  1256. $this->newMemberExists = \App\Models\Member::query()
  1257. ->where(function ($q) {
  1258. $q->where('is_deleted', false)
  1259. ->orWhereNull('is_deleted');
  1260. })
  1261. ->whereRaw('LOWER(TRIM(first_name)) = ?', [$first])
  1262. ->whereRaw('LOWER(TRIM(last_name)) = ?', [$last])
  1263. ->exists();
  1264. }
  1265. }