Sale.php 274 KB

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