tinvoiceapply.java 17 KB

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