InvoiceController.php 9.4 KB

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