Sale.php 257 KB

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