Sale.php 215 KB

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