Sale.php 251 KB

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