Sponsor.php 11 KB

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