Sale.php 256 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035
  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. ->field('s.*,u.itemid')
  2104. ->leftJoin("depart_user u", "u.uid=s.apply_id AND u.is_del=0")->where($where)
  2105. ->order("s.addtime desc")
  2106. ->page($page, $size)
  2107. ->cursor();
  2108. $data = [];
  2109. foreach ($list as $value) {
  2110. if ($value['order_type'] == 3) {
  2111. $var = Db::name("good_zixun")->where(["spuCode" => $value['good_code'], "is_del" => 0])->find();
  2112. $value["speclist"] = isset($var['specinfo']) && $var['specinfo'] != "" ? json_decode($var['specinfo'], true) : "";
  2113. } else {
  2114. $var = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  2115. ->where(['a.skuCode' => $value['skuCode']])->find();
  2116. $spec = Db::name("good_spec")->where(["spuCode" => $var['spuCode'], "is_del" => 0])->select()->toArray();
  2117. $speclist = [];
  2118. if (!empty($spec)) {
  2119. foreach ($spec as $val) {
  2120. $temp = [];
  2121. $temp['id'] = $val['id'];
  2122. $temp['spuCode'] = $val['spuCode'];
  2123. $temp['spec_id'] = $val['spec_id'];
  2124. $temp['spec_value_id'] = $val['spec_value_id'];
  2125. $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
  2126. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  2127. $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
  2128. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  2129. $speclist[] = $temp;
  2130. }
  2131. }
  2132. $value["speclist"] = empty($speclist) ? [] : $speclist;
  2133. }
  2134. $value['can'] = isset($var['cat_id']) && $var['cat_id'] != 0 ? made($var['cat_id']) : [];
  2135. $value['supplierName'] = '';
  2136. if ($value['supplierNo'] != "") {
  2137. $supplier = Db::name("business")->where(["companyNo" => $value['supplierNo']])->find();
  2138. $value['supplierName'] = isset($supplier['company']) ? $supplier['company'] : "";
  2139. }
  2140. $value['customerName'] = '';
  2141. if ($value['customer_code'] != "") {
  2142. $supplier = Db::name("customer_info")->where(["companyNo" => $value['customer_code']])->find();
  2143. $value['customerName'] = isset($supplier['companyName']) ? $supplier['companyName'] : "";
  2144. }
  2145. $value['useage'] = "";
  2146. if ($value['use_order'] != 0) {
  2147. $use = Db::name("order_use")->where(["id" => $value['use_order']])->find();
  2148. $value['useage'] = isset($use['order_use']) ? $use['order_use'] : "";
  2149. } else {
  2150. $value['use_order'] = '';
  2151. }
  2152. $value['good_num'] -= $value['th_num'];
  2153. $value['send_num'] -= $value['th_num'];
  2154. $value['total_price'] = round($value['total_price'] - $value['th_fee'], 2);
  2155. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  2156. //是否具有编辑权限
  2157. $value['is_allow_update'] = 0;
  2158. if (in_array($this->roleid, [1, 33]) || in_array($value['apply_id'], $role[DataGroupModel::$type_可编辑])) $value['is_allow_update'] = 1;
  2159. $data[] = $value;
  2160. }
  2161. return app_show(0, "获取成功", ['list' => $data, 'count' => $count]);
  2162. }
  2163. public function edit()
  2164. {
  2165. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  2166. $id = isset($this->post['id']) && $this->post['id'] !== "" ? trim($this->post['id']) : "";
  2167. if ($id == "") {
  2168. return error_show(1002, "参数id不能为空");
  2169. }
  2170. $etid = Db::name("sale")->where(["id" => $id, "is_del" => 0])->find();
  2171. if (empty($etid)) {
  2172. return error_show(1002, "未找到数据");
  2173. }
  2174. // $orderCode=isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']) :"";
  2175. // if($orderCode==""){
  2176. // return error_show(1002,"销售单code不能为空");
  2177. // }
  2178. $good_code = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : "";
  2179. if ($good_code == "") {
  2180. return error_show(1002, "商品code不能为空");
  2181. }
  2182. $ct = Db::name('good')->alias('a')->join('good_type b', 'b.good_code=a.good_code', 'left')
  2183. ->where(['b.type_code' => $good_code])->find();
  2184. if ($ct == "") {
  2185. return error_show(1002, "未找到商品数据");
  2186. }
  2187. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code'] !== "" ? trim($this->post['customer_code']) : "";
  2188. if ($customer_code == "") {
  2189. return error_show(1002, "客户code不能为空");
  2190. }
  2191. $customer = Db::name("customer_info")->where(["companyNo" => $customer_code])->find();
  2192. if ($customer == false) {
  2193. return error_show(1004, "未找到客户数据");
  2194. }
  2195. $supplierNo = $this->post['supplierNo'] && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  2196. if ($supplierNo == "") {
  2197. return error_show(1002, "参数supplierNo不能为空");
  2198. }
  2199. $supplier = Db::name("supplier")->where(["code" => $supplierNo])->find();
  2200. if ($supplier == false) {
  2201. return error_show(1004, "未找到平台供应商数据");
  2202. }
  2203. $apply_id = GetUserInfo($token);
  2204. if (empty($apply_id) || $apply_id['code'] != 0) {
  2205. return error_show(1002, "申请人数据不存在");
  2206. }
  2207. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  2208. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  2209. // $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim
  2210. //($this->post['good_name']) : "";
  2211. // $good_num = isset($this->post['good_num']) && $this->post['good_num'] !== "" ? intval
  2212. //($this->post['good_num']) : "";
  2213. // $origin_price = isset($this->post['origin_price']) && $this->post['origin_price'] !== "" ? intval
  2214. // ($this->post['origin_price']) : "";
  2215. // $sale_price = isset($this->post['sale_price']) && $this->post['sale_price'] !== "" ? trim($this->post['sale_price']) : "";
  2216. // $total_price = isset($this->post['total_price']) && $this->post['total_price'] !== "" ? trim($this->post['total_price']) : "";
  2217. // $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !== "" ? trim($this->post['post_fee']) : "";
  2218. // $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  2219. $vr = isset($this->post['order_addr']) && $this->post['order_addr'] !== "" ? $this->post['order_addr'] : "";
  2220. if ($vr == "") {
  2221. return error_show(1002, "参数order_addr不能为空");
  2222. }
  2223. $vi = isset($this->post['good_stock']) && $this->post['good_stock'] !== "" ? $this->post['good_stock'] : "";
  2224. if ($vi == "") {
  2225. return error_show(1002, "参数good_stock不能为空");
  2226. }
  2227. $good_num = intval(array_sum(array_column($vi, "num")));
  2228. Db::startTrans();
  2229. try {
  2230. $datn = [
  2231. "id" => $id,
  2232. "good_code" => $good_code,
  2233. "customer_code" => $customer_code,
  2234. "good_num" => $good_num,
  2235. "supplierNo" => $supplierNo,
  2236. "apply_id" => $rm,
  2237. "apply_name" => $ri,
  2238. "is_del" => 0,
  2239. "updatetime" => date("Y-m-d H:i:s")
  2240. ];
  2241. $datninfo = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->save($datn);
  2242. if ($datninfo > 0) {
  2243. $stn = ["order_code" => $etid['orderCode'], "status" => $etid['status'], "action_remark" => '', "action_type" => "edit"];
  2244. ActionLog::logAdd($this->post['token'], $stn, "XSQRD", $etid['status'], $datn);
  2245. // $order=["order_type"=>'XSQRD',"order_code"=>$etid['orderCode'],"order_id"=>$id,"order_status"=>$etid['status'],"before_status"=> 0];
  2246. // ProcessOrder::AddProcess($this->post['token'],$order);
  2247. //更新台账
  2248. Db::name('standing_book')
  2249. ->where('orderCode', $etid['orderCode'])
  2250. ->update([
  2251. 'customer_code' => $customer_code,
  2252. 'supplierNo' => $supplierNo,
  2253. 'updatetime' => date("Y-m-d H:i:s")
  2254. ]);
  2255. foreach ($vi as $ion) {
  2256. $iten = [];
  2257. isset($ion['id']) && $ion['id'] !== "" ? $iten['id'] = $ion['id'] : '';
  2258. $iten['orderCode'] = $etid['orderCode'];
  2259. $iten['wsm_code'] = $ion['wsm_code'];
  2260. $iten['stock_id'] = 0;
  2261. $iten['num'] = $ion['num'];
  2262. $iten['status'] = 1;
  2263. $iten['sale_price'] = $ct['original_price'];
  2264. isset($ion['id']) && $ion['id'] !== "" ? '' : $iten['addtime'] = date("Y-m-d H:i:s");
  2265. $iten['updatetime'] = date("Y-m-d H:i:s");
  2266. $vp = Db::name('sale_info')->save($iten);
  2267. if ($vp == false) {
  2268. Db::rollback();
  2269. return error_show(1002, "更新失败");
  2270. }
  2271. }
  2272. foreach ($vr as $value) {
  2273. $temp = [];
  2274. isset($value['id']) && $value['id'] !== "" ? $temp['id'] = $value['id'] : '';
  2275. $temp['orderCode'] = $etid['orderCode'];
  2276. $temp['contactor'] = $value['contactor'];
  2277. $temp['mobile'] = $value['mobile'];
  2278. $temp['addr'] = $value['addr'];
  2279. $temp['addr_code'] = $value['addr_code'];
  2280. $temp['customer_code'] = $customer_code;
  2281. $temp['receipt_quantity'] = $value['receipt_quantity'];
  2282. $temp['post_fee'] = 0;
  2283. $temp['is_del'] = $value['is_del'];
  2284. isset($value['id']) && $value['id'] !== "" ? '' : $temp['addtime'] = date("Y-m-d H:i:s");
  2285. $temp['updatetime'] = date("Y-m-d H:i:s");
  2286. $temp['arrive_time'] = date("Y-m-d H:i:s");
  2287. $dat = Db::name('order_addr')->save($temp);
  2288. if ($dat == false) {
  2289. Db::rollback();
  2290. return error_show(1002, "更新失败");
  2291. }
  2292. }
  2293. Db::commit();
  2294. return error_show(0, "更新成功");
  2295. }
  2296. Db::rollback();
  2297. return error_show(1002, "更新失败");
  2298. } catch (Exception $e) {
  2299. Db::rollback();
  2300. return error_show(1005, $e->getMessage());
  2301. }
  2302. }
  2303. public function info()
  2304. {
  2305. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  2306. if ($id == "") {
  2307. return error_show(1002, "参数id不能为空");
  2308. }
  2309. $einfo = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->find();
  2310. if (empty($einfo)) {
  2311. return error_show(1002, "未找到销售订单数据");
  2312. }
  2313. if ($einfo['order_type'] == 3) {
  2314. $goon = Db::name("good_zixun")->where(["spuCode" => $einfo['good_code'], "is_del" => 0])->find();
  2315. } else {
  2316. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')->where(['a.skuCode' => $einfo['skuCode']])->find();
  2317. }
  2318. if ($goon == false) {
  2319. return error_show(1003, "未找到商品数据");
  2320. } else {
  2321. $goon['exclusive'] = isset($goon['is_exclusive']) ? makeExcluse($goon['is_exclusive']) : "";
  2322. $unit = Db::name("unit")->where(["id" => $goon['good_unit']])->find();
  2323. $goon['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  2324. $spec = Db::name("good_spec")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->select()->toArray();
  2325. $speclist = [];
  2326. if (!empty($spec)) {
  2327. foreach ($spec as $value) {
  2328. $temp = [];
  2329. $temp['id'] = $value['id'];
  2330. $temp['spuCode'] = $value['spuCode'];
  2331. $temp['spec_id'] = $value['spec_id'];
  2332. $temp['spec_value_id'] = $value['spec_value_id'];
  2333. $temp['is_del'] = $value['is_del'];
  2334. $sp = Db::name("specs")->where(["id" => $value['spec_id']])->find();
  2335. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  2336. $spv = Db::name("spec_value")->where(["id" => $value['spec_value_id']])->find();
  2337. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  2338. $speclist[] = $temp;
  2339. }
  2340. }
  2341. $goon["speclist"] = empty($speclist) ? [] : $speclist;
  2342. $proof = Db::name("good_proof")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->order("updatetime desc")->field("id,proof_type,proof_url")->find();
  2343. $goon['proof'] = isset($proof) && $proof != false ? $proof : [];
  2344. $goon['origin_place_cn'] = "";
  2345. $goon['delivery_place_cn'] = "";
  2346. if (isset($goon['delivery_place']) && $goon['delivery_place'] !== "") {
  2347. $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  2348. list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $goon['delivery_place']);
  2349. $goon['delivery_place_cn'] = GetAddr(json_encode($place));
  2350. }
  2351. if (isset($goon['delivery_place']) && $goon['origin_place'] !== "") {
  2352. $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  2353. list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $goon['origin_place']);
  2354. $goon['origin_place_cn'] = GetAddr(json_encode($place));
  2355. }
  2356. if ($goon['brand_id'] != 0) {
  2357. $brand = Db::name("brand")->where(["id" => $goon['brand_id']])->find();
  2358. $goon["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  2359. } else {
  2360. $goon["brand_name"] = "";
  2361. $goon["brand_id"] = "";
  2362. }
  2363. $supplier = Db::name("supplier")->where(["code" => $goon['supplierNo']])->find();
  2364. $goon['supplierName'] = isset($supplier['name']) ? $supplier['name'] : "";
  2365. $goon['noble_name'] = isset($goon['noble_metal']) && $goon['noble_metal'] != 0 ? $this->noble[$goon['noble_metal']] : "";
  2366. if (isset($goon['companyNo']) && $goon['companyNo'] != "") {
  2367. $company = Db::name("business")->where(["companyNo" => $goon['companyNo']])->find();
  2368. }
  2369. $goon['company'] = isset($company['company']) ? $company['company'] : "";
  2370. }
  2371. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  2372. $in = Db::name('customer_info')->where(['companyNo' => $einfo['customer_code']])->field('companyName')->find();
  2373. $einfo['supplierName'] = '';
  2374. if ($einfo['supplierNo'] != "") {
  2375. $supplier = Db::name("business")->where(["companyNo" => $einfo['supplierNo']])->find();
  2376. $einfo['supplierName'] = isset($supplier['company']) ? $supplier['company'] : "";
  2377. }
  2378. $addr_sum = Db::name('order_addr')->where(['orderCode' => $einfo['orderCode'],"is_del"=>0])->sum('receipt_quantity');
  2379. $einfo['addr_sum']=$addr_sum;
  2380. // $fo = Db::name('order_addr')->alias("a")->leftJoin("order_out b", "a.id=b.addrid and b.is_del=0")
  2381. // ->where(['a.orderCode' => $einfo['orderCode'], 'a.is_del' => 0])->field("a.*,b.post_name,b.post_code,
  2382. // 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")
  2383. // ->select();
  2384. $addrs = [];
  2385. // if (!empty($fo)) {
  2386. // foreach ($fo as $value) {
  2387. // $value['addr_info'] = "";
  2388. // $value['send_num'] = 0;
  2389. // if ($value['addr_code'] != "" && !empty($value['addr_code'])) {
  2390. // $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  2391. // list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $value['addr_code']);
  2392. // $addr = GetAddr(json_encode($place));
  2393. // $value['addr_info'] = $addr;
  2394. // }
  2395. // $value['wsm_name'] = "";
  2396. // if (isset($value['wsm_code']) && $value['wsm_code'] != "") {
  2397. // $wsmcode = Db::name("warehouse_info")->where(["wsm_code" => $value['wsm_code']])->find();
  2398. // $value['wsm_name'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  2399. // }
  2400. // $value['sendtime'] = $value['send_status'] < 2 ? '' : $value['sendtime'];
  2401. // $addrs[] = $value;
  2402. // }
  2403. // }
  2404. $einfo['useage'] = "";
  2405. if ($einfo['use_order'] != 0) {
  2406. $use = Db::name("order_use")->where(["id" => $einfo['use_order']])->find();
  2407. $einfo['useage'] = isset($use['order_use']) ? $use['order_use'] : "";
  2408. } else {
  2409. $einfo['use_order'] = '';
  2410. }
  2411. $einfo['platform_name'] = '';
  2412. if ($einfo['platform_id'] != 0) {
  2413. $plat = Db::name("platform")->where(['id' => $einfo['platform_id']])->find();
  2414. $einfo['platform_name'] = isset($plat['platform_name']) ? $plat['platform_name'] : "";
  2415. }
  2416. $einfo['companyName'] = isset($in['companyName']) ? $in['companyName'] : "";
  2417. $einfo['addrs'] = $addrs;
  2418. $einfo['can'] = $int;
  2419. $einfo['goodinfo'] = $goon;
  2420. $einfo['good_num'] -= $einfo['th_num'];
  2421. $einfo['send_num'] -= $einfo['th_num'];
  2422. $einfo['total_price'] = round($einfo['total_price'] - $einfo['th_fee'], 2);
  2423. //处理凭证
  2424. if ($einfo['order_type'] == 1 || $einfo['order_type'] == 2) {
  2425. $proof = Db::name('good_proof')
  2426. ->field('id,proof_type,proof_url')
  2427. ->where(['id' => $einfo['proof_id'], 'is_del' => 0])
  2428. ->find();
  2429. $einfo['proof_type'] = isset($proof['proof_type']) ? $proof['proof_type'] : '';
  2430. $einfo['proof_url'] = isset($proof['proof_url']) ? $proof['proof_url'] : '';
  2431. } elseif ($einfo['order_type'] == 3) {
  2432. $einfo['proof_type'] = isset($goon['proof_type']) ? $goon['proof_type'] : '';
  2433. $einfo['proof_url'] = isset($goon['proof_url']) ? $goon['proof_url'] : '';
  2434. } else {
  2435. $einfo['proof_type'] = '';
  2436. $einfo['proof_url'] = '';
  2437. }
  2438. if (empty($einfo)) {
  2439. return error_show(1002, "未找到销售订单数据");
  2440. } else {
  2441. return app_show(0, "获取成功", $einfo);
  2442. }
  2443. }
  2444. public function del()
  2445. {
  2446. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  2447. if ($id === "") {
  2448. return error_show(1002, "参数id不能为空");
  2449. }
  2450. $ed = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->find();
  2451. if (empty($ed)) {
  2452. return error_show(1002, "未找到销售订单数据");
  2453. }
  2454. $str = Db::name('sale')->update(['id' => $id, 'is_del' => 1, 'updatetime' => date("Y-m-d H:i:s")]);
  2455. if ($str) {
  2456. $stc = ["order_code" => $id, "status" => $ed['status'], "action_remark" => '', "action_type" => "delete"];
  2457. ActionLog::logAdd($this->post['token'], $stc, "XSQRD", $ed['status'], ['id' => $id, 'is_del' => 1, 'updatetime' => date("Y-m-d H:i:s")]);
  2458. $order = ["order_type" => 'XSQRD', "order_code" => $ed['orderCode'], "order_id" => $id, "order_status" => $ed['status'],'holder_id' => $ed['apply_id']
  2459. ];
  2460. ProcessOrder::workdel($order);
  2461. return error_show(0, "删除成功");
  2462. } else {
  2463. return error_show(1002, "删除失败");
  2464. }
  2465. }
  2466. //废弃接口
  2467. public function status()
  2468. {
  2469. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  2470. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  2471. if ($id == "") {
  2472. return error_show(1002, "参数id不能为空");
  2473. }
  2474. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  2475. if ($status == "") {
  2476. return error_show(1002, "订单状态不能为空");
  2477. }
  2478. $dio = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->find();
  2479. if (empty($dio)) {
  2480. return error_show(1002, "销售单信息未找到");
  2481. }
  2482. if ($status == 1 && $dio['order_type'] == 2) {
  2483. $zxinfo = Db::name("consult")->where(["zxNo" => $dio['zxNo'], "is_del" => 0])->find();
  2484. if ($zxinfo == false) {
  2485. return error_show(1004, "未找到咨询单数据");
  2486. }
  2487. $gold['price'] = 0;
  2488. if ($zxinfo['zx_type'] == 2) {
  2489. if ($zxinfo['metals'] == "") {
  2490. return error_show(1004, "未找到咨询单贵金属类型数据");
  2491. }
  2492. $metals = $zxinfo['metals'] == '18K' ? 1 : ($zxinfo['metals'] == '24K' ? 2 : 3);
  2493. $gold = Db::name("gold_price")->where(["type" => $metals])->order("addtime desc")->find();
  2494. if (empty($gold)) {
  2495. return error_show(1004, "未找到咨询单贵金属类型数据");
  2496. }
  2497. $dio['gold_price'] = $gold['price'];
  2498. }
  2499. $wsm = Db::name("warehouse_info")->where(["supplierNo" => $zxinfo['gysNo'], "wsm_type" => 2])->find();
  2500. if (empty($wsm)) {
  2501. return error_show(1002, "供应商仓库未找到");
  2502. }
  2503. }
  2504. // $apply_id = GetUserInfo($token);
  2505. // if (empty($apply_id) || $apply_id['code'] != 0) {
  2506. // return error_show(1002, "申请人数据不存在");
  2507. // }
  2508. $rm = $this->uid;
  2509. $ri = $this->uname;
  2510. Db::startTrans();
  2511. try {
  2512. $vmp = $dio['status'];
  2513. $dio['status'] = $status;
  2514. $dio['updatetime'] = date("Y-m-d H:i:s");
  2515. $st = Db::name('sale')->save($dio);
  2516. if ($st) {
  2517. $stn = ["order_code" => $dio['orderCode'], "status" => $vmp, "action_remark" => '', "action_type" => "status"];
  2518. ActionLog::logAdd($this->post['token'], $stn, $dio['order_type'] == 2 ? "ZXQRD" : "XSQRD", $dio['status'], $dio);
  2519. $order = ["order_type" => "XSQRD", "order_code" => $dio['orderCode'], "order_id" => $id, "order_status" => $dio['status'], "before_status" => 1,'holder_id' => $dio['apply_id']];
  2520. ProcessOrder::AddProcess($this->post['token'], $order);
  2521. if ($status == 3 && $dio['order_type'] == 1) {
  2522. $dn = Db::name('sale_info')->where(['orderCode' => $st['orderCode']])->select();
  2523. foreach ($dn as $value) {
  2524. $dm = Db::name('good_stock')->where(['wsm_code' => $value['wsm_code'], 'good_type_code' => $st['good_code']])->find();
  2525. if ($dm == "") {
  2526. Db::rollback();
  2527. return error_show(1003, "未找到商品数据");
  2528. }
  2529. if ($value['num'] > $dm['usable_stock']) {
  2530. Db::rollback();
  2531. return error_show(1002, "超出库存数量");
  2532. }
  2533. $dm['usable_stock'] -= $value['num'];
  2534. $dm['wait_out_stock'] += $value['num'];
  2535. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code'], "is_del" => 0])->sum("send_num");
  2536. $value['send_num'] = $send ?? 0;
  2537. $value['wsend_num'] = $value['num'] - $value['send_num'];
  2538. $Db = Db::name('good_stock')->update($dm);
  2539. $Db['updatetime'] = date('Y-m-d H:i:s');
  2540. if ($Db == false) {
  2541. Db::rollback();
  2542. return error_show(1002, "状态更新失败");
  2543. }
  2544. }
  2545. }
  2546. if ($status == 1 && $dio['order_type'] == 2) {
  2547. $cgd = makeNo("CG");
  2548. $clll = [
  2549. "cgdNo" => $cgd,
  2550. "bkcode" => $dio['orderCode'],
  2551. "wsm_code" => $wsm['wsm_code'],
  2552. "cgder_id" => Db::name('depart_user')->where('nickname', $zxinfo['saler'])->value('uid') ?? 0,//重名以及多个账号的话…………
  2553. "cgder" => $zxinfo['saler'],
  2554. "good_code" => "GD-" . $zxinfo['cpNo'],
  2555. "good_name" => $zxinfo['cpName'],
  2556. "good_type_code" => $zxinfo['cpNo'],
  2557. "good_num" => $dio['good_num'],
  2558. "good_price" => $zxinfo['total_fee'],
  2559. "total_fee" => round($zxinfo['total_fee'] * $dio['good_num'], 2),
  2560. "pakge_fee" => $zxinfo['package_fee'],
  2561. "cert_fee" => $zxinfo['cert_fee'],
  2562. "open_fee" => $zxinfo['open_fee'],
  2563. "delivery_fee" => $zxinfo['delivery_fee'],
  2564. "mark_fee" => $zxinfo['mark_fee'],
  2565. "teach_fee" => $zxinfo['cost_fee'],
  2566. "demo_fee" => $zxinfo['demo_fee'],
  2567. "nake_fee" => $zxinfo['bare_fee'],
  2568. "weight" => isset($zxinfo['weight']) ? $zxinfo['weight'] : 0,
  2569. "supplierNo" => $zxinfo['gysNo'],
  2570. "supplier_name" => $zxinfo['gysname'],
  2571. "gold_price" => $gold['price'],
  2572. "send_num" => 0,
  2573. "wsend_num" => $dio['good_num'],
  2574. "remark" => '',
  2575. "lasttime" => date("Y-m-d H:i:s"),
  2576. "is_del" => 0,
  2577. "status" => $dio['send_type'] == 1 ? 3 : 0,
  2578. "order_type" => 2,
  2579. "order_source" => 3,//3项目,废弃接口???
  2580. "addtime" => date("Y-m-d H:i:s"),
  2581. "updatetime" => date("Y-m-d H:i:s")
  2582. ];
  2583. $cgin = Db::name("purchease_order")->insert($clll);
  2584. // var_dump(Db::name("purchease_order")->getLastSql(),$clll);
  2585. if ($cgin == false) {
  2586. $stb = ["order_code" => $cgd, "status" => $clll['status'], "action_remark" => '', "action_type" => "create"];
  2587. ActionLog::logAdd($this->post['token'], $stb, "xsd", $clll['status'], $stb);
  2588. Db::rollback();
  2589. return error_show(1002, "咨询采购单创建商品失败");
  2590. }
  2591. if ($dio['send_type'] == 1) {
  2592. $addr = Db::name('order_addr')->where(["orderCode" => $dio['orderCode'], 'is_del' => 0])->select();
  2593. $i = 1;
  2594. foreach ($addr as $value) {
  2595. $outCode = makeNo("DF");
  2596. //改变编码规则,将原来的outCode后两位换成序列号
  2597. //str_pad字符串填充
  2598. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  2599. $i++;
  2600. $data = [
  2601. "wsm_code" => $wsm['wsm_code'],
  2602. "orderCode" => $dio['orderCode'],
  2603. "outCode" => $outCode,
  2604. "order_type" => $dio['order_type'],
  2605. "apply_id" => $rm,
  2606. "apply_name" => $ri,
  2607. "addrid" => $value['id'],
  2608. "post_name" => '',
  2609. "post_code" => '',
  2610. "post_fee" => '',
  2611. "sendtime" => date("Y-m-d H:i:s"),
  2612. "send_num" => $value['receipt_quantity'],
  2613. "check_num" => 0,
  2614. "error_num" => 0,
  2615. "status" => 0,
  2616. "addtime" => date("Y-m-d H:i:s"),
  2617. "updatetime" => date("Y-m-d H:i:s")
  2618. ];
  2619. $datainfo = Db::name('order_out')->insertGetId($data);
  2620. if (!$datainfo) {
  2621. $sti = ["order_code" => $dio['orderCode'], "status" => 0, "action_remark" => '', "action_type" => "create"];
  2622. ActionLog::logAdd($this->post['token'], $sti, "xsd", 0, $sti);
  2623. Db::rollback();
  2624. return error_show(1003, "创建失败");
  2625. } else {
  2626. //修改状态,添加待办
  2627. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
  2628. "order_code" => $outCode,//出库单号
  2629. "status" => 0,//这里的status是之前的值
  2630. "action_remark" => '',//备注
  2631. "action_type" => "create"//新建create,编辑edit,更改状态status
  2632. ], "CKD", 0, $data);
  2633. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], [
  2634. "order_type" => 'CKD',
  2635. "order_code" => $outCode,//出库单号
  2636. "order_id" => $datainfo,
  2637. "order_status" => 0,
  2638. "before_status" => 0,
  2639. 'holder_id'=>$data['apply_id'],
  2640. ]);
  2641. }
  2642. }
  2643. $wsm_in_code = makeNo("CF");
  2644. $orin = [
  2645. "wsm_in_code" => $wsm_in_code,
  2646. "cgdNo" => $cgd,
  2647. "wsm_code" => $wsm['wsm_code'],
  2648. "wsm_reaper" => "",
  2649. "send_num" => $dio['good_num'],
  2650. "post_company" => '',
  2651. "post_code" => '',
  2652. "post_fee" => '',
  2653. "sendtime" => date("Y-m-d H:i:s"),
  2654. "apply_id" => $rm,
  2655. "apply_name" => $ri,
  2656. "wait_num" => 0,
  2657. "status" => 4,
  2658. "addtime" => date("Y-m-d H:i:s"),
  2659. "updatetime" => date("Y-m-d H:i:s")
  2660. ];
  2661. $win = Db::name("purchease_in")->insert($orin);
  2662. if (!$win) {
  2663. $stv = ["order_code" => $wsm_in_code, "status" => 4, "action_remark" => '', "action_type" => "create"];
  2664. ActionLog::logAdd($this->post['token'], $stv, "xsd", 4, $stv);
  2665. Db::rollback();
  2666. return error_show(1003, "创建失败");
  2667. }
  2668. //维护台账
  2669. 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}'");
  2670. $good = Db::name("good_stock")->where(["wsm_code" => $wsm['wsm_code'], "good_type_code" => $zxinfo['cpNo'], "is_del" => 0])->find();
  2671. if (empty($good)) {
  2672. $good = [
  2673. "good_type_code" => $zxinfo['cpNo'],
  2674. "wsm_code" => $wsm['wsm_code'],
  2675. "usable_stock" => 0,
  2676. "wait_out_stock" => 0,
  2677. "total_stock" => 0,
  2678. "addtime" => date("Y-m-d H:i:s"),
  2679. "updatetime" => date("Y-m-d H:i:s"),
  2680. ];
  2681. }
  2682. $good['wait_out_stock'] += $dio['good_num'];
  2683. $good['total_stock'] = $good['usable_stock'] + $good['wait_out_stock'];
  2684. $good['updatetime'] = date("Y-m-d H:i:s");
  2685. $upd = Db::name("good_stock")->save($good);
  2686. if (!$upd) {
  2687. $ste = ["order_code" => $zxinfo['cpNo'], "status" => 0, "action_remark" => '', "action_type" => "create"];
  2688. ActionLog::logAdd($this->post['token'], $ste, "xsd", 0, $ste);
  2689. Db::rollback();
  2690. return error_show(1003, "创建失败");
  2691. }
  2692. }
  2693. }
  2694. }
  2695. Db::commit();
  2696. return error_show(0, "订单状态更新成功");
  2697. } catch (Exception $e) {
  2698. Db::rollback();
  2699. return error_show(1005, $e->getMessage());
  2700. }
  2701. }
  2702. public function fee()
  2703. {
  2704. $id = isset($this->post['id']) && $this->post['id'] !== "" ? trim($this->post['id']) : "";
  2705. if ($id == "") {
  2706. return error_show(1002, "参数id不能为空");
  2707. }
  2708. $etid = Db::name("sale")->where(["id" => $id, "is_del" => 0])->find();
  2709. if (empty($etid)) {
  2710. return error_show(1002, "未找到数据");
  2711. }
  2712. $vr = isset($this->post['order_addr']) && $this->post['order_addr'] !== "" ? $this->post['order_addr'] : "";
  2713. if ($vr == "") {
  2714. return error_show(1002, "参数order_addr不能为空");
  2715. }
  2716. Db::startTrans();
  2717. try {
  2718. $data = [
  2719. "id" => $id,
  2720. "status" => 2,
  2721. "is_del" => 0,
  2722. "updatetime" => date("Y-m-d H:i:s")
  2723. ];
  2724. $datainfo = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->save($data);
  2725. if ($datainfo) {
  2726. $stx = ["order_code" => $etid['orderCode'], "status" => $etid['status'], "action_remark" => '', "action_type" => "edit"];
  2727. ActionLog::logAdd($this->post['token'], $stx, "XSQRD", 2, $data);
  2728. $order = ["order_type" => "XSQRD", "order_code" => $etid['orderCode'], "order_id" => $id, "order_status" => $data['status'], "before_status" => 0,'holder_id'=>$etid['apply_id']];
  2729. ProcessOrder::AddProcess($this->post['token'], $order);
  2730. foreach ($vr as $value) {
  2731. $tm = [];
  2732. $tm['id'] = $value['id'];
  2733. $tm['orderCode'] = $etid['orderCode'];
  2734. $tm['post_fee'] = $value['post_fee'];
  2735. $tm['updatetime'] = date("Y-m-d H:i:s");
  2736. $dn = Db::name('order_addr')->save($tm);
  2737. if ($dn == false) {
  2738. Db::rollback();
  2739. return error_show(1002, "更新失败");
  2740. }
  2741. }
  2742. Db::commit();
  2743. return error_show(0, "更新成功");
  2744. }
  2745. Db::rollback();
  2746. return error_show(1003, "更新失败");
  2747. } catch (Exception $e) {
  2748. Db::rollback();
  2749. return error_show(1005, $e->getMessage());
  2750. }
  2751. }
  2752. // 废弃
  2753. public function out()
  2754. {
  2755. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  2756. $outCode = makeNo("DF");
  2757. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  2758. if ($orderCode == "") {
  2759. return error_show(1002, "参数orderCoder不能为空");
  2760. }
  2761. $der = Db::name('sale')->where(['orderCode' => $orderCode])->find();
  2762. if ($der == "") {
  2763. return error_show(1002, "未找到出库订单单号");
  2764. }
  2765. // $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code'] !== "" ? trim($this->post['wsm_code']) : "";
  2766. // if ($wsm_code == "") {
  2767. // return error_show(1002, "参数wsm_code不能为空");
  2768. // }
  2769. // $wsm = Db::name('sale_info')->where(['wsm_code' => $wsm_code, 'orderCode' => $der['orderCode']])->find();
  2770. // if ($wsm == "") {
  2771. // return error_show(1002, "未找到仓库编码");
  2772. // }
  2773. $post_name = isset($this->post['post_name']) && $this->post['post_name'] !== "" ? trim($this->post['post_name']) : "";
  2774. if ($post_name == "") {
  2775. return error_show(1002, "物流公司不能为空");
  2776. }
  2777. $post_code = isset($this->post['post_code']) && $this->post['post_code'] !== "" ? trim($this->post['post_code']) : "";
  2778. if ($post_code == "") {
  2779. return error_show(1002, "物流单号不能为空");
  2780. }
  2781. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !== "" ? intval($this->post['post_fee']) : "";
  2782. if ($post_fee == "") {
  2783. return error_show(1002, "物流费不能为空");
  2784. }
  2785. $send_num = isset($this->post['send_num']) && $this->post['send_num'] !== "" ? $this->post['send_num'] : "";
  2786. if ($send_num == "") {
  2787. return error_show(1002, "发货数量不能为空");
  2788. }
  2789. if ($send_num > $der['wsend_num']) {
  2790. return error_show(1002, "超出可发货数量");
  2791. }
  2792. // $apply_id = GetUserInfo($token);
  2793. // if (empty($apply_id) || $apply_id['code'] != 0) {
  2794. // return error_show(1002, "申请人数据不存在");
  2795. // }
  2796. $rm = $this->uid;
  2797. $ri = $this->uname;
  2798. $addrid = isset($this->post['addrid']) && $this->post['addrid'] !== "" ? intval($this->post['addrid']) : "";
  2799. $dr = Db::name('order_addr')->where(['id' => $addrid, 'is_del' => 0])->find();
  2800. if (empty($dr)) {
  2801. return error_show(1003, "收货人信息未找到");
  2802. }
  2803. if ($dr['orderCode'] !== $orderCode) {
  2804. return error_show(1002, "订单code不存在");
  2805. }
  2806. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !== "" ? $this->post['sendtime'] : date("Y-m-d H:i:s");
  2807. // $check_num = isset($this->post['check_num']) && $this->post['check_num'] !=="" ? $this->post['check_num'] :"";
  2808. // $error_num = isset($this->post['error_num']) && $this->post['error_num'] !=="" ? $this->post['error_num'] :"";
  2809. $status = isset($this->post['status']) && $this->post['status'] !== "" ? $this->post['status'] : "1";
  2810. //$order_type = isset($this->post['order_type']) && $this->post['order_type'] !=="" ? $this->post['order_type'] :"1";
  2811. //$dn=Db::name('sale_info')->where(['wsm_code'=>$outCode])->find();
  2812. Db::startTrans();
  2813. try {
  2814. $data = [
  2815. "wsm_code" => "",
  2816. "orderCode" => $orderCode,
  2817. "outCode" => $outCode,
  2818. "order_type" => $der['order_type'],
  2819. "apply_id" => $rm,
  2820. "apply_name" => $ri,
  2821. "addrid" => $dr['id'],
  2822. "post_name" => $post_name,
  2823. "post_code" => $post_code,
  2824. "post_fee" => $post_fee,
  2825. "sendtime" => $sendtime,
  2826. "send_num" => $send_num,
  2827. "check_num" => 0,
  2828. "error_num" => 0,
  2829. "status" => $status,
  2830. "addtime" => date("Y-m-d H:i:s"),
  2831. "updatetime" => date("Y-m-d H:i:s")
  2832. ];
  2833. $datainfo = Db::name('order_out')->insert($data, true);
  2834. if ($datainfo > 0) {
  2835. $stx = ["order_code" => $outCode, "status" => $status, "action_remark" => '', "action_type" => "create"];
  2836. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], $stx, $der['order_type'] == 1 ? "CKD" : 'ZXCKD', $status, $data);
  2837. //是否库存品,待办数据推送到的人有区别,
  2838. //库存品——>推到库管,非库存品——>推送供应商负责人
  2839. $order=[];
  2840. if($data['status'] == 1){
  2841. if($der['is_stock'] == 1){
  2842. //库存品,推给库管和库管-张凯旋
  2843. $roleid = config('app.wsm_cgder_role');
  2844. $uids = Db::name('user_role')
  2845. ->where('is_del', 0)
  2846. ->whereIn('roleid', $roleid)
  2847. ->column('uid');
  2848. $order = ["order_type" => "CKD", "order_code" => $outCode, "order_id" => $datainfo, "order_status" => $data['status'], "before_status" => 0, 'handle_user_list' => implode(',', $uids)];
  2849. }else{
  2850. //推给供应商负责人
  2851. $supplier = Db::name('supplier')
  2852. ->alias('a')
  2853. ->field('a.id,a.person,a.personid')
  2854. ->leftJoin('purchease_order b', 'b.is_del=0 AND b.supplierNo=a.code')
  2855. ->leftJoin('order_num c', 'c.cgdNo=b.cgdNo')
  2856. ->where('c.orderCode', $orderCode)
  2857. ->findOrEmpty();
  2858. $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'] ?? ''];
  2859. }
  2860. }
  2861. 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']];
  2862. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], $order);
  2863. if ($status == 1) {
  2864. $item = $der['status'];
  2865. $der['send_num'] += $send_num;
  2866. $der['wsend_num'] -= $send_num;//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  2867. $der['send_status'] = $der['send_num'] == 0 ? 1 : $der['wsend_num'] == 0 ? 3 : 2;
  2868. $der['status'] = $der['send_num'] == 0 ? 3 : $der['wsend_num'] == 0 ? 5 : 4;
  2869. $der['updatetime'] = date("Y-m-d H:i:s");
  2870. $si = Db::name('sale')->save($der);
  2871. if ($si == false) {
  2872. Db::rollback();
  2873. return error_show(1003, "更新失败");
  2874. }
  2875. $stx = ["order_code" => $orderCode, "status" => $item, "action_remark" => '', "action_type" => "edit"];
  2876. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], $stx, "XSQRD", $der['status'], $der);
  2877. $order = ["order_type" => "XSQRD", "order_code" => $orderCode, "order_id" => $der['id'], "order_status" => $data['status'], "before_status" => 0,'holder_id'=>$der['apply_id']];
  2878. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], $order);
  2879. }
  2880. Db::commit();
  2881. return error_show(0, "创建成功");
  2882. } else {
  2883. Db::rollback();
  2884. return error_show(1003, "创建失败");
  2885. }
  2886. } catch (Exception $e) {
  2887. Db::rollback();
  2888. return error_show(1005, $e->getMessage());
  2889. }
  2890. }
  2891. // 废弃
  2892. public function customer()
  2893. {
  2894. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  2895. if (empty($outCode)) {
  2896. return error_show(1002, "销售订单编号不能为空");
  2897. }
  2898. $codeinfo = Db::name('order_out')->where(['outCode' => $outCode])->find();
  2899. if ($codeinfo == "") {
  2900. return error_show(1003, "未找到订单数据");
  2901. }
  2902. // $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']) :"";
  2903. // if($orderCode==""){
  2904. // return error_show(1002,"参数orderCoder不能为空");
  2905. // }
  2906. $dr = Db::name('sale')->where(['orderCode' => $codeinfo['orderCode']])->find();
  2907. if ($dr == "") {
  2908. return error_show(1002, "未找到销售订单");
  2909. }
  2910. $status = isset($this->post['status']) && $this->post['status'] !== "" ? $this->post['status'] : "";
  2911. if ($status == "") {
  2912. return error_show(10002, "状态不能为空");
  2913. }
  2914. Db::startTrans();
  2915. try {
  2916. $time = $codeinfo['status'];
  2917. $codeinfo['status'] = $status;
  2918. $codeinfo['updatetime'] = date("Y-m-d H:i:s");
  2919. $cust = Db::name('order_out')->save($codeinfo);
  2920. if ($cust) {
  2921. $stx = ["order_code" => $outCode, "status" => $time, "action_remark" => '', "action_type" => "status"];
  2922. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], $stx, "CKD", $codeinfo['status'], $codeinfo);
  2923. $order = [];
  2924. if ($status == 1) {
  2925. //推给供应商负责人
  2926. if ($dr['is_stock'] == 0) {
  2927. $supplier = Db::name('supplier')
  2928. ->alias('a')
  2929. ->field('a.id,a.person,a.personid')
  2930. ->leftJoin('purchease_order b', 'b.is_del=0 AND b.supplierNo=a.code')
  2931. ->leftJoin('order_num c', 'c.cgdNo=b.cgdNo')
  2932. ->where('c.orderCode', $codeinfo['orderCode'])
  2933. ->findOrEmpty();
  2934. $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'] ?? ''];
  2935. } else{
  2936. $roleid = config('app.wsm_cgder_role');
  2937. $uids = Db::name('user_role')
  2938. ->where('is_del', 0)
  2939. ->whereIn('roleid', $roleid)
  2940. ->column('uid');
  2941. $order = ["order_type" => "CKD", "order_code" => $outCode, "order_id" => $codeinfo['id'], "order_status" => $codeinfo['status'], "before_status" => 0, 'handle_user_list' => implode(',', $uids)];
  2942. }
  2943. }
  2944. 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']];
  2945. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], $order);
  2946. if ($status == 1) {
  2947. $stokc = Db::name("good_stock")->where(['spuCode' => $codeinfo['spuCode'], "wsm_code" => $codeinfo['wsm_code'],
  2948. "is_del" => 0])->find();
  2949. if ($stokc == false) {
  2950. Db::rollback();
  2951. return error_show(1002, "未找库存到数据");
  2952. } else {
  2953. if ($codeinfo['send_num'] > $stokc['usable_stock']) {
  2954. Db::rollback();
  2955. return error_show(1002, "超出库存数量");
  2956. }
  2957. $stokc['wait_out_stock'] += $codeinfo['send_num'];
  2958. $stokc['usable_stock'] -= $codeinfo['send_num'];
  2959. $stokc['updatetime'] = date("Y-m-d H:i:s");
  2960. }
  2961. $stoc = Db::name("good_stock")->save($stokc);
  2962. if ($stoc == false) {
  2963. Db::rollback();
  2964. return error_show(1002, "库存更新失败");
  2965. }
  2966. $item = $dr['status'];
  2967. $dr['send_num'] += $codeinfo['send_num'];
  2968. $dr['wsend_num'] -= $codeinfo['send_num'];//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  2969. $dr['send_status'] = $dr['send_num'] == 0 ? 1 : $dr['wsend_num'] == 0 ? 3 : 2;
  2970. $dr['status'] = $dr['send_num'] == 0 ? 3 : $dr['wsend_num'] == 0 ? 5 : 4;
  2971. $dr['updatetime'] = date("Y-m-d H:i:s");
  2972. $si = Db::name('sale')->save($dr);
  2973. if ($si == false) {
  2974. Db::rollback();
  2975. return error_show(1003, "更新失败");
  2976. }
  2977. $stx = ["order_code" => $dr['orderCode'], "status" => $item, "action_remark" => '', "action_type" => "edit"];
  2978. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], $stx, $dr['order_type'] == 2 ? "ZXQRD" : "XSQRD", $dr['status'],$dr);
  2979. $order = ["order_type" => "XSQRD", "order_code" => $dr['orderCode'], "order_id" => $dr['id'], "order_status" => $dr['status'], "before_status" => 0, 'holder_id' => $dr['apply_id']];
  2980. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], $order);
  2981. //商品变动日志表,good_log_code字段存储销售订单编号
  2982. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $stoc['id'], "type" => 1, 'stock' => $codeinfo['send_num'], "stock_name" => "wait_out_stock"];
  2983. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $stoc['id'], "type" => 2, 'stock' => $codeinfo['send_num'], "stock_name" => "usable_stock"];
  2984. GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname], $good_data, 'CKD');
  2985. }
  2986. if ($status == 3) {
  2987. $var = Db::name('good_stock')->where(['wsm_code' => $codeinfo['wsm_code'], 'good_type_code' => $dr['good_code']])->find();
  2988. if ($var == false) {
  2989. Db::rollback();
  2990. return error_show(1002, "未找到商品数据");
  2991. }
  2992. if ($codeinfo['send_num'] > $var['intra_stock']) {
  2993. Db::rollback();
  2994. return error_show(1003, "超出在途库存数量");
  2995. }
  2996. $var['intra_stock'] -= $codeinfo['send_num'];
  2997. $var['updatetime'] = date("Y-m-d H:i:s");
  2998. $stre = Db::name('good_stock')->save($var);
  2999. //商品变动日志表,good_log_code字段存储销售订单编号
  3000. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $var['id'], "type" => 2, 'stock' => $codeinfo['send_num'], "stock_name" => "intra_stock"];
  3001. GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname], $good_data, $codeinfo['order_type'] == 2 ? "ZXCKD" : "CKD");
  3002. if ($stre == false) {
  3003. Db::rollback();
  3004. return error_show(1002, "状态更新失败");
  3005. }
  3006. }
  3007. Db::commit();
  3008. return error_show(0, "出库订单更新成功");
  3009. } else {
  3010. Db::rollback();
  3011. return error_show(1003, "出库订单更新失败");
  3012. }
  3013. } catch (Exception $e) {
  3014. Db::rollback();
  3015. return error_show(1005, $e->getMessage());
  3016. }
  3017. }
  3018. public function difflist()
  3019. {
  3020. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  3021. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  3022. $where = [];
  3023. $cgdNo = isset($this->post['orderCode']) && $this->post['orderCode'] != "" ? trim($this->post['orderCode']) : "";
  3024. if ($cgdNo != "") {
  3025. $where[] = ['a.orderCode', "like", "%$cgdNo%"];
  3026. }
  3027. $diffNo = isset($this->post['diffCode']) && $this->post['diffCode'] != "" ? trim($this->post['diffCode']) : "";
  3028. if ($diffNo != "") {
  3029. $where[] = ['a.diffCode', "like", "%$diffNo%"];
  3030. }
  3031. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  3032. if ($status !== "") {
  3033. $where[] = ['a.status', "=", $status];
  3034. }
  3035. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  3036. if ($start != "") {
  3037. $where[] = ["a.addtime", '>=', $start];
  3038. }
  3039. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  3040. if ($end != "") {
  3041. $where[] = ["a.addtime", '<=', $end];
  3042. }
  3043. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  3044. if ($supplierNo != "") {
  3045. $where[] = ['c.supplierNo', "like", "%$supplierNo%"];
  3046. }
  3047. $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
  3048. if ($companyNo != "") {
  3049. $where[] = ['c.customer_code', "like", "%$companyNo%"];
  3050. }
  3051. // $role=$this->checkRole();
  3052. // if(!empty($role['write'])){
  3053. // $where[]=["apply_id","in",$role['write']];
  3054. // }
  3055. // $role = $this->checkDataShare();
  3056. // if (!empty($role[DataGroupModel::$type_全部])) $where[] = ['c.apply_id', 'in', $role[DataGroupModel::$type_全部]];
  3057. $role = $this->checkDataShare();
  3058. $hand = resign_hand_user($this->uid,0);
  3059. if (!empty($role[DataGroupModel::$type_全部])) {
  3060. $arr= array_unique(array_merge($hand,$role[DataGroupModel::$type_全部]));
  3061. $where[] = ['c.apply_id', 'in',$arr];
  3062. }
  3063. // if(!empty($role['platform']) ){
  3064. // $where[]=["c.platform_id","in",$role['platform']];
  3065. // }
  3066. $count = Db::name('sale_diff')
  3067. ->alias('a')
  3068. ->join("sale c", "c.orderCode=a.orderCode", "left")
  3069. ->join("customer_info v", "v.companyNo=c.customer_code", "left")
  3070. ->where($where)->count();
  3071. $total = ceil($count / $size);
  3072. $page = $page >= $total ? intval($total) : $page;
  3073. $list = Db::name('sale_diff')
  3074. ->alias('a')
  3075. ->field("a.*,c.customer_code,v.companyName,c.skuCode,c.order_type,c.supplierNo,c.apply_id")
  3076. ->join("sale c", "c.orderCode=a.orderCode", "left")
  3077. ->join("customer_info v", "v.companyNo=c.customer_code", "left")
  3078. ->where($where)
  3079. ->page($page, $size)
  3080. ->order("a.addtime desc")
  3081. ->select()
  3082. ->toArray();
  3083. $data = [];
  3084. foreach ($list as $value) {
  3085. if ($value['order_type'] == 3) {
  3086. $goon = Db::name("good_zixun")->where(["spuCode" => $value['good_code'], "is_del" => 0])->find();
  3087. } else {
  3088. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  3089. ->where(['a.skuCode' => $value['skuCode']])->find();
  3090. }
  3091. $value['can'] = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  3092. $wsm = Db::name("business")->where(['companyNo' => $value['supplierNo']])->find();
  3093. $value['supplierNo'] = isset($wsm['companyNo']) ? $wsm['companyNo'] : "";
  3094. $value['supplierName'] = isset($wsm['company']) ? $wsm['company'] : "";
  3095. //是否具有编辑权限
  3096. $value['is_allow_update'] = 0;
  3097. if (in_array($this->roleid, [1, 33]) || in_array($value['apply_id'], $role[DataGroupModel::$type_可编辑])) $value['is_allow_update'] = 1;
  3098. $data[] = $value;
  3099. }
  3100. return app_show(0, "获取成功", ['list' => $data, 'count' => $count]);
  3101. }
  3102. /**
  3103. * @return \think\response\Json|void
  3104. * @throws \think\db\exception\DataNotFoundException
  3105. * @throws \think\db\exception\DbException
  3106. * @throws \think\db\exception\ModelNotFoundException
  3107. */
  3108. //工差单审核
  3109. public function diffcheck()
  3110. {
  3111. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  3112. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  3113. if ($id === "") {
  3114. return error_show(1004, "参数id不能为空");
  3115. }
  3116. $info = Db::name("sale_diff")->where(["id" => $id])->find();
  3117. if (empty($info)) {
  3118. return error_show(1004, "订单数据未找到");
  3119. }
  3120. $sale = Db::name("sale")->where(["orderCode" => $info["orderCode"]])->find();
  3121. if ($sale == false) {
  3122. return error_show(1004, "订单数据未找到");
  3123. }
  3124. $cgd = Db::name("purchease_diff")->where(['id' => $info['cgd_diffid']])->find();
  3125. if ($cgd == false) {
  3126. return error_show(1004, "采购工差单数据未找到");
  3127. }
  3128. $is_act = isset($this->post['is_act']) && $this->post['is_act'] !== "" ? intval($this->post['is_act']) : "";
  3129. if ($is_act === '') {
  3130. return error_show(1004, "参数is_act不能为空");
  3131. }
  3132. $customer_remark = isset($this->post['customer_remark']) && $this->post['customer_remark'] !== "" ? trim($this->post['customer_remark']) : "";
  3133. if ($customer_remark === '') {
  3134. return error_show(1004, "参数customer_remark不能为空");
  3135. }
  3136. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "2";
  3137. $remark = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
  3138. $item = $info['status'];
  3139. $info['is_act'] = $is_act;
  3140. $info['customer_remark'] = $customer_remark;
  3141. $info['status'] = $status;
  3142. $info['remark'] = $remark;
  3143. $info['updatetime'] = date("Y-m-d H:i:s");
  3144. Db::startTrans();
  3145. try {
  3146. $up = Db::name("sale_diff")->save($info);
  3147. if ($up) {
  3148. $stx = ["order_code" => $info["orderCode"], "status" => $item, "action_remark" => '', "action_type" => "edit"];
  3149. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], $stx, "XSGCD", $info['status'], $info);
  3150. $order = ["order_type" => "XSGCD", "order_code" => $info["orderCode"], "order_id" => $info['id'], "order_status" => $info['status'], "before_status" => $item,'holder_id'=>$sale['apply_id']];
  3151. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], $order);
  3152. $upda = [
  3153. "status" => $status,
  3154. "remark" => $remark,
  3155. "updatetime" => date("Y-m-d H:i:s"),
  3156. ];
  3157. $old_purchease_diff = Db::name("purchease_diff")
  3158. ->field('id,cgdNo,status')
  3159. ->where(["id" => $info['cgd_diffid']])
  3160. ->find();
  3161. $cgdup = Db::name("purchease_diff")->where(["id" => $info['cgd_diffid']])->save($upda);
  3162. if ($cgdup) {
  3163. if ($status == 2) {
  3164. if ($customer_remark == 1) {
  3165. $sale['diff_weight'] = $info['diff_weight'];
  3166. $sale['diff_fee'] = $info['diff_price'];
  3167. $sale['updatetime'] = date("Y-m-d H:i:s");
  3168. $sal = Db::name("sale")->save($sale);
  3169. if ($sal == false) {
  3170. Db::rollback();
  3171. return error_show(1003, "订单更新工差失败");
  3172. }
  3173. }
  3174. $cgdup = Db::name("purchease_order")->where(["cgdNo" => $cgd['cgdNo']])->save
  3175. (["diff_weight" => $cgd['diff_weight'], "diff_fee" => $cgd['diff_price'], "updatetime" => date("Y-m-d H:i:s")]);
  3176. if ($cgdup == false) {
  3177. Db::rollback();
  3178. return error_show(1003, "采购订单更新工差失败");
  3179. }
  3180. }
  3181. $a = ["order_code" => $old_purchease_diff["cgdNo"], "status" => $old_purchease_diff['status'], "action_remark" => '', "action_type" => "status"];
  3182. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $a, "CGGCD", $upda['status'], $info);
  3183. $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']];
  3184. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $b);
  3185. Db::commit();
  3186. return app_show(0, "更新成功");
  3187. }
  3188. }
  3189. Db::rollback();
  3190. return error_show(1003, "更新失败");
  3191. } catch (\Exception $e) {
  3192. Db::rollback();
  3193. return error_show(1003, $e->getMessage());
  3194. }
  3195. }
  3196. /**
  3197. * @return \think\response\Json|void
  3198. * @throws \think\db\exception\DataNotFoundException
  3199. * @throws \think\db\exception\DbException
  3200. * @throws \think\db\exception\ModelNotFoundException
  3201. */
  3202. public function diffstatus()
  3203. {
  3204. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  3205. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  3206. if ($id === "") {
  3207. return error_show(1004, "参数id不能为空");
  3208. }
  3209. $info = Db::name("sale_diff")->where(["id" => $id])->find();
  3210. if ($info == false) {
  3211. return error_show(1004, "工差单数据未找到");
  3212. }
  3213. $sale = Db::name("sale")->where(["orderCode" => $info["orderCode"]])->find();
  3214. if ($sale == false) {
  3215. return error_show(1004, "订单数据未找到");
  3216. }
  3217. $cgd = Db::name("purchease_diff")->where(['id' => $info['cgd_diffid']])->find();
  3218. if ($cgd == false) {
  3219. return error_show(1004, "采购工差单数据未找到");
  3220. }
  3221. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "3";
  3222. $remark = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
  3223. $ite = $info['status'];
  3224. Db::startTrans();
  3225. try {
  3226. $upda = [
  3227. "status" => $status,
  3228. "remark" => $remark,
  3229. "updatetime" => date("Y-m-d H:i:s"),
  3230. ];
  3231. $up = Db::name("sale_diff")->where($info)->save($upda);
  3232. if ($up) {
  3233. $stx = ["order_code" => $info["orderCode"], "status" => $ite, "action_remark" => '', "action_type" => "status"];
  3234. ActionLog::logAdd($this->post['token'], $stx, "XSGCD", $status, $info);
  3235. $order = ["order_type" => "XSGCD", "order_code" => $info["orderCode"], "order_id" => $info['id'],"order_status" => $status, "before_status" => $ite,'holder_id'=>$sale['apply_id']];
  3236. ProcessOrder::AddProcess($this->post['token'], $order);
  3237. $cgdup = Db::name("purchease_diff")->where(["id" => $info['cgd_diffid']])->save($upda);
  3238. if ($cgdup) {
  3239. if ($status == 2 && $info['customer_remark'] == 1) {
  3240. $sale['diff_weight'] = $info['diff_weight'];
  3241. $sale['diff_fee'] = $info['diff_price'];
  3242. $sale['updatetime'] = date("Y-m-d H:i:s");
  3243. $sal = Db::name("sale")->save($sale);
  3244. if ($sal == false) {
  3245. Db::rollback();
  3246. return error_show(1003, "订单更新工差失败");
  3247. }
  3248. }
  3249. Db::commit();
  3250. return app_show(0, "更新成功");
  3251. }
  3252. }
  3253. Db::rollback();
  3254. return error_show(1003, "更新失败");
  3255. } catch (\Exception $e) {
  3256. Db::rollback();
  3257. return error_show(1003, $e->getMessage());
  3258. }
  3259. }
  3260. /**
  3261. * @return \think\response\Json|void
  3262. * @throws \think\db\exception\DataNotFoundException
  3263. * @throws \think\db\exception\DbException
  3264. * @throws \think\db\exception\ModelNotFoundException
  3265. */
  3266. public function diffinfo()
  3267. {
  3268. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  3269. if ($id === "") {
  3270. return error_show(1004, "参数id不能为空");
  3271. }
  3272. $info = Db::name('sale_diff')->where(["id" => $id])->find();
  3273. if (empty($info)) {
  3274. return error_show(1004, "工差订单数据未找到");
  3275. }
  3276. $einfo = Db::name('sale')->where(['orderCode' => $info['orderCode'], 'is_del' => 0])->find();
  3277. if ($einfo == false) {
  3278. return error_show(1002, "未找到销售订单数据");
  3279. }
  3280. if ($einfo['order_type'] == 3) {
  3281. $goon = Db::name("good_zixun")->where(["spuCode" => $einfo['good_code'], "is_del" => 0])->find();
  3282. } else {
  3283. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  3284. ->where(['a.skuCode' => $einfo['skuCode']])->find();
  3285. }
  3286. if ($goon == false) {
  3287. return error_show(1002, "未找到商品数据");
  3288. }
  3289. $info['skuCode'] = isset($goon['skuCode']) ? $goon['skuCode'] : "";
  3290. $info['spuCode'] = isset($goon['spuCode']) ? $goon['spuCode'] : "";
  3291. $info['order_type'] = isset($einfo['order_type']) ? $einfo['order_type'] : "";
  3292. $info['can'] = isset($info['cat_id']) && $info['cat_id'] !== 0 ? made($info['cat_id']) : [];
  3293. return app_show(0, "获取成功", $info);
  3294. }
  3295. //发货申请单列表
  3296. public function saleout()
  3297. {
  3298. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  3299. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  3300. $where = [["a.is_del", "=", 0]];
  3301. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  3302. if ($orderCode !== "") {
  3303. $where[] = ['a.orderCode', "like", "%$orderCode%"];
  3304. }
  3305. $order_type = isset($this->post['order_type']) && $this->post['order_type'] !== "" ? trim($this->post['order_type']) : "";
  3306. if ($order_type !== "") {
  3307. $where[] = ['a.order_type', "=", $order_type];
  3308. }
  3309. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  3310. if ($outCode !== "") {
  3311. $where[] = ['a.outCode', "like", "%$outCode%"];
  3312. }
  3313. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !== "" ? trim($this->post['apply_name']) : "";
  3314. if ($apply_name !== "") {
  3315. $where[] = ['a.apply_name', "like", "%$apply_name%"];
  3316. }
  3317. $good_code = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : "";
  3318. if ($good_code !== "") {
  3319. $where[] = ['b.good_code', "like", "%$good_code%"];
  3320. }
  3321. $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim($this->post['good_name']) : "";
  3322. if ($good_name !== "") {
  3323. $where[] = ['b.good_name', "like", "%$good_name%"];
  3324. }
  3325. $supplier_name = isset($this->post['supplier_name']) && $this->post['supplier_name'] !== "" ? trim($this->post['supplier_name']) : "";
  3326. if ($supplier_name !== "") {
  3327. $where[] = ['wpo.supplier_name', "like", "%$supplier_name%"];
  3328. }
  3329. $status = isset($this->post['status']) && $this->post['status'] !== "" ? trim($this->post['status']) : "";
  3330. if ($status !== "") {
  3331. $where[] = ['a.status', "=", $status];
  3332. }
  3333. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  3334. if ($start != "") {
  3335. $where[] = ["a.addtime", '>=', $start." 00:00:00"];
  3336. }
  3337. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  3338. if ($end != "") {
  3339. $where[] = ["a.addtime", '<=', $end." 23:59:59"];
  3340. }
  3341. $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
  3342. if ($companyNo != "") {
  3343. $where[] = ['b.customer_code', "like", "%$companyNo%"];
  3344. }
  3345. $relaComNo = isset($this->post['relaComNo']) && $this->post['relaComNo'] !== "" ? trim($this->post['relaComNo']) : "";
  3346. if ($relaComNo != '') $where[] = ['b.customer_code|b.supplierNo', '=', $relaComNo];
  3347. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] !== "" ? $this->post['cgdNo'] : "";
  3348. if ($cgdNo != "") {
  3349. $where[] = ["os.cgdNo", 'like', '%' . $cgdNo . '%'];
  3350. }
  3351. // $role = $this->checkRole();
  3352. $condition ='';
  3353. // if (!empty($role['write']) && $this->uid != "") {
  3354. // // $where[]=["a.apply_id","in",$role['write']];
  3355. // $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(',',
  3356. // $role['write']) . ")";
  3357. // }
  3358. $hand = resign_hand_user($this->uid,0);
  3359. $uidarr =implode(",",$hand);
  3360. //库管只能看到库存品订单,供应商负责人只能看到非库存品订单
  3361. if(!in_array($this->roleid,[1,33])){
  3362. //库管看到所有的库存品发货申请单
  3363. if(in_array($this->roleid,config('app.wsm_cgder_role'))) $condition .= " b.is_stock=1";
  3364. else{
  3365. $role = $this->checkDataShare();
  3366. if (!empty($role[DataGroupModel::$type_全部])) {
  3367. $arr= array_unique(array_merge($hand,$role[DataGroupModel::$type_全部]));
  3368. if($condition!='') $condition.=" or ";
  3369. $condition .= " a.apply_id in (" . implode(',',$arr) . ")";
  3370. }
  3371. }
  3372. $personid=Db::name('supplier')->field('id')->where(['is_del'=>0,'personid'=> $hand])->findOrEmpty();
  3373. if($personid){
  3374. if($condition!='') $condition.=" or ";
  3375. $condition.="(b.is_stock=0 and sip.personid in ($uidarr))";
  3376. }
  3377. }
  3378. // if(!empty($role['platform']) ){
  3379. // $where[]=["b.platform_id","in",$role['platform']];
  3380. // }
  3381. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  3382. if ($company_name !== "") $where[] = ["a.apply_id", 'in', get_company_item_user_by_name($company_name)];
  3383. //发货时间筛选
  3384. $start_sendtime = isset($this->post['start_sendtime']) && $this->post['start_sendtime'] !== "" ? $this->post['start_sendtime'] : "";
  3385. $end_sendtime = isset($this->post['end_sendtime']) && $this->post['end_sendtime'] !== "" ? $this->post['end_sendtime'] : "";
  3386. if ($start_sendtime != "" && $end_sendtime != "") {
  3387. $where[] = ["a.sendtime", 'between', [$start_sendtime." 00:00:00", $end_sendtime." 23:59:59"]];
  3388. $where[] = ["a.status", '>=', 2];//搜索发货时间时,要指定状态为已发货及之后的状态值(0待发货,1待库管发货,2已发货待收货,3已收货,4已全部退货',)
  3389. }
  3390. $count = Db::name('order_out')
  3391. ->alias('a')
  3392. ->join("sale b", "b.orderCode=a.orderCode", "left")
  3393. ->join("customer_info v", "v.companyNo=b.customer_code", "left")
  3394. ->join("warehouse_info n", "n.wsm_code=a.wsm_code", "left")
  3395. ->leftJoin("depart_user u", "u.uid=a.apply_id AND u.is_del=0")
  3396. ->leftJoin("order_send os", "os.outCode=a.outCode")
  3397. ->leftJoin("purchease_order wpo", "wpo.cgdNo=os.cgdNo")
  3398. ->leftJoin('supplier sip', 'sip.code=wpo.supplierNo')
  3399. ->where($where)
  3400. ->where($condition)
  3401. ->count();
  3402. $total = ceil($count / $size);
  3403. $page = $page >= $total ? $total : $page;
  3404. $list = Db::name('order_out')
  3405. ->alias('a')
  3406. ->join("sale b", "b.orderCode=a.orderCode", "left")
  3407. ->join("customer_info v", "v.companyNo=b.customer_code", "left")
  3408. ->join("warehouse_info n", "n.wsm_code=a.wsm_code AND n.is_del=0", "left")
  3409. ->leftJoin("order_send os", "os.outCode=a.outCode")
  3410. ->leftJoin("purchease_order wpo", "wpo.cgdNo=os.cgdNo")
  3411. ->leftJoin("depart_user u", "u.uid=a.apply_id AND u.is_del=0")
  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,u.itemid")
  3414. ->where($where)
  3415. ->where($condition)
  3416. ->order("addtime desc")
  3417. ->page($page, $size)
  3418. ->cursor();
  3419. $data = [];
  3420. foreach ($list as $value) {
  3421. if($value['order_type']==1 && $value['wsm_code']==''){
  3422. $wsmcode = Db::name("good")->alias("a")
  3423. ->leftJoin("supplier b","a.supplierNo=b.code")
  3424. ->where(["a.spuCode"=>$value['good_code']])->field("'' wsm_name,b.name,b.code")->find();
  3425. }else{
  3426. $wsmcode = Db::name("warehouse_info")->alias("k")->leftJoin("supplier c", "k.supplierNo=c.code")
  3427. ->where(["k.wsm_code" => $value['wsm_code']])->field("k.name as wsm_name,c.name,c.code")->find();
  3428. }
  3429. $addr = Db::name("order_addr")->where(["id" => $value['addrid']])->find();
  3430. if($addr){
  3431. $addinfo =$addr['addr_code']!=''?json_decode($addr['addr_code'],true)??$addr['addr_code']: '' ;
  3432. if(is_string($addinfo) &&$addinfo!=''){
  3433. $addinfo=["provice_code"=>'',"city_code"=>'',"area_code"=>''];
  3434. list($addinfo['provice_code'],$addinfo['city_code'],$addinfo['area_code']) = explode(",",$addr['addr_code']);
  3435. }
  3436. $addr['addr_cn'] =GetAddr(json_encode($addinfo));
  3437. }
  3438. if ($value['order_type'] == 3) {
  3439. $goon = Db::name("good_zixun")->field('id,cat_id')->where(["spuCode" => $value['good_code'], "is_del" => 0])->find();
  3440. } else {
  3441. $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();
  3442. }
  3443. $value['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  3444. $value['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
  3445. $value['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  3446. $value['wsm_has_account'] = (int)isset(checkHasAccountBySupplierNos([$wsmcode['code']])[$wsmcode['code']]);
  3447. $value['addr'] = isset($addr['addr']) ? $addr['addr_cn'].$addr['addr'] : "";
  3448. $value['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : "";
  3449. $value['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : "";
  3450. $value['can'] = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  3451. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  3452. $value['sendtime'] = $value['status'] < 2 ? '' : $value['sendtime'];
  3453. //是否具有编辑权限
  3454. // $value['is_allow_update'] = 0;
  3455. // if (in_array($this->roleid, [1, 33]) || in_array($value['apply_id'], $role[DataGroupModel::$type_可编辑])) $value['is_allow_update'] = 1;
  3456. $data[] = $value;
  3457. }
  3458. return app_show(0, "获取成功", ['list' => $data, 'count' => $count]);
  3459. }
  3460. public function outinfo()
  3461. {
  3462. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  3463. if ($outCode == "") {
  3464. return error_show(1002, "参数outcode不能为空");
  3465. }
  3466. $codeinfo = Db::name("order_out")->where(['outCode' => $outCode, "is_del" => 0])->find();
  3467. if (empty($codeinfo)) {
  3468. return error_show(1002, "未找到出库数据");
  3469. }
  3470. $item = Db::name("sale")->where(['orderCode' => $codeinfo['orderCode']])->find();
  3471. if ($item['order_type'] == 3) {
  3472. $goodinfo = Db::name("good_zixun")->where(["spuCode" => $item['good_code'], "is_del" => 0])->find();
  3473. if ($goodinfo == false) {
  3474. return error_show(1004, "未找到商品数据");
  3475. }
  3476. } else {
  3477. $goodinfo = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  3478. ->where(['a.skuCode' => $item['skuCode']])->find();
  3479. if ($goodinfo == false) {
  3480. return error_show(1002, "未找到商品数据");
  3481. }
  3482. }
  3483. $int = isset($goodinfo['cat_id']) && $goodinfo['cat_id'] != 0 ? made($goodinfo['cat_id']) : [];
  3484. $addr = Db::name("order_addr")->where(["id" => $codeinfo['addrid']])->find();
  3485. if($addr){
  3486. $addinfo =$addr['addr_code']!=''?json_decode($addr['addr_code'],true)??$addr['addr_code']: '' ;
  3487. if(is_string($addinfo)&&$addinfo!=''){
  3488. $addinfo=["provice_code"=>'',"city_code"=>'',"area_code"=>''];
  3489. list($addinfo['provice_code'],$addinfo['city_code'],$addinfo['area_code']) = explode(",",$addr['addr_code']);
  3490. }
  3491. $addr['addr_cn'] =GetAddr(json_encode($addinfo));
  3492. }
  3493. $codeinfo['good_name'] = isset($item['good_name']) ? $item['good_name'] : "";
  3494. $codeinfo['good_num'] = isset($item['good_num']) ? $item['good_num'] : "";
  3495. $codeinfo['good_code'] = isset($item['good_code']) ? $item['good_code'] : "";
  3496. $codeinfo['origin_price'] = isset($item['origin_price']) ? $item['origin_price'] : "";
  3497. $codeinfo['sale_price'] = isset($item['sale_price']) ? $item['sale_price'] : "";
  3498. $codeinfo['total_price'] = isset($item['total_price']) ? $item['total_price'] : "";
  3499. $codeinfo['skuCode'] = isset($item['skuCode']) ? $item['skuCode'] : "";
  3500. $codeinfo['addr'] = $addr['addr_cn']??'';
  3501. $codeinfo['addr'] .= isset($addr['addr']) ? $addr['addr'] : "";
  3502. $codeinfo['addr_code'] = isset($addr['addr_code']) ? $addr['addr_code'] : "";
  3503. $codeinfo['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : "";
  3504. $codeinfo['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : "";
  3505. $codeinfo['sendtime'] = $codeinfo['status'] < 2 ? '' : $codeinfo['sendtime'];
  3506. $codeinfo['can'] = $int;
  3507. $codeinfo['order_source'] = $item['order_source'];
  3508. $orderReturn = Db::name("order_return")->where(["outCode" => $outCode, "is_del" => 0])->order("id desc")->find();
  3509. $codeinfo['order_return'] = $orderReturn;
  3510. //获取销售订单对应的供应商是否有启用账号
  3511. $codeinfo['has_account'] = 0;
  3512. $supplierNo = Db::name('order_num')
  3513. ->alias('a')
  3514. ->leftJoin('purchease_order b', 'b.cgdNo=a.cgdNo')
  3515. ->where('a.orderCode', $codeinfo['orderCode'])
  3516. ->value('b.supplierNo');
  3517. if ($supplierNo) {
  3518. $has_account = checkHasAccountBySupplierNos([$supplierNo]);
  3519. $codeinfo['has_account'] = (int)isset($has_account[$supplierNo]);
  3520. }
  3521. $saleinfo=Db::name("sale_info")->where([["orderCode","=",$codeinfo['orderCode']],["num",">",0]])->select()
  3522. ->toArray();
  3523. if(!empty($saleinfo)){
  3524. $stockidArr=array_column($saleinfo,"stockid");
  3525. $wsm=Db::name("good_stock")->alias("a")
  3526. ->leftJoin("warehouse_info b","a.wsm_code=b.wsm_code")
  3527. ->where("a.id","in",$stockidArr)
  3528. ->column("a.wsm_code,name","a.id");
  3529. foreach ($saleinfo as &$item){
  3530. $item['wsm_code'] = $wsm[$item['stockid']]['wsm_code'];
  3531. $item['wsm_name'] = $wsm[$item['stockid']]['name'];
  3532. }
  3533. }
  3534. $codeinfo["saleinfo"] = $saleinfo;
  3535. return app_show(0, "获取成功", $codeinfo);
  3536. }
  3537. public function outadd()
  3538. {
  3539. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  3540. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  3541. if ($orderCode == "") {
  3542. return error_show(1002, "参数orderCoder不能为空");
  3543. }
  3544. $retrun =Db::name("sale_return")->where([["orderCode","=",$orderCode],["is_del","=",0],["status","<",4]])
  3545. ->count();
  3546. if($retrun>0){
  3547. return error_show(1005,"存在未完成退货订单数据");
  3548. }
  3549. $der = Db::name('sale')->where(['orderCode' => $orderCode, "is_del" => 0])->find();
  3550. if ($der == "") {
  3551. return error_show(1002, "未找到出库订单单号");
  3552. }
  3553. $send_num = isset($this->post['send_num']) && $this->post['send_num'] !== "" ? $this->post['send_num'] : "";
  3554. if ($send_num == "") {
  3555. return error_show(1002, "发货数量不能为空");
  3556. }
  3557. $wsed = Db::name("order_out")->where(["orderCode" => $orderCode])->where([["status", "<", 2], ["is_del", "=", 0]])->sum
  3558. ('send_num');
  3559. if ($send_num > $der['wsend_num'] - $wsed) {
  3560. return error_show(1002, "超出可发货数量");
  3561. }
  3562. $contactor = isset($this->post['contactor']) && $this->post['contactor'] != "" ? trim($this->post['contactor']) : "";
  3563. if ($contactor == "") {
  3564. return error_show(1002, "参数contactor不能为空");
  3565. }
  3566. $mobile = isset($this->post['mobile']) && $this->post['mobile'] != "" ? trim($this->post['mobile']) : "";
  3567. if ($mobile == "") {
  3568. return error_show(1002, "参数mobile不能为空");
  3569. }
  3570. $addr = isset($this->post['addr']) && $this->post['addr'] != "" ? trim($this->post['addr']) : "";
  3571. if ($addr == "") {
  3572. return error_show(1002, "参数addr不能为空");
  3573. }
  3574. $addr_code = isset($this->post['addr_code']) && $this->post['addr_code'] != "" ? trim($this->post['addr_code']) : "";
  3575. if ($addr_code == "") {
  3576. return error_show(1002, "参数addr_code不能为空");
  3577. }
  3578. // $apply_id = GetUserInfo($token);
  3579. // if (empty($apply_id) || $apply_id['code'] != 0) {
  3580. // return error_show(1002, "申请人数据不存在");
  3581. // }
  3582. $rm = $this->uid;//isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  3583. $ri = $this->uname;//isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  3584. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !== "" ? $this->post['sendtime'] : date("Y-m-d H:i:s");
  3585. Db::startTrans();
  3586. try {
  3587. $addrlst = [
  3588. "orderCode" => $orderCode,
  3589. "addr" => $addr,
  3590. "addr_code" => json_encode($addr_code),
  3591. "contactor" => $contactor,
  3592. "mobile" => $mobile,
  3593. "customer_code" => $der['customer_code'],
  3594. "post_fee" => $der['post_fee'],
  3595. "arrive_time" => $sendtime,
  3596. "receipt_quantity" => $send_num,
  3597. "is_del" => 0,
  3598. "addtime" => date("Y-m-d H:i:s"),
  3599. "updatetime" => date("Y-m-d H:i:s")
  3600. ];
  3601. $addrid = Db::name("order_addr")->insert($addrlst, true);
  3602. if ($addrid > 0) {
  3603. $outCode = makeNo("DF");
  3604. $order=[];
  3605. if($der['order_type']!=1){
  3606. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  3607. ->lock(true)->find();
  3608. if ($order == false) {
  3609. Db::rollback();
  3610. return error_show(1004, "未找到可以发货得采购单数据");
  3611. }
  3612. $cgd = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
  3613. if ($cgd == false) {
  3614. Db::rollback();
  3615. return error_show(1002, "未找到对应的采购单数据");
  3616. }
  3617. $order['wsend_num'] -= $send_num;
  3618. $order['send_num'] += $send_num;
  3619. $or = Db::name("order_num")->save($order);
  3620. if ($or == false) {
  3621. Db::rollback();
  3622. return error_show(1002, "发货地址添加创建失败");
  3623. }
  3624. $tep = [
  3625. "cgdNo" => $order['cgdNo'],
  3626. "outCode" => $outCode,
  3627. "send_num" => $send_num,
  3628. "status" => 1,
  3629. "addtime" => date("Y-m-d H:i:s"),
  3630. "updatetime" => date("Y-m-d H:i:s")
  3631. ];
  3632. $sen = Db::name("order_send")->save($tep);
  3633. if ($sen == false) {
  3634. Db::rollback();
  3635. return error_show(1002, "发货地址添加创建失败");
  3636. }
  3637. }else{
  3638. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  3639. ->lock(true)->find();
  3640. if ($order!= false) {
  3641. $cgd = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
  3642. if ($cgd == false) {
  3643. Db::rollback();
  3644. return error_show(1002, "未找到对应的采购单数据");
  3645. }
  3646. $order['wsend_num'] -= $send_num;
  3647. $order['send_num'] += $send_num;
  3648. $or = Db::name("order_num")->save($order);
  3649. if ($or == false) {
  3650. Db::rollback();
  3651. return error_show(1002, "发货地址添加创建失败");
  3652. }
  3653. $tep = [
  3654. "cgdNo" => $order['cgdNo'],
  3655. "outCode" => $outCode,
  3656. "send_num" => $send_num,
  3657. "status" => 1,
  3658. "addtime" => date("Y-m-d H:i:s"),
  3659. "updatetime" => date("Y-m-d H:i:s")
  3660. ];
  3661. $sen = Db::name("order_send")->save($tep);
  3662. if ($sen == false) {
  3663. Db::rollback();
  3664. return error_show(1002, "发货地址添加创建失败");
  3665. }
  3666. }else{
  3667. $order['cgdNo']='';
  3668. $cgd=[];
  3669. }
  3670. }
  3671. $data = [
  3672. "wsm_code" => isset($cgd['wsm_code']) ? $cgd['wsm_code'] : "",
  3673. "orderCode" => $orderCode,
  3674. "outCode" => $outCode,
  3675. "order_type" => $der['order_type'],
  3676. "apply_id" => $rm,
  3677. "apply_name" => $ri,
  3678. "addrid" => $addrid,
  3679. "post_name" => "",
  3680. "post_code" => "",
  3681. "post_fee" => "",
  3682. "sendtime" => $sendtime,
  3683. "send_num" => $send_num,
  3684. "check_num" => 0,
  3685. "error_num" => 0,
  3686. "status" => 1,
  3687. "addtime" => date("Y-m-d H:i:s"),
  3688. "updatetime" => date("Y-m-d H:i:s")
  3689. ];
  3690. if ($der['is_stock'] == 0) {
  3691. //维护商品所在仓库的库存 --- start
  3692. $temp = Db::name('good_stock')
  3693. ->field('id,usable_stock,wait_out_stock')
  3694. ->where(['spuCode' => $der['good_code'], 'wsm_code' => $cgd['wsm_code']])
  3695. ->find();
  3696. if (($temp == false || ($temp['usable_stock'] - $send_num) < 0) && $der['send_type'] == 1) {
  3697. Db::rollback();
  3698. return error_show(1004, '库存不足');
  3699. } else {
  3700. if (($temp == false || ($temp['usable_stock'] - $send_num) < 0) && $der['send_type'] == 2) {
  3701. $data['status'] = 0;
  3702. } else {
  3703. $up = Db::name('good_stock')
  3704. ->where('id', $temp['id'])
  3705. ->update([
  3706. 'usable_stock' => $temp['usable_stock'] - $send_num,
  3707. 'wait_out_stock' => $temp['wait_out_stock'] + $send_num,
  3708. 'updatetime' => date('Y-m-d H:i:s'),
  3709. ]);
  3710. if ($up == false) {
  3711. Db::rollback();
  3712. return error_show(1004, '库存更新失败');
  3713. }
  3714. // ::todo
  3715. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $temp['id'], "type" => 1, 'stock' => $send_num, "stock_name" => "wait_out_stock"];
  3716. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $temp['id'], "type" => 2, 'stock' => $send_num, "stock_name" => "usable_stock"];
  3717. GoodLog::LogAdd($this->post['token'], $good_data, "CKD");
  3718. }
  3719. }
  3720. }
  3721. $datainfo = Db::name('order_out')->insert($data, true);
  3722. if ($datainfo > 0) {
  3723. //修改状态,添加待办
  3724. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
  3725. "order_code" => $data['outCode'],//出库单号
  3726. "status" => $data['status'],//这里的status是之前的值
  3727. "action_remark" => '',//备注
  3728. "action_type" => "create"//新建create,编辑edit,更改状态status
  3729. ], "CKD", $data['status'], $data);
  3730. //是否库存品,待办数据推送到的人有区别,
  3731. $process = [];
  3732. if ($data['status'] == 1) {
  3733. if ($der['is_stock'] == 1) {
  3734. //库存品,推给库管和库管-张凯旋
  3735. $roleid = config('app.wsm_cgder_role');
  3736. $uids = Db::name('user_role')
  3737. ->where('is_del', 0)
  3738. ->whereIn('roleid', $roleid)
  3739. ->column('uid');
  3740. $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)];
  3741. } else{
  3742. //推给供应商负责人
  3743. $supplier = Db::name('supplier')
  3744. ->field('person,personid')
  3745. ->where('code', $cgd['supplierNo'])
  3746. ->findOrEmpty();
  3747. $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'],];
  3748. }
  3749. }
  3750. 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']];
  3751. ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], $process);
  3752. //维护台账记录
  3753. Db::name('standing_book')
  3754. ->where('orderCode', $orderCode)
  3755. ->update([
  3756. 'order_type' => $der['order_type'],
  3757. 'order_source' => $der['order_source'],
  3758. 'cgdNo' => $order['cgdNo'],
  3759. 'customer_code' => $der['customer_code'],
  3760. 'updatetime' => date('Y-m-d H:i:s'),
  3761. ]);
  3762. Db::execute("UPDATE `wsm_standing_book` SET `outCode`=CONCAT(IFNULL(`outCode`,''),',{$outCode}') WHERE `cgdNo`='{$order['cgdNo']}'");
  3763. Db::commit();
  3764. return app_show(0, "出库单新建成功");
  3765. }
  3766. }
  3767. Db::rollback();
  3768. return error_show(1004, "出库单新建失败");
  3769. } catch (Exception $e) {
  3770. Db::rollback();
  3771. return error_show(1005, $e->getMessage());
  3772. }
  3773. }
  3774. //库管发货
  3775. public function outSend()
  3776. {
  3777. $outCode = isset($this->post['outCode']) && $this->post['outCode'] != "" ? trim($this->post['outCode']) : "";
  3778. if ($outCode == "") {
  3779. return error_show(1004, "参数outCode不能为空");
  3780. }
  3781. $outinfo = Db::name("order_out")->where(["outCode" => $outCode, "is_del" => 0])->find();
  3782. if ($outinfo == false) {
  3783. return error_show(1004, "发货数据未找到");
  3784. }
  3785. if($outinfo['status']!=1){
  3786. return error_show(1004, "发货状态有误");
  3787. }
  3788. $einfo = Db::name('sale')->where(['orderCode' => $outinfo['orderCode'], 'is_del' => 0])->find();
  3789. if (empty($einfo)) {
  3790. return error_show(1002, "未找到销售订单数据");
  3791. }
  3792. $is_reurn = Db::name("sale_return")->where(['orderCode' => $outinfo['orderCode'], 'is_del' => 0])->where("status", "<", 4)->find();
  3793. if ($is_reurn != false) {
  3794. return error_show(1002, "销售单存在退货未处理完成");
  3795. }
  3796. $post_name = isset($this->post['post_name']) && $this->post['post_name'] != "" ? trim($this->post['post_name']) : "";
  3797. if ($post_name == "") {
  3798. return error_show(1004, "参数post_name不能为空");
  3799. }
  3800. $post_code = isset($this->post['post_code']) && $this->post['post_code'] != "" ? trim($this->post['post_code']) : "";
  3801. if ($post_code == "") {
  3802. return error_show(1004, "参数post_code不能为空");
  3803. }
  3804. $remark = isset($this->post['remark']) && $this->post['remark'] != "" ? trim($this->post['remark']) : "";
  3805. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] != "" ? floatval($this->post['post_fee']) : "";
  3806. $outinfo['post_name'] = $post_name;
  3807. $outinfo['post_code'] = $post_code;
  3808. $outinfo['post_fee'] = $post_fee;
  3809. $outinfo['sendtime'] = date("Y-m-d H:i:s");
  3810. $old_outinfo_status = $outinfo['status'];
  3811. $outinfo['status'] = 2;
  3812. $outinfo['updatetime'] = date("Y-m-d H:i:s");
  3813. $outinfo['remark'] = $remark;
  3814. if ($einfo['wsend_num'] < $outinfo['send_num']) {
  3815. return error_show(1004, "订单未发货数量不足");
  3816. }
  3817. Db::startTrans();
  3818. try {
  3819. $up = Db::name("order_out")->where(["outCode"=>$outCode,"status"=>1,"is_del"=>0])->update($outinfo);
  3820. $user_info = GetUserInfo($this->post['token']);
  3821. $uid = isset($user_info['data']['id']) ? $user_info['data']['id'] : 0;
  3822. $uname = isset($user_info['data']['nickname']) ? $user_info['data']['nickname'] : '';
  3823. if ($einfo['is_stock'] == 1) {
  3824. //如果是库存品,只能由库管和库管-张凯旋操作
  3825. $own_roleid = Db::name('user_role')->where(['is_del'=>0,'uid'=>$uid,'status'=>1])->value('roleid',0);
  3826. if (!in_array($own_roleid, config('app.wsm_cgder_role'))) throw new \think\Exception('库存品只能由库管人员操作');
  3827. } elseif ($einfo['is_stock'] == 0 || $einfo['order_type'] == 3) {
  3828. //非库存品和采返商品只能由供应商负责人操作
  3829. if($einfo['order_type'] == 3){
  3830. $personid = Db::name('good_zixun')->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  3831. ->where(['a.spuCode' => $einfo['good_code'], 'a.is_del' => 0])
  3832. ->value('personid');
  3833. }else{
  3834. $personid = Db::name('good')->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  3835. ->where(['a.spuCode' => $einfo['good_code'], 'a.is_del' => 0])
  3836. ->value('personid');
  3837. }
  3838. if ($uid !=$personid) throw new \think\Exception('非库存品和采返商品只能由供应商负责人操作');
  3839. }
  3840. if ($up) {
  3841. //修改状态,添加待办
  3842. ActionLog::logAdd(['id' => $uid, 'nickname' => $uname], [
  3843. "order_code" => $outinfo['outCode'],//出库单号
  3844. "status" => $old_outinfo_status,//这里的status是之前的值
  3845. "action_remark" => '',//备注
  3846. "action_type" => "edit"//新建create,编辑edit,更改状态status
  3847. ], "CKD", $outinfo['status'], $outinfo);
  3848. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $uname], [
  3849. "order_type" => 'CKD',
  3850. "order_code" => $outinfo['outCode'],//出库单号
  3851. "order_id" => $outinfo['id'],
  3852. "order_status" => $outinfo['status'],
  3853. "before_status" => $old_outinfo_status,
  3854. 'wait_id'=>$outinfo['apply_id'],
  3855. 'wait_name'=>$outinfo['apply_name'],
  3856. 'holder_id'=>$outinfo['apply_id'],
  3857. ]);
  3858. if($einfo['wsend_num']<$outinfo['send_num']){
  3859. Db::rollback();
  3860. return error_show(1002, "订单待付货数量不足");
  3861. }
  3862. $orderstatus = $einfo['status'];
  3863. $einfo['send_num'] += $outinfo['send_num'];
  3864. $einfo['wsend_num'] -= $outinfo['send_num'];
  3865. $einfo['status'] = $einfo['wsend_num'] == 0 ? 2 : ($einfo['send_num'] == 0 ? 0 : 1);
  3866. $einfo['send_status'] = $einfo['wsend_num'] == 0 ? 3 : ($einfo['send_num'] == 0 ? 1 : 2);
  3867. $einfo['updatetime'] = date("Y-m-d H:i:s");
  3868. $saleup = Db::name("sale")->save($einfo);
  3869. if ($saleup == false) {
  3870. Db::rollback();
  3871. return error_show(1002, "销售单状态更新失败");
  3872. }
  3873. // }
  3874. //修改状态,添加待办
  3875. ActionLog::logAdd(['id' => $uid, 'nickname' => $uname], [
  3876. "order_code" => $einfo['orderCode'],//出库单号
  3877. "status" => $orderstatus,//这里的status是之前的值
  3878. "action_remark" => '',//备注
  3879. "action_type" => "edit"//新建create,编辑edit,更改状态status
  3880. ], "XSQRD", $einfo['status'], $einfo);
  3881. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $uname], [
  3882. "order_type" => 'XSQRD',
  3883. "order_code" => $einfo['orderCode'],//出库单号
  3884. "order_id" => $einfo['id'],
  3885. "order_status" => $einfo['status'],
  3886. "before_status" => $orderstatus,
  3887. 'holder_id' => $einfo['apply_id']
  3888. ]);
  3889. if($einfo["order_type"]!=1 ){
  3890. $stokc = Db::name("good_stock")->where(['spuCode' => $einfo['good_code'], "wsm_code" => $outinfo['wsm_code'], "is_del" => 0])->find();
  3891. if ($stokc == false) {
  3892. Db::rollback();
  3893. return error_show(1002, "未找到库存数据");
  3894. } else {
  3895. if ($outinfo['send_num'] > $stokc['wait_out_stock']) {
  3896. Db::rollback();
  3897. return error_show(1002, "超出库存数量");
  3898. }
  3899. $stokc['wait_out_stock'] -= $outinfo['send_num'];
  3900. $stokc['total_stock'] = $stokc['wait_out_stock'] + $stokc['usable_stock'];
  3901. // $stokc['intra_stock']+=$outinfo['send_num'];
  3902. $stokc['updatetime'] = date("Y-m-d H:i:s");
  3903. }
  3904. $stoc = Db::name("good_stock")->save($stokc);
  3905. if ($stoc == false) {
  3906. Db::rollback();
  3907. return error_show(1002, "库存更新失败");
  3908. }
  3909. $outsend =Db::name("order_send")->where(["outCode"=> $outCode])->findOrEmpty();
  3910. if(empty($outsend)){
  3911. Db::rollback();
  3912. return error_show(1002, "未找到关联采购单");
  3913. }
  3914. $cgdinfo= Db::name("purchease_order")->where(["cgdNo"=>$outsend['cgdNo'],"is_del"=>0])
  3915. ->findOrEmpty();
  3916. $bn_code =makeNo("BN");
  3917. $bnin=GoodStockInfo::AddBn($stokc["id"],$bn_code,$outinfo['send_num'],$cgdinfo['good_price']??0);
  3918. if($bnin==false){
  3919. Db::rollback();
  3920. return error_show(1004,"库存bn数据新增失败");
  3921. }
  3922. $bnup =GoodStockInfo::OrderBn($outinfo['orderCode'],$stokc["id"],$outinfo['send_num'],0);
  3923. if($bnup==false){
  3924. Db::rollback();
  3925. return error_show(1004,"库存bn库存数更新失败");
  3926. }
  3927. $outsend['bnCode']=$bn_code;
  3928. $outsend['updatetime']=date("Y-m-d H:i:s");
  3929. $ps=Db::name("order_send")->save($outsend);
  3930. if($ps==false){
  3931. Db::rollback();
  3932. return error_show(1004,"库存bn库存数更新失败");
  3933. }
  3934. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $stokc['id'], "type" => 2, 'stock' => $outinfo['send_num'], "stock_name" => "wait_out_stock"];
  3935. GoodLog::LogAdd(['id' => $uid, 'nickname' => $uname], $good_data, "CKD");
  3936. }else{
  3937. $saleinfo =Db::name("sale_info")->where(["orderCode"=>$outinfo['orderCode']])->select()->toArray();
  3938. if (!empty($saleinfo)){
  3939. $bnup =SaleInfo::SendStock($outinfo['orderCode'],$outinfo['send_num']);
  3940. if($bnup==false){
  3941. Db::rollback();
  3942. return error_show(1004,"库存bn库存数更新失败");
  3943. }
  3944. }else{
  3945. $stokc = Db::name("good_stock")->where(['spuCode' => $einfo['good_code'], "wsm_code" => $outinfo['wsm_code'], "is_del" => 0])->find();
  3946. if ($stokc == false) {
  3947. Db::rollback();
  3948. return error_show(1002, "未找到库存数据");
  3949. } else {
  3950. if ($outinfo['send_num'] > $stokc['wait_out_stock']) {
  3951. Db::rollback();
  3952. return error_show(1002, "超出库存数量");
  3953. }
  3954. $stokc['wait_out_stock'] -= $outinfo['send_num'];
  3955. $stokc['total_stock'] = $stokc['wait_out_stock'] + $stokc['usable_stock'];
  3956. // $stokc['intra_stock']+=$outinfo['send_num'];
  3957. $stokc['updatetime'] = date("Y-m-d H:i:s");
  3958. }
  3959. $stoc = Db::name("good_stock")->save($stokc);
  3960. if ($stoc == false) {
  3961. Db::rollback();
  3962. return error_show(1002, "库存更新失败");
  3963. }
  3964. }
  3965. }
  3966. Cache::store("redis")->handler()->lPush("SENDOUT", $outCode);
  3967. Db::commit();
  3968. } else {
  3969. Db::rollback();
  3970. return error_show(1004, "发货失败");
  3971. }
  3972. //如果是有赞订单的话,将发货信息推到有赞
  3973. //有赞信息有可能推送失败(比如超过72小时,不允许多次修改等),所以不应该和这里的事务放到一起
  3974. if ($einfo['order_source'] == 5) {
  3975. $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]);
  3976. $res = json_decode($res, true);
  3977. if ($res['code'] != 0) return app_show(0, '发货成功,' . $res['message']);
  3978. }
  3979. return app_show(0, "发货成功");
  3980. } catch (\Exception $e) {
  3981. Db::rollback();
  3982. return error_show(1004, $e->getMessage());
  3983. }
  3984. }
  3985. public function RelaCgd($outinfo, array &$standing_book_da = [])
  3986. {
  3987. $cgd = Db::name("order_bk")->where([["spuCode", "=", $outinfo['spuCode']], ["is_del", "=", 0], ["balance_num", ">=", $outinfo['good_num']], ['companyNo', "=", $outinfo['companyNo']]])->lock(true)->find();
  3988. if ($cgd == false) {
  3989. return false;
  3990. }
  3991. $good = Db::name("good")->where(["spuCode" => $outinfo['spuCode'], "is_del" => 0])->find();
  3992. if ($good == false) {
  3993. return false;
  3994. }
  3995. $cgdinfo = Db::name("purchease_order")->where(['cgdNo' => $cgd['cgdNo'], "is_del" => 0])->find();
  3996. if ($cgdinfo == false) {
  3997. return false;
  3998. }
  3999. $QrdCgd = [
  4000. "cgdNo" => makeNo("CG"),
  4001. "bkcode" => $cgdinfo['bkcode'],
  4002. 'wsm_code' => $cgdinfo['wsm_code'],
  4003. "cgder_id" => $cgdinfo['cgder_id'],
  4004. "cgder" => $cgdinfo['cgder'],
  4005. "spuCode" => $cgdinfo['spuCode'],
  4006. "good_name" => $cgdinfo['good_name'],
  4007. "good_num" => $outinfo['good_num'],
  4008. "good_price" => $cgdinfo['good_price'],
  4009. "total_fee" => round($cgdinfo['good_price'] * $outinfo['good_num'], 2),
  4010. "pakge_fee" => $cgdinfo['pakge_fee'],
  4011. "cert_fee" => $cgdinfo['cert_fee'],
  4012. "open_fee" => $cgdinfo['open_fee'],
  4013. "delivery_fee" => $cgdinfo['delivery_fee'],
  4014. "mark_fee" => $cgdinfo['mark_fee'],
  4015. "teach_fee" => $cgdinfo['teach_fee'],
  4016. "nake_fee" => $cgdinfo['nake_fee'],
  4017. "demo_fee" => $cgdinfo['demo_fee'],
  4018. "weight" => $cgdinfo['weight'],
  4019. "diff_weight" => $cgdinfo['diff_weight'],
  4020. "diff_fee" => $cgdinfo['diff_fee'],
  4021. "gold_price" => $cgdinfo['gold_price'],
  4022. "supplierNo" => $cgdinfo['supplierNo'],
  4023. "supplier_name" => $cgdinfo['supplier_name'],
  4024. "companyNo" => $cgdinfo['companyNo'],
  4025. "send_status" => 3,
  4026. "send_num" => $outinfo['good_num'],
  4027. "wsend_num" => 0,
  4028. "remark" => $cgdinfo['remark'],
  4029. "status" => 3,
  4030. "lasttime" => $cgdinfo['lasttime'],
  4031. "is_del" => 0,
  4032. "order_type" => $outinfo['order_type'],
  4033. "order_source" => $outinfo['order_source'],
  4034. "good_type" => $cgdinfo['good_type'],
  4035. "addtime" => date("Y-m-d H:i:s"),
  4036. "updatetime" => date("Y-m-d H:i:s"),
  4037. 'good_createrid' => $good['createrid'],
  4038. 'good_creater' => $good['creater'],//商品创建人
  4039. ];
  4040. $insetrCgd = Db::name("purchease_order")->insert($QrdCgd);
  4041. if ($insetrCgd == false) {
  4042. return false;
  4043. } else {
  4044. $standing_book_da = array_merge($standing_book_da, [
  4045. 'orderCode' => $outinfo['orderCode'],
  4046. 'cgdNo' => $cgdinfo['cgdNo'],
  4047. 'spuCode' => $outinfo['spuCode'],
  4048. 'order_type' => $QrdCgd['order_type'],
  4049. 'order_source' => $QrdCgd['order_source'],
  4050. 'supplierNo' => $cgdinfo['supplierNo'],
  4051. 'companyNo' => $cgdinfo['companyNo'],
  4052. 'bk_code' => $cgdinfo['bkcode'],
  4053. 'purchease_id' => Db::name('purchease')->where('bk_code', $cgdinfo['bkcode'])->value('id'),
  4054. ]);
  4055. }
  4056. if ($good['is_gold_price'] == 1 && $good['is_stock'] == 1) {
  4057. $gold = Db::name("gold_price1")
  4058. ->field('id,price')
  4059. ->where(["type" => $good['noble_metal'], "is_del" => 0, "status" => 1])
  4060. ->order("addtime desc")
  4061. ->find();
  4062. //$saleprice(最终售价) = (打样费/购买数量 + 开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价+物流费)/(1-成本售价/100);
  4063. $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'];
  4064. $ct['cgd_gold_price'] = $gold["price"];
  4065. $updat = [
  4066. "sale_price" => $sale_price,
  4067. "total_price" => round($sale_price * $outinfo['good_num'], 2),
  4068. "origin_price" => $cgdinfo['good_price'],
  4069. "gold_price" => $gold["price"],
  4070. ];
  4071. $upsale = Db::name("sale")->where(["orderCode" => $outinfo['orderCode']])->update($updat);
  4072. if ($upsale == false) {
  4073. return false;
  4074. }
  4075. }
  4076. $merge_num = Db::name("purchease_order")->where(["bkcode" => $cgdinfo['bkcode'], "order_type" => 1, "is_del" => 0])
  4077. ->where("order_source", "<>", 0)
  4078. ->field("sum(send_num)-sum(th_num) as num")->find();
  4079. $cgd['balance_num'] = $cgd['total_num'] - $merge_num['num'];
  4080. $cgd['merge_num'] = $merge_num['num'];
  4081. $cgd['updatetime'] = date("Y-m-d H:i:s");
  4082. $up = Db::name("order_bk")->save($cgd);
  4083. if ($up == false) {
  4084. return false;
  4085. }
  4086. $data = [
  4087. "orderCode" => $outinfo['orderCode'],
  4088. "cgdNo" => $QrdCgd['cgdNo'],
  4089. "spuCode" => $outinfo['spuCode'],
  4090. "companyNo" => $outinfo['companyNo'],
  4091. "good_num" => $outinfo['good_num'],
  4092. "wsend_num" => $outinfo['good_num'],
  4093. "send_num" => 0,
  4094. "wait_num" => $outinfo['good_num'],
  4095. "status" => 1,
  4096. "source" => 1,
  4097. ];
  4098. $order = Db::name("order_num")->save($data);
  4099. if ($order == false) {
  4100. return false;
  4101. }
  4102. return true;
  4103. }
  4104. public function addother()
  4105. {
  4106. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] != "" ? trim($this->post['orderCode']) : "";
  4107. if ($orderCode == "") {
  4108. return error_show(1004, "参数orderCode不能为空");
  4109. }
  4110. $order = Db::name("sale")->where(["orderCode" => $orderCode, "is_del" => 0])->find();
  4111. if ($order == false) {
  4112. return error_show(1004, "未找到订单数据");
  4113. }
  4114. $paytime = isset($this->post['paytime']) && $this->post['paytime'] != "" ? $this->post['paytime'] : "";
  4115. $workNo = isset($this->post['workNo']) && $this->post['workNo'] != "" ? trim($this->post['workNo']) : "";
  4116. $platform_order = isset($this->post['platform_order']) && $this->post['platform_order'] != "" ? trim($this->post['platform_order']) : "";
  4117. $paytime == "" ? "" : $order['paytime'] = $paytime;
  4118. $order['workNo'] = $workNo;
  4119. $order['platform_order'] = $platform_order;
  4120. $order['updatetime'] = date("Y-m-d H:i:s");
  4121. $sa = Db::name("sale")->save($order);
  4122. if ($sa) {
  4123. return app_show(0, "更新成功");
  4124. } else {
  4125. return error_show(1004, "更新失败");
  4126. }
  4127. }
  4128. public function getPrice()
  4129. {
  4130. $skuCode = isset($this->post['skuCode']) && $this->post['skuCode'] != "" ? trim($this->post['skuCode']) : "";
  4131. if ($skuCode === "") {
  4132. return error_show(1003, "参数skuCode不能为空");
  4133. }
  4134. $is_activity = isset($this->post['is_activity']) && $this->post['is_activity'] !== "" ? intval($this->post['is_activity'])
  4135. : "";
  4136. if ($is_activity === "") {
  4137. return error_show(1003, "参数is_activity不能为空");
  4138. }
  4139. $sale_num = isset($this->post['sale_num']) && $this->post['sale_num'] !== "" ? intval($this->post['sale_num']) : "";
  4140. if ($sale_num === "") {
  4141. return error_show(1003, "参数sale_num不能为空");
  4142. }
  4143. $ct = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  4144. ->where(['a.skuCode' => $skuCode])->find();
  4145. if ($ct == false) {
  4146. return error_show(1002, "未找到商品数据");
  4147. }
  4148. $stock = 0;
  4149. if ($ct['is_stock'] == 1) {
  4150. $good_stock = Db::name("good_stock")->alias("a")->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")->where
  4151. (["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();
  4152. // $good_stock = Db::name("good_stock")->where(["spuCode" => $ct['spuCode'], "is_del" => 0])->find();
  4153. $stock = isset($good_stock['usable_stock']) ? $good_stock['usable_stock'] : "0";
  4154. }
  4155. if ($is_activity == 1) {
  4156. $act = Db::name("activity_info")->alias("a")->leftJoin("good_activity b", "a.activity_code=b.activity_code")
  4157. ->where(["a.skuCode" => $skuCode, "a.is_del" => 0, "a.status" => 1, "b.status" => 6, "b.is_del" => 0])->find();
  4158. if ($act == false) {
  4159. return error_show(1003, "未找到相关活动价");
  4160. }
  4161. if ($act['moq_num'] > $sale_num) {
  4162. return error_show(1003, "商品不满足活动价起订量{$act['moq_num']}");
  4163. }
  4164. if ($act['activity_stock'] < $sale_num) {
  4165. return error_show(1003, "商品活动库存剩余{$act['activity_stock']}");
  4166. }
  4167. return app_show(0, "获取成功", ['sale_price' => $act['activity_price'], "stock" => $act['activity_stock']]);
  4168. } else {
  4169. //good_nake 成本阶梯 good_ladder销售阶梯
  4170. $origin = Db::name("good_nake")->where([["spuCode", "=", $ct['spuCode']], ["is_del", "=", 0]])->order("min_num asc")->find();
  4171. if (!$origin) {
  4172. return error_show(1003, "没有找到成本数据");
  4173. }
  4174. $good = Db::name("good_ladder")->where(["skuCode" => $skuCode, "is_del" => 0, "status" => 1])->order("min_num asc")->find();
  4175. if (!$good) {
  4176. return error_show(1003, "未找到相关阶梯价格");
  4177. }
  4178. if (!isset($origin['min_num'])) $origin['min_num'] = 0;
  4179. //如果是库存品的话,只取销售的库存
  4180. if ($ct['is_stock'] == 1) $lastnum = $good['min_num'];
  4181. else $lastnum = max($origin['min_num'] ?? 0, $good['min_num']);
  4182. if ($sale_num < $lastnum) {
  4183. return error_show(1003, "商品不满足起订量{$lastnum}");
  4184. }
  4185. $good_temp = Db::name("good_ladder")->where(["skuCode" => $skuCode, "is_del" => 0, "status" => 1])->where('min_num', '<=', $sale_num)->order("min_num desc")->find();
  4186. if (!$good_temp) {
  4187. return error_show(1003, "未找到相关阶梯价格");
  4188. }
  4189. $cat_top_list = made($ct['cat_id']);
  4190. $cat_top_id = isset($cat_top_list[0]['id']) ? $cat_top_list[0]['id'] : 0;
  4191. if ($ct['is_gold_price'] == 1 && $cat_top_id == 6) {
  4192. $gold = Db::name("gold_price1")
  4193. ->field('id,price')
  4194. ->where(["type" => $ct['noble_metal'], "is_del" => 0, "status" => 1])
  4195. ->order("addtime desc")
  4196. ->find();
  4197. $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'];
  4198. return app_show(0, "获取成功", ['sale_price' => $saleprice, "stock" => $stock]);
  4199. }
  4200. return app_show(0, "获取成功", ['sale_price' => $good_temp['sale_price'], "stock" => $stock]);
  4201. }
  4202. }
  4203. public function saleuse()
  4204. {
  4205. $orderCode = isset($this->post['orderCode']) && !empty($this->post['orderCode']) ? $this->post['orderCode'] : "";
  4206. if ($orderCode == "") {
  4207. return error_show(1003, "参数orderCode不能为空");
  4208. }
  4209. $order = Db::name("sale")->where(["orderCode" => $orderCode, "is_del" => 0])->select()->toArray();
  4210. if (empty($order)) {
  4211. return error_show(1003, "订单数据未找到");
  4212. }
  4213. $useid = isset($this->post['useid']) && $this->post['useid'] != "" ? intval($this->post['useid']) : "";
  4214. if ($useid == "") {
  4215. return error_show(1003, "参数useid不能为空");
  4216. }
  4217. $use = Db::name("order_use")->where(["id" => $useid, "is_del" => 0])->find();
  4218. if ($use == false) {
  4219. return error_show(1003, "用途数据未找到");
  4220. }
  4221. $asve = ['use_order' => $useid, "updatetime" => date("Y-m-d H:i:s")];
  4222. $up = Db::name("sale")->where(["orderCode" => $orderCode, "is_del" => 0])->update($asve);
  4223. if ($up > 0) {
  4224. return app_show(0, "更新成功");
  4225. } else {
  4226. return error_show(1003, "更新失败");
  4227. }
  4228. }
  4229. public function goodzxinfo()
  4230. {
  4231. $orderCode = isset($this->post['spuCode']) && $this->post['spuCode'] != '' ? trim($this->post['spuCode']) : "";
  4232. if ($orderCode == "") {
  4233. return error_show(1003, "参数spuCode不能为空");
  4234. }
  4235. $order = Db::name("good_zixun")->where(["spuCode" => $orderCode, "is_del" => 0])->find();
  4236. if (empty($order)) {
  4237. return error_show(1003, "订单数据未找到");
  4238. }
  4239. $unit = Db::name("unit")->where(["id" => $order['good_unit']])->find();
  4240. if ($order['brand_id'] != 0) {
  4241. $brand = Db::name("brand")->where(["id" => $order['brand_id']])->find();
  4242. $order["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  4243. } else {
  4244. $order["brand_name"] = "";
  4245. $order["brand_id"] = "";
  4246. }
  4247. $order['specinfo'] = json_decode($order['specinfo'], true);
  4248. $order['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  4249. $order['cat_info'] = made($order['cat_id'], []);
  4250. $order['noble_name'] = isset($order['noble_metal']) && $order['noble_metal'] != 0 ? $this->noble[$order['noble_metal']] : "";
  4251. if ($order['is_gold_price'] == 1) {
  4252. $price = Db::name("gold_price1")->where(["type" => $order['noble_metal'], "status" => 1, "is_del" => 0])->order("addtime desc")->find();
  4253. }
  4254. $supplier = Db::name("supplier")->where(["code" => $order['supplierNo']])->find();
  4255. $order['supplier_name'] = isset($supplier['name']) ? $supplier['name'] : "";
  4256. $order['cgder'] = isset($supplier['person']) ? $supplier['person'] : "";
  4257. $order['cgderid'] = isset($supplier['personid']) ? $supplier['personid'] : "";
  4258. $palt = Db::name("platform")->where(["id" => $order['platform_id']])->find();
  4259. $order['platform_name'] = isset($palt['platform_name']) ? $palt['platform_name'] : "";
  4260. $company = Db::name("business")->where(["companyNo" => $order['companyNo']])->find();
  4261. $order['company'] = isset($company['company']) ? $company['company'] : "";
  4262. $order["gold_price"] = isset($price['price']) ? $price['price'] : 0;
  4263. $order["noble_weight"] = isset($order['weight']) ? $order['weight'] : 0;
  4264. return app_show(0, "获取成功", $order);
  4265. }
  4266. //获取采反商品详情,当数据不在good_zixun中,模仿goodzxinfo
  4267. public function getGoodZxInfoByNotZixun()
  4268. {
  4269. $param = $this->request->only(['spuCode'], 'post', 'trim');
  4270. $val = Validate::rule(['spuCode' => 'require']);
  4271. if (!$val->check($param)) return error_show(1004, $val->getError());
  4272. $order = Db::name('consult_bids')
  4273. ->field(true)
  4274. ->where(['spuCode' => $param['spuCode'], 'is_del' => 0])
  4275. ->find();
  4276. if (empty($order)) return error_show(1003, "数据未找到");
  4277. // $orderCode= isset($this->post['spuCode'])&&$this->post['spuCode']!=''?trim($this->post['spuCode']):"";
  4278. // if($orderCode==""){
  4279. // return error_show(1003,"参数spuCode不能为空");
  4280. // }
  4281. // $order= Db::name("good_zixun")->where(["spuCode"=>$orderCode,"is_del"=>0])->find();
  4282. // if(empty($order)){
  4283. // return error_show(1003,"订单数据未找到");
  4284. // }
  4285. $unit = Db::name("unit")->where(["id" => $order['unit_id']])->find();
  4286. if ($order['brand_id'] != 0) {
  4287. $brand = Db::name("brand")->where(["id" => $order['brand_id']])->find();
  4288. $order["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  4289. } else {
  4290. $order["brand_name"] = "";
  4291. $order["brand_id"] = "";
  4292. }
  4293. $order['specinfo'] = json_decode($order['specinfo'], true);
  4294. $order['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  4295. $order['cat_info'] = made($order['cat_id'], []);
  4296. $order['noble_name'] = isset($order['metal_id']) && $order['metal_id'] != 0 ? $this->noble[$order['metal_id']] : "";
  4297. if ($order['is_gold_price'] == 1) {
  4298. $price = Db::name("gold_price1")->where(["type" => $order['metal_id'], "status" => 1, "is_del" => 0])->order("addtime desc")->find();
  4299. }
  4300. $supplier = Db::name("supplier")->where(["code" => $order['supplierNo']])->find();
  4301. $order['supplier_name'] = isset($supplier['name']) ? $supplier['name'] : "";
  4302. // $palt =Db::name("platform")->where(["id"=>$order['platform_id']])->find();
  4303. // $order['platform_name']=isset($palt['platform_name']) ? $palt['platform_name']:"";
  4304. $order["gold_price"] = isset($price['price']) ? $price['price'] : 0;
  4305. $order["noble_weight"] = isset($order['weight']) ? $order['weight'] : 0;
  4306. $good_info = Db::name('good_basic')
  4307. ->field('id,createrid,creater')
  4308. ->where('is_del',0)
  4309. ->where('spuCode',$order['spuCode'])
  4310. ->findOrEmpty();
  4311. //补充商品创建人字段
  4312. // 2022-9-7号确认 creater创建人即采购单的采购员 咨询cgder 只是供应商负责人
  4313. $order['good_createrid']=$good_info['createrid']??$order['createrid'];
  4314. $order['good_creater']=$good_info['creater']??$order['creater'];
  4315. return app_show(0, "获取成功", $order);
  4316. }
  4317. //发货申请单导出
  4318. public function exportSaleOut()
  4319. {
  4320. $outCodes = $this->request->post('outCodes', [], 'trim');
  4321. if (empty($outCodes)) return error_show(1004, '要导出的发货申请单编号不能为空');
  4322. $i = 1;
  4323. $send_type = [1 => '直接发货', 2 => '延时发货'];
  4324. $status = [0 => '待发货', 1 => '待库管发货', 2 => '已发货待收货', 3 => '已收货', 4 => '已全部退货'];
  4325. $list = Db::name("order_out")
  4326. ->alias('a')
  4327. ->field('"" as 序号,a.addtime as 创建时间,po.cgdNo as 采购单编号,po.status as 采购单状态,po.cgder as 采购员,b.orderCode as 确认单号,b.addtime as 确认单时间,
  4328. b.good_code as 产品编号,b.good_name as 产品名称,"" as 规格,"" as 单位,po.supplier_name as 供应商名称,po.supplierNo as 供应商编号,po.nake_fee as 裸价,
  4329. 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 发货数量,
  4330. oa.contactor as 收货人,oa.mobile as 联系方式,oa.addr as 收货地址,oa.addr_code,oa.arrive_time as 到货时间,a.outCode as 发货申请单号,po.order_type,
  4331. b.platform_order 平台订单号,a.post_name 发货申请单物流公司,a.post_code 发货申请单物流单号,a.post_fee 发货申请单物流费用,a.remark 发货申请单备注,a.apply_name 申请人,b.cat_id 商品分类')
  4332. ->whereIn('a.outCode', $outCodes)
  4333. ->order("a.addtime desc")
  4334. ->withAttr('序号', function () use (&$i) {
  4335. return $i++;
  4336. })->withAttr('采购单状态', function ($val) use ($status) {
  4337. return isset($status[$val]) ? $status[$val] : '';
  4338. })->withAttr('发货方式', function ($val) use ($send_type) {
  4339. return isset($send_type[$val]) ? $send_type[$val] : '';
  4340. })->where("a.is_del", "=", 0)
  4341. ->leftJoin("sale b", "b.orderCode=a.orderCode")
  4342. ->leftJoin("order_addr oa", "oa.id=a.addrid AND oa.is_del=0")
  4343. ->leftJoin('order_send os', 'os.outCode=a.outCode ')
  4344. ->leftJoin("purchease_order po", "po.cgdNo=os.cgdNo AND po.is_del=0")
  4345. ->select()
  4346. ->toArray();
  4347. foreach ($list as &$value) {
  4348. if ($value['order_type'] == 3) {
  4349. $good = Db::name("good_zixun")
  4350. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  4351. ->field('id,specinfo,good_unit,supplierNo')
  4352. ->find();
  4353. $specinfo = isset($good['specinfo']) ? json_decode($good['specinfo'], true) : [];
  4354. $speclist = [];
  4355. foreach ($specinfo as $val) {
  4356. $speclist[] = $val['spec_name'] . ':' . $val['spec_value_name'];
  4357. }
  4358. } else {
  4359. $good = Db::name('good_basic')
  4360. ->where(['spuCode' => $value['产品编号']])
  4361. ->field('good_unit,supplierNo')
  4362. ->find();
  4363. $spec = Db::name("good_spec")
  4364. ->field('id,spec_id,spec_value_id')
  4365. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  4366. ->select()
  4367. ->toArray();
  4368. $speclist = [];
  4369. if (!empty($spec)) {
  4370. foreach ($spec as $val) {
  4371. $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', '');
  4372. }
  4373. }
  4374. }
  4375. $value['供应商编号'] = $good['supplierNo']??"";
  4376. $supplier= Db::name("supplier")->where(["code"=>$value['供应商编号']])->field("name,person")->find();
  4377. $value['供应商名称'] = $supplier['name']??"";
  4378. $value['采购员'] = $supplier['person']??"";
  4379. $good_unit = isset($good['good_unit']) ? $good['good_unit'] : 0;
  4380. $value['规格'] = empty($speclist) ? '' : implode(',', $speclist);;
  4381. $value['单位'] = $good_unit ? Db::name('unit')->where(['id' => $good_unit, 'is_del' => 0])->value('unit', '') : '';
  4382. $value['商品分类'] = implode('/', array_column(made($value['商品分类']), 'name'));
  4383. $value['发货申请单物流单号'] = ' ' . $value['发货申请单物流单号'];//添加空格防止将数字转为科学计数法(比如,韵达的快递单号就是纯数字)
  4384. $addinfo =$value['addr_code']!=''?json_decode($value['addr_code'],true)??$value['addr_code']: '' ;
  4385. if(is_string($addinfo)&&$addinfo!=''){
  4386. $addinfo=["provice_code"=>'',"city_code"=>'',"area_code"=>''];
  4387. list($addinfo['provice_code'],$addinfo['city_code'],$addinfo['area_code']) = explode(",",$value['addr_code']);
  4388. }
  4389. $addr_cn =GetAddr(json_encode($addinfo));
  4390. $value['收货地址'] = $addr_cn.$value['收货地址'];
  4391. unset($value['order_type']);
  4392. unset($value['addr_code']);
  4393. }
  4394. if (empty($list)) $list[] = '没有可供导出的数据';
  4395. $headerArr = array_keys($list[0]);
  4396. excelSave('发货申请单导出' . date('YmdHis'), $headerArr, $list);
  4397. }
  4398. //发货申请单 签收(目前只改动状态,后面要实时查询物流信息,以判断是否签收)
  4399. public function saleoutReceipt()
  4400. {
  4401. $outCode = $this->request->post('outCode', '', 'trim');
  4402. if (empty($outCode)) return error_show(1004, '发货申请单编号不能为为空');
  4403. Db::startTrans();
  4404. try {
  4405. $rs = Db::name('order_out')
  4406. ->field('id,orderCode,apply_id')
  4407. ->where(['outCode' => $outCode, 'status' => 2, "is_del" => 0])
  4408. ->find();
  4409. if (empty($rs)) throw new Exception('该出库单不存在或不允许确认收货');
  4410. //发货单收货
  4411. $res_order_out = Db::name('order_out')
  4412. ->where(['id' => $rs['id'], 'status' => 2, "is_del" => 0])
  4413. ->update(['status' => 3, 'updatetime' => date('Y-m-d H:i:s')]);
  4414. //查询该发货单所属的销售单,是否还有未收货的其他发货单
  4415. $other_res_sale = Db::name('order_out')
  4416. ->where(['orderCode' => $rs['orderCode'], 'status' => 2, "is_del" => 0])
  4417. ->where('id', '<>', $rs['id'])
  4418. ->find();
  4419. if (empty($other_res_sale)) {
  4420. //所属销售单也确认收货
  4421. Db::name('sale')
  4422. ->where(['orderCode' => $rs['orderCode'], 'status' => 0])
  4423. ->update(['status' => 2, 'updatetime' => date('Y-m-d H:i:s')]);
  4424. }
  4425. //修改状态,添加待办
  4426. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
  4427. "order_code" => $outCode,
  4428. "status" => 2,//这里的status是之前的值
  4429. "action_remark" => '',//备注
  4430. "action_type" => "status"//新建create,编辑edit,更改状态status
  4431. ], "CKD", 3, $rs);
  4432. //将待办数据变为已办
  4433. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  4434. "order_code" => $outCode,
  4435. "order_id" => $rs['id'],
  4436. "order_status" => 3,
  4437. "order_type" => 'CKD',
  4438. "before_status" => 2,
  4439. 'holder_id' => $rs['apply_id']
  4440. ]);
  4441. if ($res_order_out) {
  4442. Db::commit();
  4443. return app_show(0, '收货完成');
  4444. } else {
  4445. Db::rollback();
  4446. error_show(1005, '收货失败');
  4447. }
  4448. } catch (\Exception $e) {
  4449. Db::rollback();
  4450. return error_show(1005, $e->getMessage());
  4451. }
  4452. }
  4453. //库管批量发货
  4454. public function outSendBatchByImport()
  4455. {
  4456. $param = $this->request->only(['list', 'token'], 'post', 'trim');
  4457. if (empty($param['list'])) return error_show(1005, 'list参数不能为空');
  4458. //只允许处理所属供应商下的采购单关联的发货单
  4459. $suppliers = Db::name('order_out')
  4460. ->alias('a')
  4461. ->leftJoin('order_num b', 'b.orderCode=a.orderCode')
  4462. ->leftJoin('purchease_order c', 'c.cgdNo=b.cgdNo')
  4463. ->where([
  4464. ['a.is_del', '=', 0],
  4465. ['c.order_type', '<>', 1],
  4466. ['a.outCode', 'in', array_column($param['list'], 'outCode')],
  4467. ])->column('c.supplierNo', 'a.outCode');
  4468. //检查这些供应商编码是否开通了账号
  4469. $temp_res = SupplierRelationUser::where(['is_del' => SupplierUser::$is_del_normal, 'status' => SupplierUser::$status_normal])
  4470. ->whereIn('supplierNo', array_values($suppliers))
  4471. ->column('supplierNo');
  4472. $temp = [];
  4473. foreach ($suppliers as $k => $v) {
  4474. if (in_array($v, $temp_res)) $temp[] = $k;
  4475. }
  4476. if (!empty($temp)) return json_show(1003, '不允许处理已开通账号的供应商的单子', $temp);
  4477. $user = GetUserInfo($param['token']);
  4478. $createrid = $user['data']['id']??0;//$this->uid;
  4479. $creater = $user['data']['nickname']??0;//$this->uname;
  4480. $own_roleid = Db::name('user_role')->where(['is_del'=>0,'uid'=>$createrid,'status'=>1])->value('roleid',0);
  4481. //所有发货单信息
  4482. $order_out_infos = Db::name("order_out")
  4483. ->where('status', 1)
  4484. ->where('is_del', 0)
  4485. ->whereIn('outCode', array_column($param['list'], 'outCode'))
  4486. ->column('id,status,send_num,orderCode,wsm_code,apply_id,apply_name', 'outCode');
  4487. //处理数据
  4488. Db::startTrans();
  4489. try {
  4490. $good_data = $temp_out_codes = $yz_data = [];
  4491. foreach ($param['list'] as $value) {
  4492. if (!isset($order_out_infos[$value['outCode']])) throw new Exception($value['outCode'] . '该发货单未找到或状态不允许发货');
  4493. //每次循环都需要重新查询对应数据,为了数据及时更新
  4494. $sale_infos = Db::name("sale")
  4495. ->where('orderCode', $order_out_infos[$value['outCode']]['orderCode'])
  4496. ->where('is_del', 0)
  4497. ->column('id,send_num,wsend_num,status,good_code,order_source,order_type,apply_id,apply_name,is_stock,cgderid,orderCode', 'orderCode');
  4498. if (!isset($sale_infos[$order_out_infos[$value['outCode']]['orderCode']])) throw new Exception($value['outCode'] . '该发货单对应的销售单未找到');
  4499. //判断发货单是否重复
  4500. if (isset($temp_out_codes[$value['outCode']])) {
  4501. Db::rollback();
  4502. return app_show(1005, '下列发货申请单号重复', [$value]);
  4503. } else $temp_out_codes[$value['outCode']] = $value['outCode'];
  4504. if ($sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['is_stock'] == 1) {
  4505. if (!in_array($own_roleid, config('app.wsm_cgder_role'))) {
  4506. Db::rollback();
  4507. return app_show(1003, $value['outCode'] . '发货单只能由库管操作');
  4508. }
  4509. } else {
  4510. if($sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['order_type']==3){
  4511. $personid = Db::name('good_zixun')->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  4512. ->where(['a.spuCode' => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['good_code'], 'a.is_del' => 0])
  4513. ->value('personid');
  4514. }else{
  4515. $personid = Db::name('good')->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  4516. ->where(['a.spuCode' =>
  4517. $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['good_code'],'a.is_del' => 0])
  4518. ->value('personid');
  4519. }
  4520. if ($createrid != $personid) {
  4521. Db::rollback();
  4522. return app_show(1003, $value['outCode'] . '发货单只能由供应商负责人操作');
  4523. }
  4524. }
  4525. //【发货单】
  4526. //查询已有发货单信息及状态
  4527. //更新发货单
  4528. $up = Db::name("order_out")
  4529. ->where(['id' => $order_out_infos[$value['outCode']]['id'], 'outCode' => $value['outCode'], 'status' => 1, "is_del" => 0])
  4530. ->update([
  4531. 'post_name' => isset($value['post_name']) ? $value['post_name'] : '',
  4532. 'post_code' => $value['post_code'],
  4533. 'post_fee' => $value['post_fee'],
  4534. 'sendtime' => date('Y-m-d H:i:s'),
  4535. 'status' => 2,
  4536. 'updatetime' => date('Y-m-d H:i:s'),
  4537. 'remark' => isset($value['remark']) ? $value['remark'] : '',
  4538. ]);
  4539. if ($up) {
  4540. //修改状态,添加待办
  4541. ActionLog::logAdd(['id' => $createrid, 'nickname' => $creater], [
  4542. "order_code" => $value['outCode'],//出库单号
  4543. "status" => $order_out_infos[$value['outCode']]['status'],//这里的status是之前的值
  4544. "action_remark" => '',//备注
  4545. "action_type" => "edit"//新建create,编辑edit,更改状态status
  4546. ], "CKD", 2, $order_out_infos[$value['outCode']]);
  4547. ProcessOrder::AddProcess(['id' => $createrid, 'nickname' => $creater], [
  4548. "order_type" => 'CKD',
  4549. "order_code" => $value['outCode'],//出库单号
  4550. "order_id" => $order_out_infos[$value['outCode']]['id'],
  4551. "order_status" => 2,
  4552. "before_status" => $order_out_infos[$value['outCode']]['status'],
  4553. 'wait_id' => $order_out_infos[$value['outCode']]['apply_id'],
  4554. 'wait_name' => $order_out_infos[$value['outCode']]['apply_name'],
  4555. 'holder_id' => $order_out_infos[$value['outCode']]['apply_id'],
  4556. ]);
  4557. //【销售单】
  4558. $einfo = [];
  4559. $einfo['send_num'] = $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['send_num'] + $order_out_infos[$value['outCode']]['send_num'];
  4560. $einfo['wsend_num'] = $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['wsend_num'] - $order_out_infos[$value['outCode']]['send_num'];
  4561. $einfo['status'] = $einfo['wsend_num'] == 0 ? 2 : ($einfo['send_num'] == 0 ? 0 : 1);
  4562. $einfo['send_status'] = $einfo['wsend_num'] == 0 ? 3 : ($einfo['send_num'] == 0 ? 1 : 2);
  4563. $einfo['updatetime'] = date("Y-m-d H:i:s");
  4564. $saleup = Db::name("sale")
  4565. ->where('id', $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['id'])
  4566. ->update($einfo);
  4567. if ($saleup == false) throw new Exception($value['outCode'] . '销售单状态更新失败');
  4568. //修改状态,添加待办
  4569. ActionLog::logAdd(['id' => $createrid, 'nickname' => $creater], [
  4570. "order_code" => $order_out_infos[$value['outCode']]['orderCode'],//销售单号
  4571. "status" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['status'],//这里的status是之前的值
  4572. "action_remark" => '',//备注
  4573. "action_type" => "edit"//新建create,编辑edit,更改状态status
  4574. ], "XSQRD", $einfo['status'], $einfo);
  4575. ProcessOrder::AddProcess(['id' => $createrid, 'nickname' => $creater], [
  4576. "order_type" => 'XSQRD',
  4577. "order_code" => $order_out_infos[$value['outCode']]['orderCode'],//出库单号
  4578. "order_id" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['id'],
  4579. "order_status" => $einfo['status'],
  4580. "before_status" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['status'],
  4581. 'holder_id' => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['apply_id']
  4582. ]);
  4583. if($sale_infos[$order_out_infos[$value['outCode']]['orderCode']]["order_type"]!=1){
  4584. //【库存】
  4585. $stock = Db::name("good_stock")
  4586. ->field('id,wait_out_stock,usable_stock,total_stock,updatetime')
  4587. ->where([
  4588. "is_del" => 0,
  4589. 'spuCode' => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['good_code'],
  4590. "wsm_code" => $order_out_infos[$value['outCode']]['wsm_code'],
  4591. ])->find();
  4592. if ($stock == false) throw new Exception($value['outCode'] . '未找到库存数据');
  4593. else {
  4594. if ($order_out_infos[$value['outCode']]['send_num'] > $stock['wait_out_stock']) throw new Exception($value['outCode'] . '超出库存数量');
  4595. $stock['wait_out_stock'] -= $order_out_infos[$value['outCode']]['send_num'];
  4596. $stock['total_stock'] = $stock['wait_out_stock'] + $stock['usable_stock'];
  4597. $stock['updatetime'] = date("Y-m-d H:i:s");
  4598. $stock_rs = Db::name("good_stock")
  4599. ->where('id', $stock['id'])
  4600. ->update($stock);
  4601. if ($stock_rs == false) throw new Exception($value['outCode'] . '库存更新失败');
  4602. $outsend =Db::name("order_send")->where(["outCode"=> $value['outCode']])->findOrEmpty();
  4603. if(empty($outsend)){
  4604. Db::rollback();
  4605. return error_show(1002, "未找到关联采购单");
  4606. }
  4607. $cgdinfo= Db::name("purchease_order")->where(["cgdNo"=>$outsend['cgdNo'],"is_del"=>0])
  4608. ->findOrEmpty();
  4609. $bn_code =makeNo("BN");
  4610. $bnin=GoodStockInfo::AddBn($stock["id"],$bn_code,$order_out_infos[$value['outCode']]['send_num'],$cgdinfo['good_price']??0);
  4611. if($bnin==false){
  4612. Db::rollback();
  4613. return error_show(1004,"库存bn数据新增失败");
  4614. }
  4615. $bnup =GoodStockInfo::OrderBn($order_out_infos[$value['outCode']]['orderCode'],$stock["id"],$order_out_infos[$value['outCode']]['send_num'],0);
  4616. if($bnup==false){
  4617. Db::rollback();
  4618. return error_show(1004,"库存bn库存数更新失败");
  4619. }
  4620. $outsend['bnCode']=$bn_code;
  4621. $outsend['updatetime']=date("Y-m-d H:i:s");
  4622. $ps=Db::name("order_send")->save($outsend);
  4623. if($ps==false){
  4624. Db::rollback();
  4625. return error_show(1004,"库存bn库存数更新失败");
  4626. }
  4627. }
  4628. $good_data[] = [
  4629. 'good_log_code' => $value['outCode'],
  4630. 'stock_id' => $stock['id'],
  4631. 'type' => 2,
  4632. 'stock' => $order_out_infos[$value['outCode']]['send_num'],
  4633. 'stock_name' => 'wait_out_stock'
  4634. ];
  4635. }else{
  4636. // $bnup =SaleInfo::SendStock($order_out_infos[$value['outCode']]['orderCode'],$order_out_infos[$value['outCode']]['send_num']);
  4637. // if($bnup==false){
  4638. // Db::rollback();
  4639. // return error_show(1004,"库存bn库存数更新失败");
  4640. // }
  4641. $saleinfo =Db::name("sale_info")->where(["orderCode"=>$order_out_infos[$value['outCode']]['orderCode']])->select()->toArray();
  4642. if (!empty($saleinfo)){
  4643. $bnup =SaleInfo::SendStock($order_out_infos[$value['outCode']]['orderCode'],$order_out_infos[$value['outCode']]['send_num']);
  4644. if($bnup==false){
  4645. Db::rollback();
  4646. return error_show(1004,"库存bn库存数更新失败");
  4647. }
  4648. }else{
  4649. $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();
  4650. if ($stokc == false) {
  4651. Db::rollback();
  4652. return error_show(1002, "未找到库存数据");
  4653. } else {
  4654. if ($order_out_infos[$value['outCode']]['send_num'] > $stokc['wait_out_stock']) {
  4655. Db::rollback();
  4656. return error_show(1002, "超出库存数量");
  4657. }
  4658. $stokc['wait_out_stock'] -= $order_out_infos[$value['outCode']]['send_num'];
  4659. $stokc['total_stock'] = $stokc['wait_out_stock'] + $stokc['usable_stock'];
  4660. // $stokc['intra_stock']+=$outinfo['send_num'];
  4661. $stokc['updatetime'] = date("Y-m-d H:i:s");
  4662. }
  4663. $stoc = Db::name("good_stock")->save($stokc);
  4664. if ($stoc == false) {
  4665. Db::rollback();
  4666. return error_show(1002, "库存更新失败");
  4667. }
  4668. }
  4669. }
  4670. } else throw new Exception($value['outCode'] . '发货失败');
  4671. Cache::store("redis")->handler()->lPush("SENDOUT",$value['outCode']);
  4672. //如果是有赞订单的话,将发货信息推到有赞
  4673. if($sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['order_source']==5){
  4674. $yz_data[]=[
  4675. 'orderCode'=>$sale_infos[$order_out_infos[$value['outCode']]['orderCode']],
  4676. 'out_stype'=>isset($value['post_name']) ? $value['post_name'] : '',
  4677. 'post_code'=>$value['post_code'],
  4678. 'uid'=>$createrid,
  4679. 'uname'=>$creater,
  4680. 'order_out'=>$value['outCode'],
  4681. ];
  4682. }
  4683. }
  4684. if (!empty($good_data)) GoodLog::LogAdd(['id' => $createrid, 'nickname' => $creater], $good_data, "CKD");
  4685. Db::commit();
  4686. //如果是有赞订单的话,将发货信息推到有赞
  4687. //有赞信息有可能推送失败(比如超过72小时,不允许多次修改等),所以不应该和这里的事务放到一起
  4688. if ($yz_data) {
  4689. foreach ($yz_data as $yz) {
  4690. // $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]);
  4691. curl_request(config('app.yz_domain') . 'api/yz_out_send', $yz);
  4692. }
  4693. // $res = json_decode($res, true);
  4694. // if ($res['code'] != 0) return app_show(0, '发货成功,' . $res['message']);
  4695. }
  4696. return app_show(0, '发货成功');
  4697. } catch (Exception $exception) {
  4698. Db::rollback();
  4699. return error_show(1004, $exception->getMessage());
  4700. }
  4701. }
  4702. //获取物流信息
  4703. public function getLogisticsInformation()
  4704. {
  4705. $post_code = $this->request->post('post_code', '', 'trim');
  4706. if (empty($post_code)) return error_show(1004, '快递单号不能为空');
  4707. $postCode = explode(",", $post_code);
  4708. $rs = Db::name('express_data')
  4709. ->where('post_code', $postCode[0])
  4710. ->where('status', ">=", 0)
  4711. ->withAttr('post_data', function ($val) {
  4712. return json_decode($val, true);
  4713. })->findOrEmpty();
  4714. settype($rs, 'object');//格式统一转为对象
  4715. return app_show(0, '请求成功', $rs);
  4716. }
  4717. }