Sale.php 233 KB

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