Sale.php 218 KB

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