Sale.php 156 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241
  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. //修改状态,添加待办
  994. $process = [
  995. "order_code" => $project['projectNo'],//把项目编码放到这儿
  996. //"order_id"=>0,//没有订单id,默认0
  997. "order_status" => 2,//2表示'流程中'
  998. "order_type" => 'PRO'//PRO表示'项目'
  999. ];
  1000. ProcessOrder::AddProcess($this->post['token'], $process);
  1001. Db::commit();
  1002. return app_show(0, "咨询订单生成成功");
  1003. }catch (\Exception $e){
  1004. Db::rollback();
  1005. return error_show(1002, $e->getMessage());
  1006. }
  1007. }
  1008. /**
  1009. * @param $data
  1010. * @return false
  1011. * @throws \think\db\exception\DataNotFoundException
  1012. * @throws \think\db\exception\DbException
  1013. * @throws \think\db\exception\ModelNotFoundException
  1014. *
  1015. */
  1016. public function project_zx($data){
  1017. $goodinfo = Db::name("consult_bids")->where(["spuCode"=>$data['spuCode'],"is_del"=>0])->find();
  1018. if($goodinfo==false){
  1019. return false;
  1020. }
  1021. $orderCode=makeNo("QR");
  1022. $cgd=[
  1023. "supplierNo"=>$goodinfo['supplierNo'],
  1024. "companyNo"=>$data['companyNo'],
  1025. "spuCode"=>$data['spuCode'],
  1026. "skuCode"=>"",
  1027. "orderCode"=>$orderCode,
  1028. "good_name"=>$goodinfo['good_name'],
  1029. "sale_price"=>$data['origin_price'],
  1030. "total_fee"=>$data['origin_price']*$data['good_num'],
  1031. "pakge_fee"=>$goodinfo['pakge_fee'],
  1032. "cert_fee"=>$goodinfo['cert_fee'],
  1033. "open_fee"=>$goodinfo['open_fee'],
  1034. "cost_fee"=>$goodinfo['cost_fee'],
  1035. "mark_fee"=>$goodinfo['mark_fee'],
  1036. "demo_fee"=>$goodinfo['demo_fee'],
  1037. "good_num"=>$data['good_num'],
  1038. "createrid"=>$goodinfo['createrid'],
  1039. "creater"=>$goodinfo['creater'],
  1040. "good_type"=>1,
  1041. "order_type"=>4,
  1042. "send_way"=>$goodinfo['send_way'],
  1043. ];
  1044. $datas=[
  1045. "orderCode"=>$orderCode,
  1046. "good_code"=>$data['spuCode'],
  1047. "skuCode"=>"",
  1048. "customer_code"=>$data['customerNo'],
  1049. "good_name"=>isset($goodinfo['good_name'])&&$goodinfo['good_name']!==''?$goodinfo['good_name']:'',
  1050. "good_num"=>$data['good_num'],
  1051. "cat_id"=>$goodinfo['cat_id'],
  1052. "apply_id"=>$data['rm'],
  1053. "apply_name"=>$data['ri'],
  1054. "origin_price"=>$goodinfo['nake_fee'],
  1055. "sale_price"=>$data['sale_price'],
  1056. "post_fee"=>0,
  1057. "status"=>0,
  1058. "send_num"=>0,
  1059. "wsend_num"=>$data['good_num'],
  1060. "send_status"=>1,
  1061. "good_type"=>1,
  1062. "send_type"=>$data['sendtype'],
  1063. "supplierNo"=>$data['companyNo'],
  1064. "is_del"=>0,
  1065. "zxNo"=>$data["pgNo"],
  1066. "platform_order"=>"",
  1067. "platform_id"=>0,
  1068. "remark"=>"",
  1069. "is_stock"=>0,
  1070. "is_activity"=>0,
  1071. "proof_id"=>0,
  1072. "order_type"=>4,
  1073. "addtime"=>date("Y-m-d H:i:s"),
  1074. "updatetime"=>date("Y-m-d H:i:s"),
  1075. 'total_price'=>round($data['sale_price']*$data['good_num'],2),
  1076. ];
  1077. $datainfo = Db::name('sale')->insert($datas, true);
  1078. if($datainfo>0){
  1079. $bol = $this->createCgd($cgd);
  1080. if ($bol == false) {
  1081. return false;
  1082. }
  1083. $goodinfo['status']=6;
  1084. $goodinfo['updatetime']=date("Y-m-d H:i:s");
  1085. $sa=Db::name("consult_bids")->save($goodinfo);
  1086. if($sa==false){
  1087. return false;
  1088. }
  1089. $zxorder =Db::name("consult_order")->where(["zxNo"=>$goodinfo["zxNo"],"is_del"=>0])->find();
  1090. if($zxorder==false){
  1091. return false;
  1092. }
  1093. $limt=[
  1094. "spuCode"=>$goodinfo['spuCode'],
  1095. "good_name"=>$goodinfo['good_name'],
  1096. "brand_id"=>$goodinfo['brand_id'],
  1097. "good_unit"=>$goodinfo['unit_id'],
  1098. "cat_id"=>$goodinfo['cat_id'],
  1099. "good_type"=>0,
  1100. "moq"=>1,
  1101. "customized"=>$goodinfo['work_day'],
  1102. "tax"=>$goodinfo['tax'],
  1103. "supplierNo"=>$goodinfo["supplierNo"],
  1104. "is_auth"=>0,
  1105. "craft_desc"=>$goodinfo['good_name'],
  1106. "good_remark"=>"",
  1107. "good_img"=>$goodinfo['good_img'],
  1108. "good_thumb_img"=>"",
  1109. "good_info_img"=>"",
  1110. "platform_id"=>$zxorder['platform_id'],
  1111. "specinfo"=>$goodinfo['specinfo'],
  1112. "work_day"=>$goodinfo['work_day'],
  1113. "noble_metal"=>$goodinfo['metal_id'],
  1114. "is_gold_price"=>$goodinfo['is_gold_price'],
  1115. "config"=>$goodinfo['config'],
  1116. "other_config"=>$goodinfo['other_config'],
  1117. "weight"=>$goodinfo['weight'],
  1118. "is_diff"=>$goodinfo['is_diff'],
  1119. "supply_area"=>$goodinfo['supply_area'],
  1120. "pay_way"=>$goodinfo['pay_way'],
  1121. "send_way"=>$goodinfo['send_way'],
  1122. "status"=>1,
  1123. "is_del"=>0,
  1124. "createrid"=>$goodinfo['createrid'],
  1125. "creater"=>$goodinfo['creater'],
  1126. "addtime"=>date("Y-m-d H:i:s"),
  1127. "updatetime"=>date("Y-m-d H:i:s")
  1128. ];
  1129. $good = Db::name("good_zixun")->insert($limt);
  1130. if(!$good){
  1131. return false;
  1132. }
  1133. if ($data['sendtype']==1&&!empty($data['addrlist'])) {
  1134. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  1135. ->find();
  1136. if ($order == false) {
  1137. return false;
  1138. }
  1139. foreach ($data['addrlist'] as $value) {
  1140. $temp = [];
  1141. $addrs = [];
  1142. if ($value['addr_code'] !== '' && is_array($value['addr_code'])) {
  1143. $addrs['provice_code'] = $value['addr_code'][0];
  1144. $addrs['city_code'] = $value['addr_code'][1];
  1145. $addrs['area_code'] = $value['addr_code'][2];
  1146. $addr = json_encode($addrs);
  1147. } else {
  1148. $addr = isset($value['addr_code']) ? $value['addr_code'] : '';
  1149. }
  1150. $temp['orderCode'] = $orderCode;
  1151. $temp['contactor'] = $value['contactor'];
  1152. $temp['mobile'] = $value['mobile'];
  1153. $temp['addr'] = $value['addr'];
  1154. $temp['addr_code'] = $addr;
  1155. $temp['customer_code'] = $data['companyNo'];
  1156. $temp['receipt_quantity'] = $value['receipt_quantity'];
  1157. $temp['post_fee'] = 0;
  1158. $temp['is_del'] = 0;
  1159. $temp['addtime'] = date("Y-m-d H:i:s");
  1160. $temp['updatetime'] = date("Y-m-d H:i:s");
  1161. $temp['arrive_time'] = $data['arrtime'];
  1162. $vmp = Db::name('order_addr')->insert($temp, true);
  1163. if ($vmp > 0) {
  1164. // $outCode = makeNo("CK");
  1165. // $out = [
  1166. // "orderCode" => $orderCode,
  1167. // "outCode" => $outCode,
  1168. // "apply_id"=>$data['rm'],
  1169. // "apply_name"=>$data['ri'],
  1170. // "addrid" => $vmp,
  1171. // "post_name" => "",
  1172. // "post_code" => "",
  1173. // "post_fee" => 0,
  1174. // "sendtime" => date("Y-m-d H:i:s"),
  1175. // "send_num" => $value['receipt_quantity'],
  1176. // "check_num" => 0,
  1177. // "error_num" => 0,
  1178. // "wsm_code" => "",
  1179. // "order_type" => 4,
  1180. // "status" => 0,
  1181. // "addtime" => date("Y-m-d H:i:s"),
  1182. // "updatetime" => date("Y-m-d H:i:s")
  1183. // ];
  1184. // $ou = Db::name("order_out")->insert($out);
  1185. // if ($ou == false) {
  1186. // return false;
  1187. // }
  1188. // $order['send_num'] += $value['receipt_quantity'];
  1189. // $order['wsend_num'] -= $value['receipt_quantity'];
  1190. // if ($order['wsend_num'] < 0) {
  1191. // return false;
  1192. // }
  1193. // $ups = Db::name("order_num")->save($order);
  1194. // if ($ups) {
  1195. // $tep = [
  1196. // "cgdNo" => $order['cgdNo'],
  1197. // "outCode" => $outCode,
  1198. // "send_num" => $value['receipt_quantity'],
  1199. // "status" => 1,
  1200. // "addtime" => date("Y-m-d H:i:s"),
  1201. // "updatetime" => date("Y-m-d H:i:s")
  1202. // ];
  1203. // $sen = Db::name("order_send")->save($tep);
  1204. // if ($sen == false) {
  1205. // return false;
  1206. // }
  1207. // }
  1208. } else {
  1209. return false;
  1210. }
  1211. }
  1212. }
  1213. }
  1214. return true;
  1215. }
  1216. /**
  1217. * @param $data
  1218. * @return bool
  1219. * @throws \think\db\exception\DataNotFoundException
  1220. * @throws \think\db\exception\DbException
  1221. * @throws \think\db\exception\ModelNotFoundException
  1222. */
  1223. public function project_good($data){
  1224. $good=Db::name('good')->alias("a")->leftJoin("good_platform b","a.spuCode=b.spuCode")
  1225. ->where(["b.skuCode"=>$data['skuCode'],"a.is_del"=>0,"b.is_del"=>0])->field("b.*,a.skuCode,a.platform_code,a.plat_code")->find();
  1226. if($good==false){
  1227. return false;
  1228. }
  1229. $goodlass = Db::name("good_ladder")->where(["skuCode"=>$data['skuCode'],"is_del"=>0,"status"=>1])->where([["min_num",
  1230. "<=",$data['good_num']]])->order("min_num desc")->find();
  1231. if($goodlass==false){
  1232. return false;
  1233. }
  1234. $sale_price = $goodlass['sale_price'];
  1235. $origin = Db::name("good_nake")->where([["spuCode","=",$data['spuCode']],["min_num","<=",$data['good_num']],
  1236. ["is_del","=",0]])->order("min_num desc")->find();
  1237. if($origin==false){
  1238. return false;
  1239. }
  1240. $origin_price = $origin['nake_total'];
  1241. $orderCode=makeNo("QR");
  1242. $datas=[
  1243. "orderCode"=>$orderCode,
  1244. "good_code"=>$data['spuCode'],
  1245. "skuCode"=>$data['skuCode'],
  1246. "customer_code"=>$data['customerNo'],
  1247. "good_name"=>isset($good['good_name'])&&$good['good_name']!==''?$good['good_name']:'',
  1248. "good_num"=>$data['good_num'],
  1249. "cat_id"=>$good['cat_id'],
  1250. "apply_id"=>$data['rm'],
  1251. "apply_name"=>$data['ri'],
  1252. "origin_price"=>$origin_price,
  1253. "sale_price"=>$sale_price,
  1254. "post_fee"=>0,
  1255. "status"=>0,
  1256. "send_num"=>0,
  1257. "wsend_num"=>$data['good_num'],
  1258. "send_status"=>1,
  1259. "good_type"=>1,
  1260. "send_type"=>$data['sendtype'],
  1261. "supplierNo"=>$data['companyNo'],
  1262. "is_del"=>0,
  1263. "zxNo"=>$data["pgNo"],
  1264. "platform_order"=>"",
  1265. "platform_id"=>0,
  1266. "remark"=>"",
  1267. "is_stock"=>$good['is_stock'],
  1268. "is_activity"=>0,
  1269. "proof_id"=>0,
  1270. "order_type"=>2,
  1271. "addtime"=>date("Y-m-d H:i:s"),
  1272. "updatetime"=>date("Y-m-d H:i:s"),
  1273. 'total_price'=>round($sale_price*$data['good_num'],2),
  1274. ];
  1275. $datainfo = Db::name('sale')->insert($datas, true);
  1276. if($datainfo>0) {
  1277. $cgd=[
  1278. "supplierNo"=>$good['supplierNo'],
  1279. "companyNo"=>$data['companyNo'],
  1280. "orderCode"=>$orderCode,
  1281. "spuCode"=>$good['spuCode'],
  1282. "skuCode"=>$good['skuCode'],
  1283. "good_name"=>$good['good_name'],
  1284. "sale_price"=>$origin_price,
  1285. "total_fee"=>$origin_price*$data['good_num'],
  1286. "pakge_fee"=>$origin['package_fee'],
  1287. "cert_fee"=>$origin['cert_fee'],
  1288. "open_fee"=>$good['open_fee'],
  1289. "cost_fee"=>$origin['cost_fee'],
  1290. "mark_fee"=>$origin['mark_fee'],
  1291. "demo_fee"=>$good['demo_fee'],
  1292. "good_num"=>$data['good_num'],
  1293. "good_type"=>1,
  1294. "createrid"=>$good['createrid'],
  1295. "creater"=>$good['creater'],
  1296. "order_type"=>$good['is_stock']==1?1:2,
  1297. 'send_way'=>2
  1298. ];
  1299. if($good['is_stock']==0) {
  1300. $bol = $this->createCgd($cgd);
  1301. if ($bol == false) {
  1302. return false;
  1303. }
  1304. }else{
  1305. $dd=[
  1306. "orderCode"=>$orderCode,
  1307. "spuCode"=>$good['spuCode'],
  1308. "total_num"=>$data['good_num'],
  1309. "merge_num"=>0,
  1310. "balance_num"=>$data['good_num'],
  1311. "status"=>1,
  1312. "is_del"=>0,
  1313. "addtime"=>date("Y-m-d H:i:s"),
  1314. "updatetime"=>date("Y-m-d H:i:s"),
  1315. ];
  1316. $db=Db::name("sale_bk")->insert($dd);
  1317. if($db==false){
  1318. return false;
  1319. }
  1320. }
  1321. if ($data['sendtype']==1&&!empty($data['addrlist'])) {
  1322. // $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  1323. // ->find();
  1324. // if ($order == false) {
  1325. // return false;
  1326. // }
  1327. foreach ($data['addrlist'] as $value) {
  1328. $temp = [];
  1329. $addrs = [];
  1330. if ($value['addr_code'] !== '' && is_array($value['addr_code'])) {
  1331. $addrs['provice_code'] = $value['addr_code'][0];
  1332. $addrs['city_code'] = $value['addr_code'][1];
  1333. $addrs['area_code'] = $value['addr_code'][2];
  1334. $addr = json_encode($addrs);
  1335. } else {
  1336. $addr = isset($value['addr_code']) ? $value['addr_code'] : '';
  1337. }
  1338. $temp['orderCode'] = $orderCode;
  1339. $temp['contactor'] = $value['contactor'];
  1340. $temp['mobile'] = $value['mobile'];
  1341. $temp['addr'] = $value['addr'];
  1342. $temp['addr_code'] = $addr;
  1343. $temp['customer_code'] =$data['companyNo'];
  1344. $temp['receipt_quantity'] = $value['receipt_quantity'];
  1345. $temp['post_fee'] = 0;
  1346. $temp['is_del'] = 0;
  1347. $temp['addtime'] = date("Y-m-d H:i:s");
  1348. $temp['updatetime'] = date("Y-m-d H:i:s");
  1349. $temp['arrive_time'] = $data['arrtime'];
  1350. $vmp = Db::name('order_addr')->insert($temp, true);
  1351. if ($vmp > 0) {
  1352. // if($good['is_stock']!=1){
  1353. // $outCode = makeNo("CK");
  1354. // $out = [
  1355. // "orderCode" => $orderCode,
  1356. // "outCode" => $outCode,
  1357. // "apply_id"=>$data['rm'],
  1358. // "apply_name"=>$data['ri'],
  1359. // "addrid" => $vmp,
  1360. // "post_name" => "",
  1361. // "post_code" => "",
  1362. // "post_fee" => 0,
  1363. // "sendtime" => date("Y-m-d H:i:s"),
  1364. // "send_num" => $value['receipt_quantity'],
  1365. // "check_num" => 0,
  1366. // "error_num" => 0,
  1367. // "wsm_code" => "",
  1368. // "order_type" => 4,
  1369. // "status" => 0,
  1370. // "addtime" => date("Y-m-d H:i:s"),
  1371. // "updatetime" => date("Y-m-d H:i:s")
  1372. // ];
  1373. // $ou = Db::name("order_out")->insert($out);
  1374. // if ($ou == false) {
  1375. // return false;
  1376. // }
  1377. // $order['send_num'] += $value['receipt_quantity'];
  1378. // $order['wsend_num'] -= $value['receipt_quantity'];
  1379. // if ($order['wsend_num'] < 0) {
  1380. // return false;
  1381. // }
  1382. // $ups = Db::name("order_num")->save($order);
  1383. // if ($ups) {
  1384. // $tep = [
  1385. // "cgdNo" => $order['cgdNo'],
  1386. // "outCode" => $outCode,
  1387. // "send_num" => $value['receipt_quantity'],
  1388. // "status" => 1,
  1389. // "addtime" => date("Y-m-d H:i:s"),
  1390. // "updatetime" => date("Y-m-d H:i:s")
  1391. // ];
  1392. // $sen = Db::name("order_send")->insert($tep);
  1393. // if ($sen == false) {
  1394. // return false;
  1395. // }
  1396. // }
  1397. // }
  1398. } else {
  1399. return false;
  1400. }
  1401. }
  1402. }
  1403. }
  1404. return true;
  1405. }
  1406. public function list()
  1407. {
  1408. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  1409. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  1410. $where[] = ['is_del', "=", 0];
  1411. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  1412. if ($orderCode !== "") {
  1413. $where[] = ['orderCode', "like", "%$orderCode%"];
  1414. }
  1415. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !== "" ? trim($this->post['apply_name'])
  1416. : "";
  1417. if ($apply_name !== "") {
  1418. $where[] = ['apply_name', "like", "%$apply_name%"];
  1419. }
  1420. $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim($this->post['good_name'])
  1421. : "";
  1422. if ($good_name !== "") {
  1423. $where[] = ['good_name', "like", "%$good_name%"];
  1424. }
  1425. $good_code = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : "";
  1426. if ($good_code !== "") {
  1427. $where[] = ['good_code', "like", "%$good_code%"];
  1428. }
  1429. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code'] !== "" ? trim($this->post['customer_code']) : "";
  1430. if ($customer_code !== "") {
  1431. $where[] = ['customer_code', "like", "%$customer_code%"];
  1432. }
  1433. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  1434. if ($supplierNo !== "") {
  1435. $where[] = ['supplierNo', "like", "%$supplierNo%"];
  1436. }
  1437. $ordertype = isset($this->post['order_type']) && $this->post['order_type'] !== "" ? intval($this->post['order_type'])
  1438. : "";
  1439. if ($ordertype !== "") {
  1440. $where[] = ['order_type', "=", $ordertype];
  1441. }
  1442. $status = isset($this->post['status']) && $this->post['status'] !== "" ? trim($this->post['status']) : "";
  1443. if ($status !== "") {
  1444. $where[] = ['status', "=", $status];
  1445. }
  1446. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  1447. if ($start != "") {
  1448. $where[] = ["addtime", '>=', $start];
  1449. }
  1450. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  1451. if ($end != "") {
  1452. $where[] = ["addtime", '<=', $end];
  1453. }
  1454. $count = Db::name('sale')->where($where)->count();
  1455. $total = ceil($count / $size);
  1456. $page = $page >= $total ? $total : $page;
  1457. $list = Db::name('sale')->where($where)->order("addtime desc")->page($page, $size)->select();
  1458. $data = [];
  1459. foreach ($list as $value) {
  1460. if($value['order_type']==3|| $value['order_type']==4){
  1461. $var = Db::name("good_zixun")->where(["spuCode"=>$value['good_code'],"is_del"=>0])->find();
  1462. $value["speclist"]=isset($var['specinfo'])&&$var['specinfo']!=""? json_decode($var['specinfo'],true):"";
  1463. }else {
  1464. $var = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  1465. ->where(['a.skuCode' => $value['skuCode']])->find();
  1466. $spec = Db::name("good_spec")->where(["spuCode"=>$var['spuCode'],"is_del"=>0])->select()->toArray();
  1467. $speclist=[];
  1468. if(!empty($spec)){
  1469. foreach ($spec as $val){
  1470. $temp=[];
  1471. $temp['id']=$val['id'];
  1472. $temp['spuCode']=$val['spuCode'];
  1473. $temp['spec_id']=$val['spec_id'];
  1474. $temp['spec_value_id']=$val['spec_value_id'];
  1475. $sp = Db::name("specs")->where(["id"=>$val['spec_id']])->find();
  1476. $temp['spec_name']=isset($sp["spec_name"]) ? $sp["spec_name"]:"";
  1477. $spv = Db::name("spec_value")->where(["id"=>$val['spec_value_id']])->find();
  1478. $temp['spec_value']=isset($spv["spec_value"]) ? $spv["spec_value"]:"";
  1479. $speclist[]=$temp;
  1480. }
  1481. }
  1482. $value["speclist"]=empty($speclist)?[]:$speclist;
  1483. }
  1484. $value['can'] = isset($var['cat_id']) && $var['cat_id'] != 0 ? made($var['cat_id']) : [];
  1485. $value['supplierName'] = '';
  1486. if ($value['supplierNo'] != "") {
  1487. $supplier = Db::name("business")->where(["companyNo" => $value['supplierNo']])->find();
  1488. $value['supplierName'] = isset($supplier['company']) ? $supplier['company'] : "";
  1489. }
  1490. $value['customerName'] = '';
  1491. if ($value['customer_code'] != "") {
  1492. $supplier = Db::name("customer_info")->where(["companyNo" => $value['customer_code']])->find();
  1493. $value['customerName'] = isset($supplier['companyName']) ? $supplier['companyName'] : "";
  1494. }
  1495. $value['useage']="";
  1496. if($value['use_order']!=0){
  1497. $use =Db::name("order_use")->where(["id"=>$value['use_order']])->find();
  1498. $value['useage'] = isset($use['order_use']) ?$use['order_use']:"";
  1499. }else{
  1500. $value['use_order']='';
  1501. }
  1502. $data[] = $value;
  1503. }
  1504. return app_show(0, "获取成功", ['list' => $data, 'count' => $count]);
  1505. }
  1506. public function edit()
  1507. {
  1508. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  1509. $id = isset($this->post['id']) && $this->post['id'] !== "" ? trim($this->post['id']) : "";
  1510. if ($id == "") {
  1511. return error_show(1002, "参数id不能为空");
  1512. }
  1513. $etid = Db::name("sale")->where(["id" => $id, "is_del" => 0])->find();
  1514. if (empty($etid)) {
  1515. return error_show(1002, "未找到数据");
  1516. }
  1517. // $orderCode=isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']) :"";
  1518. // if($orderCode==""){
  1519. // return error_show(1002,"销售单code不能为空");
  1520. // }
  1521. $good_code = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : "";
  1522. if ($good_code == "") {
  1523. return error_show(1002, "商品code不能为空");
  1524. }
  1525. $ct = Db::name('good')->alias('a')->join('good_type b', 'b.good_code=a.good_code', 'left')
  1526. ->where(['b.type_code' => $good_code])->find();
  1527. if ($ct == "") {
  1528. return error_show(1002, "未找到商品数据");
  1529. }
  1530. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code'] !== "" ? trim($this->post['customer_code']) : "";
  1531. if ($customer_code == "") {
  1532. return error_show(1002, "客户code不能为空");
  1533. }
  1534. $customer = Db::name("customer_info")->where(["companyNo" => $customer_code])->find();
  1535. if ($customer == false) {
  1536. return error_show(1004, "未找到客户数据");
  1537. }
  1538. $supplierNo = $this->post['supplierNo'] && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  1539. if ($supplierNo == "") {
  1540. return error_show(1002, "参数supplierNo不能为空");
  1541. }
  1542. $supplier = Db::name("supplier")->where(["code" => $supplierNo])->find();
  1543. if ($supplier == false) {
  1544. return error_show(1004, "未找到平台供应商数据");
  1545. }
  1546. $apply_id = GetUserInfo($token);
  1547. if (empty($apply_id) || $apply_id['code'] != 0) {
  1548. return error_show(1002, "申请人数据不存在");
  1549. }
  1550. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  1551. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  1552. // $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim
  1553. //($this->post['good_name']) : "";
  1554. // $good_num = isset($this->post['good_num']) && $this->post['good_num'] !== "" ? intval
  1555. //($this->post['good_num']) : "";
  1556. // $origin_price = isset($this->post['origin_price']) && $this->post['origin_price'] !== "" ? intval
  1557. // ($this->post['origin_price']) : "";
  1558. // $sale_price = isset($this->post['sale_price']) && $this->post['sale_price'] !== "" ? trim($this->post['sale_price']) : "";
  1559. // $total_price = isset($this->post['total_price']) && $this->post['total_price'] !== "" ? trim($this->post['total_price']) : "";
  1560. // $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !== "" ? trim($this->post['post_fee']) : "";
  1561. // $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  1562. $vr = isset($this->post['order_addr']) && $this->post['order_addr'] !== "" ? $this->post['order_addr'] : "";
  1563. if ($vr == "") {
  1564. return error_show(1002, "参数order_addr不能为空");
  1565. }
  1566. $vi = isset($this->post['good_stock']) && $this->post['good_stock'] !== "" ? $this->post['good_stock'] : "";
  1567. if ($vi == "") {
  1568. return error_show(1002, "参数good_stock不能为空");
  1569. }
  1570. $good_num = intval(array_sum(array_column($vi, "num")));
  1571. Db::startTrans();
  1572. try {
  1573. $datn = [
  1574. "id" => $id,
  1575. "good_code" => $good_code,
  1576. "customer_code" => $customer_code,
  1577. "good_num" => $good_num,
  1578. "supplierNo" => $supplierNo,
  1579. "apply_id" => $rm,
  1580. "apply_name" => $ri,
  1581. "is_del" => 0,
  1582. "updatetime" => date("Y-m-d H:i:s")
  1583. ];
  1584. $datninfo = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->save($datn);
  1585. if ($datninfo > 0) {
  1586. $stn = ["order_code"=>$etid['orderCode'],"status"=>$etid['status'],"action_remark"=>'',"action_type"=>"edit"];
  1587. ActionLog::logAdd($this->post['token'],$stn,"XSQRD",$etid['status'],$datn);
  1588. $order=["order_type"=>'XSQRD',"order_code"=>$etid['orderCode'],"order_id"=>$id,"order_status"=>$etid['status']];
  1589. ProcessOrder::AddProcess($this->post['token'],$order);
  1590. foreach ($vi as $ion) {
  1591. $iten = [];
  1592. isset($ion['id']) && $ion['id'] !== "" ? $iten['id'] = $ion['id'] : '';
  1593. $iten['orderCode'] = $etid['orderCode'];
  1594. $iten['wsm_code'] = $ion['wsm_code'];
  1595. $iten['stock_id'] = 0;
  1596. $iten['num'] = $ion['num'];
  1597. $iten['status'] = 1;
  1598. $iten['sale_price'] = $ct['original_price'];
  1599. isset($ion['id']) && $ion['id'] !== "" ? '' : $iten['addtime'] = date("Y-m-d H:i:s");
  1600. $iten['updatetime'] = date("Y-m-d H:i:s");
  1601. $vp = Db::name('sale_info')->save($iten);
  1602. if ($vp == false)
  1603. {
  1604. Db::rollback();
  1605. return error_show(1002, "更新失败");
  1606. }
  1607. }
  1608. foreach ($vr as $value) {
  1609. $temp = [];
  1610. isset($value['id']) && $value['id'] !== "" ? $temp['id'] = $value['id'] : '';
  1611. $temp['orderCode'] = $etid['orderCode'];
  1612. $temp['contactor'] = $value['contactor'];
  1613. $temp['mobile'] = $value['mobile'];
  1614. $temp['addr'] = $value['addr'];
  1615. $temp['addr_code'] = $value['addr_code'];
  1616. $temp['customer_code'] = $customer_code;
  1617. $temp['receipt_quantity'] = $value['receipt_quantity'];
  1618. $temp['post_fee'] = 0;
  1619. $temp['is_del'] = $value['is_del'];
  1620. isset($value['id']) && $value['id'] !== "" ? '' : $temp['addtime'] = date("Y-m-d H:i:s");
  1621. $temp['updatetime'] = date("Y-m-d H:i:s");
  1622. $temp['arrive_time'] = date("Y-m-d H:i:s");
  1623. $dat = Db::name('order_addr')->save($temp);
  1624. if ($dat == false) {
  1625. Db::rollback();
  1626. return error_show(1002, "更新失败");
  1627. }
  1628. }
  1629. Db::commit();
  1630. return error_show(0, "更新成功");
  1631. }
  1632. Db::rollback();
  1633. return error_show(1002, "更新失败");
  1634. } catch (Exception $e) {
  1635. Db::rollback();
  1636. return error_show(1005, $e->getMessage());
  1637. }
  1638. }
  1639. public function info()
  1640. {
  1641. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  1642. if ($id == "") {
  1643. return error_show(1002, "参数id不能为空");
  1644. }
  1645. $einfo = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->find();
  1646. if (empty($einfo)) {
  1647. return error_show(1002, "未找到销售订单数据");
  1648. }
  1649. if($einfo['order_type']==3|| $einfo['order_type']==4){
  1650. $goon = Db::name("good_zixun")->where(["spuCode"=>$einfo['good_code'],"is_del"=>0])->find();
  1651. }else {
  1652. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  1653. ->where(['a.skuCode' => $einfo['skuCode']])->find();
  1654. }
  1655. if ($goon==false) {
  1656. return error_show(1003, "未找到商品数据");
  1657. }else{
  1658. $goon['exclusive']=isset($goon['is_exclusive'])?makeExcluse($goon['is_exclusive']):"";
  1659. $unit =Db::name("unit")->where(["id"=>$goon['good_unit']])->find();
  1660. $goon['unit'] = isset($unit['unit'])?$unit['unit']:'';
  1661. $spec = Db::name("good_spec")->where(["spuCode"=>$goon['spuCode'],"is_del"=>0])->select()->toArray();
  1662. $speclist=[];
  1663. if(!empty($spec)){
  1664. foreach ($spec as $value){
  1665. $temp=[];
  1666. $temp['id']=$value['id'];
  1667. $temp['spuCode']=$value['spuCode'];
  1668. $temp['spec_id']=$value['spec_id'];
  1669. $temp['spec_value_id']=$value['spec_value_id'];
  1670. $temp['is_del']=$value['is_del'];
  1671. $sp = Db::name("specs")->where(["id"=>$value['spec_id']])->find();
  1672. $temp['spec_name']=isset($sp["spec_name"]) ? $sp["spec_name"]:"";
  1673. $spv = Db::name("spec_value")->where(["id"=>$value['spec_value_id']])->find();
  1674. $temp['spec_value']=isset($spv["spec_value"]) ? $spv["spec_value"]:"";
  1675. $speclist[]=$temp;
  1676. }
  1677. }
  1678. $goon["speclist"]=empty($speclist)?[]:$speclist;
  1679. $proof =Db::name("good_proof")->where(["spuCode"=>$goon['spuCode'],"is_del"=>0])->order("updatetime desc")->field("id,proof_type,proof_url")->find();
  1680. $goon['proof'] = isset($proof)&&$proof!=false? $proof:[];
  1681. $goon['origin_place_cn']="";
  1682. $goon['delivery_place_cn']="";
  1683. if(isset($goon['delivery_place'])&&$goon['delivery_place']!==""){
  1684. $place = ["provice_code"=>"","city_code"=>"","area_code"=>""];
  1685. list($place['provice_code'],$place['city_code'],$place['area_code'])=explode(",",$goon['delivery_place']);
  1686. $goon['delivery_place_cn']=GetAddr(json_encode($place));
  1687. }
  1688. if(isset($goon['delivery_place'])&&$goon['origin_place']!==""){
  1689. $place = ["provice_code"=>"","city_code"=>"","area_code"=>""];
  1690. list($place['provice_code'],$place['city_code'],$place['area_code'])=explode(",",$goon['origin_place']);
  1691. $goon['origin_place_cn']=GetAddr(json_encode($place));
  1692. }
  1693. if($goon['brand_id']!=0){
  1694. $brand=Db::name("brand")->where(["id"=>$goon['brand_id']])->find();
  1695. $goon["brand_name"]=isset($brand['brand_name'])?$brand['brand_name']:"";
  1696. }else{
  1697. $goon["brand_name"]="";
  1698. $goon["brand_id"]="";
  1699. }
  1700. $supplier = Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
  1701. $goon['supplierName'] = isset($supplier['name'])?$supplier['name']:"";
  1702. $goon['noble_name']=isset($goon['noble_metal'])&&$goon['noble_metal']!=0?$this->noble[$goon['noble_metal']] :"";
  1703. if(isset($goon['companyNo'])&&$goon['companyNo']!=""){
  1704. $company = Db::name("business")->where(["companyNo"=>$goon['companyNo']])->find();
  1705. }
  1706. $goon['company'] = isset($company['company'])?$company['company']:"";
  1707. }
  1708. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  1709. $in = Db::name('customer_info')->where(['companyNo' => $einfo['customer_code']])->field('companyName')->find();
  1710. $einfo['supplierName'] = '';
  1711. if ($einfo['supplierNo'] != "") {
  1712. $supplier = Db::name("business")->where(["companyNo" => $einfo['supplierNo']])->find();
  1713. $einfo['supplierName'] = isset($supplier['company']) ? $supplier['company'] : "";
  1714. }
  1715. $fo = Db::name('order_addr')->alias("a")->leftJoin("order_out b","a.id=b.addrid")
  1716. ->where(['a.orderCode' => $einfo['orderCode'], 'a.is_del' => 0])->field("a.*,b.post_name,b.post_code,
  1717. 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")
  1718. ->select();
  1719. $addrs = [];
  1720. if (!empty($fo)) {
  1721. foreach ($fo as $value) {
  1722. $value['addr_info'] = "";
  1723. $value['send_num'] = 0;
  1724. if ($value['addr_code'] != "") {
  1725. $place = ["provice_code"=>"","city_code"=>"","area_code"=>""];
  1726. list($place['provice_code'],$place['city_code'],$place['area_code'])=explode(",",$value['addr_code']);
  1727. $addr = GetAddr(json_encode($place));
  1728. $value['addr_info'] = $addr;
  1729. }
  1730. $value['wsm_name']="";
  1731. if(isset($value['wsm_code'])&&$value['wsm_code']!=""){
  1732. $wsmcode = Db::name("warehouse_info")->where(["wsm_code" => $value['wsm_code']])->find();
  1733. $value['wsm_name'] =isset($wsmcode['name'])?$wsmcode['name']:"";
  1734. }
  1735. $addrs[] = $value;
  1736. }
  1737. }
  1738. $einfo['useage']="";
  1739. if($einfo['use_order']!=0){
  1740. $use =Db::name("order_use")->where(["id"=>$einfo['use_order']])->find();
  1741. $einfo['useage'] = isset($use['order_use']) ?$use['order_use']:"";
  1742. }else{
  1743. $einfo['use_order']='';
  1744. }
  1745. $einfo['platform_name']='';
  1746. if($einfo['platform_id']!=0){
  1747. $plat=Db::name("platform")->where(['id'=>$einfo['platform_id']])->find();
  1748. $einfo['platform_name']=isset($plat['platform_name'])?$plat['platform_name']:"";
  1749. }
  1750. $einfo['companyName'] = isset($in['companyName']) ? $in['companyName'] : "";
  1751. $einfo['addrs'] = $addrs;
  1752. $einfo['can'] = $int;
  1753. $einfo['goodinfo'] = $goon;
  1754. if (empty($einfo)) {
  1755. return error_show(1002, "未找到销售订单数据");
  1756. } else {
  1757. return app_show(0, "获取成功", $einfo);
  1758. }
  1759. }
  1760. public function del()
  1761. {
  1762. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  1763. if ($id === "") {
  1764. return error_show(1002, "参数id不能为空");
  1765. }
  1766. $ed = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->find();
  1767. if (empty($ed)) {
  1768. return error_show(1002, "未找到销售订单数据");
  1769. }
  1770. $str = Db::name('sale')->update(['id' => $id, 'is_del' => 1, 'updatetime' => date("Y-m-d H:i:s")]);
  1771. if ($str) {
  1772. $stc = ["order_code"=>$id,"status"=>$ed['status'],"action_remark"=>'',"action_type"=>"delete"];
  1773. ActionLog::logAdd($this->post['token'],$stc,"XSQRD",$ed['status'],['id' => $id, 'is_del' => 1, 'updatetime' => date("Y-m-d H:i:s")]);
  1774. $order=["order_type"=>'XSQRD',"order_code"=>$ed['orderCode'],"order_id"=>$id,"order_status"=>$ed['status']];
  1775. ProcessOrder::workdel($order);
  1776. return error_show(0, "删除成功");
  1777. } else {
  1778. return error_show(1002, "删除失败");
  1779. }
  1780. }
  1781. public function status()
  1782. {
  1783. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  1784. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  1785. if ($id == "") {
  1786. return error_show(1002, "参数id不能为空");
  1787. }
  1788. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  1789. if ($status == "") {
  1790. return error_show(1002, "订单状态不能为空");
  1791. }
  1792. $dio = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->find();
  1793. if (empty($dio)) {
  1794. return error_show(1002, "销售单信息未找到");
  1795. }
  1796. if ($status == 1 && $dio['order_type'] == 2) {
  1797. $zxinfo = Db::name("consult")->where(["zxNo" => $dio['zxNo'], "is_del" => 0])->find();
  1798. if ($zxinfo == false) {
  1799. return error_show(1004, "未找到咨询单数据");
  1800. }
  1801. $gold['price'] = 0;
  1802. if ($zxinfo['zx_type'] == 2) {
  1803. if ($zxinfo['metals'] == "") {
  1804. return error_show(1004, "未找到咨询单贵金属类型数据");
  1805. }
  1806. $metals = $zxinfo['metals'] == '18K' ? 1 : ($zxinfo['metals'] == '24K' ? 2 : 3);
  1807. $gold = Db::name("gold_price")->where(["type" => $metals])->order("addtime desc")->find();
  1808. if (empty($gold)) {
  1809. return error_show(1004, "未找到咨询单贵金属类型数据");
  1810. }
  1811. $dio['gold_price'] = $gold['price'];
  1812. }
  1813. $wsm = Db::name("warehouse_info")->where(["supplierNo" => $zxinfo['gysNo'], "wsm_type" => 2])->find();
  1814. if (empty($wsm)) {
  1815. return error_show(1002, "供应商仓库未找到");
  1816. }
  1817. }
  1818. $apply_id = GetUserInfo($token);
  1819. if (empty($apply_id) || $apply_id['code'] != 0) {
  1820. return error_show(1002, "申请人数据不存在");
  1821. }
  1822. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  1823. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  1824. Db::startTrans();
  1825. try {
  1826. $vmp =$dio['status'];
  1827. $dio['status'] = $status;
  1828. $dio['updatetime'] = date("Y-m-d H:i:s");
  1829. $st = Db::name('sale')->save($dio);
  1830. if ($st) {
  1831. $stn = ["order_code"=>$dio['orderCode'],"status"=>$vmp,"action_remark"=>'',"action_type"=>"status"];
  1832. ActionLog::logAdd($this->post['token'],$stn, $dio['order_type'] == 2?"ZXQRD":"XSQRD",$dio['status'],$dio);
  1833. $order=["order_type"=>$dio['order_type'] == 2?"ZXQRD":"XSQRD","order_code"=>$dio['orderCode'],"order_id"=>$id,"order_status"=>$dio['status']];
  1834. ProcessOrder::AddProcess($this->post['token'],$order);
  1835. if ($status == 3 && $dio['order_type'] == 1) {
  1836. $dn = Db::name('sale_info')->where(['orderCode' => $st['orderCode']])->select();
  1837. foreach ($dn as $value) {
  1838. $dm = Db::name('good_stock')->where(['wsm_code' => $value['wsm_code'], 'good_type_code' => $st['good_code']])->find();
  1839. if ($dm == "") {
  1840. Db::rollback();
  1841. return error_show(1003, "未找到商品数据");
  1842. }
  1843. if ($value['num'] > $dm['usable_stock']) {
  1844. Db::rollback();
  1845. return error_show(1002, "超出库存数量");
  1846. }
  1847. $dm['usable_stock'] -= $value['num'];
  1848. $dm['wait_out_stock'] += $value['num'];
  1849. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code']])->sum("send_num");
  1850. $value['send_num'] = $send ?? 0;
  1851. $value['wsend_num'] = $value['num'] - $value['send_num'];
  1852. $Db = Db::name('good_stock')->update($dm);
  1853. $Db['updatetime'] = date('Y-m-d H:i:s');
  1854. if ($Db == false) {
  1855. Db::rollback();
  1856. return error_show(1002, "状态更新失败");
  1857. }
  1858. }
  1859. }
  1860. if ($status == 1 && $dio['order_type'] == 2) {
  1861. $cgd = makeNo("CG");
  1862. $clll = [
  1863. "cgdNo" => $cgd,
  1864. "bkcode" => $dio['orderCode'],
  1865. "wsm_code" => $wsm['wsm_code'],
  1866. "cgder_id" => Db::name('depart_user')->where('nickname',$zxinfo['saler'])->value('uid')??0,//重名以及多个账号的话…………
  1867. "cgder" => $zxinfo['saler'],
  1868. "good_code" => "GD-" . $zxinfo['cpNo'],
  1869. "good_name" => $zxinfo['cpName'],
  1870. "good_type_code" => $zxinfo['cpNo'],
  1871. "good_num" => $dio['good_num'],
  1872. "good_price" => $zxinfo['total_fee'],
  1873. "total_fee" => round($zxinfo['total_fee'] * $dio['good_num'], 2),
  1874. "pakge_fee" => $zxinfo['package_fee'],
  1875. "cert_fee" => $zxinfo['cert_fee'],
  1876. "open_fee" => $zxinfo['open_fee'],
  1877. "delivery_fee" => $zxinfo['delivery_fee'],
  1878. "mark_fee" => $zxinfo['mark_fee'],
  1879. "teach_fee" => $zxinfo['cost_fee'],
  1880. "demo_fee" => $zxinfo['demo_fee'],
  1881. "nake_fee" => $zxinfo['bare_fee'],
  1882. "weight" => isset($zxinfo['weight']) ? $zxinfo['weight'] : 0,
  1883. "supplierNo" => $zxinfo['gysNo'],
  1884. "supplier_name" => $zxinfo['gysname'],
  1885. "gold_price" => $gold['price'],
  1886. "send_num" => 0,
  1887. "wsend_num" => $dio['good_num'],
  1888. "remark" => '',
  1889. "lasttime" => date("Y-m-d H:i:s"),
  1890. "is_del" => 0,
  1891. "status" => $dio['send_type'] == 1 ? 3 : 0,
  1892. "order_type" => 2,
  1893. "addtime" => date("Y-m-d H:i:s"),
  1894. "updatetime" => date("Y-m-d H:i:s")
  1895. ];
  1896. $cgin = Db::name("purchease_order")->insert($clll);
  1897. // var_dump(Db::name("purchease_order")->getLastSql(),$clll);
  1898. if ($cgin == false) {
  1899. $stb = ["order_code"=>$cgd,"status"=> $clll['status'],"action_remark"=>'',"action_type"=>"create"];
  1900. ActionLog::logAdd($this->post['token'],$stb,"xsd", $clll['status'],$stb);
  1901. Db::rollback();
  1902. return error_show(1002, "咨询采购单创建商品失败");
  1903. }
  1904. if ($dio['send_type'] == 1) {
  1905. $addr = Db::name('order_addr')->where(["orderCode" => $dio['orderCode'], 'is_del' => 0])->select();
  1906. foreach ($addr as $value) {
  1907. $outCode = makeNo("DF");
  1908. $data = [
  1909. "wsm_code" => $wsm['wsm_code'],
  1910. "orderCode" => $dio['orderCode'],
  1911. "outCode" => $outCode,
  1912. "order_type" => $dio['order_type'],
  1913. "apply_id" => $rm,
  1914. "apply_name" => $ri,
  1915. "addrid" => $value['id'],
  1916. "post_name" => '',
  1917. "post_code" => '',
  1918. "post_fee" => '',
  1919. "sendtime" => date("Y-m-d H:i:s"),
  1920. "send_num" => $value['receipt_quantity'],
  1921. "check_num" => 0,
  1922. "error_num" => 0,
  1923. "status" => 0,
  1924. "addtime" => date("Y-m-d H:i:s"),
  1925. "updatetime" => date("Y-m-d H:i:s")
  1926. ];
  1927. $datainfo = Db::name('order_out')->insert($data);
  1928. if (!$datainfo) {
  1929. $sti = ["order_code"=>$dio['orderCode'],"status"=> 0,"action_remark"=>'',"action_type"=>"create"];
  1930. ActionLog::logAdd($this->post['token'],$sti,"xsd",0,$sti);
  1931. Db::rollback();
  1932. return error_show(1003, "创建失败");
  1933. }
  1934. }
  1935. $wsm_in_code = makeNo("CF");
  1936. $orin = [
  1937. "wsm_in_code" => $wsm_in_code,
  1938. "cgdNo" => $cgd,
  1939. "wsm_code" => $wsm['wsm_code'],
  1940. "wsm_reaper" => "",
  1941. "send_num" => $dio['good_num'],
  1942. "post_company" => '',
  1943. "post_code" => '',
  1944. "post_fee" => '',
  1945. "sendtime" => date("Y-m-d H:i:s"),
  1946. "apply_id" => $rm,
  1947. "apply_name" => $ri,
  1948. "wait_num" => 0,
  1949. "status" => 4,
  1950. "addtime" => date("Y-m-d H:i:s"),
  1951. "updatetime" => date("Y-m-d H:i:s")
  1952. ];
  1953. $win = Db::name("purchease_in")->insert($orin);
  1954. if (!$win){
  1955. $stv = ["order_code"=>$wsm_in_code,"status"=>4,"action_remark"=>'',"action_type"=>"create"];
  1956. ActionLog::logAdd($this->post['token'],$stv,"xsd",4,$stv);
  1957. Db::rollback();
  1958. return error_show(1003, "创建失败");
  1959. }
  1960. $good = Db::name("good_stock")->where(["wsm_code" => $wsm['wsm_code'], "good_type_code" => $zxinfo['cpNo'], "is_del" => 0])->find();
  1961. if (empty($good)) {
  1962. $good = [
  1963. "good_type_code" => $zxinfo['cpNo'],
  1964. "wsm_code" => $wsm['wsm_code'],
  1965. "usable_stock" => 0,
  1966. "wait_out_stock" => 0,
  1967. "total_stock" => 0,
  1968. "addtime" => date("Y-m-d H:i:s"),
  1969. "updatetime" => date("Y-m-d H:i:s"),
  1970. ];
  1971. }
  1972. $good['wait_out_stock'] += $dio['good_num'];
  1973. $good['total_stock'] = $good['usable_stock'] + $good['wait_out_stock'];
  1974. $good['updatetime'] = date("Y-m-d H:i:s");
  1975. $upd = Db::name("good_stock")->save($good);
  1976. if (!$upd) {
  1977. $ste = ["order_code"=> $zxinfo['cpNo'],"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  1978. ActionLog::logAdd($this->post['token'],$ste,"xsd",0,$ste);
  1979. Db::rollback();
  1980. return error_show(1003, "创建失败");
  1981. }
  1982. }
  1983. }
  1984. }
  1985. Db::commit();
  1986. return error_show(0, "订单状态更新成功");
  1987. } catch (Exception $e) {
  1988. Db::rollback();
  1989. return error_show(1005, $e->getMessage());
  1990. }
  1991. }
  1992. public function fee()
  1993. {
  1994. $id = isset($this->post['id']) && $this->post['id'] !== "" ? trim($this->post['id']) : "";
  1995. if ($id == "") {
  1996. return error_show(1002, "参数id不能为空");
  1997. }
  1998. $etid = Db::name("sale")->where(["id" => $id, "is_del" => 0])->find();
  1999. if (empty($etid)) {
  2000. return error_show(1002, "未找到数据");
  2001. }
  2002. $vr = isset($this->post['order_addr']) && $this->post['order_addr'] !== "" ? $this->post['order_addr'] : "";
  2003. if ($vr == "") {
  2004. return error_show(1002, "参数order_addr不能为空");
  2005. }
  2006. Db::startTrans();
  2007. try {
  2008. $data = [
  2009. "id" => $id,
  2010. "status" => 2,
  2011. "is_del" => 0,
  2012. "updatetime" => date("Y-m-d H:i:s")
  2013. ];
  2014. $datainfo = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->save($data);
  2015. if ($datainfo) {
  2016. $stx = ["order_code"=> $etid['orderCode'],"status"=>$etid['status'],"action_remark"=>'',
  2017. "action_type"=>"edit"];
  2018. ActionLog::logAdd($this->post['token'],$stx,$etid['order_type'] == 2?"ZXQRD":"XSQRD",2,$data);
  2019. $order=["order_type"=>$etid['order_type'] == 2?"ZXQRD":"XSQRD","order_code"=>$etid['orderCode'],"order_id"=>$id,"order_status"=>$data['status']];
  2020. ProcessOrder::AddProcess($this->post['token'],$order);
  2021. foreach ($vr as $value) {
  2022. $tm = [];
  2023. $tm['id'] = $value['id'];
  2024. $tm['orderCode'] = $etid['orderCode'];
  2025. $tm['post_fee'] = $value['post_fee'];
  2026. $tm['updatetime'] = date("Y-m-d H:i:s");
  2027. $dn = Db::name('order_addr')->save($tm);
  2028. if ($dn == false) {
  2029. Db::rollback();
  2030. return error_show(1002, "更新失败");
  2031. }
  2032. }
  2033. Db::commit();
  2034. return error_show(0, "更新成功");
  2035. }
  2036. Db::rollback();
  2037. return error_show(1003, "更新失败");
  2038. } catch (Exception $e) {
  2039. Db::rollback();
  2040. return error_show(1005, $e->getMessage());
  2041. }
  2042. }
  2043. public function out()
  2044. {
  2045. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  2046. $outCode = makeNo("DF");
  2047. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  2048. if ($orderCode == "") {
  2049. return error_show(1002, "参数orderCoder不能为空");
  2050. }
  2051. $der = Db::name('sale')->where(['orderCode' => $orderCode])->find();
  2052. if ($der == "") {
  2053. return error_show(1002, "未找到出库订单单号");
  2054. }
  2055. // $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code'] !== "" ? trim($this->post['wsm_code']) : "";
  2056. // if ($wsm_code == "") {
  2057. // return error_show(1002, "参数wsm_code不能为空");
  2058. // }
  2059. // $wsm = Db::name('sale_info')->where(['wsm_code' => $wsm_code, 'orderCode' => $der['orderCode']])->find();
  2060. // if ($wsm == "") {
  2061. // return error_show(1002, "未找到仓库编码");
  2062. // }
  2063. $post_name = isset($this->post['post_name']) && $this->post['post_name'] !== "" ? trim($this->post['post_name']) : "";
  2064. if ($post_name == "") {
  2065. return error_show(1002, "物流公司不能为空");
  2066. }
  2067. $post_code = isset($this->post['post_code']) && $this->post['post_code'] !== "" ? trim($this->post['post_code']) : "";
  2068. if ($post_code == "") {
  2069. return error_show(1002, "物流单号不能为空");
  2070. }
  2071. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !== "" ? intval($this->post['post_fee']) : "";
  2072. if ($post_fee == "") {
  2073. return error_show(1002, "物流费不能为空");
  2074. }
  2075. $send_num = isset($this->post['send_num']) && $this->post['send_num'] !== "" ? $this->post['send_num'] : "";
  2076. if ($send_num == "") {
  2077. return error_show(1002, "发货数量不能为空");
  2078. }
  2079. if ($send_num > $der['wsend_num']) {
  2080. return error_show(1002, "超出可发货数量");
  2081. }
  2082. $apply_id = GetUserInfo($token);
  2083. if (empty($apply_id) || $apply_id['code'] != 0) {
  2084. return error_show(1002, "申请人数据不存在");
  2085. }
  2086. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  2087. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  2088. $addrid = isset($this->post['addrid']) && $this->post['addrid'] !== "" ? intval($this->post['addrid']) : "";
  2089. $dr = Db::name('order_addr')->where(['id' => $addrid, 'is_del' => 0])->find();
  2090. if (empty($dr)) {
  2091. return error_show(1003, "收货人信息未找到");
  2092. }
  2093. if ($dr['orderCode'] !== $orderCode) {
  2094. return error_show(1002, "订单code不存在");
  2095. }
  2096. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !== "" ? $this->post['sendtime'] : date("Y-m-d H:i:s");
  2097. // $check_num = isset($this->post['check_num']) && $this->post['check_num'] !=="" ? $this->post['check_num'] :"";
  2098. // $error_num = isset($this->post['error_num']) && $this->post['error_num'] !=="" ? $this->post['error_num'] :"";
  2099. $status = isset($this->post['status']) && $this->post['status'] !== "" ? $this->post['status'] : "1";
  2100. //$order_type = isset($this->post['order_type']) && $this->post['order_type'] !=="" ? $this->post['order_type'] :"1";
  2101. //$dn=Db::name('sale_info')->where(['wsm_code'=>$outCode])->find();
  2102. Db::startTrans();
  2103. try {
  2104. $data = [
  2105. "wsm_code" => "",
  2106. "orderCode" => $orderCode,
  2107. "outCode" => $outCode,
  2108. "order_type" => $der['order_type'],
  2109. "apply_id" => $rm,
  2110. "apply_name" => $ri,
  2111. "addrid" => $dr['id'],
  2112. "post_name" => $post_name,
  2113. "post_code" => $post_code,
  2114. "post_fee" => $post_fee,
  2115. "sendtime" => $sendtime,
  2116. "send_num" => $send_num,
  2117. "check_num" => 0,
  2118. "error_num" => 0,
  2119. "status" => $status,
  2120. "addtime" => date("Y-m-d H:i:s"),
  2121. "updatetime" => date("Y-m-d H:i:s")
  2122. ];
  2123. $datainfo = Db::name('order_out')->insert($data,true);
  2124. if ($datainfo>0) {
  2125. $stx = ["order_code"=>$outCode,"status"=>$status,"action_remark"=>'',"action_type"=>"create"];
  2126. ActionLog::logAdd($this->post['token'],$stx,$der['order_type']==1?"CKD":'ZXCKD',$status,$data);
  2127. $order=["order_type"=>$der['order_type']==1?"CKD":'ZXCKD',"order_code"=>$outCode,"order_id"=>$datainfo,"order_status"=>$data['status']];
  2128. ProcessOrder::AddProcess($this->post['token'],$order);
  2129. if ($status == 1) {
  2130. $item =$der['status'];
  2131. $der['send_num'] += $send_num;
  2132. $der['wsend_num'] -= $send_num;//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  2133. $der['send_status'] = $der['send_num'] == 0 ? 1 : $der['wsend_num'] == 0 ? 3 : 2;
  2134. $der['status'] = $der['send_num'] == 0 ? 3 : $der['wsend_num'] == 0 ? 5 : 4;
  2135. $der['updatetime'] = date("Y-m-d H:i:s");
  2136. $si = Db::name('sale')->save($der);
  2137. if ($si == false) {
  2138. Db::rollback();
  2139. return error_show(1003, "更新失败");
  2140. }
  2141. $stx = ["order_code"=>$orderCode,"status"=>$item,"action_remark"=>'',"action_type"=>"edit"];
  2142. ActionLog::logAdd($this->post['token'],$stx,$der['order_type'] == 2?"ZXQRD":"XSQRD",$der['status'],$der);
  2143. $order=["order_type"=>$der['order_type'] == 2?"ZXQRD":"XSQRD","order_code"=>$orderCode,"order_id"=>$der['id'],"order_status"=>$data['status']];
  2144. ProcessOrder::AddProcess($this->post['token'],$order);
  2145. $str = Db::name('good_stock')->where(['wsm_code' => $var['wsm_code'], 'good_type_code' => $der['good_code']])->find();
  2146. if ($str == false) {
  2147. Db::rollback();
  2148. return error_show(1002, "商品数据未找到");
  2149. }
  2150. if ($send_num > $str['wait_out_stock']) {
  2151. Db::rollback();
  2152. return error_show(1002, "超出库存数量");
  2153. }
  2154. $str['wait_out_stock'] -= $send_num;
  2155. $str['intra_stock'] += $send_num;
  2156. $str['updatetime'] = date("Y-m-d H:i:s");
  2157. $stre = Db::name('good_stock')->save($str);
  2158. if ($stre == false) {
  2159. Db::rollback();
  2160. return error_show(1002, "状态更新失败");
  2161. }
  2162. //商品变动日志表,good_log_code字段存储采购单号
  2163. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $str['id'], "type" => 2, 'stock' => $send_num, "stock_name" => "wait_out_stock"];
  2164. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $str['id'], "type" => 1, 'stock' => $send_num, "stock_name" => "intra_stock"];
  2165. GoodLog::LogAdd($this->post['token'],$good_data,$der['order_type'] != 1?"ZXQRD":"XSQRD");
  2166. }
  2167. Db::commit();
  2168. return error_show(0, "创建成功");
  2169. } else {
  2170. Db::rollback();
  2171. return error_show(1003, "创建失败");
  2172. }
  2173. } catch (Exception $e) {
  2174. Db::rollback();
  2175. return error_show(1005, $e->getMessage());
  2176. }
  2177. }
  2178. public function customer()
  2179. {
  2180. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  2181. if (empty($outCode)) {
  2182. return error_show(1002, "销售订单编号不能为空");
  2183. }
  2184. $codeinfo = Db::name('order_out')->where(['outCode' => $outCode])->find();
  2185. if ($codeinfo == "") {
  2186. return error_show(1003, "未找到订单数据");
  2187. }
  2188. // $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']) :"";
  2189. // if($orderCode==""){
  2190. // return error_show(1002,"参数orderCoder不能为空");
  2191. // }
  2192. $dr = Db::name('sale')->where(['orderCode' => $codeinfo['orderCode']])->find();
  2193. if ($dr == "") {
  2194. return error_show(1002, "未找到销售订单");
  2195. }
  2196. $status = isset($this->post['status']) && $this->post['status'] !== "" ? $this->post['status'] : "";
  2197. if ($status == "") {
  2198. return error_show(10002, "状态不能为空");
  2199. }
  2200. Db::startTrans();
  2201. try {
  2202. $time = $codeinfo['status'];
  2203. $codeinfo['status'] = $status;
  2204. $codeinfo['updatetime'] = date("Y-m-d H:i:s");
  2205. $cust = Db::name('order_out')->save($codeinfo);
  2206. if ($cust) {
  2207. $stx = ["order_code"=>$outCode,"status"=>$time,"action_remark"=>'',"action_type"=>"status"];
  2208. ActionLog::logAdd($this->post['token'],$stx,$codeinfo['order_type'] == 2?"ZXCKD":"CKD",$codeinfo['status'],$codeinfo);
  2209. $order=["order_type"=>$codeinfo['order_type'] == 2?"ZXCKD":"CKD","order_code"=>$outCode,
  2210. "order_id"=>$codeinfo['id'],"order_status"=>$codeinfo['status']];
  2211. ProcessOrder::AddProcess($this->post['token'],$order);
  2212. if ($status == 1) {
  2213. $stokc =Db::name("good_stock")->where(['spuCode'=>$codeinfo['spuCode'],"wsm_code"=>$codeinfo['wsm_code'],
  2214. "is_del"=>0])->find();
  2215. if($stokc==false){
  2216. Db::rollback();
  2217. return error_show(1002, "未找库存到数据");
  2218. }else{
  2219. if ($codeinfo['send_num'] > $stokc['usable_stock']) {
  2220. Db::rollback();
  2221. return error_show(1002, "超出库存数量");
  2222. }
  2223. $stokc['wait_out_stock']+=$codeinfo['send_num'];
  2224. $stokc['usable_stock']-=$codeinfo['send_num'];
  2225. $stokc['updatetime']=date("Y-m-d H:i:s");
  2226. }
  2227. $stoc= Db::name("good_stock")->save($stokc);
  2228. if($stoc==false){
  2229. Db::rollback();
  2230. return error_show(1002, "库存更新失败");
  2231. }
  2232. $item =$dr['status'];
  2233. $dr['send_num'] +=$codeinfo['send_num'];
  2234. $dr['wsend_num'] -= $codeinfo['send_num'];//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  2235. $dr['send_status'] = $dr['send_num'] == 0 ? 1 : $dr['wsend_num'] == 0 ? 3 : 2;
  2236. $dr['status'] = $dr['send_num'] == 0 ? 3 : $dr['wsend_num'] == 0 ? 5 : 4;
  2237. $dr['updatetime'] = date("Y-m-d H:i:s");
  2238. $si = Db::name('sale')->save($dr);
  2239. if ($si == false) {
  2240. Db::rollback();
  2241. return error_show(1003, "更新失败");
  2242. }
  2243. $stx = ["order_code"=>$dr['orderCode'],"status"=>$item,"action_remark"=>'',"action_type"=>"edit"];
  2244. ActionLog::logAdd($this->post['token'],$stx,$dr['order_type'] == 2?"ZXQRD":"XSQRD",$dr['status'],
  2245. $dr);
  2246. $order=["order_type"=>$dr['order_type'] == 2?"ZXQRD":"XSQRD","order_code"=>$dr['orderCode'],"order_id"=>$dr['id'],"order_status"=>$dr['status']];
  2247. ProcessOrder::AddProcess($this->post['token'],$order);
  2248. //商品变动日志表,good_log_code字段存储销售订单编号
  2249. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $stoc['id'], "type" => 1, 'stock' => $codeinfo['send_num'], "stock_name" => "wait_out_stock"];
  2250. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $stoc['id'], "type" => 2, 'stock' => $codeinfo['send_num'], "stock_name" => "usable_stock"];
  2251. GoodLog::LogAdd($this->post['token'],$good_data,'CKD');
  2252. }
  2253. if ($status == 3) {
  2254. $var = Db::name('good_stock')->where(['wsm_code' => $codeinfo['wsm_code'], 'good_type_code' => $dr['good_code']])->find();
  2255. if ($var == false) {
  2256. Db::rollback();
  2257. return error_show(1002, "未找到商品数据");
  2258. }
  2259. if ($codeinfo['send_num'] > $var['intra_stock']) {
  2260. Db::rollback();
  2261. return error_show(1003, "超出在途库存数量");
  2262. }
  2263. $var['intra_stock'] -= $codeinfo['send_num'];
  2264. $var['updatetime'] = date("Y-m-d H:i:s");
  2265. $stre = Db::name('good_stock')->save($var);
  2266. //商品变动日志表,good_log_code字段存储销售订单编号
  2267. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $var['id'], "type" => 2, 'stock' => $codeinfo['send_num'], "stock_name" => "intra_stock"];
  2268. GoodLog::LogAdd($this->post['token'],$good_data,$codeinfo['order_type'] == 2?"ZXCKD":"CKD");
  2269. if ($stre == false) {
  2270. Db::rollback();
  2271. return error_show(1002, "状态更新失败");
  2272. }
  2273. }
  2274. Db::commit();
  2275. return error_show(0, "出库订单更新成功");
  2276. } else {
  2277. Db::rollback();
  2278. return error_show(1003, "出库订单更新失败");
  2279. }
  2280. } catch (Exception $e) {
  2281. Db::rollback();
  2282. return error_show(1005, $e->getMessage());
  2283. }
  2284. }
  2285. public function difflist()
  2286. {
  2287. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  2288. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  2289. $where = [];
  2290. $cgdNo = isset($this->post['orderCode']) && $this->post['orderCode'] != "" ? trim($this->post['orderCode']) : "";
  2291. if ($cgdNo != "") {
  2292. $where[] = ['a.orderCode', "like", "%$cgdNo%"];
  2293. }
  2294. $diffNo = isset($this->post['diffCode']) && $this->post['diffCode'] != "" ? trim($this->post['diffCode']) : "";
  2295. if ($diffNo != "") {
  2296. $where[] = ['a.diffCode', "like", "%$diffNo%"];
  2297. }
  2298. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  2299. if ($status !== "") {
  2300. $where[] = ['a.status', "=", $status];
  2301. }
  2302. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  2303. if ($start != "") {
  2304. $where[] = ["a.addtime", '>=', $start];
  2305. }
  2306. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  2307. if ($end != "") {
  2308. $where[] = ["a.addtime", '<=', $end];
  2309. }
  2310. // $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !==""? intval($this->post['apply_name']):"";
  2311. // if($apply_name!==""){
  2312. // $where[]=['a.apply_name',"like","%$apply_name%"];
  2313. // }
  2314. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  2315. if ($supplierNo != "") {
  2316. // $supplier = Db::name("business")->where([['code', "like", "%$supplierNo%"]])->column('code');
  2317. //// if(empty($supplier)){
  2318. //// return error_show(1004,"未找到供应商信息");
  2319. //// }
  2320. // $wsmcode = Db::name("sale")->where(["is_del" => 0, "supplierNo" => $supplier])->column("orderCode");
  2321. $where[] = ['c.supplierNo', "like", "%$supplierNo%"];
  2322. }
  2323. $companyNo =isset($this->post['companyNo']) &&$this->post['companyNo'] !=="" ? trim($this->post['companyNo']):"";
  2324. if($companyNo!=""){
  2325. $where[]=['c.customer_code',"like","%$companyNo%"];
  2326. }
  2327. $count = Db::name('sale_diff')->alias('a')
  2328. ->join("sale c", "c.orderCode=a.orderCode", "left")
  2329. ->join("customer_info v", "v.companyNo=c.customer_code", "left")
  2330. ->where($where)->count();
  2331. $total = ceil($count / $size);
  2332. $page = $page >= $total ? intval($total) : $page;
  2333. $list = Db::name('sale_diff')->alias('a')
  2334. ->join("sale c", "c.orderCode=a.orderCode", "left")
  2335. ->join("customer_info v", "v.companyNo=c.customer_code", "left")
  2336. ->where($where)->page($page, $size)->field("a.*,c.customer_code,v.companyName,c.skuCode,c.order_type,c.supplierNo")
  2337. ->order("a.addtime desc")->select();
  2338. $data = [];
  2339. foreach ($list as $value) {
  2340. if($value['order_type']==3|| $value['order_type']==4){
  2341. $goon = Db::name("good_zixun")->where(["spuCode"=>$value['good_code'],"is_del"=>0])->find();
  2342. }else {
  2343. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  2344. ->where(['a.skuCode' => $value['skuCode']])->find();
  2345. }
  2346. $value['can'] = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  2347. $wsm = Db::name("business")->where(['companyNo' => $value['supplierNo']])->find();
  2348. $value['supplierNo'] = isset($wsm['companyNo']) ? $wsm['companyNo'] : "";
  2349. $value['supplierName'] = isset($wsm['company']) ? $wsm['company'] : "";
  2350. $data[] = $value;
  2351. }
  2352. return app_show(0, "获取成功", ['list' => $data, 'count' => $count]);
  2353. }
  2354. /**
  2355. * @return \think\response\Json|void
  2356. * @throws \think\db\exception\DataNotFoundException
  2357. * @throws \think\db\exception\DbException
  2358. * @throws \think\db\exception\ModelNotFoundException
  2359. */
  2360. public function diffcheck()
  2361. {
  2362. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  2363. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  2364. if ($id === "") {
  2365. return error_show(1004, "参数id不能为空");
  2366. }
  2367. $info = Db::name("sale_diff")->where(["id" => $id])->find();
  2368. if (empty($info)) {
  2369. return error_show(1004, "订单数据未找到");
  2370. }
  2371. $is_act = isset($this->post['is_act']) && $this->post['is_act'] !== "" ? intval($this->post['is_act']) : "";
  2372. if ($is_act === '') {
  2373. return error_show(1004, "参数is_act不能为空");
  2374. }
  2375. $customer_remark = isset($this->post['customer_remark']) && $this->post['customer_remark'] !== "" ? trim($this->post['customer_remark']) : "";
  2376. if ($customer_remark === '') {
  2377. return error_show(1004, "参数customer_remark不能为空");
  2378. }
  2379. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "2";
  2380. $remark = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
  2381. $item = $info['status'];
  2382. $info['is_act'] = $is_act;
  2383. $info['customer_remark'] = $customer_remark;
  2384. $info['status'] = $status;
  2385. $info['remark'] = $remark;
  2386. $info['updatetime'] = date("Y-m-d H:i:s");
  2387. $up = Db::name("sale_diff")->save($info);
  2388. if ($up) {
  2389. $stx = ["order_code"=>$info["orderCode"],"status"=>$item,"action_remark"=>'',"action_type"=>"edit"];
  2390. ActionLog::logAdd($this->post['token'],$stx,"QRGCD",$info['status'],$info);
  2391. $order=["order_type"=>"QRGCD","order_code"=>$info["orderCode"],"order_id"=>$info['id'],"order_status"=>$info['status']];
  2392. ProcessOrder::AddProcess($this->post['token'],$order);
  2393. return app_show(0, "更新成功");
  2394. } else {
  2395. return error_show(1003, "更新失败");
  2396. }
  2397. }
  2398. /**
  2399. * @return \think\response\Json|void
  2400. * @throws \think\db\exception\DataNotFoundException
  2401. * @throws \think\db\exception\DbException
  2402. * @throws \think\db\exception\ModelNotFoundException
  2403. */
  2404. public function diffstatus()
  2405. {
  2406. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  2407. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  2408. if ($id === "") {
  2409. return error_show(1004, "参数id不能为空");
  2410. }
  2411. $info = Db::name("sale_diff")->where(["id" => $id])->find();
  2412. if ($info==false) {
  2413. return error_show(1004, "订单数据未找到");
  2414. }
  2415. $cgd = Db::name("purchease_diff")->where(['id'=>$info['cgd_diffid']])->find();
  2416. if($cgd==false){
  2417. return error_show(1004, "采购工差单数据未找到");
  2418. }
  2419. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "3";
  2420. $remark = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
  2421. $ite=$info['status'];
  2422. Db::startTrans();
  2423. try{
  2424. $upda=[
  2425. "status"=>$status,
  2426. "remark"=>$remark,
  2427. "updatetime"=> date("Y-m-d H:i:s"),
  2428. ];
  2429. $up = Db::name("sale_diff")->where($info)->save($upda);
  2430. if ($up) {
  2431. $stx = ["order_code"=>$info["orderCode"],"status"=>$ite,"action_remark"=>'',"action_type"=>"status"];
  2432. ActionLog::logAdd($this->post['token'],$stx,"QRGCD",$info['status'],$info);
  2433. $order=["order_type"=>"QRGCD","order_code"=>$info["orderCode"],"order_id"=>$info['id'],"order_status"=>$status];
  2434. ProcessOrder::AddProcess($this->post['token'],$order);
  2435. $cgdup =Db::name("purchease_diff")->where(["id"=>$info['cgd_diffid']])->save($upda);
  2436. if($cgdup){
  2437. Db::commit();
  2438. return app_show(0, "更新成功");
  2439. }
  2440. }
  2441. Db::rollback();
  2442. return error_show(1003, "更新失败");
  2443. }catch (\Exception $e){
  2444. Db::rollback();
  2445. return error_show(1003,$e->getMessage());
  2446. }
  2447. }
  2448. /**
  2449. * @return \think\response\Json|void
  2450. * @throws \think\db\exception\DataNotFoundException
  2451. * @throws \think\db\exception\DbException
  2452. * @throws \think\db\exception\ModelNotFoundException
  2453. */
  2454. public function diffinfo()
  2455. {
  2456. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  2457. if ($id === "") {
  2458. return error_show(1004, "参数id不能为空");
  2459. }
  2460. $info = Db::name('sale_diff')->where(["id" => $id])->find();
  2461. if (empty($info)) {
  2462. return error_show(1004, "工差订单数据未找到");
  2463. }
  2464. $einfo = Db::name('sale')->where(['orderCode' => $info['orderCode'], 'is_del' => 0])->find();
  2465. if ($einfo==false) {
  2466. return error_show(1002, "未找到销售订单数据");
  2467. }
  2468. if($einfo['order_type']==3|| $einfo['order_type']==4){
  2469. $goon = Db::name("good_zixun")->where(["spuCode"=>$einfo['good_code'],"is_del"=>0])->find();
  2470. }else {
  2471. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  2472. ->where(['a.skuCode' => $einfo['skuCode']])->find();
  2473. }
  2474. if($goon==false){
  2475. return error_show(1002, "未找到商品数据");
  2476. }
  2477. $info['skuCode'] =isset($goon['skuCode']) ? $goon['skuCode']:"";
  2478. $info['spuCode'] =isset($goon['spuCode']) ? $goon['spuCode']:"";
  2479. $info['order_type'] =isset($einfo['order_type']) ? $einfo['order_type']:"";
  2480. $info['can'] = isset($info['cat_id']) && $info['cat_id'] !== 0 ? made($info['cat_id']) : [];
  2481. return app_show(0, "获取成功", $info);
  2482. }
  2483. public function saleout(){
  2484. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  2485. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']) :"10";
  2486. $where= [];
  2487. $orderCode=isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode'])
  2488. :"";
  2489. if($orderCode !==""){
  2490. $where[]=['a.orderCode',"like","%$orderCode%"];
  2491. }
  2492. $order_type=isset($this->post['order_type']) && $this->post['order_type'] !=="" ? trim($this->post['order_type'])
  2493. :"";
  2494. if($order_type !==""){
  2495. $where[]=['a.order_type',"=",$order_type];
  2496. }
  2497. $outCode=isset($this->post['outCode']) && $this->post['outCode'] !=="" ? trim($this->post['outCode'])
  2498. :"";
  2499. if($outCode !==""){
  2500. $where[]=['a.outCode',"like","%$outCode%"];
  2501. }
  2502. $apply_name=isset($this->post['apply_name']) && $this->post['apply_name'] !=="" ? trim($this->post['apply_name'])
  2503. :"";
  2504. if($apply_name !==""){
  2505. $where[]=['a.apply_name',"like","%$apply_name%"];
  2506. }
  2507. $good_code=isset($this->post['good_code']) && $this->post['good_code'] !=="" ? trim($this->post['good_code'])
  2508. :"";
  2509. if($good_code !==""){
  2510. $where[]=['b.good_code',"like","%$good_code%"];
  2511. }
  2512. $good_name=isset($this->post['good_name']) && $this->post['good_name'] !=="" ? trim($this->post['good_name'])
  2513. :"";
  2514. if($good_name !==""){
  2515. $where[]=['b.good_name',"like","%$good_name%"];
  2516. }
  2517. $status=isset($this->post['status']) && $this->post['status'] !=="" ? trim($this->post['status']):"";
  2518. if($status !==""){
  2519. $where[]=['a.status',"=",$status];
  2520. }
  2521. $start = isset($this->post['start']) && $this->post['start'] !=="" ? $this->post['start'] :"";
  2522. if($start !=""){
  2523. $where[]= ["a.addtime",'>=',$start];
  2524. }
  2525. $end = isset($this->post['end']) && $this->post['end'] !=="" ? $this->post['end'] :"";
  2526. if($end !=""){
  2527. $where[]= ["a.addtime",'<=',$end];
  2528. }
  2529. $companyNo =isset($this->post['companyNo']) &&$this->post['companyNo'] !=="" ? trim($this->post['companyNo']):"";
  2530. if($companyNo!=""){
  2531. $where[]=['b.customer_code',"like","%$companyNo%"];
  2532. }
  2533. $count = Db::name('order_out')->alias('a')
  2534. ->join("sale b", "b.orderCode=a.orderCode", "left")
  2535. ->join("customer_info v", "v.companyNo=b.customer_code", "left")
  2536. //->join("good n","n.good_code=b.good_code","left")
  2537. ->where($where)->count();
  2538. $total = ceil($count / $size);
  2539. $page = $page >= $total ? $total : $page;
  2540. $list = Db::name('order_out')->alias('a')->join("sale b", "b.orderCode=a.orderCode", "left")
  2541. ->join("customer_info v", "v.companyNo=b.customer_code", "left")
  2542. //->join("good n","n.good_code=b.good_code","left")
  2543. ->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")
  2544. ->where($where)->order("addtime desc")->page($page, $size)->select();
  2545. $data=[];
  2546. foreach ($list as $value) {
  2547. $wsmcode = Db::name("warehouse_info")->alias("k")->leftJoin("supplier c", "k.supplierNo=c.code")
  2548. ->where(["k.wsm_code" => $value['wsm_code']])->field("k.name as wsm_name,c.name,c.code")->find();
  2549. $addr = Db::name("order_addr")->where(["id" => $value['addrid']])->find();
  2550. if($value['order_type']==3|| $value['order_type']==4){
  2551. $goon = Db::name("good_zixun")->where(["spuCode"=>$value['good_code'],"is_del"=>0])->find();
  2552. }else {
  2553. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  2554. ->where(['a.skuCode' => $value['skuCode']])->find();
  2555. }
  2556. $value['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  2557. $value['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  2558. $value['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
  2559. $value['addr'] = isset($addr['addr']) ? $addr['addr'] : "";
  2560. $value['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : "";
  2561. $value['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : "";
  2562. $value['can'] = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  2563. $data[] = $value;
  2564. }
  2565. return app_show(0, "获取成功", ['list' => $data, 'count' => $count]);
  2566. }
  2567. public function outinfo()
  2568. {
  2569. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  2570. if ($outCode == "") {
  2571. return error_show(1002, "参数outcode不能为空");
  2572. }
  2573. $codeinfo = Db::name("order_out")->where(['outCode' => $outCode])->find();
  2574. if (empty($codeinfo)) {
  2575. return error_show(1002, "未找到出库数据");
  2576. }
  2577. $item = Db::name("sale")->where(['orderCode' => $codeinfo['orderCode']])->find();
  2578. if($item['order_type']==3|| $item['order_type']==4){
  2579. $goodinfo = Db::name("good_zixun")->where(["spuCode"=>$item['good_code'],"is_del"=>0])->find();
  2580. if($goodinfo==false){
  2581. return error_show(1004,"未找到商品数据");
  2582. }
  2583. }else{
  2584. $goodinfo = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  2585. ->where(['a.skuCode' =>$item['skuCode']])->find();
  2586. if ($goodinfo == false) {
  2587. return error_show(1002, "未找到商品数据");
  2588. }
  2589. }
  2590. $int = isset($goodinfo['cat_id']) && $goodinfo['cat_id'] != 0 ? made($goodinfo['cat_id']) : [];
  2591. $addr = Db::name("order_addr")->where(["id" => $codeinfo['addrid']])->find();
  2592. $codeinfo['good_name'] = isset($item['good_name']) ? $item['good_name'] : "";
  2593. $codeinfo['good_num'] = isset($item['good_num']) ? $item['good_num'] : "";
  2594. $codeinfo['good_code'] = isset($item['good_code']) ? $item['good_code'] : "";
  2595. $codeinfo['origin_price'] = isset($item['origin_price']) ? $item['origin_price'] : "";
  2596. $codeinfo['sale_price'] = isset($item['sale_price']) ? $item['sale_price'] : "";
  2597. $codeinfo['total_price'] = isset($item['total_price']) ? $item['total_price'] : "";
  2598. $codeinfo['skuCode'] = isset($item['skuCode']) ? $item['skuCode'] : "";
  2599. $codeinfo['addr'] = GetAddr($addr['addr_code']);
  2600. $codeinfo['addr'] .= isset($addr['addr']) ? $addr['addr'] : "";
  2601. $codeinfo['addr_code'] = isset($addr['addr_code']) ? $addr['addr_code'] : "";
  2602. $codeinfo['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : "";
  2603. $codeinfo['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : "";
  2604. $codeinfo['can'] = $int;
  2605. $orderReturn = Db::name("order_return")->where(["outCode"=>$outCode,"is_del"=>0])->order("id desc")->find();
  2606. $codeinfo['order_return'] = $orderReturn ;
  2607. return app_show(0, "获取成功", $codeinfo);
  2608. }
  2609. public function outadd(){
  2610. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  2611. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  2612. if ($orderCode == "") {
  2613. return error_show(1002, "参数orderCoder不能为空");
  2614. }
  2615. $der = Db::name('sale')->where(['orderCode' => $orderCode,"is_del"=>0])->find();
  2616. if ($der == "") {
  2617. return error_show(1002, "未找到出库订单单号");
  2618. }
  2619. $send_num = isset($this->post['send_num']) && $this->post['send_num'] !== "" ? $this->post['send_num'] : "";
  2620. if ($send_num == "") {
  2621. return error_show(1002, "发货数量不能为空");
  2622. }
  2623. if ($send_num > $der['wsend_num']) {
  2624. return error_show(1002, "超出可发货数量");
  2625. }
  2626. $contactor = isset($this->post['contactor'])&&$this->post['contactor']!="" ? trim($this->post['contactor']):"";
  2627. if($contactor==""){
  2628. return error_show(1002, "参数contactor不能为空");
  2629. }
  2630. $mobile = isset($this->post['mobile'])&&$this->post['mobile']!="" ? trim($this->post['mobile']):"";
  2631. if($mobile==""){
  2632. return error_show(1002, "参数mobile不能为空");
  2633. }
  2634. $addr = isset($this->post['addr'])&&$this->post['addr']!="" ? trim($this->post['addr']):"";
  2635. if($addr==""){
  2636. return error_show(1002, "参数addr不能为空");
  2637. }
  2638. $addr_code = isset($this->post['addr_code'])&&$this->post['addr_code']!="" ? trim($this->post['addr_code']):"";
  2639. if($addr_code==""){
  2640. return error_show(1002, "参数addr_code不能为空");
  2641. }
  2642. $apply_id = GetUserInfo($token);
  2643. if (empty($apply_id) || $apply_id['code'] != 0) {
  2644. return error_show(1002, "申请人数据不存在");
  2645. }
  2646. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  2647. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  2648. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !== "" ? $this->post['sendtime'] : date("Y-m-d H:i:s");
  2649. $order =Db::name("order_num")->where(["orderCode"=>$orderCode,"status"=>1])->where([["wsend_num",">=",0]])
  2650. ->select()->toArray();
  2651. if(empty($order)){
  2652. return error_show(1004,"未找到可以发货得采购单数据");
  2653. }
  2654. Db::startTrans();
  2655. try {
  2656. $addrlst =[
  2657. "orderCode"=>$orderCode,
  2658. "addr"=>$addr,
  2659. "addr_code"=>json_encode($addr_code),
  2660. "contactor"=>$contactor,
  2661. "mobile"=>$mobile,
  2662. "customer_code"=>$der['customer_code'],
  2663. "post_fee"=>$der['post_fee'],
  2664. "arrive_time"=>$sendtime,
  2665. "receipt_quantity"=>$send_num,
  2666. "is_del"=>0,
  2667. "addtime"=>date("Y-m-d H:i:s"),
  2668. "updatetime"=>date("Y-m-d H:i:s")
  2669. ];
  2670. $addrid = Db::name("order_addr")->insert($addrlst,true);
  2671. if($addrid>0){
  2672. $der['send_num'] +=$send_num;
  2673. $der['wsend_num'] -= $send_num;
  2674. $der['send_status'] = $der['send_num'] == 0 ? 1 : $der['wsend_num'] == 0 ? 3 : 2;
  2675. $der['status'] = $der['send_num'] == 0 ? 3 : $der['wsend_num'] == 0 ? 5 : 4;
  2676. $der['updatetime'] = date("Y-m-d H:i:s");
  2677. $si = Db::name('sale')->save($der);
  2678. if($si==false){
  2679. Db::rollback();
  2680. return error_show(1002,"出库单新建失败");
  2681. }
  2682. $num = $send_num;
  2683. $data=[];
  2684. foreach ($order as $val){
  2685. $outCode = makeNo("DF");
  2686. $cgd =Db::name("purchease_order")->where(["cgdNo"=>$val['cgdNo']])->find();
  2687. $tep=[];
  2688. $ornum=0;
  2689. if($val['wsend_num']>=$num){
  2690. $val['wsend_num']-=$num;
  2691. $val['send_num']+=$num;
  2692. $ornum =$num;
  2693. $num=0;
  2694. }else{
  2695. $num-=$val['wsend_num'];
  2696. $val['send_num']+=$val['wsend_num'];
  2697. $ornum =$val['wsend_num'];
  2698. $val['wsend_num']=0;
  2699. }
  2700. $or =Db::name("order_num")->save($val);
  2701. if($or==false){
  2702. Db::rollback();
  2703. return error_show(1002,"发货地址添加创建失败");
  2704. }
  2705. $tep=[
  2706. "cgdNo"=>$val['cgdNo'],
  2707. "outCode"=>$outCode,
  2708. "send_num"=>$ornum,
  2709. "status"=>1,
  2710. "addtime"=>date("Y-m-d H:i:s"),
  2711. "updatetime"=>date("Y-m-d H:i:s")
  2712. ];
  2713. $sen=Db::name("order_send")->save($tep);
  2714. if($sen==false){
  2715. Db::rollback();
  2716. return error_show(1002,"发货地址添加创建失败");
  2717. }
  2718. $data[] = [
  2719. "wsm_code" => isset($cgd['wsm_code'])? $cgd['wsm_code']:"",
  2720. "orderCode" => $orderCode,
  2721. "outCode" => $outCode,
  2722. "order_type" => $der['order_type'],
  2723. "apply_id" => $rm,
  2724. "apply_name" => $ri,
  2725. "addrid" => $addrid,
  2726. "post_name" => "",
  2727. "post_code" =>"",
  2728. "post_fee" => "",
  2729. "sendtime" => $sendtime,
  2730. "send_num" => $ornum,
  2731. "check_num" => 0,
  2732. "error_num" => 0,
  2733. "status" => 1,
  2734. "addtime" => date("Y-m-d H:i:s"),
  2735. "updatetime" => date("Y-m-d H:i:s")
  2736. ];
  2737. //维护商品所在仓库的库存 --- start
  2738. $temp = Db::name('wsm_good_stock')
  2739. ->field('id,usable_stock,wait_out_stock')
  2740. ->where(['spuCode' => $der['spuCode'], 'wsm_code' => $cgd['wsm_code']])
  2741. ->find();
  2742. if ($temp == false) {
  2743. Db::rollback();
  2744. return error_show(1004, '库存不足');
  2745. }
  2746. if (($temp['usable_stock'] - $ornum) < 0) {
  2747. Db::rollback();
  2748. return error_show(1004, '库存不足');
  2749. }
  2750. $up =Db::name('wsm_good_stock')
  2751. ->where('id', $temp['id'])
  2752. ->update([
  2753. 'usable_stock' => $temp['usable_stock'] - $ornum,
  2754. 'wait_out_stock' => $temp['wait_out_stock'] + $ornum,
  2755. 'updatetime' => date('Y-m-d H:i:s'),
  2756. ]);
  2757. if($up==false){
  2758. Db::rollback();
  2759. return error_show(1004, '库存更新失败');
  2760. }
  2761. if($num==0){
  2762. break;
  2763. }
  2764. }
  2765. $datainfo = Db::name('order_out')->insertAll($data);
  2766. if($datainfo>0){
  2767. Db::commit();
  2768. return app_show(0,"出库单新建成功");
  2769. }
  2770. }
  2771. Db::rollback();
  2772. return error_show(1004,"出库单新建失败");
  2773. } catch (Exception $e) {
  2774. Db::rollback();
  2775. return error_show(1005, $e->getMessage());
  2776. }
  2777. }
  2778. public function outdel(){
  2779. }
  2780. public function outSend(){
  2781. $outCode=isset($this->post['outCode'])&& $this->post['outCode']!=""? trim($this->post['outCode']):"";
  2782. if($outCode==""){
  2783. return error_show(1004,"参数outCode不能为空");
  2784. }
  2785. $outinfo = Db::name("order_out")->where(["outCode"=>$outCode])->find();
  2786. if($outinfo==false){
  2787. return error_show(1004,"发货数据未找到");
  2788. }
  2789. $einfo = Db::name('sale')->where(['orderCode' => $outinfo['orderCode'], 'is_del' => 0])->find();
  2790. if (empty($einfo)) {
  2791. return error_show(1002, "未找到销售订单数据");
  2792. }
  2793. $post_name = isset($this->post['post_name']) &&$this->post['post_name']!="" ? trim($this->post['post_name']):"";
  2794. if($post_name==""){
  2795. return error_show(1004,"参数post_name不能为空");
  2796. }
  2797. $post_code = isset($this->post['post_code']) &&$this->post['post_code']!="" ? trim($this->post['post_code']):"";
  2798. if($post_code==""){
  2799. return error_show(1004,"参数post_code不能为空");
  2800. }
  2801. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee']!="" ? floatval($this->post['post_fee']):"";
  2802. $outinfo['post_name'] = $post_name;
  2803. $outinfo['post_code'] = $post_code;
  2804. $outinfo['post_fee'] = $post_fee;
  2805. $outinfo['sendtime'] = date("Y-m-d H:i:s");
  2806. $outinfo['status'] = 2;
  2807. $outinfo['updatetime'] = date("Y-m-d H:i:s");
  2808. Db::startTrans();
  2809. try{
  2810. $up =Db::name("order_out")->save($outinfo);
  2811. if($up){
  2812. $stokc =Db::name("good_stock")->where(['spuCode'=>$einfo['good_code'],"wsm_code"=>$outinfo['wsm_code'],"is_del"=>0])->find();
  2813. if($stokc==false){
  2814. Db::rollback();
  2815. return error_show(1002, "未找库存到数据");
  2816. }else{
  2817. if ($outinfo['send_num'] > $stokc['wait_out_stock']) {
  2818. Db::rollback();
  2819. return error_show(1002, "超出库存数量");
  2820. }
  2821. $stokc['wait_out_stock']-=$outinfo['send_num'];
  2822. $stokc['intra_stock']+=$outinfo['send_num'];
  2823. $stokc['updatetime']=date("Y-m-d H:i:s");
  2824. }
  2825. $stoc= Db::name("good_stock")->save($stokc);
  2826. if($stoc==false){
  2827. Db::rollback();
  2828. return error_show(1002, "库存更新失败");
  2829. }
  2830. Db::commit();
  2831. return app_show(0,"发货成功");
  2832. }else{
  2833. Db::rollback();
  2834. return error_show(1004,"发货失败");
  2835. }
  2836. }catch (\Exception $e){
  2837. Db::rollback();
  2838. return error_show(1004,$e->getMessage());
  2839. }
  2840. }
  2841. public function RelaCgd($outinfo){
  2842. $der = Db::name('sale')->where(['orderCode' => $outinfo['orderCode']])->find();
  2843. if ($der == "") {
  2844. return false;
  2845. }
  2846. if($der['is_stock']==1){
  2847. $cgd = Db::name("order_bk")->where([["spuCode","=",$der['spuCode']],["is_del","=",0],["balance_num",">",0]])
  2848. ->select()->toArray();
  2849. if(empty($cgd)){
  2850. return false;
  2851. }
  2852. $send =$outinfo['send_num'];
  2853. $outsend=[];
  2854. foreach ($cgd as $value){
  2855. $temp=[];
  2856. if($value['balance_num']>=$send){
  2857. $value['balance_num']-=$send;
  2858. $value['merge_num']+=$send;
  2859. $send=0;
  2860. $temp['send_num'] =$send;
  2861. }else{
  2862. $value['balance_num'] =0;
  2863. $value['merge_num']+=$value['balance_num'];
  2864. $send -= $value['balance_num'];
  2865. $temp['send_num'] =$value['balance_num'];
  2866. }
  2867. $value['status'] = $value['balance_num']==0 ?2:$value['merge_num']==0?0:1 ;
  2868. $value['updatetime'] = date("Y-m-d H:i:s");
  2869. $bk = Db::name("order_bk")->save($value);
  2870. if($bk==false){
  2871. return false;
  2872. }
  2873. $temp['cgdNo'] = $value['cgdNo'];
  2874. $temp['outCode'] = $outinfo['outCode'];
  2875. $temp['status'] = 1;
  2876. $temp['addtime']=date("Y-m-d H:i:s");
  2877. $temp['updatetime']=date("Y-m-d H:i:s");
  2878. $outsend[]=$temp;
  2879. $order = Db::name("order_num")->where(["orderCode"=>$der['orderCode'],"cgdNo"=>$value['cgdNo'],"spuCode"=>$der['spuCode'],"status"=>1])->find();
  2880. if($order==false){
  2881. $order=[
  2882. "orderCode"=>$der['orderCode'],
  2883. "cgdNo"=>$value['cgdNo'],
  2884. "spuCode"=>$der['spuCode'],
  2885. "good_num"=>$der['good_num'],
  2886. "wsend_num"=>$der['good_num']-$temp['send_num'],
  2887. "send_num"=>$temp['send_num'],
  2888. "status"=>1,
  2889. "source"=>1
  2890. ];
  2891. }else{
  2892. $order['wsend_num']-=$temp['send_num'];
  2893. $order['send_num']+=$temp['send_num'];
  2894. }
  2895. if($order['wsend_num']<0){
  2896. return false;
  2897. }
  2898. $up =Db::name("order_num")->save($order);
  2899. if($up==false){
  2900. return false;
  2901. }
  2902. if($send==0){
  2903. break;
  2904. }
  2905. }
  2906. if(empty($temp)){
  2907. return false;
  2908. }
  2909. $insert = Db::name("order_send")->insertAll($outsend);
  2910. if($insert){
  2911. return true;
  2912. }else{
  2913. return false;
  2914. }
  2915. }else{
  2916. $order = Db::name("order_num")->where(["orderCode"=>$der['orderCode'],"spuCode"=>$der['spuCode'],
  2917. "status"=>1])->find();
  2918. if($order==false){
  2919. return false;
  2920. }else{
  2921. if($outinfo['send_num']> $order['wsend_num']){
  2922. return false;
  2923. }
  2924. $order['wsend_num']-=$outinfo['send_num'];
  2925. $order['send_num']+=$outinfo['send_num'];
  2926. if($order['wsend_num']<0){
  2927. return false;
  2928. }
  2929. $up =Db::name("order_num")->save($order);
  2930. if($up==false){
  2931. return false;
  2932. }
  2933. $temp=[];
  2934. $temp['cgdNo'] = $order['cgdNo'];
  2935. $temp['outCode'] = $outinfo['outCode'];
  2936. $temp['send_num'] =$outinfo['send_num'];
  2937. $temp['status'] = 1;
  2938. $temp['addtime']=date("Y-m-d H:i:s");
  2939. $temp['updatetime']=date("Y-m-d H:i:s");
  2940. $insert = Db::name("order_send")->insert($temp);
  2941. if($insert){
  2942. return true;
  2943. }else{
  2944. return false;
  2945. }
  2946. }
  2947. }
  2948. }
  2949. public function addother(){
  2950. $orderCode =isset($this->post['orderCode']) && $this->post['orderCode']!=""?trim($this->post['orderCode']):"";
  2951. if($orderCode==""){
  2952. return error_show(1004,"参数orderCode不能为空");
  2953. }
  2954. $order =Db::name("sale")->where(["orderCode"=>$orderCode,"is_del"=>0])->find();
  2955. if($order==false){
  2956. return error_show(1004,"未找到订单数据");
  2957. }
  2958. $paytime =isset($this->post['paytime'])&&$this->post['paytime']!=""? $this->post['paytime']:"";
  2959. $workNo =isset($this->post['workNo'])&&$this->post['workNo']!=""? trim($this->post['workNo']):"";
  2960. $platform_order =isset($this->post['platform_order'])&&$this->post['platform_order']!=""? trim($this->post['platform_order']):"";
  2961. $paytime==""?"":$order['paytime']=$paytime;
  2962. $order['workNo']=$workNo;
  2963. $order['platform_order']=$platform_order;
  2964. $order['updatetime']=date("Y-m-d H:i:s");
  2965. $sa=Db::name("sale")->save($order);
  2966. if($sa){
  2967. return app_show(0,"更新成功");
  2968. }else{
  2969. return error_show(1004,"更新失败");
  2970. }
  2971. }
  2972. public function getPrice()
  2973. {
  2974. $skuCode = isset($this->post['skuCode']) && $this->post['skuCode'] != "" ? trim($this->post['skuCode']) : "";
  2975. if ($skuCode === "") {
  2976. return error_show(1003, "参数skuCode不能为空");
  2977. }
  2978. $is_activity = isset($this->post['is_activity']) && $this->post['is_activity'] !== "" ? intval($this->post['is_activity'])
  2979. : "";
  2980. if ($is_activity === "") {
  2981. return error_show(1003, "参数is_activity不能为空");
  2982. }
  2983. $sale_num = isset($this->post['sale_num']) && $this->post['sale_num'] !== "" ? intval($this->post['sale_num']) : "";
  2984. if ($sale_num === "") {
  2985. return error_show(1003, "参数sale_num不能为空");
  2986. }
  2987. $ct = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  2988. ->where(['a.skuCode' => $skuCode])->find();
  2989. if ($ct == false) {
  2990. return error_show(1002, "未找到商品数据");
  2991. }
  2992. $stock = 0;
  2993. if ($ct['is_stock'] == 1) {
  2994. $good_stock = Db::name("good_stock")->where(["spuCode" => $ct['spuCode'], "is_del" => 0])->find();
  2995. $stock = isset($good_stock['usable_stock']) ? $good_stock['usable_stock'] : "0";
  2996. }
  2997. if ($is_activity == 1) {
  2998. $act = Db::name("activity_info")->alias("a")->leftJoin("good_activity b", "a.activity_code=b.activity_code")
  2999. ->where(["a.skuCode" => $skuCode, "a.is_del" => 0, "a.status" => 1, "b.status" => 6, "b.is_del" => 0])->find();
  3000. if ($act == false) {
  3001. return error_show(1003, "未找到相关活动价");
  3002. }
  3003. if ($act['moq_num'] > $sale_num) {
  3004. return error_show(1003, "商品不满足活动价起订量{$act['moq_num']}");
  3005. }
  3006. if ($act['activity_stock'] < $sale_num) {
  3007. return error_show(1003, "商品活动库存剩余{$act['activity_stock']}");
  3008. }
  3009. return app_show(0, "获取成功", ['sale_price' => $act['activity_price'], "stock" => $act['activity_stock']]);
  3010. } else {
  3011. if ($ct['is_stock'] == 0) {
  3012. //good_nake 成本阶梯 good_ladder销售阶梯
  3013. $origin = Db::name("good_nake")->where([["spuCode", "=", $ct['spuCode']], ["is_del", "=", 0]])->order("min_num asc")->find();
  3014. if (!$origin) {
  3015. return error_show(1003, "没有找到成本数据");
  3016. }
  3017. }
  3018. $good = Db::name("good_ladder")->where(["skuCode" => $skuCode, "is_del" => 0, "status" => 1])->order("min_num asc")->find();
  3019. if (!$good) {
  3020. return error_show(1003, "未找到相关阶梯价格");
  3021. }
  3022. if(!isset($origin['min_num'])) $origin['min_num']=0;
  3023. $lastnum = max($origin['min_num']??0, $good['min_num']);
  3024. if ($sale_num < $lastnum) {
  3025. return error_show(1003, "商品不满足起订量{$lastnum}");
  3026. }
  3027. $good_temp = Db::name("good_ladder")->where(["skuCode" => $skuCode, "is_del" => 0, "status" => 1])->where('min_num', '<=', $sale_num)->order("min_num desc")->find();
  3028. if (!$good_temp) {
  3029. return error_show(1003, "未找到相关阶梯价格");
  3030. }
  3031. return app_show(0, "获取成功", ['sale_price' => $good_temp['sale_price'], "stock" => $stock]);
  3032. }
  3033. }
  3034. public function saleuse(){
  3035. $orderCode= isset($this->post['orderCode'])&&!empty($this->post['orderCode'])?$this->post['orderCode']:"";
  3036. if($orderCode==""){
  3037. return error_show(1003,"参数orderCode不能为空");
  3038. }
  3039. $order= Db::name("sale")->where(["orderCode"=>$orderCode,"is_del"=>0])->select()->toArray();
  3040. if(empty($order)){
  3041. return error_show(1003,"订单数据未找到");
  3042. }
  3043. $useid= isset($this->post['useid'])&&$this->post['useid']!=""?intval($this->post['useid']):"";
  3044. if($useid==""){
  3045. return error_show(1003,"参数useid不能为空");
  3046. }
  3047. $use =Db::name("order_use")->where(["id"=>$useid,"is_del"=>0])->find();
  3048. if($use==false){
  3049. return error_show(1003,"用途数据未找到");
  3050. }
  3051. $asve=['use_order'=>$useid,"updatetime"=>date("Y-m-d H:i:s")];
  3052. $up=Db::name("sale")->where(["orderCode"=>$orderCode,"is_del"=>0])->update($asve);
  3053. if($up>0){
  3054. return app_show(0,"更新成功");
  3055. }else{
  3056. return error_show(1003,"更新失败");
  3057. }
  3058. }
  3059. public function goodzxinfo(){
  3060. $orderCode= isset($this->post['spuCode'])&&$this->post['spuCode']!=''?trim($this->post['spuCode']):"";
  3061. if($orderCode==""){
  3062. return error_show(1003,"参数spuCode不能为空");
  3063. }
  3064. $order= Db::name("good_zixun")->where(["spuCode"=>$orderCode,"is_del"=>0])->find();
  3065. if(empty($order)){
  3066. return error_show(1003,"订单数据未找到");
  3067. }
  3068. $unit =Db::name("unit")->where(["id"=>$order['good_unit']])->find();
  3069. if($order['brand_id']!=0){
  3070. $brand=Db::name("brand")->where(["id"=>$order['brand_id']])->find();
  3071. $order["brand_name"]=isset($brand['brand_name'])?$brand['brand_name']:"";
  3072. }else{
  3073. $order["brand_name"]="";
  3074. $order["brand_id"]="";
  3075. }
  3076. $order['specinfo']=json_decode($order['specinfo'],true);
  3077. $order['unit'] = isset($unit['unit'])?$unit['unit']:'';
  3078. $order['cat_info'] = made($order['cat_id'],[]);
  3079. $order['noble_name']=isset($order['noble_metal'])&&$order['noble_metal']!=0?$this->noble[$order['noble_metal']] :"";
  3080. if($order['is_gold_price']==1){
  3081. $price=Db::name("gold_price1")->where(["type"=>$order['noble_metal'],"status"=>1,"is_del"=>0])->order("addtime desc")->find();
  3082. }
  3083. $supplier = Db::name("supplier")->where(["code"=>$order['supplierNo']])->find();
  3084. $order['supplier_name'] = isset($supplier['name']) ? $supplier['name']:"";
  3085. $palt =Db::name("platform")->where(["id"=>$order['platform_id']])->find();
  3086. $order['platform_name']=isset($palt['platform_name']) ? $palt['platform_name']:"";
  3087. $order["gold_price"] = isset($price['price'])?$price['price']:0;
  3088. $order["noble_weight"] = isset($order['weight'])?$order['weight']:0;
  3089. return app_show(0,"获取成功",$order);
  3090. }
  3091. //获取采反商品详情,当数据不在good_zixun中,模仿goodzxinfo
  3092. public function getGoodZxInfoByNotZixun(){
  3093. $param=$this->request->only(['spuCode'],'post','trim');
  3094. $val=Validate::rule(['spuCode'=>'require']);
  3095. if(!$val->check($param)) return error_show(1004,$val->getError());
  3096. $order = Db::name('consult_bids')
  3097. ->field(true)
  3098. ->where(['spuCode'=>$param['spuCode'],'is_del'=>0])
  3099. ->find();
  3100. // $orderCode= isset($this->post['spuCode'])&&$this->post['spuCode']!=''?trim($this->post['spuCode']):"";
  3101. // if($orderCode==""){
  3102. // return error_show(1003,"参数spuCode不能为空");
  3103. // }
  3104. // $order= Db::name("good_zixun")->where(["spuCode"=>$orderCode,"is_del"=>0])->find();
  3105. // if(empty($order)){
  3106. // return error_show(1003,"订单数据未找到");
  3107. // }
  3108. $unit =Db::name("unit")->where(["id"=>$order['unit_id']])->find();
  3109. if($order['brand_id']!=0){
  3110. $brand=Db::name("brand")->where(["id"=>$order['brand_id']])->find();
  3111. $order["brand_name"]=isset($brand['brand_name'])?$brand['brand_name']:"";
  3112. }else{
  3113. $order["brand_name"]="";
  3114. $order["brand_id"]="";
  3115. }
  3116. $order['specinfo']=json_decode($order['specinfo'],true);
  3117. $order['unit'] = isset($unit['unit'])?$unit['unit']:'';
  3118. $order['cat_info'] = made($order['cat_id'],[]);
  3119. $order['noble_name']=isset($order['noble_metal'])&&$order['noble_metal']!=0?$this->noble[$order['noble_metal']] :"";
  3120. if($order['is_gold_price']==1){
  3121. $price=Db::name("gold_price1")->where(["type"=>$order['noble_metal'],"status"=>1,"is_del"=>0])->order("addtime desc")->find();
  3122. }
  3123. $supplier = Db::name("supplier")->where(["code"=>$order['supplierNo']])->find();
  3124. $order['supplier_name'] = isset($supplier['name']) ? $supplier['name']:"";
  3125. // $palt =Db::name("platform")->where(["id"=>$order['platform_id']])->find();
  3126. // $order['platform_name']=isset($palt['platform_name']) ? $palt['platform_name']:"";
  3127. $order["gold_price"] = isset($price['price'])?$price['price']:0;
  3128. $order["noble_weight"] = isset($order['weight'])?$order['weight']:0;
  3129. return app_show(0,"获取成功",$order);
  3130. }
  3131. }