RecordIN.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class RecordIN extends Component
  5. {
  6. public $records, $dataId, $member_id, $supplier_id,
  7. $causal_id,
  8. $payment_method_id,
  9. $date,
  10. $month,
  11. $year,
  12. $type,
  13. $amount,
  14. $virtual,
  15. $commercial, $update = false, $add = false;
  16. public $selectedFilter = 0;
  17. public $multipleIds = [];
  18. public $multipleAction = '';
  19. public $first = true;
  20. public $selectId = 0;
  21. public $refreshAfter = 0;
  22. public $canSave = true;
  23. public $newMemberFirstName = '';
  24. public $newMemberLastName = '';
  25. public $newMemberFiscalCode = '';
  26. public $newMemberFiscalCodeExist = false;
  27. public $causals = array();
  28. public $payments = array();
  29. public $members = array();
  30. protected $rules = [
  31. 'member_id' => 'required',
  32. 'payment_method_id' => 'required',
  33. 'causal_id' => 'required',
  34. 'amount' => 'required|numeric|gt:0'
  35. ];
  36. protected $messages = [
  37. 'member_id.required' => 'La persona è obbligatorio',
  38. 'payment_method_id.required' => 'Il metodo di pagamento è obbligatorio',
  39. 'causal_id.required' => 'La causale è obbligatoria',
  40. 'amount.required' => 'L\'importo è obbligatorio',
  41. ];
  42. public function updatedMemberId() {
  43. $this->emit('refresh');
  44. if ($this->member_id > 0)
  45. {
  46. $member = \App\Models\Member::findOrFail($this->member_id);
  47. $this->virtual = $member->getMoney();
  48. $this->newMemberFirstName = '';
  49. $this->newMemberLastName = '';
  50. $this->newMemberFiscalCode = '';
  51. $this->newMemberFiscalCodeExist = false;
  52. }
  53. }
  54. public function updatedCausalId() {
  55. //$this->emit('refresh');
  56. }
  57. public function updatedDate() {
  58. //$this->emit('refresh');
  59. }
  60. public function hydrate()
  61. {
  62. $this->emit('load-select');
  63. }
  64. /*public function updated() {
  65. $this->emit('refresh');
  66. }*/
  67. public function updatedPaymentMethodId() {
  68. //$this->emit('refresh');
  69. $this->canSave = $this->checkCanSave();
  70. }
  71. public function updatedAmount() {
  72. // $this->emit('refresh');
  73. $this->canSave = $this->checkCanSave();
  74. }
  75. public function checkCanSave()
  76. {
  77. $ret = true;
  78. if ($this->payment_method_id != null)
  79. {
  80. $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
  81. if ($payment_method->money)
  82. {
  83. $ret = $this->virtual >= $this->amount;
  84. }
  85. }
  86. return $ret;
  87. }
  88. public function resetFields(){
  89. $this->member_id = null;
  90. $this->supplier_id = null;
  91. $this->causal_id = null;
  92. $this->payment_method_id = null;
  93. $this->date = date("Y-m-d");
  94. $this->month = date("n");
  95. $this->year = date("Y");
  96. $this->type = 'IN';
  97. $this->amount = null;
  98. $this->commercial = 0;
  99. $this->newMemberFirstName = '';
  100. $this->newMemberLastName = '';
  101. $this->newMemberFiscalCode = '';
  102. $this->newMemberFiscalCodeExist = false;
  103. }
  104. public function getMemberProperty()
  105. {
  106. $ret = null;
  107. if ($this->member_id > 0)
  108. {
  109. $ret = \App\Models\Member::findOrFail($this->member_id);
  110. }
  111. return $ret;
  112. }
  113. public function getCausalProperty()
  114. {
  115. $ret = null;
  116. if ($this->causal_id > 0)
  117. {
  118. $ret = \App\Models\Causal::findOrFail($this->causal_id);
  119. }
  120. return $ret;
  121. }
  122. public function getCausale($records, $indentation)
  123. {
  124. foreach($records as $record)
  125. {
  126. $this->causals[] = array('id' => $record->id, 'name' => $record->getTree());
  127. if(count($record->childs))
  128. $this->getCausale($record->childs, $indentation + 1);
  129. }
  130. }
  131. public function mount()
  132. {
  133. $this->causals = array();
  134. $this->getCausale(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->where('type', 'IN')->get(), 0);
  135. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->orderBy('last_name')->orderBy('first_name')->get();
  136. $this->payments = \App\Models\PaymentMethod::select('id', 'name')->orderBy('name')->get();
  137. if ($this->first)
  138. {
  139. if (isset($_GET["new"]))
  140. {
  141. $this->refreshAfter = 1;
  142. $this->add();
  143. }
  144. if (isset($_GET["memberId"]))
  145. $this->member_id = $_GET["memberId"];
  146. if (isset($_GET["causalId"]))
  147. $this->causal_id = $_GET["causalId"];
  148. }
  149. $this->first = false;
  150. }
  151. public function render()
  152. {
  153. $fromDate = date("Y-m-d");
  154. $toDate = date("Y-m-d");
  155. if ($this->selectedFilter == 1)
  156. {
  157. $fromDate = date("Y-m-01");
  158. $toDate = date("Y-m-t");
  159. }
  160. if ($this->selectedFilter == 2)
  161. {
  162. $fromDate = date("Y-01-01");
  163. $toDate = date("Y-12-31");
  164. }
  165. if ($this->selectedFilter == 3)
  166. {
  167. $fromDate = date("2000-01-01");
  168. $toDate = date("Y-12-31");
  169. }
  170. $this->records = \App\Models\Record::where('type', 'IN')->whereBetween('date', [$fromDate, $toDate])->with('member', 'causal', 'payment_method')->orderBy('id', 'DESC')->get();
  171. return view('livewire.records_in');
  172. }
  173. public function executeMultipleAction(){
  174. if ($this->multipleAction == 'delete')
  175. $this->multipleDelete();
  176. }
  177. public function add()
  178. {
  179. $this->emit('load-select');
  180. $this->resetFields();
  181. $this->add = true;
  182. $this->update = false;
  183. }
  184. public function store($generate)
  185. {
  186. $this->emit('refresh');
  187. $this->validate();
  188. try {
  189. $record = \App\Models\Record::create([
  190. 'member_id' => $this->member_id,
  191. 'supplier_id' => $this->supplier_id,
  192. 'causal_id' => $this->causal_id,
  193. 'payment_method_id' => $this->payment_method_id,
  194. 'date' => $this->date,
  195. 'month' => $this->month,
  196. 'year' => $this->year,
  197. 'type' => $this->type,
  198. 'amount' => $this->amount,
  199. 'commercial' => $this->commercial,
  200. ]);
  201. $this->dataId = $record->id;
  202. if ($generate)
  203. $this->createReceipt();
  204. session()->flash('success','Movimento creato');
  205. $this->resetFields();
  206. $this->add = false;
  207. } catch (\Exception $ex) {
  208. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  209. }
  210. }
  211. public function duplicate($id){
  212. $record = \App\Models\Record::findOrFail($id);
  213. $newRecord = $record->replicate();
  214. $newRecord->save();
  215. $this->edit($newRecord->id);
  216. }
  217. public function edit($id){
  218. $this->emit('load-select');
  219. try {
  220. $record = \App\Models\Record::findOrFail($id);
  221. if( !$record) {
  222. session()->flash('error','Movimento non trovato');
  223. } else {
  224. $this->member_id = $record->member_id;
  225. $this->supplier_id = $record->supplier_id;
  226. $this->causal_id = $record->causal_id;
  227. $this->payment_method_id = $record->payment_method_id;
  228. $this->date = date("Y-m-d", strtotime($record->date));
  229. $this->month = $record->month;
  230. $this->year = $record->year;
  231. $this->type = $record->type;
  232. $this->amount = $record->amount;
  233. $this->commercial = $record->commercial;
  234. $this->dataId = $record->id;
  235. $this->update = true;
  236. $this->add = false;
  237. }
  238. } catch (\Exception $ex) {
  239. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  240. }
  241. }
  242. public function update($generate)
  243. {
  244. $this->emit('refresh');
  245. $this->validate();
  246. try {
  247. \App\Models\Record::whereId($this->dataId)->update([
  248. 'member_id' => $this->member_id,
  249. 'supplier_id' => $this->supplier_id,
  250. 'causal_id' => $this->causal_id,
  251. 'payment_method_id' => $this->payment_method_id,
  252. 'date' => date("Y-m-d", strtotime($this->date)),
  253. 'month' => $this->month,
  254. 'year' => $this->year,
  255. 'type' => $this->type,
  256. 'amount' => $this->amount,
  257. 'commercial' => $this->commercial,
  258. ]);
  259. if ($generate)
  260. $this->createReceipt();
  261. session()->flash('success','Movimento aggiornato');
  262. $this->resetFields();
  263. $this->update = false;
  264. } catch (\Exception $ex) {
  265. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  266. }
  267. }
  268. public function cancel()
  269. {
  270. $this->add = false;
  271. $this->update = false;
  272. $this->resetFields();
  273. }
  274. public function delete($id)
  275. {
  276. try{
  277. \App\Models\Record::find($id)->delete();
  278. session()->flash('success',"Movimento eliminato");
  279. }catch(\Exception $e){
  280. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  281. }
  282. }
  283. public function multipleDelete()
  284. {
  285. try{
  286. foreach($this->multipleIds as $id)
  287. {
  288. \App\Models\Record::find($id)->delete();
  289. }
  290. }catch(\Exception $e){
  291. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  292. }
  293. $this->multipleAction = '';
  294. }
  295. public function createMember()
  296. {
  297. $this->newMemberFiscalCodeExist = false;
  298. $this->validate([
  299. // 'newMemberFiscalCode'=>'required|max:16',
  300. 'newMemberFirstName'=>'required',
  301. 'newMemberLastName'=>'required'
  302. ]);
  303. // Check fiscal code exist
  304. $exist = false;
  305. if ($this->newMemberFiscalCode != '')
  306. {
  307. $check = \App\Models\Member::where('fiscal_code', $this->newMemberFiscalCode)->get();
  308. $exist = $check->count() > 0;
  309. }
  310. if (!$exist)
  311. {
  312. $member = \App\Models\Member::create([
  313. 'first_name' => $this->newMemberFirstName,
  314. 'last_name' => $this->newMemberLastName,
  315. 'fiscal_code' => $this->newMemberFiscalCode,
  316. 'status' => true
  317. ]);
  318. $this->member_id = $member->id;
  319. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->get();
  320. // $this->emit('reloadMembers');
  321. $this->emit('refresh');
  322. $this->newMemberFirstName = '';
  323. $this->newMemberLastName = '';
  324. $this->newMemberFiscalCode = '';
  325. $this->emit('saved');
  326. //$this->validate();
  327. $this->selectId++;
  328. }
  329. else
  330. {
  331. $this->newMemberFiscalCodeExist = true;
  332. }
  333. }
  334. public function createReceipt()
  335. {
  336. /*
  337. $receipt = \App\Models\Receipt::where('record_id', $this->dataId)->first();
  338. if ($receipt != null)
  339. {
  340. $receipt->update([
  341. 'member_id' => $this->member_id,
  342. 'supplier_id' => $this->supplier_id,
  343. 'causal_id' => $this->causal_id,
  344. 'payment_method_id' => $this->payment_method_id,
  345. 'date' => date("Y-m-d", strtotime($this->date)),
  346. 'month' => $this->month,
  347. 'year' => $this->year,
  348. 'type' => $this->type,
  349. 'amount' => $this->amount,
  350. 'commercial' => $this->commercial,
  351. ]);
  352. }
  353. else
  354. {
  355. $number = 1;
  356. $exist = \App\Models\Receipt::where('year', $this->year)->orderBy('number', 'DESC')->first();
  357. if ($exist != null)
  358. $number = $exist->number + 1;
  359. $receipt = \App\Models\Receipt::create([
  360. 'record_id' => $this->dataId,
  361. 'member_id' => $this->member_id,
  362. 'supplier_id' => $this->supplier_id,
  363. 'causal_id' => $this->causal_id,
  364. 'payment_method_id' => $this->payment_method_id,
  365. 'number' => $number,
  366. 'date' => $this->date,
  367. 'month' => $this->month,
  368. 'year' => $this->year,
  369. 'type' => $this->type,
  370. 'amount' => $this->amount,
  371. 'commercial' => $this->commercial,
  372. ]);
  373. }
  374. $data = [
  375. 'member' => $receipt->id
  376. ];
  377. $pdf = \PDF::loadView('partials.pdf_generate_connections', $data)->setPaper('a4', 'landscape')->output(); //
  378. return response()->streamDownload(
  379. fn() => print($pdf), 'export_protocol.pdf'
  380. );
  381. */
  382. }
  383. }