saorder.java 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637
  1. package saorder;
  2. import baseclass.tools.GetFieldsName;
  3. import baseclass.tools.Math;
  4. import ficashbill.ficashbill;
  5. import icstockbill.icstockbill;
  6. import openapi.base.SQLFactory;
  7. import openapi.base.data.Row;
  8. import openapi.base.data.Rows;
  9. import openapi.base.data.db.DBConnect;
  10. import p2.pao.PaoRemote;
  11. import p2.pao.PaoSet;
  12. import p2.pao.PaoSetRemote;
  13. import p2.util.P2AppException;
  14. import p2.util.P2Exception;
  15. import sainvoice.sainvoice;
  16. import saorder.base.BaseSaorder;
  17. import saorderdetail.saorderdetail;
  18. import taccbal.taccbal;
  19. import titem.titem;
  20. import twriteoffbill.twriteoffbill_orderchange;
  21. import java.util.*;
  22. public class saorder extends BaseSaorder {
  23. public saorder(PaoSet arg0) {
  24. super(arg0);
  25. }
  26. @Override
  27. public void init() throws P2Exception {
  28. super.init();
  29. try {
  30. if (!toBeAdded() && getThisPaoSet().getApp() != null) {
  31. if (!"新建".equals(getString("fstatus"))) {
  32. setFieldFlag(new GetFieldsName(getName()).getFields(),
  33. READONLY, true);
  34. setFieldFlag(new String[]{"closereason"}, READONLY, false);
  35. if ("提交".equals(getString("fstatus"))) {
  36. setFieldFlag("freturnmsg", READONLY, false);
  37. setFieldFlag("fprojectnotes", READONLY, false);
  38. setFieldFlag("fsaletype", READONLY, false);
  39. setFieldFlag("fnotes", READONLY, false);
  40. setFieldFlag("freviewtype", READONLY, false);
  41. }
  42. if ("审核".equals(getString("fstatus"))) {
  43. setFieldFlag("fnotes", READONLY, false);
  44. }
  45. } else {
  46. if ("促销订单".equals(getString("ftype"))) {
  47. //新建状态下促销类型为打包促销且没有生成配送方案的前提下,打包数量可编辑,否则为只读
  48. if ("打包促销".equals(getString("tpromotions.ftype")) && getPaoSet("saorderdetail_view2").isEmpty()) {
  49. setFieldFlag("fpromqty", READONLY, false);
  50. setFieldFlag("fpromqty", REQUIRED, true);
  51. } else {
  52. setFieldFlag("fpromqty", READONLY, true);
  53. setFieldFlag("fpromqty", REQUIRED, false);
  54. }
  55. }
  56. if ("02".equals(getString("faccclsnum"))) {
  57. setFieldFlag("fisusefl", READONLY, true);
  58. }
  59. if ("特殊订单".equals(getString("ftype"))) {
  60. setFieldFlag("ftypemx", READONLY, false);
  61. } else {
  62. setFieldFlag("ftypemx", READONLY, true);
  63. }
  64. if (!getPaoSet("saorderdetail_view").isEmpty()) {
  65. setFieldFlag(new String[]{"ftypemx", "fagentnum", "ftype", "fpromnum", "faccclsnum", "fprojectnum", "fbrand", "fborrowtype"}, READONLY, true);
  66. }
  67. }
  68. }
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. @Override
  74. public void canDelete() throws P2Exception {
  75. super.canDelete();
  76. if (!"新建".equals(getString("fstatus"))) {
  77. throw new P2AppException("", "非新建状态下不可删除");
  78. }
  79. if (getInt("fsourceid") > 0 && getOwner() == null) {
  80. throw new P2AppException("", "该订单由其他单据生成,不可删除");
  81. }
  82. }
  83. public boolean tobedelete = false;
  84. @Override
  85. public void delete(long l) throws P2Exception {
  86. canDelete();
  87. tobedelete = true;
  88. setValue("fstatus", "删除", 11L);
  89. setValue("description", "删除信息:" + getUserName() + "," + getDateTime_Str(), 11L);
  90. }
  91. @Override
  92. public void add() throws P2Exception {
  93. super.add();
  94. setValue("fsonum", createSoNum(), 11L);
  95. try {
  96. String app = getThisPaoSet().getApp();
  97. if ("SAORDER1".equalsIgnoreCase(app)) {
  98. setValue("ftype", "标准订单", 11L);
  99. }
  100. if ("SAORDER0".equalsIgnoreCase(app)) {
  101. setValue("ftype", "特殊订单", 11L);
  102. }
  103. if ("SAORDER01".equalsIgnoreCase(app)) {
  104. setValue("ftype", "工具订单", 11L);
  105. }
  106. if ("SAORDER02".equalsIgnoreCase(app)) {
  107. setValue("ftype", "项目订单", 11L);
  108. }
  109. if ("SAORDER2".equalsIgnoreCase(app)) {
  110. setValue("ftype", "特殊订单", 11L);
  111. }
  112. if ("SAORDER4".equalsIgnoreCase(app)) {
  113. setValue("ftype", "促销订单", 11L);
  114. }
  115. if ("SAORDER5".equalsIgnoreCase(app)) {
  116. setValue("ftype", "定制订单", 11L);
  117. }
  118. if ("SAORDER6".equalsIgnoreCase(app)) {
  119. setValue("ftype", "工具订单", 11L);
  120. }
  121. if ("SAORDER7".equalsIgnoreCase(app)) {
  122. setValue("ftype", "工具借出单", 11L);
  123. setValue("faccclsnum", "05", 11L);
  124. }
  125. if ("SAORDER8".equalsIgnoreCase(app)) {
  126. setValue("ftype", "工具借出单", 11L);
  127. setValue("faccclsnum", "05", 11L);
  128. }
  129. if ("SAORDER9".equalsIgnoreCase(app)) {
  130. setValue("ftype", "项目订单", 11L);
  131. }
  132. if ("SAORDER1".equalsIgnoreCase(app) || "SAORDER4".equalsIgnoreCase(app) || "SAORDER5".equalsIgnoreCase(app) || "SAORDER6".equalsIgnoreCase(app) || "SAORDER7".equalsIgnoreCase(app) || "SAORDER9".equalsIgnoreCase(app)) {
  133. String fagentnum = getAgentnumByHrid(getUserName());
  134. if (fagentnum != null) {
  135. setValue("fagentnum", fagentnum);
  136. }
  137. } else {
  138. //工厂做的单子
  139. setValue("createflag", true);
  140. }
  141. } catch (Exception e) {
  142. e.printStackTrace();
  143. }
  144. }
  145. @Override
  146. public boolean toBeDeleted() {
  147. return super.toBeDeleted();
  148. }
  149. @Override
  150. protected void save() throws P2Exception {
  151. super.save();
  152. if (!toBeDeleted() && !tobedelete) {
  153. if ("".equals(getString("fagentnum"))) {
  154. throw new P2AppException("", "没有指定经销商");
  155. }
  156. if (toBeAdded()) {
  157. if ("".equals(getString("fsonum"))) {
  158. setValue("fsonum", createSoNum(), 11L);
  159. }
  160. }
  161. PaoSetRemote saorderdetail_view = getPaoSet("saorderdetail_view");
  162. if ("".equals(getString("fpayagentnum"))) {
  163. setValue("fpayagentnum", getString("fagentnum"), 11L);
  164. }
  165. if ("".equals(getString("frecagentnum"))) {
  166. setValue("frecagentnum", getString("fagentnum"), 11L);
  167. }
  168. if (!toBeAdded() || (toBeAdded() && !saorderdetail_view.isEmpty())) {
  169. /**
  170. * 满减计算,满赠预计算
  171. */
  172. if ("新建".equals(getString("fstatus"))) {
  173. if (!"提交".equals(getPaoField("fstatus").getPreviousValue().asString())) {
  174. PaoSetRemote saorderdetail = getPaoSet("saorderdetail_view");
  175. ordercaculate(saorderdetail);
  176. /**
  177. * 是否免运费计算
  178. */
  179. isFreeFreightCaculate();
  180. }
  181. }
  182. }
  183. }
  184. }
  185. public void ordercaculate(PaoSetRemote saorderdetail) throws P2Exception {
  186. //订单商品总金额
  187. double ftotalamount = saorderdetail.sum("famount");
  188. //满减计算
  189. caculateMJ(saorderdetail);
  190. //订单满减后金额
  191. ftotalamount = Math.sub(ftotalamount, saorderdetail.sum("fmjamount"));
  192. //开始将折后总金额分摊到商品折后金额中
  193. if (ftotalamount > 0 && !"工具借出单".equals("ftype")) {
  194. //订单返利金使用金额
  195. double fuseflamount = caculateUseFLAmount(ftotalamount);
  196. double fagentuseflamount = getDouble("fagentuseflamount");//经销商设置的返利金使用金额
  197. if (fuseflamount > 0 && fagentuseflamount > 0 && fagentuseflamount < fuseflamount) {
  198. fuseflamount = fagentuseflamount;
  199. setValue("fuseflamount", fuseflamount, 11L);
  200. setValue("fuseflrate", fuseflamount / ftotalamount, 11L);
  201. } else {
  202. setValue("fuseflamount", fuseflamount, 11L);
  203. }
  204. shareCaculate(saorderdetail, ftotalamount, fuseflamount);
  205. }
  206. //满赠计算
  207. caculateMZ_justcaculate();
  208. }
  209. /**
  210. * 折后分摊计算
  211. *
  212. * @throws P2Exception
  213. */
  214. public void shareCaculate(PaoSetRemote saorderdetail, double ftotalamount, double fuseflamount) throws P2Exception {
  215. if (fuseflamount >= 0) {
  216. double frowflamount = 0;
  217. int n = 0;
  218. PaoRemote detailpao = null;
  219. PaoRemote lastdetailpao = null;
  220. while ((detailpao = saorderdetail.getPao(n)) != null) {
  221. if (!detailpao.toBeDeleted()) {
  222. double fqty = detailpao.getDouble("fqty");
  223. if (fqty != 0) {
  224. double fdetailamount = detailpao.getDouble("famount");
  225. double fmjamount = detailpao.getDouble("fmjamount");
  226. double fdetailflamount = Math.mul(Math.div(Math.sub(fdetailamount, fmjamount), ftotalamount), fuseflamount, 2);
  227. detailpao.setValue("fflamount", fdetailflamount, 2L);
  228. frowflamount = Math.add(frowflamount, fdetailflamount);
  229. lastdetailpao = detailpao;
  230. }
  231. }
  232. n++;
  233. }
  234. /**
  235. * 余数计算,满减额度分配过程中如果出现余数,将此数在最后个匹配的行上进行补齐。
  236. */
  237. double yu = Math.sub(frowflamount, fuseflamount);
  238. if (yu != 0) {
  239. lastdetailpao.setValue("fflamount", Math.sub(lastdetailpao.getDouble("fflamount"), yu), 2L);
  240. }
  241. }
  242. }
  243. /**
  244. * 从物料档案添加物料
  245. *
  246. * @param itemset 物料档案表
  247. * @throws P2Exception
  248. */
  249. public void additems(PaoSetRemote itemset) throws P2Exception {
  250. Vector<PaoRemote> v = itemset.getSelection();
  251. if (v.size() > 100) {
  252. throw new P2AppException("", "商品添加,不可一次超过100件商品");
  253. }
  254. PaoSetRemote SAORDERDETAIL = getPaoSet("saorderdetail_view");
  255. String ftype = getString("ftype");
  256. if ("标准订单".equals(ftype)) {
  257. Iterator<PaoRemote> it = v.iterator();
  258. //开始遍历勾选的物料
  259. while (it.hasNext()) {
  260. titem selectpao = (titem) it.next();
  261. if (selectpao.getDouble("fsaleprice") <= 0) {
  262. //如果经销商端出现金额小于0 的情况,则直接跳过。
  263. continue;
  264. }
  265. //添加数量,如果数量为0,则自动变为1
  266. double faddqty = selectpao.getDouble("faddqty");
  267. faddqty = faddqty == 0 ? 1 : faddqty;
  268. int i = 0;
  269. //isnotalreadyhave判断物料是否不存在
  270. boolean isnotalreadyhave = true;
  271. PaoRemote detailpao = null;
  272. while ((detailpao = SAORDERDETAIL.getPao(i)) != null) {
  273. if (detailpao.getString("fitemno").equals(
  274. selectpao.getString("fitemno")) && !detailpao.toBeDeleted()) {
  275. //如果匹配上了,则表示物料已存在,将该值设为false
  276. isnotalreadyhave = false;
  277. double fqty = detailpao.getDouble("fqty");
  278. double fnewqty = fqty + faddqty;
  279. detailpao.setValue("fqty", fnewqty);
  280. break;
  281. }
  282. i++;
  283. }
  284. //如果物料不存在,则新增订单行
  285. if (isnotalreadyhave) {
  286. detailpao = SAORDERDETAIL.addAtEnd();
  287. detailpao.setValue("fitemno",
  288. selectpao.getString("fitemno"));
  289. detailpao.setValue("fqty", selectpao.getDouble("faddqty"));
  290. }
  291. }
  292. } else if ("定制订单".equals(ftype)) {
  293. Iterator<PaoRemote> it = v.iterator();
  294. //开始遍历勾选的物料
  295. while (it.hasNext()) {
  296. PaoRemote selectpao = it.next();
  297. if (selectpao.getDouble("fsaleprice") <= 0) {
  298. //如果经销商端出现金额小于0 的情况,则直接跳过。
  299. continue;
  300. }
  301. //添加数量,如果数量为0,则自动变为1
  302. double faddqty = selectpao.getDouble("faddqty");
  303. faddqty = faddqty == 0 ? 1 : faddqty;
  304. int i = 0;
  305. //isnotalreadyhave判断物料是否不存在
  306. boolean isnotalreadyhave = true;
  307. PaoRemote detailpao = null;
  308. while ((detailpao = SAORDERDETAIL.getPao(i)) != null) {
  309. if (detailpao.getString("fitemno").equals(
  310. selectpao.getString("fitemno"))) {
  311. //如果匹配上了,则表示物料已存在,将该值设为false
  312. isnotalreadyhave = false;
  313. double fqty = detailpao.getDouble("fqty");
  314. double fnewqty = fqty + faddqty;
  315. detailpao.setValue("fqty", fnewqty);
  316. break;
  317. }
  318. i++;
  319. }
  320. //如果物料不存在,则新增订单行
  321. if (isnotalreadyhave) {
  322. detailpao = SAORDERDETAIL.addAtEnd();
  323. detailpao.setValue("fitemno",
  324. selectpao.getString("fitemno"));
  325. detailpao.setValue("fqty", selectpao.getDouble("faddqty"));
  326. detailpao.setValue("fprice",
  327. selectpao.getDouble("fsaleprice"));
  328. detailpao.setValue("foldprice",
  329. selectpao.getDouble("fsaleprice"));
  330. }
  331. }
  332. } else {
  333. String[] fromfields = {"fitemno", "faddqty"};
  334. String[] tofields = {"fitemno", "fqty"};
  335. BatchAdd(itemset, fromfields, SAORDERDETAIL, tofields, true);
  336. }
  337. this.getThisPaoSet().save();
  338. }
  339. /**
  340. * 从促销方案添加物料
  341. *
  342. * @param itemset 物料档案表
  343. * @throws P2Exception
  344. */
  345. public void additems_prom(PaoSetRemote itemset) throws P2Exception {
  346. Vector<PaoRemote> v = itemset.getSelection();
  347. PaoSetRemote SAORDERDETAIL = getPaoSet("saorderdetail_view");
  348. Iterator<PaoRemote> it = v.iterator();
  349. //开始遍历勾选的物料
  350. while (it.hasNext()) {
  351. PaoRemote selectpao = it.next();
  352. if (selectpao.getDouble("fsaleprice") <= 0) {
  353. //如果经销商端出现金额小于0 的情况,则直接跳过。
  354. continue;
  355. }
  356. //添加数量,如果数量为0,则自动变为1
  357. double faddqty = selectpao.getDouble("faddqty");
  358. faddqty = faddqty == 0 ? 1 : faddqty;
  359. int i = 0;
  360. //isnotalreadyhave判断物料是否不存在
  361. boolean isnotalreadyhave = true;
  362. PaoRemote detailpao = null;
  363. while ((detailpao = SAORDERDETAIL.getPao(i)) != null) {
  364. if (detailpao.getString("fitemno").equals(
  365. selectpao.getString("fitemno")) && !detailpao.toBeDeleted()) {
  366. //如果匹配上了,则表示物料已存在,将该值设为false
  367. isnotalreadyhave = false;
  368. double fqty = detailpao.getDouble("fqty");
  369. double fnewqty = 0;
  370. if ("打包促销".equals(getString("tpromotions.ftype"))) {
  371. fnewqty = faddqty;
  372. } else {
  373. fnewqty = fqty + faddqty;
  374. }
  375. detailpao.setValue("fqty", fnewqty, 2L);
  376. break;
  377. }
  378. i++;
  379. }
  380. //如果物料不存在,则新增订单行
  381. if (isnotalreadyhave) {
  382. detailpao = SAORDERDETAIL.addAtEnd();
  383. detailpao.setValue("fitemno",
  384. selectpao.getString("fitemno"));
  385. detailpao.setValue("fqty", selectpao.getDouble("faddqty"), 2L);
  386. detailpao.setValue("fprice",
  387. selectpao.getDouble("fsaleprice"));
  388. detailpao.setValue("foldprice",
  389. selectpao.getDouble("foldsaleprice"));
  390. }
  391. }
  392. }
  393. /**
  394. * 从促销方案添加物料
  395. *
  396. * @param selectpao 物料档案表
  397. * @throws P2Exception
  398. */
  399. public PaoRemote additems_prom(PaoRemote selectpao) throws P2Exception {
  400. PaoSetRemote SAORDERDETAIL = getPaoSet("saorderdetail_view");
  401. //开始遍历勾选的物料
  402. if (selectpao.getDouble("fsaleprice") <= 0) {
  403. //如果经销商端出现金额小于0 的情况,则直接跳过。
  404. return null;
  405. }
  406. //添加数量,如果数量为0,则自动变为1
  407. double faddqty = selectpao.getDouble("faddqty");
  408. faddqty = faddqty == 0 ? 1 : faddqty;
  409. int i = 0;
  410. //isnotalreadyhave判断物料是否不存在
  411. boolean isnotalreadyhave = true;
  412. PaoRemote detailpao = null;
  413. while ((detailpao = SAORDERDETAIL.getPao(i)) != null) {
  414. if (detailpao.getString("fitemno").equals(
  415. selectpao.getString("fitemno")) && !detailpao.toBeDeleted()) {
  416. //如果匹配上了,则表示物料已存在,将该值设为false
  417. isnotalreadyhave = false;
  418. double fqty = detailpao.getDouble("fqty");
  419. double fnewqty = 0;
  420. if ("打包促销".equals(getString("tpromotions.ftype"))) {
  421. fnewqty = faddqty;
  422. } else {
  423. fnewqty = fqty + faddqty;
  424. }
  425. detailpao.setValue("fqty", fnewqty, 2L);
  426. break;
  427. }
  428. i++;
  429. }
  430. //如果物料不存在,则新增订单行
  431. if (isnotalreadyhave) {
  432. detailpao = SAORDERDETAIL.addAtEnd();
  433. detailpao.setValue("fitemno",
  434. selectpao.getString("fitemno"));
  435. detailpao.setValue("fqty", selectpao.getDouble("faddqty"), 2L);
  436. detailpao.setValue("fprice",
  437. selectpao.getDouble("fsaleprice"));
  438. detailpao.setValue("foldprice",
  439. selectpao.getDouble("foldsaleprice"));
  440. }
  441. return detailpao;
  442. }
  443. // @Override
  444. // public void confirmvalidate(boolean isconfirm) throws P2Exception {
  445. //
  446. // super.confirmvalidate(isconfirm);
  447. // //订单表体,所有
  448. // PaoSetRemote saorderdetail = getPaoSet("saorderdetail");
  449. // //订单表体,除配送件外
  450. // PaoSetRemote saorderdetail_view = getPaoSet("saorderdetail_view");
  451. // //订单提交验证
  452. // if (isconfirm) {
  453. // if (!"新建".equals(getString("fstatus"))) {
  454. // throw new P2AppException("", "新建状态才可进行订单确认");
  455. // }
  456. // //经销商下单不允许出现金额为0的订单,配送件除外
  457. // if (getPaoSet("saorderdetail_view").min("fprice") < 0) {
  458. // throw new P2AppException("", "商品单价小于0,请删除该商品后重新添加");
  459. // }
  460. // if (saorderdetail.isEmpty()) {
  461. // throw new P2AppException("", "订单明细为空不可提交");
  462. // }
  463. // //非赠品商品数量不可等于0
  464. // if (saorderdetail_view.min("fqty") <= 0) {
  465. // throw new P2AppException("", "商品数量不可小于等于0");
  466. // }
  467. //
  468. // //配送方案是否生成验证
  469. // StringBuffer sql = new StringBuffer("");
  470. // sql.append("select t2.frownum,t2.fitemno from saorder t1 ");
  471. // sql.append("inner join saorderdetail t2 on t1.saorderid=t2.fparentid ");
  472. // sql.append("inner join titem t3 on t2.fitemno=t3.fitemno ");
  473. // sql.append("inner join titem_ration t4 on t3.fitemno=t4.fitemno ");
  474. // sql.append("inner join tration t5 on t4.frationnum=t5.frationnum ");
  475. // sql.append("where t1.fsonum='" + getString("fsonum") + "' and t2.fisration=0 and t3.fisrationmag=1 ");
  476. // sql.append("and t5.fstatus='审核' ");
  477. // sql.append("and (t5.fagentauthors=1 or exists(select *from tagents_ration where frationnum=t5.frationnum and fagentnum=t1.fagentnum))");
  478. // if ("标准订单".equals(getString("ftype"))) {
  479. // sql.append("and t5.fordertype1=1 ");
  480. // } else if ("特殊订单".equals(getString("ftype"))) {
  481. // sql.append("and t5.fordertype2=1 ");
  482. // } else if ("促销订单".equals(getString("ftype"))) {
  483. // sql.append("and t5.fordertype3=1 ");
  484. // } else if ("定制订单".equals(getString("ftype"))) {
  485. // sql.append("and t5.fordertype4=1 ");
  486. // } else {
  487. // sql.append("and 1=2");
  488. // }
  489. // ArrayList<HashMap<String, Object>> list = runQuerySQL(sql.toString());
  490. // if (!list.isEmpty() && getPaoSet("saorderdetail_view2").isEmpty()) {
  491. // int frownum = (int) list.get(0).get("frownum");
  492. // String fitemno = (String) list.get(0).get("fitemno");
  493. // throw new P2AppException("", "行" + frownum + "商品编号:" + fitemno + "存在配送方案,请选择配送方案后生成生成配送明细");
  494. // }
  495. // } else {
  496. // //订单退回验证
  497. // if (!"确认".equals(getString("fstatus"))) {
  498. // throw new P2AppException("", "非确认状态下不可退回");
  499. // }
  500. // }
  501. //
  502. // }
  503. // @Override
  504. // public void confirm(boolean isconfirm) throws P2Exception {
  505. //
  506. // super.confirm(isconfirm);
  507. //
  508. // if (isconfirm) {
  509. // /*单据状态变更*/
  510. // setValue("fstatus", "确认", 11L);
  511. // setValue("confirmby", getUserName(), 11L);
  512. // setValue("confirmdate", getDateTime(), 11L);
  513. //
  514. //
  515. // //微信消息推送
  516. // StringBuffer fcontent = new StringBuffer();
  517. // fcontent.append("您好,您有订单号为:").append(getString("fsonum")).append("的特殊订单,订单总金额:").append(
  518. // getDouble("saorder_totalview.famount")).append("请前往系统订单应用进行提交操作");
  519. // sendWechatMsg(getQyWechat(), fcontent.toString(), this);
  520. //
  521. //
  522. // } else {
  523. // /*单据状态变更*/
  524. // setValue("fstatus", "新建", 11L);
  525. // setValue("confirmby", "", 11L);
  526. // setValue("confirmdate", "", 11L);
  527. //
  528. //
  529. // /*退回微信消息推送*/
  530. // String fcontent = "尊敬的" + getString("fagentshortname")
  531. // + "经销商,您提交的订单号为" + getString("fsonum")
  532. // + "的特殊订单已被撤回。";
  533. // sendWechatMsg(getQyWechat(), fcontent, this);
  534. // }
  535. //
  536. // }
  537. /**
  538. * 检查商品价格是否变动
  539. */
  540. public HashMap<String, Double[]> checkitemprice() throws P2Exception {
  541. HashMap<String, Double[]> map = new HashMap<>();
  542. if (!"欠款发货".equals(getString("fborrowtype")) && !"特殊订单".equals(getString("ftype"))) {
  543. //订单表体,所有
  544. PaoSetRemote saorderdetail = getPaoSet("saorderdetail");
  545. int i = 0;
  546. while (saorderdetail.getPao(i) != null) {
  547. double order_foldprice = saorderdetail.getPao(i).getDouble("foldprice");
  548. double order_fsaleprice = saorderdetail.getPao(i).getDouble("fprice");
  549. PaoSetRemote titemset = saorderdetail.getPao(i).getPaoSet("titem");
  550. if (!titemset.isEmpty()) {
  551. titem titem = (titem) titemset.getPao(0);
  552. if (!saorderdetail.getPao(i).getBoolean("fpricechanged")) {
  553. //如果价格没有更改过,则进行价格判断
  554. double fsaleprice = titem.getAgentSalePrice(getString("fagentnum"));
  555. if (order_fsaleprice != fsaleprice) {
  556. map.put(titem.getString("fitemno"), new Double[]{order_fsaleprice, fsaleprice});
  557. }
  558. }
  559. if (!map.containsKey(titem.getString("fitemno"))) {
  560. /**
  561. * 销售价
  562. */
  563. double fprice = titem.getCardPriceByAagentnum(getString("fagentnum"));
  564. if (order_foldprice != fprice) {
  565. map.put(titem.getString("fitemno"), new Double[]{order_foldprice, fprice});
  566. }
  567. }
  568. }
  569. i++;
  570. }
  571. }
  572. return map;
  573. }
  574. /**
  575. * 更新商品价格
  576. */
  577. public void updateitemprice() throws P2Exception {
  578. HashMap<String, Double[]> map = new HashMap<>();
  579. //订单表体,所有
  580. PaoSetRemote saorderdetail = getPaoSet("saorderdetail_view");
  581. int i = 0;
  582. while (saorderdetail.getPao(i) != null) {
  583. PaoSetRemote titemset = saorderdetail.getPao(i).getPaoSet("titem");
  584. if (!titemset.isEmpty()) {
  585. titem titem = (titem) titemset.getPao(0);
  586. /**
  587. * 牌价
  588. */
  589. double foldprice = titem.getCardPriceByAagentnum(getString("fagentnum"));
  590. /**
  591. * 销售价
  592. */
  593. double fprice = titem.getAgentSalePrice(getString("fagentnum"));
  594. saorderdetail.getPao(i).setValue("fprice", fprice);
  595. saorderdetail.getPao(i).setValue("frateprice", fprice);
  596. saorderdetail.getPao(i).setValue("foldprice", foldprice, 11L);
  597. }
  598. i++;
  599. }
  600. }
  601. /**
  602. * 提交验证
  603. *
  604. * @param issubmit 是否提交 true表示提交,false表示退回
  605. * @throws P2Exception
  606. */
  607. @Override
  608. public void submitvalidate(boolean issubmit) throws P2Exception {
  609. super.submitvalidate(issubmit);
  610. if ("".equals(getString("fsignbacknum"))) {
  611. if ("工具订单".equals(getString("ftype")) || "标准订单".equals(getString("ftype")) || "项目订单".equals(getString("ftype"))) {
  612. if(getSite().equals("HY")) {
  613. throw new P2AppException("", "回签单信息不可为空!");
  614. }
  615. }
  616. }
  617. if ("工具借出单".equals(getString("ftype"))) {
  618. submitvalidate_tools(issubmit);
  619. return;
  620. }
  621. //订单表体,所有
  622. PaoSetRemote saorderdetail = getPaoSet("saorderdetail");
  623. //订单表体,除配送件外
  624. PaoSetRemote saorderdetail_view = getPaoSet("saorderdetail_view");
  625. //订单提交验证
  626. if (issubmit) {
  627. if (!"新建".equals(getString("fstatus"))) {
  628. throw new P2AppException("", "新建状态才可进行提交!");
  629. }
  630. {
  631. if ("".equals(getString("fphonenumber"))) {
  632. throw new P2AppException("", "联系方式不能为空");
  633. }
  634. if ("".equals(getString("fcontact"))) {
  635. throw new P2AppException("", "收货人不能为空");
  636. }
  637. if ("".equals(getString("faddress"))) {
  638. throw new P2AppException("", "收货地址不能为空");
  639. }
  640. if ("".equals(getString("finvoicename"))) {
  641. throw new P2AppException("", "开票单位不能为空");
  642. }
  643. if ("".equals(getString("finvoicetaxnumber"))) {
  644. throw new P2AppException("", "税号不能为空");
  645. }
  646. if ("".equals(getString("finvoiceaddress"))) {
  647. throw new P2AppException("", "开票地址不能为空");
  648. }
  649. if (saorderdetail_view.sum("fflamount") != getDouble("fuseflamount")) {
  650. throw new P2AppException("", "返利分摊金额计算错误!");
  651. }
  652. if (Math.sub(saorderdetail_view.sum("famount"), Math.add(saorderdetail_view.sum("fflamount"), saorderdetail_view.sum("fmjamount"))) != saorderdetail_view.sum("frateamount")) {
  653. throw new P2AppException("", "折后金额计算错误!");
  654. }
  655. }
  656. if (saorderdetail.isEmpty()) {
  657. throw new P2AppException("", "请至少添加一个商品");
  658. }
  659. //非赠品商品数量不可等于0
  660. if (saorderdetail_view.min("fqty") <= 0) {
  661. throw new P2AppException("", "商品数量不可小于等于0");
  662. }
  663. if ("标准订单".equals(getString("ftype")) && isLimitOrder()) {
  664. throw new P2AppException("", "您已被禁止订货");
  665. }
  666. if ("促销订单".equals(getString("ftype")) && isLimitOrder()) {
  667. throw new P2AppException("", "您已被禁止订货");
  668. }
  669. if ("项目订单".equals(getString("ftype"))) {
  670. if (getPaoSet("TPROJECT").isEmpty()) {
  671. throw new P2AppException("", "项目编号无效");
  672. }
  673. if (!"审核".equals(getPaoSet("TPROJECT").getPao(0).getString("fstatus"))) {
  674. throw new P2AppException("", "项目不在审核状态不可提交");
  675. }
  676. }
  677. //经销商下单不允许出现金额为0的订单,配送件除外
  678. if ("标准订单".equals(getString("ftype")) && !getBoolean("createflag")
  679. && getPaoSet("saorderdetail_view").min("fprice") <= 0) {
  680. throw new P2AppException("", "商品单价小于等于0,请删除该商品后重新添加");
  681. }
  682. //fissaleday
  683. int i = 0;
  684. while (saorderdetail.getPao(i) != null) {
  685. if (saorderdetail.getPao(i).getPaoSet("titem").getPao(0).getBoolean("fissaleday")) {
  686. if (saorderdetail.getPao(i).getString("fneeddate").equals("")) {
  687. throw new P2AppException("", "商品" + saorderdetail.getPao(i).getString("fitemname") + "交期管控,请填写交期");
  688. }
  689. }
  690. if ("样品".equals(getString("ftypemx"))) {
  691. if ("".equals(saorderdetail.getPao(i).getString("warehouse"))) {
  692. throw new P2AppException("", "样品订单需指定商品出库仓库");
  693. }
  694. }
  695. i++;
  696. }
  697. if ("促销订单".equals(getString("ftype"))) {
  698. PaoRemote tpromotion = getPaoSet("tpromotions").getPao(0);
  699. //活动结束日期
  700. Date fenddate = tpromotion.getDate("fenddate");
  701. //活动状态
  702. String fstatus = tpromotion.getString("fstatus");
  703. //活动起订量
  704. double fsaleminqty = tpromotion.getDouble("fsaleminqty");
  705. //活动起订金额
  706. double fsaleminamount = tpromotion.getDouble("fsaleminamount");
  707. if (fenddate.before(getDateTime())) {
  708. throw new P2AppException("", "促销活动已失效");
  709. }
  710. if (!"发布".equals(fstatus)) {
  711. throw new P2AppException("", "促销活动不在发布状态");
  712. }
  713. if ("打包促销".equals(getString("tpromotions.ftype")) && getDouble("fpromqty") <= 0) {
  714. throw new P2AppException("", "打包促销数量不可小于等于0");
  715. }
  716. if (saorderdetail_view.sum("fqty") < fsaleminqty) {
  717. throw new P2AppException("", "促销活动设置了起订量" + fsaleminqty + ",当前订单总数量不满足活动起订量");
  718. }
  719. if (saorderdetail_view.sum("famount") < fsaleminamount) {
  720. throw new P2AppException("", "促销活动设置了起订金额" + fsaleminamount + ",当前订单金额不满足活动起订金额");
  721. }
  722. //限量订购商品检验
  723. ArrayList<HashMap<String, Object>> list = runQuerySQL("select t1.fitemno,t1.fqty,t2.fsurplusqty from saorderdetail t1 inner join tpromproducts t2 on t1.fpromnum=t2.fpromnum and t1.fitemno=t2.fitemno where t2.fislimit=1 and t2.fsurplusqty<t1.fqty and t1.fsonum='" + getString("fsonum") + "'");
  724. if (!list.isEmpty()) {
  725. throw new P2AppException("", "商品编号" + list.get(0).get("fitemno") + "订购数量" + list.get(0).get("fqty") + "大于剩余可订购数量" + list.get(0).get("fsurplusqty"));
  726. }
  727. PaoSetRemote tpromagentSet = getPaoSet("tpromagents");
  728. if (!tpromagentSet.isEmpty()) {
  729. PaoRemote tpromagent = tpromagentSet.getPao(0);
  730. //是否限购
  731. if (tpromagent.getBoolean("fislimit")) {
  732. //限购类型
  733. String flimittype = tpromagent.getString("flimittype");
  734. //限购量
  735. double flimitvalue = tpromagent.getDouble("flimitvalue");
  736. //已购量
  737. double flimitvalue1 = tpromagent.getDouble("flimitvalue1");
  738. //订单总金额
  739. double ftotalamount = saorderdetail.sum("famount");
  740. if ("金额".equals(flimittype) && ftotalamount > Math.sub(flimitvalue, flimitvalue1)) {
  741. throw new P2AppException("", "您的订单总金额" + ftotalamount + "已超过剩余可订购金额" + Math.sub(flimitvalue, flimitvalue1));
  742. }
  743. }
  744. }
  745. }
  746. /*判断提交时是否需要进行扣款或账户冻结。如果需要则需进行账户可用余额的验证*/
  747. String fupdatebaltime = getSysVars().getString("fupdatebaltime");
  748. String fupdatfreeztime = getSysVars().getString("fupdatfreeztime");
  749. if ("订单提交".equals(fupdatebaltime) || "订单提交".equals(fupdatfreeztime)) {
  750. // 订单折后总额
  751. double frateamount = saorderdetail_view.sum("frateamount");
  752. //由于返利金使用额度是提交时临时计算,故不需要专门验证返利金的账户余额
  753. PaoSetRemote taccbal_view = getPaoSet("taccbal_view");
  754. //账户可用余额
  755. double fcanuseamount = taccbal_view.getPao(0).getDouble("fcanuseamount");
  756. //如果可用余额小于当前订单金额,则提示账户余额不足
  757. if (fcanuseamount < frateamount) {
  758. throw new P2AppException("", "账户可用余额不足");
  759. }
  760. }
  761. boolean haveRation = false;
  762. //配送方案是否生成验证
  763. StringBuffer sql = new StringBuffer();
  764. sql.append("select t2.frownum,t2.fitemno from saorder t1 ");
  765. sql.append("inner join saorderdetail t2 on t1.saorderid=t2.fparentid ");
  766. sql.append("inner join titem t3 on t2.fitemno=t3.fitemno and t2.siteid=t3.siteid ");
  767. sql.append("inner join titem_ration t4 on t3.fitemno=t4.fitemno and t2.siteid and t4.siteid ");
  768. sql.append("inner join tration t5 on t4.frationnum=t5.frationnum and t4.siteid=t5.siteid ");
  769. sql.append("where t1.fsonum='" + getString("fsonum") + "' and t1.siteid='" + getString("siteid") + "' and t2.fisration=0 and t3.fisrationmag=1 ");
  770. sql.append("and t5.fstatus='审核' ");
  771. sql.append("and (t5.fagentauthors=1 or exists(select *from tagents_ration where frationnum=t5.frationnum and fagentnum=t1.fagentnum)) ");
  772. if ("标准订单".equals(getString("ftype"))) {
  773. sql.append("and t5.fordertype1=1 ");
  774. } else if ("特殊订单".equals(getString("ftype"))) {
  775. sql.append("and t5.fordertype2=1 ");
  776. } else if ("促销订单".equals(getString("ftype"))) {
  777. sql.append("and t5.fordertype3=1 ");
  778. } else if ("定制订单".equals(getString("ftype"))) {
  779. sql.append("and t5.fordertype4=1 ");
  780. } else {
  781. sql.append("and 1=2 ");
  782. }
  783. ArrayList<HashMap<String, Object>> list = runQuerySQL(sql.toString());
  784. if (!list.isEmpty() && getPaoSet("saorderdetail_view2").isEmpty()) {
  785. int frownum = (int) list.get(0).get("frownum");
  786. String fitemno = (String) list.get(0).get("fitemno");
  787. throw new P2AppException("", "行" + frownum + "商品编号:" + fitemno + "存在配送方案,请选择配送方案后生成生成配送明细");
  788. }
  789. if (!getBoolean("createflag")) {
  790. //商品下架验证
  791. StringBuffer sql2 = new StringBuffer();
  792. sql2.append("select t2.frownum,t2.fitemno from saorder t1 ");
  793. sql2.append("inner join saorderdetail t2 on t1.saorderid=t2.fparentid ");
  794. sql2.append("inner join titem t3 on t2.fitemno=t3.fitemno and t2.siteid=t3.siteid ");
  795. sql2.append("where t1.fsonum='" + getString("fsonum") + "' and t1.siteid='" + getString("siteid") + "' and t3.fisonsale=0 and t1.ftype !='特殊订单'");
  796. ArrayList<HashMap<String, Object>> list2 = runQuerySQL(sql2.toString());
  797. if (!list2.isEmpty()) {
  798. int frownum = (int) list2.get(0).get("frownum");
  799. String fitemno = (String) list2.get(0).get("fitemno");
  800. throw new P2AppException("", "行" + frownum + "商品编号:" + fitemno + "已下架!");
  801. }
  802. }
  803. } else {
  804. //订单退回验证
  805. if (!"提交".equals(getString("fstatus"))) {
  806. throw new P2AppException("", "非提交状态下不可退回");
  807. }
  808. if ("SAORDER0".equalsIgnoreCase(getThisPaoSet().getApp()) && "".equals(getString("freturnmsg"))) {
  809. throw new P2AppException("", "退回原因不可为空");
  810. }
  811. }
  812. }
  813. /**
  814. * 提交验证
  815. *
  816. * @param issubmit 是否提交 true表示提交,false表示退回
  817. * @throws P2Exception
  818. */
  819. private void submitvalidate_tools(boolean issubmit) throws P2Exception {
  820. super.submitvalidate(issubmit);
  821. //订单表体,所有
  822. PaoSetRemote saorderdetail = getPaoSet("saorderdetail");
  823. //订单表体,除配送件外
  824. PaoSetRemote saorderdetail_view = getPaoSet("saorderdetail_view");
  825. //订单提交验证
  826. if (issubmit) {
  827. if (!"新建".equals(getString("fstatus"))) {
  828. throw new P2AppException("", "新建状态才可进行提交!");
  829. }
  830. {
  831. if ("".equals(getString("fphonenumber"))) {
  832. throw new P2AppException("", "联系方式不能为空");
  833. }
  834. if ("".equals(getString("fcontact"))) {
  835. throw new P2AppException("", "收货人不能为空");
  836. }
  837. if ("".equals(getString("faddress"))) {
  838. throw new P2AppException("", "收货地址不能为空");
  839. }
  840. }
  841. if (saorderdetail.isEmpty()) {
  842. throw new P2AppException("", "请至少添加一个商品");
  843. }
  844. //非赠品商品数量不可等于0
  845. if (saorderdetail_view.min("fqty") <= 0) {
  846. throw new P2AppException("", "商品数量不可小于等于0");
  847. }
  848. if (isLimitOrder()) {
  849. throw new P2AppException("", "您已被禁止订货");
  850. }
  851. } else {
  852. //订单退回验证
  853. if (!"提交".equals(getString("fstatus"))) {
  854. throw new P2AppException("", "非提交状态下不可退回");
  855. }
  856. if ("SAORDER0".equalsIgnoreCase(getThisPaoSet().getApp()) && "".equals(getString("freturnmsg"))) {
  857. throw new P2AppException("", "退回原因不可为空");
  858. }
  859. }
  860. }
  861. public void changesalervalidate() throws P2Exception {
  862. }
  863. public void changesaler() throws P2Exception {
  864. setFieldFlag("fsaler", READONLY, false);
  865. }
  866. /**
  867. * 提交
  868. *
  869. * @param issubmit
  870. * @throws P2Exception
  871. */
  872. @Override
  873. public void submit(boolean issubmit) throws P2Exception {
  874. super.submit(issubmit);
  875. if ("工具借出单".equals(getString("ftype"))) {
  876. submit_tools(issubmit);
  877. return;
  878. }
  879. /**
  880. * 订单指定账户
  881. */
  882. taccbal taccbal = null;
  883. PaoSetRemote taccbalSet = getPaoSet("taccbal");
  884. if (taccbalSet.isEmpty()) {
  885. taccbal = (taccbal) taccbalSet.addAtEnd();
  886. taccbal.setfields(getString("fpayagentnum"), getString("faccclsnum"));
  887. } else {
  888. taccbal = (taccbal) taccbalSet.getPao(0);
  889. }
  890. /**
  891. * 订单指定账户
  892. */
  893. taccbal taccbal_fl = null;
  894. PaoSetRemote taccbalSet_fl = getPaoSet("taccbal_fl");
  895. if (taccbalSet_fl.isEmpty()) {
  896. taccbal_fl = (taccbal) taccbalSet_fl.addAtEnd();
  897. taccbal_fl.setfields(getString("fpayagentnum"), "02");
  898. } else {
  899. taccbal_fl = (taccbal) taccbalSet_fl.getPao(0);
  900. }
  901. //系统参数表
  902. PaoRemote sysVars = getSysVars();
  903. /*获取系统参数*/
  904. //总仓库存可销售量扣减时机,
  905. String fupdateavlqtytime = sysVars.getString("fupdateavlqtytime");
  906. //订单扣款时机:订单提交、订单审核、销售出库单审核
  907. String fupdatebaltime = sysVars.getString("fupdatebaltime");
  908. //冻结金额时机:订单提交、订单审核
  909. String fupdatfreeztime = sysVars.getString("fupdatfreeztime");
  910. //订单提交自动审核的单据类型
  911. String forderautochecktype = getSysVars().getString("forderautochecktype");
  912. // 经销商参数是否需要提交自动审核
  913. boolean fsaorderautocheck = getPaoSet("tagents").getPao(0).getBoolean("fsaorderautocheck");
  914. //订单表体,包括辅材
  915. PaoSetRemote saorderdetailSet = getPaoSet("saorderdetail_view");
  916. // 订单指定账户扣款总额
  917. double ftotalrateamount = saorderdetailSet.sum("frateamount");
  918. // 订单使用返利总金额
  919. double fuseflamount = saorderdetailSet.sum("fflamount");
  920. //提交
  921. if (issubmit) {
  922. if ("促销订单".equals(getString("ftype"))) {
  923. updatePromotion(true);
  924. }
  925. if ("订单提交".equals(fupdatebaltime)) {
  926. /*如果系统参数设为为提交时扣款,则提交时需要生成支出凭证*/
  927. /*生成支出凭证*/
  928. String fnotes = "该单由销售订单" + getString("fsonum") + "提交时自动生成";
  929. /**
  930. * 生成返利支出凭证,如果返利金额为0,则不生成
  931. */
  932. createFicashbill_fl_Pay(taccbal_fl, fuseflamount, fnotes);
  933. /**
  934. * 生成账户支出凭证
  935. */
  936. createFicashbill_Pay(taccbal, ftotalrateamount, fnotes, true);
  937. } else if ("订单提交".equals(fupdatfreeztime)) {
  938. /*如果系统参数设为为提交时冻结金额,则提交时需要进行账户金额冻结*/
  939. /*账户余额冻结*/
  940. String fnotes = "由销售订单" + getString("fsonum") + "提交时冻结";
  941. updateTaccbal_freez(taccbal, ftotalrateamount, fnotes);
  942. }
  943. //总仓库存可销售量扣减
  944. if ("订单提交".equals(fupdateavlqtytime)) {
  945. //updateIcinvbal_total(false, false);
  946. }
  947. boolean isrowerr = getDistinctString(saorderdetailSet, "frownum", false).length != saorderdetailSet.count();
  948. //遍历订单表体
  949. int i = 0;
  950. saorderdetail detailpao = null;
  951. while ((detailpao = (saorderdetail) saorderdetailSet.getPao(i)) != null) {
  952. if (isrowerr) {
  953. detailpao.setValue("frownum", i + 1, 11L);
  954. }
  955. //重新计算产品的未发货数量
  956. detailpao.setValue("fundeliqty", detailpao.getDouble("fqty"), 11L);
  957. //如果是定制订单,则根据定制项生成定制信息
  958. if ("定制订单".equals(getString("ftype"))) {
  959. StringBuffer fcustomizedstr = new StringBuffer();
  960. PaoSetRemote saorder_customizedSet = detailpao.getPaoSet("saorder_customized");
  961. saorder_customizedSet.setOrderBy("SAORDER_CUSTOMIZID");
  962. int n = 0;
  963. PaoRemote saorder_customized = null;
  964. while ((saorder_customized = saorder_customizedSet.getPao(n)) != null) {
  965. fcustomizedstr.append(saorder_customized.getString("fname")).append(":").append(saorder_customized.getString("fvalue")).append(";");
  966. n++;
  967. }
  968. detailpao.setValue("fcustomizedstr", "{" + fcustomizedstr.toString() + "}", 11L);
  969. }
  970. i++;
  971. }
  972. /*单据状态变更*/
  973. setValue("fstatus", "提交", 11L);
  974. setValue("submitby", getUserName(), 11L);
  975. setValue("submitdate", getDateTime(), 11L);
  976. setValue("submitcount", getInt("submitcount") + 1, 11L);
  977. // 判断提交是否自动审核
  978. if (fsaorderautocheck && forderautochecktype.contains(getString("ftype"))) {
  979. boolean canCheck = true;
  980. if ("订单审核".equals(fupdatebaltime) || "订单审核".equals(fupdatfreeztime)) {
  981. // 如果系统参数设置订单审核扣款时,此时账户尚未进行扣款或冻结,
  982. // 审核前需验证账户余额是否充足,如账户余额不足,则不允许自动审核。
  983. // 通过经销商账户余额查询视图获取账户可用余额
  984. double fcanuseamount = getPaoSet("taccbal_view").getPao(0).getDouble("fcanuseamount");
  985. if (fcanuseamount < ftotalrateamount) {
  986. //如果账户可用余额小于当前订单金额,则不允许自动审核。
  987. canCheck = false;
  988. }
  989. }
  990. //如果已在订单提交时金扣款或冻结,或者账户余额充足,则当前订单自动审核
  991. if (canCheck) {
  992. check(true);
  993. }
  994. }
  995. //微信消息推送
  996. StringBuffer fcontent = new StringBuffer();
  997. fcontent.append("您好,您的订单号为:").append(getString("fsonum")).append("的").append(
  998. getString("ftype")).append("已提交,订单总金额:").append(
  999. getDouble("saorder_totalview.famount"));
  1000. sendWechatMsg(getQyWechat(), fcontent.toString(), this);
  1001. /**
  1002. * 如果是交期管控的,且库存不足的,则进行邮件提醒
  1003. */
  1004. if (issaleday() && !checkStockStatus()) {
  1005. //交期回复提醒
  1006. sendEmail(getMailByHrGroup(getString("siteid") + "1005"), getString("FAGENTSHORTNAME") + "《" + getString("ftype") + "》提交,需要进行交期回复", getString("FAGENTSHORTNAME") + "《" + getString("ftype") + "》提交待交期回复;订单号:" + getString("fsonum"));
  1007. }
  1008. //销管订单提交待审核邮件提醒
  1009. sendEmail(getMailByHrGroup(getString("siteid") + "1001"), getString("FAGENTSHORTNAME") + "《" + getString("ftype") + "》提交待审", "【订单领域】" + getString("fieldname") + ",【订单提交待审核】订单号:" + getString("fsonum") + "(订单客户:" + getString("FAGENTSHORTNAME") + ",备注:" + ("".equals(getString("fnotes")) ? "无" : getString("fnotes")) + ")已提交请审核;");
  1010. //任务箱提醒
  1011. createAssignment(getHrIdByHrGroup(getString("siteid") + "1001"), "SAORDER0", getString("FAGENTSHORTNAME") + "《" + getString("ftype") + "》提交待审", getString("FAGENTSHORTNAME") + "《" + getString("ftype") + "》提交待审;订单号:" + getString("fsonum"));
  1012. } else {
  1013. deleteAssignment();
  1014. if ("促销订单".equals(getString("ftype"))) {
  1015. updatePromotion(false);
  1016. }
  1017. /*退回操作*/
  1018. /*资金账户计算*/
  1019. if ("订单提交".equals(fupdatebaltime)) {
  1020. /*如果系统参数设为为提交时扣款,则退回时需要生成负数的支出凭证*/
  1021. /*生成支出凭证*/
  1022. String fnotes = "该单由销售订单" + getString("fsonum") + "退回时自动生成";
  1023. //PaoSetRemote ficashbillset = getPaoSet("$ficashbill_fl", "ficashbill", "ftypemx='返利金' and ftype='支出' and fstatus='审核' and fsourceobject='SAORDER' and fsourceid=" + getUniqueIDValue());
  1024. //返利不退
  1025. //createFicashbill_fl_Pay(taccbal_fl, -famount_fl, fnotes);
  1026. //createFicashbill_Pay(taccbal, -(Math.sub(ftotalamount, fuseflamount)), fnotes, true);
  1027. createFicashbill_fl_Pay(taccbal_fl, -fuseflamount, fnotes);
  1028. createFicashbill_Pay(taccbal, -ftotalrateamount, fnotes, true);
  1029. } else if ("订单提交".equals(fupdatfreeztime)) {
  1030. /*如果系统参数设为为提交时冻结账户余额,则退回时需要进行账户余额的解冻*/
  1031. deleteTaccbal_freez();
  1032. }
  1033. /*库存计算*/
  1034. //总仓库存可销售量扣减
  1035. if ("订单提交".equals(fupdateavlqtytime)) {
  1036. //如果系统参数设为提交时进行可销售数量的扣减,则退回时需要将订单数量返回到库存的可销售数量中。
  1037. //updateIcinvbal_total(true, false);
  1038. }
  1039. /*单据状态变更*/
  1040. setValue("fstatus", "新建", 11L);
  1041. setValue("submitby", "", 11L);
  1042. setValue("submitdate", "", 11L);
  1043. setValue("fnotes", getString("fnotes") + "----订单退回", 11L);
  1044. /*退回微信消息推送*/
  1045. String fcontent = "尊敬的" + getString("fagentshortname")
  1046. + "经销商,您提交的订单号为" + getString("fsonum")
  1047. + "的订单审核不通过,请登录系统查询。";
  1048. sendWechatMsg(getQyWechat(), fcontent, this);
  1049. }
  1050. }
  1051. /**
  1052. * 工具借出申请单提交
  1053. *
  1054. * @param issubmit
  1055. * @throws P2Exception
  1056. */
  1057. private void submit_tools(boolean issubmit) throws P2Exception {
  1058. super.submit(issubmit);
  1059. if (issubmit) {
  1060. /*单据状态变更*/
  1061. setValue("fstatus", "提交", 11L);
  1062. setValue("submitby", getUserName(), 11L);
  1063. setValue("submitdate", getDateTime(), 11L);
  1064. setValue("submitcount", getInt("submitcount") + 1, 11L);
  1065. /**
  1066. * 如果是交期管控的,且库存不足的,则进行邮件提醒
  1067. */
  1068. if (issaleday() && !checkStockStatus()) {
  1069. //交期回复提醒
  1070. sendEmail(getMailByHrGroup(getString("siteid") + "1005"), getString("FAGENTSHORTNAME") + "《" + getString("ftype") + "》提交,需要进行交期回复", getString("FAGENTSHORTNAME") + "《" + getString("ftype") + "》提交待交期回复;订单号:" + getString("fsonum"));
  1071. }
  1072. //销管订单提交待审核邮件提醒
  1073. sendEmail(getMailByHrGroup(getString("siteid") + "1001"), getString("fagentshortname") + "工具借出单提交待审", getString("FAGENTSHORTNAME") + "订单提交待审;订单号:" + getString("fsonum"));
  1074. //任务箱提醒
  1075. createAssignment(getHrIdByHrGroup(getString("siteid") + "1001"), "SAORDER0", getString("FAGENTSHORTNAME") + "工具借出单提交待审", getString("FAGENTSHORTNAME") + "工具借出单提交待审;单号:" + getString("fsonum"));
  1076. } else {
  1077. deleteAssignment();
  1078. setValue("fstatus", "新建", 11L);
  1079. setValue("submitby", "", 11L);
  1080. setValue("submitdate", "", 11L);
  1081. setValue("fnotes", getString("fnotes") + "----订单退回", 11L);
  1082. }
  1083. }
  1084. /**
  1085. * 交期回复验证
  1086. */
  1087. public void needdatereplyvalidate(boolean isneeddatereply) throws P2Exception {
  1088. if (isneeddatereply) {
  1089. if (!"提交".equals(getString("fstatus"))) {
  1090. throw new P2AppException("", "非提交状态下不可进行交期回复");
  1091. }
  1092. //订单表体,所有商品信息,包括辅材配件
  1093. if (issaleday()) {
  1094. PaoSetRemote saorderdetail = getPaoSet("saorderdetail");
  1095. int i = 0;
  1096. PaoRemote pao = null;
  1097. while ((pao = saorderdetail.getPao(i)) != null) {
  1098. if (pao.getPaoSet("titem").getPao(0).getBoolean("fissaleday")) {
  1099. if ("".equals(pao.getString("fplandate"))) {
  1100. throw new P2AppException("", "行" + pao.getInt("frownum") + "商品" + pao.getString("fitemname") + "为交期管控,请填写回复交期!");
  1101. }
  1102. }
  1103. i++;
  1104. }
  1105. } else {
  1106. throw new P2AppException("", "不存在交期管控的商品,无需进行交期回复,可直接进行订单审核");
  1107. }
  1108. } else {
  1109. if (!"交期待确认".equals(getString("fstatus"))) {
  1110. throw new P2AppException("", "非交期待确认状态下不可撤销交期回复");
  1111. }
  1112. }
  1113. }
  1114. /**
  1115. * 交期回复
  1116. */
  1117. public void needdatereply(boolean isneeddatereply) throws P2Exception {
  1118. if (isneeddatereply) {
  1119. setValue("needdatereplydate", getDate(), 11L);
  1120. setValue("needdatereplyby", getUserName(), 11L);
  1121. setValue("fstatus", "交期待确认", 11L);
  1122. {
  1123. //业务员邮件提醒
  1124. sendEmail(getMailByHrId(getString("fsaler")), getString("FAGENTSHORTNAME") + "交期待确认", getString("FAGENTSHORTNAME") + "《" + getString("ftype") + "》交期待确认;订单号:" + getString("fsonum"));
  1125. }
  1126. } else {
  1127. setValue("needdatereplydate", "", 11L);
  1128. setValue("needdatereplyby", "", 11L);
  1129. setValue("fstatus", "提交", 11L);
  1130. }
  1131. }
  1132. /**
  1133. * 交期确认验证
  1134. */
  1135. public void needdateconfirmvalidate(boolean isneeddateconfirm) throws P2Exception {
  1136. if (isneeddateconfirm) {
  1137. if (!"交期待确认".equals(getString("fstatus"))) {
  1138. throw new P2AppException("", "非交期待确认状态下不可进行交期确认");
  1139. }
  1140. } else {
  1141. if (!"交期确认".equals(getString("fstatus"))) {
  1142. throw new P2AppException("", "非交期确认状态下不可撤销交期确认");
  1143. }
  1144. }
  1145. }
  1146. /**
  1147. * 交期确认
  1148. */
  1149. public void needdateconfirm(boolean isneeddateconfirm) throws P2Exception {
  1150. if (isneeddateconfirm) {
  1151. setValue("needdateconfirmdate", getDate(), 11L);
  1152. setValue("needdateconfirmby", getUserName(), 11L);
  1153. setValue("fstatus", "交期确认", 11L);
  1154. //销管订单提交待审核邮件提醒
  1155. sendEmail(getMailByHrGroup(getString("siteid") + "1001"), getString("FAGENTSHORTNAME") + "《" + getString("ftype") + "》交期确认待审", getString("FAGENTSHORTNAME") + "《" + getString("ftype") + "》交期确认待审;订单号:" + getString("fsonum"));
  1156. //任务箱提醒
  1157. createAssignment(getHrIdByHrGroup(getString("siteid") + "1001"), "SAORDER0", getString("FAGENTSHORTNAME") + "《" + getString("ftype") + "》交期确认待审", getString("FAGENTSHORTNAME") + "《" + getString("ftype") + "》交期确认待审;订单号:" + getString("fsonum"));
  1158. } else {
  1159. setValue("needdateconfirmdate", "", 11L);
  1160. setValue("needdateconfirmby", "", 11L);
  1161. setValue("fstatus", "交期待确认", 11L);
  1162. }
  1163. }
  1164. /**
  1165. * 检查订单是否需要交期管控
  1166. *
  1167. * @return
  1168. */
  1169. public boolean issaleday() throws P2Exception {
  1170. PaoSetRemote saorderdetail = getPaoSet("saorderdetail");
  1171. int i = 0;
  1172. PaoRemote pao = null;
  1173. while ((pao = saorderdetail.getPao(i)) != null) {
  1174. if (pao.getPaoSet("titem").getPao(0).getBoolean("fissaleday")) {
  1175. return true;
  1176. }
  1177. i++;
  1178. }
  1179. return false;
  1180. }
  1181. /**
  1182. * 检查商品是否都上架
  1183. *
  1184. * @return
  1185. */
  1186. public String isnotonsaleitemnos() throws P2Exception {
  1187. String fitemnos = "";
  1188. PaoSetRemote saorderdetail = getPaoSet("saorderdetail");
  1189. int i = 0;
  1190. PaoRemote pao = null;
  1191. while ((pao = saorderdetail.getPao(i)) != null) {
  1192. if (!pao.getPaoSet("titem").getPao(0).getBoolean("fisonsale")) {
  1193. if (fitemnos.equals("")) {
  1194. fitemnos = pao.getString("fitemno");
  1195. } else {
  1196. fitemnos = fitemnos + "、" + pao.getString("fitemno");
  1197. }
  1198. }
  1199. i++;
  1200. }
  1201. return fitemnos;
  1202. }
  1203. /**
  1204. * @param fischeck 审核、反审核
  1205. * @param fisman 是否人工审核
  1206. * @throws P2Exception
  1207. */
  1208. public void checkvalidate(boolean fischeck, boolean fisman)
  1209. throws P2Exception {
  1210. if (fischeck) {
  1211. DBConnect dbConnect = new DBConnect();
  1212. Rows rows = dbConnect.runSqlQuery("select fitemno,fitemname from saorderdetail where fparentid='" + getUniqueIDValue() + "' and fplandate<getdate()");
  1213. for (Row row : rows) {
  1214. throw new P2AppException("", "商品" + row.getString("fitemno") + row.getString("fitemname from") + "回复交期已过期,不可审核!");
  1215. }
  1216. if ("NB".equals(getString("siteid")) && "".equals(getString("fsaletype"))) {
  1217. throw new P2AppException("", "销售分类不可为空!");
  1218. }
  1219. if ("TZ".equals(getString("siteid")) && "".equals(getString("freviewtype"))) {
  1220. throw new P2AppException("", "订单评审类型不可为空!");
  1221. }
  1222. }
  1223. if ("工具借出单".equals(getString("ftype"))) {
  1224. checkvalidate_tools(fischeck);
  1225. return;
  1226. }
  1227. //系统参数表
  1228. PaoRemote sysVars = getSysVars();
  1229. //订单表体,所有商品信息,包括辅材配件
  1230. PaoSetRemote saorderdetail = getPaoSet("saorderdetail");
  1231. if (fischeck) {
  1232. if (!"提交".equals(getString("fstatus")) && !"交期确认".equals(getString("fstatus"))) {
  1233. throw new P2AppException("", "非提交或交期确认状态不可审核");
  1234. }
  1235. /*如果是审核时进行扣款,且时人工审核的则判断账户余额不足直接抛异常,如果是自动的,则自动任务会自动跳过*/
  1236. String fupdatebaltime = getSysVars().getString("fupdatebaltime");
  1237. String fupdatfreeztime = getSysVars().getString("fupdatfreeztime");
  1238. if (fisman && ("订单审核".equals(fupdatebaltime) || "订单审核".equals(fupdatfreeztime))) {
  1239. double ftotalamount = getPaoSet("saorderdetail").sum("famount");
  1240. double fcanuseamount = getPaoSet("taccbal_view").getPao(0).getDouble("fcanuseamount");
  1241. double freezamount = 0;
  1242. if ("订单审核".equals(sysVars.getString("fdeletefreeztime"))) {
  1243. PaoSetRemote taccbal_freez = getPaoSet("$taccbal_freez",
  1244. "taccbal_freez", "fsourceobject='" + getName()
  1245. + "' and fsourceid='" + getUniqueIDValue() + "'");
  1246. if (!taccbal_freez.isEmpty()) {
  1247. /**
  1248. * 订单审核是需要解冻,需查出原订单冻结金额。防止账户余额不足引起审核失败
  1249. */
  1250. freezamount = taccbal_freez.sum("famount");
  1251. }
  1252. }
  1253. //返利可用金额
  1254. double fuseflamount = saorderdetail.sum("fflamount");
  1255. if (Math.add(Math.add(fcanuseamount, freezamount), fuseflamount) < ftotalamount) {
  1256. throw new P2AppException("", "账户可用余额不足");
  1257. }
  1258. }
  1259. if ("HY".equals(getString("siteid")) || "TZ".equals(getString("siteid"))) {
  1260. String fsaletype[] = {"标准订单", "促销订单", "工具订单", "特殊订单", "项目订单"};
  1261. if (Arrays.asList(fsaletype).contains(getString("ftype"))) {
  1262. if ("".equals(getString("fsaletype"))) {
  1263. throw new P2AppException("", "请选择销售分类!");
  1264. }
  1265. }
  1266. }
  1267. if ("项目订单".equals(getString("ftype")) && !"审核".equals(getPaoSet("TPROJECT").getPao(0).getString("fstatus"))) {
  1268. throw new P2AppException("", "项目不在审核状态不可审核");
  1269. }
  1270. } else {
  1271. if (!"审核".equals(getString("fstatus"))) {
  1272. throw new P2AppException("", "非审核状态不可反审核");
  1273. }
  1274. if (saorderdetail.sum("fqty") != saorderdetail.sum("fundeliqty")) {
  1275. throw new P2AppException("", "已经发货,不可反审核");
  1276. }
  1277. if (!getPaoSet("icstockbilldetail").isEmpty()) {
  1278. throw new P2AppException("", "存在销售出库单,不可反审核");
  1279. }
  1280. if (!getPaoSet("sainvoice").isEmpty()) {
  1281. throw new P2AppException("", "存在发货单,不可反审核");
  1282. }
  1283. if (!getPaoSet("saorderdetail_close").isEmpty()) {
  1284. throw new P2AppException("", "已存在关闭的行,不可反审核");
  1285. }
  1286. if (saorderdetail.sum("fwriteoffamount") != 0) {
  1287. throw new P2AppException("", "核销金额不为0,不可反审核");
  1288. }
  1289. if (saorderdetail.sum("finvoiceamount") != 0) {
  1290. throw new P2AppException("", "开票金额不为0,不可反审核");
  1291. }
  1292. }
  1293. }
  1294. /**
  1295. * 工具借出单审核
  1296. *
  1297. * @param fischeck 审核、反审核
  1298. * @throws P2Exception
  1299. */
  1300. public void checkvalidate_tools(boolean fischeck)
  1301. throws P2Exception {
  1302. //订单表体,所有商品信息,包括辅材配件
  1303. PaoSetRemote saorderdetail = getPaoSet("saorderdetail");
  1304. if (fischeck) {
  1305. if (issaleday()) {
  1306. if ((!"交期确认".equals(getString("fstatus")) && !"提交".equals(getString("fstatus"))) && !getBoolean("createflag")) {
  1307. throw new P2AppException("", "存在交期管控商品,非提交或交期确认状态不可审核");
  1308. }
  1309. if (!"新建".equals(getString("fstatus")) && !"提交".equals(getString("fstatus")) && !"交期确认".equals(getString("fstatus")) && getBoolean("createflag")) {
  1310. throw new P2AppException("", "非新建状态不可审核");
  1311. }
  1312. } else {
  1313. if (!"提交".equals(getString("fstatus")) && !getBoolean("createflag")) {
  1314. throw new P2AppException("", "非提交状态不可审核");
  1315. }
  1316. if (!"新建".equals(getString("fstatus")) && !"提交".equals(getString("fstatus")) && getBoolean("createflag")) {
  1317. throw new P2AppException("", "非新建状态不可审核");
  1318. }
  1319. }
  1320. } else {
  1321. if (!"审核".equals(getString("fstatus"))) {
  1322. throw new P2AppException("", "非审核状态不可反审核");
  1323. }
  1324. if (saorderdetail.sum("fqty") != saorderdetail.sum("fundeliqty")) {
  1325. throw new P2AppException("", "已经发货,不可反审核");
  1326. }
  1327. if (!getPaoSet("icstockbilldetail").isEmpty()) {
  1328. throw new P2AppException("", "存在销售出库单,不可反审核");
  1329. }
  1330. if (!getPaoSet("sainvoice").isEmpty()) {
  1331. throw new P2AppException("", "存在发货单,不可反审核");
  1332. }
  1333. if (!getPaoSet("saorderdetail_close").isEmpty()) {
  1334. throw new P2AppException("", "已存在关闭的行,不可反审核");
  1335. }
  1336. if (saorderdetail.sum("fwriteoffamount") != 0) {
  1337. throw new P2AppException("", "核销金额不为0,不可反审核");
  1338. }
  1339. if (saorderdetail.sum("finvoiceamount") != 0) {
  1340. throw new P2AppException("", "开票金额不为0,不可反审核");
  1341. }
  1342. }
  1343. }
  1344. @Override
  1345. public void check(boolean fischeck) throws P2Exception {
  1346. if ("工具借出单".equals(getString("ftype"))) {
  1347. check_tools(fischeck);
  1348. return;
  1349. }
  1350. if (fischeck && issaleday()) {
  1351. PaoSetRemote saorderdetailSet = getPaoSet("saorderdetail");
  1352. int i = 0;
  1353. while (saorderdetailSet.getPao(i) != null) {
  1354. if ("".equals(saorderdetailSet.getPao(i).getString("fplandate"))) {
  1355. saorderdetailSet.getPao(i).setValue("fplandate", saorderdetailSet.getPao(i).getString("fneeddate"), 11L);
  1356. }
  1357. i++;
  1358. }
  1359. }
  1360. /**
  1361. * 订单指定账户
  1362. */
  1363. taccbal taccbal = null;
  1364. PaoSetRemote taccbalSet = getPaoSet("taccbal");
  1365. if (taccbalSet.isEmpty()) {
  1366. taccbal = (taccbal) taccbalSet.addAtEnd();
  1367. taccbal.setfields(getString("fpayagentnum"), getString("faccclsnum"));
  1368. } else {
  1369. taccbal = (taccbal) taccbalSet.getPao(0);
  1370. }
  1371. /**
  1372. * 订单指定账户
  1373. */
  1374. taccbal taccbal_fl = null;
  1375. PaoSetRemote taccbalSet_fl = getPaoSet("taccbal_fl");
  1376. if (taccbalSet_fl.isEmpty()) {
  1377. taccbal_fl = (taccbal) taccbalSet_fl.addAtEnd();
  1378. taccbal_fl.setfields(getString("fpayagentnum"), "02");
  1379. } else {
  1380. taccbal_fl = (taccbal) taccbalSet_fl.getPao(0);
  1381. }
  1382. /**
  1383. * 订单指定账户
  1384. */
  1385. taccbal taccbal_wl = null;
  1386. PaoSetRemote taccbalSet_wl = getPaoSet("taccbal_wl");
  1387. if (taccbalSet_wl.isEmpty()) {
  1388. taccbal_wl = (taccbal) taccbalSet_wl.addAtEnd();
  1389. taccbal_wl.setfields(getString("fpayagentnum"), "03");
  1390. } else {
  1391. taccbal_wl = (taccbal) taccbalSet_wl.getPao(0);
  1392. }
  1393. //系统参数表
  1394. PaoRemote sysVars = getSysVars();
  1395. //总仓库存可销售量扣减时机,
  1396. String fupdateavlqtytime = sysVars.getString("fupdateavlqtytime");
  1397. //订单账户管控是否扣款
  1398. String fupdatebaltime = sysVars.getString("fupdatebaltime");
  1399. //订单账户管控是否冻结金额
  1400. String fupdatfreeztime = sysVars.getString("fupdatfreeztime");
  1401. //订单账户管控是否解冻金额
  1402. String fdeletefreeztime = sysVars.getString("fdeletefreeztime");
  1403. //订单表体,包括辅材赠品
  1404. PaoSetRemote saorderdetail = getPaoSet("saorderdetail");
  1405. // 订单总额
  1406. double ftotalamount = saorderdetail.sum("famount");
  1407. // 订单指定账户扣款总额
  1408. double ftotalrateamount = saorderdetail.sum("frateamount");
  1409. // 订单使用返利总金额
  1410. double fuseflamount = saorderdetail.sum("fflamount");
  1411. if (fischeck) {
  1412. deleteAssignment();
  1413. /*资金账户计算*/
  1414. if ("订单审核".equals(fupdatebaltime)) {
  1415. /*如果系统参数设为为提交时扣款,则提交时需要生成支出凭证*/
  1416. /*生成支出凭证*/
  1417. String fnotes = "该单由销售订单" + getString("fsonum") + "审核时自动生成";
  1418. /**
  1419. * 生成返利支出凭证
  1420. */
  1421. createFicashbill_fl_Pay(taccbal_fl, fuseflamount, fnotes);
  1422. /**
  1423. * 生成账户支出凭证
  1424. */
  1425. createFicashbill_Pay(taccbal, ftotalrateamount, fnotes, true);
  1426. } else if ("订单审核".equals(fupdatfreeztime)) {
  1427. /*如果系统参数设为为提交时冻结金额,则提交时需要进行账户金额冻结*/
  1428. /*账户余额冻结*/
  1429. String fnotes = "由销售订单" + getString("fsonum") + "审核时冻结";
  1430. updateTaccbal_freez(taccbal, ftotalrateamount, fnotes);
  1431. }
  1432. if ("订单审核".equals(fdeletefreeztime)) {
  1433. deleteTaccbal_freez();
  1434. }
  1435. //总仓库存可销售量扣减
  1436. if ("订单审核".equals(fupdateavlqtytime)) {
  1437. // updateIcinvbal_total(false, false);
  1438. }
  1439. /**
  1440. * 是否年度首单返利
  1441. */
  1442. boolean fisyearfirstorder_fl = fisyearfirstorder_fl(ftotalrateamount);
  1443. //年度首笔订单返利
  1444. PaoRemote tagent = getPaoSet("tagents").getPao(0);
  1445. if (fisyearfirstorder_fl) {
  1446. //年度首单返利比例
  1447. double fisfirstorderflrate = tagent.getDouble("fisfirstorderflrate");
  1448. createFicashbill_FL(taccbal_fl, "年度首单返利", "年度首单返利" + getString("fsonum"), ftotalrateamount * fisfirstorderflrate);
  1449. }
  1450. /**
  1451. * 是否年度首单物料补贴
  1452. */
  1453. boolean fisyearfirstorder_wl = fisyearfirstorder_wl(ftotalrateamount);
  1454. if (fisyearfirstorder_wl) {
  1455. //年度首单物料补贴比例
  1456. double fisfirstorderrate = tagent.getDouble("fisfirstorderrate");
  1457. createFicashbill_WL(taccbal_wl, "年度首单补贴", "年度首单物料补贴" + getString("fsonum"), ftotalrateamount * fisfirstorderrate);
  1458. }
  1459. //满赠计算。
  1460. if ("订单审核".equals(fupdatebaltime)) {
  1461. caculateMZ(taccbal_wl, taccbal_fl, getDate());
  1462. } else if ("订单提交".equals(fupdatebaltime)) {
  1463. caculateMZ(taccbal_wl, taccbal_fl, getDate("submitdate"));
  1464. }
  1465. /******** 单据状态更新 ********/
  1466. setValue("fstatus", "审核", 11L);
  1467. setValue("checkby", getUserName(), 11L);
  1468. setValue("checkdate", getDateTime(), 11L);
  1469. // {
  1470. // //业务员邮件提醒
  1471. // // sendEmail(getMailByHrId(getString("fsaler")), getString("FAGENTSHORTNAME") + "订单已审核", getString("FAGENTSHORTNAME") + "订单已审核;订单号:" + getString("fsonum"));
  1472. // try {
  1473. // if ("NB".equals(getString("siteid"))) {
  1474. // //业务员邮件提醒
  1475. // sendReportByEmail(getMailByHrId(getString("fsaler")), "NB_SAORDERDETAIL", "where=saorder.saorderid=" + getUniqueIDValue(), getString("FAGENTSHORTNAME") + "订单已审核;订单号:" + getString("fsonum"));
  1476. // //根据领域不同发送邮件给对应的小组
  1477. // sendReportByEmail(getMailByHrGroup("NB1007", getString("fieldname")), "NB_SAORDERDETAIL", "where=saorder.saorderid=" + getUniqueIDValue(), getString("FAGENTSHORTNAME") + "订单已审核;订单号:" + getString("fsonum"));
  1478. // } else if ("TZ".equals(getString("siteid"))) {
  1479. // //业务员邮件提醒
  1480. // sendReportByEmail(getMailByHrId(getString("fsaler")), "TZ_SAORDERDETAIL", "where=saorder.saorderid=" + getUniqueIDValue(), getString("FAGENTSHORTNAME") + "订单已审核;订单号:" + getString("fsonum"));
  1481. // //根据领域不同发送邮件给对应的小组
  1482. // sendReportByEmail(getMailByHrGroup("TZ1007", getString("fieldname")), "TZ_SAORDERDETAIL", "where=saorder.saorderid=" + getUniqueIDValue(), getString("FAGENTSHORTNAME") + "订单已审核;订单号:" + getString("fsonum"));
  1483. // } else if ("HY".equals(getString("siteid"))) {
  1484. // //业务员邮件提醒
  1485. // sendReportByEmail(getMailByHrId(getString("fsaler")), "HY_SAORDERDETAIL", "where=saorder.saorderid=" + getUniqueIDValue(), getString("FAGENTSHORTNAME") + "订单已审核;订单号:" + getString("fsonum"));
  1486. // //根据领域不同发送邮件给对应的小组
  1487. // sendReportByEmail(getMailByHrGroup("HY1007", getString("fieldname")), "HY_SAORDERDETAIL", "where=saorder.saorderid=" + getUniqueIDValue(), getString("FAGENTSHORTNAME") + "订单已审核;订单号:" + getString("fsonum"));
  1488. // }
  1489. // } catch (Exception e) {
  1490. // e.printStackTrace();
  1491. // }
  1492. // }
  1493. } else {
  1494. /*资金账户计算*/
  1495. if ("订单审核".equals(fupdatebaltime)) {
  1496. /*如果系统参数设为为订单审核时扣款,则反审核时需要生成负数的支出凭证*/
  1497. /*生成支出凭证*/
  1498. String fnotes = "该单由销售订单" + getString("fsonum") + "反审核时自动生成";
  1499. createFicashbill_fl_Pay(taccbal_fl, -fuseflamount, fnotes);
  1500. createFicashbill_Pay(taccbal, -ftotalrateamount, fnotes, true);
  1501. } else if ("订单审核".equals(fupdatfreeztime)) {
  1502. /*如果系统参数设为为审核时冻结账户余额,则反审核时需要进行账户余额的解冻*/
  1503. deleteTaccbal_freez();
  1504. }
  1505. if ("订单审核".equals(fdeletefreeztime)) {
  1506. /*如果系统参数设为订单审核时解冻账户余额,则反审核时需要进行账户余额的冻结*/
  1507. String fnotes = "由销售订单" + getString("fsonum") + "审核时冻结";
  1508. updateTaccbal_freez(taccbal, ftotalrateamount, fnotes);
  1509. }
  1510. //总仓库存可销售量扣减
  1511. if ("订单审核".equals(fupdateavlqtytime)) {
  1512. //updateIcinvbal_total(true, false);
  1513. }
  1514. /******** 单据状态更新 ********/
  1515. setValue("fstatus", "提交", 11L);
  1516. setValue("checkby", "", 11L);
  1517. setValue("checkdate", "", 11L);
  1518. }
  1519. }
  1520. public void sendMail() throws P2Exception {
  1521. {
  1522. //业务员邮件提醒
  1523. // sendEmail(getMailByHrId(getString("fsaler")), getString("FAGENTSHORTNAME") + "订单已审核", getString("FAGENTSHORTNAME") + "订单已审核;订单号:" + getString("fsonum"));
  1524. try {
  1525. if ("NB".equals(getString("siteid"))) {
  1526. //业务员邮件提醒
  1527. sendReportByEmail(getMailByHrId(getString("fsaler")), "NB_SAORDERDETAIL", "where=saorder.saorderid=" + getUniqueIDValue(), getString("FAGENTSHORTNAME") + "《" + getString("ftype") + "》已审核;订单号:" + getString("fsonum") + ";订单备注:" + getString("fnotes"));
  1528. //根据领域不同发送邮件给对应的小组
  1529. sendReportByEmail(getMailByHrGroup("NB1007", getString("fieldname")), "NB_SAORDERDETAIL", "where=saorder.saorderid=" + getUniqueIDValue(), getString("FAGENTSHORTNAME") + "《" + getString("ftype") + "》已审核;订单号:" + getString("fsonum") + ";订单备注:" + getString("fnotes"));
  1530. sendReportByEmail(getMailByHrGroup_Brand("NB1008", getString("fbrand")), "NB_SAORDERDETAIL", "where=saorder.saorderid=" + getUniqueIDValue(), getString("FAGENTSHORTNAME") + "《" + getString("ftype") + "》已审核;订单号:" + getString("fsonum") + ";订单备注:" + getString("fnotes"));
  1531. } else if ("TZ".equals(getString("siteid"))) {
  1532. //业务员邮件提醒
  1533. sendReportByEmail(getMailByHrId(getString("fsaler")), "TZ_SAORDERDETAIL", "where=saorder.saorderid=" + getUniqueIDValue(), getString("FAGENTSHORTNAME") + "《" + getString("ftype") + "》已审核;订单号:" + getString("fsonum") + ";订单备注:" + getString("fnotes"));
  1534. //根据领域不同发送邮件给对应的小组
  1535. sendReportByEmail(getMailByHrGroup("TZ1007", getString("fieldname")), "TZ_SAORDERDETAIL", "where=saorder.saorderid=" + getUniqueIDValue(), getString("FAGENTSHORTNAME") + "《" + getString("ftype") + "》已审核;订单号:" + getString("fsonum") + ";订单备注:" + getString("fnotes"));
  1536. sendReportByEmail(getMailByHrGroup_Brand("TZ1008", getString("fbrand")), "TZ_SAORDERDETAIL", "where=saorder.saorderid=" + getUniqueIDValue(), getString("FAGENTSHORTNAME") + "《" + getString("ftype") + "》已审核;订单号:" + getString("fsonum") + ";订单备注:" + getString("fnotes"));
  1537. } else if ("HY".equals(getString("siteid"))) {
  1538. //业务员邮件提醒
  1539. sendReportByEmail(getMailByHrId(getString("fsaler")), "HY_SAORDERDETAIL", "where=saorder.saorderid=" + getUniqueIDValue(), getString("FAGENTSHORTNAME") + "《" + getString("ftype") + "》已审核;订单号:" + getString("fsonum") + ";订单备注:" + getString("fnotes"));
  1540. //根据领域不同发送邮件给对应的小组
  1541. sendReportByEmail(getMailByHrGroup("HY1007", getString("fieldname")), "HY_SAORDERDETAIL", "where=saorder.saorderid=" + getUniqueIDValue(), getString("FAGENTSHORTNAME") + "《" + getString("ftype") + "》已审核;订单号:" + getString("fsonum") + ";订单备注:" + getString("fnotes"));
  1542. sendReportByEmail(getMailByHrGroup_Brand("TZ1008", getString("fbrand")), "HY_SAORDERDETAIL", "where=saorder.saorderid=" + getUniqueIDValue(), getString("FAGENTSHORTNAME") + "《" + getString("ftype") + "》已审核;订单号:" + getString("fsonum") + ";订单备注:" + getString("fnotes"));
  1543. }
  1544. } catch (Exception e) {
  1545. e.printStackTrace();
  1546. }
  1547. }
  1548. }
  1549. /**
  1550. * 工具借出单审核
  1551. *
  1552. * @param fischeck
  1553. * @throws P2Exception
  1554. */
  1555. public void check_tools(boolean fischeck) throws P2Exception {
  1556. if (fischeck && issaleday()) {
  1557. PaoSetRemote saorderdetailSet = getPaoSet("saorderdetail");
  1558. int i = 0;
  1559. while (saorderdetailSet.getPao(i) != null) {
  1560. if ("".equals(saorderdetailSet.getPao(i).getString("fplandate"))) {
  1561. saorderdetailSet.getPao(i).setValue("fplandate", saorderdetailSet.getPao(i).getString("fneeddate"), 11L);
  1562. }
  1563. i++;
  1564. }
  1565. }
  1566. //订单表体,包括辅材赠品
  1567. PaoSetRemote saorderdetail = getPaoSet("saorderdetail");
  1568. double ftotalrateamount = saorderdetail.sum("frateamount");
  1569. if (ftotalrateamount != 0) {
  1570. taccbal taccbal = null;
  1571. PaoSetRemote taccbalSet = getPaoSet("taccbal");
  1572. if (taccbalSet.isEmpty()) {
  1573. taccbal = (taccbal) taccbalSet.addAtEnd();
  1574. taccbal.setfields(getString("fpayagentnum"), getString("faccclsnum"));
  1575. } else {
  1576. taccbal = (taccbal) taccbalSet.getPao(0);
  1577. }
  1578. Date date = getDateTime();
  1579. ftotalrateamount = fischeck ? ftotalrateamount : -ftotalrateamount;
  1580. PaoSetRemote ficashbillset = getPaoSet("$ficashbill_fl", "ficashbill");
  1581. ficashbill ficashbill = (ficashbill) ficashbillset.addAtEnd();
  1582. ficashbill.setValues("RECBILL", getString("fpayagentnum"), "收入",
  1583. getString("faccclsnum"), date, ftotalrateamount, "工具借出申请单审核押金",
  1584. getName(), getUniqueIDValue(), "新建", date, getUserName(), date);
  1585. ficashbill.setValue("ftypemx", "货款", 11L);
  1586. ficashbill.setValue("ftypemxdetail", "工具押金", 11L);
  1587. ficashbill.check(true, taccbal);
  1588. //Long ficashbillid = fischeck ? ficashbill.getUniqueIDValue() : 0;
  1589. //setValue("ficashbillid", ficashbillid, 11L);
  1590. // int i = 0;
  1591. // while (saorderdetail.getPao(i) != null) {
  1592. // saorderdetail.getPao(i).setValue("ficashbillid", ficashbillid, 11L);
  1593. // i++;
  1594. // }
  1595. }
  1596. if (fischeck) {
  1597. deleteAssignment();
  1598. /******** 单据状态更新 ********/
  1599. setValue("fstatus", "审核", 11L);
  1600. setValue("checkby", getUserName(), 11L);
  1601. setValue("checkdate", getDateTime(), 11L);
  1602. } else {
  1603. /******** 单据状态更新 ********/
  1604. setValue("fstatus", "提交", 11L);
  1605. setValue("checkby", "", 11L);
  1606. setValue("checkdate", "", 11L);
  1607. }
  1608. }
  1609. /**
  1610. * 是否年度首单计算
  1611. *
  1612. * @param ftotalamount
  1613. * @return
  1614. * @throws P2Exception
  1615. */
  1616. public boolean fisyearfirstorder_fl(double ftotalamount) throws P2Exception {
  1617. if (!"标准订单".equals(getString("ftype"))) {
  1618. return getBoolean("fisyearfirstorder_fl");
  1619. }
  1620. PaoRemote tagent = getPaoSet("tagents").getPao(0);
  1621. //签约时间
  1622. boolean fisfirstorderfl = tagent.getBoolean("fisfirstorderfl");
  1623. if (fisfirstorderfl && ftotalamount >= tagent.getDouble("fisfirstorderflamount") && tagent.getDate("fsigningtime") != null) {
  1624. //当前签约开始时间
  1625. Calendar fsigningtime_beg = Calendar.getInstance();
  1626. fsigningtime_beg.setTime(tagent.getDate("fsigningtime"));
  1627. fsigningtime_beg.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR));
  1628. //当前签约结束时间
  1629. Calendar fsigningtime_end = Calendar.getInstance();
  1630. fsigningtime_end.setTime(tagent.getDate("fsigningtime"));
  1631. fsigningtime_end.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR) + 1);
  1632. PaoSetRemote paoset = getPaoSet("$$saorder", "saorder");
  1633. paoset.setWhere("fisyearfirstorder_fl=1 and fagentnum='" + getString("fagentnum") + "' and ftype='标准订单' and convert(varchar(10),checkdate,120)>='" + getDate_Str(fsigningtime_beg.getTime()) + "' and convert(varchar(10),checkdate,120)<'" + getDate_Str(fsigningtime_end.getTime()) + "'");
  1634. paoset.reset();
  1635. if (paoset.isEmpty()) {
  1636. setValue("fisyearfirstorder_fl", true, 11L);
  1637. setValue("fisfl", true, 11L);
  1638. }
  1639. }
  1640. return getBoolean("fisyearfirstorder_fl");
  1641. }
  1642. /**
  1643. * 是否年度首单计算
  1644. *
  1645. * @param ftotalamount
  1646. * @return
  1647. * @throws P2Exception
  1648. */
  1649. public boolean fisyearfirstorder_wl(double ftotalamount) throws P2Exception {
  1650. if (!"标准订单".equals(getString("ftype"))) {
  1651. return getBoolean("fisyearfirstorder_wl");
  1652. }
  1653. PaoRemote tagent = getPaoSet("tagents").getPao(0);
  1654. //签约时间
  1655. boolean fisfirstorder = tagent.getBoolean("fisfirstorder");
  1656. if (fisfirstorder && ftotalamount >= tagent.getDouble("fisfirstorderamount")) {
  1657. //当前签约开始时间
  1658. Calendar fsigningtime_beg = Calendar.getInstance();
  1659. fsigningtime_beg.setTime(tagent.getDate("fsigningtime"));
  1660. fsigningtime_beg.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR));
  1661. //当前签约结束时间
  1662. Calendar fsigningtime_end = Calendar.getInstance();
  1663. fsigningtime_end.setTime(tagent.getDate("fsigningtime"));
  1664. fsigningtime_end.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR) + 1);
  1665. PaoSetRemote paoset = getPaoSet("$$saorder", "saorder");
  1666. paoset.setWhere("fisyearfirstorder_wl=1 and fagentnum='" + getString("fagentnum") + "' and ftype='标准订单' and convert(varchar(10),checkdate,120)>='" + getDate_Str(fsigningtime_beg.getTime()) + "' and convert(varchar(10),checkdate,120)<'" + getDate_Str(fsigningtime_end.getTime()) + "'");
  1667. paoset.reset();
  1668. if (paoset.isEmpty()) {
  1669. setValue("fisyearfirstorder_wl", true, 11L);
  1670. }
  1671. }
  1672. return getBoolean("fisyearfirstorder_wl");
  1673. }
  1674. @Override
  1675. public int closevalidate(boolean isclose) throws P2Exception {
  1676. //系统参数表
  1677. PaoRemote sysVars = getSysVars();
  1678. //是否启用发货单
  1679. boolean fissainvoice = sysVars.getBoolean("fissainvoice");
  1680. //订单表体
  1681. PaoSetRemote saorderdetail = getPaoSet("saorderdetail_view");
  1682. Vector<PaoRemote> v = saorderdetail.getSelection();
  1683. if (isclose) {
  1684. if (!"审核".equals(getString("fstatus"))) {
  1685. throw new P2AppException("", "非审核状态下不可关闭");
  1686. }
  1687. if (v.size() == 0) {
  1688. throw new P2AppException("", "请勾选需要关闭的行再进行此操作");
  1689. }
  1690. Iterator<PaoRemote> it = v.iterator();
  1691. while (it.hasNext()) {
  1692. PaoRemote closepao = it.next();
  1693. int frownum = closepao.getInt("frownum");
  1694. if (closepao.getBoolean("fisclose")) {
  1695. throw new P2AppException("", "行" + frownum
  1696. + "已经关闭,不可重复关闭");
  1697. }
  1698. if (closepao.getDouble("fundeliqty") == 0) {
  1699. throw new P2AppException("", "行" + frownum
  1700. + "已发货完毕,不可关闭");
  1701. }
  1702. //校验是否存在未关闭的发货单
  1703. if (fissainvoice) {
  1704. PaoSetRemote sainvoicedetail = closepao.getPaoSet("sainvoicedetail");
  1705. int i = 0;
  1706. while (sainvoicedetail.getPao(i) != null) {
  1707. PaoSetRemote sainvoice = sainvoicedetail.getPao(i)
  1708. .getPaoSet("sainvoice");
  1709. if (!"关闭".equals(sainvoice.getPao(0).getString("fstatus"))) {
  1710. throw new P2AppException("", "行" + frownum
  1711. + "存在未关闭的发货单,不可关闭");
  1712. }
  1713. i++;
  1714. }
  1715. }
  1716. //校验是否存在新建的销售出库单
  1717. PaoSetRemote icstockbilldetail = closepao.getPaoSet("icstockbilldetail");
  1718. int i = 0;
  1719. while (icstockbilldetail.getPao(i) != null) {
  1720. PaoSetRemote icstockbill = icstockbilldetail.getPao(i)
  1721. .getPaoSet("icstockbill");
  1722. if ("新建".equals(icstockbill.getPao(0).getString("fstatus"))) {
  1723. throw new P2AppException("", "行" + frownum
  1724. + "存在新建的销售出库单,不可关闭");
  1725. }
  1726. i++;
  1727. }
  1728. }
  1729. } else {
  1730. if (v.size() == 0) {
  1731. throw new P2AppException("", "请购选行再进行取消关闭操作");
  1732. }
  1733. Iterator<PaoRemote> it = v.iterator();
  1734. while (it.hasNext()) {
  1735. PaoRemote unclosepao = it.next();
  1736. int frownum = unclosepao.getInt("frownum");
  1737. if (!unclosepao.getBoolean("fisclose")) {
  1738. throw new P2AppException("", "行" + frownum
  1739. + "未关闭,不可取消关闭");
  1740. }
  1741. if (unclosepao.getDouble("FUNDELIQTY") == 0) {
  1742. throw new P2AppException("", "行" + frownum
  1743. + "已发货完毕,不可取消关闭");
  1744. }
  1745. }
  1746. }
  1747. return 1;
  1748. }
  1749. @Override
  1750. public int close(boolean isclose) throws P2Exception {
  1751. /**
  1752. * 订单指定账户
  1753. */
  1754. taccbal taccbal = null;
  1755. PaoSetRemote taccbalSet = getPaoSet("taccbal");
  1756. if (taccbalSet.isEmpty()) {
  1757. taccbal = (taccbal) taccbalSet.addAtEnd();
  1758. taccbal.setfields(getString("fpayagentnum"), getString("faccclsnum"));
  1759. } else {
  1760. taccbal = (taccbal) taccbalSet.getPao(0);
  1761. }
  1762. /**
  1763. * 订单指定账户
  1764. */
  1765. taccbal taccbal_fl = null;
  1766. PaoSetRemote taccbalSet_fl = getPaoSet("taccbal_fl");
  1767. if (taccbalSet_fl.isEmpty()) {
  1768. taccbal_fl = (taccbal) taccbalSet_fl.addAtEnd();
  1769. taccbal_fl.setfields(getString("fpayagentnum"), "02");
  1770. } else {
  1771. taccbal_fl = (taccbal) taccbalSet_fl.getPao(0);
  1772. }
  1773. //系统参数表
  1774. PaoRemote sysVars = getSysVars();
  1775. //订单账户管控是否扣款
  1776. String fupdatebaltime = sysVars.getString("fupdatebaltime");
  1777. //订单账户管控是否冻结金额
  1778. String fupdatfreeztime = sysVars.getString("fupdatfreeztime");
  1779. //订单表体
  1780. PaoSetRemote saorderdetail = getPaoSet("saorderdetail_view");
  1781. // 订单指出总额
  1782. double ftotalrateamount = saorderdetail.sum("frateamount");
  1783. //当前时间
  1784. Date date = getDateTime();
  1785. // 退还金额
  1786. double returnamount = 0;
  1787. int i = 0;
  1788. PaoRemote detailpao = null;
  1789. while ((detailpao = saorderdetail.getPao(i)) != null) {
  1790. if (detailpao.isSelected()) {
  1791. // 订单金额
  1792. double frateamount = detailpao.getDouble("frateamount");
  1793. // 订单单价
  1794. // double fprice = detailpao.getDouble("fprice");
  1795. // 订单数量
  1796. double fqty = detailpao.getDouble("fqty");
  1797. // 订单未发货数量
  1798. double fundeliqty = detailpao.getDouble("fundeliqty");
  1799. /*计算订单关闭的退还金额*/
  1800. //已发货数量
  1801. double sendqty = Math.sub(fqty, fundeliqty);
  1802. //订单单价
  1803. double frateprice = detailpao.getDouble("frateprice");
  1804. //已发货金额
  1805. double sendamount = Math.mul(sendqty, frateprice, 2);
  1806. //未发货金额
  1807. double unsendamount = Math.sub(frateamount, sendamount);
  1808. //退款金额
  1809. returnamount = returnamount + unsendamount;
  1810. if (isclose) {
  1811. detailpao.setValue("fisclose", true, 11L);
  1812. detailpao.setValue("fclosedate", date, 11L);
  1813. detailpao.setValue("fclosenum", getUserName(), 11L);
  1814. detailpao.setValue("fclosereason", getString("closereason"), 11L);
  1815. } else {
  1816. detailpao.setValue("fisclose", false, 11L);
  1817. detailpao.setValue("fclosedate", "", 11L);
  1818. detailpao.setValue("fclosenum", "", 11L);
  1819. detailpao.setValue("fclosereason", "", 11L);
  1820. }
  1821. //更新促销方案商品已订购数量
  1822. if ("促销订单".equals(getString("ftype"))) {
  1823. PaoSetRemote tpromproducts = detailpao.getPaoSet("tpromproducts");
  1824. if (!tpromproducts.isEmpty()) {
  1825. PaoRemote tpromproduct = tpromproducts.getPao(0);
  1826. if (tpromproduct.getBoolean("fislimit")) {
  1827. //可销售数量
  1828. double fsurplusqty = tpromproduct.getDouble("fsurplusqty");
  1829. //更新可销售量
  1830. tpromproduct.setValue("fsurplusqty", Math.add(fsurplusqty, fundeliqty), 11L);
  1831. }
  1832. }
  1833. }
  1834. }
  1835. i++;
  1836. }
  1837. /*订单关闭账户余额更新*/
  1838. if ("订单提交".equals(fupdatebaltime) || "订单审核".equals(fupdatebaltime)) {
  1839. //如果是账户余额扣减
  1840. if (isclose) {
  1841. /*生成负数支出凭证*/
  1842. StringBuffer fnotes = new StringBuffer();
  1843. fnotes.append("该单由销售订单").append(getString("fsonum")).append("行关闭时自动生成;关闭原因:").append(getString("closereason"));
  1844. //PaoSetRemote ficashbillset = getPaoSet("$ficashbill_fl", "ficashbill", "ftypemx='返利金'and fnotes not like'%行关闭%' and ftype='支出' and fstatus='审核' and fsourceobject='SAORDER' and fsourceid=" + getUniqueIDValue());
  1845. //返利金总额
  1846. // double fuseflamount = getDouble("fuseflamount");
  1847. // fuseflamount = fuseflamount * (returnamount / ftotalrateamount);
  1848. //createFicashbill_fl_Pay(taccbal_fl, -famount_fl, fnotes.toString());
  1849. createFicashbill_Pay(taccbal, -returnamount, fnotes.toString(), true);
  1850. } else {
  1851. /*生成支出凭证*/
  1852. StringBuffer fnotes = new StringBuffer();
  1853. fnotes.append("该单由销售订单").append(getString("fsonum")).append("行反关闭时自动生成;反关闭原因:").append(getString("closereason"));
  1854. PaoSetRemote ficashbillset = getPaoSet("$ficashbill_fl", "ficashbill", "ftypemx='返利金'and fnotes not like'%行关闭%' and ftype='支出' and fstatus='审核' and fsourceobject='SAORDER' and fsourceid=" + getUniqueIDValue());
  1855. //返利金总额
  1856. //double famount_fl = ficashbillset.sum("famount");
  1857. //famount_fl = famount_fl * (returnamount / ftotalrateamount);
  1858. //createFicashbill_fl_Pay(taccbal_fl, famount_fl, fnotes.toString());
  1859. createFicashbill_Pay(taccbal, returnamount, fnotes.toString(), true);
  1860. }
  1861. } else if ("订单提交".equals(fupdatfreeztime) || "订单审核".equals(fupdatfreeztime)) {
  1862. //如果是账户冻结金额
  1863. //冻结列表
  1864. PaoSetRemote taccbal_freez = getPaoSet("$TACCBAL_FREEZ",
  1865. "taccbal_freez", "fsourceobject='" + getName()
  1866. + "' and fsourceid='" + getUniqueIDValue() + "'");
  1867. if (!taccbal_freez.isEmpty()) {
  1868. PaoRemote freezPao = taccbal_freez.getPao(0);
  1869. // 冻结金额
  1870. double famount = freezPao.getDouble("famount");
  1871. freezPao.setValue("famount", Math.sub(famount, returnamount), 11L);
  1872. if (freezPao.getDouble("famount") <= 0.1) {
  1873. taccbal_freez.deleteAll();
  1874. }
  1875. }
  1876. }
  1877. /*增加总仓可销售量 */
  1878. //updateIcinvbal_total(isclose, true);
  1879. //更新促销方案经销商已订购金额
  1880. if ("促销订单".equals(getString("ftype"))) {
  1881. PaoSetRemote tpromagents = getPaoSet("tpromagents");
  1882. if (!tpromagents.isEmpty()) {
  1883. PaoRemote tpromagent = tpromagents.getPao(0);
  1884. if (tpromagent.getBoolean("fislimit") && "金额".equals(tpromagent.getString("flimittype"))) {
  1885. //已购量
  1886. double flimitvalue1 = tpromagent.getDouble("flimitvalue1");
  1887. //更新已购量
  1888. tpromagent.setValue("flimitvalue1", Math.sub(flimitvalue1, returnamount), 11L);
  1889. }
  1890. }
  1891. }
  1892. /* 注:所有行全部关闭后,表头单据状态将通过触发器自动进行更新*/
  1893. return 1;
  1894. }
  1895. /**
  1896. * 订单结案验证
  1897. *
  1898. * @throws P2Exception
  1899. */
  1900. public void closeordervalidate() throws P2Exception {
  1901. if (!"审核".equalsIgnoreCase(getString("fstatus"))) {
  1902. throw new P2AppException("", "非审核状态下不可进行结案操作");
  1903. }
  1904. if ("HY".equals(getString("siteid"))) {
  1905. throw new P2AppException("", "当前站点不可执行订单结案操作");
  1906. }
  1907. DBConnect dbConnect = new DBConnect("ORACLE_TZ");
  1908. Rows rows = dbConnect.runSqlQuery("select SO_LINE2,sum(QTY)as QTY from v_eso_shipped_qty where SO_NO2='" + getString("fsonum") + "' and ZT='" + (getString("siteid").equalsIgnoreCase("TZ") ? "IDCZJ10" : "IBPNB10") + "' group by SO_LINE2");
  1909. if (!"".equals(rows.errmsg)) {
  1910. throw new P2AppException("", "erp连接失败");
  1911. }
  1912. PaoSetRemote saorderdetail = getPaoSet("saorderdetail");
  1913. saorderdetail.setOrderBy("frownum");
  1914. saorderdetail.reset();
  1915. for (Row row : rows) {
  1916. int SO_LINE2 = row.getInteger("SO_LINE2");//erp订单行号
  1917. double QTY = row.getInteger("QTY");//erp已发货数量
  1918. int i = 0;
  1919. while (saorderdetail.getPao(i) != null) {
  1920. int frownum = saorderdetail.getPao(i).getInt("frownum");//订单行号
  1921. if (SO_LINE2 == frownum) {
  1922. saorderdetail.getPao(i).setValue("FERPDELIQTY", QTY, 11L);
  1923. break;
  1924. }
  1925. i++;
  1926. }
  1927. }
  1928. {
  1929. String str = "";
  1930. int i = 0;
  1931. while (saorderdetail.getPao(i) != null) {
  1932. int frownum = saorderdetail.getPao(i).getInt("frownum");//订单行号
  1933. double fsaininvoiceqty = Math.sub(saorderdetail.getPao(i).getInt("fqty"), saorderdetail.getPao(i).getInt("fundeliqty"));//订单已发货数量
  1934. double FERPDELIQTY = saorderdetail.getPao(i).getDouble("FERPDELIQTY");//erp已发货数量
  1935. if (fsaininvoiceqty != FERPDELIQTY) {
  1936. str = str + "行" + frownum + "商品发货数量" + fsaininvoiceqty + "与erp发货数量" + FERPDELIQTY + "不一致,请检查;";
  1937. }
  1938. i++;
  1939. }
  1940. if (!str.equalsIgnoreCase("")) {
  1941. throw new P2AppException("", str);
  1942. }
  1943. }
  1944. }
  1945. /**
  1946. * 订单结案
  1947. *
  1948. * @throws P2Exception
  1949. */
  1950. public PaoRemote closeorder() throws P2Exception {
  1951. PaoSetRemote saorderdetail = getPaoSet("saorderdetail");
  1952. int i = 0;
  1953. while (saorderdetail.getPao(i) != null) {
  1954. //double fundeliqty = saorderdetail.getPao(i).getInt("fundeliqty");//订单未发货数量
  1955. double fqty = saorderdetail.getPao(i).getInt("fqty");//订单数量
  1956. double FERPDELIQTY = saorderdetail.getPao(i).getDouble("FERPDELIQTY");//erp已发货数量
  1957. if (fqty > FERPDELIQTY) {
  1958. saorderdetail.getPao(i).setValue("fchangenotes", "订单结案变更", 11L);
  1959. saorderdetail.getPao(i).setValue("fchangeqty", FERPDELIQTY, 11L);
  1960. }
  1961. i++;
  1962. }
  1963. PaoRemote pao = changesaorder(saorderdetail);
  1964. PaoSetRemote sainvoiceSet = getPaoSet("SAINVOICE");
  1965. int n = 0;
  1966. while (sainvoiceSet.getPao(n) != null) {
  1967. if (sainvoiceSet.getPao(n).getString("fstatus").equals("新建")) {
  1968. sainvoiceSet.getPao(n).delete();
  1969. }
  1970. n++;
  1971. }
  1972. setValue("fstatus", "关闭", 11L);
  1973. setValue("closeby", getUserName(), 11L);
  1974. setValue("closedate", getDate(), 11L);
  1975. setValue("CLOSEREASON", "订单结案", 11L);
  1976. return pao;
  1977. }
  1978. /**
  1979. * 一键发货生成发货单或销售出库单验证
  1980. *
  1981. * @throws P2Exception
  1982. */
  1983. public void sendoutvalidate() throws P2Exception {
  1984. if (!"审核".equals(getString("fstatus"))) {
  1985. throw new P2AppException("", "非审核状态下不可发货");
  1986. }
  1987. PaoSetRemote saorderdetail = getPaoSet("saorderdetail_view");
  1988. if (saorderdetail.getSelection().isEmpty()) {
  1989. throw new P2AppException("", "请购选行再进行发货操作");
  1990. }
  1991. int i = 0;
  1992. while (saorderdetail.getPao(i) != null) {
  1993. if (saorderdetail.getPao(i).isSelected()) {
  1994. int frownum = saorderdetail.getPao(i).getInt("frownum");
  1995. if (saorderdetail.getPao(i).getBoolean("fisclose")) {
  1996. throw new P2AppException("", "行" + frownum + "已经关闭,不可发货");
  1997. }
  1998. if (saorderdetail.getPao(i).getDouble("fundeliqty") == 0) {
  1999. throw new P2AppException("", "行" + frownum + "已发货完毕,不可发货");
  2000. }
  2001. }
  2002. i++;
  2003. }
  2004. }
  2005. /**
  2006. * 一键发货生成发货单或销售出库单
  2007. *
  2008. * @throws P2Exception
  2009. */
  2010. public PaoRemote sendout(String foutplace) throws P2Exception {
  2011. //系统参数表
  2012. PaoRemote sysVars = getSysVars();
  2013. //是否发货单管理。如果为true,则发货时生成发货单。否则直接生成销售出库单
  2014. boolean fissainvoice = sysVars.getBoolean("fissainvoice");
  2015. PaoSetRemote saorderdetail_view = getPaoSet("saorderdetail_view");
  2016. if (fissainvoice) {
  2017. // 生成发货单
  2018. PaoSetRemote sainvoiceset = getPaoSet("sainvoice");
  2019. sainvoiceset.setApp("SAINVOICE");
  2020. sainvoice sainvoice = (sainvoice) sainvoiceset.addAtEnd();
  2021. sainvoice.setValue("fsonum", getString("fsonum"));
  2022. sainvoice.additemnos(saorderdetail_view);
  2023. return sainvoice;
  2024. } else {
  2025. // 生成销售出库单
  2026. PaoSetRemote icstockbillset = getPaoSet("icstockbill");
  2027. icstockbillset.setWhere("fstatus='新建'");
  2028. icstockbillset.reset();
  2029. icstockbillset.setApp("STOCKBILL0");
  2030. icstockbill icstockbill = null;
  2031. if (icstockbillset.isEmpty()) {
  2032. icstockbill = (icstockbill) icstockbillset.addAtEnd();
  2033. icstockbill.setValue("fsourceid", getUniqueIDValue());
  2034. icstockbill.setValue("fsourceobject", getName());
  2035. icstockbill.setValue("fagentnum", getString("fagentnum"));
  2036. icstockbill.setValue("fnotes", getString("fnotes"));
  2037. icstockbill.setValue("foutplace", foutplace, 11L);
  2038. icstockbill.setValue("faddress", getString("faddress"));
  2039. icstockbill.setValue("fcontact", getString("fcontact"));
  2040. icstockbill.setValue("fphonenumber", getString("fphonenumber"));
  2041. icstockbill.setValue("finvoicename", getString("finvoicename"), 11L);
  2042. icstockbill.setValue("finvoiceaddress", getString("finvoiceaddress"), 11L);
  2043. icstockbill.setValue("finvoicetaxnumber", getString("finvoicetaxnumber"), 11L);
  2044. } else {
  2045. icstockbill = (icstockbill) icstockbillset.getPao(0);
  2046. PaoSetRemote icstockbilldetail = icstockbill.getPaoSet("icstockbilldetail");
  2047. int i = 0;
  2048. while (saorderdetail_view.getPao(i) != null) {
  2049. if (saorderdetail_view.getPao(i).isSelected()) {
  2050. long saorderdetailid = saorderdetail_view.getPao(i)
  2051. .getUniqueIDValue();
  2052. int m = 0;
  2053. while (icstockbilldetail.getPao(m) != null) {
  2054. if (icstockbilldetail.getPao(m).getLong(
  2055. "saorderdetailid") == saorderdetailid) {
  2056. icstockbilldetail.getPao(m).setValue(
  2057. "fqty",
  2058. saorderdetail_view.getPao(i).getDouble(
  2059. "fqty"));
  2060. saorderdetail_view.getPao(i).unselect();
  2061. break;
  2062. }
  2063. m++;
  2064. }
  2065. }
  2066. i++;
  2067. }
  2068. }
  2069. icstockbill.addRowsFromSaorder(saorderdetail_view);
  2070. icstockbill.setValue("fpayamount", icstockbill.getPaoSet("icstockbilldetail_view").sum(
  2071. "ftaxamount"), 11L);
  2072. return icstockbill;
  2073. }
  2074. }
  2075. /**
  2076. * 配送方案生成
  2077. *
  2078. * @return
  2079. * @throws P2Exception
  2080. */
  2081. public void createrationvalidate() throws P2Exception {
  2082. if (!"新建".equals(getString("fstatus"))) {
  2083. throw new P2AppException("", "非新建状态下不可生成配送方案");
  2084. }
  2085. if (!getPaoSet("saorderdetail_view2").isEmpty()) {
  2086. throw new P2AppException("", "已生成配送方案,不可重复生成");
  2087. }
  2088. }
  2089. /**
  2090. * 配送方案生成
  2091. *
  2092. * @return
  2093. * @throws P2Exception
  2094. */
  2095. public void createration() throws P2Exception {
  2096. PaoSetRemote saorderdetail_view = getPaoSet("saorderdetail_view");
  2097. ArrayList<HashMap<String, Object>> list = runQuerySQL("exec [SAORDEROPENRATION]'"
  2098. + getUniqueIDValue() + "'");
  2099. for (HashMap<String, Object> map : list) {
  2100. String fitemno = map.get("fitemno") + "";
  2101. double fqty = new Double(map.get("fqty") + "");
  2102. double frationqty = new Double(map.get("frationqty") + "");
  2103. double fprice = new Double(map.get("fprice") + "");
  2104. double frationminqty = new Double(map.get("frationminqty") + "");
  2105. double frationismincaculate = new Double(map.get("frationismincaculate") + "");
  2106. long saorderdetailid = new Long(map.get("saorderdetailid") + "");
  2107. String frationnum = map.get("frationnum") + "";
  2108. String frationtype = map.get("frationtype") + "";
  2109. PaoRemote pao = saorderdetail_view.addAtEnd();
  2110. pao.setValue("fisration", true, 2L);
  2111. pao.setValue("fitemno", fitemno, 2L);
  2112. pao.setValue("fqty", fqty, 2L);
  2113. pao.setValue("fprice", fprice, 2L);
  2114. pao.setValue("frationnum", frationnum, 11L);
  2115. pao.setValue("frationtype", frationtype, 11L);
  2116. pao.setValue("fsaorderdetailid", saorderdetailid, 11L);
  2117. pao.setValue("frationqty", frationqty, 11L);
  2118. pao.setValue("frationminqty", frationminqty, 11L);
  2119. pao.setValue("frationismincaculate", frationismincaculate, 11L);
  2120. }
  2121. if (!list.isEmpty()) {
  2122. setValue("fisration", true, 11L);
  2123. }
  2124. }
  2125. /**
  2126. * 清空辅材方案验证
  2127. *
  2128. * @throws P2Exception
  2129. */
  2130. public void deleterationvalidate() throws P2Exception {
  2131. if (!"新建".equals(getString("fstatus"))) {
  2132. throw new P2AppException("", "非新建状态下不可清空配送方案");
  2133. }
  2134. }
  2135. /**
  2136. * 清空辅材方案
  2137. *
  2138. * @throws P2Exception
  2139. */
  2140. public void deleteration() throws P2Exception {
  2141. getPaoSet("saorderdetail_view2").deleteAll();
  2142. setValue("fisration", false, 11L);
  2143. }
  2144. @Override
  2145. public void fieldAction(Object paoField, String fieldname)
  2146. throws P2Exception {
  2147. super.fieldAction(paoField, fieldname);
  2148. if ("FAGENTNUM".equals(fieldname)) {
  2149. setValue("fpayagentnum", getString("fagentnum"), 11L);
  2150. setValue("frecagentnum", getString("fagentnum"), 11L);
  2151. PaoSetRemote tagents = getPaoSet("tagents");
  2152. if (!"".equals(getString("fagentnum")) && !tagents.isEmpty()) {
  2153. if (!"促销订单".equals(getString("ftype")) && !"工具借出单".equals(getString("ftype"))) {
  2154. setValue("faccclsnum", "01");
  2155. }
  2156. PaoRemote agentpao = tagents.getPao(0);
  2157. // // 设置业务员
  2158. // PaoSetRemote tsalearea = agentpao.getPaoSet(
  2159. // "tsalearea");
  2160. // if (!tsalearea.isEmpty()) {
  2161. // setValue("fsaler", tsalearea.getPao(0).getString("fsaler"));
  2162. // } else {
  2163. // setValue("fsaler", "");
  2164. // }
  2165. // 默认收货地址设置
  2166. PaoSetRemote tagents_address = agentpao.getPaoSet(
  2167. "tagents_address_default");
  2168. if (!tagents_address.isEmpty()) {
  2169. PaoRemote address = tagents_address.getPao(0);
  2170. setValue("fcontact", address.getString("fcontact"));
  2171. setValue("fphonenumber", address.getString("fphonenumber"));
  2172. setValue("faddress", address.getString("faddress"));
  2173. setValue("fprovince", address.getString("fprovince"), 11L);
  2174. setValue("fcity", address.getString("fcity"), 11L);
  2175. setValue("fcounty", address.getString("fcounty"), 11L);
  2176. }
  2177. // 默认开票信息设置
  2178. PaoSetRemote tagents_invoice = agentpao.getPaoSet("tagents_invoice_default");
  2179. if (!tagents_invoice.isEmpty()) {
  2180. PaoRemote invoice = tagents_invoice.getPao(0);
  2181. setValue("finvoicename", invoice.getString("fname"), 11L);
  2182. setValue("finvoicetaxnumber", invoice.getString("ftaxnumber"), 11L);
  2183. setValue("finvoiceaddress", invoice.getString("faddress"), 11L);
  2184. }
  2185. PaoSetRemote tagents_field = agentpao.getPaoSet("tagents_field");
  2186. if (!tagents_field.isEmpty()) {
  2187. setValue("fieldname", tagents_field.getPao(0).getString("fieldname"), 11L);
  2188. setValue("fsaler", tagents_field.getPao(0).getString("hrid"), 11L);
  2189. }
  2190. PaoSetRemote tagents_brand = agentpao.getPaoSet("tagents_brand");
  2191. if (!tagents_brand.isEmpty()) {
  2192. setValue("fbrand", tagents_brand.getPao(0).getString("fbrand"), 11L);
  2193. }
  2194. } else {
  2195. setValue("faccclsnum", "");
  2196. setValue("fsaler", "");
  2197. setValue("fcontact", "", 11L);
  2198. setValue("fphonenumber", "", 11L);
  2199. setValue("faddress", "", 11L);
  2200. setValue("finvoicename", "", 11L);
  2201. setValue("finvoicetaxnumber", "", 11L);
  2202. setValue("finvoiceaddress", "", 11L);
  2203. setValue("fprovince", "", 11L);
  2204. setValue("fcity", "", 11L);
  2205. setValue("fcounty", "", 11L);
  2206. }
  2207. } else if ("FPROMNUM".equals(fieldname)) {
  2208. if ("促销订单".equals(getString("ftype"))) {
  2209. if ("打包促销".equals(getString("tpromotions.ftype"))) {
  2210. setFieldFlag("fpromqty", READONLY, false);
  2211. setFieldFlag("fpromqty", REQUIRED, true);
  2212. } else {
  2213. setFieldFlag("fpromqty", READONLY, true);
  2214. setFieldFlag("fpromqty", REQUIRED, false);
  2215. }
  2216. }
  2217. } else if ("FPROMQTY".equals(fieldname)) {
  2218. double fpromqty = getDouble("fpromqty");
  2219. if (fpromqty == 0) {
  2220. getPaoSet("saorderdetail_view").deleteAll();
  2221. getPaoSet("saorderdetail_view").deleteAndRemoveAll();
  2222. setFieldFlag("fpromnum", READONLY, false);
  2223. } else {
  2224. PaoSetRemote tpromproducts = getPaoSet("tpromproducts");
  2225. int i = 0;
  2226. PaoRemote pao = null;
  2227. while ((pao = tpromproducts.getPao(i)) != null) {
  2228. //打包数量
  2229. double fpackingqty = pao.getDouble("fpackingqty");
  2230. pao.setValue("faddqty", fpromqty * fpackingqty);
  2231. pao.select();
  2232. i++;
  2233. }
  2234. additems_prom(tpromproducts);
  2235. setFieldFlag("fpromnum", READONLY, true);
  2236. }
  2237. } else if ("FACCCLSNUM".equals(fieldname)) {
  2238. if ("02".equals(getString(fieldname))) {
  2239. setValue("fisusefl", false, 11L);
  2240. setFieldFlag("fisusefl", READONLY, true);
  2241. } else {
  2242. setFieldFlag("fisusefl", READONLY, false);
  2243. }
  2244. } else if ("FISUSEFL".equals(fieldname)) {
  2245. //double ftotalamount = getPaoSet("saorderdetail_view").sum("famount");
  2246. //double fmjamount = caculateMJ();
  2247. //caculateUseFLAmount(Math.sub(ftotalamount, fmjamount));
  2248. } else if ("FTYPE".equals(fieldname)) {
  2249. if ("特殊订单".equals(getString(fieldname))) {
  2250. setFieldFlag("ftypemx", READONLY, false);
  2251. } else {
  2252. setValue("ftypemx", "普通", 11L);
  2253. setFieldFlag("ftypemx", READONLY, true);
  2254. }
  2255. } else if ("FPLANDATE".equals(fieldname)) {
  2256. //回复交期
  2257. PaoSetRemote saorderdetail_view = getPaoSet("saorderdetail_view");
  2258. int i = 0;
  2259. while (saorderdetail_view.getPao(i) != null) {
  2260. if (saorderdetail_view.getPao(i).getPaoSet("titem").getPao(0).getBoolean("fissaleday")) {
  2261. saorderdetail_view.getPao(i).setValue("fplandate", getDate(fieldname), 11L);
  2262. }
  2263. i++;
  2264. }
  2265. } else if ("FNEEDDATE".equals(fieldname)) {
  2266. //回复交期
  2267. PaoSetRemote saorderdetail_view = getPaoSet("saorderdetail_view");
  2268. int i = 0;
  2269. while (saorderdetail_view.getPao(i) != null) {
  2270. if (saorderdetail_view.getPao(i).getPaoSet("titem").getPao(0).getBoolean("fissaleday")) {
  2271. saorderdetail_view.getPao(i).setValue("fneeddate", getDate(fieldname), 11L);
  2272. }
  2273. i++;
  2274. }
  2275. }
  2276. }
  2277. @Override
  2278. public void fieldValidate(Object paoField, String fieldname) throws P2Exception {
  2279. super.fieldValidate(paoField, fieldname);
  2280. if ("FPROMQTY".equals(fieldname)) {
  2281. double fpromqty = getDouble("fpromqty");
  2282. if (fpromqty < 0) {
  2283. throw new P2AppException("", "数量不可小于0");
  2284. }
  2285. } else if ("FPROJECTNOTES".equals(fieldname)) {
  2286. PaoSetRemote tprojectSet = getPaoSet("$tproject", "tproject");
  2287. tprojectSet.setWhere("fstatus='审核' and fieldname='" + getString("fieldname") + "' and fprojectname like'%" + getString("fprojectnotes") + "%' and ((fbegdate<=tproject.fbegdate and fenddate>=tproject.fbegdate ) or (fbegdate<=tproject.fenddate and fenddate>=tproject.fenddate ))");
  2288. tprojectSet.reset();
  2289. if (!tprojectSet.isEmpty()) {
  2290. ((PaoSet) getThisPaoSet()).addWarning(new P2AppException("", "当前项目有效期内已存在审核的项目报备"));
  2291. }
  2292. }
  2293. }
  2294. @Override
  2295. public PaoSetRemote fieldGetlist(Object paoField, PaoSetRemote list, String fieldname) throws P2Exception {
  2296. if ("FPAYAGENTNUM".equals(fieldname)) {
  2297. PaoSetRemote tagentsSet = getPaoSet("tagents");
  2298. if (!tagentsSet.isEmpty()) {
  2299. SQLFactory factory = new SQLFactory(this, "结算客户查询");
  2300. factory.addParameter("siteid", getInsertSite());
  2301. factory.addParameter("fagentnum", getString("fagentnum"));
  2302. DBConnect dbConnect = new DBConnect();
  2303. Rows rows = dbConnect.runSqlQuery(factory.getSQL());
  2304. String where = rows.getInWhere("fagentnum");
  2305. list.setWhere("fagentnum in " + where);
  2306. } else {
  2307. list.setWhere("1=2");
  2308. }
  2309. }
  2310. return super.fieldGetlist(paoField, list, fieldname);
  2311. }
  2312. public void getStockStatus() throws P2Exception {
  2313. PaoSetRemote saorderdetail = getPaoSet("saorderdetail");
  2314. String[] fitemnos = getDistinctString(saorderdetail, "fitemno", false);
  2315. String where = "";
  2316. for (String fitemno : fitemnos) {
  2317. if ("".equals(where)) {
  2318. where = "'" + fitemno + "'";
  2319. } else {
  2320. where = where + ",'" + fitemno + "'";
  2321. }
  2322. }
  2323. if (!"".equals(where)) {
  2324. PaoSetRemote titemSet = getPaoSet("$titem", "titem", "fitemno in (" + where + ")");
  2325. int i = 0;
  2326. titem titem = null;
  2327. while ((titem = (titem) titemSet.getPao(i)) != null) {
  2328. titem.initStockMsg();
  2329. i++;
  2330. }
  2331. }
  2332. }
  2333. /**
  2334. * 是否缺货检查
  2335. *
  2336. * @throws P2Exception
  2337. */
  2338. public boolean checkStockStatus() throws P2Exception {
  2339. getStockStatus();
  2340. PaoSetRemote saorderdetail = getPaoSet("saorderdetail");
  2341. int i = 0;
  2342. while (saorderdetail.getPao(i) != null) {
  2343. if (saorderdetail.getPao(i).getDouble("fqty") > saorderdetail.getPao(i).getPaoSet("titem").getPao(0).getDouble("ficqty")) {
  2344. return false;
  2345. }
  2346. i++;
  2347. }
  2348. return true;
  2349. }
  2350. public void changesaorder_validate(PaoSetRemote paoSetRemote) throws P2Exception {
  2351. int i = 0;
  2352. PaoRemote orderdetail = null;
  2353. while ((orderdetail = paoSetRemote.getPao(i)) != null) {
  2354. double fqty = orderdetail.getDouble("fqty");
  2355. double fchangeqty = orderdetail.getDouble("fchangeqty");
  2356. String fchangenotes = orderdetail.getString("fchangenotes");
  2357. if (fqty != fchangeqty && !orderdetail.getString("fchangeqty").equals("")) {
  2358. if (fchangenotes.equals("")) {
  2359. throw new P2AppException("", "修改备注不可为空");
  2360. }
  2361. }
  2362. i++;
  2363. }
  2364. }
  2365. public PaoRemote changesaorder(PaoSetRemote paoSetRemote) throws P2Exception {
  2366. changesaorder_validate(paoSetRemote);
  2367. HashMap<Long, saorderdetail> saorderdetailmap = new HashMap<>();
  2368. HashMap<Long, Double> saorderdetailchangeamount = new HashMap<>();
  2369. PaoSetRemote saorderdetail_changeSet = getPaoSet("$saorderdetail_change", "saorderdetail_change");
  2370. HashMap<Long, Double> map = new HashMap<Long, Double>();
  2371. int i = 0;
  2372. long id = 0;
  2373. PaoRemote saorderdetail_changePao = null;
  2374. PaoRemote orderdetail = null;
  2375. while ((orderdetail = paoSetRemote.getPao(i)) != null) {
  2376. double fqty = orderdetail.getDouble("fqty");
  2377. double fchangeqty = orderdetail.getDouble("fchangeqty");
  2378. String fchangenotes = orderdetail.getString("fchangenotes");
  2379. Long saorderdetailid = orderdetail.getUniqueIDValue();
  2380. if (fqty != fchangeqty && !orderdetail.getString("fchangeqty").equals("")) {
  2381. PaoRemote saorderdetail_change = saorderdetail_changeSet.addAtEnd();
  2382. if (id == 0) {
  2383. id = saorderdetail_change.getUniqueIDValue();
  2384. saorderdetail_changePao = saorderdetail_change;
  2385. }
  2386. saorderdetail_change.setValue("id", id);
  2387. saorderdetail_change.setValue("foldqty", fqty);
  2388. saorderdetail_change.setValue("fqty", fchangeqty);
  2389. saorderdetail_change.setValue("fchangenotes", fchangenotes);
  2390. saorderdetail_change.setValue("saorderdetailid", saorderdetailid);
  2391. saorderdetail_change.setValue("saorderid", getUniqueIDValue());
  2392. saorderdetail_change.setValue("createby", getUserName());
  2393. saorderdetail_change.setValue("createdate", getDate());
  2394. map.put(saorderdetailid, fchangeqty);
  2395. }
  2396. i++;
  2397. }
  2398. PaoSetRemote saorderdetailSet = getPaoSet("saorderdetail");
  2399. double foldrateamount = saorderdetailSet.sum("frateamount");
  2400. int n = 0;
  2401. while (saorderdetailSet.getPao(n) != null) {
  2402. long detailid = saorderdetailSet.getPao(n).getUniqueIDValue();
  2403. if (map.containsKey(detailid)) {
  2404. double fprice = saorderdetailSet.getPao(n).getDouble("fprice");
  2405. //double fmjamount = saorderdetailSet.getPao(n).getDouble("fmjamount");//行满减金额
  2406. //double fflamount = saorderdetailSet.getPao(n).getDouble("fflamount");//行返利金额
  2407. double fnewqty = map.get(detailid);
  2408. double foldqty = saorderdetailSet.getPao(n).getDouble("fqty");
  2409. double foldundeliqty = saorderdetailSet.getPao(n).getDouble("fundeliqty");
  2410. double faddqty = fnewqty - foldqty;//新增的数量
  2411. saorderdetailSet.getPao(n).setValue("fqty", foldqty + faddqty, 11L);
  2412. saorderdetailSet.getPao(n).setValue("fundeliqty", foldundeliqty + faddqty, 11L);
  2413. saorderdetailSet.getPao(n).setValue("famount", map.get(detailid) * fprice, 11L);
  2414. double famount = saorderdetailSet.getPao(n).getDouble("famount");
  2415. //saorderdetailSet.getPao(n).setValue("frateamount", Math.add(famount, Math.add(-fmjamount, -fflamount)), 11L);
  2416. //saorderdetailSet.getPao(n).setValue("frateprice", saorderdetailSet.getPao(n).getDouble("frateamount") / saorderdetailSet.getPao(n).getDouble("fqty"), 11L);
  2417. saorderdetailSet.getPao(n).setValue("frateamount", fnewqty * saorderdetailSet.getPao(n).getDouble("frateprice"), 11L);
  2418. /**
  2419. * 核销对冲计算
  2420. */
  2421. double fwriteoffamount = saorderdetailSet.getPao(n).getDouble("fwriteoffamount");//已核销金额
  2422. if (fwriteoffamount > 0 && saorderdetailSet.getPao(n).getDouble("frateamount") < fwriteoffamount) {
  2423. //如果已核销金额大于0 且调整后折后金额小于已核销金额,则需要将差价进行回冲
  2424. saorderdetailmap.put(detailid, (saorderdetail) saorderdetailSet.getPao(n));
  2425. saorderdetailchangeamount.put(detailid, Math.sub(fwriteoffamount, saorderdetailSet.getPao(n).getDouble("frateamount")));
  2426. }
  2427. }
  2428. n++;
  2429. }
  2430. double fnewrateamount = saorderdetailSet.sum("frateamount");
  2431. double ce = Math.sub(fnewrateamount, foldrateamount);
  2432. if (ce != 0) {
  2433. /**
  2434. * 订单指定账户
  2435. */
  2436. taccbal taccbal = null;
  2437. PaoSetRemote taccbalSet = getPaoSet("taccbal");
  2438. if (taccbalSet.isEmpty()) {
  2439. taccbal = (taccbal) taccbalSet.addAtEnd();
  2440. taccbal.setfields(getString("fpayagentnum"), getString("faccclsnum"));
  2441. } else {
  2442. taccbal = (taccbal) taccbalSet.getPao(0);
  2443. }
  2444. createFicashbill_Pay(taccbal, ce, "订单调整" + getString("fsonum"), true);
  2445. // if (saorderdetail_changePao != null) {?//
  2446. // boolean issuccess = new UploadDataToERP(saorderdetail_changePao, getUserInfo()).upload();?//
  2447. // }
  2448. }
  2449. /**
  2450. * 核销对冲
  2451. */
  2452. if (!saorderdetailmap.isEmpty()) {
  2453. new twriteoffbill_orderchange(this, saorderdetailmap, saorderdetailchangeamount).hedging();
  2454. }
  2455. return saorderdetail_changePao;
  2456. }
  2457. @Override
  2458. public PaoRemote duplicate() throws P2Exception {
  2459. PaoRemote pao = super.duplicate();
  2460. try {
  2461. pao.setValue("createflag", getThisPaoSet().getApp().contains("管理"), 11L);
  2462. } catch (Exception e) {
  2463. e.printStackTrace();
  2464. }
  2465. return pao;
  2466. }
  2467. }