Sale.php 156 KB

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