Sale.php 219 KB

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