InvoiceController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 icPaymentMethods(Request $request, $invoiceUid, $sessionKey = '') {
  156. $customer = $this->getICCustomer($sessionKey);
  157. if(!$customer) {
  158. abort(404);
  159. }
  160. $company = $customer->company;
  161. return view('app.invoice-center.ic-payment-methods', compact('customer', 'company'));
  162. }
  163. public function icCustomerPortal(Request $request, $sessionKey = '') {
  164. $customer = $this->getICCustomer($sessionKey);
  165. if(!$customer) {
  166. return redirect(route('icCustomerPortal'));
  167. }
  168. $client = $customer->client;
  169. $company = $customer->company;
  170. return view('app.invoice-center.ic-customer-portal', compact('customer', 'company'));
  171. }
  172. public function icManageAccount(Request $request, $sessionKey = '') {
  173. $customer = $this->getICCustomer($sessionKey);
  174. if(!$customer) {
  175. return redirect(route('icManageAccount'));
  176. }
  177. $client = $customer->client;
  178. $company = $customer->company;
  179. return view('app.invoice-center.ic-manage-account', compact('customer', 'company', 'client'));
  180. }
  181. public function companySuggestJSON(Request $request) {
  182. $term = $request->input('term') ? trim($request->input('term')) : '';
  183. if (empty($term)) return '';
  184. $matches = DB::select("
  185. SELECT company.uid,
  186. company.name as text
  187. FROM company
  188. WHERE company.name ILIKE :term
  189. ORDER BY company.name",
  190. ['term' => $term . '%']
  191. );
  192. return json_encode([
  193. "success" => true,
  194. "data" => $matches
  195. ]);
  196. }
  197. public function clientSuggestJSON(Request $request) {
  198. $term = $request->input('term') ? trim($request->input('term')) : '';
  199. if (empty($term)) return '';
  200. // if multiple words in query, check for all (max 2)
  201. $term2 = '';
  202. if(strpos($term, ' ') !== FALSE) {
  203. $terms = explode(' ', $term);
  204. $term = trim($terms[0]);
  205. $term2 = trim($terms[1]);
  206. }
  207. if(!empty($term2)) {
  208. $matches = DB::select("
  209. SELECT client.uid,
  210. (client.name_first || ' ' || client.name_last) as text
  211. FROM client
  212. WHERE (client.name_first ILIKE :term OR client.name_last ILIKE :term OR client.name_first ILIKE :term2 OR client.name_last ILIKE :term2)
  213. ORDER BY client.name_first, client.name_last",
  214. ['term' => $term . '%', 'term2' => $term2 . '%']
  215. );
  216. }
  217. else {
  218. $matches = DB::select("
  219. SELECT client.uid,
  220. (client.name_first || ' ' || client.name_last) as text
  221. FROM client
  222. WHERE (client.name_first ILIKE :term OR client.name_last ILIKE :term)
  223. ORDER BY client.name_first, client.name_last",
  224. ['term' => $term . '%']
  225. );
  226. }
  227. return json_encode([
  228. "success" => true,
  229. "data" => $matches
  230. ]);
  231. }
  232. public function customerSuggestJSON(Request $request) {
  233. $term = $request->input('term') ? trim($request->input('term')) : '';
  234. if (empty($term)) return '';
  235. // if multiple words in query, check for all (max 2)
  236. $term2 = '';
  237. if(strpos($term, ' ') !== FALSE) {
  238. $terms = explode(' ', $term);
  239. $term = trim($terms[0]);
  240. $term2 = trim($terms[1]);
  241. }
  242. if(!empty($term2)) {
  243. $matches = DB::select("
  244. SELECT customer.uid,
  245. (client.name_first || ' ' || client.name_last || ' (' || company.name || ')') as text
  246. FROM client join customer on client.id = customer.client_id join company on customer.company_id = company.id
  247. WHERE (client.name_first ILIKE :term OR client.name_last ILIKE :term OR client.name_first ILIKE :term2 OR client.name_last ILIKE :term2)
  248. ORDER BY client.name_first, client.name_last",
  249. ['term' => $term . '%', 'term2' => $term2 . '%']
  250. );
  251. }
  252. else {
  253. $matches = DB::select("
  254. SELECT customer.uid,
  255. (client.name_first || ' ' || client.name_last || ' (' || company.name || ')') as text
  256. FROM client join customer on client.id = customer.client_id join company on customer.company_id = company.id
  257. WHERE (client.name_first ILIKE :term OR client.name_last ILIKE :term)
  258. ORDER BY client.name_first, client.name_last",
  259. ['term' => $term . '%']
  260. );
  261. }
  262. return json_encode([
  263. "success" => true,
  264. "data" => $matches
  265. ]);
  266. }
  267. public function customerInvoicesJSON(Request $request, Customer $customer) {
  268. $invoices = [];
  269. foreach ($customer->invoices as $invoice) {
  270. $invoices[] = [
  271. "uid" => $invoice->uid,
  272. "text" => $invoice->displayName()
  273. ];
  274. }
  275. return json_encode([
  276. "success" => true,
  277. "data" => $invoices
  278. ]);
  279. }
  280. }