Sale.php 177 KB

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