Sponsor.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Livewire\WithFileUploads;
  5. class Sponsor extends Component
  6. {
  7. use WithFileUploads;
  8. public $records, $name, $fiscal_code, $vat, $address, $zip_code,$nation_id,$province_id,$city_id,$phone,$email,$enabled, $dataId, $update = false, $add = false;
  9. public $first_name, $last_name;
  10. public $from_date, $to_date, $subscription_date, $amount, $file, $file_old, $updateContract = false, $addContract = false, $contractId;
  11. public $isItaly = true;
  12. public $searchTxt;
  13. public $search;
  14. public $contracts = array();
  15. protected $rules = [
  16. 'name' => 'required'
  17. ];
  18. protected $messages = [
  19. 'name.required' => 'Il nome è obbligatorio'
  20. ];
  21. public function resetFields(){
  22. $this->name = '';
  23. $this->first_name = '';
  24. $this->last_name = '';
  25. $this->fiscal_code = '';
  26. $this->vat = '';
  27. $this->address = '';
  28. $this->zip_code = '';
  29. $this->nation_id = null;
  30. $this->province_id = null;
  31. $this->city_id = null;
  32. $this->phone = '';
  33. $this->email = '';
  34. $this->enabled = true;
  35. $this->contracts = array();
  36. $this->emit('load-data-table');
  37. }
  38. public function resetContract(){
  39. $this->from_date = '';
  40. $this->to_date = '';
  41. $this->subscription_date = '';
  42. $this->amount = 0;
  43. $this->file = '';
  44. $this->file_old = '';
  45. }
  46. public $sortField ='name';
  47. public $sortAsc = true;
  48. public function sortBy($field)
  49. {
  50. if($this->sortField === $field)
  51. {
  52. $this->sortAsc = ! $this->sortAsc;
  53. } else {
  54. $this->sortAsc = true;
  55. }
  56. $this->sortField = $field;
  57. }
  58. public function mount()
  59. {
  60. if (isset($_GET["new"]))
  61. $this->add();
  62. }
  63. public function search()
  64. {
  65. if ($this->searchTxt != '')
  66. {
  67. $this->search = $this->searchTxt;
  68. $this->showReset = true;
  69. }
  70. }
  71. public function resetSearch()
  72. {
  73. $this->showReset = false;
  74. $this->searchTxt = '';
  75. $this->search = $this->searchTxt;
  76. }
  77. public function render()
  78. {
  79. //if ($this->search != '')
  80. // $this->records = \App\Models\Sponsor::where('name', 'LIKE', '%' . $this->search . '%')->orWhere('first_name', 'LIKE', '%' . $this->search . '%')->orWhere('last_name', 'LIKE', '%' . $this->search . '%')->orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')->get();
  81. //else
  82. $this->records = \App\Models\Sponsor::get();
  83. $this->contracts = array();
  84. if ($this->dataId > 0)
  85. $this->contracts = \App\Models\SponsorContract::where('sponsor_id', $this->dataId)->orderBy('from_date', 'asc')->get();
  86. return view('livewire.sponsor');
  87. }
  88. public function hydrate()
  89. {
  90. $this->emit('load-select');
  91. }
  92. public function checkIsItaly()
  93. {
  94. $n = \App\Models\Nation::findOrFail($this->nation_id);
  95. $this->isItaly = $n->is_italy;
  96. }
  97. public function add()
  98. {
  99. $this->resetFields();
  100. $this->resetContract();
  101. $this->emit('load-select');
  102. $this->add = true;
  103. $this->update = false;
  104. }
  105. public function store()
  106. {
  107. $this->validate();
  108. try {
  109. \App\Models\Sponsor::create([
  110. 'name' => $this->name,
  111. 'first_name' => $this->first_name,
  112. 'last_name' => $this->last_name,
  113. 'fiscal_code' => $this->fiscal_code,
  114. 'vat' => $this->vat,
  115. 'address' => $this->address,
  116. 'zip_code' => $this->zip_code,
  117. 'nation_id' => $this->nation_id,
  118. 'province_id' => $this->province_id,
  119. 'city_id' => $this->city_id,
  120. 'phone' => $this->phone,
  121. 'email' => $this->email,
  122. 'enabled' => $this->enabled
  123. ]);
  124. session()->flash('success','Sponsor creato');
  125. $this->resetFields();
  126. $this->add = false;
  127. } catch (\Exception $ex) {
  128. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  129. }
  130. }
  131. public function edit($id){
  132. $this->emit('load-select');
  133. try {
  134. $sponsor = \App\Models\Sponsor::findOrFail($id);
  135. if( !$sponsor) {
  136. session()->flash('error','Sponsor non trovato');
  137. } else {
  138. $this->name = $sponsor->name;
  139. $this->first_name = $sponsor->first_name;
  140. $this->last_name = $sponsor->last_name;
  141. $this->fiscal_code = $sponsor->fiscal_code;
  142. $this->vat = $sponsor->vat;
  143. $this->address = $sponsor->address;
  144. $this->zip_code = $sponsor->zip_code;
  145. $this->nation_id = $sponsor->nation_id;
  146. $this->province_id = $sponsor->province_id;
  147. $this->city_id = $sponsor->city_id;
  148. $this->phone = $sponsor->phone;
  149. $this->email = $sponsor->email;
  150. $this->enabled = $sponsor->enabled;
  151. $this->dataId = $sponsor->id;
  152. $this->update = true;
  153. $this->add = false;
  154. $this->emit('load-provinces', $this->nation_id, 'provinceClass');
  155. $this->emit('load-cities', $this->province_id, 'cityClass');
  156. }
  157. } catch (\Exception $ex) {
  158. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  159. }
  160. }
  161. public function update()
  162. {
  163. $this->validate();
  164. try {
  165. \App\Models\Sponsor::whereId($this->dataId)->update([
  166. 'name' => $this->name,
  167. 'first_name' => $this->first_name,
  168. 'last_name' => $this->last_name,
  169. 'fiscal_code' => $this->fiscal_code,
  170. 'vat' => $this->vat,
  171. 'address' => $this->address,
  172. 'zip_code' => $this->zip_code,
  173. 'nation_id' => $this->nation_id,
  174. 'province_id' => $this->province_id,
  175. 'city_id' => $this->city_id,
  176. 'phone' => $this->phone,
  177. 'email' => $this->email,
  178. 'enabled' => $this->enabled
  179. ]);
  180. session()->flash('success','Sponsor aggiornato');
  181. $this->resetFields();
  182. $this->update = false;
  183. } catch (\Exception $ex) {
  184. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  185. }
  186. }
  187. public function cancel()
  188. {
  189. $this->add = false;
  190. $this->update = false;
  191. $this->resetFields();
  192. }
  193. public function delete($id)
  194. {
  195. try{
  196. \App\Models\Sponsor::find($id)->delete();
  197. session()->flash('success',"Sponsor eliminato");
  198. }catch(\Exception $e){
  199. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  200. }
  201. }
  202. public function addContract()
  203. {
  204. $this->resetContract();
  205. $this->addContract = true;
  206. $this->updateContract = false;
  207. }
  208. public function storeContract()
  209. {
  210. $this->validate();
  211. try {
  212. $name = '';
  213. try{
  214. if ($this->file)
  215. {
  216. $name = md5($this->file . microtime()).'.'.$this->file->extension();
  217. $this->file->storeAs('public', $name);
  218. }
  219. } catch (\Exception $ex) {
  220. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  221. }
  222. \App\Models\SponsorContract::create([
  223. 'sponsor_id' => $this->dataId,
  224. 'from_date' => $this->from_date,
  225. 'to_date' => $this->to_date,
  226. 'subscription_date' => $this->subscription_date,
  227. 'amount' => $this->amount,
  228. 'file' => $name
  229. ]);
  230. session()->flash('success','Contratto creato');
  231. $this->resetContract();
  232. $this->addContract = false;
  233. } catch (\Exception $ex) {
  234. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  235. }
  236. }
  237. public function editContract($id){
  238. // $this->emit('load-select');
  239. try {
  240. $sponsorContract = \App\Models\SponsorContract::findOrFail($id);
  241. if( !$sponsorContract) {
  242. session()->flash('error','Contratto non trovato');
  243. } else {
  244. $this->from_date = $sponsorContract->from_date ? date("Y-m-d", strtotime($sponsorContract->from_date)) : "";
  245. $this->to_date = $sponsorContract->to_date ? date("Y-m-d", strtotime($sponsorContract->to_date)) : "";
  246. $this->subscription_date = $sponsorContract->subscription_date ? date("Y-m-d", strtotime($sponsorContract->subscription_date)) : "";
  247. $this->amount = $sponsorContract->amount;
  248. $this->file_old = $sponsorContract->file;
  249. $this->contractId = $sponsorContract->id;
  250. $this->updateContract = true;
  251. $this->addContract = false;
  252. }
  253. } catch (\Exception $ex) {
  254. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  255. }
  256. }
  257. public function updateContract()
  258. {
  259. $this->validate();
  260. try {
  261. $name = '';
  262. try{
  263. if ($this->file)
  264. {
  265. $name = md5($this->file . microtime()).'.'.$this->file->extension();
  266. $this->file->storeAs('public', $name);
  267. }
  268. } catch (\Exception $ex) {
  269. //session()->flash('error','Errore (' . $ex->getMessage() . ')');
  270. }
  271. \App\Models\SponsorContract::whereId($this->contractId)->update([
  272. 'from_date' => $this->from_date,
  273. 'to_date' => $this->to_date,
  274. 'subscription_date' => $this->subscription_date,
  275. 'amount' => $this->amount,
  276. 'file' => $name != '' ? $name : $this->file_old,
  277. ]);
  278. session()->flash('success','Contratto aggiornato');
  279. $this->resetContract();
  280. $this->updateContract = false;
  281. } catch (\Exception $ex) {
  282. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  283. }
  284. }
  285. public function cancelContract()
  286. {
  287. $this->addContract = false;
  288. $this->updateContract = false;
  289. $this->resetContract();
  290. }
  291. public function deleteContract($id)
  292. {
  293. try{
  294. \App\Models\SponsorContract::find($id)->delete();
  295. session()->flash('success',"Contratto eliminato");
  296. }catch(\Exception $e){
  297. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  298. }
  299. }
  300. public function getNation($nation)
  301. {
  302. if ($nation > 0)
  303. {
  304. $ret = \App\Models\Nation::findOrFail($nation);
  305. return $ret->name;
  306. }
  307. return "";
  308. }
  309. public function getProvince($province)
  310. {
  311. if ($province > 0)
  312. {
  313. $ret = \App\Models\Province::findOrFail($province);
  314. return $ret->name;
  315. }
  316. return "";
  317. }
  318. public function getCity($city)
  319. {
  320. if ($city > 0)
  321. {
  322. $ret = \App\Models\City::findOrFail($city);
  323. return $ret->name;
  324. }
  325. return "";
  326. }
  327. }