Sale.php 258 KB

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