Sale.php 218 KB

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