StatSaleClass.php 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  1. <?php
  2. namespace bizGhs\stat\classes;
  3. use biz\stat\classes\StatCgClass;
  4. use biz\stat\classes\StatStockInClass;
  5. use biz\stat\classes\StatStockOutClass;
  6. use bizGhs\cg\classes\CgRefundClass;
  7. use bizGhs\custom\classes\CustomClass;
  8. use bizGhs\expend\classes\ExpendClass;
  9. use bizGhs\item\classes\ItemClassClass;
  10. use bizGhs\order\classes\OrderItemClass;
  11. use bizGhs\order\classes\PurchaseOrderClass;
  12. use bizGhs\order\classes\PurchaseOrderItemClass;
  13. use bizGhs\order\classes\RefundOrderClass;
  14. use bizGhs\order\classes\StockWastageOrderClass;
  15. use bizGhs\product\classes\ProductClass;
  16. use bizGhs\staff\classes\SalaryClass;
  17. use bizHd\goods\classes\PlantCgClass;
  18. use bizHd\order\classes\OrderClass as HdOrderClass;
  19. use bizGhs\order\classes\OrderClass;
  20. use bizHd\purchase\classes\PurchaseClass;
  21. use bizHd\refund\classes\HdRefundClass;
  22. use common\components\arrayUtil;
  23. use common\components\dateUtil;
  24. use common\components\dict;
  25. use common\components\noticeUtil;
  26. use common\components\util;
  27. use Yii;
  28. use bizGhs\base\classes\BaseClass;
  29. class StatSaleClass extends BaseClass
  30. {
  31. public static $baseFile = '\bizGhs\stat\models\StatSale';
  32. public static function wastage($mainId)
  33. {
  34. $get = Yii::$app->request->get();
  35. $where = [];
  36. $where['mainId'] = $mainId;
  37. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  38. $startTime = $get['startTime'] ?? '';
  39. $endTime = $get['endTime'] ?? '';
  40. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  41. $start = $period['startTime'];
  42. $end = $period['endTime'];
  43. $currentStartDate = date('Y-m-d', strtotime($start));
  44. $currentEndDate = date('Y-m-d', strtotime($end));
  45. $currentStartTime = $currentStartDate . ' 00:00:00';
  46. $currentEndTime = $currentEndDate . ' 23:59:59';
  47. $where['addTime'] = ['between', [$currentStartTime, $currentEndTime]];
  48. $list = StockWastageOrderClass::getAllByCondition($where, null, '*');
  49. $totalPrice = 0;
  50. if (!empty($list)) {
  51. foreach ($list as $order) {
  52. $price = $order['price'] ?? 0;
  53. $totalPrice = bcadd($totalPrice, $price, 2);
  54. }
  55. }
  56. $totalPrice = floatval($totalPrice);
  57. return ['totalPrice' => $totalPrice];
  58. }
  59. //结账跟踪 ssh 20230503
  60. public static function OrderClear($mainId)
  61. {
  62. $get = Yii::$app->request->get();
  63. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  64. $timeStyle = $get['timeStyle'] ?? 0;
  65. if (isset($get['searchTime']) && !empty($get['searchTime'])) {
  66. $searchTime = $get['searchTime'];
  67. $startTime = $get['startTime'] ?? '';
  68. $endTime = $get['endTime'] ?? '';
  69. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  70. $start = $period['startTime'];
  71. $end = $period['endTime'];
  72. $currentStartDate = date('Y-m-d', strtotime($start));
  73. $currentEndDate = date('Y-m-d', strtotime($end));
  74. $currentStartTime = $currentStartDate . ' 00:00:00';
  75. $currentEndTime = $currentEndDate . ' 23:59:59';
  76. if ($timeStyle == 0) {
  77. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  78. } else {
  79. $where['clearTime'] = ['between', [$currentStartTime, $currentEndTime]];
  80. }
  81. } else {
  82. $payTime = 'today';
  83. $startTime = $get['startTime'] ?? '';
  84. $endTime = $get['endTime'] ?? '';
  85. $period = dateUtil::formatTime($payTime, $startTime, $endTime, true);
  86. $start = $period['startTime'];
  87. $end = $period['endTime'];
  88. $currentStartDate = date('Y-m-d', strtotime($start));
  89. $currentEndDate = date('Y-m-d', strtotime($end));
  90. $currentStartTime = $currentStartDate . ' 00:00:00';
  91. $currentEndTime = $currentEndDate . ' 23:59:59';
  92. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  93. }
  94. $customId = $get['customId'] ?? 0;
  95. if (!empty($customId)) {
  96. $where['customId'] = $customId;
  97. }
  98. $unClear = $get['unClear'] ?? 0;
  99. if ($unClear == 1) {
  100. $where['clearId'] = 0;
  101. $where['remainDebtPrice>'] = 0;
  102. }
  103. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  104. return ['list' => $ghsOrderList];
  105. }
  106. //客户下单榜 ssh 20230502
  107. public static function statCustomCg($mainId)
  108. {
  109. $get = Yii::$app->request->get();
  110. $where = [];
  111. $where['mainId'] = $mainId;
  112. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  113. $startTime = $get['startTime'] ?? '';
  114. $endTime = $get['endTime'] ?? '';
  115. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  116. $start = $period['startTime'];
  117. $end = $period['endTime'];
  118. $where['time'] = ['between', [$start, $end]];
  119. $currentStartDate = date('Y-m-d', strtotime($start));
  120. $currentEndDate = date('Y-m-d', strtotime($end));
  121. $currentStartTime = $currentStartDate . ' 00:00:00';
  122. $currentEndTime = $currentEndDate . ' 23:59:59';
  123. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  124. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  125. $customList = CustomClass::getAllByCondition(['ownMainId' => $mainId], null, 'id,name,py', 'id');
  126. $arr = [];
  127. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  128. $totalAmount = 0;
  129. $totalNum = 0;
  130. if (!empty($ghsOrderList)) {
  131. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  132. $actPrice = $ghsOrder['actPrice'] ?? 0;
  133. $orderPrice = $ghsOrder['orderPrice'] ?? 0;
  134. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  135. $customId = $ghsOrder['customId'] ?? 0;
  136. $status = $ghsOrder['status'] ?? 0;
  137. $book = $ghsOrder['book'] ?? 0;
  138. if ($status == 1 || $status == 5) {
  139. continue;
  140. }
  141. if ($book == 1 && $status == 2) {
  142. continue;
  143. }
  144. $customName = $customList[$customId]['name'] ?? '';
  145. $py = $customList[$customId]['py'] ?? '';
  146. $totalNum++;
  147. $totalAmount = bcadd($totalAmount, $actPrice, 2);
  148. if (isset($arr[$customId])) {
  149. $arr[$customId]['num']++;
  150. $arr[$customId]['amount'] = bcadd($arr[$customId]['amount'], $actPrice, 2);
  151. $arr[$customId]['orderPrice'] = bcadd($arr[$customId]['orderPrice'], $orderPrice, 2);
  152. $arr[$customId]['tkPrice'] = bcadd($arr[$customId]['tkPrice'], $tkPrice, 2);
  153. } else {
  154. $arr[$customId] = [
  155. 'customId' => $customId,
  156. 'customName' => $customName,
  157. 'py' => $py,
  158. 'num' => 1,
  159. 'amount' => $actPrice,
  160. 'levelName' => '',
  161. 'orderPrice' => $orderPrice,
  162. 'tkPrice' => $tkPrice,
  163. ];
  164. }
  165. }
  166. $arr = arrayUtil::arraySort($arr, 'amount');
  167. }
  168. $totalAmount = floatval($totalAmount);
  169. return ['list' => $arr, 'totalAmount' => $totalAmount, 'totalNum' => $totalNum];
  170. }
  171. public static function profile($mainId)
  172. {
  173. //收入:批发开单、零售开单(含门店、美团和微信客服)、出库
  174. //支出:采购、调拨入库、批发退款、零售退款、店租、水电、伙食
  175. $get = Yii::$app->request->get();
  176. $where = [];
  177. $where['mainId'] = $mainId;
  178. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  179. $startTime = $get['startTime'] ?? '';
  180. $endTime = $get['endTime'] ?? '';
  181. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  182. $start = $period['startTime'];
  183. $end = $period['endTime'];
  184. $where['time'] = ['between', [$start, $end]];
  185. $currentStartDate = date('Y-m-d', strtotime($start));
  186. $currentEndDate = date('Y-m-d', strtotime($end));
  187. $currentStartTime = $currentStartDate . ' 00:00:00';
  188. $currentEndTime = $currentEndDate . ' 23:59:59';
  189. //出库
  190. $stockOut = StatStockOutClass::getSum($where);
  191. //采购入库
  192. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  193. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  194. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  195. $cg = 0;
  196. if (!empty($cgList)) {
  197. foreach ($cgList as $cgKey => $cgItem) {
  198. $actPrice = $cgItem['actPrice'] ?? 0;
  199. $paidPrice = $cgItem['paidPrice'] ?? 0;
  200. if ($actPrice <= 0) {
  201. // 如果$actPrice=0,表示这个采购单已经全部退款,不需要再统计成本,本来$paidPrice要同步清零,但没有,这个问题由徐州邂逅花坊马总,提出的需求发现
  202. $cg = bcadd($cg, 0, 2);
  203. } else {
  204. $total = bcadd($actPrice, $paidPrice, 2);
  205. //物流费用不算成本。意思是说,长短途运费+提货费+本地运费,这四项和打包费一起,参与到平摊花材运费成本的计算,但那四项不参与总的采购成本登记,因为,有另外地方会独立登记运费成本。
  206. //$paidPrice 字段说明,此字段表示收货方已付款金额,包括 $localCharge本地运费、 $pickCharge提货费、 $shortCharge短途运费(到付时算,如果不是到付,不算在$paidPrice里)、$longCharge长途运费(到付时算,如果不是到付,不算在$paidPrice里)
  207. //请搜索关键词 cg_item_cost ,有多处相似情况要考虑
  208. if (isset($cgItem['isCost']) && $cgItem['isCost'] == 0) {
  209. $shortCharge = $cgItem['shortCharge'] ?? 0;
  210. $longCharge = $cgItem['longCharge'] ?? 0;
  211. $pickCharge = $cgItem['pickCharge'] ?? 0;
  212. $localCharge = $cgItem['localCharge'] ?? 0;
  213. $a1 = bcadd($shortCharge, $longCharge, 2);
  214. $a2 = bcadd($pickCharge, $localCharge, 2);
  215. $a = bcadd($a1, $a2, 2);
  216. //注意下面是减号 bcsub
  217. $total = bcsub($total, $a, 2);
  218. }
  219. $cg = bcadd($cg, $total, 2);
  220. }
  221. }
  222. }
  223. $cg = floatval($cg);
  224. $lsCgWhere = ['mainId' => $mainId, 'status' => 4, 'belongCost' => 1];
  225. $lsCgWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  226. $lsCgList = PurchaseClass::getAllByCondition($lsCgWhere, null, '*');
  227. $lsCgAmount = 0;
  228. if (!empty($lsCgList)) {
  229. foreach ($lsCgList as $lsCgItem) {
  230. $actPrice = $lsCgItem['actPrice'] ?? 0;
  231. $lsCgAmount = bcadd($lsCgAmount, $actPrice, 2);
  232. }
  233. }
  234. $lsCgAmount = floatval($lsCgAmount);
  235. $lzWhere = ['mainId' => $mainId, 'status' => 3];
  236. $lzWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  237. $lzCgList = PlantCgClass::getAllByCondition($lzWhere, null, '*');
  238. $lzCgAmount = 0;
  239. if (!empty($lzCgList)) {
  240. foreach ($lzCgList as $lzInfo) {
  241. $actPrice = $lzInfo['actPrice'] ?? 0;
  242. $lzCgAmount = bcadd($actPrice, $lzCgAmount, 2);
  243. }
  244. }
  245. $lzCgAmount = floatval($lzCgAmount);
  246. //调拨入库
  247. $stockIn = StatStockInClass::getSum($where);
  248. $totalOrderNum = 0;
  249. //批发开单
  250. $pfIncome = 0;
  251. $pfSendCost = 0;
  252. $pfPack = 0;
  253. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  254. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  255. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  256. if (!empty($ghsOrderList)) {
  257. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  258. $actPrice = $ghsOrder['actPrice'] ?? 0;
  259. $sendCost = $ghsOrder['sendCost'] ?? 0;
  260. $refundSendCost = $ghsOrder['refundSendCost'] ?? 0;
  261. $remainSendCost = bcsub($sendCost, $refundSendCost, 2);
  262. $packCost = $ghsOrder['packCost'] ?? 0;
  263. $refundPackCost = $ghsOrder['refundPackCost'] ?? 0;
  264. $remainPackCost = bcsub($packCost, $refundPackCost, 2);
  265. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  266. $pfSendCost = bcadd($pfSendCost, $remainSendCost, 2);
  267. $pfPack = bcadd($pfPack, $remainPackCost, 2);
  268. $totalOrderNum++;
  269. }
  270. }
  271. //零售开单
  272. $payCodeIncome = 0;
  273. $lsAfterSale = 0;
  274. $lsIncome = 0;
  275. $lsSendCost = 0;
  276. $lsServiceFee = 0;
  277. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  278. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  279. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  280. foreach ($hdOrderList as $hdKey => $hdOrder) {
  281. $actPrice = $hdOrder['mainPay'] ?? 0;
  282. $onlinePay = $hdOrder['onlinePay'] ?? 1;
  283. $repeat = $hdOrder['repeat'] ?? 0;
  284. $forward = $hdOrder['forward'] ?? 0;
  285. if ($hdOrder['tkPrice'] && $hdOrder['tkPrice'] > 0) {
  286. $actPrice = bcsub($actPrice, $hdOrder['tkPrice'], 2);
  287. }
  288. if ($forward == 1) {
  289. //零售售后付款
  290. $lsAfterSale = bcadd($lsAfterSale, $actPrice, 2);
  291. } else {
  292. $cash = $hdOrder['cash'] ?? 0;
  293. $serviceFee = $hdOrder['serviceFee'] ?? 0;
  294. $sendCost = $hdOrder['sendCost'] ?? 0;
  295. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  296. $lsIncome = bcadd($lsIncome, $cash, 2);
  297. //如果$actPrice<=0,则退款取消先不计算成本
  298. if ($actPrice > 0) {
  299. $lsSendCost = bcadd($lsSendCost, $sendCost, 2);
  300. $lsServiceFee = bcadd($lsServiceFee, $serviceFee, 2);
  301. }
  302. $totalOrderNum++;
  303. if ($onlinePay == 2 && floatval($cash) <= 0 && $repeat == 1) {
  304. $payCodeIncome = bcadd($payCodeIncome, $actPrice, 2);
  305. $payCodeIncome = floatval($payCodeIncome);
  306. }
  307. }
  308. }
  309. $condition = ['mainId' => $mainId, 'addTime' => ['between', [$currentStartTime, $currentEndTime]],];
  310. $salary = 0;
  311. $salaryList = SalaryClass::getAllByCondition($condition, null, '*', null, true);
  312. if (!empty($salaryList)) {
  313. foreach ($salaryList as $item) {
  314. $salary = bcadd($salary, $item->amount, 2);
  315. }
  316. }
  317. $condition = [
  318. 'mainId' => $mainId,
  319. 'payTime' => ['between', [$currentStartTime, $currentEndTime]],
  320. 'delStatus' => 0,
  321. ];
  322. $expendList = ExpendClass::getAllByCondition($condition, null, '*', null, true);
  323. $expendTypeMap = dict::getDict('expendTypeMap');
  324. $expendAmount = [];
  325. if (!empty($expendList)) {
  326. foreach ($expendList as $item) {
  327. $type = $item->type ?? 0;
  328. $amount = $item->amount ?? 0;
  329. $name = $expendTypeMap[$type] ?? '暂无';
  330. if (!isset($expendAmount[$type]['amount'])) {
  331. $expendAmount[$type]['amount'] = 0;
  332. }
  333. $expendAmount[$type]['name'] = $name;
  334. $expendAmount[$type]['amount'] = floatval(bcadd($expendAmount[$type]['amount'], $amount, 2));
  335. }
  336. }
  337. $income = [
  338. ['name' => '批发', 'id' => 'pf', 'amount' => floatval($pfIncome)],
  339. ['name' => '零售', 'id' => 'ls', 'amount' => floatval($lsIncome)],
  340. ['name' => '调拨出库', 'id' => 'allot', 'amount' => floatval($stockOut)],
  341. ];
  342. $expend = [
  343. ['name' => '批发运费', 'id' => 'pfSendCost', 'amount' => floatval($pfSendCost)],
  344. ['name' => '批发包装费', 'id' => 'pfSendCost', 'amount' => floatval($pfPack)],
  345. ['name' => '零售运费', 'id' => 'lsSendCost', 'amount' => floatval($lsSendCost)],
  346. ['name' => '零售售后付款', 'id' => 'lsSendCost', 'amount' => floatval($lsAfterSale)],
  347. ['name' => '美团手续费', 'id' => 'mtFee', 'amount' => floatval($lsServiceFee)],
  348. ['name' => '花材采购', 'id' => 'cg', 'amount' => floatval($cg)],
  349. ['name' => '绿植资材采购', 'id' => 'cg', 'amount' => floatval($lzCgAmount)],
  350. ['name' => '零售花材采购', 'id' => 'cg', 'amount' => floatval($lsCgAmount)],
  351. ['name' => '调拨入库', 'id' => 'allot', 'amount' => floatval($stockIn)],
  352. ['name' => '员工工资', 'id' => 'salary', 'amount' => floatval($salary)],
  353. ];
  354. if (!empty($expendAmount)) {
  355. foreach ($expendAmount as $it) {
  356. $expend[] = $it;
  357. }
  358. }
  359. $balance = 0;
  360. foreach ($income as $it) {
  361. $amount = $it['amount'] ?? 0;
  362. $balance = bcadd($amount, $balance, 2);
  363. }
  364. foreach ($expend as $it) {
  365. $amount = $it['amount'] ?? 0;
  366. $balance = bcsub($balance, $amount, 2);
  367. }
  368. return ['income' => $income, 'expend' => $expend, 'balance' => floatval($balance), 'totalOrderNum' => $totalOrderNum, 'payCodeIncome' => $payCodeIncome];
  369. }
  370. //按花材统计收入 ssh 20231111
  371. public static function incomeOutItem($mainId)
  372. {
  373. $get = Yii::$app->request->get();
  374. $export = $get['export'] ?? 0;
  375. $where = [];
  376. $where['mainId'] = $mainId;
  377. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  378. $startTime = $get['startTime'] ?? '';
  379. $endTime = $get['endTime'] ?? '';
  380. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  381. $start = $period['startTime'];
  382. $end = $period['endTime'];
  383. $where['time'] = ['between', [$start, $end]];
  384. $currentStartDate = date('Y-m-d', strtotime($start));
  385. $currentEndDate = date('Y-m-d', strtotime($end));
  386. $currentStartTime = $currentStartDate . ' 00:00:00';
  387. $currentEndTime = $currentEndDate . ' 23:59:59';
  388. $s = strtotime($currentStartTime);
  389. $e = strtotime($currentEndTime);
  390. $p = bcsub($e, $s);
  391. $xx = bcmul(86400, 90);
  392. if ($p > $xx) {
  393. util::fail('查询时间不能超过三个月');
  394. }
  395. $productList = ProductClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
  396. if (empty($productList)) {
  397. util::fail('没有找到花材');
  398. }
  399. $arr = [];
  400. //打包费和运费
  401. $packFreight = 0;
  402. $onlyRefundCg = 0;
  403. $onlyRefund = 0;
  404. $totalDiscountAmount = 0;
  405. $totalLabourCost = 0;
  406. //采购入库
  407. $tbCgIncome = [];
  408. $tbCgIncomeStaff = [];
  409. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  410. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  411. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  412. if (!empty($cgList)) {
  413. foreach ($cgList as $cgKey => $cgItem) {
  414. $packingCharge = $cgItem['packingCharge'] ?? 0;
  415. $shortCharge = $cgItem['shortCharge'] ?? 0;
  416. $longCharge = $cgItem['longCharge'] ?? 0;
  417. $pickCharge = $cgItem['pickCharge'] ?? 0;
  418. $localCharge = $cgItem['localCharge'] ?? 0;
  419. $packFreight = bcadd($packFreight, $packingCharge, 2);
  420. $packFreight = bcadd($packFreight, $shortCharge, 2);
  421. $packFreight = bcadd($packFreight, $longCharge, 2);
  422. $packFreight = bcadd($packFreight, $pickCharge, 2);
  423. $packFreight = bcadd($packFreight, $localCharge, 2);
  424. $tkPrice = $cgItem['tkPrice'] ?? 0;
  425. $orderSn = $cgItem['orderSn'] ?? '';
  426. $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  427. if (!empty($cgItemInfoList)) {
  428. foreach ($cgItemInfoList as $cgData) {
  429. $itemNum = $cgData->itemNum ?? 0;
  430. $productId = $cgData->productId ?? 0;
  431. $refundNum = $cgData->refundNum ?? 0;
  432. $remainNum = bcsub($itemNum, $refundNum);
  433. $bigPrice = $cgData->bigPrice ?? 0;
  434. $amount = bcmul($remainNum, $bigPrice, 2);
  435. $itemName = $productList[$productId]['name'] ?? '已删除';
  436. $arr[$productId]['name'] = $itemName;
  437. $arr[$productId]['id'] = $productId;
  438. if (isset($arr[$productId]['expend'])) {
  439. $arr[$productId]['expend'] = bcadd($amount, $arr[$productId]['expend'], 2);
  440. $arr[$productId]['outNum'] = bcadd($arr[$productId]['outNum'], $remainNum, 2);
  441. } else {
  442. $arr[$productId]['expend'] = $amount;
  443. $arr[$productId]['outNum'] = $remainNum;
  444. $arr[$productId]['inNum'] = 0;
  445. $arr[$productId]['income'] = 0;
  446. }
  447. }
  448. if ($tkPrice > 0) {
  449. $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  450. if (!empty($cgRefundOrderList)) {
  451. foreach ($cgRefundOrderList as $cgRefundOrder) {
  452. if ($cgRefundOrder->refundType == 2) {
  453. $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
  454. }
  455. }
  456. }
  457. }
  458. }
  459. }
  460. }
  461. //批发开单
  462. $pfIncome = 0;
  463. $tbSaleIncome = [];
  464. $tbSaleIncomeStaff = [];
  465. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  466. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  467. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  468. if (!empty($ghsOrderList)) {
  469. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  470. $book = $ghsOrder['book'] ?? 0;
  471. $status = $ghsOrder['status'] ?? 0;
  472. if ($book == 1 && $status == 2) {
  473. //预订单待发货的不算收入
  474. continue;
  475. }
  476. if ($status == 0 || $status == 5) {
  477. continue;
  478. }
  479. $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
  480. $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
  481. $orderSn = $ghsOrder['orderSn'] ?? '';
  482. $actPrice = $ghsOrder['actPrice'] ?? 0;
  483. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  484. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  485. $labourCost = $ghsOrder['labourCost'] ?? 0;
  486. $discountAmount = $ghsOrder['discountAmount'] ?? 0;
  487. $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
  488. $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
  489. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  490. if (!empty($orderItemList)) {
  491. foreach ($orderItemList as $orderItem) {
  492. $refundNum = $orderItem->refundNum ?? 0;
  493. $name = $orderItem->name ?? '';
  494. $productId = $orderItem->productId ?? 0;
  495. $num = $orderItem->xhNum ?? 0;
  496. $remainNum = bcsub($num, $refundNum);
  497. $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
  498. $amount = bcmul($remainNum, $xhUnitPrice, 2);
  499. $itemName = $productList[$productId]['name'] ?? '已删除';
  500. $arr[$productId]['name'] = $itemName;
  501. $arr[$productId]['id'] = $productId;
  502. if (isset($arr[$productId]['income'])) {
  503. $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
  504. $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
  505. } else {
  506. $arr[$productId]['expend'] = 0;
  507. $arr[$productId]['outNum'] = 0;
  508. $arr[$productId]['inNum'] = $remainNum;
  509. $arr[$productId]['income'] = $amount;
  510. }
  511. //单独统计
  512. $belongCost = $orderItem->belongCost ?? 0;
  513. if ($belongCost == 1) {
  514. if (isset($tbSaleIncome[$productId])) {
  515. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  516. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  517. } else {
  518. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  519. }
  520. if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
  521. $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
  522. $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
  523. } else {
  524. $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
  525. }
  526. }
  527. }
  528. if ($tkPrice > 0) {
  529. $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  530. if (!empty($refundList)) {
  531. foreach ($refundList as $refund) {
  532. if ($refund->refundType == 1) {
  533. //退货退款前面已经考虑,这里不再处理 OK
  534. } else {
  535. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  536. }
  537. }
  538. }
  539. }
  540. }
  541. }
  542. }
  543. //零售开单
  544. $lsIncome = 0;
  545. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  546. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  547. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  548. foreach ($hdOrderList as $hdKey => $hdOrder) {
  549. $actPrice = $hdOrder['mainPay'] ?? 0;
  550. $cash = $hdOrder['cash'] ?? 0;
  551. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  552. $lsIncome = bcadd($lsIncome, $cash, 2);
  553. $orderSn = $hdOrder['orderSn'] ?? '';
  554. $tkPrice = $hdOrder['tkPrice'] ?? 0;
  555. $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  556. if (!empty($orderItemList)) {
  557. foreach ($orderItemList as $orderItem) {
  558. $unitPrice = $orderItem->unitPrice ?? 0;
  559. $refundNum = $orderItem->refundNum ?? 0;
  560. $num = $orderItem->num ?? 0;
  561. $productId = $orderItem->itemId ?? 0;
  562. $remainNum = bcsub($num, $refundNum);
  563. $amount = bcmul($remainNum, $unitPrice, 2);
  564. $itemName = $productList[$productId]['name'] ?? '已删除';
  565. $arr[$productId]['name'] = $itemName;
  566. $arr[$productId]['id'] = $productId;
  567. if (isset($arr[$productId]['income'])) {
  568. $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
  569. $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
  570. } else {
  571. $arr[$productId]['expend'] = 0;
  572. $arr[$productId]['outNum'] = 0;
  573. $arr[$productId]['inNum'] = $remainNum;
  574. $arr[$productId]['income'] = $amount;
  575. }
  576. }
  577. if ($tkPrice > 0) {
  578. $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  579. if (!empty($refundList)) {
  580. foreach ($refundList as $refund) {
  581. if ($refund->refundType == 2) {
  582. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  583. }
  584. }
  585. }
  586. }
  587. }
  588. }
  589. $totalIncome = 0;
  590. $totalExpend = 0;
  591. if (!empty($arr)) {
  592. foreach ($arr as $key => $val) {
  593. $income = $val['income'] ?? 0;
  594. $income = floatval($income);
  595. $totalIncome = bcadd($totalIncome, $income, 2);
  596. $expend = $val['expend'] ?? 0;
  597. $expend = floatval($expend);
  598. $totalExpend = bcadd($totalExpend, $expend, 2);
  599. $profit = bcsub($income, $expend, 2);
  600. $arr[$key]['profit'] = floatval($profit);
  601. $arr[$key]['income'] = floatval($income);
  602. $arr[$key]['expend'] = floatval($expend);
  603. }
  604. }
  605. //总成本算上物流打包等费用
  606. $totalExpend = bcadd($totalExpend, $packFreight, 2);
  607. //总收入算上采购仅退款
  608. $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
  609. //总收入加上人工费
  610. $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
  611. //总收入减掉优惠金额
  612. $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
  613. //总成本算上销售仅退款
  614. $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
  615. $totalProfit = bcsub($totalIncome, $totalExpend, 2);
  616. $totalIncome = floatval($totalIncome);
  617. $totalExpend = floatval($totalExpend);
  618. $totalProfit = floatval($totalProfit);
  619. $arr = array_values($arr);
  620. $arr = arrayUtil::arraySort($arr, 'profit');
  621. if ($export == 1) {
  622. $statData = [
  623. ['id' => 0, 'name' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'inNum' => 0, 'outNum' => 0],
  624. ['id' => 0, 'name' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'inNum' => 0, 'outNum' => 0],
  625. ['id' => 0, 'name' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'inNum' => 0, 'outNum' => 0],
  626. ['id' => 0, 'name' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'inNum' => 0, 'outNum' => 0],
  627. ['id' => 0, 'name' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'inNum' => 0, 'outNum' => 0],
  628. ['id' => 0, 'name' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'inNum' => 0, 'outNum' => 0],
  629. ];
  630. $list = array_merge($arr, $statData);
  631. self::exportIncomeOutItem($list, $mainId);
  632. }
  633. util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
  634. 'totalIncome' => $totalIncome,
  635. 'totalExpend' => $totalExpend,
  636. 'totalProfit' => $totalProfit,
  637. 'tbSaleIncome' => $tbSaleIncome,
  638. 'tbCgIncome' => $tbCgIncome,
  639. 'tbCgIncomeStaff' => $tbCgIncomeStaff,
  640. 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
  641. 'totalLabourCost' => $totalLabourCost,
  642. 'totalDiscountAmount' => $totalDiscountAmount,
  643. ]);
  644. }
  645. //按分类统计收入 ssh 20231111
  646. public static function classProfile($mainId)
  647. {
  648. $get = Yii::$app->request->get();
  649. $export = $get['export'] ?? 0;
  650. $where = [];
  651. $where['mainId'] = $mainId;
  652. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  653. $startTime = $get['startTime'] ?? '';
  654. $endTime = $get['endTime'] ?? '';
  655. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  656. $start = $period['startTime'];
  657. $end = $period['endTime'];
  658. $where['time'] = ['between', [$start, $end]];
  659. $currentStartDate = date('Y-m-d', strtotime($start));
  660. $currentEndDate = date('Y-m-d', strtotime($end));
  661. $currentStartTime = $currentStartDate . ' 00:00:00';
  662. $currentEndTime = $currentEndDate . ' 23:59:59';
  663. $s = strtotime($currentStartTime);
  664. $e = strtotime($currentEndTime);
  665. $p = bcsub($e, $s);
  666. $xx = bcmul(86400, 90);
  667. if ($p > $xx) {
  668. util::fail('查询时间不能超过三个月');
  669. }
  670. $classList = ItemClassClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
  671. if (empty($classList)) {
  672. util::fail('没有找到花材');
  673. }
  674. $arr = [];
  675. //打包费和运费
  676. $packFreight = 0;
  677. $onlyRefundCg = 0;
  678. $onlyRefund = 0;
  679. $totalDiscountAmount = 0;
  680. $totalLabourCost = 0;
  681. //采购入库
  682. $tbCgIncome = [];
  683. $tbCgIncomeStaff = [];
  684. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  685. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  686. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  687. if (!empty($cgList)) {
  688. foreach ($cgList as $cgKey => $cgItem) {
  689. $packingCharge = $cgItem['packingCharge'] ?? 0;
  690. $shortCharge = $cgItem['shortCharge'] ?? 0;
  691. $longCharge = $cgItem['longCharge'] ?? 0;
  692. $pickCharge = $cgItem['pickCharge'] ?? 0;
  693. $localCharge = $cgItem['localCharge'] ?? 0;
  694. $cgStaffId = $cgItem['cgStaffId'] ?? 0;
  695. $cgStaffName = $cgItem['cgStaffName'] ?? '';
  696. $actPrice = $cgItem['actPrice'] ?? 0;
  697. if ($actPrice <= 0) {
  698. continue;
  699. }
  700. $packFreight = bcadd($packFreight, $packingCharge, 2);
  701. $packFreight = bcadd($packFreight, $shortCharge, 2);
  702. $packFreight = bcadd($packFreight, $longCharge, 2);
  703. $packFreight = bcadd($packFreight, $pickCharge, 2);
  704. $packFreight = bcadd($packFreight, $localCharge, 2);
  705. $tkPrice = $cgItem['tkPrice'] ?? 0;
  706. $orderSn = $cgItem['orderSn'] ?? '';
  707. $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  708. if (!empty($cgItemInfoList)) {
  709. foreach ($cgItemInfoList as $cgData) {
  710. $itemNum = $cgData->itemNum ?? 0;
  711. $productId = $cgData->productId ?? 0;
  712. $name = $cgData->name ?? '';
  713. $refundNum = $cgData->refundNum ?? 0;
  714. $remainNum = bcsub($itemNum, $refundNum);
  715. $bigPrice = $cgData->bigPrice ?? 0;
  716. $amount = bcmul($remainNum, $bigPrice, 2);
  717. $classId = $cgData->classId;
  718. $className = $classList[$classId]['name'] ?? '已删除';
  719. if ($className == '运费') {
  720. //noticeUtil::push("{$cgData->orderSn}运费项", '15280215347');
  721. }
  722. $arr[$classId]['className'] = $className;
  723. $arr[$classId]['classId'] = $classId;
  724. if (isset($arr[$classId]['expend'])) {
  725. $arr[$classId]['expend'] = bcadd($amount, $arr[$classId]['expend'], 2);
  726. $arr[$classId]['cgNum'] = bcadd($arr[$classId]['cgNum'], $remainNum);
  727. } else {
  728. $arr[$classId]['expend'] = $amount;
  729. $arr[$classId]['income'] = 0;
  730. $arr[$classId]['cgNum'] = $remainNum;
  731. $arr[$classId]['saleNum'] = 0;
  732. }
  733. //单独统计
  734. $belongCost = $cgData->belongCost ?? 0;
  735. if ($belongCost == 1) {
  736. if (isset($tbCgIncome[$productId])) {
  737. $tbCgIncome[$productId]['num'] = bcadd($tbCgIncome[$productId]['num'], $remainNum);
  738. $tbCgIncome[$productId]['price'] = bcadd($tbCgIncome[$productId]['price'], $amount, 2);
  739. } else {
  740. $tbCgIncome[$productId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount];
  741. }
  742. if (isset($tbCgIncomeStaff[$productId]) && isset($tbCgIncomeStaff[$productId][$cgStaffId])) {
  743. $tbCgIncomeStaff[$productId][$cgStaffId]['num'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['num'], $remainNum);
  744. $tbCgIncomeStaff[$productId][$cgStaffId]['price'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['price'], $amount, 2);
  745. } else {
  746. $tbCgIncomeStaff[$productId][$cgStaffId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount, 'staffName' => $cgStaffName,];
  747. }
  748. }
  749. }
  750. if ($tkPrice > 0) {
  751. $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  752. if (!empty($cgRefundOrderList)) {
  753. foreach ($cgRefundOrderList as $cgRefundOrder) {
  754. if ($cgRefundOrder->refundType == 2) {
  755. $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
  756. }
  757. }
  758. }
  759. }
  760. }
  761. }
  762. }
  763. //批发开单
  764. $pfIncome = 0;
  765. $tbSaleIncome = [];
  766. $tbSaleIncomeStaff = [];
  767. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  768. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  769. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  770. if (!empty($ghsOrderList)) {
  771. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  772. $book = $ghsOrder['book'] ?? 0;
  773. $status = $ghsOrder['status'] ?? 0;
  774. if ($book == 1 && $status == 2) {
  775. //预订单待发货的不算收入
  776. continue;
  777. }
  778. if ($status == 0 || $status == 5) {
  779. continue;
  780. }
  781. $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
  782. $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
  783. $orderSn = $ghsOrder['orderSn'] ?? '';
  784. $actPrice = $ghsOrder['actPrice'] ?? 0;
  785. if ($actPrice <= 0) {
  786. continue;
  787. }
  788. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  789. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  790. $labourCost = $ghsOrder['labourCost'] ?? 0;
  791. $discountAmount = $ghsOrder['discountAmount'] ?? 0;
  792. $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
  793. $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
  794. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  795. if (!empty($orderItemList)) {
  796. foreach ($orderItemList as $orderItem) {
  797. $refundNum = $orderItem->refundNum ?? 0;
  798. $name = $orderItem->name ?? '';
  799. $productId = $orderItem->productId ?? 0;
  800. $num = $orderItem->xhNum ?? 0;
  801. $remainNum = bcsub($num, $refundNum);
  802. $classId = $orderItem->classId ?? 0;
  803. $className = $classList[$classId]['name'] ?? '已删除';
  804. $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
  805. $amount = bcmul($remainNum, $xhUnitPrice, 2);
  806. $arr[$classId]['className'] = $className;
  807. $arr[$classId]['classId'] = $classId;
  808. if (isset($arr[$classId]['income'])) {
  809. $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
  810. $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
  811. } else {
  812. $arr[$classId]['expend'] = 0;
  813. $arr[$classId]['income'] = $amount;
  814. $arr[$classId]['saleNum'] = $remainNum;
  815. $arr[$classId]['cgNum'] = 0;
  816. }
  817. //单独统计
  818. $belongCost = $orderItem->belongCost ?? 0;
  819. if ($belongCost == 1) {
  820. if (isset($tbSaleIncome[$productId])) {
  821. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  822. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  823. } else {
  824. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  825. }
  826. if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
  827. $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
  828. $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
  829. } else {
  830. $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
  831. }
  832. }
  833. }
  834. if ($tkPrice > 0) {
  835. $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  836. if (!empty($refundList)) {
  837. foreach ($refundList as $refund) {
  838. if ($refund->refundType == 1) {
  839. //退货退款前面已经考虑,这里不再处理 OK
  840. } else {
  841. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  842. }
  843. }
  844. }
  845. }
  846. }
  847. }
  848. }
  849. //零售开单
  850. $lsIncome = 0;
  851. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  852. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  853. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  854. foreach ($hdOrderList as $hdKey => $hdOrder) {
  855. $actPrice = $hdOrder['mainPay'] ?? 0;
  856. $cash = $hdOrder['cash'] ?? 0;
  857. if ($actPrice <= 0 && $cash <= 0) {
  858. continue;
  859. }
  860. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  861. $lsIncome = bcadd($lsIncome, $cash, 2);
  862. $orderSn = $hdOrder['orderSn'] ?? '';
  863. $tkPrice = $hdOrder['tkPrice'] ?? 0;
  864. $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  865. if (!empty($orderItemList)) {
  866. foreach ($orderItemList as $orderItem) {
  867. $classId = $orderItem->classId ?? 0;
  868. $unitPrice = $orderItem->unitPrice ?? 0;
  869. $refundNum = $orderItem->refundNum ?? 0;
  870. $num = $orderItem->num ?? 0;
  871. $productId = $orderItem->itemId ?? 0;
  872. $name = $orderItem->name ?? '';
  873. $remainNum = bcsub($num, $refundNum);
  874. $amount = bcmul($remainNum, $unitPrice, 2);
  875. $className = $classList[$classId]['name'] ?? '已删除';
  876. $arr[$classId]['className'] = $className;
  877. $arr[$classId]['classId'] = $classId;
  878. if (isset($arr[$classId]['income'])) {
  879. $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
  880. $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
  881. } else {
  882. $arr[$classId]['expend'] = 0;
  883. $arr[$classId]['income'] = $amount;
  884. $arr[$classId]['saleNum'] = $remainNum;
  885. $arr[$classId]['cgNum'] = 0;
  886. }
  887. $belongCost = $orderItem->belongCost ?? 0;
  888. if ($belongCost == 1) {
  889. if (isset($tbSaleIncome[$productId])) {
  890. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  891. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  892. } else {
  893. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  894. }
  895. }
  896. }
  897. if ($tkPrice > 0) {
  898. $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  899. if (!empty($refundList)) {
  900. foreach ($refundList as $refund) {
  901. if ($refund->refundType == 2) {
  902. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  903. }
  904. }
  905. }
  906. }
  907. }
  908. }
  909. $totalIncome = 0;
  910. $totalExpend = 0;
  911. if (!empty($arr)) {
  912. foreach ($arr as $key => $val) {
  913. $income = $val['income'] ?? 0;
  914. $income = floatval($income);
  915. $totalIncome = bcadd($totalIncome, $income, 2);
  916. $expend = $val['expend'] ?? 0;
  917. $expend = floatval($expend);
  918. $totalExpend = bcadd($totalExpend, $expend, 2);
  919. $profit = bcsub($income, $expend, 2);
  920. $arr[$key]['profit'] = floatval($profit);
  921. $arr[$key]['income'] = floatval($income);
  922. $arr[$key]['expend'] = floatval($expend);
  923. }
  924. }
  925. //总成本算上物流打包等费用
  926. $totalExpend = bcadd($totalExpend, $packFreight, 2);
  927. //总收入算上采购仅退款
  928. $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
  929. //总收入加上人工费
  930. $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
  931. //总收入减掉优惠金额
  932. $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
  933. //总成本算上销售仅退款
  934. $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
  935. $totalProfit = bcsub($totalIncome, $totalExpend, 2);
  936. $totalIncome = floatval($totalIncome);
  937. $totalExpend = floatval($totalExpend);
  938. $totalProfit = floatval($totalProfit);
  939. $arr = array_values($arr);
  940. $arr = arrayUtil::arraySort($arr, 'profit');
  941. if ($export == 1) {
  942. $statData = [
  943. ['classId' => 0, 'className' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'saleNum' => 0, 'cgNum' => 0],
  944. ['classId' => 0, 'className' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'saleNum' => 0, 'cgNum' => 0],
  945. ['classId' => 0, 'className' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'saleNum' => 0, 'cgNum' => 0],
  946. ['classId' => 0, 'className' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'saleNum' => 0, 'cgNum' => 0],
  947. ['classId' => 0, 'className' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'saleNum' => 0, 'cgNum' => 0],
  948. ['classId' => 0, 'className' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'saleNum' => 0, 'cgNum' => 0],
  949. ];
  950. $list = array_merge($arr, $statData);
  951. self::exportIncomeClass($list, $mainId);
  952. }
  953. util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
  954. 'totalIncome' => $totalIncome,
  955. 'totalExpend' => $totalExpend,
  956. 'totalProfit' => $totalProfit,
  957. 'tbSaleIncome' => $tbSaleIncome,
  958. 'tbCgIncome' => $tbCgIncome,
  959. 'tbCgIncomeStaff' => $tbCgIncomeStaff,
  960. 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
  961. 'totalLabourCost' => $totalLabourCost,
  962. 'totalDiscountAmount' => $totalDiscountAmount,
  963. ]);
  964. }
  965. public static function exportIncomeOutItem($list, $mainId)
  966. {
  967. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  968. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  969. $objPHPExcel = new \PHPExcel();
  970. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  971. ->setLastModifiedBy("Maarten Balliauw")
  972. ->setTitle("Office 2007 XLSX Document")
  973. ->setSubject("Office 2007 XLSX Document")
  974. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  975. ->setKeywords("office 2007 openxml php")
  976. ->setCategory("file");
  977. $ghsTitle = '供货商';
  978. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  979. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  980. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  981. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  982. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  983. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  984. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  985. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  986. $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售收入');
  987. $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售数量');
  988. $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购成本');
  989. $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购数量');
  990. $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
  991. //设置宽度
  992. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  993. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  994. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  995. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  996. //加粗
  997. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  998. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  999. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1000. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1001. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1002. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1003. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1004. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1005. $baseRow = 2;
  1006. foreach ($list as $key => $custom) {
  1007. $i = $baseRow + $key;
  1008. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
  1009. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['income']);
  1010. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['inNum']);
  1011. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['expend']);
  1012. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['outNum']);
  1013. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
  1014. //居左
  1015. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1016. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1017. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1018. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1019. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1020. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1021. }
  1022. $fileName = '收支花材-' . date("m-d");
  1023. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1024. $objPHPExcel->setActiveSheetIndex(0);
  1025. $dir = './priceTable/' . $mainId;
  1026. if (file_exists($dir) == false) {
  1027. mkdir($dir, 0777, true);
  1028. }
  1029. $date = $fileName;
  1030. $file = $date . '.xls';
  1031. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1032. if (file_exists($dir . '/' . $file)) {
  1033. unlink($dir . '/' . $file);
  1034. }
  1035. $objWriter->save($dir . '/' . $file);
  1036. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1037. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1038. }
  1039. public static function exportIncomeClass($list, $mainId)
  1040. {
  1041. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1042. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1043. $objPHPExcel = new \PHPExcel();
  1044. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1045. ->setLastModifiedBy("Maarten Balliauw")
  1046. ->setTitle("Office 2007 XLSX Document")
  1047. ->setSubject("Office 2007 XLSX Document")
  1048. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1049. ->setKeywords("office 2007 openxml php")
  1050. ->setCategory("file");
  1051. $ghsTitle = '供货商';
  1052. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1053. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1054. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1055. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1056. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1057. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1058. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1059. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  1060. $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售数量');
  1061. $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售收入');
  1062. $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购数量');
  1063. $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购成本');
  1064. $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
  1065. //设置宽度
  1066. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
  1067. //加粗
  1068. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1069. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1070. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1071. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1072. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1073. $objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setSize(9)->setBold(true);
  1074. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1075. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1076. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1077. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1078. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1079. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1080. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1081. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1082. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1083. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1084. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1085. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1086. $baseRow = 2;
  1087. foreach ($list as $key => $custom) {
  1088. $i = $baseRow + $key;
  1089. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['className']);
  1090. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['saleNum']);
  1091. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['income']);
  1092. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['cgNum']);
  1093. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['expend']);
  1094. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
  1095. //居左
  1096. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1097. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1098. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1099. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1100. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1101. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1102. }
  1103. $fileName = '收支分类-' . time();
  1104. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1105. $objPHPExcel->setActiveSheetIndex(0);
  1106. $dir = './priceTable/' . $mainId;
  1107. if (file_exists($dir) == false) {
  1108. mkdir($dir, 0777, true);
  1109. }
  1110. $date = $fileName;
  1111. $file = $date . '.xls';
  1112. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1113. if (file_exists($dir . '/' . $file)) {
  1114. unlink($dir . '/' . $file);
  1115. }
  1116. $objWriter->save($dir . '/' . $file);
  1117. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1118. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1119. }
  1120. public static function exportItemSale($list, $mainId)
  1121. {
  1122. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1123. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1124. $objPHPExcel = new \PHPExcel();
  1125. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1126. ->setLastModifiedBy("Maarten Balliauw")
  1127. ->setTitle("Office 2007 XLSX Document")
  1128. ->setSubject("Office 2007 XLSX Document")
  1129. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1130. ->setKeywords("office 2007 openxml php")
  1131. ->setCategory("file");
  1132. $ghsTitle = '供货商';
  1133. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1134. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1135. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1136. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1137. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1138. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1139. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1140. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  1141. $objPHPExcel->getActiveSheet()->setCellValue('B1', '总数量');
  1142. $objPHPExcel->getActiveSheet()->setCellValue('C1', '总金额');
  1143. $objPHPExcel->getActiveSheet()->setCellValue('D1', '毛利');
  1144. $objPHPExcel->getActiveSheet()->setCellValue('E1', '毛利率');
  1145. //设置宽度
  1146. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  1147. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  1148. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  1149. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
  1150. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
  1151. //加粗
  1152. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1153. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1154. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1155. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1156. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1157. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1158. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1159. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1160. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1161. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1162. $baseRow = 2;
  1163. foreach ($list as $key => $custom) {
  1164. $i = $baseRow + $key;
  1165. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
  1166. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['num']);
  1167. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['amount']);
  1168. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['gross']);
  1169. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['mll'] . '%');
  1170. //居左
  1171. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1172. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1173. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1174. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1175. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1176. }
  1177. $fileName = '花材销量-' . date("m-d");
  1178. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1179. $objPHPExcel->setActiveSheetIndex(0);
  1180. $dir = './priceTable/' . $mainId;
  1181. if (file_exists($dir) == false) {
  1182. mkdir($dir, 0777, true);
  1183. }
  1184. $date = $fileName;
  1185. $file = $date . '.xls';
  1186. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1187. if (file_exists($dir . '/' . $file)) {
  1188. unlink($dir . '/' . $file);
  1189. }
  1190. $objWriter->save($dir . '/' . $file);
  1191. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1192. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1193. }
  1194. public static function send($mainId)
  1195. {
  1196. $get = Yii::$app->request->get();
  1197. $where = [];
  1198. $where['mainId'] = $mainId;
  1199. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  1200. $startTime = $get['startTime'] ?? '';
  1201. $endTime = $get['endTime'] ?? '';
  1202. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  1203. $start = $period['startTime'];
  1204. $end = $period['endTime'];
  1205. $where['time'] = ['between', [$start, $end]];
  1206. $currentStartDate = date('Y-m-d', strtotime($start));
  1207. $currentEndDate = date('Y-m-d', strtotime($end));
  1208. $currentStartTime = $currentStartDate . ' 00:00:00';
  1209. $currentEndTime = $currentEndDate . ' 23:59:59';
  1210. $s = strtotime($currentStartTime);
  1211. $e = strtotime($currentEndTime);
  1212. $p = bcsub($e, $s);
  1213. $xx = bcmul(86400, 90);
  1214. if ($p > $xx) {
  1215. util::fail('查询时间不能超过三个月');
  1216. }
  1217. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  1218. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  1219. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  1220. $arr = [];
  1221. if (!empty($ghsOrderList)) {
  1222. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  1223. $book = $ghsOrder['book'] ?? 0;
  1224. $status = $ghsOrder['status'] ?? 0;
  1225. if ($book == 1 && $status == 2) {
  1226. //预订单待发货的不算收入
  1227. continue;
  1228. }
  1229. if ($status == 0 || $status == 5) {
  1230. continue;
  1231. }
  1232. $orderSn = $ghsOrder['orderSn'] ?? '';
  1233. $actPrice = $ghsOrder['actPrice'] ?? 0;
  1234. if ($actPrice <= 0) {
  1235. continue;
  1236. }
  1237. $sendStaffId = $ghsOrder['sendStaffId'] ?? 0;
  1238. $sendStaffName = isset($ghsOrder['sendStaffName']) && !empty($ghsOrder['sendStaffName']) ? $ghsOrder['sendStaffName'] : '未记名';
  1239. if (isset($arr[$sendStaffId])) {
  1240. $arr[$sendStaffId]['orderNum'] = bcadd($arr[$sendStaffId]['orderNum'], 1);
  1241. $arr[$sendStaffId]['amount'] = bcadd($arr[$sendStaffId]['amount'], $actPrice, 2);
  1242. } else {
  1243. $arr[$sendStaffId] = ['staffName' => $sendStaffName, 'orderNum' => 1, 'itemNum' => 0, 'amount' => $actPrice, 'staffId' => $sendStaffId];
  1244. }
  1245. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  1246. if (!empty($orderItemList)) {
  1247. foreach ($orderItemList as $orderItem) {
  1248. $refundNum = $orderItem->refundNum ?? 0;
  1249. $num = $orderItem->xhNum ?? 0;
  1250. $remainNum = bcsub($num, $refundNum);
  1251. $arr[$sendStaffId]['itemNum'] = bcadd($arr[$sendStaffId]['itemNum'], $remainNum);
  1252. }
  1253. }
  1254. }
  1255. }
  1256. if (!empty($arr)) {
  1257. $arr = array_values($arr);
  1258. }
  1259. return $arr;
  1260. }
  1261. }