RecordIN.php 49 KB

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