Sale.php 263 KB

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