StatisticsService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Construction;
  4. use App\Model\ConstructionProductInfo;
  5. use App\Model\InOutRecord;
  6. use App\Model\InvoiceOrder;
  7. use App\Model\InvoiceOrderInfo;
  8. use App\Model\Product;
  9. use App\Model\ProductCategory;
  10. use App\Model\PurchaseOrder;
  11. use App\Model\PurchaseOrderInfo;
  12. use App\Model\ReturnExchangeOrder;
  13. use App\Model\ReturnExchangeOrderProductInfo;
  14. use App\Model\SalesOrder;
  15. use App\Model\SalesOrderProductInfo;
  16. use Illuminate\Support\Facades\DB;
  17. class StatisticsService extends Service
  18. {
  19. public function statisticsXs($data,$user){
  20. }
  21. public function inoutrecord(){
  22. $in_data = InvoiceOrder::where('del_time',0)
  23. ->select('id','sales_order_id')
  24. ->get()->toArray();
  25. $map = [];
  26. $s_p = SalesOrderProductInfo::whereIn('sales_order_id',array_column($in_data,'sales_order_id'))
  27. ->where('del_time',0)
  28. ->select('sales_order_id','product_id','final_amount','price')
  29. ->get()->toArray();
  30. foreach ($s_p as $value){
  31. $map[$value['sales_order_id']][] = [
  32. 'product_id' => $value['product_id'],
  33. 'final_amount' => $value['final_amount'],
  34. 'price' => $value['price'],
  35. ];
  36. }
  37. DB::beginTransaction();
  38. try {
  39. foreach ($in_data as $value){
  40. $tmp = $map[$value['sales_order_id']] ?? [];
  41. if(empty($tmp)) continue;
  42. foreach ($tmp as $val){
  43. InvoiceOrderInfo::where('del_time',0)
  44. ->where('invoice_id', $value['id'])
  45. ->where('product_id', $val['product_id'])
  46. ->update([
  47. 'final_amount' => $val['final_amount'],
  48. 'price' => $val['price'],
  49. ]);
  50. }
  51. }
  52. }catch (\Throwable $exception){
  53. DB::rollBack();
  54. dd($exception->getMessage());
  55. }
  56. DB::commit();
  57. dd(1);
  58. }
  59. public function inoutrecord2(){
  60. DB::beginTransaction();
  61. try {
  62. DB::table('in_out_record')
  63. ->where('price',0)
  64. ->whereIn('order_type', array_values(ReturnExchangeOrder::$prefix))
  65. ->select('id','order_number','product_id')
  66. ->orderBy('id','asc')
  67. ->chunk(200,function ($data) {;
  68. $data = Collect($data)->map(function ($object) {
  69. return (array)$object;
  70. })->toArray();
  71. $map = ReturnExchangeOrder::where('del_time',0)
  72. ->whereIn('order_number',array_column($data,'order_number'))
  73. ->pluck('id','order_number')
  74. ->toArray();
  75. $result = ReturnExchangeOrderProductInfo::where('del_time',0)
  76. ->whereIn('return_exchange_id',array_values($map))
  77. ->select('return_exchange_id','return_exchange_price','product_id')
  78. ->get()->toArray();
  79. $result_map = [];
  80. foreach ($result as $v){
  81. $result_map[$v['return_exchange_id']][$v['product_id']] = [
  82. 'product_id' => $v['product_id'],
  83. 'price' => $v['return_exchange_price'],
  84. ];
  85. }
  86. foreach ($data as $value){
  87. $tmp_id = $map[$value['order_number']] ?? 0;
  88. if($tmp_id < 0) continue;
  89. $tmp = $result_map[$tmp_id][$value['product_id']] ?? [];
  90. if(empty($tmp)) continue;
  91. InOutRecord::where('id',$value['id'])->update([
  92. 'price' => $tmp['price']
  93. ]);
  94. }
  95. });
  96. DB::commit();
  97. }catch (\Throwable $exception){
  98. DB::rollBack();
  99. dd($exception->getMessage());
  100. }
  101. dd(1);
  102. }
  103. public function statisticsJc($data,$user){
  104. if(empty($data['top_depart_id'])) return [false, '请选择门店'];
  105. $model = Product::ProductClear($user,$data);
  106. $model = $model->where('del_time',0)
  107. ->select('title','id','code','depart_id','top_depart_id','product_attribute')
  108. ->orderby('product_attribute', 'desc')
  109. ->orderby('id', 'desc');
  110. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  111. if(isset($data['state'])) $model->where('state', $data['state']);
  112. if(isset($data['is_use'])) $model->where('is_use', $data['is_use']);
  113. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  114. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  115. if(! empty($data['product_category'])) {
  116. $product_category = ProductCategory::where('del_time',0)
  117. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  118. ->select('id')
  119. ->get()->toArray();
  120. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  121. }
  122. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  123. $list = $this->limit($model,'',$data);
  124. $list = $this->fillData($list,$user,$data);
  125. return [true, $list];
  126. }
  127. public function fillData($data, $user, $search){
  128. if(empty($data['data'])) return $data;
  129. //产品
  130. $product = array_column($data,'id');
  131. //本月入库 本月出库
  132. list($in, $out) = $this->getThisMonthData($product,$user,$search);
  133. //上月结存
  134. $lastJc = $this->getLastMonthBalance($product,$user,$search);
  135. foreach ($data['data'] as $key => $value){
  136. $data['data'][$key]['in_number'] = $in[$value['id']] ?? 0;
  137. $data['data'][$key]['out_number'] = $out[$value['id']] ?? 0;
  138. $data['data'][$key]['last_jc_number'] = $lastJc[$value['id']] ?? 0;
  139. $data['data'][$key]['last_jc_number'] = $lastJc[$value['id']] ?? 0;
  140. }
  141. return $data;
  142. }
  143. //本月入库 出库
  144. public function getThisMonthData($product = [], $user = [], $search = []){
  145. $in = $out = [];
  146. $startStamp = strtotime(date("Y-m-01 00:00:00"));
  147. $endStamp = strtotime(date("Y-m-t 23:59:59"));
  148. //本月出和入的数据
  149. $model = InOutRecord::TopClear($user,$search);
  150. $list = $model->where('del_time',0)
  151. ->where('crt_time','>=',$startStamp)
  152. ->where('crt_time','<=',$endStamp)
  153. ->whereIn('product_id',$product)
  154. ->select('product_id','number')
  155. ->get()->toArray();
  156. foreach ($list as $value){
  157. if($value['number'] >= 0){
  158. if(isset($in[$value['product_id']])){
  159. $in[$value['product_id']] += $value['number'];
  160. }else{
  161. $in[$value['product_id']] = $value['number'];
  162. }
  163. }else{
  164. if(isset($out[$value['product_id']])){
  165. $out[$value['product_id']] += abs($value['number']);
  166. }else{
  167. $out[$value['product_id']] = abs($value['number']);
  168. }
  169. }
  170. }
  171. return [$in, $out];
  172. }
  173. //上月结存
  174. public function getLastMonthBalance($product = [], $user = [], $search = []){
  175. $return = [];
  176. $startStamp = strtotime(date("Y-m-01 00:00:00"));
  177. $model = InOutRecord::TopClear($user,$search);
  178. $list = $model->where('del_time',0)
  179. ->where('crt_time','<',$startStamp)
  180. ->whereIn('product_id',$product)
  181. ->select('product_id','number')
  182. ->get()->toArray();
  183. foreach ($list as $value){
  184. if(isset($return[$value['product_id']])){
  185. $return[$value['product_id']] += $value['number'];
  186. }else{
  187. $return[$value['product_id']] = $value['number'];
  188. }
  189. }
  190. return $return;
  191. }
  192. //本月入库
  193. public function getThisMonthIn1($product = [], $user = [], $search = []){
  194. $return = [];
  195. $startStamp = strtotime(date("Y-m-01 00:00:00"));
  196. $endStamp = strtotime(date("Y-m-t 23:59:59"));
  197. //本月采购单
  198. $model = PurchaseOrder::Clear($user,$search);
  199. $list = $model->where('del_time',0)
  200. ->where('state', PurchaseOrder::STATE_Four)
  201. ->where('crt_time','>=',$startStamp)
  202. ->where('crt_time','<=',$endStamp)
  203. ->select('id')
  204. ->get()->toArray();
  205. if(empty($list)) return $return;
  206. //本月采购产品
  207. $purchase_product_array = [];
  208. $purchase_product = PurchaseOrderInfo::where('del_time',0)
  209. ->whereIn('product_id',$product)
  210. ->whereIn('purchase_order_id',array_column($list,'id'))
  211. ->select('product_id','number','price')
  212. ->get()->toArray();
  213. foreach ($purchase_product as $value){
  214. $total = bcmul($value['number'],$value['price'],2);
  215. if(isset($purchase_product_array[$value['product_id']])){
  216. $purchase_product_array[$value['product_id']]['number'] += $value['number'];
  217. $total_tmp = bcadd($purchase_product_array[$value['product_id']]['total'], $total, 2);
  218. $purchase_product_array[$value['product_id']]['total'] = $total_tmp;
  219. }else{
  220. $purchase_product_array[$value['product_id']] = [
  221. 'number' => $value['number'],
  222. 'total' => $total,
  223. ];
  224. }
  225. }
  226. //本月退货(采购)
  227. $model2 = ReturnExchangeOrder::Clear($user, $search);
  228. $return_list = $model2->where('del_time',0)
  229. ->where('state', ReturnExchangeOrder::State_two)
  230. ->where('type',ReturnExchangeOrder::Order_type2)
  231. ->where('crt_time','>=',$startStamp)
  232. ->where('crt_time','<=',$endStamp)
  233. ->select('id')
  234. ->get()->toArray();
  235. //本月退货产品
  236. $return_product_array = [];
  237. if(! empty($return_list)){
  238. $return_product = ReturnExchangeOrderProductInfo::where('del_time',0)
  239. ->where('return_exchange_id', array_column($return_list, 'id'))
  240. ->whereIn('product_id',$product)
  241. ->where('return_or_exchange',ReturnExchangeOrderProductInfo::type_one)
  242. ->select('product_id','number','return_or_exchange')
  243. ->get()->toArray();
  244. foreach ($return_product as $value){
  245. $total = bcmul($value['number'],$value['return_or_exchange'],2);
  246. if(isset($return_product_array[$value['product_id']])){
  247. $return_product_array[$value['product_id']]['number'] += $value['number'];
  248. $total_tmp = bcadd($return_product_array[$value['product_id']]['total'], $total, 2);
  249. $return_product_array[$value['product_id']]['total'] = $total_tmp;
  250. }else{
  251. $return_product_array[$value['product_id']] = [
  252. 'number' => $value['number'],
  253. 'total' => $total,
  254. ];
  255. }
  256. }
  257. }
  258. foreach ($return_product_array as $p => $n){
  259. $number_tmp = -$n['number'];
  260. $total_tmp = -$n['total'];
  261. $purchase_product_tmp = $purchase_product_array[$p] ?? [];
  262. if(empty($purchase_product_tmp)){
  263. $purchase_product_array[$p] = [
  264. 'number' => $number_tmp,
  265. 'total' => $total_tmp,
  266. ];
  267. }else{
  268. $purchase_product_array[$p]['number'] += $number_tmp;
  269. $total_tmp2 = bcadd($purchase_product_array[$p]['total'], $total_tmp, 2);
  270. $purchase_product_array[$p]['total'] += $total_tmp2;
  271. }
  272. }
  273. return $purchase_product_array;
  274. }
  275. public function getThisMonthOut($product = [], $user = [], $search = []){
  276. }
  277. }