Sale.php 277 KB

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