Sale.php 213 KB

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