Sponsor.php 12 KB

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