Sale.php 288 KB

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