Sale.php 215 KB

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