Sale.php 230 KB

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