Sale.php 232 KB

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