Sale.php 256 KB

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