salogis.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. package salogis;
  2. import baseclass.PaoCust;
  3. import baseclass.tools.GetFieldsName;
  4. import baseclass.tools.Math;
  5. import openapi.base.SQLFactory;
  6. import openapi.base.data.Row;
  7. import openapi.base.data.Rows;
  8. import openapi.base.data.db.DBConnect;
  9. import p2.pao.PaoRemote;
  10. import p2.pao.PaoSet;
  11. import p2.pao.PaoSetRemote;
  12. import p2.util.P2AppException;
  13. import p2.util.P2Exception;
  14. import java.util.HashMap;
  15. public class salogis extends PaoCust {
  16. public salogis(PaoSet arg0) {
  17. super(arg0);
  18. }
  19. @Override
  20. public void canDelete() throws P2Exception {
  21. super.canDelete();
  22. if (!"新建".equals(getString("fstatus"))) {
  23. throw new P2AppException("", "非新建状态不可进行删除操作");
  24. }
  25. }
  26. @Override
  27. public void init() throws P2Exception {
  28. super.init();
  29. if (!toBeAdded() && getThisPaoSet().getApp() != null) {
  30. if (!"新建".equals(getString("fstatus"))) {
  31. setFieldFlag(new GetFieldsName(getName()).getFields(),
  32. READONLY, true);
  33. } else {
  34. if (!getPaoSet("salogisdetail").isEmpty()) {
  35. setFieldFlag(new String[]{"fagentnum"},
  36. READONLY, true);
  37. }
  38. }
  39. }
  40. }
  41. @Override
  42. public void add() throws P2Exception {
  43. super.add();
  44. setValue("fbillnum", getBillNum(getThisPaoSet().getApp()));
  45. }
  46. @Override
  47. protected void save() throws P2Exception {
  48. super.save();
  49. }
  50. public void addBills(PaoSetRemote paoset) throws P2Exception {
  51. PaoSetRemote salogisdetailSet = getPaoSet("salogisdetail");
  52. String[] ffreightstatuss = getDistinctString(paoset, "ffreightstatus", true);
  53. String[] frecagentnum = getDistinctString(paoset, "frecagentnum", true);
  54. String[] faddress = getDistinctString(paoset, "faddress", true);
  55. String[] fcontact = getDistinctString(paoset, "fcontact", true);
  56. String[] fphonenumber = getDistinctString(paoset, "fphonenumber", true);
  57. if (ffreightstatuss.length > 1) {
  58. throw new P2AppException("", "不可同时添加两种运费状态的发货单");
  59. }
  60. if (ffreightstatuss.length == 1 && !"".equals(getString("ffreightstatus")) && !ffreightstatuss[0].equals(getString("ffreightstatus"))) {
  61. throw new P2AppException("", "不可同时添加两种运费状态的发货单");
  62. }
  63. if (frecagentnum.length > 1) {
  64. throw new P2AppException("", "不可同时添加两个不同收货单位的发货单");
  65. }
  66. if (frecagentnum.length == 1 && !"".equals(getString("frecagentnum")) && !frecagentnum[0].equals(getString("frecagentnum"))) {
  67. throw new P2AppException("", "不可同时添加两个不同收货单位的发货单");
  68. }
  69. if (faddress.length > 1) {
  70. throw new P2AppException("", "不可同时添加两个不同收货地址的发货单");
  71. }
  72. if (fcontact.length > 1) {
  73. throw new P2AppException("", "不可同时添加两个不同收货人的发货单");
  74. }
  75. if (fphonenumber.length > 1) {
  76. throw new P2AppException("", "不可同时添加两个不同联系方式的发货单");
  77. }
  78. if (!salogisdetailSet.isEmpty()) {
  79. DBConnect dbConnect = new DBConnect();
  80. Rows rows = dbConnect.runSqlQuery("select t1.faddress,t1.fcontact,t1.fphonenumber from SALOGISDETAIL t1 inner join sainvoice t2 on t1.fsourceid=t2.sainvoiceid and t1.FSOURCEOBJECT='sainvoice'and t1.fparentid=" + getUniqueIDValue());
  81. for (Row row : rows) {
  82. if (faddress.length == 1 && !faddress[0].equals(row.getString("faddress"))) {
  83. throw new P2AppException("", "不可同时添加两个不同收货地址的发货单");
  84. }
  85. if (fcontact.length == 1 && !fcontact[0].equals(row.getString("fcontact"))) {
  86. throw new P2AppException("", "不可同时添加两个不同收货人的发货单");
  87. }
  88. if (fphonenumber.length == 1 && !fphonenumber[0].equals(row.getString("fphonenumber"))) {
  89. throw new P2AppException("", "不可同时添加两个不同联系方式的发货单");
  90. }
  91. }
  92. }
  93. BatchAdd(paoset, new String[]{"sainvoiceid", "$SAINVOICE"}, salogisdetailSet, new String[]{"fsourceid", "fsourceobject"}, true);
  94. if (!salogisdetailSet.isEmpty()) {
  95. PaoSetRemote sainvoiceSet = salogisdetailSet.getPao(0).getPaoSet("sainvoice");
  96. if (!sainvoiceSet.isEmpty()) {
  97. setValue("ffreightstatus", sainvoiceSet.getPao(0).getString("ffreightstatus"), 11L);
  98. setValue("frecagentnum", sainvoiceSet.getPao(0).getString("frecagentnum"), 11L);
  99. setValue("faddress", sainvoiceSet.getPao(0).getString("faddress"), 11L);
  100. setValue("fcontact", sainvoiceSet.getPao(0).getString("fcontact"), 11L);
  101. setValue("fphonenumber", sainvoiceSet.getPao(0).getString("fphonenumber"), 11L);
  102. setValue("fprovince", sainvoiceSet.getPao(0).getString("fprovince"), 11L);
  103. setValue("fcity", sainvoiceSet.getPao(0).getString("fcity"), 11L);
  104. setValue("fcounty", sainvoiceSet.getPao(0).getString("fcounty"), 11L);
  105. }
  106. }
  107. String[] sainvocieids = getStrings(salogisdetailSet, "fsourceid");
  108. SQLFactory sqlFactory = new SQLFactory(this, "发货单备注查询");
  109. sqlFactory.addParameter("siteid", getString("siteid"));
  110. sqlFactory.addParameter_in("sainvoiceid", sainvocieids);
  111. DBConnect dbConnect = new DBConnect();
  112. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  113. if (!rows.isEmpty()) {
  114. setValue("fnotes", rows.get(0).getString("fnotes"), 11L);
  115. }
  116. }
  117. @Override
  118. public void checkvalidate(boolean ischeck) throws P2Exception {
  119. super.checkvalidate(ischeck);
  120. if (ischeck) {
  121. if (!"新建".equals(getString("fstatus"))) {
  122. throw new P2AppException("", "非新建状态不可进行审核操作");
  123. }
  124. PaoSetRemote salogisdetailSet = getPaoSet("salogisdetail");
  125. if (salogisdetailSet.isEmpty()) {
  126. throw new P2AppException("", "物流明细不能为空");
  127. }
  128. PaoRemote var = getSysVars();
  129. if (var.getBoolean("fissalogineedboxnum")) {
  130. if (getPaoSet("salogisdetail_pack").isEmpty()) {
  131. throw new P2AppException("", "装箱列表不能为空");
  132. }
  133. } else {
  134. //判断有无上传附件
  135. PaoSetRemote doclinksSet = getPaoSet("$doclinks", "doclinks", "ownertable='" + getName() + "' and ownerid=" + getUniqueIDValue());
  136. if (doclinksSet.isEmpty()) {
  137. throw new P2AppException("", "请上传装箱明细附件");
  138. }
  139. }
  140. PaoSetRemote salogis_boxcheck = getPaoSet("salogis_boxcheck");
  141. if (!salogis_boxcheck.isEmpty()) {
  142. int i = 0;
  143. while (salogis_boxcheck.getPao(i) != null) {
  144. throw new P2AppException("", "商品" + salogis_boxcheck.getPao(i).getString("fitemno") + "装箱数量和发货数量不符");
  145. }
  146. }
  147. int i = 0;
  148. while (salogisdetailSet.getPao(i) != null) {
  149. PaoSetRemote sainvoiceSet = salogisdetailSet.getPao(i).getPaoSet("sainvoice");
  150. if (sainvoiceSet.isEmpty()) {
  151. throw new P2AppException("", "发货单不存在");
  152. }
  153. PaoRemote sainvoice = sainvoiceSet.getPao(0);
  154. String finvonum = sainvoice.getString("finvonum");
  155. if (!"复核".equals(sainvoice.getString("fstatus"))) {
  156. throw new P2AppException("", "发货单" + finvonum + "不在复核状态");
  157. }
  158. //工具序列号状态变更
  159. String saordertype = sainvoiceSet.getPao(0).getPaoSet("saorder").getPao(0).getString("ftype");
  160. PaoSetRemote sainvoicedetail = salogisdetailSet.getPao(i).getPaoSet("sainvoicedetail");
  161. int n = 0;
  162. while (sainvoicedetail.getPao(n) != null) {
  163. PaoSetRemote salogis_toolscodeSet = sainvoicedetail.getPao(n).getPaoSet("salogis_toolscode");
  164. int m = 0;
  165. PaoRemote salogis_toolscodePao = null;
  166. while ((salogis_toolscodePao = salogis_toolscodeSet.getPao(m)) != null) {
  167. String fcode = salogis_toolscodePao.getString("fcode");
  168. if ("".equals(fcode)) {
  169. throw new P2AppException("", "工具序列号不可为空");
  170. }
  171. if (salogis_toolscodePao.getPaoSet("ttoolscode").isEmpty()) {
  172. throw new P2AppException("", "工具序列号" + salogis_toolscodePao.getString("fcode") + "不在工具序列号档案中");
  173. }
  174. PaoRemote ttoolscodePao = salogis_toolscodePao.getPaoSet("ttoolscode").getPao(0);
  175. if (!"在库".equals(ttoolscodePao.getString("fstatus"))) {
  176. throw new P2AppException("", "工具序列号" + fcode + "不是在库状态");
  177. }
  178. if ("工具借出单".equals(saordertype)) {
  179. ttoolscodePao.setValue("fstatus", ischeck ? "借出" : "在库");
  180. } else {
  181. ttoolscodePao.setValue("fstatus", ischeck ? "售出" : "在库");
  182. }
  183. ttoolscodePao.setValue("fisused", ischeck, 11L);
  184. ttoolscodePao.setValue("foutdate", ischeck ? getDate() : null, 11L);
  185. m++;
  186. }
  187. n++;
  188. }
  189. i++;
  190. }
  191. } else {
  192. if (!"审核".equals(getString("fstatus"))) {
  193. throw new P2AppException("", "非审核状态不可进行反审核操作");
  194. }
  195. }
  196. }
  197. @Override
  198. public void check(boolean ischeck) throws P2Exception {
  199. super.check(ischeck);
  200. DBConnect dbConnect = new DBConnect();
  201. // Rows rows = dbConnect.runSqlQuery("select t.saorderdetailid,t.finvoiceamount-t.finvoiceamount2 as finvoiceamount from (\n" +
  202. // "select t4.saorderdetailid,isnull(t4.finvoiceamount,0)as finvoiceamount,sum(isnull(t3.finvoiceamount,0))finvoiceamount2 from salogis t1\n" +
  203. // "inner join salogisdetail t2 on t1.salogisid=t2.fparentid\n" +
  204. // "inner join sainvoicedetail t3 on t2.fsourceid=t3.fparentid and t2.fsourceobject='SAINVOICE'\n" +
  205. // "inner join saorderdetail t4 on t3.fsourceid=t4.saorderdetailid\n" +
  206. // "where t1.salogisid='" + getUniqueIDValue() + "'\n" +
  207. // "group by t4.saorderdetailid,t4.finvoiceamount\n" +
  208. // ")t where t.finvoiceamount-t.finvoiceamount2>0");
  209. Rows rows = dbConnect.runSqlQuery("\n" +
  210. "select convert(bigint,t.saorderdetailid)as saorderdetailid,sum(t.finvoiceamount)-sum(t.finvoiceamount2) as finvoiceamount from (\n" +
  211. "\tselect t4.saorderdetailid,isnull(t4.finvoiceamount,0)as finvoiceamount,0 finvoiceamount2 from salogis t1\n" +
  212. "\tinner join salogisdetail t2 on t1.salogisid=t2.fparentid\n" +
  213. "\tinner join sainvoicedetail t3 on t2.fsourceid=t3.fparentid and t2.fsourceobject='SAINVOICE'\n" +
  214. "\tinner join saorderdetail t4 on t3.fsourceid=t4.saorderdetailid\n" +
  215. "\twhere t1.salogisid='" + getUniqueIDValue() + "'\n" +
  216. "\tunion all\n" +
  217. "\tselect t4.fsourceid,0 as finvoiceamount,isnull(t4.finvoiceamount,0) as finvoiceamount2 from salogis t1\n" +
  218. "\tinner join salogisdetail t2 on t1.salogisid=t2.fparentid\n" +
  219. "\tinner join sainvoicedetail t3 on t2.fsourceid=t3.fparentid and t2.fsourceobject='SAINVOICE'\n" +
  220. "\tinner join sainvoicedetail t4 on t3.fsourceid=t4.fsourceid\n" +
  221. "\twhere t1.salogisid='" + getUniqueIDValue() + "'\n" +
  222. "\t\n" +
  223. ")t \n" +
  224. "group by t.saorderdetailid\n" +
  225. "having sum(t.finvoiceamount)-sum(t.finvoiceamount2)>0");
  226. HashMap<Long, Double> finvoiceamountmap = new HashMap();//可分配开票金额map
  227. for (Row row : rows) {
  228. finvoiceamountmap.put(row.getLong("saorderdetailid"), row.getDouble("finvoiceamount"));
  229. }
  230. PaoSetRemote salogisdetailSet = getPaoSet("salogisdetail");
  231. int i = 0;
  232. while (salogisdetailSet.getPao(i) != null) {
  233. PaoSetRemote sainvoicedetail = salogisdetailSet.getPao(i).getPaoSet("sainvoicedetail");
  234. PaoSetRemote sainvoiceSet = salogisdetailSet.getPao(i).getPaoSet("sainvoice");
  235. sainvoiceSet.getPao(0).setValue("fstatus", ischeck ? "关闭" : "复核", 11L);
  236. sainvoiceSet.getPao(0).setValue("closeby", ischeck ? getUserName() : "", 11L);
  237. if (ischeck) {
  238. sainvoiceSet.getPao(0).setValue("closedate", getDateTime(), 11L);
  239. } else {
  240. sainvoiceSet.getPao(0).setValue("closedate", "", 11L);
  241. }
  242. if ("".equals(getString("frecagentnum"))) {
  243. setValue("ffreightstatus", sainvoiceSet.getPao(0).getString("ffreightstatus"), 11L);
  244. setValue("frecagentnum", sainvoiceSet.getPao(0).getString("frecagentnum"), 11L);
  245. }
  246. //工具序列号状态变更
  247. String saordertype = sainvoiceSet.getPao(0).getPaoSet("saorder").getPao(0).getString("ftype");
  248. int n = 0;
  249. while (sainvoicedetail.getPao(n) != null) {
  250. PaoSetRemote salogis_toolscodeSet = sainvoicedetail.getPao(n).getPaoSet("salogis_toolscode");
  251. int m = 0;
  252. PaoRemote salogis_toolscodePao = null;
  253. while ((salogis_toolscodePao = salogis_toolscodeSet.getPao(m)) != null) {
  254. PaoRemote ttoolscodePao = salogis_toolscodePao.getPaoSet("ttoolscode").getPao(0);
  255. if ("工具借出单".equals(saordertype)) {
  256. ttoolscodePao.setValue("fstatus", ischeck ? "借出" : "在库");
  257. } else {
  258. ttoolscodePao.setValue("fstatus", ischeck ? "售出" : "在库");
  259. }
  260. ttoolscodePao.setValue("fisused", ischeck, 11L);
  261. ttoolscodePao.setValue("foutdate", ischeck ? getDate() : null, 11L);
  262. ttoolscodePao.setValue("fagentnum", ischeck ? getString("fagentnum") : "", 11L);
  263. m++;
  264. }
  265. if (ischeck) {
  266. double famount = sainvoicedetail.getPao(n).getDouble("famount");
  267. long saorderdetailid = sainvoicedetail.getPao(n).getLong("fsourceid");
  268. if (finvoiceamountmap.containsKey(saorderdetailid)) {
  269. if (famount > finvoiceamountmap.get(saorderdetailid)) {
  270. sainvoicedetail.getPao(n).setValue("finvoiceamount", finvoiceamountmap.get(saorderdetailid));
  271. finvoiceamountmap.remove(saorderdetailid);
  272. } else {
  273. sainvoicedetail.getPao(n).setValue("finvoiceamount", famount);
  274. finvoiceamountmap.put(saorderdetailid, Math.sub(finvoiceamountmap.get(saorderdetailid), famount));
  275. }
  276. }
  277. } else {
  278. sainvoicedetail.getPao(n).setValue("finvoiceamount", 0, 11L);
  279. }
  280. n++;
  281. }
  282. i++;
  283. }
  284. if (ischeck) {
  285. setValue("fstatus", "审核", 11L);
  286. setValue("checkby", getUserName(), 11L);
  287. setValue("checkdate", getDateTime(), 11L);
  288. //微信消息推送
  289. StringBuffer fcontent = new StringBuffer();
  290. fcontent.append("您的订单已出库!");
  291. sendWechatMsg(getAgentQywechat(getString("fagentnum")), fcontent.toString(), this);
  292. try {
  293. //业务员邮件提醒
  294. PaoRemote tagent = getPaoSet("tagents").getPao(0);
  295. PaoSetRemote tagents_fieldSet = tagent.getPaoSet("TAGENTS_FIELD");
  296. String fsalers[] = getStrings(tagents_fieldSet, "hrid");
  297. //sendEmail(getMailByHrId(fsalers), tagent.getString("FAGENTSHORTNAME") + "物流单已审核", "【物流单审核】物流单:" + getString("fbillnum") + ",订单客户:" + tagent.getString("FAGENTSHORTNAME") + "已审核完成");
  298. //业务员邮件提醒
  299. if (getString("siteid").equalsIgnoreCase("TZ")) {
  300. sendReportByEmail(getMailByHrId(fsalers), "TZ_salogis", "where=salogis.salogisid=" + getUniqueIDValue(), "【物流单审核】物流单:" + getString("fbillnum") + ",订单客户:" + tagent.getString("FAGENTSHORTNAME") + "已审核完成");
  301. } else if (getString("siteid").equalsIgnoreCase("NB")) {
  302. sendReportByEmail(getMailByHrId(fsalers), "salogis", "where=salogis.salogisid=" + getUniqueIDValue(), "【物流单审核】物流单:" + getString("fbillnum") + ",订单客户:" + tagent.getString("FAGENTSHORTNAME") + "已审核完成");
  303. } else if (getString("siteid").equalsIgnoreCase("HY")) {
  304. sendReportByEmail(getMailByHrId(fsalers), "HY_salogis_test", "where=salogis.salogisid=" + getUniqueIDValue(), "【物流单审核】物流单:" + getString("fbillnum") + ",订单客户:" + tagent.getString("FAGENTSHORTNAME") + "已审核完成");
  305. }
  306. } catch (Exception e) {
  307. e.printStackTrace();
  308. }
  309. } else {
  310. setValue("fstatus", "新建", 11L);
  311. setValue("checkby", "", 11L);
  312. setValue("checkdate", "", 11L);
  313. }
  314. }
  315. /**
  316. * 更新发货单已核销数量
  317. *
  318. * @throws P2Exception
  319. */
  320. public void updatesainvoicewriteamount() throws P2Exception {
  321. DBConnect dbConnect = new DBConnect();
  322. Rows rows = dbConnect.runSqlQuery("select t2.fsourceid as saorderdetailid from salogisdetail t1 inner join sainvoicedetail t2 on t1.fsourceid=t2.fparentid where t1.fparentid='" + getUniqueIDValue() + "'");
  323. for (Row row : rows) {
  324. dbConnect.runSqlUpdate("exec updatesainvoicewriteoffamount " + row.getString("saorderdetailid"));
  325. }
  326. }
  327. /**
  328. * 收货确认验证
  329. *
  330. * @param fisreceive
  331. * @throws P2Exception
  332. */
  333. public void receivevalidate(boolean fisreceive) throws P2Exception {
  334. if (fisreceive) {
  335. if ("到货".equals(getString("fstatus"))) {
  336. throw new P2AppException("", "已收货,不能重复进行收货操作");
  337. }
  338. if (!"审核".equals(getString("fstatus"))) {
  339. throw new P2AppException("", "非审核状态不可进行收货操作");
  340. }
  341. } else {
  342. if (!"到货".equals(getString("fstatus"))) {
  343. throw new P2AppException("", "未收货,不能进行取消收货操作");
  344. }
  345. }
  346. }
  347. /**
  348. * 收货确认
  349. *
  350. * @param fisreceive
  351. * @throws P2Exception
  352. */
  353. public void receive(boolean fisreceive) throws P2Exception {
  354. PaoSetRemote salogisdetailSet = getPaoSet("salogisdetail");
  355. int i = 0;
  356. while (salogisdetailSet.getPao(i) != null) {
  357. PaoSetRemote sainvoiceSet = salogisdetailSet.getPao(i).getPaoSet("sainvoice");
  358. if (fisreceive) {
  359. sainvoiceSet.getPao(0).setValue("receivedate", getDate(), 11L);
  360. } else {
  361. sainvoiceSet.getPao(0).setValue("receivedate", "", 11L);
  362. }
  363. sainvoiceSet.getPao(0).setValue("fisreceiver", fisreceive, 11L);
  364. i++;
  365. }
  366. setValue("fisreceiver", fisreceive, 11L);
  367. if (fisreceive) {
  368. setValue("fstatus", "到货", 11L);
  369. setValue("receiveby", getUserName(), 11L);
  370. setValue("receivedate", getDateTime(), 11L);
  371. } else {
  372. setValue("fstatus", "审核", 11L);
  373. setValue("receiveby", "", 11L);
  374. setValue("receivedate", "", 11L);
  375. }
  376. }
  377. @Override
  378. public void fieldAction(Object paoField, String fieldname) throws P2Exception {
  379. super.fieldAction(paoField, fieldname);
  380. if ("FLOGISNO".equals(fieldname)) {
  381. String flogisno = getString("flogisno").replace(" ", "");
  382. setValue("FLOGISNO", flogisno, 11L);
  383. }
  384. }
  385. @Override
  386. public void fieldValidate(Object paoField, String fieldname) throws P2Exception {
  387. super.fieldValidate(paoField, fieldname);
  388. }
  389. @Override
  390. public PaoSetRemote fieldGetlist(Object paoField, PaoSetRemote list, String fieldname) throws P2Exception {
  391. return super.fieldGetlist(paoField, list, fieldname);
  392. }
  393. }