Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.10 KB | None | 0 0
  1. drop procedure if exists `ImageTotalInLibraryWithDates`;
  2.  
  3. DELIMITER ;;
  4. CREATE PROCEDURE `ImageTotalInLibraryWithDates`(IN `year_start` INT, IN `month_start` INT, IN `day_start` INT, IN `year_end` INT, IN `month_end` INT, IN `day_end` INT, IN `InAccountId` INT, IN `InClinicId` INT, IN `InIsBilled` CHAR)
  5. BEGIN
  6. DECLARE `DefaultInIsBilled` CHAR DEFAULT '';
  7.  
  8. DECLARE `PastDate` DATETIME;
  9. DECLARE `CurrentDate` DATETIME;
  10. DECLARE `TempDate` VARCHAR(20);
  11.  
  12.  
  13. SET `TempDate` = (SELECT CONCAT(`year_start`,'-',`month_start`,'-',`day_start`, ' ', ' 00:00:00'));
  14. SET `PastDate` = (SELECT CAST(`TempDate` AS DATETIME));
  15.  
  16. SET `TempDate` = (SELECT CONCAT(`year_end`,'-',`month_end`,'-',`day_end`, ' ', ' 23:59:59'));
  17. SET `CurrentDate` = (SELECT CAST(`TempDate` AS DATETIME));
  18.  
  19. if (`InIsBilled` IS NULL) THEN
  20. SET `DefaultInIsBilled` = '_';
  21. ELSE
  22. SET `DefaultInIsBilled` = `InIsBilled`;
  23. END IF;
  24.  
  25. select
  26. sum(Count) as `PeriodTotal`,
  27. GROUP_CONCAT(`Id`) as `ImageUploadIds`,
  28. SUBSTRING_INDEX( GROUP_CONCAT(CAST(`Id` AS CHAR) ORDER BY `CreateDate`), ',', 1 ) AS `BeginningId`,
  29. SUBSTRING_INDEX( GROUP_CONCAT(CAST(`CreateDate` AS CHAR) ORDER BY `CreateDate`), ',', 1 ) AS `BeginningDate`,
  30. SUBSTRING_INDEX( GROUP_CONCAT(CAST(`CreateDate` AS CHAR) ORDER BY `CreateDate` DESC), ',', 1 ) AS `EndingDate`,
  31. DATE_FORMAT(CreateDate, '%Y-%m') as `UploadPeriod`,
  32. `IsBilled`,
  33. YEAR(CreateDate) as `Year`,
  34. MONTH(CreateDate) as `Month`
  35. from ImageUploads iu
  36. inner join `Clinics` `c`
  37. on `c`.`clinicid` = `iu`.`ClinicId`
  38. where `iu`.`ClinicId` = `InClinicId`
  39. and `iu`.`IsBilled` LIKE `DefaultInIsBilled`
  40. and iu.DoctorPracticeId is not NULL
  41. and (`iu`.`CreateDate` between `PastDate` and `CurrentDate`)
  42. and `c`.`AccountId` = `InAccountId`
  43. group by `UploadPeriod`,`CreateDate`, `IsBilled`
  44. order by `UploadPeriod`, `Year`, `Month` desc ;
  45. END;;
  46. DELIMITER ;
  47.  
  48. drop procedure if exists `ImageTotalGetInLibraryWithDates`;
  49.  
  50. DELIMITER ;;
  51. CREATE PROCEDURE `ImageTotalGetInLibraryWithDates`(IN `year_start` INT, IN `month_start` INT, IN `day_start` INT, IN `year_end` INT, IN `month_end` INT, IN `day_end` INT, IN `InAccountId` INT, IN `InIsBilled` CHAR)
  52. BEGIN
  53. DECLARE `DefaultInIsBilled` CHAR DEFAULT '';
  54.  
  55. DECLARE `PastDate` DATETIME;
  56. DECLARE `CurrentDate` DATETIME;
  57. DECLARE `TempDate` VARCHAR(20);
  58.  
  59.  
  60. SET `TempDate` = (SELECT CONCAT(`year_start`,'-',`month_start`,'-',`day_start`, ' ', ' 00:00:00'));
  61. SET `PastDate` = (SELECT CAST(`TempDate` AS DATETIME));
  62.  
  63. SET `TempDate` = (SELECT CONCAT(`year_end`,'-',`month_end`,'-',`day_end`, ' ', ' 23:59:59'));
  64. SET `CurrentDate` = (SELECT CAST(`TempDate` AS DATETIME));
  65.  
  66.  
  67. if (`InIsBilled` IS NULL) THEN
  68. SET `DefaultInIsBilled` = '_';
  69. ELSE
  70. SET `DefaultInIsBilled` = `InIsBilled`;
  71. END IF;
  72.  
  73. if (`InAccountId` IS NULL) THEN
  74. select
  75. `iu`.`ClinicId`,
  76. sum(`iu`.`Count`) as `Total`,
  77. `c`.`AccountId`
  78. from
  79. ImageUploads `iu`
  80. inner join `Clinics` `c`
  81. on `c`.clinicid = `iu`.ClinicId
  82. where
  83. `iu`.`IsBilled` LIKE `DefaultInIsBilled`
  84. and iu.DoctorPracticeId is not NULL
  85. and (`iu`.`CreateDate` between `PastDate` and `CurrentDate`)
  86. GROUP BY `iu`.`ClinicId`;
  87. ELSE
  88. select
  89. `iu`.`ClinicId`,
  90. sum(`iu`.`Count`) as `Total`,
  91. `c`.`AccountId`
  92. from
  93. ImageUploads `iu`
  94. inner join `Clinics` `c`
  95. on `c`.clinicid = `iu`.ClinicId
  96. where
  97. `iu`.`IsBilled` LIKE `DefaultInIsBilled`
  98. and iu.DoctorPracticeId is not NULL
  99. and (`iu`.`CreateDate` between `PastDate` and `CurrentDate`)
  100. and `c`.`AccountId` = `InAccountId`
  101. GROUP BY `iu`.`ClinicId`;
  102. END IF;
  103. END;;
  104. DELIMITER ;
  105.  
  106. drop procedure if exists `BillingHistory`;
  107.  
  108. DELIMITER ;;
  109. CREATE PROCEDURE `BillingHistory`(IN `InAccountId` INT, IN `InYear` INT, IN `InMonth` INT, IN `InIsBilled` CHAR)
  110. BEGIN
  111.  
  112. DECLARE `DefaultInIsBilled` CHAR DEFAULT '';
  113. if (`InIsBilled` IS NULL) THEN
  114. SET `DefaultInIsBilled` = '_';
  115. ELSE
  116. SET `DefaultInIsBilled` = `InIsBilled`;
  117. END IF;
  118.  
  119.  
  120. if (`InYear` IS NULL AND `InMonth` IS NULL) THEN
  121. select
  122. se.*
  123. ,SUM(iu.Count) as PeriodTotal
  124. ,DATE_FORMAT(iu.CreateDate,'%Y-%m') as UploadPeriod
  125. ,YEAR(iu.CreateDate) as `Year`, MONTH(iu.CreateDate) as `Month`
  126. ,a.AccountId
  127. from ImageUploads iu
  128. inner join Clinics c
  129. on c.ClinicId = iu.ClinicId
  130. inner join Accounts a
  131. on c.AccountId = a.AccountId
  132. inner join StripeCharges sc
  133. on (
  134. sc.AccountId = a.AccountId
  135. -- to be removed and DATE_FORMAT(DATE_SUB(sc.CreateDate, INTERVAL 1 MONTH),'%Y-%m') = DATE_FORMAT(iu.CreateDate,'%Y-%m')
  136. -- Stripe Charge (less on month, and only use the year and month) compared to image upload period
  137. )
  138. inner join StripeEvents se
  139. on (
  140. se.Id = sc.StripeEventId
  141. )
  142. where a.AccountId = `InAccountId`
  143. and `iu`.IsBilled LIKE `DefaultInIsBilled`
  144. group by se.Id
  145. order by se.`CreateDate`, `Year`, `Month` desc
  146. ;
  147. ELSE
  148. select
  149. se.*
  150. ,SUM(`iu`.`Count`) as `PeriodTotal`
  151. ,DATE_FORMAT(`iu`.`CreateDate`,'%Y-%m') as `UploadPeriod`
  152. ,YEAR(`iu`.`CreateDate`) as `Year`,
  153. MONTH(`iu`.`CreateDate`) as `Month`
  154. , a.`AccountId`
  155. from `ImageUploads` `iu`
  156. inner join `Clinics` c
  157. on c.`ClinicId` = iu.`ClinicId`
  158. inner join Accounts a
  159. on c.`AccountId` = a.`AccountId`
  160. inner join `StripeCharges` sc
  161. on (
  162. sc.`AccountId` = a.`AccountId`
  163. -- to be removed and DATE_FORMAT(DATE_SUB(sc.`CreateDate`, INTERVAL 1 MONTH),'%Y-%m') = DATE_FORMAT(iu.`CreateDate`,'%Y-%m')
  164. -- Stripe Charge (less on month, and only use the year and month) compared to image upload period
  165. )
  166. inner join StripeEvents se
  167. on (
  168. se.`Id` = sc.`StripeEventId`
  169. )
  170. where a.`AccountId` = `InAccountId`
  171. and `iu`.`IsBilled` LIKE `DefaultInIsBilled`
  172. group by se.`Id`
  173. having `Year`= `InYear` and `Month` = `InMonth`
  174. order by se.`CreateDate`, `Year`, `Month` desc
  175. ;
  176.  
  177. END IF;
  178. END;;
  179. DELIMITER ;
  180.  
  181. drop procedure if exists `ImageTotalGet`;
  182.  
  183. DELIMITER ;;
  184. CREATE PROCEDURE `ImageTotalGet`(IN `year` INT, IN `month` INT, IN `InAccountId` INT, IN `InIsBilled` CHAR)
  185. BEGIN
  186. DECLARE `DefaultInIsBilled` CHAR DEFAULT '';
  187. if (`InIsBilled` IS NULL) THEN
  188. SET `DefaultInIsBilled` = '_';
  189. ELSE
  190. SET `DefaultInIsBilled` = `InIsBilled`;
  191. END IF;
  192.  
  193. if (`InAccountId` IS NULL) THEN
  194. select
  195. `iu`.`ClinicId`,
  196. sum(`iu`.`Count`) as `Total`,
  197. `c`.`AccountId`
  198. from
  199. ImageUploads `iu`
  200. inner join `Clinics` `c`
  201. on `c`.clinicid = `iu`.ClinicId
  202. where
  203. `iu`.`IsBilled` LIKE `DefaultInIsBilled`
  204. and YEAR(`iu`.`CreateDate`) = `year`
  205. and MONTH(`iu`.`CreateDate`) = `month`
  206. GROUP BY `iu`.`ClinicId`;
  207. ELSE
  208. select
  209. `iu`.`ClinicId`,
  210. sum(`iu`.`Count`) as `Total`,
  211. `c`.`AccountId`
  212. from
  213. ImageUploads `iu`
  214. inner join `Clinics` `c`
  215. on `c`.clinicid = `iu`.ClinicId
  216. where
  217. `iu`.`IsBilled` LIKE `DefaultInIsBilled`
  218. and YEAR(`iu`.`CreateDate`) = `year`
  219. and MONTH(`iu`.`CreateDate`) = `month`
  220. and `c`.`AccountId` = `InAccountId`
  221. GROUP BY `iu`.`ClinicId`;
  222. END IF;
  223. END;;
  224. DELIMITER ;
  225.  
  226. drop procedure if exists `ImageTotalGetInLibrary`;
  227.  
  228. DELIMITER ;;
  229. CREATE PROCEDURE `ImageTotalGetInLibrary`(IN `year` INT, IN `month` INT, IN `InAccountId` INT, IN `InIsBilled` CHAR)
  230. BEGIN
  231. DECLARE `DefaultInIsBilled` CHAR DEFAULT '';
  232. if (`InIsBilled` IS NULL) THEN
  233. SET `DefaultInIsBilled` = '_';
  234. ELSE
  235. SET `DefaultInIsBilled` = `InIsBilled`;
  236. END IF;
  237.  
  238. if (`InAccountId` IS NULL) THEN
  239. select
  240. `iu`.`ClinicId`,
  241. sum(`iu`.`Count`) as `Total`,
  242. `c`.`AccountId`
  243. from
  244. ImageUploads `iu`
  245. inner join `Clinics` `c`
  246. on `c`.clinicid = `iu`.ClinicId
  247. where
  248. `iu`.`IsBilled` LIKE `DefaultInIsBilled`
  249. and iu.DoctorPracticeId is not NULL
  250. and YEAR(`iu`.`CreateDate`) = `year`
  251. and MONTH(`iu`.`CreateDate`) = `month`
  252. GROUP BY `iu`.`ClinicId`;
  253. ELSE
  254. select
  255. `iu`.`ClinicId`,
  256. sum(`iu`.`Count`) as `Total`,
  257. `c`.`AccountId`
  258. from
  259. ImageUploads `iu`
  260. inner join `Clinics` `c`
  261. on `c`.clinicid = `iu`.ClinicId
  262. where
  263. `iu`.`IsBilled` LIKE `DefaultInIsBilled`
  264. and iu.DoctorPracticeId is not NULL
  265. and YEAR(`iu`.`CreateDate`) = `year`
  266. and MONTH(`iu`.`CreateDate`) = `month`
  267. and `c`.`AccountId` = `InAccountId`
  268. GROUP BY `iu`.`ClinicId`;
  269. END IF;
  270. END;;
  271. DELIMITER ;
  272.  
  273. drop procedure if exists `ImageTotalInLibrary`;
  274.  
  275. DELIMITER ;;
  276. CREATE PROCEDURE `ImageTotalInLibrary`(IN `InClinicId` INT, IN `InIsBilled` CHAR)
  277. BEGIN
  278. DECLARE `DefaultInIsBilled` CHAR DEFAULT '' ;
  279. if (`InIsBilled` IS NULL) THEN
  280. SET `DefaultInIsBilled` = '_';
  281. ELSE
  282. SET `DefaultInIsBilled` = `InIsBilled`;
  283. END IF;
  284.  
  285. select
  286. sum(Count) as `PeriodTotal`,
  287. GROUP_CONCAT(`Id`) as `ImageUploadIds`,
  288. SUBSTRING_INDEX( GROUP_CONCAT(CAST(`Id` AS CHAR) ORDER BY `CreateDate`), ',', 1 ) AS `BeginningId`,
  289. SUBSTRING_INDEX( GROUP_CONCAT(CAST(`CreateDate` AS CHAR) ORDER BY `CreateDate`), ',', 1 ) AS `BeginningDate`,
  290. SUBSTRING_INDEX( GROUP_CONCAT(CAST(`CreateDate` AS CHAR) ORDER BY `CreateDate` DESC), ',', 1 ) AS `EndingDate`,
  291. DATE_FORMAT(CreateDate, '%Y-%m') as `UploadPeriod`,
  292. `IsBilled`,
  293. YEAR(CreateDate) as `Year`,
  294. MONTH(CreateDate) as `Month`
  295. from ImageUploads iu
  296. where ClinicId = `InClinicId`
  297. and iu.DoctorPracticeId is not NULL
  298. and iu.`IsBilled` LIKE `DefaultInIsBilled`
  299. group by `UploadPeriod`,`CreateDate`, `IsBilled`
  300. order by `UploadPeriod`, `Year`, `Month` desc ;
  301. END;;
  302. DELIMITER ;
  303.  
  304. drop procedure if exists `ImageTotals`;
  305.  
  306. DELIMITER ;;
  307. CREATE PROCEDURE `ImageTotals`(IN `InClinicId` INT, IN `InIsBilled` CHAR)
  308. BEGIN
  309. DECLARE `DefaultInIsBilled` CHAR DEFAULT '';
  310. if (`InIsBilled` IS NULL) THEN
  311. SET `DefaultInIsBilled` = '_';
  312. ELSE
  313. SET `DefaultInIsBilled` = `InIsBilled`;
  314. END IF;
  315.  
  316. select
  317. sum(Count) as `PeriodTotal`,
  318. GROUP_CONCAT(`Id`) as `ImageUploadIds`,
  319. SUBSTRING_INDEX( GROUP_CONCAT(CAST(`Id` AS CHAR) ORDER BY `CreateDate`), ',', 1 ) AS `BeginningId`,
  320. SUBSTRING_INDEX( GROUP_CONCAT(CAST(`CreateDate` AS CHAR) ORDER BY `CreateDate`), ',', 1 ) AS `BeginningDate`,
  321. SUBSTRING_INDEX( GROUP_CONCAT(CAST(`CreateDate` AS CHAR) ORDER BY `CreateDate` DESC), ',', 1 ) AS `EndingDate`,
  322. DATE_FORMAT(CreateDate, '%Y-%m') as `UploadPeriod`,
  323. `IsBilled`,
  324. YEAR(CreateDate) as `Year`,
  325. MONTH(CreateDate) as `Month`
  326. from ImageUploads iu
  327. where ClinicId = `InClinicId`
  328. and iu.`IsBilled` LIKE `DefaultInIsBilled`
  329. group by `UploadPeriod`, `IsBilled`
  330. order by `UploadPeriod`, `Year`, `Month` desc ;
  331. END;;
  332. DELIMITER ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement