Sale.php 246 KB

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