Sale.php 213 KB

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