InvoiceController.php 8.6 KB

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