Sale.php 255 KB

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