InvoiceController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 icCustomerPortal(Request $request, $customerSlug) {
  131. $customer = Customer::where('slug', $customerSlug)->where('is_active', true)->first();
  132. if (!$customer) abort(404);
  133. $company = $customer->company;
  134. return view('app.invoice-center.ic-customer-portal', compact('customer', 'company'));
  135. }
  136. public function companySuggestJSON(Request $request) {
  137. $term = $request->input('term') ? trim($request->input('term')) : '';
  138. if (empty($term)) return '';
  139. $matches = DB::select("
  140. SELECT company.uid,
  141. company.name as text
  142. FROM company
  143. WHERE company.name ILIKE :term
  144. ORDER BY company.name",
  145. ['term' => $term . '%']
  146. );
  147. return json_encode([
  148. "success" => true,
  149. "data" => $matches
  150. ]);
  151. }
  152. public function clientSuggestJSON(Request $request) {
  153. $term = $request->input('term') ? trim($request->input('term')) : '';
  154. if (empty($term)) return '';
  155. // if multiple words in query, check for all (max 2)
  156. $term2 = '';
  157. if(strpos($term, ' ') !== FALSE) {
  158. $terms = explode(' ', $term);
  159. $term = trim($terms[0]);
  160. $term2 = trim($terms[1]);
  161. }
  162. if(!empty($term2)) {
  163. $matches = DB::select("
  164. SELECT client.uid,
  165. (client.name_first || ' ' || client.name_last) as text
  166. FROM client
  167. WHERE (client.name_first ILIKE :term OR client.name_last ILIKE :term OR client.name_first ILIKE :term2 OR client.name_last ILIKE :term2)
  168. ORDER BY client.name_first, client.name_last",
  169. ['term' => $term . '%', 'term2' => $term2 . '%']
  170. );
  171. }
  172. else {
  173. $matches = DB::select("
  174. SELECT client.uid,
  175. (client.name_first || ' ' || client.name_last) as text
  176. FROM client
  177. WHERE (client.name_first ILIKE :term OR client.name_last ILIKE :term)
  178. ORDER BY client.name_first, client.name_last",
  179. ['term' => $term . '%']
  180. );
  181. }
  182. return json_encode([
  183. "success" => true,
  184. "data" => $matches
  185. ]);
  186. }
  187. public function customerSuggestJSON(Request $request) {
  188. $term = $request->input('term') ? trim($request->input('term')) : '';
  189. if (empty($term)) return '';
  190. // if multiple words in query, check for all (max 2)
  191. $term2 = '';
  192. if(strpos($term, ' ') !== FALSE) {
  193. $terms = explode(' ', $term);
  194. $term = trim($terms[0]);
  195. $term2 = trim($terms[1]);
  196. }
  197. if(!empty($term2)) {
  198. $matches = DB::select("
  199. SELECT customer.uid,
  200. (client.name_first || ' ' || client.name_last || ' (' || company.name || ')') as text
  201. FROM client join customer on client.id = customer.client_id join company on customer.company_id = company.id
  202. WHERE (client.name_first ILIKE :term OR client.name_last ILIKE :term OR client.name_first ILIKE :term2 OR client.name_last ILIKE :term2)
  203. ORDER BY client.name_first, client.name_last",
  204. ['term' => $term . '%', 'term2' => $term2 . '%']
  205. );
  206. }
  207. else {
  208. $matches = DB::select("
  209. SELECT customer.uid,
  210. (client.name_first || ' ' || client.name_last || ' (' || company.name || ')') as text
  211. FROM client join customer on client.id = customer.client_id join company on customer.company_id = company.id
  212. WHERE (client.name_first ILIKE :term OR client.name_last ILIKE :term)
  213. ORDER BY client.name_first, client.name_last",
  214. ['term' => $term . '%']
  215. );
  216. }
  217. return json_encode([
  218. "success" => true,
  219. "data" => $matches
  220. ]);
  221. }
  222. public function customerInvoicesJSON(Request $request, Customer $customer) {
  223. $invoices = [];
  224. foreach ($customer->invoices as $invoice) {
  225. $invoices[] = [
  226. "uid" => $invoice->uid,
  227. "text" => $invoice->displayName()
  228. ];
  229. }
  230. return json_encode([
  231. "success" => true,
  232. "data" => $invoices
  233. ]);
  234. }
  235. }