Sale.php 277 KB

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