Sale.php 281 KB

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