Subscription.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace App\Http\Livewire;
  3. use App\Http\Middleware\TenantMiddleware;
  4. use Livewire\Component;
  5. use Illuminate\Support\Facades\Auth;
  6. class Subscription extends Component
  7. {
  8. protected $listeners = ['setCausal' => 'setCausal', 'setSubscriptionCausal' => 'setSubscriptionCausal'];
  9. public $records;
  10. public $dataId;
  11. public $update = false;
  12. public $add = false;
  13. public $name;
  14. public $causal_id;
  15. public $sub_causal_id;
  16. public $subscription_price;
  17. public $prices;
  18. public $enabled;
  19. public $msgPrices = '';
  20. public $course_subscriptions = [];
  21. public $typeIN = 'IN';
  22. public $setSubscriptionCausal = 'setSubscriptionCausal';
  23. protected $rules = [
  24. 'name' => 'required',
  25. 'subscription_price' => 'required|min:0|not_in:0'
  26. ];
  27. protected $messages = [
  28. 'name.required' => 'Il nome è obbligatorio',
  29. 'subscription_price.required' => 'Deve essere maggiore di zero',
  30. 'subscription_price.not_in' => 'Deve essere maggiore di zero',
  31. ];
  32. public function boot()
  33. {
  34. app(TenantMiddleware::class)->setupTenantConnection();
  35. }
  36. public function resetFields()
  37. {
  38. $this->name = '';
  39. $this->causal_id = null;
  40. $this->sub_causal_id = null;
  41. $this->subscription_price = 0;
  42. $this->prices = [];
  43. $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
  44. $this->enabled = true;
  45. $this->emit('load-data-table');
  46. }
  47. public function mount()
  48. {
  49. if (Auth::user()->level != env('LEVEL_ADMIN', 0))
  50. return redirect()->to('/dashboard');
  51. $this->course_subscriptions = \App\Models\CourseSubscription::select('*')->where('enabled', true)->orderBy('name', 'asc')->get();
  52. }
  53. public function render()
  54. {
  55. $this->records = \App\Models\Subscription::all();
  56. return view('livewire.subscription');
  57. }
  58. public function add()
  59. {
  60. $this->resetFields();
  61. $this->add = true;
  62. $this->update = false;
  63. $this->emit('setEdit', true);
  64. }
  65. public function store()
  66. {
  67. $this->validate();
  68. try {
  69. $this->msgPrices = '';
  70. if ($this->prices[0]['course_subscription_id'] == null)
  71. $this->msgPrices = 'Devi inserire almeno un prezzo';
  72. $subscriptions = array_column($this->prices, 'course_subscription_id');
  73. $unique_subscriptions = array_unique($subscriptions);
  74. if (count($subscriptions) != count($unique_subscriptions))
  75. $this->msgPrices = 'Non è possibile aggiungere più volte la stessa tipologia di pagamento';
  76. if ($this->msgPrices == '') {
  77. $subscription = new \App\Models\Subscription();
  78. $subscription->name = $this->name;
  79. $subscription->subscription_price = currencyToDouble($this->subscription_price);
  80. $subscription->prices = json_encode($this->prices);
  81. $subscription->enabled = $this->enabled;
  82. $causal = "PAGAMENTO ABBONAMENTO";
  83. $cp = \App\Models\Causal::where('name', $causal)->first();
  84. if (!$cp) {
  85. $cp = new \App\Models\Causal();
  86. $cp->name = $causal;
  87. $cp->type = "IN";
  88. $cp->parent_id = null;
  89. $cp->money = false;
  90. $cp->corrispettivo_fiscale = false;
  91. $cp->no_receipt = false;
  92. $cp->user_status = false;
  93. $cp->no_first = false;
  94. $cp->no_records = false;
  95. $cp->enabled = true;
  96. $cp->save();
  97. }
  98. $subscription->causal_id = $cp->id;
  99. $causal = "PAGAMENTO ISCRIZIONE";
  100. $ci = \App\Models\Causal::where('name', $causal)->first();
  101. if (!$ci) {
  102. $ci = new \App\Models\Causal();
  103. $ci->name = $causal;
  104. $ci->type = "IN";
  105. $ci->parent_id = null;
  106. $ci->money = false;
  107. $ci->corrispettivo_fiscale = false;
  108. $ci->no_receipt = false;
  109. $ci->user_status = false;
  110. $ci->no_first = false;
  111. $ci->no_records = false;
  112. $ci->enabled = true;
  113. $ci->save();
  114. }
  115. $subscription->sub_causal_id = $ci->id;
  116. $subscription->save();
  117. session()->flash('success', 'Abbonamento creato');
  118. $this->resetFields();
  119. $this->add = false;
  120. $this->emit('setEdit', false);
  121. }
  122. } catch (\Exception $ex) {
  123. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  124. }
  125. }
  126. public function edit($id)
  127. {
  128. $this->resetFields();
  129. try {
  130. $subscription = \App\Models\Subscription::findOrFail($id);
  131. if (!$subscription) {
  132. session()->flash('error', 'Abbonamento non trovato');
  133. } else {
  134. $this->name = $subscription->name;
  135. $this->enabled = $subscription->enabled;
  136. $this->causal_id = $subscription->causal_id;
  137. $this->sub_causal_id = $subscription->sub_causal_id;
  138. $this->subscription_price = formatPrice($subscription->subscription_price);
  139. $this->prices = array();
  140. if ($subscription->prices != null) {
  141. foreach (json_decode($subscription->prices) as $z) {
  142. $this->prices[] = array("course_subscription_id" => $z->course_subscription_id, "price" => $z->price);
  143. }
  144. } else {
  145. $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
  146. }
  147. $this->dataId = $subscription->id;
  148. $this->update = true;
  149. $this->add = false;
  150. }
  151. $this->emit('setEdit', true);
  152. } catch (\Exception $ex) {
  153. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  154. }
  155. }
  156. public function update()
  157. {
  158. $this->validate();
  159. try {
  160. $this->msgPrices = '';
  161. if ($this->prices[0]['course_subscription_id'] == null)
  162. $this->msgPrices = 'Devi inserire almeno un prezzo';
  163. $subscriptions = array_column($this->prices, 'course_subscription_id');
  164. $unique_subscriptions = array_unique($subscriptions);
  165. if (count($subscriptions) != count($unique_subscriptions))
  166. $this->msgPrices = 'Non è possibile aggiungere più volte la stessa tipologia di pagamento';
  167. if ($this->msgPrices == '') {
  168. \App\Models\Subscription::whereId($this->dataId)->update([
  169. 'name' => $this->name,
  170. 'causal_id' => $this->causal_id,
  171. 'sub_causal_id' => $this->sub_causal_id,
  172. 'subscription_price' => currencyToDouble($this->subscription_price),
  173. 'prices' => json_encode($this->prices),
  174. 'enabled' => $this->enabled
  175. ]);
  176. session()->flash('success', 'Abbonamento aggiornato');
  177. $this->resetFields();
  178. $this->update = false;
  179. $this->emit('setEdit', false);
  180. }
  181. } catch (\Exception $ex) {
  182. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  183. }
  184. }
  185. public function cancel()
  186. {
  187. $this->add = false;
  188. $this->update = false;
  189. $this->resetFields();
  190. $this->emit('setEdit', false);
  191. }
  192. public function delete($id)
  193. {
  194. try {
  195. \App\Models\Subscription::find($id)->delete();
  196. session()->flash('success', "Abbonamento eliminato");
  197. return redirect(request()->header('Referer'));
  198. } catch (\Exception $e) {
  199. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  200. }
  201. }
  202. public function setCausal($id, $idx)
  203. {
  204. $this->causal_id = $id;
  205. }
  206. public function setSubscriptionCausal($id, $idx)
  207. {
  208. $this->sub_causal_id = $id;
  209. }
  210. public function duplicate($id, $isMultiple)
  211. {
  212. $subscription = \App\Models\Subscription::findOrFail($id);
  213. $new_subscription = $subscription->replicate();
  214. $new_subscription->save();
  215. if (!$isMultiple)
  216. $this->edit($new_subscription->id);
  217. }
  218. public function duplicateMultiple($ids)
  219. {
  220. foreach ($ids as $id) {
  221. $this->duplicate($id, true);
  222. }
  223. return redirect()->to('/subscriptions');
  224. }
  225. public function addPrice()
  226. {
  227. $this->prices[] = array('course_subscription_id' => null, 'price' => 0);
  228. }
  229. public function delPrice($idx)
  230. {
  231. unset($this->prices[$idx]);
  232. }
  233. }