Advertisement
0re5ama

Untitled

Sep 9th, 2021
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.28 KB | None | 0 0
  1. do
  2. $$
  3. declare
  4. cur_head refcursor;
  5. cur_det refcursor;
  6. preg record;
  7. pitem record;
  8. errmsg text;
  9. item_id int;
  10. item_cat_id int;
  11. status_id int;
  12. br_id int;
  13. br_status int;
  14. br_det_count int;
  15. br_det_id int = :br_det_id;
  16. src_id int = 1; --Billing Register
  17. bill_type_id int = 1; --Vat Bill
  18. begin
  19.  
  20. open cur_head for
  21. select prd."BillDate",
  22. prd."InvoiceNo", prd."SellerName", prd."SellerPAN",
  23. br."EntryBy", br."BranchId", prd."PPNO",
  24. br."FiscalYearId", br."MonthId",
  25. br."BusinessPanNo",
  26. fy."FiscalYearEng"
  27. || br."BusinessPanNo"
  28. || 'BBBR'
  29. || lpad(nextval('"BillingRegister".seq_sales_tran')::varchar(10), 10, '0')
  30. as "TranNo",
  31. 0 as "TotalAmount",
  32. 0 as "TotalTaxableAmount",
  33. 0 as "TotalTaxAmount",
  34. 0 as "TotalTaxableImportAmount",
  35. 0 as "TotalTaxExemptAmount"
  36. -- sum(coalesce(replace(prd."TotalAmount", ',','')::numeric, 0)) as "TotalAmount",
  37. -- sum(coalesce(replace(prd."TaxableAmount", ',', '')::numeric, 0)) as "TotalTaxableAmount",
  38. -- sum(coalesce(NULLIF(regexp_replace(prd."TaxAmount", 'D','','g'), '')::numeric, 0)) as "TotalTaxAmount",
  39. -- sum(coalesce(NULLIF(regexp_replace(prd."TaxableImportAmount", 'D','','g'), '')::numeric, 0)) as "TotalTaxableImportAmount",
  40. -- sum(coalesce(NULLIF(regexp_replace(prd."TaxExemptAmount", 'D','','g'), '')::numeric, 0)) as "TotalTaxExemptAmount"
  41. from "BillingRegister"."PurchaseRegisterDumps" prd
  42. join "BillingRegister"."BillingRegisterDetails" brd
  43. on brd."Id" = prd."BillingRegisterDetailId"
  44. join "BillingRegister"."BillingRegisters" br
  45. on brd."BillingRegisterId" = br."Id"
  46. join "BillingRegister"."FiscalYears" fy
  47. on fy."Id" = br."FiscalYearId"
  48. where prd."BillingRegisterDetailId" = :br_det_id
  49. group by prd."BillDate", prd."InvoiceNo", prd."SellerName",
  50. prd."SellerPAN", br."EntryBy", br."BranchId", prd."PPNO",
  51. br."FiscalYearId", br."MonthId", br."BusinessPanNo", fy."FiscalYearEng";
  52. loop
  53. fetch cur_head into preg;
  54. exit when not found;
  55. begin
  56. insert into "BillingRegister"."PurchaseRegisters"
  57. (
  58. "BillDate",
  59. "BillDateNep",
  60. "InvoiceNo",
  61. "SellerName",
  62. "SellerPAN",
  63. "EntryBy",
  64. "BranchId",
  65. "PPNo",
  66. "FiscalYearId",
  67. "MonthId",
  68. "BillingDataSourceId",
  69. "ImportedData",
  70. "TranNo",
  71. "TotalAmount",
  72. "TotalPayableAmount",
  73. "TotalTaxAmount",
  74. "TotalTaxExemptAmount",
  75. "TotalTaxableAmount",
  76. "BRDetId",
  77. "TotalTaxableImportAmount",
  78. "UUID",
  79. "EntryDate"
  80. ) values (
  81. get_eng_date(preg."BillDate"),
  82. preg."BillDate",
  83. preg."InvoiceNo",
  84. preg."SellerName",
  85. preg."SellerPAN",
  86. preg."EntryBy",
  87. preg."BranchId",
  88. preg."PPNO",
  89. preg."FiscalYearId",
  90. preg."MonthId",
  91. src_id,
  92. true,
  93. preg."TranNo",
  94. coalesce(replace(preg."TotalAmount", ',', '')::numeric, 0),
  95. coalesce(replace(preg."TotalAmount", ',', '')::numeric, 0)
  96. + coalesce(replace(preg."TotalTaxAmount", ',', '')::numeric, 0),
  97. coalesce(replace(preg."TotalTaxAmount", ',', '')::numeric, 0),
  98. coalesce(replace(preg."TotalTaxExemptAmount", ',', '')::numeric, 0),
  99. coalesce(replace(preg."TotalTaxableAmount", ',', '')::numeric, 0),
  100. br_det_id,
  101. coalesce(replace(preg."TotalTaxableImportAmount", ',', '')::numeric, 0),
  102. uuid_generate_v4(),
  103. now()
  104. );
  105. exception when others then
  106. update "BillingRegister"."PurchaseRegisterDumps"
  107. set "StatusId" = 2,
  108. "Issues" = case
  109. when "Issues" is null
  110. then sqlerrm || ' (' || sqlstate || ')'
  111. else "Issues" || ', ' || sqlerrm || ' (' || sqlstate || ')'
  112. end
  113. where "BillingRegisterDetailId" = br_det_id
  114. and "InvoiceNo" = preg."InvoiceNo";
  115. end;
  116. end loop;
  117. close cur_head;
  118.  
  119. /*** TODO: insert into items here instead of below ***/
  120.  
  121. open cur_det for
  122. select
  123. prd."ItemName", coalesce(prd."Quantity", ',', '') as "Quantity", prd."TotalAmount",
  124. prd."TaxExemptAmount", prd."TaxableAmount",
  125. prd."TaxAmount", prd."TaxableImportAmount",
  126. prd."TaxableImportTax", prd."TaxableCapitalGoodsAmount",
  127. prd."TaxableCapitalGoodsTax", sr."Id", i."Id" as item_id,
  128. b."PAN", prd."Id" as prd_id
  129. from "BillingRegister"."PurchaseRegisterDumps" prd
  130. join "BillingRegister"."BillingRegisterDetails" brd
  131. on brd."Id" = prd."BillingRegisterDetailId"
  132. join "BillingRegister"."BillingRegisters" br
  133. on brd."BillingRegisterId" = br."Id"
  134. join "BillingRegister"."PurchaseRegisters" sr
  135. on sr."BranchId" = br."BranchId"
  136. and sr."InvoiceNo" = prd."InvoiceNo"
  137. join "BillingRegister"."Branches" b
  138. on b."Id" = br."BranchId"
  139. left outer join "BillingRegister"."Items" i
  140. on i."PAN" = b."PAN"
  141. and i."NameEng" = prd."ItemName"
  142. where prd."BillingRegisterDetailId" = :br_det_id
  143. and prd."StatusId" is null;
  144. loop
  145. fetch cur_det into pitem;
  146. exit when not found;
  147. begin
  148. errmsg := null;
  149. insert into public.test (entrydate, msg) values (now(), 'test');
  150. if coalesce(replace(pitem."TaxAmount", ',', ''), '') ~ '^[0-9\.]*$' then
  151. if coalesce(replace(pitem."TaxAmount", ',', '')::numeric, 0) = 0 then
  152. item_cat_id := 1;
  153. else
  154. item_cat_id := 2;
  155. end if;
  156. else
  157. errmsg := coalesce(errmsg, '') || ', TaxAmount should be number';
  158. end if;
  159. if item_cat_id = 2 and
  160. round(coalesce(NULLIF(regexp_replace(pitem."TaxableAmount", 'D','','g'), '')::numeric, 0) * 0.13, 2)
  161. <> round(coalesce(NULLIF(regexp_replace(pitem."TaxAmount", 'D','','g'), '')::numeric, 0), 2) then
  162. errmsg := coalesce(errmsg, '') || ', Incorrect Tax Amount';
  163. else
  164. item_id := (select "Id" from "BillingRegister"."Items" where "PAN" = pitem."PAN" and "NameEng" = pitem."ItemName");
  165. if pitem.item_id is null and item_id is null then
  166. insert into "BillingRegister"."Items"
  167. ("NameEng", "NameNep", "ItemSubCategoryId", "PAN")
  168. values
  169. (pitem."ItemName", pitem."ItemName", item_cat_id, pitem."PAN")
  170. returning "Id" into item_id;
  171. end if;
  172. begin
  173. insert into "BillingRegister"."PurchaseItems"
  174. (
  175. "ItemId",
  176. "Quantity",
  177. "TotalAmount",
  178. "TaxExemptAmount",
  179. "TaxableAmount",
  180. "TaxAmount",
  181. "TaxableImportAmount",
  182. "TaxableImportTax",
  183. "TaxableCapitalGoodsAmount",
  184. "TaxableCapitalGoodsTax",
  185. "PurchaseRegisterId",
  186. "TotalPayableAmount",
  187. "Rate",
  188. "PurchaseTypeId"
  189. )
  190. values
  191. (
  192. coalesce(pitem.item_id, item_id),
  193. coalesce(NULLIF(regexp_replace(pitem."Quantity", 'D','','g'), '')::numeric, 0),
  194. coalesce(NULLIF(regexp_replace(pitem."TotalAmount", 'D','','g'), '')::numeric, 0),
  195. coalesce(NULLIF(regexp_replace(pitem."TaxExemptAmount", 'D','','g'), '')::numeric, 0),
  196. coalesce(NULLIF(regexp_replace(pitem."TaxableAmount", 'D','','g'), '')::numeric, 0),
  197. coalesce(NULLIF(regexp_replace(pitem."TaxAmount", 'D','','g'), '')::numeric, 0),
  198. coalesce(NULLIF(regexp_replace(pitem."TaxableImportAmount", 'D','','g'), '')::numeric, 0),
  199. coalesce(NULLIF(regexp_replace(pitem."TaxableImportTax", 'D','','g'), '')::numeric, 0),
  200. coalesce(NULLIF(regexp_replace(pitem."TaxableCapitalGoodsAmount", 'D','','g'), '')::numeric, 0),
  201. coalesce(NULLIF(regexp_replace(pitem."TaxableCapitalGoodsTax", 'D','','g'), '')::numeric, 0),
  202. pitem."Id",
  203. coalesce(NULLIF(regexp_replace(pitem."TotalAmount", 'D','','g'), '')::numeric, 0) / coalesce(NULLIF(regexp_replace(pitem."Quantity", 'D','','g'), '')::numeric, 0),
  204. coalesce(NULLIF(regexp_replace(pitem."TotalAmount", 'D','','g'), '')::numeric, 0) + coalesce(NULLIF(regexp_replace(pitem."TaxAmount", 'D','','g'), '')::numeric, 0),
  205. case coalesce(NULLIF(regexp_replace(pitem."TaxableCapitalGoodsAmount", 'D','','g'), '')::numeric, 0)
  206. when 0 then 1
  207. else 2
  208. end
  209. );
  210. /*
  211. exception when others then
  212. errmsg := sqlerrm || ' (' || sqlstate || ')';
  213. */
  214. end;
  215. end if;
  216. if errmsg is not null then
  217. update "BillingRegister"."PurchaseRegisterDumps"
  218. set "StatusId" = 2,
  219. "Issues" = case
  220. when "Issues" is null
  221. then errmsg
  222. else "Issues" || ', ' || errmsg
  223. end
  224. where "Id" = pitem."prd_id";
  225. else
  226. update "BillingRegister"."PurchaseRegisterDumps"
  227. set "StatusId" = 1
  228. where "Id" = pitem."prd_id"
  229. and "StatusId" is null;
  230. end if;
  231. end;
  232. end loop;
  233. close cur_det;
  234.  
  235. if (
  236. select count("Id")
  237. from "BillingRegister"."PurchaseRegisterDumps"
  238. where "BillingRegisterDetailId" = br_det_id
  239. and "StatusId" = 2
  240. ) > 0 then
  241. status_id := 5;
  242. -- TODO: later do validation and then insert instead of insert and validate
  243.  
  244. delete from "BillingRegister"."PurchaseItems" si
  245. where "Id" in (
  246. select ssi."Id"
  247. from "BillingRegister"."PurchaseItems" ssi
  248. join "BillingRegister"."PurchaseRegisters" sr
  249. on sr."Id" = ssi."PurchaseRegisterId"
  250. where sr."BRDetId" = br_det_id
  251. );
  252.  
  253. delete from "BillingRegister"."PurchaseRegisters" sr
  254. where "BRDetId" = br_det_id;
  255.  
  256. else
  257. status_id := 4;
  258. delete from "BillingRegister"."PurchaseRegisterDumps"
  259. where "BillingRegisterDetailId" = br_det_id;
  260. end if;
  261.  
  262. update "BillingRegister"."BillingRegisterDetails"
  263. set "StatusId" = status_id
  264. where "Id" = br_det_id;
  265. select "BillingRegisterId" into br_id
  266. from "BillingRegister"."BillingRegisterDetails"
  267. where "Id" = br_det_id;
  268.  
  269. select count("Id") into br_det_count
  270. from "BillingRegister"."BillingRegisterDetails"
  271. where "BillingRegisterId" = br_id;
  272.  
  273. if (
  274. select count("Id")
  275. from "BillingRegister"."BillingRegisterDetails"
  276. where "BillingRegisterId" = br_id
  277. and "StatusId" = 1
  278. ) > 0 then
  279. br_status := 1;
  280. elsif (
  281. select count("Id")
  282. from "BillingRegister"."BillingRegisterDetails"
  283. where "BillingRegisterId" = br_id
  284. and "StatusId" = 2
  285. ) > 0 then
  286. br_status := 2;
  287. elsif (
  288. select count("Id")
  289. from "BillingRegister"."BillingRegisterDetails"
  290. where "BillingRegisterId" = br_id
  291. and "StatusId" = 3
  292. ) > 0 then
  293. br_status := 3;
  294. elsif (
  295. select count("Id")
  296. from "BillingRegister"."BillingRegisterDetails"
  297. where "BillingRegisterId" = br_id
  298. and ("StatusId" = 4 or "StatusId" = 7)
  299. ) = br_det_count then
  300. br_status := 4;
  301. else
  302. br_status := 5;
  303. end if;
  304.  
  305. update "BillingRegister"."BillingRegisters"
  306. set "BRStatusId" = br_status
  307. where "Id" = br_id;
  308. end;
  309. $$;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement