PurchaseLogic.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\BaseGoods;
  4. use app\common\model\BaseGoodsItem;
  5. use app\common\model\BaseGoodsParam;
  6. use app\common\model\Depot;
  7. use app\common\model\DepotGoods;
  8. use app\common\model\DepotGoodsItem;
  9. use app\common\model\DepotOrder;
  10. use app\common\model\DepotOrderItem;
  11. use app\common\model\GoodsParam;
  12. use app\common\model\OfflineOrder;
  13. use app\common\model\OfflineOrderRefund;
  14. use app\common\model\StoreGoods;
  15. use app\common\model\StoreGoodsItem;
  16. use app\common\model\SupplierOrder;
  17. use app\common\model\SupplierOrderRefund;
  18. use app\common\model\SupplierRelevancyOrder;
  19. use library\tools\Data;
  20. use think\Db;
  21. use think\Exception;
  22. /**
  23. * 供货管理
  24. * Class PurchaseLogic
  25. */
  26. class PurchaseLogic
  27. {
  28. public static $ret = ['code'=>200,'msg'=>'数据保存成功!'];
  29. // 采购订单审核通过后管理供货商订单
  30. public static function relevancyOrder($order_id)
  31. {
  32. $order_info = SupplierOrder::where('id',$order_id)->find()->toArray();
  33. $relevancy = [
  34. 'order_id'=>$order_id,
  35. 'supplier_id'=>$order_info['supplier_id'],
  36. 'order_money'=>$order_info['order_money'],
  37. ];
  38. Data::save('SupplierRelevancyOrder',$relevancy,'order_id',['order_id'=>$order_id]);
  39. return true;
  40. }
  41. // 仓库商品上线售卖
  42. public static function deoptGoodsOnline($deopt_goods)
  43. {
  44. // 仓库商品详情
  45. $ret = ['code'=>200,'msg'=>''];
  46. $deopt_info = DepotGoods::with('itemList')->where('id',$deopt_goods)->find()->toArray();
  47. if($deopt_info['online_id']) return $ret;
  48. $no_field = ['id','create_at'];
  49. // 商品基础信息
  50. $base_goods = BaseGoods::where('id',$deopt_info['goods_id'])->find()->toArray();
  51. $base_goods['depot_goods'] = $deopt_info['id'];
  52. $base_goods['base_id'] = $base_goods['id'];
  53. $base_goods['depot_id'] = $deopt_info['depot_id'];
  54. foreach ($no_field as $field){
  55. unset($base_goods[$field]);
  56. }
  57. $base_param = BaseGoodsParam::where('goods_id',$deopt_goods)->value('goods_param');
  58. Db::startTrans();
  59. try {
  60. $line_goods = StoreGoods::create($base_goods,true);// 添加到线上商城
  61. if($base_param) GoodsParam::create(['goods_id'=>$line_goods->id,'goods_param'=>$base_param]);
  62. DepotGoods::where('id',$deopt_goods)->update(['online_id'=>$line_goods->id]);
  63. array_walk($deopt_info['item_list'],function (&$v,$k)use($deopt_info,$line_goods){
  64. $base_item = BaseGoodsItem::where('id',$v['spec_id'])->find()->toArray();
  65. $store_item = [
  66. 'goods_id' => $line_goods->id,
  67. 'goods_spec' => $v['goods_spec'],
  68. 'spec_title' => $base_item['spec_title'],
  69. 'goods_no' => $base_item['goods_no'],
  70. 'cover' => $base_item['cover'],
  71. 'stock' => $v['stock'],
  72. 'base_stock' => $v['base_stock'],
  73. 'original_price' => $base_item['original_price'],
  74. 'sell_price' => $base_item['sell_price'],
  75. 'virtual' => $base_item['virtual'],
  76. 'weight' => $base_item['weight'],
  77. 'depot_item' => $v['id'],
  78. ];
  79. StoreGoodsItem::create($store_item,true);
  80. });
  81. Db::commit();
  82. }catch (\Exception $e){
  83. $ret['code'] = 201;
  84. $ret['msg'] = $e->getMessage();
  85. Db::rollback();
  86. }
  87. return $ret;
  88. }
  89. // 供货订单入库
  90. public static function supplierOrderDepot($order_id,$depot_id,$admin_user){
  91. $supplier_order = SupplierOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
  92. $check_depot = DepotGoods::where(['goods_id'=>$supplier_order['goods_id'],'depot_id'=>$depot_id])->find();
  93. $ret = ['code'=>200,'msg'=>'入库成功'];
  94. if($supplier_order['depot_user']) return ['code'=>201,'msg'=>'商品已入库'];
  95. if(!$check_depot) return ['code'=>201,'msg'=>'商品没有添加到该仓库'];
  96. //var_dump($supplier_order,$check_depot);die();
  97. // 商品入库
  98. Db::startTrans();
  99. try {
  100. list($depot_info,$stock_info) = [[],[]];
  101. foreach ($supplier_order['order_item'] as $iv) {
  102. if($iv['num'] == 0) continue;
  103. //仓库规格添加【仓库】
  104. DepotGoodsItem::where(['depot_id'=>$depot_id,'goods_id'=>$check_depot->id,'spec_id'=>$iv['spec_id']])->update(['stock'=>Db::raw('stock +'.$iv['num']),'base_stock'=>Db::raw('base_stock +'.$iv['num'])]);
  105. //规格库存添加【上商城商品】
  106. if($check_depot->online_id){
  107. $depot_item = DepotGoodsItem::where(['goods_id'=>$check_depot->id,'depot_id'=>$depot_id,'spec_id'=>$iv['spec_id']])->value('id');
  108. StoreGoodsItem::where(['goods_id'=>$check_depot->online_id,'depot_item'=>$depot_item])->update(['stock'=>Db::raw('stock +'.$iv['num']),'base_stock'=>Db::raw('base_stock +'.$iv['num'])]);
  109. // 商品库存【商城】
  110. array_push($stock_info, [
  111. 'goods_id' => $check_depot->online_id,
  112. 'goods_spec' => $iv['goods_spec'],
  113. 'number_stock' => $iv['num'],
  114. ]);
  115. }
  116. // 仓库入库记录
  117. array_push($depot_info, [
  118. 'depot_id' => $depot_id,
  119. 'order_id' => $order_id,
  120. 'goods_id' => $supplier_order['goods_id'],
  121. 'spec_id' => $iv['spec_id'],
  122. 'goods_spec' => $iv['goods_spec'],
  123. 'num' => $iv['num'],
  124. 'remark' => '商品入库',
  125. 'type' => 1,
  126. ]);
  127. }
  128. // 线上商城总库存【不一定用,先更新】
  129. if($check_depot->online_id) {
  130. $total_num = array_sum(array_column($supplier_order['order_item'],'num'));
  131. StoreGoods::where(['id'=>$check_depot->online_id])->update(['stock'=>Db::raw('stock +'.$total_num),'base_stock'=>Db::raw('base_stock +'.$total_num)]);
  132. }
  133. // 修改入库订单
  134. SupplierOrder::where('id',$order_id)->update(['depot_user'=>$admin_user,'depot_status'=>1,'depot_id'=>$depot_id]);
  135. // 库存记录
  136. if(!empty($stock_info)) Db::name('GoodsStock')->insertAll($stock_info);
  137. if(!empty($depot_info)) Db::name('DepotGoodsInfo')->insertAll($depot_info);
  138. Db::commit();
  139. }catch (\Exception $e) {
  140. $ret['code'] = 202;
  141. $ret['msg'] = $e->getMessage();
  142. Db::rollback();
  143. }
  144. return $ret;
  145. }
  146. // 供货订单申请退款
  147. public static function supplierOrderApplyRefund($order_id,$reason)
  148. {
  149. $ret = ['code'=>200,'msg'=>'ok'];
  150. $order_info = SupplierOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
  151. if(in_array($order_info['refund_status'],[1,2])) return ['code'=>201,'msg'=>'订单已申请退款'];
  152. Db::startTrans();
  153. try {
  154. $refund_info = [
  155. 'order_id' => $order_id,
  156. 'reason' => $reason,
  157. 'order_num' => $order_info['order_num'],
  158. 'supplier_id' => $order_info['supplier_id'],
  159. 'order_money' => $order_info['order_money'],
  160. 'goods_id' => $order_info['goods_id'],
  161. 'sh_status' => 0,
  162. ];
  163. Data::save('SupplierOrderRefund',$refund_info,'order_id',['order_id'=>$order_id]);// 退款记录
  164. SupplierOrder::where('id',$order_id)->update(['refund_status'=>1]);//关联订单
  165. SupplierRelevancyOrder::where(['order_id'=>$order_id])->update(['refund_status'=>1]);// 供货订单
  166. Db::commit();
  167. }catch (\Exception $e){
  168. $ret['code'] = 202;
  169. $ret['msg'] = $e->getMessage();
  170. Db::rollback();
  171. }
  172. return $ret;
  173. }
  174. // 供货订单退款申请审核
  175. public static function auditSupplierRefund($order_id,$sh_status,$remark)
  176. {
  177. $order_info = SupplierOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
  178. if(in_array($order_info['refund_status'],[2])) return ['code'=>201,'msg'=>'订单已退款,请勿重新审核'];
  179. Db::startTrans();
  180. try {
  181. SupplierOrder::where('id',$order_id)->update(['refund_status'=>$sh_status + 1]);// 订单退款状态
  182. SupplierRelevancyOrder::where('order_id',$order_id)->update(['refund_status'=>$sh_status + 1]);//关联订单退款状态
  183. SupplierOrderRefund::where('order_id',$order_id)->update(['sh_status'=>$sh_status,'remark'=>$remark,'sh_time'=>date('Y-m-d H:i:s')]);// 退款记录
  184. Db::commit();
  185. }catch (\Exception $e){
  186. self::$ret['code'] = 201;
  187. self::$ret['msg'] = $e->getMessage();
  188. Db::rollback();
  189. }
  190. return self::$ret;
  191. }
  192. // 供货订单关联商品出库【已入库订单需要出库记录】
  193. public static function depotRefund($order_id)
  194. {
  195. $order_info = SupplierOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
  196. if(in_array($order_info['refund_status'],[4])) return ['code'=>201,'msg'=>'已出库,请勿重新出库'];
  197. Db::startTrans();
  198. try {
  199. list($depot_info,$stock_info) = [[],[]];
  200. $check_depot = DepotGoods::where(['goods_id'=>$order_info['goods_id'],'depot_id'=>$order_info['depot_id']])->find();
  201. SupplierOrder::where('id',$order_id)->update(['refund_status'=>4]);// 订单退款状态
  202. SupplierOrderRefund::where('order_id',$order_id)->update(['sh_status'=>3]);// 退款记录
  203. foreach ($order_info['order_item'] as $iv) {
  204. if($iv['num'] == 0) continue;
  205. //仓库规格扣减
  206. DepotGoodsItem::where(['depot_id'=>$order_info['depot_id'],'goods_id'=>$check_depot->id,'spec_id'=>$iv['spec_id']])->update(['stock'=>Db::raw('stock -'.$iv['num']),'base_stock'=>Db::raw('base_stock -'.$iv['num'])]);
  207. //规格库存添加【上商城商品】
  208. if($check_depot->online_id){
  209. $depot_item = DepotGoodsItem::where(['goods_id'=>$check_depot->id,'depot_id'=>$order_info['depot_id'],'spec_id'=>$iv['spec_id']])->value('id');
  210. StoreGoodsItem::where(['goods_id'=>$check_depot->online_id,'depot_item'=>$depot_item])->update(['stock'=>Db::raw('stock +'.$iv['num']),'base_stock'=>Db::raw('base_stock +'.$iv['num'])]);
  211. // 商品库存【商城】
  212. array_push($stock_info, [
  213. 'goods_id' => $check_depot->online_id,
  214. 'goods_spec' => $iv['goods_spec'],
  215. 'number_stock' => -$iv['num'],
  216. ]);
  217. }
  218. // 仓库入库记录
  219. array_push($depot_info, [
  220. 'depot_id' => $order_info['depot_id'],
  221. 'order_id' => $order_id,
  222. 'goods_id' => $order_info['goods_id'],
  223. 'spec_id' => $iv['spec_id'],
  224. 'goods_spec' => $iv['goods_spec'],
  225. 'num' => -$iv['num'],
  226. 'remark' => '供货订单退款,商品入库',
  227. 'type' => 2,
  228. ]);
  229. }
  230. // 线上商城总库存【不一定用,先更新】
  231. if($check_depot->online_id) {
  232. $total_num = array_sum(array_column($order_info['order_item'],'num'));
  233. StoreGoods::where(['id'=>$check_depot->online_id])->update(['stock'=>Db::raw('stock +'.$total_num),'base_stock'=>Db::raw('base_stock +'.$total_num)]);
  234. }
  235. if(!empty($stock_info)) Db::name('GoodsStock')->insertAll($stock_info);
  236. if(!empty($depot_info)) Db::name('DepotGoodsInfo')->insertAll($depot_info);
  237. Db::commit();
  238. }catch (\Exception $e){
  239. self::$ret['code'] = 201;
  240. self::$ret['msg'] = $e->getMessage();
  241. Db::rollback();
  242. }
  243. return self::$ret;
  244. }
  245. // 仓库出库【线上商城订单】
  246. public static function depotOrderOutStock($order_id,$out_num,$remark)
  247. {
  248. $depot_order = DepotOrder::with('itemList')->where('id',$order_id)->find()->toArray();
  249. if($depot_order['out_num']) return ['code'=>201,'msg'=>'已出库'];
  250. Db::startTrans();
  251. try {
  252. list($depot_info) = [[]];
  253. DepotOrder::where(['id'=>$order_id])->update(['out_num'=>$out_num,'remark'=>$remark]);
  254. DepotOrderItem::where(['depot_order'=>$order_id])->update(['out_num'=>$out_num]);
  255. foreach ($depot_order['item_list'] as $item) {
  256. DepotGoodsItem::where('id',$item['spec_id'])->update(['stock'=>Db::raw('stock -'.$item['num'])]);
  257. $base_goods = BaseGoods::where('id',$item['goods_id'])->value('goods_id');
  258. $base_item = BaseGoodsItem::where('id',$item['spec_id'])->value('spec_id');
  259. array_push($depot_info, [
  260. 'depot_id' => $depot_order['depot_id'],
  261. 'order_id' => $depot_order['order_id'],
  262. 'goods_id' => $base_goods,
  263. 'spec_id' => $base_item,
  264. 'goods_spec' => $item['goods_spec'],
  265. 'num' => $item['num'],
  266. 'remark' => '商城订单出库',
  267. 'type' => 2,
  268. ]);
  269. }
  270. if(!empty($depot_info)) Db::name('DepotGoodsInfo')->insertAll($depot_info);
  271. Db::commit();
  272. }catch (\Exception $e){
  273. self::$ret['code'] = 201;
  274. self::$ret['msg'] = $e->getMessage();
  275. Db::rollback();
  276. }
  277. return self::$ret;
  278. }
  279. // 线下订单退款申请
  280. public static function offlineOrderRefund($order_id,$reason)
  281. {
  282. $ret = ['code'=>200,'msg'=>'ok'];
  283. $order_info = OfflineOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
  284. if(in_array($order_info['refund_status'],[1,2])) return ['code'=>201,'msg'=>'订单已申请退款'];
  285. Db::startTrans();
  286. try {
  287. $refund_info = [
  288. 'order_id' => $order_id,
  289. 'reason' => $reason,
  290. 'order_num' => $order_info['order_num'],
  291. 'order_money' => $order_info['order_money'],
  292. 'goods_id' => $order_info['goods_id'],
  293. 'sh_status' => 0,
  294. ];
  295. Data::save('OfflineOrderRefund',$refund_info,'order_id',['order_id'=>$order_id]);// 退款记录
  296. OfflineOrder::where('id',$order_id)->update(['refund_status'=>1]);//关联订单
  297. Db::commit();
  298. }catch (\Exception $e){
  299. $ret['code'] = 202;
  300. $ret['msg'] = $e->getMessage();
  301. Db::rollback();
  302. }
  303. return $ret;
  304. }
  305. // 线下订单退款申请审核
  306. public static function auditOfflineRefund($order_id,$sh_status,$remark)
  307. {
  308. $order_info = OfflineOrder::with('orderItem')->where('id',$order_id)->find()->toArray();
  309. if(in_array($order_info['refund_status'],[2])) return ['code'=>201,'msg'=>'订单已退款,请勿重新审核'];
  310. Db::startTrans();
  311. try {
  312. OfflineOrder::where('id',$order_id)->update(['refund_status'=>$sh_status + 1]);// 订单退款状态
  313. OfflineOrderRefund::where('order_id',$order_id)->update(['sh_status'=>$sh_status,'remark'=>$remark,'sh_time'=>date('Y-m-d H:i:s')]);// 退款记录
  314. Db::commit();
  315. }catch (\Exception $e){
  316. self::$ret['code'] = 201;
  317. self::$ret['msg'] = $e->getMessage();
  318. Db::rollback();
  319. }
  320. return self::$ret;
  321. }
  322. }