tinvoiceapply.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. package tinvoiceapply;
  2. import baseclass.PaoCust;
  3. import baseclass.tools.GetFieldsName;
  4. import baseclass.tools.Math;
  5. import openapi.base.data.Row;
  6. import openapi.base.data.Rows;
  7. import openapi.base.data.db.DBConnect;
  8. import p2.pao.PaoRemote;
  9. import p2.pao.PaoSet;
  10. import p2.pao.PaoSetRemote;
  11. import p2.util.P2AppException;
  12. import p2.util.P2Exception;
  13. import tinvoicebill.tinvoicebill;
  14. import java.util.HashMap;
  15. public class tinvoiceapply extends PaoCust {
  16. public tinvoiceapply(PaoSet arg0) {
  17. super(arg0);
  18. }
  19. @Override
  20. public void init() throws P2Exception {
  21. super.init();
  22. if (!toBeAdded()) {
  23. if ("审核".equals(getString("fstatus"))) {
  24. GetFieldsName fieldsName = new GetFieldsName(getName());
  25. setFieldFlag(fieldsName.getFields(), READONLY, true);
  26. }
  27. }
  28. }
  29. @Override
  30. public void add() throws P2Exception {
  31. super.add();
  32. setValue("fbillnum", getBillNum(getThisPaoSet().getApp()));
  33. PaoSetRemote tinvoiceapplySet = getPaoSet("$" + getName(), getName(), "fstatus='审核'");
  34. tinvoiceapplySet.setOrderBy("tinvoiceapplyid desc");
  35. tinvoiceapplySet.reset();
  36. if (!tinvoiceapplySet.isEmpty()) {
  37. String[] fields = new String[]{"finvoicechecker", "finvoicepayee", "finvoiceclerk"};
  38. copyFieldValue(tinvoiceapplySet.getPao(0), fields, this, fields);
  39. }
  40. if (getThisPaoSet().getApp().equalsIgnoreCase("INVOICEAPP")) {
  41. setValue("ftype", "自动", 11L);
  42. } else if (getThisPaoSet().getApp().equalsIgnoreCase("INVOICEOLD")) {
  43. setValue("ftype", "手工", 11L);
  44. }
  45. }
  46. @Override
  47. public void canDelete() throws P2Exception {
  48. if (!"新建".equals(getString("fstatus"))) {
  49. throw new P2AppException("", "非新建状态下不可删除");
  50. }
  51. super.canDelete();
  52. }
  53. @Override
  54. public void delete(long l) throws P2Exception {
  55. super.delete(l);
  56. getPaoSet("tinvoiceapplyorder").deleteAll();
  57. getPaoSet("tinvoicebill").deleteAll();
  58. }
  59. public void additemnos(PaoSetRemote saorderdetail) throws P2Exception {
  60. String[] fromfields = {"fparentid", "saorderdetailid"};
  61. String[] tpfields = {"saorderid", "saorderdetailid"};
  62. BatchAdd(saorderdetail, fromfields, getPaoSet("tinvoiceapplyorder"), tpfields, true);
  63. }
  64. @Override
  65. public void submitvalidate(boolean issubmit) throws P2Exception {
  66. super.submitvalidate(issubmit);
  67. PaoSetRemote tinvoiceapplyorderSet = getPaoSet("tinvoiceapplyorder");
  68. /****************** 单据检验 *********************/
  69. if (issubmit) {
  70. if (!"新建".equals(getString("fstatus"))) {
  71. throw new P2AppException("", "非新建状态下不可提交");
  72. }
  73. if (tinvoiceapplyorderSet.isEmpty()) {
  74. throw new P2AppException("", "订单信息为空不可提交");
  75. }
  76. if (tinvoiceapplyorderSet.min("finvoiceamount") <= 0) {
  77. throw new P2AppException("", "订单行开票金额必须大于0");
  78. }
  79. int i = 0;
  80. PaoRemote detailpao = null;
  81. while ((detailpao = tinvoiceapplyorderSet.getPao(i)) != null) {
  82. double finvoiceamount = detailpao.getDouble("finvoiceamount");//本次开票金额
  83. PaoRemote saorderdetail = detailpao.getPaoSet("saorderdetail").getPao(0);
  84. PaoSetRemote saorder_receivedviewSet = saorderdetail.getPaoSet("saorder_receivedview");
  85. double freceivedamount = 0;
  86. if (!saorder_receivedviewSet.isEmpty()) {
  87. freceivedamount = saorder_receivedviewSet.getPao(0).getDouble("freceivedamount");//订单已收货金额
  88. }
  89. String fsonum = saorderdetail.getString("fsonum");//订单号
  90. String fitemname = saorderdetail.getString("fitemname");//商品名称
  91. double finvoiceamount_order = saorderdetail.getDouble("finvoiceamount");//订单已开票金额
  92. if (finvoiceamount > Math.sub(freceivedamount, finvoiceamount_order)) {
  93. throw new P2AppException("", "订单" + fsonum + "商品" + fitemname + "本次开票金额" + finvoiceamount + "超过订单可开票金额" + Math.sub(freceivedamount, finvoiceamount_order));
  94. }
  95. i++;
  96. }
  97. } else {
  98. if (!"提交".equals(getString("fstatus"))) {
  99. throw new P2AppException("", "非提交状态下不可退回");
  100. }
  101. }
  102. }
  103. String orderinvoicecheck = "";
  104. public String orderinvoicecheck() throws P2Exception {
  105. orderinvoicecheck = "";
  106. DBConnect dbConnect = new DBConnect();
  107. Rows rows = dbConnect.runSqlQuery("select distinct t3.fsonum,t3.finvoicetaxnumber,t3.finvoicename from tinvoiceapply t1\n" +
  108. "inner join tinvoiceapplyorder t2 on t1.siteid=t2.siteid and t1.tinvoiceapplyid=t2.tinvoiceapplyid\n" +
  109. "inner join saorder t3 on t2.siteid=t3.siteid and t2.saorderid=t3.saorderid\n" +
  110. "where t1.tinvoiceapplyid='" + getUniqueIDValue() + "' and isnull(t1.finvoicetaxnumber,'')!=isnull(t3.finvoicetaxnumber,'') ");
  111. for (Row row : rows) {
  112. orderinvoicecheck = orderinvoicecheck + row.getString("fsonum") + ";";
  113. }
  114. return orderinvoicecheck;
  115. }
  116. @Override
  117. public void submit(boolean issubmit) throws P2Exception {
  118. super.submit(issubmit);
  119. if (issubmit) {
  120. if (!orderinvoicecheck.equals("")) {
  121. PaoSetRemote tinvoiceapplyorderSet = getPaoSet("tinvoiceapplyorder");
  122. HashMap<String, PaoRemote> map = getPaoMap(tinvoiceapplyorderSet, "saorderid");
  123. for (String saorderid : map.keySet()) {
  124. PaoRemote saorder = map.get(saorderid);
  125. saorder.setValue("finvoicetaxnumber", getString("finvoicetaxnumber"), 11L);
  126. saorder.setValue("finvoicename", getString("finvoicename"), 11L);
  127. saorder.setValue("finvoiceaddress", getString("finvoiceaddress"), 11L);
  128. }
  129. }
  130. }
  131. setValue("fstatus", issubmit ? "提交" : "新建", 11L);
  132. setValue("submitby", issubmit ? getUserName() : "", 11L);
  133. setValue("submitdate", issubmit ? getDateTime() : null, 11L);
  134. }
  135. public void checkvalidate(boolean ischeck) throws P2Exception {
  136. PaoSetRemote tinvoiceapplyorderSet = getPaoSet("tinvoiceapplyorder");
  137. PaoSetRemote tinvoicebillSet = getPaoSet("tinvoicebill");
  138. /****************** 单据检验 *********************/
  139. if (ischeck) {
  140. if (!"提交".equals(getString("fstatus"))) {
  141. throw new P2AppException("", "非提交状态下不可审核");
  142. }
  143. if (tinvoiceapplyorderSet.isEmpty()) {
  144. throw new P2AppException("", "订单信息为空不可审核");
  145. }
  146. if (tinvoicebillSet.isEmpty()) {
  147. throw new P2AppException("", "开票信息为空不可审核");
  148. }
  149. if (tinvoiceapplyorderSet.min("finvoiceamount") <= 0) {
  150. throw new P2AppException("", "订单行开票金额必须大于0");
  151. }
  152. PaoSetRemote tinvoiceapply_totalview = getPaoSet("tinvoiceapply_totalview");
  153. if (tinvoiceapply_totalview.getPao(0).getDouble("forderamount") != tinvoiceapply_totalview.getPao(0).getDouble("fbillamount")) {
  154. throw new P2AppException("", "订单金额和开票金额不一致");
  155. }
  156. int i = 0;
  157. PaoRemote detailpao = null;
  158. while ((detailpao = tinvoiceapplyorderSet.getPao(i)) != null) {
  159. double finvoiceamount = detailpao.getDouble("finvoiceamount");//本次开票金额
  160. PaoRemote saorderdetail = detailpao.getPaoSet("saorderdetail").getPao(0);
  161. PaoSetRemote saorder_receivedviewSet = saorderdetail.getPaoSet("saorder_receivedview");
  162. double freceivedamount = 0;
  163. if (!saorder_receivedviewSet.isEmpty()) {
  164. freceivedamount = saorder_receivedviewSet.getPao(0).getDouble("freceivedamount");//订单已收货金额
  165. }
  166. String fsonum = saorderdetail.getString("fsonum");//订单号
  167. String fitemname = saorderdetail.getString("fitemname");//商品名称
  168. double finvoiceamount_order = saorderdetail.getDouble("finvoiceamount");//订单已开票金额
  169. if (finvoiceamount > Math.sub(freceivedamount, finvoiceamount_order)) {
  170. throw new P2AppException("", "订单" + fsonum + "商品" + fitemname + "本次开票金额" + finvoiceamount + "超过订单可开票金额" + Math.sub(freceivedamount, finvoiceamount_order));
  171. }
  172. i++;
  173. }
  174. } else {
  175. if (!"手工".equalsIgnoreCase(getString("ftype"))) {
  176. int i = 0;
  177. while (tinvoicebillSet.getPao(i) != null) {
  178. if (!(tinvoicebillSet.getPao(i).getString("status").equals("") || tinvoicebillSet.getPao(i).getString("status").equals("22") || tinvoicebillSet.getPao(i).getString("status").equals("3"))) {
  179. throw new P2AppException("", "已对接开票接口,不可进行反审核!");
  180. }
  181. i++;
  182. }
  183. }
  184. }
  185. }
  186. public void check(boolean ischeck) throws P2Exception {
  187. if (ischeck) {
  188. //if ("手工".equalsIgnoreCase(getString("ftype"))) {
  189. PaoSetRemote tinvoiceapplyorderSet = getPaoSet("tinvoiceapplyorder");
  190. int i = 0;
  191. PaoRemote detailpao = null;
  192. while ((detailpao = tinvoiceapplyorderSet.getPao(i)) != null) {
  193. double finvoiceamount_detail = detailpao.getDouble("finvoiceamount");//本次开票金额
  194. PaoRemote saorderdetail = detailpao.getPaoSet("saorderdetail").getPao(0);
  195. double finvoiceamount_order = saorderdetail.getDouble("finvoiceamount");//订单已开票金额
  196. saorderdetail.setValue("finvoiceamount", Math.add(finvoiceamount_order, finvoiceamount_detail), 111L);
  197. i++;
  198. }
  199. //}
  200. setValue("fstatus", "审核", 11L);
  201. setValue("checkby", getUserName(), 11L);
  202. setValue("checkdate", getDateTime(), 11L);
  203. } else {
  204. //if ("手工".equalsIgnoreCase(getString("ftype"))) {
  205. PaoSetRemote tinvoiceapplyorderSet = getPaoSet("tinvoiceapplyorder");
  206. int i = 0;
  207. PaoRemote detailpao = null;
  208. while ((detailpao = tinvoiceapplyorderSet.getPao(i)) != null) {
  209. double finvoiceamount_detail = detailpao.getDouble("finvoiceamount");//本次开票金额
  210. PaoRemote saorderdetail = detailpao.getPaoSet("saorderdetail").getPao(0);
  211. double finvoiceamount_order = saorderdetail.getDouble("finvoiceamount");//订单已开票金额
  212. saorderdetail.setValue("finvoiceamount", Math.sub(finvoiceamount_order, finvoiceamount_detail), 111L);
  213. i++;
  214. }
  215. // }
  216. setValue("fstatus", "提交", 11L);
  217. setValue("checkby", "", 11L);
  218. setValue("checkdate", "", 11L);
  219. }
  220. }
  221. /**
  222. * 一键添加发票
  223. */
  224. public void addBills() throws P2Exception {
  225. PaoSetRemote tinvoiceapply_totalview = getPaoSet("tinvoiceapply_totalview");
  226. if (!tinvoiceapply_totalview.isEmpty() && getPaoSet("tinvoicebill").isEmpty()) {
  227. double forderamount = tinvoiceapply_totalview.getPao(0).getDouble("forderamount");
  228. double famountlimit = getDouble("famountlimit");
  229. double count = java.lang.Math.ceil(Math.div(forderamount, famountlimit));
  230. PaoSetRemote tinvoicebillSet = getPaoSet("tinvoicebill");
  231. for (int i = 0; i < count; i++) {
  232. PaoRemote tinvoicebill = tinvoicebillSet.addAtEnd();
  233. if (count == 1) {
  234. PaoSetRemote tinvoicebillitemSet = tinvoicebill.getPaoSet("tinvoicebillitem");
  235. PaoSetRemote tinvoiceapplyorderSet = getPaoSet("tinvoiceapplyorder");
  236. int n = 0;
  237. while (tinvoiceapplyorderSet.getPao(n) != null) {
  238. PaoRemote tinvoiceapplyorder = tinvoiceapplyorderSet.getPao(n);
  239. PaoRemote saorderdetail = tinvoiceapplyorder.getPaoSet("saorderdetail").getPao(0);
  240. PaoRemote tinvoicebillitem = tinvoicebillitemSet.addAtEnd();
  241. tinvoicebillitem.setValue("tinvoiceapplyordid", tinvoiceapplyorder.getUniqueIDValue());
  242. tinvoicebillitem.setValue("goodscode", saorderdetail.getString("fitemno"));
  243. tinvoicebillitem.setValue("goodsname", saorderdetail.getString("fitemname"));
  244. tinvoicebillitem.setValue("spectype", saorderdetail.getString("fspec"));
  245. tinvoicebillitem.setValue("unit", saorderdetail.getPaoSet("tunit").getPao(0).getString("fname"));
  246. tinvoicebillitem.setValue("num", tinvoiceapplyorder.getDouble("finvoiceqty"), 11L);
  247. tinvoicebillitem.setValue("price", tinvoiceapplyorder.getDouble("fprice"), 11L);
  248. tinvoicebillitem.setValue("taxincludedamount", tinvoiceapplyorder.getDouble("finvoiceamount"), 11L);
  249. n++;
  250. }
  251. }
  252. }
  253. }
  254. }
  255. /**
  256. * 根据勾选的蓝票信息生成红票信息
  257. *
  258. * @throws P2Exception
  259. */
  260. public void addRedBill() throws P2Exception {
  261. PaoSetRemote tinvoicebillSet = getPaoSet("tinvoicebill");
  262. int i = 0;
  263. tinvoicebill tinvoicebill = null;
  264. while ((tinvoicebill = (tinvoicebill) tinvoicebillSet.getPao(i)) != null) {
  265. if (tinvoicebill.isSelected()) {
  266. if (!tinvoicebill.getString("frb").equals("蓝")) {
  267. throw new P2AppException("", "红字发票不可执行此操作");
  268. }
  269. if (!tinvoicebill.getString("status").equals("2")) {
  270. throw new P2AppException("", "对没有完成开票的数据行不可进行红冲操作");
  271. }
  272. }
  273. i++;
  274. }
  275. i = 0;
  276. while ((tinvoicebill = (tinvoicebill) tinvoicebillSet.getPao(i)) != null) {
  277. if (tinvoicebill.isSelected()) {
  278. tinvoicebill.addRedBill();
  279. }
  280. i++;
  281. }
  282. }
  283. /**
  284. * 对接开票接口进行发票生成
  285. *
  286. * @throws P2Exception
  287. */
  288. public void createBlueInvoice() throws P2Exception {
  289. PaoSetRemote tinvoicebillSet = getPaoSet("tinvoicebill");
  290. if (tinvoicebillSet.getSelection().size() != 1) {
  291. throw new P2AppException("", "需勾选一行进行发票创建");
  292. }
  293. int i = 0;
  294. tinvoicebill tinvoicebill = null;
  295. while ((tinvoicebill = (tinvoicebill) tinvoicebillSet.getPao(i)) != null) {
  296. if (tinvoicebill.isSelected() && "".equals(tinvoicebill.getString("finvoiceserialnum"))) {
  297. tinvoicebill.createBlueInvoice();
  298. try {
  299. Thread.sleep(2000L);
  300. } catch (Exception e) {
  301. }
  302. }
  303. i++;
  304. }
  305. }
  306. /**
  307. * 对接开票接口进行发票生成
  308. *
  309. * @throws P2Exception
  310. */
  311. public void createRedInvoice() throws P2Exception {
  312. PaoSetRemote tinvoicebill_redSet = getPaoSet("tinvoicebill_red");
  313. if (tinvoicebill_redSet.getSelection().size() != 1) {
  314. throw new P2AppException("", "需勾选一行进行发票创建");
  315. }
  316. int i = 0;
  317. tinvoicebill tinvoicebill = null;
  318. while ((tinvoicebill = (tinvoicebill) tinvoicebill_redSet.getPao(i)) != null) {
  319. if (tinvoicebill.isSelected() && "".equals(tinvoicebill.getString("finvoiceserialnum"))) {
  320. tinvoicebill.createRedInvoice();
  321. try {
  322. Thread.sleep(2000L);
  323. } catch (Exception e) {
  324. }
  325. }
  326. i++;
  327. }
  328. }
  329. // public void queryInvoice() throws P2Exception {
  330. // PaoSetRemote tinvoicebillSet = getPaoSet("tinvoicebill");
  331. // int i = 0;
  332. // tinvoicebill tinvoicebill = null;
  333. // while ((tinvoicebill = (tinvoicebill) tinvoicebillSet.getPao(i)) != null) {
  334. // if (!"".equals(tinvoicebill.getString("finvoiceserialnum"))) {
  335. // tinvoicebill.queryInvoice();
  336. // }
  337. // i++;
  338. // }
  339. // }
  340. //
  341. // public void queryRedInvoice() throws P2Exception {
  342. // PaoSetRemote tinvoicebill_redSet = getPaoSet("tinvoicebill_red");
  343. // int i = 0;
  344. // tinvoicebill tinvoicebill = null;
  345. // while ((tinvoicebill = (tinvoicebill) tinvoicebill_redSet.getPao(i)) != null) {
  346. // if (!"".equals(tinvoicebill.getString("finvoiceserialnum"))) {
  347. // tinvoicebill.queryInvoice();
  348. // }
  349. // i++;
  350. // }
  351. // }
  352. @Override
  353. public void fieldValidate(Object paoField, String fieldname) throws P2Exception {
  354. super.fieldValidate(paoField, fieldname);
  355. }
  356. @Override
  357. public void fieldAction(Object paoField, String fieldname) throws P2Exception {
  358. super.fieldAction(paoField, fieldname);
  359. if ("FAGENTNUM".equals(fieldname)) {
  360. setValue("finvoicetaxnumber", "", 11L);
  361. setValue("finvoicename", "", 11L);
  362. setValue("finvoiceaddress", "", 11L);
  363. } else if ("FINVOICETAXNUMBER".equals(fieldname)) {
  364. String finvoicetaxnumber = getString(fieldname);
  365. DBConnect dbConnect = new DBConnect();
  366. Rows rows = dbConnect.runSqlQuery("select fphonenumber,(isnull(fbank,'')+isnull(fbankcardid,''))as finvoicebankandaccount from tagents_invoice where ftaxnumber='" + finvoicetaxnumber + "' and fbankcardid is not null");
  367. if (!rows.isEmpty()) {
  368. //setValue("FINVOICEMOBILE", rows.get(0).getString("fphonenumber"));
  369. setValue("FINVOICEBANKANDACCOUNT", rows.get(0).getString("finvoicebankandaccount"));
  370. } else {
  371. //setValue("FINVOICEMOBILE", "");
  372. setValue("FINVOICEBANKANDACCOUNT", "");
  373. }
  374. }
  375. }
  376. public void changetype() throws P2Exception {
  377. setValue("ftype", "手工", 11L);
  378. }
  379. }