Sale.php 262 KB

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