Sale.php 290 KB

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