Sale.php 274 KB

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