Sale.php 241 KB

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