Sale.php 258 KB

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