Sale.php 260 KB

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