Sale.php 279 KB

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