Sale.php 224 KB

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