Sale.php 259 KB

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