InvoiceController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Lib\Backend;
  4. use App\Models\AppSession;
  5. use App\Models\BillingReport;
  6. use App\Models\CareMonth;
  7. use App\Models\ClaimEDI;
  8. use App\Models\ClientProChange;
  9. use App\Models\Company;
  10. use App\Models\Customer;
  11. use App\Models\CustomerTransaction;
  12. use App\Models\GiftCard;
  13. use App\Models\Handout;
  14. use App\Models\Invoice;
  15. use App\Models\InvoiceTransaction;
  16. use App\Models\MBClaim;
  17. use App\Models\Measurement;
  18. use App\Models\Bill;
  19. use App\Models\Claim;
  20. use App\Models\Client;
  21. use App\Models\McpRequest;
  22. use App\Models\McCodeCheck;
  23. use App\Models\Note;
  24. use App\Models\Pack;
  25. use App\Models\Pro;
  26. use App\Models\Product;
  27. use App\Models\ProFavorite;
  28. use App\Models\ProGeneralAvailability;
  29. use App\Models\ProProAccess;
  30. use App\Models\ProRate;
  31. use App\Models\ProSpecificAvailability;
  32. use App\Models\ProSpecificUnavailability;
  33. use App\Models\ProTeam;
  34. use App\Models\ProTextShortcut;
  35. use App\Models\ProTransaction;
  36. use App\Models\Shipment;
  37. use App\Models\SupplyOrder;
  38. use App\Models\Team;
  39. use App\Models\Ticket;
  40. use App\Models\AccountInvite;
  41. use App\Models\ClientMeasurementDaysPerMonth;
  42. use App\Models\ClientBDTDevice;
  43. use App\Models\ClientMemo;
  44. use Carbon\Carbon;
  45. use Cassandra\Custom;
  46. use Illuminate\Pagination\LengthAwarePaginator;
  47. use Illuminate\Support\Facades\Cookie;
  48. use Illuminate\Support\Facades\DB;
  49. use Illuminate\Support\Facades\Http;
  50. use MongoDB\Driver\Session;
  51. use PDF;
  52. use DateTime;
  53. use DateTimeZone;
  54. use Illuminate\Http\Request;
  55. use App\Models\SegmentTemplate;
  56. use App\Models\VisitTemplate;
  57. use App\Models\VisitTemplateSegmentTemplate;
  58. use App\Models\VisitTemplateAccess;
  59. class InvoiceController extends Controller
  60. {
  61. private static $PAGE_SIZE = 25;
  62. public function companies(Request $request) {
  63. $records = Company::orderBy('name', 'ASC')->where('is_active', true)->paginate(InvoiceController::$PAGE_SIZE);
  64. return view ('app.invoice-center.companies', compact('records'));
  65. }
  66. public function customers(Request $request) {
  67. $records = Customer::orderBy('created_at', 'DESC');
  68. $company = null;
  69. if($request->input('companyUid')) {
  70. $company = Company::where('uid', $request->input('companyUid'))->first();
  71. if($company) {
  72. $records = $records->where('company_id', $company->id);
  73. }
  74. }
  75. $records = $records->paginate(InvoiceController::$PAGE_SIZE);
  76. return view ('app.invoice-center.customers', compact('records', 'company'));
  77. }
  78. public function giftCards(Request $request) {
  79. $records = GiftCard::orderBy('created_at', 'DESC');
  80. $company = null;
  81. if($request->input('companyUid')) {
  82. $company = Company::where('uid', $request->input('companyUid'))->first();
  83. if($company) {
  84. $records = $records->where('company_id', $company->id);
  85. }
  86. }
  87. $records = $records->paginate(InvoiceController::$PAGE_SIZE);
  88. return view ('app.invoice-center.gift-cards', compact('records', 'company'));
  89. }
  90. public function invoices(Request $request) {
  91. $records = Invoice::orderBy('created_at', 'DESC');
  92. $customer = null;
  93. if($request->input('customerUid')) {
  94. $customer = Customer::where('uid', $request->input('customerUid'))->first();
  95. if($customer) {
  96. $records = $records->where('customer_id', $customer->id);
  97. }
  98. }
  99. $records = $records->paginate(InvoiceController::$PAGE_SIZE);
  100. return view ('app.invoice-center.invoices', compact('records', 'customer'));
  101. }
  102. public function customerTransactions(Request $request) {
  103. $records = CustomerTransaction::orderBy('created_at', 'DESC');
  104. $customer = null;
  105. if($request->input('customerUid')) {
  106. $customer = Customer::where('uid', $request->input('customerUid'))->first();
  107. if($customer) {
  108. $records = $records->where('customer_id', $customer->id);
  109. }
  110. }
  111. $records = $records->paginate(InvoiceController::$PAGE_SIZE);
  112. return view ('app.invoice-center.customer-transactions', compact('records', 'customer'));
  113. }
  114. public function invoiceTransactions(Request $request) {
  115. $records = InvoiceTransaction::orderBy('created_at', 'DESC');
  116. $invoice = null;
  117. if($request->input('invoiceUid')) {
  118. $invoice = Invoice::where('uid', $request->input('invoiceUid'))->first();
  119. if($invoice) {
  120. $records = $records->where('invoice_id', $invoice->id);
  121. }
  122. }
  123. $records = $records->paginate(InvoiceController::$PAGE_SIZE);
  124. return view ('app.invoice-center.invoice-transactions', compact('records', 'invoice'));
  125. }
  126. private function getICCustomer($sessionKey) {
  127. $customer = false;
  128. if($sessionKey) {
  129. Cookie::queue('sessionKey', $sessionKey);
  130. }
  131. else {
  132. $session = get_current_session();
  133. if(!$session) {
  134. abort(403);
  135. }
  136. else {
  137. $customer = Customer::where('id', $session->customer_id)->first();
  138. if(!$customer) {
  139. abort(403);
  140. }
  141. }
  142. }
  143. return $customer;
  144. }
  145. public function icPayInvoice(Request $request, $invoiceUid, $sessionKey = '') {
  146. $customer = $this->getICCustomer($sessionKey);
  147. if(!$customer) {
  148. return redirect(route('icPayInvoice', ['invoiceUid' => $invoiceUid]));
  149. }
  150. $invoice = Invoice::where('uid', $invoiceUid)->where('is_active', true)->first();
  151. if (!$invoice) abort(404);
  152. $company = $customer->company;
  153. return view('app.invoice-center.ic-pay-invoice', compact('invoice', 'customer', 'company'));
  154. }
  155. public function icCustomerPortal(Request $request, $sessionKey = '') {
  156. $customer = $this->getICCustomer($sessionKey);
  157. if(!$customer) {
  158. return redirect(route('icCustomerPortal'));
  159. }
  160. $client = $customer->client;
  161. $company = $customer->company;
  162. return view('app.invoice-center.ic-customer-portal', compact('customer', 'company'));
  163. }
  164. public function icManageAccount(Request $request, $sessionKey = '') {
  165. $customer = $this->getICCustomer($sessionKey);
  166. if(!$customer) {
  167. return redirect(route('icManageAccount'));
  168. }
  169. $client = $customer->client;
  170. $company = $customer->company;
  171. return view('app.invoice-center.ic-manage-account', compact('customer', 'company', 'client'));
  172. }
  173. public function companySuggestJSON(Request $request) {
  174. $term = $request->input('term') ? trim($request->input('term')) : '';
  175. if (empty($term)) return '';
  176. $matches = DB::select("
  177. SELECT company.uid,
  178. company.name as text
  179. FROM company
  180. WHERE company.name ILIKE :term
  181. ORDER BY company.name",
  182. ['term' => $term . '%']
  183. );
  184. return json_encode([
  185. "success" => true,
  186. "data" => $matches
  187. ]);
  188. }
  189. public function clientSuggestJSON(Request $request) {
  190. $term = $request->input('term') ? trim($request->input('term')) : '';
  191. if (empty($term)) return '';
  192. // if multiple words in query, check for all (max 2)
  193. $term2 = '';
  194. if(strpos($term, ' ') !== FALSE) {
  195. $terms = explode(' ', $term);
  196. $term = trim($terms[0]);
  197. $term2 = trim($terms[1]);
  198. }
  199. if(!empty($term2)) {
  200. $matches = DB::select("
  201. SELECT client.uid,
  202. (client.name_first || ' ' || client.name_last) as text
  203. FROM client
  204. WHERE (client.name_first ILIKE :term OR client.name_last ILIKE :term OR client.name_first ILIKE :term2 OR client.name_last ILIKE :term2)
  205. ORDER BY client.name_first, client.name_last",
  206. ['term' => $term . '%', 'term2' => $term2 . '%']
  207. );
  208. }
  209. else {
  210. $matches = DB::select("
  211. SELECT client.uid,
  212. (client.name_first || ' ' || client.name_last) as text
  213. FROM client
  214. WHERE (client.name_first ILIKE :term OR client.name_last ILIKE :term)
  215. ORDER BY client.name_first, client.name_last",
  216. ['term' => $term . '%']
  217. );
  218. }
  219. return json_encode([
  220. "success" => true,
  221. "data" => $matches
  222. ]);
  223. }
  224. public function customerSuggestJSON(Request $request) {
  225. $term = $request->input('term') ? trim($request->input('term')) : '';
  226. if (empty($term)) return '';
  227. // if multiple words in query, check for all (max 2)
  228. $term2 = '';
  229. if(strpos($term, ' ') !== FALSE) {
  230. $terms = explode(' ', $term);
  231. $term = trim($terms[0]);
  232. $term2 = trim($terms[1]);
  233. }
  234. if(!empty($term2)) {
  235. $matches = DB::select("
  236. SELECT customer.uid,
  237. (client.name_first || ' ' || client.name_last || ' (' || company.name || ')') as text
  238. FROM client join customer on client.id = customer.client_id join company on customer.company_id = company.id
  239. WHERE (client.name_first ILIKE :term OR client.name_last ILIKE :term OR client.name_first ILIKE :term2 OR client.name_last ILIKE :term2)
  240. ORDER BY client.name_first, client.name_last",
  241. ['term' => $term . '%', 'term2' => $term2 . '%']
  242. );
  243. }
  244. else {
  245. $matches = DB::select("
  246. SELECT customer.uid,
  247. (client.name_first || ' ' || client.name_last || ' (' || company.name || ')') as text
  248. FROM client join customer on client.id = customer.client_id join company on customer.company_id = company.id
  249. WHERE (client.name_first ILIKE :term OR client.name_last ILIKE :term)
  250. ORDER BY client.name_first, client.name_last",
  251. ['term' => $term . '%']
  252. );
  253. }
  254. return json_encode([
  255. "success" => true,
  256. "data" => $matches
  257. ]);
  258. }
  259. public function customerInvoicesJSON(Request $request, Customer $customer) {
  260. $invoices = [];
  261. foreach ($customer->invoices as $invoice) {
  262. $invoices[] = [
  263. "uid" => $invoice->uid,
  264. "text" => $invoice->displayName()
  265. ];
  266. }
  267. return json_encode([
  268. "success" => true,
  269. "data" => $invoices
  270. ]);
  271. }
  272. }