Sale.php 232 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605
  1. <?php
  2. namespace app\admin\controller;
  3. use app\abutment\model\SupplierRelationUser;
  4. use app\abutment\model\SupplierUser;
  5. use app\admin\model\GoodLog;
  6. use app\admin\model\GoodStockInfo;use app\admin\model\ProcessOrder;
  7. use Exception;
  8. use think\App;
  9. use think\facade\Cache;
  10. use think\facade\Config;
  11. use think\facade\Db;
  12. use app\admin\model\ActionLog;
  13. use think\facade\Validate;
  14. //销售单
  15. class Sale extends Base
  16. {
  17. public $noble = [];
  18. public function __construct(App $app)
  19. {
  20. parent::__construct($app);
  21. $this->noble = \think\facade\Config::get("noble");
  22. }
  23. public function create()
  24. {
  25. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  26. $orderCode = makeNo("QR");
  27. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code'] !== "" ? trim($this->post['customer_code']) : "";
  28. if ($customer_code == "") {
  29. return error_show(1002, "客户customer_code不能为空");
  30. }
  31. $customer = Db::name("customer_info")->where(["companyNo" => $customer_code])->find();
  32. if ($customer == false) {
  33. return error_show(1004, "未找到客户数据");
  34. }
  35. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  36. if ($supplierNo == "") {
  37. return error_show(1002, "参数supplierNo不能为空");
  38. }
  39. $supplier = Db::name("business")->where(["companyNo" => $supplierNo])->find();
  40. if ($supplier == false) {
  41. return error_show(1004, "未找到平台供应商数据");
  42. }
  43. $goodtype = isset($this->post['goodtype']) && $this->post['goodtype'] !== "" ? intval($this->post['goodtype']) : "1";
  44. $order_type = isset($this->post['order_type']) && $this->post['order_type'] !== "" ? intval($this->post['order_type']) : "1";
  45. $sendtype = isset($this->post['sendtype']) && $this->post['sendtype'] !== "" ? intval($this->post['sendtype']) : "";
  46. if ($sendtype === '') {
  47. return error_show(1004, "参数sendtype不能为空");
  48. }
  49. $platform_id = isset($this->post['platform_id']) && $this->post['platform_id'] !== "" ? intval($this->post['platform_id']) : "";
  50. if ($platform_id == '') {
  51. return error_show(1004, "参数platform_id不能为空");
  52. }
  53. $platform_order = isset($this->post['platform_order']) && $this->post['platform_order'] !== "" ? trim($this->post['platform_order']) : "";
  54. $good_code = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : "";
  55. if ($good_code == '') {
  56. return error_show(1004, "参数good_code不能为空");
  57. }
  58. $good_num = isset($this->post['good_num']) && $this->post['good_num'] !== "" ? intval($this->post['good_num']) : "";
  59. if ($good_num === '') {
  60. return error_show(1004, "参数good_num不能为空");
  61. }
  62. $arrtime = isset($this->post['arrtime']) && $this->post['arrtime'] != "" ? $this->post['arrtime'] : "";
  63. if ($arrtime == "") {
  64. return error_show(1004, "参数arrtime不能为空");
  65. }
  66. $paytime = isset($this->post['paytime']) && $this->post['paytime'] != "" ? $this->post['paytime'] : "";
  67. $workNo = isset($this->post['workNo']) && $this->post['workNo'] != "" ? trim($this->post['workNo']) : "";
  68. // $poNo =isset($this->post['poNo'])&&$this->post['poNo']!=""? trim($this->post['poNo']):"";
  69. $ct = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  70. ->where(['a.skuCode' => $good_code])->field("b.*,a.skuCode,a.platform_code,a.plat_code")->find();
  71. if ($ct == false) {
  72. return error_show(1002, "未找到商品数据");
  73. }
  74. $goodinfo = $ct;
  75. $is_stock = $ct['is_stock'];
  76. $spuCode = $ct['spuCode'];
  77. $skuCode = $ct['skuCode'];
  78. $is_activity = isset($this->post['is_activity']) && $this->post['is_activity'] !== "" ? $this->post['is_activity'] : "";
  79. $actcode = isset($this->post['act_code']) && $this->post['act_code'] !== "" ? trim($this->post['act_code']) : "";
  80. if ($order_type == 1) {
  81. if ($is_activity === "") {
  82. return error_show(1002, "参数is_activity不能为空");
  83. }
  84. if ($is_activity == 1 && $actcode == "") {
  85. return error_show(1002, "参数act_code不能为空");
  86. }
  87. }
  88. if ($goodinfo['is_stock'] == 1) {
  89. $stock_num = Db::name("good_stock")->alias("a")
  90. ->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")
  91. ->leftJoin("good_stock_info c", "a.id=c.stockid")
  92. ->where(["spuCode" => $spuCode, "a.is_del" => 0, "a.status" => 1, "b.wsm_type" => 5, "b.companyNo" =>$supplierNo])
  93. ->sum('balance_num');
  94. // if ($stock == false || $stock['usable_stock'] < $good_num) {
  95. // return error_show(1003, "库存数量不足");
  96. // }
  97. if($stock_num < $good_num){
  98. return error_show(1003, "库存数量不足");
  99. }
  100. $origin_price = 0;
  101. } else {
  102. if ($is_activity == 0) {
  103. $origin = Db::name("good_nake")->where([["spuCode", "=", $spuCode], ["min_num", "<=", $good_num], ["is_del", "=", 0]])->order("min_num desc")->find();
  104. if ($origin == false) {
  105. return error_show(1003, "未找到相关阶梯成本价格");
  106. }
  107. $origin_price = $origin['nake_total'];
  108. }
  109. }
  110. $sale_price = isset($this->post['good_price']) && $this->post['good_price'] !== "" ? floatval($this->post['good_price']) : '';
  111. $ct['cgd_gold_price'] = 0;
  112. if ($goodtype == 1) {
  113. if ($is_activity == 1) {
  114. $act = Db::name("activity_info")->alias("a")->leftJoin("good_activity b", "a.activity_code=b.activity_code")
  115. ->where(["a.skuCode" => $skuCode, "a.activity_code" => $actcode, "a.is_del" => 0, "a.status" => 1, "b.status" => 6, "b.is_del" => 0])
  116. ->find();
  117. if ($act == false) {
  118. return error_show(1003, "未找到相关活动价");
  119. }
  120. if ($act['moq_num'] > $good_num) {
  121. return error_show(1003, "商品不满足活动价起订量{$act['moq_num']}");
  122. }
  123. if ($act['activity_stock'] < $good_num) {
  124. return error_show(1003, "商品活动库存剩余{$act['activity_stock']}");
  125. }
  126. $sale_price = $sale_price !== "" ? $sale_price : $act['activity_price'];
  127. $origin_price = $act['cost_price'];
  128. } else {
  129. $good = Db::name("good_ladder")->where(["skuCode" => $skuCode, "is_del" => 0, "status" => 1])->where([["min_num", "<=", $good_num]])->order("min_num desc")->find();
  130. if ($good == false) {
  131. return error_show(1003, "未找到相关阶梯价格");
  132. }
  133. $sale_price = $sale_price !== "" ? $sale_price : $good['sale_price'];
  134. if ($ct['is_gold_price'] == 1 && $is_stock != 1) {
  135. $gold = Db::name("gold_price1")
  136. ->field('id,price')
  137. ->where(["type" => $ct['noble_metal'], "is_del" => 0, "status" => 1])
  138. ->order("addtime desc")
  139. ->find();
  140. //$saleprice(最终售价) = (打样费/购买数量 + 开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价+物流费)/(1-成本售价/100);
  141. $sale_price = $sale_price !== "" ? $sale_price : ($ct['demo_fee'] / $good_num + $ct['open_fee'] / $good_num
  142. + $ct['noble_weight'] * $gold["price"] + $good['cost_fee'] * $ct['noble_weight'] + $origin['package_fee'] + $origin['mark_fee'] + $origin['cert_fee'] + $origin['nake_fee'] + $origin['delivery_fee']);
  143. $ct['cgd_gold_price'] = $gold["price"];
  144. }
  145. }
  146. } else {
  147. $sale_price = 0;
  148. }
  149. $supplier_info = Db::name('supplier')
  150. ->field('id,person,personid')
  151. ->where('code',$ct['supplierNo'])
  152. ->findOrEmpty();
  153. $cgd = [
  154. "supplierNo" => $ct['supplierNo'],
  155. "companyNo" => $supplierNo,
  156. "orderCode" => $orderCode,
  157. "spuCode" => $ct['spuCode'],
  158. "skuCode" => $ct['skuCode'],
  159. "good_name" => $ct['good_name'],
  160. "sale_price" => $origin_price,
  161. "total_fee" => $origin_price * $good_num,
  162. "pakge_fee" => isset($origin['package_fee']) ? $origin['package_fee'] : 0,
  163. "cert_fee" => isset($origin['cert_fee']) ? $origin['cert_fee'] : 0,
  164. "open_fee" => $ct['open_fee'],
  165. "cost_fee" => isset($origin['cost_fee']) ? $origin['cost_fee'] : 0,
  166. "mark_fee" => isset($origin['mark_fee']) ? $origin['mark_fee'] : 0,
  167. "nake_fee" => isset($origin['nake_fee']) ? $origin['nake_fee'] : 0,
  168. "delivery_fee" => isset($origin['delivery_fee']) ? $origin['delivery_fee'] : 0,
  169. "demo_fee" => $ct['demo_fee'],
  170. "good_num" => $good_num,
  171. "good_type" => $goodtype,
  172. "weight" => $ct['noble_weight'],
  173. "gold_price" => isset($ct['cgd_gold_price']) ? $ct['cgd_gold_price'] : 0,
  174. "order_type" => $is_stock == 1 ? 1 : 2,
  175. "order_source" => 1,//1直接下单
  176. "createrid" => $ct['createrid'],
  177. "creater" => $ct['creater'],
  178. 'send_way' => 2,
  179. 'good_createrid' => $goodinfo['createrid'],
  180. 'good_creater' => $goodinfo['creater'],//商品创建人
  181. ];
  182. $addrlist = isset($this->post['addrlist']) && $this->post['addrlist'] !== "" ? $this->post['addrlist'] : "";
  183. $send_num = 0;
  184. if ($sendtype == 1) {
  185. if ($addrlist == "" || empty($addrlist) || !is_array($addrlist)) {
  186. return error_show(1004, "参数addrlist不能为空");
  187. }
  188. $send_num = array_sum(array_column($addrlist, "receipt_quantity"));
  189. }
  190. if ($send_num > $good_num) {
  191. return error_show(1004, "发货数量超出订单总数量");
  192. }
  193. $remark = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
  194. $proof_id = isset($this->post['proof_id']) && $this->post['proof_id'] !== "" ? intval($this->post['proof_id']) : 0;
  195. // $apply_id = GetUserInfo($token);
  196. // if (empty($apply_id) || $apply_id['code'] != 0) {
  197. // return error_show(1002, "申请人数据不存在");
  198. // }
  199. $rm = $this->uid;
  200. $ri = $this->uname;
  201. Db::startTrans();
  202. try {
  203. $data = [
  204. "orderCode" => $orderCode,
  205. "good_code" => $spuCode,
  206. "skuCode" => $skuCode,
  207. "customer_code" => $customer_code,
  208. "good_name" => isset($goodinfo['good_name']) && $goodinfo['good_name'] !== '' ? $goodinfo['good_name'] : '',
  209. "good_num" => $good_num,
  210. "cat_id" => $goodinfo['cat_id'],
  211. "apply_id" => $rm,
  212. "apply_name" => $ri,
  213. "origin_price" => $origin_price,
  214. "sale_price" => $sale_price,
  215. "post_fee" => 0,
  216. "status" => 0,
  217. "send_num" => 0,
  218. "wsend_num" => $good_num,
  219. "send_status" => 0,
  220. "good_type" => $goodtype,
  221. "send_type" => $sendtype,
  222. "supplierNo" => $supplierNo,
  223. "is_del" => 0,
  224. "zxNo" => "",
  225. "platform_order" => $platform_order,
  226. "platform_id" => $platform_id,
  227. "remark" => $remark,
  228. "is_stock" => $is_stock,
  229. "is_activity" => $is_activity === "" ? 0 : $is_activity,
  230. "activity_code" => $actcode,
  231. "order_type" => $is_stock == 1 ? 1 : 2,
  232. "order_source" => 1,//1直接下单
  233. // "poNo"=>$poNo,
  234. 'good_weight' => $ct['noble_weight'],
  235. 'gold_price' => isset($ct['cgd_gold_price']) ? $ct['cgd_gold_price'] : 0,
  236. 'cost_price' => isset($good['cost_fee']) ? $good['cost_fee'] : 0,
  237. 'diff_weight' => 0,
  238. 'diff_fee' => 0,
  239. "workNo" => $workNo,
  240. "addtime" => date("Y-m-d H:i:s"),
  241. "updatetime" => date("Y-m-d H:i:s"),
  242. 'total_price' => round($sale_price * $good_num, 2),
  243. 'proof_id' => $proof_id,
  244. 'cgderid' => $supplier_info['personid'],
  245. 'cgder' => $supplier_info['person'],//采购员(供应商负责人)
  246. 'good_createrid' => $goodinfo['createrid'],
  247. 'good_creater' => $goodinfo['creater'],//商品创建人
  248. ];
  249. $paytime == "" ? "" : $data['paytime'] = $paytime;
  250. $datainfo = Db::name('sale')->insert($data, true);
  251. if ($datainfo > 0) {
  252. //修改状态,添加待办
  253. ActionLog::logAdd($this->post['token'], [
  254. "order_code" => $orderCode,//销售单code
  255. "status" => 0,//这里的status是之前的值
  256. "action_remark" => '',//备注
  257. "action_type" => "create"//新建create,编辑edit,更改状态status
  258. ], "XSQRD", 0, $this->post);
  259. ProcessOrder::AddProcess($this->post['token'], [
  260. "order_type" => 'XSQRD',
  261. "order_code" => $orderCode,//销售单code
  262. "order_id" => $datainfo,
  263. "order_status" => 0,
  264. "before_status" => 0,
  265. 'holder_id' => $data['apply_id']
  266. ]);
  267. if ($is_activity == 1) {
  268. $actup = [
  269. "activity_stock" => $act['activity_stock'] - $good_num,
  270. "updatetime" => date("Y-m-d H:i:s")
  271. ];
  272. $actupp = Db::name("activity_info")->where(["skuCode" => $skuCode, "activity_code" => $actcode,
  273. "is_del" => 0, "status" => 1])->save($actup);
  274. if ($actupp == false) {
  275. Db::rollback();
  276. return error_show(1002, "活动库存修改失败");
  277. }
  278. }
  279. $outstatus = 0;
  280. $standing_book_da = ['sale_id' => $datainfo, 'customer_code' => $customer_code, 'skuCode' => $skuCode, 'updatetime' => date('Y-m-d H:i:s')];
  281. if ($is_stock == 0) {
  282. $bol = $this->createCgd($cgd, $standing_book_da);
  283. if ($bol == false) {
  284. Db::rollback();
  285. return error_show(1002, "订单创建失败");
  286. }
  287. } else {
  288. $outstatus = 1;
  289. // $bol = $this->RelaCgd([
  290. // 'orderCode' => $orderCode,
  291. // "good_num" => $good_num,
  292. // "spuCode" => $spuCode,
  293. // "companyNo" => $supplierNo,
  294. // "cost_fee" => isset($good['cost_fee']) ? $good['cost_fee'] : 0,
  295. // 'order_type' => $data['order_type'],
  296. // 'order_source' => $data['order_source'],
  297. // ], $standing_book_da);
  298. // if ($bol == false) {
  299. // Db::rollback();
  300. // return error_show(1002, "库存商品关联采购单失败");
  301. // }
  302. if (isset($stock)) {
  303. $stockid= Db::name("good_stock")->alias("a")
  304. ->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")
  305. ->leftJoin("good_stock_info c", "a.id=c.stockid")
  306. ->where(["spuCode" => $spuCode, "a.is_del" => 0, "a.status" => 1, "b.wsm_type" => 5, "b.companyNo" =>$supplierNo])
  307. ->column('a.id');
  308. $stockinfo =GoodStockInfo::OrderBn($orderCode,$stockid,$good_num);
  309. if($stockinfo==false){
  310. Db::rollback();
  311. return error_show(1002, "Bn库存更新库存失败");
  312. }
  313. //商品变动日志表,good_log_code字段存储采购单号
  314. // $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $stock['id'], "type" => 2, 'stock' => $good_num, "stock_name" => "usable_stock"];
  315. // $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $stock['id'], "type" => 1, 'stock' => $good_num, "stock_name" => "wait_out_stock"];
  316. // GoodLog::LogAdd($this->post['token'], $good_data, "XSQRD");
  317. }
  318. }
  319. if ($sendtype == 1) {
  320. $i=1;
  321. foreach ($addrlist as $value) {
  322. $temp = [];
  323. $addrs = [];
  324. if ($value['addr_code'] !== '' && is_array($value['addr_code']) && !empty($value['addr_code'])) {
  325. $addrs['provice_code'] = $value['addr_code'][0];
  326. $addrs['city_code'] = $value['addr_code'][1];
  327. $addrs['area_code'] = $value['addr_code'][2];
  328. $addr = json_encode($addrs);
  329. } else {
  330. $addr = isset($value['addr_code']) ? $value['addr_code'] : '';
  331. }
  332. $temp['orderCode'] = $orderCode;
  333. $temp['contactor'] = $value['contactor'];
  334. $temp['mobile'] = $value['mobile'];
  335. $temp['addr'] = $value['addr'];
  336. $temp['addr_code'] = $addr;
  337. $temp['customer_code'] = $customer_code;
  338. $temp['receipt_quantity'] = $value['receipt_quantity'];
  339. $temp['post_fee'] = 0;
  340. $temp['is_del'] = 0;
  341. $temp['addtime'] = date("Y-m-d H:i:s");
  342. $temp['updatetime'] = date("Y-m-d H:i:s");
  343. $temp['arrive_time'] = $arrtime;
  344. $vmp = Db::name('order_addr')->insert($temp, true);
  345. if ($vmp > 0) {
  346. $num = $value['receipt_quantity'];
  347. $outCode = makeNo("DF");
  348. //改变编码规则,将原来的outCode后两位换成序列号
  349. //str_pad字符串填充
  350. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  351. $i++;
  352. if($is_stock!=1) {
  353. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  354. ->lock(true)->find();
  355. if ($order == false) {
  356. return error_show(1004, "未找到可以发货得采购单数据");
  357. }
  358. $order['wsend_num'] -= $num;
  359. $order['send_num'] += $num;
  360. $or = Db::name("order_num")->save($order);
  361. if ($or == false) {
  362. Db::rollback();
  363. return error_show(1002, "发货地址添加创建失败");
  364. }
  365. $tep = [
  366. "cgdNo" => $order['cgdNo'],
  367. "outCode" => $outCode,
  368. "send_num" => $num,
  369. "status" => 1,
  370. "addtime" => date("Y-m-d H:i:s"),
  371. "updatetime" => date("Y-m-d H:i:s")
  372. ];
  373. $sen = Db::name("order_send")->save($tep);
  374. if ($sen == false) {
  375. Db::rollback();
  376. return error_show(1002, "发货地址添加创建失败");
  377. }
  378. $cgdinfo = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
  379. if ($cgdinfo == false) {
  380. Db::rollback();
  381. return error_show(1002, "未匹配到采购数据");
  382. }
  383. }
  384. $out = [
  385. "orderCode" => $orderCode,
  386. "outCode" => $outCode,
  387. "apply_id" => $rm,
  388. "apply_name" => $ri,
  389. "addrid" => $vmp,
  390. "post_name" => "",
  391. "post_code" => "",
  392. "post_fee" => 0,
  393. "sendtime" => date("Y-m-d H:i:s"),
  394. "send_num" => $num,
  395. "check_num" => 0,
  396. "error_num" => 0,
  397. "wsm_code" => $is_stock == 1 ?'':$cgdinfo['wsm_code'],
  398. "order_type" => $is_stock == 1 ? 1 : 2,
  399. "status" => $outstatus,
  400. "addtime" => date("Y-m-d H:i:s"),
  401. "updatetime" => date("Y-m-d H:i:s")
  402. ];
  403. $ou = Db::name("order_out")->insertGetId($out);
  404. if ($ou == false) {
  405. Db::rollback();
  406. return error_show(1002, "发货地址添加创建失败");
  407. } else {
  408. //修改状态,添加待办
  409. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
  410. "order_code" => $outCode,//出库单号
  411. "status" => $out['status'],//这里的status是之前的值
  412. "action_remark" => '',//备注
  413. "action_type" => "create"//新建create,编辑edit,更改状态status
  414. ], "CKD", $out['status'], $out);
  415. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], [
  416. "order_type" => 'CKD',
  417. "order_code" => $outCode,//出库单号
  418. "order_id" => $ou,
  419. "order_status" => $out['status'],
  420. "before_status" => $out['status'],
  421. 'holder_id'=>$out['apply_id'],
  422. ]);
  423. // $standing_book_da['outCode'] = $outCode;
  424. if (isset($standing_book_da['outCode'])) $standing_book_da['outCode'][] = $outCode;
  425. else $standing_book_da['outCode'] = [$outCode];
  426. }
  427. }
  428. else {
  429. Db::rollback();
  430. return error_show(1002, "发货地址添加创建失败");
  431. }
  432. }
  433. }
  434. //处理台账
  435. if (isset($standing_book_da['outCode'])) $standing_book_da['outCode'] = implode(',', $standing_book_da['outCode']);//发货单号拼接
  436. Db::name('standing_book')->insert(array_merge($standing_book_da, ['addtime' => date('Y-m-d H:i:s'), 'updatetime' => date('Y-m-d H:i:s'), 'standBookNo' => makeNo('IO')]));
  437. Db::commit();
  438. return app_show(0, "咨询订单创建成功", ["order_code" => $orderCode]);
  439. }
  440. Db::rollback();
  441. return error_show(1002, "咨询订单创建失败");
  442. } catch (\Exception $e) {
  443. Db::rollback();
  444. return error_show(1005, $e->getMessage());
  445. }
  446. }
  447. //采购单
  448. public function createCgd($data, array &$standing_book_da = [])
  449. {
  450. $cgdCode = makeNo("CG");
  451. $supplier = Db::name("supplier")->where(["code" => $data['supplierNo'], "is_del" => 0])->find();
  452. if ($supplier == false) {
  453. return false;
  454. }
  455. $wsm = Db::name("warehouse_info")->where(["supplierNo" => $data["supplierNo"], "companyNo" => $data['companyNo'], "wsm_type" => 2, "is_del" => 0])->find();
  456. if ($wsm == false) {
  457. $wsm_code = makeNo("WSM");
  458. $inwsm = [
  459. "wsm_code" => $wsm_code,
  460. "name" => $supplier['name'],
  461. "wsm_type" => 2,
  462. "supplierNo" => $supplier['code'],
  463. "addr" => "",
  464. "addrs_code" => "",
  465. "contactor" => $data['order_type'] == 1 ? $supplier['personid'] : $data['createrid'],
  466. "contactor_name" => $data['order_type'] == 1 ? $supplier['person'] : $data['creater'],
  467. "mobile" => "",
  468. "position" => "",
  469. "companyNo" => $data['companyNo'],
  470. "status" => 1,
  471. "is_del" => 0,
  472. "addtime" => date("Y-m-d H:i:s"),
  473. "updatetime" => date("Y-m-d H:i:s")
  474. ];
  475. $in = Db::name("warehouse_info")->insert($inwsm);
  476. if ($in == false) {
  477. return false;
  478. }
  479. } else {
  480. $wsm_code = $wsm['wsm_code'];
  481. }
  482. $cg = ["cgdNo" => $cgdCode,
  483. "bkcode" => "",
  484. "wsm_code" => $wsm_code,
  485. "cgder" => $data['creater'],
  486. "cgder_id" => $data['createrid'],
  487. "spuCode" => $data['spuCode'],
  488. "skuCode" => $data['skuCode'],
  489. "good_name" => $data['good_name'],
  490. "good_num" => $data['good_num'],
  491. "good_price" => $data['sale_price'],
  492. "total_fee" => round($data['sale_price'] * $data['good_num'], 2),
  493. "pakge_fee" => $data['pakge_fee'],
  494. "cert_fee" => $data['cert_fee'],
  495. "open_fee" => $data['open_fee'],
  496. "teach_fee" => $data['cost_fee'],
  497. "mark_fee" => $data['mark_fee'],
  498. "demo_fee" => $data['demo_fee'],
  499. "nake_fee" => $data['nake_fee'],
  500. "weight" => $data['weight'],
  501. "delivery_fee" => $data['delivery_fee'],
  502. "gold_price" => $data['gold_price'],
  503. "diff_weight" => "0",
  504. "diff_fee" => "0",
  505. "supplierNo" => $data['supplierNo'],
  506. "supplier_name" => $supplier['name'],
  507. "companyNo" => $data['companyNo'],
  508. "send_status" => 1,
  509. "send_num" => 0,
  510. "wsend_num" => $data['good_num'],
  511. "remark" => "",
  512. "status" => 0,//0初始化
  513. "lasttime" => date("Y-m-d H:i:s"),
  514. "is_del" => 0,
  515. "order_type" => $data['order_type'],
  516. "order_source" => $data['order_source'],
  517. "good_type" => $data['good_type'],
  518. "addtime" => date("Y-m-d H:i:s"),
  519. "updatetime" => date("Y-m-d H:i:s"),
  520. 'good_createrid' => $data['good_createrid'],
  521. 'good_creater' => $data['good_creater'],//商品创建人
  522. ];
  523. $up = Db::name("purchease_order")->insert($cg, true);
  524. if ($up) {
  525. //修改状态,添加待办
  526. ActionLog::logAdd($this->post['token'], [
  527. "order_code" => $cg['cgdNo'],//销售单code
  528. "status" => 0,//这里的status是之前的值
  529. "action_remark" => '',//备注
  530. "action_type" => "create"//新建create,编辑edit,更改状态status
  531. ], "CGD", $cg['status'], $cg);
  532. ProcessOrder::AddProcess($this->post['token'], [
  533. "order_type" => 'CGD',
  534. "order_code" => $cg['cgdNo'],//销售单code
  535. "order_id" => $up,
  536. "order_status" => $cg['status'],
  537. "before_status" => 0,
  538. 'holder_id' => $supplier['personid']
  539. ]);
  540. $standing_book_da = array_merge($standing_book_da, [
  541. 'orderCode' => $data['orderCode'],
  542. 'cgdNo' => $cgdCode,
  543. 'spuCode' => $data['spuCode'],
  544. 'skuCode' => $data['skuCode'],
  545. 'order_type' => $data['order_type'],
  546. 'order_source' => $data['order_source'],
  547. 'supplierNo' => $data['supplierNo'],
  548. "companyNo" => $data['companyNo'],
  549. ]);
  550. $rela = [
  551. "orderCode" => $data['orderCode'],
  552. "cgdNo" => $cgdCode,
  553. "spuCode" => $data['spuCode'],
  554. "good_num" => $data['good_num'],
  555. "wsend_num" => $data['good_num'],
  556. "send_num" => 0,
  557. "wait_num" => 0,
  558. "status" => 1,
  559. "source" => 2
  560. ];
  561. $re = Db::name("order_num")->save($rela);
  562. if ($re == false) {
  563. return false;
  564. } else {
  565. $stokc = Db::name("good_stock")->where(['spuCode' => $data['spuCode'], "wsm_code" => $wsm_code, "is_del" => 0])->find();
  566. if ($stokc == false) {
  567. $stokc = [
  568. "spuCode" => $data['spuCode'],
  569. "wsm_code" => $wsm_code,
  570. "wait_in_stock" => $data['good_num'],
  571. "wait_out_stock" => 0,
  572. "usable_stock" => 0,
  573. "intra_stock" => 0,
  574. "total_stock" => 0,
  575. "status" => 1,
  576. "addtime" => date("Y-m-d H:i:s"),
  577. "updatetime" => date("Y-m-d H:i:s")
  578. ];
  579. } else {
  580. $stokc['wait_in_stock'] += $data['good_num'];
  581. $stokc['updatetime'] = date("Y-m-d H:i:s");
  582. }
  583. $stoc = Db::name("good_stock")->save($stokc);
  584. if ($stoc == false) {
  585. return false;
  586. }
  587. $good_data[] = ['good_log_code' => $cgdCode, "stock_id" => isset($stoc['id']) ? $stoc['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1,
  588. 'stock' => $data['good_num'], "stock_name" => "wait_in_stock"];
  589. GoodLog::LogAdd($this->post['token'], $good_data, "CGD");
  590. return true;
  591. }
  592. } else {
  593. return false;
  594. }
  595. }
  596. public function addAddr()
  597. {
  598. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] != "" ? trim($this->post['orderCode']) : "";
  599. $etid = Db::name("sale")->where(["orderCode" => $orderCode, "is_del" => 0])->find();
  600. if (empty($etid)) {
  601. return error_show(1002, "未找到数据");
  602. }
  603. $retrun =Db::name("sale_return")->where([["orderCode","=",$orderCode],["is_del","=",0],["status","<",4]])
  604. ->count();
  605. if($retrun>0){
  606. return error_show(1005,"存在未完成退货订单数据");
  607. }
  608. $arrtime = isset($this->post['arrtime']) && $this->post['arrtime'] != "" ? $this->post['arrtime'] : "";
  609. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  610. $apply_id = GetUserInfo($token);
  611. if (empty($apply_id) || $apply_id['code'] != 0) {
  612. return error_show(1002, "申请人数据不存在");
  613. }
  614. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  615. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  616. $addrlist = isset($this->post['addrlist']) && $this->post['addrlist'] !== "" ? $this->post['addrlist'] : "";
  617. if ($addrlist == "" || empty($addrlist) || !is_array($addrlist)) {
  618. return error_show(1004, "参数addrlist不能为空");
  619. }
  620. Db::startTrans();
  621. try {
  622. $i=1;
  623. foreach ($addrlist as $value) {
  624. $temp = [];
  625. $addrs = [];
  626. if ($value['addr_code'] !== '' && is_array($value['addr_code']) && !empty($value['addr_code'])) {
  627. $addrs['provice_code'] = $value['addr_code'][0];
  628. $addrs['city_code'] = $value['addr_code'][1];
  629. $addrs['area_code'] = $value['addr_code'][2];
  630. $addr = json_encode($addrs);
  631. } else {
  632. $addr = isset($value['addr_code']) && $value['addr_code'] !== '' ? $value['addr_code'] : '';
  633. }
  634. $temp['orderCode'] = $orderCode;
  635. $temp['contactor'] = $value['contactor'];
  636. $temp['mobile'] = $value['mobile'];
  637. $temp['addr'] = $value['addr'];
  638. $temp['addr_code'] = $addr;
  639. $temp['customer_code'] = $etid['customer_code'];
  640. $temp['receipt_quantity'] = $value['receipt_quantity'];
  641. $temp['post_fee'] = 0;
  642. $temp['is_del'] = 0;
  643. $temp['addtime'] = date("Y-m-d H:i:s");
  644. $temp['updatetime'] = date("Y-m-d H:i:s");
  645. if ($arrtime != "") {
  646. $temp['arrive_time'] = $arrtime;
  647. }
  648. $vmp = Db::name('order_addr')->insert($temp, true);
  649. if ($vmp > 0) {
  650. $num = $value['receipt_quantity'];
  651. $outCode = makeNo("DF");
  652. //维护商品所在仓库的库存 --- start
  653. if ($etid['is_stock'] != 1){
  654. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  655. ->find();
  656. if (empty($order) || $order['wsend_num'] < $num) {
  657. return error_show(1004, "未找到可以发货得采购单数据");
  658. }
  659. //改变编码规则,将原来的outCode后两位换成序列号
  660. //str_pad字符串填充
  661. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  662. $i++;
  663. $order['send_num'] += $num;
  664. $order['wsend_num'] -= $num;
  665. $or = Db::name("order_num")->save($order);
  666. if ($or == false) {
  667. Db::rollback();
  668. return error_show(1002, "发货地址添加创建失败");
  669. }
  670. $tep = [
  671. "cgdNo" => $order['cgdNo'],
  672. "outCode" => $outCode,
  673. "send_num" => $num,
  674. "status" => 1,
  675. "addtime" => date("Y-m-d H:i:s"),
  676. "updatetime" => date("Y-m-d H:i:s")
  677. ];
  678. $sen = Db::name("order_send")->save($tep);
  679. if ($sen == false) {
  680. Db::rollback();
  681. return error_show(1002, "发货地址添加创建失败");
  682. }
  683. $cgdinfo = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
  684. if ($cgdinfo == false) {
  685. Db::rollback();
  686. return error_show(1002, "未匹配到采购数据");
  687. }
  688. $temp = Db::name('good_stock')
  689. ->field('id,usable_stock,wait_out_stock')
  690. ->where(['spuCode' => $etid['spuCode'], 'wsm_code' => $cgdinfo['wsm_code']])
  691. ->find();
  692. //非库存品 导入地址生成发货单时占用库存 可用库存进入待出库
  693. if ($temp == false || ($temp['usable_stock'] - $num) < 0) {
  694. $status = 0;
  695. } else {
  696. if (($temp['usable_stock'] - $num) < 0) {
  697. Db::rollback();
  698. return error_show(1004, '库存不足');
  699. }
  700. $up = Db::name('good_stock')
  701. ->where('id', $temp['id'])
  702. ->update([
  703. 'usable_stock' => $temp['usable_stock'] - $num,
  704. 'wait_out_stock' => $temp['wait_out_stock'] + $num,
  705. 'updatetime' => date('Y-m-d H:i:s'),
  706. ]);
  707. if ($up == false) {
  708. Db::rollback();
  709. return error_show(1004, '库存更新失败');
  710. }
  711. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $temp['id'], "type" => 1, 'stock' => $num, "stock_name" => "wait_out_stock"];
  712. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $temp['id'], "type" => 2, 'stock' => $num, "stock_name" => "usable_stock"];
  713. GoodLog::LogAdd($this->post['token'], $good_data, "CKD");
  714. $status = 1;
  715. }
  716. }else{
  717. $stockid=Db::name("sale_info")->where(["orderCode"=>$orderCode])->value("stockid","");
  718. if($stockid==''){
  719. Db::rollback();
  720. return error_show(1005,"未找到对应库存仓库");
  721. }
  722. $cgdinfo = Db::name("good_stock")->where(["id"=>$stockid])->findOrEmpty();
  723. if(empty($cgdinfo)){
  724. Db::rollback();
  725. return error_show(1005,"未找到对应库存仓库");
  726. }
  727. }
  728. $out = [
  729. "orderCode" => $orderCode,
  730. "outCode" => $outCode,
  731. "apply_id" => $rm,
  732. "apply_name" => $ri,
  733. "addrid" => $vmp,
  734. "post_name" => "",
  735. "post_code" => "",
  736. "post_fee" => 0,
  737. "sendtime" => date("Y-m-d H:i:s"),
  738. "send_num" => $num,
  739. "check_num" => 0,
  740. "error_num" => 1,
  741. "wsm_code" => $cgdinfo['wsm_code'],
  742. "order_type" => $etid['order_type'],
  743. "status" => $status,
  744. "addtime" => date("Y-m-d H:i:s"),
  745. "updatetime" => date("Y-m-d H:i:s")
  746. ];
  747. $ou = Db::name("order_out")->insertGetId($out);
  748. if ($ou == false) {
  749. Db::rollback();
  750. return error_show(1002, "发货地址添加创建失败");
  751. } else {
  752. //修改状态,添加待办
  753. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
  754. "order_code" => $outCode,//出库单号
  755. "status" => 0,//这里的status是之前的值
  756. "action_remark" => '',//备注
  757. "action_type" => "create"//新建create,编辑edit,更改状态status
  758. ], "CKD", 0, $out);
  759. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], [
  760. "order_type" => 'CKD',
  761. "order_code" => $outCode,//出库单号
  762. "order_id" => $ou,
  763. "order_status" => $status,
  764. "before_status" => 0,
  765. 'holder_id'=>$out['apply_id'],
  766. ]);
  767. //处理台账-既然是根据orderCode更新的数据,那么肯定是有数据的,只需要更新就行了
  768. Db::execute("UPDATE `wsm_standing_book` SET `outCode`=CONCAT(IFNULL(`outCode`,''),',{$outCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$orderCode}'");
  769. }
  770. //维护商品所在仓库的库存 --- end
  771. } else {
  772. Db::rollback();
  773. return error_show(1002, "发货地址添加创建失败");
  774. }
  775. }
  776. // $OrderBn = GoodStockInfo::OrderBn($orderCode,$stock_id,$num);
  777. // if($OrderBn==false){
  778. // Db::rollback();
  779. // return error_show(1004,"Bn库存数关联订单失败");
  780. // }
  781. Db::commit();
  782. return app_show(0, "发货地址添加完成");
  783. } catch (\Exception $e) {
  784. Db::rollback();
  785. return error_show(1002, $e->getMessage());
  786. }
  787. }
  788. public function consultOrder()
  789. {
  790. $bidNo = isset($this->post['bidNo']) && $this->post['bidNo'] != "" ? trim($this->post['bidNo']) : "";
  791. if ($bidNo == "") {
  792. return error_show(1003, "参数bidNo不能为空");
  793. }
  794. $zxinfo = Db::name("consult_bids")->where(["bidNo" => $bidNo, "is_del" => 0])->find();
  795. if ($zxinfo == false) {
  796. return error_show(1003, "未找到咨询单商品信息");
  797. }
  798. if ($zxinfo['status'] != 6) {
  799. return error_show(1003, "咨询单状态有误无法转单");
  800. }
  801. $zx = Db::name("consult_info")->where(["infoNo" => $zxinfo['infoNo'], "is_del" => 0])->find();
  802. if ($zx == false) {
  803. return error_show(1003, "未找到咨询单信息");
  804. }
  805. if ($zx['status'] != 4) {
  806. return error_show(1003, "咨询单状态有误无法转单");
  807. }
  808. $zxorder = Db::name("consult_order")->where(["zxNo" => $zx["zxNo"], "is_del" => 0])->find();
  809. if ($zxorder == false) {
  810. return error_show(1003, "未找到咨询单信息");
  811. }
  812. $good_num = isset($this->post['good_num']) && $this->post['good_num'] != "" ? intval($this->post['good_num']) : "";
  813. if ($good_num === "") {
  814. return error_show(1003, "参数 good_num 不能为空");
  815. }
  816. $sale_price = isset($this->post['sale_price']) && $this->post['sale_price'] != "" ? floatval($this->post['sale_price']) : "";
  817. if ($sale_price === "") {
  818. return error_show(1003, "参数 sale_price 销售单价 不能为空");
  819. }
  820. if ($sale_price < $zxinfo['sale_price']) {
  821. return error_show(1003, "修改的销售单价不能低于原来的销售单价");
  822. }
  823. $sendtype = isset($this->post['sendtype']) && $this->post['sendtype'] != "" ? intval($this->post['sendtype']) : "";
  824. if ($sendtype == "") {
  825. return error_show(1003, "参数sendtype不能为空");
  826. }
  827. $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] != "" ? trim($this->post['companyNo']) : "";
  828. if ($companyNo == "") {
  829. return error_show(1003, "参数 companyNo 不能为空");
  830. }
  831. $company = Db::name("business")->where(["companyNo" => $companyNo, "is_del" => 0])->find();
  832. if ($company == false) {
  833. return error_show(1003, "业务公司数据未找到");
  834. }
  835. $remark = isset($this->post['remark']) && $this->post['remark'] != "" ? trim($this->post['remark']) : "";
  836. $orderCode = makeNo("QR");
  837. $spuCode = $zxinfo['spuCode'];
  838. $skuCode = "";
  839. $is_stock = 0;
  840. $cgd = [
  841. "supplierNo" => $zxinfo['supplierNo'],
  842. "companyNo" => $companyNo,
  843. "spuCode" => $spuCode,
  844. "skuCode" => $skuCode,
  845. "orderCode" => $orderCode,
  846. "good_name" => $zxinfo['good_name'],
  847. "sale_price" => $zxinfo['total_fee'],
  848. "total_fee" => round($zxinfo['total_fee'] * $good_num, 2),
  849. "pakge_fee" => $zxinfo['pakge_fee'],
  850. "cert_fee" => $zxinfo['cert_fee'],
  851. "open_fee" => $zxinfo['open_fee'],
  852. "cost_fee" => $zxinfo['cost_fee'],
  853. "mark_fee" => $zxinfo['mark_fee'],
  854. "demo_fee" => $zxinfo['demo_fee'],
  855. "nake_fee" => $zxinfo['nake_fee'],
  856. "delivery_fee" => $zxinfo['delivery_fee'],
  857. "good_num" => $good_num,
  858. "createrid" => $zxinfo['createrid'],
  859. "creater" => $zxinfo['creater'],
  860. "good_type" => 1,
  861. "weight" => $zxinfo['good_weight'],
  862. "gold_price" => $zxinfo['gold_price'],
  863. "is_diff" => $zxinfo['is_diff'],
  864. "order_type" => 3,
  865. "order_source" => 2,//咨询
  866. 'send_way' => $zxinfo['send_way'],
  867. 'good_createrid' => $zxinfo['createrid'],
  868. 'good_creater' => $zxinfo['creater'],//商品创建人id
  869. ];
  870. $token = isset($this->post['token']) && $this->post['token'] != "" ? trim($this->post['token']) : "";
  871. if ($token == "") {
  872. return error_show(102, "参数token不能为空");
  873. }
  874. $apply_id = GetUserInfo($token);
  875. if (empty($apply_id) || $apply_id['code'] != 0) {
  876. return error_show(102, "申请人数据不存在");
  877. }
  878. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  879. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  880. $va = isset($this->post['order_addr']) && $this->post['order_addr'] !== "" ? $this->post['order_addr'] : "";
  881. $send_num = 0;
  882. if ($sendtype == 1) {
  883. if ($va == "") {
  884. return error_show(1002, "参数order_addr不能为空");
  885. }
  886. $send_num = array_sum(array_column($va, "receipt_quantity"));
  887. }
  888. if ($good_num < $send_num) {
  889. return error_show(1002, "发货数量不能超过购买数量");
  890. }
  891. $arrtime = isset($this->post['arrtime']) && $this->post['arrtime'] != "" ? $this->post['arrtime'] : "";
  892. if ($arrtime == "") {
  893. return error_show(1002, "参数arrtime不能为空");
  894. }
  895. //新加字段
  896. $platform_order = isset($this->post['platform_order']) && $this->post['platform_order'] != "" ? $this->post['platform_order'] : "";
  897. $workNo = isset($this->post['workNo']) && $this->post['workNo'] != "" ? $this->post['workNo'] : "";
  898. $proof_type = isset($this->post['proof_type']) && $this->post['proof_type'] != "" ? intval($this->post['proof_type']) : 0;
  899. $proof_url = isset($this->post['proof_url']) && $this->post['proof_url'] != "" ? trim($this->post['proof_url']) : '';
  900. Db::startTrans();
  901. try {
  902. $zx = Db::name("consult_info")->where(["infoNo" => $zxinfo['infoNo'], "is_del" => 0])->lock(true)->find();
  903. if ($zx == false) {
  904. Db::rollback();
  905. return error_show(1003, "未找到咨询单信息");
  906. }
  907. if ($companyNo != $zxorder['companyNo']) {
  908. $zxord = Db::name("consult_order")->where($zxorder)->update(["companyNo" => $companyNo, "updatetime" => date("Y-m-d H:i:s")]);
  909. if ($zxord == false) {
  910. Db::rollback();
  911. return error_show(1003, "咨询单更新业务公司失败");
  912. }
  913. }
  914. $supplier_temp_info=Db::name("supplier")
  915. ->field('id,person,personid')
  916. ->where(["code"=>$zxinfo['supplierNo']])
  917. ->findOrEmpty();
  918. $data = [
  919. "orderCode" => $orderCode,
  920. "good_code" => $spuCode,
  921. "skuCode" => $skuCode,
  922. "customer_code" => $zxorder['khNo'],
  923. "good_name" => isset($zxinfo['good_name']) && $zxinfo['good_name'] !== '' ? $zxinfo['good_name'] : '',
  924. "good_num" => $good_num,
  925. "cat_id" => $zxinfo['cat_id'],
  926. "apply_id" => $rm,
  927. "apply_name" => $ri,
  928. "origin_price" => $zxinfo['total_fee'],
  929. "sale_price" => $sale_price,//销售单价支持修改
  930. "post_fee" => 0,
  931. "status" => 0,
  932. "send_num" => 0,
  933. "wsend_num" => $good_num,
  934. "send_status" => 1,
  935. "good_type" => 1,
  936. "send_type" => $sendtype,
  937. "supplierNo" => $companyNo,
  938. "is_del" => 0,
  939. "zxNo" => $bidNo,
  940. "platform_order" => $platform_order,
  941. "platform_id" => $zxorder['platform_code'],
  942. "remark" => $remark,
  943. "arrive_time" => $arrtime,
  944. "is_stock" => 0,
  945. "is_activity" => 0,
  946. "activity_code" => '',
  947. "proof_id" => 0,
  948. "order_type" => 3,
  949. "order_source" => 2,//2咨询
  950. 'good_weight' => $zxinfo['good_weight'],
  951. 'gold_price' => $zxinfo['gold_price'],
  952. 'cost_price' => $zxinfo['sale_cost_fee'],
  953. 'diff_weight' => 0,
  954. 'diff_fee' => 0,
  955. "addtime" => date("Y-m-d H:i:s"),
  956. "updatetime" => date("Y-m-d H:i:s"),
  957. 'total_price' => round($sale_price * $good_num, 2),
  958. 'workNo' => $workNo,
  959. 'cgderid' => $supplier_temp_info['personid'],
  960. 'cgder' => $supplier_temp_info['person'],//采购员(采购单对应的供应商负责人)
  961. 'good_createrid' => $zxinfo['createrid'],
  962. 'good_creater' => $zxinfo['creater'],//商品创建人id
  963. ];
  964. $datainfo = Db::name('sale')->insert($data, true);
  965. if ($datainfo > 0) {
  966. $standing_book_data = [
  967. 'sale_id' => $datainfo,
  968. 'infoNo' => $zxinfo['infoNo'],
  969. 'updatetime' => date('Y-m-d H:i:s')
  970. ];
  971. //修改状态,添加待办
  972. ActionLog::logAdd($this->post['token'], [
  973. "order_code" => $orderCode,//销售单code
  974. "status" => 0,//这里的status是之前的值
  975. "action_remark" => '',//备注
  976. "action_type" => "create"//新建create,编辑edit,更改状态status
  977. ], "XSQRD", $data['status'], $data);
  978. ProcessOrder::AddProcess($this->post['token'], [
  979. "order_type" => 'XSQRD',
  980. "order_code" => $orderCode,//销售单code
  981. "order_id" => $datainfo,
  982. "order_status" => $data['status'],
  983. "before_status" => 0,
  984. 'holder_id' => $data['apply_id']
  985. ]);
  986. $old_zx_status = $zx['status'];
  987. $sa = Db::name("consult_info")->where($zx)->save(["status" => 5, "updatetime" => date("Y-m-d H:i:s")]);
  988. if ($sa == false) {
  989. Db::rollback();
  990. return error_show(1002, "咨询单修改失败");
  991. } else {
  992. //修改状态,添加待办
  993. ActionLog::logAdd($this->post['token'], [
  994. "order_code" => $zx['infoNo'],//咨询单详情编号
  995. "status" => $old_zx_status,//这里的status是之前的值
  996. "action_remark" => '',//备注
  997. "action_type" => "status"//新建create,编辑edit,更改状态status
  998. ], "ZXD", 5, $zx);
  999. }
  1000. $bol = $this->createCgd($cgd, $standing_book_data);
  1001. if ($bol == false) {
  1002. Db::rollback();
  1003. return error_show(1002, "咨询订单创建失败");
  1004. }
  1005. $limt = [
  1006. "spuCode" => $zxinfo['spuCode'],
  1007. "good_name" => $zxinfo['good_name'],
  1008. "brand_id" => $zxinfo['brand_id'],
  1009. "good_unit" => $zxinfo['unit_id'],
  1010. "cat_id" => $zxinfo['cat_id'],
  1011. "good_type" => 0,
  1012. "moq" => 1,
  1013. "customized" => $zxinfo['work_day'],
  1014. "tax" => $zxinfo['tax'],
  1015. "supplierNo" => $zxinfo["supplierNo"],
  1016. "is_auth" => 0,
  1017. "craft_desc" => $zxinfo['good_name'],
  1018. "good_remark" => "",
  1019. "platform_id" => $zxorder['platform_id'],
  1020. "good_img" => $zxinfo['good_img'],
  1021. "good_thumb_img" => "",
  1022. "good_info_img" => "",
  1023. "specinfo" => $zxinfo['specinfo'],
  1024. "work_day" => $zxinfo['work_day'],
  1025. "noble_metal" => $zxinfo['metal_id'],
  1026. "is_gold_price" => $zxinfo['is_gold_price'],
  1027. "config" => $zxinfo['config'],
  1028. "other_config" => $zxinfo['other_config'],
  1029. "weight" => $zxinfo['weight'],
  1030. "good_weight" => $zxinfo['good_weight'],
  1031. "is_diff" => $zxinfo['is_diff'],
  1032. "supply_area" => $zxinfo['supply_area'],
  1033. "pay_way" => $zxinfo['pay_way'],
  1034. "send_way" => $zxinfo['send_way'],
  1035. "companyNo" => $companyNo,
  1036. "status" => 1,
  1037. "is_del" => 0,
  1038. "createrid" => $zxinfo['createrid'],
  1039. "creater" => $zxinfo['creater'],
  1040. "addtime" => date("Y-m-d H:i:s"),
  1041. "updatetime" => date("Y-m-d H:i:s"),
  1042. 'proof_type' => $proof_type,//凭证类型
  1043. 'proof_url' => $proof_url,//凭证文件
  1044. ];
  1045. $good = Db::name("good_zixun")->insert($limt);
  1046. if (!$good) {
  1047. Db::rollback();
  1048. return error_show(1006, "咨询商品录入失败");
  1049. }
  1050. if (!empty($va)) {
  1051. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  1052. ->find();
  1053. if ($order == false) {
  1054. Db::rollback();
  1055. return error_show(1006, "采购单关联数据未找到");
  1056. }
  1057. $i = 1;
  1058. foreach ($va as $value) {
  1059. $temp = [];
  1060. $addrs = [];
  1061. if ($value['addr_code'] !== '' && is_array($value['addr_code']) && !empty($value['addr_code'])) {
  1062. $addrs['provice_code'] = $value['addr_code'][0];
  1063. $addrs['city_code'] = $value['addr_code'][1];
  1064. $addrs['area_code'] = $value['addr_code'][2];
  1065. $addr = json_encode($addrs);
  1066. } else {
  1067. $addr = isset($value['addr_code']) ? $value['addr_code'] : '';
  1068. }
  1069. $temp['orderCode'] = $orderCode;
  1070. $temp['contactor'] = $value['contactor'];
  1071. $temp['mobile'] = $value['mobile'];
  1072. $temp['addr'] = $value['addr'];
  1073. $temp['addr_code'] = $addr;
  1074. $temp['customer_code'] = $zxorder['khNo'];
  1075. $temp['receipt_quantity'] = $value['receipt_quantity'];
  1076. $temp['post_fee'] = 0;
  1077. $temp['is_del'] = 0;
  1078. $temp['addtime'] = date("Y-m-d H:i:s");
  1079. $temp['updatetime'] = date("Y-m-d H:i:s");
  1080. $temp['arrive_time'] = $arrtime;
  1081. $vmp = Db::name('order_addr')->insert($temp, true);
  1082. if ($vmp > 0) {
  1083. $outCode = makeNo("DF");
  1084. //改变编码规则,将原来的outCode后两位换成序列号
  1085. //str_pad字符串填充
  1086. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  1087. $i++;
  1088. $out = [
  1089. "orderCode" => $orderCode,
  1090. "outCode" => $outCode,
  1091. "apply_id" => $rm,
  1092. "apply_name" => $ri,
  1093. "addrid" => $vmp,
  1094. "post_name" => "",
  1095. "post_code" => "",
  1096. "post_fee" => 0,
  1097. "sendtime" => date("Y-m-d H:i:s"),
  1098. "send_num" => $value['receipt_quantity'],
  1099. "check_num" => 0,
  1100. "error_num" => 0,
  1101. "wsm_code" => "",
  1102. "order_type" => 3,
  1103. "status" => 0,
  1104. "addtime" => date("Y-m-d H:i:s"),
  1105. "updatetime" => date("Y-m-d H:i:s")
  1106. ];
  1107. $ou = Db::name("order_out")->insertGetId($out);
  1108. if ($ou == false) {
  1109. Db::rollback();
  1110. return error_show(1002, "发货地址添加创建失败");
  1111. } else {
  1112. //修改状态,添加待办
  1113. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
  1114. "order_code" => $outCode,//出库单号
  1115. "status" => 0,//这里的status是之前的值
  1116. "action_remark" => '',//备注
  1117. "action_type" => "create"//新建create,编辑edit,更改状态status
  1118. ], "CKD", 0, $out);
  1119. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], [
  1120. "order_type" => 'CKD',
  1121. "order_code" => $outCode,//出库单号
  1122. "order_id" => $ou,
  1123. "order_status" => 0,
  1124. "before_status" => 0,
  1125. 'holder_id'=>$out['apply_id'],
  1126. ]);
  1127. //将发货编号添加到台账中
  1128. $standing_book_data['outCode'][] = $outCode;
  1129. }
  1130. $order['send_num'] += $value['receipt_quantity'];
  1131. $order['wsend_num'] -= $value['receipt_quantity'];
  1132. if ($order['wsend_num'] < 0) {
  1133. Db::rollback();
  1134. return error_show(1002, "发货数量已超出总数");
  1135. }
  1136. $ups = Db::name("order_num")->save($order);
  1137. if ($ups) {
  1138. $tep = [
  1139. "cgdNo" => $order['cgdNo'],
  1140. "outCode" => $outCode,
  1141. "send_num" => $value['receipt_quantity'],
  1142. "status" => 1,
  1143. "addtime" => date("Y-m-d H:i:s"),
  1144. "updatetime" => date("Y-m-d H:i:s")
  1145. ];
  1146. $sen = Db::name("order_send")->save($tep);
  1147. if ($sen == false) {
  1148. Db::rollback();
  1149. return error_show(1002, "发货地址添加创建失败");
  1150. }
  1151. }
  1152. } else {
  1153. Db::rollback();
  1154. return error_show(1002, "发货地址添加创建失败");
  1155. }
  1156. }
  1157. }
  1158. //处理台账
  1159. if (isset($standing_book_data['outCode'])) {
  1160. Db::execute("UPDATE `wsm_standing_book` SET `outCode`=CONCAT(IFNULL(`outCode`,''),'," . implode(',', $standing_book_data['outCode']) . "'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `infoNo`='{$standing_book_data['infoNo']}'");
  1161. unset($standing_book_data['outCode']);
  1162. }
  1163. Db::name('standing_book')->where('infoNo', $standing_book_data['infoNo'])->update($standing_book_data);
  1164. Db::commit();
  1165. return error_show(0, "咨询订单创建成功", ["order_code" => $orderCode]);
  1166. }
  1167. Db::rollback();
  1168. return error_show(1002, "咨询订单创建失败");
  1169. } catch (\Exception $e) {
  1170. Db::rollback();
  1171. return error_show(1003, $e->getMessage());
  1172. }
  1173. }
  1174. /**
  1175. * @return \think\response\Json|void
  1176. * @throws \think\db\exception\DataNotFoundException
  1177. * @throws \think\db\exception\DbException
  1178. * @throws \think\db\exception\ModelNotFoundException
  1179. */
  1180. public function projectCreate()
  1181. {
  1182. $planNo = isset($this->post['planNo']) && $this->post['planNo'] != "" ? trim($this->post['planNo']) : "";
  1183. if ($planNo == "") {
  1184. return error_show(1004, "参数planNo不能为空");
  1185. }
  1186. $plan = Db::name("project_plan")->where(["planNo" => $planNo, "is_del" => 0])->find();
  1187. if ($plan == false) {
  1188. return error_show(1004, "未找到方案信息");
  1189. }
  1190. $feedbackid = Db::name("project_plan_rela")->where(["planNo" => $planNo, "is_del" => 0])->column("feedback_id,sale_price,sale_num");
  1191. if ($feedbackid == false) {
  1192. return error_show(1004, "未找到方案反馈信息");
  1193. }
  1194. $project = Db::name("project")->where(["projectNo" => $plan['projectNo'], "is_del" => 0])->find();
  1195. if ($project == false) {
  1196. return error_show(1004, "未找到方案信息");
  1197. }
  1198. $arrtime = isset($this->post['arrtime']) && $this->post['arrtime'] != "" ? $this->post['arrtime'] : "";
  1199. if ($arrtime == "") {
  1200. return error_show(1002, "参数arrtime不能为空");
  1201. }
  1202. $planinfo = isset($this->post['planinfo']) && !empty($this->post['planinfo']) ? $this->post['planinfo'] : "";
  1203. if ($planinfo == "") {
  1204. return error_show(1002, "参数planinfo不能为空");
  1205. }
  1206. $token = isset($this->post['token']) && $this->post['token'] != "" ? trim($this->post['token']) : "";
  1207. if ($token == "") {
  1208. return error_show(102, "参数token不能为空");
  1209. }
  1210. $apply_id = GetUserInfo($token);
  1211. if (empty($apply_id) || $apply_id['code'] != 0) {
  1212. return error_show(102, "申请人数据不存在");
  1213. }
  1214. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  1215. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  1216. Db::startTrans();
  1217. try {
  1218. $plan = Db::name("project_plan")->where(["planNo" => $planNo, "is_del" => 0])->lock(true)->find();
  1219. if ($plan == false) {
  1220. Db::rollback();
  1221. return error_show(1004, "未找到方案信息");
  1222. }
  1223. $feedid = array_column($feedbackid, "feedback_id");
  1224. foreach ($planinfo as $key => $value) {
  1225. $good = [];
  1226. $feedback = Db::name("project_feedback")->where(["id" => $value['feedback_id'], "projectNo" => $plan['projectNo'],
  1227. "is_del" => 0])->find();
  1228. if ($feedback == false) {
  1229. Db::rollback();
  1230. return error_show(1002, "反馈数据未找到");
  1231. }
  1232. $key = array_search($feedback['id'], $feedid);
  1233. $price = $feedbackid[$key];
  1234. $good['spuCode'] = $feedback['spuCode'];
  1235. $good['skuCode'] = $feedback['skuCode'];
  1236. $good['good_num'] = $price['sale_num'];
  1237. $good['sendtype'] = $value['sendtype'];
  1238. $good['sale_price'] = $price['sale_price'];
  1239. $good['origin_price'] = $feedback['origin_price'];
  1240. $good['companyNo'] = $project['companyNo'];
  1241. $good['customerNo'] = $project['khNo'];
  1242. $good['pgNo'] = $feedback['pgNo'];
  1243. $good['platform_id'] = $project['platform_id'];
  1244. $good['arrtime'] = $arrtime;
  1245. $good['rm'] = $rm;
  1246. $good['ri'] = $ri;
  1247. $good['send_num'] = 0;
  1248. $standing_book_data = ['projectNo' => $plan['projectNo'], 'updatetime' => date('Y-m-d H:i:s')];
  1249. if ($value['sendtype'] == 1) {
  1250. if (!isset($value['addrlist']) || empty($value['addrlist'])) {
  1251. Db::rollback();
  1252. return error_show(1002, "订单地址不能为空");
  1253. }
  1254. $good['addrlist'] = $value['addrlist'];
  1255. $good['send_num'] = array_sum(array_column($value['addrlist'], "receipt_quantity"));
  1256. }
  1257. if ($feedback['data_source'] == 1) {
  1258. $feed = $this->project_good($good, $standing_book_data);
  1259. if ($feed !== true) {
  1260. Db::rollback();
  1261. switch ($feed) {
  1262. case 1:
  1263. $msg = '库存不足';
  1264. break;
  1265. case 2:
  1266. $msg = '库存更新失败';
  1267. break;
  1268. case 3:
  1269. $msg = '关联采购失败';
  1270. break;
  1271. case 4:
  1272. $msg = '商品信息未找到';
  1273. break;
  1274. default:
  1275. $msg = '咨询订单生成失败';
  1276. }
  1277. return error_show(1002, $msg);
  1278. }
  1279. } else {
  1280. $feed = $this->project_zx($good, $standing_book_data);
  1281. if ($feed == false) {
  1282. Db::rollback();
  1283. return error_show(1002, "咨询订单生成失败");
  1284. }
  1285. }
  1286. $old_fk_status = $feedback['status'];
  1287. $fed = Db::name("project_feedback")->where($feedback)->save(['status' => 2, "updatetime" => date("Y-m-d H:i:s")]);
  1288. if ($fed == false) {
  1289. Db::rollback();
  1290. return error_show(1002, "项目订单生成失败");
  1291. }
  1292. //处理台账(一个项目会生成多个销售订单,第一个更新,其余的新增)
  1293. //项目创建时已有供应商和企业,不能修改
  1294. if (isset($standing_book_data['companyNo'])) unset($standing_book_data['companyNo']);
  1295. if (isset($standing_book_data['customer_code'])) unset($standing_book_data['customer_code']);
  1296. if (isset($standing_book_data['outCode'])) $standing_book_data['outCode'] = implode(',', $standing_book_data['outCode']);
  1297. //普通商品
  1298. if ($feedback['data_source'] == 1) {
  1299. if ($key == 0) Db::name('standing_book')->where('projectNo', $plan['projectNo'])->update($standing_book_data);
  1300. else {
  1301. $temp = Db::name('standing_book')->where('projectNo', $plan['projectNo'])->find();
  1302. if (!empty($temp)) {
  1303. unset($temp['id']);
  1304. $temp = array_merge($temp, $standing_book_data);
  1305. $temp['standBookNo'] = makeNo('IO');
  1306. Db::name('standing_book')->insert($temp);
  1307. }
  1308. }
  1309. } else {
  1310. //项目类竞价单
  1311. Db::name('standing_book')->where(['projectNo' => $plan['projectNo'], 'infoNo' => $standing_book_data['infoNo']])->update($standing_book_data);
  1312. }
  1313. }
  1314. $old_plan_status = $plan['status'];
  1315. // $plan['status']=2;
  1316. // $plan['updatetime']=date("Y-m-d H:i:s");
  1317. $in = Db::name("project_plan")->where($plan)->save(['status' => 2, "updatetime" => date("Y-m-d H:i:s")]);
  1318. if ($in == false) {
  1319. Db::rollback();
  1320. return error_show(1002, "项目订单生成失败");
  1321. } else {
  1322. //修改状态,添加待办,只记录动作
  1323. ActionLog::logAdd($this->post['token'], [
  1324. "order_code" => $planNo,//编码
  1325. "status" => $old_plan_status,//这里的status是之前的值
  1326. "action_remark" => '',//备注
  1327. "action_type" => "edit"//新建create,编辑edit,更改状态status
  1328. ], "PRP", 2, $plan);
  1329. }
  1330. $old_project_status = $project["status"];
  1331. // $project["status"]=6;
  1332. // $project['updatetime']=date("Y-m-d H:i:s");
  1333. $pr = Db::name("project")->where($project)->save(["updatetime" => date("Y-m-d H:i:s"), "status" => 6]);
  1334. if ($pr == false) {
  1335. Db::rollback();
  1336. return error_show(1002, "项目订单生成失败");
  1337. } else {
  1338. //修改状态,添加待办
  1339. ActionLog::logAdd($this->post['token'], [
  1340. "order_code" => $project['projectNo'],//项目编码
  1341. "status" => $old_project_status,//这里的status是之前的值
  1342. "action_remark" => '',//备注
  1343. "action_type" => "status"//新建create,编辑edit,更改状态status
  1344. ], "PRO", 6, $project);
  1345. ProcessOrder::AddProcess($this->post['token'], [
  1346. "order_type" => 'PRO',
  1347. "order_code" => $project['projectNo'],
  1348. "order_id" => $project['id'],
  1349. "order_status" => 6,
  1350. "before_status" => $old_project_status,
  1351. 'holder_id'=>$project['createrid']
  1352. ]);
  1353. }
  1354. //修改状态,添加待办,只记录动作
  1355. ActionLog::logAdd($this->post['token'], [
  1356. "order_code" => $project['projectNo'],//编码,这里是project的编码
  1357. "status" => 1,//这里的status是之前的值
  1358. "action_remark" => '',//备注
  1359. "action_type" => "create"//新建create,编辑edit,更改状态status
  1360. ], "PRT", 2, $this->post);
  1361. $process = ["order_code" => $project['projectNo'], "order_id" => $project['id'], "order_status" => 2, "order_type" => "PRT", "before_status" => 1];
  1362. ProcessOrder::AddProcess($this->post['token'], $process);
  1363. Db::commit();
  1364. return app_show(0, "咨询订单生成成功");
  1365. } catch (\Exception $e) {
  1366. Db::rollback();
  1367. return error_show(1002, $e->getMessage());
  1368. }
  1369. }
  1370. /**
  1371. * @param $data
  1372. * @return false
  1373. * @throws \think\db\exception\DataNotFoundException
  1374. * @throws \think\db\exception\DbException
  1375. * @throws \think\db\exception\ModelNotFoundException
  1376. *
  1377. */
  1378. public function project_zx($data, array &$standing_book_data = [])
  1379. {
  1380. $goodinfo = Db::name("consult_bids")->where(["spuCode" => $data['spuCode'], "is_del" => 0])->find();
  1381. if ($goodinfo == false) {
  1382. return false;
  1383. }
  1384. $orderCode = makeNo("QR");
  1385. $cgd = [
  1386. "supplierNo" => $goodinfo['supplierNo'],
  1387. "companyNo" => $data['companyNo'],
  1388. "spuCode" => $data['spuCode'],
  1389. "skuCode" => "",
  1390. "orderCode" => $orderCode,
  1391. "good_name" => $goodinfo['good_name'],
  1392. "sale_price" => $goodinfo['total_fee'],
  1393. "total_fee" => round($goodinfo['total_fee'] * $data['good_num'], 2),
  1394. "pakge_fee" => $goodinfo['pakge_fee'],
  1395. "cert_fee" => $goodinfo['cert_fee'],
  1396. "open_fee" => $goodinfo['open_fee'],
  1397. "cost_fee" => $goodinfo['cost_fee'],
  1398. "mark_fee" => $goodinfo['mark_fee'],
  1399. "demo_fee" => $goodinfo['demo_fee'],
  1400. "nake_fee" => $goodinfo['nake_fee'],
  1401. "good_num" => $data['good_num'],
  1402. "delivery_fee" => $goodinfo['delivery_fee'],
  1403. "createrid" => $goodinfo['createrid'],
  1404. "creater" => $goodinfo['creater'],
  1405. "weight" => $goodinfo['good_weight'],
  1406. "gold_price" => $goodinfo['gold_price'],
  1407. "good_type" => 1,
  1408. "order_type" => 3,//3咨询商品
  1409. "order_source" => 3,//项目
  1410. "send_way" => $goodinfo['send_way'],
  1411. 'good_createrid' => $goodinfo['createrid'],
  1412. 'good_creater' => $goodinfo['creater'],//商品创建人
  1413. ];
  1414. $supplier_temp_info = Db::name('supplier')
  1415. ->field('id,person,personid')
  1416. ->where('code',$goodinfo['supplierNo'])
  1417. ->findOrEmpty();
  1418. $datas = [
  1419. "orderCode" => $orderCode,
  1420. "good_code" => $data['spuCode'],
  1421. "skuCode" => "",
  1422. "customer_code" => $data['customerNo'],
  1423. "good_name" => isset($goodinfo['good_name']) && $goodinfo['good_name'] !== '' ? $goodinfo['good_name'] : '',
  1424. "good_num" => $data['good_num'],
  1425. "cat_id" => $goodinfo['cat_id'],
  1426. "apply_id" => $data['rm'],
  1427. "apply_name" => $data['ri'],
  1428. "origin_price" => $goodinfo['total_fee'],
  1429. "sale_price" => $data['sale_price'],
  1430. "post_fee" => 0,
  1431. "status" => 0,
  1432. "send_num" => 0,
  1433. "wsend_num" => $data['good_num'],
  1434. "send_status" => 1,
  1435. "good_type" => 1,
  1436. "send_type" => $data['sendtype'],
  1437. "supplierNo" => $data['companyNo'],
  1438. "is_del" => 0,
  1439. "zxNo" => $data["pgNo"],
  1440. "platform_order" => "",
  1441. "platform_id" => $data['platform_id'],
  1442. "remark" => "",
  1443. "is_stock" => 0,
  1444. "is_activity" => 0,
  1445. "activity_code" => '',
  1446. "proof_id" => 0,
  1447. "order_type" => 3,//咨询商品
  1448. "order_source" => 3,//项目
  1449. 'good_weight' => $goodinfo['good_weight'],
  1450. 'gold_price' => $goodinfo['gold_price'],
  1451. 'cost_price' => $goodinfo['sale_cost_fee'],
  1452. 'diff_weight' => 0,
  1453. 'diff_fee' => 0,
  1454. "addtime" => date("Y-m-d H:i:s"),
  1455. "updatetime" => date("Y-m-d H:i:s"),
  1456. 'total_price' => round($data['sale_price'] * $data['good_num'], 2),
  1457. 'cgderid' => $supplier_temp_info['personid'],
  1458. 'cgder' => $supplier_temp_info['person'],//采购员(供应商负责人)
  1459. 'good_createrid' => $goodinfo['createrid'],
  1460. 'good_creater' => $goodinfo['creater'],//商品创建人
  1461. ];
  1462. $datainfo = Db::name('sale')->insert($datas, true);
  1463. if ($datainfo > 0) {
  1464. $standing_book_data['sale_id'] = $datainfo;
  1465. $standing_book_data['customer_code'] = $datas['customer_code'];
  1466. $standing_book_data['infoNo'] = $goodinfo['infoNo'];
  1467. //修改状态,添加待办
  1468. ActionLog::logAdd($this->post['token'], [
  1469. "order_code" => $orderCode,//项目编码
  1470. "status" => 0,//这里的status是之前的值
  1471. "action_remark" => '',//备注
  1472. "action_type" => "status"//新建create,编辑edit,更改状态status
  1473. ], "XSQRD", 0, $datas);
  1474. ProcessOrder::AddProcess($this->post['token'], [
  1475. "order_type" => 'XSQRD',
  1476. "order_code" => $orderCode,
  1477. "order_id" => $datainfo,
  1478. "order_status" => 0,
  1479. "before_status" => 0,
  1480. 'holder_id' => $datas['apply_id']
  1481. ]);
  1482. $bol = $this->createCgd($cgd, $standing_book_data);
  1483. if ($bol == false) {
  1484. return false;
  1485. }
  1486. $bidstatus = $goodinfo['status'];
  1487. $goodinfo['status'] = 6;
  1488. $goodinfo['updatetime'] = date("Y-m-d H:i:s");
  1489. $sa = Db::name("consult_bids")->save($goodinfo);
  1490. if ($sa == false) {
  1491. return false;
  1492. }
  1493. //修改状态,添加待办
  1494. ActionLog::logAdd($this->post['token'], [
  1495. "order_code" => $goodinfo['bidNo'],//项目编码
  1496. "status" => $bidstatus,//这里的status是之前的值
  1497. "action_remark" => '',//备注
  1498. "action_type" => "status"//新建create,编辑edit,更改状态status
  1499. ], "FKD", 6, $datas);
  1500. $zxorder = Db::name("consult_order")->where(["zxNo" => $goodinfo["zxNo"], "is_del" => 0])->find();
  1501. if ($zxorder == false) {
  1502. return false;
  1503. }
  1504. $limt = [
  1505. "spuCode" => $goodinfo['spuCode'],
  1506. "good_name" => $goodinfo['good_name'],
  1507. "brand_id" => $goodinfo['brand_id'],
  1508. "good_unit" => $goodinfo['unit_id'],
  1509. "cat_id" => $goodinfo['cat_id'],
  1510. "good_type" => 0,
  1511. "moq" => 1,
  1512. "customized" => $goodinfo['work_day'],
  1513. "tax" => $goodinfo['tax'],
  1514. "supplierNo" => $goodinfo["supplierNo"],
  1515. "is_auth" => 0,
  1516. "craft_desc" => $goodinfo['good_name'],
  1517. "good_remark" => "",
  1518. "good_img" => $goodinfo['good_img'],
  1519. "good_thumb_img" => "",
  1520. "good_info_img" => "",
  1521. "platform_id" => $zxorder['platform_id'],
  1522. "specinfo" => $goodinfo['specinfo'],
  1523. "work_day" => $goodinfo['work_day'],
  1524. "noble_metal" => $goodinfo['metal_id'],
  1525. "is_gold_price" => $goodinfo['is_gold_price'],
  1526. "good_weight" => $goodinfo['good_weight'],
  1527. "companyNo" => $zxorder['companyNo'],
  1528. "config" => $goodinfo['config'],
  1529. "other_config" => $goodinfo['other_config'],
  1530. "weight" => $goodinfo['weight'],
  1531. "is_diff" => $goodinfo['is_diff'],
  1532. "supply_area" => $goodinfo['supply_area'],
  1533. "pay_way" => $goodinfo['pay_way'],
  1534. "send_way" => $goodinfo['send_way'],
  1535. "status" => 1,
  1536. "is_del" => 0,
  1537. "createrid" => $goodinfo['createrid'],
  1538. "creater" => $goodinfo['creater'],
  1539. "addtime" => date("Y-m-d H:i:s"),
  1540. "updatetime" => date("Y-m-d H:i:s")
  1541. ];
  1542. $good = Db::name("good_zixun")->insert($limt);
  1543. if (!$good) {
  1544. return false;
  1545. }
  1546. if ($data['sendtype'] == 1 && !empty($data['addrlist'])) {
  1547. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  1548. ->find();
  1549. if ($order == false) {
  1550. return false;
  1551. }
  1552. $i = 1;
  1553. foreach ($data['addrlist'] as $value) {
  1554. $temp = [];
  1555. $addrs = [];
  1556. if ($value['addr_code'] !== '' && is_array($value['addr_code']) && !empty($value['addr_code'])) {
  1557. $addrs['provice_code'] = $value['addr_code'][0];
  1558. $addrs['city_code'] = $value['addr_code'][1];
  1559. $addrs['area_code'] = $value['addr_code'][2];
  1560. $addr = json_encode($addrs);
  1561. } else {
  1562. $addr = isset($value['addr_code']) ? $value['addr_code'] : '';
  1563. }
  1564. $temp['orderCode'] = $orderCode;
  1565. $temp['contactor'] = $value['contactor'];
  1566. $temp['mobile'] = $value['mobile'];
  1567. $temp['addr'] = $value['addr'];
  1568. $temp['addr_code'] = $addr;
  1569. $temp['customer_code'] = $data['companyNo'];
  1570. $temp['receipt_quantity'] = $value['receipt_quantity'];
  1571. $temp['post_fee'] = 0;
  1572. $temp['is_del'] = 0;
  1573. $temp['addtime'] = date("Y-m-d H:i:s");
  1574. $temp['updatetime'] = date("Y-m-d H:i:s");
  1575. $temp['arrive_time'] = $data['arrtime'];
  1576. $vmp = Db::name('order_addr')->insert($temp, true);
  1577. if ($vmp > 0) {
  1578. $outCode = makeNo("DF");
  1579. //改变编码规则,将原来的outCode后两位换成序列号
  1580. //str_pad字符串填充
  1581. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  1582. $i++;
  1583. $out = [
  1584. "orderCode" => $orderCode,
  1585. "outCode" => $outCode,
  1586. "apply_id" => $data['rm'],
  1587. "apply_name" => $data['ri'],
  1588. "addrid" => $vmp,
  1589. "post_name" => "",
  1590. "post_code" => "",
  1591. "post_fee" => 0,
  1592. "sendtime" => date("Y-m-d H:i:s"),
  1593. "send_num" => $value['receipt_quantity'],
  1594. "check_num" => 0,
  1595. "error_num" => 0,
  1596. "wsm_code" => "",
  1597. "order_type" => 3,//3咨询商品
  1598. "status" => 0,
  1599. "addtime" => date("Y-m-d H:i:s"),
  1600. "updatetime" => date("Y-m-d H:i:s")
  1601. ];
  1602. $ou = Db::name("order_out")->insertGetId($out);
  1603. if ($ou == false) {
  1604. return false;
  1605. }
  1606. //修改状态,添加待办
  1607. ActionLog::logAdd($this->post['token'], [
  1608. "order_code" => $outCode,//项目编码
  1609. "status" => 0,//这里的status是之前的值
  1610. "action_remark" => '',//备注
  1611. "action_type" => "status"//新建create,编辑edit,更改状态status
  1612. ], "CKD", 0, $out);
  1613. ProcessOrder::AddProcess($this->post['token'], [
  1614. "order_type" => 'CKD',
  1615. "order_code" => $outCode,
  1616. "order_id" => $ou,
  1617. "order_status" => 0,
  1618. "before_status" => 0,
  1619. 'holder_id'=>$out['apply_id'],
  1620. ]);
  1621. $order['send_num'] += $value['receipt_quantity'];
  1622. $order['wsend_num'] -= $value['receipt_quantity'];
  1623. if ($order['wsend_num'] < 0) {
  1624. return false;
  1625. }
  1626. $ups = Db::name("order_num")->save($order);
  1627. if ($ups) {
  1628. $tep = [
  1629. "cgdNo" => $order['cgdNo'],
  1630. "outCode" => $outCode,
  1631. "send_num" => $value['receipt_quantity'],
  1632. "status" => 1,
  1633. "addtime" => date("Y-m-d H:i:s"),
  1634. "updatetime" => date("Y-m-d H:i:s")
  1635. ];
  1636. $sen = Db::name("order_send")->save($tep);
  1637. if ($sen == false) {
  1638. return false;
  1639. }
  1640. $standing_book_data['outCode'][] = $outCode;
  1641. }
  1642. } else {
  1643. return false;
  1644. }
  1645. }
  1646. }
  1647. }
  1648. return true;
  1649. }
  1650. /**
  1651. * @param $data
  1652. * @return bool
  1653. * @throws \think\db\exception\DataNotFoundException
  1654. * @throws \think\db\exception\DbException
  1655. * @throws \think\db\exception\ModelNotFoundException
  1656. */
  1657. public function project_good($data, array &$standing_book_data = [])
  1658. {
  1659. $good = Db::name('good')
  1660. ->alias("a")
  1661. ->leftJoin("good_platform b", "a.spuCode=b.spuCode")
  1662. ->where(["b.skuCode" => $data['skuCode'], "a.is_del" => 0, "b.is_del" => 0])
  1663. ->field("a.*,b.skuCode,b.platform_code")->find();
  1664. if ($good == false) {
  1665. return 4; //商品信息未找到
  1666. }
  1667. $goodlass = Db::name("good_ladder")->where(["skuCode" => $data['skuCode'], "is_del" => 0, "status" => 1])->where([["min_num", "<=", $data['good_num']]])->order("min_num desc")->find();
  1668. if ($goodlass == false) {
  1669. return 4;//商品信息未找到
  1670. }
  1671. $sale_price = $goodlass['sale_price'];
  1672. $origin_price = 0;
  1673. if ($good['is_stock'] == 0) {
  1674. $origin = Db::name("good_nake")->where([["spuCode", "=", $data['spuCode']], ["min_num", "<=", $data['good_num']],
  1675. ["is_del", "=", 0]])->order("min_num desc")->find();
  1676. if ($origin == false) {
  1677. return 4;//商品信息未找到
  1678. }
  1679. $origin_price = $origin['nake_total'];
  1680. }
  1681. if ($good['is_gold_price'] == 1 && $good['is_stock'] == 0) {
  1682. $gold = Db::name("gold_price1")
  1683. ->field('id,price')
  1684. ->where(["type" => $good['noble_metal'], "is_del" => 0, "status" => 1])
  1685. ->order("addtime desc")
  1686. ->find();
  1687. //$saleprice(最终售价) = (打样费/购买数量 + 开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价+物流费)/(1-成本售价/100);
  1688. $sale_price = $good['demo_fee'] / $data['good_num'] + $good['open_fee'] / $data['good_num'] + $good['noble_weight'] * $gold["price"] + $goodlass['cost_fee'] * $good['noble_weight'] + $origin['package_fee'] + $origin['mark_fee'] + $origin['cert_fee'] + $origin['nake_fee'] + $origin['delivery_fee'];
  1689. $good['cgd_gold_price'] = $gold["price"];
  1690. }
  1691. $orderCode = makeNo("QR");
  1692. $supplier_temp_info = Db::name('supplier')
  1693. ->field('id,person,personid')
  1694. ->where('code',$good['supplierNo'])
  1695. ->findOrEmpty();
  1696. $datas = [
  1697. "orderCode" => $orderCode,
  1698. "good_code" => $data['spuCode'],
  1699. "skuCode" => $data['skuCode'],
  1700. "customer_code" => $data['customerNo'],
  1701. "good_name" => isset($good['good_name']) && $good['good_name'] !== '' ? $good['good_name'] : '',
  1702. "good_num" => $data['good_num'],
  1703. "cat_id" => $good['cat_id'],
  1704. "apply_id" => $data['rm'],
  1705. "apply_name" => $data['ri'],
  1706. "origin_price" => $origin_price,
  1707. "sale_price" => $sale_price,
  1708. "post_fee" => 0,
  1709. "status" => 0,
  1710. "send_num" => 0,
  1711. "wsend_num" => $data['good_num'],
  1712. "send_status" => 1,
  1713. "good_type" => 1,
  1714. "send_type" => $data['sendtype'],
  1715. "supplierNo" => $data['companyNo'],
  1716. "is_del" => 0,
  1717. "zxNo" => $data["pgNo"],
  1718. "platform_order" => "",
  1719. "platform_id" => $data['platform_id'],
  1720. "remark" => "",
  1721. "is_stock" => $good['is_stock'],
  1722. "is_activity" => 0,
  1723. "activity_code" => '',
  1724. "proof_id" => 0,
  1725. "order_type" => $good['is_stock'] == 1 ? 1 : 2,//1备库,2非库存品
  1726. 'order_source' => 3,//3项目
  1727. 'good_weight' => $good['noble_weight'],
  1728. 'gold_price' => isset($good['cgd_gold_price']) ? $good['cgd_gold_price'] : 0,
  1729. 'cost_price' => $goodlass['cost_fee'],
  1730. 'diff_weight' => 0,
  1731. 'diff_fee' => 0,
  1732. "addtime" => date("Y-m-d H:i:s"),
  1733. "updatetime" => date("Y-m-d H:i:s"),
  1734. 'total_price' => round($sale_price * $data['good_num'], 2),
  1735. 'cgderid' => $supplier_temp_info['personid'],
  1736. 'cgder' => $supplier_temp_info['person'],//采购员(供应商负责人)
  1737. 'good_createrid' => $good['createrid'],
  1738. 'good_creater' => $good['creater'],//商品创建人
  1739. ];
  1740. $datainfo = Db::name('sale')->insert($datas, true);
  1741. if ($datainfo > 0) {
  1742. $standing_book_data['sale_id'] = $datainfo;
  1743. //修改状态,添加待办
  1744. ActionLog::logAdd($this->post['token'], [
  1745. "order_code" => $orderCode,//项目编码
  1746. "status" => 0,//这里的status是之前的值
  1747. "action_remark" => '',//备注
  1748. "action_type" => "status"//新建create,编辑edit,更改状态status
  1749. ], "XSQRD", 0, $datas);
  1750. ProcessOrder::AddProcess($this->post['token'], [
  1751. "order_type" => 'XSQRD',
  1752. "order_code" => $orderCode,
  1753. "order_id" => $datainfo,
  1754. "order_status" => 0,
  1755. "before_status" => 0,
  1756. 'holder_id' => $datas['apply_id']
  1757. ]);
  1758. $cgd = [
  1759. "supplierNo" => $good['supplierNo'],
  1760. "companyNo" => $data['companyNo'],
  1761. "orderCode" => $orderCode,
  1762. "spuCode" => $good['spuCode'],
  1763. "skuCode" => $good['skuCode'],
  1764. "good_name" => $good['good_name'],
  1765. "sale_price" => $origin_price,
  1766. "total_fee" => $origin_price * $data['good_num'],
  1767. "pakge_fee" => isset($origin['package_fee']) ? $origin['package_fee'] : 0,
  1768. "cert_fee" => isset($origin['cert_fee']) ? $origin['cert_fee'] : 0,
  1769. "open_fee" => $good['open_fee'],
  1770. "cost_fee" => isset($origin['cost_fee']) ? $origin['cost_fee'] : 0,
  1771. "mark_fee" => isset($origin['mark_fee']) ? $origin['mark_fee'] : 0,
  1772. "demo_fee" => $good['demo_fee'],
  1773. "nake_fee" => isset($origin['nake_fee']) ? $origin['nake_fee'] : 0,
  1774. "delivery_fee" => isset($origin['delivery_fee']) ? $origin['delivery_fee'] : 0,
  1775. "good_num" => $data['good_num'],
  1776. "good_type" => 1,
  1777. "weight" => $good['noble_weight'],
  1778. "gold_price" => isset($good['cgd_gold_price']) ? $good['cgd_gold_price'] : 0,
  1779. "createrid" => $good['createrid'],
  1780. "creater" => $good['creater'],
  1781. "order_type" => $good['is_stock'] == 1 ? 1 : 2,
  1782. 'order_source' => 3,//3项目
  1783. 'send_way' => 2,
  1784. 'good_createrid' => $good['createrid'],
  1785. 'good_creater' => $good['creater'],//商品创建人
  1786. ];
  1787. if ($good['is_stock'] == 0) {
  1788. $bol = $this->createCgd($cgd, $standing_book_data);
  1789. if ($bol == false) {
  1790. return 3;//关联采购失败
  1791. }
  1792. } else {
  1793. $stock = Db::name("good_stock")->alias("a")->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")->where
  1794. (["spuCode" => $good['spuCode'], "a.is_del" => 0, "a.status" => 1, "b.wsm_type" => 5, "b.companyNo"
  1795. => $data['companyNo']])->field("a.id,a.usable_stock,a.wait_out_stock,a.wsm_code")->find();
  1796. if ($stock == false || $stock['usable_stock'] < $data['good_num']) {
  1797. return 1; //库存不足
  1798. }
  1799. $stock_num =Db::name("good_stock_info")->where(["stockid"=>$stock['id']])->sum('balance_num');
  1800. if($stock_num < $data['good_num']){
  1801. return 1; //库存不足
  1802. }
  1803. // $bol = $this->RelaCgd(['orderCode' => $orderCode, "good_num" => $data['good_num'], "spuCode" => $good['spuCode'], "companyNo" => $data['companyNo'], "cost_fee" => $goodlass['cost_fee'], 'order_type' => $cgd['order_type'], 'order_source' => $cgd['order_source']], $standing_book_data);
  1804. // if ($bol == false) {
  1805. // return 3;//关联采购失败
  1806. // }
  1807. if (isset($stock)) {
  1808. $stck = [
  1809. "usable_stock" => $stock['usable_stock'] - $data['good_num'],
  1810. "wait_out_stock" => $stock['wait_out_stock'] + $data['good_num'],
  1811. "updatetime" => date("Y-m-d H:i:s")
  1812. ];
  1813. $upad = Db::name("good_stock")->where($stock)->update($stck);
  1814. if ($upad == false) {
  1815. return 2; //库存更新失败
  1816. }
  1817. $stockinfo =GoodStockInfo::OrderBn($orderCode,$stock['id'],$data['good_num']);
  1818. if($stockinfo==false){
  1819. return 2; //库存不足
  1820. }
  1821. //商品变动日志表,good_log_code字段存储采购单号
  1822. $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $stock['id'], "type" => 2, 'stock' => $data['good_num'], "stock_name" => "usable_stock"];
  1823. $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $stock['id'], "type" => 1, 'stock' => $data['good_num'], "stock_name" => "wait_out_stock"];
  1824. GoodLog::LogAdd($this->post['token'], $good_data, "XSQRD");
  1825. }
  1826. }
  1827. if ($data['sendtype'] == 1 && !empty($data['addrlist'])) {
  1828. $i = 1;
  1829. foreach ($data['addrlist'] as $value) {
  1830. $temp = [];
  1831. $addrs = [];
  1832. if ($value['addr_code'] !== '' && is_array($value['addr_code']) && !empty($value['addr_code'])) {
  1833. $addrs['provice_code'] = $value['addr_code'][0];
  1834. $addrs['city_code'] = $value['addr_code'][1];
  1835. $addrs['area_code'] = $value['addr_code'][2];
  1836. $addr = json_encode($addrs);
  1837. } else {
  1838. $addr = isset($value['addr_code']) ? $value['addr_code'] : '';
  1839. }
  1840. $temp['orderCode'] = $orderCode;
  1841. $temp['contactor'] = $value['contactor'];
  1842. $temp['mobile'] = $value['mobile'];
  1843. $temp['addr'] = $value['addr'];
  1844. $temp['addr_code'] = $addr;
  1845. $temp['customer_code'] = $data['companyNo'];
  1846. $temp['receipt_quantity'] = $value['receipt_quantity'];
  1847. $temp['post_fee'] = 0;
  1848. $temp['is_del'] = 0;
  1849. $temp['addtime'] = date("Y-m-d H:i:s");
  1850. $temp['updatetime'] = date("Y-m-d H:i:s");
  1851. $temp['arrive_time'] = $data['arrtime'];
  1852. $vmp = Db::name('order_addr')->insert($temp, true);
  1853. if ($vmp > 0) {
  1854. $standing_book_data['customer_code'] = $temp['customer_code'];
  1855. $num = $value['receipt_quantity'];
  1856. $outCode = makeNo("DF");
  1857. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  1858. $i++;
  1859. if ($good['is_stock'] != 1) {
  1860. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  1861. ->lock(true)->find();
  1862. if ($order == false) {
  1863. return 3;
  1864. }
  1865. //改变编码规则,将原来的outCode后两位换成序列号
  1866. //str_pad字符串填充
  1867. $order['wsend_num'] -= $num;
  1868. $order['send_num'] += $num;
  1869. $or = Db::name("order_num")->save($order);
  1870. if ($or == false) {
  1871. return 3;
  1872. }
  1873. $tep = [
  1874. "cgdNo" => $order['cgdNo'],
  1875. "outCode" => $outCode,
  1876. "send_num" => $num,
  1877. "status" => 1,
  1878. "addtime" => date("Y-m-d H:i:s"),
  1879. "updatetime" => date("Y-m-d H:i:s")
  1880. ];
  1881. $sen = Db::name("order_send")->save($tep);
  1882. if ($sen == false) {
  1883. return 3;
  1884. }
  1885. $cgdinfo = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
  1886. if ($cgdinfo == false) {
  1887. return 4;
  1888. }
  1889. }
  1890. $out = [
  1891. "orderCode" => $orderCode,
  1892. "outCode" => $outCode,
  1893. "apply_id" => $data['rm'],
  1894. "apply_name" => $data['ri'],
  1895. "addrid" => $vmp,
  1896. "post_name" => "",
  1897. "post_code" => "",
  1898. "post_fee" => 0,
  1899. "sendtime" => date("Y-m-d H:i:s"),
  1900. "send_num" => $num,
  1901. "check_num" => 0,
  1902. "error_num" => 0,
  1903. "wsm_code" => $good['is_stock'] == 1 ?$stock['wsm_code']:$cgdinfo['wsm_code'],
  1904. "order_type" => 1,
  1905. "status" => $good['is_stock'] == 1 ? 1 : 0,
  1906. "addtime" => date("Y-m-d H:i:s"),
  1907. "updatetime" => date("Y-m-d H:i:s")
  1908. ];
  1909. $ou = Db::name("order_out")->insertGetId($out);
  1910. if ($ou == false) {
  1911. return 0;
  1912. } else {
  1913. $standing_book_data['outCode'][] = $outCode;
  1914. //修改状态,添加待办
  1915. ActionLog::logAdd($this->post['token'], [
  1916. "order_code" => $outCode,//出库单号
  1917. "status" => 0,//这里的status是之前的值
  1918. "action_remark" => '',//备注
  1919. "action_type" => "create"//新建create,编辑edit,更改状态status
  1920. ], "CKD", 0, $out);
  1921. ProcessOrder::AddProcess($this->post['token'], [
  1922. "order_type" => 'CKD',
  1923. "order_code" => $outCode,//出库单号
  1924. "order_id" => $ou,
  1925. "order_status" => 0,
  1926. "before_status" => 0,
  1927. 'holder_id'=>$out['apply_id'],
  1928. ]);
  1929. }
  1930. } else {
  1931. return false;
  1932. }
  1933. }
  1934. }
  1935. // }
  1936. }
  1937. return true;
  1938. }
  1939. public function list()
  1940. {
  1941. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  1942. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  1943. $where[] = ['s.is_del', "=", 0];
  1944. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  1945. if ($orderCode !== "") {
  1946. $where[] = ['s.orderCode', "like", "%$orderCode%"];
  1947. }
  1948. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !== "" ? trim($this->post['apply_name']) : "";
  1949. if ($apply_name !== "") {
  1950. $where[] = ['s.apply_name', "like", "%$apply_name%"];
  1951. }
  1952. $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim($this->post['good_name']) : "";
  1953. if ($good_name !== "") {
  1954. $where[] = ['s.good_name', "like", "%$good_name%"];
  1955. }
  1956. $good_code = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : "";
  1957. if ($good_code !== "") {
  1958. $where[] = ['s.good_code', "like", "%$good_code%"];
  1959. }
  1960. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code'] !== "" ? trim($this->post['customer_code']) : "";
  1961. if ($customer_code !== "") {
  1962. $where[] = ['s.customer_code', "like", "%$customer_code%"];
  1963. }
  1964. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  1965. if ($supplierNo !== "") {
  1966. $where[] = ['s.supplierNo', "like", "%$supplierNo%"];
  1967. }
  1968. $ordertype = isset($this->post['order_type']) && $this->post['order_type'] !== "" ? intval($this->post['order_type']) : "";
  1969. if ($ordertype !== "") {
  1970. $where[] = ['s.order_type', "=", $ordertype];
  1971. }
  1972. $status = isset($this->post['status']) && $this->post['status'] !== "" ? trim($this->post['status']) : "";
  1973. if ($status !== "") {
  1974. $where[] = ['s.status', "=", $status];
  1975. }
  1976. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  1977. if ($start != "") {
  1978. $where[] = ["s.addtime", '>=', $start];
  1979. }
  1980. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  1981. if ($end != "") {
  1982. $end .= " 23:59:59";
  1983. $where[] = ["s.addtime", '<=', $end];
  1984. }
  1985. $role = $this->checkRole();
  1986. if (!empty($role['write'])) {
  1987. $where[] = ["s.apply_id", "in", $role['write']];
  1988. }
  1989. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  1990. if ($company_name !== "") $where[] = ["s.apply_id", 'in', get_company_item_user_by_name($company_name)];
  1991. // if(!empty($role['platform']) ){
  1992. // $where[]=["s.platform_id","in",$role['platform']];
  1993. // }
  1994. $count = Db::name('sale')->alias('s')->where($where)->count();
  1995. $total = ceil($count / $size);
  1996. $page = $page >= $total ? $total : $page;
  1997. $list = Db::name('sale')
  1998. ->alias('s')
  1999. ->field('s.*,u.itemid')
  2000. ->leftJoin("depart_user u", "u.uid=s.apply_id AND u.is_del=0")->where($where)
  2001. ->order("s.addtime desc")
  2002. ->page($page, $size)
  2003. ->cursor();
  2004. $data = [];
  2005. foreach ($list as $value) {
  2006. if ($value['order_type'] == 3) {
  2007. $var = Db::name("good_zixun")->where(["spuCode" => $value['good_code'], "is_del" => 0])->find();
  2008. $value["speclist"] = isset($var['specinfo']) && $var['specinfo'] != "" ? json_decode($var['specinfo'], true) : "";
  2009. } else {
  2010. $var = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  2011. ->where(['a.skuCode' => $value['skuCode']])->find();
  2012. $spec = Db::name("good_spec")->where(["spuCode" => $var['spuCode'], "is_del" => 0])->select()->toArray();
  2013. $speclist = [];
  2014. if (!empty($spec)) {
  2015. foreach ($spec as $val) {
  2016. $temp = [];
  2017. $temp['id'] = $val['id'];
  2018. $temp['spuCode'] = $val['spuCode'];
  2019. $temp['spec_id'] = $val['spec_id'];
  2020. $temp['spec_value_id'] = $val['spec_value_id'];
  2021. $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
  2022. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  2023. $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
  2024. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  2025. $speclist[] = $temp;
  2026. }
  2027. }
  2028. $value["speclist"] = empty($speclist) ? [] : $speclist;
  2029. }
  2030. $value['can'] = isset($var['cat_id']) && $var['cat_id'] != 0 ? made($var['cat_id']) : [];
  2031. $value['supplierName'] = '';
  2032. if ($value['supplierNo'] != "") {
  2033. $supplier = Db::name("business")->where(["companyNo" => $value['supplierNo']])->find();
  2034. $value['supplierName'] = isset($supplier['company']) ? $supplier['company'] : "";
  2035. }
  2036. $value['customerName'] = '';
  2037. if ($value['customer_code'] != "") {
  2038. $supplier = Db::name("customer_info")->where(["companyNo" => $value['customer_code']])->find();
  2039. $value['customerName'] = isset($supplier['companyName']) ? $supplier['companyName'] : "";
  2040. }
  2041. $value['useage'] = "";
  2042. if ($value['use_order'] != 0) {
  2043. $use = Db::name("order_use")->where(["id" => $value['use_order']])->find();
  2044. $value['useage'] = isset($use['order_use']) ? $use['order_use'] : "";
  2045. } else {
  2046. $value['use_order'] = '';
  2047. }
  2048. $value['good_num'] -= $value['th_num'];
  2049. $value['send_num'] -= $value['th_num'];
  2050. $value['total_price'] = round($value['total_price'] - $value['th_fee'], 2);
  2051. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  2052. $data[] = $value;
  2053. }
  2054. return app_show(0, "获取成功", ['list' => $data, 'count' => $count]);
  2055. }
  2056. public function edit()
  2057. {
  2058. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  2059. $id = isset($this->post['id']) && $this->post['id'] !== "" ? trim($this->post['id']) : "";
  2060. if ($id == "") {
  2061. return error_show(1002, "参数id不能为空");
  2062. }
  2063. $etid = Db::name("sale")->where(["id" => $id, "is_del" => 0])->find();
  2064. if (empty($etid)) {
  2065. return error_show(1002, "未找到数据");
  2066. }
  2067. // $orderCode=isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']) :"";
  2068. // if($orderCode==""){
  2069. // return error_show(1002,"销售单code不能为空");
  2070. // }
  2071. $good_code = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : "";
  2072. if ($good_code == "") {
  2073. return error_show(1002, "商品code不能为空");
  2074. }
  2075. $ct = Db::name('good')->alias('a')->join('good_type b', 'b.good_code=a.good_code', 'left')
  2076. ->where(['b.type_code' => $good_code])->find();
  2077. if ($ct == "") {
  2078. return error_show(1002, "未找到商品数据");
  2079. }
  2080. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code'] !== "" ? trim($this->post['customer_code']) : "";
  2081. if ($customer_code == "") {
  2082. return error_show(1002, "客户code不能为空");
  2083. }
  2084. $customer = Db::name("customer_info")->where(["companyNo" => $customer_code])->find();
  2085. if ($customer == false) {
  2086. return error_show(1004, "未找到客户数据");
  2087. }
  2088. $supplierNo = $this->post['supplierNo'] && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  2089. if ($supplierNo == "") {
  2090. return error_show(1002, "参数supplierNo不能为空");
  2091. }
  2092. $supplier = Db::name("supplier")->where(["code" => $supplierNo])->find();
  2093. if ($supplier == false) {
  2094. return error_show(1004, "未找到平台供应商数据");
  2095. }
  2096. $apply_id = GetUserInfo($token);
  2097. if (empty($apply_id) || $apply_id['code'] != 0) {
  2098. return error_show(1002, "申请人数据不存在");
  2099. }
  2100. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  2101. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  2102. // $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim
  2103. //($this->post['good_name']) : "";
  2104. // $good_num = isset($this->post['good_num']) && $this->post['good_num'] !== "" ? intval
  2105. //($this->post['good_num']) : "";
  2106. // $origin_price = isset($this->post['origin_price']) && $this->post['origin_price'] !== "" ? intval
  2107. // ($this->post['origin_price']) : "";
  2108. // $sale_price = isset($this->post['sale_price']) && $this->post['sale_price'] !== "" ? trim($this->post['sale_price']) : "";
  2109. // $total_price = isset($this->post['total_price']) && $this->post['total_price'] !== "" ? trim($this->post['total_price']) : "";
  2110. // $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !== "" ? trim($this->post['post_fee']) : "";
  2111. // $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  2112. $vr = isset($this->post['order_addr']) && $this->post['order_addr'] !== "" ? $this->post['order_addr'] : "";
  2113. if ($vr == "") {
  2114. return error_show(1002, "参数order_addr不能为空");
  2115. }
  2116. $vi = isset($this->post['good_stock']) && $this->post['good_stock'] !== "" ? $this->post['good_stock'] : "";
  2117. if ($vi == "") {
  2118. return error_show(1002, "参数good_stock不能为空");
  2119. }
  2120. $good_num = intval(array_sum(array_column($vi, "num")));
  2121. Db::startTrans();
  2122. try {
  2123. $datn = [
  2124. "id" => $id,
  2125. "good_code" => $good_code,
  2126. "customer_code" => $customer_code,
  2127. "good_num" => $good_num,
  2128. "supplierNo" => $supplierNo,
  2129. "apply_id" => $rm,
  2130. "apply_name" => $ri,
  2131. "is_del" => 0,
  2132. "updatetime" => date("Y-m-d H:i:s")
  2133. ];
  2134. $datninfo = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->save($datn);
  2135. if ($datninfo > 0) {
  2136. $stn = ["order_code" => $etid['orderCode'], "status" => $etid['status'], "action_remark" => '', "action_type" => "edit"];
  2137. ActionLog::logAdd($this->post['token'], $stn, "XSQRD", $etid['status'], $datn);
  2138. // $order=["order_type"=>'XSQRD',"order_code"=>$etid['orderCode'],"order_id"=>$id,"order_status"=>$etid['status'],"before_status"=> 0];
  2139. // ProcessOrder::AddProcess($this->post['token'],$order);
  2140. //更新台账
  2141. Db::name('standing_book')
  2142. ->where('orderCode', $etid['orderCode'])
  2143. ->update([
  2144. 'customer_code' => $customer_code,
  2145. 'supplierNo' => $supplierNo,
  2146. 'updatetime' => date("Y-m-d H:i:s")
  2147. ]);
  2148. foreach ($vi as $ion) {
  2149. $iten = [];
  2150. isset($ion['id']) && $ion['id'] !== "" ? $iten['id'] = $ion['id'] : '';
  2151. $iten['orderCode'] = $etid['orderCode'];
  2152. $iten['wsm_code'] = $ion['wsm_code'];
  2153. $iten['stock_id'] = 0;
  2154. $iten['num'] = $ion['num'];
  2155. $iten['status'] = 1;
  2156. $iten['sale_price'] = $ct['original_price'];
  2157. isset($ion['id']) && $ion['id'] !== "" ? '' : $iten['addtime'] = date("Y-m-d H:i:s");
  2158. $iten['updatetime'] = date("Y-m-d H:i:s");
  2159. $vp = Db::name('sale_info')->save($iten);
  2160. if ($vp == false) {
  2161. Db::rollback();
  2162. return error_show(1002, "更新失败");
  2163. }
  2164. }
  2165. foreach ($vr as $value) {
  2166. $temp = [];
  2167. isset($value['id']) && $value['id'] !== "" ? $temp['id'] = $value['id'] : '';
  2168. $temp['orderCode'] = $etid['orderCode'];
  2169. $temp['contactor'] = $value['contactor'];
  2170. $temp['mobile'] = $value['mobile'];
  2171. $temp['addr'] = $value['addr'];
  2172. $temp['addr_code'] = $value['addr_code'];
  2173. $temp['customer_code'] = $customer_code;
  2174. $temp['receipt_quantity'] = $value['receipt_quantity'];
  2175. $temp['post_fee'] = 0;
  2176. $temp['is_del'] = $value['is_del'];
  2177. isset($value['id']) && $value['id'] !== "" ? '' : $temp['addtime'] = date("Y-m-d H:i:s");
  2178. $temp['updatetime'] = date("Y-m-d H:i:s");
  2179. $temp['arrive_time'] = date("Y-m-d H:i:s");
  2180. $dat = Db::name('order_addr')->save($temp);
  2181. if ($dat == false) {
  2182. Db::rollback();
  2183. return error_show(1002, "更新失败");
  2184. }
  2185. }
  2186. Db::commit();
  2187. return error_show(0, "更新成功");
  2188. }
  2189. Db::rollback();
  2190. return error_show(1002, "更新失败");
  2191. } catch (Exception $e) {
  2192. Db::rollback();
  2193. return error_show(1005, $e->getMessage());
  2194. }
  2195. }
  2196. public function info()
  2197. {
  2198. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  2199. if ($id == "") {
  2200. return error_show(1002, "参数id不能为空");
  2201. }
  2202. $einfo = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->find();
  2203. if (empty($einfo)) {
  2204. return error_show(1002, "未找到销售订单数据");
  2205. }
  2206. if ($einfo['order_type'] == 3) {
  2207. $goon = Db::name("good_zixun")->where(["spuCode" => $einfo['good_code'], "is_del" => 0])->find();
  2208. } else {
  2209. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  2210. ->where(['a.skuCode' => $einfo['skuCode']])->find();
  2211. }
  2212. if ($goon == false) {
  2213. return error_show(1003, "未找到商品数据");
  2214. } else {
  2215. $goon['exclusive'] = isset($goon['is_exclusive']) ? makeExcluse($goon['is_exclusive']) : "";
  2216. $unit = Db::name("unit")->where(["id" => $goon['good_unit']])->find();
  2217. $goon['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  2218. $spec = Db::name("good_spec")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->select()->toArray();
  2219. $speclist = [];
  2220. if (!empty($spec)) {
  2221. foreach ($spec as $value) {
  2222. $temp = [];
  2223. $temp['id'] = $value['id'];
  2224. $temp['spuCode'] = $value['spuCode'];
  2225. $temp['spec_id'] = $value['spec_id'];
  2226. $temp['spec_value_id'] = $value['spec_value_id'];
  2227. $temp['is_del'] = $value['is_del'];
  2228. $sp = Db::name("specs")->where(["id" => $value['spec_id']])->find();
  2229. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  2230. $spv = Db::name("spec_value")->where(["id" => $value['spec_value_id']])->find();
  2231. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  2232. $speclist[] = $temp;
  2233. }
  2234. }
  2235. $goon["speclist"] = empty($speclist) ? [] : $speclist;
  2236. $proof = Db::name("good_proof")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->order("updatetime desc")->field("id,proof_type,proof_url")->find();
  2237. $goon['proof'] = isset($proof) && $proof != false ? $proof : [];
  2238. $goon['origin_place_cn'] = "";
  2239. $goon['delivery_place_cn'] = "";
  2240. if (isset($goon['delivery_place']) && $goon['delivery_place'] !== "") {
  2241. $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  2242. list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $goon['delivery_place']);
  2243. $goon['delivery_place_cn'] = GetAddr(json_encode($place));
  2244. }
  2245. if (isset($goon['delivery_place']) && $goon['origin_place'] !== "") {
  2246. $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  2247. list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $goon['origin_place']);
  2248. $goon['origin_place_cn'] = GetAddr(json_encode($place));
  2249. }
  2250. if ($goon['brand_id'] != 0) {
  2251. $brand = Db::name("brand")->where(["id" => $goon['brand_id']])->find();
  2252. $goon["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  2253. } else {
  2254. $goon["brand_name"] = "";
  2255. $goon["brand_id"] = "";
  2256. }
  2257. $supplier = Db::name("supplier")->where(["code" => $goon['supplierNo']])->find();
  2258. $goon['supplierName'] = isset($supplier['name']) ? $supplier['name'] : "";
  2259. $goon['noble_name'] = isset($goon['noble_metal']) && $goon['noble_metal'] != 0 ? $this->noble[$goon['noble_metal']] : "";
  2260. if (isset($goon['companyNo']) && $goon['companyNo'] != "") {
  2261. $company = Db::name("business")->where(["companyNo" => $goon['companyNo']])->find();
  2262. }
  2263. $goon['company'] = isset($company['company']) ? $company['company'] : "";
  2264. }
  2265. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  2266. $in = Db::name('customer_info')->where(['companyNo' => $einfo['customer_code']])->field('companyName')->find();
  2267. $einfo['supplierName'] = '';
  2268. if ($einfo['supplierNo'] != "") {
  2269. $supplier = Db::name("business")->where(["companyNo" => $einfo['supplierNo']])->find();
  2270. $einfo['supplierName'] = isset($supplier['company']) ? $supplier['company'] : "";
  2271. }
  2272. $addr_sum = Db::name('order_addr')->where(['orderCode' => $einfo['orderCode'],"is_del"=>0])->sum('receipt_quantity');
  2273. $einfo['addr_sum']=$addr_sum;
  2274. // $fo = Db::name('order_addr')->alias("a")->leftJoin("order_out b", "a.id=b.addrid and b.is_del=0")
  2275. // ->where(['a.orderCode' => $einfo['orderCode'], 'a.is_del' => 0])->field("a.*,b.post_name,b.post_code,
  2276. // b.post_fee,b.sendtime,b.send_num,b.check_num,b.error_num,b.wsm_code,b.order_type,b.status as send_status,b.outCode")
  2277. // ->select();
  2278. $addrs = [];
  2279. // if (!empty($fo)) {
  2280. // foreach ($fo as $value) {
  2281. // $value['addr_info'] = "";
  2282. // $value['send_num'] = 0;
  2283. // if ($value['addr_code'] != "" && !empty($value['addr_code'])) {
  2284. // $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  2285. // list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $value['addr_code']);
  2286. // $addr = GetAddr(json_encode($place));
  2287. // $value['addr_info'] = $addr;
  2288. // }
  2289. // $value['wsm_name'] = "";
  2290. // if (isset($value['wsm_code']) && $value['wsm_code'] != "") {
  2291. // $wsmcode = Db::name("warehouse_info")->where(["wsm_code" => $value['wsm_code']])->find();
  2292. // $value['wsm_name'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  2293. // }
  2294. // $value['sendtime'] = $value['send_status'] < 2 ? '' : $value['sendtime'];
  2295. // $addrs[] = $value;
  2296. // }
  2297. // }
  2298. $einfo['useage'] = "";
  2299. if ($einfo['use_order'] != 0) {
  2300. $use = Db::name("order_use")->where(["id" => $einfo['use_order']])->find();
  2301. $einfo['useage'] = isset($use['order_use']) ? $use['order_use'] : "";
  2302. } else {
  2303. $einfo['use_order'] = '';
  2304. }
  2305. $einfo['platform_name'] = '';
  2306. if ($einfo['platform_id'] != 0) {
  2307. $plat = Db::name("platform")->where(['id' => $einfo['platform_id']])->find();
  2308. $einfo['platform_name'] = isset($plat['platform_name']) ? $plat['platform_name'] : "";
  2309. }
  2310. $einfo['companyName'] = isset($in['companyName']) ? $in['companyName'] : "";
  2311. $einfo['addrs'] = $addrs;
  2312. $einfo['can'] = $int;
  2313. $einfo['goodinfo'] = $goon;
  2314. $einfo['good_num'] -= $einfo['th_num'];
  2315. $einfo['send_num'] -= $einfo['th_num'];
  2316. $einfo['total_price'] = round($einfo['total_price'] - $einfo['th_fee'], 2);
  2317. //处理凭证
  2318. if ($einfo['order_type'] == 1 || $einfo['order_type'] == 2) {
  2319. $proof = Db::name('good_proof')
  2320. ->field('id,proof_type,proof_url')
  2321. ->where(['id' => $einfo['proof_id'], 'is_del' => 0])
  2322. ->find();
  2323. $einfo['proof_type'] = isset($proof['proof_type']) ? $proof['proof_type'] : '';
  2324. $einfo['proof_url'] = isset($proof['proof_url']) ? $proof['proof_url'] : '';
  2325. } elseif ($einfo['order_type'] == 3) {
  2326. $einfo['proof_type'] = isset($goon['proof_type']) ? $goon['proof_type'] : '';
  2327. $einfo['proof_url'] = isset($goon['proof_url']) ? $goon['proof_url'] : '';
  2328. } else {
  2329. $einfo['proof_type'] = '';
  2330. $einfo['proof_url'] = '';
  2331. }
  2332. if (empty($einfo)) {
  2333. return error_show(1002, "未找到销售订单数据");
  2334. } else {
  2335. return app_show(0, "获取成功", $einfo);
  2336. }
  2337. }
  2338. public function del()
  2339. {
  2340. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  2341. if ($id === "") {
  2342. return error_show(1002, "参数id不能为空");
  2343. }
  2344. $ed = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->find();
  2345. if (empty($ed)) {
  2346. return error_show(1002, "未找到销售订单数据");
  2347. }
  2348. $str = Db::name('sale')->update(['id' => $id, 'is_del' => 1, 'updatetime' => date("Y-m-d H:i:s")]);
  2349. if ($str) {
  2350. $stc = ["order_code" => $id, "status" => $ed['status'], "action_remark" => '', "action_type" => "delete"];
  2351. ActionLog::logAdd($this->post['token'], $stc, "XSQRD", $ed['status'], ['id' => $id, 'is_del' => 1, 'updatetime' => date("Y-m-d H:i:s")]);
  2352. $order = ["order_type" => 'XSQRD', "order_code" => $ed['orderCode'], "order_id" => $id, "order_status" => $ed['status'],'holder_id' => $ed['apply_id']
  2353. ];
  2354. ProcessOrder::workdel($order);
  2355. return error_show(0, "删除成功");
  2356. } else {
  2357. return error_show(1002, "删除失败");
  2358. }
  2359. }
  2360. //废弃接口
  2361. public function status()
  2362. {
  2363. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  2364. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  2365. if ($id == "") {
  2366. return error_show(1002, "参数id不能为空");
  2367. }
  2368. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  2369. if ($status == "") {
  2370. return error_show(1002, "订单状态不能为空");
  2371. }
  2372. $dio = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->find();
  2373. if (empty($dio)) {
  2374. return error_show(1002, "销售单信息未找到");
  2375. }
  2376. if ($status == 1 && $dio['order_type'] == 2) {
  2377. $zxinfo = Db::name("consult")->where(["zxNo" => $dio['zxNo'], "is_del" => 0])->find();
  2378. if ($zxinfo == false) {
  2379. return error_show(1004, "未找到咨询单数据");
  2380. }
  2381. $gold['price'] = 0;
  2382. if ($zxinfo['zx_type'] == 2) {
  2383. if ($zxinfo['metals'] == "") {
  2384. return error_show(1004, "未找到咨询单贵金属类型数据");
  2385. }
  2386. $metals = $zxinfo['metals'] == '18K' ? 1 : ($zxinfo['metals'] == '24K' ? 2 : 3);
  2387. $gold = Db::name("gold_price")->where(["type" => $metals])->order("addtime desc")->find();
  2388. if (empty($gold)) {
  2389. return error_show(1004, "未找到咨询单贵金属类型数据");
  2390. }
  2391. $dio['gold_price'] = $gold['price'];
  2392. }
  2393. $wsm = Db::name("warehouse_info")->where(["supplierNo" => $zxinfo['gysNo'], "wsm_type" => 2])->find();
  2394. if (empty($wsm)) {
  2395. return error_show(1002, "供应商仓库未找到");
  2396. }
  2397. }
  2398. // $apply_id = GetUserInfo($token);
  2399. // if (empty($apply_id) || $apply_id['code'] != 0) {
  2400. // return error_show(1002, "申请人数据不存在");
  2401. // }
  2402. $rm = $this->uid;
  2403. $ri = $this->uname;
  2404. Db::startTrans();
  2405. try {
  2406. $vmp = $dio['status'];
  2407. $dio['status'] = $status;
  2408. $dio['updatetime'] = date("Y-m-d H:i:s");
  2409. $st = Db::name('sale')->save($dio);
  2410. if ($st) {
  2411. $stn = ["order_code" => $dio['orderCode'], "status" => $vmp, "action_remark" => '', "action_type" => "status"];
  2412. ActionLog::logAdd($this->post['token'], $stn, $dio['order_type'] == 2 ? "ZXQRD" : "XSQRD", $dio['status'], $dio);
  2413. $order = ["order_type" => "XSQRD", "order_code" => $dio['orderCode'], "order_id" => $id, "order_status" => $dio['status'], "before_status" => 1,'holder_id' => $dio['apply_id']];
  2414. ProcessOrder::AddProcess($this->post['token'], $order);
  2415. if ($status == 3 && $dio['order_type'] == 1) {
  2416. $dn = Db::name('sale_info')->where(['orderCode' => $st['orderCode']])->select();
  2417. foreach ($dn as $value) {
  2418. $dm = Db::name('good_stock')->where(['wsm_code' => $value['wsm_code'], 'good_type_code' => $st['good_code']])->find();
  2419. if ($dm == "") {
  2420. Db::rollback();
  2421. return error_show(1003, "未找到商品数据");
  2422. }
  2423. if ($value['num'] > $dm['usable_stock']) {
  2424. Db::rollback();
  2425. return error_show(1002, "超出库存数量");
  2426. }
  2427. $dm['usable_stock'] -= $value['num'];
  2428. $dm['wait_out_stock'] += $value['num'];
  2429. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code'], "is_del" => 0])->sum("send_num");
  2430. $value['send_num'] = $send ?? 0;
  2431. $value['wsend_num'] = $value['num'] - $value['send_num'];
  2432. $Db = Db::name('good_stock')->update($dm);
  2433. $Db['updatetime'] = date('Y-m-d H:i:s');
  2434. if ($Db == false) {
  2435. Db::rollback();
  2436. return error_show(1002, "状态更新失败");
  2437. }
  2438. }
  2439. }
  2440. if ($status == 1 && $dio['order_type'] == 2) {
  2441. $cgd = makeNo("CG");
  2442. $clll = [
  2443. "cgdNo" => $cgd,
  2444. "bkcode" => $dio['orderCode'],
  2445. "wsm_code" => $wsm['wsm_code'],
  2446. "cgder_id" => Db::name('depart_user')->where('nickname', $zxinfo['saler'])->value('uid') ?? 0,//重名以及多个账号的话…………
  2447. "cgder" => $zxinfo['saler'],
  2448. "good_code" => "GD-" . $zxinfo['cpNo'],
  2449. "good_name" => $zxinfo['cpName'],
  2450. "good_type_code" => $zxinfo['cpNo'],
  2451. "good_num" => $dio['good_num'],
  2452. "good_price" => $zxinfo['total_fee'],
  2453. "total_fee" => round($zxinfo['total_fee'] * $dio['good_num'], 2),
  2454. "pakge_fee" => $zxinfo['package_fee'],
  2455. "cert_fee" => $zxinfo['cert_fee'],
  2456. "open_fee" => $zxinfo['open_fee'],
  2457. "delivery_fee" => $zxinfo['delivery_fee'],
  2458. "mark_fee" => $zxinfo['mark_fee'],
  2459. "teach_fee" => $zxinfo['cost_fee'],
  2460. "demo_fee" => $zxinfo['demo_fee'],
  2461. "nake_fee" => $zxinfo['bare_fee'],
  2462. "weight" => isset($zxinfo['weight']) ? $zxinfo['weight'] : 0,
  2463. "supplierNo" => $zxinfo['gysNo'],
  2464. "supplier_name" => $zxinfo['gysname'],
  2465. "gold_price" => $gold['price'],
  2466. "send_num" => 0,
  2467. "wsend_num" => $dio['good_num'],
  2468. "remark" => '',
  2469. "lasttime" => date("Y-m-d H:i:s"),
  2470. "is_del" => 0,
  2471. "status" => $dio['send_type'] == 1 ? 3 : 0,
  2472. "order_type" => 2,
  2473. "order_source" => 3,//3项目,废弃接口???
  2474. "addtime" => date("Y-m-d H:i:s"),
  2475. "updatetime" => date("Y-m-d H:i:s")
  2476. ];
  2477. $cgin = Db::name("purchease_order")->insert($clll);
  2478. // var_dump(Db::name("purchease_order")->getLastSql(),$clll);
  2479. if ($cgin == false) {
  2480. $stb = ["order_code" => $cgd, "status" => $clll['status'], "action_remark" => '', "action_type" => "create"];
  2481. ActionLog::logAdd($this->post['token'], $stb, "xsd", $clll['status'], $stb);
  2482. Db::rollback();
  2483. return error_show(1002, "咨询采购单创建商品失败");
  2484. }
  2485. if ($dio['send_type'] == 1) {
  2486. $addr = Db::name('order_addr')->where(["orderCode" => $dio['orderCode'], 'is_del' => 0])->select();
  2487. $i = 1;
  2488. foreach ($addr as $value) {
  2489. $outCode = makeNo("DF");
  2490. //改变编码规则,将原来的outCode后两位换成序列号
  2491. //str_pad字符串填充
  2492. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  2493. $i++;
  2494. $data = [
  2495. "wsm_code" => $wsm['wsm_code'],
  2496. "orderCode" => $dio['orderCode'],
  2497. "outCode" => $outCode,
  2498. "order_type" => $dio['order_type'],
  2499. "apply_id" => $rm,
  2500. "apply_name" => $ri,
  2501. "addrid" => $value['id'],
  2502. "post_name" => '',
  2503. "post_code" => '',
  2504. "post_fee" => '',
  2505. "sendtime" => date("Y-m-d H:i:s"),
  2506. "send_num" => $value['receipt_quantity'],
  2507. "check_num" => 0,
  2508. "error_num" => 0,
  2509. "status" => 0,
  2510. "addtime" => date("Y-m-d H:i:s"),
  2511. "updatetime" => date("Y-m-d H:i:s")
  2512. ];
  2513. $datainfo = Db::name('order_out')->insertGetId($data);
  2514. if (!$datainfo) {
  2515. $sti = ["order_code" => $dio['orderCode'], "status" => 0, "action_remark" => '', "action_type" => "create"];
  2516. ActionLog::logAdd($this->post['token'], $sti, "xsd", 0, $sti);
  2517. Db::rollback();
  2518. return error_show(1003, "创建失败");
  2519. } else {
  2520. //修改状态,添加待办
  2521. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
  2522. "order_code" => $outCode,//出库单号
  2523. "status" => 0,//这里的status是之前的值
  2524. "action_remark" => '',//备注
  2525. "action_type" => "create"//新建create,编辑edit,更改状态status
  2526. ], "CKD", 0, $data);
  2527. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], [
  2528. "order_type" => 'CKD',
  2529. "order_code" => $outCode,//出库单号
  2530. "order_id" => $datainfo,
  2531. "order_status" => 0,
  2532. "before_status" => 0,
  2533. 'holder_id'=>$data['apply_id'],
  2534. ]);
  2535. }
  2536. }
  2537. $wsm_in_code = makeNo("CF");
  2538. $orin = [
  2539. "wsm_in_code" => $wsm_in_code,
  2540. "cgdNo" => $cgd,
  2541. "wsm_code" => $wsm['wsm_code'],
  2542. "wsm_reaper" => "",
  2543. "send_num" => $dio['good_num'],
  2544. "post_company" => '',
  2545. "post_code" => '',
  2546. "post_fee" => '',
  2547. "sendtime" => date("Y-m-d H:i:s"),
  2548. "apply_id" => $rm,
  2549. "apply_name" => $ri,
  2550. "wait_num" => 0,
  2551. "status" => 4,
  2552. "addtime" => date("Y-m-d H:i:s"),
  2553. "updatetime" => date("Y-m-d H:i:s")
  2554. ];
  2555. $win = Db::name("purchease_in")->insert($orin);
  2556. if (!$win) {
  2557. $stv = ["order_code" => $wsm_in_code, "status" => 4, "action_remark" => '', "action_type" => "create"];
  2558. ActionLog::logAdd($this->post['token'], $stv, "xsd", 4, $stv);
  2559. Db::rollback();
  2560. return error_show(1003, "创建失败");
  2561. }
  2562. //维护台账
  2563. Db::execute("UPDATE `wsm_standing_book` SET `wsm_in_code`=CONCAT(IFNULL(`wsm_in_code`,''),',{$wsm_in_code}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `cgdNo`='{$cgd}'");
  2564. $good = Db::name("good_stock")->where(["wsm_code" => $wsm['wsm_code'], "good_type_code" => $zxinfo['cpNo'], "is_del" => 0])->find();
  2565. if (empty($good)) {
  2566. $good = [
  2567. "good_type_code" => $zxinfo['cpNo'],
  2568. "wsm_code" => $wsm['wsm_code'],
  2569. "usable_stock" => 0,
  2570. "wait_out_stock" => 0,
  2571. "total_stock" => 0,
  2572. "addtime" => date("Y-m-d H:i:s"),
  2573. "updatetime" => date("Y-m-d H:i:s"),
  2574. ];
  2575. }
  2576. $good['wait_out_stock'] += $dio['good_num'];
  2577. $good['total_stock'] = $good['usable_stock'] + $good['wait_out_stock'];
  2578. $good['updatetime'] = date("Y-m-d H:i:s");
  2579. $upd = Db::name("good_stock")->save($good);
  2580. if (!$upd) {
  2581. $ste = ["order_code" => $zxinfo['cpNo'], "status" => 0, "action_remark" => '', "action_type" => "create"];
  2582. ActionLog::logAdd($this->post['token'], $ste, "xsd", 0, $ste);
  2583. Db::rollback();
  2584. return error_show(1003, "创建失败");
  2585. }
  2586. }
  2587. }
  2588. }
  2589. Db::commit();
  2590. return error_show(0, "订单状态更新成功");
  2591. } catch (Exception $e) {
  2592. Db::rollback();
  2593. return error_show(1005, $e->getMessage());
  2594. }
  2595. }
  2596. public function fee()
  2597. {
  2598. $id = isset($this->post['id']) && $this->post['id'] !== "" ? trim($this->post['id']) : "";
  2599. if ($id == "") {
  2600. return error_show(1002, "参数id不能为空");
  2601. }
  2602. $etid = Db::name("sale")->where(["id" => $id, "is_del" => 0])->find();
  2603. if (empty($etid)) {
  2604. return error_show(1002, "未找到数据");
  2605. }
  2606. $vr = isset($this->post['order_addr']) && $this->post['order_addr'] !== "" ? $this->post['order_addr'] : "";
  2607. if ($vr == "") {
  2608. return error_show(1002, "参数order_addr不能为空");
  2609. }
  2610. Db::startTrans();
  2611. try {
  2612. $data = [
  2613. "id" => $id,
  2614. "status" => 2,
  2615. "is_del" => 0,
  2616. "updatetime" => date("Y-m-d H:i:s")
  2617. ];
  2618. $datainfo = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->save($data);
  2619. if ($datainfo) {
  2620. $stx = ["order_code" => $etid['orderCode'], "status" => $etid['status'], "action_remark" => '', "action_type" => "edit"];
  2621. ActionLog::logAdd($this->post['token'], $stx, "XSQRD", 2, $data);
  2622. $order = ["order_type" => "XSQRD", "order_code" => $etid['orderCode'], "order_id" => $id, "order_status" => $data['status'], "before_status" => 0,'holder_id'=>$etid['apply_id']];
  2623. ProcessOrder::AddProcess($this->post['token'], $order);
  2624. foreach ($vr as $value) {
  2625. $tm = [];
  2626. $tm['id'] = $value['id'];
  2627. $tm['orderCode'] = $etid['orderCode'];
  2628. $tm['post_fee'] = $value['post_fee'];
  2629. $tm['updatetime'] = date("Y-m-d H:i:s");
  2630. $dn = Db::name('order_addr')->save($tm);
  2631. if ($dn == false) {
  2632. Db::rollback();
  2633. return error_show(1002, "更新失败");
  2634. }
  2635. }
  2636. Db::commit();
  2637. return error_show(0, "更新成功");
  2638. }
  2639. Db::rollback();
  2640. return error_show(1003, "更新失败");
  2641. } catch (Exception $e) {
  2642. Db::rollback();
  2643. return error_show(1005, $e->getMessage());
  2644. }
  2645. }
  2646. // 废弃
  2647. public function out()
  2648. {
  2649. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  2650. $outCode = makeNo("DF");
  2651. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  2652. if ($orderCode == "") {
  2653. return error_show(1002, "参数orderCoder不能为空");
  2654. }
  2655. $der = Db::name('sale')->where(['orderCode' => $orderCode])->find();
  2656. if ($der == "") {
  2657. return error_show(1002, "未找到出库订单单号");
  2658. }
  2659. // $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code'] !== "" ? trim($this->post['wsm_code']) : "";
  2660. // if ($wsm_code == "") {
  2661. // return error_show(1002, "参数wsm_code不能为空");
  2662. // }
  2663. // $wsm = Db::name('sale_info')->where(['wsm_code' => $wsm_code, 'orderCode' => $der['orderCode']])->find();
  2664. // if ($wsm == "") {
  2665. // return error_show(1002, "未找到仓库编码");
  2666. // }
  2667. $post_name = isset($this->post['post_name']) && $this->post['post_name'] !== "" ? trim($this->post['post_name']) : "";
  2668. if ($post_name == "") {
  2669. return error_show(1002, "物流公司不能为空");
  2670. }
  2671. $post_code = isset($this->post['post_code']) && $this->post['post_code'] !== "" ? trim($this->post['post_code']) : "";
  2672. if ($post_code == "") {
  2673. return error_show(1002, "物流单号不能为空");
  2674. }
  2675. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !== "" ? intval($this->post['post_fee']) : "";
  2676. if ($post_fee == "") {
  2677. return error_show(1002, "物流费不能为空");
  2678. }
  2679. $send_num = isset($this->post['send_num']) && $this->post['send_num'] !== "" ? $this->post['send_num'] : "";
  2680. if ($send_num == "") {
  2681. return error_show(1002, "发货数量不能为空");
  2682. }
  2683. if ($send_num > $der['wsend_num']) {
  2684. return error_show(1002, "超出可发货数量");
  2685. }
  2686. $apply_id = GetUserInfo($token);
  2687. if (empty($apply_id) || $apply_id['code'] != 0) {
  2688. return error_show(1002, "申请人数据不存在");
  2689. }
  2690. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  2691. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  2692. $addrid = isset($this->post['addrid']) && $this->post['addrid'] !== "" ? intval($this->post['addrid']) : "";
  2693. $dr = Db::name('order_addr')->where(['id' => $addrid, 'is_del' => 0])->find();
  2694. if (empty($dr)) {
  2695. return error_show(1003, "收货人信息未找到");
  2696. }
  2697. if ($dr['orderCode'] !== $orderCode) {
  2698. return error_show(1002, "订单code不存在");
  2699. }
  2700. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !== "" ? $this->post['sendtime'] : date("Y-m-d H:i:s");
  2701. // $check_num = isset($this->post['check_num']) && $this->post['check_num'] !=="" ? $this->post['check_num'] :"";
  2702. // $error_num = isset($this->post['error_num']) && $this->post['error_num'] !=="" ? $this->post['error_num'] :"";
  2703. $status = isset($this->post['status']) && $this->post['status'] !== "" ? $this->post['status'] : "1";
  2704. //$order_type = isset($this->post['order_type']) && $this->post['order_type'] !=="" ? $this->post['order_type'] :"1";
  2705. //$dn=Db::name('sale_info')->where(['wsm_code'=>$outCode])->find();
  2706. Db::startTrans();
  2707. try {
  2708. $data = [
  2709. "wsm_code" => "",
  2710. "orderCode" => $orderCode,
  2711. "outCode" => $outCode,
  2712. "order_type" => $der['order_type'],
  2713. "apply_id" => $rm,
  2714. "apply_name" => $ri,
  2715. "addrid" => $dr['id'],
  2716. "post_name" => $post_name,
  2717. "post_code" => $post_code,
  2718. "post_fee" => $post_fee,
  2719. "sendtime" => $sendtime,
  2720. "send_num" => $send_num,
  2721. "check_num" => 0,
  2722. "error_num" => 0,
  2723. "status" => $status,
  2724. "addtime" => date("Y-m-d H:i:s"),
  2725. "updatetime" => date("Y-m-d H:i:s")
  2726. ];
  2727. $datainfo = Db::name('order_out')->insert($data, true);
  2728. if ($datainfo > 0) {
  2729. $stx = ["order_code" => $outCode, "status" => $status, "action_remark" => '', "action_type" => "create"];
  2730. ActionLog::logAdd($this->post['token'], $stx, $der['order_type'] == 1 ? "CKD" : 'ZXCKD', $status, $data);
  2731. $order = ["order_type" => "CKD", "order_code" => $outCode, "order_id" => $datainfo, "order_status" => $data['status'], "before_status" => 0, 'holder_id'=>$data['apply_id']
  2732. ];
  2733. ProcessOrder::AddProcess($this->post['token'], $order);
  2734. if ($status == 1) {
  2735. $item = $der['status'];
  2736. $der['send_num'] += $send_num;
  2737. $der['wsend_num'] -= $send_num;//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  2738. $der['send_status'] = $der['send_num'] == 0 ? 1 : $der['wsend_num'] == 0 ? 3 : 2;
  2739. $der['status'] = $der['send_num'] == 0 ? 3 : $der['wsend_num'] == 0 ? 5 : 4;
  2740. $der['updatetime'] = date("Y-m-d H:i:s");
  2741. $si = Db::name('sale')->save($der);
  2742. if ($si == false) {
  2743. Db::rollback();
  2744. return error_show(1003, "更新失败");
  2745. }
  2746. $stx = ["order_code" => $orderCode, "status" => $item, "action_remark" => '', "action_type" => "edit"];
  2747. ActionLog::logAdd($this->post['token'], $stx, "XSQRD", $der['status'], $der);
  2748. $order = ["order_type" => "XSQRD", "order_code" => $orderCode, "order_id" => $der['id'], "order_status" => $data['status'], "before_status" => 0,'holder_id'=>$der['apply_id']];
  2749. ProcessOrder::AddProcess($this->post['token'], $order);
  2750. }
  2751. Db::commit();
  2752. return error_show(0, "创建成功");
  2753. } else {
  2754. Db::rollback();
  2755. return error_show(1003, "创建失败");
  2756. }
  2757. } catch (Exception $e) {
  2758. Db::rollback();
  2759. return error_show(1005, $e->getMessage());
  2760. }
  2761. }
  2762. // 废弃
  2763. public function customer()
  2764. {
  2765. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  2766. if (empty($outCode)) {
  2767. return error_show(1002, "销售订单编号不能为空");
  2768. }
  2769. $codeinfo = Db::name('order_out')->where(['outCode' => $outCode])->find();
  2770. if ($codeinfo == "") {
  2771. return error_show(1003, "未找到订单数据");
  2772. }
  2773. // $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']) :"";
  2774. // if($orderCode==""){
  2775. // return error_show(1002,"参数orderCoder不能为空");
  2776. // }
  2777. $dr = Db::name('sale')->where(['orderCode' => $codeinfo['orderCode']])->find();
  2778. if ($dr == "") {
  2779. return error_show(1002, "未找到销售订单");
  2780. }
  2781. $status = isset($this->post['status']) && $this->post['status'] !== "" ? $this->post['status'] : "";
  2782. if ($status == "") {
  2783. return error_show(10002, "状态不能为空");
  2784. }
  2785. Db::startTrans();
  2786. try {
  2787. $time = $codeinfo['status'];
  2788. $codeinfo['status'] = $status;
  2789. $codeinfo['updatetime'] = date("Y-m-d H:i:s");
  2790. $cust = Db::name('order_out')->save($codeinfo);
  2791. if ($cust) {
  2792. $stx = ["order_code" => $outCode, "status" => $time, "action_remark" => '', "action_type" => "status"];
  2793. ActionLog::logAdd($this->post['token'], $stx, "CKD", $codeinfo['status'], $codeinfo);
  2794. $order = ["order_type" => "CKD", "order_code" => $outCode, "order_id" => $codeinfo['id'], "order_status" => $codeinfo['status'], "before_status" => 0, 'holder_id'=>$codeinfo['apply_id']];
  2795. ProcessOrder::AddProcess($this->post['token'], $order);
  2796. if ($status == 1) {
  2797. $stokc = Db::name("good_stock")->where(['spuCode' => $codeinfo['spuCode'], "wsm_code" => $codeinfo['wsm_code'],
  2798. "is_del" => 0])->find();
  2799. if ($stokc == false) {
  2800. Db::rollback();
  2801. return error_show(1002, "未找库存到数据");
  2802. } else {
  2803. if ($codeinfo['send_num'] > $stokc['usable_stock']) {
  2804. Db::rollback();
  2805. return error_show(1002, "超出库存数量");
  2806. }
  2807. $stokc['wait_out_stock'] += $codeinfo['send_num'];
  2808. $stokc['usable_stock'] -= $codeinfo['send_num'];
  2809. $stokc['updatetime'] = date("Y-m-d H:i:s");
  2810. }
  2811. $stoc = Db::name("good_stock")->save($stokc);
  2812. if ($stoc == false) {
  2813. Db::rollback();
  2814. return error_show(1002, "库存更新失败");
  2815. }
  2816. $item = $dr['status'];
  2817. $dr['send_num'] += $codeinfo['send_num'];
  2818. $dr['wsend_num'] -= $codeinfo['send_num'];//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  2819. $dr['send_status'] = $dr['send_num'] == 0 ? 1 : $dr['wsend_num'] == 0 ? 3 : 2;
  2820. $dr['status'] = $dr['send_num'] == 0 ? 3 : $dr['wsend_num'] == 0 ? 5 : 4;
  2821. $dr['updatetime'] = date("Y-m-d H:i:s");
  2822. $si = Db::name('sale')->save($dr);
  2823. if ($si == false) {
  2824. Db::rollback();
  2825. return error_show(1003, "更新失败");
  2826. }
  2827. $stx = ["order_code" => $dr['orderCode'], "status" => $item, "action_remark" => '', "action_type" => "edit"];
  2828. ActionLog::logAdd($this->post['token'], $stx, $dr['order_type'] == 2 ? "ZXQRD" : "XSQRD", $dr['status'],$dr);
  2829. $order = ["order_type" => "XSQRD", "order_code" => $dr['orderCode'], "order_id" => $dr['id'], "order_status" => $dr['status'], "before_status" => 0, 'holder_id' => $dr['apply_id']];
  2830. ProcessOrder::AddProcess($this->post['token'], $order);
  2831. //商品变动日志表,good_log_code字段存储销售订单编号
  2832. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $stoc['id'], "type" => 1, 'stock' => $codeinfo['send_num'], "stock_name" => "wait_out_stock"];
  2833. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $stoc['id'], "type" => 2, 'stock' => $codeinfo['send_num'], "stock_name" => "usable_stock"];
  2834. GoodLog::LogAdd($this->post['token'], $good_data, 'CKD');
  2835. }
  2836. if ($status == 3) {
  2837. $var = Db::name('good_stock')->where(['wsm_code' => $codeinfo['wsm_code'], 'good_type_code' => $dr['good_code']])->find();
  2838. if ($var == false) {
  2839. Db::rollback();
  2840. return error_show(1002, "未找到商品数据");
  2841. }
  2842. if ($codeinfo['send_num'] > $var['intra_stock']) {
  2843. Db::rollback();
  2844. return error_show(1003, "超出在途库存数量");
  2845. }
  2846. $var['intra_stock'] -= $codeinfo['send_num'];
  2847. $var['updatetime'] = date("Y-m-d H:i:s");
  2848. $stre = Db::name('good_stock')->save($var);
  2849. //商品变动日志表,good_log_code字段存储销售订单编号
  2850. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $var['id'], "type" => 2, 'stock' => $codeinfo['send_num'], "stock_name" => "intra_stock"];
  2851. GoodLog::LogAdd($this->post['token'], $good_data, $codeinfo['order_type'] == 2 ? "ZXCKD" : "CKD");
  2852. if ($stre == false) {
  2853. Db::rollback();
  2854. return error_show(1002, "状态更新失败");
  2855. }
  2856. }
  2857. Db::commit();
  2858. return error_show(0, "出库订单更新成功");
  2859. } else {
  2860. Db::rollback();
  2861. return error_show(1003, "出库订单更新失败");
  2862. }
  2863. } catch (Exception $e) {
  2864. Db::rollback();
  2865. return error_show(1005, $e->getMessage());
  2866. }
  2867. }
  2868. public function difflist()
  2869. {
  2870. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  2871. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  2872. $where = [];
  2873. $cgdNo = isset($this->post['orderCode']) && $this->post['orderCode'] != "" ? trim($this->post['orderCode']) : "";
  2874. if ($cgdNo != "") {
  2875. $where[] = ['a.orderCode', "like", "%$cgdNo%"];
  2876. }
  2877. $diffNo = isset($this->post['diffCode']) && $this->post['diffCode'] != "" ? trim($this->post['diffCode']) : "";
  2878. if ($diffNo != "") {
  2879. $where[] = ['a.diffCode', "like", "%$diffNo%"];
  2880. }
  2881. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  2882. if ($status !== "") {
  2883. $where[] = ['a.status', "=", $status];
  2884. }
  2885. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  2886. if ($start != "") {
  2887. $where[] = ["a.addtime", '>=', $start];
  2888. }
  2889. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  2890. if ($end != "") {
  2891. $where[] = ["a.addtime", '<=', $end];
  2892. }
  2893. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  2894. if ($supplierNo != "") {
  2895. $where[] = ['c.supplierNo', "like", "%$supplierNo%"];
  2896. }
  2897. $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
  2898. if ($companyNo != "") {
  2899. $where[] = ['c.customer_code', "like", "%$companyNo%"];
  2900. }
  2901. // $role=$this->checkRole();
  2902. // if(!empty($role['write'])){
  2903. // $where[]=["apply_id","in",$role['write']];
  2904. // }
  2905. // if(!empty($role['platform']) ){
  2906. // $where[]=["c.platform_id","in",$role['platform']];
  2907. // }
  2908. $count = Db::name('sale_diff')->alias('a')
  2909. ->join("sale c", "c.orderCode=a.orderCode", "left")
  2910. ->join("customer_info v", "v.companyNo=c.customer_code", "left")
  2911. ->where($where)->count();
  2912. $total = ceil($count / $size);
  2913. $page = $page >= $total ? intval($total) : $page;
  2914. $list = Db::name('sale_diff')->alias('a')
  2915. ->join("sale c", "c.orderCode=a.orderCode", "left")
  2916. ->join("customer_info v", "v.companyNo=c.customer_code", "left")
  2917. ->where($where)->page($page, $size)->field("a.*,c.customer_code,v.companyName,c.skuCode,c.order_type,c.supplierNo")
  2918. ->order("a.addtime desc")->select();
  2919. $data = [];
  2920. foreach ($list as $value) {
  2921. if ($value['order_type'] == 3) {
  2922. $goon = Db::name("good_zixun")->where(["spuCode" => $value['good_code'], "is_del" => 0])->find();
  2923. } else {
  2924. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  2925. ->where(['a.skuCode' => $value['skuCode']])->find();
  2926. }
  2927. $value['can'] = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  2928. $wsm = Db::name("business")->where(['companyNo' => $value['supplierNo']])->find();
  2929. $value['supplierNo'] = isset($wsm['companyNo']) ? $wsm['companyNo'] : "";
  2930. $value['supplierName'] = isset($wsm['company']) ? $wsm['company'] : "";
  2931. $data[] = $value;
  2932. }
  2933. return app_show(0, "获取成功", ['list' => $data, 'count' => $count]);
  2934. }
  2935. /**
  2936. * @return \think\response\Json|void
  2937. * @throws \think\db\exception\DataNotFoundException
  2938. * @throws \think\db\exception\DbException
  2939. * @throws \think\db\exception\ModelNotFoundException
  2940. */
  2941. //工差单审核
  2942. public function diffcheck()
  2943. {
  2944. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  2945. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  2946. if ($id === "") {
  2947. return error_show(1004, "参数id不能为空");
  2948. }
  2949. $info = Db::name("sale_diff")->where(["id" => $id])->find();
  2950. if (empty($info)) {
  2951. return error_show(1004, "订单数据未找到");
  2952. }
  2953. $sale = Db::name("sale")->where(["orderCode" => $info["orderCode"]])->find();
  2954. if ($sale == false) {
  2955. return error_show(1004, "订单数据未找到");
  2956. }
  2957. $cgd = Db::name("purchease_diff")->where(['id' => $info['cgd_diffid']])->find();
  2958. if ($cgd == false) {
  2959. return error_show(1004, "采购工差单数据未找到");
  2960. }
  2961. $is_act = isset($this->post['is_act']) && $this->post['is_act'] !== "" ? intval($this->post['is_act']) : "";
  2962. if ($is_act === '') {
  2963. return error_show(1004, "参数is_act不能为空");
  2964. }
  2965. $customer_remark = isset($this->post['customer_remark']) && $this->post['customer_remark'] !== "" ? trim($this->post['customer_remark']) : "";
  2966. if ($customer_remark === '') {
  2967. return error_show(1004, "参数customer_remark不能为空");
  2968. }
  2969. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "2";
  2970. $remark = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
  2971. $item = $info['status'];
  2972. $info['is_act'] = $is_act;
  2973. $info['customer_remark'] = $customer_remark;
  2974. $info['status'] = $status;
  2975. $info['remark'] = $remark;
  2976. $info['updatetime'] = date("Y-m-d H:i:s");
  2977. Db::startTrans();
  2978. try {
  2979. $up = Db::name("sale_diff")->save($info);
  2980. if ($up) {
  2981. $stx = ["order_code" => $info["orderCode"], "status" => $item, "action_remark" => '', "action_type" => "edit"];
  2982. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], $stx, "XSGCD", $info['status'], $info);
  2983. $order = ["order_type" => "XSGCD", "order_code" => $info["orderCode"], "order_id" => $info['id'], "order_status" => $info['status'], "before_status" => $item,'holder_id'=>$sale['apply_id']];
  2984. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], $order);
  2985. $upda = [
  2986. "status" => $status,
  2987. "remark" => $remark,
  2988. "updatetime" => date("Y-m-d H:i:s"),
  2989. ];
  2990. $old_purchease_diff = Db::name("purchease_diff")
  2991. ->field('id,cgdNo,status')
  2992. ->where(["id" => $info['cgd_diffid']])
  2993. ->find();
  2994. $cgdup = Db::name("purchease_diff")->where(["id" => $info['cgd_diffid']])->save($upda);
  2995. if ($cgdup) {
  2996. if ($status == 2) {
  2997. if ($customer_remark == 1) {
  2998. $sale['diff_weight'] = $info['diff_weight'];
  2999. $sale['diff_fee'] = $info['diff_price'];
  3000. $sale['updatetime'] = date("Y-m-d H:i:s");
  3001. $sal = Db::name("sale")->save($sale);
  3002. if ($sal == false) {
  3003. Db::rollback();
  3004. return error_show(1003, "订单更新工差失败");
  3005. }
  3006. }
  3007. $cgdup = Db::name("purchease_order")->where(["cgdNo" => $cgd['cgdNo']])->save
  3008. (["diff_weight" => $cgd['diff_weight'], "diff_fee" => $cgd['diff_price'], "updatetime" => date("Y-m-d H:i:s")]);
  3009. if ($cgdup == false) {
  3010. Db::rollback();
  3011. return error_show(1003, "采购订单更新工差失败");
  3012. }
  3013. }
  3014. $a = ["order_code" => $old_purchease_diff["cgdNo"], "status" => $old_purchease_diff['status'], "action_remark" => '', "action_type" => "status"];
  3015. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $a, "CGGCD", $upda['status'], $info);
  3016. $b = ["order_type" => "CGGCD", "order_code" => $old_purchease_diff["cgdNo"], "order_id" => $old_purchease_diff['id'], "order_status" => $upda['status'], "before_status" => $old_purchease_diff['status'], 'holder_id' => $cgd['apply_id']];
  3017. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $b);
  3018. Db::commit();
  3019. return app_show(0, "更新成功");
  3020. }
  3021. }
  3022. Db::rollback();
  3023. return error_show(1003, "更新失败");
  3024. } catch (\Exception $e) {
  3025. Db::rollback();
  3026. return error_show(1003, $e->getMessage());
  3027. }
  3028. }
  3029. /**
  3030. * @return \think\response\Json|void
  3031. * @throws \think\db\exception\DataNotFoundException
  3032. * @throws \think\db\exception\DbException
  3033. * @throws \think\db\exception\ModelNotFoundException
  3034. */
  3035. public function diffstatus()
  3036. {
  3037. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  3038. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  3039. if ($id === "") {
  3040. return error_show(1004, "参数id不能为空");
  3041. }
  3042. $info = Db::name("sale_diff")->where(["id" => $id])->find();
  3043. if ($info == false) {
  3044. return error_show(1004, "工差单数据未找到");
  3045. }
  3046. $sale = Db::name("sale")->where(["orderCode" => $info["orderCode"]])->find();
  3047. if ($sale == false) {
  3048. return error_show(1004, "订单数据未找到");
  3049. }
  3050. $cgd = Db::name("purchease_diff")->where(['id' => $info['cgd_diffid']])->find();
  3051. if ($cgd == false) {
  3052. return error_show(1004, "采购工差单数据未找到");
  3053. }
  3054. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "3";
  3055. $remark = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
  3056. $ite = $info['status'];
  3057. Db::startTrans();
  3058. try {
  3059. $upda = [
  3060. "status" => $status,
  3061. "remark" => $remark,
  3062. "updatetime" => date("Y-m-d H:i:s"),
  3063. ];
  3064. $up = Db::name("sale_diff")->where($info)->save($upda);
  3065. if ($up) {
  3066. $stx = ["order_code" => $info["orderCode"], "status" => $ite, "action_remark" => '', "action_type" => "status"];
  3067. ActionLog::logAdd($this->post['token'], $stx, "XSGCD", $status, $info);
  3068. $order = ["order_type" => "XSGCD", "order_code" => $info["orderCode"], "order_id" => $info['id'],"order_status" => $status, "before_status" => $ite,'holder_id'=>$sale['apply_id']];
  3069. ProcessOrder::AddProcess($this->post['token'], $order);
  3070. $cgdup = Db::name("purchease_diff")->where(["id" => $info['cgd_diffid']])->save($upda);
  3071. if ($cgdup) {
  3072. if ($status == 2 && $info['customer_remark'] == 1) {
  3073. $sale['diff_weight'] = $info['diff_weight'];
  3074. $sale['diff_fee'] = $info['diff_price'];
  3075. $sale['updatetime'] = date("Y-m-d H:i:s");
  3076. $sal = Db::name("sale")->save($sale);
  3077. if ($sal == false) {
  3078. Db::rollback();
  3079. return error_show(1003, "订单更新工差失败");
  3080. }
  3081. }
  3082. Db::commit();
  3083. return app_show(0, "更新成功");
  3084. }
  3085. }
  3086. Db::rollback();
  3087. return error_show(1003, "更新失败");
  3088. } catch (\Exception $e) {
  3089. Db::rollback();
  3090. return error_show(1003, $e->getMessage());
  3091. }
  3092. }
  3093. /**
  3094. * @return \think\response\Json|void
  3095. * @throws \think\db\exception\DataNotFoundException
  3096. * @throws \think\db\exception\DbException
  3097. * @throws \think\db\exception\ModelNotFoundException
  3098. */
  3099. public function diffinfo()
  3100. {
  3101. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  3102. if ($id === "") {
  3103. return error_show(1004, "参数id不能为空");
  3104. }
  3105. $info = Db::name('sale_diff')->where(["id" => $id])->find();
  3106. if (empty($info)) {
  3107. return error_show(1004, "工差订单数据未找到");
  3108. }
  3109. $einfo = Db::name('sale')->where(['orderCode' => $info['orderCode'], 'is_del' => 0])->find();
  3110. if ($einfo == false) {
  3111. return error_show(1002, "未找到销售订单数据");
  3112. }
  3113. if ($einfo['order_type'] == 3) {
  3114. $goon = Db::name("good_zixun")->where(["spuCode" => $einfo['good_code'], "is_del" => 0])->find();
  3115. } else {
  3116. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  3117. ->where(['a.skuCode' => $einfo['skuCode']])->find();
  3118. }
  3119. if ($goon == false) {
  3120. return error_show(1002, "未找到商品数据");
  3121. }
  3122. $info['skuCode'] = isset($goon['skuCode']) ? $goon['skuCode'] : "";
  3123. $info['spuCode'] = isset($goon['spuCode']) ? $goon['spuCode'] : "";
  3124. $info['order_type'] = isset($einfo['order_type']) ? $einfo['order_type'] : "";
  3125. $info['can'] = isset($info['cat_id']) && $info['cat_id'] !== 0 ? made($info['cat_id']) : [];
  3126. return app_show(0, "获取成功", $info);
  3127. }
  3128. public function saleout()
  3129. {
  3130. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  3131. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  3132. $where = [["a.is_del", "=", 0]];
  3133. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  3134. if ($orderCode !== "") {
  3135. $where[] = ['a.orderCode', "like", "%$orderCode%"];
  3136. }
  3137. $order_type = isset($this->post['order_type']) && $this->post['order_type'] !== "" ? trim($this->post['order_type']) : "";
  3138. if ($order_type !== "") {
  3139. $where[] = ['a.order_type', "=", $order_type];
  3140. }
  3141. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  3142. if ($outCode !== "") {
  3143. $where[] = ['a.outCode', "like", "%$outCode%"];
  3144. }
  3145. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !== "" ? trim($this->post['apply_name']) : "";
  3146. if ($apply_name !== "") {
  3147. $where[] = ['a.apply_name', "like", "%$apply_name%"];
  3148. }
  3149. $good_code = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : "";
  3150. if ($good_code !== "") {
  3151. $where[] = ['b.good_code', "like", "%$good_code%"];
  3152. }
  3153. $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim($this->post['good_name']) : "";
  3154. if ($good_name !== "") {
  3155. $where[] = ['b.good_name', "like", "%$good_name%"];
  3156. }
  3157. $supplier_name = isset($this->post['supplier_name']) && $this->post['supplier_name'] !== "" ? trim($this->post['supplier_name']) : "";
  3158. if ($supplier_name !== "") {
  3159. $where[] = ['wpo.supplier_name', "like", "%$supplier_name%"];
  3160. }
  3161. $status = isset($this->post['status']) && $this->post['status'] !== "" ? trim($this->post['status']) : "";
  3162. if ($status !== "") {
  3163. $where[] = ['a.status', "=", $status];
  3164. }
  3165. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  3166. if ($start != "") {
  3167. $where[] = ["a.addtime", '>=', $start." 00:00:00"];
  3168. }
  3169. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  3170. if ($end != "") {
  3171. $where[] = ["a.addtime", '<=', $end." 23:59:59"];
  3172. }
  3173. $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
  3174. if ($companyNo != "") {
  3175. $where[] = ['b.customer_code', "like", "%$companyNo%"];
  3176. }
  3177. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] !== "" ? $this->post['cgdNo'] : "";
  3178. if ($cgdNo != "") {
  3179. $where[] = ["os.cgdNo", 'like', '%' . $cgdNo . '%'];
  3180. }
  3181. $role = $this->checkRole();
  3182. $condition = '';
  3183. if (!empty($role['write']) && $this->uid != "") {
  3184. // $where[]=["a.apply_id","in",$role['write']];
  3185. $condition .= " (b.is_stock=1 and n.contactor = {$this->uid}) or (b.is_stock=0 and wpo.cgder_id = {$this->uid}) or a.apply_id in (" . implode(',',
  3186. $role['write']) . ")";
  3187. }
  3188. // if(!empty($role['platform']) ){
  3189. // $where[]=["b.platform_id","in",$role['platform']];
  3190. // }
  3191. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  3192. if ($company_name !== "") $where[] = ["a.apply_id", 'in', get_company_item_user_by_name($company_name)];
  3193. //发货时间筛选
  3194. $start_sendtime = isset($this->post['start_sendtime']) && $this->post['start_sendtime'] !== "" ? $this->post['start_sendtime'] : "";
  3195. $end_sendtime = isset($this->post['end_sendtime']) && $this->post['end_sendtime'] !== "" ? $this->post['end_sendtime'] : "";
  3196. if ($start_sendtime != "" && $end_sendtime != "") {
  3197. $where[] = ["a.sendtime", 'between', [$start_sendtime." 00:00:00", $end_sendtime." 23:59:59"]];
  3198. $where[] = ["a.status", '>=', 2];//搜索发货时间时,要指定状态为已发货及之后的状态值(0待发货,1待库管发货,2已发货待收货,3已收货,4已全部退货',)
  3199. }
  3200. $count = Db::name('order_out')
  3201. ->alias('a')
  3202. ->join("sale b", "b.orderCode=a.orderCode", "left")
  3203. ->join("customer_info v", "v.companyNo=b.customer_code", "left")
  3204. ->join("warehouse_info n", "n.wsm_code=a.wsm_code", "left")
  3205. ->leftJoin("depart_user u", "u.uid=a.apply_id AND u.is_del=0")
  3206. ->leftJoin("order_send os", "os.outCode=a.outCode")
  3207. ->leftJoin("purchease_order wpo", "wpo.cgdNo=os.cgdNo")
  3208. ->where($where)
  3209. ->where($condition)
  3210. ->count();
  3211. $total = ceil($count / $size);
  3212. $page = $page >= $total ? $total : $page;
  3213. $list = Db::name('order_out')
  3214. ->alias('a')
  3215. ->join("sale b", "b.orderCode=a.orderCode", "left")
  3216. ->join("customer_info v", "v.companyNo=b.customer_code", "left")
  3217. ->join("warehouse_info n", "n.wsm_code=a.wsm_code AND n.is_del=0", "left")
  3218. ->leftJoin("order_send os", "os.outCode=a.outCode")
  3219. ->leftJoin("purchease_order wpo", "wpo.cgdNo=os.cgdNo")
  3220. ->leftJoin("depart_user u", "u.uid=a.apply_id AND u.is_del=0")
  3221. ->field("a.*,b.order_type,b.order_source,b.good_name,b.good_code,b.skuCode,b.supplierNo,b.customer_code,v.companyName,b.origin_price,b.sale_price,b.total_price,os.cgdNo,u.itemid")
  3222. ->where($where)
  3223. ->where($condition)
  3224. ->order("addtime desc")
  3225. ->page($page, $size)
  3226. ->cursor();
  3227. $data = [];
  3228. foreach ($list as $value) {
  3229. $wsmcode = Db::name("warehouse_info")->alias("k")->leftJoin("supplier c", "k.supplierNo=c.code")
  3230. ->where(["k.wsm_code" => $value['wsm_code']])->field("k.name as wsm_name,c.name,c.code")->find();
  3231. $addr = Db::name("order_addr")->where(["id" => $value['addrid']])->find();
  3232. if($addr){
  3233. $addinfo =$addr['addr_code']!=''?json_decode($addr['addr_code'],true)??$addr['addr_code']: '' ;
  3234. if(is_string($addinfo) &&$addinfo!=''){
  3235. $addinfo=["provice_code"=>'',"city_code"=>'',"area_code"=>''];
  3236. list($addinfo['provice_code'],$addinfo['city_code'],$addinfo['area_code']) = explode(",",$addr['addr_code']);
  3237. }
  3238. $addr['addr_cn'] =GetAddr(json_encode($addinfo));
  3239. }
  3240. if ($value['order_type'] == 3) {
  3241. $goon = Db::name("good_zixun")->field('id,cat_id')->where(["spuCode" => $value['good_code'], "is_del" => 0])->find();
  3242. } else {
  3243. $goon = Db::name('good_platform')->field('a.id,b.cat_id')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')->where(['a.skuCode' => $value['skuCode']])->find();
  3244. }
  3245. $value['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  3246. $value['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
  3247. $value['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  3248. $value['wsm_has_account'] = (int)isset(checkHasAccountBySupplierNos([$wsmcode['code']])[$wsmcode['code']]);
  3249. $value['addr'] = isset($addr['addr']) ? $addr['addr_cn'].$addr['addr'] : "";
  3250. $value['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : "";
  3251. $value['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : "";
  3252. $value['can'] = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  3253. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  3254. $value['sendtime'] = $value['status'] < 2 ? '' : $value['sendtime'];
  3255. $data[] = $value;
  3256. }
  3257. return app_show(0, "获取成功", ['list' => $data, 'count' => $count]);
  3258. }
  3259. public function outinfo()
  3260. {
  3261. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  3262. if ($outCode == "") {
  3263. return error_show(1002, "参数outcode不能为空");
  3264. }
  3265. $codeinfo = Db::name("order_out")->where(['outCode' => $outCode, "is_del" => 0])->find();
  3266. if (empty($codeinfo)) {
  3267. return error_show(1002, "未找到出库数据");
  3268. }
  3269. $item = Db::name("sale")->where(['orderCode' => $codeinfo['orderCode']])->find();
  3270. if ($item['order_type'] == 3) {
  3271. $goodinfo = Db::name("good_zixun")->where(["spuCode" => $item['good_code'], "is_del" => 0])->find();
  3272. if ($goodinfo == false) {
  3273. return error_show(1004, "未找到商品数据");
  3274. }
  3275. } else {
  3276. $goodinfo = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  3277. ->where(['a.skuCode' => $item['skuCode']])->find();
  3278. if ($goodinfo == false) {
  3279. return error_show(1002, "未找到商品数据");
  3280. }
  3281. }
  3282. $int = isset($goodinfo['cat_id']) && $goodinfo['cat_id'] != 0 ? made($goodinfo['cat_id']) : [];
  3283. $addr = Db::name("order_addr")->where(["id" => $codeinfo['addrid']])->find();
  3284. if($addr){
  3285. $addinfo =$addr['addr_code']!=''?json_decode($addr['addr_code'],true)??$addr['addr_code']: '' ;
  3286. if(is_string($addinfo)&&$addinfo!=''){
  3287. $addinfo=["provice_code"=>'',"city_code"=>'',"area_code"=>''];
  3288. list($addinfo['provice_code'],$addinfo['city_code'],$addinfo['area_code']) = explode(",",$addr['addr_code']);
  3289. }
  3290. $addr['addr_cn'] =GetAddr(json_encode($addinfo));
  3291. }
  3292. $codeinfo['good_name'] = isset($item['good_name']) ? $item['good_name'] : "";
  3293. $codeinfo['good_num'] = isset($item['good_num']) ? $item['good_num'] : "";
  3294. $codeinfo['good_code'] = isset($item['good_code']) ? $item['good_code'] : "";
  3295. $codeinfo['origin_price'] = isset($item['origin_price']) ? $item['origin_price'] : "";
  3296. $codeinfo['sale_price'] = isset($item['sale_price']) ? $item['sale_price'] : "";
  3297. $codeinfo['total_price'] = isset($item['total_price']) ? $item['total_price'] : "";
  3298. $codeinfo['skuCode'] = isset($item['skuCode']) ? $item['skuCode'] : "";
  3299. $codeinfo['addr'] = $addr['addr_cn']??'';
  3300. $codeinfo['addr'] .= isset($addr['addr']) ? $addr['addr'] : "";
  3301. $codeinfo['addr_code'] = isset($addr['addr_code']) ? $addr['addr_code'] : "";
  3302. $codeinfo['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : "";
  3303. $codeinfo['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : "";
  3304. $codeinfo['sendtime'] = $codeinfo['status'] < 2 ? '' : $codeinfo['sendtime'];
  3305. $codeinfo['can'] = $int;
  3306. $codeinfo['order_source'] = $item['order_source'];
  3307. $orderReturn = Db::name("order_return")->where(["outCode" => $outCode, "is_del" => 0])->order("id desc")->find();
  3308. $codeinfo['order_return'] = $orderReturn;
  3309. //获取销售订单对应的供应商是否有启用账号
  3310. $codeinfo['has_account'] = 0;
  3311. $supplierNo = Db::name('order_num')
  3312. ->alias('a')
  3313. ->leftJoin('purchease_order b', 'b.cgdNo=a.cgdNo')
  3314. ->where('a.orderCode', $codeinfo['orderCode'])
  3315. ->value('b.supplierNo');
  3316. if ($supplierNo) {
  3317. $has_account = checkHasAccountBySupplierNos([$supplierNo]);
  3318. $codeinfo['has_account'] = (int)isset($has_account[$supplierNo]);
  3319. }
  3320. return app_show(0, "获取成功", $codeinfo);
  3321. }
  3322. public function outadd()
  3323. {
  3324. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  3325. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  3326. if ($orderCode == "") {
  3327. return error_show(1002, "参数orderCoder不能为空");
  3328. }
  3329. $retrun =Db::name("sale_return")->where([["orderCode","=",$orderCode],["is_del","=",0],["status","<",4]])
  3330. ->count();
  3331. if($retrun>0){
  3332. return error_show(1005,"存在未完成退货订单数据");
  3333. }
  3334. $der = Db::name('sale')->where(['orderCode' => $orderCode, "is_del" => 0])->find();
  3335. if ($der == "") {
  3336. return error_show(1002, "未找到出库订单单号");
  3337. }
  3338. $send_num = isset($this->post['send_num']) && $this->post['send_num'] !== "" ? $this->post['send_num'] : "";
  3339. if ($send_num == "") {
  3340. return error_show(1002, "发货数量不能为空");
  3341. }
  3342. $wsed = Db::name("order_out")->where(["orderCode" => $orderCode])->where([["status", "<", 2], ["is_del", "=", 0]])->sum
  3343. ('send_num');
  3344. if ($send_num > $der['wsend_num'] - $wsed) {
  3345. return error_show(1002, "超出可发货数量");
  3346. }
  3347. $contactor = isset($this->post['contactor']) && $this->post['contactor'] != "" ? trim($this->post['contactor']) : "";
  3348. if ($contactor == "") {
  3349. return error_show(1002, "参数contactor不能为空");
  3350. }
  3351. $mobile = isset($this->post['mobile']) && $this->post['mobile'] != "" ? trim($this->post['mobile']) : "";
  3352. if ($mobile == "") {
  3353. return error_show(1002, "参数mobile不能为空");
  3354. }
  3355. $addr = isset($this->post['addr']) && $this->post['addr'] != "" ? trim($this->post['addr']) : "";
  3356. if ($addr == "") {
  3357. return error_show(1002, "参数addr不能为空");
  3358. }
  3359. $addr_code = isset($this->post['addr_code']) && $this->post['addr_code'] != "" ? trim($this->post['addr_code']) : "";
  3360. if ($addr_code == "") {
  3361. return error_show(1002, "参数addr_code不能为空");
  3362. }
  3363. $apply_id = GetUserInfo($token);
  3364. if (empty($apply_id) || $apply_id['code'] != 0) {
  3365. return error_show(1002, "申请人数据不存在");
  3366. }
  3367. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  3368. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  3369. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !== "" ? $this->post['sendtime'] : date("Y-m-d H:i:s");
  3370. Db::startTrans();
  3371. try {
  3372. $addrlst = [
  3373. "orderCode" => $orderCode,
  3374. "addr" => $addr,
  3375. "addr_code" => json_encode($addr_code),
  3376. "contactor" => $contactor,
  3377. "mobile" => $mobile,
  3378. "customer_code" => $der['customer_code'],
  3379. "post_fee" => $der['post_fee'],
  3380. "arrive_time" => $sendtime,
  3381. "receipt_quantity" => $send_num,
  3382. "is_del" => 0,
  3383. "addtime" => date("Y-m-d H:i:s"),
  3384. "updatetime" => date("Y-m-d H:i:s")
  3385. ];
  3386. $addrid = Db::name("order_addr")->insert($addrlst, true);
  3387. if ($addrid > 0) {
  3388. $outCode = makeNo("DF");
  3389. if($der['order_type']!=1){
  3390. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  3391. ->lock(true)->find();
  3392. if ($order == false) {
  3393. Db::rollback();
  3394. return error_show(1004, "未找到可以发货得采购单数据");
  3395. }
  3396. $cgd = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
  3397. if ($cgd == false) {
  3398. Db::rollback();
  3399. return error_show(1002, "未找到对应的采购单数据");
  3400. }
  3401. $order['wsend_num'] -= $send_num;
  3402. $order['send_num'] += $send_num;
  3403. $or = Db::name("order_num")->save($order);
  3404. if ($or == false) {
  3405. Db::rollback();
  3406. return error_show(1002, "发货地址添加创建失败");
  3407. }
  3408. $tep = [
  3409. "cgdNo" => $order['cgdNo'],
  3410. "outCode" => $outCode,
  3411. "send_num" => $send_num,
  3412. "status" => 1,
  3413. "addtime" => date("Y-m-d H:i:s"),
  3414. "updatetime" => date("Y-m-d H:i:s")
  3415. ];
  3416. $sen = Db::name("order_send")->save($tep);
  3417. if ($sen == false) {
  3418. Db::rollback();
  3419. return error_show(1002, "发货地址添加创建失败");
  3420. }
  3421. }else{
  3422. $stockid=Db::name("sale_info")->where(["orderCode"=>$orderCode])->value("stockid","");
  3423. if($stockid==''){
  3424. Db::rollback();
  3425. return error_show(1005,"未找到对应库存仓库");
  3426. }
  3427. $cgd = Db::name("good_stock")->where(["id"=>$stockid])->findOrEmpty();
  3428. if(empty($cgd)){
  3429. Db::rollback();
  3430. return error_show(1005,"未找到对应库存仓库");
  3431. }
  3432. }
  3433. $data = [
  3434. "wsm_code" => isset($cgd['wsm_code']) ? $cgd['wsm_code'] : "",
  3435. "orderCode" => $orderCode,
  3436. "outCode" => $outCode,
  3437. "order_type" => $der['order_type'],
  3438. "apply_id" => $rm,
  3439. "apply_name" => $ri,
  3440. "addrid" => $addrid,
  3441. "post_name" => "",
  3442. "post_code" => "",
  3443. "post_fee" => "",
  3444. "sendtime" => $sendtime,
  3445. "send_num" => $send_num,
  3446. "check_num" => 0,
  3447. "error_num" => 0,
  3448. "status" => 1,
  3449. "addtime" => date("Y-m-d H:i:s"),
  3450. "updatetime" => date("Y-m-d H:i:s")
  3451. ];
  3452. if ($der['is_stock'] == 0) {
  3453. //维护商品所在仓库的库存 --- start
  3454. $temp = Db::name('good_stock')
  3455. ->field('id,usable_stock,wait_out_stock')
  3456. ->where(['spuCode' => $der['good_code'], 'wsm_code' => $cgd['wsm_code']])
  3457. ->find();
  3458. if (($temp == false || ($temp['usable_stock'] - $send_num) < 0) && $der['send_type'] == 1) {
  3459. Db::rollback();
  3460. return error_show(1004, '库存不足');
  3461. } else {
  3462. if (($temp == false || ($temp['usable_stock'] - $send_num) < 0) && $der['send_type'] == 2) {
  3463. $data['status'] = 0;
  3464. } else {
  3465. $up = Db::name('good_stock')
  3466. ->where('id', $temp['id'])
  3467. ->update([
  3468. 'usable_stock' => $temp['usable_stock'] - $send_num,
  3469. 'wait_out_stock' => $temp['wait_out_stock'] + $send_num,
  3470. 'updatetime' => date('Y-m-d H:i:s'),
  3471. ]);
  3472. if ($up == false) {
  3473. Db::rollback();
  3474. return error_show(1004, '库存更新失败');
  3475. }
  3476. // ::todo
  3477. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $temp['id'], "type" => 1, 'stock' => $send_num, "stock_name" => "wait_out_stock"];
  3478. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $temp['id'], "type" => 2, 'stock' => $send_num, "stock_name" => "usable_stock"];
  3479. GoodLog::LogAdd($this->post['token'], $good_data, "CKD");
  3480. }
  3481. }
  3482. }
  3483. $datainfo = Db::name('order_out')->insert($data, true);
  3484. if ($datainfo > 0) {
  3485. //修改状态,添加待办
  3486. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
  3487. "order_code" => $data['outCode'],//出库单号
  3488. "status" => $data['status'],//这里的status是之前的值
  3489. "action_remark" => '',//备注
  3490. "action_type" => "create"//新建create,编辑edit,更改状态status
  3491. ], "CKD", $data['status'], $data);
  3492. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], [
  3493. "order_type" => 'CKD',
  3494. "order_code" => $data['outCode'],//出库单号
  3495. "order_id" => $datainfo,
  3496. "order_status" => $data['status'],
  3497. "before_status" => 0,
  3498. 'holder_id'=>$data['apply_id']
  3499. ]);
  3500. //维护台账记录
  3501. Db::name('standing_book')
  3502. ->where('orderCode', $orderCode)
  3503. ->update([
  3504. 'order_type' => $der['order_type'],
  3505. 'order_source' => $der['order_source'],
  3506. 'cgdNo' => $order['cgdNo'],
  3507. 'customer_code' => $der['customer_code'],
  3508. 'updatetime' => date('Y-m-d H:i:s'),
  3509. ]);
  3510. Db::execute("UPDATE `wsm_standing_book` SET `outCode`=CONCAT(IFNULL(`outCode`,''),',{$outCode}') WHERE `cgdNo`='{$cgd['cgdNo']}'");
  3511. Db::commit();
  3512. return app_show(0, "出库单新建成功");
  3513. }
  3514. }
  3515. Db::rollback();
  3516. return error_show(1004, "出库单新建失败");
  3517. } catch (Exception $e) {
  3518. Db::rollback();
  3519. return error_show(1005, $e->getMessage());
  3520. }
  3521. }
  3522. //库管发货
  3523. public function outSend()
  3524. {
  3525. $outCode = isset($this->post['outCode']) && $this->post['outCode'] != "" ? trim($this->post['outCode']) : "";
  3526. if ($outCode == "") {
  3527. return error_show(1004, "参数outCode不能为空");
  3528. }
  3529. $outinfo = Db::name("order_out")->where(["outCode" => $outCode, "is_del" => 0])->find();
  3530. if ($outinfo == false) {
  3531. return error_show(1004, "发货数据未找到");
  3532. }
  3533. $einfo = Db::name('sale')->where(['orderCode' => $outinfo['orderCode'], 'is_del' => 0])->find();
  3534. if (empty($einfo)) {
  3535. return error_show(1002, "未找到销售订单数据");
  3536. }
  3537. $is_reurn = Db::name("sale_return")->where(['orderCode' => $outinfo['orderCode'], 'is_del' => 0])->where("status", "<", 4)->find();
  3538. if ($is_reurn != false) {
  3539. return error_show(1002, "销售单存在退货未处理完成");
  3540. }
  3541. $post_name = isset($this->post['post_name']) && $this->post['post_name'] != "" ? trim($this->post['post_name']) : "";
  3542. if ($post_name == "") {
  3543. return error_show(1004, "参数post_name不能为空");
  3544. }
  3545. $post_code = isset($this->post['post_code']) && $this->post['post_code'] != "" ? trim($this->post['post_code']) : "";
  3546. if ($post_code == "") {
  3547. return error_show(1004, "参数post_code不能为空");
  3548. }
  3549. $remark = isset($this->post['remark']) && $this->post['remark'] != "" ? trim($this->post['remark']) : "";
  3550. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] != "" ? floatval($this->post['post_fee']) : "";
  3551. $outinfo['post_name'] = $post_name;
  3552. $outinfo['post_code'] = $post_code;
  3553. $outinfo['post_fee'] = $post_fee;
  3554. $outinfo['sendtime'] = date("Y-m-d H:i:s");
  3555. $old_outinfo_status = $outinfo['status'];
  3556. $outinfo['status'] = 2;
  3557. $outinfo['updatetime'] = date("Y-m-d H:i:s");
  3558. $outinfo['remark'] = $remark;
  3559. if ($einfo['wsend_num'] < $outinfo['send_num']) {
  3560. return error_show(1004, "订单未发货数量不足");
  3561. }
  3562. Db::startTrans();
  3563. try {
  3564. $up = Db::name("order_out")->save($outinfo);
  3565. $user_info = GetUserInfo($this->post['token']);
  3566. $uid = isset($user_info['data']['id']) ? $user_info['data']['id'] : 0;
  3567. $uname = isset($user_info['data']['nickname']) ? $user_info['data']['nickname'] : '';
  3568. if ($up) {
  3569. //修改状态,添加待办
  3570. ActionLog::logAdd(['id' => $uid, 'nickname' => $uname], [
  3571. "order_code" => $outinfo['outCode'],//出库单号
  3572. "status" => $old_outinfo_status,//这里的status是之前的值
  3573. "action_remark" => '',//备注
  3574. "action_type" => "edit"//新建create,编辑edit,更改状态status
  3575. ], "CKD", $outinfo['status'], $outinfo);
  3576. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $uname], [
  3577. "order_type" => 'CKD',
  3578. "order_code" => $outinfo['outCode'],//出库单号
  3579. "order_id" => $outinfo['id'],
  3580. "order_status" => $outinfo['status'],
  3581. "before_status" => $old_outinfo_status,
  3582. 'wait_id'=>$outinfo['apply_id'],
  3583. 'wait_name'=>$outinfo['apply_name'],
  3584. 'holder_id'=>$outinfo['apply_id'],
  3585. ]);
  3586. if($einfo['wsend_num']<$outinfo['send_num']){
  3587. Db::rollback();
  3588. return error_show(1002, "订单待付货数量不足");
  3589. }
  3590. $orderstatus = $einfo['status'];
  3591. $einfo['send_num'] += $outinfo['send_num'];
  3592. $einfo['wsend_num'] -= $outinfo['send_num'];
  3593. $einfo['status'] = $einfo['wsend_num'] == 0 ? 2 : ($einfo['send_num'] == 0 ? 0 : 1);
  3594. $einfo['send_status'] = $einfo['wsend_num'] == 0 ? 3 : ($einfo['send_num'] == 0 ? 1 : 2);
  3595. $einfo['updatetime'] = date("Y-m-d H:i:s");
  3596. $saleup = Db::name("sale")->save($einfo);
  3597. if ($saleup == false) {
  3598. Db::rollback();
  3599. return error_show(1002, "销售单状态更新失败");
  3600. }
  3601. // }
  3602. //修改状态,添加待办
  3603. ActionLog::logAdd(['id' => $uid, 'nickname' => $uname], [
  3604. "order_code" => $einfo['orderCode'],//出库单号
  3605. "status" => $orderstatus,//这里的status是之前的值
  3606. "action_remark" => '',//备注
  3607. "action_type" => "edit"//新建create,编辑edit,更改状态status
  3608. ], "XSQRD", $einfo['status'], $einfo);
  3609. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $uname], [
  3610. "order_type" => 'XSQRD',
  3611. "order_code" => $einfo['orderCode'],//出库单号
  3612. "order_id" => $einfo['id'],
  3613. "order_status" => $einfo['status'],
  3614. "before_status" => $orderstatus,
  3615. 'holder_id' => $einfo['apply_id']
  3616. ]);
  3617. $stokc = Db::name("good_stock")->where(['spuCode' => $einfo['good_code'], "wsm_code" => $outinfo['wsm_code'], "is_del" => 0])->find();
  3618. if ($stokc == false) {
  3619. Db::rollback();
  3620. return error_show(1002, "未找到库存数据");
  3621. } else {
  3622. if ($outinfo['send_num'] > $stokc['wait_out_stock']) {
  3623. Db::rollback();
  3624. return error_show(1002, "超出库存数量");
  3625. }
  3626. $stokc['wait_out_stock'] -= $outinfo['send_num'];
  3627. $stokc['total_stock'] = $stokc['wait_out_stock'] + $stokc['usable_stock'];
  3628. // $stokc['intra_stock']+=$outinfo['send_num'];
  3629. $stokc['updatetime'] = date("Y-m-d H:i:s");
  3630. }
  3631. $stoc = Db::name("good_stock")->save($stokc);
  3632. if ($stoc == false) {
  3633. Db::rollback();
  3634. return error_show(1002, "库存更新失败");
  3635. }
  3636. if($einfo["order_type"]!=1){
  3637. $outsend =Db::name("order_send")->where(["outCode"=> $outCode])->findOrEmpty();
  3638. if(empty($outsend)){
  3639. Db::rollback();
  3640. return error_show(1002, "未找到关联采购单");
  3641. }
  3642. $cgdinfo= Db::name("purchease_order")->where(["cgdNo"=>$outsend['cgdNo'],"is_del"=>0])
  3643. ->findOrEmpty();
  3644. $bn_code =makeNo("BN");
  3645. $bnin=GoodStockInfo::AddBn($stokc["id"],$bn_code,$outinfo['send_num'],$cgdinfo['good_price']??0);
  3646. if($bnin==false){
  3647. Db::rollback();
  3648. return error_show(1004,"库存bn数据新增失败");
  3649. }
  3650. $bnup =GoodStockInfo::OrderBn($outinfo['orderCode'],$stokc["id"],$outinfo['send_num']);
  3651. if($bnup==false){
  3652. Db::rollback();
  3653. return error_show(1004,"库存bn库存数更新失败");
  3654. }
  3655. }
  3656. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $stokc['id'], "type" => 2, 'stock' => $outinfo['send_num'], "stock_name" => "wait_out_stock"];
  3657. GoodLog::LogAdd(['id' => $uid, 'nickname' => $uname], $good_data, "CKD");
  3658. Cache::store("redis")->handler()->lPush("SENDOUT", $outCode);
  3659. Db::commit();
  3660. } else {
  3661. Db::rollback();
  3662. return error_show(1004, "发货失败");
  3663. }
  3664. //如果是有赞订单的话,将发货信息推到有赞
  3665. //有赞信息有可能推送失败(比如超过72小时,不允许多次修改等),所以不应该和这里的事务放到一起
  3666. if ($einfo['order_source'] == 5) {
  3667. $res = curl_request(config('app.yz_domain') . 'api/yz_out_send', ['orderCode' => $einfo['orderCode'], 'out_stype' => $post_name, 'post_code' => $post_code, 'uid' => $uid, 'uname' => $uname, 'order_out' => $outCode]);
  3668. $res = json_decode($res, true);
  3669. if ($res['code'] != 0) return app_show(0, '发货成功,' . $res['message']);
  3670. }
  3671. return app_show(0, "发货成功");
  3672. } catch (\Exception $e) {
  3673. Db::rollback();
  3674. return error_show(1004, $e->getMessage());
  3675. }
  3676. }
  3677. public function RelaCgd($outinfo, array &$standing_book_da = [])
  3678. {
  3679. $cgd = Db::name("order_bk")->where([["spuCode", "=", $outinfo['spuCode']], ["is_del", "=", 0], ["balance_num", ">=", $outinfo['good_num']], ['companyNo', "=", $outinfo['companyNo']]])->lock(true)->find();
  3680. if ($cgd == false) {
  3681. return false;
  3682. }
  3683. $good = Db::name("good")->where(["spuCode" => $outinfo['spuCode'], "is_del" => 0])->find();
  3684. if ($good == false) {
  3685. return false;
  3686. }
  3687. $cgdinfo = Db::name("purchease_order")->where(['cgdNo' => $cgd['cgdNo'], "is_del" => 0])->find();
  3688. if ($cgdinfo == false) {
  3689. return false;
  3690. }
  3691. $QrdCgd = [
  3692. "cgdNo" => makeNo("CG"),
  3693. "bkcode" => $cgdinfo['bkcode'],
  3694. 'wsm_code' => $cgdinfo['wsm_code'],
  3695. "cgder_id" => $cgdinfo['cgder_id'],
  3696. "cgder" => $cgdinfo['cgder'],
  3697. "spuCode" => $cgdinfo['spuCode'],
  3698. "good_name" => $cgdinfo['good_name'],
  3699. "good_num" => $outinfo['good_num'],
  3700. "good_price" => $cgdinfo['good_price'],
  3701. "total_fee" => round($cgdinfo['good_price'] * $outinfo['good_num'], 2),
  3702. "pakge_fee" => $cgdinfo['pakge_fee'],
  3703. "cert_fee" => $cgdinfo['cert_fee'],
  3704. "open_fee" => $cgdinfo['open_fee'],
  3705. "delivery_fee" => $cgdinfo['delivery_fee'],
  3706. "mark_fee" => $cgdinfo['mark_fee'],
  3707. "teach_fee" => $cgdinfo['teach_fee'],
  3708. "nake_fee" => $cgdinfo['nake_fee'],
  3709. "demo_fee" => $cgdinfo['demo_fee'],
  3710. "weight" => $cgdinfo['weight'],
  3711. "diff_weight" => $cgdinfo['diff_weight'],
  3712. "diff_fee" => $cgdinfo['diff_fee'],
  3713. "gold_price" => $cgdinfo['gold_price'],
  3714. "supplierNo" => $cgdinfo['supplierNo'],
  3715. "supplier_name" => $cgdinfo['supplier_name'],
  3716. "companyNo" => $cgdinfo['companyNo'],
  3717. "send_status" => 3,
  3718. "send_num" => $outinfo['good_num'],
  3719. "wsend_num" => 0,
  3720. "remark" => $cgdinfo['remark'],
  3721. "status" => 3,
  3722. "lasttime" => $cgdinfo['lasttime'],
  3723. "is_del" => 0,
  3724. "order_type" => $outinfo['order_type'],
  3725. "order_source" => $outinfo['order_source'],
  3726. "good_type" => $cgdinfo['good_type'],
  3727. "addtime" => date("Y-m-d H:i:s"),
  3728. "updatetime" => date("Y-m-d H:i:s"),
  3729. 'good_createrid' => $good['createrid'],
  3730. 'good_creater' => $good['creater'],//商品创建人
  3731. ];
  3732. $insetrCgd = Db::name("purchease_order")->insert($QrdCgd);
  3733. if ($insetrCgd == false) {
  3734. return false;
  3735. } else {
  3736. $standing_book_da = array_merge($standing_book_da, [
  3737. 'orderCode' => $outinfo['orderCode'],
  3738. 'cgdNo' => $cgdinfo['cgdNo'],
  3739. 'spuCode' => $outinfo['spuCode'],
  3740. 'order_type' => $QrdCgd['order_type'],
  3741. 'order_source' => $QrdCgd['order_source'],
  3742. 'supplierNo' => $cgdinfo['supplierNo'],
  3743. 'companyNo' => $cgdinfo['companyNo'],
  3744. 'bk_code' => $cgdinfo['bkcode'],
  3745. 'purchease_id' => Db::name('purchease')->where('bk_code', $cgdinfo['bkcode'])->value('id'),
  3746. ]);
  3747. }
  3748. if ($good['is_gold_price'] == 1 && $good['is_stock'] == 1) {
  3749. $gold = Db::name("gold_price1")
  3750. ->field('id,price')
  3751. ->where(["type" => $good['noble_metal'], "is_del" => 0, "status" => 1])
  3752. ->order("addtime desc")
  3753. ->find();
  3754. //$saleprice(最终售价) = (打样费/购买数量 + 开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价+物流费)/(1-成本售价/100);
  3755. $sale_price = $good['demo_fee'] / $outinfo['good_num'] + $good['open_fee'] / $outinfo['good_num'] + $good['noble_weight'] * $gold["price"] + $outinfo['cost_fee'] * $good['noble_weight'] + $cgdinfo['pakge_fee'] + $cgdinfo['mark_fee'] + $cgdinfo['cert_fee'] + $cgdinfo['nake_fee'] + $cgdinfo['delivery_fee'];
  3756. $ct['cgd_gold_price'] = $gold["price"];
  3757. $updat = [
  3758. "sale_price" => $sale_price,
  3759. "total_price" => round($sale_price * $outinfo['good_num'], 2),
  3760. "origin_price" => $cgdinfo['good_price'],
  3761. "gold_price" => $gold["price"],
  3762. ];
  3763. $upsale = Db::name("sale")->where(["orderCode" => $outinfo['orderCode']])->update($updat);
  3764. if ($upsale == false) {
  3765. return false;
  3766. }
  3767. }
  3768. $merge_num = Db::name("purchease_order")->where(["bkcode" => $cgdinfo['bkcode'], "order_type" => 1, "is_del" => 0])
  3769. ->where("order_source", "<>", 0)
  3770. ->field("sum(send_num)-sum(th_num) as num")->find();
  3771. $cgd['balance_num'] = $cgd['total_num'] - $merge_num['num'];
  3772. $cgd['merge_num'] = $merge_num['num'];
  3773. $cgd['updatetime'] = date("Y-m-d H:i:s");
  3774. $up = Db::name("order_bk")->save($cgd);
  3775. if ($up == false) {
  3776. return false;
  3777. }
  3778. $data = [
  3779. "orderCode" => $outinfo['orderCode'],
  3780. "cgdNo" => $QrdCgd['cgdNo'],
  3781. "spuCode" => $outinfo['spuCode'],
  3782. "companyNo" => $outinfo['companyNo'],
  3783. "good_num" => $outinfo['good_num'],
  3784. "wsend_num" => $outinfo['good_num'],
  3785. "send_num" => 0,
  3786. "wait_num" => $outinfo['good_num'],
  3787. "status" => 1,
  3788. "source" => 1,
  3789. ];
  3790. $order = Db::name("order_num")->save($data);
  3791. if ($order == false) {
  3792. return false;
  3793. }
  3794. return true;
  3795. }
  3796. public function addother()
  3797. {
  3798. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] != "" ? trim($this->post['orderCode']) : "";
  3799. if ($orderCode == "") {
  3800. return error_show(1004, "参数orderCode不能为空");
  3801. }
  3802. $order = Db::name("sale")->where(["orderCode" => $orderCode, "is_del" => 0])->find();
  3803. if ($order == false) {
  3804. return error_show(1004, "未找到订单数据");
  3805. }
  3806. $paytime = isset($this->post['paytime']) && $this->post['paytime'] != "" ? $this->post['paytime'] : "";
  3807. $workNo = isset($this->post['workNo']) && $this->post['workNo'] != "" ? trim($this->post['workNo']) : "";
  3808. $platform_order = isset($this->post['platform_order']) && $this->post['platform_order'] != "" ? trim($this->post['platform_order']) : "";
  3809. $paytime == "" ? "" : $order['paytime'] = $paytime;
  3810. $order['workNo'] = $workNo;
  3811. $order['platform_order'] = $platform_order;
  3812. $order['updatetime'] = date("Y-m-d H:i:s");
  3813. $sa = Db::name("sale")->save($order);
  3814. if ($sa) {
  3815. return app_show(0, "更新成功");
  3816. } else {
  3817. return error_show(1004, "更新失败");
  3818. }
  3819. }
  3820. public function getPrice()
  3821. {
  3822. $skuCode = isset($this->post['skuCode']) && $this->post['skuCode'] != "" ? trim($this->post['skuCode']) : "";
  3823. if ($skuCode === "") {
  3824. return error_show(1003, "参数skuCode不能为空");
  3825. }
  3826. $is_activity = isset($this->post['is_activity']) && $this->post['is_activity'] !== "" ? intval($this->post['is_activity'])
  3827. : "";
  3828. if ($is_activity === "") {
  3829. return error_show(1003, "参数is_activity不能为空");
  3830. }
  3831. $sale_num = isset($this->post['sale_num']) && $this->post['sale_num'] !== "" ? intval($this->post['sale_num']) : "";
  3832. if ($sale_num === "") {
  3833. return error_show(1003, "参数sale_num不能为空");
  3834. }
  3835. $ct = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  3836. ->where(['a.skuCode' => $skuCode])->find();
  3837. if ($ct == false) {
  3838. return error_show(1002, "未找到商品数据");
  3839. }
  3840. $stock = 0;
  3841. if ($ct['is_stock'] == 1) {
  3842. $good_stock = Db::name("good_stock")->alias("a")->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")->where
  3843. (["spuCode" => $ct['spuCode'], "a.is_del" => 0, "a.status" => 1, "b.wsm_type" => 5])->field("a.id,a.usable_stock,a.wait_out_stock")->find();
  3844. // $good_stock = Db::name("good_stock")->where(["spuCode" => $ct['spuCode'], "is_del" => 0])->find();
  3845. $stock = isset($good_stock['usable_stock']) ? $good_stock['usable_stock'] : "0";
  3846. }
  3847. if ($is_activity == 1) {
  3848. $act = Db::name("activity_info")->alias("a")->leftJoin("good_activity b", "a.activity_code=b.activity_code")
  3849. ->where(["a.skuCode" => $skuCode, "a.is_del" => 0, "a.status" => 1, "b.status" => 6, "b.is_del" => 0])->find();
  3850. if ($act == false) {
  3851. return error_show(1003, "未找到相关活动价");
  3852. }
  3853. if ($act['moq_num'] > $sale_num) {
  3854. return error_show(1003, "商品不满足活动价起订量{$act['moq_num']}");
  3855. }
  3856. if ($act['activity_stock'] < $sale_num) {
  3857. return error_show(1003, "商品活动库存剩余{$act['activity_stock']}");
  3858. }
  3859. return app_show(0, "获取成功", ['sale_price' => $act['activity_price'], "stock" => $act['activity_stock']]);
  3860. } else {
  3861. //good_nake 成本阶梯 good_ladder销售阶梯
  3862. $origin = Db::name("good_nake")->where([["spuCode", "=", $ct['spuCode']], ["is_del", "=", 0]])->order("min_num asc")->find();
  3863. if (!$origin) {
  3864. return error_show(1003, "没有找到成本数据");
  3865. }
  3866. $good = Db::name("good_ladder")->where(["skuCode" => $skuCode, "is_del" => 0, "status" => 1])->order("min_num asc")->find();
  3867. if (!$good) {
  3868. return error_show(1003, "未找到相关阶梯价格");
  3869. }
  3870. if (!isset($origin['min_num'])) $origin['min_num'] = 0;
  3871. //如果是库存品的话,只取销售的库存
  3872. if ($ct['is_stock'] == 1) $lastnum = $good['min_num'];
  3873. else $lastnum = max($origin['min_num'] ?? 0, $good['min_num']);
  3874. if ($sale_num < $lastnum) {
  3875. return error_show(1003, "商品不满足起订量{$lastnum}");
  3876. }
  3877. $good_temp = Db::name("good_ladder")->where(["skuCode" => $skuCode, "is_del" => 0, "status" => 1])->where('min_num', '<=', $sale_num)->order("min_num desc")->find();
  3878. if (!$good_temp) {
  3879. return error_show(1003, "未找到相关阶梯价格");
  3880. }
  3881. $cat_top_list = made($ct['cat_id']);
  3882. $cat_top_id = isset($cat_top_list[0]['id']) ? $cat_top_list[0]['id'] : 0;
  3883. if ($ct['is_gold_price'] == 1 && $cat_top_id == 6) {
  3884. $gold = Db::name("gold_price1")
  3885. ->field('id,price')
  3886. ->where(["type" => $ct['noble_metal'], "is_del" => 0, "status" => 1])
  3887. ->order("addtime desc")
  3888. ->find();
  3889. $saleprice = $ct['demo_fee'] / $sale_num + $ct['open_fee'] / $sale_num + $ct['noble_weight'] * $gold["price"] + $good_temp['cost_fee'] * $ct['noble_weight'] + $origin['package_fee'] + $origin['mark_fee'] + $origin['cert_fee'] + $origin['nake_fee'] + $origin['delivery_fee'];
  3890. return app_show(0, "获取成功", ['sale_price' => $saleprice, "stock" => $stock]);
  3891. }
  3892. return app_show(0, "获取成功", ['sale_price' => $good_temp['sale_price'], "stock" => $stock]);
  3893. }
  3894. }
  3895. public function saleuse()
  3896. {
  3897. $orderCode = isset($this->post['orderCode']) && !empty($this->post['orderCode']) ? $this->post['orderCode'] : "";
  3898. if ($orderCode == "") {
  3899. return error_show(1003, "参数orderCode不能为空");
  3900. }
  3901. $order = Db::name("sale")->where(["orderCode" => $orderCode, "is_del" => 0])->select()->toArray();
  3902. if (empty($order)) {
  3903. return error_show(1003, "订单数据未找到");
  3904. }
  3905. $useid = isset($this->post['useid']) && $this->post['useid'] != "" ? intval($this->post['useid']) : "";
  3906. if ($useid == "") {
  3907. return error_show(1003, "参数useid不能为空");
  3908. }
  3909. $use = Db::name("order_use")->where(["id" => $useid, "is_del" => 0])->find();
  3910. if ($use == false) {
  3911. return error_show(1003, "用途数据未找到");
  3912. }
  3913. $asve = ['use_order' => $useid, "updatetime" => date("Y-m-d H:i:s")];
  3914. $up = Db::name("sale")->where(["orderCode" => $orderCode, "is_del" => 0])->update($asve);
  3915. if ($up > 0) {
  3916. return app_show(0, "更新成功");
  3917. } else {
  3918. return error_show(1003, "更新失败");
  3919. }
  3920. }
  3921. public function goodzxinfo()
  3922. {
  3923. $orderCode = isset($this->post['spuCode']) && $this->post['spuCode'] != '' ? trim($this->post['spuCode']) : "";
  3924. if ($orderCode == "") {
  3925. return error_show(1003, "参数spuCode不能为空");
  3926. }
  3927. $order = Db::name("good_zixun")->where(["spuCode" => $orderCode, "is_del" => 0])->find();
  3928. if (empty($order)) {
  3929. return error_show(1003, "订单数据未找到");
  3930. }
  3931. $unit = Db::name("unit")->where(["id" => $order['good_unit']])->find();
  3932. if ($order['brand_id'] != 0) {
  3933. $brand = Db::name("brand")->where(["id" => $order['brand_id']])->find();
  3934. $order["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  3935. } else {
  3936. $order["brand_name"] = "";
  3937. $order["brand_id"] = "";
  3938. }
  3939. $order['specinfo'] = json_decode($order['specinfo'], true);
  3940. $order['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  3941. $order['cat_info'] = made($order['cat_id'], []);
  3942. $order['noble_name'] = isset($order['noble_metal']) && $order['noble_metal'] != 0 ? $this->noble[$order['noble_metal']] : "";
  3943. if ($order['is_gold_price'] == 1) {
  3944. $price = Db::name("gold_price1")->where(["type" => $order['noble_metal'], "status" => 1, "is_del" => 0])->order("addtime desc")->find();
  3945. }
  3946. $supplier = Db::name("supplier")->where(["code" => $order['supplierNo']])->find();
  3947. $order['supplier_name'] = isset($supplier['name']) ? $supplier['name'] : "";
  3948. $order['cgder'] = isset($supplier['person']) ? $supplier['person'] : "";
  3949. $order['cgderid'] = isset($supplier['personid']) ? $supplier['personid'] : "";
  3950. $palt = Db::name("platform")->where(["id" => $order['platform_id']])->find();
  3951. $order['platform_name'] = isset($palt['platform_name']) ? $palt['platform_name'] : "";
  3952. $company = Db::name("business")->where(["companyNo" => $order['companyNo']])->find();
  3953. $order['company'] = isset($company['company']) ? $company['company'] : "";
  3954. $order["gold_price"] = isset($price['price']) ? $price['price'] : 0;
  3955. $order["noble_weight"] = isset($order['weight']) ? $order['weight'] : 0;
  3956. return app_show(0, "获取成功", $order);
  3957. }
  3958. //获取采反商品详情,当数据不在good_zixun中,模仿goodzxinfo
  3959. public function getGoodZxInfoByNotZixun()
  3960. {
  3961. $param = $this->request->only(['spuCode'], 'post', 'trim');
  3962. $val = Validate::rule(['spuCode' => 'require']);
  3963. if (!$val->check($param)) return error_show(1004, $val->getError());
  3964. $order = Db::name('consult_bids')
  3965. ->field(true)
  3966. ->where(['spuCode' => $param['spuCode'], 'is_del' => 0])
  3967. ->find();
  3968. if (empty($order)) return error_show(1003, "数据未找到");
  3969. // $orderCode= isset($this->post['spuCode'])&&$this->post['spuCode']!=''?trim($this->post['spuCode']):"";
  3970. // if($orderCode==""){
  3971. // return error_show(1003,"参数spuCode不能为空");
  3972. // }
  3973. // $order= Db::name("good_zixun")->where(["spuCode"=>$orderCode,"is_del"=>0])->find();
  3974. // if(empty($order)){
  3975. // return error_show(1003,"订单数据未找到");
  3976. // }
  3977. $unit = Db::name("unit")->where(["id" => $order['unit_id']])->find();
  3978. if ($order['brand_id'] != 0) {
  3979. $brand = Db::name("brand")->where(["id" => $order['brand_id']])->find();
  3980. $order["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  3981. } else {
  3982. $order["brand_name"] = "";
  3983. $order["brand_id"] = "";
  3984. }
  3985. $order['specinfo'] = json_decode($order['specinfo'], true);
  3986. $order['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  3987. $order['cat_info'] = made($order['cat_id'], []);
  3988. $order['noble_name'] = isset($order['metal_id']) && $order['metal_id'] != 0 ? $this->noble[$order['metal_id']] : "";
  3989. if ($order['is_gold_price'] == 1) {
  3990. $price = Db::name("gold_price1")->where(["type" => $order['metal_id'], "status" => 1, "is_del" => 0])->order("addtime desc")->find();
  3991. }
  3992. $supplier = Db::name("supplier")->where(["code" => $order['supplierNo']])->find();
  3993. $order['supplier_name'] = isset($supplier['name']) ? $supplier['name'] : "";
  3994. // $palt =Db::name("platform")->where(["id"=>$order['platform_id']])->find();
  3995. // $order['platform_name']=isset($palt['platform_name']) ? $palt['platform_name']:"";
  3996. $order["gold_price"] = isset($price['price']) ? $price['price'] : 0;
  3997. $order["noble_weight"] = isset($order['weight']) ? $order['weight'] : 0;
  3998. $good_info = Db::name('good_basic')
  3999. ->field('id,createrid,creater')
  4000. ->where('is_del',0)
  4001. ->where('spuCode',$order['spuCode'])
  4002. ->findOrEmpty();
  4003. //补充商品创建人字段
  4004. // 2022-9-7号确认 creater创建人即采购单的采购员 咨询cgder 只是供应商负责人
  4005. $order['good_createrid']=$good_info['createrid']??$order['createrid'];
  4006. $order['good_creater']=$good_info['creater']??$order['creater'];
  4007. return app_show(0, "获取成功", $order);
  4008. }
  4009. //发货申请单导出
  4010. public function exportSaleOut()
  4011. {
  4012. $outCodes = $this->request->post('outCodes', [], 'trim');
  4013. if (empty($outCodes)) return error_show(1004, '要导出的发货申请单编号不能为空');
  4014. $i = 1;
  4015. $send_type = [1 => '直接发货', 2 => '延时发货'];
  4016. $status = [0 => '待发货', 1 => '待库管发货', 2 => '已发货待收货', 3 => '已收货', 4 => '已全部退货'];
  4017. $list = Db::name("order_out")
  4018. ->alias('a')
  4019. ->field('"" as 序号,a.addtime as 创建时间,po.cgdNo as 采购单编号,po.status as 采购单状态,po.cgder as 采购员,b.orderCode as 确认单号,b.addtime as 确认单时间,b.good_code as 产品编号,b.good_name as 产品名称,"" as 规格,"" as 单位,po.supplier_name as 供应商名称,po.supplierNo as 供应商编号,po.nake_fee as 裸价,a.post_fee as 物流费,b.send_type as 发货方式,po.good_price as 采购单价,b.remark as 确认单备注,po.good_num as 采购数量,po.total_fee as 采购货款,a.send_num 发货数量,oa.contactor as 收货人,oa.mobile as 联系方式,oa.addr as 收货地址,oa.addr_code,oa.arrive_time as 到货时间,a.outCode as 发货申请单号,po.order_type,b.platform_order 平台订单号,a.post_name 发货申请单物流公司,a.post_code 发货申请单物流单号,a.post_fee 发货申请单物流费用,a.remark 发货申请单备注,a.apply_name 申请人,b.cat_id 商品分类')
  4020. ->whereIn('a.outCode', $outCodes)
  4021. ->order("a.addtime desc")
  4022. ->withAttr('序号', function () use (&$i) {
  4023. return $i++;
  4024. })->withAttr('采购单状态', function ($val) use ($status) {
  4025. return isset($status[$val]) ? $status[$val] : '';
  4026. })->withAttr('发货方式', function ($val) use ($send_type) {
  4027. return isset($send_type[$val]) ? $send_type[$val] : '';
  4028. })->where("a.is_del", "=", 0)
  4029. ->leftJoin("sale b", "b.orderCode=a.orderCode")
  4030. ->leftJoin("order_addr oa", "oa.id=a.addrid AND oa.is_del=0")
  4031. ->leftJoin('order_send os', 'os.outCode=a.outCode ')
  4032. ->leftJoin("purchease_order po", "po.cgdNo=os.cgdNo AND po.is_del=0")
  4033. ->select()
  4034. ->toArray();
  4035. foreach ($list as &$value) {
  4036. if ($value['order_type'] == 3) {
  4037. $temp = Db::name("good_zixun")
  4038. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  4039. ->field('id,specinfo,good_unit')
  4040. ->find();
  4041. $good_unit = isset($temp['good_unit']) ? $temp['good_unit'] : 0;
  4042. $specinfo = isset($temp['specinfo']) ? json_decode($temp['specinfo'], true) : [];
  4043. $speclist = [];
  4044. foreach ($specinfo as $val) {
  4045. $speclist[] = $val['spec_name'] . ':' . $val['spec_value_name'];
  4046. }
  4047. } else {
  4048. $good_unit = Db::name('good_basic')
  4049. ->where(['spuCode' => $value['产品编号']])
  4050. ->value('good_unit', 0);
  4051. $spec = Db::name("good_spec")
  4052. ->field('id,spec_id,spec_value_id')
  4053. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  4054. ->select()
  4055. ->toArray();
  4056. $speclist = [];
  4057. if (!empty($spec)) {
  4058. foreach ($spec as $val) {
  4059. $speclist[] = Db::name("specs")->where(["id" => $val['spec_id']])->value('spec_name', '') . ':' . Db::name("spec_value")->where(["id" => $val['spec_value_id']])->value('spec_value', '');
  4060. }
  4061. }
  4062. }
  4063. $value['规格'] = empty($speclist) ? '' : implode(',', $speclist);;
  4064. $value['单位'] = $good_unit ? Db::name('unit')->where(['id' => $good_unit, 'is_del' => 0])->value('unit', '') : '';
  4065. $value['商品分类'] = implode('/', array_column(made($value['商品分类']), 'name'));
  4066. $value['发货申请单物流单号'] = ' ' . $value['发货申请单物流单号'];//添加空格防止将数字转为科学计数法(比如,韵达的快递单号就是纯数字)
  4067. $addinfo =$value['addr_code']!=''?json_decode($value['addr_code'],true)??$value['addr_code']: '' ;
  4068. if(is_string($addinfo)&&$addinfo!=''){
  4069. $addinfo=["provice_code"=>'',"city_code"=>'',"area_code"=>''];
  4070. list($addinfo['provice_code'],$addinfo['city_code'],$addinfo['area_code']) = explode(",",$value['addr_code']);
  4071. }
  4072. $addr_cn =GetAddr(json_encode($addinfo));
  4073. $value['收货地址'] = $addr_cn.$value['收货地址'];
  4074. unset($value['order_type']);
  4075. unset($value['addr_code']);
  4076. }
  4077. if (empty($list)) $list[] = '没有可供导出的数据';
  4078. $headerArr = array_keys($list[0]);
  4079. excelSave('发货申请单导出' . date('YmdHis'), $headerArr, $list);
  4080. }
  4081. //发货申请单 签收(目前只改动状态,后面要实时查询物流信息,以判断是否签收)
  4082. public function saleoutReceipt()
  4083. {
  4084. $outCode = $this->request->post('outCode', '', 'trim');
  4085. if (empty($outCode)) return error_show(1004, '发货申请单编号不能为为空');
  4086. Db::startTrans();
  4087. try {
  4088. $rs = Db::name('order_out')
  4089. ->field('id,orderCode,apply_id')
  4090. ->where(['outCode' => $outCode, 'status' => 2, "is_del" => 0])
  4091. ->find();
  4092. if (empty($rs)) throw new Exception('该出库单不存在或不允许确认收货');
  4093. //发货单收货
  4094. $res_order_out = Db::name('order_out')
  4095. ->where(['id' => $rs['id'], 'status' => 2, "is_del" => 0])
  4096. ->update(['status' => 3, 'updatetime' => date('Y-m-d H:i:s')]);
  4097. //查询该发货单所属的销售单,是否还有未收货的其他发货单
  4098. $other_res_sale = Db::name('order_out')
  4099. ->where(['orderCode' => $rs['orderCode'], 'status' => 2, "is_del" => 0])
  4100. ->where('id', '<>', $rs['id'])
  4101. ->find();
  4102. if (empty($other_res_sale)) {
  4103. //所属销售单也确认收货
  4104. Db::name('sale')
  4105. ->where(['orderCode' => $rs['orderCode'], 'status' => 0])
  4106. ->update(['status' => 2, 'updatetime' => date('Y-m-d H:i:s')]);
  4107. }
  4108. //修改状态,添加待办
  4109. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
  4110. "order_code" => $outCode,
  4111. "status" => 2,//这里的status是之前的值
  4112. "action_remark" => '',//备注
  4113. "action_type" => "status"//新建create,编辑edit,更改状态status
  4114. ], "CKD", 3, $rs);
  4115. //将待办数据变为已办
  4116. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  4117. "order_code" => $outCode,
  4118. "order_id" => $rs['id'],
  4119. "order_status" => 3,
  4120. "order_type" => 'CKD',
  4121. "before_status" => 2,
  4122. 'holder_id' => $rs['apply_id']
  4123. ]);
  4124. if ($res_order_out) {
  4125. Db::commit();
  4126. return app_show(0, '收货完成');
  4127. } else {
  4128. Db::rollback();
  4129. error_show(1005, '收货失败');
  4130. }
  4131. } catch (\Exception $e) {
  4132. Db::rollback();
  4133. return error_show(1005, $e->getMessage());
  4134. }
  4135. }
  4136. //库管批量发货
  4137. public function outSendBatchByImport()
  4138. {
  4139. $param = $this->request->only(['list', 'token'], 'post', 'trim');
  4140. if (empty($param['list'])) return error_show(1005, 'list参数不能为空');
  4141. //只允许处理所属供应商下的采购单关联的发货单
  4142. $suppliers = Db::name('order_out')
  4143. ->alias('a')
  4144. ->leftJoin('order_num b', 'b.orderCode=a.orderCode')
  4145. ->leftJoin('purchease_order c', 'c.cgdNo=b.cgdNo')
  4146. ->where([
  4147. ['a.is_del', '=', 0],
  4148. ['c.order_type', '<>', 1],
  4149. ['a.outCode', 'in', array_column($param['list'], 'outCode')],
  4150. ])->column('c.supplierNo', 'a.outCode');
  4151. //检查这些供应商编码是否开通了账号
  4152. $temp_res = SupplierRelationUser::where(['is_del' => SupplierUser::$is_del_normal, 'status' => SupplierUser::$status_normal])
  4153. ->whereIn('supplierNo', array_values($suppliers))
  4154. ->column('supplierNo');
  4155. $temp = [];
  4156. foreach ($suppliers as $k => $v) {
  4157. if (in_array($v, $temp_res)) $temp[] = $k;
  4158. }
  4159. if (!empty($temp)) return json_show(1003, '不允许处理已开通账号的供应商的单子', $temp);
  4160. // $user = GetUserInfo($param['token']);
  4161. $createrid = $this->uid;
  4162. $creater = $this->uname;
  4163. //所有发货单信息
  4164. $order_out_infos = Db::name("order_out")
  4165. ->where('status', 1)
  4166. ->where('is_del', 0)
  4167. ->whereIn('outCode', array_column($param['list'], 'outCode'))
  4168. ->column('id,status,send_num,orderCode,wsm_code,apply_id,apply_name', 'outCode');
  4169. //处理数据
  4170. Db::startTrans();
  4171. try {
  4172. $good_data = [];
  4173. $temp_out_codes = [];
  4174. foreach ($param['list'] as $value) {
  4175. if (!isset($order_out_infos[$value['outCode']])) throw new Exception($value['outCode'] . '该发货单未找到或状态不允许发货');
  4176. //每次循环都需要重新查询对应数据,为了数据及时更新
  4177. $sale_infos = Db::name("sale")
  4178. ->where('orderCode', $order_out_infos[$value['outCode']]['orderCode'])
  4179. ->where('is_del', 0)
  4180. ->column('id,send_num,wsend_num,status,good_code,order_source,order_type', 'orderCode');
  4181. if (!isset($sale_infos[$order_out_infos[$value['outCode']]['orderCode']])) throw new Exception($value['outCode'] . '该发货单对应的销售单未找到');
  4182. //判断发货单是否重复
  4183. if (isset($temp_out_codes[$value['outCode']])) {
  4184. Db::rollback();
  4185. return app_show(1005, '下列发货申请单号重复', [$value]);
  4186. } else $temp_out_codes[$value['outCode']] = $value['outCode'];
  4187. //【发货单】
  4188. //查询已有发货单信息及状态
  4189. //更新发货单
  4190. $up = Db::name("order_out")
  4191. ->where(['id' => $order_out_infos[$value['outCode']]['id'], 'outCode' => $value['outCode'], 'status' => 1, "is_del" => 0])
  4192. ->update([
  4193. 'post_name' => isset($value['post_name']) ? $value['post_name'] : '',
  4194. 'post_code' => $value['post_code'],
  4195. 'post_fee' => $value['post_fee'],
  4196. 'sendtime' => date('Y-m-d H:i:s'),
  4197. 'status' => 2,
  4198. 'updatetime' => date('Y-m-d H:i:s'),
  4199. 'remark' => isset($value['remark']) ? $value['remark'] : '',
  4200. ]);
  4201. if ($up) {
  4202. //修改状态,添加待办
  4203. ActionLog::logAdd(['id' => $createrid, 'nickname' => $creater], [
  4204. "order_code" => $value['outCode'],//出库单号
  4205. "status" => $order_out_infos[$value['outCode']]['status'],//这里的status是之前的值
  4206. "action_remark" => '',//备注
  4207. "action_type" => "edit"//新建create,编辑edit,更改状态status
  4208. ], "CKD", 2, $order_out_infos[$value['outCode']]);
  4209. ProcessOrder::AddProcess(['id' => $createrid, 'nickname' => $creater], [
  4210. "order_type" => 'CKD',
  4211. "order_code" => $value['outCode'],//出库单号
  4212. "order_id" => $order_out_infos[$value['outCode']]['id'],
  4213. "order_status" => 2,
  4214. "before_status" => $order_out_infos[$value['outCode']]['status'],
  4215. 'wait_id' => $order_out_infos[$value['outCode']]['apply_id'],
  4216. 'wait_name' => $order_out_infos[$value['outCode']]['apply_name'],
  4217. 'holder_id' => $order_out_infos[$value['outCode']]['apply_id'],
  4218. ]);
  4219. //【销售单】
  4220. $einfo = [];
  4221. $einfo['send_num'] = $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['send_num'] + $order_out_infos[$value['outCode']]['send_num'];
  4222. $einfo['wsend_num'] = $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['wsend_num'] - $order_out_infos[$value['outCode']]['send_num'];
  4223. $einfo['status'] = $einfo['wsend_num'] == 0 ? 2 : ($einfo['send_num'] == 0 ? 0 : 1);
  4224. $einfo['send_status'] = $einfo['wsend_num'] == 0 ? 3 : ($einfo['send_num'] == 0 ? 1 : 2);
  4225. $einfo['updatetime'] = date("Y-m-d H:i:s");
  4226. $saleup = Db::name("sale")
  4227. ->where('id', $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['id'])
  4228. ->update($einfo);
  4229. if ($saleup == false) throw new Exception($value['outCode'] . '销售单状态更新失败');
  4230. //修改状态,添加待办
  4231. ActionLog::logAdd(['id' => $createrid, 'nickname' => $creater], [
  4232. "order_code" => $order_out_infos[$value['outCode']]['orderCode'],//销售单号
  4233. "status" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['status'],//这里的status是之前的值
  4234. "action_remark" => '',//备注
  4235. "action_type" => "edit"//新建create,编辑edit,更改状态status
  4236. ], "XSQRD", $einfo['status'], $einfo);
  4237. ProcessOrder::AddProcess(['id' => $createrid, 'nickname' => $creater], [
  4238. "order_type" => 'XSQRD',
  4239. "order_code" => $order_out_infos[$value['outCode']]['orderCode'],//出库单号
  4240. "order_id" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['id'],
  4241. "order_status" => $einfo['status'],
  4242. "before_status" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['status'],
  4243. 'holder_id' => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['apply_id']
  4244. ]);
  4245. //【库存】
  4246. $stock = Db::name("good_stock")
  4247. ->field('id,wait_out_stock,usable_stock,total_stock,updatetime')
  4248. ->where([
  4249. "is_del" => 0,
  4250. 'spuCode' => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['good_code'],
  4251. "wsm_code" => $order_out_infos[$value['outCode']]['wsm_code'],
  4252. ])->find();
  4253. if ($stock == false) throw new Exception($value['outCode'] . '未找到库存数据');
  4254. else {
  4255. if ($order_out_infos[$value['outCode']]['send_num'] > $stock['wait_out_stock']) throw new Exception($value['outCode'] . '超出库存数量');
  4256. $stock['wait_out_stock'] -= $order_out_infos[$value['outCode']]['send_num'];
  4257. $stock['total_stock'] = $stock['wait_out_stock'] + $stock['usable_stock'];
  4258. $stock['updatetime'] = date("Y-m-d H:i:s");
  4259. $stock_rs = Db::name("good_stock")
  4260. ->where('id', $stock['id'])
  4261. ->update($stock);
  4262. if ($stock_rs == false) throw new Exception($value['outCode'] . '库存更新失败');
  4263. if($sale_infos[$order_out_infos[$value['outCode']]['orderCode']]["order_type"]!=1){
  4264. $outsend =Db::name("order_send")->where(["outCode"=> $value['outCode']])->findOrEmpty();
  4265. if(empty($outsend)){
  4266. Db::rollback();
  4267. return error_show(1002, "未找到关联采购单");
  4268. }
  4269. $cgdinfo= Db::name("purchease_order")->where(["cgdNo"=>$outsend['cgdNo'],"is_del"=>0])
  4270. ->findOrEmpty();
  4271. $bn_code =makeNo("BN");
  4272. $bnin=GoodStockInfo::AddBn($stock["id"],$bn_code,$order_out_infos[$value['outCode']]['send_num'],$cgdinfo['good_price']??0);
  4273. if($bnin==false){
  4274. Db::rollback();
  4275. return error_show(1004,"库存bn数据新增失败");
  4276. }
  4277. $bnup =GoodStockInfo::OrderBn($order_out_infos[$value['outCode']]['orderCode'],$stock["id"],$order_out_infos[$value['outCode']]['send_num']);
  4278. if($bnup==false){
  4279. Db::rollback();
  4280. return error_show(1004,"库存bn库存数更新失败");
  4281. }
  4282. }
  4283. $good_data[] = [
  4284. 'good_log_code' => $value['outCode'],
  4285. 'stock_id' => $stock['id'],
  4286. 'type' => 2,
  4287. 'stock' => $order_out_infos[$value['outCode']]['send_num'],
  4288. 'stock_name' => 'wait_out_stock'
  4289. ];
  4290. }
  4291. } else throw new Exception($value['outCode'] . '发货失败');
  4292. Cache::store("redis")->handler()->lPush("SENDOUT",$value['outCode']);
  4293. }
  4294. if (!empty($good_data)) GoodLog::LogAdd(['id' => $createrid, 'nickname' => $creater], $good_data, "CKD");
  4295. Db::commit();
  4296. return app_show(0, '发货成功');
  4297. } catch (Exception $exception) {
  4298. Db::rollback();
  4299. return error_show(1004, $exception->getMessage());
  4300. }
  4301. }
  4302. //获取物流信息
  4303. public function getLogisticsInformation()
  4304. {
  4305. $post_code = $this->request->post('post_code', '', 'trim');
  4306. if (empty($post_code)) return error_show(1004, '快递单号不能为空');
  4307. $postCode = explode(",", $post_code);
  4308. $rs = Db::name('express_data')
  4309. ->where('post_code', $postCode[0])
  4310. ->where('status', ">=", 0)
  4311. ->withAttr('post_data', function ($val) {
  4312. return json_decode($val, true);
  4313. })->findOrEmpty();
  4314. settype($rs, 'object');//格式统一转为对象
  4315. return app_show(0, '请求成功', $rs);
  4316. }
  4317. }