Sponsor.php 12 KB

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