Sale.php 260 KB

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