Sale.php 254 KB

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