Sale.php 223 KB

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