AdminController.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\AccountingItem;
  4. use App\Models\AccountingItemTemplate;
  5. use App\Models\AppSetting;
  6. use App\Models\Claim;
  7. use App\Models\Lead;
  8. use App\Models\Appointment;
  9. use App\Models\BDTDevice;
  10. use App\Models\CareMonth;
  11. use App\Models\Client;
  12. use App\Models\ClientBDTDevice;
  13. use App\Models\ClientInfoLine;
  14. use App\Models\Erx;
  15. use App\Models\Facility;
  16. use App\Models\Handout;
  17. use App\Models\IncomingReport;
  18. use App\Models\InternalMessage;
  19. use App\Models\MBClaim;
  20. use App\Models\MBPayer;
  21. use App\Models\Note;
  22. use App\Models\NoteTemplate;
  23. use App\Models\Pro;
  24. use App\Models\Product;
  25. use App\Models\ProProAccess;
  26. use App\Models\SectionTemplate;
  27. use App\Models\Shipment;
  28. use App\Models\SupplyOrder;
  29. use App\Models\Ticket;
  30. use Illuminate\Http\Request;
  31. use Illuminate\Support\Facades\DB;
  32. use Illuminate\Support\Facades\File;
  33. use App\Models\Bill;
  34. use App\Models\ClientSMS;
  35. use App\Models\Point;
  36. use Illuminate\Support\Facades\Http;
  37. use Illuminate\Support\Str;
  38. use PDF;
  39. use Illuminate\Support\Facades\Schema;
  40. use App\Models\AdminPatient;
  41. use App\Models\SupplyOrderView;
  42. use Illuminate\Pagination\LengthAwarePaginator;
  43. use Ramsey\Uuid\Uuid;
  44. class AdminController extends Controller
  45. {
  46. public function patients(Request $request)
  47. {
  48. // DB::enableQueryLog();
  49. $filters = $request->all();
  50. $patients = AdminPatient::whereNull('shadow_pro_id');
  51. if ($request->input('name')) {
  52. $name = trim($request->input('name'));
  53. if ($name) {
  54. $patients = $patients->where(function ($q) use ($name) {
  55. $q->where('display_name', 'ILIKE', '%' . $name . '%');
  56. });
  57. }
  58. }
  59. if ($request->input('mcp')) {
  60. if($request->input('mcp') == 'NO_MCP'){
  61. $patients = $patients->whereNull('mcp_pro_id');
  62. }else{
  63. $mcp = Pro::where('uid', trim($request->input('mcp')))->first();
  64. if ($mcp) {
  65. $patients = $patients->where('mcp_pro_id', $mcp->id);
  66. }
  67. }
  68. }
  69. if ($request->input('na')) {
  70. if($request->input('na') == 'NO_NA'){
  71. $patients = $patients->whereNull('default_na_pro_id');
  72. }else{
  73. $na = Pro::where('uid', trim($request->input('na')))->first();
  74. if ($na) {
  75. $patients = $patients->where('default_na_pro_id', $na->id);
  76. }
  77. }
  78. }
  79. if ($request->input('ob')) {
  80. if ($request->input('ob') == 'yes') {
  81. $patients = $patients->where('has_mcp_done_onboarding_visit', 'YES');
  82. } else {
  83. $patients = $patients->where('has_mcp_done_onboarding_visit', '!=', 'YES');
  84. }
  85. }
  86. if ($request->input('next_appointment_category')) {
  87. if($request->input('next_appointment_category') == 'NONE'){
  88. $patients = $patients->whereNull('next_mcp_appointment_id');
  89. }else{
  90. $self = $this;
  91. $patients = $patients->whereHas('nextMcpAppointment', function($pQry) use ($request, $self){
  92. return $self->filterMultiQuery($request, $pQry, 'raw_date', 'next_appointment_category', 'next_appointment_value_1', 'next_appointment_value_2');
  93. });
  94. }
  95. }
  96. if ($request->input('chart_number')) {
  97. $patients = $patients->where('chart_number', 'ILIKE' , '%'.$request->input('chart_number').'%');
  98. }
  99. if ($request->input('home_address_state')) {
  100. if($request->input('home_address_state') == 'NONE'){
  101. $patients = $patients->whereRaw("mailing_address_state IS NULL OR TRIM(BOTH FROM mailing_address_state = ''");
  102. }else if($request->input('home_address_state') == 'NOT_MD'){
  103. $patients = $patients->whereRaw("(TRIM(BOTH FROM mailing_address_state) NOT ILIKE 'MD' AND TRIM(BOTH FROM mailing_address_state) NOT ILIKE 'MARYLAND')");
  104. }else{
  105. $patients = $patients->whereRaw("TRIM(BOTH FROM mailing_address_state) = '" . $request->input('home_address_state') . "'");
  106. }
  107. }
  108. $this->filterMultiQuery($request, $patients, 'age_in_years', 'age_category', 'age_value_1', 'age_value_2', false);
  109. $this->filterSimpleQuery($request, $patients, 'sex', 'sex');
  110. $this->filterMultiQuery($request, $patients, 'usual_bmi_max', 'bmi_category', 'bmi_value_1', 'bmi_value_2', false);
  111. $this->filterMultiQuery($request, $patients, 'most_recent_weight_at', 'last_weighed_in_category', 'last_weighed_in_value_1', 'last_weighed_in_value_2');
  112. $this->filterMultiQuery($request, $patients, 'most_recent_bp_at', 'last_bp_category', 'last_bp_value_1', 'last_bp_value_2');
  113. $this->filterMultiQuery($request, $patients, 'created_at', 'created_at', 'created_at_value_1', 'created_at_value_2');
  114. $this->filterMultiQuery($request, $patients, 'most_recent_completed_mcp_note_date', 'last_visit_category', 'last_visit_value_1', 'last_visit_value_2');
  115. $fVal = $request->input('has_email');
  116. if($fVal) {
  117. if($fVal === 'YES') {
  118. $patients = $patients->whereRaw("(email_address IS NOT NULL AND TRIM(email_address) != '')");
  119. }
  120. else {
  121. $patients = $patients->whereRaw("(email_address IS NULL OR TRIM(email_address) = '')");
  122. }
  123. }
  124. $fVal = $request->input('has_account');
  125. if($fVal) {
  126. if($fVal === 'YES') {
  127. $patients = $patients->whereRaw("((SELECT COUNT(ac.id) FROM account_client ac WHERE ac.client_id = admin_patient_list.id) > 0)");
  128. }
  129. else {
  130. $patients = $patients->whereRaw("((SELECT COUNT(ac.id) FROM account_client ac WHERE ac.client_id = admin_patient_list.id) = 0)");
  131. }
  132. }
  133. $fVal = $request->input('has_default_mcp_company_pro');
  134. if($fVal) {
  135. if($fVal === 'YES') {
  136. $patients = $patients->whereRaw("(default_mcp_company_pro_id IS NOT NULL)");
  137. }
  138. else {
  139. $patients = $patients->whereRaw("(default_mcp_company_pro_id IS NULL)");
  140. }
  141. }
  142. $fVal = $request->input('has_default_mcp_company_pro_payer');
  143. if($fVal) {
  144. if($fVal === 'YES') {
  145. $patients = $patients->whereRaw("(default_mcp_company_pro_payer_id IS NOT NULL)");
  146. }
  147. else {
  148. $patients = $patients->whereRaw("(default_mcp_company_pro_payer_id IS NULL)");
  149. }
  150. }
  151. $fVal = $request->input('has_default_mcp_company_location');
  152. if($fVal) {
  153. if($fVal === 'YES') {
  154. $patients = $patients->whereRaw("(default_mcp_company_location_id IS NOT NULL)");
  155. }
  156. else {
  157. $patients = $patients->whereRaw("(default_mcp_company_location_id IS NULL)");
  158. }
  159. }
  160. $fVal = $request->input('has_bp_device');
  161. if($fVal) {
  162. if($fVal === 'YES') {
  163. $patients = $patients->whereRaw("((SELECT count(sh.id) " .
  164. "FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id " .
  165. "WHERE so.product_id = 1 AND sh.status IN ('DELIVERED', 'DISPATCHED') AND so.client_id = admin_patient_list.id) > 0)");
  166. }
  167. else {
  168. $patients = $patients->whereRaw("((SELECT count(sh.id) " .
  169. "FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id " .
  170. "WHERE so.product_id = 1 AND sh.status IN ('DELIVERED', 'DISPATCHED') AND so.client_id = admin_patient_list.id) = 0)");
  171. }
  172. }
  173. $fVal = $request->input('has_weight_scale');
  174. if($fVal) {
  175. if($fVal === 'YES') {
  176. $patients = $patients->whereRaw("((SELECT count(sh.id) " .
  177. "FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id " .
  178. "WHERE so.product_id = 2 AND sh.status IN ('DELIVERED', 'DISPATCHED') AND so.client_id = admin_patient_list.id) > 0)");
  179. }
  180. else {
  181. $patients = $patients->whereRaw("((SELECT count(sh.id) " .
  182. "FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id " .
  183. "WHERE so.product_id = 2 AND sh.status IN ('DELIVERED', 'DISPATCHED') AND so.client_id = admin_patient_list.id) = 0)");
  184. }
  185. }
  186. $fVal = $request->input('has_pulse_ox');
  187. if($fVal) {
  188. if($fVal === 'YES') {
  189. $patients = $patients->whereRaw("((SELECT count(sh.id) " .
  190. "FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id " .
  191. "WHERE so.product_id = 3 AND sh.status IN ('DELIVERED', 'DISPATCHED') AND so.client_id = admin_patient_list.id) > 0)");
  192. }
  193. else {
  194. $patients = $patients->whereRaw("((SELECT count(sh.id) " .
  195. "FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id " .
  196. "WHERE so.product_id = 3 AND sh.status IN ('DELIVERED', 'DISPATCHED') AND so.client_id = admin_patient_list.id) = 0)");
  197. }
  198. }
  199. $fVal = $request->input('has_temp_fun');
  200. if($fVal) {
  201. if($fVal === 'YES') {
  202. $patients = $patients->whereRaw("((SELECT count(sh.id) " .
  203. "FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id " .
  204. "WHERE so.product_id = 4 AND sh.status IN ('DELIVERED', 'DISPATCHED') AND so.client_id = admin_patient_list.id) > 0)");
  205. }
  206. else {
  207. $patients = $patients->whereRaw("((SELECT count(sh.id) " .
  208. "FROM shipment sh LEFT JOIN supply_order so ON so.shipment_id = sh.id " .
  209. "WHERE so.product_id = 4 AND sh.status IN ('DELIVERED', 'DISPATCHED') AND so.client_id = admin_patient_list.id) = 0)");
  210. }
  211. }
  212. $fVal = $request->input('imei');
  213. if($fVal) {
  214. $patients = $patients->whereRaw("((SELECT count(cbd.id) FROM client_bdt_device cbd
  215. WHERE cbd.client_id = admin_patient_list.id AND cbd.device_id IN (SELECT bd.id FROM bdt_device bd WHERE bd.imei LIKE '%$fVal%' AND bd.is_active IS TRUE)) > 0)");
  216. }
  217. if($request->input('number_of_measurements')){
  218. $keyName = $request->input('number_of_measurements');
  219. $measurementCountQuery = '(SELECT COUNT(*) FROM measurement WHERE measurement.client_id = admin_patient_list.id AND is_active IS TRUE AND is_cellular IS TRUE AND is_cellular_zero IS NOT TRUE)';
  220. switch($keyName) {
  221. case 'EXACTLY':
  222. if($request->input('number_of_measurements_value_1')) {
  223. $patients->whereRaw($measurementCountQuery . '='.$request->input('number_of_measurements_value_1'));
  224. }
  225. break;
  226. case 'LESS_THAN':
  227. if($request->input('number_of_measurements_value_1')) {
  228. $patients->whereRaw($measurementCountQuery . '<='.$request->input('number_of_measurements_value_1'));
  229. }
  230. break;
  231. case 'GREATER_THAN':
  232. if($request->input('number_of_measurements_value_1')) {
  233. $patients->whereRaw($measurementCountQuery . '>='.$request->input('number_of_measurements_value_1'));
  234. }
  235. break;
  236. case 'BETWEEN':
  237. if($request->input('number_of_measurements_value_1') && $request->input('number_of_measurements_value_2')) {
  238. $patients->whereRaw($measurementCountQuery.'>='.$request->input('number_of_measurements_value_1') .' AND '. $measurementCountQuery . '<='.$request->input('number_of_measurements_value_2'));
  239. }
  240. break;
  241. case 'NOT_BETWEEN':
  242. if($request->input('number_of_measurements_value_1') && $request->input('number_of_measurements_value_2')) {
  243. $patients->where(function ($q) use ($request, $measurementCountQuery) {
  244. $q->whereRaw($measurementCountQuery . '<'.$request->input('number_of_measurements_value_1') .' OR '. $measurementCountQuery . '>'.$request->input('number_of_measurements_value_2'));
  245. });
  246. }
  247. break;
  248. }
  249. }
  250. $status = $request->input('status');
  251. if($status){
  252. if($status === 'ACTIVE'){
  253. $patients->where('is_active', true)->where(function($q) use ($status){
  254. return $q->where('client_engagement_status_category', $status)
  255. ->orWhereNull('client_engagement_status_category');
  256. });
  257. }elseif($status === 'NONE'){
  258. $patients->whereNull('client_engagement_status_category');
  259. }else {
  260. $patients->where('client_engagement_status_category', $status);
  261. }
  262. }
  263. $initiative = $request->input('initiative');
  264. if($initiative){
  265. $wildCardedInitiative = '%'.$initiative.'%';
  266. $patients->where('initiative', 'ilike', $wildCardedInitiative);
  267. }
  268. $include_test_records = $request->input('include_test_records');
  269. if(!$include_test_records && $status != 'DUMMY'){
  270. $patients = $patients->where(function ($q) {
  271. $q->whereNull('client_engagement_status_category')
  272. ->orWhere('client_engagement_status_category', '<>', 'DUMMY');
  273. });
  274. }
  275. $zero_deductible = $request->input('zero_deductible');
  276. if($zero_deductible){
  277. $patients = $patients->where(function ($q) {
  278. $q->where('mpb_remaining', 0);
  279. });
  280. }
  281. $insurance = $request->get('insurance');
  282. if($insurance){
  283. if(strpos($insurance, '_new_|') === 0){
  284. $trimmed = trim(str_replace('_new_|', '', $insurance));
  285. $condSql = "EXISTS(SELECT 1 FROM insurance_card WHERE client_id = admin_patient_list.id AND is_active = TRUE AND (carrier_category ILIKE '%{$trimmed}%' OR carrier_name ILIKE '%{$trimmed}%'))";
  286. $patients = $patients->whereRaw($condSql);
  287. }
  288. else {
  289. if($insurance === 'MEDICARE'){
  290. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery) {
  291. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  292. });
  293. }else{
  294. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery) use ($insurance) {
  295. return $cpcQuery->where('commercial_payer_id', '=', $insurance);
  296. });
  297. }
  298. }
  299. }
  300. $companyID = $request->get('company');
  301. if($companyID){
  302. if($companyID == 'NONE'){
  303. $patients = $patients->doesntHave('companyClients');
  304. }else{
  305. $patients = $patients->whereHas('companyClients', function($qry)use($companyID){
  306. if($companyID != 'ANY'){
  307. return $qry->where('company_id', $companyID);
  308. }
  309. });
  310. }
  311. }
  312. // search by tag
  313. if ($request->input('tags')) {
  314. $tags = trim($request->input('tags'));
  315. if ($tags) {
  316. try {
  317. $condSql = "((SELECT COUNT(ct.id) FROM client_tag ct WHERE ct.client_id = admin_patient_list.id AND UPPER(ct.tag) LIKE '%" . strtoupper($tags) . "%') > 0)";
  318. $patients = $patients->whereRaw($condSql);
  319. }
  320. catch (\Exception $e) {
  321. dd($e->getMessage());
  322. }
  323. }
  324. }
  325. $sortBy = $request->input('sort_by') ?: 'name_first';
  326. $sortDir = $request->input('sort_dir') ?: 'ASC';
  327. $sortBySQL = "$sortBy $sortDir NULLS LAST";
  328. if($sortBy !== 'client_engagement_status_category' && $request->input('status')) {
  329. $sortBySQL = "client_engagement_status_category DESC NULLS LAST";
  330. }
  331. if(@$filters['mapView'] == 1){
  332. $patients = $patients->orderByRaw($sortBySQL)->paginate(100);
  333. }else{
  334. $patients = $patients->orderByRaw($sortBySQL)->paginate(25);
  335. }
  336. $oldInsurances = DB::select('SELECT DISTINCT commercial_payer_name, commercial_payer_id FROM client_primary_coverage WHERE commercial_payer_name IS NOT NULL ORDER BY commercial_payer_name ASC');
  337. $newInsurances = DB::select("select distinct COALESCE(ic.carrier_name, ic.carrier_category) as payer_name from insurance_card ic");
  338. // $qLog = DB::getQueryLog();
  339. // dd($qLog);
  340. return view('app.admin.patients', compact('patients', 'filters', 'oldInsurances', 'newInsurances'));
  341. }
  342. public function partBPatients(Request $request){
  343. $filters = $request->all();
  344. $patients = Client::whereNull('shadow_pro_id');
  345. if ($request->input('name')) {
  346. $name = trim($request->input('name'));
  347. if ($name) {
  348. $patients = $patients->where(function ($q) use ($name) {
  349. $q->where('name_first', 'ILIKE', '%' . $name . '%')
  350. ->orWhere('name_last', 'ILIKE', '%' . $name . '%');
  351. });
  352. }
  353. }
  354. if ($request->input('mcp')) {
  355. if($request->input('mcp') == 'NO_MCP'){
  356. $patients = $patients->whereNull('mcp_pro_id');
  357. }else{
  358. $mcp = Pro::where('uid', trim($request->input('mcp')))->first();
  359. if ($mcp) {
  360. $patients = $patients->where('mcp_pro_id', $mcp->id);
  361. }
  362. }
  363. }
  364. if ($request->input('na')) {
  365. if($request->input('na') == 'NO_NA'){
  366. $patients = $patients->whereNull('default_na_pro_id');
  367. }else{
  368. $na = Pro::where('uid', trim($request->input('na')))->first();
  369. if ($na) {
  370. $patients = $patients->where('default_na_pro_id', $na->id);
  371. }
  372. }
  373. }
  374. if ($request->input('next_appointment_category')) {
  375. if($request->input('next_appointment_category') == 'NONE'){
  376. $patients = $patients->whereNull('next_mcp_appointment_id');
  377. }
  378. }
  379. if ($request->input('chart_number')) {
  380. $patients = $patients->where('chart_number', 'ILIKE' , '%'.$request->input('chart_number').'%');
  381. }
  382. if ($request->input('home_address_state')) {
  383. if($request->input('home_address_state') == 'NONE'){
  384. $patients = $patients->whereNull('mailing_address_state');
  385. }else if($request->input('home_address_state') == 'NOT_MD'){
  386. $patients = $patients->where('mailing_address_state', '<>' , 'MD');
  387. }else{
  388. $patients = $patients->where('mailing_address_state', '=' , $request->input('home_address_state'));
  389. }
  390. }
  391. $this->filterMultiQuery($request, $patients, 'age_in_years', 'age_category', 'age_value_1', 'age_value_2', false);
  392. $this->filterSimpleQuery($request, $patients, 'sex', 'sex');
  393. $this->filterMultiQuery($request, $patients, 'usual_bmi_max', 'bmi_category', 'bmi_value_1', 'bmi_value_2', false);
  394. $this->filterMultiQuery($request, $patients, 'most_recent_weight_at', 'last_weighed_in_category', 'last_weighed_in_value_1', 'last_weighed_in_value_2');
  395. $this->filterMultiQuery($request, $patients, 'most_recent_bp_at', 'last_bp_category', 'last_bp_value_1', 'last_bp_value_2');
  396. if($request->input('deductible')){
  397. $keyName = $request->input('deductible');
  398. switch($keyName) {
  399. case 'EXACTLY':
  400. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  401. return $q->where('auto_medicare_mpb_deductible', '=', $request->input('deductible_value_1'));
  402. });
  403. break;
  404. case 'LESS_THAN':
  405. if($request->input('deductible_value_1')) {
  406. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  407. return $q->where('auto_medicare_mpb_deductible', '<=', $request->input('deductible_value_1'));
  408. });
  409. }
  410. break;
  411. case 'GREATER_THAN':
  412. if($request->input('deductible_value_1')) {
  413. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  414. return $q->where('auto_medicare_mpb_deductible', '>=', $request->input('deductible_value_1'));
  415. });
  416. }
  417. break;
  418. case 'BETWEEN':
  419. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  420. return $q->where('auto_medicare_mpb_deductible', '>=', $request->input('deductible_value_1'))
  421. ->where('auto_medicare_mpb_deductible', '<=', $request->input('deductible_value_2'));
  422. });
  423. break;
  424. case 'NOT_BETWEEN':
  425. if($request->input('deductible_value_1') && $request->input('deductible_value_2')) {
  426. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($q) use ($request){
  427. return $q->where(function($qq) use ($request){
  428. return $qq->where('auto_medicare_mpb_deductible', '<', $request->input('deductible_value_1'))
  429. ->orWhere('auto_medicare_mpb_deductible', '>', $request->input('deductible_value_2'));
  430. });
  431. });
  432. }
  433. break;
  434. }
  435. }
  436. switch($request->input('status')) {
  437. case 'ACTIVE':
  438. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', true);
  439. break;
  440. case 'AWAITING_VISIT':
  441. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', false);
  442. break;
  443. case 'INACTIVE':
  444. $patients->where('is_active', '<>', true);
  445. break;
  446. }
  447. $initiative = $request->input('initiative');
  448. if($initiative){
  449. $wildCardedInitiative = '%'.$initiative.'%';
  450. $patients->where('initiative', 'ilike', $wildCardedInitiative);
  451. }
  452. $include_test_records = $request->input('include_test_records');
  453. if(!$include_test_records){
  454. $patients = $patients->where(function ($q) {
  455. return $q->whereNull('client_engagement_status_category')
  456. ->orWhere('client_engagement_status_category', '<>', 'DUMMY');
  457. });
  458. }
  459. $with_claim_not_closed = $request->input('with_claim_not_closed');
  460. if($with_claim_not_closed){
  461. $patients = $patients->whereHas('notes', function ($q) {
  462. return $q->where('is_claim_closed', false)
  463. ->where('is_signed_by_hcp', true)
  464. ->where('is_cancelled', false);
  465. });
  466. }
  467. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  468. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  469. });
  470. $patients = $patients->orderBy('created_at', 'DESC')->paginate(25);
  471. return view('app.admin.part_b_patients', compact('patients', 'filters'));
  472. }
  473. public function leads(Request $request){
  474. $leads = Lead::whereNotNull('created_at');
  475. $leads = $leads->orderBy('created_at', 'DESC')->paginate(25);
  476. return view('app.admin.leads', compact('leads'));
  477. }
  478. public function notes(Request $request)
  479. {
  480. $notes = Note::paginate(5);
  481. // SELECT * FROM note WHERE client_id IN (SELECT id FROM client WHERE mcp_pro_id = :me.id);
  482. return view('app.mcp.notes', compact('notes'));
  483. }
  484. public function notes_pending_summary_suggestion(Request $request){
  485. $pro = $this->performer->pro;
  486. $data = [
  487. 'records' => $pro->get_notes_pending_summary_suggestion_as_admin()
  488. ];
  489. return view('app.admin.notes_pending_summary_suggestion', $data);
  490. }
  491. public function notes_rejected_summary_suggestion(Request $request){
  492. $pro = $this->performer->pro;
  493. $data = [
  494. 'records' => $pro->get_notes_rejected_summary_suggestion_as_admin()
  495. ];
  496. return view('app.admin.notes_rejected_summary_suggestion', $data);
  497. }
  498. public function appointments(Request $request)
  499. {
  500. $appointments = Appointment::paginate(5);
  501. return view('app.mcp.appointments', compact('appointments'));
  502. }
  503. public function bills(Request $request)
  504. {
  505. $bills = Bill::paginate(5);
  506. return view('app.mcp.bills', compact('bills'));
  507. }
  508. public function erx_and_orders(Request $request)
  509. {
  510. $erxAndOrders = Erx::paginate(5);
  511. return view('app.mcp.erx_and_orders', compact('erxAndOrders'));
  512. }
  513. public function reports(Request $request)
  514. {
  515. $data = [];
  516. return view('app.mcp.reports', $data);
  517. }
  518. public function supply_orders(Request $request)
  519. {
  520. $supplyOrders = SupplyOrderView::paginate(5);
  521. return view('app.mcp.supply_orders', compact('supplyOrders'));
  522. }
  523. public function getCreateNewPatientScriptTemplate(Request $request){
  524. $template = $request->get('template');
  525. if(!$template) return $this->fail('No script template');
  526. $path = resource_path() . '/views/app/patient/create-patient/scripts/' . $template . '.blade.php';
  527. if(!File::exists($path)) return $this->fail('Invalid script template');
  528. $templateContent = file_get_contents($path);
  529. return $this->pass($templateContent);
  530. }
  531. public function bdtDevices(Request $request)
  532. {
  533. $filters = $request->all();
  534. $bdtDevices = BDTDevice::query();
  535. $imei = $request->input('imei');
  536. if($imei){
  537. $bdtDevices = $bdtDevices->where('imei', '=', $imei);
  538. }
  539. $client = $request->input('client');
  540. if($client){
  541. $client = '%'.$client.'%';
  542. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice', function($cbdtdQuery) use ($client) {
  543. return $cbdtdQuery->whereHas('client', function($clientQuery) use ($client){
  544. return $clientQuery->where(function($q) use ($client){
  545. return $q->where('name_first', 'ilike', $client)
  546. ->orWhere('name_last', 'ilike', $client)
  547. ->orWhere('cell_number', 'ilike', $client)
  548. ->orWhereRaw("name_first||' '||name_last ILIKE "."'".$client."'");
  549. });
  550. });
  551. });
  552. }
  553. $is_issued = $request->input('is_issued');
  554. if($is_issued){
  555. if($is_issued == 'YES'){
  556. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice');
  557. }
  558. if($is_issued == 'NO'){
  559. $bdtDevices = $bdtDevices->whereDoesntHave('clientBDTDevice');
  560. }
  561. }
  562. $is_issued = $request->input('is_issued');
  563. if($is_issued){
  564. if($is_issued == 'YES'){
  565. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice');
  566. }
  567. if($is_issued == 'NO'){
  568. $bdtDevices = $bdtDevices->whereDoesntHave('clientBDTDevice');
  569. }
  570. }
  571. $mcp = $request->input('mcp');
  572. if($mcp){
  573. $bdtDevices = $bdtDevices->whereHas('clientBDTDevice', function($cbdtdQuery) use ($mcp) {
  574. return $cbdtdQuery->whereHas('client', function($clientQuery) use ($mcp){
  575. $mcpPro = Pro::where('uid', $mcp)->first();
  576. return $clientQuery->where('mcp_pro_id', $mcpPro->id);
  577. });
  578. });
  579. }
  580. $bdtDevices = $bdtDevices->paginate(20);
  581. return view('app.admin.bdt_devices', compact('bdtDevices', 'filters'));
  582. }
  583. public function patientsMissingDefasultSettings(Request $request){
  584. $filters = $request->all();
  585. $patients = Client::whereNull('shadow_pro_id');
  586. $patients = $patients->where(function($qry){
  587. return $qry->orWhereNull('mcp_pro_id')->orWhereNull('default_mcp_company_pro_id')->orWhereNull('default_mcp_company_pro_payer_id')->orWhereNull('default_mcp_company_location_id');
  588. });
  589. if ($request->input('name')) {
  590. $name = trim($request->input('name'));
  591. if ($name) {
  592. $patients = $patients->where(function ($q) use ($name) {
  593. $q->where('name_first', 'ILIKE', '%' . $name . '%')
  594. ->orWhere('name_last', 'ILIKE', '%' . $name . '%');
  595. });
  596. }
  597. }
  598. if ($request->input('mcp')) {
  599. if($request->input('mcp') == 'NO_MCP'){
  600. $patients = $patients->whereNull('mcp_pro_id');
  601. }else{
  602. $mcp = Pro::where('uid', trim($request->input('mcp')))->first();
  603. if ($mcp) {
  604. $patients = $patients->where('mcp_pro_id', $mcp->id);
  605. }
  606. }
  607. }
  608. if ($request->input('chart_number')) {
  609. $patients = $patients->where('chart_number', 'ILIKE' , '%'.$request->input('chart_number').'%');
  610. }
  611. $status = $request->input('status');
  612. if($status){
  613. if($status == 'ACTIVE'){
  614. $patients->where('is_active', true)->where(function($q) use ($status){
  615. return $q->where('client_engagement_status_category', $status)
  616. ->orWhereNull('client_engagement_status_category');
  617. });
  618. }else {
  619. $patients->where('client_engagement_status_category', $status);
  620. }
  621. }
  622. $insurance = $request->get('insurance');
  623. if($insurance){
  624. if($insurance === 'MEDICARE'){
  625. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  626. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  627. });
  628. }elseif($insurance === 'MEDICARE_PENDING'){
  629. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  630. return $cpcQuery->where('plan_type', 'MEDICARE')->where('is_covered', '!=', 'YES');
  631. });
  632. }elseif($insurance === 'NOT_COVERED'){
  633. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  634. return $cpcQuery->where('is_covered', '!=', 'YES');
  635. });
  636. }elseif($insurance === 'PENDING'){
  637. $patients = $patients->whereHas('effectiveClientPrimaryCoverage', function($cpcQuery){
  638. return $cpcQuery->where('is_covered', '=', 'UNKNOWN');
  639. });
  640. }
  641. else{
  642. $patients = $patients->whereDoesntHave('effectiveClientPrimaryCoverage', function($cpcQuery){
  643. return $cpcQuery->where('is_partbprimary', '=', 'YES');
  644. });
  645. }
  646. }
  647. $missing_default_settings = $request->get('missing_default_settings');
  648. if($missing_default_settings){
  649. if($missing_default_settings === 'NO_MCP') $patients = $patients->whereNull('mcp_pro_id');
  650. if($missing_default_settings === 'NO_MCP_COMPANY_PRO') $patients = $patients->whereNull('default_mcp_company_pro_id');
  651. if($missing_default_settings === 'NO_MCP_COMPANY_PRO_PAYER') $patients = $patients->whereNull('default_mcp_company_pro_payer_id');
  652. if($missing_default_settings === 'NO_MCP_COMPANY_LOCATION') $patients = $patients->whereNull('default_mcp_company_location_id');
  653. }
  654. $care_plan = $request->get('care_plan');
  655. if($care_plan){
  656. if($care_plan === 'UNSIGNED_CARE_PLANS'){
  657. $patients = $patients->whereHas('notes', function($noteQuery){
  658. return $noteQuery->where('cm_setup_manager_signature_status', '!=', 'SIGNED');
  659. });
  660. }
  661. if($care_plan === 'UNCLEARED_CARE_PLANS'){
  662. $patients = $patients->where('has_care_plan_flag', true)->where('is_flag_cleared', false);
  663. }
  664. }
  665. $patients = $patients->orderBy('created_at', 'DESC')->paginate(50);
  666. return view('app.admin.patients_missing_default_settings', compact('patients', 'filters'));
  667. }
  668. public function points(Request $request)
  669. {
  670. $filters = $request->all();
  671. $points = Point::query();
  672. $points = $points->where('is_removed', '!=', true)
  673. ->where('category', '!=', 'REVIEW')
  674. ->where('category', '!=', 'PLAN');
  675. $intentions = DB::select("SELECT intention, COUNT(intention) as count FROM point WHERE intention is not null and is_removed is not true and category != 'REVIEW' and category != 'PLAN' GROUP BY intention ORDER BY intention ASC");
  676. $categories = [];
  677. $names = [];
  678. if ($request->input('mcp')) {
  679. $mcp = Pro::where('uid', trim($request->input('mcp')))->first();
  680. if ($mcp) {
  681. // $points = $points->where('created_by_pro_id', $mcp->id);
  682. }
  683. }
  684. $implodedIntentions = null;
  685. if ($request->input('intentions')) {
  686. $points = $points->whereIn('intention', $request->input('intentions'));
  687. $implodedIntentions = join("','", $request->input('intentions'));
  688. $categories = DB::select("SELECT category, COUNT(id) as count FROM point WHERE intention in ('" . $implodedIntentions . "') and is_removed is not true and category != 'REVIEW' and category != 'PLAN' GROUP BY category ORDER BY category ASC");
  689. }
  690. if ($request->input('categories')) {
  691. $points = $points->whereIn('category', $request->input('categories'));
  692. $implodedCategories = join("','", $request->input('categories'));
  693. $names = DB::select("select (data::json)->>'name' as name, count(id) as count from point where category in ('".$implodedCategories."') and is_removed is not true and intention in ('".$implodedIntentions."') and category != 'REVIEW' and category != 'PLAN' group by 1 order by 1 asc");
  694. }
  695. if ($request->input('names')){
  696. $implodedNames = join("','", $request->input('names'));
  697. $points = $points->whereRaw("(data::json)->>'name' in ('".$implodedNames."')");
  698. }
  699. $points = $points->paginate(30);
  700. $_point = new Point;
  701. $tableName = $_point->getTable();
  702. $columns = Schema::getColumnListing($tableName);
  703. return view('app.admin.points.index', compact('points', 'filters', 'columns', 'intentions', 'categories', 'names'));
  704. }
  705. public function pointDetails(Request $request, $uid){
  706. $point = Point::where('uid', $uid)->first();
  707. $tableName = $point->getTable();
  708. $columns = Schema::getColumnListing($tableName);
  709. return view('app.admin.points.record-details', compact('point', 'columns'));
  710. }
  711. public function messages(Request $request)
  712. {
  713. $messages = InternalMessage::orderBy('created_at', 'desc')->paginate(50);
  714. return view('app.admin.messages', compact('messages'));
  715. }
  716. public function patientsNotesPointsFilter(Request $request){
  717. $filters = $request->all();
  718. $searchArrayStrings = null;
  719. $searchString = $request->get('string');
  720. if($searchString){
  721. $searchStrings = explode(',', $searchString);
  722. $searchArray = [];
  723. foreach($searchStrings as $string){
  724. $s = "'" . '%'.strtolower(trim($string)).'%'. "'";
  725. array_push($searchArray, $s);
  726. }
  727. $searchArrayStrings = implode(',', $searchArray);
  728. }
  729. //Query distinct clients who have points that contain specific substrings in the point.data
  730. $qry = "
  731. SELECT
  732. DISTINCT ON (p.client_id)
  733. p.client_id,
  734. p.id,
  735. p.uid,
  736. c.uid as client_uid,
  737. c.chart_number,
  738. c.cell_number,
  739. c.phone_home,
  740. c.phone_mobile,
  741. c.phone_work,
  742. c.email_address,
  743. c.dob,
  744. (c.name_first ||' '||c.name_last) as patient_name,
  745. (mcp.name_first ||' '||mcp.name_last) as mcp_name,
  746. c.most_recent_completed_mcp_note_date as last_visit_date,
  747. (cover.plan_type) as cover
  748. FROM
  749. point p
  750. LEFT JOIN client c on c.id = p.client_id
  751. LEFT JOIN pro mcp on mcp.id = c.mcp_pro_id
  752. LEFT JOIN client_primary_coverage cover on cover.client_id = c.id
  753. ";
  754. if($searchArrayStrings){
  755. $qry = $qry . " WHERE lower(p.data) ILIKE any (array[".$searchArrayStrings."])";
  756. }
  757. $qry = $qry . " ORDER BY p.client_id DESC, p.created_at DESC";
  758. $records = null;
  759. if($searchArrayStrings){
  760. $records = DB::select($qry);
  761. $page = $request->get('page', 1);
  762. $size = 20;
  763. $collect = collect($records);
  764. $records = new LengthAwarePaginator(
  765. $collect->forPage($page, $size),
  766. $collect->count(),
  767. $size,
  768. $page
  769. );
  770. $records->setPath(route('admin.patients-notes-points-filter'));
  771. }
  772. return view('app.admin.patients-notes-points-filter', compact('records', 'filters'));
  773. }
  774. public function manageAccountingItemsForBill(Request $request, Bill $bill) {
  775. return view('app.admin.accounting-items-for-bill', ['bill' => $bill]);
  776. }
  777. public function putClaimProperty(Request $request)
  778. {
  779. $claim = Claim::where('uid', $request->get('uid'))->first();
  780. if($claim){
  781. $json = $claim->detail_json;
  782. if($json) {
  783. $parsed = json_decode($json, true);
  784. if(!$parsed) $parsed = [];
  785. }
  786. else $parsed = [];
  787. $parsed[$request->get('name')] = $request->get('value');
  788. $json = json_encode($parsed);
  789. DB::select("UPDATE claim SET detail_json = '$json' WHERE uid = '$claim->uid'");
  790. return json_encode([
  791. "success" => true,
  792. "data" => $claim->uid
  793. ]);
  794. }
  795. return json_encode([
  796. "success" => false,
  797. "error" => "Claim does not exist."
  798. ]);
  799. }
  800. public function putAppSetting(Request $request)
  801. {
  802. $key = $request->get('key');
  803. $value = $request->get('value');
  804. $setting = AppSetting::where('key', $key)->first();
  805. if($setting){
  806. DB::select("UPDATE app_setting SET value = :value WHERE key = :key", [
  807. 'value' => $value,
  808. 'key' => $key
  809. ]);
  810. }
  811. else {
  812. $uuid = Str::uuid();
  813. DB::select("INSERT INTO app_setting
  814. (id, uid, created_at, last_updated_at, created_by_pro_id, created_by_session_id, type, detail_json, key, value, data_type)
  815. VALUES
  816. (nextval('app_setting_id_seq'), :uuid, NOW(), NOW(),
  817. NULL, NULL, 'AppSetting', NULL,
  818. :key, :value, NULL)", [
  819. 'uuid' => $uuid,
  820. 'value' => $value,
  821. 'key' => $key
  822. ]);
  823. }
  824. return json_encode([
  825. "success" => true,
  826. "data" => null
  827. ]);
  828. }
  829. public function accountingItems(Request $request) {
  830. if($request->input('_ql')) DB::enableQueryLog();
  831. $columns = [
  832. 'ai.id',
  833. 'ai.uid',
  834. 'ai.entity_type',
  835. 'ai.entity_id',
  836. 'ai.entity_uid',
  837. 'ai.pro_id',
  838. 'ai.client_id',
  839. 'ai.note_id',
  840. 'ai.care_month_id',
  841. 'ai.bill_id',
  842. 'ai.supply_order_id',
  843. 'ai.positive_or_negative',
  844. 'ai.memo',
  845. 'ai.expected_value',
  846. 'ai.received_value',
  847. 'ai.created_at',
  848. 'ai.last_updated_at',
  849. 'ai.is_open',
  850. 'ai.is_active',
  851. 'cl.uid as client_uid',
  852. 'cl.name_first as client_name_first',
  853. 'cl.name_last as client_name_last',
  854. 'p.uid as pro_uid',
  855. 'p.name_first as pro_name_first',
  856. 'p.name_last as pro_name_last',
  857. 'n.uid as note_uid',
  858. 'cm.uid as care_month_uid',
  859. 'o.uid as supply_order_uid',
  860. 'b.uid as bill_uid',
  861. ];
  862. $tables = [
  863. 'accounting_item ai',
  864. 'LEFT JOIN client cl ON cl.id = ai.client_id',
  865. 'LEFT JOIN pro p ON p.id = ai.pro_id',
  866. 'LEFT JOIN note n ON n.id = ai.note_id',
  867. 'LEFT JOIN supply_order o ON o.id = ai.supply_order_id',
  868. 'LEFT JOIN care_month cm ON cm.id = ai.care_month_id',
  869. 'LEFT JOIN bill b ON b.id = ai.bill_id',
  870. ];
  871. $conditions = ["ai.id > 0"];
  872. $params = [];
  873. if($request->input('y')) {
  874. $conditions[] = 'EXTRACT(YEAR from ai.created_at) = :y';
  875. $params['y'] = intval($request->input('y'));
  876. }
  877. if($request->input('y') && $request->input('m')) {
  878. $conditions[] = 'EXTRACT(MONTH from ai.created_at) = :m';
  879. $params['m'] = intval($request->input('m'));
  880. }
  881. if($request->input('type') == 'revenue')
  882. $conditions[] = 'ai.positive_or_negative = 0';
  883. elseif($request->input('type') == 'cost')
  884. $conditions[] = 'ai.positive_or_negative = 1';
  885. if($request->input('context')) {
  886. $conditions[] = 'ai.entity_type = :context';
  887. $params['context'] = $request->input('context');
  888. }
  889. if($request->input('open') == 'open')
  890. $conditions[] = 'ai.is_open IS TRUE';
  891. elseif($request->input('open') == 'closed')
  892. $conditions[] = 'ai.is_open IS FALSE';
  893. if($request->input('active') == 'active')
  894. $conditions[] = 'ai.is_active IS TRUE';
  895. elseif($request->input('active') == 'inactive')
  896. $conditions[] = 'ai.is_active IS FALSE';
  897. if($request->input('pro')) {
  898. $conditions[] = 'p.uid = :proUid';
  899. $params['proUid'] = $request->input('pro');
  900. }
  901. if($request->input('client')) {
  902. $conditions[] = 'cl.uid = :clientUid';
  903. $params['clientUid'] = $request->input('client');
  904. }
  905. // execute
  906. $columns = implode(",\n", $columns);
  907. $tables = implode("\n", $tables);
  908. $conditions = implode(" AND\n", $conditions);
  909. $pagination = "";
  910. if($request->input('paginate')) {
  911. $page = $request->get('page', 1);
  912. $size = $request->get('size', 25);
  913. $pagination = "OFFSET ".(($page - 1) * $size) . " LIMIT $size";
  914. }
  915. $countSql = "SELECT count(ai.id) as count FROM $tables WHERE $conditions";
  916. $countResult = DB::select($countSql, $params);
  917. $dataSql = "SELECT
  918. $columns
  919. FROM
  920. $tables
  921. WHERE
  922. $conditions
  923. ORDER BY
  924. ai.created_at DESC
  925. $pagination";
  926. $dataResult = DB::select($dataSql, $params);
  927. $revenueTotalSql = "SELECT
  928. SUM(COALESCE(ai.expected_value, 0)) as total_expected_value, SUM(COALESCE(ai.received_value, 0)) as total_received_value
  929. FROM
  930. $tables
  931. WHERE
  932. $conditions AND ai.positive_or_negative = 0";
  933. $revenueTotalResult = DB::select($revenueTotalSql, $params);
  934. $costTotalSql = "SELECT
  935. SUM(COALESCE(ai.expected_value, 0)) as total_expected_value, SUM(COALESCE(ai.received_value, 0)) as total_received_value
  936. FROM
  937. $tables
  938. WHERE
  939. $conditions AND ai.positive_or_negative = 1";
  940. $costTotalResult = DB::select($costTotalSql, $params);
  941. $expectedTotal = $revenueTotalResult[0]->total_expected_value - $costTotalResult[0]->total_expected_value;
  942. $receivedTotal = $revenueTotalResult[0]->total_received_value - $costTotalResult[0]->total_received_value;
  943. $paginator = null;
  944. if($request->input('paginate')) {
  945. $paginator = new LengthAwarePaginator($dataResult, $countResult[0]->count, $size, $page);
  946. $paginator->setPath(route('accounting-items'));
  947. }
  948. // all clients
  949. $allClientsSQL = "SELECT DISTINCT(ai.client_id), cl.uid as client_uid, cl.name_first, cl.name_last
  950. FROM
  951. $tables
  952. WHERE
  953. $conditions";
  954. $allClientsResult = DB::select($allClientsSQL, $params);
  955. // all pros
  956. $allProsSQL = "SELECT DISTINCT(ai.pro_id), p.uid as pro_uid, p.name_first, p.name_last
  957. FROM
  958. $tables
  959. WHERE
  960. $conditions";
  961. $allProsResult = DB::select($allProsSQL, $params);
  962. $html = view('app.admin.accounting-items', [
  963. 'total' => $countResult[0]->count,
  964. 'records' => $dataResult,
  965. 'paginator' => $paginator,
  966. 'expected_total' => $expectedTotal,
  967. 'received_total' => $receivedTotal,
  968. 'all_clients' => $allClientsResult,
  969. 'all_pros' => $allProsResult,
  970. ]);
  971. if($request->input('_ql')) printQueryLog(DB::getQueryLog());
  972. return $html;
  973. }
  974. public function accountingItemTemplates(Request $request) {
  975. $records = AccountingItemTemplate::orderBy('entity_type')->get();
  976. return view('app.admin.accounting-item-templates', compact('records'));
  977. }
  978. public function createAccountingItemTemplate(Request $request) {
  979. $id = DB::select("select (coalesce(max(id), 1) + 1) as nextid from accounting_item_template");
  980. $template = new AccountingItemTemplate();
  981. $template->id = $id[0]->nextid;
  982. $template->uid = Uuid::uuid4();
  983. $template->is_active = true;
  984. $template->created_at = date('Y-m-d H:i:s');
  985. $template->last_updated_at = date('Y-m-d H:i:s');
  986. $template->entity_type = $request->input('entityType');
  987. $template->memo = $request->input('memo');
  988. $template->expected_value = $request->input('expectedValue');
  989. $template->received_value = $request->input('receivedValue');
  990. $template->is_open = $request->input('isOpen');
  991. $template->positive_or_negative = $request->input('positiveOrNegative');
  992. $template->save();
  993. return $this->pass($template->uid);
  994. }
  995. public function updateAccountingItemTemplate(Request $request) {
  996. $template = AccountingItemTemplate::where('uid', $request->input('uid'))->first();
  997. $template->last_updated_at = date('Y-m-d H:i:s');
  998. $template->entity_type = $request->input('entityType');
  999. $template->memo = $request->input('memo');
  1000. $template->expected_value = $request->input('expectedValue');
  1001. $template->received_value = $request->input('receivedValue');
  1002. $template->is_open = $request->input('isOpen');
  1003. $template->positive_or_negative = $request->input('positiveOrNegative');
  1004. $template->save();
  1005. return $this->pass($template->uid);
  1006. }
  1007. }