Sale.php 248 KB

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