Sale.php 287 KB

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