Sale.php 256 KB

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